MapFormatH3M.cpp 52 KB

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