1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-21 00:19:29 +02:00

Merged changes from upstream and fixed compilation caused by API changes

This commit is contained in:
Ivan Savenko
2014-06-03 22:45:18 +03:00
143 changed files with 8788 additions and 8457 deletions

View File

@ -1,14 +1,3 @@
/*
* CMapGenerator.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 "CMapGenerator.h"
@ -20,33 +9,84 @@
#include "../CObjectClassesHandler.h"
#include "../CTownHandler.h"
#include "../StringConstants.h"
#include "../filesystem/Filesystem.h"
#include "CRmgTemplate.h"
#include "CRmgTemplateZone.h"
#include "CZonePlacer.h"
CMapGenerator::CMapGenerator() : mapGenOptions(nullptr), randomSeed(0)
void CMapGenerator::foreach_neighbour(const int3 &pos, std::function<void(int3& pos)> foo)
{
for(const int3 &dir : dirs)
{
int3 n = pos + dir;
if(map->isInTheMap(n))
foo(n);
}
}
CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
mapGenOptions(mapGenOptions), randomSeed(randomSeed), monolithIndex(0)
{
rand.setSeed(randomSeed);
}
void CMapGenerator::initTiles()
{
map->initTerrain();
int width = map->width;
int height = map->height;
int level = map->twoLevel ? 2 : 1;
tiles = new CTileInfo**[width];
for (int i = 0; i < width; ++i)
{
tiles[i] = new CTileInfo*[height];
for (int j = 0; j < height; ++j)
{
tiles[i][j] = new CTileInfo[level];
}
}
}
CMapGenerator::~CMapGenerator()
{
//FIXME: what if map is not present anymore?
if (tiles && map)
{
for (int i=0; i < map->width; i++)
{
for(int j=0; j < map->height; j++)
{
delete [] tiles[i][j];
}
delete [] tiles[i];
}
delete [] tiles;
}
}
std::unique_ptr<CMap> CMapGenerator::generate(CMapGenOptions * mapGenOptions, int randomSeed /*= std::time(nullptr)*/)
std::unique_ptr<CMap> CMapGenerator::generate()
{
this->randomSeed = randomSeed;
rand.setSeed(this->randomSeed);
this->mapGenOptions = mapGenOptions;
this->mapGenOptions->finalize(rand);
mapGenOptions->finalize(rand);
map = make_unique<CMap>();
editManager = map->getEditManager();
editManager->getUndoManager().setUndoRedoLimit(0);
addHeaderInfo();
genTerrain();
genTowns();
try
{
editManager->getUndoManager().setUndoRedoLimit(0);
addHeaderInfo();
initTiles();
genZones();
map->calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
fillZones();
}
catch (rmgException &e)
{
logGlobal->errorStream() << "Random map generation received exception: " << e.what();
}
return std::move(map);
}
@ -57,10 +97,10 @@ std::string CMapGenerator::getMapDescription() const
std::stringstream ss;
ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, Random seed was %d, size %dx%d") +
", levels %s, humans %d, computers %d, water %s, monster %s, second expansion map") % mapGenOptions->getMapTemplate()->getName() %
", levels %s, humans %d, computers %d, water %s, monster %s, second expansion map") % mapGenOptions->getMapTemplate()->getName() %
randomSeed % map->width % map->height % (map->twoLevel ? "2" : "1") % static_cast<int>(mapGenOptions->getPlayerCount()) %
static_cast<int>(mapGenOptions->getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions->getWaterContent()] %
monsterStrengthStr[mapGenOptions->getMonsterStrength()]);
monsterStrengthStr[mapGenOptions->getMonsterStrength()]);
for(const auto & pair : mapGenOptions->getPlayersSettings())
{
@ -126,7 +166,7 @@ void CMapGenerator::addPlayerInfo()
player.canHumanPlay = true;
}
auto itTeam = RandomGeneratorUtil::nextItem(teamNumbers[j], rand);
auto itTeam = RandomGeneratorUtil::nextItem(teamNumbers[j], rand);
player.team = TeamID(*itTeam);
teamNumbers[j].erase(itTeam);
map->players[pSettings.getColor().getNum()] = player;
@ -136,47 +176,106 @@ void CMapGenerator::addPlayerInfo()
+ (mapGenOptions->getCompOnlyTeamCount() == 0 ? mapGenOptions->getCompOnlyPlayerCount() : mapGenOptions->getCompOnlyTeamCount());
}
void CMapGenerator::genTerrain()
void CMapGenerator::genZones()
{
map->initTerrain();
editManager->clearTerrain(&rand);
editManager->getTerrainSelection().selectRange(MapRect(int3(4, 4, 0), 24, 30));
editManager->getTerrainSelection().selectRange(MapRect(int3(0, 0, 0), mapGenOptions->getWidth(), mapGenOptions->getHeight()));
editManager->drawTerrain(ETerrainType::GRASS, &rand);
auto pcnt = mapGenOptions->getPlayerCount();
auto w = mapGenOptions->getWidth();
auto h = mapGenOptions->getHeight();
auto tmpl = mapGenOptions->getMapTemplate();
zones = tmpl->getZones(); //copy from template (refactor?)
int player_per_side = zones.size() > 4 ? 3 : 2;
logGlobal->infoStream() << boost::format("Map size %d %d, players per side %d") % w % h % player_per_side;
CZonePlacer placer(this);
placer.placeZones(mapGenOptions, &rand);
placer.assignZones(mapGenOptions);
int i = 0;
for(auto const it : zones)
{
CRmgTemplateZone * zone = it.second;
zone->setType(i < pcnt ? ETemplateZoneType::PLAYER_START : ETemplateZoneType::TREASURE);
this->zones[it.first] = zone;
++i;
}
logGlobal->infoStream() << "Zones generated successfully";
}
void CMapGenerator::genTowns()
{
//FIXME mock gen
const int3 townPos[2] = { int3(11, 7, 0), int3(19,7, 0) };
void CMapGenerator::fillZones()
{
logGlobal->infoStream() << "Started filling zones";
for(size_t i = 0; i < map->players.size(); ++i)
createConnections();
for (auto it : zones)
{
auto & playerInfo = map->players[i];
if(!playerInfo.canAnyonePlay()) break;
//make sure all connections are passable before creating borders
it.second->createBorder(this);
it.second->fill(this);
}
logGlobal->infoStream() << "Zones filled successfully";
}
PlayerColor owner(i);
int side = i % 2;
auto town = new CGTownInstance();
town->ID = Obj::TOWN;
int townId = mapGenOptions->getPlayersSettings().find(PlayerColor(i))->second.getStartingTown();
if(townId == CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
void CMapGenerator::createConnections()
{
for (auto connection : mapGenOptions->getMapTemplate()->getConnections())
{
auto zoneA = connection.getZoneA();
auto zoneB = connection.getZoneB();
//rearrange tiles in random order
auto tilesCopy = zoneA->getTileInfo();
std::vector<int3> tiles(tilesCopy.begin(), tilesCopy.end());
//TODO: hwo to use std::shuffle with our generator?
//std::random_shuffle (tiles.begin(), tiles.end(), &gen->rand.nextInt);
int i, n;
n = (tiles.end() - tiles.begin());
for (i=n-1; i>0; --i)
{
// select default towns
townId = rand.nextInt(8);
std::swap (tiles.begin()[i],tiles.begin()[rand.nextInt(i+1)]);
}
town->subID = townId;
town->tempOwner = owner;
town->appearance = VLC->objtypeh->getHandlerFor(town->ID, town->subID)->getTemplates(map->getTile(townPos[side]).terType).front();
town->builtBuildings.insert(BuildingID::FORT);
town->builtBuildings.insert(BuildingID::DEFAULT);
editManager->insertObject(town, int3(townPos[side].x, townPos[side].y + (i / 2) * 5, 0));
// Update player info
playerInfo.allowedFactions.clear();
playerInfo.allowedFactions.insert(townId);
playerInfo.hasMainTown = true;
playerInfo.posOfMainTown = town->pos - int3(2, 0, 0);
playerInfo.generateHeroAtMainTown = true;
int3 guardPos(-1,-1,-1);
auto otherZoneTiles = zoneB->getTileInfo();
auto otherZoneCenter = zoneB->getPos();
for (auto tile : tiles)
{
foreach_neighbour (tile, [&guardPos, tile, &otherZoneTiles](int3 &pos)
{
if (vstd::contains(otherZoneTiles, pos))
guardPos = tile;
});
if (guardPos.valid())
{
zoneA->addMonster (this, guardPos, connection.getGuardStrength()); //TODO: set value according to template
//zones can make paths only in their own area
zoneA->crunchPath (this, guardPos, zoneA->getPos(), zoneA->getId()); //make connection towards our zone center
zoneB->crunchPath (this, guardPos, zoneB->getPos(), zoneB->getId()); //make connection towards other zone center
break; //we're done with this connection
}
}
if (!guardPos.valid())
{
auto teleport1 = new CGTeleport;
teleport1->ID = Obj::MONOLITH_TWO_WAY;
teleport1->subID = getNextMonlithIndex();
auto teleport2 = new CGTeleport(*teleport1);
zoneA->addRequiredObject (teleport1, connection.getGuardStrength());
zoneB->addRequiredObject (teleport2, connection.getGuardStrength());
}
}
}
@ -191,3 +290,73 @@ void CMapGenerator::addHeaderInfo()
map->difficulty = 1;
addPlayerInfo();
}
std::map<TRmgTemplateZoneId, CRmgTemplateZone*> CMapGenerator::getZones() const
{
return zones;
}
bool CMapGenerator::isBlocked(const int3 &tile) const
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
return tiles[tile.x][tile.y][tile.z].isBlocked();
}
bool CMapGenerator::shouldBeBlocked(const int3 &tile) const
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
return tiles[tile.x][tile.y][tile.z].shouldBeBlocked();
}
bool CMapGenerator::isPossible(const int3 &tile) const
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
return tiles[tile.x][tile.y][tile.z].isPossible();
}
bool CMapGenerator::isFree(const int3 &tile) const
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
return tiles[tile.x][tile.y][tile.z].isFree();
}
void CMapGenerator::setOccupied(const int3 &tile, ETileType::ETileType state)
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
tiles[tile.x][tile.y][tile.z].setOccupied(state);
}
CTileInfo CMapGenerator::getTile(const int3& tile) const
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
return tiles[tile.x][tile.y][tile.z];
}
void CMapGenerator::setNearestObjectDistance(int3 &tile, int value)
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
tiles[tile.x][tile.y][tile.z].setNearestObjectDistance(value);
}
int CMapGenerator::getNearestObjectDistance(const int3 &tile) const
{
if (!map->isInTheMap(tile))
throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
return tiles[tile.x][tile.y][tile.z].getNearestObjectDistance();
}
int CMapGenerator::getNextMonlithIndex()
{
return monolithIndex++;
}