MapFormatH3M.cpp 50 KB

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