CCallback.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. #include <boost/thread/shared_mutex.hpp>
  22. #ifdef min
  23. #undef min
  24. #endif
  25. #ifdef max
  26. #undef max
  27. #endif
  28. extern CSharedCond<std::set<IPack*> > mess;
  29. HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
  30. :src(Src),dst(Dst),ho(Ho)
  31. {
  32. owner = ho->getOwner();
  33. };
  34. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  35. {
  36. CGHeroInstance * hero = NULL;
  37. if (idtype==0)
  38. {
  39. if (player==-1)
  40. hero=gs->players[player+1].heroes[ID];
  41. else
  42. hero=gs->players[player].heroes[ID];
  43. }
  44. else if (idtype==1 && player>=0) //looking for it in local area
  45. {
  46. for (int i=0; i<gs->players[player].heroes.size();i++)
  47. {
  48. if (gs->players[player].heroes[i]->type->ID == ID)
  49. hero = gs->players[player].heroes[i];
  50. }
  51. }
  52. else //idtype==1; player<0
  53. {
  54. for(std::map<ui8, PlayerState>::iterator j=gs->players.begin(); j!=gs->players.end(); ++j)
  55. {
  56. for (int i=0; i<(*j).second.heroes.size();i++)
  57. {
  58. if ((*j).second.heroes[i]->type->ID == ID)
  59. {
  60. hero = (*j).second.heroes[i];
  61. }
  62. }
  63. }
  64. }
  65. if (!hero)
  66. return false; //can't find hero
  67. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  68. return false; //invalid path
  69. //check path format
  70. if (pathType==0)
  71. CPathfinder::convertPath(path,pathType);
  72. if (pathType>1)
  73. #ifndef __GNUC__
  74. throw std::exception("Unknown path format");
  75. #else
  76. throw std::exception();
  77. #endif
  78. CPath * ourPath = path;
  79. if(!ourPath)
  80. return false;
  81. for(int i=ourPath->nodes.size()-1; i>0; i--)
  82. {
  83. int3 stpos(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z),
  84. endpos(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  85. HeroMoveDetails curd(stpos,endpos,hero);
  86. *cl->serv << ui16(501) << hero->id << stpos << endpos;
  87. {//wait till there is server answer
  88. boost::unique_lock<boost::mutex> lock(*mess.mx);
  89. while(std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>) == mess.res->end())
  90. mess.cv->wait(lock);
  91. std::set<IPack*>::iterator itr = std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>);
  92. TryMoveHero tmh = *static_cast<TryMoveHero*>(*itr);
  93. mess.res->erase(itr);
  94. if(!tmh.result)
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. void CCallback::selectionMade(int selection, int asker)
  101. {
  102. *cl->serv << ui16(2001) << ui32(asker) << ui32(selection);
  103. }
  104. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount)
  105. {
  106. if(player!=obj->tempOwner) return;
  107. *cl->serv << ui16(506) << obj->id << ID << amount;
  108. }
  109. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  110. {
  111. if(((player>=0) && obj->tempOwner != player) || obj->army.slots.size()<2)
  112. return false;
  113. *cl->serv << ui16(503) << obj->id << ui8(stackPos);
  114. return true;
  115. }
  116. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  117. {
  118. *cl->serv << ui16(507) << obj->id << ui8(stackPos) << ui32(newID);
  119. return false;
  120. }
  121. void CCallback::endTurn()
  122. {
  123. std::cout << "Player "<<(unsigned)player<<" end his turn."<<std::endl;
  124. cl->serv->wmx->lock();
  125. *cl->serv << ui16(100); //report that we ended turn
  126. cl->serv->wmx->unlock();
  127. }
  128. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos)
  129. {
  130. return gs->getUpgradeInfo(const_cast<CArmedInstance*>(obj),stackPos);
  131. }
  132. const StartInfo * CCallback::getStartInfo()
  133. {
  134. return gs->scenarioOps;
  135. }
  136. int CCallback::howManyTowns()
  137. {
  138. return gs->players[gs->currentPlayer].towns.size();
  139. }
  140. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  141. {
  142. if (!mode)
  143. return gs->players[gs->currentPlayer].towns[val];
  144. else
  145. {
  146. //TODO: add some smart ID to the CTownInstance
  147. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  148. //{
  149. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  150. // return gs->players[gs->currentPlayer].towns[i];
  151. //}
  152. return NULL;
  153. }
  154. return NULL;
  155. }
  156. int CCallback::howManyHeroes()
  157. {
  158. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  159. return gs->players[player].heroes.size();
  160. }
  161. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  162. {
  163. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  164. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  165. return NULL;
  166. if (!mode)
  167. if(val<gs->players[player].heroes.size())
  168. return gs->players[player].heroes[val];
  169. else return NULL;
  170. else
  171. {
  172. for (int i=0; i<gs->players[player].heroes.size();i++)
  173. {
  174. if (gs->players[player].heroes[i]->type->ID==val)
  175. return gs->players[player].heroes[i];
  176. }
  177. }
  178. return NULL;
  179. }
  180. int CCallback::getResourceAmount(int type)
  181. {
  182. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  183. return gs->players[player].resources[type];
  184. }
  185. std::vector<si32> CCallback::getResourceAmount()
  186. {
  187. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  188. return gs->players[player].resources;
  189. }
  190. int CCallback::getDate(int mode)
  191. {
  192. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  193. return gs->getDate(mode);
  194. }
  195. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  196. {
  197. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  198. std::vector<std::string> ret;
  199. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  200. ret.push_back(obj->hoverName);
  201. return ret;
  202. }
  203. bool CCallback::verifyPath(CPath * path, bool blockSea)
  204. {
  205. for (int i=0;i<path->nodes.size();i++)
  206. {
  207. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
  208. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
  209. return false; //path is wrong - one of the tiles is blocked
  210. if (blockSea)
  211. {
  212. if (i==0)
  213. continue;
  214. if (
  215. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==water)
  216. &&
  217. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=water))
  218. ||
  219. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=water)
  220. &&
  221. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==water))
  222. ||
  223. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==rock)
  224. )
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap()
  231. {
  232. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  233. return gs->players[player].fogOfWarMap;
  234. }
  235. bool CCallback::isVisible(int3 pos, int Player)
  236. {
  237. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  238. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  239. }
  240. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
  241. {
  242. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  243. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  244. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  245. {
  246. for (int j=0;j<(*i).second.towns.size();j++)
  247. {
  248. if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
  249. {
  250. ret.push_back((*i).second.towns[j]);
  251. }
  252. }
  253. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  254. return ret;
  255. }
  256. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  257. {
  258. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  259. std::vector < const CGHeroInstance *> ret;
  260. for(int i=0;i<gs->map->heroes.size();i++)
  261. {
  262. if( (gs->map->heroes[i]->tempOwner==player) ||
  263. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  264. {
  265. ret.push_back(gs->map->heroes[i]);
  266. }
  267. }
  268. return ret;
  269. }
  270. bool CCallback::isVisible(int3 pos)
  271. {
  272. return isVisible(pos,player);
  273. }
  274. int CCallback::getMyColor()
  275. {
  276. return player;
  277. }
  278. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  279. {
  280. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  281. for (int i=0; i<gs->players[player].heroes.size();i++)
  282. {
  283. if (gs->players[player].heroes[i]==hero)
  284. return i;
  285. }
  286. return -1;
  287. }
  288. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
  289. {
  290. if(!obj)
  291. return NULL;
  292. if(obj->ID == 34)
  293. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  294. else if(obj->ID == 98)
  295. return &(dynamic_cast<const CGTownInstance*>(obj)->army);
  296. else return NULL;
  297. }
  298. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  299. {
  300. if(s1->tempOwner != player || s2->tempOwner != player)
  301. return -1;
  302. *cl->serv << ui16(502) << ui8(1) << s1->id << ui8(p1) << s2->id << ui8(p2);
  303. return 0;
  304. }
  305. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  306. {
  307. if ((s1->tempOwner!= player || s2->tempOwner!=player))
  308. {
  309. return -1;
  310. }
  311. *cl->serv << ui16(502) << ui8(2) << s1->id << ui8(p1) << s2->id << ui8(p2);
  312. return 0;
  313. }
  314. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  315. {
  316. if (s1->tempOwner!= player || s2->tempOwner!=player || (!val))
  317. {
  318. return -1;
  319. }
  320. *cl->serv << ui16(502) << ui8(3) << s1->id << ui8(p1) << s2->id << ui8(p2) << si32(val);
  321. return 0;
  322. }
  323. bool CCallback::dismissHero(const CGHeroInstance *hero)
  324. {
  325. if(player!=hero->tempOwner) return false;
  326. *cl->serv << ui16(500) << hero->id;
  327. return true;
  328. }
  329. int CCallback::getMySerial()
  330. {
  331. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  332. return gs->players[player].serial;
  333. }
  334. bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
  335. {
  336. if(!hero1 || !hero2) //incorrect data
  337. return false;
  338. CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
  339. CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
  340. if(worn1 && worn2)
  341. {
  342. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
  343. }
  344. else if(worn1 && !worn2)
  345. {
  346. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
  347. }
  348. else if(!worn1 && worn2)
  349. {
  350. std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
  351. }
  352. else
  353. {
  354. std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
  355. }
  356. return true;
  357. }
  358. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  359. {
  360. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  361. if(town->tempOwner!=player)
  362. return false;
  363. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  364. for(int i=0;i<7;i++)
  365. if(b->resources[i] > gs->players[player].resources[i])
  366. return false; //lack of resources
  367. *cl->serv << ui16(504) << town->id << buildingID;
  368. //TODO: check if we are allowed to build
  369. return true;
  370. }
  371. int CCallback::battleGetBattlefieldType()
  372. {
  373. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  374. return CGI->mh->ttiles[gs->curB->tile.x][gs->curB->tile.y][gs->curB->tile.z].tileInfo->tertype;
  375. }
  376. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  377. {
  378. //TODO - write
  379. return -1;
  380. }
  381. int CCallback::battleGetStack(int pos)
  382. {
  383. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  384. return gs->battleGetStack(pos);
  385. }
  386. CStack* CCallback::battleGetStackByID(int ID)
  387. {
  388. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  389. if(!gs->curB) return NULL;
  390. return gs->curB->getStack(ID);
  391. }
  392. CStack* CCallback::battleGetStackByPos(int pos)
  393. {
  394. return battleGetStackByID(battleGetStack(pos));
  395. }
  396. int CCallback::battleGetPos(int stack)
  397. {
  398. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  399. for(int g=0; g<gs->curB->stacks.size(); ++g)
  400. {
  401. if(gs->curB->stacks[g]->ID == stack)
  402. return gs->curB->stacks[g]->position;
  403. }
  404. return -1;
  405. }
  406. std::map<int, CStack> CCallback::battleGetStacks()
  407. {
  408. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  409. std::map<int, CStack> ret;
  410. for(int g=0; g<gs->curB->stacks.size(); ++g)
  411. {
  412. ret[gs->curB->stacks[g]->ID] = *(gs->curB->stacks[g]);
  413. }
  414. return ret;
  415. }
  416. CCreature CCallback::battleGetCreature(int number)
  417. {
  418. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  419. for(int h=0; h<gs->curB->stacks.size(); ++h)
  420. {
  421. if(gs->curB->stacks[h]->ID == number) //creature found
  422. return *(gs->curB->stacks[h]->creature);
  423. }
  424. #ifndef __GNUC__
  425. throw new std::exception("Cannot find the creature");
  426. #else
  427. throw new std::exception();
  428. #endif
  429. }
  430. std::vector<int> CCallback::battleGetAvailableHexes(int ID)
  431. {
  432. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  433. return gs->curB->getAccessibility(ID);
  434. //return gs->battleGetRange(ID);
  435. }
  436. bool CCallback::battleIsStackMine(int ID)
  437. {
  438. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  439. for(int h=0; h<gs->curB->stacks.size(); ++h)
  440. {
  441. if(gs->curB->stacks[h]->ID == ID) //creature found
  442. return gs->curB->stacks[h]->owner == player;
  443. }
  444. return false;
  445. }
  446. bool CCallback::battleCanShoot(int ID, int dest) //TODO: finish
  447. {
  448. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  449. if(battleGetStackByID(ID)->creature->isShooting()
  450. && battleGetStack(dest) != -1
  451. && battleGetStackByPos(dest)->owner != battleGetStackByID(ID)->owner
  452. && battleGetStackByPos(dest)->alive)
  453. return true;
  454. return false;
  455. }