NetPacksLib.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. /*
  2. * NetPacksLib.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ArtifactUtils.h"
  12. #include "NetPacks.h"
  13. #include "NetPackVisitor.h"
  14. #include "CGeneralTextHandler.h"
  15. #include "CArtHandler.h"
  16. #include "CHeroHandler.h"
  17. #include "VCMI_Lib.h"
  18. #include "mapping/CMap.h"
  19. #include "spells/CSpellHandler.h"
  20. #include "CCreatureHandler.h"
  21. #include "gameState/CGameState.h"
  22. #include "gameState/TavernHeroesPool.h"
  23. #include "CStack.h"
  24. #include "battle/BattleInfo.h"
  25. #include "CTownHandler.h"
  26. #include "mapping/CMapInfo.h"
  27. #include "StartInfo.h"
  28. #include "CPlayerState.h"
  29. #include "TerrainHandler.h"
  30. #include "mapObjects/CGCreature.h"
  31. #include "mapObjects/CGMarket.h"
  32. #include "mapObjectConstructors/AObjectTypeHandler.h"
  33. #include "mapObjectConstructors/CObjectClassesHandler.h"
  34. #include "campaign/CampaignState.h"
  35. #include "GameSettings.h"
  36. VCMI_LIB_NAMESPACE_BEGIN
  37. void CPack::visit(ICPackVisitor & visitor)
  38. {
  39. visitBasic(visitor);
  40. // visitBasic may destroy this and in such cases we do not want to call visitTyped
  41. if(visitor.callTyped())
  42. {
  43. visitTyped(visitor);
  44. }
  45. }
  46. void CPack::visitBasic(ICPackVisitor & visitor)
  47. {
  48. }
  49. void CPack::visitTyped(ICPackVisitor & visitor)
  50. {
  51. }
  52. void CPackForClient::visitBasic(ICPackVisitor & visitor)
  53. {
  54. visitor.visitForClient(*this);
  55. }
  56. void CPackForServer::visitBasic(ICPackVisitor & visitor)
  57. {
  58. visitor.visitForServer(*this);
  59. }
  60. void CPackForLobby::visitBasic(ICPackVisitor & visitor)
  61. {
  62. visitor.visitForLobby(*this);
  63. }
  64. bool CPackForLobby::isForServer() const
  65. {
  66. return false;
  67. }
  68. bool CLobbyPackToServer::isForServer() const
  69. {
  70. return true;
  71. }
  72. void PackageApplied::visitTyped(ICPackVisitor & visitor)
  73. {
  74. visitor.visitPackageApplied(*this);
  75. }
  76. void SystemMessage::visitTyped(ICPackVisitor & visitor)
  77. {
  78. visitor.visitSystemMessage(*this);
  79. }
  80. void PlayerBlocked::visitTyped(ICPackVisitor & visitor)
  81. {
  82. visitor.visitPlayerBlocked(*this);
  83. }
  84. void PlayerCheated::visitTyped(ICPackVisitor & visitor)
  85. {
  86. visitor.visitPlayerCheated(*this);
  87. }
  88. void PlayerStartsTurn::visitTyped(ICPackVisitor & visitor)
  89. {
  90. visitor.visitPlayerStartsTurn(*this);
  91. }
  92. void DaysWithoutTown::visitTyped(ICPackVisitor & visitor)
  93. {
  94. visitor.visitDaysWithoutTown(*this);
  95. }
  96. void EntitiesChanged::visitTyped(ICPackVisitor & visitor)
  97. {
  98. visitor.visitEntitiesChanged(*this);
  99. }
  100. void SetResources::visitTyped(ICPackVisitor & visitor)
  101. {
  102. visitor.visitSetResources(*this);
  103. }
  104. void SetPrimSkill::visitTyped(ICPackVisitor & visitor)
  105. {
  106. visitor.visitSetPrimSkill(*this);
  107. }
  108. void SetSecSkill::visitTyped(ICPackVisitor & visitor)
  109. {
  110. visitor.visitSetSecSkill(*this);
  111. }
  112. void HeroVisitCastle::visitTyped(ICPackVisitor & visitor)
  113. {
  114. visitor.visitHeroVisitCastle(*this);
  115. }
  116. void ChangeSpells::visitTyped(ICPackVisitor & visitor)
  117. {
  118. visitor.visitChangeSpells(*this);
  119. }
  120. void SetMana::visitTyped(ICPackVisitor & visitor)
  121. {
  122. visitor.visitSetMana(*this);
  123. }
  124. void SetMovePoints::visitTyped(ICPackVisitor & visitor)
  125. {
  126. visitor.visitSetMovePoints(*this);
  127. }
  128. void FoWChange::visitTyped(ICPackVisitor & visitor)
  129. {
  130. visitor.visitFoWChange(*this);
  131. }
  132. void SetAvailableHero::visitTyped(ICPackVisitor & visitor)
  133. {
  134. visitor.visitSetAvailableHeroes(*this);
  135. }
  136. void GiveBonus::visitTyped(ICPackVisitor & visitor)
  137. {
  138. visitor.visitGiveBonus(*this);
  139. }
  140. void ChangeObjPos::visitTyped(ICPackVisitor & visitor)
  141. {
  142. visitor.visitChangeObjPos(*this);
  143. }
  144. void PlayerEndsTurn::visitTyped(ICPackVisitor & visitor)
  145. {
  146. visitor.visitPlayerEndsTurn(*this);
  147. }
  148. void PlayerEndsGame::visitTyped(ICPackVisitor & visitor)
  149. {
  150. visitor.visitPlayerEndsGame(*this);
  151. }
  152. void PlayerReinitInterface::visitTyped(ICPackVisitor & visitor)
  153. {
  154. visitor.visitPlayerReinitInterface(*this);
  155. }
  156. void RemoveBonus::visitTyped(ICPackVisitor & visitor)
  157. {
  158. visitor.visitRemoveBonus(*this);
  159. }
  160. void SetCommanderProperty::visitTyped(ICPackVisitor & visitor)
  161. {
  162. visitor.visitSetCommanderProperty(*this);
  163. }
  164. void AddQuest::visitTyped(ICPackVisitor & visitor)
  165. {
  166. visitor.visitAddQuest(*this);
  167. }
  168. void UpdateArtHandlerLists::visitTyped(ICPackVisitor & visitor)
  169. {
  170. visitor.visitUpdateArtHandlerLists(*this);
  171. }
  172. void UpdateMapEvents::visitTyped(ICPackVisitor & visitor)
  173. {
  174. visitor.visitUpdateMapEvents(*this);
  175. }
  176. void UpdateCastleEvents::visitTyped(ICPackVisitor & visitor)
  177. {
  178. visitor.visitUpdateCastleEvents(*this);
  179. }
  180. void ChangeFormation::visitTyped(ICPackVisitor & visitor)
  181. {
  182. visitor.visitChangeFormation(*this);
  183. }
  184. void RemoveObject::visitTyped(ICPackVisitor & visitor)
  185. {
  186. visitor.visitRemoveObject(*this);
  187. }
  188. void TryMoveHero::visitTyped(ICPackVisitor & visitor)
  189. {
  190. visitor.visitTryMoveHero(*this);
  191. }
  192. void NewStructures::visitTyped(ICPackVisitor & visitor)
  193. {
  194. visitor.visitNewStructures(*this);
  195. }
  196. void RazeStructures::visitTyped(ICPackVisitor & visitor)
  197. {
  198. visitor.visitRazeStructures(*this);
  199. }
  200. void SetAvailableCreatures::visitTyped(ICPackVisitor & visitor)
  201. {
  202. visitor.visitSetAvailableCreatures(*this);
  203. }
  204. void SetHeroesInTown::visitTyped(ICPackVisitor & visitor)
  205. {
  206. visitor.visitSetHeroesInTown(*this);
  207. }
  208. void HeroRecruited::visitTyped(ICPackVisitor & visitor)
  209. {
  210. visitor.visitHeroRecruited(*this);
  211. }
  212. void GiveHero::visitTyped(ICPackVisitor & visitor)
  213. {
  214. visitor.visitGiveHero(*this);
  215. }
  216. void OpenWindow::visitTyped(ICPackVisitor & visitor)
  217. {
  218. visitor.visitOpenWindow(*this);
  219. }
  220. void NewObject::visitTyped(ICPackVisitor & visitor)
  221. {
  222. visitor.visitNewObject(*this);
  223. }
  224. void SetAvailableArtifacts::visitTyped(ICPackVisitor & visitor)
  225. {
  226. visitor.visitSetAvailableArtifacts(*this);
  227. }
  228. void NewArtifact::visitTyped(ICPackVisitor & visitor)
  229. {
  230. visitor.visitNewArtifact(*this);
  231. }
  232. void ChangeStackCount::visitTyped(ICPackVisitor & visitor)
  233. {
  234. visitor.visitChangeStackCount(*this);
  235. }
  236. void SetStackType::visitTyped(ICPackVisitor & visitor)
  237. {
  238. visitor.visitSetStackType(*this);
  239. }
  240. void EraseStack::visitTyped(ICPackVisitor & visitor)
  241. {
  242. visitor.visitEraseStack(*this);
  243. }
  244. void SwapStacks::visitTyped(ICPackVisitor & visitor)
  245. {
  246. visitor.visitSwapStacks(*this);
  247. }
  248. void InsertNewStack::visitTyped(ICPackVisitor & visitor)
  249. {
  250. visitor.visitInsertNewStack(*this);
  251. }
  252. void RebalanceStacks::visitTyped(ICPackVisitor & visitor)
  253. {
  254. visitor.visitRebalanceStacks(*this);
  255. }
  256. void BulkRebalanceStacks::visitTyped(ICPackVisitor & visitor)
  257. {
  258. visitor.visitBulkRebalanceStacks(*this);
  259. }
  260. void BulkSmartRebalanceStacks::visitTyped(ICPackVisitor & visitor)
  261. {
  262. visitor.visitBulkSmartRebalanceStacks(*this);
  263. }
  264. void PutArtifact::visitTyped(ICPackVisitor & visitor)
  265. {
  266. visitor.visitPutArtifact(*this);
  267. }
  268. void EraseArtifact::visitTyped(ICPackVisitor & visitor)
  269. {
  270. visitor.visitEraseArtifact(*this);
  271. }
  272. void MoveArtifact::visitTyped(ICPackVisitor & visitor)
  273. {
  274. visitor.visitMoveArtifact(*this);
  275. }
  276. void BulkMoveArtifacts::visitTyped(ICPackVisitor & visitor)
  277. {
  278. visitor.visitBulkMoveArtifacts(*this);
  279. }
  280. void AssembledArtifact::visitTyped(ICPackVisitor & visitor)
  281. {
  282. visitor.visitAssembledArtifact(*this);
  283. }
  284. void DisassembledArtifact::visitTyped(ICPackVisitor & visitor)
  285. {
  286. visitor.visitDisassembledArtifact(*this);
  287. }
  288. void HeroVisit::visitTyped(ICPackVisitor & visitor)
  289. {
  290. visitor.visitHeroVisit(*this);
  291. }
  292. void NewTurn::visitTyped(ICPackVisitor & visitor)
  293. {
  294. visitor.visitNewTurn(*this);
  295. }
  296. void InfoWindow::visitTyped(ICPackVisitor & visitor)
  297. {
  298. visitor.visitInfoWindow(*this);
  299. }
  300. void SetObjectProperty::visitTyped(ICPackVisitor & visitor)
  301. {
  302. visitor.visitSetObjectProperty(*this);
  303. }
  304. void ChangeObjectVisitors::visitTyped(ICPackVisitor & visitor)
  305. {
  306. visitor.visitChangeObjectVisitors(*this);
  307. }
  308. void PrepareHeroLevelUp::visitTyped(ICPackVisitor & visitor)
  309. {
  310. visitor.visitPrepareHeroLevelUp(*this);
  311. }
  312. void HeroLevelUp::visitTyped(ICPackVisitor & visitor)
  313. {
  314. visitor.visitHeroLevelUp(*this);
  315. }
  316. void CommanderLevelUp::visitTyped(ICPackVisitor & visitor)
  317. {
  318. visitor.visitCommanderLevelUp(*this);
  319. }
  320. void BlockingDialog::visitTyped(ICPackVisitor & visitor)
  321. {
  322. visitor.visitBlockingDialog(*this);
  323. }
  324. void GarrisonDialog::visitTyped(ICPackVisitor & visitor)
  325. {
  326. visitor.visitGarrisonDialog(*this);
  327. }
  328. void ExchangeDialog::visitTyped(ICPackVisitor & visitor)
  329. {
  330. visitor.visitExchangeDialog(*this);
  331. }
  332. void TeleportDialog::visitTyped(ICPackVisitor & visitor)
  333. {
  334. visitor.visitTeleportDialog(*this);
  335. }
  336. void MapObjectSelectDialog::visitTyped(ICPackVisitor & visitor)
  337. {
  338. visitor.visitMapObjectSelectDialog(*this);
  339. }
  340. void BattleStart::visitTyped(ICPackVisitor & visitor)
  341. {
  342. visitor.visitBattleStart(*this);
  343. }
  344. void BattleNextRound::visitTyped(ICPackVisitor & visitor)
  345. {
  346. visitor.visitBattleNextRound(*this);
  347. }
  348. void BattleSetActiveStack::visitTyped(ICPackVisitor & visitor)
  349. {
  350. visitor.visitBattleSetActiveStack(*this);
  351. }
  352. void BattleResult::visitTyped(ICPackVisitor & visitor)
  353. {
  354. visitor.visitBattleResult(*this);
  355. }
  356. void BattleLogMessage::visitTyped(ICPackVisitor & visitor)
  357. {
  358. visitor.visitBattleLogMessage(*this);
  359. }
  360. void BattleStackMoved::visitTyped(ICPackVisitor & visitor)
  361. {
  362. visitor.visitBattleStackMoved(*this);
  363. }
  364. void BattleUnitsChanged::visitTyped(ICPackVisitor & visitor)
  365. {
  366. visitor.visitBattleUnitsChanged(*this);
  367. }
  368. void BattleAttack::visitTyped(ICPackVisitor & visitor)
  369. {
  370. visitor.visitBattleAttack(*this);
  371. }
  372. void StartAction::visitTyped(ICPackVisitor & visitor)
  373. {
  374. visitor.visitStartAction(*this);
  375. }
  376. void EndAction::visitTyped(ICPackVisitor & visitor)
  377. {
  378. visitor.visitEndAction(*this);
  379. }
  380. void BattleSpellCast::visitTyped(ICPackVisitor & visitor)
  381. {
  382. visitor.visitBattleSpellCast(*this);
  383. }
  384. void SetStackEffect::visitTyped(ICPackVisitor & visitor)
  385. {
  386. visitor.visitSetStackEffect(*this);
  387. }
  388. void StacksInjured::visitTyped(ICPackVisitor & visitor)
  389. {
  390. visitor.visitStacksInjured(*this);
  391. }
  392. void BattleResultsApplied::visitTyped(ICPackVisitor & visitor)
  393. {
  394. visitor.visitBattleResultsApplied(*this);
  395. }
  396. void BattleObstaclesChanged::visitTyped(ICPackVisitor & visitor)
  397. {
  398. visitor.visitBattleObstaclesChanged(*this);
  399. }
  400. void BattleSetStackProperty::visitTyped(ICPackVisitor & visitor)
  401. {
  402. visitor.visitBattleSetStackProperty(*this);
  403. }
  404. void BattleTriggerEffect::visitTyped(ICPackVisitor & visitor)
  405. {
  406. visitor.visitBattleTriggerEffect(*this);
  407. }
  408. void BattleUpdateGateState::visitTyped(ICPackVisitor & visitor)
  409. {
  410. visitor.visitBattleUpdateGateState(*this);
  411. }
  412. void AdvmapSpellCast::visitTyped(ICPackVisitor & visitor)
  413. {
  414. visitor.visitAdvmapSpellCast(*this);
  415. }
  416. void ShowWorldViewEx::visitTyped(ICPackVisitor & visitor)
  417. {
  418. visitor.visitShowWorldViewEx(*this);
  419. }
  420. void EndTurn::visitTyped(ICPackVisitor & visitor)
  421. {
  422. visitor.visitEndTurn(*this);
  423. }
  424. void DismissHero::visitTyped(ICPackVisitor & visitor)
  425. {
  426. visitor.visitDismissHero(*this);
  427. }
  428. void MoveHero::visitTyped(ICPackVisitor & visitor)
  429. {
  430. visitor.visitMoveHero(*this);
  431. }
  432. void CastleTeleportHero::visitTyped(ICPackVisitor & visitor)
  433. {
  434. visitor.visitCastleTeleportHero(*this);
  435. }
  436. void ArrangeStacks::visitTyped(ICPackVisitor & visitor)
  437. {
  438. visitor.visitArrangeStacks(*this);
  439. }
  440. void BulkMoveArmy::visitTyped(ICPackVisitor & visitor)
  441. {
  442. visitor.visitBulkMoveArmy(*this);
  443. }
  444. void BulkSplitStack::visitTyped(ICPackVisitor & visitor)
  445. {
  446. visitor.visitBulkSplitStack(*this);
  447. }
  448. void BulkMergeStacks::visitTyped(ICPackVisitor & visitor)
  449. {
  450. visitor.visitBulkMergeStacks(*this);
  451. }
  452. void BulkSmartSplitStack::visitTyped(ICPackVisitor & visitor)
  453. {
  454. visitor.visitBulkSmartSplitStack(*this);
  455. }
  456. void DisbandCreature::visitTyped(ICPackVisitor & visitor)
  457. {
  458. visitor.visitDisbandCreature(*this);
  459. }
  460. void BuildStructure::visitTyped(ICPackVisitor & visitor)
  461. {
  462. visitor.visitBuildStructure(*this);
  463. }
  464. void RazeStructure::visitTyped(ICPackVisitor & visitor)
  465. {
  466. visitor.visitRazeStructure(*this);
  467. }
  468. void RecruitCreatures::visitTyped(ICPackVisitor & visitor)
  469. {
  470. visitor.visitRecruitCreatures(*this);
  471. }
  472. void UpgradeCreature::visitTyped(ICPackVisitor & visitor)
  473. {
  474. visitor.visitUpgradeCreature(*this);
  475. }
  476. void GarrisonHeroSwap::visitTyped(ICPackVisitor & visitor)
  477. {
  478. visitor.visitGarrisonHeroSwap(*this);
  479. }
  480. void ExchangeArtifacts::visitTyped(ICPackVisitor & visitor)
  481. {
  482. visitor.visitExchangeArtifacts(*this);
  483. }
  484. void BulkExchangeArtifacts::visitTyped(ICPackVisitor & visitor)
  485. {
  486. visitor.visitBulkExchangeArtifacts(*this);
  487. }
  488. void AssembleArtifacts::visitTyped(ICPackVisitor & visitor)
  489. {
  490. visitor.visitAssembleArtifacts(*this);
  491. }
  492. void EraseArtifactByClient::visitTyped(ICPackVisitor & visitor)
  493. {
  494. visitor.visitEraseArtifactByClient(*this);
  495. }
  496. void BuyArtifact::visitTyped(ICPackVisitor & visitor)
  497. {
  498. visitor.visitBuyArtifact(*this);
  499. }
  500. void TradeOnMarketplace::visitTyped(ICPackVisitor & visitor)
  501. {
  502. visitor.visitTradeOnMarketplace(*this);
  503. }
  504. void SetFormation::visitTyped(ICPackVisitor & visitor)
  505. {
  506. visitor.visitSetFormation(*this);
  507. }
  508. void HireHero::visitTyped(ICPackVisitor & visitor)
  509. {
  510. visitor.visitHireHero(*this);
  511. }
  512. void BuildBoat::visitTyped(ICPackVisitor & visitor)
  513. {
  514. visitor.visitBuildBoat(*this);
  515. }
  516. void QueryReply::visitTyped(ICPackVisitor & visitor)
  517. {
  518. visitor.visitQueryReply(*this);
  519. }
  520. void MakeAction::visitTyped(ICPackVisitor & visitor)
  521. {
  522. visitor.visitMakeAction(*this);
  523. }
  524. void DigWithHero::visitTyped(ICPackVisitor & visitor)
  525. {
  526. visitor.visitDigWithHero(*this);
  527. }
  528. void CastAdvSpell::visitTyped(ICPackVisitor & visitor)
  529. {
  530. visitor.visitCastAdvSpell(*this);
  531. }
  532. void SaveGame::visitTyped(ICPackVisitor & visitor)
  533. {
  534. visitor.visitSaveGame(*this);
  535. }
  536. void PlayerMessage::visitTyped(ICPackVisitor & visitor)
  537. {
  538. visitor.visitPlayerMessage(*this);
  539. }
  540. void PlayerMessageClient::visitTyped(ICPackVisitor & visitor)
  541. {
  542. visitor.visitPlayerMessageClient(*this);
  543. }
  544. void CenterView::visitTyped(ICPackVisitor & visitor)
  545. {
  546. visitor.visitCenterView(*this);
  547. }
  548. void LobbyClientConnected::visitTyped(ICPackVisitor & visitor)
  549. {
  550. visitor.visitLobbyClientConnected(*this);
  551. }
  552. void LobbyClientDisconnected::visitTyped(ICPackVisitor & visitor)
  553. {
  554. visitor.visitLobbyClientDisconnected(*this);
  555. }
  556. void LobbyChatMessage::visitTyped(ICPackVisitor & visitor)
  557. {
  558. visitor.visitLobbyChatMessage(*this);
  559. }
  560. void LobbyGuiAction::visitTyped(ICPackVisitor & visitor)
  561. {
  562. visitor.visitLobbyGuiAction(*this);
  563. }
  564. void LobbyLoadProgress::visitTyped(ICPackVisitor & visitor)
  565. {
  566. visitor.visitLobbyLoadProgress(*this);
  567. }
  568. void LobbyEndGame::visitTyped(ICPackVisitor & visitor)
  569. {
  570. visitor.visitLobbyEndGame(*this);
  571. }
  572. void LobbyStartGame::visitTyped(ICPackVisitor & visitor)
  573. {
  574. visitor.visitLobbyStartGame(*this);
  575. }
  576. void LobbyChangeHost::visitTyped(ICPackVisitor & visitor)
  577. {
  578. visitor.visitLobbyChangeHost(*this);
  579. }
  580. void LobbyUpdateState::visitTyped(ICPackVisitor & visitor)
  581. {
  582. visitor.visitLobbyUpdateState(*this);
  583. }
  584. void LobbySetMap::visitTyped(ICPackVisitor & visitor)
  585. {
  586. visitor.visitLobbySetMap(*this);
  587. }
  588. void LobbySetCampaign::visitTyped(ICPackVisitor & visitor)
  589. {
  590. visitor.visitLobbySetCampaign(*this);
  591. }
  592. void LobbySetCampaignMap::visitTyped(ICPackVisitor & visitor)
  593. {
  594. visitor.visitLobbySetCampaignMap(*this);
  595. }
  596. void LobbySetCampaignBonus::visitTyped(ICPackVisitor & visitor)
  597. {
  598. visitor.visitLobbySetCampaignBonus(*this);
  599. }
  600. void LobbyChangePlayerOption::visitTyped(ICPackVisitor & visitor)
  601. {
  602. visitor.visitLobbyChangePlayerOption(*this);
  603. }
  604. void LobbySetPlayer::visitTyped(ICPackVisitor & visitor)
  605. {
  606. visitor.visitLobbySetPlayer(*this);
  607. }
  608. void LobbySetSimturns::visitTyped(ICPackVisitor & visitor)
  609. {
  610. visitor.visitLobbySetSimturns(*this);
  611. }
  612. void LobbySetTurnTime::visitTyped(ICPackVisitor & visitor)
  613. {
  614. visitor.visitLobbySetTurnTime(*this);
  615. }
  616. void LobbySetDifficulty::visitTyped(ICPackVisitor & visitor)
  617. {
  618. visitor.visitLobbySetDifficulty(*this);
  619. }
  620. void LobbyForceSetPlayer::visitTyped(ICPackVisitor & visitor)
  621. {
  622. visitor.visitLobbyForceSetPlayer(*this);
  623. }
  624. void LobbyShowMessage::visitTyped(ICPackVisitor & visitor)
  625. {
  626. visitor.visitLobbyShowMessage(*this);
  627. }
  628. void SetResources::applyGs(CGameState * gs) const
  629. {
  630. assert(player.isValidPlayer());
  631. if(abs)
  632. gs->getPlayerState(player)->resources = res;
  633. else
  634. gs->getPlayerState(player)->resources += res;
  635. //just ensure that player resources are not negative
  636. //server is responsible to check if player can afford deal
  637. //but events on server side are allowed to take more than player have
  638. gs->getPlayerState(player)->resources.positive();
  639. }
  640. void SetPrimSkill::applyGs(CGameState * gs) const
  641. {
  642. CGHeroInstance * hero = gs->getHero(id);
  643. assert(hero);
  644. hero->setPrimarySkill(which, val, abs);
  645. }
  646. void SetSecSkill::applyGs(CGameState * gs) const
  647. {
  648. CGHeroInstance *hero = gs->getHero(id);
  649. hero->setSecSkillLevel(which, val, abs);
  650. }
  651. void SetCommanderProperty::applyGs(CGameState *gs)
  652. {
  653. CCommanderInstance * commander = gs->getHero(heroid)->commander;
  654. assert (commander);
  655. switch (which)
  656. {
  657. case BONUS:
  658. commander->accumulateBonus (std::make_shared<Bonus>(accumulatedBonus));
  659. break;
  660. case SPECIAL_SKILL:
  661. commander->accumulateBonus (std::make_shared<Bonus>(accumulatedBonus));
  662. commander->specialSkills.insert (additionalInfo);
  663. break;
  664. case SECONDARY_SKILL:
  665. commander->secondarySkills[additionalInfo] = static_cast<ui8>(amount);
  666. break;
  667. case ALIVE:
  668. if (amount)
  669. commander->setAlive(true);
  670. else
  671. commander->setAlive(false);
  672. break;
  673. case EXPERIENCE:
  674. commander->giveStackExp(amount); //TODO: allow setting exp for stacks via netpacks
  675. break;
  676. }
  677. }
  678. void AddQuest::applyGs(CGameState * gs) const
  679. {
  680. assert (vstd::contains(gs->players, player));
  681. auto * vec = &gs->players[player].quests;
  682. if (!vstd::contains(*vec, quest))
  683. vec->push_back (quest);
  684. else
  685. logNetwork->warn("Warning! Attempt to add duplicated quest");
  686. }
  687. void UpdateArtHandlerLists::applyGs(CGameState * gs) const
  688. {
  689. VLC->arth->minors = minors;
  690. VLC->arth->majors = majors;
  691. VLC->arth->treasures = treasures;
  692. VLC->arth->relics = relics;
  693. }
  694. void UpdateMapEvents::applyGs(CGameState * gs) const
  695. {
  696. gs->map->events = events;
  697. }
  698. void UpdateCastleEvents::applyGs(CGameState * gs) const
  699. {
  700. auto * t = gs->getTown(town);
  701. t->events = events;
  702. }
  703. void ChangeFormation::applyGs(CGameState * gs) const
  704. {
  705. gs->getHero(hid)->setFormation(formation);
  706. }
  707. void HeroVisitCastle::applyGs(CGameState * gs) const
  708. {
  709. CGHeroInstance *h = gs->getHero(hid);
  710. CGTownInstance *t = gs->getTown(tid);
  711. assert(h);
  712. assert(t);
  713. if(start())
  714. t->setVisitingHero(h);
  715. else
  716. t->setVisitingHero(nullptr);
  717. }
  718. void ChangeSpells::applyGs(CGameState *gs)
  719. {
  720. CGHeroInstance *hero = gs->getHero(hid);
  721. if(learn)
  722. for(const auto & sid : spells)
  723. hero->addSpellToSpellbook(sid);
  724. else
  725. for(const auto & sid : spells)
  726. hero->removeSpellFromSpellbook(sid);
  727. }
  728. void SetMana::applyGs(CGameState * gs) const
  729. {
  730. CGHeroInstance * hero = gs->getHero(hid);
  731. assert(hero);
  732. if(absolute)
  733. hero->mana = val;
  734. else
  735. hero->mana += val;
  736. vstd::amax(hero->mana, 0); //not less than 0
  737. }
  738. void SetMovePoints::applyGs(CGameState * gs) const
  739. {
  740. CGHeroInstance *hero = gs->getHero(hid);
  741. assert(hero);
  742. if(absolute)
  743. hero->setMovementPoints(val);
  744. else
  745. hero->setMovementPoints(hero->movementPointsRemaining() + val);
  746. }
  747. void FoWChange::applyGs(CGameState *gs)
  748. {
  749. TeamState * team = gs->getPlayerTeam(player);
  750. auto fogOfWarMap = team->fogOfWarMap;
  751. for(const int3 & t : tiles)
  752. (*fogOfWarMap)[t.z][t.x][t.y] = mode;
  753. if (mode == 0) //do not hide too much
  754. {
  755. std::unordered_set<int3> tilesRevealed;
  756. for (auto & elem : gs->map->objects)
  757. {
  758. const CGObjectInstance *o = elem;
  759. if (o)
  760. {
  761. switch(o->ID)
  762. {
  763. case Obj::HERO:
  764. case Obj::MINE:
  765. case Obj::TOWN:
  766. case Obj::ABANDONED_MINE:
  767. if(vstd::contains(team->players, o->tempOwner)) //check owned observators
  768. gs->getTilesInRange(tilesRevealed, o->getSightCenter(), o->getSightRadius(), o->tempOwner, 1);
  769. break;
  770. }
  771. }
  772. }
  773. for(const int3 & t : tilesRevealed) //probably not the most optimal solution ever
  774. (*fogOfWarMap)[t.z][t.x][t.y] = 1;
  775. }
  776. }
  777. void SetAvailableHero::applyGs(CGameState *gs)
  778. {
  779. gs->heroesPool->setHeroForPlayer(player, slotID, hid, army, roleID);
  780. }
  781. void GiveBonus::applyGs(CGameState *gs)
  782. {
  783. CBonusSystemNode *cbsn = nullptr;
  784. switch(who)
  785. {
  786. case ETarget::HERO:
  787. cbsn = gs->getHero(ObjectInstanceID(id));
  788. break;
  789. case ETarget::PLAYER:
  790. cbsn = gs->getPlayerState(PlayerColor(id));
  791. break;
  792. case ETarget::TOWN:
  793. cbsn = gs->getTown(ObjectInstanceID(id));
  794. break;
  795. case ETarget::BATTLE:
  796. assert(Bonus::OneBattle(&bonus));
  797. cbsn = dynamic_cast<CBonusSystemNode*>(gs->getBattle(BattleID(id)));
  798. break;
  799. }
  800. assert(cbsn);
  801. if(Bonus::OneWeek(&bonus))
  802. bonus.turnsRemain = 8 - gs->getDate(Date::DAY_OF_WEEK); // set correct number of days before adding bonus
  803. auto b = std::make_shared<Bonus>(bonus);
  804. cbsn->addNewBonus(b);
  805. std::string &descr = b->description;
  806. if(bdescr.empty() && (bonus.type == BonusType::LUCK || bonus.type == BonusType::MORALE))
  807. {
  808. if (bonus.source == BonusSource::OBJECT)
  809. {
  810. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  811. }
  812. else if(bonus.source == BonusSource::TOWN_STRUCTURE)
  813. {
  814. descr = bonus.description;
  815. return;
  816. }
  817. else
  818. {
  819. descr = bdescr.toString();
  820. }
  821. }
  822. else
  823. {
  824. descr = bdescr.toString();
  825. }
  826. // Some of(?) versions of H3 use %s here instead of %d. Try to replace both of them
  827. boost::replace_first(descr, "%d", std::to_string(std::abs(bonus.val)));
  828. boost::replace_first(descr, "%s", std::to_string(std::abs(bonus.val)));
  829. }
  830. void ChangeObjPos::applyGs(CGameState *gs)
  831. {
  832. CGObjectInstance *obj = gs->getObjInstance(objid);
  833. if(!obj)
  834. {
  835. logNetwork->error("Wrong ChangeObjPos: object %d doesn't exist!", objid.getNum());
  836. return;
  837. }
  838. gs->map->removeBlockVisTiles(obj);
  839. obj->pos = nPos + obj->getVisitableOffset();
  840. gs->map->addBlockVisTiles(obj);
  841. }
  842. void ChangeObjectVisitors::applyGs(CGameState * gs) const
  843. {
  844. switch (mode) {
  845. case VISITOR_ADD:
  846. gs->getHero(hero)->visitedObjects.insert(object);
  847. gs->getPlayerState(gs->getHero(hero)->tempOwner)->visitedObjects.insert(object);
  848. break;
  849. case VISITOR_ADD_TEAM:
  850. {
  851. TeamState *ts = gs->getPlayerTeam(gs->getHero(hero)->tempOwner);
  852. for(const auto & color : ts->players)
  853. {
  854. gs->getPlayerState(color)->visitedObjects.insert(object);
  855. }
  856. }
  857. break;
  858. case VISITOR_CLEAR:
  859. for (CGHeroInstance * hero : gs->map->allHeroes)
  860. {
  861. if (hero)
  862. {
  863. hero->visitedObjects.erase(object); // remove visit info from all heroes, including those that are not present on map
  864. }
  865. }
  866. for(auto &elem : gs->players)
  867. {
  868. elem.second.visitedObjects.erase(object);
  869. }
  870. break;
  871. case VISITOR_REMOVE:
  872. gs->getHero(hero)->visitedObjects.erase(object);
  873. break;
  874. }
  875. }
  876. void PlayerEndsGame::applyGs(CGameState * gs) const
  877. {
  878. PlayerState *p = gs->getPlayerState(player);
  879. if(victoryLossCheckResult.victory())
  880. {
  881. p->status = EPlayerStatus::WINNER;
  882. // TODO: Campaign-specific code might as well go somewhere else
  883. // keep all heroes from the winning player
  884. if(p->human && gs->scenarioOps->campState)
  885. {
  886. std::vector<CGHeroInstance *> crossoverHeroes;
  887. for (CGHeroInstance * hero : gs->map->heroesOnMap)
  888. if (hero->tempOwner == player)
  889. crossoverHeroes.push_back(hero);
  890. gs->scenarioOps->campState->setCurrentMapAsConquered(crossoverHeroes);
  891. }
  892. }
  893. else
  894. {
  895. p->status = EPlayerStatus::LOSER;
  896. }
  897. // defeated player may be making turn right now
  898. gs->actingPlayers.erase(player);
  899. }
  900. void PlayerReinitInterface::applyGs(CGameState *gs)
  901. {
  902. if(!gs || !gs->scenarioOps)
  903. return;
  904. //TODO: what does mean if more that one player connected?
  905. if(playerConnectionId == PlayerSettings::PLAYER_AI)
  906. {
  907. for(const auto & player : players)
  908. gs->scenarioOps->getIthPlayersSettings(player).connectedPlayerIDs.clear();
  909. }
  910. }
  911. void RemoveBonus::applyGs(CGameState *gs)
  912. {
  913. CBonusSystemNode * node = nullptr;
  914. if (who == GiveBonus::ETarget::HERO)
  915. node = gs->getHero(ObjectInstanceID(whoID));
  916. else
  917. node = gs->getPlayerState(PlayerColor(whoID));
  918. BonusList &bonuses = node->getExportedBonusList();
  919. for(const auto & b : bonuses)
  920. {
  921. if(vstd::to_underlying(b->source) == source && b->sid == id)
  922. {
  923. bonus = *b; //backup bonus (to show to interfaces later)
  924. node->removeBonus(b);
  925. break;
  926. }
  927. }
  928. }
  929. void RemoveObject::applyGs(CGameState *gs)
  930. {
  931. CGObjectInstance *obj = gs->getObjInstance(objectID);
  932. logGlobal->debug("removing object id=%d; address=%x; name=%s", objectID, (intptr_t)obj, obj->getObjectName());
  933. //unblock tiles
  934. gs->map->removeBlockVisTiles(obj);
  935. if(obj->ID == Obj::HERO) //remove beaten hero
  936. {
  937. auto * beatenHero = dynamic_cast<CGHeroInstance *>(obj);
  938. assert(beatenHero);
  939. PlayerState * p = gs->getPlayerState(beatenHero->tempOwner);
  940. gs->map->heroesOnMap -= beatenHero;
  941. p->heroes -= beatenHero;
  942. auto * siegeNode = beatenHero->whereShouldBeAttachedOnSiege(gs);
  943. // FIXME: workaround:
  944. // hero should be attached to siegeNode after battle
  945. // however this code might also be called on dismissing hero while in town
  946. if (siegeNode && vstd::contains(beatenHero->getParentNodes(), siegeNode))
  947. beatenHero->detachFrom(*siegeNode);
  948. beatenHero->tempOwner = PlayerColor::NEUTRAL; //no one owns beaten hero
  949. vstd::erase_if(beatenHero->artifactsInBackpack, [](const ArtSlotInfo& asi)
  950. {
  951. return asi.artifact->artType->getId() == ArtifactID::GRAIL;
  952. });
  953. if(beatenHero->visitedTown)
  954. {
  955. if(beatenHero->visitedTown->garrisonHero == beatenHero)
  956. beatenHero->visitedTown->garrisonHero = nullptr;
  957. else
  958. beatenHero->visitedTown->visitingHero = nullptr;
  959. beatenHero->visitedTown = nullptr;
  960. beatenHero->inTownGarrison = false;
  961. }
  962. //return hero to the pool, so he may reappear in tavern
  963. gs->heroesPool->addHeroToPool(beatenHero);
  964. gs->map->objects[objectID.getNum()] = nullptr;
  965. //If hero on Boat is removed, the Boat disappears
  966. if(beatenHero->boat)
  967. {
  968. beatenHero->detachFrom(const_cast<CGBoat&>(*beatenHero->boat));
  969. gs->map->instanceNames.erase(beatenHero->boat->instanceName);
  970. gs->map->objects[beatenHero->boat->id.getNum()].dellNull();
  971. beatenHero->boat = nullptr;
  972. }
  973. return;
  974. }
  975. const auto * quest = dynamic_cast<const IQuestObject *>(obj);
  976. if (quest)
  977. {
  978. gs->map->quests[quest->quest->qid] = nullptr;
  979. for (auto &player : gs->players)
  980. {
  981. for (auto &q : player.second.quests)
  982. {
  983. if (q.obj == obj)
  984. {
  985. q.obj = nullptr;
  986. }
  987. }
  988. }
  989. }
  990. for (TriggeredEvent & event : gs->map->triggeredEvents)
  991. {
  992. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  993. {
  994. if (cond.object == obj)
  995. {
  996. if (cond.condition == EventCondition::DESTROY || cond.condition == EventCondition::DESTROY_0)
  997. {
  998. cond.condition = EventCondition::CONST_VALUE;
  999. cond.value = 1; // destroyed object, from now on always fulfilled
  1000. }
  1001. else if (cond.condition == EventCondition::CONTROL || cond.condition == EventCondition::HAVE_0)
  1002. {
  1003. cond.condition = EventCondition::CONST_VALUE;
  1004. cond.value = 0; // destroyed object, from now on can not be fulfilled
  1005. }
  1006. }
  1007. return cond;
  1008. };
  1009. event.trigger = event.trigger.morph(patcher);
  1010. }
  1011. gs->map->instanceNames.erase(obj->instanceName);
  1012. gs->map->objects[objectID.getNum()].dellNull();
  1013. gs->map->calculateGuardingGreaturePositions();
  1014. }
  1015. static int getDir(const int3 & src, const int3 & dst)
  1016. {
  1017. int ret = -1;
  1018. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  1019. {
  1020. ret = 1;
  1021. }
  1022. else if(dst.x == src.x && dst.y+1 == src.y) //t
  1023. {
  1024. ret = 2;
  1025. }
  1026. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  1027. {
  1028. ret = 3;
  1029. }
  1030. else if(dst.x-1 == src.x && dst.y == src.y) //r
  1031. {
  1032. ret = 4;
  1033. }
  1034. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  1035. {
  1036. ret = 5;
  1037. }
  1038. else if(dst.x == src.x && dst.y-1 == src.y) //b
  1039. {
  1040. ret = 6;
  1041. }
  1042. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  1043. {
  1044. ret = 7;
  1045. }
  1046. else if(dst.x+1 == src.x && dst.y == src.y) //l
  1047. {
  1048. ret = 8;
  1049. }
  1050. return ret;
  1051. }
  1052. void TryMoveHero::applyGs(CGameState *gs)
  1053. {
  1054. CGHeroInstance *h = gs->getHero(id);
  1055. if (!h)
  1056. {
  1057. logGlobal->error("Attempt ot move unavailable hero %d", id.getNum());
  1058. return;
  1059. }
  1060. h->setMovementPoints(movePoints);
  1061. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end)
  1062. {
  1063. auto dir = getDir(start,end);
  1064. if(dir > 0 && dir <= 8)
  1065. h->moveDir = dir;
  1066. //else don`t change move direction - hero might have traversed the subterranean gate, direction should be kept
  1067. }
  1068. if(result == EMBARK) //hero enters boat at destination tile
  1069. {
  1070. const TerrainTile &tt = gs->map->getTile(h->convertToVisitablePos(end));
  1071. assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == Obj::BOAT); //the only visitable object at destination is Boat
  1072. auto * boat = dynamic_cast<CGBoat *>(tt.visitableObjects.back());
  1073. assert(boat);
  1074. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  1075. h->boat = boat;
  1076. h->attachTo(*boat);
  1077. boat->hero = h;
  1078. }
  1079. else if(result == DISEMBARK) //hero leaves boat to destination tile
  1080. {
  1081. auto * b = const_cast<CGBoat *>(h->boat);
  1082. b->direction = h->moveDir;
  1083. b->pos = start;
  1084. b->hero = nullptr;
  1085. gs->map->addBlockVisTiles(b);
  1086. h->detachFrom(*b);
  1087. h->boat = nullptr;
  1088. }
  1089. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  1090. {
  1091. gs->map->removeBlockVisTiles(h);
  1092. h->pos = end;
  1093. if(auto * b = const_cast<CGBoat *>(h->boat))
  1094. b->pos = end;
  1095. gs->map->addBlockVisTiles(h);
  1096. }
  1097. auto fogOfWarMap = gs->getPlayerTeam(h->getOwner())->fogOfWarMap;
  1098. for(const int3 & t : fowRevealed)
  1099. (*fogOfWarMap)[t.z][t.x][t.y] = 1;
  1100. }
  1101. void NewStructures::applyGs(CGameState *gs)
  1102. {
  1103. CGTownInstance *t = gs->getTown(tid);
  1104. for(const auto & id : bid)
  1105. {
  1106. assert(t->town->buildings.at(id) != nullptr);
  1107. t->builtBuildings.insert(id);
  1108. t->updateAppearance();
  1109. auto currentBuilding = t->town->buildings.at(id);
  1110. if(currentBuilding->overrideBids.empty())
  1111. continue;
  1112. for(const auto & overrideBid : currentBuilding->overrideBids)
  1113. {
  1114. t->overriddenBuildings.insert(overrideBid);
  1115. t->deleteTownBonus(overrideBid);
  1116. }
  1117. }
  1118. t->builded = builded;
  1119. t->recreateBuildingsBonuses();
  1120. }
  1121. void RazeStructures::applyGs(CGameState *gs)
  1122. {
  1123. CGTownInstance *t = gs->getTown(tid);
  1124. for(const auto & id : bid)
  1125. {
  1126. t->builtBuildings.erase(id);
  1127. t->updateAppearance();
  1128. }
  1129. t->destroyed = destroyed; //yeaha
  1130. t->recreateBuildingsBonuses();
  1131. }
  1132. void SetAvailableCreatures::applyGs(CGameState * gs) const
  1133. {
  1134. auto * dw = dynamic_cast<CGDwelling *>(gs->getObjInstance(tid));
  1135. assert(dw);
  1136. dw->creatures = creatures;
  1137. }
  1138. void SetHeroesInTown::applyGs(CGameState * gs) const
  1139. {
  1140. CGTownInstance *t = gs->getTown(tid);
  1141. CGHeroInstance * v = gs->getHero(visiting);
  1142. CGHeroInstance * g = gs->getHero(garrison);
  1143. bool newVisitorComesFromGarrison = v && v == t->garrisonHero;
  1144. bool newGarrisonComesFromVisiting = g && g == t->visitingHero;
  1145. if(newVisitorComesFromGarrison)
  1146. t->setGarrisonedHero(nullptr);
  1147. if(newGarrisonComesFromVisiting)
  1148. t->setVisitingHero(nullptr);
  1149. if(!newGarrisonComesFromVisiting || v)
  1150. t->setVisitingHero(v);
  1151. if(!newVisitorComesFromGarrison || g)
  1152. t->setGarrisonedHero(g);
  1153. if(v)
  1154. {
  1155. gs->map->addBlockVisTiles(v);
  1156. }
  1157. if(g)
  1158. {
  1159. gs->map->removeBlockVisTiles(g);
  1160. }
  1161. }
  1162. void HeroRecruited::applyGs(CGameState * gs) const
  1163. {
  1164. CGHeroInstance *h = gs->heroesPool->takeHeroFromPool(hid);
  1165. CGTownInstance *t = gs->getTown(tid);
  1166. PlayerState *p = gs->getPlayerState(player);
  1167. if (boatId != ObjectInstanceID::NONE)
  1168. {
  1169. CGObjectInstance *obj = gs->getObjInstance(boatId);
  1170. auto * boat = dynamic_cast<CGBoat *>(obj);
  1171. if (boat)
  1172. {
  1173. gs->map->removeBlockVisTiles(boat);
  1174. h->attachToBoat(boat);
  1175. }
  1176. }
  1177. h->setOwner(player);
  1178. h->pos = tile;
  1179. h->initObj(gs->getRandomGenerator());
  1180. if(h->id == ObjectInstanceID())
  1181. {
  1182. h->id = ObjectInstanceID(static_cast<si32>(gs->map->objects.size()));
  1183. gs->map->objects.emplace_back(h);
  1184. }
  1185. else
  1186. gs->map->objects[h->id.getNum()] = h;
  1187. gs->map->heroesOnMap.emplace_back(h);
  1188. p->heroes.emplace_back(h);
  1189. h->attachTo(*p);
  1190. gs->map->addBlockVisTiles(h);
  1191. if(t)
  1192. t->setVisitingHero(h);
  1193. }
  1194. void GiveHero::applyGs(CGameState * gs) const
  1195. {
  1196. CGHeroInstance *h = gs->getHero(id);
  1197. if (boatId != ObjectInstanceID::NONE)
  1198. {
  1199. CGObjectInstance *obj = gs->getObjInstance(boatId);
  1200. auto * boat = dynamic_cast<CGBoat *>(obj);
  1201. if (boat)
  1202. {
  1203. gs->map->removeBlockVisTiles(boat);
  1204. h->attachToBoat(boat);
  1205. }
  1206. }
  1207. //bonus system
  1208. h->detachFrom(gs->globalEffects);
  1209. h->attachTo(*gs->getPlayerState(player));
  1210. auto oldVisitablePos = h->visitablePos();
  1211. gs->map->removeBlockVisTiles(h,true);
  1212. h->appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, h->type->heroClass->getIndex())->getTemplates().front();
  1213. h->setOwner(player);
  1214. h->setMovementPoints(h->movementPointsLimit(true));
  1215. h->pos = h->convertFromVisitablePos(oldVisitablePos);
  1216. gs->map->heroesOnMap.emplace_back(h);
  1217. gs->getPlayerState(h->getOwner())->heroes.emplace_back(h);
  1218. gs->map->addBlockVisTiles(h);
  1219. h->inTownGarrison = false;
  1220. }
  1221. void NewObject::applyGs(CGameState *gs)
  1222. {
  1223. TerrainId terrainType = ETerrainId::NONE;
  1224. if (!gs->isInTheMap(targetPos))
  1225. {
  1226. logGlobal->error("Attempt to create object outside map at %s!", targetPos.toString());
  1227. return;
  1228. }
  1229. const TerrainTile & t = gs->map->getTile(targetPos);
  1230. terrainType = t.terType->getId();
  1231. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  1232. CGObjectInstance * o = handler->create();
  1233. handler->configureObject(o, gs->getRandomGenerator());
  1234. if (ID == Obj::MONSTER) //probably more options will be needed
  1235. {
  1236. //CStackInstance hlp;
  1237. auto * cre = dynamic_cast<CGCreature *>(o);
  1238. //cre->slots[0] = hlp;
  1239. assert(cre);
  1240. cre->notGrowingTeam = cre->neverFlees = false;
  1241. cre->character = 2;
  1242. cre->gainedArtifact = ArtifactID::NONE;
  1243. cre->identifier = -1;
  1244. cre->addToSlot(SlotID(0), new CStackInstance(CreatureID(subID), -1)); //add placeholder stack
  1245. }
  1246. assert(!handler->getTemplates(terrainType).empty());
  1247. if (handler->getTemplates().empty())
  1248. {
  1249. logGlobal->error("Attempt to create object (%d %d) with no templates!", ID, subID);
  1250. return;
  1251. }
  1252. if (!handler->getTemplates(terrainType).empty())
  1253. o->appearance = handler->getTemplates(terrainType).front();
  1254. else
  1255. o->appearance = handler->getTemplates().front();
  1256. o->id = ObjectInstanceID(static_cast<si32>(gs->map->objects.size()));
  1257. o->ID = ID;
  1258. o->subID = subID;
  1259. o->pos = targetPos + o->getVisitableOffset();
  1260. gs->map->objects.emplace_back(o);
  1261. gs->map->addBlockVisTiles(o);
  1262. o->initObj(gs->getRandomGenerator());
  1263. gs->map->calculateGuardingGreaturePositions();
  1264. createdObjectID = o->id;
  1265. logGlobal->debug("Added object id=%d; address=%x; name=%s", o->id, (intptr_t)o, o->getObjectName());
  1266. }
  1267. void NewArtifact::applyGs(CGameState *gs)
  1268. {
  1269. assert(!vstd::contains(gs->map->artInstances, art));
  1270. assert(!art->getParentNodes().size());
  1271. assert(art->artType);
  1272. art->setType(art->artType);
  1273. if(art->isCombined())
  1274. {
  1275. for(const auto & part : art->artType->getConstituents())
  1276. art->addPart(ArtifactUtils::createNewArtifactInstance(part), ArtifactPosition::PRE_FIRST);
  1277. }
  1278. gs->map->addNewArtifactInstance(art);
  1279. }
  1280. const CStackInstance * StackLocation::getStack()
  1281. {
  1282. if(!army->hasStackAtSlot(slot))
  1283. {
  1284. logNetwork->warn("%s don't have a stack at slot %d", army->nodeName(), slot.getNum());
  1285. return nullptr;
  1286. }
  1287. return &army->getStack(slot);
  1288. }
  1289. struct ObjectRetriever
  1290. {
  1291. const CArmedInstance * operator()(const ConstTransitivePtr<CGHeroInstance> &h) const
  1292. {
  1293. return h;
  1294. }
  1295. const CArmedInstance * operator()(const ConstTransitivePtr<CStackInstance> &s) const
  1296. {
  1297. return s->armyObj;
  1298. }
  1299. };
  1300. template<typename T>
  1301. struct GetBase
  1302. {
  1303. template <typename TArg>
  1304. T * operator()(TArg &arg) const
  1305. {
  1306. return arg;
  1307. }
  1308. };
  1309. void ArtifactLocation::removeArtifact()
  1310. {
  1311. CArtifactInstance *a = getArt();
  1312. assert(a);
  1313. a->removeFrom(*this);
  1314. }
  1315. const CArmedInstance * ArtifactLocation::relatedObj() const
  1316. {
  1317. return std::visit(ObjectRetriever(), artHolder);
  1318. }
  1319. PlayerColor ArtifactLocation::owningPlayer() const
  1320. {
  1321. const auto * obj = relatedObj();
  1322. return obj ? obj->tempOwner : PlayerColor::NEUTRAL;
  1323. }
  1324. CArtifactSet *ArtifactLocation::getHolderArtSet()
  1325. {
  1326. return std::visit(GetBase<CArtifactSet>(), artHolder);
  1327. }
  1328. CBonusSystemNode *ArtifactLocation::getHolderNode()
  1329. {
  1330. return std::visit(GetBase<CBonusSystemNode>(), artHolder);
  1331. }
  1332. const CArtifactInstance *ArtifactLocation::getArt() const
  1333. {
  1334. const auto * s = getSlot();
  1335. if(s)
  1336. return s->getArt();
  1337. else
  1338. return nullptr;
  1339. }
  1340. CArtifactSet * ArtifactLocation::getHolderArtSet() const
  1341. {
  1342. auto * t = const_cast<ArtifactLocation *>(this);
  1343. return t->getHolderArtSet();
  1344. }
  1345. const CBonusSystemNode * ArtifactLocation::getHolderNode() const
  1346. {
  1347. auto * t = const_cast<ArtifactLocation *>(this);
  1348. return t->getHolderNode();
  1349. }
  1350. CArtifactInstance *ArtifactLocation::getArt()
  1351. {
  1352. const ArtifactLocation *t = this;
  1353. return const_cast<CArtifactInstance*>(t->getArt());
  1354. }
  1355. const ArtSlotInfo *ArtifactLocation::getSlot() const
  1356. {
  1357. return getHolderArtSet()->getSlot(slot);
  1358. }
  1359. void ChangeStackCount::applyGs(CGameState * gs)
  1360. {
  1361. auto * srcObj = gs->getArmyInstance(army);
  1362. if(!srcObj)
  1363. logNetwork->error("[CRITICAL] ChangeStackCount: invalid army object %d, possible game state corruption.", army.getNum());
  1364. if(absoluteValue)
  1365. srcObj->setStackCount(slot, count);
  1366. else
  1367. srcObj->changeStackCount(slot, count);
  1368. }
  1369. void SetStackType::applyGs(CGameState * gs)
  1370. {
  1371. auto * srcObj = gs->getArmyInstance(army);
  1372. if(!srcObj)
  1373. logNetwork->error("[CRITICAL] SetStackType: invalid army object %d, possible game state corruption.", army.getNum());
  1374. srcObj->setStackType(slot, type);
  1375. }
  1376. void EraseStack::applyGs(CGameState * gs)
  1377. {
  1378. auto * srcObj = gs->getArmyInstance(army);
  1379. if(!srcObj)
  1380. logNetwork->error("[CRITICAL] EraseStack: invalid army object %d, possible game state corruption.", army.getNum());
  1381. srcObj->eraseStack(slot);
  1382. }
  1383. void SwapStacks::applyGs(CGameState * gs)
  1384. {
  1385. auto * srcObj = gs->getArmyInstance(srcArmy);
  1386. if(!srcObj)
  1387. logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum());
  1388. auto * dstObj = gs->getArmyInstance(dstArmy);
  1389. if(!dstObj)
  1390. logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum());
  1391. CStackInstance * s1 = srcObj->detachStack(srcSlot);
  1392. CStackInstance * s2 = dstObj->detachStack(dstSlot);
  1393. srcObj->putStack(srcSlot, s2);
  1394. dstObj->putStack(dstSlot, s1);
  1395. }
  1396. void InsertNewStack::applyGs(CGameState *gs)
  1397. {
  1398. if(auto * obj = gs->getArmyInstance(army))
  1399. obj->putStack(slot, new CStackInstance(type, count));
  1400. else
  1401. logNetwork->error("[CRITICAL] InsertNewStack: invalid army object %d, possible game state corruption.", army.getNum());
  1402. }
  1403. void RebalanceStacks::applyGs(CGameState * gs)
  1404. {
  1405. auto * srcObj = gs->getArmyInstance(srcArmy);
  1406. if(!srcObj)
  1407. logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum());
  1408. auto * dstObj = gs->getArmyInstance(dstArmy);
  1409. if(!dstObj)
  1410. logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum());
  1411. StackLocation src(srcObj, srcSlot);
  1412. StackLocation dst(dstObj, dstSlot);
  1413. const CCreature * srcType = src.army->getCreature(src.slot);
  1414. TQuantity srcCount = src.army->getStackCount(src.slot);
  1415. bool stackExp = VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE);
  1416. if(srcCount == count) //moving whole stack
  1417. {
  1418. [[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot);
  1419. if(c) //stack at dest -> merge
  1420. {
  1421. assert(c == srcType);
  1422. auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT);
  1423. auto alDest = ArtifactLocation (dst.getStack(), ArtifactPosition::CREATURE_SLOT);
  1424. auto * artHere = alHere.getArt();
  1425. auto * artDest = alDest.getArt();
  1426. if (artHere)
  1427. {
  1428. if (alDest.getArt())
  1429. {
  1430. auto * hero = dynamic_cast<CGHeroInstance *>(src.army.get());
  1431. auto dstSlot = ArtifactUtils::getArtBackpackPosition(hero, alDest.getArt()->getTypeId());
  1432. if(hero && dstSlot != ArtifactPosition::PRE_FIRST)
  1433. {
  1434. artDest->move (alDest, ArtifactLocation (hero, dstSlot));
  1435. }
  1436. //else - artifact cna be lost :/
  1437. else
  1438. {
  1439. EraseArtifact ea;
  1440. ea.al = alDest;
  1441. ea.applyGs(gs);
  1442. logNetwork->warn("Cannot move artifact! No free slots");
  1443. }
  1444. artHere->move (alHere, alDest);
  1445. //TODO: choose from dialog
  1446. }
  1447. else //just move to the other slot before stack gets erased
  1448. {
  1449. artHere->move (alHere, alDest);
  1450. }
  1451. }
  1452. if (stackExp)
  1453. {
  1454. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  1455. src.army->eraseStack(src.slot);
  1456. dst.army->changeStackCount(dst.slot, count);
  1457. dst.army->setStackExp(dst.slot, totalExp /(dst.army->getStackCount(dst.slot))); //mean
  1458. }
  1459. else
  1460. {
  1461. src.army->eraseStack(src.slot);
  1462. dst.army->changeStackCount(dst.slot, count);
  1463. }
  1464. }
  1465. else //move stack to an empty slot, no exp change needed
  1466. {
  1467. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  1468. dst.army->putStack(dst.slot, stackDetached);
  1469. }
  1470. }
  1471. else
  1472. {
  1473. [[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot);
  1474. if(c) //stack at dest -> rebalance
  1475. {
  1476. assert(c == srcType);
  1477. if (stackExp)
  1478. {
  1479. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  1480. src.army->changeStackCount(src.slot, -count);
  1481. dst.army->changeStackCount(dst.slot, count);
  1482. dst.army->setStackExp(dst.slot, totalExp /(src.army->getStackCount(src.slot) + dst.army->getStackCount(dst.slot))); //mean
  1483. }
  1484. else
  1485. {
  1486. src.army->changeStackCount(src.slot, -count);
  1487. dst.army->changeStackCount(dst.slot, count);
  1488. }
  1489. }
  1490. else //split stack to an empty slot
  1491. {
  1492. src.army->changeStackCount(src.slot, -count);
  1493. dst.army->addToSlot(dst.slot, srcType->getId(), count, false);
  1494. if (stackExp)
  1495. dst.army->setStackExp(dst.slot, src.army->getStackExperience(src.slot));
  1496. }
  1497. }
  1498. CBonusSystemNode::treeHasChanged();
  1499. }
  1500. void BulkRebalanceStacks::applyGs(CGameState * gs)
  1501. {
  1502. for(auto & move : moves)
  1503. move.applyGs(gs);
  1504. }
  1505. void BulkSmartRebalanceStacks::applyGs(CGameState * gs)
  1506. {
  1507. for(auto & move : moves)
  1508. move.applyGs(gs);
  1509. for(auto & change : changes)
  1510. change.applyGs(gs);
  1511. }
  1512. void PutArtifact::applyGs(CGameState *gs)
  1513. {
  1514. assert(art->canBePutAt(al));
  1515. // Ensure that artifact has been correctly added via NewArtifact pack
  1516. assert(vstd::contains(gs->map->artInstances, art));
  1517. assert(!art->getParentNodes().empty());
  1518. art->putAt(al);
  1519. }
  1520. void EraseArtifact::applyGs(CGameState *gs)
  1521. {
  1522. const auto * slot = al.getSlot();
  1523. if(slot->locked)
  1524. {
  1525. logGlobal->debug("Erasing locked artifact: %s", slot->artifact->artType->getNameTranslated());
  1526. DisassembledArtifact dis;
  1527. dis.al.artHolder = al.artHolder;
  1528. auto * aset = al.getHolderArtSet();
  1529. #ifndef NDEBUG
  1530. bool found = false;
  1531. #endif
  1532. for(auto& p : aset->artifactsWorn)
  1533. {
  1534. auto art = p.second.artifact;
  1535. if(art->isCombined() && art->isPart(slot->artifact))
  1536. {
  1537. dis.al.slot = aset->getArtPos(art);
  1538. #ifndef NDEBUG
  1539. found = true;
  1540. #endif
  1541. break;
  1542. }
  1543. }
  1544. assert(found && "Failed to determine the assembly this locked artifact belongs to");
  1545. logGlobal->debug("Found the corresponding assembly: %s", dis.al.getSlot()->artifact->artType->getNameTranslated());
  1546. dis.applyGs(gs);
  1547. }
  1548. else
  1549. {
  1550. logGlobal->debug("Erasing artifact %s", slot->artifact->artType->getNameTranslated());
  1551. }
  1552. al.removeArtifact();
  1553. }
  1554. void MoveArtifact::applyGs(CGameState * gs)
  1555. {
  1556. CArtifactInstance * art = src.getArt();
  1557. assert(!ArtifactUtils::isSlotEquipment(dst.slot) || !dst.getArt());
  1558. art->move(src, dst);
  1559. }
  1560. void BulkMoveArtifacts::applyGs(CGameState * gs)
  1561. {
  1562. enum class EBulkArtsOp
  1563. {
  1564. BULK_MOVE,
  1565. BULK_REMOVE,
  1566. BULK_PUT
  1567. };
  1568. auto bulkArtsOperation = [this](std::vector<LinkedSlots> & artsPack,
  1569. CArtifactSet * artSet, EBulkArtsOp operation) -> void
  1570. {
  1571. int numBackpackArtifactsMoved = 0;
  1572. for(auto & slot : artsPack)
  1573. {
  1574. // When an object gets removed from the backpack, the backpack shrinks
  1575. // so all the following indices will be affected. Thus, we need to update
  1576. // the subsequent artifact slots to account for that
  1577. auto srcPos = slot.srcPos;
  1578. if(ArtifactUtils::isSlotBackpack(srcPos) && (operation != EBulkArtsOp::BULK_PUT))
  1579. {
  1580. srcPos = ArtifactPosition(srcPos.num - numBackpackArtifactsMoved);
  1581. }
  1582. const auto * slotInfo = artSet->getSlot(srcPos);
  1583. assert(slotInfo);
  1584. auto * art = const_cast<CArtifactInstance *>(slotInfo->getArt());
  1585. assert(art);
  1586. switch(operation)
  1587. {
  1588. case EBulkArtsOp::BULK_MOVE:
  1589. const_cast<CArtifactInstance*>(art)->move(
  1590. ArtifactLocation(srcArtHolder, srcPos), ArtifactLocation(dstArtHolder, slot.dstPos));
  1591. break;
  1592. case EBulkArtsOp::BULK_REMOVE:
  1593. art->removeFrom(ArtifactLocation(dstArtHolder, srcPos));
  1594. break;
  1595. case EBulkArtsOp::BULK_PUT:
  1596. art->putAt(ArtifactLocation(srcArtHolder, slot.dstPos));
  1597. break;
  1598. default:
  1599. break;
  1600. }
  1601. if(srcPos >= ArtifactPosition::BACKPACK_START)
  1602. {
  1603. numBackpackArtifactsMoved++;
  1604. }
  1605. }
  1606. };
  1607. if(swap)
  1608. {
  1609. // Swap
  1610. auto * leftSet = getSrcHolderArtSet();
  1611. auto * rightSet = getDstHolderArtSet();
  1612. CArtifactFittingSet artFittingSet(leftSet->bearerType());
  1613. artFittingSet.artifactsWorn = rightSet->artifactsWorn;
  1614. artFittingSet.artifactsInBackpack = rightSet->artifactsInBackpack;
  1615. bulkArtsOperation(artsPack1, rightSet, EBulkArtsOp::BULK_REMOVE);
  1616. bulkArtsOperation(artsPack0, leftSet, EBulkArtsOp::BULK_MOVE);
  1617. bulkArtsOperation(artsPack1, &artFittingSet, EBulkArtsOp::BULK_PUT);
  1618. }
  1619. else
  1620. {
  1621. bulkArtsOperation(artsPack0, getSrcHolderArtSet(), EBulkArtsOp::BULK_MOVE);
  1622. }
  1623. }
  1624. void AssembledArtifact::applyGs(CGameState *gs)
  1625. {
  1626. CArtifactSet * artSet = al.getHolderArtSet();
  1627. [[maybe_unused]] const CArtifactInstance *transformedArt = al.getArt();
  1628. assert(transformedArt);
  1629. bool combineEquipped = !ArtifactUtils::isSlotBackpack(al.slot);
  1630. assert(vstd::contains_if(ArtifactUtils::assemblyPossibilities(artSet, transformedArt->artType->getId(), combineEquipped), [=](const CArtifact * art)->bool
  1631. {
  1632. return art->getId() == builtArt->getId();
  1633. }));
  1634. auto * combinedArt = new CArtifactInstance(builtArt);
  1635. gs->map->addNewArtifactInstance(combinedArt);
  1636. // Retrieve all constituents
  1637. for(const CArtifact * constituent : builtArt->getConstituents())
  1638. {
  1639. ArtifactPosition pos = combineEquipped ? artSet->getArtPos(constituent->getId(), true, false) :
  1640. artSet->getArtBackpackPos(constituent->getId());
  1641. assert(pos != ArtifactPosition::PRE_FIRST);
  1642. CArtifactInstance * constituentInstance = artSet->getArt(pos);
  1643. //move constituent from hero to be part of new, combined artifact
  1644. constituentInstance->removeFrom(ArtifactLocation(al.artHolder, pos));
  1645. if(combineEquipped)
  1646. {
  1647. if(!vstd::contains(combinedArt->artType->getPossibleSlots().at(artSet->bearerType()), al.slot)
  1648. && vstd::contains(combinedArt->artType->getPossibleSlots().at(artSet->bearerType()), pos))
  1649. al.slot = pos;
  1650. if(al.slot == pos)
  1651. pos = ArtifactPosition::PRE_FIRST;
  1652. }
  1653. else
  1654. {
  1655. al.slot = std::min(al.slot, pos);
  1656. pos = ArtifactPosition::PRE_FIRST;
  1657. }
  1658. combinedArt->addPart(constituentInstance, pos);
  1659. }
  1660. //put new combined artifacts
  1661. combinedArt->putAt(al);
  1662. }
  1663. void DisassembledArtifact::applyGs(CGameState *gs)
  1664. {
  1665. auto * disassembled = al.getArt();
  1666. assert(disassembled);
  1667. auto parts = disassembled->getPartsInfo();
  1668. disassembled->removeFrom(al);
  1669. for(auto & part : parts)
  1670. {
  1671. ArtifactLocation partLoc = al;
  1672. // ArtifactPosition::PRE_FIRST is value of main part slot -> it'll replace combined artifact in its pos
  1673. partLoc.slot = (ArtifactUtils::isSlotEquipment(part.slot) ? part.slot : al.slot);
  1674. disassembled->detachFrom(*part.art);
  1675. part.art->putAt(partLoc);
  1676. }
  1677. gs->map->eraseArtifactInstance(disassembled);
  1678. }
  1679. void HeroVisit::applyGs(CGameState *gs)
  1680. {
  1681. }
  1682. void SetAvailableArtifacts::applyGs(CGameState * gs) const
  1683. {
  1684. if(id >= 0)
  1685. {
  1686. if(auto * bm = dynamic_cast<CGBlackMarket *>(gs->map->objects[id].get()))
  1687. {
  1688. bm->artifacts = arts;
  1689. }
  1690. else
  1691. {
  1692. logNetwork->error("Wrong black market id!");
  1693. }
  1694. }
  1695. else
  1696. {
  1697. CGTownInstance::merchantArtifacts = arts;
  1698. }
  1699. }
  1700. void NewTurn::applyGs(CGameState *gs)
  1701. {
  1702. gs->day = day;
  1703. // Update bonuses before doing anything else so hero don't get more MP than needed
  1704. gs->globalEffects.removeBonusesRecursive(Bonus::OneDay); //works for children -> all game objs
  1705. gs->globalEffects.reduceBonusDurations(Bonus::NDays);
  1706. gs->globalEffects.reduceBonusDurations(Bonus::OneWeek);
  1707. //TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
  1708. for(const NewTurn::Hero & h : heroes) //give mana/movement point
  1709. {
  1710. CGHeroInstance *hero = gs->getHero(h.id);
  1711. if(!hero)
  1712. {
  1713. logGlobal->error("Hero %d not found in NewTurn::applyGs", h.id.getNum());
  1714. continue;
  1715. }
  1716. hero->setMovementPoints(h.move);
  1717. hero->mana = h.mana;
  1718. }
  1719. gs->heroesPool->onNewDay();
  1720. for(const auto & re : res)
  1721. {
  1722. assert(re.first.isValidPlayer());
  1723. gs->getPlayerState(re.first)->resources = re.second;
  1724. }
  1725. for(const auto & creatureSet : cres) //set available creatures in towns
  1726. creatureSet.second.applyGs(gs);
  1727. for(CGTownInstance* t : gs->map->towns)
  1728. t->builded = 0;
  1729. if(gs->getDate(Date::DAY_OF_WEEK) == 1)
  1730. gs->updateRumor();
  1731. }
  1732. void SetObjectProperty::applyGs(CGameState * gs) const
  1733. {
  1734. CGObjectInstance *obj = gs->getObjInstance(id);
  1735. if(!obj)
  1736. {
  1737. logNetwork->error("Wrong object ID - property cannot be set!");
  1738. return;
  1739. }
  1740. auto * cai = dynamic_cast<CArmedInstance *>(obj);
  1741. if(what == ObjProperty::OWNER && cai)
  1742. {
  1743. if(obj->ID == Obj::TOWN)
  1744. {
  1745. auto * t = dynamic_cast<CGTownInstance *>(obj);
  1746. assert(t);
  1747. PlayerColor oldOwner = t->tempOwner;
  1748. if(oldOwner.isValidPlayer())
  1749. {
  1750. auto * state = gs->getPlayerState(oldOwner);
  1751. state->towns -= t;
  1752. if(state->towns.empty())
  1753. *state->daysWithoutCastle = 0;
  1754. }
  1755. if(PlayerColor(val).isValidPlayer())
  1756. {
  1757. PlayerState * p = gs->getPlayerState(PlayerColor(val));
  1758. p->towns.emplace_back(t);
  1759. //reset counter before NewTurn to avoid no town message if game loaded at turn when one already captured
  1760. if(p->daysWithoutCastle)
  1761. p->daysWithoutCastle = std::nullopt;
  1762. }
  1763. }
  1764. CBonusSystemNode & nodeToMove = cai->whatShouldBeAttached();
  1765. nodeToMove.detachFrom(cai->whereShouldBeAttached(gs));
  1766. obj->setProperty(what,val);
  1767. nodeToMove.attachTo(cai->whereShouldBeAttached(gs));
  1768. }
  1769. else //not an armed instance
  1770. {
  1771. obj->setProperty(what,val);
  1772. }
  1773. }
  1774. void PrepareHeroLevelUp::applyGs(CGameState * gs)
  1775. {
  1776. auto * hero = gs->getHero(heroId);
  1777. assert(hero);
  1778. auto proposedSkills = hero->getLevelUpProposedSecondarySkills();
  1779. if(skills.size() == 1 || hero->tempOwner == PlayerColor::NEUTRAL) //choose skill automatically
  1780. {
  1781. skills.push_back(*RandomGeneratorUtil::nextItem(proposedSkills, hero->skillsInfo.rand));
  1782. }
  1783. else
  1784. {
  1785. skills = proposedSkills;
  1786. }
  1787. }
  1788. void HeroLevelUp::applyGs(CGameState * gs) const
  1789. {
  1790. auto * hero = gs->getHero(heroId);
  1791. assert(hero);
  1792. hero->levelUp(skills);
  1793. }
  1794. void CommanderLevelUp::applyGs(CGameState * gs) const
  1795. {
  1796. auto * hero = gs->getHero(heroId);
  1797. assert(hero);
  1798. auto commander = hero->commander;
  1799. assert(commander);
  1800. commander->levelUp();
  1801. }
  1802. void BattleStart::applyGs(CGameState * gs) const
  1803. {
  1804. assert(battleID == gs->nextBattleID);
  1805. gs->currentBattles.emplace_back(info);
  1806. info->battleID = gs->nextBattleID;
  1807. info->localInit();
  1808. gs->nextBattleID = vstd::next(gs->nextBattleID, 1);
  1809. }
  1810. void BattleNextRound::applyGs(CGameState * gs) const
  1811. {
  1812. gs->getBattle(battleID)->nextRound();
  1813. }
  1814. void BattleSetActiveStack::applyGs(CGameState * gs) const
  1815. {
  1816. gs->getBattle(battleID)->nextTurn(stack);
  1817. }
  1818. void BattleTriggerEffect::applyGs(CGameState * gs) const
  1819. {
  1820. CStack * st = gs->getBattle(battleID)->getStack(stackID);
  1821. assert(st);
  1822. switch(static_cast<BonusType>(effect))
  1823. {
  1824. case BonusType::HP_REGENERATION:
  1825. {
  1826. int64_t toHeal = val;
  1827. st->heal(toHeal, EHealLevel::HEAL, EHealPower::PERMANENT);
  1828. break;
  1829. }
  1830. case BonusType::MANA_DRAIN:
  1831. {
  1832. CGHeroInstance * h = gs->getHero(ObjectInstanceID(additionalInfo));
  1833. st->drainedMana = true;
  1834. h->mana -= val;
  1835. vstd::amax(h->mana, 0);
  1836. break;
  1837. }
  1838. case BonusType::POISON:
  1839. {
  1840. auto b = st->getBonusLocalFirst(Selector::source(BonusSource::SPELL_EFFECT, SpellID::POISON)
  1841. .And(Selector::type()(BonusType::STACK_HEALTH)));
  1842. if (b)
  1843. b->val = val;
  1844. break;
  1845. }
  1846. case BonusType::ENCHANTER:
  1847. case BonusType::MORALE:
  1848. break;
  1849. case BonusType::FEAR:
  1850. st->fear = true;
  1851. break;
  1852. default:
  1853. logNetwork->error("Unrecognized trigger effect type %d", effect);
  1854. }
  1855. }
  1856. void BattleUpdateGateState::applyGs(CGameState * gs) const
  1857. {
  1858. if(gs->getBattle(battleID))
  1859. gs->getBattle(battleID)->si.gateState = state;
  1860. }
  1861. void BattleCancelled::applyGs(CGameState * gs) const
  1862. {
  1863. auto currentBattle = boost::range::find_if(gs->currentBattles, [&](const auto & battle)
  1864. {
  1865. return battle->battleID == battleID;
  1866. });
  1867. assert(currentBattle != gs->currentBattles.end());
  1868. gs->currentBattles.erase(currentBattle);
  1869. }
  1870. void BattleResultAccepted::applyGs(CGameState * gs) const
  1871. {
  1872. // Remove any "until next battle" bonuses
  1873. for(auto & res : heroResult)
  1874. {
  1875. if(res.hero)
  1876. res.hero->removeBonusesRecursive(Bonus::OneBattle);
  1877. }
  1878. if(winnerSide != 2)
  1879. {
  1880. // Grow up growing artifacts
  1881. const auto hero = heroResult[winnerSide].hero;
  1882. if (hero)
  1883. {
  1884. if(hero->commander && hero->commander->alive)
  1885. {
  1886. for(auto & art : hero->commander->artifactsWorn)
  1887. art.second.artifact->growingUp();
  1888. }
  1889. for(auto & art : hero->artifactsWorn)
  1890. {
  1891. art.second.artifact->growingUp();
  1892. }
  1893. }
  1894. }
  1895. if(VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE))
  1896. {
  1897. if(heroResult[0].army)
  1898. heroResult[0].army->giveStackExp(heroResult[0].exp);
  1899. if(heroResult[1].army)
  1900. heroResult[1].army->giveStackExp(heroResult[1].exp);
  1901. CBonusSystemNode::treeHasChanged();
  1902. }
  1903. auto currentBattle = boost::range::find_if(gs->currentBattles, [&](const auto & battle)
  1904. {
  1905. return battle->battleID == battleID;
  1906. });
  1907. assert(currentBattle != gs->currentBattles.end());
  1908. gs->currentBattles.erase(currentBattle);
  1909. }
  1910. void BattleLogMessage::applyGs(CGameState *gs)
  1911. {
  1912. //nothing
  1913. }
  1914. void BattleLogMessage::applyBattle(IBattleState * battleState)
  1915. {
  1916. //nothing
  1917. }
  1918. void BattleStackMoved::applyGs(CGameState *gs)
  1919. {
  1920. applyBattle(gs->getBattle(battleID));
  1921. }
  1922. void BattleStackMoved::applyBattle(IBattleState * battleState)
  1923. {
  1924. battleState->moveUnit(stack, tilesToMove.back());
  1925. }
  1926. void BattleStackAttacked::applyGs(CGameState * gs)
  1927. {
  1928. applyBattle(gs->getBattle(battleID));
  1929. }
  1930. void BattleStackAttacked::applyBattle(IBattleState * battleState)
  1931. {
  1932. battleState->setUnitState(newState.id, newState.data, newState.healthDelta);
  1933. }
  1934. void BattleAttack::applyGs(CGameState * gs)
  1935. {
  1936. CStack * attacker = gs->getBattle(battleID)->getStack(stackAttacking);
  1937. assert(attacker);
  1938. attackerChanges.applyGs(gs);
  1939. for(BattleStackAttacked & stackAttacked : bsa)
  1940. stackAttacked.applyGs(gs);
  1941. attacker->removeBonusesRecursive(Bonus::UntilAttack);
  1942. }
  1943. void StartAction::applyGs(CGameState *gs)
  1944. {
  1945. CStack *st = gs->getBattle(battleID)->getStack(ba.stackNumber);
  1946. if(ba.actionType == EActionType::END_TACTIC_PHASE)
  1947. {
  1948. gs->getBattle(battleID)->tacticDistance = 0;
  1949. return;
  1950. }
  1951. if(gs->getBattle(battleID)->tacticDistance)
  1952. {
  1953. // moves in tactics phase do not affect creature status
  1954. // (tactics stack queue is managed by client)
  1955. return;
  1956. }
  1957. if (ba.isUnitAction())
  1958. {
  1959. assert(st); // stack must exists for all non-hero actions
  1960. switch(ba.actionType)
  1961. {
  1962. case EActionType::DEFEND:
  1963. st->waiting = false;
  1964. st->defending = true;
  1965. st->defendingAnim = true;
  1966. break;
  1967. case EActionType::WAIT:
  1968. st->defendingAnim = false;
  1969. st->waiting = true;
  1970. st->waitedThisTurn = true;
  1971. break;
  1972. case EActionType::HERO_SPELL: //no change in current stack state
  1973. break;
  1974. default: //any active stack action - attack, catapult, heal, spell...
  1975. st->waiting = false;
  1976. st->defendingAnim = false;
  1977. st->movedThisRound = true;
  1978. break;
  1979. }
  1980. }
  1981. else
  1982. {
  1983. if(ba.actionType == EActionType::HERO_SPELL)
  1984. gs->getBattle(battleID)->sides[ba.side].usedSpellsHistory.push_back(ba.spell);
  1985. }
  1986. }
  1987. void BattleSpellCast::applyGs(CGameState * gs) const
  1988. {
  1989. if(castByHero)
  1990. {
  1991. if(side < 2)
  1992. {
  1993. gs->getBattle(battleID)->sides[side].castSpellsCount++;
  1994. }
  1995. }
  1996. }
  1997. void SetStackEffect::applyGs(CGameState *gs)
  1998. {
  1999. applyBattle(gs->getBattle(battleID));
  2000. }
  2001. void SetStackEffect::applyBattle(IBattleState * battleState)
  2002. {
  2003. for(const auto & stackData : toRemove)
  2004. battleState->removeUnitBonus(stackData.first, stackData.second);
  2005. for(const auto & stackData : toUpdate)
  2006. battleState->updateUnitBonus(stackData.first, stackData.second);
  2007. for(const auto & stackData : toAdd)
  2008. battleState->addUnitBonus(stackData.first, stackData.second);
  2009. }
  2010. void StacksInjured::applyGs(CGameState *gs)
  2011. {
  2012. applyBattle(gs->getBattle(battleID));
  2013. }
  2014. void StacksInjured::applyBattle(IBattleState * battleState)
  2015. {
  2016. for(BattleStackAttacked stackAttacked : stacks)
  2017. stackAttacked.applyBattle(battleState);
  2018. }
  2019. void BattleUnitsChanged::applyGs(CGameState *gs)
  2020. {
  2021. applyBattle(gs->getBattle(battleID));
  2022. }
  2023. void BattleUnitsChanged::applyBattle(IBattleState * battleState)
  2024. {
  2025. for(auto & elem : changedStacks)
  2026. {
  2027. switch(elem.operation)
  2028. {
  2029. case BattleChanges::EOperation::RESET_STATE:
  2030. battleState->setUnitState(elem.id, elem.data, elem.healthDelta);
  2031. break;
  2032. case BattleChanges::EOperation::REMOVE:
  2033. battleState->removeUnit(elem.id);
  2034. break;
  2035. case BattleChanges::EOperation::ADD:
  2036. battleState->addUnit(elem.id, elem.data);
  2037. break;
  2038. case BattleChanges::EOperation::UPDATE:
  2039. battleState->updateUnit(elem.id, elem.data);
  2040. break;
  2041. default:
  2042. logNetwork->error("Unknown unit operation %d", static_cast<int>(elem.operation));
  2043. break;
  2044. }
  2045. }
  2046. }
  2047. void BattleObstaclesChanged::applyGs(CGameState * gs)
  2048. {
  2049. applyBattle(gs->getBattle(battleID));
  2050. }
  2051. void BattleObstaclesChanged::applyBattle(IBattleState * battleState)
  2052. {
  2053. for(const auto & change : changes)
  2054. {
  2055. switch(change.operation)
  2056. {
  2057. case BattleChanges::EOperation::REMOVE:
  2058. battleState->removeObstacle(change.id);
  2059. break;
  2060. case BattleChanges::EOperation::ADD:
  2061. battleState->addObstacle(change);
  2062. break;
  2063. case BattleChanges::EOperation::UPDATE:
  2064. battleState->updateObstacle(change);
  2065. break;
  2066. default:
  2067. logNetwork->error("Unknown obstacle operation %d", static_cast<int>(change.operation));
  2068. break;
  2069. }
  2070. }
  2071. }
  2072. CatapultAttack::CatapultAttack() = default;
  2073. CatapultAttack::~CatapultAttack() = default;
  2074. void CatapultAttack::applyGs(CGameState * gs)
  2075. {
  2076. applyBattle(gs->getBattle(battleID));
  2077. }
  2078. void CatapultAttack::visitTyped(ICPackVisitor & visitor)
  2079. {
  2080. visitor.visitCatapultAttack(*this);
  2081. }
  2082. void CatapultAttack::applyBattle(IBattleState * battleState)
  2083. {
  2084. const auto * town = battleState->getDefendedTown();
  2085. if(!town)
  2086. return;
  2087. if(town->fortLevel() == CGTownInstance::NONE)
  2088. return;
  2089. for(const auto & part : attackedParts)
  2090. {
  2091. auto newWallState = SiegeInfo::applyDamage(battleState->getWallState(part.attackedPart), part.damageDealt);
  2092. battleState->setWallState(part.attackedPart, newWallState);
  2093. }
  2094. }
  2095. void BattleSetStackProperty::applyGs(CGameState * gs) const
  2096. {
  2097. CStack * stack = gs->getBattle(battleID)->getStack(stackID);
  2098. switch(which)
  2099. {
  2100. case CASTS:
  2101. {
  2102. if(absolute)
  2103. logNetwork->error("Can not change casts in absolute mode");
  2104. else
  2105. stack->casts.use(-val);
  2106. break;
  2107. }
  2108. case ENCHANTER_COUNTER:
  2109. {
  2110. auto & counter = gs->getBattle(battleID)->sides[gs->getBattle(battleID)->whatSide(stack->unitOwner())].enchanterCounter;
  2111. if(absolute)
  2112. counter = val;
  2113. else
  2114. counter += val;
  2115. vstd::amax(counter, 0);
  2116. break;
  2117. }
  2118. case UNBIND:
  2119. {
  2120. stack->removeBonusesRecursive(Selector::type()(BonusType::BIND_EFFECT));
  2121. break;
  2122. }
  2123. case CLONED:
  2124. {
  2125. stack->cloned = true;
  2126. break;
  2127. }
  2128. case HAS_CLONE:
  2129. {
  2130. stack->cloneID = val;
  2131. break;
  2132. }
  2133. }
  2134. }
  2135. void PlayerCheated::applyGs(CGameState * gs) const
  2136. {
  2137. if(!player.isValidPlayer())
  2138. return;
  2139. gs->getPlayerState(player)->enteredLosingCheatCode = losingCheatCode;
  2140. gs->getPlayerState(player)->enteredWinningCheatCode = winningCheatCode;
  2141. gs->getPlayerState(player)->cheated = true;
  2142. }
  2143. void PlayerStartsTurn::applyGs(CGameState * gs) const
  2144. {
  2145. assert(gs->actingPlayers.count(player) == 0);
  2146. gs->actingPlayers.insert(player);
  2147. }
  2148. void PlayerEndsTurn::applyGs(CGameState * gs) const
  2149. {
  2150. assert(gs->actingPlayers.count(player) == 1);
  2151. gs->actingPlayers.erase(player);
  2152. }
  2153. void DaysWithoutTown::applyGs(CGameState * gs) const
  2154. {
  2155. auto & playerState = gs->players[player];
  2156. playerState.daysWithoutCastle = daysWithoutCastle;
  2157. }
  2158. void TurnTimeUpdate::applyGs(CGameState *gs) const
  2159. {
  2160. auto & playerState = gs->players[player];
  2161. playerState.turnTimer = turnTimer;
  2162. }
  2163. Component::Component(const CStackBasicDescriptor & stack)
  2164. : id(EComponentType::CREATURE)
  2165. , subtype(stack.type->getId())
  2166. , val(stack.count)
  2167. {
  2168. }
  2169. void EntitiesChanged::applyGs(CGameState * gs)
  2170. {
  2171. for(const auto & change : changes)
  2172. gs->updateEntity(change.metatype, change.entityIndex, change.data);
  2173. }
  2174. const CArtifactInstance * ArtSlotInfo::getArt() const
  2175. {
  2176. if(locked)
  2177. {
  2178. logNetwork->warn("ArtifactLocation::getArt: This location is locked!");
  2179. return nullptr;
  2180. }
  2181. return artifact;
  2182. }
  2183. CArtifactSet * BulkMoveArtifacts::getSrcHolderArtSet()
  2184. {
  2185. return std::visit(GetBase<CArtifactSet>(), srcArtHolder);
  2186. }
  2187. CArtifactSet * BulkMoveArtifacts::getDstHolderArtSet()
  2188. {
  2189. return std::visit(GetBase<CArtifactSet>(), dstArtHolder);
  2190. }
  2191. VCMI_LIB_NAMESPACE_END