NetPacksLib.cpp 62 KB

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