1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-15 13:33:36 +02:00

Place Mines after Towns and Monoliths.

This commit is contained in:
Tomasz Zieliński 2023-03-29 16:54:22 +02:00
parent 5ed3c2d518
commit 401f2342c0
4 changed files with 131 additions and 54 deletions

View File

@ -107,6 +107,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/rmg/RmgMap.cpp
${MAIN_LIB_DIR}/rmg/ConnectionsPlacer.cpp
${MAIN_LIB_DIR}/rmg/WaterAdopter.cpp
${MAIN_LIB_DIR}/rmg/MinePlacer.cpp
${MAIN_LIB_DIR}/rmg/TownPlacer.cpp
${MAIN_LIB_DIR}/rmg/WaterProxy.cpp
${MAIN_LIB_DIR}/rmg/WaterRoutes.cpp
@ -345,6 +346,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/rmg/RmgMap.h
${MAIN_LIB_DIR}/rmg/ConnectionsPlacer.h
${MAIN_LIB_DIR}/rmg/WaterAdopter.h
${MAIN_LIB_DIR}/rmg/MinePlacer.h
${MAIN_LIB_DIR}/rmg/TownPlacer.h
${MAIN_LIB_DIR}/rmg/WaterProxy.h
${MAIN_LIB_DIR}/rmg/WaterRoutes.h

97
lib/rmg/MinePlacer.cpp Normal file
View File

@ -0,0 +1,97 @@
/*
* 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 "MinePlacer.h"
#include "TownPlacer.h"
#include "CMapGenerator.h"
#include "RmgMap.h"
#include "../mapping/CMap.h"
#include "../mapping/CMapEditManager.h"
#include "../mapObjects/CObjectClassesHandler.h"
#include "../spells/CSpellHandler.h" //for choosing random spells
#include "RmgPath.h"
#include "RmgObject.h"
#include "ObjectManager.h"
#include "Functions.h"
#include "RoadPlacer.h"
#include "WaterAdopter.h"
#include "TileInfo.h"
VCMI_LIB_NAMESPACE_BEGIN
void MinePlacer::process()
{
auto * manager = zone.getModificator<ObjectManager>();
if(!manager)
{
logGlobal->error("ObjectManager doesn't exist for zone %d, skip modificator %s", zone.getId(), getName());
return;
}
placeMines(*manager);
}
void MinePlacer::init()
{
DEPENDENCY(TownPlacer);
POSTFUNCTION(ObjectManager);
POSTFUNCTION(RoadPlacer);
}
bool MinePlacer::placeMines(ObjectManager & manager)
{
using namespace Res;
std::vector<CGMine*> createdMines;
std::vector<std::pair<CGObjectInstance*, ui32>> requiredObjects;
for(const auto & mineInfo : zone.getMinesInfo())
{
ERes res = static_cast<ERes>(mineInfo.first);
for(int i = 0; i < mineInfo.second; ++i)
{
auto mineHandler = VLC->objtypeh->getHandlerFor(Obj::MINE, res);
const auto & rmginfo = mineHandler->getRMGInfo();
auto * mine = dynamic_cast<CGMine *>(mineHandler->create());
mine->producedResource = res;
mine->tempOwner = PlayerColor::NEUTRAL;
mine->producedQuantity = mine->defaultResProduction();
createdMines.push_back(mine);
if(!i && (res == ERes::WOOD || res == ERes::ORE))
manager.addCloseObject(mine, rmginfo.value); //only first wood&ore mines are close
else
requiredObjects.push_back(std::pair<CGObjectInstance*, ui32>(mine, rmginfo.value));
}
}
//Shuffle mines to avoid patterns, but don't shuffle key objects like towns
RandomGeneratorUtil::randomShuffle(requiredObjects, generator.rand);
for (const auto& obj : requiredObjects)
{
manager.addRequiredObject(obj.first, obj.second);
}
//create extra resources
if(int extraRes = generator.getConfig().mineExtraResources)
{
for(auto * mine : createdMines)
{
for(int rc = generator.rand.nextInt(1, extraRes); rc > 0; --rc)
{
auto * resourse = dynamic_cast<CGResource *>(VLC->objtypeh->getHandlerFor(Obj::RESOURCE, mine->producedResource)->create());
resourse->amount = CGResource::RANDOM_AMOUNT;
manager.addNearbyObject(resourse, mine);
}
}
}
return true;
}
VCMI_LIB_NAMESPACE_END

30
lib/rmg/MinePlacer.h Normal file
View File

@ -0,0 +1,30 @@
/*
* MinePlacer.h, 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
*
*/
#pragma once
#include "Zone.h"
VCMI_LIB_NAMESPACE_BEGIN
class ObjectManager;
class MinePlacer: public Modificator
{
public:
MODIFICATOR(MinePlacer);
void process() override;
void init() override;
protected:
bool placeMines(ObjectManager & manager);
};
VCMI_LIB_NAMESPACE_END

View File

@ -21,6 +21,7 @@
#include "ObjectManager.h"
#include "Functions.h"
#include "RoadPlacer.h"
#include "MinePlacer.h"
#include "WaterAdopter.h"
#include "TileInfo.h"
@ -35,13 +36,12 @@ void TownPlacer::process()
return;
}
placeTowns(*manager);
placeMines(*manager);
}
void TownPlacer::init()
{
POSTFUNCTION(MinePlacer);
POSTFUNCTION(ObjectManager);
POSTFUNCTION(RoadPlacer);
}
@ -153,58 +153,6 @@ int3 TownPlacer::placeMainTown(ObjectManager & manager, CGTownInstance & town)
return position;
}
bool TownPlacer::placeMines(ObjectManager & manager)
{
using namespace Res;
std::vector<CGMine*> createdMines;
std::vector<std::pair<CGObjectInstance*, ui32>> requiredObjects;
for(const auto & mineInfo : zone.getMinesInfo())
{
ERes res = static_cast<ERes>(mineInfo.first);
for(int i = 0; i < mineInfo.second; ++i)
{
auto mineHandler = VLC->objtypeh->getHandlerFor(Obj::MINE, res);
const auto & rmginfo = mineHandler->getRMGInfo();
auto * mine = dynamic_cast<CGMine *>(mineHandler->create());
mine->producedResource = res;
mine->tempOwner = PlayerColor::NEUTRAL;
mine->producedQuantity = mine->defaultResProduction();
createdMines.push_back(mine);
if(!i && (res == ERes::WOOD || res == ERes::ORE))
manager.addCloseObject(mine, rmginfo.value); //only first wood&ore mines are close
else
requiredObjects.push_back(std::pair<CGObjectInstance*, ui32>(mine, rmginfo.value));
}
}
//Shuffle mines to avoid patterns, but don't shuffle key objects like towns
RandomGeneratorUtil::randomShuffle(requiredObjects, generator.rand);
for (const auto& obj : requiredObjects)
{
manager.addRequiredObject(obj.first, obj.second);
}
//create extra resources
if(int extraRes = generator.getConfig().mineExtraResources)
{
for(auto * mine : createdMines)
{
for(int rc = generator.rand.nextInt(1, extraRes); rc > 0; --rc)
{
auto * resourse = dynamic_cast<CGResource *>(VLC->objtypeh->getHandlerFor(Obj::RESOURCE, mine->producedResource)->create());
resourse->amount = CGResource::RANDOM_AMOUNT;
manager.addNearbyObject(resourse, mine);
}
}
}
return true;
}
void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
{
for(const auto & t : rmgObject.getArea().getBorderOutside())