MapFormatH3M.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  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/Filesystem.h"
  15. #include "CMap.h"
  16. #include "../CSpellHandler.h"
  17. #include "../CCreatureHandler.h"
  18. #include "../CHeroHandler.h"
  19. #include "../CObjectHandler.h"
  20. #include "../CDefObjInfoHandler.h"
  21. #include "../VCMI_Lib.h"
  22. const bool CMapLoaderH3M::IS_PROFILING_ENABLED = false;
  23. CMapLoaderH3M::CMapLoaderH3M(CInputStream * stream) : map(nullptr), reader(stream),inputStream(stream)
  24. {
  25. }
  26. CMapLoaderH3M::~CMapLoaderH3M()
  27. {
  28. }
  29. std::unique_ptr<CMap> CMapLoaderH3M::loadMap()
  30. {
  31. // Init map object by parsing the input buffer
  32. map = new CMap();
  33. mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));
  34. init();
  35. return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));;
  36. }
  37. std::unique_ptr<CMapHeader> CMapLoaderH3M::loadMapHeader()
  38. {
  39. // Read header
  40. mapHeader = make_unique<CMapHeader>();
  41. readHeader();
  42. return std::move(mapHeader);
  43. }
  44. void CMapLoaderH3M::init()
  45. {
  46. //FIXME: get rid of double input process
  47. si64 temp_size = inputStream->getSize();
  48. inputStream->seek(0);
  49. auto temp_buffer = new ui8[temp_size];
  50. inputStream->read(temp_buffer,temp_size);
  51. // Compute checksum
  52. boost::crc_32_type result;
  53. result.process_bytes(temp_buffer, temp_size);
  54. map->checksum = result.checksum();
  55. delete [] temp_buffer;
  56. inputStream->seek(0);
  57. CStopWatch sw;
  58. struct MapLoadingTime
  59. {
  60. std::string name;
  61. si64 time;
  62. MapLoadingTime(std::string name, si64 time) : name(name),
  63. time(time)
  64. {
  65. }
  66. };
  67. std::vector<MapLoadingTime> times;
  68. readHeader();
  69. times.push_back(MapLoadingTime("header", sw.getDiff()));
  70. map->allHeroes.resize(map->allowedHeroes.size());
  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(auto & elem : map->objects)
  91. {
  92. if(!elem->defInfo) continue;
  93. map->addBlockVisTiles(elem);
  94. }
  95. times.push_back(MapLoadingTime("blocked/visitable tiles", sw.getDiff()));
  96. // Print profiling times
  97. if(IS_PROFILING_ENABLED)
  98. {
  99. for(MapLoadingTime & mlt : times)
  100. {
  101. logGlobal->debugStream() << "\tReading " << mlt.name << " took " << mlt.time << " ms.";
  102. }
  103. }
  104. }
  105. void CMapLoaderH3M::readHeader()
  106. {
  107. // Check map for validity
  108. // Note: disabled, causes decompression of the entire file ( = SLOW)
  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].hasRandomHero = reader.readBool();
  205. mapHeader->players[i].mainCustomHeroId = reader.readUInt8();
  206. if(mapHeader->players[i].mainCustomHeroId != 0xff)
  207. {
  208. mapHeader->players[i].mainCustomHeroPortrait = reader.readUInt8();
  209. if (mapHeader->players[i].mainCustomHeroPortrait == 0xff)
  210. mapHeader->players[i].mainCustomHeroPortrait = -1; //correct 1-byte -1 (0xff) into 4-byte -1
  211. mapHeader->players[i].mainCustomHeroName = reader.readString();
  212. }
  213. else
  214. mapHeader->players[i].mainCustomHeroId = -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. for(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. auto 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. if(hero->artifactsWorn.size() || hero->artifactsInBackpack.size())
  507. {
  508. logGlobal->warnStream() << boost::format("Hero %s at %s has set artifacts twice (in map properties and on adventure map instance). Using the latter set...") % hero->name % hero->pos;
  509. hero->artifactsInBackpack.clear();
  510. while(hero->artifactsWorn.size())
  511. hero->eraseArtSlot(hero->artifactsWorn.begin()->first);
  512. }
  513. for(int pom = 0; pom < 16; pom++)
  514. {
  515. loadArtifactToSlot(hero, pom);
  516. }
  517. // misc5 art //17
  518. if(map->version >= EMapFormat::SOD)
  519. {
  520. assert(!hero->getArt(ArtifactPosition::MACH4));
  521. if(!loadArtifactToSlot(hero, ArtifactPosition::MACH4))
  522. {
  523. // catapult by default
  524. assert(!hero->getArt(ArtifactPosition::MACH4));
  525. hero->putArtifact(ArtifactPosition::MACH4, createArtifact(ArtifactID::CATAPULT));
  526. }
  527. }
  528. loadArtifactToSlot(hero, ArtifactPosition::SPELLBOOK);
  529. // 19 //???what is that? gap in file or what? - it's probably fifth slot..
  530. if(map->version > EMapFormat::ROE)
  531. {
  532. loadArtifactToSlot(hero, ArtifactPosition::MISC5);
  533. }
  534. else
  535. {
  536. reader.skip(1);
  537. }
  538. // bag artifacts //20
  539. // number of artifacts in hero's bag
  540. int amount = reader.readUInt16();
  541. for(int ss = 0; ss < amount; ++ss)
  542. {
  543. loadArtifactToSlot(hero, GameConstants::BACKPACK_START + hero->artifactsInBackpack.size());
  544. }
  545. }
  546. }
  547. bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
  548. {
  549. const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff;
  550. int aid;
  551. if(map->version == EMapFormat::ROE)
  552. {
  553. aid = reader.readUInt8();
  554. }
  555. else
  556. {
  557. aid = reader.readUInt16();
  558. }
  559. bool isArt = aid != artmask;
  560. if(isArt)
  561. {
  562. if(vstd::contains(VLC->arth->bigArtifacts, aid) && slot >= GameConstants::BACKPACK_START)
  563. {
  564. logGlobal->warnStream() << "Warning: A big artifact (war machine) in hero's backpack, ignoring...";
  565. return false;
  566. }
  567. if(aid == 0 && slot == ArtifactPosition::MISC5)
  568. {
  569. //TODO: check how H3 handles it -> art 0 in slot 18 in AB map
  570. logGlobal->warnStream() << "Spellbook to MISC5 slot? Putting it spellbook place. AB format peculiarity ? (format "
  571. << static_cast<int>(map->version) << ")";
  572. slot = ArtifactPosition::SPELLBOOK;
  573. }
  574. hero->putArtifact(ArtifactPosition(slot), createArtifact(aid));
  575. }
  576. return isArt;
  577. }
  578. CArtifactInstance * CMapLoaderH3M::createArtifact(int aid, int spellID /*= -1*/)
  579. {
  580. CArtifactInstance * a = nullptr;
  581. if(aid >= 0)
  582. {
  583. if(spellID < 0)
  584. {
  585. a = CArtifactInstance::createNewArtifactInstance(aid);
  586. }
  587. else
  588. {
  589. a = CArtifactInstance::createScroll(SpellID(spellID).toSpell());
  590. }
  591. }
  592. else //FIXME: create combined artifact instance for random combined artifacts, just in case
  593. {
  594. a = new CArtifactInstance(); //random, empty
  595. }
  596. map->addNewArtifactInstance(a);
  597. //TODO make it nicer
  598. if(a->artType && (!!a->artType->constituents))
  599. {
  600. CCombinedArtifactInstance * comb = dynamic_cast<CCombinedArtifactInstance *>(a);
  601. for(CCombinedArtifactInstance::ConstituentInfo & ci : comb->constituentsInfo)
  602. {
  603. map->addNewArtifactInstance(ci.art);
  604. }
  605. }
  606. return a;
  607. }
  608. void CMapLoaderH3M::readTerrain()
  609. {
  610. map->initTerrain();
  611. // Read terrain
  612. for(int a = 0; a < 2; ++a)
  613. {
  614. if(a == 1 && !map->twoLevel)
  615. {
  616. break;
  617. }
  618. for(int c = 0; c < map->width; c++)
  619. {
  620. for(int z = 0; z < map->height; z++)
  621. {
  622. auto & tile = map->getTile(int3(z, c, a));
  623. tile.terType = ETerrainType(reader.readUInt8());
  624. tile.terView = reader.readUInt8();
  625. tile.riverType = static_cast<ERiverType::ERiverType>(reader.readUInt8());
  626. tile.riverDir = reader.readUInt8();
  627. tile.roadType = static_cast<ERoadType::ERoadType>(reader.readUInt8());
  628. tile.roadDir = reader.readUInt8();
  629. tile.extTileFlags = reader.readUInt8();
  630. tile.blocked = (tile.terType == ETerrainType::ROCK ? 1 : 0); //underground tiles are always blocked
  631. tile.visitable = 0;
  632. }
  633. }
  634. }
  635. }
  636. void CMapLoaderH3M::readDefInfo()
  637. {
  638. int defAmount = reader.readUInt32();
  639. map->customDefs.reserve(defAmount + 8);
  640. // Read custom defs
  641. for(int idd = 0; idd < defAmount; ++idd)
  642. {
  643. auto defInfo = new CGDefInfo();
  644. defInfo->name = reader.readString();
  645. std::transform(defInfo->name.begin(),defInfo->name.end(),defInfo->name.begin(),(int(*)(int))toupper);
  646. ui8 bytes[12];
  647. for(auto & byte : bytes)
  648. {
  649. byte = reader.readUInt8();
  650. }
  651. defInfo->terrainAllowed = reader.readUInt16();
  652. defInfo->terrainMenu = reader.readUInt16();
  653. defInfo->id = Obj(reader.readUInt32());
  654. defInfo->subid = reader.readUInt32();
  655. defInfo->type = reader.readUInt8();
  656. defInfo->printPriority = reader.readUInt8();
  657. for(int zi = 0; zi < 6; ++zi)
  658. {
  659. defInfo->blockMap[zi] = reverse(bytes[zi]);
  660. }
  661. for(int zi = 0; zi < 6; ++zi)
  662. {
  663. defInfo->visitMap[zi] = reverse(bytes[6 + zi]);
  664. }
  665. reader.skip(16);
  666. if(defInfo->id != Obj::HERO && defInfo->id != Obj::RANDOM_HERO)
  667. {
  668. CGDefInfo * h = VLC->dobjinfo->gobjs[defInfo->id][defInfo->subid];
  669. if(!h)
  670. {
  671. //remove fake entry
  672. VLC->dobjinfo->gobjs[defInfo->id].erase(defInfo->subid);
  673. if(VLC->dobjinfo->gobjs[defInfo->id].size())
  674. {
  675. VLC->dobjinfo->gobjs.erase(defInfo->id);
  676. }
  677. logGlobal->warnStream() << "\t\tWarning: no defobjinfo entry for object ID="
  678. << defInfo->id << " subID=" << defInfo->subid;
  679. }
  680. else
  681. {
  682. defInfo->visitDir = VLC->dobjinfo->gobjs[defInfo->id][defInfo->subid]->visitDir;
  683. }
  684. }
  685. else
  686. {
  687. defInfo->visitDir = 0xff;
  688. }
  689. if(defInfo->id == Obj::EVENT)
  690. {
  691. std::memset(defInfo->blockMap, 255, 6);
  692. }
  693. //calculating coverageMap
  694. defInfo->fetchInfoFromMSK();
  695. map->customDefs.push_back(defInfo);
  696. }
  697. //add holes - they always can appear
  698. for(int i = 0; i < 8 ; ++i)
  699. {
  700. map->customDefs.push_back(VLC->dobjinfo->gobjs[Obj::HOLE][i]);
  701. }
  702. }
  703. void CMapLoaderH3M::readObjects()
  704. {
  705. int howManyObjs = reader.readUInt32();
  706. for(int ww = 0; ww < howManyObjs; ++ww)
  707. {
  708. CGObjectInstance * nobj = nullptr;
  709. int3 objPos = readInt3();
  710. int defnum = reader.readUInt32();
  711. ObjectInstanceID idToBeGiven = ObjectInstanceID(map->objects.size());
  712. CGDefInfo * defInfo = map->customDefs.at(defnum);
  713. reader.skip(5);
  714. switch(defInfo->id)
  715. {
  716. case Obj::EVENT:
  717. {
  718. auto evnt = new CGEvent();
  719. nobj = evnt;
  720. readMessageAndGuards(evnt->message, evnt);
  721. evnt->gainedExp = reader.readUInt32();
  722. evnt->manaDiff = reader.readUInt32();
  723. evnt->moraleDiff = reader.readInt8();
  724. evnt->luckDiff = reader.readInt8();
  725. readResourses(evnt->resources);
  726. evnt->primskills.resize(GameConstants::PRIMARY_SKILLS);
  727. for(int x = 0; x < 4; ++x)
  728. {
  729. evnt->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
  730. }
  731. int gabn = reader.readUInt8(); // Number of gained abilities
  732. for(int oo = 0; oo < gabn; ++oo)
  733. {
  734. evnt->abilities.push_back(SecondarySkill(reader.readUInt8()));
  735. evnt->abilityLevels.push_back(reader.readUInt8());
  736. }
  737. int gart = reader.readUInt8(); // Number of gained artifacts
  738. for(int oo = 0; oo < gart; ++oo)
  739. {
  740. if(map->version == EMapFormat::ROE)
  741. {
  742. evnt->artifacts.push_back(ArtifactID(reader.readUInt8()));
  743. }
  744. else
  745. {
  746. evnt->artifacts.push_back(ArtifactID(reader.readUInt16()));
  747. }
  748. }
  749. int gspel = reader.readUInt8(); // Number of gained spells
  750. for(int oo = 0; oo < gspel; ++oo)
  751. {
  752. evnt->spells.push_back(SpellID(reader.readUInt8()));
  753. }
  754. int gcre = reader.readUInt8(); //number of gained creatures
  755. readCreatureSet(&evnt->creatures, gcre);
  756. reader.skip(8);
  757. evnt->availableFor = reader.readUInt8();
  758. evnt->computerActivate = reader.readUInt8();
  759. evnt->removeAfterVisit = reader.readUInt8();
  760. evnt->humanActivate = true;
  761. reader.skip(4);
  762. break;
  763. }
  764. case Obj::HERO:
  765. case Obj::RANDOM_HERO:
  766. case Obj::PRISON:
  767. {
  768. nobj = readHero(idToBeGiven);
  769. break;
  770. }
  771. case Obj::ARENA:
  772. case Obj::MERCENARY_CAMP:
  773. case Obj::MARLETTO_TOWER:
  774. case Obj::STAR_AXIS:
  775. case Obj::GARDEN_OF_REVELATION:
  776. case Obj::LEARNING_STONE:
  777. case Obj::TREE_OF_KNOWLEDGE:
  778. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  779. case Obj::SCHOOL_OF_MAGIC:
  780. case Obj::SCHOOL_OF_WAR:
  781. {
  782. nobj = new CGVisitableOPH();
  783. break;
  784. }
  785. case Obj::MYSTICAL_GARDEN:
  786. case Obj::WINDMILL:
  787. case Obj::WATER_WHEEL:
  788. {
  789. nobj = new CGVisitableOPW();
  790. break;
  791. }
  792. case Obj::MONOLITH1:
  793. case Obj::MONOLITH2:
  794. case Obj::MONOLITH3:
  795. case Obj::SUBTERRANEAN_GATE:
  796. case Obj::WHIRLPOOL:
  797. {
  798. nobj = new CGTeleport();
  799. break;
  800. }
  801. case Obj::CAMPFIRE:
  802. case Obj::FLOTSAM:
  803. case Obj::SEA_CHEST:
  804. case Obj::SHIPWRECK_SURVIVOR:
  805. {
  806. nobj = new CGPickable();
  807. break;
  808. }
  809. case Obj::TREASURE_CHEST:
  810. if(defInfo->subid == 0)
  811. {
  812. nobj = new CGPickable();
  813. }
  814. else
  815. {
  816. //WoG pickable object
  817. //TODO: possible special handling
  818. nobj = new CGObjectInstance();
  819. }
  820. break;
  821. case Obj::MONSTER: //Monster
  822. case Obj::RANDOM_MONSTER:
  823. case Obj::RANDOM_MONSTER_L1:
  824. case Obj::RANDOM_MONSTER_L2:
  825. case Obj::RANDOM_MONSTER_L3:
  826. case Obj::RANDOM_MONSTER_L4:
  827. case Obj::RANDOM_MONSTER_L5:
  828. case Obj::RANDOM_MONSTER_L6:
  829. case Obj::RANDOM_MONSTER_L7:
  830. {
  831. auto cre = new CGCreature();
  832. nobj = cre;
  833. if(map->version > EMapFormat::ROE)
  834. {
  835. cre->identifier = reader.readUInt32();
  836. map->questIdentifierToId[cre->identifier] = idToBeGiven;
  837. }
  838. auto hlp = new CStackInstance();
  839. hlp->count = reader.readUInt16();
  840. //type will be set during initialization
  841. cre->putStack(SlotID(0), hlp);
  842. cre->character = reader.readUInt8();
  843. bool hasMessage = reader.readBool();
  844. if(hasMessage)
  845. {
  846. cre->message = reader.readString();
  847. readResourses(cre->resources);
  848. int artID;
  849. if (map->version == EMapFormat::ROE)
  850. {
  851. artID = reader.readUInt8();
  852. }
  853. else
  854. {
  855. artID = reader.readUInt16();
  856. }
  857. if(map->version == EMapFormat::ROE)
  858. {
  859. if(artID != 0xff)
  860. {
  861. cre->gainedArtifact = ArtifactID(artID);
  862. }
  863. else
  864. {
  865. cre->gainedArtifact = ArtifactID::NONE;
  866. }
  867. }
  868. else
  869. {
  870. if(artID != 0xffff)
  871. {
  872. cre->gainedArtifact = ArtifactID(artID);
  873. }
  874. else
  875. {
  876. cre->gainedArtifact = ArtifactID::NONE;
  877. }
  878. }
  879. }
  880. cre->neverFlees = reader.readUInt8();
  881. cre->notGrowingTeam =reader.readUInt8();
  882. reader.skip(2);
  883. break;
  884. }
  885. case Obj::OCEAN_BOTTLE:
  886. case Obj::SIGN:
  887. {
  888. auto sb = new CGSignBottle();
  889. nobj = sb;
  890. sb->message = reader.readString();
  891. reader.skip(4);
  892. break;
  893. }
  894. case Obj::SEER_HUT:
  895. {
  896. nobj = readSeerHut();
  897. map->addQuest(nobj);
  898. break;
  899. }
  900. case Obj::WITCH_HUT:
  901. {
  902. auto wh = new CGWitchHut();
  903. nobj = wh;
  904. // in RoE we cannot specify it - all are allowed (I hope)
  905. if(map->version > EMapFormat::ROE)
  906. {
  907. for(int i = 0 ; i < 4; ++i)
  908. {
  909. ui8 c = reader.readUInt8();
  910. for(int yy = 0; yy < 8; ++yy)
  911. {
  912. if(i * 8 + yy < GameConstants::SKILL_QUANTITY)
  913. {
  914. if(c == (c | static_cast<ui8>(std::pow(2., yy))))
  915. {
  916. wh->allowedAbilities.push_back(i * 8 + yy);
  917. }
  918. }
  919. }
  920. }
  921. }
  922. else
  923. {
  924. // RoE map
  925. for(int gg = 0; gg < GameConstants::SKILL_QUANTITY; ++gg)
  926. {
  927. wh->allowedAbilities.push_back(gg);
  928. }
  929. }
  930. break;
  931. }
  932. case Obj::SCHOLAR:
  933. {
  934. auto sch = new CGScholar();
  935. nobj = sch;
  936. sch->bonusType = static_cast<CGScholar::EBonusType>(reader.readUInt8());
  937. sch->bonusID = reader.readUInt8();
  938. reader.skip(6);
  939. break;
  940. }
  941. case Obj::GARRISON:
  942. case Obj::GARRISON2:
  943. {
  944. auto gar = new CGGarrison();
  945. nobj = gar;
  946. nobj->setOwner(PlayerColor(reader.readUInt8()));
  947. reader.skip(3);
  948. readCreatureSet(gar, 7);
  949. if(map->version > EMapFormat::ROE)
  950. {
  951. gar->removableUnits = reader.readBool();
  952. }
  953. else
  954. {
  955. gar->removableUnits = true;
  956. }
  957. reader.skip(8);
  958. break;
  959. }
  960. case Obj::ARTIFACT:
  961. case Obj::RANDOM_ART:
  962. case Obj::RANDOM_TREASURE_ART:
  963. case Obj::RANDOM_MINOR_ART:
  964. case Obj::RANDOM_MAJOR_ART:
  965. case Obj::RANDOM_RELIC_ART:
  966. case Obj::SPELL_SCROLL:
  967. {
  968. int artID = ArtifactID::NONE; //random, set later
  969. int spellID = -1;
  970. auto art = new CGArtifact();
  971. nobj = art;
  972. readMessageAndGuards(art->message, art);
  973. if(defInfo->id == Obj::SPELL_SCROLL)
  974. {
  975. spellID = reader.readUInt32();
  976. artID = 1;
  977. }
  978. else if(defInfo->id == Obj::ARTIFACT)
  979. {
  980. //specific artifact
  981. artID = defInfo->subid;
  982. }
  983. art->storedArtifact = createArtifact(artID, spellID);
  984. break;
  985. }
  986. case Obj::RANDOM_RESOURCE:
  987. case Obj::RESOURCE:
  988. {
  989. auto res = new CGResource();
  990. nobj = res;
  991. readMessageAndGuards(res->message, res);
  992. res->amount = reader.readUInt32();
  993. if(defInfo->subid == Res::GOLD)
  994. {
  995. // Gold is multiplied by 100.
  996. res->amount *= 100;
  997. }
  998. reader.skip(4);
  999. break;
  1000. }
  1001. case Obj::RANDOM_TOWN:
  1002. case Obj::TOWN:
  1003. {
  1004. nobj = readTown(defInfo->subid);
  1005. break;
  1006. }
  1007. case Obj::MINE:
  1008. case Obj::ABANDONED_MINE:
  1009. {
  1010. nobj = new CGMine();
  1011. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1012. reader.skip(3);
  1013. break;
  1014. }
  1015. case Obj::CREATURE_GENERATOR1:
  1016. case Obj::CREATURE_GENERATOR2:
  1017. case Obj::CREATURE_GENERATOR3:
  1018. case Obj::CREATURE_GENERATOR4:
  1019. {
  1020. nobj = new CGDwelling();
  1021. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1022. reader.skip(3);
  1023. break;
  1024. }
  1025. case Obj::REFUGEE_CAMP:
  1026. case Obj::WAR_MACHINE_FACTORY:
  1027. {
  1028. nobj = new CGDwelling();
  1029. break;
  1030. }
  1031. case Obj::SHRINE_OF_MAGIC_INCANTATION:
  1032. case Obj::SHRINE_OF_MAGIC_GESTURE:
  1033. case Obj::SHRINE_OF_MAGIC_THOUGHT:
  1034. {
  1035. auto shr = new CGShrine();
  1036. nobj = shr;
  1037. ui8 raw_id = reader.readUInt8();
  1038. if (255 == raw_id)
  1039. {
  1040. shr->spell = SpellID(SpellID::NONE);
  1041. }
  1042. else
  1043. {
  1044. shr->spell = SpellID(raw_id);
  1045. }
  1046. reader.skip(3);
  1047. break;
  1048. }
  1049. case Obj::PANDORAS_BOX:
  1050. {
  1051. auto box = new CGPandoraBox();
  1052. nobj = box;
  1053. readMessageAndGuards(box->message, box);
  1054. box->gainedExp = reader.readUInt32();
  1055. box->manaDiff = reader.readUInt32();
  1056. box->moraleDiff = reader.readInt8();
  1057. box->luckDiff = reader.readInt8();
  1058. readResourses(box->resources);
  1059. box->primskills.resize(GameConstants::PRIMARY_SKILLS);
  1060. for(int x = 0; x < 4; ++x)
  1061. {
  1062. box->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
  1063. }
  1064. int gabn = reader.readUInt8();//number of gained abilities
  1065. for(int oo = 0; oo < gabn; ++oo)
  1066. {
  1067. box->abilities.push_back(SecondarySkill(reader.readUInt8()));
  1068. box->abilityLevels.push_back(reader.readUInt8());
  1069. }
  1070. int gart = reader.readUInt8(); //number of gained artifacts
  1071. for(int oo = 0; oo < gart; ++oo)
  1072. {
  1073. if(map->version > EMapFormat::ROE)
  1074. {
  1075. box->artifacts.push_back(ArtifactID(reader.readUInt16()));
  1076. }
  1077. else
  1078. {
  1079. box->artifacts.push_back(ArtifactID(reader.readUInt8()));
  1080. }
  1081. }
  1082. int gspel = reader.readUInt8(); //number of gained spells
  1083. for(int oo = 0; oo < gspel; ++oo)
  1084. {
  1085. box->spells.push_back(SpellID(reader.readUInt8()));
  1086. }
  1087. int gcre = reader.readUInt8(); //number of gained creatures
  1088. readCreatureSet(&box->creatures, gcre);
  1089. reader.skip(8);
  1090. break;
  1091. }
  1092. case Obj::GRAIL:
  1093. {
  1094. map->grailPos = objPos;
  1095. map->grailRadious = reader.readUInt32();
  1096. continue;
  1097. }
  1098. case Obj::RANDOM_DWELLING: //same as castle + level range
  1099. case Obj::RANDOM_DWELLING_LVL: //same as castle, fixed level
  1100. case Obj::RANDOM_DWELLING_FACTION: //level range, fixed faction
  1101. {
  1102. nobj = new CGDwelling();
  1103. CSpecObjInfo * spec = nullptr;
  1104. switch(defInfo->id)
  1105. {
  1106. break; case Obj::RANDOM_DWELLING: spec = new CCreGenLeveledCastleInfo();
  1107. break; case Obj::RANDOM_DWELLING_LVL: spec = new CCreGenAsCastleInfo();
  1108. break; case Obj::RANDOM_DWELLING_FACTION: spec = new CCreGenLeveledInfo();
  1109. }
  1110. spec->player = PlayerColor(reader.readUInt32());
  1111. //216 and 217
  1112. if (auto castleSpec = dynamic_cast<CCreGenAsCastleInfo *>(spec))
  1113. {
  1114. castleSpec->identifier = reader.readUInt32();
  1115. if(!castleSpec->identifier)
  1116. {
  1117. castleSpec->asCastle = false;
  1118. castleSpec->castles[0] = reader.readUInt8();
  1119. castleSpec->castles[1] = reader.readUInt8();
  1120. }
  1121. else
  1122. {
  1123. castleSpec->asCastle = true;
  1124. }
  1125. }
  1126. //216 and 218
  1127. if (auto lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec))
  1128. {
  1129. lvlSpec->minLevel = std::max(reader.readUInt8(), ui8(1));
  1130. lvlSpec->maxLevel = std::min(reader.readUInt8(), ui8(7));
  1131. }
  1132. nobj->setOwner(spec->player);
  1133. static_cast<CGDwelling *>(nobj)->info = spec;
  1134. break;
  1135. }
  1136. case Obj::QUEST_GUARD:
  1137. {
  1138. auto guard = new CGQuestGuard();
  1139. map->addQuest(guard);
  1140. readQuest(guard);
  1141. nobj = guard;
  1142. break;
  1143. }
  1144. case Obj::FAERIE_RING:
  1145. case Obj::SWAN_POND:
  1146. case Obj::IDOL_OF_FORTUNE:
  1147. case Obj::FOUNTAIN_OF_FORTUNE:
  1148. case Obj::RALLY_FLAG:
  1149. case Obj::OASIS:
  1150. case Obj::TEMPLE:
  1151. case Obj::WATERING_HOLE:
  1152. case Obj::FOUNTAIN_OF_YOUTH:
  1153. case Obj::BUOY:
  1154. case Obj::MERMAID:
  1155. case Obj::STABLES:
  1156. {
  1157. nobj = new CGBonusingObject();
  1158. break;
  1159. }
  1160. case Obj::MAGIC_WELL:
  1161. {
  1162. nobj = new CGMagicWell();
  1163. break;
  1164. }
  1165. case Obj::COVER_OF_DARKNESS:
  1166. case Obj::REDWOOD_OBSERVATORY:
  1167. case Obj::PILLAR_OF_FIRE:
  1168. {
  1169. nobj = new CGObservatory();
  1170. break;
  1171. }
  1172. case Obj::CORPSE:
  1173. case Obj::LEAN_TO:
  1174. case Obj::WAGON:
  1175. case Obj::WARRIORS_TOMB:
  1176. {
  1177. nobj = new CGOnceVisitable();
  1178. break;
  1179. }
  1180. case Obj::BOAT:
  1181. {
  1182. nobj = new CGBoat();
  1183. break;
  1184. }
  1185. case Obj::SIRENS:
  1186. {
  1187. nobj = new CGSirens();
  1188. break;
  1189. }
  1190. case Obj::SHIPYARD:
  1191. {
  1192. nobj = new CGShipyard();
  1193. nobj->setOwner(PlayerColor(reader.readUInt32()));
  1194. break;
  1195. }
  1196. case Obj::HERO_PLACEHOLDER: //hero placeholder
  1197. {
  1198. auto hp = new CGHeroPlaceholder();
  1199. nobj = hp;
  1200. hp->setOwner(PlayerColor(reader.readUInt8()));
  1201. int htid = reader.readUInt8();; //hero type id
  1202. nobj->subID = htid;
  1203. if(htid == 0xff)
  1204. {
  1205. hp->power = reader.readUInt8();
  1206. }
  1207. else
  1208. {
  1209. hp->power = 0;
  1210. }
  1211. break;
  1212. }
  1213. case Obj::KEYMASTER:
  1214. {
  1215. nobj = new CGKeymasterTent();
  1216. break;
  1217. }
  1218. case Obj::BORDERGUARD:
  1219. {
  1220. nobj = new CGBorderGuard();
  1221. map->addQuest(nobj);
  1222. break;
  1223. }
  1224. case Obj::BORDER_GATE:
  1225. {
  1226. nobj = new CGBorderGate();
  1227. map->addQuest (nobj);
  1228. break;
  1229. }
  1230. case Obj::EYE_OF_MAGI:
  1231. case Obj::HUT_OF_MAGI:
  1232. {
  1233. nobj = new CGMagi();
  1234. break;
  1235. }
  1236. case Obj::CREATURE_BANK:
  1237. case Obj::DERELICT_SHIP:
  1238. case Obj::DRAGON_UTOPIA:
  1239. case Obj::CRYPT:
  1240. case Obj::SHIPWRECK:
  1241. {
  1242. nobj = new CBank();
  1243. break;
  1244. }
  1245. case Obj::PYRAMID: //Pyramid of WoG object
  1246. {
  1247. if(defInfo->subid == 0)
  1248. {
  1249. nobj = new CGPyramid();
  1250. }
  1251. else
  1252. {
  1253. //WoG object
  1254. //TODO: possible special handling
  1255. nobj = new CGObjectInstance();
  1256. }
  1257. break;
  1258. }
  1259. case Obj::CARTOGRAPHER:
  1260. {
  1261. nobj = new CCartographer();
  1262. break;
  1263. }
  1264. case Obj::MAGIC_SPRING:
  1265. {
  1266. nobj = new CGMagicSpring();
  1267. break;
  1268. }
  1269. case Obj::DEN_OF_THIEVES:
  1270. {
  1271. nobj = new CGDenOfthieves();
  1272. break;
  1273. }
  1274. case Obj::OBELISK:
  1275. {
  1276. nobj = new CGObelisk();
  1277. break;
  1278. }
  1279. case Obj::LIGHTHOUSE: //Lighthouse
  1280. {
  1281. nobj = new CGLighthouse();
  1282. nobj->tempOwner = PlayerColor(reader.readUInt32());
  1283. break;
  1284. }
  1285. case Obj::ALTAR_OF_SACRIFICE:
  1286. case Obj::TRADING_POST:
  1287. case Obj::FREELANCERS_GUILD:
  1288. case Obj::TRADING_POST_SNOW:
  1289. {
  1290. nobj = new CGMarket();
  1291. break;
  1292. }
  1293. case Obj::UNIVERSITY:
  1294. {
  1295. nobj = new CGUniversity();
  1296. break;
  1297. }
  1298. case Obj::BLACK_MARKET:
  1299. {
  1300. nobj = new CGBlackMarket();
  1301. break;
  1302. }
  1303. default: //any other object
  1304. {
  1305. nobj = new CGObjectInstance();
  1306. break;
  1307. }
  1308. }
  1309. nobj->pos = objPos;
  1310. nobj->ID = defInfo->id;
  1311. nobj->id = idToBeGiven;
  1312. if(nobj->ID != Obj::HERO && nobj->ID != Obj::HERO_PLACEHOLDER && nobj->ID != Obj::PRISON)
  1313. {
  1314. nobj->subID = defInfo->subid;
  1315. }
  1316. nobj->defInfo = defInfo;
  1317. assert(idToBeGiven == ObjectInstanceID(map->objects.size()));
  1318. map->objects.push_back(nobj);
  1319. if(nobj->ID == Obj::TOWN)
  1320. {
  1321. map->towns.push_back(static_cast<CGTownInstance *>(nobj));
  1322. }
  1323. if(nobj->ID == Obj::HERO)
  1324. {
  1325. map->heroesOnMap.push_back(static_cast<CGHeroInstance*>(nobj));
  1326. }
  1327. }
  1328. std::sort(map->heroesOnMap.begin(), map->heroesOnMap.end(), [](const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)
  1329. {
  1330. return a->subID < b->subID;
  1331. });
  1332. }
  1333. void CMapLoaderH3M::readCreatureSet(CCreatureSet * out, int number)
  1334. {
  1335. const bool version = (map->version > EMapFormat::ROE);
  1336. const int maxID = version ? 0xffff : 0xff;
  1337. for(int ir = 0; ir < number; ++ir)
  1338. {
  1339. CreatureID creID;
  1340. int count;
  1341. if (version)
  1342. {
  1343. creID = CreatureID(reader.readUInt16());
  1344. }
  1345. else
  1346. {
  1347. creID = CreatureID(reader.readUInt8());
  1348. }
  1349. count = reader.readUInt16();
  1350. // Empty slot
  1351. if(creID == maxID) continue;
  1352. auto hlp = new CStackInstance();
  1353. hlp->count = count;
  1354. if(creID > maxID - 0xf)
  1355. {
  1356. //this will happen when random object has random army
  1357. creID = CreatureID(maxID + 1 - creID + VLC->creh->creatures.size());
  1358. hlp->idRand = creID;
  1359. }
  1360. else
  1361. {
  1362. hlp->setType(creID);
  1363. }
  1364. out->putStack(SlotID(ir), hlp);
  1365. }
  1366. out->validTypes(true);
  1367. }
  1368. CGObjectInstance * CMapLoaderH3M::readHero(ObjectInstanceID idToBeGiven)
  1369. {
  1370. auto nhi = new CGHeroInstance();
  1371. if(map->version > EMapFormat::ROE)
  1372. {
  1373. unsigned int identifier = reader.readUInt32();
  1374. map->questIdentifierToId[identifier] = idToBeGiven;
  1375. }
  1376. PlayerColor owner = PlayerColor(reader.readUInt8());
  1377. nhi->subID = reader.readUInt8();
  1378. assert(!nhi->getArt(ArtifactPosition::MACH4));
  1379. //If hero of this type has been predefined, use that as a base.
  1380. //Instance data will overwrite the predefined values where appropriate.
  1381. for(auto & elem : map->predefinedHeroes)
  1382. {
  1383. if(elem->subID == nhi->subID)
  1384. {
  1385. logGlobal->debugStream() << "Hero " << nhi->subID << " will be taken from the predefined heroes list.";
  1386. delete nhi;
  1387. nhi = elem;
  1388. break;
  1389. }
  1390. }
  1391. nhi->setOwner(owner);
  1392. nhi->portrait = nhi->subID;
  1393. for(auto & elem : map->disposedHeroes)
  1394. {
  1395. if(elem.heroId == nhi->subID)
  1396. {
  1397. nhi->name = elem.name;
  1398. nhi->portrait = elem.portrait;
  1399. break;
  1400. }
  1401. }
  1402. bool hasName = reader.readBool();
  1403. if(hasName)
  1404. {
  1405. nhi->name = reader.readString();
  1406. }
  1407. if(map->version > EMapFormat::AB)
  1408. {
  1409. bool hasExp = reader.readBool();
  1410. if(hasExp)
  1411. {
  1412. nhi->exp = reader.readUInt32();
  1413. }
  1414. else
  1415. {
  1416. nhi->exp = 0xffffffff;
  1417. }
  1418. }
  1419. else
  1420. {
  1421. nhi->exp = reader.readUInt32();
  1422. //0 means "not set" in <=AB maps
  1423. if(!nhi->exp)
  1424. {
  1425. nhi->exp = 0xffffffff;
  1426. }
  1427. }
  1428. bool hasPortrait = reader.readBool();
  1429. if(hasPortrait)
  1430. {
  1431. nhi->portrait = reader.readUInt8();
  1432. }
  1433. bool hasSecSkills = reader.readBool();
  1434. if(hasSecSkills)
  1435. {
  1436. if(nhi->secSkills.size())
  1437. {
  1438. nhi->secSkills.clear();
  1439. //logGlobal->warnStream() << boost::format("Hero %s subID=%d has set secondary skills twice (in map properties and on adventure map instance). Using the latter set...") % nhi->name % nhi->subID;
  1440. }
  1441. int howMany = reader.readUInt32();
  1442. nhi->secSkills.resize(howMany);
  1443. for(int yy = 0; yy < howMany; ++yy)
  1444. {
  1445. nhi->secSkills[yy].first = SecondarySkill(reader.readUInt8());
  1446. nhi->secSkills[yy].second = reader.readUInt8();
  1447. }
  1448. }
  1449. bool hasGarison = reader.readBool();
  1450. if(hasGarison)
  1451. {
  1452. readCreatureSet(nhi, 7);
  1453. }
  1454. nhi->formation = reader.readUInt8();
  1455. loadArtifactsOfHero(nhi);
  1456. nhi->patrol.patrolRadious = reader.readUInt8();
  1457. if(nhi->patrol.patrolRadious == 0xff)
  1458. {
  1459. nhi->patrol.patrolling = false;
  1460. }
  1461. else
  1462. {
  1463. nhi->patrol.patrolling = true;
  1464. }
  1465. if(map->version > EMapFormat::ROE)
  1466. {
  1467. bool hasCustomBiography = reader.readBool();
  1468. if(hasCustomBiography)
  1469. {
  1470. nhi->biography = reader.readString();
  1471. }
  1472. nhi->sex = reader.readUInt8();
  1473. // Remove trash
  1474. if (nhi->sex != 0xFF)
  1475. {
  1476. nhi->sex &= 1;
  1477. }
  1478. }
  1479. else
  1480. {
  1481. nhi->sex = 0xFF;
  1482. }
  1483. // Spells
  1484. if(map->version > EMapFormat::AB)
  1485. {
  1486. bool hasCustomSpells = reader.readBool();
  1487. if(nhi->spells.size())
  1488. {
  1489. nhi->clear();
  1490. logGlobal->warnStream() << boost::format("Hero %s subID=%d has spells set twice (in map properties and on adventure map instance). Using the latter set...") % nhi->name % nhi->subID;
  1491. }
  1492. if(hasCustomSpells)
  1493. {
  1494. nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"
  1495. readSpells(nhi->spells);
  1496. }
  1497. }
  1498. else if(map->version == EMapFormat::AB)
  1499. {
  1500. //we can read one spell
  1501. ui8 buff = reader.readUInt8();
  1502. if(buff != 254)
  1503. {
  1504. nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"
  1505. if(buff < 254) //255 means no spells
  1506. {
  1507. nhi->spells.insert(SpellID(buff));
  1508. }
  1509. }
  1510. }
  1511. if(map->version > EMapFormat::AB)
  1512. {
  1513. bool hasCustomPrimSkills = reader.readBool();
  1514. if(hasCustomPrimSkills)
  1515. {
  1516. auto ps = nhi->getAllBonuses(Selector::type(Bonus::PRIMARY_SKILL)
  1517. .And(Selector::sourceType(Bonus::HERO_BASE_SKILL)), nullptr);
  1518. if(ps->size())
  1519. {
  1520. logGlobal->warnStream() << boost::format("Hero %s subID=%d has set primary skills twice (in map properties and on adventure map instance). Using the latter set...") % nhi->name % nhi->subID;
  1521. for(auto b : *ps)
  1522. nhi->removeBonus(b);
  1523. }
  1524. for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; ++xx)
  1525. {
  1526. nhi->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader.readUInt8());
  1527. }
  1528. }
  1529. }
  1530. reader.skip(16);
  1531. return nhi;
  1532. }
  1533. CGSeerHut * CMapLoaderH3M::readSeerHut()
  1534. {
  1535. auto hut = new CGSeerHut();
  1536. if(map->version > EMapFormat::ROE)
  1537. {
  1538. readQuest(hut);
  1539. }
  1540. else
  1541. {
  1542. //RoE
  1543. int artID = reader.readUInt8();
  1544. if (artID != 255)
  1545. {
  1546. //not none quest
  1547. hut->quest->m5arts.push_back (artID);
  1548. hut->quest->missionType = CQuest::MISSION_ART;
  1549. }
  1550. else
  1551. {
  1552. hut->quest->missionType = CQuest::MISSION_NONE;
  1553. }
  1554. hut->quest->lastDay = -1; //no timeout
  1555. hut->quest->isCustomFirst = hut->quest->isCustomNext = hut->quest->isCustomComplete = false;
  1556. }
  1557. if (hut->quest->missionType)
  1558. {
  1559. auto rewardType = static_cast<CGSeerHut::ERewardType>(reader.readUInt8());
  1560. hut->rewardType = rewardType;
  1561. switch(rewardType)
  1562. {
  1563. case CGSeerHut::EXPERIENCE:
  1564. {
  1565. hut->rVal = reader.readUInt32();
  1566. break;
  1567. }
  1568. case CGSeerHut::MANA_POINTS:
  1569. {
  1570. hut->rVal = reader.readUInt32();
  1571. break;
  1572. }
  1573. case CGSeerHut::MORALE_BONUS:
  1574. {
  1575. hut->rVal = reader.readUInt8();
  1576. break;
  1577. }
  1578. case CGSeerHut::LUCK_BONUS:
  1579. {
  1580. hut->rVal = reader.readUInt8();
  1581. break;
  1582. }
  1583. case CGSeerHut::RESOURCES:
  1584. {
  1585. hut->rID = reader.readUInt8();
  1586. // Only the first 3 bytes are used. Skip the 4th.
  1587. hut->rVal = reader.readUInt32() & 0x00ffffff;
  1588. break;
  1589. }
  1590. case CGSeerHut::PRIMARY_SKILL:
  1591. {
  1592. hut->rID = reader.readUInt8();
  1593. hut->rVal = reader.readUInt8();
  1594. break;
  1595. }
  1596. case CGSeerHut::SECONDARY_SKILL:
  1597. {
  1598. hut->rID = reader.readUInt8();
  1599. hut->rVal = reader.readUInt8();
  1600. break;
  1601. }
  1602. case CGSeerHut::ARTIFACT:
  1603. {
  1604. if (map->version == EMapFormat::ROE)
  1605. {
  1606. hut->rID = reader.readUInt8();
  1607. }
  1608. else
  1609. {
  1610. hut->rID = reader.readUInt16();
  1611. }
  1612. break;
  1613. }
  1614. case CGSeerHut::SPELL:
  1615. {
  1616. hut->rID = reader.readUInt8();
  1617. break;
  1618. }
  1619. case CGSeerHut::CREATURE:
  1620. {
  1621. if(map->version > EMapFormat::ROE)
  1622. {
  1623. hut->rID = reader.readUInt16();
  1624. hut->rVal = reader.readUInt16();
  1625. }
  1626. else
  1627. {
  1628. hut->rID = reader.readUInt8();
  1629. hut->rVal = reader.readUInt16();
  1630. }
  1631. break;
  1632. }
  1633. }
  1634. reader.skip(2);
  1635. }
  1636. else
  1637. {
  1638. // missionType==255
  1639. reader.skip(3);
  1640. }
  1641. return hut;
  1642. }
  1643. void CMapLoaderH3M::readQuest(IQuestObject * guard)
  1644. {
  1645. guard->quest->missionType = static_cast<CQuest::Emission>(reader.readUInt8());
  1646. switch(guard->quest->missionType)
  1647. {
  1648. case 0:
  1649. return;
  1650. case 2:
  1651. {
  1652. guard->quest->m2stats.resize(4);
  1653. for(int x = 0; x < 4; ++x)
  1654. {
  1655. guard->quest->m2stats[x] = reader.readUInt8();
  1656. }
  1657. }
  1658. break;
  1659. case 1:
  1660. case 3:
  1661. case 4:
  1662. {
  1663. guard->quest->m13489val = reader.readUInt32();
  1664. break;
  1665. }
  1666. case 5:
  1667. {
  1668. int artNumber = reader.readUInt8();
  1669. for(int yy = 0; yy < artNumber; ++yy)
  1670. {
  1671. int artid = reader.readUInt16();
  1672. guard->quest->m5arts.push_back(artid);
  1673. map->allowedArtifact[artid] = false; //these are unavailable for random generation
  1674. }
  1675. break;
  1676. }
  1677. case 6:
  1678. {
  1679. int typeNumber = reader.readUInt8();
  1680. guard->quest->m6creatures.resize(typeNumber);
  1681. for(int hh = 0; hh < typeNumber; ++hh)
  1682. {
  1683. guard->quest->m6creatures[hh].type = VLC->creh->creatures[reader.readUInt16()];
  1684. guard->quest->m6creatures[hh].count = reader.readUInt16();
  1685. }
  1686. break;
  1687. }
  1688. case 7:
  1689. {
  1690. guard->quest->m7resources.resize(7);
  1691. for(int x = 0; x < 7; ++x)
  1692. {
  1693. guard->quest->m7resources[x] = reader.readUInt32();
  1694. }
  1695. break;
  1696. }
  1697. case 8:
  1698. case 9:
  1699. {
  1700. guard->quest->m13489val = reader.readUInt8();
  1701. break;
  1702. }
  1703. }
  1704. int limit = reader.readUInt32();
  1705. if(limit == (static_cast<int>(0xffffffff)))
  1706. {
  1707. guard->quest->lastDay = -1;
  1708. }
  1709. else
  1710. {
  1711. guard->quest->lastDay = limit;
  1712. }
  1713. guard->quest->firstVisitText = reader.readString();
  1714. guard->quest->nextVisitText = reader.readString();
  1715. guard->quest->completedText = reader.readString();
  1716. guard->quest->isCustomFirst = guard->quest->firstVisitText.size() > 0;
  1717. guard->quest->isCustomNext = guard->quest->nextVisitText.size() > 0;
  1718. guard->quest->isCustomComplete = guard->quest->completedText.size() > 0;
  1719. }
  1720. CGTownInstance * CMapLoaderH3M::readTown(int castleID)
  1721. {
  1722. auto nt = new CGTownInstance();
  1723. if(map->version > EMapFormat::ROE)
  1724. {
  1725. nt->identifier = reader.readUInt32();
  1726. }
  1727. nt->tempOwner = PlayerColor(reader.readUInt8());
  1728. bool hasName = reader.readBool();
  1729. if(hasName)
  1730. {
  1731. nt->name = reader.readString();
  1732. }
  1733. bool hasGarrison = reader.readBool();
  1734. if(hasGarrison)
  1735. {
  1736. readCreatureSet(nt, 7);
  1737. }
  1738. nt->formation = reader.readUInt8();
  1739. bool hasCustomBuildings = reader.readBool();
  1740. if(hasCustomBuildings)
  1741. {
  1742. readBitmask(nt->builtBuildings,6,48,false);
  1743. readBitmask(nt->forbiddenBuildings,6,48,false);
  1744. nt->builtBuildings = convertBuildings(nt->builtBuildings, castleID);
  1745. nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings, castleID);
  1746. }
  1747. // Standard buildings
  1748. else
  1749. {
  1750. bool hasFort = reader.readBool();
  1751. if(hasFort)
  1752. {
  1753. nt->builtBuildings.insert(BuildingID::FORT);
  1754. }
  1755. //means that set of standard building should be included
  1756. nt->builtBuildings.insert(BuildingID::DEFAULT);
  1757. }
  1758. if(map->version > EMapFormat::ROE)
  1759. {
  1760. for(int i = 0; i < 9; ++i)
  1761. {
  1762. ui8 c = reader.readUInt8();
  1763. for(int yy = 0; yy < 8; ++yy)
  1764. {
  1765. if(i * 8 + yy < GameConstants::SPELLS_QUANTITY)
  1766. {
  1767. if(c == (c | static_cast<ui8>(std::pow(2., yy))))
  1768. {
  1769. nt->obligatorySpells.push_back(SpellID(i * 8 + yy));
  1770. }
  1771. }
  1772. }
  1773. }
  1774. }
  1775. for(int i = 0; i < 9; ++i)
  1776. {
  1777. ui8 c = reader.readUInt8();
  1778. for(int yy = 0; yy < 8; ++yy)
  1779. {
  1780. if(i * 8 + yy < GameConstants::SPELLS_QUANTITY)
  1781. {
  1782. if(c != (c | static_cast<ui8>(std::pow(2., yy))))
  1783. {
  1784. nt->possibleSpells.push_back(SpellID(i * 8 + yy));
  1785. }
  1786. }
  1787. }
  1788. }
  1789. // Read castle events
  1790. int numberOfEvent = reader.readUInt32();
  1791. for(int gh = 0; gh < numberOfEvent; ++gh)
  1792. {
  1793. CCastleEvent nce;
  1794. nce.town = nt;
  1795. nce.name = reader.readString();
  1796. nce.message = reader.readString();
  1797. readResourses(nce.resources);
  1798. nce.players = reader.readUInt8();
  1799. if(map->version > EMapFormat::AB)
  1800. {
  1801. nce.humanAffected = reader.readUInt8();
  1802. }
  1803. else
  1804. {
  1805. nce.humanAffected = true;
  1806. }
  1807. nce.computerAffected = reader.readUInt8();
  1808. nce.firstOccurence = reader.readUInt16();
  1809. nce.nextOccurence = reader.readUInt8();
  1810. reader.skip(17);
  1811. // New buildings
  1812. readBitmask(nce.buildings,6,48,false);
  1813. nce.buildings = convertBuildings(nce.buildings, castleID, false);
  1814. nce.creatures.resize(7);
  1815. for(int vv = 0; vv < 7; ++vv)
  1816. {
  1817. nce.creatures[vv] = reader.readUInt16();
  1818. }
  1819. reader.skip(4);
  1820. nt->events.push_back(nce);
  1821. }
  1822. if(map->version > EMapFormat::AB)
  1823. {
  1824. nt->alignment = reader.readUInt8();
  1825. }
  1826. reader.skip(3);
  1827. return nt;
  1828. }
  1829. std::set<BuildingID> CMapLoaderH3M::convertBuildings(const std::set<BuildingID> h3m, int castleID, bool addAuxiliary /*= true*/)
  1830. {
  1831. std::map<int, BuildingID> mapa;
  1832. std::set<BuildingID> ret;
  1833. // Note: this file is parsed many times.
  1834. const JsonNode config(ResourceID("config/buildings5.json"));
  1835. for(const JsonNode & entry : config["table"].Vector())
  1836. {
  1837. int town = entry["town"].Float();
  1838. if (town == castleID || town == -1)
  1839. {
  1840. mapa[entry["h3"].Float()] = BuildingID((si32)entry["vcmi"].Float());
  1841. }
  1842. }
  1843. for(auto & elem : h3m)
  1844. {
  1845. if(mapa[elem] >= 0)
  1846. {
  1847. ret.insert(mapa[elem]);
  1848. }
  1849. // horde buildings
  1850. else if(mapa[elem] >= (-GameConstants::CREATURES_PER_TOWN))
  1851. {
  1852. int level = (mapa[elem]);
  1853. //(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns
  1854. ret.insert(BuildingID(level - 30));
  1855. }
  1856. else
  1857. {
  1858. logGlobal->warnStream() << "Conversion warning: unknown building " << elem << " in castle "
  1859. << castleID;
  1860. }
  1861. }
  1862. if(addAuxiliary)
  1863. {
  1864. //village hall is always present
  1865. ret.insert(BuildingID::VILLAGE_HALL);
  1866. }
  1867. if(ret.find(BuildingID::CITY_HALL) != ret.end())
  1868. {
  1869. ret.insert(BuildingID::EXTRA_CITY_HALL);
  1870. }
  1871. if(ret.find(BuildingID::TOWN_HALL) != ret.end())
  1872. {
  1873. ret.insert(BuildingID::EXTRA_TOWN_HALL);
  1874. }
  1875. if(ret.find(BuildingID::CAPITOL) != ret.end())
  1876. {
  1877. ret.insert(BuildingID::EXTRA_CAPITOL);
  1878. }
  1879. return ret;
  1880. }
  1881. void CMapLoaderH3M::readEvents()
  1882. {
  1883. int numberOfEvents = reader.readUInt32();
  1884. for(int yyoo = 0; yyoo < numberOfEvents; ++yyoo)
  1885. {
  1886. CMapEvent ne;
  1887. ne.name = reader.readString();
  1888. ne.message = reader.readString();
  1889. readResourses(ne.resources);
  1890. ne.players = reader.readUInt8();
  1891. if(map->version > EMapFormat::AB)
  1892. {
  1893. ne.humanAffected = reader.readUInt8();
  1894. }
  1895. else
  1896. {
  1897. ne.humanAffected = true;
  1898. }
  1899. ne.computerAffected = reader.readUInt8();
  1900. ne.firstOccurence = reader.readUInt16();
  1901. ne.nextOccurence = reader.readUInt8();
  1902. reader.skip(17);
  1903. map->events.push_back(ne);
  1904. }
  1905. }
  1906. void CMapLoaderH3M::readMessageAndGuards(std::string& message, CCreatureSet* guards)
  1907. {
  1908. bool hasMessage = reader.readBool();
  1909. if(hasMessage)
  1910. {
  1911. message = reader.readString();
  1912. bool hasGuards = reader.readBool();
  1913. if(hasGuards)
  1914. {
  1915. readCreatureSet(guards, 7);
  1916. }
  1917. reader.skip(4);
  1918. }
  1919. }
  1920. void CMapLoaderH3M::readSpells(std::set<SpellID>& dest)
  1921. {
  1922. readBitmask(dest,9,GameConstants::SPELLS_QUANTITY,false);
  1923. }
  1924. void CMapLoaderH3M::readResourses(TResources& resources)
  1925. {
  1926. resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?
  1927. for(int x = 0; x < 7; ++x)
  1928. {
  1929. resources[x] = reader.readUInt32();
  1930. }
  1931. }
  1932. template <class Indenifier>
  1933. void CMapLoaderH3M::readBitmask(std::set<Indenifier>& dest, const int byteCount, const int limit, bool negate)
  1934. {
  1935. std::vector<bool> temp;
  1936. temp.resize(limit,true);
  1937. readBitmask(temp, byteCount, limit, negate);
  1938. for(int i = 0; i< std::min(temp.size(), static_cast<size_t>(limit)); i++)
  1939. {
  1940. if(temp[i])
  1941. {
  1942. dest.insert(static_cast<Indenifier>(i));
  1943. }
  1944. // else
  1945. // {
  1946. // dest.erase(static_cast<Indenifier>(i));
  1947. // }
  1948. }
  1949. }
  1950. void CMapLoaderH3M::readBitmask(std::vector<bool>& dest, const int byteCount, const int limit, bool negate)
  1951. {
  1952. for(int byte = 0; byte < byteCount; ++byte)
  1953. {
  1954. const ui8 mask = reader.readUInt8();
  1955. for(int bit = 0; bit < 8; ++bit)
  1956. {
  1957. if(byte * 8 + bit < limit)
  1958. {
  1959. const bool flag = mask & (1 << bit);
  1960. if((negate && flag) || (!negate && !flag))
  1961. dest[byte * 8 + bit] = false;
  1962. }
  1963. }
  1964. }
  1965. }
  1966. ui8 CMapLoaderH3M::reverse(ui8 arg)
  1967. {
  1968. ui8 ret = 0;
  1969. for(int i = 0; i < 8; ++i)
  1970. {
  1971. if((arg & (1 << i)) >> i)
  1972. {
  1973. ret |= (128 >> i);
  1974. }
  1975. }
  1976. return ret;
  1977. }