CCallback.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. pack.player = player;
  58. boost::unique_lock<boost::mutex> lock(*cl->serv->wmx);
  59. *cl->serv << &pack;
  60. }
  61. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
  62. {
  63. if(player!=obj->tempOwner && obj->ID != 106)
  64. return;
  65. RecruitCreatures pack(obj->id,ID,amount,level);
  66. sendRequest(&pack);
  67. }
  68. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  69. {
  70. if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  71. return false;
  72. DisbandCreature pack(stackPos,obj->id);
  73. sendRequest(&pack);
  74. return true;
  75. }
  76. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  77. {
  78. UpgradeCreature pack(stackPos,obj->id,newID);
  79. sendRequest(&pack);
  80. return false;
  81. }
  82. void CCallback::endTurn()
  83. {
  84. tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
  85. EndTurn pack;
  86. sendRequest(&pack); //report that we ended turn
  87. }
  88. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  89. {
  90. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  91. sendRequest(&pack);
  92. return 0;
  93. }
  94. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  95. {
  96. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  97. sendRequest(&pack);
  98. return 0;
  99. }
  100. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
  101. {
  102. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  103. sendRequest(&pack);
  104. return 0;
  105. }
  106. bool CCallback::dismissHero(const CGHeroInstance *hero)
  107. {
  108. if(player!=hero->tempOwner) return false;
  109. DismissHero pack(hero->id);
  110. sendRequest(&pack);
  111. return true;
  112. }
  113. // int CCallback::getMySerial() const
  114. // {
  115. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  116. // return gs->players[player].serial;
  117. // }
  118. bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
  119. {
  120. if(player!=hero1->tempOwner && player!=hero2->tempOwner)
  121. return false;
  122. ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
  123. sendRequest(&ea);
  124. return true;
  125. }
  126. /**
  127. * Assembles or disassembles a combination artifact.
  128. * @param hero Hero holding the artifact(s).
  129. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  130. * @param assemble True for assembly operation, false for disassembly.
  131. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  132. * artifact to assemble to. Otherwise it's not used.
  133. */
  134. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
  135. {
  136. if (player != hero->tempOwner)
  137. return false;
  138. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  139. sendRequest(&aa);
  140. return true;
  141. }
  142. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  143. {
  144. //CGTownInstance * t = const_cast<CGTownInstance *>(town);
  145. if(town->tempOwner!=player)
  146. return false;
  147. if(!canBuildStructure(town, buildingID))
  148. return false;
  149. // const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  150. // for(int i=0;i<b->resources.size();i++)
  151. // if(b->resources[i] > gs->players[player].resources[i])
  152. // return false; //lack of resources
  153. BuildStructure pack(town->id,buildingID);
  154. sendRequest(&pack);
  155. return true;
  156. }
  157. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  158. {
  159. if(town->tempOwner != player) return;
  160. GarrisonHeroSwap pack(town->id);
  161. sendRequest(&pack);
  162. }
  163. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  164. {
  165. if(hero->tempOwner != player) return;
  166. BuyArtifact pack(hero->id,aid);
  167. sendRequest(&pack);
  168. }
  169. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  170. {
  171. TradeOnMarketplace pack;
  172. pack.market = market;
  173. pack.hero = hero;
  174. pack.mode = mode;
  175. pack.r1 = id1;
  176. pack.r2 = id2;
  177. pack.val = val1;
  178. sendRequest(&pack);
  179. }
  180. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  181. {
  182. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  183. SetFormation pack(hero->id,tight);
  184. sendRequest(&pack);
  185. }
  186. void CCallback::setSelection(const CArmedInstance * obj)
  187. {
  188. SetSelection ss;
  189. ss.player = player;
  190. ss.id = obj->id;
  191. sendRequest(&(CPackForClient&)ss);
  192. if(obj->ID == HEROI_TYPE)
  193. {
  194. if(cl->pathInfo->hero != obj) //calculate new paths only if we selected a different hero
  195. cl->calculatePaths(static_cast<const CGHeroInstance *>(obj));
  196. //nasty workaround. TODO: nice workaround
  197. cl->gs->getPlayer(player)->currentSelection = obj->id;
  198. }
  199. }
  200. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  201. {
  202. ui8 i=0;
  203. for(; i<gs->players[player].availableHeroes.size(); i++)
  204. {
  205. if(gs->players[player].availableHeroes[i] == hero)
  206. {
  207. HireHero pack(i,townOrTavern->id);
  208. pack.player = player;
  209. sendRequest(&pack);
  210. return;
  211. }
  212. }
  213. }
  214. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  215. {
  216. return gs->getPath(src,dest,hero, ret);
  217. }
  218. void CCallback::save( const std::string &fname )
  219. {
  220. cl->save(fname);
  221. }
  222. void CCallback::sendMessage(const std::string &mess)
  223. {
  224. PlayerMessage pm(player, mess);
  225. sendRequest(&(CPackForClient&)pm);
  226. }
  227. void CCallback::buildBoat( const IShipyard *obj )
  228. {
  229. BuildBoat bb;
  230. bb.objid = obj->o->id;
  231. sendRequest(&bb);
  232. }
  233. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  234. :CBattleCallback(GS, Player, C)
  235. {
  236. cl = C;
  237. waitTillRealize = false;
  238. }
  239. const CGPathNode * CCallback::getPathInfo( int3 tile )
  240. {
  241. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  242. }
  243. bool CCallback::getPath2( int3 dest, CGPath &ret )
  244. {
  245. if (!gs->map->isInTheMap(dest))
  246. return false;
  247. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  248. assert(cl->pathInfo->hero == h);
  249. if(cl->pathInfo->hpos != h->getPosition(false) || !cl->pathInfo->isValid) //hero position changed, must update paths
  250. {
  251. recalculatePaths();
  252. }
  253. boost::unique_lock<boost::mutex> pathLock(cl->pathMx);
  254. return cl->pathInfo->getPath(dest, ret);
  255. }
  256. void CCallback::recalculatePaths()
  257. {
  258. cl->calculatePaths(cl->IGameCallback::getSelectedHero(player));
  259. }
  260. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  261. {
  262. gs->calculatePaths(hero, out, src, movement);
  263. }
  264. void CCallback::dig( const CGObjectInstance *hero )
  265. {
  266. DigWithHero dwh;
  267. dwh.id = hero->id;
  268. sendRequest(&dwh);
  269. }
  270. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  271. {
  272. CastAdvSpell cas;
  273. cas.hid = hero->id;
  274. cas.sid = spellID;
  275. cas.pos = pos;
  276. sendRequest(&cas);
  277. }
  278. void CCallback::unregisterMyInterface()
  279. {
  280. assert(player >= 0); //works only for player callback
  281. cl->playerint.erase(player);
  282. cl->battleints.erase(player);
  283. //TODO? should callback be disabled as well?
  284. }