#ifndef __MAP_H__ #define __MAP_H__ #ifdef _MSC_VER #pragma warning (disable : 4482) #endif #include #include #include #include #include #include "../global.h" #ifndef _MSC_VER #include "../hch/CObjectHandler.h" #include "../hch/CDefObjInfoHandler.h" #endif /* * map.h, 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 * */ class CGDefInfo; class CGObjectInstance; class CGHeroInstance; class CQuest; class CGTownInstance; enum ESortBy{_playerAm, _size, _format, _name, _viccon, _loscon}; enum EDefType {TOWN_DEF, HERO_DEF, CREATURES_DEF, SEERHUT_DEF, RESOURCE_DEF, TERRAINOBJ_DEF, EVENTOBJ_DEF, SIGN_DEF, GARRISON_DEF, ARTIFACT_DEF, WITCHHUT_DEF, SCHOLAR_DEF, PLAYERONLY_DEF, SHRINE_DEF, SPELLSCROLL_DEF, PANDORA_DEF, GRAIL_DEF, CREGEN_DEF, CREGEN2_DEF, CREGEN3_DEF, BORDERGUARD_DEF, HEROPLACEHOLDER_DEF}; class DLL_EXPORT CSpecObjInfo { public: virtual ~CSpecObjInfo(){}; }; class DLL_EXPORT CCreGenObjInfo : public CSpecObjInfo { public: unsigned char player; //owner bool asCastle; int identifier; unsigned char castles[2]; //allowed castles }; class DLL_EXPORT CCreGen2ObjInfo : public CSpecObjInfo { public: unsigned char player; //owner bool asCastle; int identifier; unsigned char castles[2]; //allowed castles unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6> }; class DLL_EXPORT CCreGen3ObjInfo : public CSpecObjInfo { public: unsigned char player; //owner unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6> }; struct DLL_EXPORT TerrainTile { enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock}; enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver}; enum Eroad {dirtRoad=1, grazvelRoad, cobblestoneRoad}; EterrainType tertype; // type of terrain unsigned char terview; // look of terrain Eriver nuine; // type of Eriver (0 if there is no river) unsigned char rivDir; // direction of Eriver Eroad malle; // type of Eroad (0 if there is no river) unsigned char roadDir; // direction of Eroad unsigned char siodmyTajemniczyBajt; //bitfield, info whether this tile is coastal and how to rotate tile graphics bool visitable; //false = not visitable; true = visitable bool blocked; //false = free; true = blocked; std::vector visitableObjects; //pointers to objects hero can visit while being on this tile std::vector blockingObjects; //pointers to objects that are blocking this tile template void serialize(Handler &h, const int version) { h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt; if(!h.saving) { visitable = blocked = false; //these flags (and obj vectors) will be restored in map serialization } } }; struct DLL_EXPORT SheroName //name of starting hero { int heroID; std::string heroName; template void serialize(Handler &h, const int version) { h & heroID & heroName; } }; struct DLL_EXPORT PlayerInfo { si32 p7, p8, p9; ui8 powerPlacehodlers; //q-ty of hero placeholders containing hero type, WARNING: powerPlacehodlers sometimes gives false 0 (eg. even if there is one placeholder), maybe different meaning??? ui8 canHumanPlay; ui8 canComputerPlay; ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer) ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux); ui8 isFactionRandom; ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard std::string mainHeroName; std::vector heroesNames; ui8 hasMainTown; ui8 generateHeroAtMainTown; int3 posOfMainTown; ui8 team; ui8 generateHero; PlayerInfo(): p7(0), p8(0), p9(0), canHumanPlay(0), canComputerPlay(0), AITactic(0), allowedFactions(0), isFactionRandom(0), mainHeroPortrait(0), hasMainTown(0), generateHeroAtMainTown(0), team(255), generateHero(0) {}; template void serialize(Handler &h, const int version) { h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom & mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown & posOfMainTown & team & generateHero; } }; struct DLL_EXPORT LossCondition { ElossCon typeOfLossCon; int3 pos; si32 timeLimit; // in days; -1 if not used const CGObjectInstance *obj; //set during map parsing: hero/town (depending on typeOfLossCon); NULL if not used template void serialize(Handler &h, const int version) { h & typeOfLossCon & pos & timeLimit & obj; } LossCondition(); }; struct DLL_EXPORT CVictoryCondition { EvictoryConditions condition; //ID of condition ui8 allowNormalVictory, appliesToAI; int3 pos; //pos of city to upgrade (3); pos of town to build grail, {-1,-1,-1} if not relevant (4); hero pos (5); town pos(6); monster pos (7); destination pos(8) si32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8) si32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3); const CGObjectInstance *obj; //object of specific monster / city / hero instance (NULL if not used); set during map parsing CVictoryCondition(); template void serialize(Handler &h, const int version) { h & condition & allowNormalVictory & appliesToAI & pos & ID & count & obj; } }; struct DLL_EXPORT Rumor { std::string name, text; template void serialize(Handler &h, const int version) { h & name & text; } }; struct DLL_EXPORT DisposedHero { ui32 ID; ui16 portrait; //0xFF - default std::string name; ui8 players; //who can hire this hero (bitfield) template void serialize(Handler &h, const int version) { h & ID & portrait & name & players; } }; class DLL_EXPORT CMapEvent { public: std::string name, message; std::vector resources; //gained / taken resources ui8 players; //affected players ui8 humanAffected; ui8 computerAffected; ui32 firstOccurence; ui32 nextOccurence; //after nextOccurance day event will occur; if it it 0, event occures only one time; template void serialize(Handler &h, const int version) { h & name & message & resources & players & humanAffected & computerAffected & firstOccurence & nextOccurence; } bool operator<(const CMapEvent &b) const { return firstOccurence < b.firstOccurence; } }; class DLL_EXPORT CMapHeader { public: enum Eformat {invalid, WoG=0x33, AB=0x15, RoE=0x0e, SoD=0x1c}; Eformat version; // version of map Eformat ui8 areAnyPLayers; // if there are any playable players on map si32 height, width, twoLevel; //sizes std::string name; //name of map std::string description; //and description ui8 difficulty; // 0 easy - 4 impossible ui8 levelLimit; LossCondition lossCondition; CVictoryCondition victoryCondition; //victory conditions std::vector players; // info about players - size 8 ui8 howManyTeams; std::vector allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed std::vector placeholdedHeroes; //ID of types of heroes in placeholders void initFromMemory(const unsigned char *bufor, int &i); void loadViCLossConditions( const unsigned char * bufor, int &i); void loadPlayerInfo( int &pom, const unsigned char * bufor, int &i); CMapHeader(const unsigned char *map); //an argument is a reference to string described a map (unpacked) CMapHeader(); virtual ~CMapHeader(); template void serialize(Handler &h, const int Version) { h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers; h & players & lossCondition & victoryCondition & howManyTeams; } }; class DLL_EXPORT CMapInfo : public CMapHeader { public: ui8 seldiff; //selected difficulty (only in saved games) std::string filename; std::string date; int playerAmnt, humenPlayers; CMapInfo(); CMapInfo(const std::string &fname, const unsigned char *map); void init(const std::string &fname, const unsigned char *map); void countPlayers(); }; class DLL_EXPORT mapSorter { public: ESortBy sortBy; bool operator()(const CMapHeader *a, const CMapHeader *b) { switch (sortBy) { case _format: return (a->versionversion); break; case _loscon: return (a->lossCondition.typeOfLossConlossCondition.typeOfLossCon); break; case _playerAm: int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA; playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0; for (int i=0;i<8;i++) { if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;} else if (a->players[i].canComputerPlay) {playerAmntA++;} if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;} else if (b->players[i].canComputerPlay) {playerAmntB++;} } if (playerAmntB!=playerAmntA) return (playerAmntAwidthwidth); break; case _viccon: return (a->victoryCondition.condition < b->victoryCondition.condition); break; case _name: return (a->namename); break; default: return (a->namename); break; } }; mapSorter(ESortBy es):sortBy(es){}; }; struct DLL_EXPORT Mapa : public CMapHeader { ui32 checksum; TerrainTile*** terrain; std::vector rumors; std::vector disposedHeroes; std::vector predefinedHeroes; std::vector defy; // list of .def files with definitions from .h3m (may be custom) std::vector allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed std::vector allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed std::vector allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed std::list events; int3 grailPos; int grailRadious; std::vector objects; std::vector heroes; std::vector towns; void initFromBytes( const unsigned char * bufor); //creates map from decompressed .h3m data void readEvents( const unsigned char * bufor, int &i); void readObjects( const unsigned char * bufor, int &i); void loadQuest( CQuest * guard, const unsigned char * bufor, int & i); void readDefInfo( const unsigned char * bufor, int &i); void readTerrain( const unsigned char * bufor, int &i); void readPredefinedHeroes( const unsigned char * bufor, int &i); void readHeader( const unsigned char * bufor, int &i); void readRumors( const unsigned char * bufor, int &i); void loadHero( CGObjectInstance * &nobj, const unsigned char * bufor, int &i); void loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid); int loadSeerHut( const unsigned char * bufor, int i, CGObjectInstance *& nobj); void checkForObjectives(); void addBlockVisTiles(CGObjectInstance * obj); void removeBlockVisTiles(CGObjectInstance * obj, bool total=false); Mapa(std::string filename); //creates map structure from .h3m file Mapa(); ~Mapa(); TerrainTile &getTile(int3 tile); const TerrainTile &getTile(int3 tile) const; CGHeroInstance * getHero(int ID, int mode=0); bool isInTheMap(const int3 &pos) const; bool isWaterTile(const int3 &pos) const; //out-of-pos safe template void serialize(Handler &h, const int formatVersion) { h & static_cast(*this); h & rumors & allowedSpell & allowedAbilities & allowedArtifact & allowedHeroes & events & grailPos; //TODO: viccondetails if(h.saving) { //saving terrain for (int i = 0; i < width ; i++) for (int j = 0; j < height ; j++) for (int k = 0; k <= twoLevel ; k++) h & terrain[i][j][k]; } else { //loading terrain terrain = new TerrainTile**[width]; // allocate memory for (int ii=0;ii defs; if(h.saving) //create vector with all defs used on map { for(unsigned int i=0; idefInfo->serial = -1; //set serial to serial -1 - indicates that def is not present in defs vector for(unsigned int i=0; idefInfo; if(cur->serial < 0) { cur->serial = defs.size(); defs.push_back(cur); } } } h & ((h.saving) ? defs : defy); //objects if(h.saving) { ui32 hlp = objects.size(); h & hlp; } else { ui32 hlp; h & hlp; objects.resize(hlp); } //static structures h & CGTeleport::objs; h & CGTeleport::gates; h & CGKeys::playerKeyMap; h & CGMagi::eyelist; h & CGPyramid::pyramidConfig; for(unsigned int i=0; idefInfo->serial) : shlp); //read / write pos of definfo in defs vector if(!h.saving) obj->defInfo = defy[shlp]; } } if(!h.saving) { for(unsigned int i=0; iID == HEROI_TYPE) heroes.push_back(static_cast(objects[i])); else if(objects[i]->ID == TOWNI_TYPE) towns.push_back(static_cast(objects[i])); addBlockVisTiles(objects[i]); //recreate blockvis map } for(unsigned int i=0; ipos; vistile.x++; for(unsigned int j=0; jpos) //hero stands on the town entrance { if(heroes[i]->inTownGarrison) { towns[j]->garrisonHero = heroes[i]; removeBlockVisTiles(heroes[i]); } else { towns[j]->visitingHero = heroes[i]; } heroes[i]->visitedTown = towns[j]; break; } } vistile.x -= 2; //manifest pos const TerrainTile &t = getTile(vistile); if(t.tertype != TerrainTile::water) continue; //hero stands on the water - he must be in the boat for(unsigned int j = 0; j < t.visitableObjects.size(); j++) { if(t.visitableObjects[j]->ID == 8) { CGBoat *b = static_cast(t.visitableObjects[j]); heroes[i]->boat = b; b->hero = heroes[i]; removeBlockVisTiles(b); break; } } } //heroes loop } //!saving } }; #endif // __MAP_H__