NetPacksRunner.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "../lib/NetPacks.h"
  2. #include "Client.h"
  3. #include "../lib/CGameState.h"
  4. #include "../lib/Connection.h"
  5. #include "../lib/CGameInterface.h"
  6. #include <boost/bind.hpp>
  7. #include <boost/foreach.hpp>
  8. #include <boost/thread.hpp>
  9. #include <boost/thread/shared_mutex.hpp>
  10. #include "../lib/BattleState.h"
  11. #include "CheckTime.h"
  12. //macros to avoid code duplication - calls given method with given arguments if interface for specific player is present
  13. //awaiting variadic templates...
  14. #define BATTLE_INTERFACE_CALL_IF_PRESENT(function,...) \
  15. do \
  16. { \
  17. if(cl->ai) \
  18. { \
  19. CheckTime("AI was processing info for "); \
  20. cl->ai->function(__VA_ARGS__); \
  21. } \
  22. } while(0)
  23. #define UNEXPECTED_PACK assert(0)
  24. void SetResources::applyCl( CClient *cl )
  25. {
  26. UNEXPECTED_PACK;
  27. }
  28. void SetResource::applyCl( CClient *cl )
  29. {
  30. UNEXPECTED_PACK;
  31. }
  32. void SetPrimSkill::applyCl( CClient *cl )
  33. {
  34. UNEXPECTED_PACK;
  35. }
  36. void SetSecSkill::applyCl( CClient *cl )
  37. {
  38. UNEXPECTED_PACK;
  39. }
  40. void HeroVisitCastle::applyCl( CClient *cl )
  41. {
  42. UNEXPECTED_PACK;
  43. }
  44. void ChangeSpells::applyCl( CClient *cl )
  45. {
  46. UNEXPECTED_PACK;
  47. }
  48. void SetMana::applyCl( CClient *cl )
  49. {
  50. UNEXPECTED_PACK;
  51. }
  52. void SetMovePoints::applyCl( CClient *cl )
  53. {
  54. UNEXPECTED_PACK;
  55. }
  56. void FoWChange::applyCl( CClient *cl )
  57. {
  58. UNEXPECTED_PACK;
  59. }
  60. void SetAvailableHeroes::applyCl( CClient *cl )
  61. {
  62. UNEXPECTED_PACK;
  63. }
  64. void ChangeStackCount::applyCl( CClient *cl )
  65. {
  66. UNEXPECTED_PACK;
  67. }
  68. void SetStackType::applyCl( CClient *cl )
  69. {
  70. UNEXPECTED_PACK;
  71. }
  72. void EraseStack::applyCl( CClient *cl )
  73. {
  74. UNEXPECTED_PACK;
  75. }
  76. void SwapStacks::applyCl( CClient *cl )
  77. {
  78. UNEXPECTED_PACK;
  79. }
  80. void InsertNewStack::applyCl( CClient *cl )
  81. {
  82. UNEXPECTED_PACK;
  83. }
  84. void RebalanceStacks::applyCl( CClient *cl )
  85. {
  86. UNEXPECTED_PACK;
  87. }
  88. void PutArtifact::applyCl( CClient *cl )
  89. {
  90. UNEXPECTED_PACK;
  91. }
  92. void EraseArtifact::applyCl( CClient *cl )
  93. {
  94. UNEXPECTED_PACK;
  95. }
  96. void MoveArtifact::applyCl( CClient *cl )
  97. {
  98. UNEXPECTED_PACK;
  99. }
  100. void AssembledArtifact::applyCl( CClient *cl )
  101. {
  102. UNEXPECTED_PACK;
  103. }
  104. void DisassembledArtifact::applyCl( CClient *cl )
  105. {
  106. UNEXPECTED_PACK;
  107. }
  108. void HeroVisit::applyCl( CClient *cl )
  109. {
  110. UNEXPECTED_PACK;
  111. }
  112. void NewTurn::applyCl( CClient *cl )
  113. {
  114. UNEXPECTED_PACK;
  115. }
  116. void GiveBonus::applyCl( CClient *cl )
  117. {
  118. UNEXPECTED_PACK;
  119. }
  120. void ChangeObjPos::applyFirstCl( CClient *cl )
  121. {
  122. UNEXPECTED_PACK;
  123. }
  124. void ChangeObjPos::applyCl( CClient *cl )
  125. {
  126. UNEXPECTED_PACK;
  127. }
  128. void PlayerEndsGame::applyCl( CClient *cl )
  129. {
  130. UNEXPECTED_PACK;
  131. }
  132. void RemoveBonus::applyCl( CClient *cl )
  133. {
  134. UNEXPECTED_PACK;
  135. }
  136. void UpdateCampaignState::applyCl( CClient *cl )
  137. {
  138. UNEXPECTED_PACK;
  139. }
  140. void RemoveObject::applyFirstCl( CClient *cl )
  141. {
  142. UNEXPECTED_PACK;
  143. }
  144. void RemoveObject::applyCl( CClient *cl )
  145. {
  146. UNEXPECTED_PACK;
  147. }
  148. void TryMoveHero::applyFirstCl( CClient *cl )
  149. {
  150. UNEXPECTED_PACK;
  151. }
  152. void TryMoveHero::applyCl( CClient *cl )
  153. {
  154. UNEXPECTED_PACK;
  155. }
  156. void NewStructures::applyCl( CClient *cl )
  157. {
  158. UNEXPECTED_PACK;
  159. }
  160. void RazeStructures::applyCl (CClient *cl)
  161. {
  162. UNEXPECTED_PACK;
  163. }
  164. void SetAvailableCreatures::applyCl( CClient *cl )
  165. {
  166. UNEXPECTED_PACK;
  167. }
  168. void SetHeroesInTown::applyCl( CClient *cl )
  169. {
  170. UNEXPECTED_PACK;
  171. }
  172. void HeroRecruited::applyCl( CClient *cl )
  173. {
  174. UNEXPECTED_PACK;
  175. }
  176. void GiveHero::applyCl( CClient *cl )
  177. {
  178. UNEXPECTED_PACK;
  179. }
  180. void GiveHero::applyFirstCl( CClient *cl )
  181. {
  182. UNEXPECTED_PACK;
  183. }
  184. void InfoWindow::applyCl( CClient *cl )
  185. {
  186. UNEXPECTED_PACK;
  187. }
  188. void SetObjectProperty::applyCl( CClient *cl )
  189. {
  190. UNEXPECTED_PACK;
  191. }
  192. void HeroLevelUp::applyCl( CClient *cl )
  193. {
  194. UNEXPECTED_PACK;
  195. }
  196. void BlockingDialog::applyCl( CClient *cl )
  197. {
  198. UNEXPECTED_PACK;
  199. }
  200. void GarrisonDialog::applyCl(CClient *cl)
  201. {
  202. UNEXPECTED_PACK;
  203. }
  204. void BattleStart::applyCl( CClient *cl )
  205. {
  206. //TODO!!!!
  207. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStart, info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], cl->ai->playerID);
  208. }
  209. void BattleNextRound::applyFirstCl(CClient *cl)
  210. {
  211. BATTLE_INTERFACE_CALL_IF_PRESENT(battleNewRoundFirst,round);
  212. }
  213. void BattleNextRound::applyCl( CClient *cl )
  214. {
  215. BATTLE_INTERFACE_CALL_IF_PRESENT(battleNewRound,round);
  216. }
  217. void BattleSetActiveStack::applyCl( CClient *cl )
  218. {
  219. CStack * activated = GS(cl)->curB->getStack(stack);
  220. int playerToCall = -1; //player that will move activated stack
  221. if( activated->hasBonusOfType(Bonus::HYPNOTIZED))
  222. playerToCall = ( GS(cl)->curB->sides[0] == activated->owner ? GS(cl)->curB->sides[1] : GS(cl)->curB->sides[0] );
  223. else
  224. playerToCall = activated->owner;
  225. if(cl->ai && cl->ai->playerID == playerToCall)
  226. cl->requestMoveFromAI(activated);
  227. }
  228. void BattleResult::applyFirstCl( CClient *cl )
  229. {
  230. BATTLE_INTERFACE_CALL_IF_PRESENT(battleEnd,this);
  231. }
  232. void BattleStackMoved::applyFirstCl( CClient *cl )
  233. {
  234. const CStack * movedStack = GS(cl)->curB->getStack(stack);
  235. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStackMoved,movedStack,tilesToMove,distance);
  236. }
  237. void BattleStackAttacked::applyCl( CClient *cl )
  238. {
  239. std::vector<BattleStackAttacked> bsa;
  240. bsa.push_back(*this);
  241. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStacksAttacked,bsa);
  242. }
  243. void BattleAttack::applyFirstCl( CClient *cl )
  244. {
  245. BATTLE_INTERFACE_CALL_IF_PRESENT(battleAttack,this);
  246. for (int g=0; g<bsa.size(); ++g)
  247. {
  248. for (int z=0; z<bsa[g].healedStacks.size(); ++z)
  249. {
  250. bsa[g].healedStacks[z].applyCl(cl);
  251. }
  252. }
  253. }
  254. void BattleAttack::applyCl( CClient *cl )
  255. {
  256. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStacksAttacked,bsa);
  257. }
  258. void StartAction::applyFirstCl( CClient *cl )
  259. {
  260. cl->curbaction = new BattleAction(ba);
  261. BATTLE_INTERFACE_CALL_IF_PRESENT(actionStarted, &ba);
  262. }
  263. void BattleSpellCast::applyCl( CClient *cl )
  264. {
  265. BATTLE_INTERFACE_CALL_IF_PRESENT(battleSpellCast,this);
  266. if(id >= 66 && id <= 69) //elemental summoning
  267. {
  268. BATTLE_INTERFACE_CALL_IF_PRESENT(battleNewStackAppeared,GS(cl)->curB->stacks.back());
  269. }
  270. }
  271. void SetStackEffect::applyCl( CClient *cl )
  272. {
  273. //informing about effects
  274. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStacksEffectsSet,*this);
  275. }
  276. void StacksInjured::applyCl( CClient *cl )
  277. {
  278. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStacksAttacked,stacks);
  279. }
  280. void BattleResultsApplied::applyCl( CClient *cl )
  281. {
  282. cl->terminate = true;
  283. CloseServer cs;
  284. *cl->serv << &cs;
  285. }
  286. void StacksHealedOrResurrected::applyCl( CClient *cl )
  287. {
  288. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  289. for(int v=0; v<healedStacks.size(); ++v)
  290. {
  291. shiftedHealed.push_back(std::make_pair(healedStacks[v].stackID, healedStacks[v].healedHP));
  292. }
  293. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStacksHealedRes, shiftedHealed, lifeDrain, tentHealing, drainedFrom);
  294. }
  295. void ObstaclesRemoved::applyCl( CClient *cl )
  296. {
  297. //inform interfaces about removed obstacles
  298. BATTLE_INTERFACE_CALL_IF_PRESENT(battleObstaclesRemoved, obstacles);
  299. }
  300. void CatapultAttack::applyCl( CClient *cl )
  301. {
  302. //inform interfaces about catapult attack
  303. BATTLE_INTERFACE_CALL_IF_PRESENT(battleCatapultAttacked, *this);
  304. }
  305. void BattleStacksRemoved::applyCl( CClient *cl )
  306. {
  307. //inform interfaces about removed stacks
  308. BATTLE_INTERFACE_CALL_IF_PRESENT(battleStacksRemoved, *this);
  309. }
  310. CGameState* CPackForClient::GS( CClient *cl )
  311. {
  312. return cl->gs;
  313. }
  314. void EndAction::applyCl( CClient *cl )
  315. {
  316. BATTLE_INTERFACE_CALL_IF_PRESENT(actionFinished, cl->curbaction);
  317. delNull(cl->curbaction);
  318. }
  319. void PackageApplied::applyCl( CClient *cl )
  320. {
  321. ui8 player = GS(cl)->currentPlayer;
  322. //INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  323. if(cl->waitingRequest.get() == packType)
  324. cl->waitingRequest.setn(false);
  325. else if(cl->waitingRequest.get())
  326. tlog3 << "Surprising server message!\n";
  327. }
  328. void SystemMessage::applyCl( CClient *cl )
  329. {
  330. std::ostringstream str;
  331. str << "System message: " << text;
  332. tlog4 << str.str() << std::endl;
  333. }
  334. void PlayerBlocked::applyCl( CClient *cl )
  335. {
  336. UNEXPECTED_PACK;
  337. }
  338. void YourTurn::applyCl( CClient *cl )
  339. {
  340. UNEXPECTED_PACK;
  341. }
  342. void SaveGame::applyCl(CClient *cl)
  343. {
  344. UNEXPECTED_PACK;
  345. }
  346. void PlayerMessage::applyCl(CClient *cl)
  347. {
  348. std::ostringstream str;
  349. str << "Player "<<(int)player<<" sends a message: " << text;
  350. tlog4 << str.str() << std::endl;
  351. }
  352. void SetSelection::applyCl(CClient *cl)
  353. {
  354. UNEXPECTED_PACK;
  355. }
  356. void ShowInInfobox::applyCl(CClient *cl)
  357. {
  358. UNEXPECTED_PACK;
  359. }
  360. void AdvmapSpellCast::applyCl(CClient *cl)
  361. {
  362. UNEXPECTED_PACK;
  363. }
  364. void OpenWindow::applyCl(CClient *cl)
  365. {
  366. UNEXPECTED_PACK;
  367. }
  368. void CenterView::applyCl(CClient *cl)
  369. {
  370. UNEXPECTED_PACK;
  371. }
  372. void NewObject::applyCl(CClient *cl)
  373. {
  374. UNEXPECTED_PACK;
  375. }
  376. void SetAvailableArtifacts::applyCl(CClient *cl)
  377. {
  378. UNEXPECTED_PACK;
  379. }
  380. void TradeComponents::applyCl(CClient *cl)
  381. {
  382. UNEXPECTED_PACK;
  383. }