2
0

MapFormatH3M.cpp 59 KB

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