MapFormatH3M.cpp 58 KB

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