summaryrefslogtreecommitdiffstats
path: root/src/Simulator/SimulatorManager.cpp
blob: 06254a1f23bfd45330813fd99dfcd7f96116e5ca (plain) (blame)
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

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "SimulatorManager.h"
#include "../Chunk.h"
#include "../Cuboid.h"
#include "../World.h"





cSimulatorManager::cSimulatorManager(cWorld & a_World) :
	m_World(a_World), m_Ticks(0)
{
}





cSimulatorManager::~cSimulatorManager() {}





void cSimulatorManager::Simulate(float a_Dt)
{
	m_Ticks++;

	for (cSimulators::iterator itr = m_Simulators.begin(); itr != m_Simulators.end(); ++itr)
	{
		if ((m_Ticks % itr->second) == 0)
		{
			itr->first->Simulate(a_Dt);
		}
	}
}





void cSimulatorManager::SimulateChunk(std::chrono::milliseconds a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk)
{
	// m_Ticks has already been increased in Simulate()

	for (cSimulators::iterator itr = m_Simulators.begin(); itr != m_Simulators.end(); ++itr)
	{
		if ((m_Ticks % itr->second) == 0)
		{
			itr->first->SimulateChunk(a_Dt, a_ChunkX, a_ChunkZ, a_Chunk);
		}
	}
}





void cSimulatorManager::WakeUp(cChunk & a_Chunk, Vector3i a_Position)
{
	ASSERT(a_Chunk.IsValid());

	for (const auto & Item : m_Simulators)
	{
		Item.first->WakeUp(a_Chunk, a_Position, a_Chunk.GetBlock(a_Position));
	}

	for (const auto & Offset : cSimulator::AdjacentOffsets)
	{
		auto Relative = a_Position + Offset;

		if (!cChunkDef::IsValidHeight(Relative))
		{
			continue;
		}

		const auto Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(Relative);

		if ((Chunk == nullptr) || !Chunk->IsValid())
		{
			continue;
		}

		// Stored block to give to simulators for performance
		// Since they all need this we save them querying it themselves
		const auto Block = Chunk->GetBlock(Relative);

		for (const auto & Item : m_Simulators)
		{
			Item.first->WakeUp(*Chunk, Relative, Offset, Block);
		}
	}
}





void cSimulatorManager::WakeUp(const cCuboid & a_Area)
{
	cCuboid area(a_Area);
	area.Sort();
	area.Expand(1, 1, 1, 1, 1, 1);  // Expand the area to contain the neighbors, too.
	area.ClampY(0, cChunkDef::Height - 1);

	cChunkCoords ChunkStart = cChunkDef::BlockToChunk(area.p1);
	cChunkCoords ChunkEnd = cChunkDef::BlockToChunk(area.p2);

	// Add all blocks, in a per-chunk manner:
	for (int cz = ChunkStart.m_ChunkZ; cz <= ChunkEnd.m_ChunkZ; ++cz)
	{
		for (int cx = ChunkStart.m_ChunkX; cx <= ChunkEnd.m_ChunkX; ++cx)
		{
			m_World.DoWithChunk(
				cx,
				cz,
				[this, &area](cChunk & a_CBChunk)
				{
					int startX = std::max(area.p1.x, a_CBChunk.GetPosX() * cChunkDef::Width);
					int startZ = std::max(area.p1.z, a_CBChunk.GetPosZ() * cChunkDef::Width);
					int endX = std::min(area.p2.x, a_CBChunk.GetPosX() * cChunkDef::Width + cChunkDef::Width - 1);
					int endZ = std::min(area.p2.z, a_CBChunk.GetPosZ() * cChunkDef::Width + cChunkDef::Width - 1);

					for (const auto & Item : m_Simulators)
					{
						const auto Simulator = Item.first;

						for (int y = area.p1.y; y <= area.p2.y; ++y)
						{
							for (int z = startZ; z <= endZ; ++z)
							{
								for (int x = startX; x <= endX; ++x)
								{
									const auto Position = cChunkDef::AbsoluteToRelative({x, y, z});
									Simulator->WakeUp(a_CBChunk, Position, a_CBChunk.GetBlock(Position));
								}  // for x
							}  // for z
						}  // for y
					}
					return true;
				}
			);
		}  // for cx
	}  // for cz
}





void cSimulatorManager::RegisterSimulator(cSimulator * a_Simulator, int a_Rate)
{
	m_Simulators.push_back(std::make_pair(a_Simulator, a_Rate));
}