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. 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. for(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. auto 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. if(hero->artifactsWorn.size() || hero->artifactsInBackpack.size())
  506. {
  507. 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;
  508. hero->artifactsInBackpack.clear();
  509. while(hero->artifactsWorn.size())
  510. hero->eraseArtSlot(hero->artifactsWorn.begin()->first);
  511. }
  512. for(int pom = 0; pom < 16; pom++)
  513. {
  514. loadArtifactToSlot(hero, pom);
  515. }
  516. // misc5 art //17
  517. if(map->version >= EMapFormat::SOD)
  518. {
  519. assert(!hero->getArt(ArtifactPosition::MACH4));
  520. if(!loadArtifactToSlot(hero, ArtifactPosition::MACH4))
  521. {
  522. // catapult by default
  523. assert(!hero->getArt(ArtifactPosition::MACH4));
  524. hero->putArtifact(ArtifactPosition::MACH4, createArtifact(ArtifactID::CATAPULT));
  525. }
  526. }
  527. loadArtifactToSlot(hero, ArtifactPosition::SPELLBOOK);
  528. // 19 //???what is that? gap in file or what? - it's probably fifth slot..
  529. if(map->version > EMapFormat::ROE)
  530. {
  531. loadArtifactToSlot(hero, ArtifactPosition::MISC5);
  532. }
  533. else
  534. {
  535. reader.skip(1);
  536. }
  537. // bag artifacts //20
  538. // number of artifacts in hero's bag
  539. int amount = reader.readUInt16();
  540. for(int ss = 0; ss < amount; ++ss)
  541. {
  542. loadArtifactToSlot(hero, GameConstants::BACKPACK_START + hero->artifactsInBackpack.size());
  543. }
  544. }
  545. }
  546. bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
  547. {
  548. const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff;
  549. int aid;
  550. if(map->version == EMapFormat::ROE)
  551. {
  552. aid = reader.readUInt8();
  553. }
  554. else
  555. {
  556. aid = reader.readUInt16();
  557. }
  558. bool isArt = aid != artmask;
  559. if(isArt)
  560. {
  561. if(vstd::contains(VLC->arth->bigArtifacts, aid) && slot >= GameConstants::BACKPACK_START)
  562. {
  563. logGlobal->warnStream() << "Warning: A big artifact (war machine) in hero's backpack, ignoring...";
  564. return false;
  565. }
  566. if(aid == 0 && slot == ArtifactPosition::MISC5)
  567. {
  568. //TODO: check how H3 handles it -> art 0 in slot 18 in AB map
  569. logGlobal->warnStream() << "Spellbook to MISC5 slot? Putting it spellbook place. AB format peculiarity ? (format "
  570. << static_cast<int>(map->version) << ")";
  571. slot = ArtifactPosition::SPELLBOOK;
  572. }
  573. hero->putArtifact(ArtifactPosition(slot), createArtifact(aid));
  574. }
  575. return isArt;
  576. }
  577. CArtifactInstance * CMapLoaderH3M::createArtifact(int aid, int spellID /*= -1*/)
  578. {
  579. CArtifactInstance * a = nullptr;
  580. if(aid >= 0)
  581. {
  582. if(spellID < 0)
  583. {
  584. a = CArtifactInstance::createNewArtifactInstance(aid);
  585. }
  586. else
  587. {
  588. a = CArtifactInstance::createScroll(SpellID(spellID).toSpell());
  589. }
  590. }
  591. else //FIXME: create combined artifact instance for random combined artifacts, just in case
  592. {
  593. a = new CArtifactInstance(); //random, empty
  594. }
  595. map->addNewArtifactInstance(a);
  596. //TODO make it nicer
  597. if(a->artType && (!!a->artType->constituents))
  598. {
  599. CCombinedArtifactInstance * comb = dynamic_cast<CCombinedArtifactInstance *>(a);
  600. for(CCombinedArtifactInstance::ConstituentInfo & ci : comb->constituentsInfo)
  601. {
  602. map->addNewArtifactInstance(ci.art);
  603. }
  604. }
  605. return a;
  606. }
  607. void CMapLoaderH3M::readTerrain()
  608. {
  609. map->initTerrain();
  610. // Read terrain
  611. for(int a = 0; a < 2; ++a)
  612. {
  613. if(a == 1 && !map->twoLevel)
  614. {
  615. break;
  616. }
  617. for(int c = 0; c < map->width; c++)
  618. {
  619. for(int z = 0; z < map->height; z++)
  620. {
  621. auto & tile = map->getTile(int3(z, c, a));
  622. tile.terType = ETerrainType(reader.readUInt8());
  623. tile.terView = reader.readUInt8();
  624. tile.riverType = static_cast<ERiverType::ERiverType>(reader.readUInt8());
  625. tile.riverDir = reader.readUInt8();
  626. tile.roadType = static_cast<ERoadType::ERoadType>(reader.readUInt8());
  627. tile.roadDir = reader.readUInt8();
  628. tile.extTileFlags = reader.readUInt8();
  629. tile.blocked = (tile.terType == ETerrainType::ROCK ? 1 : 0); //underground tiles are always blocked
  630. tile.visitable = 0;
  631. }
  632. }
  633. }
  634. }
  635. void CMapLoaderH3M::readDefInfo()
  636. {
  637. int defAmount = reader.readUInt32();
  638. map->customDefs.reserve(defAmount + 8);
  639. // Read custom defs
  640. for(int idd = 0; idd < defAmount; ++idd)
  641. {
  642. auto defInfo = new CGDefInfo();
  643. defInfo->name = reader.readString();
  644. std::transform(defInfo->name.begin(),defInfo->name.end(),defInfo->name.begin(),(int(*)(int))toupper);
  645. ui8 bytes[12];
  646. for(auto & byte : bytes)
  647. {
  648. byte = reader.readUInt8();
  649. }
  650. defInfo->terrainAllowed = reader.readUInt16();
  651. defInfo->terrainMenu = reader.readUInt16();
  652. defInfo->id = Obj(reader.readUInt32());
  653. defInfo->subid = reader.readUInt32();
  654. defInfo->type = reader.readUInt8();
  655. defInfo->printPriority = reader.readUInt8();
  656. for(int zi = 0; zi < 6; ++zi)
  657. {
  658. defInfo->blockMap[zi] = reverse(bytes[zi]);
  659. }
  660. for(int zi = 0; zi < 6; ++zi)
  661. {
  662. defInfo->visitMap[zi] = reverse(bytes[6 + zi]);
  663. }
  664. reader.skip(16);
  665. if(defInfo->id != Obj::HERO && defInfo->id != Obj::RANDOM_HERO)
  666. {
  667. CGDefInfo * h = VLC->dobjinfo->gobjs[defInfo->id][defInfo->subid];
  668. if(!h)
  669. {
  670. //remove fake entry
  671. VLC->dobjinfo->gobjs[defInfo->id].erase(defInfo->subid);
  672. if(VLC->dobjinfo->gobjs[defInfo->id].size())
  673. {
  674. VLC->dobjinfo->gobjs.erase(defInfo->id);
  675. }
  676. logGlobal->warnStream() << "\t\tWarning: no defobjinfo entry for object ID="
  677. << defInfo->id << " subID=" << defInfo->subid;
  678. }
  679. else
  680. {
  681. defInfo->visitDir = VLC->dobjinfo->gobjs[defInfo->id][defInfo->subid]->visitDir;
  682. }
  683. }
  684. else
  685. {
  686. defInfo->visitDir = 0xff;
  687. }
  688. if(defInfo->id == Obj::EVENT)
  689. {
  690. std::memset(defInfo->blockMap, 255, 6);
  691. }
  692. //calculating coverageMap
  693. defInfo->fetchInfoFromMSK();
  694. map->customDefs.push_back(defInfo);
  695. }
  696. //add holes - they always can appear
  697. for(int i = 0; i < 8 ; ++i)
  698. {
  699. map->customDefs.push_back(VLC->dobjinfo->gobjs[Obj::HOLE][i]);
  700. }
  701. }
  702. void CMapLoaderH3M::readObjects()
  703. {
  704. int howManyObjs = reader.readUInt32();
  705. for(int ww = 0; ww < howManyObjs; ++ww)
  706. {
  707. CGObjectInstance * nobj = nullptr;
  708. int3 objPos = readInt3();
  709. int defnum = reader.readUInt32();
  710. ObjectInstanceID idToBeGiven = ObjectInstanceID(map->objects.size());
  711. CGDefInfo * defInfo = map->customDefs.at(defnum);
  712. reader.skip(5);
  713. switch(defInfo->id)
  714. {
  715. case Obj::EVENT:
  716. {
  717. auto evnt = new CGEvent();
  718. nobj = evnt;
  719. readMessageAndGuards(evnt->message, evnt);
  720. evnt->gainedExp = reader.readUInt32();
  721. evnt->manaDiff = reader.readUInt32();
  722. evnt->moraleDiff = reader.readInt8();
  723. evnt->luckDiff = reader.readInt8();
  724. readResourses(evnt->resources);
  725. evnt->primskills.resize(GameConstants::PRIMARY_SKILLS);
  726. for(int x = 0; x < 4; ++x)
  727. {
  728. evnt->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
  729. }
  730. int gabn = reader.readUInt8(); // Number of gained abilities
  731. for(int oo = 0; oo < gabn; ++oo)
  732. {
  733. evnt->abilities.push_back(SecondarySkill(reader.readUInt8()));
  734. evnt->abilityLevels.push_back(reader.readUInt8());
  735. }
  736. int gart = reader.readUInt8(); // Number of gained artifacts
  737. for(int oo = 0; oo < gart; ++oo)
  738. {
  739. if(map->version == EMapFormat::ROE)
  740. {
  741. evnt->artifacts.push_back(ArtifactID(reader.readUInt8()));
  742. }
  743. else
  744. {
  745. evnt->artifacts.push_back(ArtifactID(reader.readUInt16()));
  746. }
  747. }
  748. int gspel = reader.readUInt8(); // Number of gained spells
  749. for(int oo = 0; oo < gspel; ++oo)
  750. {
  751. evnt->spells.push_back(SpellID(reader.readUInt8()));
  752. }
  753. int gcre = reader.readUInt8(); //number of gained creatures
  754. readCreatureSet(&evnt->creatures, gcre);
  755. reader.skip(8);
  756. evnt->availableFor = reader.readUInt8();
  757. evnt->computerActivate = reader.readUInt8();
  758. evnt->removeAfterVisit = reader.readUInt8();
  759. evnt->humanActivate = true;
  760. reader.skip(4);
  761. break;
  762. }
  763. case Obj::HERO:
  764. case Obj::RANDOM_HERO:
  765. case Obj::PRISON:
  766. {
  767. nobj = readHero(idToBeGiven);
  768. break;
  769. }
  770. case Obj::ARENA:
  771. case Obj::MERCENARY_CAMP:
  772. case Obj::MARLETTO_TOWER:
  773. case Obj::STAR_AXIS:
  774. case Obj::GARDEN_OF_REVELATION:
  775. case Obj::LEARNING_STONE:
  776. case Obj::TREE_OF_KNOWLEDGE:
  777. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  778. case Obj::SCHOOL_OF_MAGIC:
  779. case Obj::SCHOOL_OF_WAR:
  780. {
  781. nobj = new CGVisitableOPH();
  782. break;
  783. }
  784. case Obj::MYSTICAL_GARDEN:
  785. case Obj::WINDMILL:
  786. case Obj::WATER_WHEEL:
  787. {
  788. nobj = new CGVisitableOPW();
  789. break;
  790. }
  791. case Obj::MONOLITH1:
  792. case Obj::MONOLITH2:
  793. case Obj::MONOLITH3:
  794. case Obj::SUBTERRANEAN_GATE:
  795. case Obj::WHIRLPOOL:
  796. {
  797. nobj = new CGTeleport();
  798. break;
  799. }
  800. case Obj::CAMPFIRE:
  801. case Obj::FLOTSAM:
  802. case Obj::SEA_CHEST:
  803. case Obj::SHIPWRECK_SURVIVOR:
  804. {
  805. nobj = new CGPickable();
  806. break;
  807. }
  808. case Obj::TREASURE_CHEST:
  809. if(defInfo->subid == 0)
  810. {
  811. nobj = new CGPickable();
  812. }
  813. else
  814. {
  815. //WoG pickable object
  816. //TODO: possible special handling
  817. nobj = new CGObjectInstance();
  818. }
  819. break;
  820. case Obj::MONSTER: //Monster
  821. case Obj::RANDOM_MONSTER:
  822. case Obj::RANDOM_MONSTER_L1:
  823. case Obj::RANDOM_MONSTER_L2:
  824. case Obj::RANDOM_MONSTER_L3:
  825. case Obj::RANDOM_MONSTER_L4:
  826. case Obj::RANDOM_MONSTER_L5:
  827. case Obj::RANDOM_MONSTER_L6:
  828. case Obj::RANDOM_MONSTER_L7:
  829. {
  830. auto cre = new CGCreature();
  831. nobj = cre;
  832. if(map->version > EMapFormat::ROE)
  833. {
  834. cre->identifier = reader.readUInt32();
  835. map->questIdentifierToId[cre->identifier] = idToBeGiven;
  836. }
  837. auto hlp = new CStackInstance();
  838. hlp->count = reader.readUInt16();
  839. //type will be set during initialization
  840. cre->putStack(SlotID(0), hlp);
  841. cre->character = reader.readUInt8();
  842. bool hasMessage = reader.readBool();
  843. if(hasMessage)
  844. {
  845. cre->message = reader.readString();
  846. readResourses(cre->resources);
  847. int artID;
  848. if (map->version == EMapFormat::ROE)
  849. {
  850. artID = reader.readUInt8();
  851. }
  852. else
  853. {
  854. artID = reader.readUInt16();
  855. }
  856. if(map->version == EMapFormat::ROE)
  857. {
  858. if(artID != 0xff)
  859. {
  860. cre->gainedArtifact = ArtifactID(artID);
  861. }
  862. else
  863. {
  864. cre->gainedArtifact = ArtifactID::NONE;
  865. }
  866. }
  867. else
  868. {
  869. if(artID != 0xffff)
  870. {
  871. cre->gainedArtifact = ArtifactID(artID);
  872. }
  873. else
  874. {
  875. cre->gainedArtifact = ArtifactID::NONE;
  876. }
  877. }
  878. }
  879. cre->neverFlees = reader.readUInt8();
  880. cre->notGrowingTeam =reader.readUInt8();
  881. reader.skip(2);
  882. break;
  883. }
  884. case Obj::OCEAN_BOTTLE:
  885. case Obj::SIGN:
  886. {
  887. auto sb = new CGSignBottle();
  888. nobj = sb;
  889. sb->message = reader.readString();
  890. reader.skip(4);
  891. break;
  892. }
  893. case Obj::SEER_HUT:
  894. {
  895. nobj = readSeerHut();
  896. map->addQuest(nobj);
  897. break;
  898. }
  899. case Obj::WITCH_HUT:
  900. {
  901. auto wh = new CGWitchHut();
  902. nobj = wh;
  903. // in RoE we cannot specify it - all are allowed (I hope)
  904. if(map->version > EMapFormat::ROE)
  905. {
  906. for(int i = 0 ; i < 4; ++i)
  907. {
  908. ui8 c = reader.readUInt8();
  909. for(int yy = 0; yy < 8; ++yy)
  910. {
  911. if(i * 8 + yy < GameConstants::SKILL_QUANTITY)
  912. {
  913. if(c == (c | static_cast<ui8>(std::pow(2., yy))))
  914. {
  915. wh->allowedAbilities.push_back(i * 8 + yy);
  916. }
  917. }
  918. }
  919. }
  920. }
  921. else
  922. {
  923. // RoE map
  924. for(int gg = 0; gg < GameConstants::SKILL_QUANTITY; ++gg)
  925. {
  926. wh->allowedAbilities.push_back(gg);
  927. }
  928. }
  929. break;
  930. }
  931. case Obj::SCHOLAR:
  932. {
  933. auto sch = new CGScholar();
  934. nobj = sch;
  935. sch->bonusType = static_cast<CGScholar::EBonusType>(reader.readUInt8());
  936. sch->bonusID = reader.readUInt8();
  937. reader.skip(6);
  938. break;
  939. }
  940. case Obj::GARRISON:
  941. case Obj::GARRISON2:
  942. {
  943. auto gar = new CGGarrison();
  944. nobj = gar;
  945. nobj->setOwner(PlayerColor(reader.readUInt8()));
  946. reader.skip(3);
  947. readCreatureSet(gar, 7);
  948. if(map->version > EMapFormat::ROE)
  949. {
  950. gar->removableUnits = reader.readBool();
  951. }
  952. else
  953. {
  954. gar->removableUnits = true;
  955. }
  956. reader.skip(8);
  957. break;
  958. }
  959. case Obj::ARTIFACT:
  960. case Obj::RANDOM_ART:
  961. case Obj::RANDOM_TREASURE_ART:
  962. case Obj::RANDOM_MINOR_ART:
  963. case Obj::RANDOM_MAJOR_ART:
  964. case Obj::RANDOM_RELIC_ART:
  965. case Obj::SPELL_SCROLL:
  966. {
  967. int artID = ArtifactID::NONE; //random, set later
  968. int spellID = -1;
  969. auto art = new CGArtifact();
  970. nobj = art;
  971. readMessageAndGuards(art->message, art);
  972. if(defInfo->id == Obj::SPELL_SCROLL)
  973. {
  974. spellID = reader.readUInt32();
  975. artID = 1;
  976. }
  977. else if(defInfo->id == Obj::ARTIFACT)
  978. {
  979. //specific artifact
  980. artID = defInfo->subid;
  981. }
  982. art->storedArtifact = createArtifact(artID, spellID);
  983. break;
  984. }
  985. case Obj::RANDOM_RESOURCE:
  986. case Obj::RESOURCE:
  987. {
  988. auto res = new CGResource();
  989. nobj = res;
  990. readMessageAndGuards(res->message, res);
  991. res->amount = reader.readUInt32();
  992. if(defInfo->subid == Res::GOLD)
  993. {
  994. // Gold is multiplied by 100.
  995. res->amount *= 100;
  996. }
  997. reader.skip(4);
  998. break;
  999. }
  1000. case Obj::RANDOM_TOWN:
  1001. case Obj::TOWN:
  1002. {
  1003. nobj = readTown(defInfo->subid);
  1004. break;
  1005. }
  1006. case Obj::MINE:
  1007. case Obj::ABANDONED_MINE:
  1008. {
  1009. nobj = new CGMine();
  1010. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1011. reader.skip(3);
  1012. break;
  1013. }
  1014. case Obj::CREATURE_GENERATOR1:
  1015. case Obj::CREATURE_GENERATOR2:
  1016. case Obj::CREATURE_GENERATOR3:
  1017. case Obj::CREATURE_GENERATOR4:
  1018. {
  1019. nobj = new CGDwelling();
  1020. nobj->setOwner(PlayerColor(reader.readUInt8()));
  1021. reader.skip(3);
  1022. break;
  1023. }
  1024. case Obj::REFUGEE_CAMP:
  1025. case Obj::WAR_MACHINE_FACTORY:
  1026. {
  1027. nobj = new CGDwelling();
  1028. break;
  1029. }
  1030. case Obj::SHRINE_OF_MAGIC_INCANTATION:
  1031. case Obj::SHRINE_OF_MAGIC_GESTURE:
  1032. case Obj::SHRINE_OF_MAGIC_THOUGHT:
  1033. {
  1034. auto shr = new CGShrine();
  1035. nobj = shr;
  1036. ui8 raw_id = reader.readUInt8();
  1037. if (255 == raw_id)
  1038. {
  1039. shr->spell = SpellID(SpellID::NONE);
  1040. }
  1041. else
  1042. {
  1043. shr->spell = SpellID(raw_id);
  1044. }
  1045. reader.skip(3);
  1046. break;
  1047. }
  1048. case Obj::PANDORAS_BOX:
  1049. {
  1050. auto box = new CGPandoraBox();
  1051. nobj = box;
  1052. readMessageAndGuards(box->message, box);
  1053. box->gainedExp = reader.readUInt32();
  1054. box->manaDiff = reader.readUInt32();
  1055. box->moraleDiff = reader.readInt8();
  1056. box->luckDiff = reader.readInt8();
  1057. readResourses(box->resources);
  1058. box->primskills.resize(GameConstants::PRIMARY_SKILLS);
  1059. for(int x = 0; x < 4; ++x)
  1060. {
  1061. box->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
  1062. }
  1063. int gabn = reader.readUInt8();//number of gained abilities
  1064. for(int oo = 0; oo < gabn; ++oo)
  1065. {
  1066. box->abilities.push_back(SecondarySkill(reader.readUInt8()));
  1067. box->abilityLevels.push_back(reader.readUInt8());
  1068. }
  1069. int gart = reader.readUInt8(); //number of gained artifacts
  1070. for(int oo = 0; oo < gart; ++oo)
  1071. {
  1072. if(map->version > EMapFormat::ROE)
  1073. {
  1074. box->artifacts.push_back(ArtifactID(reader.readUInt16()));
  1075. }
  1076. else
  1077. {
  1078. box->artifacts.push_back(ArtifactID(reader.readUInt8()));
  1079. }
  1080. }
  1081. int gspel = reader.readUInt8(); //number of gained spells
  1082. for(int oo = 0; oo < gspel; ++oo)
  1083. {
  1084. box->spells.push_back(SpellID(reader.readUInt8()));
  1085. }
  1086. int gcre = reader.readUInt8(); //number of gained creatures
  1087. readCreatureSet(&box->creatures, gcre);
  1088. reader.skip(8);
  1089. break;
  1090. }
  1091. case Obj::GRAIL:
  1092. {
  1093. map->grailPos = objPos;
  1094. map->grailRadious = reader.readUInt32();
  1095. continue;
  1096. }
  1097. case Obj::RANDOM_DWELLING: //same as castle + level range
  1098. case Obj::RANDOM_DWELLING_LVL: //same as castle, fixed level
  1099. case Obj::RANDOM_DWELLING_FACTION: //level range, fixed faction
  1100. {
  1101. nobj = new CGDwelling();
  1102. CSpecObjInfo * spec = nullptr;
  1103. switch(defInfo->id)
  1104. {
  1105. break; case Obj::RANDOM_DWELLING: spec = new CCreGenLeveledCastleInfo();
  1106. break; case Obj::RANDOM_DWELLING_LVL: spec = new CCreGenAsCastleInfo();
  1107. break; case Obj::RANDOM_DWELLING_FACTION: spec = new CCreGenLeveledInfo();
  1108. }
  1109. spec->player = PlayerColor(reader.readUInt32());
  1110. //216 and 217
  1111. if (auto castleSpec = dynamic_cast<CCreGenAsCastleInfo *>(spec))
  1112. {
  1113. castleSpec->identifier = reader.readUInt32();
  1114. if(!castleSpec->identifier)
  1115. {
  1116. castleSpec->asCastle = false;
  1117. castleSpec->castles[0] = reader.readUInt8();
  1118. castleSpec->castles[1] = reader.readUInt8();
  1119. }
  1120. else
  1121. {
  1122. castleSpec->asCastle = true;
  1123. }
  1124. }
  1125. //216 and 218
  1126. if (auto lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec))
  1127. {
  1128. lvlSpec->minLevel = std::max(reader.readUInt8(), ui8(1));
  1129. lvlSpec->maxLevel = std::min(reader.readUInt8(), ui8(7));
  1130. }
  1131. nobj->setOwner(spec->player);
  1132. static_cast<CGDwelling *>(nobj)->info = spec;
  1133. break;
  1134. }
  1135. case Obj::QUEST_GUARD:
  1136. {
  1137. auto guard = new CGQuestGuard();
  1138. map->addQuest(guard);
  1139. readQuest(guard);
  1140. nobj = guard;
  1141. break;
  1142. }
  1143. case Obj::FAERIE_RING:
  1144. case Obj::SWAN_POND:
  1145. case Obj::IDOL_OF_FORTUNE:
  1146. case Obj::FOUNTAIN_OF_FORTUNE:
  1147. case Obj::RALLY_FLAG:
  1148. case Obj::OASIS:
  1149. case Obj::TEMPLE:
  1150. case Obj::WATERING_HOLE:
  1151. case Obj::FOUNTAIN_OF_YOUTH:
  1152. case Obj::BUOY:
  1153. case Obj::MERMAID:
  1154. case Obj::STABLES:
  1155. {
  1156. nobj = new CGBonusingObject();
  1157. break;
  1158. }
  1159. case Obj::MAGIC_WELL:
  1160. {
  1161. nobj = new CGMagicWell();
  1162. break;
  1163. }
  1164. case Obj::COVER_OF_DARKNESS:
  1165. case Obj::REDWOOD_OBSERVATORY:
  1166. case Obj::PILLAR_OF_FIRE:
  1167. {
  1168. nobj = new CGObservatory();
  1169. break;
  1170. }
  1171. case Obj::CORPSE:
  1172. case Obj::LEAN_TO:
  1173. case Obj::WAGON:
  1174. case Obj::WARRIORS_TOMB:
  1175. {
  1176. nobj = new CGOnceVisitable();
  1177. break;
  1178. }
  1179. case Obj::BOAT:
  1180. {
  1181. nobj = new CGBoat();
  1182. break;
  1183. }
  1184. case Obj::SIRENS:
  1185. {
  1186. nobj = new CGSirens();
  1187. break;
  1188. }
  1189. case Obj::SHIPYARD:
  1190. {
  1191. nobj = new CGShipyard();
  1192. nobj->setOwner(PlayerColor(reader.readUInt32()));
  1193. break;
  1194. }
  1195. case Obj::HERO_PLACEHOLDER: //hero placeholder
  1196. {
  1197. auto hp = new CGHeroPlaceholder();
  1198. nobj = hp;
  1199. hp->setOwner(PlayerColor(reader.readUInt8()));
  1200. int htid = reader.readUInt8();; //hero type id
  1201. nobj->subID = htid;
  1202. if(htid == 0xff)
  1203. {
  1204. hp->power = reader.readUInt8();
  1205. }
  1206. else
  1207. {
  1208. hp->power = 0;
  1209. }
  1210. break;
  1211. }
  1212. case Obj::KEYMASTER:
  1213. {
  1214. nobj = new CGKeymasterTent();
  1215. break;
  1216. }
  1217. case Obj::BORDERGUARD:
  1218. {
  1219. nobj = new CGBorderGuard();
  1220. map->addQuest(nobj);
  1221. break;
  1222. }
  1223. case Obj::BORDER_GATE:
  1224. {
  1225. nobj = new CGBorderGate();
  1226. map->addQuest (nobj);
  1227. break;
  1228. }
  1229. case Obj::EYE_OF_MAGI:
  1230. case Obj::HUT_OF_MAGI:
  1231. {
  1232. nobj = new CGMagi();
  1233. break;
  1234. }
  1235. case Obj::CREATURE_BANK:
  1236. case Obj::DERELICT_SHIP:
  1237. case Obj::DRAGON_UTOPIA:
  1238. case Obj::CRYPT:
  1239. case Obj::SHIPWRECK:
  1240. {
  1241. nobj = new CBank();
  1242. break;
  1243. }
  1244. case Obj::PYRAMID: //Pyramid of WoG object
  1245. {
  1246. if(defInfo->subid == 0)
  1247. {
  1248. nobj = new CGPyramid();
  1249. }
  1250. else
  1251. {
  1252. //WoG object
  1253. //TODO: possible special handling
  1254. nobj = new CGObjectInstance();
  1255. }
  1256. break;
  1257. }
  1258. case Obj::CARTOGRAPHER:
  1259. {
  1260. nobj = new CCartographer();
  1261. break;
  1262. }
  1263. case Obj::MAGIC_SPRING:
  1264. {
  1265. nobj = new CGMagicSpring();
  1266. break;
  1267. }
  1268. case Obj::DEN_OF_THIEVES:
  1269. {
  1270. nobj = new CGDenOfthieves();
  1271. break;
  1272. }
  1273. case Obj::OBELISK:
  1274. {
  1275. nobj = new CGObelisk();
  1276. break;
  1277. }
  1278. case Obj::LIGHTHOUSE: //Lighthouse
  1279. {
  1280. nobj = new CGLighthouse();
  1281. nobj->tempOwner = PlayerColor(reader.readUInt32());
  1282. break;
  1283. }
  1284. case Obj::ALTAR_OF_SACRIFICE:
  1285. case Obj::TRADING_POST:
  1286. case Obj::FREELANCERS_GUILD:
  1287. case Obj::TRADING_POST_SNOW:
  1288. {
  1289. nobj = new CGMarket();
  1290. break;
  1291. }
  1292. case Obj::UNIVERSITY:
  1293. {
  1294. nobj = new CGUniversity();
  1295. break;
  1296. }
  1297. case Obj::BLACK_MARKET:
  1298. {
  1299. nobj = new CGBlackMarket();
  1300. break;
  1301. }
  1302. default: //any other object
  1303. {
  1304. nobj = new CGObjectInstance();
  1305. break;
  1306. }
  1307. }
  1308. nobj->pos = objPos;
  1309. nobj->ID = defInfo->id;
  1310. nobj->id = idToBeGiven;
  1311. if(nobj->ID != Obj::HERO && nobj->ID != Obj::HERO_PLACEHOLDER && nobj->ID != Obj::PRISON)
  1312. {
  1313. nobj->subID = defInfo->subid;
  1314. }
  1315. nobj->defInfo = defInfo;
  1316. assert(idToBeGiven == ObjectInstanceID(map->objects.size()));
  1317. map->objects.push_back(nobj);
  1318. if(nobj->ID == Obj::TOWN)
  1319. {
  1320. map->towns.push_back(static_cast<CGTownInstance *>(nobj));
  1321. }
  1322. if(nobj->ID == Obj::HERO)
  1323. {
  1324. map->heroesOnMap.push_back(static_cast<CGHeroInstance*>(nobj));
  1325. }
  1326. }
  1327. std::sort(map->heroesOnMap.begin(), map->heroesOnMap.end(), [](const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)
  1328. {
  1329. return a->subID < b->subID;
  1330. });
  1331. }
  1332. void CMapLoaderH3M::readCreatureSet(CCreatureSet * out, int number)
  1333. {
  1334. const bool version = (map->version > EMapFormat::ROE);
  1335. const int maxID = version ? 0xffff : 0xff;
  1336. for(int ir = 0; ir < number; ++ir)
  1337. {
  1338. CreatureID creID;
  1339. int count;
  1340. if (version)
  1341. {
  1342. creID = CreatureID(reader.readUInt16());
  1343. }
  1344. else
  1345. {
  1346. creID = CreatureID(reader.readUInt8());
  1347. }
  1348. count = reader.readUInt16();
  1349. // Empty slot
  1350. if(creID == maxID) continue;
  1351. auto hlp = new CStackInstance();
  1352. hlp->count = count;
  1353. if(creID > maxID - 0xf)
  1354. {
  1355. //this will happen when random object has random army
  1356. creID = CreatureID(maxID + 1 - creID + VLC->creh->creatures.size());
  1357. hlp->idRand = creID;
  1358. }
  1359. else
  1360. {
  1361. hlp->setType(creID);
  1362. }
  1363. out->putStack(SlotID(ir), hlp);
  1364. }
  1365. out->validTypes(true);
  1366. }
  1367. CGObjectInstance * CMapLoaderH3M::readHero(ObjectInstanceID idToBeGiven)
  1368. {
  1369. auto nhi = new CGHeroInstance();
  1370. int identifier = 0;
  1371. if(map->version > EMapFormat::ROE)
  1372. {
  1373. 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. }