CCallback.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "lib/CCreatureHandler.h"
  4. #include "client/CGameInfo.h"
  5. #include "lib/CGameState.h"
  6. #include "lib/BattleState.h"
  7. #include "client/CPlayerInterface.h"
  8. #include "client/Client.h"
  9. #include "lib/map.h"
  10. #include "lib/CBuildingHandler.h"
  11. #include "lib/CDefObjInfoHandler.h"
  12. #include "lib/CGeneralTextHandler.h"
  13. #include "lib/CHeroHandler.h"
  14. #include "lib/CObjectHandler.h"
  15. #include "lib/Connection.h"
  16. #include "lib/NetPacks.h"
  17. #include "client/mapHandler.h"
  18. #include <boost/foreach.hpp>
  19. #include <boost/thread.hpp>
  20. #include <boost/thread/shared_mutex.hpp>
  21. #include "lib/CSpellHandler.h"
  22. #include "lib/CArtHandler.h"
  23. #ifdef min
  24. #undef min
  25. #endif
  26. #ifdef max
  27. #undef max
  28. #endif
  29. /*
  30. * CCallback.cpp, part of VCMI engine
  31. *
  32. * Authors: listed in file AUTHORS in main folder
  33. *
  34. * License: GNU General Public License v2.0 or later
  35. * Full text of license available in license.txt file, in main folder
  36. *
  37. */
  38. template <ui16 N> bool isType(CPack *pack)
  39. {
  40. return pack->getType() == N;
  41. }
  42. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  43. {
  44. CastleTeleportHero pack(who->id, where->id, 1);
  45. sendRequest(&pack);
  46. return true;
  47. }
  48. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
  49. {
  50. MoveHero pack(dst,h->id);
  51. sendRequest(&pack);
  52. return true;
  53. }
  54. void CCallback::selectionMade(int selection, int asker)
  55. {
  56. QueryReply pack(asker,selection);
  57. *cl->serv << &pack;
  58. }
  59. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
  60. {
  61. if(player!=obj->tempOwner && obj->ID != 106)
  62. return;
  63. RecruitCreatures pack(obj->id,ID,amount,level);
  64. sendRequest(&pack);
  65. }
  66. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  67. {
  68. if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  69. return false;
  70. DisbandCreature pack(stackPos,obj->id);
  71. sendRequest(&pack);
  72. return true;
  73. }
  74. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  75. {
  76. UpgradeCreature pack(stackPos,obj->id,newID);
  77. sendRequest(&pack);
  78. return false;
  79. }
  80. void CCallback::endTurn()
  81. {
  82. tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
  83. EndTurn pack;
  84. sendRequest(&pack); //report that we ended turn
  85. }
  86. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  87. {
  88. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  89. sendRequest(&pack);
  90. return 0;
  91. }
  92. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  93. {
  94. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  95. sendRequest(&pack);
  96. return 0;
  97. }
  98. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
  99. {
  100. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  101. sendRequest(&pack);
  102. return 0;
  103. }
  104. bool CCallback::dismissHero(const CGHeroInstance *hero)
  105. {
  106. if(player!=hero->tempOwner) return false;
  107. DismissHero pack(hero->id);
  108. sendRequest(&pack);
  109. return true;
  110. }
  111. // int CCallback::getMySerial() const
  112. // {
  113. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  114. // return gs->players[player].serial;
  115. // }
  116. bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
  117. {
  118. if(player!=hero1->tempOwner && player!=hero2->tempOwner)
  119. return false;
  120. ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
  121. sendRequest(&ea);
  122. return true;
  123. }
  124. /**
  125. * Assembles or disassembles a combination artifact.
  126. * @param hero Hero holding the artifact(s).
  127. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  128. * @param assemble True for assembly operation, false for disassembly.
  129. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  130. * artifact to assemble to. Otherwise it's not used.
  131. */
  132. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
  133. {
  134. if (player != hero->tempOwner)
  135. return false;
  136. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  137. sendRequest(&aa);
  138. return true;
  139. }
  140. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  141. {
  142. //CGTownInstance * t = const_cast<CGTownInstance *>(town);
  143. if(town->tempOwner!=player)
  144. return false;
  145. if(!canBuildStructure(town, buildingID))
  146. return false;
  147. // const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  148. // for(int i=0;i<b->resources.size();i++)
  149. // if(b->resources[i] > gs->players[player].resources[i])
  150. // return false; //lack of resources
  151. BuildStructure pack(town->id,buildingID);
  152. sendRequest(&pack);
  153. return true;
  154. }
  155. int CBattleCallback::battleMakeAction(BattleAction* action)
  156. {
  157. assert(action->actionType == BattleAction::HERO_SPELL);
  158. MakeCustomAction mca(*action);
  159. sendRequest(&mca);
  160. return 0;
  161. }
  162. template <typename T>
  163. void CBattleCallback::sendRequest(const T* request)
  164. {
  165. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  166. if(waitTillRealize)
  167. cl->waitingRequest.set(true);
  168. *cl->serv << request;
  169. if(waitTillRealize)
  170. {
  171. if(unlockGsWhenWaiting)
  172. gs->mx->unlock_shared();
  173. cl->waitingRequest.waitWhileTrue();
  174. if(unlockGsWhenWaiting)
  175. gs->mx->lock_shared();
  176. }
  177. }
  178. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  179. {
  180. if(town->tempOwner != player) return;
  181. GarrisonHeroSwap pack(town->id);
  182. sendRequest(&pack);
  183. }
  184. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  185. {
  186. if(hero->tempOwner != player) return;
  187. BuyArtifact pack(hero->id,aid);
  188. sendRequest(&pack);
  189. }
  190. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  191. {
  192. TradeOnMarketplace pack;
  193. pack.market = market;
  194. pack.hero = hero;
  195. pack.mode = mode;
  196. pack.r1 = id1;
  197. pack.r2 = id2;
  198. pack.val = val1;
  199. sendRequest(&pack);
  200. }
  201. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  202. {
  203. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  204. SetFormation pack(hero->id,tight);
  205. sendRequest(&pack);
  206. }
  207. void CCallback::setSelection(const CArmedInstance * obj)
  208. {
  209. SetSelection ss;
  210. ss.player = player;
  211. ss.id = obj->id;
  212. sendRequest(&ss);
  213. if(obj->ID == HEROI_TYPE)
  214. {
  215. if(cl->pathInfo->hero != obj) //calculate new paths only if we selected a different hero
  216. cl->calculatePaths(static_cast<const CGHeroInstance *>(obj));
  217. //nasty workaround. TODO: nice workaround
  218. cl->gs->getPlayer(player)->currentSelection = obj->id;
  219. }
  220. }
  221. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  222. {
  223. ui8 i=0;
  224. for(; i<gs->players[player].availableHeroes.size(); i++)
  225. {
  226. if(gs->players[player].availableHeroes[i] == hero)
  227. {
  228. HireHero pack(i,townOrTavern->id);
  229. pack.player = player;
  230. sendRequest(&pack);
  231. return;
  232. }
  233. }
  234. }
  235. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  236. {
  237. return gs->getPath(src,dest,hero, ret);
  238. }
  239. void CCallback::save( const std::string &fname )
  240. {
  241. cl->save(fname);
  242. }
  243. void CCallback::sendMessage(const std::string &mess)
  244. {
  245. PlayerMessage pm(player, mess);
  246. sendRequest(&pm);
  247. }
  248. void CCallback::buildBoat( const IShipyard *obj )
  249. {
  250. BuildBoat bb;
  251. bb.objid = obj->o->id;
  252. sendRequest(&bb);
  253. }
  254. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  255. :CBattleCallback(GS, Player, C)
  256. {
  257. waitTillRealize = false;
  258. }
  259. const CGPathNode * CCallback::getPathInfo( int3 tile )
  260. {
  261. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  262. }
  263. bool CCallback::getPath2( int3 dest, CGPath &ret )
  264. {
  265. if (!gs->map->isInTheMap(dest))
  266. return false;
  267. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  268. assert(cl->pathInfo->hero == h);
  269. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  270. {
  271. recalculatePaths();
  272. }
  273. boost::unique_lock<boost::mutex> pathLock(cl->pathMx);
  274. return cl->pathInfo->getPath(dest, ret);
  275. }
  276. void CCallback::recalculatePaths()
  277. {
  278. cl->calculatePaths(cl->IGameCallback::getSelectedHero(player));
  279. }
  280. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  281. {
  282. gs->calculatePaths(hero, out, src, movement);
  283. }
  284. void CCallback::dig( const CGObjectInstance *hero )
  285. {
  286. DigWithHero dwh;
  287. dwh.id = hero->id;
  288. sendRequest(&dwh);
  289. }
  290. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  291. {
  292. CastAdvSpell cas;
  293. cas.hid = hero->id;
  294. cas.sid = spellID;
  295. cas.pos = pos;
  296. sendRequest(&cas);
  297. }
  298. void CCallback::unregisterMyInterface()
  299. {
  300. assert(player >= 0); //works only for player callback
  301. cl->playerint.erase(player);
  302. cl->battleints.erase(player);
  303. //TODO? should callback be disabled as well?
  304. }
  305. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  306. {
  307. gs = GS;
  308. player = Player;
  309. cl = C;
  310. }
  311. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  312. {
  313. MakeAction ma;
  314. ma.ba = *action;
  315. sendRequest(&ma);
  316. return true;
  317. }