MapFormatH3M.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  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. for(int x = 0; x < 7; ++x)
  1819. {
  1820. nce->resources[x] = readUI32();
  1821. }
  1822. nce->players = readUI8();
  1823. if(map->version > EMapFormat::AB)
  1824. {
  1825. nce->humanAffected = readUI8();
  1826. }
  1827. else
  1828. {
  1829. nce->humanAffected = true;
  1830. }
  1831. nce->computerAffected = readUI8();
  1832. nce->firstOccurence = readUI16();
  1833. nce->nextOccurence = readUI8();
  1834. skip(17);
  1835. // New buildings
  1836. for(int byte = 0; byte < 6; ++byte)
  1837. {
  1838. ui8 m = readUI8();
  1839. for(int bit = 0; bit < 8; ++bit)
  1840. {
  1841. if(m & (1 << bit))
  1842. {
  1843. nce->buildings.insert(byte * 8 + bit);
  1844. }
  1845. }
  1846. }
  1847. nce->buildings = convertBuildings(nce->buildings, castleID, false);
  1848. nce->creatures.resize(7);
  1849. for(int vv = 0; vv < 7; ++vv)
  1850. {
  1851. nce->creatures[vv] = readUI16();
  1852. }
  1853. skip(4);
  1854. nt->events.push_back(nce);
  1855. }
  1856. if(map->version > EMapFormat::AB)
  1857. {
  1858. nt->alignment = readUI8();
  1859. }
  1860. skip(3);
  1861. return nt;
  1862. }
  1863. std::set<si32> CMapLoaderH3M::convertBuildings(const std::set<si32> h3m, int castleID, bool addAuxiliary /*= true*/)
  1864. {
  1865. std::map<int, int> mapa;
  1866. std::set<si32> ret;
  1867. // Note: this file is parsed many times.
  1868. const JsonNode config(ResourceID("config/buildings5.json"));
  1869. BOOST_FOREACH(const JsonNode & entry, config["table"].Vector())
  1870. {
  1871. int town = entry["town"].Float();
  1872. if (town == castleID || town == -1)
  1873. {
  1874. mapa[entry["h3"].Float()] = entry["vcmi"].Float();
  1875. }
  1876. }
  1877. for(auto i = h3m.begin(); i != h3m.end(); ++i)
  1878. {
  1879. if(mapa[*i] >= 0)
  1880. {
  1881. ret.insert(mapa[*i]);
  1882. }
  1883. // horde buildings
  1884. else if(mapa[*i] >= (-GameConstants::CREATURES_PER_TOWN))
  1885. {
  1886. int level = (mapa[*i]);
  1887. //(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns
  1888. ret.insert(level - 30);
  1889. }
  1890. else
  1891. {
  1892. tlog3 << "Conversion warning: unknown building " << *i << " in castle "
  1893. << castleID << std::endl;
  1894. }
  1895. }
  1896. if(addAuxiliary)
  1897. {
  1898. //village hall is always present
  1899. ret.insert(EBuilding::VILLAGE_HALL);
  1900. }
  1901. if(ret.find(EBuilding::CITY_HALL) != ret.end())
  1902. {
  1903. ret.insert(EBuilding::EXTRA_CITY_HALL);
  1904. }
  1905. if(ret.find(EBuilding::TOWN_HALL) != ret.end())
  1906. {
  1907. ret.insert(EBuilding::EXTRA_TOWN_HALL);
  1908. }
  1909. if(ret.find(EBuilding::CAPITOL) != ret.end())
  1910. {
  1911. ret.insert(EBuilding::EXTRA_CAPITOL);
  1912. }
  1913. return ret;
  1914. }
  1915. void CMapLoaderH3M::readEvents()
  1916. {
  1917. int numberOfEvents = readUI32();
  1918. for(int yyoo = 0; yyoo < numberOfEvents; ++yyoo)
  1919. {
  1920. CMapEvent * ne = new CMapEvent();
  1921. ne->name = readString();
  1922. ne->message = readString();
  1923. readResourses(ne->resources);
  1924. ne->players = readUI8();
  1925. if(map->version > EMapFormat::AB)
  1926. {
  1927. ne->humanAffected = readUI8();
  1928. }
  1929. else
  1930. {
  1931. ne->humanAffected = true;
  1932. }
  1933. ne->computerAffected = readUI8();
  1934. ne->firstOccurence = readUI16();
  1935. ne->nextOccurence = readUI8();
  1936. skip(17);
  1937. map->events.push_back(ne);
  1938. }
  1939. }
  1940. void CMapLoaderH3M::readSpells(std::set<TSpell>& dest)
  1941. {
  1942. for(int byte = 0; byte < 9; ++byte)
  1943. {
  1944. ui8 c = readUI8();
  1945. for(int bit = 0; bit < 8; ++bit)
  1946. {
  1947. if(byte * 8 + bit < GameConstants::SPELLS_QUANTITY)
  1948. {
  1949. if(c & (1 << bit))
  1950. {
  1951. dest.insert(byte * 8 + bit);
  1952. }
  1953. }
  1954. }
  1955. }
  1956. }
  1957. void CMapLoaderH3M::readResourses(TResources& resources)
  1958. {
  1959. resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?
  1960. for(int x = 0; x < 7; ++x)
  1961. {
  1962. resources[x] = readUI32();
  1963. }
  1964. }
  1965. void CMapLoaderH3M::readBitmask(std::vector<bool>& dest, const int byteCount, const int limit, bool negate)
  1966. {
  1967. for(int byte = 0; byte < byteCount; ++byte)
  1968. {
  1969. const ui8 mask = readUI8();
  1970. for(int bit = 0; bit < 8; ++bit)
  1971. {
  1972. if(byte * 8 + bit < limit)
  1973. {
  1974. const bool flag = mask & (1 << bit);
  1975. if((negate && flag) || (!negate && !flag))
  1976. dest[byte * 8 + bit] = false;
  1977. }
  1978. }
  1979. }
  1980. }
  1981. ui8 CMapLoaderH3M::reverse(ui8 arg)
  1982. {
  1983. ui8 ret = 0;
  1984. for(int i = 0; i < 8; ++i)
  1985. {
  1986. if((arg & (1 << i)) >> i)
  1987. {
  1988. ret |= (128 >> i);
  1989. }
  1990. }
  1991. return ret;
  1992. }