CCallback.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  146. for(int i=0;i<b->resources.size();i++)
  147. if(b->resources[i] > gs->players[player].resources[i])
  148. return false; //lack of resources
  149. BuildStructure pack(town->id,buildingID);
  150. sendRequest(&pack);
  151. return true;
  152. }
  153. int CBattleCallback::battleMakeAction(BattleAction* action)
  154. {
  155. assert(action->actionType == BattleAction::HERO_SPELL);
  156. MakeCustomAction mca(*action);
  157. sendRequest(&mca);
  158. return 0;
  159. }
  160. template <typename T>
  161. void CBattleCallback::sendRequest(const T* request)
  162. {
  163. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  164. if(waitTillRealize)
  165. cl->waitingRequest.set(true);
  166. *cl->serv << request;
  167. if(waitTillRealize)
  168. cl->waitingRequest.waitWhileTrue();
  169. }
  170. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  171. {
  172. if(town->tempOwner != player) return;
  173. GarrisonHeroSwap pack(town->id);
  174. sendRequest(&pack);
  175. }
  176. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  177. {
  178. if(hero->tempOwner != player) return;
  179. BuyArtifact pack(hero->id,aid);
  180. sendRequest(&pack);
  181. }
  182. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  183. {
  184. TradeOnMarketplace pack;
  185. pack.market = market;
  186. pack.hero = hero;
  187. pack.mode = mode;
  188. pack.r1 = id1;
  189. pack.r2 = id2;
  190. pack.val = val1;
  191. sendRequest(&pack);
  192. }
  193. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  194. {
  195. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  196. SetFormation pack(hero->id,tight);
  197. sendRequest(&pack);
  198. }
  199. void CCallback::setSelection(const CArmedInstance * obj)
  200. {
  201. SetSelection ss;
  202. ss.player = player;
  203. ss.id = obj->id;
  204. sendRequest(&ss);
  205. if(obj->ID == HEROI_TYPE)
  206. {
  207. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  208. //nasty workaround. TODO: nice workaround
  209. cl->gs->getPlayer(player)->currentSelection = obj->id;
  210. }
  211. }
  212. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  213. {
  214. ui8 i=0;
  215. for(; i<gs->players[player].availableHeroes.size(); i++)
  216. {
  217. if(gs->players[player].availableHeroes[i] == hero)
  218. {
  219. HireHero pack(i,townOrTavern->id);
  220. pack.player = player;
  221. sendRequest(&pack);
  222. return;
  223. }
  224. }
  225. }
  226. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  227. {
  228. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  229. return gs->getPath(src,dest,hero, ret);
  230. }
  231. void CCallback::save( const std::string &fname )
  232. {
  233. cl->save(fname);
  234. }
  235. void CCallback::sendMessage(const std::string &mess)
  236. {
  237. PlayerMessage pm(player, mess);
  238. sendRequest(&pm);
  239. }
  240. void CCallback::buildBoat( const IShipyard *obj )
  241. {
  242. BuildBoat bb;
  243. bb.objid = obj->o->id;
  244. sendRequest(&bb);
  245. }
  246. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  247. :CBattleCallback(GS, Player, C)
  248. {
  249. waitTillRealize = false;
  250. }
  251. const CGPathNode * CCallback::getPathInfo( int3 tile )
  252. {
  253. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  254. }
  255. bool CCallback::getPath2( int3 dest, CGPath &ret )
  256. {
  257. if (!gs->map->isInTheMap(dest))
  258. return false;
  259. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  260. assert(cl->pathInfo->hero == h);
  261. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  262. {
  263. recalculatePaths();
  264. }
  265. return cl->pathInfo->getPath(dest, ret);
  266. }
  267. void CCallback::recalculatePaths()
  268. {
  269. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  270. }
  271. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  272. {
  273. gs->calculatePaths(hero, out, src, movement);
  274. }
  275. void CCallback::dig( const CGObjectInstance *hero )
  276. {
  277. DigWithHero dwh;
  278. dwh.id = hero->id;
  279. sendRequest(&dwh);
  280. }
  281. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  282. {
  283. CastAdvSpell cas;
  284. cas.hid = hero->id;
  285. cas.sid = spellID;
  286. cas.pos = pos;
  287. sendRequest(&cas);
  288. }
  289. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  290. {
  291. gs = GS;
  292. player = Player;
  293. cl = C;
  294. }
  295. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  296. {
  297. MakeAction ma;
  298. ma.ba = *action;
  299. sendRequest(&ma);
  300. return true;
  301. }