NetPacksLib.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. #include "StdInc.h"
  2. #include "NetPacks.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "mapObjects/CObjectClassesHandler.h"
  5. #include "CArtHandler.h"
  6. #include "CHeroHandler.h"
  7. #include "mapObjects/CObjectHandler.h"
  8. #include "CModHandler.h"
  9. #include "VCMI_Lib.h"
  10. #include "mapping/CMap.h"
  11. #include "spells/CSpellHandler.h"
  12. #include "CCreatureHandler.h"
  13. #include "CGameState.h"
  14. #include "CStack.h"
  15. #include "BattleInfo.h"
  16. #include "CTownHandler.h"
  17. #include "mapping/CMapInfo.h"
  18. #include "StartInfo.h"
  19. #include "CPlayerState.h"
  20. /*
  21. * NetPacksLib.cpp, part of VCMI engine
  22. *
  23. * Authors: listed in file AUTHORS in main folder
  24. *
  25. * License: GNU General Public License v2.0 or later
  26. * Full text of license available in license.txt file, in main folder
  27. *
  28. */
  29. #undef min
  30. #undef max
  31. std::ostream & operator<<(std::ostream & out, const CPack * pack)
  32. {
  33. return out << (pack? pack->toString() : "<nullptr>");
  34. }
  35. DLL_LINKAGE void SetResources::applyGs(CGameState *gs)
  36. {
  37. assert(player < PlayerColor::PLAYER_LIMIT);
  38. if(abs)
  39. gs->getPlayer(player)->resources = res;
  40. else
  41. gs->getPlayer(player)->resources += res;
  42. //just ensure that player resources are not negative
  43. //server is responsible to check if player can afford deal
  44. //but events on server side are allowed to take more than player have
  45. gs->getPlayer(player)->resources.positive();
  46. }
  47. DLL_LINKAGE void SetPrimSkill::applyGs(CGameState *gs)
  48. {
  49. CGHeroInstance * hero = gs->getHero(id);
  50. assert(hero);
  51. hero->setPrimarySkill(which, val, abs);
  52. }
  53. DLL_LINKAGE void SetSecSkill::applyGs(CGameState *gs)
  54. {
  55. CGHeroInstance *hero = gs->getHero(id);
  56. hero->setSecSkillLevel(which, val, abs);
  57. }
  58. DLL_LINKAGE SelectMap::SelectMap(const CMapInfo &src)
  59. {
  60. mapInfo = &src;
  61. free = false;
  62. }
  63. DLL_LINKAGE SelectMap::SelectMap()
  64. {
  65. mapInfo = nullptr;
  66. free = true;
  67. }
  68. DLL_LINKAGE SelectMap::~SelectMap()
  69. {
  70. if(free)
  71. delete mapInfo;
  72. }
  73. DLL_LINKAGE UpdateStartOptions::UpdateStartOptions(StartInfo &src)
  74. {
  75. options = &src;
  76. free = false;
  77. }
  78. DLL_LINKAGE UpdateStartOptions::UpdateStartOptions()
  79. {
  80. options = nullptr;
  81. free = true;
  82. }
  83. DLL_LINKAGE UpdateStartOptions::~UpdateStartOptions()
  84. {
  85. if(free)
  86. delete options;
  87. }
  88. DLL_LINKAGE void SetCommanderProperty::applyGs(CGameState *gs)
  89. {
  90. CCommanderInstance * commander = gs->getHero(heroid)->commander;
  91. assert (commander);
  92. switch (which)
  93. {
  94. case BONUS:
  95. commander->accumulateBonus (std::make_shared<Bonus>(accumulatedBonus));
  96. break;
  97. case SPECIAL_SKILL:
  98. commander->accumulateBonus (std::make_shared<Bonus>(accumulatedBonus));
  99. commander->specialSKills.insert (additionalInfo);
  100. break;
  101. case SECONDARY_SKILL:
  102. commander->secondarySkills[additionalInfo] = amount;
  103. break;
  104. case ALIVE:
  105. if (amount)
  106. commander->setAlive(true);
  107. else
  108. commander->setAlive(false);
  109. break;
  110. case EXPERIENCE:
  111. commander->giveStackExp(amount); //TODO: allow setting exp for stacks via netpacks
  112. break;
  113. }
  114. }
  115. DLL_LINKAGE void AddQuest::applyGs(CGameState *gs)
  116. {
  117. assert (vstd::contains(gs->players, player));
  118. auto vec = &gs->players[player].quests;
  119. if (!vstd::contains(*vec, quest))
  120. vec->push_back (quest);
  121. else
  122. logNetwork->warnStream() << "Warning! Attempt to add duplicated quest";
  123. }
  124. DLL_LINKAGE void UpdateArtHandlerLists::applyGs(CGameState *gs)
  125. {
  126. VLC->arth->minors = minors;
  127. VLC->arth->majors = majors;
  128. VLC->arth->treasures = treasures;
  129. VLC->arth->relics = relics;
  130. }
  131. DLL_LINKAGE void UpdateMapEvents::applyGs(CGameState *gs)
  132. {
  133. gs->map->events = events;
  134. }
  135. DLL_LINKAGE void UpdateCastleEvents::applyGs(CGameState *gs)
  136. {
  137. auto t = gs->getTown(town);
  138. t->events = events;
  139. }
  140. DLL_LINKAGE void ChangeFormation::applyGs(CGameState *gs)
  141. {
  142. gs->getHero(hid)->setFormation(formation);
  143. }
  144. DLL_LINKAGE void HeroVisitCastle::applyGs(CGameState *gs)
  145. {
  146. CGHeroInstance *h = gs->getHero(hid);
  147. CGTownInstance *t = gs->getTown(tid);
  148. assert(h);
  149. assert(t);
  150. if(start())
  151. t->setVisitingHero(h);
  152. else
  153. t->setVisitingHero(nullptr);
  154. }
  155. DLL_LINKAGE void ChangeSpells::applyGs(CGameState *gs)
  156. {
  157. CGHeroInstance *hero = gs->getHero(hid);
  158. if(learn)
  159. for(auto sid : spells)
  160. hero->spells.insert(sid);
  161. else
  162. for(auto sid : spells)
  163. hero->spells.erase(sid);
  164. }
  165. DLL_LINKAGE void SetMana::applyGs(CGameState *gs)
  166. {
  167. CGHeroInstance * hero = gs->getHero(hid);
  168. assert(hero);
  169. if(absolute)
  170. hero->mana = val;
  171. else
  172. hero->mana += val;
  173. vstd::amax(hero->mana, 0); //not less than 0
  174. }
  175. DLL_LINKAGE void SetMovePoints::applyGs(CGameState *gs)
  176. {
  177. CGHeroInstance *hero = gs->getHero(hid);
  178. assert(hero);
  179. if(absolute)
  180. hero->movement = val;
  181. else
  182. hero->movement += val;
  183. vstd::amax(hero->movement, 0); //not less than 0
  184. }
  185. DLL_LINKAGE void FoWChange::applyGs(CGameState *gs)
  186. {
  187. TeamState * team = gs->getPlayerTeam(player);
  188. for(int3 t : tiles)
  189. team->fogOfWarMap[t.x][t.y][t.z] = mode;
  190. if (mode == 0) //do not hide too much
  191. {
  192. std::unordered_set<int3, ShashInt3> tilesRevealed;
  193. for (auto & elem : gs->map->objects)
  194. {
  195. const CGObjectInstance *o = elem;
  196. if (o)
  197. {
  198. switch(o->ID)
  199. {
  200. case Obj::HERO:
  201. case Obj::MINE:
  202. case Obj::TOWN:
  203. case Obj::ABANDONED_MINE:
  204. if(vstd::contains(team->players, o->tempOwner)) //check owned observators
  205. gs->getTilesInRange(tilesRevealed, o->getSightCenter(), o->getSightRadius(), o->tempOwner, 1);
  206. break;
  207. }
  208. }
  209. }
  210. for(int3 t : tilesRevealed) //probably not the most optimal solution ever
  211. team->fogOfWarMap[t.x][t.y][t.z] = 1;
  212. }
  213. }
  214. DLL_LINKAGE void SetAvailableHeroes::applyGs(CGameState *gs)
  215. {
  216. PlayerState *p = gs->getPlayer(player);
  217. p->availableHeroes.clear();
  218. for (int i = 0; i < GameConstants::AVAILABLE_HEROES_PER_PLAYER; i++)
  219. {
  220. CGHeroInstance *h = (hid[i]>=0 ? gs->hpool.heroesPool[hid[i]].get() : nullptr);
  221. if(h && army[i])
  222. h->setToArmy(army[i]);
  223. p->availableHeroes.push_back(h);
  224. }
  225. }
  226. DLL_LINKAGE void GiveBonus::applyGs(CGameState *gs)
  227. {
  228. CBonusSystemNode *cbsn = nullptr;
  229. switch(who)
  230. {
  231. case HERO:
  232. cbsn = gs->getHero(ObjectInstanceID(id));
  233. break;
  234. case PLAYER:
  235. cbsn = gs->getPlayer(PlayerColor(id));
  236. break;
  237. case TOWN:
  238. cbsn = gs->getTown(ObjectInstanceID(id));
  239. break;
  240. }
  241. assert(cbsn);
  242. if(Bonus::OneWeek(&bonus))
  243. bonus.turnsRemain = 8 - gs->getDate(Date::DAY_OF_WEEK); // set correct number of days before adding bonus
  244. auto b = std::make_shared<Bonus>(bonus);
  245. cbsn->addNewBonus(b);
  246. std::string &descr = b->description;
  247. if(!bdescr.message.size()
  248. && bonus.source == Bonus::OBJECT
  249. && (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE))
  250. {
  251. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  252. }
  253. else
  254. {
  255. bdescr.toString(descr);
  256. }
  257. // Some of(?) versions of H3 use %s here instead of %d. Try to replace both of them
  258. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  259. boost::replace_first(descr,"%s",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  260. }
  261. DLL_LINKAGE void ChangeObjPos::applyGs(CGameState *gs)
  262. {
  263. CGObjectInstance *obj = gs->getObjInstance(objid);
  264. if(!obj)
  265. {
  266. logNetwork->errorStream() << "Wrong ChangeObjPos: object " << objid.getNum() << " doesn't exist!";
  267. return;
  268. }
  269. gs->map->removeBlockVisTiles(obj);
  270. obj->pos = nPos;
  271. gs->map->addBlockVisTiles(obj);
  272. }
  273. DLL_LINKAGE void ChangeObjectVisitors::applyGs(CGameState *gs)
  274. {
  275. switch (mode) {
  276. case VISITOR_ADD:
  277. gs->getHero(hero)->visitedObjects.insert(object);
  278. gs->getPlayer(gs->getHero(hero)->tempOwner)->visitedObjects.insert(object);
  279. break;
  280. case VISITOR_ADD_TEAM:
  281. {
  282. TeamState *ts = gs->getPlayerTeam(gs->getHero(hero)->tempOwner);
  283. for (auto & color : ts->players)
  284. {
  285. gs->getPlayer(color)->visitedObjects.insert(object);
  286. }
  287. }
  288. break;
  289. case VISITOR_CLEAR:
  290. for (CGHeroInstance * hero : gs->map->allHeroes)
  291. {
  292. if (hero)
  293. {
  294. hero->visitedObjects.erase(object); // remove visit info from all heroes, including those that are not present on map
  295. }
  296. }
  297. for(auto &elem : gs->players)
  298. {
  299. elem.second.visitedObjects.erase(object);
  300. }
  301. break;
  302. case VISITOR_REMOVE:
  303. gs->getHero(hero)->visitedObjects.erase(object);
  304. break;
  305. }
  306. }
  307. DLL_LINKAGE void PlayerEndsGame::applyGs(CGameState *gs)
  308. {
  309. PlayerState *p = gs->getPlayer(player);
  310. if(victoryLossCheckResult.victory()) p->status = EPlayerStatus::WINNER;
  311. else p->status = EPlayerStatus::LOSER;
  312. }
  313. DLL_LINKAGE void RemoveBonus::applyGs(CGameState *gs)
  314. {
  315. CBonusSystemNode *node;
  316. if (who == HERO)
  317. node = gs->getHero(ObjectInstanceID(whoID));
  318. else
  319. node = gs->getPlayer(PlayerColor(whoID));
  320. BonusList &bonuses = node->getExportedBonusList();
  321. for (int i = 0; i < bonuses.size(); i++)
  322. {
  323. auto b = bonuses[i];
  324. if(b->source == source && b->sid == id)
  325. {
  326. bonus = *b; //backup bonus (to show to interfaces later)
  327. node->removeBonus(b);
  328. break;
  329. }
  330. }
  331. }
  332. DLL_LINKAGE void RemoveObject::applyGs(CGameState *gs)
  333. {
  334. CGObjectInstance *obj = gs->getObjInstance(id);
  335. logGlobal->debugStream() << "removing object id=" << id << "; address=" << (intptr_t)obj << "; name=" << obj->getObjectName();
  336. //unblock tiles
  337. gs->map->removeBlockVisTiles(obj);
  338. if(obj->ID==Obj::HERO)
  339. {
  340. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  341. PlayerState *p = gs->getPlayer(h->tempOwner);
  342. gs->map->heroesOnMap -= h;
  343. p->heroes -= h;
  344. h->detachFrom(h->whereShouldBeAttached(gs));
  345. h->tempOwner = PlayerColor::NEUTRAL; //no one owns beaten hero
  346. vstd::erase_if(h->artifactsInBackpack, [](const ArtSlotInfo& asi)
  347. {
  348. return asi.artifact->artType->id == ArtifactID::GRAIL;
  349. });
  350. if(h->visitedTown)
  351. {
  352. if(h->inTownGarrison)
  353. h->visitedTown->garrisonHero = nullptr;
  354. else
  355. h->visitedTown->visitingHero = nullptr;
  356. h->visitedTown = nullptr;
  357. }
  358. //return hero to the pool, so he may reappear in tavern
  359. gs->hpool.heroesPool[h->subID] = h;
  360. if(!vstd::contains(gs->hpool.pavailable, h->subID))
  361. gs->hpool.pavailable[h->subID] = 0xff;
  362. gs->map->objects[id.getNum()] = nullptr;
  363. //If hero on Boat is removed, the Boat disappears
  364. if(h->boat)
  365. {
  366. gs->map->instanceNames.erase(h->boat->instanceName);
  367. gs->map->objects[h->boat->id.getNum()].dellNull();
  368. h->boat = nullptr;
  369. }
  370. return;
  371. }
  372. auto quest = dynamic_cast<const IQuestObject *>(obj);
  373. if (quest)
  374. {
  375. gs->map->quests[quest->quest->qid] = nullptr;
  376. for (auto &player : gs->players)
  377. {
  378. for (auto &q : player.second.quests)
  379. {
  380. if (q.obj == obj)
  381. {
  382. q.obj = nullptr;
  383. }
  384. }
  385. }
  386. }
  387. for (TriggeredEvent & event : gs->map->triggeredEvents)
  388. {
  389. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  390. {
  391. if (cond.object == obj)
  392. {
  393. if (cond.condition == EventCondition::DESTROY || cond.condition == EventCondition::DESTROY_0)
  394. {
  395. cond.condition = EventCondition::CONST_VALUE;
  396. cond.value = 1; // destroyed object, from now on always fulfilled
  397. }
  398. else if (cond.condition == EventCondition::CONTROL || cond.condition == EventCondition::HAVE_0)
  399. {
  400. cond.condition = EventCondition::CONST_VALUE;
  401. cond.value = 0; // destroyed object, from now on can not be fulfilled
  402. }
  403. }
  404. return cond;
  405. };
  406. event.trigger = event.trigger.morph(patcher);
  407. }
  408. gs->map->instanceNames.erase(obj->instanceName);
  409. gs->map->objects[id.getNum()].dellNull();
  410. gs->map->calculateGuardingGreaturePositions();
  411. }
  412. static int getDir(int3 src, int3 dst)
  413. {
  414. int ret = -1;
  415. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  416. {
  417. ret = 1;
  418. }
  419. else if(dst.x == src.x && dst.y+1 == src.y) //t
  420. {
  421. ret = 2;
  422. }
  423. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  424. {
  425. ret = 3;
  426. }
  427. else if(dst.x-1 == src.x && dst.y == src.y) //r
  428. {
  429. ret = 4;
  430. }
  431. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  432. {
  433. ret = 5;
  434. }
  435. else if(dst.x == src.x && dst.y-1 == src.y) //b
  436. {
  437. ret = 6;
  438. }
  439. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  440. {
  441. ret = 7;
  442. }
  443. else if(dst.x+1 == src.x && dst.y == src.y) //l
  444. {
  445. ret = 8;
  446. }
  447. return ret;
  448. }
  449. void TryMoveHero::applyGs(CGameState *gs)
  450. {
  451. CGHeroInstance *h = gs->getHero(id);
  452. if (!h)
  453. {
  454. logGlobal->errorStream() << "Attempt ot move unavailable hero " << id;
  455. return;
  456. }
  457. h->movement = movePoints;
  458. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end)
  459. {
  460. auto dir = getDir(start,end);
  461. if(dir > 0 && dir <= 8)
  462. h->moveDir = dir;
  463. //else don`t change move direction - hero might have traversed the subterranean gate, direction should be kept
  464. }
  465. if(result == EMBARK) //hero enters boat at destination tile
  466. {
  467. const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
  468. assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == Obj::BOAT); //the only visitable object at destination is Boat
  469. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.back());
  470. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  471. h->boat = boat;
  472. boat->hero = h;
  473. }
  474. else if(result == DISEMBARK) //hero leaves boat to destination tile
  475. {
  476. CGBoat *b = const_cast<CGBoat *>(h->boat);
  477. b->direction = h->moveDir;
  478. b->pos = start;
  479. b->hero = nullptr;
  480. gs->map->addBlockVisTiles(b);
  481. h->boat = nullptr;
  482. }
  483. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  484. {
  485. gs->map->removeBlockVisTiles(h);
  486. h->pos = end;
  487. if(CGBoat *b = const_cast<CGBoat *>(h->boat))
  488. b->pos = end;
  489. gs->map->addBlockVisTiles(h);
  490. }
  491. for(int3 t : fowRevealed)
  492. gs->getPlayerTeam(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  493. }
  494. DLL_LINKAGE void NewStructures::applyGs(CGameState *gs)
  495. {
  496. CGTownInstance *t = gs->getTown(tid);
  497. for(const auto & id : bid)
  498. {
  499. assert(t->town->buildings.at(id) != nullptr);
  500. t->builtBuildings.insert(id);
  501. t->updateAppearance();
  502. }
  503. t->builded = builded;
  504. t->recreateBuildingsBonuses();
  505. }
  506. DLL_LINKAGE void RazeStructures::applyGs(CGameState *gs)
  507. {
  508. CGTownInstance *t = gs->getTown(tid);
  509. for(const auto & id : bid)
  510. {
  511. t->builtBuildings.erase(id);
  512. t->updateAppearance();
  513. }
  514. t->destroyed = destroyed; //yeaha
  515. t->recreateBuildingsBonuses();
  516. }
  517. DLL_LINKAGE void SetAvailableCreatures::applyGs(CGameState *gs)
  518. {
  519. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->getObjInstance(tid));
  520. assert(dw);
  521. dw->creatures = creatures;
  522. }
  523. DLL_LINKAGE void SetHeroesInTown::applyGs(CGameState *gs)
  524. {
  525. CGTownInstance *t = gs->getTown(tid);
  526. CGHeroInstance *v = gs->getHero(visiting),
  527. *g = gs->getHero(garrison);
  528. bool newVisitorComesFromGarrison = v && v == t->garrisonHero;
  529. bool newGarrisonComesFromVisiting = g && g == t->visitingHero;
  530. if(newVisitorComesFromGarrison)
  531. t->setGarrisonedHero(nullptr);
  532. if(newGarrisonComesFromVisiting)
  533. t->setVisitingHero(nullptr);
  534. if(!newGarrisonComesFromVisiting || v)
  535. t->setVisitingHero(v);
  536. if(!newVisitorComesFromGarrison || g)
  537. t->setGarrisonedHero(g);
  538. if(v)
  539. {
  540. gs->map->addBlockVisTiles(v);
  541. }
  542. if(g)
  543. {
  544. gs->map->removeBlockVisTiles(g);
  545. }
  546. }
  547. DLL_LINKAGE void HeroRecruited::applyGs(CGameState *gs)
  548. {
  549. assert(vstd::contains(gs->hpool.heroesPool, hid));
  550. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  551. CGTownInstance *t = gs->getTown(tid);
  552. PlayerState *p = gs->getPlayer(player);
  553. assert(!h->boat);
  554. h->setOwner(player);
  555. h->pos = tile;
  556. bool fresh = !h->isInitialized();
  557. if(fresh)
  558. { // this is a fresh hero who hasn't appeared yet
  559. h->movement = h->maxMovePoints(true);
  560. }
  561. gs->hpool.heroesPool.erase(hid);
  562. if(h->id == ObjectInstanceID())
  563. {
  564. h->id = ObjectInstanceID(gs->map->objects.size());
  565. gs->map->objects.push_back(h);
  566. }
  567. else
  568. gs->map->objects[h->id.getNum()] = h;
  569. gs->map->heroesOnMap.push_back(h);
  570. p->heroes.push_back(h);
  571. h->attachTo(p);
  572. if(fresh)
  573. {
  574. h->initObj(gs->getRandomGenerator());
  575. }
  576. gs->map->addBlockVisTiles(h);
  577. if(t)
  578. {
  579. t->setVisitingHero(h);
  580. }
  581. }
  582. DLL_LINKAGE void GiveHero::applyGs(CGameState *gs)
  583. {
  584. CGHeroInstance *h = gs->getHero(id);
  585. //bonus system
  586. h->detachFrom(&gs->globalEffects);
  587. h->attachTo(gs->getPlayer(player));
  588. h->appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, h->type->heroClass->id)->getTemplates().front();
  589. gs->map->removeBlockVisTiles(h,true);
  590. h->setOwner(player);
  591. h->movement = h->maxMovePoints(true);
  592. gs->map->heroesOnMap.push_back(h);
  593. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  594. gs->map->addBlockVisTiles(h);
  595. h->inTownGarrison = false;
  596. }
  597. DLL_LINKAGE void NewObject::applyGs(CGameState *gs)
  598. {
  599. const TerrainTile &t = gs->map->getTile(pos);
  600. ETerrainType terrainType = t.terType;
  601. CGObjectInstance *o = nullptr;
  602. switch(ID)
  603. {
  604. case Obj::BOAT:
  605. o = new CGBoat();
  606. terrainType = ETerrainType::WATER; //TODO: either boat should only spawn on water, or all water objects should be handled this way
  607. break;
  608. case Obj::MONSTER: //probably more options will be needed
  609. o = new CGCreature();
  610. {
  611. //CStackInstance hlp;
  612. CGCreature *cre = static_cast<CGCreature*>(o);
  613. //cre->slots[0] = hlp;
  614. cre->notGrowingTeam = cre->neverFlees = 0;
  615. cre->character = 2;
  616. cre->gainedArtifact = ArtifactID::NONE;
  617. cre->identifier = -1;
  618. cre->addToSlot(SlotID(0), new CStackInstance(CreatureID(subID), -1)); //add placeholder stack
  619. }
  620. break;
  621. default:
  622. o = new CGObjectInstance();
  623. break;
  624. }
  625. o->ID = ID;
  626. o->subID = subID;
  627. o->pos = pos;
  628. o->appearance = VLC->objtypeh->getHandlerFor(o->ID, o->subID)->getTemplates(terrainType).front();
  629. id = o->id = ObjectInstanceID(gs->map->objects.size());
  630. gs->map->objects.push_back(o);
  631. gs->map->addBlockVisTiles(o);
  632. o->initObj(gs->getRandomGenerator());
  633. gs->map->calculateGuardingGreaturePositions();
  634. logGlobal->debugStream() << "added object id=" << id << "; address=" << (intptr_t)o << "; name=" << o->getObjectName();
  635. }
  636. DLL_LINKAGE void NewArtifact::applyGs(CGameState *gs)
  637. {
  638. assert(!vstd::contains(gs->map->artInstances, art));
  639. gs->map->addNewArtifactInstance(art);
  640. assert(!art->getParentNodes().size());
  641. art->setType(art->artType);
  642. if (CCombinedArtifactInstance* cart = dynamic_cast<CCombinedArtifactInstance*>(art.get()))
  643. cart->createConstituents();
  644. }
  645. DLL_LINKAGE const CStackInstance * StackLocation::getStack()
  646. {
  647. if(!army->hasStackAtSlot(slot))
  648. {
  649. logNetwork->warnStream() << "Warning: " << army->nodeName() << " don't have a stack at slot " << slot;
  650. return nullptr;
  651. }
  652. return &army->getStack(slot);
  653. }
  654. struct ObjectRetriever : boost::static_visitor<const CArmedInstance *>
  655. {
  656. const CArmedInstance * operator()(const ConstTransitivePtr<CGHeroInstance> &h) const
  657. {
  658. return h;
  659. }
  660. const CArmedInstance * operator()(const ConstTransitivePtr<CStackInstance> &s) const
  661. {
  662. return s->armyObj;
  663. }
  664. };
  665. template <typename T>
  666. struct GetBase : boost::static_visitor<T*>
  667. {
  668. template <typename TArg>
  669. T * operator()(TArg &arg) const
  670. {
  671. return arg;
  672. }
  673. };
  674. DLL_LINKAGE void ArtifactLocation::removeArtifact()
  675. {
  676. CArtifactInstance *a = getArt();
  677. assert(a);
  678. a->removeFrom(*this);
  679. }
  680. DLL_LINKAGE const CArmedInstance * ArtifactLocation::relatedObj() const
  681. {
  682. return boost::apply_visitor(ObjectRetriever(), artHolder);
  683. }
  684. DLL_LINKAGE PlayerColor ArtifactLocation::owningPlayer() const
  685. {
  686. auto obj = relatedObj();
  687. return obj ? obj->tempOwner : PlayerColor::NEUTRAL;
  688. }
  689. DLL_LINKAGE CArtifactSet *ArtifactLocation::getHolderArtSet()
  690. {
  691. return boost::apply_visitor(GetBase<CArtifactSet>(), artHolder);
  692. }
  693. DLL_LINKAGE CBonusSystemNode *ArtifactLocation::getHolderNode()
  694. {
  695. return boost::apply_visitor(GetBase<CBonusSystemNode>(), artHolder);
  696. }
  697. DLL_LINKAGE const CArtifactInstance *ArtifactLocation::getArt() const
  698. {
  699. const ArtSlotInfo *s = getSlot();
  700. if(s && s->artifact)
  701. {
  702. if(!s->locked)
  703. return s->artifact;
  704. else
  705. {
  706. logNetwork->warnStream() << "ArtifactLocation::getArt: This location is locked!";
  707. return nullptr;
  708. }
  709. }
  710. return nullptr;
  711. }
  712. DLL_LINKAGE const CArtifactSet * ArtifactLocation::getHolderArtSet() const
  713. {
  714. ArtifactLocation *t = const_cast<ArtifactLocation*>(this);
  715. return t->getHolderArtSet();
  716. }
  717. DLL_LINKAGE const CBonusSystemNode * ArtifactLocation::getHolderNode() const
  718. {
  719. ArtifactLocation *t = const_cast<ArtifactLocation*>(this);
  720. return t->getHolderNode();
  721. }
  722. DLL_LINKAGE CArtifactInstance *ArtifactLocation::getArt()
  723. {
  724. const ArtifactLocation *t = this;
  725. return const_cast<CArtifactInstance*>(t->getArt());
  726. }
  727. DLL_LINKAGE const ArtSlotInfo *ArtifactLocation::getSlot() const
  728. {
  729. return getHolderArtSet()->getSlot(slot);
  730. }
  731. DLL_LINKAGE void ChangeStackCount::applyGs(CGameState *gs)
  732. {
  733. if(absoluteValue)
  734. sl.army->setStackCount(sl.slot, count);
  735. else
  736. sl.army->changeStackCount(sl.slot, count);
  737. }
  738. DLL_LINKAGE void SetStackType::applyGs(CGameState *gs)
  739. {
  740. sl.army->setStackType(sl.slot, type);
  741. }
  742. DLL_LINKAGE void EraseStack::applyGs(CGameState *gs)
  743. {
  744. sl.army->eraseStack(sl.slot);
  745. }
  746. DLL_LINKAGE void SwapStacks::applyGs(CGameState *gs)
  747. {
  748. CStackInstance *s1 = sl1.army->detachStack(sl1.slot),
  749. *s2 = sl2.army->detachStack(sl2.slot);
  750. sl2.army->putStack(sl2.slot, s1);
  751. sl1.army->putStack(sl1.slot, s2);
  752. }
  753. DLL_LINKAGE void InsertNewStack::applyGs(CGameState *gs)
  754. {
  755. auto s = new CStackInstance(stack.type, stack.count);
  756. sl.army->putStack(sl.slot, s);
  757. }
  758. DLL_LINKAGE void RebalanceStacks::applyGs(CGameState *gs)
  759. {
  760. const CCreature *srcType = src.army->getCreature(src.slot);
  761. TQuantity srcCount = src.army->getStackCount(src.slot);
  762. bool stackExp = VLC->modh->modules.STACK_EXP;
  763. if(srcCount == count) //moving whole stack
  764. {
  765. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> merge
  766. {
  767. assert(c == srcType);
  768. UNUSED(c);
  769. auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT);
  770. auto alDest = ArtifactLocation (dst.getStack(), ArtifactPosition::CREATURE_SLOT);
  771. auto artHere = alHere.getArt();
  772. auto artDest = alDest.getArt();
  773. if (artHere)
  774. {
  775. if (alDest.getArt())
  776. {
  777. auto hero = dynamic_cast <CGHeroInstance *>(src.army.get());
  778. if (hero)
  779. {
  780. artDest->move (alDest, ArtifactLocation (hero, alDest.getArt()->firstBackpackSlot (hero)));
  781. }
  782. //else - artifact cna be lost :/
  783. else
  784. {
  785. logNetwork->warnStream() << "Artifact is present at destination slot!";
  786. }
  787. artHere->move (alHere, alDest);
  788. //TODO: choose from dialog
  789. }
  790. else //just move to the other slot before stack gets erased
  791. {
  792. artHere->move (alHere, alDest);
  793. }
  794. }
  795. if (stackExp)
  796. {
  797. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  798. src.army->eraseStack(src.slot);
  799. dst.army->changeStackCount(dst.slot, count);
  800. dst.army->setStackExp(dst.slot, totalExp /(dst.army->getStackCount(dst.slot))); //mean
  801. }
  802. else
  803. {
  804. src.army->eraseStack(src.slot);
  805. dst.army->changeStackCount(dst.slot, count);
  806. }
  807. }
  808. else //move stack to an empty slot, no exp change needed
  809. {
  810. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  811. dst.army->putStack(dst.slot, stackDetached);
  812. }
  813. }
  814. else
  815. {
  816. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
  817. {
  818. assert(c == srcType);
  819. UNUSED(c);
  820. if (stackExp)
  821. {
  822. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  823. src.army->changeStackCount(src.slot, -count);
  824. dst.army->changeStackCount(dst.slot, count);
  825. dst.army->setStackExp(dst.slot, totalExp /(src.army->getStackCount(src.slot) + dst.army->getStackCount(dst.slot))); //mean
  826. }
  827. else
  828. {
  829. src.army->changeStackCount(src.slot, -count);
  830. dst.army->changeStackCount(dst.slot, count);
  831. }
  832. }
  833. else //split stack to an empty slot
  834. {
  835. src.army->changeStackCount(src.slot, -count);
  836. dst.army->addToSlot(dst.slot, srcType->idNumber, count, false);
  837. if (stackExp)
  838. dst.army->setStackExp(dst.slot, src.army->getStackExperience(src.slot));
  839. }
  840. }
  841. CBonusSystemNode::treeHasChanged();
  842. }
  843. DLL_LINKAGE void PutArtifact::applyGs(CGameState *gs)
  844. {
  845. assert(art->canBePutAt(al));
  846. art->putAt(al);
  847. //al.hero->putArtifact(al.slot, art);
  848. }
  849. DLL_LINKAGE void EraseArtifact::applyGs(CGameState *gs)
  850. {
  851. auto slot = al.getSlot();
  852. if(slot->locked)
  853. {
  854. logGlobal->debugStream() << "Erasing locked artifact: " << slot->artifact->artType->Name();
  855. DisassembledArtifact dis;
  856. dis.al.artHolder = al.artHolder;
  857. auto aset = al.getHolderArtSet();
  858. #ifndef NDEBUG
  859. bool found = false;
  860. #endif
  861. for(auto& p : aset->artifactsWorn)
  862. {
  863. auto art = p.second.artifact;
  864. if(art->canBeDisassembled() && art->isPart(slot->artifact))
  865. {
  866. dis.al.slot = aset->getArtPos(art);
  867. #ifndef NDEBUG
  868. found = true;
  869. #endif
  870. break;
  871. }
  872. }
  873. assert(found && "Failed to determine the assembly this locked artifact belongs to");
  874. logGlobal->debugStream() << "Found the corresponding assembly: " << dis.al.getSlot()->artifact->artType->Name();
  875. dis.applyGs(gs);
  876. }
  877. else
  878. {
  879. logGlobal->debugStream() << "Erasing artifact " << slot->artifact->artType->Name();
  880. }
  881. al.removeArtifact();
  882. }
  883. DLL_LINKAGE void MoveArtifact::applyGs(CGameState *gs)
  884. {
  885. CArtifactInstance *a = src.getArt();
  886. if(dst.slot < GameConstants::BACKPACK_START)
  887. assert(!dst.getArt());
  888. a->move(src, dst);
  889. //TODO what'll happen if Titan's thunder is equipped by pickin git up or the start of game?
  890. if (a->artType->id == ArtifactID::TITANS_THUNDER && dst.slot == ArtifactPosition::RIGHT_HAND) //Titan's Thunder creates new spellbook on equip
  891. {
  892. auto hPtr = boost::get<ConstTransitivePtr<CGHeroInstance> >(&dst.artHolder);
  893. if(hPtr)
  894. {
  895. CGHeroInstance *h = *hPtr;
  896. if(h && !h->hasSpellbook())
  897. gs->giveHeroArtifact(h, ArtifactID::SPELLBOOK);
  898. }
  899. }
  900. }
  901. DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs)
  902. {
  903. CArtifactSet *artSet = al.getHolderArtSet();
  904. const CArtifactInstance *transformedArt = al.getArt();
  905. assert(transformedArt);
  906. assert(vstd::contains(transformedArt->assemblyPossibilities(artSet), builtArt));
  907. UNUSED(transformedArt);
  908. auto combinedArt = new CCombinedArtifactInstance(builtArt);
  909. gs->map->addNewArtifactInstance(combinedArt);
  910. //retrieve all constituents
  911. for(const CArtifact * constituent : *builtArt->constituents)
  912. {
  913. ArtifactPosition pos = artSet->getArtPos(constituent->id);
  914. assert(pos >= 0);
  915. CArtifactInstance *constituentInstance = artSet->getArt(pos);
  916. //move constituent from hero to be part of new, combined artifact
  917. constituentInstance->removeFrom(ArtifactLocation(al.artHolder, pos));
  918. combinedArt->addAsConstituent(constituentInstance, pos);
  919. if(!vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], al.slot) && vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], pos))
  920. al.slot = pos;
  921. }
  922. //put new combined artifacts
  923. combinedArt->putAt(al);
  924. }
  925. DLL_LINKAGE void DisassembledArtifact::applyGs(CGameState *gs)
  926. {
  927. CCombinedArtifactInstance *disassembled = dynamic_cast<CCombinedArtifactInstance*>(al.getArt());
  928. assert(disassembled);
  929. std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo;
  930. disassembled->removeFrom(al);
  931. for(CCombinedArtifactInstance::ConstituentInfo &ci : constituents)
  932. {
  933. ArtifactLocation constituentLoc = al;
  934. constituentLoc.slot = (ci.slot >= 0 ? ci.slot : al.slot); //-1 is slot of main constituent -> it'll replace combined artifact in its pos
  935. disassembled->detachFrom(ci.art);
  936. ci.art->putAt(constituentLoc);
  937. }
  938. gs->map->eraseArtifactInstance(disassembled);
  939. }
  940. DLL_LINKAGE void HeroVisit::applyGs(CGameState *gs)
  941. {
  942. }
  943. DLL_LINKAGE void SetAvailableArtifacts::applyGs(CGameState *gs)
  944. {
  945. if(id >= 0)
  946. {
  947. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
  948. {
  949. bm->artifacts = arts;
  950. }
  951. else
  952. {
  953. logNetwork->errorStream() << "Wrong black market id!";
  954. }
  955. }
  956. else
  957. {
  958. CGTownInstance::merchantArtifacts = arts;
  959. }
  960. }
  961. DLL_LINKAGE void NewTurn::applyGs(CGameState *gs)
  962. {
  963. gs->day = day;
  964. // Update bonuses before doing anything else so hero don't get more MP than needed
  965. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  966. gs->globalEffects.updateBonuses(Bonus::NDays);
  967. gs->globalEffects.updateBonuses(Bonus::OneWeek);
  968. //TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
  969. for(NewTurn::Hero h : heroes) //give mana/movement point
  970. {
  971. CGHeroInstance *hero = gs->getHero(h.id);
  972. if(!hero)
  973. {
  974. // retreated or surrendered hero who has not been reset yet
  975. for(auto& hp : gs->hpool.heroesPool)
  976. {
  977. if(hp.second->id == h.id)
  978. {
  979. hero = hp.second;
  980. break;
  981. }
  982. }
  983. }
  984. if(!hero)
  985. {
  986. logGlobal->errorStream() << "Hero " << h.id << " not found in NewTurn::applyGs";
  987. continue;
  988. }
  989. hero->movement = h.move;
  990. hero->mana = h.mana;
  991. }
  992. for(auto i = res.cbegin(); i != res.cend(); i++)
  993. {
  994. assert(i->first < PlayerColor::PLAYER_LIMIT);
  995. gs->getPlayer(i->first)->resources = i->second;
  996. }
  997. for(auto creatureSet : cres) //set available creatures in towns
  998. creatureSet.second.applyGs(gs);
  999. for(CGTownInstance* t : gs->map->towns)
  1000. t->builded = 0;
  1001. if(gs->getDate(Date::DAY_OF_WEEK) == 1)
  1002. gs->updateRumor();
  1003. //count days without town for all players, regardless of their turn order
  1004. for (auto &p : gs->players)
  1005. {
  1006. PlayerState & playerState = p.second;
  1007. if (playerState.status == EPlayerStatus::INGAME)
  1008. {
  1009. if (playerState.towns.empty())
  1010. {
  1011. if (playerState.daysWithoutCastle)
  1012. ++(*playerState.daysWithoutCastle);
  1013. else playerState.daysWithoutCastle = 0;
  1014. }
  1015. else
  1016. {
  1017. playerState.daysWithoutCastle = boost::none;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. DLL_LINKAGE void SetObjectProperty::applyGs(CGameState *gs)
  1023. {
  1024. CGObjectInstance *obj = gs->getObjInstance(id);
  1025. if(!obj)
  1026. {
  1027. logNetwork->errorStream() << "Wrong object ID - property cannot be set!";
  1028. return;
  1029. }
  1030. CArmedInstance *cai = dynamic_cast<CArmedInstance *>(obj);
  1031. if(what == ObjProperty::OWNER && cai)
  1032. {
  1033. if(obj->ID == Obj::TOWN)
  1034. {
  1035. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  1036. if(t->tempOwner < PlayerColor::PLAYER_LIMIT)
  1037. gs->getPlayer(t->tempOwner)->towns -= t;
  1038. if(val < PlayerColor::PLAYER_LIMIT_I)
  1039. {
  1040. PlayerState * p = gs->getPlayer(PlayerColor(val));
  1041. p->towns.push_back(t);
  1042. //reset counter before NewTurn to avoid no town message if game loaded at turn when one already captured
  1043. if(p->daysWithoutCastle)
  1044. p->daysWithoutCastle = boost::none;
  1045. }
  1046. }
  1047. CBonusSystemNode *nodeToMove = cai->whatShouldBeAttached();
  1048. nodeToMove->detachFrom(cai->whereShouldBeAttached(gs));
  1049. obj->setProperty(what,val);
  1050. nodeToMove->attachTo(cai->whereShouldBeAttached(gs));
  1051. }
  1052. else //not an armed instance
  1053. {
  1054. obj->setProperty(what,val);
  1055. }
  1056. }
  1057. DLL_LINKAGE void PrepareHeroLevelUp::applyGs(CGameState *gs)
  1058. {
  1059. CGHeroInstance * h = gs->getHero(hero->id);
  1060. auto proposedSkills = h->getLevelUpProposedSecondarySkills();
  1061. if(skills.size() == 1 || hero->tempOwner == PlayerColor::NEUTRAL) //choose skill automatically
  1062. {
  1063. skills.push_back(*RandomGeneratorUtil::nextItem(proposedSkills, h->skillsInfo.rand));
  1064. }
  1065. else
  1066. {
  1067. skills = proposedSkills;
  1068. }
  1069. }
  1070. DLL_LINKAGE void HeroLevelUp::applyGs(CGameState *gs)
  1071. {
  1072. CGHeroInstance * h = gs->getHero(hero->id);
  1073. h->levelUp(skills);
  1074. }
  1075. DLL_LINKAGE void CommanderLevelUp::applyGs (CGameState *gs)
  1076. {
  1077. CCommanderInstance * commander = gs->getHero(hero->id)->commander;
  1078. assert (commander);
  1079. commander->levelUp();
  1080. }
  1081. DLL_LINKAGE void BattleStart::applyGs(CGameState *gs)
  1082. {
  1083. gs->curB = info;
  1084. gs->curB->localInit();
  1085. }
  1086. DLL_LINKAGE void BattleNextRound::applyGs(CGameState *gs)
  1087. {
  1088. for (int i = 0; i < 2; ++i)
  1089. {
  1090. gs->curB->sides[i].castSpellsCount = 0;
  1091. vstd::amax(--gs->curB->sides[i].enchanterCounter, 0);
  1092. }
  1093. gs->curB->round = round;
  1094. for(CStack *s : gs->curB->stacks)
  1095. {
  1096. s->state -= EBattleStackState::DEFENDING;
  1097. s->state -= EBattleStackState::WAITING;
  1098. s->state -= EBattleStackState::MOVED;
  1099. s->state -= EBattleStackState::HAD_MORALE;
  1100. s->state -= EBattleStackState::FEAR;
  1101. s->state -= EBattleStackState::DRAINED_MANA;
  1102. s->counterAttacksPerformed = 0;
  1103. s->counterAttacksTotalCache = 0;
  1104. // new turn effects
  1105. s->updateBonuses(Bonus::NTurns);
  1106. if(s->alive() && vstd::contains(s->state, EBattleStackState::CLONED))
  1107. {
  1108. //cloned stack has special lifetime marker
  1109. //check it after bonuses updated in battleTurnPassed()
  1110. if(!s->hasBonus(Selector::type(Bonus::NONE).And(Selector::source(Bonus::SPELL_EFFECT, SpellID::CLONE))))
  1111. s->makeGhost();
  1112. }
  1113. }
  1114. for(auto &obst : gs->curB->obstacles)
  1115. obst->battleTurnPassed();
  1116. }
  1117. DLL_LINKAGE void BattleSetActiveStack::applyGs(CGameState *gs)
  1118. {
  1119. gs->curB->activeStack = stack;
  1120. CStack *st = gs->curB->getStack(stack);
  1121. //remove bonuses that last until when stack gets new turn
  1122. st->popBonuses(Bonus::UntilGetsTurn);
  1123. if(vstd::contains(st->state,EBattleStackState::MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  1124. st->state.insert(EBattleStackState::HAD_MORALE);
  1125. }
  1126. DLL_LINKAGE void BattleTriggerEffect::applyGs(CGameState *gs)
  1127. {
  1128. CStack *st = gs->curB->getStack(stackID);
  1129. switch (effect)
  1130. {
  1131. case Bonus::HP_REGENERATION:
  1132. st->firstHPleft += val;
  1133. vstd::amin (st->firstHPleft, (ui32)st->MaxHealth());
  1134. break;
  1135. case Bonus::MANA_DRAIN:
  1136. {
  1137. CGHeroInstance * h = gs->getHero(ObjectInstanceID(additionalInfo));
  1138. st->state.insert (EBattleStackState::DRAINED_MANA);
  1139. h->mana -= val;
  1140. vstd::amax(h->mana, 0);
  1141. break;
  1142. }
  1143. case Bonus::POISON:
  1144. {
  1145. auto b = st->getBonusLocalFirst(Selector::source(Bonus::SPELL_EFFECT, SpellID::POISON)
  1146. .And(Selector::type(Bonus::STACK_HEALTH)));
  1147. if (b)
  1148. b->val = val;
  1149. break;
  1150. }
  1151. case Bonus::ENCHANTER:
  1152. break;
  1153. case Bonus::FEAR:
  1154. st->state.insert(EBattleStackState::FEAR);
  1155. break;
  1156. default:
  1157. logNetwork->warnStream() << "Unrecognized trigger effect type "<< effect;
  1158. }
  1159. }
  1160. DLL_LINKAGE void BattleObstaclePlaced::applyGs(CGameState *gs)
  1161. {
  1162. gs->curB->obstacles.push_back(obstacle);
  1163. }
  1164. DLL_LINKAGE void BattleUpdateGateState::applyGs(CGameState *gs)
  1165. {
  1166. if(gs->curB)
  1167. gs->curB->si.gateState = state;
  1168. }
  1169. void BattleResult::applyGs(CGameState *gs)
  1170. {
  1171. for (CStack *s : gs->curB->stacks)
  1172. {
  1173. if (s->base && s->base->armyObj && vstd::contains(s->state, EBattleStackState::SUMMONED))
  1174. {
  1175. //stack with SUMMONED flag but coming from garrison -> most likely resurrected, needs to be removed
  1176. assert(&s->base->armyObj->getStack(s->slot) == s->base);
  1177. const_cast<CArmedInstance*>(s->base->armyObj)->eraseStack(s->slot);
  1178. }
  1179. }
  1180. for (auto & elem : gs->curB->stacks)
  1181. delete elem;
  1182. for(int i = 0; i < 2; ++i)
  1183. {
  1184. if(auto h = gs->curB->battleGetFightingHero(i))
  1185. {
  1186. h->popBonuses(Bonus::OneBattle); //remove any "until next battle" bonuses
  1187. if (h->commander && h->commander->alive)
  1188. {
  1189. for (auto art : h->commander->artifactsWorn) //increment bonuses for commander artifacts
  1190. {
  1191. art.second.artifact->artType->levelUpArtifact (art.second.artifact);
  1192. }
  1193. }
  1194. }
  1195. }
  1196. if(VLC->modh->modules.STACK_EXP)
  1197. {
  1198. for(int i = 0; i < 2; i++)
  1199. if(exp[i])
  1200. gs->curB->battleGetArmyObject(i)->giveStackExp(exp[i]);
  1201. CBonusSystemNode::treeHasChanged();
  1202. }
  1203. for(int i = 0; i < 2; i++)
  1204. gs->curB->battleGetArmyObject(i)->battle = nullptr;
  1205. gs->curB.dellNull();
  1206. }
  1207. void BattleStackMoved::applyGs(CGameState *gs)
  1208. {
  1209. CStack *s = gs->curB->getStack(stack);
  1210. assert(s);
  1211. BattleHex dest = tilesToMove.back();
  1212. //if unit ended movement on quicksands that were created by enemy, that quicksand patch becomes visible for owner
  1213. for(auto &oi : gs->curB->obstacles)
  1214. {
  1215. if(oi->obstacleType == CObstacleInstance::QUICKSAND
  1216. && vstd::contains(oi->getAffectedTiles(), tilesToMove.back()))
  1217. {
  1218. SpellCreatedObstacle *sands = dynamic_cast<SpellCreatedObstacle*>(oi.get());
  1219. assert(sands);
  1220. if(sands->casterSide != !s->attackerOwned)
  1221. sands->visibleForAnotherSide = true;
  1222. }
  1223. }
  1224. s->position = dest;
  1225. }
  1226. DLL_LINKAGE void BattleStackAttacked::applyGs(CGameState *gs)
  1227. {
  1228. CStack *at = gs->curB->getStack(stackAttacked);
  1229. assert(at);
  1230. at->popBonuses(Bonus::UntilBeingAttacked);
  1231. at->count = newAmount;
  1232. at->firstHPleft = newHP;
  1233. if(killed())
  1234. {
  1235. at->state -= EBattleStackState::ALIVE;
  1236. if(at->cloneID >= 0)
  1237. {
  1238. //remove clone as well
  1239. CStack * clone = gs->curB->getStack(at->cloneID);
  1240. if(clone)
  1241. clone->makeGhost();
  1242. at->cloneID = -1;
  1243. }
  1244. }
  1245. //life drain handling
  1246. for (auto & elem : healedStacks)
  1247. {
  1248. elem.applyGs(gs);
  1249. }
  1250. if (willRebirth())
  1251. {
  1252. at->casts--;
  1253. at->state.insert(EBattleStackState::ALIVE); //hmm?
  1254. }
  1255. if (cloneKilled())
  1256. {
  1257. //"hide" killed creatures instead so we keep info about it
  1258. at->makeGhost();
  1259. for(CStack * s : gs->curB->stacks)
  1260. {
  1261. if(s->cloneID == at->ID)
  1262. s->cloneID = -1;
  1263. }
  1264. }
  1265. //killed summoned creature should be removed like clone
  1266. if(killed() && vstd::contains(at->state, EBattleStackState::SUMMONED))
  1267. {
  1268. at->makeGhost();
  1269. }
  1270. }
  1271. DLL_LINKAGE void BattleAttack::applyGs(CGameState *gs)
  1272. {
  1273. CStack *attacker = gs->curB->getStack(stackAttacking);
  1274. if(counter())
  1275. attacker->counterAttacksPerformed++;
  1276. if(shot())
  1277. {
  1278. //don't remove ammo if we have a working ammo cart
  1279. bool hasAmmoCart = false;
  1280. for(const CStack * st : gs->curB->stacks)
  1281. {
  1282. if(st->owner == attacker->owner && st->getCreature()->idNumber == CreatureID::AMMO_CART && st->alive())
  1283. {
  1284. hasAmmoCart = true;
  1285. break;
  1286. }
  1287. }
  1288. if (!hasAmmoCart)
  1289. {
  1290. attacker->shots--;
  1291. }
  1292. }
  1293. for(BattleStackAttacked & stackAttacked : bsa)
  1294. stackAttacked.applyGs(gs);
  1295. attacker->popBonuses(Bonus::UntilAttack);
  1296. }
  1297. DLL_LINKAGE void StartAction::applyGs(CGameState *gs)
  1298. {
  1299. CStack *st = gs->curB->getStack(ba.stackNumber);
  1300. if(ba.actionType == Battle::END_TACTIC_PHASE)
  1301. {
  1302. gs->curB->tacticDistance = 0;
  1303. return;
  1304. }
  1305. if(gs->curB->tacticDistance)
  1306. {
  1307. // moves in tactics phase do not affect creature status
  1308. // (tactics stack queue is managed by client)
  1309. return;
  1310. }
  1311. if(ba.actionType != Battle::HERO_SPELL) //don't check for stack if it's custom action by hero
  1312. {
  1313. assert(st);
  1314. }
  1315. else
  1316. {
  1317. gs->curB->sides[ba.side].usedSpellsHistory.push_back(SpellID(ba.additionalInfo).toSpell());
  1318. }
  1319. switch(ba.actionType)
  1320. {
  1321. case Battle::DEFEND:
  1322. st->state -= EBattleStackState::DEFENDING_ANIM;
  1323. st->state.insert(EBattleStackState::DEFENDING);
  1324. st->state.insert(EBattleStackState::DEFENDING_ANIM);
  1325. break;
  1326. case Battle::WAIT:
  1327. st->state -= EBattleStackState::DEFENDING_ANIM;
  1328. st->state.insert(EBattleStackState::WAITING);
  1329. return;
  1330. case Battle::HERO_SPELL: //no change in current stack state
  1331. return;
  1332. default: //any active stack action - attack, catapult, heal, spell...
  1333. st->state -= EBattleStackState::DEFENDING_ANIM;
  1334. st->state.insert(EBattleStackState::MOVED);
  1335. break;
  1336. }
  1337. if(st)
  1338. st->state -= EBattleStackState::WAITING; //if stack was waiting it has made move, so it won't be "waiting" anymore (if the action was WAIT, then we have returned)
  1339. }
  1340. DLL_LINKAGE void BattleSpellCast::applyGs(CGameState *gs)
  1341. {
  1342. assert(gs->curB);
  1343. const CSpell * spell = SpellID(id).toSpell();
  1344. spell->applyBattle(gs->curB, this);
  1345. }
  1346. void actualizeEffect(CStack * s, const Bonus & ef)
  1347. {
  1348. for(auto stackBonus : s->getBonusList()) //TODO: optimize
  1349. {
  1350. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == ef.type && stackBonus->subtype == ef.subtype)
  1351. {
  1352. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
  1353. }
  1354. }
  1355. CBonusSystemNode::treeHasChanged();
  1356. }
  1357. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  1358. {
  1359. //actualizing features vector
  1360. for(const Bonus &fromEffect : ef)
  1361. {
  1362. actualizeEffect(s, fromEffect);
  1363. }
  1364. }
  1365. DLL_LINKAGE void SetStackEffect::applyGs(CGameState *gs)
  1366. {
  1367. if(effect.empty() && cumulativeEffects.empty())
  1368. {
  1369. logGlobal->errorStream() << "Trying to apply SetStackEffect with no effects";
  1370. return;
  1371. }
  1372. si32 spellid = effect.empty() ? cumulativeEffects.begin()->sid : effect.begin()->sid; //effects' source ID
  1373. auto processEffect = [spellid, this](CStack * sta, const Bonus & effect, bool cumulative)
  1374. {
  1375. if(cumulative || !sta->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid).And(Selector::typeSubtype(effect.type, effect.subtype))))
  1376. {
  1377. //no such effect or cumulative - add new
  1378. logBonus->traceStream() << sta->nodeName() << " receives a new bonus: " << effect.Description();
  1379. sta->addNewBonus(std::make_shared<Bonus>(effect));
  1380. }
  1381. else
  1382. {
  1383. logBonus->traceStream() << sta->nodeName() << " updated bonus: " << effect.Description();
  1384. actualizeEffect(sta, effect);
  1385. }
  1386. };
  1387. for(ui32 id : stacks)
  1388. {
  1389. CStack *s = gs->curB->getStack(id);
  1390. if(s)
  1391. {
  1392. for(const Bonus & fromEffect : effect)
  1393. processEffect(s, fromEffect, false);
  1394. for(const Bonus & fromEffect : cumulativeEffects)
  1395. processEffect(s, fromEffect, true);
  1396. }
  1397. else
  1398. logNetwork->errorStream() << "Cannot find stack " << id;
  1399. }
  1400. for(auto & para : uniqueBonuses)
  1401. {
  1402. CStack *s = gs->curB->getStack(para.first);
  1403. if(s)
  1404. processEffect(s, para.second, false);
  1405. else
  1406. logNetwork->errorStream() << "Cannot find stack " << para.first;
  1407. }
  1408. for(auto & para : cumulativeUniqueBonuses)
  1409. {
  1410. CStack *s = gs->curB->getStack(para.first);
  1411. if(s)
  1412. processEffect(s, para.second, true);
  1413. else
  1414. logNetwork->errorStream() << "Cannot find stack " << para.first;
  1415. }
  1416. }
  1417. DLL_LINKAGE void StacksInjured::applyGs(CGameState *gs)
  1418. {
  1419. for(BattleStackAttacked stackAttacked : stacks)
  1420. stackAttacked.applyGs(gs);
  1421. }
  1422. DLL_LINKAGE void StacksHealedOrResurrected::applyGs(CGameState *gs)
  1423. {
  1424. for(auto & elem : healedStacks)
  1425. {
  1426. CStack * changedStack = gs->curB->getStack(elem.stackID, false);
  1427. //checking if we resurrect a stack that is under a living stack
  1428. auto accessibility = gs->curB->getAccesibility();
  1429. if(!changedStack->alive() && !accessibility.accessible(changedStack->position, changedStack))
  1430. {
  1431. logNetwork->errorStream() << "Cannot resurrect " << changedStack->nodeName() << " because hex " << changedStack->position << " is occupied!";
  1432. return; //position is already occupied
  1433. }
  1434. //applying changes
  1435. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1436. if(resurrected)
  1437. {
  1438. if(changedStack->count > 0 || changedStack->firstHPleft > 0)
  1439. logGlobal->warn("Dead stack %s with positive total HP %d", changedStack->nodeName(), changedStack->totalHealth());
  1440. changedStack->state.insert(EBattleStackState::ALIVE);
  1441. }
  1442. int res;
  1443. if(canOverheal) //for example WoG ghost soul steal ability allows getting more units than before battle
  1444. res = elem.healedHP / changedStack->MaxHealth();
  1445. else
  1446. res = std::min(elem.healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count);
  1447. changedStack->count += res;
  1448. if(elem.lowLevelResurrection)
  1449. changedStack->resurrected += res;
  1450. changedStack->firstHPleft += elem.healedHP - res * changedStack->MaxHealth();
  1451. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1452. {
  1453. changedStack->firstHPleft -= changedStack->MaxHealth();
  1454. if(changedStack->baseAmount > changedStack->count)
  1455. {
  1456. changedStack->count += 1;
  1457. }
  1458. }
  1459. vstd::amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1460. if(resurrected)
  1461. {
  1462. //removing all spells effects
  1463. auto selector = [](const Bonus * b)
  1464. {
  1465. //Special case: DISRUPTING_RAY is "immune" to dispell
  1466. //Other even PERMANENT effects can be removed
  1467. if(b->source == Bonus::SPELL_EFFECT)
  1468. return b->sid != SpellID::DISRUPTING_RAY;
  1469. else
  1470. return false;
  1471. };
  1472. changedStack->popBonuses(selector);
  1473. }
  1474. else if(cure)
  1475. {
  1476. //removing all effects from negative spells
  1477. auto selector = [](const Bonus* b)
  1478. {
  1479. //Special case: DISRUPTING_RAY is "immune" to dispell
  1480. //Other even PERMANENT effects can be removed
  1481. if(b->source == Bonus::SPELL_EFFECT)
  1482. {
  1483. const CSpell * sourceSpell = SpellID(b->sid).toSpell();
  1484. if(!sourceSpell)
  1485. return false;
  1486. return sourceSpell->id != SpellID::DISRUPTING_RAY && sourceSpell->isNegative();
  1487. }
  1488. else
  1489. return false;
  1490. };
  1491. changedStack->popBonuses(selector);
  1492. }
  1493. }
  1494. }
  1495. DLL_LINKAGE void ObstaclesRemoved::applyGs(CGameState *gs)
  1496. {
  1497. if(gs->curB) //if there is a battle
  1498. {
  1499. for(const si32 rem_obst :obstacles)
  1500. {
  1501. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1502. {
  1503. if(gs->curB->obstacles[i]->uniqueID == rem_obst) //remove this obstacle
  1504. {
  1505. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. }
  1512. DLL_LINKAGE CatapultAttack::CatapultAttack()
  1513. {
  1514. attacker = -1;
  1515. }
  1516. DLL_LINKAGE CatapultAttack::~CatapultAttack()
  1517. {
  1518. }
  1519. DLL_LINKAGE void CatapultAttack::applyGs(CGameState *gs)
  1520. {
  1521. if(gs->curB && gs->curB->town && gs->curB->town->fortLevel() != CGTownInstance::NONE) //if there is a battle and it's a siege
  1522. {
  1523. for(const auto &it :attackedParts)
  1524. {
  1525. gs->curB->si.wallState[it.attackedPart] =
  1526. SiegeInfo::applyDamage(EWallState::EWallState(gs->curB->si.wallState[it.attackedPart]), it.damageDealt);
  1527. }
  1528. }
  1529. }
  1530. DLL_LINKAGE std::string CatapultAttack::AttackInfo::toString() const
  1531. {
  1532. return boost::str(boost::format("{AttackInfo: destinationTile '%d', attackedPart '%d', damageDealt '%d'}")
  1533. % destinationTile % static_cast<int>(attackedPart) % static_cast<int>(damageDealt));
  1534. }
  1535. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CatapultAttack::AttackInfo & attackInfo)
  1536. {
  1537. return out << attackInfo.toString();
  1538. }
  1539. DLL_LINKAGE std::string CatapultAttack::toString() const
  1540. {
  1541. return boost::str(boost::format("{CatapultAttack: attackedParts '%s', attacker '%d'}") % attackedParts % attacker);
  1542. }
  1543. DLL_LINKAGE void BattleStacksRemoved::applyGs(CGameState *gs)
  1544. {
  1545. if(!gs->curB)
  1546. return;
  1547. while(!stackIDs.empty())
  1548. {
  1549. ui32 rem_stack = *stackIDs.begin();
  1550. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1551. {
  1552. if(gs->curB->stacks[b]->ID == rem_stack) //if found
  1553. {
  1554. CStack * toRemove = gs->curB->stacks[b];
  1555. toRemove->state.erase(EBattleStackState::ALIVE);
  1556. toRemove->state.erase(EBattleStackState::GHOST_PENDING);
  1557. toRemove->state.insert(EBattleStackState::GHOST);
  1558. toRemove->detachFromAll();//TODO: may be some bonuses should remain
  1559. //stack may be removed instantly (not being killed first)
  1560. //handle clone remove also here
  1561. if(toRemove->cloneID >= 0)
  1562. {
  1563. stackIDs.insert(toRemove->cloneID);
  1564. toRemove->cloneID = -1;
  1565. }
  1566. //cleanup remaining clone links if any
  1567. for(CStack * s : gs->curB->stacks)
  1568. {
  1569. if(s->cloneID == toRemove->ID)
  1570. s->cloneID = -1;
  1571. }
  1572. break;
  1573. }
  1574. }
  1575. stackIDs.erase(rem_stack);
  1576. }
  1577. }
  1578. DLL_LINKAGE void BattleStackAdded::applyGs(CGameState *gs)
  1579. {
  1580. newStackID = 0;
  1581. if (!BattleHex(pos).isValid())
  1582. {
  1583. logNetwork->warnStream() << "No place found for new stack!";
  1584. return;
  1585. }
  1586. CStackBasicDescriptor csbd(creID, amount);
  1587. CStack * addedStack = gs->curB->generateNewStack(csbd, attacker, SlotID::SUMMONED_SLOT_PLACEHOLDER, pos); //TODO: netpacks?
  1588. if (summoned)
  1589. addedStack->state.insert(EBattleStackState::SUMMONED);
  1590. gs->curB->localInitStack(addedStack);
  1591. gs->curB->stacks.push_back(addedStack); //the stack is not "SUMMONED", it is permanent
  1592. newStackID = addedStack->ID;
  1593. }
  1594. DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState *gs)
  1595. {
  1596. CStack * stack = gs->curB->getStack(stackID);
  1597. switch (which)
  1598. {
  1599. case CASTS:
  1600. {
  1601. if (absolute)
  1602. stack->casts = val;
  1603. else
  1604. stack->casts += val;
  1605. vstd::amax(stack->casts, 0);
  1606. break;
  1607. }
  1608. case ENCHANTER_COUNTER:
  1609. {
  1610. auto & counter = gs->curB->sides[gs->curB->whatSide(stack->owner)].enchanterCounter;
  1611. if (absolute)
  1612. counter = val;
  1613. else
  1614. counter += val;
  1615. vstd::amax(counter, 0);
  1616. break;
  1617. }
  1618. case UNBIND:
  1619. {
  1620. stack->popBonuses(Selector::type(Bonus::BIND_EFFECT));
  1621. break;
  1622. }
  1623. case CLONED:
  1624. {
  1625. stack->state.insert(EBattleStackState::CLONED);
  1626. break;
  1627. }
  1628. case HAS_CLONE:
  1629. {
  1630. stack->cloneID = val;
  1631. break;
  1632. }
  1633. }
  1634. }
  1635. DLL_LINKAGE void PlayerCheated::applyGs(CGameState *gs)
  1636. {
  1637. if(!player.isValidPlayer())
  1638. return;
  1639. gs->getPlayer(player)->enteredLosingCheatCode = losingCheatCode;
  1640. gs->getPlayer(player)->enteredWinningCheatCode = winningCheatCode;
  1641. }
  1642. DLL_LINKAGE void YourTurn::applyGs(CGameState *gs)
  1643. {
  1644. gs->currentPlayer = player;
  1645. auto & playerState = gs->players[player];
  1646. playerState.daysWithoutCastle = daysWithoutCastle;
  1647. }
  1648. DLL_LINKAGE Component::Component(const CStackBasicDescriptor &stack)
  1649. : id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1650. {
  1651. }