NetPacksLib.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #define VCMI_DLL
  2. #include "../lib/NetPacks.h"
  3. #include "../hch/CGeneralTextHandler.h"
  4. #include "../hch/CDefObjInfoHandler.h"
  5. #include "../hch/CHeroHandler.h"
  6. #include "../hch/CObjectHandler.h"
  7. #include "../lib/VCMI_Lib.h"
  8. #include "../map.h"
  9. #include "../hch/CSpellHandler.h"
  10. #include <boost/bind.hpp>
  11. #include <boost/foreach.hpp>
  12. #include <boost/thread.hpp>
  13. #include <boost/thread/shared_mutex.hpp>
  14. DLL_EXPORT void SetResource::applyGs( CGameState *gs )
  15. {
  16. gs->getPlayer(player)->resources[resid] = val;
  17. }
  18. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  19. {
  20. for(int i=0;i<res.size();i++)
  21. gs->getPlayer(player)->resources[i] = res[i];
  22. }
  23. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  24. {
  25. CGHeroInstance *hero = gs->getHero(id);
  26. if(which <4)
  27. {
  28. if(abs)
  29. hero->primSkills[which] = val;
  30. else
  31. hero->primSkills[which] += val;
  32. }
  33. else if(which == 4) //XP
  34. {
  35. if(abs)
  36. hero->exp = val;
  37. else
  38. hero->exp += val;
  39. }
  40. }
  41. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  42. {
  43. CGHeroInstance *hero = gs->getHero(id);
  44. if(hero->getSecSkillLevel(which) == 0)
  45. {
  46. hero->secSkills.push_back(std::pair<int,int>(which, val));
  47. }
  48. else
  49. {
  50. for(unsigned i=0;i<hero->secSkills.size();i++)
  51. {
  52. if(hero->secSkills[i].first == which)
  53. {
  54. if(abs)
  55. hero->secSkills[i].second = val;
  56. else
  57. hero->secSkills[i].second += val;
  58. }
  59. }
  60. }
  61. }
  62. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  63. {
  64. CGHeroInstance *h = gs->getHero(hid);
  65. CGTownInstance *t = gs->getTown(tid);
  66. if(start())
  67. {
  68. if(garrison())
  69. {
  70. t->garrisonHero = h;
  71. h->visitedTown = t;
  72. h->inTownGarrison = true;
  73. }
  74. else
  75. {
  76. t->visitingHero = h;
  77. h->visitedTown = t;
  78. h->inTownGarrison = false;
  79. }
  80. }
  81. else
  82. {
  83. if(garrison())
  84. {
  85. t->garrisonHero = NULL;
  86. h->visitedTown = NULL;
  87. h->inTownGarrison = false;
  88. }
  89. else
  90. {
  91. t->visitingHero = NULL;
  92. h->visitedTown = NULL;
  93. h->inTownGarrison = false;
  94. }
  95. }
  96. }
  97. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  98. {
  99. CGHeroInstance *hero = gs->getHero(hid);
  100. if(learn)
  101. BOOST_FOREACH(ui32 sid, spells)
  102. hero->spells.insert(sid);
  103. else
  104. BOOST_FOREACH(ui32 sid, spells)
  105. hero->spells.erase(sid);
  106. }
  107. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  108. {
  109. CGHeroInstance *hero = gs->getHero(hid);
  110. hero->mana = val;
  111. }
  112. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  113. {
  114. CGHeroInstance *hero = gs->getHero(hid);
  115. hero->movement = val;
  116. }
  117. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  118. {
  119. BOOST_FOREACH(int3 t, tiles)
  120. gs->getPlayer(player)->fogOfWarMap[t.x][t.y][t.z] = mode;
  121. }
  122. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  123. {
  124. gs->getPlayer(player)->availableHeroes.clear();
  125. CGHeroInstance *h = (hid1>=0 ? gs->hpool.heroesPool[hid1] : NULL);
  126. gs->getPlayer(player)->availableHeroes.push_back(h);
  127. if(h && flags & 1)
  128. {
  129. h->army.slots.clear();
  130. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  131. }
  132. h = (hid2>=0 ? gs->hpool.heroesPool[hid2] : NULL);
  133. gs->getPlayer(player)->availableHeroes.push_back(h);
  134. if(flags & 2)
  135. {
  136. h->army.slots.clear();
  137. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  138. }
  139. }
  140. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  141. {
  142. CGHeroInstance *h = gs->getHero(hid);
  143. h->bonuses.push_back(bonus);
  144. h->bonuses.back().description = toString(bdescr);
  145. }
  146. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  147. {
  148. CGObjectInstance *obj = gs->map->objects[objid];
  149. if(!obj)
  150. {
  151. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  152. return;
  153. }
  154. gs->map->removeBlockVisTiles(obj);
  155. obj->pos = nPos;
  156. gs->map->addBlockVisTiles(obj);
  157. }
  158. DLL_EXPORT void RemoveObject::applyGs( CGameState *gs )
  159. {
  160. CGObjectInstance *obj = gs->map->objects[id];
  161. if(obj->ID==HEROI_TYPE)
  162. {
  163. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  164. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  165. gs->map->heroes.erase(nitr);
  166. int player = h->tempOwner;
  167. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  168. gs->getPlayer(player)->heroes.erase(nitr);
  169. if(h->visitedTown)
  170. {
  171. if(h->inTownGarrison)
  172. h->visitedTown->garrisonHero = NULL;
  173. else
  174. h->visitedTown->visitingHero = NULL;
  175. h->visitedTown = NULL;
  176. }
  177. //TODO: add to the pool?
  178. }
  179. gs->map->objects[id] = NULL;
  180. //unblock tiles
  181. if(obj->defInfo)
  182. {
  183. gs->map->removeBlockVisTiles(obj);
  184. }
  185. }
  186. void TryMoveHero::applyGs( CGameState *gs )
  187. {
  188. CGHeroInstance *h = gs->getHero(id);
  189. h->movement = movePoints;
  190. if(start!=end && result)
  191. {
  192. gs->map->removeBlockVisTiles(h);
  193. h->pos = end;
  194. gs->map->addBlockVisTiles(h);
  195. }
  196. BOOST_FOREACH(int3 t, fowRevealed)
  197. gs->getPlayer(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  198. }
  199. DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  200. {
  201. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  202. {
  203. CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  204. ai->army = i->second;
  205. if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  206. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->army = i->second;
  207. else if(ai->ID==HEROI_TYPE)
  208. {
  209. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  210. if(h->visitedTown && h->inTownGarrison)
  211. h->visitedTown->army = i->second;
  212. }
  213. }
  214. }
  215. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  216. {
  217. CGTownInstance*t = gs->getTown(tid);
  218. BOOST_FOREACH(si32 id,bid)
  219. t->builtBuildings.insert(id);
  220. t->builded = builded;
  221. }
  222. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  223. {
  224. gs->getTown(tid)->strInfo.creatures = creatures;
  225. }
  226. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  227. {
  228. CGTownInstance *t = gs->getTown(tid);
  229. CGHeroInstance *v = gs->getHero(visiting),
  230. *g = gs->getHero(garrison);
  231. t->visitingHero = v;
  232. t->garrisonHero = g;
  233. if(v)
  234. {
  235. v->visitedTown = t;
  236. v->inTownGarrison = false;
  237. gs->map->addBlockVisTiles(v);
  238. }
  239. if(g)
  240. {
  241. g->visitedTown = t;
  242. g->inTownGarrison = true;
  243. gs->map->removeBlockVisTiles(g);
  244. }
  245. }
  246. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  247. {
  248. CGHeroInstance *h = gs->getHero(hid);
  249. h->artifacts = artifacts;
  250. h->artifWorn = artifWorn;
  251. }
  252. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  253. {
  254. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  255. CGTownInstance *t = gs->getTown(tid);
  256. h->setOwner(player);
  257. h->pos = tile;
  258. h->movement = h->maxMovePoints(true);
  259. gs->hpool.heroesPool.erase(hid);
  260. if(h->id < 0)
  261. {
  262. h->id = gs->map->objects.size();
  263. gs->map->objects.push_back(h);
  264. }
  265. else
  266. gs->map->objects[h->id] = h;
  267. h->initHeroDefInfo();
  268. gs->map->heroes.push_back(h);
  269. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  270. gs->map->addBlockVisTiles(h);
  271. t->visitingHero = h;
  272. h->visitedTown = t;
  273. h->inTownGarrison = false;
  274. }
  275. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  276. {
  277. CGHeroInstance *h = gs->getHero(id);
  278. gs->map->removeBlockVisTiles(h,true);
  279. h->setOwner(player);
  280. h->movement = h->maxMovePoints(true);
  281. h->initHeroDefInfo();
  282. gs->map->heroes.push_back(h);
  283. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  284. gs->map->addBlockVisTiles(h);
  285. h->inTownGarrison = false;
  286. }
  287. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  288. {
  289. gs->day = day;
  290. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  291. {
  292. static_cast<CGHeroInstance*>(gs->map->objects[h.id])->movement = h.move;
  293. static_cast<CGHeroInstance*>(gs->map->objects[h.id])->mana = h.mana;
  294. }
  295. BOOST_FOREACH(SetResources h, res) //give resources
  296. h.applyGs(gs);
  297. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  298. h.applyGs(gs);
  299. if(resetBuilded) //reset amount of structures set in this turn in towns
  300. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  301. t->builded = 0;
  302. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  303. h->bonuses.remove_if(HeroBonus::OneDay);
  304. if(gs->getDate(1) == 7) //new week
  305. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  306. h->bonuses.remove_if(HeroBonus::OneWeek);
  307. }
  308. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  309. {
  310. CGObjectInstance *obj = gs->map->objects[id];
  311. if(!obj)
  312. tlog1 << "Wrong object ID - property cannot be set!\n";
  313. else
  314. obj->setProperty(what,val);
  315. }
  316. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  317. {
  318. gs->map->objects[id]->hoverName = toString(name);
  319. }
  320. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  321. {
  322. gs->getHero(heroid)->level = level;
  323. }
  324. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  325. {
  326. gs->curB = info;
  327. }
  328. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  329. {
  330. gs->curB->castedSpells[0] = gs->curB->castedSpells[1] = 0;
  331. gs->curB->round = round;
  332. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  333. {
  334. s->state -= DEFENDING;
  335. s->state -= WAITING;
  336. s->state -= MOVED;
  337. s->state -= HAD_MORALE;
  338. s->counterAttacks = 1;
  339. }
  340. }
  341. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  342. {
  343. gs->curB->activeStack = stack;
  344. CStack *st = gs->curB->getStack(stack);
  345. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  346. st->state.insert(HAD_MORALE);
  347. }
  348. void BattleResult::applyGs( CGameState *gs )
  349. {
  350. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  351. delete gs->curB->stacks[i];
  352. //remove any "until next battle" bonuses
  353. CGHeroInstance *h;
  354. h = gs->getHero(gs->curB->hero1);
  355. if(h)
  356. h->bonuses.remove_if(HeroBonus::OneBattle);
  357. h = gs->getHero(gs->curB->hero2);
  358. if(h)
  359. h->bonuses.remove_if(HeroBonus::OneBattle);
  360. delete gs->curB;
  361. gs->curB = NULL;
  362. }
  363. void BattleStackMoved::applyGs( CGameState *gs )
  364. {
  365. gs->curB->getStack(stack)->position = tile;
  366. }
  367. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  368. {
  369. CStack * at = gs->curB->getStack(stackAttacked);
  370. at->amount = newAmount;
  371. at->firstHPleft = newHP;
  372. if(killed())
  373. at->state -= ALIVE;
  374. }
  375. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  376. {
  377. CStack *attacker = gs->curB->getStack(stackAttacking);
  378. if(counter())
  379. attacker->counterAttacks--;
  380. if(shot())
  381. attacker->shots--;
  382. bsa.applyGs(gs);
  383. }
  384. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  385. {
  386. CStack *st = gs->curB->getStack(ba.stackNumber);
  387. switch(ba.actionType)
  388. {
  389. case 3:
  390. st->state.insert(DEFENDING);
  391. break;
  392. case 8:
  393. st->state.insert(WAITING);
  394. break;
  395. case 2: case 6: case 7: case 9: case 10: case 11:
  396. st->state.insert(MOVED);
  397. break;
  398. }
  399. }
  400. DLL_EXPORT void SpellCasted::applyGs( CGameState *gs )
  401. {
  402. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  403. if(h)
  404. {
  405. h->mana -= VLC->spellh->spells[id].costs[skill];
  406. if(h->mana < 0) h->mana = 0;
  407. }
  408. if(side >= 0 && side < 2)
  409. {
  410. gs->curB->castedSpells[side]++;
  411. }
  412. }
  413. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  414. {
  415. CStack *s = gs->curB->getStack(stack);
  416. s->effects.push_back(effect);
  417. }
  418. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  419. {
  420. gs->currentPlayer = player;
  421. }
  422. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  423. {
  424. gs->getPlayer(player)->currentSelection = id;
  425. }