MapFormatH3M.cpp 58 KB

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