NetPacksLib.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  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 LobbySetTurnTime::visitTyped(ICPackVisitor & visitor)
  609. {
  610. visitor.visitLobbySetTurnTime(*this);
  611. }
  612. void LobbySetDifficulty::visitTyped(ICPackVisitor & visitor)
  613. {
  614. visitor.visitLobbySetDifficulty(*this);
  615. }
  616. void LobbyForceSetPlayer::visitTyped(ICPackVisitor & visitor)
  617. {
  618. visitor.visitLobbyForceSetPlayer(*this);
  619. }
  620. void LobbyShowMessage::visitTyped(ICPackVisitor & visitor)
  621. {
  622. visitor.visitLobbyShowMessage(*this);
  623. }
  624. void SetResources::applyGs(CGameState * gs) const
  625. {
  626. assert(player.isValidPlayer());
  627. if(abs)
  628. gs->getPlayerState(player)->resources = res;
  629. else
  630. gs->getPlayerState(player)->resources += res;
  631. //just ensure that player resources are not negative
  632. //server is responsible to check if player can afford deal
  633. //but events on server side are allowed to take more than player have
  634. gs->getPlayerState(player)->resources.positive();
  635. }
  636. void SetPrimSkill::applyGs(CGameState * gs) const
  637. {
  638. CGHeroInstance * hero = gs->getHero(id);
  639. assert(hero);
  640. hero->setPrimarySkill(which, val, abs);
  641. }
  642. void SetSecSkill::applyGs(CGameState * gs) const
  643. {
  644. CGHeroInstance *hero = gs->getHero(id);
  645. hero->setSecSkillLevel(which, val, abs);
  646. }
  647. void SetCommanderProperty::applyGs(CGameState *gs)
  648. {
  649. CCommanderInstance * commander = gs->getHero(heroid)->commander;
  650. assert (commander);
  651. switch (which)
  652. {
  653. case BONUS:
  654. commander->accumulateBonus (std::make_shared<Bonus>(accumulatedBonus));
  655. break;
  656. case SPECIAL_SKILL:
  657. commander->accumulateBonus (std::make_shared<Bonus>(accumulatedBonus));
  658. commander->specialSkills.insert (additionalInfo);
  659. break;
  660. case SECONDARY_SKILL:
  661. commander->secondarySkills[additionalInfo] = static_cast<ui8>(amount);
  662. break;
  663. case ALIVE:
  664. if (amount)
  665. commander->setAlive(true);
  666. else
  667. commander->setAlive(false);
  668. break;
  669. case EXPERIENCE:
  670. commander->giveStackExp(amount); //TODO: allow setting exp for stacks via netpacks
  671. break;
  672. }
  673. }
  674. void AddQuest::applyGs(CGameState * gs) const
  675. {
  676. assert (vstd::contains(gs->players, player));
  677. auto * vec = &gs->players[player].quests;
  678. if (!vstd::contains(*vec, quest))
  679. vec->push_back (quest);
  680. else
  681. logNetwork->warn("Warning! Attempt to add duplicated quest");
  682. }
  683. void UpdateArtHandlerLists::applyGs(CGameState * gs) const
  684. {
  685. VLC->arth->minors = minors;
  686. VLC->arth->majors = majors;
  687. VLC->arth->treasures = treasures;
  688. VLC->arth->relics = relics;
  689. }
  690. void UpdateMapEvents::applyGs(CGameState * gs) const
  691. {
  692. gs->map->events = events;
  693. }
  694. void UpdateCastleEvents::applyGs(CGameState * gs) const
  695. {
  696. auto * t = gs->getTown(town);
  697. t->events = events;
  698. }
  699. void ChangeFormation::applyGs(CGameState * gs) const
  700. {
  701. gs->getHero(hid)->setFormation(formation);
  702. }
  703. void HeroVisitCastle::applyGs(CGameState * gs) const
  704. {
  705. CGHeroInstance *h = gs->getHero(hid);
  706. CGTownInstance *t = gs->getTown(tid);
  707. assert(h);
  708. assert(t);
  709. if(start())
  710. t->setVisitingHero(h);
  711. else
  712. t->setVisitingHero(nullptr);
  713. }
  714. void ChangeSpells::applyGs(CGameState *gs)
  715. {
  716. CGHeroInstance *hero = gs->getHero(hid);
  717. if(learn)
  718. for(const auto & sid : spells)
  719. hero->addSpellToSpellbook(sid);
  720. else
  721. for(const auto & sid : spells)
  722. hero->removeSpellFromSpellbook(sid);
  723. }
  724. void SetMana::applyGs(CGameState * gs) const
  725. {
  726. CGHeroInstance * hero = gs->getHero(hid);
  727. assert(hero);
  728. if(absolute)
  729. hero->mana = val;
  730. else
  731. hero->mana += val;
  732. vstd::amax(hero->mana, 0); //not less than 0
  733. }
  734. void SetMovePoints::applyGs(CGameState * gs) const
  735. {
  736. CGHeroInstance *hero = gs->getHero(hid);
  737. assert(hero);
  738. if(absolute)
  739. hero->setMovementPoints(val);
  740. else
  741. hero->setMovementPoints(hero->movementPointsRemaining() + val);
  742. }
  743. void FoWChange::applyGs(CGameState *gs)
  744. {
  745. TeamState * team = gs->getPlayerTeam(player);
  746. auto fogOfWarMap = team->fogOfWarMap;
  747. for(const int3 & t : tiles)
  748. (*fogOfWarMap)[t.z][t.x][t.y] = mode;
  749. if (mode == 0) //do not hide too much
  750. {
  751. std::unordered_set<int3> tilesRevealed;
  752. for (auto & elem : gs->map->objects)
  753. {
  754. const CGObjectInstance *o = elem;
  755. if (o)
  756. {
  757. switch(o->ID)
  758. {
  759. case Obj::HERO:
  760. case Obj::MINE:
  761. case Obj::TOWN:
  762. case Obj::ABANDONED_MINE:
  763. if(vstd::contains(team->players, o->tempOwner)) //check owned observators
  764. gs->getTilesInRange(tilesRevealed, o->getSightCenter(), o->getSightRadius(), o->tempOwner, 1);
  765. break;
  766. }
  767. }
  768. }
  769. for(const int3 & t : tilesRevealed) //probably not the most optimal solution ever
  770. (*fogOfWarMap)[t.z][t.x][t.y] = 1;
  771. }
  772. }
  773. void SetAvailableHero::applyGs(CGameState *gs)
  774. {
  775. gs->heroesPool->setHeroForPlayer(player, slotID, hid, army, roleID);
  776. }
  777. void GiveBonus::applyGs(CGameState *gs)
  778. {
  779. CBonusSystemNode *cbsn = nullptr;
  780. switch(who)
  781. {
  782. case ETarget::HERO:
  783. cbsn = gs->getHero(ObjectInstanceID(id));
  784. break;
  785. case ETarget::PLAYER:
  786. cbsn = gs->getPlayerState(PlayerColor(id));
  787. break;
  788. case ETarget::TOWN:
  789. cbsn = gs->getTown(ObjectInstanceID(id));
  790. break;
  791. case ETarget::BATTLE:
  792. assert(Bonus::OneBattle(&bonus));
  793. cbsn = dynamic_cast<CBonusSystemNode*>(gs->getBattle(BattleID(id)));
  794. break;
  795. }
  796. assert(cbsn);
  797. if(Bonus::OneWeek(&bonus))
  798. bonus.turnsRemain = 8 - gs->getDate(Date::DAY_OF_WEEK); // set correct number of days before adding bonus
  799. auto b = std::make_shared<Bonus>(bonus);
  800. cbsn->addNewBonus(b);
  801. std::string &descr = b->description;
  802. if(bdescr.empty() && (bonus.type == BonusType::LUCK || bonus.type == BonusType::MORALE))
  803. {
  804. if (bonus.source == BonusSource::OBJECT)
  805. {
  806. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  807. }
  808. else if(bonus.source == BonusSource::TOWN_STRUCTURE)
  809. {
  810. descr = bonus.description;
  811. return;
  812. }
  813. else
  814. {
  815. descr = bdescr.toString();
  816. }
  817. }
  818. else
  819. {
  820. descr = bdescr.toString();
  821. }
  822. // Some of(?) versions of H3 use %s here instead of %d. Try to replace both of them
  823. boost::replace_first(descr, "%d", std::to_string(std::abs(bonus.val)));
  824. boost::replace_first(descr, "%s", std::to_string(std::abs(bonus.val)));
  825. }
  826. void ChangeObjPos::applyGs(CGameState *gs)
  827. {
  828. CGObjectInstance *obj = gs->getObjInstance(objid);
  829. if(!obj)
  830. {
  831. logNetwork->error("Wrong ChangeObjPos: object %d doesn't exist!", objid.getNum());
  832. return;
  833. }
  834. gs->map->removeBlockVisTiles(obj);
  835. obj->pos = nPos + obj->getVisitableOffset();
  836. gs->map->addBlockVisTiles(obj);
  837. }
  838. void ChangeObjectVisitors::applyGs(CGameState * gs) const
  839. {
  840. switch (mode) {
  841. case VISITOR_ADD:
  842. gs->getHero(hero)->visitedObjects.insert(object);
  843. gs->getPlayerState(gs->getHero(hero)->tempOwner)->visitedObjects.insert(object);
  844. break;
  845. case VISITOR_ADD_TEAM:
  846. {
  847. TeamState *ts = gs->getPlayerTeam(gs->getHero(hero)->tempOwner);
  848. for(const auto & color : ts->players)
  849. {
  850. gs->getPlayerState(color)->visitedObjects.insert(object);
  851. }
  852. }
  853. break;
  854. case VISITOR_CLEAR:
  855. for (CGHeroInstance * hero : gs->map->allHeroes)
  856. {
  857. if (hero)
  858. {
  859. hero->visitedObjects.erase(object); // remove visit info from all heroes, including those that are not present on map
  860. }
  861. }
  862. for(auto &elem : gs->players)
  863. {
  864. elem.second.visitedObjects.erase(object);
  865. }
  866. break;
  867. case VISITOR_REMOVE:
  868. gs->getHero(hero)->visitedObjects.erase(object);
  869. break;
  870. }
  871. }
  872. void PlayerEndsGame::applyGs(CGameState * gs) const
  873. {
  874. PlayerState *p = gs->getPlayerState(player);
  875. if(victoryLossCheckResult.victory())
  876. {
  877. p->status = EPlayerStatus::WINNER;
  878. // TODO: Campaign-specific code might as well go somewhere else
  879. // keep all heroes from the winning player
  880. if(p->human && gs->scenarioOps->campState)
  881. {
  882. std::vector<CGHeroInstance *> crossoverHeroes;
  883. for (CGHeroInstance * hero : gs->map->heroesOnMap)
  884. if (hero->tempOwner == player)
  885. crossoverHeroes.push_back(hero);
  886. gs->scenarioOps->campState->setCurrentMapAsConquered(crossoverHeroes);
  887. }
  888. }
  889. else
  890. {
  891. p->status = EPlayerStatus::LOSER;
  892. }
  893. // defeated player may be making turn right now
  894. gs->actingPlayers.erase(player);
  895. }
  896. void PlayerReinitInterface::applyGs(CGameState *gs)
  897. {
  898. if(!gs || !gs->scenarioOps)
  899. return;
  900. //TODO: what does mean if more that one player connected?
  901. if(playerConnectionId == PlayerSettings::PLAYER_AI)
  902. {
  903. for(const auto & player : players)
  904. gs->scenarioOps->getIthPlayersSettings(player).connectedPlayerIDs.clear();
  905. }
  906. }
  907. void RemoveBonus::applyGs(CGameState *gs)
  908. {
  909. CBonusSystemNode * node = nullptr;
  910. if (who == GiveBonus::ETarget::HERO)
  911. node = gs->getHero(ObjectInstanceID(whoID));
  912. else
  913. node = gs->getPlayerState(PlayerColor(whoID));
  914. BonusList &bonuses = node->getExportedBonusList();
  915. for(const auto & b : bonuses)
  916. {
  917. if(vstd::to_underlying(b->source) == source && b->sid == id)
  918. {
  919. bonus = *b; //backup bonus (to show to interfaces later)
  920. node->removeBonus(b);
  921. break;
  922. }
  923. }
  924. }
  925. void RemoveObject::applyGs(CGameState *gs)
  926. {
  927. CGObjectInstance *obj = gs->getObjInstance(objectID);
  928. logGlobal->debug("removing object id=%d; address=%x; name=%s", objectID, (intptr_t)obj, obj->getObjectName());
  929. //unblock tiles
  930. gs->map->removeBlockVisTiles(obj);
  931. if(obj->ID == Obj::HERO) //remove beaten hero
  932. {
  933. auto * beatenHero = dynamic_cast<CGHeroInstance *>(obj);
  934. assert(beatenHero);
  935. PlayerState * p = gs->getPlayerState(beatenHero->tempOwner);
  936. gs->map->heroesOnMap -= beatenHero;
  937. p->heroes -= beatenHero;
  938. auto * siegeNode = beatenHero->whereShouldBeAttachedOnSiege(gs);
  939. // FIXME: workaround:
  940. // hero should be attached to siegeNode after battle
  941. // however this code might also be called on dismissing hero while in town
  942. if (siegeNode && vstd::contains(beatenHero->getParentNodes(), siegeNode))
  943. beatenHero->detachFrom(*siegeNode);
  944. beatenHero->tempOwner = PlayerColor::NEUTRAL; //no one owns beaten hero
  945. vstd::erase_if(beatenHero->artifactsInBackpack, [](const ArtSlotInfo& asi)
  946. {
  947. return asi.artifact->artType->getId() == ArtifactID::GRAIL;
  948. });
  949. if(beatenHero->visitedTown)
  950. {
  951. if(beatenHero->visitedTown->garrisonHero == beatenHero)
  952. beatenHero->visitedTown->garrisonHero = nullptr;
  953. else
  954. beatenHero->visitedTown->visitingHero = nullptr;
  955. beatenHero->visitedTown = nullptr;
  956. beatenHero->inTownGarrison = false;
  957. }
  958. //return hero to the pool, so he may reappear in tavern
  959. gs->heroesPool->addHeroToPool(beatenHero);
  960. gs->map->objects[objectID.getNum()] = nullptr;
  961. //If hero on Boat is removed, the Boat disappears
  962. if(beatenHero->boat)
  963. {
  964. beatenHero->detachFrom(const_cast<CGBoat&>(*beatenHero->boat));
  965. gs->map->instanceNames.erase(beatenHero->boat->instanceName);
  966. gs->map->objects[beatenHero->boat->id.getNum()].dellNull();
  967. beatenHero->boat = nullptr;
  968. }
  969. return;
  970. }
  971. const auto * quest = dynamic_cast<const IQuestObject *>(obj);
  972. if (quest)
  973. {
  974. gs->map->quests[quest->quest->qid] = nullptr;
  975. for (auto &player : gs->players)
  976. {
  977. for (auto &q : player.second.quests)
  978. {
  979. if (q.obj == obj)
  980. {
  981. q.obj = nullptr;
  982. }
  983. }
  984. }
  985. }
  986. for (TriggeredEvent & event : gs->map->triggeredEvents)
  987. {
  988. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  989. {
  990. if (cond.object == obj)
  991. {
  992. if (cond.condition == EventCondition::DESTROY || cond.condition == EventCondition::DESTROY_0)
  993. {
  994. cond.condition = EventCondition::CONST_VALUE;
  995. cond.value = 1; // destroyed object, from now on always fulfilled
  996. }
  997. else if (cond.condition == EventCondition::CONTROL || cond.condition == EventCondition::HAVE_0)
  998. {
  999. cond.condition = EventCondition::CONST_VALUE;
  1000. cond.value = 0; // destroyed object, from now on can not be fulfilled
  1001. }
  1002. }
  1003. return cond;
  1004. };
  1005. event.trigger = event.trigger.morph(patcher);
  1006. }
  1007. gs->map->instanceNames.erase(obj->instanceName);
  1008. gs->map->objects[objectID.getNum()].dellNull();
  1009. gs->map->calculateGuardingGreaturePositions();
  1010. }
  1011. static int getDir(const int3 & src, const int3 & dst)
  1012. {
  1013. int ret = -1;
  1014. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  1015. {
  1016. ret = 1;
  1017. }
  1018. else if(dst.x == src.x && dst.y+1 == src.y) //t
  1019. {
  1020. ret = 2;
  1021. }
  1022. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  1023. {
  1024. ret = 3;
  1025. }
  1026. else if(dst.x-1 == src.x && dst.y == src.y) //r
  1027. {
  1028. ret = 4;
  1029. }
  1030. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  1031. {
  1032. ret = 5;
  1033. }
  1034. else if(dst.x == src.x && dst.y-1 == src.y) //b
  1035. {
  1036. ret = 6;
  1037. }
  1038. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  1039. {
  1040. ret = 7;
  1041. }
  1042. else if(dst.x+1 == src.x && dst.y == src.y) //l
  1043. {
  1044. ret = 8;
  1045. }
  1046. return ret;
  1047. }
  1048. void TryMoveHero::applyGs(CGameState *gs)
  1049. {
  1050. CGHeroInstance *h = gs->getHero(id);
  1051. if (!h)
  1052. {
  1053. logGlobal->error("Attempt ot move unavailable hero %d", id.getNum());
  1054. return;
  1055. }
  1056. h->setMovementPoints(movePoints);
  1057. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end)
  1058. {
  1059. auto dir = getDir(start,end);
  1060. if(dir > 0 && dir <= 8)
  1061. h->moveDir = dir;
  1062. //else don`t change move direction - hero might have traversed the subterranean gate, direction should be kept
  1063. }
  1064. if(result == EMBARK) //hero enters boat at destination tile
  1065. {
  1066. const TerrainTile &tt = gs->map->getTile(h->convertToVisitablePos(end));
  1067. assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == Obj::BOAT); //the only visitable object at destination is Boat
  1068. auto * boat = dynamic_cast<CGBoat *>(tt.visitableObjects.back());
  1069. assert(boat);
  1070. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  1071. h->boat = boat;
  1072. h->attachTo(*boat);
  1073. boat->hero = h;
  1074. }
  1075. else if(result == DISEMBARK) //hero leaves boat to destination tile
  1076. {
  1077. auto * b = const_cast<CGBoat *>(h->boat);
  1078. b->direction = h->moveDir;
  1079. b->pos = start;
  1080. b->hero = nullptr;
  1081. gs->map->addBlockVisTiles(b);
  1082. h->detachFrom(*b);
  1083. h->boat = nullptr;
  1084. }
  1085. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  1086. {
  1087. gs->map->removeBlockVisTiles(h);
  1088. h->pos = end;
  1089. if(auto * b = const_cast<CGBoat *>(h->boat))
  1090. b->pos = end;
  1091. gs->map->addBlockVisTiles(h);
  1092. }
  1093. auto fogOfWarMap = gs->getPlayerTeam(h->getOwner())->fogOfWarMap;
  1094. for(const int3 & t : fowRevealed)
  1095. (*fogOfWarMap)[t.z][t.x][t.y] = 1;
  1096. }
  1097. void NewStructures::applyGs(CGameState *gs)
  1098. {
  1099. CGTownInstance *t = gs->getTown(tid);
  1100. for(const auto & id : bid)
  1101. {
  1102. assert(t->town->buildings.at(id) != nullptr);
  1103. t->builtBuildings.insert(id);
  1104. t->updateAppearance();
  1105. auto currentBuilding = t->town->buildings.at(id);
  1106. if(currentBuilding->overrideBids.empty())
  1107. continue;
  1108. for(const auto & overrideBid : currentBuilding->overrideBids)
  1109. {
  1110. t->overriddenBuildings.insert(overrideBid);
  1111. t->deleteTownBonus(overrideBid);
  1112. }
  1113. }
  1114. t->builded = builded;
  1115. t->recreateBuildingsBonuses();
  1116. }
  1117. void RazeStructures::applyGs(CGameState *gs)
  1118. {
  1119. CGTownInstance *t = gs->getTown(tid);
  1120. for(const auto & id : bid)
  1121. {
  1122. t->builtBuildings.erase(id);
  1123. t->updateAppearance();
  1124. }
  1125. t->destroyed = destroyed; //yeaha
  1126. t->recreateBuildingsBonuses();
  1127. }
  1128. void SetAvailableCreatures::applyGs(CGameState * gs) const
  1129. {
  1130. auto * dw = dynamic_cast<CGDwelling *>(gs->getObjInstance(tid));
  1131. assert(dw);
  1132. dw->creatures = creatures;
  1133. }
  1134. void SetHeroesInTown::applyGs(CGameState * gs) const
  1135. {
  1136. CGTownInstance *t = gs->getTown(tid);
  1137. CGHeroInstance * v = gs->getHero(visiting);
  1138. CGHeroInstance * g = gs->getHero(garrison);
  1139. bool newVisitorComesFromGarrison = v && v == t->garrisonHero;
  1140. bool newGarrisonComesFromVisiting = g && g == t->visitingHero;
  1141. if(newVisitorComesFromGarrison)
  1142. t->setGarrisonedHero(nullptr);
  1143. if(newGarrisonComesFromVisiting)
  1144. t->setVisitingHero(nullptr);
  1145. if(!newGarrisonComesFromVisiting || v)
  1146. t->setVisitingHero(v);
  1147. if(!newVisitorComesFromGarrison || g)
  1148. t->setGarrisonedHero(g);
  1149. if(v)
  1150. {
  1151. gs->map->addBlockVisTiles(v);
  1152. }
  1153. if(g)
  1154. {
  1155. gs->map->removeBlockVisTiles(g);
  1156. }
  1157. }
  1158. void HeroRecruited::applyGs(CGameState * gs) const
  1159. {
  1160. CGHeroInstance *h = gs->heroesPool->takeHeroFromPool(hid);
  1161. CGTownInstance *t = gs->getTown(tid);
  1162. PlayerState *p = gs->getPlayerState(player);
  1163. if (boatId != ObjectInstanceID::NONE)
  1164. {
  1165. CGObjectInstance *obj = gs->getObjInstance(boatId);
  1166. auto * boat = dynamic_cast<CGBoat *>(obj);
  1167. if (boat)
  1168. {
  1169. gs->map->removeBlockVisTiles(boat);
  1170. h->attachToBoat(boat);
  1171. }
  1172. }
  1173. h->setOwner(player);
  1174. h->pos = tile;
  1175. h->initObj(gs->getRandomGenerator());
  1176. if(h->id == ObjectInstanceID())
  1177. {
  1178. h->id = ObjectInstanceID(static_cast<si32>(gs->map->objects.size()));
  1179. gs->map->objects.emplace_back(h);
  1180. }
  1181. else
  1182. gs->map->objects[h->id.getNum()] = h;
  1183. gs->map->heroesOnMap.emplace_back(h);
  1184. p->heroes.emplace_back(h);
  1185. h->attachTo(*p);
  1186. gs->map->addBlockVisTiles(h);
  1187. if(t)
  1188. t->setVisitingHero(h);
  1189. }
  1190. void GiveHero::applyGs(CGameState * gs) const
  1191. {
  1192. CGHeroInstance *h = gs->getHero(id);
  1193. if (boatId != ObjectInstanceID::NONE)
  1194. {
  1195. CGObjectInstance *obj = gs->getObjInstance(boatId);
  1196. auto * boat = dynamic_cast<CGBoat *>(obj);
  1197. if (boat)
  1198. {
  1199. gs->map->removeBlockVisTiles(boat);
  1200. h->attachToBoat(boat);
  1201. }
  1202. }
  1203. //bonus system
  1204. h->detachFrom(gs->globalEffects);
  1205. h->attachTo(*gs->getPlayerState(player));
  1206. auto oldVisitablePos = h->visitablePos();
  1207. gs->map->removeBlockVisTiles(h,true);
  1208. h->appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, h->type->heroClass->getIndex())->getTemplates().front();
  1209. h->setOwner(player);
  1210. h->setMovementPoints(h->movementPointsLimit(true));
  1211. h->pos = h->convertFromVisitablePos(oldVisitablePos);
  1212. gs->map->heroesOnMap.emplace_back(h);
  1213. gs->getPlayerState(h->getOwner())->heroes.emplace_back(h);
  1214. gs->map->addBlockVisTiles(h);
  1215. h->inTownGarrison = false;
  1216. }
  1217. void NewObject::applyGs(CGameState *gs)
  1218. {
  1219. TerrainId terrainType = ETerrainId::NONE;
  1220. if (!gs->isInTheMap(targetPos))
  1221. {
  1222. logGlobal->error("Attempt to create object outside map at %s!", targetPos.toString());
  1223. return;
  1224. }
  1225. const TerrainTile & t = gs->map->getTile(targetPos);
  1226. terrainType = t.terType->getId();
  1227. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  1228. CGObjectInstance * o = handler->create();
  1229. handler->configureObject(o, gs->getRandomGenerator());
  1230. if (ID == Obj::MONSTER) //probably more options will be needed
  1231. {
  1232. //CStackInstance hlp;
  1233. auto * cre = dynamic_cast<CGCreature *>(o);
  1234. //cre->slots[0] = hlp;
  1235. assert(cre);
  1236. cre->notGrowingTeam = cre->neverFlees = false;
  1237. cre->character = 2;
  1238. cre->gainedArtifact = ArtifactID::NONE;
  1239. cre->identifier = -1;
  1240. cre->addToSlot(SlotID(0), new CStackInstance(CreatureID(subID), -1)); //add placeholder stack
  1241. }
  1242. assert(!handler->getTemplates(terrainType).empty());
  1243. if (handler->getTemplates().empty())
  1244. {
  1245. logGlobal->error("Attempt to create object (%d %d) with no templates!", ID, subID);
  1246. return;
  1247. }
  1248. if (!handler->getTemplates(terrainType).empty())
  1249. o->appearance = handler->getTemplates(terrainType).front();
  1250. else
  1251. o->appearance = handler->getTemplates().front();
  1252. o->id = ObjectInstanceID(static_cast<si32>(gs->map->objects.size()));
  1253. o->ID = ID;
  1254. o->subID = subID;
  1255. o->pos = targetPos + o->getVisitableOffset();
  1256. gs->map->objects.emplace_back(o);
  1257. gs->map->addBlockVisTiles(o);
  1258. o->initObj(gs->getRandomGenerator());
  1259. gs->map->calculateGuardingGreaturePositions();
  1260. createdObjectID = o->id;
  1261. logGlobal->debug("Added object id=%d; address=%x; name=%s", o->id, (intptr_t)o, o->getObjectName());
  1262. }
  1263. void NewArtifact::applyGs(CGameState *gs)
  1264. {
  1265. assert(!vstd::contains(gs->map->artInstances, art));
  1266. assert(!art->getParentNodes().size());
  1267. assert(art->artType);
  1268. art->setType(art->artType);
  1269. if(art->isCombined())
  1270. {
  1271. for(const auto & part : art->artType->getConstituents())
  1272. art->addPart(ArtifactUtils::createNewArtifactInstance(part), ArtifactPosition::PRE_FIRST);
  1273. }
  1274. gs->map->addNewArtifactInstance(art);
  1275. }
  1276. const CStackInstance * StackLocation::getStack()
  1277. {
  1278. if(!army->hasStackAtSlot(slot))
  1279. {
  1280. logNetwork->warn("%s don't have a stack at slot %d", army->nodeName(), slot.getNum());
  1281. return nullptr;
  1282. }
  1283. return &army->getStack(slot);
  1284. }
  1285. struct ObjectRetriever
  1286. {
  1287. const CArmedInstance * operator()(const ConstTransitivePtr<CGHeroInstance> &h) const
  1288. {
  1289. return h;
  1290. }
  1291. const CArmedInstance * operator()(const ConstTransitivePtr<CStackInstance> &s) const
  1292. {
  1293. return s->armyObj;
  1294. }
  1295. };
  1296. template<typename T>
  1297. struct GetBase
  1298. {
  1299. template <typename TArg>
  1300. T * operator()(TArg &arg) const
  1301. {
  1302. return arg;
  1303. }
  1304. };
  1305. void ArtifactLocation::removeArtifact()
  1306. {
  1307. CArtifactInstance *a = getArt();
  1308. assert(a);
  1309. a->removeFrom(*this);
  1310. }
  1311. const CArmedInstance * ArtifactLocation::relatedObj() const
  1312. {
  1313. return std::visit(ObjectRetriever(), artHolder);
  1314. }
  1315. PlayerColor ArtifactLocation::owningPlayer() const
  1316. {
  1317. const auto * obj = relatedObj();
  1318. return obj ? obj->tempOwner : PlayerColor::NEUTRAL;
  1319. }
  1320. CArtifactSet *ArtifactLocation::getHolderArtSet()
  1321. {
  1322. return std::visit(GetBase<CArtifactSet>(), artHolder);
  1323. }
  1324. CBonusSystemNode *ArtifactLocation::getHolderNode()
  1325. {
  1326. return std::visit(GetBase<CBonusSystemNode>(), artHolder);
  1327. }
  1328. const CArtifactInstance *ArtifactLocation::getArt() const
  1329. {
  1330. const auto * s = getSlot();
  1331. if(s)
  1332. return s->getArt();
  1333. else
  1334. return nullptr;
  1335. }
  1336. CArtifactSet * ArtifactLocation::getHolderArtSet() const
  1337. {
  1338. auto * t = const_cast<ArtifactLocation *>(this);
  1339. return t->getHolderArtSet();
  1340. }
  1341. const CBonusSystemNode * ArtifactLocation::getHolderNode() const
  1342. {
  1343. auto * t = const_cast<ArtifactLocation *>(this);
  1344. return t->getHolderNode();
  1345. }
  1346. CArtifactInstance *ArtifactLocation::getArt()
  1347. {
  1348. const ArtifactLocation *t = this;
  1349. return const_cast<CArtifactInstance*>(t->getArt());
  1350. }
  1351. const ArtSlotInfo *ArtifactLocation::getSlot() const
  1352. {
  1353. return getHolderArtSet()->getSlot(slot);
  1354. }
  1355. void ChangeStackCount::applyGs(CGameState * gs)
  1356. {
  1357. auto * srcObj = gs->getArmyInstance(army);
  1358. if(!srcObj)
  1359. logNetwork->error("[CRITICAL] ChangeStackCount: invalid army object %d, possible game state corruption.", army.getNum());
  1360. if(absoluteValue)
  1361. srcObj->setStackCount(slot, count);
  1362. else
  1363. srcObj->changeStackCount(slot, count);
  1364. }
  1365. void SetStackType::applyGs(CGameState * gs)
  1366. {
  1367. auto * srcObj = gs->getArmyInstance(army);
  1368. if(!srcObj)
  1369. logNetwork->error("[CRITICAL] SetStackType: invalid army object %d, possible game state corruption.", army.getNum());
  1370. srcObj->setStackType(slot, type);
  1371. }
  1372. void EraseStack::applyGs(CGameState * gs)
  1373. {
  1374. auto * srcObj = gs->getArmyInstance(army);
  1375. if(!srcObj)
  1376. logNetwork->error("[CRITICAL] EraseStack: invalid army object %d, possible game state corruption.", army.getNum());
  1377. srcObj->eraseStack(slot);
  1378. }
  1379. void SwapStacks::applyGs(CGameState * gs)
  1380. {
  1381. auto * srcObj = gs->getArmyInstance(srcArmy);
  1382. if(!srcObj)
  1383. logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum());
  1384. auto * dstObj = gs->getArmyInstance(dstArmy);
  1385. if(!dstObj)
  1386. logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum());
  1387. CStackInstance * s1 = srcObj->detachStack(srcSlot);
  1388. CStackInstance * s2 = dstObj->detachStack(dstSlot);
  1389. srcObj->putStack(srcSlot, s2);
  1390. dstObj->putStack(dstSlot, s1);
  1391. }
  1392. void InsertNewStack::applyGs(CGameState *gs)
  1393. {
  1394. if(auto * obj = gs->getArmyInstance(army))
  1395. obj->putStack(slot, new CStackInstance(type, count));
  1396. else
  1397. logNetwork->error("[CRITICAL] InsertNewStack: invalid army object %d, possible game state corruption.", army.getNum());
  1398. }
  1399. void RebalanceStacks::applyGs(CGameState * gs)
  1400. {
  1401. auto * srcObj = gs->getArmyInstance(srcArmy);
  1402. if(!srcObj)
  1403. logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum());
  1404. auto * dstObj = gs->getArmyInstance(dstArmy);
  1405. if(!dstObj)
  1406. logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum());
  1407. StackLocation src(srcObj, srcSlot);
  1408. StackLocation dst(dstObj, dstSlot);
  1409. const CCreature * srcType = src.army->getCreature(src.slot);
  1410. TQuantity srcCount = src.army->getStackCount(src.slot);
  1411. bool stackExp = VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE);
  1412. if(srcCount == count) //moving whole stack
  1413. {
  1414. [[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot);
  1415. if(c) //stack at dest -> merge
  1416. {
  1417. assert(c == srcType);
  1418. auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT);
  1419. auto alDest = ArtifactLocation (dst.getStack(), ArtifactPosition::CREATURE_SLOT);
  1420. auto * artHere = alHere.getArt();
  1421. auto * artDest = alDest.getArt();
  1422. if (artHere)
  1423. {
  1424. if (alDest.getArt())
  1425. {
  1426. auto * hero = dynamic_cast<CGHeroInstance *>(src.army.get());
  1427. auto dstSlot = ArtifactUtils::getArtBackpackPosition(hero, alDest.getArt()->getTypeId());
  1428. if(hero && dstSlot != ArtifactPosition::PRE_FIRST)
  1429. {
  1430. artDest->move (alDest, ArtifactLocation (hero, dstSlot));
  1431. }
  1432. //else - artifact cna be lost :/
  1433. else
  1434. {
  1435. EraseArtifact ea;
  1436. ea.al = alDest;
  1437. ea.applyGs(gs);
  1438. logNetwork->warn("Cannot move artifact! No free slots");
  1439. }
  1440. artHere->move (alHere, alDest);
  1441. //TODO: choose from dialog
  1442. }
  1443. else //just move to the other slot before stack gets erased
  1444. {
  1445. artHere->move (alHere, alDest);
  1446. }
  1447. }
  1448. if (stackExp)
  1449. {
  1450. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  1451. src.army->eraseStack(src.slot);
  1452. dst.army->changeStackCount(dst.slot, count);
  1453. dst.army->setStackExp(dst.slot, totalExp /(dst.army->getStackCount(dst.slot))); //mean
  1454. }
  1455. else
  1456. {
  1457. src.army->eraseStack(src.slot);
  1458. dst.army->changeStackCount(dst.slot, count);
  1459. }
  1460. }
  1461. else //move stack to an empty slot, no exp change needed
  1462. {
  1463. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  1464. dst.army->putStack(dst.slot, stackDetached);
  1465. }
  1466. }
  1467. else
  1468. {
  1469. [[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot);
  1470. if(c) //stack at dest -> rebalance
  1471. {
  1472. assert(c == srcType);
  1473. if (stackExp)
  1474. {
  1475. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  1476. src.army->changeStackCount(src.slot, -count);
  1477. dst.army->changeStackCount(dst.slot, count);
  1478. dst.army->setStackExp(dst.slot, totalExp /(src.army->getStackCount(src.slot) + dst.army->getStackCount(dst.slot))); //mean
  1479. }
  1480. else
  1481. {
  1482. src.army->changeStackCount(src.slot, -count);
  1483. dst.army->changeStackCount(dst.slot, count);
  1484. }
  1485. }
  1486. else //split stack to an empty slot
  1487. {
  1488. src.army->changeStackCount(src.slot, -count);
  1489. dst.army->addToSlot(dst.slot, srcType->getId(), count, false);
  1490. if (stackExp)
  1491. dst.army->setStackExp(dst.slot, src.army->getStackExperience(src.slot));
  1492. }
  1493. }
  1494. CBonusSystemNode::treeHasChanged();
  1495. }
  1496. void BulkRebalanceStacks::applyGs(CGameState * gs)
  1497. {
  1498. for(auto & move : moves)
  1499. move.applyGs(gs);
  1500. }
  1501. void BulkSmartRebalanceStacks::applyGs(CGameState * gs)
  1502. {
  1503. for(auto & move : moves)
  1504. move.applyGs(gs);
  1505. for(auto & change : changes)
  1506. change.applyGs(gs);
  1507. }
  1508. void PutArtifact::applyGs(CGameState *gs)
  1509. {
  1510. assert(art->canBePutAt(al));
  1511. // Ensure that artifact has been correctly added via NewArtifact pack
  1512. assert(vstd::contains(gs->map->artInstances, art));
  1513. assert(!art->getParentNodes().empty());
  1514. art->putAt(al);
  1515. }
  1516. void EraseArtifact::applyGs(CGameState *gs)
  1517. {
  1518. const auto * slot = al.getSlot();
  1519. if(slot->locked)
  1520. {
  1521. logGlobal->debug("Erasing locked artifact: %s", slot->artifact->artType->getNameTranslated());
  1522. DisassembledArtifact dis;
  1523. dis.al.artHolder = al.artHolder;
  1524. auto * aset = al.getHolderArtSet();
  1525. #ifndef NDEBUG
  1526. bool found = false;
  1527. #endif
  1528. for(auto& p : aset->artifactsWorn)
  1529. {
  1530. auto art = p.second.artifact;
  1531. if(art->isCombined() && art->isPart(slot->artifact))
  1532. {
  1533. dis.al.slot = aset->getArtPos(art);
  1534. #ifndef NDEBUG
  1535. found = true;
  1536. #endif
  1537. break;
  1538. }
  1539. }
  1540. assert(found && "Failed to determine the assembly this locked artifact belongs to");
  1541. logGlobal->debug("Found the corresponding assembly: %s", dis.al.getSlot()->artifact->artType->getNameTranslated());
  1542. dis.applyGs(gs);
  1543. }
  1544. else
  1545. {
  1546. logGlobal->debug("Erasing artifact %s", slot->artifact->artType->getNameTranslated());
  1547. }
  1548. al.removeArtifact();
  1549. }
  1550. void MoveArtifact::applyGs(CGameState * gs)
  1551. {
  1552. CArtifactInstance * art = src.getArt();
  1553. assert(!ArtifactUtils::isSlotEquipment(dst.slot) || !dst.getArt());
  1554. art->move(src, dst);
  1555. }
  1556. void BulkMoveArtifacts::applyGs(CGameState * gs)
  1557. {
  1558. enum class EBulkArtsOp
  1559. {
  1560. BULK_MOVE,
  1561. BULK_REMOVE,
  1562. BULK_PUT
  1563. };
  1564. auto bulkArtsOperation = [this](std::vector<LinkedSlots> & artsPack,
  1565. CArtifactSet * artSet, EBulkArtsOp operation) -> void
  1566. {
  1567. int numBackpackArtifactsMoved = 0;
  1568. for(auto & slot : artsPack)
  1569. {
  1570. // When an object gets removed from the backpack, the backpack shrinks
  1571. // so all the following indices will be affected. Thus, we need to update
  1572. // the subsequent artifact slots to account for that
  1573. auto srcPos = slot.srcPos;
  1574. if(ArtifactUtils::isSlotBackpack(srcPos) && (operation != EBulkArtsOp::BULK_PUT))
  1575. {
  1576. srcPos = ArtifactPosition(srcPos.num - numBackpackArtifactsMoved);
  1577. }
  1578. const auto * slotInfo = artSet->getSlot(srcPos);
  1579. assert(slotInfo);
  1580. auto * art = const_cast<CArtifactInstance *>(slotInfo->getArt());
  1581. assert(art);
  1582. switch(operation)
  1583. {
  1584. case EBulkArtsOp::BULK_MOVE:
  1585. const_cast<CArtifactInstance*>(art)->move(
  1586. ArtifactLocation(srcArtHolder, srcPos), ArtifactLocation(dstArtHolder, slot.dstPos));
  1587. break;
  1588. case EBulkArtsOp::BULK_REMOVE:
  1589. art->removeFrom(ArtifactLocation(dstArtHolder, srcPos));
  1590. break;
  1591. case EBulkArtsOp::BULK_PUT:
  1592. art->putAt(ArtifactLocation(srcArtHolder, slot.dstPos));
  1593. break;
  1594. default:
  1595. break;
  1596. }
  1597. if(srcPos >= ArtifactPosition::BACKPACK_START)
  1598. {
  1599. numBackpackArtifactsMoved++;
  1600. }
  1601. }
  1602. };
  1603. if(swap)
  1604. {
  1605. // Swap
  1606. auto * leftSet = getSrcHolderArtSet();
  1607. auto * rightSet = getDstHolderArtSet();
  1608. CArtifactFittingSet artFittingSet(leftSet->bearerType());
  1609. artFittingSet.artifactsWorn = rightSet->artifactsWorn;
  1610. artFittingSet.artifactsInBackpack = rightSet->artifactsInBackpack;
  1611. bulkArtsOperation(artsPack1, rightSet, EBulkArtsOp::BULK_REMOVE);
  1612. bulkArtsOperation(artsPack0, leftSet, EBulkArtsOp::BULK_MOVE);
  1613. bulkArtsOperation(artsPack1, &artFittingSet, EBulkArtsOp::BULK_PUT);
  1614. }
  1615. else
  1616. {
  1617. bulkArtsOperation(artsPack0, getSrcHolderArtSet(), EBulkArtsOp::BULK_MOVE);
  1618. }
  1619. }
  1620. void AssembledArtifact::applyGs(CGameState *gs)
  1621. {
  1622. CArtifactSet * artSet = al.getHolderArtSet();
  1623. [[maybe_unused]] const CArtifactInstance *transformedArt = al.getArt();
  1624. assert(transformedArt);
  1625. bool combineEquipped = !ArtifactUtils::isSlotBackpack(al.slot);
  1626. assert(vstd::contains_if(ArtifactUtils::assemblyPossibilities(artSet, transformedArt->artType->getId(), combineEquipped), [=](const CArtifact * art)->bool
  1627. {
  1628. return art->getId() == builtArt->getId();
  1629. }));
  1630. auto * combinedArt = new CArtifactInstance(builtArt);
  1631. gs->map->addNewArtifactInstance(combinedArt);
  1632. // Retrieve all constituents
  1633. for(const CArtifact * constituent : builtArt->getConstituents())
  1634. {
  1635. ArtifactPosition pos = combineEquipped ? artSet->getArtPos(constituent->getId(), true, false) :
  1636. artSet->getArtBackpackPos(constituent->getId());
  1637. assert(pos != ArtifactPosition::PRE_FIRST);
  1638. CArtifactInstance * constituentInstance = artSet->getArt(pos);
  1639. //move constituent from hero to be part of new, combined artifact
  1640. constituentInstance->removeFrom(ArtifactLocation(al.artHolder, pos));
  1641. if(combineEquipped)
  1642. {
  1643. if(!vstd::contains(combinedArt->artType->getPossibleSlots().at(artSet->bearerType()), al.slot)
  1644. && vstd::contains(combinedArt->artType->getPossibleSlots().at(artSet->bearerType()), pos))
  1645. al.slot = pos;
  1646. if(al.slot == pos)
  1647. pos = ArtifactPosition::PRE_FIRST;
  1648. }
  1649. else
  1650. {
  1651. al.slot = std::min(al.slot, pos);
  1652. pos = ArtifactPosition::PRE_FIRST;
  1653. }
  1654. combinedArt->addPart(constituentInstance, pos);
  1655. }
  1656. //put new combined artifacts
  1657. combinedArt->putAt(al);
  1658. }
  1659. void DisassembledArtifact::applyGs(CGameState *gs)
  1660. {
  1661. auto * disassembled = al.getArt();
  1662. assert(disassembled);
  1663. auto parts = disassembled->getPartsInfo();
  1664. disassembled->removeFrom(al);
  1665. for(auto & part : parts)
  1666. {
  1667. ArtifactLocation partLoc = al;
  1668. // ArtifactPosition::PRE_FIRST is value of main part slot -> it'll replace combined artifact in its pos
  1669. partLoc.slot = (ArtifactUtils::isSlotEquipment(part.slot) ? part.slot : al.slot);
  1670. disassembled->detachFrom(*part.art);
  1671. part.art->putAt(partLoc);
  1672. }
  1673. gs->map->eraseArtifactInstance(disassembled);
  1674. }
  1675. void HeroVisit::applyGs(CGameState *gs)
  1676. {
  1677. }
  1678. void SetAvailableArtifacts::applyGs(CGameState * gs) const
  1679. {
  1680. if(id >= 0)
  1681. {
  1682. if(auto * bm = dynamic_cast<CGBlackMarket *>(gs->map->objects[id].get()))
  1683. {
  1684. bm->artifacts = arts;
  1685. }
  1686. else
  1687. {
  1688. logNetwork->error("Wrong black market id!");
  1689. }
  1690. }
  1691. else
  1692. {
  1693. CGTownInstance::merchantArtifacts = arts;
  1694. }
  1695. }
  1696. void NewTurn::applyGs(CGameState *gs)
  1697. {
  1698. gs->day = day;
  1699. // Update bonuses before doing anything else so hero don't get more MP than needed
  1700. gs->globalEffects.removeBonusesRecursive(Bonus::OneDay); //works for children -> all game objs
  1701. gs->globalEffects.reduceBonusDurations(Bonus::NDays);
  1702. gs->globalEffects.reduceBonusDurations(Bonus::OneWeek);
  1703. //TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
  1704. for(const NewTurn::Hero & h : heroes) //give mana/movement point
  1705. {
  1706. CGHeroInstance *hero = gs->getHero(h.id);
  1707. if(!hero)
  1708. {
  1709. logGlobal->error("Hero %d not found in NewTurn::applyGs", h.id.getNum());
  1710. continue;
  1711. }
  1712. hero->setMovementPoints(h.move);
  1713. hero->mana = h.mana;
  1714. }
  1715. gs->heroesPool->onNewDay();
  1716. for(const auto & re : res)
  1717. {
  1718. assert(re.first.isValidPlayer());
  1719. gs->getPlayerState(re.first)->resources = re.second;
  1720. }
  1721. for(const auto & creatureSet : cres) //set available creatures in towns
  1722. creatureSet.second.applyGs(gs);
  1723. for(CGTownInstance* t : gs->map->towns)
  1724. t->builded = 0;
  1725. if(gs->getDate(Date::DAY_OF_WEEK) == 1)
  1726. gs->updateRumor();
  1727. }
  1728. void SetObjectProperty::applyGs(CGameState * gs) const
  1729. {
  1730. CGObjectInstance *obj = gs->getObjInstance(id);
  1731. if(!obj)
  1732. {
  1733. logNetwork->error("Wrong object ID - property cannot be set!");
  1734. return;
  1735. }
  1736. auto * cai = dynamic_cast<CArmedInstance *>(obj);
  1737. if(what == ObjProperty::OWNER && cai)
  1738. {
  1739. if(obj->ID == Obj::TOWN)
  1740. {
  1741. auto * t = dynamic_cast<CGTownInstance *>(obj);
  1742. assert(t);
  1743. PlayerColor oldOwner = t->tempOwner;
  1744. if(oldOwner.isValidPlayer())
  1745. {
  1746. auto * state = gs->getPlayerState(oldOwner);
  1747. state->towns -= t;
  1748. if(state->towns.empty())
  1749. *state->daysWithoutCastle = 0;
  1750. }
  1751. if(PlayerColor(val).isValidPlayer())
  1752. {
  1753. PlayerState * p = gs->getPlayerState(PlayerColor(val));
  1754. p->towns.emplace_back(t);
  1755. //reset counter before NewTurn to avoid no town message if game loaded at turn when one already captured
  1756. if(p->daysWithoutCastle)
  1757. p->daysWithoutCastle = std::nullopt;
  1758. }
  1759. }
  1760. CBonusSystemNode & nodeToMove = cai->whatShouldBeAttached();
  1761. nodeToMove.detachFrom(cai->whereShouldBeAttached(gs));
  1762. obj->setProperty(what,val);
  1763. nodeToMove.attachTo(cai->whereShouldBeAttached(gs));
  1764. }
  1765. else //not an armed instance
  1766. {
  1767. obj->setProperty(what,val);
  1768. }
  1769. }
  1770. void PrepareHeroLevelUp::applyGs(CGameState * gs)
  1771. {
  1772. auto * hero = gs->getHero(heroId);
  1773. assert(hero);
  1774. auto proposedSkills = hero->getLevelUpProposedSecondarySkills();
  1775. if(skills.size() == 1 || hero->tempOwner == PlayerColor::NEUTRAL) //choose skill automatically
  1776. {
  1777. skills.push_back(*RandomGeneratorUtil::nextItem(proposedSkills, hero->skillsInfo.rand));
  1778. }
  1779. else
  1780. {
  1781. skills = proposedSkills;
  1782. }
  1783. }
  1784. void HeroLevelUp::applyGs(CGameState * gs) const
  1785. {
  1786. auto * hero = gs->getHero(heroId);
  1787. assert(hero);
  1788. hero->levelUp(skills);
  1789. }
  1790. void CommanderLevelUp::applyGs(CGameState * gs) const
  1791. {
  1792. auto * hero = gs->getHero(heroId);
  1793. assert(hero);
  1794. auto commander = hero->commander;
  1795. assert(commander);
  1796. commander->levelUp();
  1797. }
  1798. void BattleStart::applyGs(CGameState * gs) const
  1799. {
  1800. assert(battleID == gs->nextBattleID);
  1801. gs->currentBattles.emplace_back(info);
  1802. info->battleID = gs->nextBattleID;
  1803. info->localInit();
  1804. gs->nextBattleID = vstd::next(gs->nextBattleID, 1);
  1805. }
  1806. void BattleNextRound::applyGs(CGameState * gs) const
  1807. {
  1808. gs->getBattle(battleID)->nextRound();
  1809. }
  1810. void BattleSetActiveStack::applyGs(CGameState * gs) const
  1811. {
  1812. gs->getBattle(battleID)->nextTurn(stack);
  1813. }
  1814. void BattleTriggerEffect::applyGs(CGameState * gs) const
  1815. {
  1816. CStack * st = gs->getBattle(battleID)->getStack(stackID);
  1817. assert(st);
  1818. switch(static_cast<BonusType>(effect))
  1819. {
  1820. case BonusType::HP_REGENERATION:
  1821. {
  1822. int64_t toHeal = val;
  1823. st->heal(toHeal, EHealLevel::HEAL, EHealPower::PERMANENT);
  1824. break;
  1825. }
  1826. case BonusType::MANA_DRAIN:
  1827. {
  1828. CGHeroInstance * h = gs->getHero(ObjectInstanceID(additionalInfo));
  1829. st->drainedMana = true;
  1830. h->mana -= val;
  1831. vstd::amax(h->mana, 0);
  1832. break;
  1833. }
  1834. case BonusType::POISON:
  1835. {
  1836. auto b = st->getBonusLocalFirst(Selector::source(BonusSource::SPELL_EFFECT, SpellID::POISON)
  1837. .And(Selector::type()(BonusType::STACK_HEALTH)));
  1838. if (b)
  1839. b->val = val;
  1840. break;
  1841. }
  1842. case BonusType::ENCHANTER:
  1843. case BonusType::MORALE:
  1844. break;
  1845. case BonusType::FEAR:
  1846. st->fear = true;
  1847. break;
  1848. default:
  1849. logNetwork->error("Unrecognized trigger effect type %d", effect);
  1850. }
  1851. }
  1852. void BattleUpdateGateState::applyGs(CGameState * gs) const
  1853. {
  1854. if(gs->getBattle(battleID))
  1855. gs->getBattle(battleID)->si.gateState = state;
  1856. }
  1857. void BattleCancelled::applyGs(CGameState * gs) const
  1858. {
  1859. auto currentBattle = boost::range::find_if(gs->currentBattles, [&](const auto & battle)
  1860. {
  1861. return battle->battleID == battleID;
  1862. });
  1863. assert(currentBattle != gs->currentBattles.end());
  1864. gs->currentBattles.erase(currentBattle);
  1865. }
  1866. void BattleResultAccepted::applyGs(CGameState * gs) const
  1867. {
  1868. // Remove any "until next battle" bonuses
  1869. for(auto & res : heroResult)
  1870. {
  1871. if(res.hero)
  1872. res.hero->removeBonusesRecursive(Bonus::OneBattle);
  1873. }
  1874. if(winnerSide != 2)
  1875. {
  1876. // Grow up growing artifacts
  1877. const auto hero = heroResult[winnerSide].hero;
  1878. if (hero)
  1879. {
  1880. if(hero->commander && hero->commander->alive)
  1881. {
  1882. for(auto & art : hero->commander->artifactsWorn)
  1883. art.second.artifact->growingUp();
  1884. }
  1885. for(auto & art : hero->artifactsWorn)
  1886. {
  1887. art.second.artifact->growingUp();
  1888. }
  1889. }
  1890. }
  1891. if(VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE))
  1892. {
  1893. if(heroResult[0].army)
  1894. heroResult[0].army->giveStackExp(heroResult[0].exp);
  1895. if(heroResult[1].army)
  1896. heroResult[1].army->giveStackExp(heroResult[1].exp);
  1897. CBonusSystemNode::treeHasChanged();
  1898. }
  1899. auto currentBattle = boost::range::find_if(gs->currentBattles, [&](const auto & battle)
  1900. {
  1901. return battle->battleID == battleID;
  1902. });
  1903. assert(currentBattle != gs->currentBattles.end());
  1904. gs->currentBattles.erase(currentBattle);
  1905. }
  1906. void BattleLogMessage::applyGs(CGameState *gs)
  1907. {
  1908. //nothing
  1909. }
  1910. void BattleLogMessage::applyBattle(IBattleState * battleState)
  1911. {
  1912. //nothing
  1913. }
  1914. void BattleStackMoved::applyGs(CGameState *gs)
  1915. {
  1916. applyBattle(gs->getBattle(battleID));
  1917. }
  1918. void BattleStackMoved::applyBattle(IBattleState * battleState)
  1919. {
  1920. battleState->moveUnit(stack, tilesToMove.back());
  1921. }
  1922. void BattleStackAttacked::applyGs(CGameState * gs)
  1923. {
  1924. applyBattle(gs->getBattle(battleID));
  1925. }
  1926. void BattleStackAttacked::applyBattle(IBattleState * battleState)
  1927. {
  1928. battleState->setUnitState(newState.id, newState.data, newState.healthDelta);
  1929. }
  1930. void BattleAttack::applyGs(CGameState * gs)
  1931. {
  1932. CStack * attacker = gs->getBattle(battleID)->getStack(stackAttacking);
  1933. assert(attacker);
  1934. attackerChanges.applyGs(gs);
  1935. for(BattleStackAttacked & stackAttacked : bsa)
  1936. stackAttacked.applyGs(gs);
  1937. attacker->removeBonusesRecursive(Bonus::UntilAttack);
  1938. }
  1939. void StartAction::applyGs(CGameState *gs)
  1940. {
  1941. CStack *st = gs->getBattle(battleID)->getStack(ba.stackNumber);
  1942. if(ba.actionType == EActionType::END_TACTIC_PHASE)
  1943. {
  1944. gs->getBattle(battleID)->tacticDistance = 0;
  1945. return;
  1946. }
  1947. if(gs->getBattle(battleID)->tacticDistance)
  1948. {
  1949. // moves in tactics phase do not affect creature status
  1950. // (tactics stack queue is managed by client)
  1951. return;
  1952. }
  1953. if (ba.isUnitAction())
  1954. {
  1955. assert(st); // stack must exists for all non-hero actions
  1956. switch(ba.actionType)
  1957. {
  1958. case EActionType::DEFEND:
  1959. st->waiting = false;
  1960. st->defending = true;
  1961. st->defendingAnim = true;
  1962. break;
  1963. case EActionType::WAIT:
  1964. st->defendingAnim = false;
  1965. st->waiting = true;
  1966. st->waitedThisTurn = true;
  1967. break;
  1968. case EActionType::HERO_SPELL: //no change in current stack state
  1969. break;
  1970. default: //any active stack action - attack, catapult, heal, spell...
  1971. st->waiting = false;
  1972. st->defendingAnim = false;
  1973. st->movedThisRound = true;
  1974. break;
  1975. }
  1976. }
  1977. else
  1978. {
  1979. if(ba.actionType == EActionType::HERO_SPELL)
  1980. gs->getBattle(battleID)->sides[ba.side].usedSpellsHistory.push_back(ba.spell);
  1981. }
  1982. }
  1983. void BattleSpellCast::applyGs(CGameState * gs) const
  1984. {
  1985. if(castByHero)
  1986. {
  1987. if(side < 2)
  1988. {
  1989. gs->getBattle(battleID)->sides[side].castSpellsCount++;
  1990. }
  1991. }
  1992. }
  1993. void SetStackEffect::applyGs(CGameState *gs)
  1994. {
  1995. applyBattle(gs->getBattle(battleID));
  1996. }
  1997. void SetStackEffect::applyBattle(IBattleState * battleState)
  1998. {
  1999. for(const auto & stackData : toRemove)
  2000. battleState->removeUnitBonus(stackData.first, stackData.second);
  2001. for(const auto & stackData : toUpdate)
  2002. battleState->updateUnitBonus(stackData.first, stackData.second);
  2003. for(const auto & stackData : toAdd)
  2004. battleState->addUnitBonus(stackData.first, stackData.second);
  2005. }
  2006. void StacksInjured::applyGs(CGameState *gs)
  2007. {
  2008. applyBattle(gs->getBattle(battleID));
  2009. }
  2010. void StacksInjured::applyBattle(IBattleState * battleState)
  2011. {
  2012. for(BattleStackAttacked stackAttacked : stacks)
  2013. stackAttacked.applyBattle(battleState);
  2014. }
  2015. void BattleUnitsChanged::applyGs(CGameState *gs)
  2016. {
  2017. applyBattle(gs->getBattle(battleID));
  2018. }
  2019. void BattleUnitsChanged::applyBattle(IBattleState * battleState)
  2020. {
  2021. for(auto & elem : changedStacks)
  2022. {
  2023. switch(elem.operation)
  2024. {
  2025. case BattleChanges::EOperation::RESET_STATE:
  2026. battleState->setUnitState(elem.id, elem.data, elem.healthDelta);
  2027. break;
  2028. case BattleChanges::EOperation::REMOVE:
  2029. battleState->removeUnit(elem.id);
  2030. break;
  2031. case BattleChanges::EOperation::ADD:
  2032. battleState->addUnit(elem.id, elem.data);
  2033. break;
  2034. case BattleChanges::EOperation::UPDATE:
  2035. battleState->updateUnit(elem.id, elem.data);
  2036. break;
  2037. default:
  2038. logNetwork->error("Unknown unit operation %d", static_cast<int>(elem.operation));
  2039. break;
  2040. }
  2041. }
  2042. }
  2043. void BattleObstaclesChanged::applyGs(CGameState * gs)
  2044. {
  2045. applyBattle(gs->getBattle(battleID));
  2046. }
  2047. void BattleObstaclesChanged::applyBattle(IBattleState * battleState)
  2048. {
  2049. for(const auto & change : changes)
  2050. {
  2051. switch(change.operation)
  2052. {
  2053. case BattleChanges::EOperation::REMOVE:
  2054. battleState->removeObstacle(change.id);
  2055. break;
  2056. case BattleChanges::EOperation::ADD:
  2057. battleState->addObstacle(change);
  2058. break;
  2059. case BattleChanges::EOperation::UPDATE:
  2060. battleState->updateObstacle(change);
  2061. break;
  2062. default:
  2063. logNetwork->error("Unknown obstacle operation %d", static_cast<int>(change.operation));
  2064. break;
  2065. }
  2066. }
  2067. }
  2068. CatapultAttack::CatapultAttack() = default;
  2069. CatapultAttack::~CatapultAttack() = default;
  2070. void CatapultAttack::applyGs(CGameState * gs)
  2071. {
  2072. applyBattle(gs->getBattle(battleID));
  2073. }
  2074. void CatapultAttack::visitTyped(ICPackVisitor & visitor)
  2075. {
  2076. visitor.visitCatapultAttack(*this);
  2077. }
  2078. void CatapultAttack::applyBattle(IBattleState * battleState)
  2079. {
  2080. const auto * town = battleState->getDefendedTown();
  2081. if(!town)
  2082. return;
  2083. if(town->fortLevel() == CGTownInstance::NONE)
  2084. return;
  2085. for(const auto & part : attackedParts)
  2086. {
  2087. auto newWallState = SiegeInfo::applyDamage(battleState->getWallState(part.attackedPart), part.damageDealt);
  2088. battleState->setWallState(part.attackedPart, newWallState);
  2089. }
  2090. }
  2091. void BattleSetStackProperty::applyGs(CGameState * gs) const
  2092. {
  2093. CStack * stack = gs->getBattle(battleID)->getStack(stackID);
  2094. switch(which)
  2095. {
  2096. case CASTS:
  2097. {
  2098. if(absolute)
  2099. logNetwork->error("Can not change casts in absolute mode");
  2100. else
  2101. stack->casts.use(-val);
  2102. break;
  2103. }
  2104. case ENCHANTER_COUNTER:
  2105. {
  2106. auto & counter = gs->getBattle(battleID)->sides[gs->getBattle(battleID)->whatSide(stack->unitOwner())].enchanterCounter;
  2107. if(absolute)
  2108. counter = val;
  2109. else
  2110. counter += val;
  2111. vstd::amax(counter, 0);
  2112. break;
  2113. }
  2114. case UNBIND:
  2115. {
  2116. stack->removeBonusesRecursive(Selector::type()(BonusType::BIND_EFFECT));
  2117. break;
  2118. }
  2119. case CLONED:
  2120. {
  2121. stack->cloned = true;
  2122. break;
  2123. }
  2124. case HAS_CLONE:
  2125. {
  2126. stack->cloneID = val;
  2127. break;
  2128. }
  2129. }
  2130. }
  2131. void PlayerCheated::applyGs(CGameState * gs) const
  2132. {
  2133. if(!player.isValidPlayer())
  2134. return;
  2135. gs->getPlayerState(player)->enteredLosingCheatCode = losingCheatCode;
  2136. gs->getPlayerState(player)->enteredWinningCheatCode = winningCheatCode;
  2137. gs->getPlayerState(player)->cheated = true;
  2138. }
  2139. void PlayerStartsTurn::applyGs(CGameState * gs) const
  2140. {
  2141. assert(gs->actingPlayers.count(player) == 0);
  2142. gs->actingPlayers.insert(player);
  2143. }
  2144. void PlayerEndsTurn::applyGs(CGameState * gs) const
  2145. {
  2146. assert(gs->actingPlayers.count(player) == 1);
  2147. gs->actingPlayers.erase(player);
  2148. }
  2149. void DaysWithoutTown::applyGs(CGameState * gs) const
  2150. {
  2151. auto & playerState = gs->players[player];
  2152. playerState.daysWithoutCastle = daysWithoutCastle;
  2153. }
  2154. void TurnTimeUpdate::applyGs(CGameState *gs) const
  2155. {
  2156. auto & playerState = gs->players[player];
  2157. playerState.turnTimer = turnTimer;
  2158. }
  2159. Component::Component(const CStackBasicDescriptor & stack)
  2160. : id(EComponentType::CREATURE)
  2161. , subtype(stack.type->getId())
  2162. , val(stack.count)
  2163. {
  2164. }
  2165. void EntitiesChanged::applyGs(CGameState * gs)
  2166. {
  2167. for(const auto & change : changes)
  2168. gs->updateEntity(change.metatype, change.entityIndex, change.data);
  2169. }
  2170. const CArtifactInstance * ArtSlotInfo::getArt() const
  2171. {
  2172. if(locked)
  2173. {
  2174. logNetwork->warn("ArtifactLocation::getArt: This location is locked!");
  2175. return nullptr;
  2176. }
  2177. return artifact;
  2178. }
  2179. CArtifactSet * BulkMoveArtifacts::getSrcHolderArtSet()
  2180. {
  2181. return std::visit(GetBase<CArtifactSet>(), srcArtHolder);
  2182. }
  2183. CArtifactSet * BulkMoveArtifacts::getDstHolderArtSet()
  2184. {
  2185. return std::visit(GetBase<CArtifactSet>(), dstArtHolder);
  2186. }
  2187. VCMI_LIB_NAMESPACE_END