MapFormatH3M.cpp 50 KB

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