2012-11-21 12:13:33 +03:00
|
|
|
#include "StdInc.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "CMap.h"
|
|
|
|
|
|
|
|
#include "../CArtHandler.h"
|
2012-11-20 20:53:45 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../CTownHandler.h"
|
|
|
|
#include "../CHeroHandler.h"
|
2012-11-03 17:29:54 +03:00
|
|
|
#include "../CDefObjInfoHandler.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../CSpellHandler.h"
|
2013-04-19 14:43:11 +03:00
|
|
|
#include "CMapEditManager.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2012-11-06 19:39:29 +03:00
|
|
|
SHeroName::SHeroName() : heroId(-1)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:53:45 +03:00
|
|
|
PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
|
2013-01-06 22:30:12 +03:00
|
|
|
aiTactic(EAiTactic::RANDOM), isFactionRandom(false), mainHeroPortrait(-1), hasMainTown(false),
|
|
|
|
generateHeroAtMainTown(false), team(255), generateHero(false), p7(0), hasHero(false), customHeroID(-1), powerPlaceholders(-1)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
auto allowed = VLC->townh->getDefaultAllowed();
|
|
|
|
for (size_t i=0; i<allowed.size(); i++)
|
|
|
|
if (allowed[i])
|
|
|
|
allowedFactions.insert(i);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si8 PlayerInfo::defaultCastle() const
|
|
|
|
{
|
2013-01-24 01:09:58 +03:00
|
|
|
if(allowedFactions.size() == 1 || !isFactionRandom)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2013-01-24 01:09:58 +03:00
|
|
|
// faction can't be chosen - set to first that is marked as allowed
|
|
|
|
assert(!allowedFactions.empty());
|
2012-11-11 15:23:31 +03:00
|
|
|
return *allowedFactions.begin();
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2012-11-11 15:23:31 +03:00
|
|
|
// set to random
|
|
|
|
return -1;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si8 PlayerInfo::defaultHero() const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
// we will generate hero in front of main town
|
2012-12-10 17:28:27 +03:00
|
|
|
if((generateHeroAtMainTown && hasMainTown) || hasHero)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
//random hero
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2012-11-11 15:23:31 +03:00
|
|
|
return -2;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-14 21:52:05 +03:00
|
|
|
bool PlayerInfo::canAnyonePlay() const
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
return canHumanPlay || canComputerPlay;
|
2013-04-14 21:52:05 +03:00
|
|
|
}
|
|
|
|
|
2012-11-06 19:39:29 +03:00
|
|
|
LossCondition::LossCondition() : typeOfLossCon(ELossConditionType::LOSSSTANDARD),
|
2012-11-11 15:23:31 +03:00
|
|
|
pos(int3(-1, -1, -1)), timeLimit(-1), obj(nullptr)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
VictoryCondition::VictoryCondition() : condition(EVictoryConditionType::WINSTANDARD),
|
2012-11-11 15:23:31 +03:00
|
|
|
allowNormalVictory(false), appliesToAI(false), pos(int3(-1, -1, -1)), objectId(0),
|
|
|
|
count(0), obj(nullptr)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DisposedHero::DisposedHero() : heroId(0), portrait(255), players(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CMapEvent::CMapEvent() : players(0), humanAffected(0), computerAffected(0),
|
2012-11-11 15:23:31 +03:00
|
|
|
firstOccurence(0), nextOccurence(0)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMapEvent::earlierThan(const CMapEvent & other) const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return firstOccurence < other.firstOccurence;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return firstOccurence <= other.firstOccurence;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CCastleEvent::CCastleEvent() : town(nullptr)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-06 19:39:29 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TerrainTile::TerrainTile() : terType(ETerrainType::BORDER), terView(0), riverType(ERiverType::NO_RIVER),
|
2012-11-11 15:23:31 +03:00
|
|
|
riverDir(0), roadType(ERoadType::NO_ROAD), roadDir(0), extTileFlags(0), visitable(false),
|
|
|
|
blocked(false)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::entrableTerrain(const TerrainTile * from /*= NULL*/) const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return entrableTerrain(from ? from->terType != ETerrainType::WATER : true, from ? from->terType == ETerrainType::WATER : true);
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return terType != ETerrainType::ROCK
|
2013-04-14 22:24:31 +03:00
|
|
|
&& ((allowSea && terType == ETerrainType::WATER) || (allowLand && terType != ETerrainType::WATER));
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::isClear(const TerrainTile *from /*= NULL*/) const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return entrableTerrain(from) && !blocked;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int TerrainTile::topVisitableId() const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return visitableObjects.size() ? visitableObjects.back()->ID : -1;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::isCoastal() const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return extTileFlags & 64;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::hasFavourableWinds() const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return extTileFlags & 128;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::isWater() const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return terType == ETerrainType::WATER;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:53:45 +03:00
|
|
|
CMapHeader::CMapHeader() : version(EMapFormat::SOD), height(72), width(72),
|
|
|
|
twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
allowedHeroes = VLC->heroh->getDefaultAllowed();
|
2013-03-03 20:06:03 +03:00
|
|
|
players.resize(PlayerColor::PLAYER_LIMIT_I);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMapHeader::~CMapHeader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-28 18:06:14 +03:00
|
|
|
CMap::CMap() : checksum(0), grailPos(-1, -1, -1), grailRadious(0), terrain(nullptr)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
2013-01-06 22:30:12 +03:00
|
|
|
allowedAbilities = VLC->heroh->getDefaultAllowedAbilities();
|
2013-04-21 15:49:26 +03:00
|
|
|
allowedArtifact = VLC->arth->getDefaultAllowed();
|
|
|
|
allowedSpell = VLC->spellh->getDefaultAllowed();
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMap::~CMap()
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
if(terrain)
|
|
|
|
{
|
|
|
|
for(int ii=0;ii<width;ii++)
|
|
|
|
{
|
|
|
|
for(int jj=0;jj<height;jj++)
|
|
|
|
delete [] terrain[ii][jj];
|
|
|
|
delete [] terrain[ii];
|
|
|
|
}
|
|
|
|
delete [] terrain;
|
|
|
|
}
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
for(int fx=0; fx<8; ++fx)
|
|
|
|
{
|
|
|
|
for(int fy=0; fy<6; ++fy)
|
|
|
|
{
|
|
|
|
int xVal = obj->pos.x + fx - 7;
|
|
|
|
int yVal = obj->pos.y + fy - 5;
|
|
|
|
int zVal = obj->pos.z;
|
|
|
|
if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
|
|
|
|
{
|
|
|
|
TerrainTile & curt = terrain[xVal][yVal][zVal];
|
|
|
|
if(total || ((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
|
|
|
|
{
|
|
|
|
curt.visitableObjects -= obj;
|
|
|
|
curt.visitable = curt.visitableObjects.size();
|
|
|
|
}
|
|
|
|
if(total || !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
|
|
|
|
{
|
|
|
|
curt.blockingObjects -= obj;
|
|
|
|
curt.blocked = curt.blockingObjects.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
void CMap::addBlockVisTiles(CGObjectInstance * obj)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
for(int fx=0; fx<8; ++fx)
|
|
|
|
{
|
|
|
|
for(int fy=0; fy<6; ++fy)
|
|
|
|
{
|
|
|
|
int xVal = obj->pos.x + fx - 7;
|
|
|
|
int yVal = obj->pos.y + fy - 5;
|
|
|
|
int zVal = obj->pos.z;
|
|
|
|
if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
|
|
|
|
{
|
|
|
|
TerrainTile & curt = terrain[xVal][yVal][zVal];
|
|
|
|
if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
|
|
|
|
{
|
|
|
|
curt.visitableObjects.push_back(obj);
|
|
|
|
curt.visitable = true;
|
|
|
|
}
|
|
|
|
if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
|
|
|
|
{
|
|
|
|
curt.blockingObjects.push_back(obj);
|
|
|
|
curt.blocked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CGHeroInstance * CMap::getHero(int heroID)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
for(ui32 i=0; i<heroes.size();i++)
|
|
|
|
if(heroes[i]->subID == heroID)
|
|
|
|
return heroes[i];
|
|
|
|
return nullptr;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
bool CMap::isInTheMap(const int3 & pos) const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-20 20:53:45 +03:00
|
|
|
if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
|
|
|
|
|| pos.z > (twoLevel ? 1 : 0))
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return false;
|
2012-11-20 20:53:45 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
TerrainTile & CMap::getTile(const int3 & tile)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(isInTheMap(tile));
|
2012-11-11 15:23:31 +03:00
|
|
|
return terrain[tile.x][tile.y][tile.z];
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
const TerrainTile & CMap::getTile(const int3 & tile) const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(isInTheMap(tile));
|
2012-11-11 15:23:31 +03:00
|
|
|
return terrain[tile.x][tile.y][tile.z];
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CMap::isWaterTile(const int3 &pos) const
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return isInTheMap(pos) && getTile(pos).terType == ETerrainType::WATER;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:53:45 +03:00
|
|
|
const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, bool lookForHero)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-20 20:53:45 +03:00
|
|
|
const std::vector<CGObjectInstance *> & objs = getTile(pos).visitableObjects;
|
2012-11-11 15:23:31 +03:00
|
|
|
assert(objs.size());
|
|
|
|
if(objs.size() > 1 && lookForHero && objs.front()->ID != Obj::HERO)
|
|
|
|
{
|
|
|
|
assert(objs.back()->ID == Obj::HERO);
|
|
|
|
return objs.back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return objs.front();
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMap::checkForObjectives()
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
if(isInTheMap(victoryCondition.pos))
|
2012-11-20 20:53:45 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
victoryCondition.obj = getObjectiveObjectFrom(victoryCondition.pos, victoryCondition.condition == EVictoryConditionType::BEATHERO);
|
2012-11-20 20:53:45 +03:00
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2012-11-11 15:23:31 +03:00
|
|
|
if(isInTheMap(lossCondition.pos))
|
2012-11-20 20:53:45 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
lossCondition.obj = getObjectiveObjectFrom(lossCondition.pos, lossCondition.typeOfLossCon == ELossConditionType::LOSSHERO);
|
2012-11-20 20:53:45 +03:00
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:53:45 +03:00
|
|
|
void CMap::addNewArtifactInstance(CArtifactInstance * art)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2013-02-14 02:55:42 +03:00
|
|
|
art->id = ArtifactInstanceID(artInstances.size());
|
2012-11-11 15:23:31 +03:00
|
|
|
artInstances.push_back(art);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:53:45 +03:00
|
|
|
void CMap::eraseArtifactInstance(CArtifactInstance * art)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2013-02-14 02:55:42 +03:00
|
|
|
assert(artInstances[art->id.getNum()] == art);
|
|
|
|
artInstances[art->id.getNum()].dellNull();
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
|
|
|
|
void CMap::addQuest(CGObjectInstance * quest)
|
|
|
|
{
|
|
|
|
auto q = dynamic_cast<IQuestObject *>(quest);
|
|
|
|
q->quest->qid = quests.size();
|
|
|
|
quests.push_back(q->quest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMap::initTerrain()
|
|
|
|
{
|
|
|
|
terrain = new TerrainTile**[width];
|
|
|
|
for(int i = 0; i < width; ++i)
|
|
|
|
{
|
|
|
|
terrain[i] = new TerrainTile*[height];
|
|
|
|
for(int j = 0; j < height; ++j)
|
|
|
|
{
|
|
|
|
terrain[i][j] = new TerrainTile[twoLevel ? 2 : 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 14:43:11 +03:00
|
|
|
|
|
|
|
CMapEditManager * CMap::getEditManager()
|
|
|
|
{
|
|
|
|
if(!editManager) editManager = make_unique<CMapEditManager>(this);
|
|
|
|
return editManager.get();
|
|
|
|
}
|