MapFormatH3M.cpp 59 KB

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