MapFormatH3M.cpp 59 KB

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