File: Modificator.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (161 lines) | stat: -rw-r--r-- 3,256 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * 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
 *
 */

#include "StdInc.h"
#include "Modificator.h"
#include "../Functions.h"
#include "../CMapGenerator.h"
#include "../RmgMap.h"
#include "../../CStopWatch.h"
#include "../../mapping/CMap.h"

VCMI_LIB_NAMESPACE_BEGIN

Modificator::Modificator(Zone & zone, RmgMap & map, CMapGenerator & generator) : zone(zone), map(map), generator(generator)
{
	mapProxy = map.getMapProxy();
}

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);
	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
				it = preceeders.erase(it);
			}
			else
			{
				return false;
			}
		}

		//If a job is finished, it should be already erased from a queue
		return !finished;
	}
}

bool Modificator::isFinished()
{
	Lock lock(mx, boost::try_to_lock);
	if (!lock.owns_lock())
	{
		return false;
	}
	else
	{
		return finished;
	}
}

void Modificator::run()
{
	Lock lock(mx);

	if(!finished)
	{
		logGlobal->trace("Modificator zone %d - %s - started", zone.getId(), getName());
		CStopWatch processTime;
		try
		{
			process();
		}
		catch(rmgException &e)
		{
			logGlobal->error("Modificator %s, exception: %s", getName(), e.what());
		}
#ifdef RMG_DUMP
		dump();
#endif
		finished = true;
		logGlobal->trace("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
	}
}

void Modificator::dependency(Modificator * modificator)
{
	if(modificator && modificator != this)
	{
		//TODO: use vstd::contains
		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();
	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))
		return '.'; //free path
	if(zone.areaPossible()->contains(t))
		return ' '; //possible
	if(zone.areaUsed()->contains(t))
		return 'U'; //used
	if(zone.area()->contains(t))
	{
		if(map.shouldBeBlocked(t))
			return '#'; //obstacle
		else
			return '^'; //visitable points?
	}
	return '?';
}

VCMI_LIB_NAMESPACE_END