MapFormatH3M.cpp 50 KB

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