MapFormatH3M.cpp 59 KB

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