2
0

CCallback.cpp 10 KB

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