NetPacksRunner.cpp 10.0 KB

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