CCallback.cpp 9.4 KB

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