NetPacksLib.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #define VCMI_DLL
  2. #include "../lib/NetPacks.h"
  3. #include "../hch/CGeneralTextHandler.h"
  4. #include "../hch/CDefObjInfoHandler.h"
  5. #include "../hch/CArtHandler.h"
  6. #include "../hch/CHeroHandler.h"
  7. #include "../hch/CObjectHandler.h"
  8. #include "../lib/VCMI_Lib.h"
  9. #include "../map.h"
  10. #include "../hch/CSpellHandler.h"
  11. #include <boost/bind.hpp>
  12. #include <boost/foreach.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/algorithm/string/replace.hpp>
  15. #include <boost/thread.hpp>
  16. #include <boost/thread/shared_mutex.hpp>
  17. /*
  18. * NetPacksLib.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. DLL_EXPORT void SetResource::applyGs( CGameState *gs )
  27. {
  28. gs->getPlayer(player)->resources[resid] = val;
  29. }
  30. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  31. {
  32. for(int i=0;i<res.size();i++)
  33. gs->getPlayer(player)->resources[i] = res[i];
  34. }
  35. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  36. {
  37. CGHeroInstance *hero = gs->getHero(id);
  38. if(which <4)
  39. {
  40. if(abs)
  41. hero->primSkills[which] = val;
  42. else
  43. hero->primSkills[which] += val;
  44. }
  45. else if(which == 4) //XP
  46. {
  47. if(abs)
  48. hero->exp = val;
  49. else
  50. hero->exp += val;
  51. }
  52. }
  53. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  54. {
  55. CGHeroInstance *hero = gs->getHero(id);
  56. if(hero->getSecSkillLevel(which) == 0)
  57. {
  58. hero->secSkills.push_back(std::pair<int,int>(which, val));
  59. }
  60. else
  61. {
  62. for(unsigned i=0;i<hero->secSkills.size();i++)
  63. {
  64. if(hero->secSkills[i].first == which)
  65. {
  66. if(abs)
  67. hero->secSkills[i].second = val;
  68. else
  69. hero->secSkills[i].second += val;
  70. }
  71. }
  72. }
  73. }
  74. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  75. {
  76. CGHeroInstance *h = gs->getHero(hid);
  77. CGTownInstance *t = gs->getTown(tid);
  78. if(start())
  79. {
  80. if(garrison())
  81. {
  82. t->garrisonHero = h;
  83. h->visitedTown = t;
  84. h->inTownGarrison = true;
  85. }
  86. else
  87. {
  88. t->visitingHero = h;
  89. h->visitedTown = t;
  90. h->inTownGarrison = false;
  91. }
  92. }
  93. else
  94. {
  95. if(garrison())
  96. {
  97. t->garrisonHero = NULL;
  98. h->visitedTown = NULL;
  99. h->inTownGarrison = false;
  100. }
  101. else
  102. {
  103. t->visitingHero = NULL;
  104. h->visitedTown = NULL;
  105. h->inTownGarrison = false;
  106. }
  107. }
  108. }
  109. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  110. {
  111. CGHeroInstance *hero = gs->getHero(hid);
  112. if(learn)
  113. BOOST_FOREACH(ui32 sid, spells)
  114. hero->spells.insert(sid);
  115. else
  116. BOOST_FOREACH(ui32 sid, spells)
  117. hero->spells.erase(sid);
  118. }
  119. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  120. {
  121. CGHeroInstance *hero = gs->getHero(hid);
  122. hero->mana = val;
  123. }
  124. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  125. {
  126. CGHeroInstance *hero = gs->getHero(hid);
  127. hero->movement = val;
  128. }
  129. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  130. {
  131. BOOST_FOREACH(int3 t, tiles)
  132. gs->getPlayer(player)->fogOfWarMap[t.x][t.y][t.z] = mode;
  133. }
  134. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  135. {
  136. gs->getPlayer(player)->availableHeroes.clear();
  137. CGHeroInstance *h = (hid1>=0 ? gs->hpool.heroesPool[hid1] : NULL);
  138. gs->getPlayer(player)->availableHeroes.push_back(h);
  139. if(h && flags & 1)
  140. {
  141. h->army.slots.clear();
  142. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  143. }
  144. h = (hid2>=0 ? gs->hpool.heroesPool[hid2] : NULL);
  145. gs->getPlayer(player)->availableHeroes.push_back(h);
  146. if(flags & 2)
  147. {
  148. h->army.slots.clear();
  149. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  150. }
  151. }
  152. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  153. {
  154. CGHeroInstance *h = gs->getHero(hid);
  155. h->bonuses.push_back(bonus);
  156. std::string &descr = h->bonuses.back().description;
  157. if(!bdescr.texts.size()
  158. && bonus.source == HeroBonus::OBJECT
  159. && (bonus.type == HeroBonus::LUCK || bonus.type == HeroBonus::MORALE || bonus.type == HeroBonus::MORALE_AND_LUCK)
  160. && gs->map->objects[bonus.id]->ID == 26) //it's morale/luck bonus from an event without description
  161. {
  162. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  163. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  164. }
  165. else
  166. {
  167. descr = toString(bdescr);
  168. }
  169. }
  170. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  171. {
  172. CGObjectInstance *obj = gs->map->objects[objid];
  173. if(!obj)
  174. {
  175. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  176. return;
  177. }
  178. gs->map->removeBlockVisTiles(obj);
  179. obj->pos = nPos;
  180. gs->map->addBlockVisTiles(obj);
  181. }
  182. DLL_EXPORT void RemoveObject::applyGs( CGameState *gs )
  183. {
  184. CGObjectInstance *obj = gs->map->objects[id];
  185. if(obj->ID==HEROI_TYPE)
  186. {
  187. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  188. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  189. gs->map->heroes.erase(nitr);
  190. int player = h->tempOwner;
  191. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  192. gs->getPlayer(player)->heroes.erase(nitr);
  193. if(h->visitedTown)
  194. {
  195. if(h->inTownGarrison)
  196. h->visitedTown->garrisonHero = NULL;
  197. else
  198. h->visitedTown->visitingHero = NULL;
  199. h->visitedTown = NULL;
  200. }
  201. //TODO: add to the pool?
  202. }
  203. gs->map->objects[id] = NULL;
  204. //unblock tiles
  205. if(obj->defInfo)
  206. {
  207. gs->map->removeBlockVisTiles(obj);
  208. }
  209. }
  210. void TryMoveHero::applyGs( CGameState *gs )
  211. {
  212. CGHeroInstance *h = gs->getHero(id);
  213. h->movement = movePoints;
  214. if(start!=end && result)
  215. {
  216. gs->map->removeBlockVisTiles(h);
  217. h->pos = end;
  218. gs->map->addBlockVisTiles(h);
  219. }
  220. BOOST_FOREACH(int3 t, fowRevealed)
  221. gs->getPlayer(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  222. }
  223. DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  224. {
  225. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  226. {
  227. CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  228. ai->army = i->second;
  229. if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  230. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->army = i->second;
  231. else if(ai->ID==HEROI_TYPE)
  232. {
  233. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  234. if(h->visitedTown && h->inTownGarrison)
  235. h->visitedTown->army = i->second;
  236. }
  237. }
  238. }
  239. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  240. {
  241. CGTownInstance*t = gs->getTown(tid);
  242. BOOST_FOREACH(si32 id,bid)
  243. t->builtBuildings.insert(id);
  244. t->builded = builded;
  245. }
  246. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  247. {
  248. gs->getTown(tid)->strInfo.creatures = creatures;
  249. }
  250. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  251. {
  252. CGTownInstance *t = gs->getTown(tid);
  253. CGHeroInstance *v = gs->getHero(visiting),
  254. *g = gs->getHero(garrison);
  255. t->visitingHero = v;
  256. t->garrisonHero = g;
  257. if(v)
  258. {
  259. v->visitedTown = t;
  260. v->inTownGarrison = false;
  261. gs->map->addBlockVisTiles(v);
  262. }
  263. if(g)
  264. {
  265. g->visitedTown = t;
  266. g->inTownGarrison = true;
  267. gs->map->removeBlockVisTiles(g);
  268. }
  269. }
  270. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  271. {
  272. CGHeroInstance *h = gs->getHero(hid);
  273. std::vector<ui32> equiped, unequiped;
  274. for(std::map<ui16,ui32>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  275. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  276. unequiped.push_back(i->second);
  277. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  278. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  279. equiped.push_back(i->second);
  280. h->artifacts = artifacts;
  281. h->artifWorn = artifWorn;
  282. BOOST_FOREACH(ui32 id, unequiped)
  283. {
  284. while(1)
  285. {
  286. std::list<HeroBonus>::iterator hlp = std::find_if(h->bonuses.begin(),h->bonuses.end(),boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,id));
  287. if(hlp != h->bonuses.end())
  288. {
  289. lost.push_back(&*hlp);
  290. h->bonuses.erase(hlp);
  291. }
  292. else
  293. {
  294. break;
  295. }
  296. }
  297. }
  298. BOOST_FOREACH(ui32 id, equiped)
  299. {
  300. CArtifact &art = VLC->arth->artifacts[id];
  301. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  302. {
  303. gained.push_back(&*i);
  304. h->bonuses.push_back(*i);
  305. }
  306. }
  307. }
  308. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  309. {
  310. if(art<0)
  311. {
  312. if(pos<19)
  313. artifWorn.erase(pos);
  314. else
  315. artifacts -= artifacts[pos-19];
  316. }
  317. else
  318. {
  319. if(pos<19)
  320. artifWorn[pos] = art;
  321. else
  322. if(pos-19 < artifacts.size())
  323. artifacts[pos-19] = art;
  324. else
  325. artifacts.push_back(art);
  326. }
  327. }
  328. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  329. {
  330. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  331. CGTownInstance *t = gs->getTown(tid);
  332. h->setOwner(player);
  333. h->pos = tile;
  334. h->movement = h->maxMovePoints(true);
  335. gs->hpool.heroesPool.erase(hid);
  336. if(h->id < 0)
  337. {
  338. h->id = gs->map->objects.size();
  339. gs->map->objects.push_back(h);
  340. }
  341. else
  342. gs->map->objects[h->id] = h;
  343. h->initHeroDefInfo();
  344. gs->map->heroes.push_back(h);
  345. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  346. gs->map->addBlockVisTiles(h);
  347. t->visitingHero = h;
  348. h->visitedTown = t;
  349. h->inTownGarrison = false;
  350. }
  351. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  352. {
  353. CGHeroInstance *h = gs->getHero(id);
  354. gs->map->removeBlockVisTiles(h,true);
  355. h->setOwner(player);
  356. h->movement = h->maxMovePoints(true);
  357. h->initHeroDefInfo();
  358. gs->map->heroes.push_back(h);
  359. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  360. gs->map->addBlockVisTiles(h);
  361. h->inTownGarrison = false;
  362. }
  363. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  364. {
  365. gs->day = day;
  366. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  367. {
  368. CGHeroInstance *hero = gs->getHero(h.id);
  369. hero->movement = h.move;
  370. hero->mana = h.mana;
  371. }
  372. BOOST_FOREACH(SetResources h, res) //give resources
  373. h.applyGs(gs);
  374. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  375. h.applyGs(gs);
  376. if(resetBuilded) //reset amount of structures set in this turn in towns
  377. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  378. t->builded = 0;
  379. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  380. h->bonuses.remove_if(HeroBonus::OneDay);
  381. if(gs->getDate(1) == 7) //new week
  382. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  383. h->bonuses.remove_if(HeroBonus::OneWeek);
  384. }
  385. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  386. {
  387. CGObjectInstance *obj = gs->map->objects[id];
  388. if(!obj)
  389. tlog1 << "Wrong object ID - property cannot be set!\n";
  390. else
  391. obj->setProperty(what,val);
  392. }
  393. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  394. {
  395. gs->map->objects[id]->hoverName = toString(name);
  396. }
  397. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  398. {
  399. gs->getHero(heroid)->level = level;
  400. }
  401. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  402. {
  403. gs->curB = info;
  404. }
  405. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  406. {
  407. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  408. gs->curB->round = round;
  409. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  410. {
  411. s->state -= DEFENDING;
  412. s->state -= WAITING;
  413. s->state -= MOVED;
  414. s->state -= HAD_MORALE;
  415. s->counterAttacks = 1;
  416. //remove effects and restore only those with remaining turns in duration
  417. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  418. s->effects.clear();
  419. for(int i=0; i < tmpEffects.size(); i++)
  420. {
  421. tmpEffects[i].turnsRemain--;
  422. if(tmpEffects[i].turnsRemain > 0)
  423. s->effects.push_back(tmpEffects[i]);
  424. }
  425. }
  426. }
  427. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  428. {
  429. gs->curB->activeStack = stack;
  430. CStack *st = gs->curB->getStack(stack);
  431. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  432. st->state.insert(HAD_MORALE);
  433. }
  434. void BattleResult::applyGs( CGameState *gs )
  435. {
  436. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  437. delete gs->curB->stacks[i];
  438. //remove any "until next battle" bonuses
  439. CGHeroInstance *h;
  440. h = gs->getHero(gs->curB->hero1);
  441. if(h)
  442. h->bonuses.remove_if(HeroBonus::OneBattle);
  443. h = gs->getHero(gs->curB->hero2);
  444. if(h)
  445. h->bonuses.remove_if(HeroBonus::OneBattle);
  446. delete gs->curB;
  447. gs->curB = NULL;
  448. }
  449. void BattleStackMoved::applyGs( CGameState *gs )
  450. {
  451. gs->curB->getStack(stack)->position = tile;
  452. }
  453. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  454. {
  455. CStack * at = gs->curB->getStack(stackAttacked);
  456. at->amount = newAmount;
  457. at->firstHPleft = newHP;
  458. if(killed())
  459. at->state -= ALIVE;
  460. }
  461. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  462. {
  463. CStack *attacker = gs->curB->getStack(stackAttacking);
  464. if(counter())
  465. attacker->counterAttacks--;
  466. if(shot())
  467. attacker->shots--;
  468. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  469. stackAttacked.applyGs(gs);
  470. }
  471. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  472. {
  473. CStack *st = gs->curB->getStack(ba.stackNumber);
  474. switch(ba.actionType)
  475. {
  476. case 3:
  477. st->state.insert(DEFENDING);
  478. break;
  479. case 8:
  480. st->state.insert(WAITING);
  481. break;
  482. case 2: case 6: case 7: case 9: case 10: case 11:
  483. st->state.insert(MOVED);
  484. break;
  485. }
  486. }
  487. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  488. {
  489. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  490. if(h)
  491. {
  492. h->mana -= VLC->spellh->spells[id].costs[skill];
  493. if(h->mana < 0) h->mana = 0;
  494. }
  495. if(side >= 0 && side < 2)
  496. {
  497. gs->curB->castSpells[side]++;
  498. }
  499. if(gs->curB && id == 35) //dispel
  500. {
  501. CStack *s = gs->curB->getStackT(tile);
  502. if(s)
  503. {
  504. s->effects.clear(); //removing all effects
  505. }
  506. }
  507. }
  508. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  509. {
  510. BOOST_FOREACH(ui32 id, stacks)
  511. {
  512. CStack *s = gs->curB->getStack(id);
  513. if(s)
  514. {
  515. s->effects.push_back(effect); //adding effect
  516. }
  517. else
  518. tlog1 << "Cannot find stack " << id << std::endl;
  519. }
  520. }
  521. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  522. {
  523. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  524. stackAttacked.applyGs(gs);
  525. }
  526. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  527. {
  528. gs->currentPlayer = player;
  529. }
  530. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  531. {
  532. gs->getPlayer(player)->currentSelection = id;
  533. }