MapFormatH3M.cpp 86 KB

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