MapFormatH3M.cpp 59 KB

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