NetPacksLib.cpp 62 KB

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