123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181 |
- /*
- * MapFormatH3M.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "MapFormatH3M.h"
- #include <boost/crc.hpp>
- #include "../CStopWatch.h"
- #include "../Filesystem/CResourceLoader.h"
- #include "../Filesystem/CInputStream.h"
- #include "CMap.h"
- #include "../CSpellHandler.h"
- #include "../CCreatureHandler.h"
- #include "../CHeroHandler.h"
- #include "../CObjectHandler.h"
- #include "../CDefObjInfoHandler.h"
- #include "../VCMI_Lib.h"
- const bool CMapLoaderH3M::IS_PROFILING_ENABLED = false;
- CMapLoaderH3M::CMapLoaderH3M(CInputStream * stream) : map(nullptr), reader(stream),inputStream(stream)
- {
- }
- CMapLoaderH3M::~CMapLoaderH3M()
- {
- }
- std::unique_ptr<CMap> CMapLoaderH3M::loadMap()
- {
- // Init map object by parsing the input buffer
- map = new CMap();
- mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));
- init();
- return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));;
- }
- std::unique_ptr<CMapHeader> CMapLoaderH3M::loadMapHeader()
- {
- // Read header
- mapHeader = make_unique<CMapHeader>();
- readHeader();
- return std::move(mapHeader);
- }
- void CMapLoaderH3M::init()
- {
- //FIXME: get rid of double input process
- si64 temp_size = inputStream->getSize();
- inputStream->seek(0);
- ui8 * temp_buffer = new ui8[temp_size];
- inputStream->read(temp_buffer,temp_size);
- // Compute checksum
- boost::crc_32_type result;
- result.process_bytes(temp_buffer, temp_size);
- map->checksum = result.checksum();
- delete [] temp_buffer;
- inputStream->seek(0);
- CStopWatch sw;
- struct MapLoadingTime
- {
- std::string name;
- si64 time;
- MapLoadingTime(std::string name, si64 time) : name(name),
- time(time)
- {
- }
- };
- std::vector<MapLoadingTime> times;
- readHeader();
- times.push_back(MapLoadingTime("header", sw.getDiff()));
- readDisposedHeroes();
- times.push_back(MapLoadingTime("disposed heroes", sw.getDiff()));
- readAllowedArtifacts();
- times.push_back(MapLoadingTime("allowed artifacts", sw.getDiff()));
- readAllowedSpellsAbilities();
- times.push_back(MapLoadingTime("allowed spells and abilities", sw.getDiff()));
- readRumors();
- times.push_back(MapLoadingTime("rumors", sw.getDiff()));
- readPredefinedHeroes();
- times.push_back(MapLoadingTime("predefined heroes", sw.getDiff()));
- readTerrain();
- times.push_back(MapLoadingTime("terrain", sw.getDiff()));
- readDefInfo();
- times.push_back(MapLoadingTime("def info", sw.getDiff()));
- readObjects();
- times.push_back(MapLoadingTime("objects", sw.getDiff()));
- readEvents();
- times.push_back(MapLoadingTime("events", sw.getDiff()));
- // Calculate blocked / visitable positions
- for(int f = 0; f < map->objects.size(); ++f)
- {
- if(!map->objects[f]->defInfo) continue;
- map->addBlockVisTiles(map->objects[f]);
- }
- times.push_back(MapLoadingTime("blocked/visitable tiles", sw.getDiff()));
- // Print profiling times
- if(IS_PROFILING_ENABLED)
- {
- BOOST_FOREACH(MapLoadingTime & mlt, times)
- {
- tlog0 << "\tReading " << mlt.name << " took " << mlt.time << " ms." << std::endl;
- }
- }
- }
- void CMapLoaderH3M::readHeader()
- {
- // Check map for validity
- if(inputStream->getSize() < 50)
- {
- throw std::runtime_error("Corrupted map file.");
- }
- // Map version
- mapHeader->version = (EMapFormat::EMapFormat)(reader.readUInt32());
- if(mapHeader->version != EMapFormat::ROE && mapHeader->version != EMapFormat::AB && mapHeader->version != EMapFormat::SOD
- && mapHeader->version != EMapFormat::WOG)
- {
- throw std::runtime_error("Invalid map format!");
- }
- // Read map name, description, dimensions,...
- mapHeader->areAnyPlayers = reader.readBool();
- mapHeader->height = mapHeader->width = reader.readUInt32();
- mapHeader->twoLevel = reader.readBool();
- mapHeader->name = reader.readString();
- mapHeader->description = reader.readString();
- mapHeader->difficulty = reader.readInt8();
- if(mapHeader->version != EMapFormat::ROE)
- {
- mapHeader->levelLimit = reader.readUInt8();
- }
- else
- {
- mapHeader->levelLimit = 0;
- }
- readPlayerInfo();
- readVictoryLossConditions();
- readTeamInfo();
- readAllowedHeroes();
- }
- void CMapLoaderH3M::readPlayerInfo()
- {
- for(int i = 0; i < mapHeader->players.size(); ++i)
- {
- mapHeader->players[i].canHumanPlay = reader.readBool();
- mapHeader->players[i].canComputerPlay = reader.readBool();
- // If nobody can play with this player
- if((!(mapHeader->players[i].canHumanPlay || mapHeader->players[i].canComputerPlay)))
- {
- switch(mapHeader->version)
- {
- case EMapFormat::SOD:
- case EMapFormat::WOG:
- reader.skip(13);
- break;
- case EMapFormat::AB:
- reader.skip(12);
- break;
- case EMapFormat::ROE:
- reader.skip(6);
- break;
- }
- continue;
- }
- mapHeader->players[i].aiTactic = static_cast<EAiTactic::EAiTactic>(reader.readUInt8());
- if(mapHeader->version == EMapFormat::SOD || mapHeader->version == EMapFormat::WOG)
- {
- mapHeader->players[i].p7 = reader.readUInt8();
- }
- else
- {
- mapHeader->players[i].p7 = -1;
- }
- // Factions this player can choose
- ui16 allowedFactions = reader.readUInt8();
- // How many factions will be read from map
- ui16 totalFactions = GameConstants::F_NUMBER;
- if(mapHeader->version != EMapFormat::ROE)
- allowedFactions += reader.readUInt8() * 256;
- else
- totalFactions--; //exclude conflux for ROE
- for(int fact = 0; fact < totalFactions; ++fact)
- {
- if(!(allowedFactions & (1 << fact)))
- {
- mapHeader->players[i].allowedFactions.erase(fact);
- }
- }
- mapHeader->players[i].isFactionRandom = reader.readBool();
- mapHeader->players[i].hasMainTown = reader.readBool();
- if(mapHeader->players[i].hasMainTown)
- {
- if(mapHeader->version != EMapFormat::ROE)
- {
- mapHeader->players[i].generateHeroAtMainTown = reader.readBool();
- mapHeader->players[i].generateHero = reader.readBool();
- }
- else
- {
- mapHeader->players[i].generateHeroAtMainTown = true;
- mapHeader->players[i].generateHero = false;
- }
- mapHeader->players[i].posOfMainTown = readInt3();
- }
- mapHeader->players[i].hasHero = reader.readBool();
- mapHeader->players[i].customHeroID = reader.readUInt8();
- if(mapHeader->players[i].customHeroID != 0xff)
- {
- mapHeader->players[i].mainHeroPortrait = reader.readUInt8();
- if (mapHeader->players[i].mainHeroPortrait == 0xff)
- mapHeader->players[i].mainHeroPortrait = -1; //correct 1-byte -1 (0xff) into 4-byte -1
- mapHeader->players[i].mainHeroName = reader.readString();
- }
- else
- mapHeader->players[i].customHeroID = -1; //correct 1-byte -1 (0xff) into 4-byte -1
- if(mapHeader->version != EMapFormat::ROE)
- {
- mapHeader->players[i].powerPlaceholders = reader.readUInt8(); //unknown byte
- int heroCount = reader.readUInt8();
- reader.skip(3);
- for(int pp = 0; pp < heroCount; ++pp)
- {
- SHeroName vv;
- vv.heroId = reader.readUInt8();
- vv.heroName = reader.readString();
- mapHeader->players[i].heroesNames.push_back(vv);
- }
- }
- }
- }
- void CMapLoaderH3M::readVictoryLossConditions()
- {
- mapHeader->victoryCondition.obj = nullptr;
- mapHeader->victoryCondition.condition = (EVictoryConditionType::EVictoryConditionType)reader.readUInt8();
- // Specific victory conditions
- if(mapHeader->victoryCondition.condition != EVictoryConditionType::WINSTANDARD)
- {
- mapHeader->victoryCondition.allowNormalVictory = reader.readBool();
- mapHeader->victoryCondition.appliesToAI = reader.readBool();
- // Read victory conditions
- // int nr = 0;
- switch(mapHeader->victoryCondition.condition)
- {
- case EVictoryConditionType::ARTIFACT:
- {
- mapHeader->victoryCondition.objectId = reader.readUInt8();
- if (mapHeader->version != EMapFormat::ROE)
- reader.skip(1);
- break;
- }
- case EVictoryConditionType::GATHERTROOP:
- {
- mapHeader->victoryCondition.objectId = reader.readUInt8();
- if (mapHeader->version != EMapFormat::ROE)
- reader.skip(1);
- mapHeader->victoryCondition.count = reader.readUInt32();
- break;
- }
- case EVictoryConditionType::GATHERRESOURCE:
- {
- mapHeader->victoryCondition.objectId = reader.readUInt8();
- mapHeader->victoryCondition.count = reader.readUInt32();
- break;
- }
- case EVictoryConditionType::BUILDCITY:
- {
- mapHeader->victoryCondition.pos = readInt3();
- mapHeader->victoryCondition.count = reader.readUInt8();
- mapHeader->victoryCondition.objectId = reader.readUInt8();
- break;
- }
- case EVictoryConditionType::BUILDGRAIL:
- {
- int3 p = readInt3();
- if(p.z > 2)
- {
- p = int3(-1,-1,-1);
- }
- mapHeader->victoryCondition.pos = p;
- break;
- }
- case EVictoryConditionType::BEATHERO:
- case EVictoryConditionType::CAPTURECITY:
- case EVictoryConditionType::BEATMONSTER:
- {
- mapHeader->victoryCondition.pos = readInt3();
- break;
- }
- case EVictoryConditionType::TAKEDWELLINGS:
- case EVictoryConditionType::TAKEMINES:
- {
- break;
- }
- case EVictoryConditionType::TRANSPORTITEM:
- {
- mapHeader->victoryCondition.objectId = reader.readUInt8();
- mapHeader->victoryCondition.pos = readInt3();
- break;
- }
- default:
- assert(0);
- }
- }
- // Read loss conditions
- mapHeader->lossCondition.typeOfLossCon = (ELossConditionType::ELossConditionType) reader.readUInt8();
- switch(mapHeader->lossCondition.typeOfLossCon)
- {
- case ELossConditionType::LOSSCASTLE:
- case ELossConditionType::LOSSHERO:
- {
- mapHeader->lossCondition.pos = readInt3();
- break;
- }
- case ELossConditionType::TIMEEXPIRES:
- {
- mapHeader->lossCondition.timeLimit = reader.readUInt16();
- break;
- }
- }
- }
- void CMapLoaderH3M::readTeamInfo()
- {
- mapHeader->howManyTeams = reader.readUInt8();
- if(mapHeader->howManyTeams > 0)
- {
- // Teams
- for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
- {
- mapHeader->players[i].team = TeamID(reader.readUInt8());
- }
- }
- else
- {
- // No alliances
- for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
- {
- if(mapHeader->players[i].canComputerPlay || mapHeader->players[i].canHumanPlay)
- {
- mapHeader->players[i].team = TeamID(mapHeader->howManyTeams++);
- }
- }
- }
- }
- void CMapLoaderH3M::readAllowedHeroes()
- {
- mapHeader->allowedHeroes.resize(VLC->heroh->heroes.size(), true);
- const int bytes = mapHeader->version == EMapFormat::ROE ? 16 : 20;
- readBitmask(mapHeader->allowedHeroes,bytes,GameConstants::HEROES_QUANTITY, false);
- // Probably reserved for further heroes
- if(mapHeader->version > EMapFormat::ROE)
- {
- int placeholdersQty = reader.readUInt32();
- for(int p = 0; p < placeholdersQty; ++p)
- {
- mapHeader->placeholdedHeroes.push_back(reader.readUInt8());
- }
- }
- }
- void CMapLoaderH3M::readDisposedHeroes()
- {
- // Reading disposed heroes (20 bytes)
- if(map->version >= EMapFormat::SOD)
- {
- ui8 disp = reader.readUInt8();
- map->disposedHeroes.resize(disp);
- for(int g = 0; g < disp; ++g)
- {
- map->disposedHeroes[g].heroId = reader.readUInt8();
- map->disposedHeroes[g].portrait = reader.readUInt8();
- map->disposedHeroes[g].name = reader.readString();
- map->disposedHeroes[g].players = reader.readUInt8();
- }
- }
- //omitting NULLS
- reader.skip(31);
- }
- void CMapLoaderH3M::readAllowedArtifacts()
- {
- map->allowedArtifact.resize (VLC->arth->artifacts.size(),true); //handle new artifacts, make them allowed by default
- // Reading allowed artifacts: 17 or 18 bytes
- if(map->version != EMapFormat::ROE)
- {
- const int bytes = map->version == EMapFormat::AB ? 17 : 18;
- readBitmask(map->allowedArtifact,bytes,GameConstants::ARTIFACTS_QUANTITY);
- }
- // ban combo artifacts
- if (map->version == EMapFormat::ROE || map->version == EMapFormat::AB)
- {
- BOOST_FOREACH(CArtifact * artifact, VLC->arth->artifacts)
- {
- // combo
- if (artifact->constituents)
- {
- map->allowedArtifact[artifact->id] = false;
- }
- }
- if (map->version == EMapFormat::ROE)
- {
- // Armageddon's Blade
- map->allowedArtifact[128] = false;
- }
- }
- // Messy, but needed
- if(map->victoryCondition.condition == EVictoryConditionType::ARTIFACT
- || map->victoryCondition.condition == EVictoryConditionType::TRANSPORTITEM)
- {
- map->allowedArtifact[map->victoryCondition.objectId] = false;
- }
- }
- void CMapLoaderH3M::readAllowedSpellsAbilities()
- {
- // Read allowed spells
- map->allowedSpell.resize(GameConstants::SPELLS_QUANTITY, true);
- // Read allowed abilities
- map->allowedAbilities.resize(GameConstants::SKILL_QUANTITY, true);
- if(map->version >= EMapFormat::SOD)
- {
- // Reading allowed spells (9 bytes)
- const int spell_bytes = 9;
- readBitmask(map->allowedSpell, spell_bytes, GameConstants::SPELLS_QUANTITY);
- // Allowed hero's abilities (4 bytes)
- const int abil_bytes = 4;
- readBitmask(map->allowedAbilities, abil_bytes, GameConstants::SKILL_QUANTITY);
- }
- }
- void CMapLoaderH3M::readRumors()
- {
- int rumNr = reader.readUInt32();
- for(int it = 0; it < rumNr; it++)
- {
- Rumor ourRumor;
- ourRumor.name = reader.readString();
- ourRumor.text = reader.readString();
- map->rumors.push_back(ourRumor);
- }
- }
- void CMapLoaderH3M::readPredefinedHeroes()
- {
- switch(map->version)
- {
- case EMapFormat::WOG:
- case EMapFormat::SOD:
- {
- // Disposed heroes
- for(int z = 0; z < GameConstants::HEROES_QUANTITY; z++)
- {
- int custom = reader.readUInt8();
- if(!custom) continue;
- CGHeroInstance * hero = new CGHeroInstance();
- hero->ID = Obj::HERO;
- hero->subID = z;
- bool hasExp = reader.readBool();
- if(hasExp)
- {
- hero->exp = reader.readUInt32();
- }
- else
- {
- hero->exp = 0;
- }
- bool hasSecSkills = reader.readBool();
- if(hasSecSkills)
- {
- int howMany = reader.readUInt32();
- hero->secSkills.resize(howMany);
- for(int yy = 0; yy < howMany; ++yy)
- {
- hero->secSkills[yy].first = SecondarySkill(reader.readUInt8());
- hero->secSkills[yy].second = reader.readUInt8();
- }
- }
- loadArtifactsOfHero(hero);
- bool hasCustomBio = reader.readBool();
- if(hasCustomBio)
- {
- hero->biography = reader.readString();
- }
- // 0xFF is default, 00 male, 01 female
- hero->sex = reader.readUInt8();
- bool hasCustomSpells = reader.readBool();
- if(hasCustomSpells)
- {
- readSpells(hero->spells);
- }
- bool hasCustomPrimSkills = reader.readBool();
- if(hasCustomPrimSkills)
- {
- for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; xx++)
- {
- hero->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader.readUInt8());
- }
- }
- map->predefinedHeroes.push_back(hero);
- }
- break;
- }
- case EMapFormat::ROE:
- break;
- }
- }
- void CMapLoaderH3M::loadArtifactsOfHero(CGHeroInstance * hero)
- {
- bool artSet = reader.readBool();
- // True if artifact set is not default (hero has some artifacts)
- if(artSet)
- {
- for(int pom = 0; pom < 16; pom++)
- {
- loadArtifactToSlot(hero, pom);
- }
- // misc5 art //17
- if(map->version >= EMapFormat::SOD)
- {
- if(!loadArtifactToSlot(hero, ArtifactPosition::MACH4))
- {
- // catapult by default
- hero->putArtifact(ArtifactPosition::MACH4, createArtifact(ArtifactID::CATAPULT));
- }
- }
- loadArtifactToSlot(hero, ArtifactPosition::SPELLBOOK);
- // 19 //???what is that? gap in file or what? - it's probably fifth slot..
- if(map->version > EMapFormat::ROE)
- {
- loadArtifactToSlot(hero, ArtifactPosition::MISC5);
- }
- else
- {
- reader.skip(1);
- }
- // bag artifacts //20
- // number of artifacts in hero's bag
- int amount = reader.readUInt16();
- for(int ss = 0; ss < amount; ++ss)
- {
- loadArtifactToSlot(hero, GameConstants::BACKPACK_START + hero->artifactsInBackpack.size());
- }
- }
- }
- bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
- {
- const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff;
- int aid;
- if(map->version == EMapFormat::ROE)
- {
- aid = reader.readUInt8();
- }
- else
- {
- aid = reader.readUInt16();
- }
- bool isArt = aid != artmask;
- if(isArt)
- {
- if(vstd::contains(VLC->arth->bigArtifacts, aid) && slot >= GameConstants::BACKPACK_START)
- {
- tlog3 << "Warning: A big artifact (war machine) in hero's backpack, ignoring..." << std::endl;
- return false;
- }
- if(aid == 0 && slot == ArtifactPosition::MISC5)
- {
- //TODO: check how H3 handles it -> art 0 in slot 18 in AB map
- tlog3 << "Spellbook to MISC5 slot? Putting it spellbook place. AB format peculiarity ? (format "
- << static_cast<int>(map->version) << ")" << std::endl;
- slot = ArtifactPosition::SPELLBOOK;
- }
- hero->putArtifact(ArtifactPosition(slot), createArtifact(aid));
- }
- return isArt;
- }
- CArtifactInstance * CMapLoaderH3M::createArtifact(int aid, int spellID /*= -1*/)
- {
- CArtifactInstance * a = nullptr;
- if(aid >= 0)
- {
- if(spellID < 0)
- {
- a = CArtifactInstance::createNewArtifactInstance(aid);
- }
- else
- {
- a = CArtifactInstance::createScroll(SpellID(spellID).toSpell());
- }
- }
- else //FIXME: create combined artifact instance for random combined artifacts, just in case
- {
- a = new CArtifactInstance();
- }
- map->addNewArtifactInstance(a);
- //TODO make it nicer
- if(a->artType && a->artType->constituents != nullptr)
- {
- CCombinedArtifactInstance * comb = dynamic_cast<CCombinedArtifactInstance *>(a);
- BOOST_FOREACH(CCombinedArtifactInstance::ConstituentInfo & ci, comb->constituentsInfo)
- {
- map->addNewArtifactInstance(ci.art);
- }
- }
- return a;
- }
- void CMapLoaderH3M::readTerrain()
- {
- map->initTerrain();
- // Read terrain
- for(int a = 0; a < 2; ++a)
- {
- if(a == 1 && !map->twoLevel)
- {
- break;
- }
- for(int c = 0; c < map->width; c++)
- {
- for(int z = 0; z < map->height; z++)
- {
- map->terrain[z][c][a].terType = ETerrainType(reader.readUInt8());
- map->terrain[z][c][a].terView = reader.readUInt8();
- map->terrain[z][c][a].riverType = static_cast<ERiverType::ERiverType>(reader.readUInt8());
- map->terrain[z][c][a].riverDir = reader.readUInt8();
- map->terrain[z][c][a].roadType = static_cast<ERoadType::ERoadType>(reader.readUInt8());
- map->terrain[z][c][a].roadDir = reader.readUInt8();
- map->terrain[z][c][a].extTileFlags = reader.readUInt8();
- map->terrain[z][c][a].blocked = (map->terrain[z][c][a].terType == ETerrainType::ROCK ? 1 : 0); //underground tiles are always blocked
- map->terrain[z][c][a].visitable = 0;
- }
- }
- }
- }
- void CMapLoaderH3M::readDefInfo()
- {
- int defAmount = reader.readUInt32();
- map->customDefs.reserve(defAmount + 8);
- // Read custom defs
- for(int idd = 0; idd < defAmount; ++idd)
- {
- CGDefInfo * defInfo = new CGDefInfo();
- defInfo->name = reader.readString();
- std::transform(defInfo->name.begin(),defInfo->name.end(),defInfo->name.begin(),(int(*)(int))toupper);
- ui8 bytes[12];
- for(int v = 0; v < 12; ++v)
- {
- bytes[v] = reader.readUInt8();
- }
- defInfo->terrainAllowed = reader.readUInt16();
- defInfo->terrainMenu = reader.readUInt16();
- defInfo->id = Obj(reader.readUInt32());
- defInfo->subid = reader.readUInt32();
- defInfo->type = reader.readUInt8();
- defInfo->printPriority = reader.readUInt8();
- for(int zi = 0; zi < 6; ++zi)
- {
- defInfo->blockMap[zi] = reverse(bytes[zi]);
- }
- for(int zi = 0; zi < 6; ++zi)
- {
- defInfo->visitMap[zi] = reverse(bytes[6 + zi]);
- }
- reader.skip(16);
- if(defInfo->id != Obj::HERO && defInfo->id != Obj::RANDOM_HERO)
- {
- CGDefInfo * h = VLC->dobjinfo->gobjs[defInfo->id][defInfo->subid];
- if(!h)
- {
- //remove fake entry
- VLC->dobjinfo->gobjs[defInfo->id].erase(defInfo->subid);
- if(VLC->dobjinfo->gobjs[defInfo->id].size())
- {
- VLC->dobjinfo->gobjs.erase(defInfo->id);
- }
- tlog2 << "\t\tWarning: no defobjinfo entry for object ID="
- << defInfo->id << " subID=" << defInfo->subid << std::endl;
- }
- else
- {
- defInfo->visitDir = VLC->dobjinfo->gobjs[defInfo->id][defInfo->subid]->visitDir;
- }
- }
- else
- {
- defInfo->visitDir = 0xff;
- }
- if(defInfo->id == Obj::EVENT)
- {
- std::memset(defInfo->blockMap, 255, 6);
- }
- //calculating coverageMap
- defInfo->fetchInfoFromMSK();
- map->customDefs.push_back(defInfo);
- }
- //add holes - they always can appear
- for(int i = 0; i < 8 ; ++i)
- {
- map->customDefs.push_back(VLC->dobjinfo->gobjs[Obj::HOLE][i]);
- }
- }
- void CMapLoaderH3M::readObjects()
- {
- int howManyObjs = reader.readUInt32();
- for(int ww = 0; ww < howManyObjs; ++ww)
- {
- CGObjectInstance * nobj = 0;
- int3 objPos = readInt3();
- int defnum = reader.readUInt32();
- ObjectInstanceID idToBeGiven = ObjectInstanceID(map->objects.size());
- CGDefInfo * defInfo = map->customDefs.at(defnum);
- reader.skip(5);
- switch(defInfo->id)
- {
- case Obj::EVENT:
- {
- CGEvent * evnt = new CGEvent();
- nobj = evnt;
- readMessageAndGuards(evnt->message, evnt);
- evnt->gainedExp = reader.readUInt32();
- evnt->manaDiff = reader.readUInt32();
- evnt->moraleDiff = reader.readInt8();
- evnt->luckDiff = reader.readInt8();
- readResourses(evnt->resources);
- evnt->primskills.resize(GameConstants::PRIMARY_SKILLS);
- for(int x = 0; x < 4; ++x)
- {
- evnt->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
- }
- int gabn = reader.readUInt8(); // Number of gained abilities
- for(int oo = 0; oo < gabn; ++oo)
- {
- evnt->abilities.push_back(SecondarySkill(reader.readUInt8()));
- evnt->abilityLevels.push_back(reader.readUInt8());
- }
- int gart = reader.readUInt8(); // Number of gained artifacts
- for(int oo = 0; oo < gart; ++oo)
- {
- if(map->version == EMapFormat::ROE)
- {
- evnt->artifacts.push_back(ArtifactID(reader.readUInt8()));
- }
- else
- {
- evnt->artifacts.push_back(ArtifactID(reader.readUInt16()));
- }
- }
- int gspel = reader.readUInt8(); // Number of gained spells
- for(int oo = 0; oo < gspel; ++oo)
- {
- evnt->spells.push_back(SpellID(reader.readUInt8()));
- }
- int gcre = reader.readUInt8(); //number of gained creatures
- readCreatureSet(&evnt->creatures, gcre);
- reader.skip(8);
- evnt->availableFor = reader.readUInt8();
- evnt->computerActivate = reader.readUInt8();
- evnt->removeAfterVisit = reader.readUInt8();
- evnt->humanActivate = true;
- reader.skip(4);
- break;
- }
- case Obj::HERO:
- case Obj::RANDOM_HERO:
- case Obj::PRISON:
- {
- nobj = readHero(idToBeGiven);
- break;
- }
- case Obj::ARENA:
- case Obj::MERCENARY_CAMP:
- case Obj::MARLETTO_TOWER:
- case Obj::STAR_AXIS:
- case Obj::GARDEN_OF_REVELATION:
- case Obj::LEARNING_STONE:
- case Obj::TREE_OF_KNOWLEDGE:
- case Obj::LIBRARY_OF_ENLIGHTENMENT:
- case Obj::SCHOOL_OF_MAGIC:
- case Obj::SCHOOL_OF_WAR:
- {
- nobj = new CGVisitableOPH();
- break;
- }
- case Obj::MYSTICAL_GARDEN:
- case Obj::WINDMILL:
- case Obj::WATER_WHEEL:
- {
- nobj = new CGVisitableOPW();
- break;
- }
- case Obj::MONOLITH1:
- case Obj::MONOLITH2:
- case Obj::MONOLITH3:
- case Obj::SUBTERRANEAN_GATE:
- case Obj::WHIRLPOOL:
- {
- nobj = new CGTeleport();
- break;
- }
- case Obj::CAMPFIRE:
- case Obj::FLOTSAM:
- case Obj::SEA_CHEST:
- case Obj::SHIPWRECK_SURVIVOR:
- {
- nobj = new CGPickable();
- break;
- }
- case Obj::TREASURE_CHEST:
- if(defInfo->subid == 0)
- {
- nobj = new CGPickable();
- }
- else
- {
- //WoG pickable object
- //TODO: possible special handling
- nobj = new CGObjectInstance();
- }
- break;
- case Obj::MONSTER: //Monster
- case Obj::RANDOM_MONSTER:
- case Obj::RANDOM_MONSTER_L1:
- case Obj::RANDOM_MONSTER_L2:
- case Obj::RANDOM_MONSTER_L3:
- case Obj::RANDOM_MONSTER_L4:
- case Obj::RANDOM_MONSTER_L5:
- case Obj::RANDOM_MONSTER_L6:
- case Obj::RANDOM_MONSTER_L7:
- {
- CGCreature * cre = new CGCreature();
- nobj = cre;
- if(map->version > EMapFormat::ROE)
- {
- cre->identifier = reader.readUInt32();
- map->questIdentifierToId[cre->identifier] = idToBeGiven;
- }
- CStackInstance * hlp = new CStackInstance();
- hlp->count = reader.readUInt16();
- //type will be set during initialization
- cre->putStack(SlotID(0), hlp);
- cre->character = reader.readUInt8();
- bool hasMessage = reader.readBool();
- if(hasMessage)
- {
- cre->message = reader.readString();
- readResourses(cre->resources);
- int artID;
- if (map->version == EMapFormat::ROE)
- {
- artID = reader.readUInt8();
- }
- else
- {
- artID = reader.readUInt16();
- }
- if(map->version == EMapFormat::ROE)
- {
- if(artID != 0xff)
- {
- cre->gainedArtifact = ArtifactID(artID);
- }
- else
- {
- cre->gainedArtifact = ArtifactID::NONE;
- }
- }
- else
- {
- if(artID != 0xffff)
- {
- cre->gainedArtifact = ArtifactID(artID);
- }
- else
- {
- cre->gainedArtifact = ArtifactID::NONE;
- }
- }
- }
- cre->neverFlees = reader.readUInt8();
- cre->notGrowingTeam =reader.readUInt8();
- reader.skip(2);
- break;
- }
- case Obj::OCEAN_BOTTLE:
- case Obj::SIGN:
- {
- CGSignBottle * sb = new CGSignBottle();
- nobj = sb;
- sb->message = reader.readString();
- reader.skip(4);
- break;
- }
- case Obj::SEER_HUT:
- {
- nobj = readSeerHut();
- map->addQuest(nobj);
- break;
- }
- case Obj::WITCH_HUT:
- {
- CGWitchHut * wh = new CGWitchHut();
- nobj = wh;
- // in RoE we cannot specify it - all are allowed (I hope)
- if(map->version > EMapFormat::ROE)
- {
- for(int i = 0 ; i < 4; ++i)
- {
- ui8 c = reader.readUInt8();
- for(int yy = 0; yy < 8; ++yy)
- {
- if(i * 8 + yy < GameConstants::SKILL_QUANTITY)
- {
- if(c == (c | static_cast<ui8>(std::pow(2., yy))))
- {
- wh->allowedAbilities.push_back(i * 8 + yy);
- }
- }
- }
- }
- }
- else
- {
- // RoE map
- for(int gg = 0; gg < GameConstants::SKILL_QUANTITY; ++gg)
- {
- wh->allowedAbilities.push_back(gg);
- }
- }
- break;
- }
- case Obj::SCHOLAR:
- {
- CGScholar * sch = new CGScholar();
- nobj = sch;
- sch->bonusType = static_cast<CGScholar::EBonusType>(reader.readUInt8());
- sch->bonusID = reader.readUInt8();
- reader.skip(6);
- break;
- }
- case Obj::GARRISON:
- case Obj::GARRISON2:
- {
- CGGarrison * gar = new CGGarrison();
- nobj = gar;
- nobj->setOwner(PlayerColor(reader.readUInt8()));
- reader.skip(3);
- readCreatureSet(gar, 7);
- if(map->version > EMapFormat::ROE)
- {
- gar->removableUnits = reader.readBool();
- }
- else
- {
- gar->removableUnits = true;
- }
- reader.skip(8);
- break;
- }
- case Obj::ARTIFACT:
- case Obj::RANDOM_ART:
- case Obj::RANDOM_TREASURE_ART:
- case Obj::RANDOM_MINOR_ART:
- case Obj::RANDOM_MAJOR_ART:
- case Obj::RANDOM_RELIC_ART:
- case Obj::SPELL_SCROLL:
- {
- int artID = -1;
- int spellID = -1;
- CGArtifact * art = new CGArtifact();
- nobj = art;
- readMessageAndGuards(art->message, art);
- if(defInfo->id == Obj::SPELL_SCROLL)
- {
- spellID = reader.readUInt32();
- artID = 1;
- }
- else if(defInfo->id == Obj::ARTIFACT)
- {
- //specific artifact
- artID = defInfo->subid;
- }
- art->storedArtifact = createArtifact(artID, spellID);
- break;
- }
- case Obj::RANDOM_RESOURCE:
- case Obj::RESOURCE:
- {
- CGResource * res = new CGResource();
- nobj = res;
- readMessageAndGuards(res->message, res);
- res->amount = reader.readUInt32();
- if(defInfo->subid == Res::GOLD)
- {
- // Gold is multiplied by 100.
- res->amount *= 100;
- }
- reader.skip(4);
- break;
- }
- case Obj::RANDOM_TOWN:
- case Obj::TOWN:
- {
- nobj = readTown(defInfo->subid);
- break;
- }
- case Obj::MINE:
- case Obj::ABANDONED_MINE:
- {
- nobj = new CGMine();
- nobj->setOwner(PlayerColor(reader.readUInt8()));
- reader.skip(3);
- break;
- }
- case Obj::CREATURE_GENERATOR1:
- case Obj::CREATURE_GENERATOR2:
- case Obj::CREATURE_GENERATOR3:
- case Obj::CREATURE_GENERATOR4:
- {
- nobj = new CGDwelling();
- nobj->setOwner(PlayerColor(reader.readUInt8()));
- reader.skip(3);
- break;
- }
- case Obj::REFUGEE_CAMP:
- case Obj::WAR_MACHINE_FACTORY:
- {
- nobj = new CGDwelling();
- break;
- }
- case Obj::SHRINE_OF_MAGIC_INCANTATION:
- case Obj::SHRINE_OF_MAGIC_GESTURE:
- case Obj::SHRINE_OF_MAGIC_THOUGHT:
- {
- CGShrine * shr = new CGShrine();
- nobj = shr;
- ui8 raw_id = reader.readUInt8();
- if (255 == raw_id)
- {
- shr->spell = SpellID(SpellID::NONE);
- }
- else
- {
- shr->spell = SpellID(raw_id);
- }
- reader.skip(3);
- break;
- }
- case Obj::PANDORAS_BOX:
- {
- CGPandoraBox * box = new CGPandoraBox();
- nobj = box;
- readMessageAndGuards(box->message, box);
- box->gainedExp = reader.readUInt32();
- box->manaDiff = reader.readUInt32();
- box->moraleDiff = reader.readInt8();
- box->luckDiff = reader.readInt8();
- readResourses(box->resources);
- box->primskills.resize(GameConstants::PRIMARY_SKILLS);
- for(int x = 0; x < 4; ++x)
- {
- box->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader.readUInt8());
- }
- int gabn = reader.readUInt8();//number of gained abilities
- for(int oo = 0; oo < gabn; ++oo)
- {
- box->abilities.push_back(SecondarySkill(reader.readUInt8()));
- box->abilityLevels.push_back(reader.readUInt8());
- }
- int gart = reader.readUInt8(); //number of gained artifacts
- for(int oo = 0; oo < gart; ++oo)
- {
- if(map->version > EMapFormat::ROE)
- {
- box->artifacts.push_back(ArtifactID(reader.readUInt16()));
- }
- else
- {
- box->artifacts.push_back(ArtifactID(reader.readUInt8()));
- }
- }
- int gspel = reader.readUInt8(); //number of gained spells
- for(int oo = 0; oo < gspel; ++oo)
- {
- box->spells.push_back(SpellID(reader.readUInt8()));
- }
- int gcre = reader.readUInt8(); //number of gained creatures
- readCreatureSet(&box->creatures, gcre);
- reader.skip(8);
- break;
- }
- case Obj::GRAIL:
- {
- map->grailPos = objPos;
- map->grailRadious = reader.readUInt32();
- continue;
- }
- case Obj::RANDOM_DWELLING: //same as castle + level range
- case Obj::RANDOM_DWELLING_LVL: //same as castle, fixed level
- case Obj::RANDOM_DWELLING_FACTION: //level range, fixed faction
- {
- nobj = new CGDwelling();
- CSpecObjInfo * spec = nullptr;
- switch(defInfo->id)
- {
- break; case Obj::RANDOM_DWELLING: spec = new CCreGenLeveledCastleInfo();
- break; case Obj::RANDOM_DWELLING_LVL: spec = new CCreGenAsCastleInfo();
- break; case Obj::RANDOM_DWELLING_FACTION: spec = new CCreGenLeveledInfo();
- }
- spec->player = PlayerColor(reader.readUInt32());
- //216 and 217
- if (auto castleSpec = dynamic_cast<CCreGenAsCastleInfo *>(spec))
- {
- castleSpec->identifier = reader.readUInt32();
- if(!castleSpec->identifier)
- {
- castleSpec->asCastle = false;
- castleSpec->castles[0] = reader.readUInt8();
- castleSpec->castles[1] = reader.readUInt8();
- }
- else
- {
- castleSpec->asCastle = true;
- }
- }
- //216 and 218
- if (auto lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec))
- {
- lvlSpec->minLevel = std::max(reader.readUInt8(), ui8(1));
- lvlSpec->maxLevel = std::min(reader.readUInt8(), ui8(7));
- }
- nobj->setOwner(spec->player);
- static_cast<CGDwelling *>(nobj)->info = spec;
- break;
- }
- case Obj::QUEST_GUARD:
- {
- CGQuestGuard * guard = new CGQuestGuard();
- map->addQuest(guard);
- readQuest(guard);
- nobj = guard;
- break;
- }
- case Obj::FAERIE_RING:
- case Obj::SWAN_POND:
- case Obj::IDOL_OF_FORTUNE:
- case Obj::FOUNTAIN_OF_FORTUNE:
- case Obj::RALLY_FLAG:
- case Obj::OASIS:
- case Obj::TEMPLE:
- case Obj::WATERING_HOLE:
- case Obj::FOUNTAIN_OF_YOUTH:
- case Obj::BUOY:
- case Obj::MERMAID:
- case Obj::STABLES:
- {
- nobj = new CGBonusingObject();
- break;
- }
- case Obj::MAGIC_WELL:
- {
- nobj = new CGMagicWell();
- break;
- }
- case Obj::COVER_OF_DARKNESS:
- case Obj::REDWOOD_OBSERVATORY:
- case Obj::PILLAR_OF_FIRE:
- {
- nobj = new CGObservatory();
- break;
- }
- case Obj::CORPSE:
- case Obj::LEAN_TO:
- case Obj::WAGON:
- case Obj::WARRIORS_TOMB:
- {
- nobj = new CGOnceVisitable();
- break;
- }
- case Obj::BOAT:
- {
- nobj = new CGBoat();
- break;
- }
- case Obj::SIRENS:
- {
- nobj = new CGSirens();
- break;
- }
- case Obj::SHIPYARD:
- {
- nobj = new CGShipyard();
- nobj->setOwner(PlayerColor(reader.readUInt32()));
- break;
- }
- case Obj::HERO_PLACEHOLDER: //hero placeholder
- {
- CGHeroPlaceholder * hp = new CGHeroPlaceholder();
- nobj = hp;
- hp->setOwner(PlayerColor(reader.readUInt8()));
- int htid = reader.readUInt8();; //hero type id
- nobj->subID = htid;
- if(htid == 0xff)
- {
- hp->power = reader.readUInt8();
- }
- else
- {
- hp->power = 0;
- }
- break;
- }
- case Obj::KEYMASTER:
- {
- nobj = new CGKeymasterTent();
- break;
- }
- case Obj::BORDERGUARD:
- {
- nobj = new CGBorderGuard();
- map->addQuest(nobj);
- break;
- }
- case Obj::BORDER_GATE:
- {
- nobj = new CGBorderGate();
- map->addQuest (nobj);
- break;
- }
- case Obj::EYE_OF_MAGI:
- case Obj::HUT_OF_MAGI:
- {
- nobj = new CGMagi();
- break;
- }
- case Obj::CREATURE_BANK:
- case Obj::DERELICT_SHIP:
- case Obj::DRAGON_UTOPIA:
- case Obj::CRYPT:
- case Obj::SHIPWRECK:
- {
- nobj = new CBank();
- break;
- }
- case Obj::PYRAMID: //Pyramid of WoG object
- {
- if(defInfo->subid == 0)
- {
- nobj = new CGPyramid();
- }
- else
- {
- //WoG object
- //TODO: possible special handling
- nobj = new CGObjectInstance();
- }
- break;
- }
- case Obj::CARTOGRAPHER:
- {
- nobj = new CCartographer();
- break;
- }
- case Obj::MAGIC_SPRING:
- {
- nobj = new CGMagicSpring();
- break;
- }
- case Obj::DEN_OF_THIEVES:
- {
- nobj = new CGDenOfthieves();
- break;
- }
- case Obj::OBELISK:
- {
- nobj = new CGObelisk();
- break;
- }
- case Obj::LIGHTHOUSE: //Lighthouse
- {
- nobj = new CGLighthouse();
- nobj->tempOwner = PlayerColor(reader.readUInt32());
- break;
- }
- case Obj::ALTAR_OF_SACRIFICE:
- case Obj::TRADING_POST:
- case Obj::FREELANCERS_GUILD:
- case Obj::TRADING_POST_SNOW:
- {
- nobj = new CGMarket();
- break;
- }
- case Obj::UNIVERSITY:
- {
- nobj = new CGUniversity();
- break;
- }
- case Obj::BLACK_MARKET:
- {
- nobj = new CGBlackMarket();
- break;
- }
- default: //any other object
- {
- nobj = new CGObjectInstance();
- break;
- }
- }
- nobj->pos = objPos;
- nobj->ID = defInfo->id;
- nobj->id = idToBeGiven;
- if(nobj->ID != Obj::HERO && nobj->ID != Obj::HERO_PLACEHOLDER && nobj->ID != Obj::PRISON)
- {
- nobj->subID = defInfo->subid;
- }
- nobj->defInfo = defInfo;
- assert(idToBeGiven == ObjectInstanceID(map->objects.size()));
- map->objects.push_back(nobj);
- if(nobj->ID == Obj::TOWN)
- {
- map->towns.push_back(static_cast<CGTownInstance *>(nobj));
- }
- if(nobj->ID == Obj::HERO)
- {
- map->heroes.push_back(static_cast<CGHeroInstance*>(nobj));
- }
- }
- std::sort(map->heroes.begin(), map->heroes.end(), [](const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)
- {
- return a->subID < b->subID;
- });
- }
- void CMapLoaderH3M::readCreatureSet(CCreatureSet * out, int number)
- {
- const bool version = (map->version > EMapFormat::ROE);
- const int maxID = version ? 0xffff : 0xff;
- for(int ir = 0; ir < number; ++ir)
- {
- CreatureID creID;
- int count;
- if (version)
- {
- creID = CreatureID(reader.readUInt16());
- }
- else
- {
- creID = CreatureID(reader.readUInt8());
- }
- count = reader.readUInt16();
- // Empty slot
- if(creID == maxID) continue;
- CStackInstance * hlp = new CStackInstance();
- hlp->count = count;
- if(creID > maxID - 0xf)
- {
- //this will happen when random object has random army
- creID = CreatureID(maxID + 1 - creID + VLC->creh->creatures.size());
- hlp->idRand = creID;
- }
- else
- {
- hlp->setType(creID);
- }
- out->putStack(SlotID(ir), hlp);
- }
- out->validTypes(true);
- }
- CGObjectInstance * CMapLoaderH3M::readHero(ObjectInstanceID idToBeGiven)
- {
- CGHeroInstance * nhi = new CGHeroInstance();
- int identifier = 0;
- if(map->version > EMapFormat::ROE)
- {
- identifier = reader.readUInt32();
- map->questIdentifierToId[identifier] = idToBeGiven;
- }
- PlayerColor owner = PlayerColor(reader.readUInt8());
- nhi->subID = reader.readUInt8();
- for(int j = 0; j < map->predefinedHeroes.size(); ++j)
- {
- if(map->predefinedHeroes[j]->subID == nhi->subID)
- {
- tlog0 << "Hero " << nhi->subID << " will be taken from the predefined heroes list." << std::endl;
- delete nhi;
- nhi = map->predefinedHeroes[j];
- break;
- }
- }
- nhi->setOwner(owner);
- nhi->portrait = nhi->subID;
- for(int j = 0; j < map->disposedHeroes.size(); ++j)
- {
- if(map->disposedHeroes[j].heroId == nhi->subID)
- {
- nhi->name = map->disposedHeroes[j].name;
- nhi->portrait = map->disposedHeroes[j].portrait;
- break;
- }
- }
- bool hasName = reader.readBool();
- if(hasName)
- {
- nhi->name = reader.readString();
- }
- if(map->version > EMapFormat::AB)
- {
- bool hasExp = reader.readBool();
- if(hasExp)
- {
- nhi->exp = reader.readUInt32();
- }
- else
- {
- nhi->exp = 0xffffffff;
- }
- }
- else
- {
- nhi->exp = reader.readUInt32();
- //0 means "not set" in <=AB maps
- if(!nhi->exp)
- {
- nhi->exp = 0xffffffff;
- }
- }
- bool hasPortrait = reader.readBool();
- if(hasPortrait)
- {
- nhi->portrait = reader.readUInt8();
- }
- bool hasSecSkills = reader.readBool();
- if(hasSecSkills)
- {
- int howMany = reader.readUInt32();
- nhi->secSkills.resize(howMany);
- for(int yy = 0; yy < howMany; ++yy)
- {
- nhi->secSkills[yy].first = SecondarySkill(reader.readUInt8());
- nhi->secSkills[yy].second = reader.readUInt8();
- }
- }
- bool hasGarison = reader.readBool();
- if(hasGarison)
- {
- readCreatureSet(nhi, 7);
- }
- nhi->formation = reader.readUInt8();
- loadArtifactsOfHero(nhi);
- nhi->patrol.patrolRadious = reader.readUInt8();
- if(nhi->patrol.patrolRadious == 0xff)
- {
- nhi->patrol.patrolling = false;
- }
- else
- {
- nhi->patrol.patrolling = true;
- }
- if(map->version > EMapFormat::ROE)
- {
- bool hasCustomBiography = reader.readBool();
- if(hasCustomBiography)
- {
- nhi->biography = reader.readString();
- }
- nhi->sex = reader.readUInt8();
- // Remove trash
- if (nhi->sex != 0xFF)
- {
- nhi->sex &= 1;
- }
- }
- else
- {
- nhi->sex = 0xFF;
- }
- // Spells
- if(map->version > EMapFormat::AB)
- {
- bool hasCustomSpells = reader.readBool();
- if(hasCustomSpells)
- {
- nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"
- readSpells(nhi->spells);
- }
- }
- else if(map->version == EMapFormat::AB)
- {
- //we can read one spell
- ui8 buff = reader.readUInt8();
- if(buff != 254)
- {
- nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"
- if(buff < 254) //255 means no spells
- {
- nhi->spells.insert(SpellID(buff));
- }
- }
- }
- if(map->version > EMapFormat::AB)
- {
- bool hasCustomPrimSkills = reader.readBool();
- if(hasCustomPrimSkills)
- {
- for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; ++xx)
- {
- nhi->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader.readUInt8());
- }
- }
- }
- reader.skip(16);
- return nhi;
- }
- CGSeerHut * CMapLoaderH3M::readSeerHut()
- {
- CGSeerHut * hut = new CGSeerHut();
- if(map->version > EMapFormat::ROE)
- {
- readQuest(hut);
- }
- else
- {
- //RoE
- int artID = reader.readUInt8();
- if (artID != 255)
- {
- //not none quest
- hut->quest->m5arts.push_back (artID);
- hut->quest->missionType = CQuest::MISSION_ART;
- }
- else
- {
- hut->quest->missionType = CQuest::MISSION_NONE;
- }
- hut->quest->lastDay = -1; //no timeout
- hut->quest->isCustomFirst = hut->quest->isCustomNext = hut->quest->isCustomComplete = false;
- }
- if (hut->quest->missionType)
- {
- auto rewardType = static_cast<CGSeerHut::ERewardType>(reader.readUInt8());
- hut->rewardType = rewardType;
- switch(rewardType)
- {
- case CGSeerHut::EXPERIENCE:
- {
- hut->rVal = reader.readUInt32();
- break;
- }
- case CGSeerHut::MANA_POINTS:
- {
- hut->rVal = reader.readUInt32();
- break;
- }
- case CGSeerHut::MORALE_BONUS:
- {
- hut->rVal = reader.readUInt8();
- break;
- }
- case CGSeerHut::LUCK_BONUS:
- {
- hut->rVal = reader.readUInt8();
- break;
- }
- case CGSeerHut::RESOURCES:
- {
- hut->rID = reader.readUInt8();
- // Only the first 3 bytes are used. Skip the 4th.
- hut->rVal = reader.readUInt32() & 0x00ffffff;
- break;
- }
- case CGSeerHut::PRIMARY_SKILL:
- {
- hut->rID = reader.readUInt8();
- hut->rVal = reader.readUInt8();
- break;
- }
- case CGSeerHut::SECONDARY_SKILL:
- {
- hut->rID = reader.readUInt8();
- hut->rVal = reader.readUInt8();
- break;
- }
- case CGSeerHut::ARTIFACT:
- {
- if (map->version == EMapFormat::ROE)
- {
- hut->rID = reader.readUInt8();
- }
- else
- {
- hut->rID = reader.readUInt16();
- }
- break;
- }
- case CGSeerHut::SPELL:
- {
- hut->rID = reader.readUInt8();
- break;
- }
- case CGSeerHut::CREATURE:
- {
- if(map->version > EMapFormat::ROE)
- {
- hut->rID = reader.readUInt16();
- hut->rVal = reader.readUInt16();
- }
- else
- {
- hut->rID = reader.readUInt8();
- hut->rVal = reader.readUInt16();
- }
- break;
- }
- }
- reader.skip(2);
- }
- else
- {
- // missionType==255
- reader.skip(3);
- }
- return hut;
- }
- void CMapLoaderH3M::readQuest(IQuestObject * guard)
- {
- guard->quest->missionType = static_cast<CQuest::Emission>(reader.readUInt8());
- switch(guard->quest->missionType)
- {
- case 0:
- return;
- case 2:
- {
- guard->quest->m2stats.resize(4);
- for(int x = 0; x < 4; ++x)
- {
- guard->quest->m2stats[x] = reader.readUInt8();
- }
- }
- break;
- case 1:
- case 3:
- case 4:
- {
- guard->quest->m13489val = reader.readUInt32();
- break;
- }
- case 5:
- {
- int artNumber = reader.readUInt8();
- for(int yy = 0; yy < artNumber; ++yy)
- {
- int artid = reader.readUInt16();
- guard->quest->m5arts.push_back(artid);
- map->allowedArtifact[artid] = false; //these are unavailable for random generation
- }
- break;
- }
- case 6:
- {
- int typeNumber = reader.readUInt8();
- guard->quest->m6creatures.resize(typeNumber);
- for(int hh = 0; hh < typeNumber; ++hh)
- {
- guard->quest->m6creatures[hh].type = VLC->creh->creatures[reader.readUInt16()];
- guard->quest->m6creatures[hh].count = reader.readUInt16();
- }
- break;
- }
- case 7:
- {
- guard->quest->m7resources.resize(7);
- for(int x = 0; x < 7; ++x)
- {
- guard->quest->m7resources[x] = reader.readUInt32();
- }
- break;
- }
- case 8:
- case 9:
- {
- guard->quest->m13489val = reader.readUInt8();
- break;
- }
- }
- int limit = reader.readUInt32();
- if(limit == (static_cast<int>(0xffffffff)))
- {
- guard->quest->lastDay = -1;
- }
- else
- {
- guard->quest->lastDay = limit;
- }
- guard->quest->firstVisitText = reader.readString();
- guard->quest->nextVisitText = reader.readString();
- guard->quest->completedText = reader.readString();
- guard->quest->isCustomFirst = guard->quest->firstVisitText.size() > 0;
- guard->quest->isCustomNext = guard->quest->nextVisitText.size() > 0;
- guard->quest->isCustomComplete = guard->quest->completedText.size() > 0;
- }
- CGTownInstance * CMapLoaderH3M::readTown(int castleID)
- {
- CGTownInstance * nt = new CGTownInstance();
- if(map->version > EMapFormat::ROE)
- {
- nt->identifier = reader.readUInt32();
- }
- nt->tempOwner = PlayerColor(reader.readUInt8());
- bool hasName = reader.readBool();
- if(hasName)
- {
- nt->name = reader.readString();
- }
- bool hasGarrison = reader.readBool();
- if(hasGarrison)
- {
- readCreatureSet(nt, 7);
- }
- nt->formation = reader.readUInt8();
- bool hasCustomBuildings = reader.readBool();
- if(hasCustomBuildings)
- {
- readBitmask(nt->builtBuildings,6,48,false);
- readBitmask(nt->forbiddenBuildings,6,48,false);
- nt->builtBuildings = convertBuildings(nt->builtBuildings, castleID);
- nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings, castleID);
- }
- // Standard buildings
- else
- {
- bool hasFort = reader.readBool();
- if(hasFort)
- {
- nt->builtBuildings.insert(BuildingID::FORT);
- }
- //means that set of standard building should be included
- nt->builtBuildings.insert(BuildingID::DEFAULT);
- }
- if(map->version > EMapFormat::ROE)
- {
- for(int i = 0; i < 9; ++i)
- {
- ui8 c = reader.readUInt8();
- for(int yy = 0; yy < 8; ++yy)
- {
- if(i * 8 + yy < GameConstants::SPELLS_QUANTITY)
- {
- if(c == (c | static_cast<ui8>(std::pow(2., yy))))
- {
- nt->obligatorySpells.push_back(SpellID(i * 8 + yy));
- }
- }
- }
- }
- }
- for(int i = 0; i < 9; ++i)
- {
- ui8 c = reader.readUInt8();
- for(int yy = 0; yy < 8; ++yy)
- {
- if(i * 8 + yy < GameConstants::SPELLS_QUANTITY)
- {
- if(c != (c | static_cast<ui8>(std::pow(2., yy))))
- {
- nt->possibleSpells.push_back(SpellID(i * 8 + yy));
- }
- }
- }
- }
- // Read castle events
- int numberOfEvent = reader.readUInt32();
- for(int gh = 0; gh < numberOfEvent; ++gh)
- {
- CCastleEvent nce;
- nce.town = nt;
- nce.name = reader.readString();
- nce.message = reader.readString();
- readResourses(nce.resources);
- nce.players = reader.readUInt8();
- if(map->version > EMapFormat::AB)
- {
- nce.humanAffected = reader.readUInt8();
- }
- else
- {
- nce.humanAffected = true;
- }
- nce.computerAffected = reader.readUInt8();
- nce.firstOccurence = reader.readUInt16();
- nce.nextOccurence = reader.readUInt8();
- reader.skip(17);
- // New buildings
- readBitmask(nce.buildings,6,48,false);
- nce.buildings = convertBuildings(nce.buildings, castleID, false);
- nce.creatures.resize(7);
- for(int vv = 0; vv < 7; ++vv)
- {
- nce.creatures[vv] = reader.readUInt16();
- }
- reader.skip(4);
- nt->events.push_back(nce);
- }
- if(map->version > EMapFormat::AB)
- {
- nt->alignment = reader.readUInt8();
- }
- reader.skip(3);
- return nt;
- }
- std::set<BuildingID> CMapLoaderH3M::convertBuildings(const std::set<BuildingID> h3m, int castleID, bool addAuxiliary /*= true*/)
- {
- std::map<int, BuildingID> mapa;
- std::set<BuildingID> ret;
- // Note: this file is parsed many times.
- const JsonNode config(ResourceID("config/buildings5.json"));
- BOOST_FOREACH(const JsonNode & entry, config["table"].Vector())
- {
- int town = entry["town"].Float();
- if (town == castleID || town == -1)
- {
- mapa[entry["h3"].Float()] = BuildingID((si32)entry["vcmi"].Float());
- }
- }
- for(auto i = h3m.begin(); i != h3m.end(); ++i)
- {
- if(mapa[*i] >= 0)
- {
- ret.insert(mapa[*i]);
- }
- // horde buildings
- else if(mapa[*i] >= (-GameConstants::CREATURES_PER_TOWN))
- {
- int level = (mapa[*i]);
- //(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns
- ret.insert(BuildingID(level - 30));
- }
- else
- {
- tlog3 << "Conversion warning: unknown building " << *i << " in castle "
- << castleID << std::endl;
- }
- }
- if(addAuxiliary)
- {
- //village hall is always present
- ret.insert(BuildingID::VILLAGE_HALL);
- }
- if(ret.find(BuildingID::CITY_HALL) != ret.end())
- {
- ret.insert(BuildingID::EXTRA_CITY_HALL);
- }
- if(ret.find(BuildingID::TOWN_HALL) != ret.end())
- {
- ret.insert(BuildingID::EXTRA_TOWN_HALL);
- }
- if(ret.find(BuildingID::CAPITOL) != ret.end())
- {
- ret.insert(BuildingID::EXTRA_CAPITOL);
- }
- return ret;
- }
- void CMapLoaderH3M::readEvents()
- {
- int numberOfEvents = reader.readUInt32();
- for(int yyoo = 0; yyoo < numberOfEvents; ++yyoo)
- {
- CMapEvent ne;
- ne.name = reader.readString();
- ne.message = reader.readString();
- readResourses(ne.resources);
- ne.players = reader.readUInt8();
- if(map->version > EMapFormat::AB)
- {
- ne.humanAffected = reader.readUInt8();
- }
- else
- {
- ne.humanAffected = true;
- }
- ne.computerAffected = reader.readUInt8();
- ne.firstOccurence = reader.readUInt16();
- ne.nextOccurence = reader.readUInt8();
- reader.skip(17);
- map->events.push_back(ne);
- }
- }
- void CMapLoaderH3M::readMessageAndGuards(std::string& message, CCreatureSet* guards)
- {
- bool hasMessage = reader.readBool();
- if(hasMessage)
- {
- message = reader.readString();
- bool hasGuards = reader.readBool();
- if(hasGuards)
- {
- readCreatureSet(guards, 7);
- }
- reader.skip(4);
- }
- }
- void CMapLoaderH3M::readSpells(std::set<SpellID>& dest)
- {
- readBitmask(dest,9,GameConstants::SPELLS_QUANTITY,false);
- }
- void CMapLoaderH3M::readResourses(TResources& resources)
- {
- resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?
- for(int x = 0; x < 7; ++x)
- {
- resources[x] = reader.readUInt32();
- }
- }
- template <class Indenifier>
- void CMapLoaderH3M::readBitmask(std::set<Indenifier>& dest, const int byteCount, const int limit, bool negate)
- {
- std::vector<bool> temp;
- temp.resize(limit,true);
- readBitmask(temp, byteCount, limit, negate);
- for(int i = 0; i< std::min(temp.size(), static_cast<size_t>(limit)); i++)
- {
- if(temp[i])
- {
- dest.insert(static_cast<Indenifier>(i));
- }
- // else
- // {
- // dest.erase(static_cast<Indenifier>(i));
- // }
- }
- }
- void CMapLoaderH3M::readBitmask(std::vector<bool>& dest, const int byteCount, const int limit, bool negate)
- {
- for(int byte = 0; byte < byteCount; ++byte)
- {
- const ui8 mask = reader.readUInt8();
- for(int bit = 0; bit < 8; ++bit)
- {
- if(byte * 8 + bit < limit)
- {
- const bool flag = mask & (1 << bit);
- if((negate && flag) || (!negate && !flag))
- dest[byte * 8 + bit] = false;
- }
- }
- }
- }
- ui8 CMapLoaderH3M::reverse(ui8 arg)
- {
- ui8 ret = 0;
- for(int i = 0; i < 8; ++i)
- {
- if((arg & (1 << i)) >> i)
- {
- ret |= (128 >> i);
- }
- }
- return ret;
- }
|