NetPacksLib.cpp 62 KB

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