CCallback.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "CPathfinder.h"
  4. #include "hch/CHeroHandler.h"
  5. #include "hch/CTownHandler.h"
  6. #include "CGameInfo.h"
  7. #include "hch/CAmbarCendamo.h"
  8. #include "mapHandler.h"
  9. #include "CGameState.h"
  10. #include "CPlayerInterface.h"
  11. #include "hch/CGeneralTextHandler.h"
  12. #include "CAdvmapInterface.h"
  13. #include "CPlayerInterface.h"
  14. #include "hch/CBuildingHandler.h"
  15. #include "hch/CObjectHandler.h"
  16. #include "lib/Connection.h"
  17. #include "client/Client.h"
  18. #include <boost/thread.hpp>
  19. #include <boost/foreach.hpp>
  20. #include "lib/NetPacks.h"
  21. //LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
  22. extern CSharedCond<std::set<IPack*> > mess;
  23. HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
  24. :src(Src),dst(Dst),ho(Ho)
  25. {
  26. owner = ho->getOwner();
  27. };
  28. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  29. {
  30. CGHeroInstance * hero = NULL;
  31. if (idtype==0)
  32. {
  33. if (player==-1)
  34. hero=gs->players[player+1].heroes[ID];
  35. else
  36. hero=gs->players[player].heroes[ID];
  37. }
  38. else if (idtype==1 && player>=0) //looking for it in local area
  39. {
  40. for (int i=0; i<gs->players[player].heroes.size();i++)
  41. {
  42. if (gs->players[player].heroes[i]->type->ID == ID)
  43. hero = gs->players[player].heroes[i];
  44. }
  45. }
  46. else //idtype==1; player<0
  47. {
  48. for(std::map<ui8, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
  49. {
  50. for (int i=0; i<(*j).second.heroes.size();i++)
  51. {
  52. if ((*j).second.heroes[i]->type->ID == ID)
  53. {
  54. hero = (*j).second.heroes[i];
  55. }
  56. }
  57. }
  58. }
  59. if (!hero)
  60. return false; //can't find hero
  61. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  62. return false; //invalid path
  63. //check path format
  64. if (pathType==0)
  65. CPathfinder::convertPath(path,pathType);
  66. if (pathType>1)
  67. throw std::exception("Unknown path format");
  68. CPath * ourPath = path;
  69. if(!ourPath)
  70. return false;
  71. for(int i=ourPath->nodes.size()-1; i>0; i--)
  72. {
  73. int3 stpos(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z),
  74. endpos(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  75. HeroMoveDetails curd(stpos,endpos,hero);
  76. *cl->serv << ui16(501) << hero->id << stpos << endpos;
  77. {//wait till there is server answer
  78. boost::unique_lock<boost::mutex> lock(*mess.mx);
  79. while(std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>) == mess.res->end())
  80. mess.cv->wait(lock);
  81. std::set<IPack*>::iterator itr = std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>);
  82. TryMoveHero tmh = *static_cast<TryMoveHero*>(*itr);
  83. mess.res->erase(itr);
  84. if(!tmh.result)
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. void CCallback::selectionMade(int selection, int asker)
  91. {
  92. //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
  93. //IChosen * ask = (IChosen *)asker;
  94. //ask->chosen(selection);
  95. }
  96. void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount)
  97. {
  98. if(amount<=0) return;
  99. if(obj->ID==98) //recruiting from town
  100. {
  101. int ser=-1; //used dwelling level
  102. CGTownInstance *t = const_cast<CGTownInstance*>(static_cast<const CGTownInstance*>(obj));
  103. //verify
  104. bool found = false;
  105. typedef std::pair<const int,int> Parka;
  106. for(std::map<si32,ui32>::iterator av=t->strInfo.creatures.begin();av!=t->strInfo.creatures.end();av++)
  107. {
  108. if( ( found = (ID == t->town->basicCreatures[av->first]) ) //creature is available among basic cretures
  109. || (found = (ID == t->town->upgradedCreatures[av->first])) )//creature is available among upgraded cretures
  110. {
  111. amount = min(amount,av->second); //reduce recruited amount up to available amount
  112. ser = av->first;
  113. break;
  114. }
  115. }
  116. if(!found) //no such creature
  117. return;
  118. if(amount > CGI->creh->creatures[ID].maxAmount(gs->players[player].resources))
  119. return; //not enough resources
  120. //for(int i=0;i<RESOURCE_QUANTITY;i++)
  121. // if (gs->players[player].resources[i] < (CGI->creh->creatures[ID].cost[i] * amount))
  122. // return; //not enough resources
  123. if(amount<=0) return;
  124. //recruit
  125. int slot = -1; //slot ID
  126. std::pair<si32,std::pair<ui32,si32> > parb;
  127. for(int i=0;i<7;i++) //TODO: if there is already stack of same creatures it should be used always
  128. {
  129. if(((!t->army.slots[i].first) && (!t->army.slots[i].second)) || (t->army.slots[i].first == ID)) //slot is free or there is saem creature
  130. {
  131. slot = i;
  132. break;
  133. }
  134. }
  135. if(slot<0) //no free slot
  136. return;
  137. for(int i=0;i<RESOURCE_QUANTITY;i++)
  138. gs->players[player].resources[i] -= (CGI->creh->creatures[ID].cost[i] * amount);
  139. t->strInfo.creatures[ser] -= amount;
  140. if(t->army.slots[slot].first) //add new creatures to the existing stack
  141. {
  142. t->army.slots[slot].second += amount;
  143. }
  144. else //create new stack in the garrison
  145. {
  146. t->army.slots[slot].first = ID;
  147. t->army.slots[slot].second = amount;
  148. }
  149. cl->playerint[player]->garrisonChanged(obj);
  150. }
  151. //TODO: recruit from dwellings on the adventure map
  152. }
  153. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  154. {
  155. if(((player>=0) && obj->tempOwner != player) || obj->army.slots.size()<2)
  156. return false;
  157. *cl->serv << ui16(503) << obj->id << ui8(stackPos);
  158. return true;
  159. }
  160. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  161. {
  162. //TODO: write
  163. return false;
  164. }
  165. void CCallback::endTurn()
  166. {
  167. std::cout << "Player "<<(unsigned)player<<" end his turn."<<std::endl;
  168. cl->serv->wmx->lock();
  169. *cl->serv << ui16(100); //report that we ended turn
  170. cl->serv->wmx->unlock();
  171. }
  172. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos)
  173. {
  174. UpgradeInfo ret;
  175. CCreature *base = &CGI->creh->creatures[((CArmedInstance *)obj)->army.slots[stackPos].first];
  176. if((obj->ID == 98) || ((obj->ID == 34) && static_cast<const CGHeroInstance*>(obj)->visitedTown))
  177. {
  178. CGTownInstance * t;
  179. if(obj->ID == 98)
  180. t = static_cast<CGTownInstance *>(const_cast<CArmedInstance *>(obj));
  181. else
  182. t = static_cast<const CGHeroInstance*>(obj)->visitedTown;
  183. for(std::set<si32>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
  184. {
  185. if( (*i) >= 37 && (*i) < 44 ) //upgraded creature dwelling
  186. {
  187. int nid = t->town->upgradedCreatures[(*i)-37]; //upgrade offered by that building
  188. if(base->upgrades.find(nid) != base->upgrades.end()) //possible upgrade
  189. {
  190. ret.newID.push_back(nid);
  191. ret.cost.push_back(std::set<std::pair<int,int> >());
  192. for(int j=0;j<RESOURCE_QUANTITY;j++)
  193. {
  194. int dif = CGI->creh->creatures[nid].cost[j] - base->cost[j];
  195. if(dif)
  196. ret.cost[ret.cost.size()-1].insert(std::make_pair(j,dif));
  197. }
  198. }
  199. }
  200. }//end for
  201. }
  202. //TODO: check if hero ability makes some upgrades possible
  203. if(ret.newID.size())
  204. ret.oldID = base->idNumber;
  205. return ret;
  206. }
  207. const StartInfo * CCallback::getStartInfo()
  208. {
  209. return gs->scenarioOps;
  210. }
  211. int CCallback::howManyTowns()
  212. {
  213. return gs->players[gs->currentPlayer].towns.size();
  214. }
  215. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  216. {
  217. if (!mode)
  218. return gs->players[gs->currentPlayer].towns[val];
  219. else
  220. {
  221. //TODO: add some smart ID to the CTownInstance
  222. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  223. //{
  224. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  225. // return gs->players[gs->currentPlayer].towns[i];
  226. //}
  227. return NULL;
  228. }
  229. return NULL;
  230. }
  231. int CCallback::howManyHeroes()
  232. {
  233. return gs->players[player].heroes.size();
  234. }
  235. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  236. {
  237. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  238. return NULL;
  239. if (!mode)
  240. if(val<gs->players[player].heroes.size())
  241. return gs->players[player].heroes[val];
  242. else return NULL;
  243. else
  244. {
  245. for (int i=0; i<gs->players[player].heroes.size();i++)
  246. {
  247. if (gs->players[player].heroes[i]->type->ID==val)
  248. return gs->players[player].heroes[i];
  249. }
  250. }
  251. return NULL;
  252. }
  253. int CCallback::getResourceAmount(int type)
  254. {
  255. return gs->players[player].resources[type];
  256. }
  257. std::vector<si32> CCallback::getResourceAmount()
  258. {
  259. return gs->players[player].resources;
  260. }
  261. int CCallback::getDate(int mode)
  262. {
  263. return gs->getDate(mode);
  264. }
  265. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  266. {
  267. std::vector<std::string> ret;
  268. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  269. ret.push_back(obj->hoverName);
  270. return ret;
  271. }
  272. bool CCallback::verifyPath(CPath * path, bool blockSea)
  273. {
  274. for (int i=0;i<path->nodes.size();i++)
  275. {
  276. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
  277. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
  278. return false; //path is wrong - one of the tiles is blocked
  279. if (blockSea)
  280. {
  281. if (i==0)
  282. continue;
  283. if (
  284. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==EterrainType::water)
  285. &&
  286. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=EterrainType::water))
  287. ||
  288. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=EterrainType::water)
  289. &&
  290. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==EterrainType::water))
  291. ||
  292. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==EterrainType::rock)
  293. )
  294. return false;
  295. }
  296. }
  297. return true;
  298. }
  299. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap()
  300. {
  301. return gs->players[player].fogOfWarMap;
  302. }
  303. bool CCallback::isVisible(int3 pos, int Player)
  304. {
  305. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  306. }
  307. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
  308. {
  309. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  310. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  311. {
  312. for (int j=0;j<(*i).second.towns.size();j++)
  313. {
  314. if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
  315. {
  316. ret.push_back((*i).second.towns[j]);
  317. }
  318. }
  319. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  320. return ret;
  321. }
  322. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  323. {
  324. std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
  325. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  326. {
  327. for (int j=0;j<(*i).second.heroes.size();j++)
  328. {
  329. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  330. {
  331. ret.push_back((*i).second.heroes[j]);
  332. }
  333. }
  334. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  335. return ret;
  336. }
  337. bool CCallback::isVisible(int3 pos)
  338. {
  339. return isVisible(pos,player);
  340. }
  341. int CCallback::getMyColor()
  342. {
  343. return player;
  344. }
  345. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  346. {
  347. for (int i=0; i<gs->players[player].heroes.size();i++)
  348. {
  349. if (gs->players[player].heroes[i]==hero)
  350. return i;
  351. }
  352. return -1;
  353. }
  354. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
  355. {
  356. if(!obj)
  357. return NULL;
  358. if(obj->ID == 34)
  359. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  360. else if(obj->ID == 98)
  361. return &(dynamic_cast<const CGTownInstance*>(obj)->army);
  362. else return NULL;
  363. }
  364. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  365. {
  366. if(s1->tempOwner != player || s2->tempOwner != player)
  367. return -1;
  368. *cl->serv << ui16(502) << ui8(1) << s1->id << ui8(p1) << s2->id << ui8(p2);
  369. return 0;
  370. }
  371. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  372. {
  373. if ((s1->tempOwner!= player || s2->tempOwner!=player))
  374. {
  375. return -1;
  376. }
  377. *cl->serv << ui16(502) << ui8(2) << s1->id << ui8(p1) << s2->id << ui8(p2);
  378. return 0;
  379. }
  380. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  381. {
  382. if (s1->tempOwner!= player || s2->tempOwner!=player || (!val))
  383. {
  384. return -1;
  385. }
  386. *cl->serv << ui16(502) << ui8(3) << s1->id << ui8(p1) << s2->id << ui8(p2) << si32(val);
  387. return 0;
  388. }
  389. bool CCallback::dismissHero(const CGHeroInstance *hero)
  390. {
  391. CGHeroInstance * Vhero = const_cast<CGHeroInstance *>(hero);
  392. CGI->mh->removeObject(Vhero);
  393. std::vector<CGHeroInstance*>::iterator nitr = find(CGI->state->players[player].heroes.begin(), CGI->state->players[player].heroes.end(), Vhero);
  394. CGI->state->players[player].heroes.erase(nitr);
  395. LOCPLINT->adventureInt->heroList.updateHList();
  396. return false;
  397. }
  398. int CCallback::getMySerial()
  399. {
  400. return gs->players[player].serial;
  401. }
  402. bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
  403. {
  404. if(!hero1 || !hero2) //incorrect data
  405. return false;
  406. CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
  407. CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
  408. if(worn1 && worn2)
  409. {
  410. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
  411. }
  412. else if(worn1 && !worn2)
  413. {
  414. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
  415. }
  416. else if(!worn1 && worn2)
  417. {
  418. std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
  419. }
  420. else
  421. {
  422. std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
  423. }
  424. return true;
  425. }
  426. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  427. {
  428. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  429. if(town->tempOwner!=player)
  430. return false;
  431. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  432. for(int i=0;i<7;i++)
  433. if(b->resources[i] > gs->players[player].resources[i])
  434. return false; //lack of resources
  435. *cl->serv << ui16(504) << town->id << buildingID;
  436. //TODO: check if we are allowed to build
  437. return true;
  438. }
  439. int CCallback::battleGetBattlefieldType()
  440. {
  441. return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].tileInfo->tertype;
  442. }
  443. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  444. {
  445. //TODO - write
  446. return -1;
  447. }
  448. int CCallback::battleGetStack(int pos)
  449. {
  450. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  451. {
  452. if(CGI->state->curB->stacks[g]->position == pos ||
  453. ( CGI->state->curB->stacks[g]->creature->isDoubleWide() &&
  454. ( (CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position-1 == pos) ||
  455. (!CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position+1 == pos)
  456. )
  457. )
  458. )
  459. return CGI->state->curB->stacks[g]->ID;
  460. }
  461. return -1;
  462. }
  463. CStack* CCallback::battleGetStackByID(int ID)
  464. {
  465. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  466. {
  467. if(CGI->state->curB->stacks[g]->ID == ID)
  468. return CGI->state->curB->stacks[g];
  469. }
  470. return NULL;
  471. }
  472. CStack* CCallback::battleGetStackByPos(int pos)
  473. {
  474. return battleGetStackByID(battleGetStack(pos));
  475. }
  476. int CCallback::battleGetPos(int stack)
  477. {
  478. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  479. {
  480. if(CGI->state->curB->stacks[g]->ID == stack)
  481. return CGI->state->curB->stacks[g]->position;
  482. }
  483. return -1;
  484. }
  485. std::map<int, CStack> CCallback::battleGetStacks()
  486. {
  487. std::map<int, CStack> ret;
  488. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  489. {
  490. ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
  491. }
  492. return ret;
  493. }
  494. CCreature CCallback::battleGetCreature(int number)
  495. {
  496. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  497. {
  498. if(CGI->state->curB->stacks[h]->ID == number) //creature found
  499. return *(CGI->state->curB->stacks[h]->creature);
  500. }
  501. throw new std::exception("Cannot find the creature");
  502. }
  503. std::vector<int> CCallback::battleGetAvailableHexes(int ID)
  504. {
  505. return CGI->state->battleGetRange(ID);
  506. }
  507. bool CCallback::battleIsStackMine(int ID)
  508. {
  509. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  510. {
  511. if(CGI->state->curB->stacks[h]->ID == ID) //creature found
  512. return CGI->state->curB->stacks[h]->owner == player;
  513. }
  514. return false;
  515. }