1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/lib/rmg/modificators/Modificator.cpp

161 lines
3.2 KiB
C++
Raw Normal View History

2023-05-07 07:48:12 +02:00
/*
* Modificator.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
*
*/
2023-05-20 15:41:03 +02:00
#include "StdInc.h"
2023-05-07 07:48:12 +02:00
#include "Modificator.h"
2023-05-20 10:17:37 +02:00
#include "../Functions.h"
#include "../CMapGenerator.h"
#include "../RmgMap.h"
#include "../../CStopWatch.h"
#include "../../mapping/CMap.h"
2023-05-07 07:48:12 +02:00
VCMI_LIB_NAMESPACE_BEGIN
Modificator::Modificator(Zone & zone, RmgMap & map, CMapGenerator & generator) : zone(zone), map(map), generator(generator)
{
mapProxy = map.getMapProxy();
2023-05-07 07:48:12 +02:00
}
void Modificator::setName(const std::string & n)
{
name = n;
}
const std::string & Modificator::getName() const
{
return name;
}
bool Modificator::isReady()
{
Lock lock(mx, boost::try_to_lock);
2023-05-07 07:48:12 +02:00
if (!lock.owns_lock())
{
return false;
}
else
{
//Check prerequisites
for (auto it = preceeders.begin(); it != preceeders.end();)
{
if ((*it)->isFinished()) //OK
{
//This preceeder won't be checked in the future
2023-05-07 07:48:12 +02:00
it = preceeders.erase(it);
}
else
{
return false;
2023-05-07 07:48:12 +02:00
}
}
//If a job is finished, it should be already erased from a queue
2023-05-07 07:48:12 +02:00
return !finished;
}
}
bool Modificator::isFinished()
{
Lock lock(mx, boost::try_to_lock);
2023-05-07 07:48:12 +02:00
if (!lock.owns_lock())
{
return false;
}
else
{
return finished;
}
}
void Modificator::run()
{
Lock lock(mx);
if(!finished)
{
2024-02-25 12:40:01 +02:00
logGlobal->trace("Modificator zone %d - %s - started", zone.getId(), getName());
2023-05-07 07:48:12 +02:00
CStopWatch processTime;
try
{
process();
}
catch(rmgException &e)
{
logGlobal->error("Modificator %s, exception: %s", getName(), e.what());
}
#ifdef RMG_DUMP
dump();
#endif
finished = true;
2024-02-25 12:40:01 +02:00
logGlobal->trace("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
2023-05-07 07:48:12 +02:00
}
}
void Modificator::dependency(Modificator * modificator)
{
if(modificator && modificator != this)
{
//TODO: use vstd::contains
2023-05-07 07:48:12 +02:00
if(std::find(preceeders.begin(), preceeders.end(), modificator) == preceeders.end())
preceeders.push_back(modificator);
}
}
void Modificator::postfunction(Modificator * modificator)
{
if(modificator && modificator != this)
{
if(std::find(modificator->preceeders.begin(), modificator->preceeders.end(), this) == modificator->preceeders.end())
modificator->preceeders.push_back(this);
}
}
void Modificator::dump()
{
// TODO: Refactor to lock zone area only once
std::ofstream out(boost::str(boost::format("seed_%d_modzone_%d_%s.txt") % generator.getRandomSeed() % zone.getId() % getName()));
int levels = map.levels();
int width = map.width();
int height = map.height();
2023-05-07 07:48:12 +02:00
for(int z = 0; z < levels; z++)
{
for(int j=0; j<height; j++)
{
for(int i=0; i<width; i++)
{
out << dump(int3(i, j, z));
}
out << std::endl;
}
out << std::endl;
}
out << std::endl;
}
char Modificator::dump(const int3 & t)
{
if(zone.freePaths()->contains(t))
2023-05-07 07:48:12 +02:00
return '.'; //free path
if(zone.areaPossible()->contains(t))
2023-05-07 07:48:12 +02:00
return ' '; //possible
if(zone.areaUsed()->contains(t))
2023-05-07 07:48:12 +02:00
return 'U'; //used
if(zone.area()->contains(t))
2023-05-07 07:48:12 +02:00
{
if(map.shouldBeBlocked(t))
return '#'; //obstacle
else
return '^'; //visitable points?
}
return '?';
}
VCMI_LIB_NAMESPACE_END