2013-01-06 22:30:12 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CMapGenerator.h"
|
|
|
|
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../mapping/CMap.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../mapping/CMapEditManager.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../CObjectHandler.h"
|
|
|
|
#include "../CDefObjInfoHandler.h"
|
|
|
|
#include "../CTownHandler.h"
|
|
|
|
#include "../StringConstants.h"
|
2014-05-22 17:27:13 +03:00
|
|
|
#include "../filesystem/Filesystem.h"
|
2014-05-22 20:25:17 +03:00
|
|
|
#include "CRmgTemplate.h"
|
|
|
|
#include "CRmgTemplateZone.h"
|
2014-05-24 13:42:06 +03:00
|
|
|
#include "CZonePlacer.h"
|
2013-08-19 21:20:11 +03:00
|
|
|
|
2014-05-24 19:39:58 +03:00
|
|
|
void CMapGenerator::foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo)
|
|
|
|
{
|
|
|
|
//for(const int3 &dir : dirs)
|
|
|
|
//{
|
|
|
|
// const int3 n = pos + dir;
|
|
|
|
// if(map->isInTheMap(n))
|
|
|
|
// foo(pos+dir);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-23 12:56:51 +03:00
|
|
|
CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
|
2014-05-22 17:27:13 +03:00
|
|
|
mapGenOptions(mapGenOptions), randomSeed(randomSeed)
|
|
|
|
{
|
2014-05-22 20:25:17 +03:00
|
|
|
rand.setSeed(randomSeed);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMapGenerator::~CMapGenerator()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-23 14:23:03 +03:00
|
|
|
std::unique_ptr<CMap> CMapGenerator::generate()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2014-05-24 13:42:06 +03:00
|
|
|
mapGenOptions->finalize(rand);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-05-24 13:42:06 +03:00
|
|
|
map = make_unique<CMap>();
|
|
|
|
editManager = map->getEditManager();
|
2014-05-23 18:12:31 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
editManager->getUndoManager().setUndoRedoLimit(0);
|
|
|
|
addHeaderInfo();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-05-23 18:12:31 +03:00
|
|
|
genZones();
|
2014-05-23 20:14:20 +03:00
|
|
|
map->calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
|
2014-05-23 18:12:31 +03:00
|
|
|
fillZones();
|
|
|
|
}
|
|
|
|
catch (rmgException &e)
|
|
|
|
{
|
2014-05-23 20:14:20 +03:00
|
|
|
logGlobal->errorStream() << "Random map generation received exception: " << e.what();
|
2014-05-23 18:12:31 +03:00
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
return std::move(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMapGenerator::getMapDescription() const
|
|
|
|
{
|
|
|
|
const std::string waterContentStr[3] = { "none", "normal", "islands" };
|
|
|
|
const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
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") +
|
2014-05-23 12:56:51 +03:00
|
|
|
", 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()]);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-05-23 12:56:51 +03:00
|
|
|
for(const auto & pair : mapGenOptions->getPlayersSettings())
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
const auto & pSettings = pair.second;
|
|
|
|
if(pSettings.getPlayerType() == EPlayerType::HUMAN)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()] << " is human";
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-04-14 22:24:31 +03:00
|
|
|
if(pSettings.getStartingTown() != CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
|
2014-05-22 22:40:34 +03:00
|
|
|
<< " town choice is " << VLC->townh->factions[pSettings.getStartingTown()]->name;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::addPlayerInfo()
|
|
|
|
{
|
|
|
|
// Calculate which team numbers exist
|
|
|
|
std::array<std::list<int>, 2> teamNumbers; // 0= cpu/human, 1= cpu only
|
|
|
|
int teamOffset = 0;
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
2014-05-23 12:56:51 +03:00
|
|
|
int playerCount = i == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getCompOnlyPlayerCount();
|
|
|
|
int teamCount = i == 0 ? mapGenOptions->getTeamCount() : mapGenOptions->getCompOnlyTeamCount();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
if(playerCount == 0)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
int playersPerTeam = playerCount /
|
|
|
|
(teamCount == 0 ? playerCount : teamCount);
|
|
|
|
int teamCountNorm = teamCount;
|
|
|
|
if(teamCountNorm == 0)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
teamCountNorm = playerCount;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
for(int j = 0; j < teamCountNorm; ++j)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
for(int k = 0; k < playersPerTeam; ++k)
|
|
|
|
{
|
|
|
|
teamNumbers[i].push_back(j + teamOffset);
|
|
|
|
}
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
for(int j = 0; j < playerCount - teamCountNorm * playersPerTeam; ++j)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
teamNumbers[i].push_back(j + teamOffset);
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
teamOffset += teamCountNorm;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Team numbers are assigned randomly to every player
|
2014-05-23 12:56:51 +03:00
|
|
|
for(const auto & pair : mapGenOptions->getPlayersSettings())
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
const auto & pSettings = pair.second;
|
2013-01-06 22:30:12 +03:00
|
|
|
PlayerInfo player;
|
|
|
|
player.canComputerPlay = true;
|
2013-04-14 22:24:31 +03:00
|
|
|
int j = pSettings.getPlayerType() == EPlayerType::COMP_ONLY ? 1 : 0;
|
2013-01-06 22:30:12 +03:00
|
|
|
if(j == 0)
|
|
|
|
{
|
|
|
|
player.canHumanPlay = true;
|
|
|
|
}
|
2014-05-29 18:34:46 +03:00
|
|
|
|
|
|
|
auto itTeam = RandomGeneratorUtil::nextItem(teamNumbers[j], rand);
|
2013-03-03 20:06:03 +03:00
|
|
|
player.team = TeamID(*itTeam);
|
2013-01-06 22:30:12 +03:00
|
|
|
teamNumbers[j].erase(itTeam);
|
2013-03-03 20:06:03 +03:00
|
|
|
map->players[pSettings.getColor().getNum()] = player;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2014-05-23 12:56:51 +03:00
|
|
|
map->howManyTeams = (mapGenOptions->getTeamCount() == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getTeamCount())
|
|
|
|
+ (mapGenOptions->getCompOnlyTeamCount() == 0 ? mapGenOptions->getCompOnlyPlayerCount() : mapGenOptions->getCompOnlyTeamCount());
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2014-05-22 17:27:13 +03:00
|
|
|
void CMapGenerator::genZones()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-22 17:49:28 +03:00
|
|
|
map->initTerrain();
|
2014-05-22 20:25:17 +03:00
|
|
|
editManager->clearTerrain(&rand);
|
2014-05-23 12:56:51 +03:00
|
|
|
editManager->getTerrainSelection().selectRange(MapRect(int3(0, 0, 0), mapGenOptions->getWidth(), mapGenOptions->getHeight()));
|
2014-05-22 20:25:17 +03:00
|
|
|
editManager->drawTerrain(ETerrainType::GRASS, &rand);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-05-23 12:56:51 +03:00
|
|
|
auto pcnt = mapGenOptions->getPlayerCount();
|
|
|
|
auto w = mapGenOptions->getWidth();
|
|
|
|
auto h = mapGenOptions->getHeight();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-14 21:52:05 +03:00
|
|
|
|
2014-05-23 12:56:51 +03:00
|
|
|
auto tmpl = mapGenOptions->getMapTemplate();
|
2014-05-24 13:42:06 +03:00
|
|
|
zones = tmpl->getZones(); //copy from template (refactor?)
|
2013-01-20 17:43:58 +03:00
|
|
|
|
2014-05-22 17:27:13 +03:00
|
|
|
int player_per_side = zones.size() > 4 ? 3 : 2;
|
|
|
|
int zones_cnt = zones.size() > 4 ? 9 : 4;
|
|
|
|
|
|
|
|
logGlobal->infoStream() << boost::format("Map size %d %d, players per side %d") % w % h % player_per_side;
|
|
|
|
|
2014-05-24 13:42:06 +03:00
|
|
|
CZonePlacer placer(this);
|
|
|
|
placer.placeZones(mapGenOptions, &rand);
|
2014-05-24 15:06:08 +03:00
|
|
|
placer.assignZones(mapGenOptions);
|
2014-05-24 13:42:06 +03:00
|
|
|
|
2014-05-22 17:27:13 +03:00
|
|
|
int i = 0;
|
|
|
|
int part_w = w/player_per_side;
|
|
|
|
int part_h = h/player_per_side;
|
2014-05-24 15:06:08 +03:00
|
|
|
|
|
|
|
|
2014-05-22 17:27:13 +03:00
|
|
|
for(auto const it : zones)
|
|
|
|
{
|
2014-05-22 20:25:17 +03:00
|
|
|
CRmgTemplateZone * zone = it.second;
|
2014-05-22 17:27:13 +03:00
|
|
|
std::vector<int3> shape;
|
|
|
|
int left = part_w*(i%player_per_side);
|
|
|
|
int top = part_h*(i/player_per_side);
|
|
|
|
shape.push_back(int3(left, top, 0));
|
|
|
|
shape.push_back(int3(left + part_w, top, 0));
|
|
|
|
shape.push_back(int3(left + part_w, top + part_h, 0));
|
|
|
|
shape.push_back(int3(left, top + part_h, 0));
|
2014-05-22 20:25:17 +03:00
|
|
|
zone->setShape(shape);
|
|
|
|
zone->setType(i < pcnt ? ETemplateZoneType::PLAYER_START : ETemplateZoneType::TREASURE);
|
2014-05-22 17:27:13 +03:00
|
|
|
this->zones[it.first] = zone;
|
|
|
|
++i;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2014-05-22 17:27:13 +03:00
|
|
|
logGlobal->infoStream() << "Zones generated successfully";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::fillZones()
|
|
|
|
{
|
|
|
|
logGlobal->infoStream() << "Started filling zones";
|
2014-05-22 20:25:17 +03:00
|
|
|
for(auto it : zones)
|
2014-05-22 17:27:13 +03:00
|
|
|
{
|
2014-05-22 20:25:17 +03:00
|
|
|
it.second->fill(this);
|
2014-05-22 17:27:13 +03:00
|
|
|
}
|
|
|
|
logGlobal->infoStream() << "Zones filled successfully";
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::addHeaderInfo()
|
|
|
|
{
|
|
|
|
map->version = EMapFormat::SOD;
|
2014-05-23 12:56:51 +03:00
|
|
|
map->width = mapGenOptions->getWidth();
|
|
|
|
map->height = mapGenOptions->getHeight();
|
|
|
|
map->twoLevel = mapGenOptions->getHasTwoLevels();
|
2013-01-06 22:30:12 +03:00
|
|
|
map->name = VLC->generaltexth->allTexts[740];
|
|
|
|
map->description = getMapDescription();
|
|
|
|
map->difficulty = 1;
|
|
|
|
addPlayerInfo();
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<TRmgTemplateZoneId, CRmgTemplateZone*> CMapGenerator::getZones() const
|
|
|
|
{
|
|
|
|
return zones;
|
2014-05-29 18:34:46 +03:00
|
|
|
}
|