MapFormatH3M.cpp 48 KB

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