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"
|
2013-12-29 14:27:38 +03:00
|
|
|
#include "../CCreatureHandler.h"
|
2012-11-20 20:53:45 +03:00
|
|
|
#include "../CTownHandler.h"
|
|
|
|
#include "../CHeroHandler.h"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "../mapObjects/CObjectClassesHandler.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2013-12-29 14:27:38 +03:00
|
|
|
#include "../CGeneralTextHandler.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-12-23 18:59:37 +03:00
|
|
|
aiTactic(EAiTactic::RANDOM), isFactionRandom(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
|
|
|
|
generateHeroAtMainTown(false), team(255), hasRandomHero(false), /* following are unused */ generateHero(false), p7(0), powerPlaceholders(-1)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2014-05-23 18:12:31 +03:00
|
|
|
allowedFactions = VLC->townh->getAllowedFactions();
|
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
|
2013-12-23 18:59:37 +03:00
|
|
|
if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
|
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
|
|
|
}
|
|
|
|
|
2013-12-23 18:59:37 +03:00
|
|
|
bool PlayerInfo::hasCustomMainHero() const
|
|
|
|
{
|
|
|
|
return !mainCustomHeroName.empty() && mainCustomHeroPortrait != -1;
|
|
|
|
}
|
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
EventCondition::EventCondition(EWinLoseType condition):
|
|
|
|
object(nullptr),
|
|
|
|
value(-1),
|
|
|
|
objectType(-1),
|
|
|
|
position(-1, -1, -1),
|
|
|
|
condition(condition)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:47:23 +03:00
|
|
|
EventCondition::EventCondition(EWinLoseType condition, si32 value, si32 objectType, int3 position):
|
|
|
|
object(nullptr),
|
|
|
|
value(value),
|
|
|
|
objectType(objectType),
|
|
|
|
position(position),
|
|
|
|
condition(condition)
|
|
|
|
{}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
bool TerrainTile::entrableTerrain(const TerrainTile * from /*= nullptr*/) const
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
bool TerrainTile::isClear(const TerrainTile *from /*= nullptr*/) const
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2014-02-17 20:28:39 +03:00
|
|
|
CGObjectInstance * TerrainTile::topVisitableObj() const
|
|
|
|
{
|
|
|
|
return visitableObjects.size() ? visitableObjects.back() : nullptr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-11 20:36:11 +03:00
|
|
|
const int CMapHeader::MAP_SIZE_SMALL = 36;
|
|
|
|
const int CMapHeader::MAP_SIZE_MIDDLE = 72;
|
|
|
|
const int CMapHeader::MAP_SIZE_LARGE = 108;
|
|
|
|
const int CMapHeader::MAP_SIZE_XLARGE = 144;
|
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
void CMapHeader::setupEvents()
|
|
|
|
{
|
|
|
|
EventCondition victoryCondition(EventCondition::STANDARD_WIN);
|
|
|
|
EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
|
|
|
|
defeatCondition.value = 7;
|
|
|
|
|
|
|
|
//Victory condition - defeat all
|
|
|
|
TriggeredEvent standardVictory;
|
|
|
|
standardVictory.effect.type = EventEffect::VICTORY;
|
|
|
|
standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
|
|
|
|
standardVictory.identifier = "standardVictory";
|
|
|
|
standardVictory.description = ""; // TODO: display in quest window
|
|
|
|
standardVictory.onFulfill = VLC->generaltexth->allTexts[659];
|
|
|
|
standardVictory.trigger = EventExpression(victoryCondition);
|
|
|
|
|
|
|
|
//Loss condition - 7 days without town
|
|
|
|
TriggeredEvent standardDefeat;
|
|
|
|
standardDefeat.effect.type = EventEffect::DEFEAT;
|
|
|
|
standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];
|
|
|
|
standardDefeat.identifier = "standardDefeat";
|
|
|
|
standardDefeat.description = ""; // TODO: display in quest window
|
|
|
|
standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];
|
|
|
|
standardDefeat.trigger = EventExpression(defeatCondition);
|
|
|
|
|
|
|
|
triggeredEvents.push_back(standardVictory);
|
|
|
|
triggeredEvents.push_back(standardDefeat);
|
|
|
|
|
|
|
|
victoryIconIndex = 11;
|
|
|
|
victoryMessage = VLC->generaltexth->victoryConditions[0];
|
|
|
|
|
|
|
|
defeatIconIndex = 3;
|
|
|
|
defeatMessage = VLC->generaltexth->lossCondtions[0];
|
|
|
|
}
|
|
|
|
|
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-12-29 14:27:38 +03:00
|
|
|
setupEvents();
|
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-05-21 22:26:31 +03:00
|
|
|
allHeroes.resize(allowedHeroes.size());
|
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)
|
|
|
|
{
|
2014-04-01 14:53:28 +03:00
|
|
|
for (int i=0; i<width; i++)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2014-04-01 14:53:28 +03:00
|
|
|
for(int j=0; j<height; j++)
|
|
|
|
{
|
|
|
|
delete [] terrain[i][j];
|
|
|
|
delete [] guardingCreaturePositions[i][j];
|
|
|
|
}
|
|
|
|
delete [] terrain[i];
|
|
|
|
delete [] guardingCreaturePositions[i];
|
2012-11-11 15:23:31 +03:00
|
|
|
}
|
|
|
|
delete [] terrain;
|
2014-04-01 14:53:28 +03:00
|
|
|
delete [] guardingCreaturePositions;
|
2012-11-11 15:23:31 +03:00
|
|
|
}
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
|
|
|
|
{
|
2014-01-03 02:48:38 +03:00
|
|
|
for(int fx=0; fx<obj->getWidth(); ++fx)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2014-01-03 02:48:38 +03:00
|
|
|
for(int fy=0; fy<obj->getHeight(); ++fy)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2014-01-03 02:48:38 +03:00
|
|
|
int xVal = obj->pos.x - fx;
|
|
|
|
int yVal = obj->pos.y - fy;
|
2012-11-11 15:23:31 +03:00
|
|
|
int zVal = obj->pos.z;
|
|
|
|
if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
|
|
|
|
{
|
|
|
|
TerrainTile & curt = terrain[xVal][yVal][zVal];
|
2014-01-03 02:48:38 +03:00
|
|
|
if(total || obj->visitableAt(xVal, yVal))
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
curt.visitableObjects -= obj;
|
|
|
|
curt.visitable = curt.visitableObjects.size();
|
|
|
|
}
|
2014-01-03 02:48:38 +03:00
|
|
|
if(total || obj->blockingAt(xVal, yVal))
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2014-01-03 02:48:38 +03:00
|
|
|
for(int fx=0; fx<obj->getWidth(); ++fx)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2014-01-03 02:48:38 +03:00
|
|
|
for(int fy=0; fy<obj->getHeight(); ++fy)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2014-01-03 02:48:38 +03:00
|
|
|
int xVal = obj->pos.x - fx;
|
|
|
|
int yVal = obj->pos.y - fy;
|
2012-11-11 15:23:31 +03:00
|
|
|
int zVal = obj->pos.z;
|
|
|
|
if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
|
|
|
|
{
|
|
|
|
TerrainTile & curt = terrain[xVal][yVal][zVal];
|
2014-01-03 02:48:38 +03:00
|
|
|
if( obj->visitableAt(xVal, yVal))
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
curt.visitableObjects.push_back(obj);
|
|
|
|
curt.visitable = true;
|
|
|
|
}
|
2014-01-03 02:48:38 +03:00
|
|
|
if( obj->blockingAt(xVal, yVal))
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
curt.blockingObjects.push_back(obj);
|
|
|
|
curt.blocked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2014-04-01 14:53:28 +03:00
|
|
|
void CMap::calculateGuardingGreaturePositions()
|
|
|
|
{
|
|
|
|
int levels = twoLevel ? 2 : 1;
|
|
|
|
for (int i=0; i<width; i++)
|
|
|
|
{
|
|
|
|
for(int j=0; j<height; j++)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < levels; k++)
|
|
|
|
guardingCreaturePositions[i][j][k] = guardingCreaturePosition(int3(i,j,k));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
CGHeroInstance * CMap::getHero(int heroID)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : heroesOnMap)
|
|
|
|
if(elem->subID == heroID)
|
|
|
|
return elem;
|
2012-11-11 15:23:31 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-24 02:07:54 +03:00
|
|
|
bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst ) const
|
|
|
|
{
|
|
|
|
for(ui32 b=0; b<pom->visitableObjects.size(); ++b) //checking destination tile
|
|
|
|
{
|
|
|
|
if(!vstd::contains(pom->blockingObjects, pom->visitableObjects[b])) //this visitable object is not blocking, ignore
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CGObjectInstance * obj = pom->visitableObjects[b];
|
|
|
|
|
|
|
|
if (!obj->appearance.isVisitableFrom(src.x - dst.x, src.y - dst.y))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-04-01 14:53:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int3 CMap::guardingCreaturePosition (int3 pos) const
|
2014-05-24 02:07:54 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
const int3 originalPos = pos;
|
|
|
|
// Give monster at position priority.
|
|
|
|
if (!isInTheMap(pos))
|
|
|
|
return int3(-1, -1, -1);
|
|
|
|
const TerrainTile &posTile = getTile(pos);
|
|
|
|
if (posTile.visitable)
|
|
|
|
{
|
|
|
|
for (CGObjectInstance* obj : posTile.visitableObjects)
|
|
|
|
{
|
|
|
|
if(obj->blockVisit)
|
|
|
|
{
|
|
|
|
if (obj->ID == Obj::MONSTER) // Monster
|
|
|
|
return pos;
|
|
|
|
else
|
|
|
|
return int3(-1, -1, -1); //blockvis objects are not guarded by neighbouring creatures
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if there are any monsters adjacent.
|
|
|
|
bool water = posTile.isWater();
|
|
|
|
|
|
|
|
pos -= int3(1, 1, 0); // Start with top left.
|
|
|
|
for (int dx = 0; dx < 3; dx++)
|
|
|
|
{
|
|
|
|
for (int dy = 0; dy < 3; dy++)
|
|
|
|
{
|
|
|
|
if (isInTheMap(pos))
|
|
|
|
{
|
|
|
|
const auto & tile = getTile(pos);
|
|
|
|
if (tile.visitable && (tile.isWater() == water))
|
|
|
|
{
|
|
|
|
for (CGObjectInstance* obj : tile.visitableObjects)
|
|
|
|
{
|
|
|
|
if (obj->ID == Obj::MONSTER && checkForVisitableDir(pos, &posTile, originalPos)) // Monster being able to attack investigated tile
|
|
|
|
{
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos.y++;
|
|
|
|
}
|
|
|
|
pos.y -= 3;
|
|
|
|
pos.x++;
|
|
|
|
}
|
|
|
|
|
2014-04-01 14:53:28 +03:00
|
|
|
return int3(-1, -1, -1);
|
|
|
|
}
|
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, Obj::EObj type)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
for (CGObjectInstance * object : getTile(pos).visitableObjects)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
if (object->ID == type)
|
|
|
|
return object;
|
2012-11-11 15:23:31 +03:00
|
|
|
}
|
2014-01-01 19:12:53 +03:00
|
|
|
// There is weird bug because of which sometimes heroes will not be found properly despite having correct position
|
|
|
|
// Try to workaround that and find closest object that we can use
|
|
|
|
|
|
|
|
logGlobal->errorStream() << "Failed to find object of type " << int(type) << " at " << pos;
|
|
|
|
logGlobal->errorStream() << "Will try to find closest matching object";
|
|
|
|
|
|
|
|
CGObjectInstance * bestMatch = nullptr;
|
|
|
|
for (CGObjectInstance * object : objects)
|
|
|
|
{
|
2014-02-01 15:54:55 +03:00
|
|
|
if (object && object->ID == type)
|
2014-01-01 19:12:53 +03:00
|
|
|
{
|
|
|
|
if (bestMatch == nullptr)
|
|
|
|
bestMatch = object;
|
|
|
|
else
|
|
|
|
{
|
2014-05-25 21:42:25 +03:00
|
|
|
if (object->pos.dist2dSQ(pos) < bestMatch->pos.dist2dSQ(pos))
|
2014-01-01 19:12:53 +03:00
|
|
|
bestMatch = object;// closer than one we already found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
|
|
|
|
|
2014-06-24 20:39:36 +03:00
|
|
|
logGlobal->errorStream() << "Will use " << bestMatch->getObjectName() << " from " << bestMatch->pos;
|
2014-01-01 19:12:53 +03:00
|
|
|
return bestMatch;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMap::checkForObjectives()
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
// NOTE: probably should be moved to MapFormatH3M.cpp
|
|
|
|
for (TriggeredEvent & event : triggeredEvents)
|
2012-11-20 20:53:45 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
auto patcher = [&](EventCondition & cond)
|
|
|
|
{
|
|
|
|
switch (cond.condition)
|
|
|
|
{
|
|
|
|
break; case EventCondition::HAVE_ARTIFACT:
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", VLC->arth->artifacts[cond.objectType]->Name());
|
|
|
|
|
|
|
|
break; case EventCondition::HAVE_CREATURES:
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->creatures[cond.objectType]->nameSing);
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
|
|
|
|
|
|
|
|
break; case EventCondition::HAVE_RESOURCES:
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", VLC->generaltexth->restypes[cond.objectType]);
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
|
|
|
|
|
|
|
|
break; case EventCondition::HAVE_BUILDING:
|
|
|
|
if (isInTheMap(cond.position))
|
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
|
|
|
|
|
|
|
|
break; case EventCondition::CONTROL:
|
|
|
|
if (isInTheMap(cond.position))
|
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
|
|
|
|
|
|
|
|
if (cond.object)
|
|
|
|
{
|
|
|
|
const CGTownInstance *town = dynamic_cast<const CGTownInstance*>(cond.object);
|
|
|
|
if (town)
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", town->name);
|
|
|
|
const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
|
|
|
|
if (hero)
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
break; case EventCondition::DESTROY:
|
|
|
|
if (isInTheMap(cond.position))
|
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
|
|
|
|
|
|
|
|
if (cond.object)
|
|
|
|
{
|
|
|
|
const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
|
|
|
|
if (hero)
|
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
|
|
|
|
}
|
|
|
|
break; case EventCondition::TRANSPORT:
|
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
|
|
|
|
//break; case EventCondition::DAYS_PASSED:
|
|
|
|
//break; case EventCondition::IS_HUMAN:
|
|
|
|
//break; case EventCondition::DAYS_WITHOUT_TOWN:
|
|
|
|
//break; case EventCondition::STANDARD_WIN:
|
|
|
|
}
|
|
|
|
};
|
|
|
|
event.trigger.forEach(patcher);
|
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()
|
|
|
|
{
|
2014-04-01 14:53:28 +03:00
|
|
|
int level = twoLevel ? 2 : 1;
|
2013-01-06 22:30:12 +03:00
|
|
|
terrain = new TerrainTile**[width];
|
2014-04-01 14:53:28 +03:00
|
|
|
guardingCreaturePositions = new int3**[width];
|
|
|
|
for (int i = 0; i < width; ++i)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
terrain[i] = new TerrainTile*[height];
|
2014-04-01 14:53:28 +03:00
|
|
|
guardingCreaturePositions[i] = new int3*[height];
|
|
|
|
for (int j = 0; j < height; ++j)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2014-04-01 14:53:28 +03:00
|
|
|
terrain[i][j] = new TerrainTile[level];
|
|
|
|
guardingCreaturePositions[i][j] = new int3[level];
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 14:43:11 +03:00
|
|
|
|
|
|
|
CMapEditManager * CMap::getEditManager()
|
|
|
|
{
|
|
|
|
if(!editManager) editManager = make_unique<CMapEditManager>(this);
|
|
|
|
return editManager.get();
|
|
|
|
}
|