2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CMap.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
|
|
|
|
*
|
|
|
|
*/
|
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"
|
2023-01-11 15:17:24 +02:00
|
|
|
#include "../RiverHandler.h"
|
|
|
|
#include "../RoadHandler.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../TerrainHandler.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/ObjectTemplate.h"
|
2013-12-29 14:27:38 +03:00
|
|
|
#include "../CGeneralTextHandler.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "../spells/CSpellHandler.h"
|
2018-03-31 07:56:40 +02:00
|
|
|
#include "../CSkillHandler.h"
|
2013-04-19 14:43:11 +03:00
|
|
|
#include "CMapEditManager.h"
|
2023-05-20 17:28:35 +02:00
|
|
|
#include "CMapOperation.h"
|
2016-11-13 12:38:42 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void Rumor::serializeJson(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
handler.serializeString("name", name);
|
|
|
|
handler.serializeString("text", text);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-21 11:34:23 +02:00
|
|
|
TerrainTile::TerrainTile():
|
|
|
|
terType(nullptr),
|
|
|
|
terView(0),
|
2023-01-10 01:05:28 +02:00
|
|
|
riverType(VLC->riverTypeHandler->getById(River::NO_RIVER)),
|
2022-09-21 11:34:23 +02:00
|
|
|
riverDir(0),
|
2023-01-10 01:05:28 +02:00
|
|
|
roadType(VLC->roadTypeHandler->getById(Road::NO_ROAD)),
|
2022-09-21 11:34:23 +02:00
|
|
|
roadDir(0),
|
|
|
|
extTileFlags(0),
|
|
|
|
visitable(false),
|
2012-11-11 15:23:31 +03:00
|
|
|
blocked(false)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
bool TerrainTile::entrableTerrain(const TerrainTile * from) const
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
2022-09-21 11:34:23 +02:00
|
|
|
return entrableTerrain(from ? from->terType->isLand() : true, from ? from->terType->isWater() : true);
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
|
|
|
|
{
|
2022-09-21 11:34:23 +02:00
|
|
|
return terType->isPassable()
|
|
|
|
&& ((allowSea && terType->isWater()) || (allowLand && terType->isLand()));
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
bool TerrainTile::isClear(const TerrainTile * from) 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
|
|
|
}
|
|
|
|
|
2015-03-08 14:05:24 +02:00
|
|
|
Obj TerrainTile::topVisitableId(bool excludeTop) const
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
2015-03-08 14:05:24 +02:00
|
|
|
return topVisitableObj(excludeTop) ? topVisitableObj(excludeTop)->ID : Obj(Obj::NO_OBJ);
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2015-03-08 14:05:24 +02:00
|
|
|
CGObjectInstance * TerrainTile::topVisitableObj(bool excludeTop) const
|
2014-02-17 20:28:39 +03:00
|
|
|
{
|
2015-11-22 04:34:37 +02:00
|
|
|
if(visitableObjects.empty() || (excludeTop && visitableObjects.size() == 1))
|
|
|
|
return nullptr;
|
2015-03-08 14:05:24 +02:00
|
|
|
|
2015-11-22 04:34:37 +02:00
|
|
|
if(excludeTop)
|
|
|
|
return visitableObjects[visitableObjects.size()-2];
|
|
|
|
|
|
|
|
return visitableObjects.back();
|
2014-02-17 20:28:39 +03:00
|
|
|
}
|
|
|
|
|
2015-11-29 11:32:06 +02:00
|
|
|
EDiggingStatus TerrainTile::getDiggingStatus(const bool excludeTop) const
|
|
|
|
{
|
2022-09-21 11:34:23 +02:00
|
|
|
if(terType->isWater() || !terType->isPassable())
|
2015-11-29 11:32:06 +02:00
|
|
|
return EDiggingStatus::WRONG_TERRAIN;
|
|
|
|
|
|
|
|
int allowedBlocked = excludeTop ? 1 : 0;
|
|
|
|
if(blockingObjects.size() > allowedBlocked || topVisitableObj(excludeTop))
|
|
|
|
return EDiggingStatus::TILE_OCCUPIED;
|
|
|
|
else
|
|
|
|
return EDiggingStatus::CAN_DIG;
|
|
|
|
}
|
|
|
|
|
2015-12-06 01:23:41 +02:00
|
|
|
bool TerrainTile::hasFavorableWinds() const
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return extTileFlags & 128;
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainTile::isWater() const
|
|
|
|
{
|
2022-09-21 11:34:23 +02:00
|
|
|
return terType->isWater();
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2016-11-27 21:37:41 +02:00
|
|
|
CMap::CMap()
|
2023-06-06 15:32:26 +02:00
|
|
|
: checksum(0)
|
|
|
|
, grailPos(-1, -1, -1)
|
|
|
|
, grailRadius(0)
|
|
|
|
, uidCounter(0)
|
2012-11-06 19:39:29 +03:00
|
|
|
{
|
2013-05-21 22:26:31 +03:00
|
|
|
allHeroes.resize(allowedHeroes.size());
|
2018-03-31 07:56:40 +02:00
|
|
|
allowedAbilities = VLC->skillh->getDefaultAllowed();
|
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()
|
|
|
|
{
|
2022-12-03 18:47:03 +02:00
|
|
|
getEditManager()->getUndoManager().clearAll();
|
2016-08-19 15:56:26 +02:00
|
|
|
|
|
|
|
for(auto obj : objects)
|
|
|
|
obj.dellNull();
|
2016-09-13 00:30:47 +02:00
|
|
|
|
|
|
|
for(auto quest : quests)
|
|
|
|
quest.dellNull();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
resetStaticData();
|
2012-11-06 19:39:29 +03:00
|
|
|
}
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
|
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
const int zVal = obj->pos.z;
|
|
|
|
for(int fx = 0; fx < obj->getWidth(); ++fx)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
int xVal = obj->pos.x - fx;
|
|
|
|
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 yVal = obj->pos.y - fy;
|
2022-09-18 16:39:10 +02:00
|
|
|
if(xVal>=0 && xVal < width && yVal>=0 && yVal < height)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
TerrainTile & curt = terrain[zVal][xVal][yVal];
|
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)
|
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
const int zVal = obj->pos.z;
|
|
|
|
for(int fx = 0; fx < obj->getWidth(); ++fx)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
int xVal = obj->pos.x - fx;
|
|
|
|
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 yVal = obj->pos.y - fy;
|
2022-09-18 16:39:10 +02:00
|
|
|
if(xVal>=0 && xVal < width && yVal >= 0 && yVal < height)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
TerrainTile & curt = terrain[zVal][xVal][yVal];
|
|
|
|
if(obj->visitableAt(xVal, yVal))
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
curt.visitableObjects.push_back(obj);
|
|
|
|
curt.visitable = true;
|
|
|
|
}
|
2022-09-18 16:39:10 +02: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;
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int z = 0; z < levels; z++)
|
2014-04-01 14:53:28 +03:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int x = 0; x < width; x++)
|
2014-04-01 14:53:28 +03:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
guardingCreaturePositions[z][x][y] = guardingCreaturePosition(int3(x, y, z));
|
|
|
|
}
|
2014-04-01 14:53:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-15 20:46:28 +02:00
|
|
|
bool CMap::isCoastalTile(const int3 & pos) const
|
|
|
|
{
|
|
|
|
//todo: refactoring: extract neighbor tile iterator and use it in GameState
|
|
|
|
static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
|
|
|
|
int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
|
|
|
|
|
|
|
|
if(!isInTheMap(pos))
|
|
|
|
{
|
2017-08-12 13:36:04 +02:00
|
|
|
logGlobal->error("Coastal check outside of map: %s", pos.toString());
|
2015-11-13 12:08:25 +02:00
|
|
|
return false;
|
2015-08-15 20:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(isWaterTile(pos))
|
|
|
|
return false;
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & dir : dirs)
|
2015-08-15 20:46:28 +02:00
|
|
|
{
|
2015-11-13 12:08:25 +02:00
|
|
|
const int3 hlp = pos + dir;
|
|
|
|
|
2015-08-15 20:46:28 +02:00
|
|
|
if(!isInTheMap(hlp))
|
|
|
|
continue;
|
|
|
|
const TerrainTile &hlpt = getTile(hlp);
|
|
|
|
if(hlpt.isWater())
|
2015-11-13 12:08:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-15 20:46:28 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
bool CMap::isInTheMap(const int3 & pos) const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
return pos.x >= 0 && pos.y >= 0 && pos.z >= 0 && pos.x < width && pos.y < height && pos.z <= (twoLevel ? 1 : 0);
|
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));
|
2022-09-18 16:39:10 +02:00
|
|
|
return terrain[tile.z][tile.x][tile.y];
|
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));
|
2022-09-18 16:39:10 +02:00
|
|
|
return terrain[tile.z][tile.x][tile.y];
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CMap::isWaterTile(const int3 &pos) const
|
|
|
|
{
|
2015-08-15 20:46:28 +02:00
|
|
|
return isInTheMap(pos) && getTile(pos).isWater();
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
2016-12-21 11:10:37 +02:00
|
|
|
bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
|
|
|
|
{
|
|
|
|
const TerrainTile * dstTile = &getTile(dst);
|
|
|
|
const TerrainTile * srcTile = &getTile(src);
|
|
|
|
return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const
|
2014-05-24 02:07:54 +03:00
|
|
|
{
|
2015-04-10 08:50:21 +02:00
|
|
|
if (!pom->entrableTerrain()) //rock is never accessible
|
|
|
|
return false;
|
2023-02-11 18:30:06 +02:00
|
|
|
for(auto * obj : pom->visitableObjects) //checking destination tile
|
2014-05-24 02:07:54 +03:00
|
|
|
{
|
2015-05-25 17:04:17 +02:00
|
|
|
if(!vstd::contains(pom->blockingObjects, obj)) //this visitable object is not blocking, ignore
|
2014-05-24 02:07:54 +03:00
|
|
|
continue;
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
if (!obj->appearance->isVisitableFrom(src.x - dst.x, src.y - dst.y))
|
2014-05-24 02:07:54 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
const CGObjectInstance * CMap::getObjectiveObjectFrom(const 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
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
logGlobal->error("Failed to find object of type %d at %s", static_cast<int>(type), pos.toString());
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->error("Will try to find closest matching object");
|
2014-01-01 19:12:53 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Will use %s from %s", bestMatch->getObjectName(), bestMatch->pos.toString());
|
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
|
|
|
{
|
2014-08-07 19:40:22 +03:00
|
|
|
auto patcher = [&](EventCondition cond) -> EventExpression::Variant
|
2013-12-29 14:27:38 +03:00
|
|
|
{
|
|
|
|
switch (cond.condition)
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::HAVE_ARTIFACT:
|
2023-01-02 15:58:56 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", VLC->arth->objects[cond.objectType]->getNameTranslated());
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::HAVE_CREATURES:
|
2023-01-02 18:00:51 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->objects[cond.objectType]->getNameSingularTranslated());
|
2023-02-11 18:30:06 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%d", std::to_string(cond.value));
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::HAVE_RESOURCES:
|
2013-12-29 14:27:38 +03:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", VLC->generaltexth->restypes[cond.objectType]);
|
2023-02-11 18:30:06 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%d", std::to_string(cond.value));
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::HAVE_BUILDING:
|
2013-12-29 14:27:38 +03:00
|
|
|
if (isInTheMap(cond.position))
|
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::CONTROL:
|
2013-12-29 14:27:38 +03:00
|
|
|
if (isInTheMap(cond.position))
|
2023-02-11 18:30:06 +02:00
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, static_cast<Obj::EObj>(cond.objectType));
|
2013-12-29 14:27:38 +03:00
|
|
|
|
|
|
|
if (cond.object)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
const auto * town = dynamic_cast<const CGTownInstance *>(cond.object);
|
2013-12-29 14:27:38 +03:00
|
|
|
if (town)
|
2023-01-04 15:17:50 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", town->getNameTranslated());
|
2023-02-11 18:30:06 +02:00
|
|
|
const auto * hero = dynamic_cast<const CGHeroInstance *>(cond.object);
|
2013-12-29 14:27:38 +03:00
|
|
|
if (hero)
|
2023-01-02 13:27:03 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", hero->getNameTranslated());
|
2013-12-29 14:27:38 +03:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::DESTROY:
|
2013-12-29 14:27:38 +03:00
|
|
|
if (isInTheMap(cond.position))
|
2023-02-11 18:30:06 +02:00
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, static_cast<Obj::EObj>(cond.objectType));
|
2013-12-29 14:27:38 +03:00
|
|
|
|
|
|
|
if (cond.object)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
const auto * hero = dynamic_cast<const CGHeroInstance *>(cond.object);
|
2013-12-29 14:27:38 +03:00
|
|
|
if (hero)
|
2023-01-02 13:27:03 +02:00
|
|
|
boost::algorithm::replace_first(event.onFulfill, "%s", hero->getNameTranslated());
|
2013-12-29 14:27:38 +03:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
|
|
|
case EventCondition::TRANSPORT:
|
2013-12-29 14:27:38 +03:00
|
|
|
cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
|
2016-11-13 12:38:42 +02:00
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
//break; case EventCondition::DAYS_PASSED:
|
|
|
|
//break; case EventCondition::IS_HUMAN:
|
|
|
|
//break; case EventCondition::DAYS_WITHOUT_TOWN:
|
|
|
|
//break; case EventCondition::STANDARD_WIN:
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
//TODO: support new condition format
|
|
|
|
case EventCondition::HAVE_0:
|
|
|
|
break;
|
|
|
|
case EventCondition::DESTROY_0:
|
|
|
|
break;
|
|
|
|
case EventCondition::HAVE_BUILDING_0:
|
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
}
|
2014-08-07 19:40:22 +03:00
|
|
|
return cond;
|
2013-12-29 14:27:38 +03:00
|
|
|
};
|
2014-08-07 19:40:22 +03:00
|
|
|
event.trigger = event.trigger.morph(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
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
art->id = ArtifactInstanceID(static_cast<si32>(artInstances.size()));
|
|
|
|
artInstances.emplace_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
|
|
|
{
|
2022-09-17 13:04:01 +02:00
|
|
|
//TODO: handle for artifacts removed in map editor
|
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
|
|
|
|
2017-05-28 15:23:42 +02:00
|
|
|
void CMap::addNewQuestInstance(CQuest* quest)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
quest->qid = static_cast<si32>(quests.size());
|
2023-02-11 18:30:06 +02:00
|
|
|
quests.emplace_back(quest);
|
2017-05-28 15:23:42 +02:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMap::removeQuestInstance(CQuest * quest)
|
|
|
|
|
|
|
|
{
|
|
|
|
//TODO: should be called only by map editor.
|
|
|
|
//During game, completed quests or quests from removed objects stay forever
|
|
|
|
|
|
|
|
//Shift indexes
|
|
|
|
auto iter = std::next(quests.begin(), quest->qid);
|
|
|
|
iter = quests.erase(iter);
|
|
|
|
for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
|
|
|
|
{
|
|
|
|
(*iter)->qid = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMap::setUniqueInstanceName(CGObjectInstance * obj)
|
|
|
|
|
|
|
|
{
|
|
|
|
//this gives object unique name even if objects are removed later
|
|
|
|
|
|
|
|
auto uid = uidCounter++;
|
|
|
|
|
|
|
|
boost::format fmt("%s_%d");
|
|
|
|
fmt % obj->typeName % uid;
|
|
|
|
obj->instanceName = fmt.str();
|
|
|
|
}
|
|
|
|
|
2016-02-22 18:26:42 +02:00
|
|
|
void CMap::addNewObject(CGObjectInstance * obj)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
if(obj->id != ObjectInstanceID(static_cast<si32>(objects.size())))
|
2016-02-22 18:26:42 +02:00
|
|
|
throw std::runtime_error("Invalid object instance id");
|
|
|
|
|
2022-11-15 02:20:55 +02:00
|
|
|
if(obj->instanceName.empty())
|
2016-02-22 18:26:42 +02:00
|
|
|
throw std::runtime_error("Object instance name missing");
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
if (vstd::contains(instanceNames, obj->instanceName))
|
2016-09-08 13:24:28 +02:00
|
|
|
throw std::runtime_error("Object instance name duplicated: "+obj->instanceName);
|
2016-02-22 18:26:42 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
objects.emplace_back(obj);
|
2016-09-08 13:24:28 +02:00
|
|
|
instanceNames[obj->instanceName] = obj;
|
|
|
|
addBlockVisTiles(obj);
|
2016-02-22 18:26:42 +02:00
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
//TODO: how about defeated heroes recruited again?
|
|
|
|
|
2017-05-28 15:23:42 +02:00
|
|
|
obj->afterAddToMap(this);
|
2016-02-22 18:26:42 +02:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMap::moveObject(CGObjectInstance * obj, const int3 & pos)
|
|
|
|
{
|
|
|
|
removeBlockVisTiles(obj);
|
|
|
|
obj->pos = pos;
|
|
|
|
addBlockVisTiles(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMap::removeObject(CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
removeBlockVisTiles(obj);
|
|
|
|
instanceNames.erase(obj->instanceName);
|
|
|
|
|
|
|
|
//update indeces
|
|
|
|
auto iter = std::next(objects.begin(), obj->id.getNum());
|
|
|
|
iter = objects.erase(iter);
|
|
|
|
for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
|
|
|
|
{
|
|
|
|
(*iter)->id = ObjectInstanceID(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->afterRemoveFromMap(this);
|
|
|
|
|
|
|
|
//TOOD: Clean artifact instances (mostly worn by hero?) and quests related to this object
|
|
|
|
}
|
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
void CMap::initTerrain()
|
|
|
|
{
|
2023-06-06 15:32:26 +02:00
|
|
|
terrain.resize(boost::extents[levels()][width][height]);
|
|
|
|
guardingCreaturePositions.resize(boost::extents[levels()][width][height]);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-04-19 14:43:11 +03:00
|
|
|
|
|
|
|
CMapEditManager * CMap::getEditManager()
|
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
if(!editManager) editManager = std::make_unique<CMapEditManager>(this);
|
2013-04-19 14:43:11 +03:00
|
|
|
return editManager.get();
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
void CMap::resetStaticData()
|
|
|
|
{
|
|
|
|
CGKeys::reset();
|
|
|
|
CGMagi::reset();
|
|
|
|
CGObelisk::reset();
|
|
|
|
CGTownInstance::reset();
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|