MapFormatH3M.cpp 59 KB

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