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