MapFormatH3M.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739
  1. /*
  2. * MapFormatH3M.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 "MapFormatH3M.h"
  12. #include "CMap.h"
  13. #include "MapReaderH3M.h"
  14. #include "MapFormatSettings.h"
  15. #include "../CCreatureHandler.h"
  16. #include "../texts/CGeneralTextHandler.h"
  17. #include "../CSkillHandler.h"
  18. #include "../CStopWatch.h"
  19. #include "../IGameSettings.h"
  20. #include "../RiverHandler.h"
  21. #include "../RoadHandler.h"
  22. #include "../TerrainHandler.h"
  23. #include "../GameLibrary.h"
  24. #include "../constants/StringConstants.h"
  25. #include "../entities/artifact/CArtHandler.h"
  26. #include "../entities/hero/CHeroHandler.h"
  27. #include "../filesystem/CBinaryReader.h"
  28. #include "../filesystem/Filesystem.h"
  29. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  30. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  31. #include "../mapObjectConstructors/CommonConstructors.h"
  32. #include "../mapObjects/CGCreature.h"
  33. #include "../mapObjects/CGResource.h"
  34. #include "../mapObjects/CQuest.h"
  35. #include "../mapObjects/MapObjects.h"
  36. #include "../mapObjects/ObjectTemplate.h"
  37. #include "../modding/ModScope.h"
  38. #include "../networkPacks/Component.h"
  39. #include "../networkPacks/ArtifactLocation.h"
  40. #include "../spells/CSpellHandler.h"
  41. #include "../texts/TextOperations.h"
  42. VCMI_LIB_NAMESPACE_BEGIN
  43. static std::string convertMapName(std::string input)
  44. {
  45. boost::algorithm::to_lower(input);
  46. boost::algorithm::trim(input);
  47. boost::algorithm::erase_all(input, ".");
  48. size_t slashPos = input.find_last_of('/');
  49. if(slashPos != std::string::npos)
  50. return input.substr(slashPos + 1);
  51. return input;
  52. }
  53. CMapLoaderH3M::CMapLoaderH3M(const std::string & mapName, const std::string & modName, const std::string & encodingName, CInputStream * stream)
  54. : map(nullptr)
  55. , reader(new MapReaderH3M(stream))
  56. , inputStream(stream)
  57. , mapName(convertMapName(mapName))
  58. , modName(modName)
  59. , fileEncoding(encodingName)
  60. {
  61. }
  62. //must be instantiated in .cpp file for access to complete types of all member fields
  63. CMapLoaderH3M::~CMapLoaderH3M() = default;
  64. std::unique_ptr<CMap> CMapLoaderH3M::loadMap(IGameInfoCallback * cb)
  65. {
  66. // Init map object by parsing the input buffer
  67. map = new CMap(cb);
  68. mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));
  69. init();
  70. return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));
  71. }
  72. std::unique_ptr<CMapHeader> CMapLoaderH3M::loadMapHeader()
  73. {
  74. // Read header
  75. mapHeader = std::make_unique<CMapHeader>();
  76. readHeader();
  77. return std::move(mapHeader);
  78. }
  79. void CMapLoaderH3M::init()
  80. {
  81. inputStream->seek(0);
  82. readHeader();
  83. readMapOptions();
  84. readAllowedArtifacts();
  85. readAllowedSpellsAbilities();
  86. readRumors();
  87. readPredefinedHeroes();
  88. readTerrain();
  89. readObjectTemplates();
  90. readObjects();
  91. readEvents();
  92. map->calculateGuardingGreaturePositions();
  93. afterRead();
  94. //map->banWaterContent(); //Not sure if force this for custom scenarios
  95. }
  96. void CMapLoaderH3M::readHeader()
  97. {
  98. // Map version
  99. mapHeader->version = static_cast<EMapFormat>(reader->readUInt32());
  100. if(mapHeader->version == EMapFormat::HOTA)
  101. {
  102. uint32_t hotaVersion = reader->readUInt32();
  103. features = MapFormatFeaturesH3M::find(mapHeader->version, hotaVersion);
  104. reader->setFormatLevel(features);
  105. if(features.levelHOTA8)
  106. {
  107. int hotaVersionMajor = reader->readUInt32();
  108. int hotaVersionMinor = reader->readUInt32();
  109. int hotaVersionPatch = reader->readUInt32();
  110. logGlobal->trace("Loading HotA map, version %d.%d.%d", hotaVersionMajor, hotaVersionMinor, hotaVersionPatch);
  111. }
  112. if(features.levelHOTA1)
  113. {
  114. bool isMirrorMap = reader->readBool();
  115. bool isArenaMap = reader->readBool();
  116. //TODO: HotA
  117. if (isMirrorMap)
  118. logGlobal->warn("Map '%s': Mirror maps are not yet supported!", mapName);
  119. if (isArenaMap)
  120. logGlobal->warn("Map '%s': Arena maps are not supported!", mapName);
  121. }
  122. if(features.levelHOTA2)
  123. {
  124. int32_t terrainTypesCount = reader->readUInt32();
  125. assert(features.terrainsCount == terrainTypesCount);
  126. if (features.terrainsCount != terrainTypesCount)
  127. logGlobal->warn("Map '%s': Expected %d terrains, but %d found!", mapName, features.terrainsCount, terrainTypesCount);
  128. }
  129. if(features.levelHOTA5)
  130. {
  131. int32_t townTypesCount = reader->readUInt32();
  132. int8_t allowedDifficultiesMask = reader->readInt8Checked(0, 31);
  133. assert(features.factionsCount == townTypesCount);
  134. if (features.factionsCount != townTypesCount)
  135. logGlobal->warn("Map '%s': Expected %d factions, but %d found!", mapName, features.factionsCount, townTypesCount);
  136. if (allowedDifficultiesMask != 0)
  137. logGlobal->warn("Map '%s': List of allowed difficulties (%d) is not implemented!", mapName, static_cast<int>(allowedDifficultiesMask));
  138. }
  139. if(features.levelHOTA7)
  140. {
  141. bool canHireDefeatedHeroes = reader->readBool();
  142. if (!canHireDefeatedHeroes)
  143. logGlobal->warn("Map '%s': Option to block hiring of defeated heroes is not implemented!", mapName);
  144. }
  145. if(features.levelHOTA8)
  146. {
  147. bool forceMatchingVersion = reader->readBool();
  148. if (forceMatchingVersion)
  149. logGlobal->warn("Map '%s': This map is forced to use specific hota version!", mapName);
  150. }
  151. }
  152. else
  153. {
  154. features = MapFormatFeaturesH3M::find(mapHeader->version, 0);
  155. reader->setFormatLevel(features);
  156. }
  157. if (!LIBRARY->mapFormat->isSupported(mapHeader->version))
  158. throw std::runtime_error("Unsupported map format! Format ID " + std::to_string(static_cast<int>(mapHeader->version)));
  159. const MapIdentifiersH3M & identifierMapper = LIBRARY->mapFormat->getMapping(mapHeader->version);
  160. reader->setIdentifierRemapper(identifierMapper);
  161. // Read map name, description, dimensions,...
  162. mapHeader->areAnyPlayers = reader->readBool();
  163. mapHeader->height = mapHeader->width = reader->readInt32();
  164. mapHeader->twoLevel = reader->readBool();
  165. mapHeader->name.appendTextID(readLocalizedString("header.name"));
  166. mapHeader->description.appendTextID(readLocalizedString("header.description"));
  167. mapHeader->author.appendRawString("");
  168. mapHeader->authorContact.appendRawString("");
  169. mapHeader->mapVersion.appendRawString("");
  170. mapHeader->creationDateTime = 0;
  171. mapHeader->difficulty = static_cast<EMapDifficulty>(reader->readInt8Checked(0, 4));
  172. if(features.levelAB)
  173. mapHeader->levelLimit = reader->readInt8Checked(0, std::min(100u, LIBRARY->heroh->maxSupportedLevel()));
  174. else
  175. mapHeader->levelLimit = 0;
  176. readPlayerInfo();
  177. readVictoryLossConditions();
  178. readTeamInfo();
  179. readAllowedHeroes();
  180. readDisposedHeroes();
  181. }
  182. void CMapLoaderH3M::readPlayerInfo()
  183. {
  184. for(int i = 0; i < mapHeader->players.size(); ++i)
  185. {
  186. auto & playerInfo = mapHeader->players[i];
  187. playerInfo.canHumanPlay = reader->readBool();
  188. playerInfo.canComputerPlay = reader->readBool();
  189. // If nobody can play with this player - skip loading of these properties
  190. if((!(playerInfo.canHumanPlay || playerInfo.canComputerPlay)))
  191. {
  192. if(features.levelROE)
  193. reader->skipUnused(6);
  194. if(features.levelAB)
  195. reader->skipUnused(6);
  196. if(features.levelSOD)
  197. reader->skipUnused(1);
  198. continue;
  199. }
  200. playerInfo.aiTactic = static_cast<EAiTactic>(reader->readInt8Checked(-1, 3));
  201. if(features.levelSOD)
  202. reader->skipUnused(1); //faction is selectable
  203. std::set<FactionID> allowedFactions;
  204. reader->readBitmaskFactions(allowedFactions, false);
  205. playerInfo.isFactionRandom = reader->readBool();
  206. const bool allFactionsAllowed = playerInfo.isFactionRandom && allowedFactions.size() == features.factionsCount;
  207. if(!allFactionsAllowed)
  208. playerInfo.allowedFactions = allowedFactions;
  209. playerInfo.hasMainTown = reader->readBool();
  210. if(playerInfo.hasMainTown)
  211. {
  212. if(features.levelAB)
  213. {
  214. playerInfo.generateHeroAtMainTown = reader->readBool();
  215. reader->skipUnused(1); // starting town type, unused
  216. }
  217. else
  218. {
  219. playerInfo.generateHeroAtMainTown = true;
  220. }
  221. playerInfo.posOfMainTown = reader->readInt3();
  222. }
  223. playerInfo.hasRandomHero = reader->readBool();
  224. playerInfo.mainCustomHeroId = reader->readHero();
  225. if(playerInfo.mainCustomHeroId != HeroTypeID::NONE)
  226. {
  227. playerInfo.mainCustomHeroPortrait = reader->readHeroPortrait();
  228. playerInfo.mainCustomHeroNameTextId = readLocalizedString(TextIdentifier("header", "player", i, "mainHeroName"));
  229. }
  230. if(features.levelAB)
  231. {
  232. reader->skipUnused(1); //TODO: check meaning?
  233. size_t heroCount = reader->readUInt32();
  234. for(size_t pp = 0; pp < heroCount; ++pp)
  235. {
  236. SHeroName vv;
  237. vv.heroId = reader->readHero();
  238. vv.heroName = readLocalizedString(TextIdentifier("header", "heroNames", vv.heroId.getNum()));
  239. playerInfo.heroesNames.push_back(vv);
  240. }
  241. }
  242. }
  243. }
  244. void CMapLoaderH3M::readVictoryLossConditions()
  245. {
  246. mapHeader->triggeredEvents.clear();
  247. mapHeader->victoryMessage.clear();
  248. mapHeader->defeatMessage.clear();
  249. auto vicCondition = static_cast<EVictoryConditionType>(reader->readInt8Checked(-1, 12));
  250. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  251. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  252. defeatCondition.value = 7;
  253. TriggeredEvent standardVictory;
  254. standardVictory.effect.type = EventEffect::VICTORY;
  255. standardVictory.effect.toOtherMessage.appendTextID("core.genrltxt.5");
  256. standardVictory.identifier = "standardVictory";
  257. standardVictory.description.clear(); // TODO: display in quest window
  258. standardVictory.onFulfill.appendTextID("core.genrltxt.659");
  259. standardVictory.trigger = EventExpression(victoryCondition);
  260. TriggeredEvent standardDefeat;
  261. standardDefeat.effect.type = EventEffect::DEFEAT;
  262. standardDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.8");
  263. standardDefeat.identifier = "standardDefeat";
  264. standardDefeat.description.clear(); // TODO: display in quest window
  265. standardDefeat.onFulfill.appendTextID("core.genrltxt.7");
  266. standardDefeat.trigger = EventExpression(defeatCondition);
  267. // Specific victory conditions
  268. if(vicCondition == EVictoryConditionType::WINSTANDARD)
  269. {
  270. // create normal condition
  271. mapHeader->triggeredEvents.push_back(standardVictory);
  272. mapHeader->victoryIconIndex = 11;
  273. mapHeader->victoryMessage.appendTextID("core.vcdesc.0");
  274. }
  275. else
  276. {
  277. TriggeredEvent specialVictory;
  278. specialVictory.effect.type = EventEffect::VICTORY;
  279. specialVictory.identifier = "specialVictory";
  280. specialVictory.description.clear(); // TODO: display in quest window
  281. mapHeader->victoryIconIndex = static_cast<ui16>(vicCondition);
  282. bool allowNormalVictory = reader->readBool();
  283. bool appliesToAI = reader->readBool();
  284. switch(vicCondition)
  285. {
  286. case EVictoryConditionType::ARTIFACT:
  287. {
  288. assert(allowNormalVictory == true); // not selectable in editor
  289. EventCondition cond(EventCondition::HAVE_ARTIFACT);
  290. cond.objectType = reader->readArtifact();
  291. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.281");
  292. specialVictory.onFulfill.appendTextID("core.genrltxt.280");
  293. specialVictory.trigger = EventExpression(cond);
  294. mapHeader->victoryMessage.appendTextID("core.vcdesc.1");
  295. break;
  296. }
  297. case EVictoryConditionType::GATHERTROOP:
  298. {
  299. EventCondition cond(EventCondition::HAVE_CREATURES);
  300. cond.objectType = reader->readCreature();
  301. cond.value = reader->readInt32();
  302. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.277");
  303. specialVictory.onFulfill.appendTextID("core.genrltxt.276");
  304. specialVictory.trigger = EventExpression(cond);
  305. mapHeader->victoryMessage.appendTextID("core.vcdesc.2");
  306. break;
  307. }
  308. case EVictoryConditionType::GATHERRESOURCE:
  309. {
  310. EventCondition cond(EventCondition::HAVE_RESOURCES);
  311. cond.objectType = reader->readGameResID();
  312. cond.value = reader->readInt32();
  313. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.279");
  314. specialVictory.onFulfill.appendTextID("core.genrltxt.278");
  315. specialVictory.trigger = EventExpression(cond);
  316. mapHeader->victoryMessage.appendTextID("core.vcdesc.3");
  317. break;
  318. }
  319. case EVictoryConditionType::BUILDCITY:
  320. {
  321. assert(appliesToAI == true); // not selectable in editor
  322. EventExpression::OperatorAll oper;
  323. EventCondition cond(EventCondition::HAVE_BUILDING);
  324. cond.position = reader->readInt3();
  325. cond.objectType = BuildingID::HALL_LEVEL(reader->readInt8Checked(0,3) + 1);
  326. oper.expressions.emplace_back(cond);
  327. cond.objectType = BuildingID::FORT_LEVEL(reader->readInt8Checked(0, 2));
  328. oper.expressions.emplace_back(cond);
  329. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.283");
  330. specialVictory.onFulfill.appendTextID("core.genrltxt.282");
  331. specialVictory.trigger = EventExpression(oper);
  332. mapHeader->victoryMessage.appendTextID("core.vcdesc.4");
  333. break;
  334. }
  335. case EVictoryConditionType::BUILDGRAIL:
  336. {
  337. assert(allowNormalVictory == true); // not selectable in editor
  338. assert(appliesToAI == true); // not selectable in editor
  339. EventCondition cond(EventCondition::HAVE_BUILDING);
  340. cond.objectType = BuildingID(BuildingID::GRAIL);
  341. cond.position = reader->readInt3();
  342. if(cond.position.z > 2)
  343. cond.position = int3(-1, -1, -1);
  344. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.285");
  345. specialVictory.onFulfill.appendTextID("core.genrltxt.284");
  346. specialVictory.trigger = EventExpression(cond);
  347. mapHeader->victoryMessage.appendTextID("core.vcdesc.5");
  348. break;
  349. }
  350. case EVictoryConditionType::BEATHERO:
  351. {
  352. if (!allowNormalVictory)
  353. logGlobal->debug("Map %s: Has 'beat hero' as victory condition, but 'allow normal victory' not set. Ignoring", mapName);
  354. allowNormalVictory = true; // H3 behavior
  355. assert(appliesToAI == false); // not selectable in editor
  356. EventCondition cond(EventCondition::DESTROY);
  357. cond.objectType = MapObjectID(MapObjectID::HERO);
  358. cond.position = reader->readInt3();
  359. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.253");
  360. specialVictory.onFulfill.appendTextID("core.genrltxt.252");
  361. specialVictory.trigger = EventExpression(cond);
  362. mapHeader->victoryMessage.appendTextID("core.vcdesc.6");
  363. break;
  364. }
  365. case EVictoryConditionType::CAPTURECITY:
  366. {
  367. EventCondition cond(EventCondition::CONTROL);
  368. cond.objectType = MapObjectID(MapObjectID::TOWN);
  369. cond.position = reader->readInt3();
  370. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.250");
  371. specialVictory.onFulfill.appendTextID("core.genrltxt.249");
  372. specialVictory.trigger = EventExpression(cond);
  373. mapHeader->victoryMessage.appendTextID("core.vcdesc.7");
  374. break;
  375. }
  376. case EVictoryConditionType::BEATMONSTER:
  377. {
  378. assert(appliesToAI == true); // not selectable in editor
  379. EventCondition cond(EventCondition::DESTROY);
  380. cond.objectType = MapObjectID(MapObjectID::MONSTER);
  381. cond.position = reader->readInt3();
  382. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.287");
  383. specialVictory.onFulfill.appendTextID("core.genrltxt.286");
  384. specialVictory.trigger = EventExpression(cond);
  385. mapHeader->victoryMessage.appendTextID("core.vcdesc.8");
  386. break;
  387. }
  388. case EVictoryConditionType::TAKEDWELLINGS:
  389. {
  390. EventExpression::OperatorAll oper;
  391. oper.expressions.emplace_back(EventCondition(EventCondition::CONTROL, 0, Obj(Obj::CREATURE_GENERATOR1)));
  392. oper.expressions.emplace_back(EventCondition(EventCondition::CONTROL, 0, Obj(Obj::CREATURE_GENERATOR4)));
  393. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.289");
  394. specialVictory.onFulfill.appendTextID("core.genrltxt.288");
  395. specialVictory.trigger = EventExpression(oper);
  396. mapHeader->victoryMessage.appendTextID("core.vcdesc.9");
  397. break;
  398. }
  399. case EVictoryConditionType::TAKEMINES:
  400. {
  401. EventCondition cond(EventCondition::CONTROL);
  402. cond.objectType = MapObjectID(MapObjectID::MINE);
  403. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.291");
  404. specialVictory.onFulfill.appendTextID("core.genrltxt.290");
  405. specialVictory.trigger = EventExpression(cond);
  406. mapHeader->victoryMessage.appendTextID("core.vcdesc.10");
  407. break;
  408. }
  409. case EVictoryConditionType::TRANSPORTITEM:
  410. {
  411. assert(allowNormalVictory == true); // not selectable in editor
  412. EventCondition cond(EventCondition::TRANSPORT);
  413. cond.objectType = reader->readArtifact8();
  414. cond.position = reader->readInt3();
  415. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.293");
  416. specialVictory.onFulfill.appendTextID("core.genrltxt.292");
  417. specialVictory.trigger = EventExpression(cond);
  418. mapHeader->victoryMessage.appendTextID("core.vcdesc.11");
  419. break;
  420. }
  421. case EVictoryConditionType::HOTA_ELIMINATE_ALL_MONSTERS:
  422. {
  423. assert(appliesToAI == false); // not selectable in editor
  424. EventCondition cond(EventCondition::DESTROY);
  425. cond.objectType = MapObjectID(MapObjectID::MONSTER);
  426. specialVictory.effect.toOtherMessage.appendTextID("vcmi.map.victoryCondition.eliminateMonsters.toOthers");
  427. specialVictory.onFulfill.appendTextID("vcmi.map.victoryCondition.eliminateMonsters.toSelf");
  428. specialVictory.trigger = EventExpression(cond);
  429. mapHeader->victoryMessage.appendTextID("core.vcdesc.12");
  430. mapHeader->victoryIconIndex = 12;
  431. break;
  432. }
  433. case EVictoryConditionType::HOTA_SURVIVE_FOR_DAYS:
  434. {
  435. assert(appliesToAI == false); // not selectable in editor
  436. EventCondition cond(EventCondition::DAYS_PASSED);
  437. cond.value = reader->readUInt32();
  438. specialVictory.effect.toOtherMessage.appendTextID("vcmi.map.victoryCondition.daysPassed.toOthers");
  439. specialVictory.onFulfill.appendTextID("vcmi.map.victoryCondition.daysPassed.toSelf");
  440. specialVictory.trigger = EventExpression(cond);
  441. mapHeader->victoryMessage.appendTextID("core.vcdesc.13");
  442. mapHeader->victoryIconIndex = 13;
  443. break;
  444. }
  445. default:
  446. assert(0);
  447. }
  448. if(allowNormalVictory)
  449. {
  450. size_t playersOnMap = boost::range::count_if(
  451. mapHeader->players,
  452. [](const PlayerInfo & info)
  453. {
  454. return info.canAnyonePlay();
  455. }
  456. );
  457. if(playersOnMap == 1)
  458. {
  459. logGlobal->warn("Map %s: Only one player exists, but normal victory allowed!", mapName);
  460. allowNormalVictory = false; // makes sense? Not much. Works as H3? Yes!
  461. }
  462. }
  463. // if condition is human-only turn it into following construction: AllOf(human, condition)
  464. if(!appliesToAI)
  465. {
  466. EventExpression::OperatorAll oper;
  467. EventCondition notAI(EventCondition::IS_HUMAN);
  468. notAI.value = 1;
  469. oper.expressions.emplace_back(notAI);
  470. oper.expressions.push_back(specialVictory.trigger.get());
  471. specialVictory.trigger = EventExpression(oper);
  472. }
  473. // if normal victory allowed - add one more quest
  474. if(allowNormalVictory)
  475. {
  476. mapHeader->victoryMessage.appendRawString(" / ");
  477. mapHeader->victoryMessage.appendTextID("core.vcdesc.0");
  478. mapHeader->triggeredEvents.push_back(standardVictory);
  479. }
  480. mapHeader->triggeredEvents.push_back(specialVictory);
  481. }
  482. // Read loss conditions
  483. auto lossCond = static_cast<ELossConditionType>(reader->readInt8Checked(-1, 2));
  484. if(lossCond == ELossConditionType::LOSSSTANDARD)
  485. {
  486. mapHeader->defeatIconIndex = 3;
  487. mapHeader->defeatMessage.appendTextID("core.lcdesc.0");
  488. }
  489. else
  490. {
  491. TriggeredEvent specialDefeat;
  492. specialDefeat.effect.type = EventEffect::DEFEAT;
  493. specialDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.5");
  494. specialDefeat.identifier = "specialDefeat";
  495. specialDefeat.description.clear(); // TODO: display in quest window
  496. mapHeader->defeatIconIndex = static_cast<ui16>(lossCond);
  497. switch(lossCond)
  498. {
  499. case ELossConditionType::LOSSCASTLE:
  500. {
  501. EventExpression::OperatorNone noneOf;
  502. EventCondition cond(EventCondition::CONTROL);
  503. cond.objectType = Obj(Obj::TOWN);
  504. cond.position = reader->readInt3();
  505. noneOf.expressions.emplace_back(cond);
  506. specialDefeat.onFulfill.appendTextID("core.genrltxt.251");
  507. specialDefeat.trigger = EventExpression(noneOf);
  508. mapHeader->defeatMessage.appendTextID("core.lcdesc.1");
  509. break;
  510. }
  511. case ELossConditionType::LOSSHERO:
  512. {
  513. EventExpression::OperatorNone noneOf;
  514. EventCondition cond(EventCondition::CONTROL);
  515. cond.objectType = Obj(Obj::HERO);
  516. cond.position = reader->readInt3();
  517. noneOf.expressions.emplace_back(cond);
  518. specialDefeat.onFulfill.appendTextID("core.genrltxt.253");
  519. specialDefeat.trigger = EventExpression(noneOf);
  520. mapHeader->defeatMessage.appendTextID("core.lcdesc.2");
  521. break;
  522. }
  523. case ELossConditionType::TIMEEXPIRES:
  524. {
  525. EventCondition cond(EventCondition::DAYS_PASSED);
  526. cond.value = reader->readUInt16();
  527. specialDefeat.onFulfill.appendTextID("core.genrltxt.254");
  528. specialDefeat.trigger = EventExpression(cond);
  529. mapHeader->defeatMessage.appendTextID("core.lcdesc.3");
  530. break;
  531. }
  532. }
  533. // turn simple loss condition into complete one that can be evaluated later:
  534. // - any of :
  535. // - days without town: 7
  536. // - all of:
  537. // - is human
  538. // - (expression)
  539. EventExpression::OperatorAll allOf;
  540. EventCondition isHuman(EventCondition::IS_HUMAN);
  541. isHuman.value = 1;
  542. allOf.expressions.emplace_back(isHuman);
  543. allOf.expressions.push_back(specialDefeat.trigger.get());
  544. specialDefeat.trigger = EventExpression(allOf);
  545. mapHeader->triggeredEvents.push_back(specialDefeat);
  546. }
  547. mapHeader->triggeredEvents.push_back(standardDefeat);
  548. }
  549. void CMapLoaderH3M::readTeamInfo()
  550. {
  551. mapHeader->howManyTeams = reader->readUInt8();
  552. if(mapHeader->howManyTeams > 0)
  553. {
  554. // Teams
  555. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
  556. mapHeader->players[i].team = TeamID(reader->readUInt8());
  557. }
  558. else
  559. {
  560. // No alliances
  561. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  562. if(mapHeader->players[i].canComputerPlay || mapHeader->players[i].canHumanPlay)
  563. mapHeader->players[i].team = TeamID(mapHeader->howManyTeams++);
  564. }
  565. }
  566. void CMapLoaderH3M::readAllowedHeroes()
  567. {
  568. mapHeader->allowedHeroes = LIBRARY->heroh->getDefaultAllowed();
  569. if(features.levelHOTA0)
  570. reader->readBitmaskHeroesSized(mapHeader->allowedHeroes, false);
  571. else
  572. reader->readBitmaskHeroes(mapHeader->allowedHeroes, false);
  573. if(features.levelAB)
  574. {
  575. size_t placeholdersQty = reader->readUInt32();
  576. for (size_t i = 0; i < placeholdersQty; ++i)
  577. {
  578. auto heroID = reader->readHero();
  579. mapHeader->reservedCampaignHeroes.insert(heroID);
  580. }
  581. }
  582. }
  583. void CMapLoaderH3M::readDisposedHeroes()
  584. {
  585. // Reading disposed heroes (20 bytes)
  586. if(features.levelSOD)
  587. {
  588. size_t disp = reader->readUInt8();
  589. mapHeader->disposedHeroes.resize(disp);
  590. for(size_t g = 0; g < disp; ++g)
  591. {
  592. mapHeader->disposedHeroes[g].heroId = reader->readHero();
  593. mapHeader->disposedHeroes[g].portrait = reader->readHeroPortrait();
  594. mapHeader->disposedHeroes[g].name = readLocalizedString(TextIdentifier("header", "heroes", mapHeader->disposedHeroes[g].heroId.getNum()));
  595. reader->readBitmaskPlayers(mapHeader->disposedHeroes[g].players, false);
  596. }
  597. }
  598. }
  599. void CMapLoaderH3M::readMapOptions()
  600. {
  601. //omitting NULLS
  602. reader->skipZero(31);
  603. if(features.levelHOTA0)
  604. {
  605. bool allowSpecialMonths = reader->readBool();
  606. map->overrideGameSetting(EGameSettings::CREATURES_ALLOW_RANDOM_SPECIAL_WEEKS, JsonNode(allowSpecialMonths));
  607. reader->skipZero(3);
  608. }
  609. if(features.levelHOTA1)
  610. {
  611. int32_t combinedArtifactsCount = reader->readInt32();
  612. int32_t combinedArtifactsBytes = (combinedArtifactsCount + 7) / 8;
  613. for (int i = 0; i < combinedArtifactsBytes; ++i)
  614. {
  615. uint8_t mask = reader->readUInt8();
  616. if (mask != 0)
  617. logGlobal->warn("Map '%s': Option to ban specific combined artifacts is not implemented!", mapName);
  618. }
  619. }
  620. if(features.levelHOTA3)
  621. {
  622. //TODO: HotA
  623. int32_t roundLimit = reader->readInt32();
  624. if(roundLimit != -1)
  625. logGlobal->warn("Map '%s': roundLimit of %d is not implemented!", mapName, roundLimit);
  626. }
  627. if(features.levelHOTA5)
  628. {
  629. for (int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
  630. {
  631. // unconfirmed, but only remainig option according to changelog
  632. bool heroRecruitmentBlocked = reader->readBool();
  633. if (heroRecruitmentBlocked)
  634. logGlobal->warn("Map '%s': option to ban hero recruitment for %s is not implemented!!", mapName, PlayerColor(i).toString());
  635. }
  636. }
  637. }
  638. void CMapLoaderH3M::readAllowedArtifacts()
  639. {
  640. map->allowedArtifact = LIBRARY->arth->getDefaultAllowed();
  641. if(features.levelAB)
  642. {
  643. if(features.levelHOTA0)
  644. reader->readBitmaskArtifactsSized(map->allowedArtifact, true);
  645. else
  646. reader->readBitmaskArtifacts(map->allowedArtifact, true);
  647. }
  648. // ban combo artifacts
  649. if(!features.levelSOD)
  650. {
  651. for(auto const & artifact : LIBRARY->arth->objects)
  652. if(artifact->isCombined())
  653. map->allowedArtifact.erase(artifact->getId());
  654. }
  655. if(!features.levelAB)
  656. {
  657. map->allowedArtifact.erase(ArtifactID::VIAL_OF_DRAGON_BLOOD);
  658. map->allowedArtifact.erase(ArtifactID::ARMAGEDDONS_BLADE);
  659. }
  660. // Messy, but needed
  661. for(TriggeredEvent & event : map->triggeredEvents)
  662. {
  663. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  664. {
  665. if(cond.condition == EventCondition::HAVE_ARTIFACT || cond.condition == EventCondition::TRANSPORT)
  666. {
  667. map->allowedArtifact.erase(cond.objectType.as<ArtifactID>());
  668. }
  669. return cond;
  670. };
  671. event.trigger = event.trigger.morph(patcher);
  672. }
  673. }
  674. void CMapLoaderH3M::readAllowedSpellsAbilities()
  675. {
  676. map->allowedSpells = LIBRARY->spellh->getDefaultAllowed();
  677. map->allowedAbilities = LIBRARY->skillh->getDefaultAllowed();
  678. if(features.levelSOD)
  679. {
  680. reader->readBitmaskSpells(map->allowedSpells, true);
  681. reader->readBitmaskSkills(map->allowedAbilities, true);
  682. }
  683. }
  684. void CMapLoaderH3M::readRumors()
  685. {
  686. size_t rumorsCount = reader->readUInt32();
  687. assert(rumorsCount < 1000); // sanity check
  688. for(size_t it = 0; it < rumorsCount; it++)
  689. {
  690. Rumor ourRumor;
  691. ourRumor.name = readBasicString();
  692. ourRumor.text.appendTextID(readLocalizedString(TextIdentifier("header", "rumor", it, "text")));
  693. map->rumors.push_back(ourRumor);
  694. }
  695. }
  696. void CMapLoaderH3M::readPredefinedHeroes()
  697. {
  698. if(!features.levelSOD)
  699. return;
  700. uint32_t heroesCount = features.heroesCount;
  701. if(features.levelHOTA0)
  702. heroesCount = reader->readUInt32();
  703. assert(heroesCount <= features.heroesCount);
  704. for(int heroID = 0; heroID < heroesCount; heroID++)
  705. {
  706. bool custom = reader->readBool();
  707. if(!custom)
  708. continue;
  709. auto hero = std::make_shared<CGHeroInstance>(map->cb);
  710. hero->ID = Obj::HERO;
  711. hero->subID = heroID;
  712. bool hasExp = reader->readBool();
  713. if(hasExp)
  714. {
  715. hero->exp = reader->readUInt32();
  716. }
  717. else
  718. {
  719. hero->exp = 0;
  720. }
  721. bool hasSecSkills = reader->readBool();
  722. if(hasSecSkills)
  723. {
  724. uint32_t howMany = reader->readUInt32();
  725. hero->secSkills.resize(howMany);
  726. for(int yy = 0; yy < howMany; ++yy)
  727. {
  728. hero->secSkills[yy].first = reader->readSkill();
  729. hero->secSkills[yy].second = reader->readInt8Checked(1,3);
  730. }
  731. }
  732. loadArtifactsOfHero(hero.get());
  733. bool hasCustomBio = reader->readBool();
  734. if(hasCustomBio)
  735. hero->biographyCustomTextId = readLocalizedString(TextIdentifier("heroes", heroID, "biography"));
  736. // 0xFF is default, 00 male, 01 female
  737. hero->gender = static_cast<EHeroGender>(reader->readInt8Checked(-1, 1));
  738. assert(hero->gender == EHeroGender::MALE || hero->gender == EHeroGender::FEMALE || hero->gender == EHeroGender::DEFAULT);
  739. bool hasCustomSpells = reader->readBool();
  740. if(hasCustomSpells)
  741. reader->readBitmaskSpells(hero->spells, false);
  742. bool hasCustomPrimSkills = reader->readBool();
  743. if(hasCustomPrimSkills)
  744. {
  745. for(int skillID = 0; skillID < GameConstants::PRIMARY_SKILLS; skillID++)
  746. {
  747. hero->pushPrimSkill(static_cast<PrimarySkill>(skillID), reader->readUInt8());
  748. }
  749. }
  750. map->addToHeroPool(hero);
  751. logGlobal->debug("Map '%s': Hero predefined in map: %s", mapName, hero->getHeroType()->getJsonKey());
  752. }
  753. if(features.levelHOTA5)
  754. {
  755. for(int heroID = 0; heroID < heroesCount; heroID++)
  756. {
  757. bool alwaysAddSkills = reader->readBool(); // prevent heroes from receiving additional random secondary skills at the start of the map if they are not of the first level
  758. bool cannotGainXP = reader->readBool();
  759. int32_t level = reader->readInt32(); // Needs investigation how this interacts with usual setting of level via experience
  760. assert(level > 0);
  761. if (!alwaysAddSkills)
  762. logGlobal->warn("Map '%s': Option to prevent hero %d from gaining skills on map start is not implemented!", mapName, heroID);
  763. if (cannotGainXP)
  764. logGlobal->warn("Map '%s': Option to prevent hero %d from receiveing experience is not implemented!", mapName, heroID);
  765. if (level > 1)
  766. logGlobal->warn("Map '%s': Option to set level of hero %d to %d is not implemented!", mapName, heroID, level);
  767. }
  768. }
  769. }
  770. void CMapLoaderH3M::loadArtifactsOfHero(CGHeroInstance * hero)
  771. {
  772. bool hasArtSet = reader->readBool();
  773. // True if artifact set is not default (hero has some artifacts)
  774. if(!hasArtSet)
  775. return;
  776. // Workaround - if hero has customized artifacts game should not attempt to add spellbook based on hero type
  777. hero->spells.insert(SpellID::SPELLBOOK_PRESET);
  778. if(!hero->artifactsWorn.empty() || !hero->artifactsInBackpack.empty())
  779. {
  780. logGlobal->debug("Hero %d at %s has set artifacts twice (in map properties and on adventure map instance). Using the latter set...", hero->getHeroTypeID().getNum(), hero->anchorPos().toString());
  781. hero->artifactsInBackpack.clear();
  782. while(!hero->artifactsWorn.empty())
  783. hero->removeArtifact(hero->artifactsWorn.begin()->first);
  784. }
  785. for(int i = 0; i < features.artifactSlotsCount; i++)
  786. loadArtifactToSlot(hero, i);
  787. // bag artifacts
  788. // number of artifacts in hero's bag
  789. size_t amount = reader->readUInt16();
  790. for(size_t i = 0; i < amount; ++i)
  791. {
  792. loadArtifactToSlot(hero, ArtifactPosition::BACKPACK_START + static_cast<int>(hero->artifactsInBackpack.size()));
  793. }
  794. }
  795. bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
  796. {
  797. ArtifactID artifactID = reader->readArtifact();
  798. SpellID scrollSpell = SpellID::NONE;
  799. if (features.levelHOTA5)
  800. scrollSpell = reader->readSpell16();
  801. if(artifactID == ArtifactID::NONE)
  802. return false;
  803. const Artifact * art = artifactID.toEntity(LIBRARY);
  804. if(!art)
  805. {
  806. logGlobal->warn("Map '%s': Invalid artifact in hero's backpack, ignoring...", mapName);
  807. return false;
  808. }
  809. if(art->isBig() && slot >= ArtifactPosition::BACKPACK_START)
  810. {
  811. logGlobal->warn("Map '%s': A big artifact (war machine) in hero's backpack, ignoring...", mapName);
  812. return false;
  813. }
  814. // H3 bug workaround - Enemy hero on 3rd scenario of Good1.h3c campaign ("Long Live The Queen")
  815. // He has Shackles of War (normally - MISC slot artifact) in LEFT_HAND slot set in editor
  816. // Artifact seems to be missing in game, so skip artifacts that don't fit target slot
  817. if(ArtifactID(artifactID).toArtifact()->canBePutAt(hero, ArtifactPosition(slot)))
  818. {
  819. auto * artifact = map->createArtifact(artifactID, scrollSpell);
  820. map->putArtifactInstance(*hero, artifact->getId(), slot);
  821. }
  822. else
  823. {
  824. logGlobal->warn("Map '%s': Artifact '%s' can't be put at the slot %d", mapName, ArtifactID(artifactID).toArtifact()->getNameTranslated(), slot);
  825. return false;
  826. }
  827. return true;
  828. }
  829. void CMapLoaderH3M::readTerrain()
  830. {
  831. map->initTerrain();
  832. // Read terrain
  833. int3 pos;
  834. for(pos.z = 0; pos.z < map->levels(); ++pos.z)
  835. {
  836. //OH3 format is [z][y][x]
  837. for(pos.y = 0; pos.y < map->height; pos.y++)
  838. {
  839. for(pos.x = 0; pos.x < map->width; pos.x++)
  840. {
  841. auto & tile = map->getTile(pos);
  842. tile.terrainType = reader->readTerrain();
  843. tile.terView = reader->readUInt8();
  844. tile.riverType = reader->readRiver();
  845. tile.riverDir = reader->readUInt8();
  846. tile.roadType = reader->readRoad();
  847. tile.roadDir = reader->readUInt8();
  848. tile.extTileFlags = reader->readUInt8();
  849. }
  850. }
  851. }
  852. map->calculateWaterContent();
  853. }
  854. void CMapLoaderH3M::readObjectTemplates()
  855. {
  856. uint32_t defAmount = reader->readUInt32();
  857. originalTemplates.reserve(defAmount);
  858. remappedTemplates.reserve(defAmount);
  859. // Read custom defs
  860. for(int defID = 0; defID < defAmount; ++defID)
  861. {
  862. auto tmpl = reader->readObjectTemplate();
  863. originalTemplates.push_back(tmpl);
  864. auto remapped = std::make_shared<ObjectTemplate>(*tmpl);
  865. reader->remapTemplate(*remapped);
  866. remappedTemplates.push_back(remapped);
  867. if (!CResourceHandler::get()->existsResource(remapped->animationFile.addPrefix("SPRITES/")))
  868. logMod->warn("Template animation %s of type (%d %d) is missing!", remapped->animationFile.getOriginalName(), remapped->id, remapped->subid );
  869. }
  870. }
  871. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readEvent(const int3 & mapPosition, const ObjectInstanceID & idToBeGiven)
  872. {
  873. auto object = std::make_shared<CGEvent>(map->cb);
  874. readBoxContent(object.get(), mapPosition, idToBeGiven);
  875. reader->readBitmaskPlayers(object->availableFor, false);
  876. object->computerActivate = reader->readBool();
  877. object->removeAfterVisit = reader->readBool();
  878. reader->skipZero(4);
  879. if(features.levelHOTA3)
  880. object->humanActivate = reader->readBool();
  881. else
  882. object->humanActivate = true;
  883. readBoxHotaContent(object.get(), mapPosition, idToBeGiven);
  884. return object;
  885. }
  886. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readPandora(const int3 & mapPosition, const ObjectInstanceID & idToBeGiven)
  887. {
  888. auto object = std::make_shared<CGPandoraBox>(map->cb);
  889. readBoxContent(object.get(), mapPosition, idToBeGiven);
  890. if(features.levelHOTA5)
  891. reader->skipZero(1); // Unknown value, always 0 so far
  892. readBoxHotaContent(object.get(), mapPosition, idToBeGiven);
  893. return object;
  894. }
  895. void CMapLoaderH3M::readBoxContent(CGPandoraBox * object, const int3 & mapPosition, const ObjectInstanceID & idToBeGiven)
  896. {
  897. readMessageAndGuards(object->message, object, mapPosition, idToBeGiven);
  898. Rewardable::VisitInfo vinfo;
  899. auto & reward = vinfo.reward;
  900. reward.heroExperience = reader->readUInt32();
  901. reward.manaDiff = reader->readInt32();
  902. if(auto val = reader->readInt8Checked(-3, 3))
  903. reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(idToBeGiven));
  904. if(auto val = reader->readInt8Checked(-3, 3))
  905. reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(idToBeGiven));
  906. reader->readResources(reward.resources);
  907. for(int x = 0; x < GameConstants::PRIMARY_SKILLS; ++x)
  908. reward.primary.at(x) = reader->readUInt8();
  909. size_t gabn = reader->readUInt8(); //number of gained abilities
  910. for(size_t oo = 0; oo < gabn; ++oo)
  911. {
  912. auto rId = reader->readSkill();
  913. auto rVal = reader->readInt8Checked(1,3);
  914. reward.secondary[rId] = rVal;
  915. }
  916. size_t gart = reader->readUInt8(); //number of gained artifacts
  917. for(size_t oo = 0; oo < gart; ++oo)
  918. {
  919. ArtifactID grantedArtifact = reader->readArtifact();
  920. if (features.levelHOTA5)
  921. {
  922. SpellID scrollSpell = reader->readSpell16();
  923. if (grantedArtifact == ArtifactID::SPELL_SCROLL)
  924. reward.grantedScrolls.push_back(scrollSpell);
  925. }
  926. else
  927. reward.grantedArtifacts.push_back(grantedArtifact);
  928. }
  929. size_t gspel = reader->readUInt8(); //number of gained spells
  930. for(size_t oo = 0; oo < gspel; ++oo)
  931. reward.spells.push_back(reader->readSpell());
  932. size_t gcre = reader->readUInt8(); //number of gained creatures
  933. for(size_t oo = 0; oo < gcre; ++oo)
  934. {
  935. auto rId = reader->readCreature();
  936. auto rVal = reader->readUInt16();
  937. reward.creatures.emplace_back(rId, rVal);
  938. }
  939. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  940. object->configuration.info.push_back(vinfo);
  941. reader->skipZero(8);
  942. }
  943. void CMapLoaderH3M::readBoxHotaContent(CGPandoraBox * object, const int3 & mapPosition, const ObjectInstanceID & idToBeGiven)
  944. {
  945. if(features.levelHOTA5)
  946. {
  947. int32_t movementMode = reader->readInt32(); // Give, Take, Nullify, Set, Replenish
  948. int32_t movementAmount = reader->readInt32();
  949. assert(movementMode >= 0 && movementMode <= 4);
  950. if (movementMode != 0 || movementAmount != 0)
  951. logGlobal->warn("Map '%s': Event/Pandora %s option to modify (mode %d) movement points by %d is not implemented!", mapName, mapPosition.toString(), movementMode, movementAmount);
  952. }
  953. if(features.levelHOTA6)
  954. {
  955. int32_t allowedDifficultiesMask = reader->readInt32();
  956. assert(allowedDifficultiesMask > 0 && allowedDifficultiesMask < 32);
  957. if (allowedDifficultiesMask != 31)
  958. logGlobal->warn("Map '%s': Event/Pandora %s availability by difficulty (mode %d) is not implemented!", mapName, mapPosition.toString(), allowedDifficultiesMask);
  959. }
  960. }
  961. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readMonster(const int3 & mapPosition, const ObjectInstanceID & objectInstanceID)
  962. {
  963. auto object = std::make_shared<CGCreature>(map->cb);
  964. object->id = objectInstanceID;
  965. if(features.levelAB)
  966. {
  967. object->identifier = reader->readUInt32();
  968. questIdentifierToId[object->identifier] = objectInstanceID;
  969. }
  970. auto hlp = std::make_unique<CStackInstance>(map->cb);
  971. hlp->setCount(reader->readUInt16());
  972. //type will be set during initialization
  973. object->putStack(SlotID(0), std::move(hlp));
  974. //TODO: 0-4 is h3 range. 5 is hota extension for exact aggression?
  975. object->character = reader->readInt8Checked(0, 5);
  976. bool hasMessage = reader->readBool();
  977. if(hasMessage)
  978. {
  979. object->message.appendTextID(readLocalizedString(TextIdentifier("monster", mapPosition.x, mapPosition.y, mapPosition.z, "message")));
  980. reader->readResources(object->resources);
  981. object->gainedArtifact = reader->readArtifact();
  982. }
  983. object->neverFlees = reader->readBool();
  984. object->notGrowingTeam = reader->readBool();
  985. reader->skipZero(2);
  986. if(features.levelHOTA3)
  987. {
  988. //TODO: HotA
  989. int32_t aggressionExact = reader->readInt32(); // -1 = default, 1-10 = possible values range
  990. bool joinOnlyForMoney = reader->readBool(); // if true, monsters will only join for money
  991. int32_t joinPercent = reader->readInt32(); // 100 = default, percent of monsters that will join on successful aggression check
  992. int32_t upgradedStack = reader->readInt32(); // Presence of upgraded stack, -1 = random, 0 = never, 1 = always
  993. int32_t stacksCount = reader->readInt32(); // TODO: check possible values. How many creature stacks will be present on battlefield, -1 = default
  994. if(aggressionExact != -1 || joinOnlyForMoney || joinPercent != 100 || upgradedStack != -1 || stacksCount != -1)
  995. logGlobal->warn(
  996. "Map '%s': Wandering monsters %s settings %d %d %d %d %d are not implemented!",
  997. mapName,
  998. mapPosition.toString(),
  999. aggressionExact,
  1000. int(joinOnlyForMoney),
  1001. joinPercent,
  1002. upgradedStack,
  1003. stacksCount
  1004. );
  1005. }
  1006. if (features.levelHOTA5)
  1007. {
  1008. bool sizeByValue = reader->readBool();//FIXME: double-check this flag effect
  1009. int32_t targetValue = reader->readInt32();
  1010. if (sizeByValue || targetValue)
  1011. logGlobal->warn( "Map '%s': Wandering monsters %s option to set unit size to %d (%d) AI value is not implemented!", mapName, mapPosition.toString(), targetValue, sizeByValue);
  1012. }
  1013. return object;
  1014. }
  1015. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readSign(const int3 & mapPosition)
  1016. {
  1017. auto object = std::make_shared<CGSignBottle>(map->cb);
  1018. object->message.appendTextID(readLocalizedString(TextIdentifier("sign", mapPosition.x, mapPosition.y, mapPosition.z, "message")));
  1019. reader->skipZero(4);
  1020. return object;
  1021. }
  1022. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readWitchHut(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1023. {
  1024. auto object = readGeneric(position, objectTemplate);
  1025. auto rewardable = std::dynamic_pointer_cast<CRewardableObject>(object);
  1026. // AB and later maps have allowed abilities defined in H3M
  1027. if(features.levelAB)
  1028. {
  1029. std::set<SecondarySkill> allowedAbilities;
  1030. reader->readBitmaskSkills(allowedAbilities, false);
  1031. if (rewardable)
  1032. {
  1033. if(allowedAbilities.size() != 1)
  1034. {
  1035. auto defaultAllowed = LIBRARY->skillh->getDefaultAllowed();
  1036. for(int skillID = features.skillsCount; skillID < defaultAllowed.size(); ++skillID)
  1037. if(defaultAllowed.count(skillID))
  1038. allowedAbilities.insert(SecondarySkill(skillID));
  1039. }
  1040. JsonNode variable;
  1041. if (allowedAbilities.size() == 1)
  1042. {
  1043. variable.String() = LIBRARY->skills()->getById(*allowedAbilities.begin())->getJsonKey();
  1044. }
  1045. else
  1046. {
  1047. JsonVector anyOfList;
  1048. for (auto const & skill : allowedAbilities)
  1049. {
  1050. JsonNode entry;
  1051. entry.String() = LIBRARY->skills()->getById(skill)->getJsonKey();
  1052. anyOfList.push_back(entry);
  1053. }
  1054. variable["anyOf"].Vector() = anyOfList;
  1055. }
  1056. variable.setModScope(ModScope::scopeGame()); // list may include skills from all mods
  1057. rewardable->configuration.presetVariable("secondarySkill", "gainedSkill", variable);
  1058. }
  1059. else
  1060. {
  1061. logGlobal->warn("Failed to set allowed secondary skills to a Witch Hut! Object is not rewardable!");
  1062. }
  1063. }
  1064. return object;
  1065. }
  1066. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readScholar(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1067. {
  1068. enum class ScholarBonusType : int8_t {
  1069. RANDOM = -1,
  1070. PRIM_SKILL = 0,
  1071. SECONDARY_SKILL = 1,
  1072. SPELL = 2,
  1073. };
  1074. auto object = readGeneric(position, objectTemplate);
  1075. auto rewardable = std::dynamic_pointer_cast<CRewardableObject>(object);
  1076. uint8_t bonusTypeRaw = reader->readInt8Checked(-1, 2);
  1077. auto bonusType = static_cast<ScholarBonusType>(bonusTypeRaw);
  1078. auto bonusID = reader->readUInt8();
  1079. if (rewardable)
  1080. {
  1081. switch (bonusType)
  1082. {
  1083. case ScholarBonusType::PRIM_SKILL:
  1084. {
  1085. JsonNode variable;
  1086. JsonNode dice;
  1087. variable.String() = NPrimarySkill::names[bonusID];
  1088. variable.setModScope(ModScope::scopeGame());
  1089. dice.Integer() = 80;
  1090. rewardable->configuration.presetVariable("primarySkill", "gainedStat", variable);
  1091. rewardable->configuration.presetVariable("dice", "0", dice);
  1092. break;
  1093. }
  1094. case ScholarBonusType::SECONDARY_SKILL:
  1095. {
  1096. JsonNode variable;
  1097. JsonNode dice;
  1098. variable.String() = LIBRARY->skills()->getByIndex(bonusID)->getJsonKey();
  1099. variable.setModScope(ModScope::scopeGame());
  1100. dice.Integer() = 50;
  1101. rewardable->configuration.presetVariable("secondarySkill", "gainedSkill", variable);
  1102. rewardable->configuration.presetVariable("dice", "0", dice);
  1103. break;
  1104. }
  1105. case ScholarBonusType::SPELL:
  1106. {
  1107. JsonNode variable;
  1108. JsonNode dice;
  1109. variable.String() = LIBRARY->spells()->getByIndex(bonusID)->getJsonKey();
  1110. variable.setModScope(ModScope::scopeGame());
  1111. dice.Integer() = 20;
  1112. rewardable->configuration.presetVariable("spell", "gainedSpell", variable);
  1113. rewardable->configuration.presetVariable("dice", "0", dice);
  1114. break;
  1115. }
  1116. case ScholarBonusType::RANDOM:
  1117. break;// No-op
  1118. default:
  1119. logGlobal->warn("Map '%s': Invalid Scholar settings! Ignoring...", mapName);
  1120. }
  1121. }
  1122. else
  1123. {
  1124. logGlobal->warn("Failed to set reward parameters for a Scholar! Object is not rewardable!");
  1125. }
  1126. reader->skipZero(6);
  1127. return object;
  1128. }
  1129. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readGarrison(const int3 & mapPosition, const ObjectInstanceID & idToBeGiven)
  1130. {
  1131. auto object = std::make_shared<CGGarrison>(map->cb);
  1132. setOwnerAndValidate(mapPosition, object.get(), reader->readPlayer32());
  1133. readCreatureSet(object.get(), idToBeGiven);
  1134. if(features.levelAB)
  1135. object->removableUnits = reader->readBool();
  1136. else
  1137. object->removableUnits = true;
  1138. reader->skipZero(8);
  1139. return object;
  1140. }
  1141. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readArtifact(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate, const ObjectInstanceID & idToBeGiven)
  1142. {
  1143. ArtifactID artID = ArtifactID::NONE; //random, set later
  1144. auto object = std::make_shared<CGArtifact>(map->cb);
  1145. readMessageAndGuards(object->message, object.get(), mapPosition, idToBeGiven);
  1146. //specific artifact
  1147. if(objectTemplate->id == Obj::ARTIFACT)
  1148. artID = ArtifactID(objectTemplate->subid);
  1149. if(features.levelHOTA5)
  1150. {
  1151. uint32_t pickupMode = reader->readUInt32();
  1152. uint8_t pickupFlags = reader->readUInt8();
  1153. assert(pickupMode == 0 || pickupMode == 1 || pickupMode == 2); // DISABLED, RANDOM, CUSTOM
  1154. if (pickupMode != 0)
  1155. logGlobal->warn("Map '%s': Artifact %s: not implemented pickup mode %d (flags: %d)", mapName, mapPosition.toString(), pickupMode, static_cast<int>(pickupFlags));
  1156. }
  1157. if (artID.hasValue())
  1158. object->setArtifactInstance(map->createArtifact(artID, SpellID::NONE));
  1159. // else - random, will be initialized later
  1160. return object;
  1161. }
  1162. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readScroll(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate, const ObjectInstanceID & idToBeGiven)
  1163. {
  1164. auto object = std::make_shared<CGArtifact>(map->cb);
  1165. readMessageAndGuards(object->message, object.get(), mapPosition, idToBeGiven);
  1166. SpellID spellID = reader->readSpell32();
  1167. object->setArtifactInstance(map->createArtifact(ArtifactID::SPELL_SCROLL, spellID.getNum()));
  1168. return object;
  1169. }
  1170. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readResource(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate, const ObjectInstanceID & idToBeGiven)
  1171. {
  1172. auto object = std::make_shared<CGResource>(map->cb);
  1173. readMessageAndGuards(object->message, object.get(), mapPosition, idToBeGiven);
  1174. object->amount = reader->readUInt32();
  1175. if (objectTemplate->id != Obj::RANDOM_RESOURCE)
  1176. {
  1177. const auto & baseHandler = LIBRARY->objtypeh->getHandlerFor(objectTemplate->id, objectTemplate->subid);
  1178. const auto & ourHandler = std::dynamic_pointer_cast<ResourceInstanceConstructor>(baseHandler);
  1179. object->amount *= ourHandler->getAmountMultiplier();
  1180. }
  1181. reader->skipZero(4);
  1182. return object;
  1183. }
  1184. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readMine(const int3 & mapPosition)
  1185. {
  1186. auto object = std::make_shared<CGMine>(map->cb);
  1187. setOwnerAndValidate(mapPosition, object.get(), reader->readPlayer32());
  1188. return object;
  1189. }
  1190. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readAbandonedMine(const int3 & mapPosition)
  1191. {
  1192. auto object = std::make_shared<CGMine>(map->cb);
  1193. object->setOwner(PlayerColor::NEUTRAL);
  1194. reader->readBitmaskResources(object->abandonedMineResources, false);
  1195. if(features.levelHOTA5)
  1196. {
  1197. bool customGuards = reader->readBool();
  1198. CreatureID guardsUnit = reader->readCreature32();
  1199. int32_t guardsMin = reader->readInt32();
  1200. int32_t guardsMax = reader->readInt32();
  1201. if (customGuards)
  1202. {
  1203. assert(guardsMin <= guardsMax);
  1204. assert(guardsUnit.hasValue());
  1205. logGlobal->warn("Map '%s': Abandoned Mine %s: not implemented guards of %d-%d %s", mapName, mapPosition.toString(), guardsMin, guardsMax, guardsUnit.toEntity(LIBRARY)->getJsonKey());
  1206. }
  1207. }
  1208. return object;
  1209. }
  1210. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readDwelling(const int3 & position)
  1211. {
  1212. auto object = std::make_shared<CGDwelling>(map->cb);
  1213. setOwnerAndValidate(position, object.get(), reader->readPlayer32());
  1214. return object;
  1215. }
  1216. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readDwellingRandom(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1217. {
  1218. auto object = std::make_shared<CGDwelling>(map->cb);
  1219. setOwnerAndValidate(mapPosition, object.get(), reader->readPlayer32());
  1220. object->randomizationInfo = CGDwellingRandomizationInfo();
  1221. bool hasFactionInfo = objectTemplate->id == Obj::RANDOM_DWELLING || objectTemplate->id == Obj::RANDOM_DWELLING_LVL;
  1222. bool hasLevelInfo = objectTemplate->id == Obj::RANDOM_DWELLING || objectTemplate->id == Obj::RANDOM_DWELLING_FACTION;
  1223. if (hasFactionInfo)
  1224. {
  1225. object->randomizationInfo->identifier = reader->readUInt32();
  1226. if(object->randomizationInfo->identifier == 0)
  1227. reader->readBitmaskFactions(object->randomizationInfo->allowedFactions, false);
  1228. }
  1229. else
  1230. object->randomizationInfo->allowedFactions.insert(FactionID(objectTemplate->subid));
  1231. if(hasLevelInfo)
  1232. {
  1233. object->randomizationInfo->minLevel = std::max(reader->readUInt8(), static_cast<ui8>(0)) + 1;
  1234. object->randomizationInfo->maxLevel = std::min(reader->readUInt8(), static_cast<ui8>(6)) + 1;
  1235. }
  1236. else
  1237. {
  1238. object->randomizationInfo->minLevel = objectTemplate->subid;
  1239. object->randomizationInfo->maxLevel = objectTemplate->subid;
  1240. }
  1241. return object;
  1242. }
  1243. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readShrine(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1244. {
  1245. auto object = readGeneric(position, objectTemplate);
  1246. auto rewardable = std::dynamic_pointer_cast<CRewardableObject>(object);
  1247. SpellID spell = reader->readSpell32();
  1248. if (rewardable)
  1249. {
  1250. if(spell != SpellID::NONE)
  1251. {
  1252. JsonNode variable;
  1253. variable.String() = LIBRARY->spells()->getById(spell)->getJsonKey();
  1254. variable.setModScope(ModScope::scopeGame()); // list may include spells from all mods
  1255. rewardable->configuration.presetVariable("spell", "gainedSpell", variable);
  1256. }
  1257. }
  1258. else
  1259. {
  1260. logGlobal->warn("Failed to set selected spell to a Shrine!. Object is not rewardable!");
  1261. }
  1262. return object;
  1263. }
  1264. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readHeroPlaceholder(const int3 & mapPosition)
  1265. {
  1266. auto object = std::make_shared<CGHeroPlaceholder>(map->cb);
  1267. setOwnerAndValidate(mapPosition, object.get(), reader->readPlayer());
  1268. HeroTypeID htid = reader->readHero(); //hero type id
  1269. if(htid.getNum() == -1)
  1270. {
  1271. object->powerRank = reader->readUInt8();
  1272. logGlobal->debug("Map '%s': Hero placeholder: by power at %s, owned by %s", mapName, mapPosition.toString(), object->getOwner().toString());
  1273. }
  1274. else
  1275. {
  1276. object->heroType = htid;
  1277. logGlobal->debug("Map '%s': Hero placeholder: %s at %s, owned by %s", mapName, LIBRARY->heroh->getById(htid)->getJsonKey(), mapPosition.toString(), object->getOwner().toString());
  1278. }
  1279. if(features.levelHOTA5)
  1280. {
  1281. bool customizedStatingUnits = reader->readBool();
  1282. if (customizedStatingUnits)
  1283. logGlobal->warn("Map '%s': Hero placeholder: not implemented option to customize starting units", mapName);
  1284. for (int i = 0; i < 7; ++i)
  1285. {
  1286. int32_t unitAmount = reader->readInt32();
  1287. CreatureID unitToGive = reader->readCreature32();
  1288. if (unitToGive.hasValue())
  1289. logGlobal->warn("Map '%s': Hero placeholder: not implemented option to give %d units of type %d on map start to slot %d is not implemented!", mapName, unitAmount, unitToGive.toEntity(LIBRARY)->getJsonKey(), i);
  1290. }
  1291. int32_t artifactsToGive = reader->readInt32();
  1292. assert(artifactsToGive >= 0);
  1293. assert(artifactsToGive < 100); // technically legal, but not possible in h3
  1294. for (int i = 0; i < artifactsToGive; ++i)
  1295. {
  1296. // NOTE: this might actually be 2 bytes for artifact ID + 2 bytes for spell scroll
  1297. ArtifactID startingArtifact = reader->readArtifact32();
  1298. logGlobal->warn("Map '%s': Hero placeholder: not implemented option to give hero artifact %d", mapName, startingArtifact.toEntity(LIBRARY)->getJsonKey());
  1299. }
  1300. }
  1301. return object;
  1302. }
  1303. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readGrail(const int3 & mapPosition)
  1304. {
  1305. map->grailPos = mapPosition;
  1306. map->grailRadius = reader->readInt32();
  1307. return nullptr;
  1308. }
  1309. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readHotaBattleLocation(const int3 & mapPosition)
  1310. {
  1311. // Battle location for arena mode in HotA
  1312. logGlobal->warn("Map '%s': Arena mode is not supported!", mapName);
  1313. return nullptr;
  1314. }
  1315. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readGeneric(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1316. {
  1317. if(LIBRARY->objtypeh->knownSubObjects(objectTemplate->id).count(objectTemplate->subid))
  1318. return LIBRARY->objtypeh->getHandlerFor(objectTemplate->id, objectTemplate->subid)->create(map->cb, objectTemplate);
  1319. logGlobal->warn("Map '%s': Unrecognized object %d:%d ('%s') at %s found!", mapName, objectTemplate->id.toEnum(), objectTemplate->subid, objectTemplate->animationFile.getOriginalName(), mapPosition.toString());
  1320. return std::make_shared<CGObjectInstance>(map->cb);
  1321. }
  1322. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readQuestGuard(const int3 & mapPosition)
  1323. {
  1324. auto guard = std::make_shared<CGQuestGuard>(map->cb);
  1325. readQuest(guard.get(), mapPosition);
  1326. return guard;
  1327. }
  1328. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readShipyard(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1329. {
  1330. auto object = readGeneric(mapPosition, objectTemplate);
  1331. setOwnerAndValidate(mapPosition, object.get(), reader->readPlayer32());
  1332. return object;
  1333. }
  1334. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readLighthouse(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1335. {
  1336. auto object = readGeneric(mapPosition, objectTemplate);
  1337. setOwnerAndValidate(mapPosition, object.get(), reader->readPlayer32());
  1338. return object;
  1339. }
  1340. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readBank(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1341. {
  1342. if(features.levelHOTA3)
  1343. {
  1344. //TODO: HotA
  1345. // index of guards preset. -1 = random, 0-4 = index of possible guards settings
  1346. int32_t guardsPresetIndex = reader->readInt32();
  1347. // presence of upgraded stack: -1 = random, 0 = never, 1 = always
  1348. int8_t upgradedStackPresence = reader->readInt8Checked(-1, 1);
  1349. assert(vstd::iswithin(guardsPresetIndex, -1, 4));
  1350. assert(vstd::iswithin(upgradedStackPresence, -1, 1));
  1351. // list of possible artifacts in reward
  1352. // - if list is empty, artifacts are either not present in reward or random
  1353. // - if non-empty, then list always have same number of elements as number of artifacts in bank
  1354. // - ArtifactID::NONE indictates random artifact, other values indicate artifact that should be used as reward
  1355. std::vector<ArtifactID> artifacts;
  1356. int artNumber = reader->readUInt32();
  1357. for(int yy = 0; yy < artNumber; ++yy)
  1358. {
  1359. artifacts.push_back(reader->readArtifact32());
  1360. }
  1361. if(guardsPresetIndex != -1 || upgradedStackPresence != -1 || !artifacts.empty())
  1362. logGlobal->warn(
  1363. "Map '%s': creature bank at %s settings %d %d %d are not implemented!",
  1364. mapName,
  1365. mapPosition.toString(),
  1366. guardsPresetIndex,
  1367. int(upgradedStackPresence),
  1368. artifacts.size()
  1369. );
  1370. }
  1371. return readGeneric(mapPosition, objectTemplate);
  1372. }
  1373. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readRewardWithArtifact(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1374. {
  1375. if(features.levelHOTA5)
  1376. {
  1377. // TREASURE_CHEST - rewards index, if 3 - then 2nd value is artifact
  1378. // CORPSE - rewards index, if 1 - then 2nd value is artifact
  1379. // SHIPWRECK_SURVIVOR - if content = 0 then 2nd value is granted artifact
  1380. // SEA_CHEST - rewards index, if 2 - then 2nd value is artifact
  1381. // FLOTSAM - rewards index (0-3) + -1
  1382. // TREE_OF_KNOWLEDGE - rewards index (0-2) + -1
  1383. // PYRAMID - if content = 0 then 2nd value is granted spell
  1384. // WARRIORS_TOMB - if content = 0 then 2nd value is granted artifact
  1385. // HOTA_JETSAM - rewards index (0-3) + -1
  1386. // HOTA_VIAL_OF_MANA - rewards index (0-3) + -1
  1387. int32_t content = reader->readInt32();
  1388. ArtifactID artifact;
  1389. if (content != -1)
  1390. {
  1391. artifact = reader->readArtifact32(); // NOTE: might be 2 byte artifact + 2 bytes scroll spell
  1392. logGlobal->warn("Map '%s': Object (%d) %s settings %d %d are not implemented!", mapName, objectTemplate->id, mapPosition.toString(), content, artifact);
  1393. }
  1394. else
  1395. reader->skipUnused(4); // garbage data, usually -1, but sometimes uninitialized
  1396. }
  1397. return readGeneric(mapPosition, objectTemplate);
  1398. }
  1399. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readBlackMarket(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1400. {
  1401. if(features.levelHOTA5)
  1402. {
  1403. for (int i = 0; i < 7; ++i)
  1404. {
  1405. ArtifactID artifact = reader->readArtifact();
  1406. SpellID spellID = reader->readSpell16();
  1407. if (artifact.hasValue())
  1408. {
  1409. if (artifact != ArtifactID::SPELL_SCROLL)
  1410. logGlobal->warn("Map '%s': Black Market at %s: option to sell artifact %s is not implemented", mapName, mapPosition.toString(), artifact.toEntity(LIBRARY)->getJsonKey());
  1411. else
  1412. logGlobal->warn("Map '%s': Black Market at %s: option to sell scroll %s is not implemented", mapName, mapPosition.toString(), spellID.toEntity(LIBRARY)->getJsonKey());
  1413. }
  1414. }
  1415. }
  1416. return readGeneric(mapPosition, objectTemplate);
  1417. }
  1418. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readUniversity(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1419. {
  1420. if(features.levelHOTA5)
  1421. {
  1422. int32_t customized = reader->readInt32();
  1423. std::set<SecondarySkill> allowedSkills;
  1424. reader->readBitmaskSkills(allowedSkills, false);
  1425. // NOTE: check how this interacts with hota Seafaring Academy that is guaranteed to give Navigation
  1426. assert(customized == -1 || customized == 0);
  1427. if (customized != -1)
  1428. logGlobal->warn("Map '%s': University at %s: option to give specific skills out of %d is not implemented", mapName, mapPosition.toString(), allowedSkills.size());
  1429. }
  1430. return readGeneric(mapPosition, objectTemplate);
  1431. }
  1432. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readRewardWithArtifactAndResources(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
  1433. {
  1434. if(features.levelHOTA5)
  1435. {
  1436. // Sea Barrel / 0 -> aID = -1, aA = resource amount, rA = resource type, aB = garbage, rB = 0
  1437. // Sea Barrel / 1 -> aID = -1, aA = 1, rA = 1, aB = garbage, rB = 0
  1438. // Ancient Lamp / 0 -> aID = -1, aA = amount to recruit, rA = 0, aB = 1, rB = 0
  1439. // Grave / 0 -> aID = artifact to give, aA = resource amount, rA = resource type, aB = 1, rB = garbage
  1440. // CAMPFIRE 12 / 0 -> aID = -1, aA = gold amount, rA = gold type, aB = resource amount, rB = resource type
  1441. // WAGON 105 / 0 -> aID = -1 or artifact, aA = resource amount, rA = resource type, aB = 1, rB = garbage?
  1442. // WAGON 105 / 1 -> empty / garbage?
  1443. // LEAN_TO / 39 / 0 -> aID = -1, aA = resource amount, rA = resource type, aB = garbage, rB = 0
  1444. int32_t content = reader->readInt32();
  1445. int32_t artifact = reader->readInt32();
  1446. int32_t amountA = reader->readInt32();
  1447. int8_t resourceA = reader->readInt8();
  1448. int32_t amountB = reader->readInt32();
  1449. int8_t resourceB = reader->readInt8();
  1450. if (content != -1)
  1451. logGlobal->warn("Map '%s': Object (%d) %s settings %d %d %d %d %d %d are not implemented!", mapName, objectTemplate->id, mapPosition.toString(), content, artifact, amountA, static_cast<int>(resourceA), amountB, static_cast<int>(resourceB));
  1452. }
  1453. return readGeneric(mapPosition, objectTemplate);
  1454. }
  1455. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readObject(MapObjectID id, MapObjectSubID subid, std::shared_ptr<const ObjectTemplate> objectTemplate, const int3 & mapPosition, const ObjectInstanceID & objectInstanceID)
  1456. {
  1457. switch(id.toEnum())
  1458. {
  1459. case Obj::EVENT:
  1460. return readEvent(mapPosition, objectInstanceID);
  1461. case Obj::HERO:
  1462. case Obj::RANDOM_HERO:
  1463. case Obj::PRISON:
  1464. return readHero(mapPosition, objectInstanceID);
  1465. case Obj::MONSTER:
  1466. case Obj::RANDOM_MONSTER:
  1467. case Obj::RANDOM_MONSTER_L1:
  1468. case Obj::RANDOM_MONSTER_L2:
  1469. case Obj::RANDOM_MONSTER_L3:
  1470. case Obj::RANDOM_MONSTER_L4:
  1471. case Obj::RANDOM_MONSTER_L5:
  1472. case Obj::RANDOM_MONSTER_L6:
  1473. case Obj::RANDOM_MONSTER_L7:
  1474. return readMonster(mapPosition, objectInstanceID);
  1475. case Obj::OCEAN_BOTTLE:
  1476. case Obj::SIGN:
  1477. return readSign(mapPosition);
  1478. case Obj::SEER_HUT:
  1479. return readSeerHut(mapPosition, objectInstanceID);
  1480. case Obj::WITCH_HUT:
  1481. return readWitchHut(mapPosition, objectTemplate);
  1482. case Obj::SCHOLAR:
  1483. return readScholar(mapPosition, objectTemplate);
  1484. case Obj::GARRISON:
  1485. case Obj::GARRISON2:
  1486. return readGarrison(mapPosition, objectInstanceID);
  1487. case Obj::ARTIFACT:
  1488. case Obj::RANDOM_ART:
  1489. case Obj::RANDOM_TREASURE_ART:
  1490. case Obj::RANDOM_MINOR_ART:
  1491. case Obj::RANDOM_MAJOR_ART:
  1492. case Obj::RANDOM_RELIC_ART:
  1493. return readArtifact(mapPosition, objectTemplate, objectInstanceID);
  1494. case Obj::SPELL_SCROLL:
  1495. return readScroll(mapPosition, objectTemplate, objectInstanceID);
  1496. case Obj::RANDOM_RESOURCE:
  1497. case Obj::RESOURCE:
  1498. return readResource(mapPosition, objectTemplate, objectInstanceID);
  1499. case Obj::RANDOM_TOWN:
  1500. case Obj::TOWN:
  1501. return readTown(mapPosition, objectTemplate, objectInstanceID);
  1502. case Obj::MINE:
  1503. case Obj::ABANDONED_MINE:
  1504. if (subid < 7)
  1505. return readMine(mapPosition);
  1506. else
  1507. return readAbandonedMine(mapPosition);
  1508. case Obj::CREATURE_GENERATOR1:
  1509. case Obj::CREATURE_GENERATOR2:
  1510. case Obj::CREATURE_GENERATOR3:
  1511. case Obj::CREATURE_GENERATOR4:
  1512. return readDwelling(mapPosition);
  1513. case Obj::SHRINE_OF_MAGIC_INCANTATION:
  1514. case Obj::SHRINE_OF_MAGIC_GESTURE:
  1515. case Obj::SHRINE_OF_MAGIC_THOUGHT:
  1516. return readShrine(mapPosition, objectTemplate);
  1517. case Obj::PANDORAS_BOX:
  1518. return readPandora(mapPosition, objectInstanceID);
  1519. case Obj::GRAIL:
  1520. if (subid < 1000)
  1521. return readGrail(mapPosition);
  1522. else
  1523. return readHotaBattleLocation(mapPosition);
  1524. case Obj::RANDOM_DWELLING:
  1525. case Obj::RANDOM_DWELLING_LVL:
  1526. case Obj::RANDOM_DWELLING_FACTION:
  1527. return readDwellingRandom(mapPosition, objectTemplate);
  1528. case Obj::QUEST_GUARD:
  1529. return readQuestGuard(mapPosition);
  1530. case Obj::SHIPYARD:
  1531. return readShipyard(mapPosition, objectTemplate);
  1532. case Obj::HERO_PLACEHOLDER:
  1533. return readHeroPlaceholder(mapPosition);
  1534. case Obj::LIGHTHOUSE:
  1535. return readLighthouse(mapPosition, objectTemplate);
  1536. case Obj::CREATURE_BANK:
  1537. case Obj::DERELICT_SHIP:
  1538. case Obj::DRAGON_UTOPIA:
  1539. case Obj::CRYPT:
  1540. case Obj::SHIPWRECK:
  1541. return readBank(mapPosition, objectTemplate);
  1542. case Obj::TREASURE_CHEST:
  1543. case Obj::CORPSE:
  1544. case Obj::SHIPWRECK_SURVIVOR:
  1545. case Obj::SEA_CHEST:
  1546. case Obj::FLOTSAM:
  1547. case Obj::TREE_OF_KNOWLEDGE:
  1548. case Obj::PYRAMID:
  1549. case Obj::WARRIORS_TOMB:
  1550. return readRewardWithArtifact(mapPosition, objectTemplate);
  1551. case Obj::CAMPFIRE:
  1552. case Obj::WAGON:
  1553. case Obj::LEAN_TO:
  1554. return readRewardWithArtifactAndResources(mapPosition, objectTemplate);
  1555. case Obj::BORDER_GATE:
  1556. if (subid == 1000) // HotA hacks - Quest Gate
  1557. return readQuestGuard(mapPosition);
  1558. if (subid == 1001) // HotA hacks - Grave
  1559. return readRewardWithArtifactAndResources(mapPosition, objectTemplate);
  1560. return readGeneric(mapPosition, objectTemplate);
  1561. case Obj::HOTA_CUSTOM_OBJECT_1:
  1562. // 0 -> Ancient Lamp
  1563. // 1 -> Sea Barrel
  1564. // 2 -> Jetsam
  1565. // 3 -> Vial of Mana
  1566. if (subid == 0 || subid == 1)
  1567. return readRewardWithArtifactAndResources(mapPosition, objectTemplate);
  1568. else
  1569. return readRewardWithArtifact(mapPosition, objectTemplate);
  1570. case Obj::HOTA_CUSTOM_OBJECT_2:
  1571. if (subid == 0) // Seafaring Academy
  1572. return readUniversity(mapPosition, objectTemplate);
  1573. else
  1574. return readGeneric(mapPosition, objectTemplate);
  1575. case Obj::BLACK_MARKET:
  1576. return readBlackMarket(mapPosition, objectTemplate);
  1577. case Obj::UNIVERSITY:
  1578. return readUniversity(mapPosition, objectTemplate);
  1579. default: //any other object
  1580. return readGeneric(mapPosition, objectTemplate);
  1581. }
  1582. }
  1583. void CMapLoaderH3M::readObjects()
  1584. {
  1585. uint32_t objectsCount = reader->readUInt32();
  1586. for(uint32_t i = 0; i < objectsCount; ++i)
  1587. {
  1588. int3 mapPosition = reader->readInt3();
  1589. assert(map->isInTheMap(mapPosition) || map->isInTheMap(mapPosition - int3(0,8,0)) || map->isInTheMap(mapPosition - int3(8,0,0)) || map->isInTheMap(mapPosition - int3(8,8,0)));
  1590. uint32_t defIndex = reader->readUInt32();
  1591. std::shared_ptr<ObjectTemplate> originalTemplate = originalTemplates.at(defIndex);
  1592. std::shared_ptr<ObjectTemplate> remappedTemplate = remappedTemplates.at(defIndex);
  1593. auto originalID = originalTemplate->id;
  1594. auto originalSubID = originalTemplate->subid;
  1595. reader->skipZero(5);
  1596. ObjectInstanceID newObjectID = map->allocateUniqueInstanceID();
  1597. auto newObject = readObject(originalID, originalSubID, remappedTemplate, mapPosition, newObjectID);
  1598. if(!newObject)
  1599. continue;
  1600. newObject->setAnchorPos(mapPosition);
  1601. newObject->ID = remappedTemplate->id;
  1602. newObject->id = newObjectID;
  1603. if(newObject->ID != Obj::HERO && newObject->ID != Obj::HERO_PLACEHOLDER && newObject->ID != Obj::PRISON)
  1604. {
  1605. newObject->subID = remappedTemplate->subid;
  1606. }
  1607. newObject->appearance = remappedTemplate;
  1608. if (newObject->isVisitable() && !map->isInTheMap(newObject->visitablePos()))
  1609. logGlobal->error("Map '%s': Object at %s - outside of map borders!", mapName, mapPosition.toString());
  1610. map->generateUniqueInstanceName(newObject.get());
  1611. map->addNewObject(newObject);
  1612. }
  1613. }
  1614. void CMapLoaderH3M::readCreatureSet(CArmedInstance * out, const ObjectInstanceID & idToBeGiven)
  1615. {
  1616. constexpr int unitsToRead = 7;
  1617. out->id = idToBeGiven;
  1618. for(int index = 0; index < unitsToRead; ++index)
  1619. {
  1620. CreatureID creatureID = reader->readCreature();
  1621. int count = reader->readUInt16();
  1622. // Empty slot
  1623. if(creatureID == CreatureID::NONE)
  1624. continue;
  1625. auto result = std::make_unique<CStackInstance>(map->cb);
  1626. result->setCount(count);
  1627. if(creatureID < CreatureID::NONE)
  1628. {
  1629. int value = -creatureID.getNum() - 2;
  1630. assert(value >= 0 && value < 14);
  1631. uint8_t level = value / 2;
  1632. uint8_t upgrade = value % 2;
  1633. //this will happen when random object has random army
  1634. result->randomStack = CStackInstance::RandomStackInfo{level, upgrade};
  1635. }
  1636. else
  1637. {
  1638. result->setType(creatureID);
  1639. }
  1640. out->putStack(SlotID(index), std::move(result));
  1641. }
  1642. out->validTypes(true);
  1643. }
  1644. void CMapLoaderH3M::setOwnerAndValidate(const int3 & mapPosition, CGObjectInstance * object, const PlayerColor & owner)
  1645. {
  1646. assert(owner.isValidPlayer() || owner == PlayerColor::NEUTRAL);
  1647. if(owner == PlayerColor::NEUTRAL)
  1648. {
  1649. object->setOwner(PlayerColor::NEUTRAL);
  1650. return;
  1651. }
  1652. if(!owner.isValidPlayer())
  1653. {
  1654. object->setOwner(PlayerColor::NEUTRAL);
  1655. logGlobal->warn("Map '%s': Object at %s - owned by invalid player %d! Will be set to neutral!", mapName, mapPosition.toString(), int(owner.getNum()));
  1656. return;
  1657. }
  1658. if(!mapHeader->players[owner.getNum()].canAnyonePlay())
  1659. {
  1660. object->setOwner(PlayerColor::NEUTRAL);
  1661. logGlobal->warn("Map '%s': Object at %s - owned by non-existing player %d! Will be set to neutral!", mapName, mapPosition.toString(), int(owner.getNum())
  1662. );
  1663. return;
  1664. }
  1665. object->setOwner(owner);
  1666. }
  1667. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readHero(const int3 & mapPosition, const ObjectInstanceID & objectInstanceID)
  1668. {
  1669. if(features.levelAB)
  1670. {
  1671. unsigned int identifier = reader->readUInt32();
  1672. questIdentifierToId[identifier] = objectInstanceID;
  1673. }
  1674. PlayerColor owner = reader->readPlayer();
  1675. HeroTypeID heroType = reader->readHero();
  1676. //If hero of this type has been predefined, use that as a base.
  1677. //Instance data will overwrite the predefined values where appropriate.
  1678. std::shared_ptr<CGHeroInstance> object;
  1679. if (heroType.hasValue())
  1680. object = map->tryTakeFromHeroPool(heroType);
  1681. if (!object)
  1682. {
  1683. object = std::make_shared<CGHeroInstance>(map->cb);
  1684. object->subID = heroType.getNum();
  1685. }
  1686. setOwnerAndValidate(mapPosition, object.get(), owner);
  1687. for(auto & elem : map->disposedHeroes)
  1688. {
  1689. if(elem.heroId == object->getHeroTypeID())
  1690. {
  1691. object->nameCustomTextId = elem.name;
  1692. object->customPortraitSource = elem.portrait;
  1693. break;
  1694. }
  1695. }
  1696. bool hasName = reader->readBool();
  1697. if(hasName)
  1698. object->nameCustomTextId = readLocalizedString(TextIdentifier("heroes", object->getHeroTypeID().getNum(), "name"));
  1699. if(features.levelSOD)
  1700. {
  1701. bool hasCustomExperience = reader->readBool();
  1702. if(hasCustomExperience)
  1703. object->exp = reader->readUInt32();
  1704. }
  1705. else
  1706. {
  1707. object->exp = reader->readUInt32();
  1708. //0 means "not set" in <=AB maps
  1709. if(!object->exp)
  1710. object->exp = CGHeroInstance::UNINITIALIZED_EXPERIENCE;
  1711. }
  1712. bool hasPortrait = reader->readBool();
  1713. if(hasPortrait)
  1714. object->customPortraitSource = reader->readHeroPortrait();
  1715. bool hasSecSkills = reader->readBool();
  1716. if(hasSecSkills)
  1717. {
  1718. if(!object->secSkills.empty())
  1719. {
  1720. if(object->secSkills[0].first != SecondarySkill::NONE)
  1721. logGlobal->debug("Map '%s': Hero %s subID=%d has set secondary skills twice (in map properties and on adventure map instance). Using the latter set...", mapName, object->getNameTextID(), object->subID);
  1722. object->secSkills.clear();
  1723. }
  1724. uint32_t skillsCount = reader->readUInt32();
  1725. object->secSkills.resize(skillsCount);
  1726. for(int i = 0; i < skillsCount; ++i)
  1727. {
  1728. object->secSkills[i].first = reader->readSkill();
  1729. object->secSkills[i].second = reader->readInt8Checked(1,3);
  1730. }
  1731. }
  1732. bool hasGarison = reader->readBool();
  1733. if(hasGarison)
  1734. readCreatureSet(object.get(), objectInstanceID);
  1735. object->formation = static_cast<EArmyFormation>(reader->readInt8Checked(0, 1));
  1736. assert(object->formation == EArmyFormation::LOOSE || object->formation == EArmyFormation::TIGHT);
  1737. loadArtifactsOfHero(object.get());
  1738. object->patrol.patrolRadius = reader->readUInt8();
  1739. object->patrol.patrolling = (object->patrol.patrolRadius != 0xff);
  1740. if(features.levelAB)
  1741. {
  1742. bool hasCustomBiography = reader->readBool();
  1743. if(hasCustomBiography)
  1744. object->biographyCustomTextId = readLocalizedString(TextIdentifier("heroes", object->subID, "biography"));
  1745. object->gender = static_cast<EHeroGender>(reader->readInt8Checked(-1, 1));
  1746. assert(object->gender == EHeroGender::MALE || object->gender == EHeroGender::FEMALE || object->gender == EHeroGender::DEFAULT);
  1747. }
  1748. else
  1749. {
  1750. object->gender = EHeroGender::DEFAULT;
  1751. }
  1752. // Spells
  1753. if(features.levelSOD)
  1754. {
  1755. bool hasCustomSpells = reader->readBool();
  1756. if(hasCustomSpells)
  1757. {
  1758. if(!object->spells.empty())
  1759. {
  1760. object->spells.clear();
  1761. logGlobal->debug("Hero %s subID=%d has spells set twice (in map properties and on adventure map instance). Using the latter set...", object->getNameTextID(), object->subID);
  1762. }
  1763. reader->readBitmaskSpells(object->spells, false);
  1764. object->spells.insert(SpellID::PRESET); //placeholder "preset spells"
  1765. }
  1766. }
  1767. else if(features.levelAB)
  1768. {
  1769. //we can read one spell
  1770. SpellID spell = reader->readSpell();
  1771. // workaround: VCMI uses 'PRESET' spell to indicate that spellbook has been preconfigured on map
  1772. // but H3 uses 'PRESET' spell (-2) to indicate that game should give standard spell to hero
  1773. if(spell == SpellID::NONE)
  1774. object->spells.insert(SpellID::PRESET); //spellbook is preconfigured to be empty
  1775. if (spell.hasValue())
  1776. object->spells.insert(spell);
  1777. }
  1778. if(features.levelSOD)
  1779. {
  1780. bool hasCustomPrimSkills = reader->readBool();
  1781. if(hasCustomPrimSkills)
  1782. {
  1783. auto ps = object->getAllBonuses(Selector::type()(BonusType::PRIMARY_SKILL).And(Selector::sourceType()(BonusSource::HERO_BASE_SKILL)), nullptr);
  1784. if(ps->size())
  1785. {
  1786. logGlobal->debug("Hero %s has set primary skills twice (in map properties and on adventure map instance). Using the latter set...", object->getHeroTypeID().getNum() );
  1787. for(const auto & b : *ps)
  1788. object->removeBonus(b);
  1789. }
  1790. for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; ++xx)
  1791. {
  1792. object->pushPrimSkill(static_cast<PrimarySkill>(xx), reader->readUInt8());
  1793. }
  1794. }
  1795. }
  1796. if (object->subID != MapObjectSubID())
  1797. logGlobal->debug("Map '%s': Hero on map: %s at %s, owned by %s", mapName, object->getHeroType()->getJsonKey(), mapPosition.toString(), object->getOwner().toString());
  1798. else
  1799. logGlobal->debug("Map '%s': Hero on map: (random) at %s, owned by %s", mapName, mapPosition.toString(), object->getOwner().toString());
  1800. reader->skipZero(16);
  1801. if(features.levelHOTA5)
  1802. {
  1803. bool alwaysAddSkills = reader->readBool(); // prevent heroes from receiving additional random secondary skills at the start of the map if they are not of the first level
  1804. bool cannotGainXP = reader->readBool();
  1805. int32_t level = reader->readInt32(); // Needs investigation how this interacts with usual setting of level via experience
  1806. assert(level > 0);
  1807. if (!alwaysAddSkills)
  1808. logGlobal->warn("Map '%s': Option to prevent hero %d from gaining skills on map start is not implemented!", mapName, object->subID.num);
  1809. if (cannotGainXP)
  1810. logGlobal->warn("Map '%s': Option to prevent hero %d from receiveing experience is not implemented!", mapName, object->subID.num);
  1811. if (level > 1)
  1812. logGlobal->warn("Map '%s': Option to set level of hero %d to %d is not implemented!", mapName, object->subID.num, level);
  1813. }
  1814. return object;
  1815. }
  1816. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readSeerHut(const int3 & position, const ObjectInstanceID & idToBeGiven)
  1817. {
  1818. auto hut = std::make_shared<CGSeerHut>(map->cb);
  1819. uint32_t questsCount = 1;
  1820. if(features.levelHOTA3)
  1821. questsCount = reader->readUInt32();
  1822. //TODO: HotA
  1823. if(questsCount > 1)
  1824. logGlobal->warn("Map '%s': Seer Hut at %s - %d quests are not implemented!", mapName, position.toString(), questsCount);
  1825. for(size_t i = 0; i < questsCount; ++i)
  1826. readSeerHutQuest(hut.get(), position, idToBeGiven);
  1827. if(features.levelHOTA3)
  1828. {
  1829. uint32_t repeateableQuestsCount = reader->readUInt32();
  1830. hut->getQuest().repeatedQuest = repeateableQuestsCount != 0;
  1831. if(repeateableQuestsCount != 0)
  1832. logGlobal->warn("Map '%s': Seer Hut at %s - %d repeatable quests are not implemented!", mapName, position.toString(), repeateableQuestsCount);
  1833. for(size_t i = 0; i < repeateableQuestsCount; ++i)
  1834. readSeerHutQuest(hut.get(), position, idToBeGiven);
  1835. }
  1836. reader->skipZero(2);
  1837. return hut;
  1838. }
  1839. enum class ESeerHutRewardType : uint8_t
  1840. {
  1841. NOTHING = 0,
  1842. EXPERIENCE = 1,
  1843. MANA_POINTS = 2,
  1844. MORALE = 3,
  1845. LUCK = 4,
  1846. RESOURCES = 5,
  1847. PRIMARY_SKILL = 6,
  1848. SECONDARY_SKILL = 7,
  1849. ARTIFACT = 8,
  1850. SPELL = 9,
  1851. CREATURE = 10,
  1852. };
  1853. void CMapLoaderH3M::readSeerHutQuest(CGSeerHut * hut, const int3 & position, const ObjectInstanceID & idToBeGiven)
  1854. {
  1855. EQuestMission missionType = EQuestMission::NONE;
  1856. if(features.levelAB)
  1857. {
  1858. missionType = readQuest(hut, position);
  1859. }
  1860. else
  1861. {
  1862. //RoE
  1863. auto artID = reader->readArtifact();
  1864. if(artID != ArtifactID::NONE)
  1865. {
  1866. //not none quest
  1867. hut->getQuest().mission.artifacts.push_back(artID);
  1868. missionType = EQuestMission::ARTIFACT;
  1869. }
  1870. hut->getQuest().lastDay = -1; //no timeout
  1871. hut->getQuest().isCustomFirst = false;
  1872. hut->getQuest().isCustomNext = false;
  1873. hut->getQuest().isCustomComplete = false;
  1874. }
  1875. if(missionType != EQuestMission::NONE)
  1876. {
  1877. auto rewardType = static_cast<ESeerHutRewardType>(reader->readInt8Checked(0, 10));
  1878. Rewardable::VisitInfo vinfo;
  1879. auto & reward = vinfo.reward;
  1880. switch(rewardType)
  1881. {
  1882. case ESeerHutRewardType::NOTHING:
  1883. {
  1884. // no-op
  1885. break;
  1886. }
  1887. case ESeerHutRewardType::EXPERIENCE:
  1888. {
  1889. reward.heroExperience = reader->readUInt32();
  1890. break;
  1891. }
  1892. case ESeerHutRewardType::MANA_POINTS:
  1893. {
  1894. reward.manaDiff = reader->readUInt32();
  1895. break;
  1896. }
  1897. case ESeerHutRewardType::MORALE:
  1898. {
  1899. reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, reader->readInt8Checked(-3, 3), BonusSourceID(idToBeGiven));
  1900. break;
  1901. }
  1902. case ESeerHutRewardType::LUCK:
  1903. {
  1904. reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, reader->readInt8Checked(-3, 3), BonusSourceID(idToBeGiven));
  1905. break;
  1906. }
  1907. case ESeerHutRewardType::RESOURCES:
  1908. {
  1909. auto rId = reader->readGameResID();
  1910. auto rVal = reader->readUInt32();
  1911. reward.resources[rId] = rVal;
  1912. break;
  1913. }
  1914. case ESeerHutRewardType::PRIMARY_SKILL:
  1915. {
  1916. auto rId = reader->readPrimary();
  1917. auto rVal = reader->readUInt8();
  1918. reward.primary.at(rId.getNum()) = rVal;
  1919. break;
  1920. }
  1921. case ESeerHutRewardType::SECONDARY_SKILL:
  1922. {
  1923. auto rId = reader->readSkill();
  1924. auto rVal = reader->readInt8Checked(1,3);
  1925. reward.secondary[rId] = rVal;
  1926. break;
  1927. }
  1928. case ESeerHutRewardType::ARTIFACT:
  1929. {
  1930. ArtifactID grantedArtifact = reader->readArtifact();
  1931. if (features.levelHOTA5)
  1932. {
  1933. SpellID scrollSpell = reader->readSpell16();
  1934. if (grantedArtifact == ArtifactID::SPELL_SCROLL)
  1935. reward.grantedScrolls.push_back(scrollSpell);
  1936. }
  1937. else
  1938. reward.grantedArtifacts.push_back(grantedArtifact);
  1939. break;
  1940. }
  1941. case ESeerHutRewardType::SPELL:
  1942. {
  1943. reward.spells.push_back(reader->readSpell());
  1944. break;
  1945. }
  1946. case ESeerHutRewardType::CREATURE:
  1947. {
  1948. auto rId = reader->readCreature();
  1949. auto rVal = reader->readUInt16();
  1950. reward.creatures.emplace_back(rId, rVal);
  1951. break;
  1952. }
  1953. default:
  1954. {
  1955. assert(0);
  1956. }
  1957. }
  1958. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  1959. hut->configuration.info.push_back(vinfo);
  1960. }
  1961. else
  1962. {
  1963. // missionType==255
  1964. reader->skipZero(1);
  1965. }
  1966. }
  1967. EQuestMission CMapLoaderH3M::readQuest(IQuestObject * guard, const int3 & position)
  1968. {
  1969. auto missionId = static_cast<EQuestMission>(reader->readInt8Checked(0, 10));
  1970. switch(missionId)
  1971. {
  1972. case EQuestMission::NONE:
  1973. return missionId;
  1974. case EQuestMission::PRIMARY_SKILL:
  1975. {
  1976. for(int x = 0; x < 4; ++x)
  1977. {
  1978. guard->getQuest().mission.primary[x] = reader->readUInt8();
  1979. }
  1980. break;
  1981. }
  1982. case EQuestMission::LEVEL:
  1983. {
  1984. guard->getQuest().mission.heroLevel = reader->readUInt32();
  1985. break;
  1986. }
  1987. case EQuestMission::KILL_HERO:
  1988. case EQuestMission::KILL_CREATURE:
  1989. {
  1990. // NOTE: assert might fail on multi-quest seers
  1991. //assert(questsToResolve.count(guard) == 0);
  1992. questsToResolve[guard] = reader->readUInt32();
  1993. break;
  1994. }
  1995. case EQuestMission::ARTIFACT:
  1996. {
  1997. size_t artNumber = reader->readUInt8();
  1998. for(size_t yy = 0; yy < artNumber; ++yy)
  1999. {
  2000. ArtifactID requiredArtifact = reader->readArtifact();
  2001. if (features.levelHOTA5)
  2002. {
  2003. SpellID scrollSpell = reader->readSpell16();
  2004. if (requiredArtifact == ArtifactID::SPELL_SCROLL)
  2005. guard->getQuest().mission.scrolls.push_back(scrollSpell);
  2006. }
  2007. else
  2008. guard->getQuest().mission.artifacts.push_back(requiredArtifact);
  2009. map->allowedArtifact.erase(requiredArtifact); //these are unavailable for random generation
  2010. }
  2011. break;
  2012. }
  2013. case EQuestMission::ARMY:
  2014. {
  2015. size_t typeNumber = reader->readUInt8();
  2016. guard->getQuest().mission.creatures.resize(typeNumber);
  2017. for(size_t hh = 0; hh < typeNumber; ++hh)
  2018. {
  2019. guard->getQuest().mission.creatures[hh].setType(reader->readCreature().toCreature());
  2020. guard->getQuest().mission.creatures[hh].setCount(reader->readUInt16());
  2021. }
  2022. break;
  2023. }
  2024. case EQuestMission::RESOURCES:
  2025. {
  2026. for(int x = 0; x < 7; ++x)
  2027. guard->getQuest().mission.resources[x] = reader->readUInt32();
  2028. break;
  2029. }
  2030. case EQuestMission::HERO:
  2031. {
  2032. guard->getQuest().mission.heroes.push_back(reader->readHero());
  2033. break;
  2034. }
  2035. case EQuestMission::PLAYER:
  2036. {
  2037. guard->getQuest().mission.players.push_back(reader->readPlayer());
  2038. break;
  2039. }
  2040. case EQuestMission::HOTA_MULTI:
  2041. {
  2042. uint32_t missionSubID = reader->readUInt32();
  2043. assert(missionSubID < 3);
  2044. if(missionSubID == 0)
  2045. {
  2046. missionId = EQuestMission::HOTA_HERO_CLASS;
  2047. std::set<HeroClassID> heroClasses;
  2048. reader->readBitmaskHeroClassesSized(heroClasses, false);
  2049. for(auto & hc : heroClasses)
  2050. guard->getQuest().mission.heroClasses.push_back(hc);
  2051. break;
  2052. }
  2053. if(missionSubID == 1)
  2054. {
  2055. missionId = EQuestMission::HOTA_REACH_DATE;
  2056. guard->getQuest().mission.daysPassed = reader->readUInt32() + 1;
  2057. break;
  2058. }
  2059. if(missionSubID == 2)
  2060. {
  2061. missionId = EQuestMission::HOTA_GAME_DIFFICULTY;
  2062. int32_t difficultyMask = reader->readUInt32();
  2063. assert(difficultyMask > 0 && difficultyMask < 32);
  2064. logGlobal->warn("Map '%s': Seer Hut at %s: Difficulty-specific quest (%d) is not implemented!", mapName, position.toString(), difficultyMask);
  2065. break;
  2066. }
  2067. break;
  2068. }
  2069. default:
  2070. {
  2071. assert(0);
  2072. }
  2073. }
  2074. guard->getQuest().lastDay = reader->readInt32();
  2075. guard->getQuest().firstVisitText.appendTextID(readLocalizedString(TextIdentifier("quest", position.x, position.y, position.z, "firstVisit")));
  2076. guard->getQuest().nextVisitText.appendTextID(readLocalizedString(TextIdentifier("quest", position.x, position.y, position.z, "nextVisit")));
  2077. guard->getQuest().completedText.appendTextID(readLocalizedString(TextIdentifier("quest", position.x, position.y, position.z, "completed")));
  2078. guard->getQuest().isCustomFirst = !guard->getQuest().firstVisitText.empty();
  2079. guard->getQuest().isCustomNext = !guard->getQuest().nextVisitText.empty();
  2080. guard->getQuest().isCustomComplete = !guard->getQuest().completedText.empty();
  2081. return missionId;
  2082. }
  2083. std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readTown(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate, const ObjectInstanceID & idToBeGiven)
  2084. {
  2085. auto object = std::make_shared<CGTownInstance>(map->cb);
  2086. if(features.levelAB)
  2087. object->identifier = reader->readUInt32();
  2088. setOwnerAndValidate(position, object.get(), reader->readPlayer());
  2089. std::optional<FactionID> faction;
  2090. if (objectTemplate->id == Obj::TOWN)
  2091. faction = FactionID(objectTemplate->subid);
  2092. bool hasName = reader->readBool();
  2093. if(hasName)
  2094. object->setNameTextId(readLocalizedString(TextIdentifier("town", position.x, position.y, position.z, "name")));
  2095. bool hasGarrison = reader->readBool();
  2096. if(hasGarrison)
  2097. readCreatureSet(object.get(), idToBeGiven);
  2098. object->formation = static_cast<EArmyFormation>(reader->readInt8Checked(0, 1));
  2099. assert(object->formation == EArmyFormation::LOOSE || object->formation == EArmyFormation::TIGHT);
  2100. bool hasCustomBuildings = reader->readBool();
  2101. if(hasCustomBuildings)
  2102. {
  2103. std::set<BuildingID> builtBuildings;
  2104. reader->readBitmaskBuildings(builtBuildings, faction);
  2105. for(const auto & building : builtBuildings)
  2106. object->addBuilding(building);
  2107. reader->readBitmaskBuildings(object->forbiddenBuildings, faction);
  2108. }
  2109. // Standard buildings
  2110. else
  2111. {
  2112. bool hasFort = reader->readBool();
  2113. if(hasFort)
  2114. object->addBuilding(BuildingID::FORT);
  2115. //means that set of standard building should be included
  2116. object->addBuilding(BuildingID::DEFAULT);
  2117. }
  2118. if(features.levelAB)
  2119. {
  2120. std::set<SpellID> spellsMask;
  2121. reader->readBitmaskSpells(spellsMask, false);
  2122. std::copy(spellsMask.begin(), spellsMask.end(), std::back_inserter(object->obligatorySpells));
  2123. }
  2124. {
  2125. std::set<SpellID> spellsMask = LIBRARY->spellh->getDefaultAllowed(); // by default - include spells from mods
  2126. reader->readBitmaskSpells(spellsMask, true);
  2127. std::copy(spellsMask.begin(), spellsMask.end(), std::back_inserter(object->possibleSpells));
  2128. }
  2129. if(features.levelHOTA1)
  2130. object->spellResearchAllowed = reader->readBool();
  2131. if(features.levelHOTA5)
  2132. {
  2133. // Most likely customization of special buildings per faction -> 4x11 table
  2134. // presumably:
  2135. // 0 -> default / allowed
  2136. // 1 -> built?
  2137. // 2 -> banned?
  2138. uint32_t specialBuildingsSize = reader->readUInt32();
  2139. for (int i = 0; i < specialBuildingsSize; ++i)
  2140. {
  2141. int factionID = i / 4;
  2142. int buildingID = i % 4;
  2143. int8_t specialBuildingBuilt = reader->readInt8Checked(0, 2);
  2144. if (specialBuildingBuilt != 0)
  2145. logGlobal->warn("Map '%s': Town at %s: Constructing / banning town-specific special building %d in faction %d on start is not implemented!", mapName, position.toString(), buildingID, factionID);
  2146. }
  2147. }
  2148. // Read castle events
  2149. uint32_t eventsCount = reader->readUInt32();
  2150. for(int eventID = 0; eventID < eventsCount; ++eventID)
  2151. {
  2152. CCastleEvent event;
  2153. event.creatures.resize(7);
  2154. readEventCommon(event, TextIdentifier("town", position.x, position.y, position.z, "event", eventID, "description"));
  2155. if(features.levelHOTA5)
  2156. {
  2157. int32_t creatureGrowth8 = reader->readInt32();
  2158. // always 44
  2159. int32_t hotaAmount = reader->readInt32();
  2160. // contains bitmask on which town-specific buildings to build
  2161. // 4 bits / town, for each of special building in town (special 1 - special 4)
  2162. int32_t hotaSpecialA = reader->readInt32();
  2163. int16_t hotaSpecialB = reader->readInt16();
  2164. if (hotaSpecialA != 0 || hotaSpecialB != 0 || hotaAmount != 44)
  2165. logGlobal->warn("Map '%s': Town at %s: Constructing town-specific special buildings in event is not implemented!", mapName, position.toString());
  2166. event.creatures.push_back(creatureGrowth8);
  2167. }
  2168. if(features.levelHOTA7)
  2169. {
  2170. [[maybe_unused]] bool neutralAffected = reader->readBool();
  2171. }
  2172. // New buildings
  2173. reader->readBitmaskBuildings(event.buildings, faction);
  2174. for(int i = 0; i < 7; ++i)
  2175. event.creatures[i] = reader->readUInt16();
  2176. reader->skipZero(4);
  2177. object->events.push_back(event);
  2178. }
  2179. if(features.levelSOD)
  2180. {
  2181. object->alignmentToPlayer = PlayerColor::NEUTRAL; // "same as owner or random"
  2182. uint8_t alignment = reader->readUInt8();
  2183. if(alignment != 255)
  2184. {
  2185. if(alignment < PlayerColor::PLAYER_LIMIT.getNum())
  2186. {
  2187. if (mapHeader->players[alignment].canAnyonePlay())
  2188. object->alignmentToPlayer = PlayerColor(alignment);
  2189. else
  2190. logGlobal->warn("Map '%s': Alignment of town at %s is invalid! Player %d is not present on map!", mapName, position.toString(), static_cast<int>(alignment));
  2191. }
  2192. else
  2193. {
  2194. // TODO: HOTA support
  2195. uint8_t invertedAlignment = alignment - PlayerColor::PLAYER_LIMIT.getNum();
  2196. if(invertedAlignment < PlayerColor::PLAYER_LIMIT.getNum())
  2197. {
  2198. logGlobal->warn("Map '%s': Alignment of town at %s 'not as player %d' is not implemented!", mapName, position.toString(), alignment - PlayerColor::PLAYER_LIMIT.getNum());
  2199. }
  2200. else
  2201. {
  2202. logGlobal->warn("Map '%s': Alignment of town at %s is corrupted!!", mapName, position.toString());
  2203. }
  2204. }
  2205. }
  2206. }
  2207. reader->skipZero(3);
  2208. return object;
  2209. }
  2210. void CMapLoaderH3M::readEventCommon(CMapEvent & event, const TextIdentifier & messageID)
  2211. {
  2212. event.name = readBasicString();
  2213. event.message.appendTextID(readLocalizedString(messageID));
  2214. reader->readResources(event.resources);
  2215. reader->readBitmaskPlayers(event.players, false);
  2216. if(features.levelSOD)
  2217. event.humanAffected = reader->readBool();
  2218. else
  2219. event.humanAffected = true;
  2220. event.computerAffected = reader->readBool();
  2221. event.firstOccurrence = reader->readUInt16();
  2222. event.nextOccurrence = reader->readUInt16();
  2223. reader->skipZero(16);
  2224. if (features.levelHOTA7)
  2225. {
  2226. int32_t allowedDifficultiesMask = reader->readInt32();
  2227. assert(allowedDifficultiesMask > 0 && allowedDifficultiesMask < 32);
  2228. if (allowedDifficultiesMask != 31)
  2229. logGlobal->warn("Map '%s': Map event: availability by difficulty (mode %d) is not implemented!", mapName, allowedDifficultiesMask);
  2230. }
  2231. }
  2232. void CMapLoaderH3M::readEvents()
  2233. {
  2234. uint32_t eventsCount = reader->readUInt32();
  2235. for(int eventID = 0; eventID < eventsCount; ++eventID)
  2236. {
  2237. CMapEvent event;
  2238. readEventCommon(event, TextIdentifier("event", eventID, "description"));
  2239. // garbage bytes that were present in HOTA5 & HOTA6
  2240. if (features.levelHOTA5 && !features.levelHOTA7)
  2241. reader->skipUnused(14);
  2242. map->events.push_back(event);
  2243. }
  2244. }
  2245. void CMapLoaderH3M::readMessageAndGuards(MetaString & message, CArmedInstance * guards, const int3 & position, const ObjectInstanceID & idToBeGiven)
  2246. {
  2247. bool hasMessage = reader->readBool();
  2248. if(hasMessage)
  2249. {
  2250. message.appendTextID(readLocalizedString(TextIdentifier("guards", position.x, position.y, position.z, "message")));
  2251. bool hasGuards = reader->readBool();
  2252. if(hasGuards)
  2253. readCreatureSet(guards, idToBeGiven);
  2254. reader->skipZero(4);
  2255. }
  2256. }
  2257. std::string CMapLoaderH3M::readBasicString()
  2258. {
  2259. return TextOperations::toUnicode(reader->readBaseString(), fileEncoding);
  2260. }
  2261. std::string CMapLoaderH3M::readLocalizedString(const TextIdentifier & stringIdentifier)
  2262. {
  2263. std::string mapString = TextOperations::toUnicode(reader->readBaseString(), fileEncoding);
  2264. TextIdentifier fullIdentifier("map", mapName, stringIdentifier.get());
  2265. if(mapString.empty())
  2266. return "";
  2267. return mapRegisterLocalizedString(modName, *mapHeader, fullIdentifier, mapString);
  2268. }
  2269. void CMapLoaderH3M::afterRead()
  2270. {
  2271. //convert main town positions for all players to actual object position, in H3M it is position of active tile
  2272. for(auto & p : map->players)
  2273. {
  2274. int3 posOfMainTown = p.posOfMainTown;
  2275. if(posOfMainTown.isValid() && map->isInTheMap(posOfMainTown))
  2276. {
  2277. const TerrainTile & t = map->getTile(posOfMainTown);
  2278. const CGObjectInstance * mainTown = nullptr;
  2279. for(ObjectInstanceID objID : t.visitableObjects)
  2280. {
  2281. const CGObjectInstance * object = map->getObject(objID);
  2282. if(object->ID == Obj::TOWN || object->ID == Obj::RANDOM_TOWN)
  2283. {
  2284. mainTown = object;
  2285. break;
  2286. }
  2287. }
  2288. if(mainTown == nullptr)
  2289. continue;
  2290. p.posOfMainTown = posOfMainTown + mainTown->getVisitableOffset();
  2291. }
  2292. }
  2293. for (auto & quest : questsToResolve)
  2294. quest.first->getQuest().killTarget = questIdentifierToId.at(quest.second);
  2295. }
  2296. VCMI_LIB_NAMESPACE_END