MapFormatH3M.cpp 59 KB

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