NetPacksLib.cpp 60 KB

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