summaryrefslogtreecommitdiffstats
path: root/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp
blob: 9f8c0f39d7080ccfb463344157e6296b3747f894 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226


#include "Globals.h"

#include "IncrementalRedstoneSimulator.h"
#include "Chunk.h"

#include "CommandBlockHandler.h"
#include "DoorHandler.h"
#include "RedstoneHandler.h"
#include "RedstoneTorchHandler.h"
#include "RedstoneWireHandler.h"
#include "RedstoneRepeaterHandler.h"
#include "RedstoneToggleHandler.h"
#include "SolidBlockHandler.h"
#include "RedstoneLampHandler.h"
#include "RedstoneBlockHandler.h"
#include "PistonHandler.h"
#include "SmallGateHandler.h"
#include "NoteBlockHandler.h"
#include "TNTHandler.h"
#include "PoweredRailHandler.h"
#include "PressurePlateHandler.h"
#include "TripwireHookHandler.h"
#include "DropSpenserHandler.h"
#include "RedstoneComparatorHandler.h"
#include "TrappedChestHandler.h"





cRedstoneHandler * cIncrementalRedstoneSimulator::CreateComponent(cWorld & a_World, BLOCKTYPE a_BlockType, cIncrementalRedstoneSimulatorChunkData * a_Data)
{
	switch (a_BlockType)
	{
		case E_BLOCK_ACTIVATOR_RAIL:
		case E_BLOCK_DETECTOR_RAIL:
		case E_BLOCK_POWERED_RAIL:
		{
			static cPoweredRailHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_ACTIVE_COMPARATOR:
		case E_BLOCK_INACTIVE_COMPARATOR:
		{
			static cRedstoneComparatorHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_DISPENSER:
		case E_BLOCK_DROPPER:
		{
			static cDropSpenserHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
		case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE:
		case E_BLOCK_STONE_PRESSURE_PLATE:
		case E_BLOCK_WOODEN_PRESSURE_PLATE:
		{
			static cPressurePlateHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_FENCE_GATE:
		case E_BLOCK_IRON_TRAPDOOR:
		case E_BLOCK_TRAPDOOR:
		{
			static cSmallGateHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_REDSTONE_LAMP_OFF:
		case E_BLOCK_REDSTONE_LAMP_ON:
		{
			static cRedstoneLampHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_REDSTONE_REPEATER_OFF:
		case E_BLOCK_REDSTONE_REPEATER_ON:
		{
			static cRedstoneRepeaterHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_REDSTONE_TORCH_OFF:
		case E_BLOCK_REDSTONE_TORCH_ON:
		{
			static cRedstoneTorchHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_PISTON:
		case E_BLOCK_STICKY_PISTON:
		{
			static cPistonHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_LEVER:
		case E_BLOCK_STONE_BUTTON:
		case E_BLOCK_WOODEN_BUTTON:
		{
			static cRedstoneToggleHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_BLOCK_OF_REDSTONE:
		{
			static cRedstoneBlockHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_COMMAND_BLOCK:
		{
			static cCommandBlockHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_NOTE_BLOCK:
		{
			static cNoteBlockHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_REDSTONE_WIRE:
		{
			static cRedstoneWireHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_TNT:
		{
			static cTNTHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_TRAPPED_CHEST:
		{
			static cTrappedChestHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		case E_BLOCK_TRIPWIRE_HOOK:
		{
			static cTripwireHookHandler ComponentHandler(a_World);
			return &ComponentHandler;
		}
		default:
		{
			if (cBlockDoorHandler::IsDoorBlockType(a_BlockType))
			{
				static cDoorHandler ComponentHandler(a_World);
				return &ComponentHandler;
			}

			if (cBlockInfo::FullyOccupiesVoxel(a_BlockType))
			{
				static cSolidBlockHandler ComponentHandler(a_World);
				return &ComponentHandler;
			}
			return nullptr;
		}
	}
}





void cIncrementalRedstoneSimulator::Simulate(float a_dt)
{
	for (auto & DelayInfo : m_Data.m_MechanismDelays)
	{
		if ((--DelayInfo.second.first) == 0)
		{
			m_Data.GetActiveBlocks().emplace_back(DelayInfo.first);
		}
	}

	// Build our work queue
	cVector3iArray WorkQueue;
	std::swap(WorkQueue, m_Data.GetActiveBlocks());

	// Process the work queue
	while (!WorkQueue.empty())
	{
		// Grab the first element and remove it from the list
		Vector3i CurrentLocation = WorkQueue.back();
		WorkQueue.pop_back();

		BLOCKTYPE CurrentBlock;
		NIBBLETYPE CurrentMeta;
		if (!m_World.GetBlockTypeMeta(CurrentLocation.x, CurrentLocation.y, CurrentLocation.z, CurrentBlock, CurrentMeta))
		{
			continue;
		}

		auto CurrentHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, CurrentBlock, &m_Data);
		if (CurrentHandler == nullptr)  // Block at CurrentPosition doesn't have a corresponding redstone handler
		{
			// Clean up cached PowerData for CurrentPosition
			static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData()->ErasePowerData(CurrentLocation);

			continue;
		}

		cRedstoneHandler::PoweringData Power;
		for (const auto & Location : CurrentHandler->GetValidSourcePositions(CurrentLocation, CurrentBlock, CurrentMeta))
		{
			BLOCKTYPE PotentialBlock;
			NIBBLETYPE PotentialMeta;
			if ((Location.y < 0) || (Location.y > cChunkDef::Height))
			{
				continue;
			}
			m_World.GetBlockTypeMeta(Location.x, Location.y, Location.z, PotentialBlock, PotentialMeta);

			auto PotentialSourceHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, PotentialBlock, &m_Data);
			if (PotentialSourceHandler == nullptr)
			{
				continue;
			}

			decltype(Power) PotentialPower(PotentialBlock, PotentialSourceHandler->GetPowerDeliveredToPosition(Location, PotentialBlock, PotentialMeta, CurrentLocation, CurrentBlock));
			Power = std::max(Power, PotentialPower);
		}

		// Inform the handler to update
		cVector3iArray Updates = CurrentHandler->Update(CurrentLocation, CurrentBlock, CurrentMeta, Power);
		WorkQueue.insert(WorkQueue.end(), Updates.begin(), Updates.end());

		if (IsAlwaysTicked(CurrentBlock))
		{
			m_Data.GetActiveBlocks().emplace_back(CurrentLocation);
		}
	}
}