MapFormatH3M.cpp 50 KB

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