2
0

MapFormatH3M.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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 <boost/crc.hpp>
  12. #include "MapFormatH3M.h"
  13. #include "CMap.h"
  14. #include "../CStopWatch.h"
  15. #include "../filesystem/Filesystem.h"
  16. #include "../spells/CSpellHandler.h"
  17. #include "../CCreatureHandler.h"
  18. #include "../CGeneralTextHandler.h"
  19. #include "../CHeroHandler.h"
  20. #include "../mapObjects/CObjectClassesHandler.h"
  21. #include "../mapObjects/MapObjects.h"
  22. #include "../VCMI_Lib.h"
  23. #include "../NetPacksBase.h"
  24. const bool CMapLoaderH3M::IS_PROFILING_ENABLED = false;
  25. CMapLoaderH3M::CMapLoaderH3M(CInputStream * stream) : map(nullptr), reader(stream),inputStream(stream)
  26. {
  27. }
  28. CMapLoaderH3M::~CMapLoaderH3M()
  29. {
  30. }
  31. std::unique_ptr<CMap> CMapLoaderH3M::loadMap()
  32. {
  33. // Init map object by parsing the input buffer
  34. map = new CMap();
  35. mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));
  36. init();
  37. return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));;
  38. }
  39. std::unique_ptr<CMapHeader> CMapLoaderH3M::loadMapHeader()
  40. {
  41. // Read header
  42. mapHeader = make_unique<CMapHeader>();
  43. readHeader();
  44. return std::move(mapHeader);
  45. }
  46. void CMapLoaderH3M::init()
  47. {
  48. //FIXME: get rid of double input process
  49. si64 temp_size = inputStream->getSize();
  50. inputStream->seek(0);
  51. auto temp_buffer = new ui8[temp_size];
  52. inputStream->read(temp_buffer,temp_size);
  53. // Compute checksum
  54. boost::crc_32_type result;
  55. result.process_bytes(temp_buffer, temp_size);
  56. map->checksum = result.checksum();
  57. delete [] temp_buffer;
  58. inputStream->seek(0);
  59. CStopWatch sw;
  60. struct MapLoadingTime
  61. {
  62. std::string name;
  63. si64 time;
  64. MapLoadingTime(std::string name, si64 time) : name(name),
  65. time(time)
  66. {
  67. }
  68. };
  69. std::vector<MapLoadingTime> times;
  70. readHeader();
  71. times.push_back(MapLoadingTime("header", sw.getDiff()));
  72. map->allHeroes.resize(map->allowedHeroes.size());
  73. readDisposedHeroes();
  74. times.push_back(MapLoadingTime("disposed heroes", sw.getDiff()));
  75. readAllowedArtifacts();
  76. times.push_back(MapLoadingTime("allowed artifacts", sw.getDiff()));
  77. readAllowedSpellsAbilities();
  78. times.push_back(MapLoadingTime("allowed spells and abilities", sw.getDiff()));
  79. readRumors();
  80. times.push_back(MapLoadingTime("rumors", sw.getDiff()));
  81. readPredefinedHeroes();
  82. times.push_back(MapLoadingTime("predefined heroes", sw.getDiff()));
  83. readTerrain();
  84. times.push_back(MapLoadingTime("terrain", sw.getDiff()));
  85. readDefInfo();
  86. times.push_back(MapLoadingTime("def info", sw.getDiff()));
  87. readObjects();
  88. times.push_back(MapLoadingTime("objects", sw.getDiff()));
  89. readEvents();
  90. times.push_back(MapLoadingTime("events", sw.getDiff()));
  91. // Calculate blocked / visitable positions
  92. for(auto & elem : map->objects)
  93. {
  94. map->addBlockVisTiles(elem);
  95. }
  96. times.push_back(MapLoadingTime("blocked/visitable tiles", sw.getDiff()));
  97. // Print profiling times
  98. if(IS_PROFILING_ENABLED)
  99. {
  100. for(MapLoadingTime & mlt : times)
  101. {
  102. logGlobal->debugStream() << "\tReading " << mlt.name << " took " << mlt.time << " ms.";
  103. }
  104. }
  105. map->calculateGuardingGreaturePositions();
  106. }
  107. void CMapLoaderH3M::readHeader()
  108. {
  109. // Check map for validity
  110. // Note: disabled, causes decompression of the entire file ( = SLOW)
  111. //if(inputStream->getSize() < 50)
  112. //{
  113. // throw std::runtime_error("Corrupted map file.");
  114. //}
  115. // Map version
  116. mapHeader->version = (EMapFormat::EMapFormat)(reader.readUInt32());
  117. if(mapHeader->version != EMapFormat::ROE && mapHeader->version != EMapFormat::AB && mapHeader->version != EMapFormat::SOD
  118. && mapHeader->version != EMapFormat::WOG)
  119. {
  120. throw std::runtime_error("Invalid map format!");
  121. }
  122. // Read map name, description, dimensions,...
  123. mapHeader->areAnyPlayers = reader.readBool();
  124. mapHeader->height = mapHeader->width = reader.readUInt32();
  125. mapHeader->twoLevel = reader.readBool();
  126. mapHeader->name = reader.readString();
  127. mapHeader->description = reader.readString();
  128. mapHeader->difficulty = reader.readInt8();
  129. if(mapHeader->version != EMapFormat::ROE)
  130. {
  131. mapHeader->levelLimit = reader.readUInt8();
  132. }
  133. else
  134. {
  135. mapHeader->levelLimit = 0;
  136. }
  137. readPlayerInfo();
  138. readVictoryLossConditions();
  139. readTeamInfo();
  140. readAllowedHeroes();
  141. }
  142. void CMapLoaderH3M::readPlayerInfo()
  143. {
  144. for(int i = 0; i < mapHeader->players.size(); ++i)
  145. {
  146. mapHeader->players[i].canHumanPlay = reader.readBool();
  147. mapHeader->players[i].canComputerPlay = reader.readBool();
  148. // If nobody can play with this player
  149. if((!(mapHeader->players[i].canHumanPlay || mapHeader->players[i].canComputerPlay)))
  150. {
  151. switch(mapHeader->version)
  152. {
  153. case EMapFormat::SOD:
  154. case EMapFormat::WOG:
  155. reader.skip(13);
  156. break;
  157. case EMapFormat::AB:
  158. reader.skip(12);
  159. break;
  160. case EMapFormat::ROE:
  161. reader.skip(6);
  162. break;
  163. }
  164. continue;
  165. }
  166. mapHeader->players[i].aiTactic = static_cast<EAiTactic::EAiTactic>(reader.readUInt8());
  167. if(mapHeader->version == EMapFormat::SOD || mapHeader->version == EMapFormat::WOG)
  168. {
  169. mapHeader->players[i].p7 = reader.readUInt8();
  170. }
  171. else
  172. {
  173. mapHeader->players[i].p7 = -1;
  174. }
  175. // Factions this player can choose
  176. ui16 allowedFactions = reader.readUInt8();
  177. // How many factions will be read from map
  178. ui16 totalFactions = GameConstants::F_NUMBER;
  179. if(mapHeader->version != EMapFormat::ROE)
  180. allowedFactions += reader.readUInt8() * 256;
  181. else
  182. totalFactions--; //exclude conflux for ROE
  183. for(int fact = 0; fact < totalFactions; ++fact)
  184. {
  185. if(!(allowedFactions & (1 << fact)))
  186. {
  187. mapHeader->players[i].allowedFactions.erase(fact);
  188. }
  189. }
  190. mapHeader->players[i].isFactionRandom = reader.readBool();
  191. mapHeader->players[i].hasMainTown = reader.readBool();
  192. if(mapHeader->players[i].hasMainTown)
  193. {
  194. if(mapHeader->version != EMapFormat::ROE)
  195. {
  196. mapHeader->players[i].generateHeroAtMainTown = reader.readBool();
  197. mapHeader->players[i].generateHero = reader.readBool();
  198. }
  199. else
  200. {
  201. mapHeader->players[i].generateHeroAtMainTown = true;
  202. mapHeader->players[i].generateHero = false;
  203. }
  204. mapHeader->players[i].posOfMainTown = readInt3();
  205. }
  206. mapHeader->players[i].hasRandomHero = reader.readBool();
  207. mapHeader->players[i].mainCustomHeroId = reader.readUInt8();
  208. if(mapHeader->players[i].mainCustomHeroId != 0xff)
  209. {
  210. mapHeader->players[i].mainCustomHeroPortrait = reader.readUInt8();
  211. if (mapHeader->players[i].mainCustomHeroPortrait == 0xff)
  212. mapHeader->players[i].mainCustomHeroPortrait = -1; //correct 1-byte -1 (0xff) into 4-byte -1
  213. mapHeader->players[i].mainCustomHeroName = reader.readString();
  214. }
  215. else
  216. mapHeader->players[i].mainCustomHeroId = -1; //correct 1-byte -1 (0xff) into 4-byte -1
  217. if(mapHeader->version != EMapFormat::ROE)
  218. {
  219. mapHeader->players[i].powerPlaceholders = reader.readUInt8(); //unknown byte
  220. int heroCount = reader.readUInt8();
  221. reader.skip(3);
  222. for(int pp = 0; pp < heroCount; ++pp)
  223. {
  224. SHeroName vv;
  225. vv.heroId = reader.readUInt8();
  226. vv.heroName = reader.readString();
  227. mapHeader->players[i].heroesNames.push_back(vv);
  228. }
  229. }
  230. }
  231. }
  232. namespace EVictoryConditionType
  233. {
  234. enum EVictoryConditionType { ARTIFACT, GATHERTROOP, GATHERRESOURCE, BUILDCITY, BUILDGRAIL, BEATHERO,
  235. CAPTURECITY, BEATMONSTER, TAKEDWELLINGS, TAKEMINES, TRANSPORTITEM, WINSTANDARD = 255 };
  236. }
  237. namespace ELossConditionType
  238. {
  239. enum ELossConditionType { LOSSCASTLE, LOSSHERO, TIMEEXPIRES, LOSSSTANDARD = 255 };
  240. }
  241. void CMapLoaderH3M::readVictoryLossConditions()
  242. {
  243. mapHeader->triggeredEvents.clear();
  244. auto vicCondition = (EVictoryConditionType::EVictoryConditionType)reader.readUInt8();
  245. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  246. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  247. defeatCondition.value = 7;
  248. TriggeredEvent standardVictory;
  249. standardVictory.effect.type = EventEffect::VICTORY;
  250. standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
  251. standardVictory.identifier = "standardVictory";
  252. standardVictory.description = ""; // TODO: display in quest window
  253. standardVictory.onFulfill = VLC->generaltexth->allTexts[659];
  254. standardVictory.trigger = EventExpression(victoryCondition);
  255. TriggeredEvent standardDefeat;
  256. standardDefeat.effect.type = EventEffect::DEFEAT;
  257. standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];
  258. standardDefeat.identifier = "standardDefeat";
  259. standardDefeat.description = ""; // TODO: display in quest window
  260. standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];
  261. standardDefeat.trigger = EventExpression(defeatCondition);
  262. // Specific victory conditions
  263. if(vicCondition == EVictoryConditionType::WINSTANDARD)
  264. {
  265. // create normal condition
  266. mapHeader->triggeredEvents.push_back(standardVictory);
  267. mapHeader->victoryIconIndex = 11;
  268. mapHeader->victoryMessage = VLC->generaltexth->victoryConditions[0];
  269. }
  270. else
  271. {
  272. TriggeredEvent specialVictory;
  273. specialVictory.effect.type = EventEffect::VICTORY;
  274. specialVictory.identifier = "specialVictory";
  275. specialVictory.description = ""; // TODO: display in quest window
  276. mapHeader->victoryIconIndex = ui16(vicCondition);
  277. mapHeader->victoryMessage = VLC->generaltexth->victoryConditions[size_t(vicCondition) + 1];
  278. bool allowNormalVictory = reader.readBool();
  279. bool appliesToAI = reader.readBool();
  280. if (allowNormalVictory)
  281. {
  282. size_t playersOnMap = boost::range::count_if(mapHeader->players,[](const PlayerInfo & info) { return info.canAnyonePlay();});
  283. if (playersOnMap == 1)
  284. {
  285. logGlobal->warnStream() << "Map " << mapHeader->name << " has only one player but allows normal victory?";
  286. allowNormalVictory = false; // makes sense? Not much. Works as H3? Yes!
  287. }
  288. }
  289. switch(vicCondition)
  290. {
  291. case EVictoryConditionType::ARTIFACT:
  292. {
  293. EventCondition cond(EventCondition::HAVE_ARTIFACT);
  294. cond.objectType = reader.readUInt8();
  295. if (mapHeader->version != EMapFormat::ROE)
  296. reader.skip(1);
  297. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[281];
  298. specialVictory.onFulfill = VLC->generaltexth->allTexts[280];
  299. specialVictory.trigger = EventExpression(cond);
  300. break;
  301. }
  302. case EVictoryConditionType::GATHERTROOP:
  303. {
  304. EventCondition cond(EventCondition::HAVE_CREATURES);
  305. cond.objectType = reader.readUInt8();
  306. if (mapHeader->version != EMapFormat::ROE)
  307. reader.skip(1);
  308. cond.value = reader.readUInt32();
  309. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[277];
  310. specialVictory.onFulfill = VLC->generaltexth->allTexts[276];
  311. specialVictory.trigger = EventExpression(cond);
  312. break;
  313. }
  314. case EVictoryConditionType::GATHERRESOURCE:
  315. {
  316. EventCondition cond(EventCondition::HAVE_RESOURCES);
  317. cond.objectType = reader.readUInt8();
  318. cond.value = reader.readUInt32();
  319. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[279];
  320. specialVictory.onFulfill = VLC->generaltexth->allTexts[278];
  321. specialVictory.trigger = EventExpression(cond);
  322. break;
  323. }
  324. case EVictoryConditionType::BUILDCITY:
  325. {
  326. EventExpression::OperatorAll oper;
  327. EventCondition cond(EventCondition::HAVE_BUILDING);
  328. cond.position = readInt3();
  329. cond.objectType = BuildingID::VILLAGE_HALL + reader.readUInt8();
  330. oper.expressions.push_back(cond);
  331. cond.objectType = BuildingID::FORT + reader.readUInt8();
  332. oper.expressions.push_back(cond);
  333. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[283];
  334. specialVictory.onFulfill = VLC->generaltexth->allTexts[282];
  335. specialVictory.trigger = EventExpression(oper);
  336. break;
  337. }
  338. case EVictoryConditionType::BUILDGRAIL:
  339. {
  340. EventCondition cond(EventCondition::HAVE_BUILDING);
  341. cond.objectType = BuildingID::GRAIL;
  342. cond.position = readInt3();
  343. if(cond.position.z > 2)
  344. cond.position = int3(-1,-1,-1);
  345. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[285];
  346. specialVictory.onFulfill = VLC->generaltexth->allTexts[284];
  347. specialVictory.trigger = EventExpression(cond);
  348. break;
  349. }
  350. case EVictoryConditionType::BEATHERO:
  351. {
  352. EventCondition cond(EventCondition::DESTROY);
  353. cond.objectType = Obj::HERO;
  354. cond.position = readInt3();
  355. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[253];
  356. specialVictory.onFulfill = VLC->generaltexth->allTexts[252];
  357. specialVictory.trigger = EventExpression(cond);
  358. break;
  359. }
  360. case EVictoryConditionType::CAPTURECITY:
  361. {
  362. EventCondition cond(EventCondition::CONTROL);
  363. cond.objectType = Obj::TOWN;
  364. cond.position = readInt3();
  365. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[250];
  366. specialVictory.onFulfill = VLC->generaltexth->allTexts[249];
  367. specialVictory.trigger = EventExpression(cond);
  368. break;
  369. }
  370. case EVictoryConditionType::BEATMONSTER:
  371. {
  372. EventCondition cond(EventCondition::DESTROY);
  373. cond.objectType = Obj::MONSTER;
  374. cond.position = readInt3();
  375. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[287];
  376. specialVictory.onFulfill = VLC->generaltexth->allTexts[286];
  377. specialVictory.trigger = EventExpression(cond);
  378. break;
  379. }
  380. case EVictoryConditionType::TAKEDWELLINGS:
  381. {
  382. EventExpression::OperatorAll oper;
  383. oper.expressions.push_back(EventCondition(EventCondition::CONTROL, 0, Obj::CREATURE_GENERATOR1));
  384. oper.expressions.push_back(EventCondition(EventCondition::CONTROL, 0, Obj::CREATURE_GENERATOR4));
  385. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[289];
  386. specialVictory.onFulfill = VLC->generaltexth->allTexts[288];
  387. specialVictory.trigger = EventExpression(oper);
  388. break;
  389. }
  390. case EVictoryConditionType::TAKEMINES:
  391. {
  392. EventCondition cond(EventCondition::CONTROL);
  393. cond.objectType = Obj::MINE;
  394. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[291];
  395. specialVictory.onFulfill = VLC->generaltexth->allTexts[290];
  396. specialVictory.trigger = EventExpression(cond);
  397. break;
  398. }
  399. case EVictoryConditionType::TRANSPORTITEM:
  400. {
  401. EventCondition cond(EventCondition::TRANSPORT);
  402. cond.objectType = reader.readUInt8();
  403. cond.position = readInt3();
  404. specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[293];
  405. specialVictory.onFulfill = VLC->generaltexth->allTexts[292];
  406. specialVictory.trigger = EventExpression(cond);
  407. break;
  408. }
  409. default:
  410. assert(0);
  411. }
  412. // if condition is human-only turn it into following construction: AllOf(human, condition)
  413. if (!appliesToAI)
  414. {
  415. EventExpression::OperatorAll oper;
  416. EventCondition notAI(EventCondition::IS_HUMAN);
  417. notAI.value = 1;
  418. oper.expressions.push_back(notAI);
  419. oper.expressions.push_back(specialVictory.trigger.get());
  420. specialVictory.trigger = EventExpression(oper);
  421. }
  422. // if normal victory allowed - add one more quest
  423. if (allowNormalVictory)
  424. {
  425. mapHeader->victoryMessage += " / ";
  426. mapHeader->victoryMessage += VLC->generaltexth->victoryConditions[0];
  427. mapHeader->triggeredEvents.push_back(standardVictory);
  428. }
  429. mapHeader->triggeredEvents.push_back(specialVictory);
  430. }
  431. // Read loss conditions
  432. auto lossCond = (ELossConditionType::ELossConditionType)reader.readUInt8();
  433. if (lossCond == ELossConditionType::LOSSSTANDARD)
  434. {
  435. mapHeader->defeatIconIndex = 3;
  436. mapHeader->defeatMessage = VLC->generaltexth->lossCondtions[0];
  437. }
  438. else
  439. {
  440. TriggeredEvent specialDefeat;
  441. specialDefeat.effect.type = EventEffect::DEFEAT;
  442. specialDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
  443. specialDefeat.identifier = "specialDefeat";
  444. specialDefeat.description = ""; // TODO: display in quest window
  445. mapHeader->defeatIconIndex = ui16(lossCond);
  446. mapHeader->defeatMessage = VLC->generaltexth->lossCondtions[size_t(lossCond) + 1];
  447. switch(lossCond)
  448. {
  449. case ELossConditionType::LOSSCASTLE:
  450. {
  451. EventExpression::OperatorNone noneOf;
  452. EventCondition cond(EventCondition::CONTROL);
  453. cond.objectType = Obj::TOWN;
  454. cond.position = readInt3();
  455. noneOf.expressions.push_back(cond);
  456. specialDefeat.onFulfill = VLC->generaltexth->allTexts[251];
  457. specialDefeat.trigger = EventExpression(noneOf);
  458. break;
  459. }
  460. case ELossConditionType::LOSSHERO:
  461. {
  462. EventExpression::OperatorNone noneOf;
  463. EventCondition cond(EventCondition::CONTROL);
  464. cond.objectType = Obj::HERO;
  465. cond.position = readInt3();
  466. noneOf.expressions.push_back(cond);
  467. specialDefeat.onFulfill = VLC->generaltexth->allTexts[253];
  468. specialDefeat.trigger = EventExpression(noneOf);
  469. break;
  470. }
  471. case ELossConditionType::TIMEEXPIRES:
  472. {
  473. EventCondition cond(EventCondition::DAYS_PASSED);
  474. cond.value = reader.readUInt16();
  475. specialDefeat.onFulfill = VLC->generaltexth->allTexts[254];
  476. specialDefeat.trigger = EventExpression(cond);
  477. break;
  478. }
  479. }
  480. // turn simple loss condition into complete one that can be evaluated later:
  481. // - any of :
  482. // - days without town: 7
  483. // - all of:
  484. // - is human
  485. // - (expression)
  486. EventExpression::OperatorAll allOf;
  487. EventCondition isHuman(EventCondition::IS_HUMAN);
  488. isHuman.value = 1;
  489. allOf.expressions.push_back(isHuman);
  490. allOf.expressions.push_back(specialDefeat.trigger.get());
  491. specialDefeat.trigger = EventExpression(allOf);
  492. mapHeader->triggeredEvents.push_back(specialDefeat);
  493. }
  494. mapHeader->triggeredEvents.push_back(standardDefeat);
  495. }
  496. void CMapLoaderH3M::readTeamInfo()
  497. {
  498. mapHeader->howManyTeams = reader.readUInt8();
  499. if(mapHeader->howManyTeams > 0)
  500. {
  501. // Teams
  502. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
  503. {
  504. mapHeader->players[i].team = TeamID(reader.readUInt8());
  505. }
  506. }
  507. else
  508. {
  509. // No alliances
  510. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  511. {
  512. if(mapHeader->players[i].canComputerPlay || mapHeader->players[i].canHumanPlay)
  513. {
  514. mapHeader->players[i].team = TeamID(mapHeader->howManyTeams++);
  515. }
  516. }
  517. }
  518. }
  519. void CMapLoaderH3M::readAllowedHeroes()
  520. {
  521. mapHeader->allowedHeroes.resize(VLC->heroh->heroes.size(), true);
  522. const int bytes = mapHeader->version == EMapFormat::ROE ? 16 : 20;
  523. readBitmask(mapHeader->allowedHeroes,bytes,GameConstants::HEROES_QUANTITY, false);
  524. // Probably reserved for further heroes
  525. if(mapHeader->version > EMapFormat::ROE)
  526. {
  527. int placeholdersQty = reader.readUInt32();
  528. for(int p = 0; p < placeholdersQty; ++p)
  529. {
  530. mapHeader->placeholdedHeroes.push_back(reader.readUInt8());
  531. }
  532. }
  533. }
  534. void CMapLoaderH3M::readDisposedHeroes()
  535. {
  536. // Reading disposed heroes (20 bytes)
  537. if(map->version >= EMapFormat::SOD)
  538. {
  539. ui8 disp = reader.readUInt8();
  540. map->disposedHeroes.resize(disp);
  541. for(int g = 0; g < disp; ++g)
  542. {
  543. map->disposedHeroes[g].heroId = reader.readUInt8();
  544. map->disposedHeroes[g].portrait = reader.readUInt8();
  545. map->disposedHeroes[g].name = reader.readString();
  546. map->disposedHeroes[g].players = reader.readUInt8();
  547. }
  548. }
  549. //omitting NULLS
  550. reader.skip(31);
  551. }
  552. void CMapLoaderH3M::readAllowedArtifacts()
  553. {
  554. map->allowedArtifact.resize (VLC->arth->artifacts.size(),true); //handle new artifacts, make them allowed by default
  555. // Reading allowed artifacts: 17 or 18 bytes
  556. if(map->version != EMapFormat::ROE)
  557. {
  558. const int bytes = map->version == EMapFormat::AB ? 17 : 18;
  559. readBitmask(map->allowedArtifact,bytes,GameConstants::ARTIFACTS_QUANTITY);
  560. }
  561. // ban combo artifacts
  562. if (map->version == EMapFormat::ROE || map->version == EMapFormat::AB)
  563. {
  564. for(CArtifact * artifact : VLC->arth->artifacts)
  565. {
  566. // combo
  567. if (artifact->constituents)
  568. {
  569. map->allowedArtifact[artifact->id] = false;
  570. }
  571. }
  572. if (map->version == EMapFormat::ROE)
  573. {
  574. map->allowedArtifact[ArtifactID::ARMAGEDDONS_BLADE] = false;
  575. }
  576. }
  577. // Messy, but needed
  578. for (TriggeredEvent & event : map->triggeredEvents)
  579. {
  580. auto patcher = [&](EventCondition cond) -> EventExpression::Variant
  581. {
  582. if (cond.condition == EventCondition::HAVE_ARTIFACT ||
  583. cond.condition == EventCondition::TRANSPORT)
  584. {
  585. map->allowedArtifact[cond.objectType] = false;
  586. }
  587. return cond;
  588. };
  589. event.trigger = event.trigger.morph(patcher);
  590. }
  591. }
  592. void CMapLoaderH3M::readAllowedSpellsAbilities()
  593. {
  594. // Read allowed spells, including new ones
  595. map->allowedSpell.resize(VLC->spellh->objects.size(), true);
  596. // Read allowed abilities
  597. map->allowedAbilities.resize(GameConstants::SKILL_QUANTITY, true);
  598. if(map->version >= EMapFormat::SOD)
  599. {
  600. // Reading allowed spells (9 bytes)
  601. const int spell_bytes = 9;
  602. readBitmask(map->allowedSpell, spell_bytes, GameConstants::SPELLS_QUANTITY);
  603. // Allowed hero's abilities (4 bytes)
  604. const int abil_bytes = 4;
  605. readBitmask(map->allowedAbilities, abil_bytes, GameConstants::SKILL_QUANTITY);
  606. }
  607. //do not generate special abilities and spells
  608. for (auto spell : VLC->spellh->objects)
  609. if (spell->isSpecialSpell() || spell->isCreatureAbility())
  610. map->allowedSpell[spell->id] = false;
  611. }
  612. void CMapLoaderH3M::readRumors()
  613. {
  614. int rumNr = reader.readUInt32();
  615. for(int it = 0; it < rumNr; it++)
  616. {
  617. Rumor ourRumor;
  618. ourRumor.name = reader.readString();
  619. ourRumor.text = reader.readString();
  620. map->rumors.push_back(ourRumor);
  621. }
  622. }
  623. void CMapLoaderH3M::readPredefinedHeroes()
  624. {
  625. switch(map->version)
  626. {
  627. case EMapFormat::WOG:
  628. case EMapFormat::SOD:
  629. {
  630. // Disposed heroes
  631. for(int z = 0; z < GameConstants::HEROES_QUANTITY; z++)
  632. {
  633. int custom = reader.readUInt8();
  634. if(!custom) continue;
  635. auto hero = new CGHeroInstance();
  636. hero->ID = Obj::HERO;
  637. hero->subID = z;
  638. bool hasExp = reader.readBool();
  639. if(hasExp)
  640. {
  641. hero->exp = reader.readUInt32();
  642. }
  643. else
  644. {
  645. hero->exp = 0;
  646. }
  647. bool hasSecSkills = reader.readBool();
  648. if(hasSecSkills)
  649. {
  650. int howMany = reader.readUInt32();
  651. hero->secSkills.resize(howMany);
  652. for(int yy = 0; yy < howMany; ++yy)
  653. {
  654. hero->secSkills[yy].first = SecondarySkill(reader.readUInt8());
  655. hero->secSkills[yy].second = reader.readUInt8();
  656. }
  657. }
  658. loadArtifactsOfHero(hero);
  659. bool hasCustomBio = reader.readBool();
  660. if(hasCustomBio)
  661. {
  662. hero->biography = reader.readString();
  663. }
  664. // 0xFF is default, 00 male, 01 female
  665. hero->sex = reader.readUInt8();
  666. bool hasCustomSpells = reader.readBool();
  667. if(hasCustomSpells)
  668. {
  669. readSpells(hero->spells);
  670. }
  671. bool hasCustomPrimSkills = reader.readBool();
  672. if(hasCustomPrimSkills)
  673. {
  674. for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; xx++)
  675. {
  676. hero->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader.readUInt8());
  677. }
  678. }
  679. map->predefinedHeroes.push_back(hero);
  680. }
  681. break;
  682. }
  683. case EMapFormat::ROE:
  684. break;
  685. }
  686. }
  687. void CMapLoaderH3M::loadArtifactsOfHero(CGHeroInstance * hero)
  688. {
  689. bool artSet = reader.readBool();
  690. // True if artifact set is not default (hero has some artifacts)
  691. if(artSet)
  692. {
  693. if(hero->artifactsWorn.size() || hero->artifactsInBackpack.size())
  694. {
  695. logGlobal->warnStream() << boost::format("Hero %s at %s has set artifacts twice (in map properties and on adventure map instance). Using the latter set...") % hero->name % hero->pos;
  696. hero->artifactsInBackpack.clear();
  697. while(hero->artifactsWorn.size())
  698. hero->eraseArtSlot(hero->artifactsWorn.begin()->first);
  699. }
  700. for(int pom = 0; pom < 16; pom++)
  701. {
  702. loadArtifactToSlot(hero, pom);
  703. }
  704. // misc5 art //17
  705. if(map->version >= EMapFormat::SOD)
  706. {
  707. assert(!hero->getArt(ArtifactPosition::MACH4));
  708. if(!loadArtifactToSlot(hero, ArtifactPosition::MACH4))
  709. {
  710. // catapult by default
  711. assert(!hero->getArt(ArtifactPosition::MACH4));
  712. hero->putArtifact(ArtifactPosition::MACH4, CArtifactInstance::createArtifact(map, ArtifactID::CATAPULT));
  713. }
  714. }
  715. loadArtifactToSlot(hero, ArtifactPosition::SPELLBOOK);
  716. // 19 //???what is that? gap in file or what? - it's probably fifth slot..
  717. if(map->version > EMapFormat::ROE)
  718. {
  719. loadArtifactToSlot(hero, ArtifactPosition::MISC5);
  720. }
  721. else
  722. {
  723. reader.skip(1);
  724. }
  725. // bag artifacts //20
  726. // number of artifacts in hero's bag
  727. int amount = reader.readUInt16();
  728. for(int ss = 0; ss < amount; ++ss)
  729. {
  730. loadArtifactToSlot(hero, GameConstants::BACKPACK_START + hero->artifactsInBackpack.size());
  731. }
  732. }
  733. }
  734. bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
  735. {
  736. const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff;
  737. int aid;
  738. if(map->version == EMapFormat::ROE)
  739. {
  740. aid = reader.readUInt8();
  741. }
  742. else
  743. {
  744. aid = reader.readUInt16();
  745. }
  746. bool isArt = aid != artmask;
  747. if(isArt)
  748. {
  749. if(vstd::contains(VLC->arth->bigArtifacts, aid) && slot >= GameConstants::BACKPACK_START)
  750. {
  751. logGlobal->warnStream() << "Warning: A big artifact (war machine) in hero's backpack, ignoring...";
  752. return false;
  753. }
  754. if(aid == 0 && slot == ArtifactPosition::MISC5)
  755. {
  756. //TODO: check how H3 handles it -> art 0 in slot 18 in AB map
  757. logGlobal->warnStream() << "Spellbook to MISC5 slot? Putting it spellbook place. AB format peculiarity ? (format "
  758. << static_cast<int>(map->version) << ")";
  759. slot = ArtifactPosition::SPELLBOOK;
  760. }
  761. // this is needed, because some H3M maps (last scenario of ROE map) contain invalid data like misplaced artifacts
  762. auto artifact = CArtifactInstance::createArtifact(map, aid);
  763. auto artifactPos = ArtifactPosition(slot);
  764. if (artifact->canBePutAt(ArtifactLocation(hero, artifactPos)))
  765. {
  766. hero->putArtifact(artifactPos, artifact);
  767. }
  768. else
  769. {
  770. logGlobal->debugStream() << "Artifact can't be put at the specified location."; //TODO add more debugging information
  771. }
  772. }
  773. return isArt;
  774. }
  775. void CMapLoaderH3M::readTerrain()
  776. {
  777. map->initTerrain();
  778. // Read terrain
  779. for(int a = 0; a < 2; ++a)
  780. {
  781. if(a == 1 && !map->twoLevel)
  782. {
  783. break;
  784. }
  785. for(int c = 0; c < map->width; c++)
  786. {
  787. for(int z = 0; z < map->height; z++)
  788. {
  789. auto & tile = map->getTile(int3(z, c, a));
  790. tile.terType = ETerrainType(reader.readUInt8());
  791. tile.terView = reader.readUInt8();
  792. tile.riverType = static_cast<ERiverType::ERiverType>(reader.readUInt8());
  793. tile.riverDir = reader.readUInt8();
  794. tile.roadType = static_cast<ERoadType::ERoadType>(reader.readUInt8());
  795. tile.roadDir = reader.readUInt8();
  796. tile.extTileFlags = reader.readUInt8();
  797. tile.blocked = ((tile.terType == ETerrainType::ROCK || tile.terType == ETerrainType::BORDER ) ? true : false); //underground tiles are always blocked
  798. tile.visitable = 0;
  799. }
  800. }
  801. }
  802. }
  803. void CMapLoaderH3M::readDefInfo()
  804. {
  805. int defAmount = reader.readUInt32();
  806. templates.reserve(defAmount);
  807. // Read custom defs
  808. for(int idd = 0; idd < defAmount; ++idd)
  809. {
  810. ObjectTemplate tmpl;
  811. tmpl.readMap(reader);
  812. templates.push_back(tmpl);
  813. }
  814. }
  815. void CMapLoaderH3M::readObjects()
  816. {
  817. int howManyObjs = reader.readUInt32();
  818. for(int ww = 0; ww < howManyObjs; ++ww)
  819. {
  820. CGObjectInstance * nobj = nullptr;
  821. int3 objPos = readInt3();
  822. int defnum = reader.readUInt32();
  823. ObjectInstanceID idToBeGiven = ObjectInstanceID(map->objects.size());
  824. ObjectTemplate & objTempl = templates.at(defnum);
  825. reader.skip(5);
  826. switch(objTempl.id)
  827. {
  828. case Obj::EVENT:
  829. {
  830. auto evnt = new CGEvent();
  831. nobj = evnt;
  832. readMessageAndGuards(evnt->message, evnt);
  833. evnt->gainedExp = reader.readUInt32();
  834. evnt->manaDiff = reader.readUInt32();
  835. evnt->moraleDiff = reader.readInt8();
  836. evnt->luckDiff = reader.readInt8();
  837. readResourses(evnt->resources);
  838. evnt->primskills.resize(GameConstants::PRIMARY_SKILLS);
  839. for(int x = 0; x < 4; ++x)
  840. {
  841. evnt->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
  842. }
  843. int gabn = reader.readUInt8(); // Number of gained abilities
  844. for(int oo = 0; oo < gabn; ++oo)
  845. {
  846. evnt->abilities.push_back(SecondarySkill(reader.readUInt8()));
  847. evnt->abilityLevels.push_back(reader.readUInt8());
  848. }
  849. int gart = reader.readUInt8(); // Number of gained artifacts
  850. for(int oo = 0; oo < gart; ++oo)
  851. {
  852. if(map->version == EMapFormat::ROE)
  853. {
  854. evnt->artifacts.push_back(ArtifactID(reader.readUInt8()));
  855. }
  856. else
  857. {
  858. evnt->artifacts.push_back(ArtifactID(reader.readUInt16()));
  859. }
  860. }
  861. int gspel = reader.readUInt8(); // Number of gained spells
  862. for(int oo = 0; oo < gspel; ++oo)
  863. {
  864. evnt->spells.push_back(SpellID(reader.readUInt8()));
  865. }
  866. int gcre = reader.readUInt8(); //number of gained creatures
  867. readCreatureSet(&evnt->creatures, gcre);
  868. reader.skip(8);
  869. evnt->availableFor = reader.readUInt8();
  870. evnt->computerActivate = reader.readUInt8();
  871. evnt->removeAfterVisit = reader.readUInt8();
  872. evnt->humanActivate = true;
  873. reader.skip(4);
  874. break;
  875. }
  876. case Obj::HERO:
  877. case Obj::RANDOM_HERO:
  878. case Obj::PRISON:
  879. {
  880. nobj = readHero(idToBeGiven, objPos);
  881. break;
  882. }
  883. case Obj::MONSTER: //Monster
  884. case Obj::RANDOM_MONSTER:
  885. case Obj::RANDOM_MONSTER_L1:
  886. case Obj::RANDOM_MONSTER_L2:
  887. case Obj::RANDOM_MONSTER_L3:
  888. case Obj::RANDOM_MONSTER_L4:
  889. case Obj::RANDOM_MONSTER_L5:
  890. case Obj::RANDOM_MONSTER_L6:
  891. case Obj::RANDOM_MONSTER_L7:
  892. {
  893. auto cre = new CGCreature();
  894. nobj = cre;
  895. if(map->version > EMapFormat::ROE)
  896. {
  897. cre->identifier = reader.readUInt32();
  898. map->questIdentifierToId[cre->identifier] = idToBeGiven;
  899. }
  900. auto hlp = new CStackInstance();
  901. hlp->count = reader.readUInt16();
  902. //type will be set during initialization
  903. cre->putStack(SlotID(0), hlp);
  904. cre->character = reader.readUInt8();
  905. bool hasMessage = reader.readBool();
  906. if(hasMessage)
  907. {
  908. cre->message = reader.readString();
  909. readResourses(cre->resources);
  910. int artID;
  911. if (map->version == EMapFormat::ROE)
  912. {
  913. artID = reader.readUInt8();
  914. }
  915. else
  916. {
  917. artID = reader.readUInt16();
  918. }
  919. if(map->version == EMapFormat::ROE)
  920. {
  921. if(artID != 0xff)
  922. {
  923. cre->gainedArtifact = ArtifactID(artID);
  924. }
  925. else
  926. {
  927. cre->gainedArtifact = ArtifactID::NONE;
  928. }
  929. }
  930. else
  931. {
  932. if(artID != 0xffff)
  933. {
  934. cre->gainedArtifact = ArtifactID(artID);
  935. }
  936. else
  937. {
  938. cre->gainedArtifact = ArtifactID::NONE;
  939. }
  940. }
  941. }
  942. cre->neverFlees = reader.readUInt8();
  943. cre->notGrowingTeam =reader.readUInt8();
  944. reader.skip(2);
  945. break;
  946. }
  947. case Obj::OCEAN_BOTTLE:
  948. case Obj::SIGN:
  949. {
  950. auto sb = new CGSignBottle();
  951. nobj = sb;
  952. sb->message = reader.readString();
  953. reader.skip(4);
  954. break;
  955. }
  956. case Obj::SEER_HUT:
  957. {
  958. nobj = readSeerHut();
  959. map->addQuest(nobj);
  960. break;
  961. }
  962. case Obj::WITCH_HUT:
  963. {
  964. auto wh = new CGWitchHut();
  965. nobj = wh;
  966. // in RoE we cannot specify it - all are allowed (I hope)
  967. if(map->version > EMapFormat::ROE)
  968. {
  969. for(int i = 0 ; i < 4; ++i)
  970. {
  971. ui8 c = reader.readUInt8();
  972. for(int yy = 0; yy < 8; ++yy)
  973. {
  974. if(i * 8 + yy < GameConstants::SKILL_QUANTITY)
  975. {
  976. if(c == (c | static_cast<ui8>(std::pow(2., yy))))
  977. {
  978. wh->allowedAbilities.push_back(i * 8 + yy);
  979. }
  980. }
  981. }
  982. }
  983. }
  984. else
  985. {
  986. // RoE map
  987. for(int gg = 0; gg < GameConstants::SKILL_QUANTITY; ++gg)
  988. {
  989. wh->allowedAbilities.push_back(gg);
  990. }
  991. }
  992. break;
  993. }
  994. case Obj::SCHOLAR:
  995. {
  996. auto sch = new CGScholar();
  997. nobj = sch;
  998. sch->bonusType = static_cast<CGScholar::EBonusType>(reader.readUInt8());
  999. sch->bonusID = reader.readUInt8();
  1000. reader.skip(6);
  1001. break;
  1002. }
  1003. case Obj::GARRISON:
  1004. case Obj::GARRISON2:
  1005. {
  1006. auto gar = new CGGarrison();
  1007. nobj = gar;
  1008. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1009. reader.skip(3);
  1010. readCreatureSet(gar, 7);
  1011. if(map->version > EMapFormat::ROE)
  1012. {
  1013. gar->removableUnits = reader.readBool();
  1014. }
  1015. else
  1016. {
  1017. gar->removableUnits = true;
  1018. }
  1019. reader.skip(8);
  1020. break;
  1021. }
  1022. case Obj::ARTIFACT:
  1023. case Obj::RANDOM_ART:
  1024. case Obj::RANDOM_TREASURE_ART:
  1025. case Obj::RANDOM_MINOR_ART:
  1026. case Obj::RANDOM_MAJOR_ART:
  1027. case Obj::RANDOM_RELIC_ART:
  1028. case Obj::SPELL_SCROLL:
  1029. {
  1030. int artID = ArtifactID::NONE; //random, set later
  1031. int spellID = -1;
  1032. auto art = new CGArtifact();
  1033. nobj = art;
  1034. readMessageAndGuards(art->message, art);
  1035. if(objTempl.id == Obj::SPELL_SCROLL)
  1036. {
  1037. spellID = reader.readUInt32();
  1038. artID = ArtifactID::SPELL_SCROLL;
  1039. }
  1040. else if(objTempl.id == Obj::ARTIFACT)
  1041. {
  1042. //specific artifact
  1043. artID = objTempl.subid;
  1044. }
  1045. art->storedArtifact = CArtifactInstance::createArtifact(map, artID, spellID);
  1046. break;
  1047. }
  1048. case Obj::RANDOM_RESOURCE:
  1049. case Obj::RESOURCE:
  1050. {
  1051. auto res = new CGResource();
  1052. nobj = res;
  1053. readMessageAndGuards(res->message, res);
  1054. res->amount = reader.readUInt32();
  1055. if(objTempl.subid == Res::GOLD)
  1056. {
  1057. // Gold is multiplied by 100.
  1058. res->amount *= 100;
  1059. }
  1060. reader.skip(4);
  1061. break;
  1062. }
  1063. case Obj::RANDOM_TOWN:
  1064. case Obj::TOWN:
  1065. {
  1066. nobj = readTown(objTempl.subid);
  1067. break;
  1068. }
  1069. case Obj::MINE:
  1070. case Obj::ABANDONED_MINE:
  1071. {
  1072. nobj = new CGMine();
  1073. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1074. reader.skip(3);
  1075. break;
  1076. }
  1077. case Obj::CREATURE_GENERATOR1:
  1078. case Obj::CREATURE_GENERATOR2:
  1079. case Obj::CREATURE_GENERATOR3:
  1080. case Obj::CREATURE_GENERATOR4:
  1081. {
  1082. nobj = new CGDwelling();
  1083. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1084. reader.skip(3);
  1085. break;
  1086. }
  1087. case Obj::SHRINE_OF_MAGIC_INCANTATION:
  1088. case Obj::SHRINE_OF_MAGIC_GESTURE:
  1089. case Obj::SHRINE_OF_MAGIC_THOUGHT:
  1090. {
  1091. auto shr = new CGShrine();
  1092. nobj = shr;
  1093. ui8 raw_id = reader.readUInt8();
  1094. if (255 == raw_id)
  1095. {
  1096. shr->spell = SpellID(SpellID::NONE);
  1097. }
  1098. else
  1099. {
  1100. shr->spell = SpellID(raw_id);
  1101. }
  1102. reader.skip(3);
  1103. break;
  1104. }
  1105. case Obj::PANDORAS_BOX:
  1106. {
  1107. auto box = new CGPandoraBox();
  1108. nobj = box;
  1109. readMessageAndGuards(box->message, box);
  1110. box->gainedExp = reader.readUInt32();
  1111. box->manaDiff = reader.readUInt32();
  1112. box->moraleDiff = reader.readInt8();
  1113. box->luckDiff = reader.readInt8();
  1114. readResourses(box->resources);
  1115. box->primskills.resize(GameConstants::PRIMARY_SKILLS);
  1116. for(int x = 0; x < 4; ++x)
  1117. {
  1118. box->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
  1119. }
  1120. int gabn = reader.readUInt8();//number of gained abilities
  1121. for(int oo = 0; oo < gabn; ++oo)
  1122. {
  1123. box->abilities.push_back(SecondarySkill(reader.readUInt8()));
  1124. box->abilityLevels.push_back(reader.readUInt8());
  1125. }
  1126. int gart = reader.readUInt8(); //number of gained artifacts
  1127. for(int oo = 0; oo < gart; ++oo)
  1128. {
  1129. if(map->version > EMapFormat::ROE)
  1130. {
  1131. box->artifacts.push_back(ArtifactID(reader.readUInt16()));
  1132. }
  1133. else
  1134. {
  1135. box->artifacts.push_back(ArtifactID(reader.readUInt8()));
  1136. }
  1137. }
  1138. int gspel = reader.readUInt8(); //number of gained spells
  1139. for(int oo = 0; oo < gspel; ++oo)
  1140. {
  1141. box->spells.push_back(SpellID(reader.readUInt8()));
  1142. }
  1143. int gcre = reader.readUInt8(); //number of gained creatures
  1144. readCreatureSet(&box->creatures, gcre);
  1145. reader.skip(8);
  1146. break;
  1147. }
  1148. case Obj::GRAIL:
  1149. {
  1150. map->grailPos = objPos;
  1151. map->grailRadius = reader.readUInt32();
  1152. continue;
  1153. }
  1154. case Obj::RANDOM_DWELLING: //same as castle + level range
  1155. case Obj::RANDOM_DWELLING_LVL: //same as castle, fixed level
  1156. case Obj::RANDOM_DWELLING_FACTION: //level range, fixed faction
  1157. {
  1158. nobj = new CGDwelling();
  1159. CSpecObjInfo * spec = nullptr;
  1160. switch(objTempl.id)
  1161. {
  1162. break; case Obj::RANDOM_DWELLING: spec = new CCreGenLeveledCastleInfo();
  1163. break; case Obj::RANDOM_DWELLING_LVL: spec = new CCreGenAsCastleInfo();
  1164. break; case Obj::RANDOM_DWELLING_FACTION: spec = new CCreGenLeveledInfo();
  1165. }
  1166. spec->player = PlayerColor(reader.readUInt32());
  1167. //216 and 217
  1168. if (auto castleSpec = dynamic_cast<CCreGenAsCastleInfo *>(spec))
  1169. {
  1170. castleSpec->identifier = reader.readUInt32();
  1171. if(!castleSpec->identifier)
  1172. {
  1173. castleSpec->asCastle = false;
  1174. castleSpec->castles[0] = reader.readUInt8();
  1175. castleSpec->castles[1] = reader.readUInt8();
  1176. }
  1177. else
  1178. {
  1179. castleSpec->asCastle = true;
  1180. }
  1181. }
  1182. //216 and 218
  1183. if (auto lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec))
  1184. {
  1185. lvlSpec->minLevel = std::max(reader.readUInt8(), ui8(1));
  1186. lvlSpec->maxLevel = std::min(reader.readUInt8(), ui8(7));
  1187. }
  1188. nobj->setOwner(spec->player);
  1189. static_cast<CGDwelling *>(nobj)->info = spec;
  1190. break;
  1191. }
  1192. case Obj::QUEST_GUARD:
  1193. {
  1194. auto guard = new CGQuestGuard();
  1195. map->addQuest(guard);
  1196. readQuest(guard);
  1197. nobj = guard;
  1198. break;
  1199. }
  1200. case Obj::SHIPYARD:
  1201. {
  1202. nobj = new CGShipyard();
  1203. nobj->setOwner(PlayerColor(reader.readUInt32()));
  1204. break;
  1205. }
  1206. case Obj::HERO_PLACEHOLDER: //hero placeholder
  1207. {
  1208. auto hp = new CGHeroPlaceholder();
  1209. nobj = hp;
  1210. hp->setOwner(PlayerColor(reader.readUInt8()));
  1211. int htid = reader.readUInt8();; //hero type id
  1212. nobj->subID = htid;
  1213. if(htid == 0xff)
  1214. {
  1215. hp->power = reader.readUInt8();
  1216. logGlobal->infoStream() << "Hero placeholder: by power at " << objPos;
  1217. }
  1218. else
  1219. {
  1220. logGlobal->infoStream() << "Hero placeholder: " << VLC->heroh->heroes[htid]->name << " at " << objPos;
  1221. hp->power = 0;
  1222. }
  1223. break;
  1224. }
  1225. case Obj::BORDERGUARD:
  1226. {
  1227. nobj = new CGBorderGuard();
  1228. map->addQuest(nobj);
  1229. break;
  1230. }
  1231. case Obj::BORDER_GATE:
  1232. {
  1233. nobj = new CGBorderGate();
  1234. map->addQuest (nobj);
  1235. break;
  1236. }
  1237. case Obj::PYRAMID: //Pyramid of WoG object
  1238. {
  1239. if(objTempl.subid == 0)
  1240. {
  1241. nobj = new CBank();
  1242. }
  1243. else
  1244. {
  1245. //WoG object
  1246. //TODO: possible special handling
  1247. nobj = new CGObjectInstance();
  1248. }
  1249. break;
  1250. }
  1251. case Obj::LIGHTHOUSE: //Lighthouse
  1252. {
  1253. nobj = new CGLighthouse();
  1254. nobj->tempOwner = PlayerColor(reader.readUInt32());
  1255. break;
  1256. }
  1257. default: //any other object
  1258. {
  1259. if (VLC->objtypeh->knownSubObjects(objTempl.id).count(objTempl.subid))
  1260. {
  1261. nobj = VLC->objtypeh->getHandlerFor(objTempl.id, objTempl.subid)->create(objTempl);
  1262. }
  1263. else
  1264. {
  1265. logGlobal->warnStream() << "Unrecognized object: " << objTempl.id << ":" << objTempl.subid << " at " << objPos
  1266. << " on map " << map->name;
  1267. nobj = new CGObjectInstance();
  1268. }
  1269. break;
  1270. }
  1271. }
  1272. nobj->pos = objPos;
  1273. nobj->ID = objTempl.id;
  1274. nobj->id = idToBeGiven;
  1275. if(nobj->ID != Obj::HERO && nobj->ID != Obj::HERO_PLACEHOLDER && nobj->ID != Obj::PRISON)
  1276. {
  1277. nobj->subID = objTempl.subid;
  1278. }
  1279. nobj->appearance = objTempl;
  1280. assert(idToBeGiven == ObjectInstanceID(map->objects.size()));
  1281. map->objects.push_back(nobj);
  1282. if(nobj->ID == Obj::TOWN)
  1283. {
  1284. map->towns.push_back(static_cast<CGTownInstance *>(nobj));
  1285. }
  1286. if(nobj->ID == Obj::HERO)
  1287. {
  1288. logGlobal->debugStream() << "Hero: " << VLC->heroh->heroes[nobj->subID]->name << " at " << objPos;
  1289. map->heroesOnMap.push_back(static_cast<CGHeroInstance*>(nobj));
  1290. }
  1291. }
  1292. std::sort(map->heroesOnMap.begin(), map->heroesOnMap.end(), [](const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)
  1293. {
  1294. return a->subID < b->subID;
  1295. });
  1296. }
  1297. void CMapLoaderH3M::readCreatureSet(CCreatureSet * out, int number)
  1298. {
  1299. const bool version = (map->version > EMapFormat::ROE);
  1300. const int maxID = version ? 0xffff : 0xff;
  1301. for(int ir = 0; ir < number; ++ir)
  1302. {
  1303. CreatureID creID;
  1304. int count;
  1305. if (version)
  1306. {
  1307. creID = CreatureID(reader.readUInt16());
  1308. }
  1309. else
  1310. {
  1311. creID = CreatureID(reader.readUInt8());
  1312. }
  1313. count = reader.readUInt16();
  1314. // Empty slot
  1315. if(creID == maxID)
  1316. continue;
  1317. auto hlp = new CStackInstance();
  1318. hlp->count = count;
  1319. if(creID > maxID - 0xf)
  1320. {
  1321. //this will happen when random object has random army
  1322. hlp->idRand = maxID - creID - 1;
  1323. }
  1324. else
  1325. {
  1326. hlp->setType(creID);
  1327. }
  1328. out->putStack(SlotID(ir), hlp);
  1329. }
  1330. out->validTypes(true);
  1331. }
  1332. CGObjectInstance * CMapLoaderH3M::readHero(ObjectInstanceID idToBeGiven, const int3 & initialPos)
  1333. {
  1334. auto nhi = new CGHeroInstance();
  1335. if(map->version > EMapFormat::ROE)
  1336. {
  1337. unsigned int identifier = reader.readUInt32();
  1338. map->questIdentifierToId[identifier] = idToBeGiven;
  1339. }
  1340. PlayerColor owner = PlayerColor(reader.readUInt8());
  1341. nhi->subID = reader.readUInt8();
  1342. assert(!nhi->getArt(ArtifactPosition::MACH4));
  1343. //If hero of this type has been predefined, use that as a base.
  1344. //Instance data will overwrite the predefined values where appropriate.
  1345. for(auto & elem : map->predefinedHeroes)
  1346. {
  1347. if(elem->subID == nhi->subID)
  1348. {
  1349. logGlobal->debugStream() << "Hero " << nhi->subID << " will be taken from the predefined heroes list.";
  1350. delete nhi;
  1351. nhi = elem;
  1352. break;
  1353. }
  1354. }
  1355. nhi->setOwner(owner);
  1356. nhi->portrait = nhi->subID;
  1357. for(auto & elem : map->disposedHeroes)
  1358. {
  1359. if(elem.heroId == nhi->subID)
  1360. {
  1361. nhi->name = elem.name;
  1362. nhi->portrait = elem.portrait;
  1363. break;
  1364. }
  1365. }
  1366. bool hasName = reader.readBool();
  1367. if(hasName)
  1368. {
  1369. nhi->name = reader.readString();
  1370. }
  1371. if(map->version > EMapFormat::AB)
  1372. {
  1373. bool hasExp = reader.readBool();
  1374. if(hasExp)
  1375. {
  1376. nhi->exp = reader.readUInt32();
  1377. }
  1378. else
  1379. {
  1380. nhi->exp = 0xffffffff;
  1381. }
  1382. }
  1383. else
  1384. {
  1385. nhi->exp = reader.readUInt32();
  1386. //0 means "not set" in <=AB maps
  1387. if(!nhi->exp)
  1388. {
  1389. nhi->exp = 0xffffffff;
  1390. }
  1391. }
  1392. bool hasPortrait = reader.readBool();
  1393. if(hasPortrait)
  1394. {
  1395. nhi->portrait = reader.readUInt8();
  1396. }
  1397. bool hasSecSkills = reader.readBool();
  1398. if(hasSecSkills)
  1399. {
  1400. if(nhi->secSkills.size())
  1401. {
  1402. nhi->secSkills.clear();
  1403. //logGlobal->warnStream() << boost::format("Hero %s subID=%d has set secondary skills twice (in map properties and on adventure map instance). Using the latter set...") % nhi->name % nhi->subID;
  1404. }
  1405. int howMany = reader.readUInt32();
  1406. nhi->secSkills.resize(howMany);
  1407. for(int yy = 0; yy < howMany; ++yy)
  1408. {
  1409. nhi->secSkills[yy].first = SecondarySkill(reader.readUInt8());
  1410. nhi->secSkills[yy].second = reader.readUInt8();
  1411. }
  1412. }
  1413. bool hasGarison = reader.readBool();
  1414. if(hasGarison)
  1415. {
  1416. readCreatureSet(nhi, 7);
  1417. }
  1418. nhi->formation = reader.readUInt8();
  1419. loadArtifactsOfHero(nhi);
  1420. nhi->patrol.patrolRadius = reader.readUInt8();
  1421. if(nhi->patrol.patrolRadius == 0xff)
  1422. {
  1423. nhi->patrol.patrolling = false;
  1424. }
  1425. else
  1426. {
  1427. nhi->patrol.patrolling = true;
  1428. nhi->patrol.initialPos = CGHeroInstance::convertPosition(initialPos, false);
  1429. }
  1430. if(map->version > EMapFormat::ROE)
  1431. {
  1432. bool hasCustomBiography = reader.readBool();
  1433. if(hasCustomBiography)
  1434. {
  1435. nhi->biography = reader.readString();
  1436. }
  1437. nhi->sex = reader.readUInt8();
  1438. // Remove trash
  1439. if (nhi->sex != 0xFF)
  1440. {
  1441. nhi->sex &= 1;
  1442. }
  1443. }
  1444. else
  1445. {
  1446. nhi->sex = 0xFF;
  1447. }
  1448. // Spells
  1449. if(map->version > EMapFormat::AB)
  1450. {
  1451. bool hasCustomSpells = reader.readBool();
  1452. if(nhi->spells.size())
  1453. {
  1454. nhi->clear();
  1455. logGlobal->warnStream() << boost::format("Hero %s subID=%d has spells set twice (in map properties and on adventure map instance). Using the latter set...") % nhi->name % nhi->subID;
  1456. }
  1457. if(hasCustomSpells)
  1458. {
  1459. nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"
  1460. readSpells(nhi->spells);
  1461. }
  1462. }
  1463. else if(map->version == EMapFormat::AB)
  1464. {
  1465. //we can read one spell
  1466. ui8 buff = reader.readUInt8();
  1467. if(buff != 254)
  1468. {
  1469. nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"
  1470. if(buff < 254) //255 means no spells
  1471. {
  1472. nhi->spells.insert(SpellID(buff));
  1473. }
  1474. }
  1475. }
  1476. if(map->version > EMapFormat::AB)
  1477. {
  1478. bool hasCustomPrimSkills = reader.readBool();
  1479. if(hasCustomPrimSkills)
  1480. {
  1481. auto ps = nhi->getAllBonuses(Selector::type(Bonus::PRIMARY_SKILL)
  1482. .And(Selector::sourceType(Bonus::HERO_BASE_SKILL)), nullptr);
  1483. if(ps->size())
  1484. {
  1485. logGlobal->warnStream() << boost::format("Hero %s subID=%d has set primary skills twice (in map properties and on adventure map instance). Using the latter set...") % nhi->name % nhi->subID;
  1486. for(auto b : *ps)
  1487. nhi->removeBonus(b);
  1488. }
  1489. for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; ++xx)
  1490. {
  1491. nhi->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader.readUInt8());
  1492. }
  1493. }
  1494. }
  1495. reader.skip(16);
  1496. return nhi;
  1497. }
  1498. CGSeerHut * CMapLoaderH3M::readSeerHut()
  1499. {
  1500. auto hut = new CGSeerHut();
  1501. if(map->version > EMapFormat::ROE)
  1502. {
  1503. readQuest(hut);
  1504. }
  1505. else
  1506. {
  1507. //RoE
  1508. int artID = reader.readUInt8();
  1509. if (artID != 255)
  1510. {
  1511. //not none quest
  1512. hut->quest->m5arts.push_back (artID);
  1513. hut->quest->missionType = CQuest::MISSION_ART;
  1514. }
  1515. else
  1516. {
  1517. hut->quest->missionType = CQuest::MISSION_NONE;
  1518. }
  1519. hut->quest->lastDay = -1; //no timeout
  1520. hut->quest->isCustomFirst = hut->quest->isCustomNext = hut->quest->isCustomComplete = false;
  1521. }
  1522. if (hut->quest->missionType)
  1523. {
  1524. auto rewardType = static_cast<CGSeerHut::ERewardType>(reader.readUInt8());
  1525. hut->rewardType = rewardType;
  1526. switch(rewardType)
  1527. {
  1528. case CGSeerHut::EXPERIENCE:
  1529. {
  1530. hut->rVal = reader.readUInt32();
  1531. break;
  1532. }
  1533. case CGSeerHut::MANA_POINTS:
  1534. {
  1535. hut->rVal = reader.readUInt32();
  1536. break;
  1537. }
  1538. case CGSeerHut::MORALE_BONUS:
  1539. {
  1540. hut->rVal = reader.readUInt8();
  1541. break;
  1542. }
  1543. case CGSeerHut::LUCK_BONUS:
  1544. {
  1545. hut->rVal = reader.readUInt8();
  1546. break;
  1547. }
  1548. case CGSeerHut::RESOURCES:
  1549. {
  1550. hut->rID = reader.readUInt8();
  1551. // Only the first 3 bytes are used. Skip the 4th.
  1552. hut->rVal = reader.readUInt32() & 0x00ffffff;
  1553. break;
  1554. }
  1555. case CGSeerHut::PRIMARY_SKILL:
  1556. {
  1557. hut->rID = reader.readUInt8();
  1558. hut->rVal = reader.readUInt8();
  1559. break;
  1560. }
  1561. case CGSeerHut::SECONDARY_SKILL:
  1562. {
  1563. hut->rID = reader.readUInt8();
  1564. hut->rVal = reader.readUInt8();
  1565. break;
  1566. }
  1567. case CGSeerHut::ARTIFACT:
  1568. {
  1569. if (map->version == EMapFormat::ROE)
  1570. {
  1571. hut->rID = reader.readUInt8();
  1572. }
  1573. else
  1574. {
  1575. hut->rID = reader.readUInt16();
  1576. }
  1577. break;
  1578. }
  1579. case CGSeerHut::SPELL:
  1580. {
  1581. hut->rID = reader.readUInt8();
  1582. break;
  1583. }
  1584. case CGSeerHut::CREATURE:
  1585. {
  1586. if(map->version > EMapFormat::ROE)
  1587. {
  1588. hut->rID = reader.readUInt16();
  1589. hut->rVal = reader.readUInt16();
  1590. }
  1591. else
  1592. {
  1593. hut->rID = reader.readUInt8();
  1594. hut->rVal = reader.readUInt16();
  1595. }
  1596. break;
  1597. }
  1598. }
  1599. reader.skip(2);
  1600. }
  1601. else
  1602. {
  1603. // missionType==255
  1604. reader.skip(3);
  1605. }
  1606. return hut;
  1607. }
  1608. void CMapLoaderH3M::readQuest(IQuestObject * guard)
  1609. {
  1610. guard->quest->missionType = static_cast<CQuest::Emission>(reader.readUInt8());
  1611. switch(guard->quest->missionType)
  1612. {
  1613. case 0:
  1614. return;
  1615. case 2:
  1616. {
  1617. guard->quest->m2stats.resize(4);
  1618. for(int x = 0; x < 4; ++x)
  1619. {
  1620. guard->quest->m2stats[x] = reader.readUInt8();
  1621. }
  1622. }
  1623. break;
  1624. case 1:
  1625. case 3:
  1626. case 4:
  1627. {
  1628. guard->quest->m13489val = reader.readUInt32();
  1629. break;
  1630. }
  1631. case 5:
  1632. {
  1633. int artNumber = reader.readUInt8();
  1634. for(int yy = 0; yy < artNumber; ++yy)
  1635. {
  1636. int artid = reader.readUInt16();
  1637. guard->quest->m5arts.push_back(artid);
  1638. map->allowedArtifact[artid] = false; //these are unavailable for random generation
  1639. }
  1640. break;
  1641. }
  1642. case 6:
  1643. {
  1644. int typeNumber = reader.readUInt8();
  1645. guard->quest->m6creatures.resize(typeNumber);
  1646. for(int hh = 0; hh < typeNumber; ++hh)
  1647. {
  1648. guard->quest->m6creatures[hh].type = VLC->creh->creatures[reader.readUInt16()];
  1649. guard->quest->m6creatures[hh].count = reader.readUInt16();
  1650. }
  1651. break;
  1652. }
  1653. case 7:
  1654. {
  1655. guard->quest->m7resources.resize(7);
  1656. for(int x = 0; x < 7; ++x)
  1657. {
  1658. guard->quest->m7resources[x] = reader.readUInt32();
  1659. }
  1660. break;
  1661. }
  1662. case 8:
  1663. case 9:
  1664. {
  1665. guard->quest->m13489val = reader.readUInt8();
  1666. break;
  1667. }
  1668. }
  1669. int limit = reader.readUInt32();
  1670. if(limit == (static_cast<int>(0xffffffff)))
  1671. {
  1672. guard->quest->lastDay = -1;
  1673. }
  1674. else
  1675. {
  1676. guard->quest->lastDay = limit;
  1677. }
  1678. guard->quest->firstVisitText = reader.readString();
  1679. guard->quest->nextVisitText = reader.readString();
  1680. guard->quest->completedText = reader.readString();
  1681. guard->quest->isCustomFirst = guard->quest->firstVisitText.size() > 0;
  1682. guard->quest->isCustomNext = guard->quest->nextVisitText.size() > 0;
  1683. guard->quest->isCustomComplete = guard->quest->completedText.size() > 0;
  1684. }
  1685. CGTownInstance * CMapLoaderH3M::readTown(int castleID)
  1686. {
  1687. auto nt = new CGTownInstance();
  1688. if(map->version > EMapFormat::ROE)
  1689. {
  1690. nt->identifier = reader.readUInt32();
  1691. }
  1692. nt->tempOwner = PlayerColor(reader.readUInt8());
  1693. bool hasName = reader.readBool();
  1694. if(hasName)
  1695. {
  1696. nt->name = reader.readString();
  1697. }
  1698. bool hasGarrison = reader.readBool();
  1699. if(hasGarrison)
  1700. {
  1701. readCreatureSet(nt, 7);
  1702. }
  1703. nt->formation = reader.readUInt8();
  1704. bool hasCustomBuildings = reader.readBool();
  1705. if(hasCustomBuildings)
  1706. {
  1707. readBitmask(nt->builtBuildings,6,48,false);
  1708. readBitmask(nt->forbiddenBuildings,6,48,false);
  1709. nt->builtBuildings = convertBuildings(nt->builtBuildings, castleID);
  1710. nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings, castleID);
  1711. }
  1712. // Standard buildings
  1713. else
  1714. {
  1715. bool hasFort = reader.readBool();
  1716. if(hasFort)
  1717. {
  1718. nt->builtBuildings.insert(BuildingID::FORT);
  1719. }
  1720. //means that set of standard building should be included
  1721. nt->builtBuildings.insert(BuildingID::DEFAULT);
  1722. }
  1723. if(map->version > EMapFormat::ROE)
  1724. {
  1725. for(int i = 0; i < 9; ++i)
  1726. {
  1727. ui8 c = reader.readUInt8();
  1728. for(int yy = 0; yy < 8; ++yy)
  1729. {
  1730. if(i * 8 + yy < GameConstants::SPELLS_QUANTITY)
  1731. {
  1732. if(c == (c | static_cast<ui8>(std::pow(2., yy)))) //add obligatory spell even if it's banned on a map (?)
  1733. {
  1734. nt->obligatorySpells.push_back(SpellID(i * 8 + yy));
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }
  1740. for(int i = 0; i < 9; ++i)
  1741. {
  1742. ui8 c = reader.readUInt8();
  1743. for(int yy = 0; yy < 8; ++yy)
  1744. {
  1745. int spellid = i * 8 + yy;
  1746. if(spellid < GameConstants::SPELLS_QUANTITY)
  1747. {
  1748. if(c != (c | static_cast<ui8>(std::pow(2., yy))) && map->allowedSpell[spellid]) //add random spell only if it's allowed on entire map
  1749. {
  1750. nt->possibleSpells.push_back(SpellID(spellid));
  1751. }
  1752. }
  1753. }
  1754. }
  1755. //add all spells from mods
  1756. //TODO: allow customize new spells in towns
  1757. for (int i = SpellID::AFTER_LAST; i < VLC->spellh->objects.size(); ++i)
  1758. {
  1759. nt->possibleSpells.push_back(SpellID(i));
  1760. }
  1761. // Read castle events
  1762. int numberOfEvent = reader.readUInt32();
  1763. for(int gh = 0; gh < numberOfEvent; ++gh)
  1764. {
  1765. CCastleEvent nce;
  1766. nce.town = nt;
  1767. nce.name = reader.readString();
  1768. nce.message = reader.readString();
  1769. readResourses(nce.resources);
  1770. nce.players = reader.readUInt8();
  1771. if(map->version > EMapFormat::AB)
  1772. {
  1773. nce.humanAffected = reader.readUInt8();
  1774. }
  1775. else
  1776. {
  1777. nce.humanAffected = true;
  1778. }
  1779. nce.computerAffected = reader.readUInt8();
  1780. nce.firstOccurence = reader.readUInt16();
  1781. nce.nextOccurence = reader.readUInt8();
  1782. reader.skip(17);
  1783. // New buildings
  1784. readBitmask(nce.buildings,6,48,false);
  1785. nce.buildings = convertBuildings(nce.buildings, castleID, false);
  1786. nce.creatures.resize(7);
  1787. for(int vv = 0; vv < 7; ++vv)
  1788. {
  1789. nce.creatures[vv] = reader.readUInt16();
  1790. }
  1791. reader.skip(4);
  1792. nt->events.push_back(nce);
  1793. }
  1794. if(map->version > EMapFormat::AB)
  1795. {
  1796. nt->alignment = reader.readUInt8();
  1797. }
  1798. reader.skip(3);
  1799. return nt;
  1800. }
  1801. std::set<BuildingID> CMapLoaderH3M::convertBuildings(const std::set<BuildingID> h3m, int castleID, bool addAuxiliary /*= true*/)
  1802. {
  1803. std::map<int, BuildingID> mapa;
  1804. std::set<BuildingID> ret;
  1805. // Note: this file is parsed many times.
  1806. const JsonNode config(ResourceID("config/buildings5.json"));
  1807. for(const JsonNode & entry : config["table"].Vector())
  1808. {
  1809. int town = entry["town"].Float();
  1810. if (town == castleID || town == -1)
  1811. {
  1812. mapa[entry["h3"].Float()] = BuildingID((si32)entry["vcmi"].Float());
  1813. }
  1814. }
  1815. for(auto & elem : h3m)
  1816. {
  1817. if(mapa[elem] >= 0)
  1818. {
  1819. ret.insert(mapa[elem]);
  1820. }
  1821. // horde buildings
  1822. else if(mapa[elem] >= (-GameConstants::CREATURES_PER_TOWN))
  1823. {
  1824. int level = (mapa[elem]);
  1825. //(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns
  1826. ret.insert(BuildingID(level - 30));
  1827. }
  1828. else
  1829. {
  1830. logGlobal->warnStream() << "Conversion warning: unknown building " << elem << " in castle "
  1831. << castleID;
  1832. }
  1833. }
  1834. if(addAuxiliary)
  1835. {
  1836. //village hall is always present
  1837. ret.insert(BuildingID::VILLAGE_HALL);
  1838. }
  1839. if(ret.find(BuildingID::CITY_HALL) != ret.end())
  1840. {
  1841. ret.insert(BuildingID::EXTRA_CITY_HALL);
  1842. }
  1843. if(ret.find(BuildingID::TOWN_HALL) != ret.end())
  1844. {
  1845. ret.insert(BuildingID::EXTRA_TOWN_HALL);
  1846. }
  1847. if(ret.find(BuildingID::CAPITOL) != ret.end())
  1848. {
  1849. ret.insert(BuildingID::EXTRA_CAPITOL);
  1850. }
  1851. return ret;
  1852. }
  1853. void CMapLoaderH3M::readEvents()
  1854. {
  1855. int numberOfEvents = reader.readUInt32();
  1856. for(int yyoo = 0; yyoo < numberOfEvents; ++yyoo)
  1857. {
  1858. CMapEvent ne;
  1859. ne.name = reader.readString();
  1860. ne.message = reader.readString();
  1861. readResourses(ne.resources);
  1862. ne.players = reader.readUInt8();
  1863. if(map->version > EMapFormat::AB)
  1864. {
  1865. ne.humanAffected = reader.readUInt8();
  1866. }
  1867. else
  1868. {
  1869. ne.humanAffected = true;
  1870. }
  1871. ne.computerAffected = reader.readUInt8();
  1872. ne.firstOccurence = reader.readUInt16();
  1873. ne.nextOccurence = reader.readUInt8();
  1874. reader.skip(17);
  1875. map->events.push_back(ne);
  1876. }
  1877. }
  1878. void CMapLoaderH3M::readMessageAndGuards(std::string& message, CCreatureSet* guards)
  1879. {
  1880. bool hasMessage = reader.readBool();
  1881. if(hasMessage)
  1882. {
  1883. message = reader.readString();
  1884. bool hasGuards = reader.readBool();
  1885. if(hasGuards)
  1886. {
  1887. readCreatureSet(guards, 7);
  1888. }
  1889. reader.skip(4);
  1890. }
  1891. }
  1892. void CMapLoaderH3M::readSpells(std::set<SpellID>& dest)
  1893. {
  1894. readBitmask(dest,9,GameConstants::SPELLS_QUANTITY,false);
  1895. }
  1896. void CMapLoaderH3M::readResourses(TResources& resources)
  1897. {
  1898. resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?
  1899. for(int x = 0; x < 7; ++x)
  1900. {
  1901. resources[x] = reader.readUInt32();
  1902. }
  1903. }
  1904. template <class Indenifier>
  1905. void CMapLoaderH3M::readBitmask(std::set<Indenifier>& dest, const int byteCount, const int limit, bool negate)
  1906. {
  1907. std::vector<bool> temp;
  1908. temp.resize(limit,true);
  1909. readBitmask(temp, byteCount, limit, negate);
  1910. for(int i = 0; i< std::min(temp.size(), static_cast<size_t>(limit)); i++)
  1911. {
  1912. if(temp[i])
  1913. {
  1914. dest.insert(static_cast<Indenifier>(i));
  1915. }
  1916. // else
  1917. // {
  1918. // dest.erase(static_cast<Indenifier>(i));
  1919. // }
  1920. }
  1921. }
  1922. void CMapLoaderH3M::readBitmask(std::vector<bool>& dest, const int byteCount, const int limit, bool negate)
  1923. {
  1924. for(int byte = 0; byte < byteCount; ++byte)
  1925. {
  1926. const ui8 mask = reader.readUInt8();
  1927. for(int bit = 0; bit < 8; ++bit)
  1928. {
  1929. if(byte * 8 + bit < limit)
  1930. {
  1931. const bool flag = mask & (1 << bit);
  1932. if((negate && flag) || (!negate && !flag))
  1933. dest[byte * 8 + bit] = false;
  1934. }
  1935. }
  1936. }
  1937. }
  1938. ui8 CMapLoaderH3M::reverse(ui8 arg)
  1939. {
  1940. ui8 ret = 0;
  1941. for(int i = 0; i < 8; ++i)
  1942. {
  1943. if((arg & (1 << i)) >> i)
  1944. {
  1945. ret |= (128 >> i);
  1946. }
  1947. }
  1948. return ret;
  1949. }