summaryrefslogtreecommitdiffstats
path: root/src/Simulator/IncrementalRedstoneSimulator/RedstoneSimulatorChunkData.h
blob: 83374e311480b8e9c705ed039ff789943c45d2a0 (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

#pragma once

#include "RedstoneHandler.h"
#include "../RedstoneSimulator.h"
#include <unordered_map>





class cIncrementalRedstoneSimulatorChunkData : public cRedstoneSimulatorChunkData
{

public:
	void WakeUp(const Vector3i & a_Position)
	{
		m_ActiveBlocks.push_back(a_Position);
	}

	cVector3iArray & GetActiveBlocks()
	{
		return m_ActiveBlocks;
	}

	const cRedstoneHandler::PoweringData GetCachedPowerData(const Vector3i & a_Position) const
	{
		auto Result = m_CachedPowerLevels.find(a_Position);
		return (Result == m_CachedPowerLevels.end()) ? cRedstoneHandler::PoweringData() : Result->second;
	}

	void SetCachedPowerData(const Vector3i & a_Position, cRedstoneHandler::PoweringData a_PoweringData)
	{
		m_CachedPowerLevels[a_Position] = a_PoweringData;
	}

	std::pair<int, bool> * GetMechanismDelayInfo(const Vector3i & a_Position)
	{
		auto Result = m_MechanismDelays.find(a_Position);
		return (Result == m_MechanismDelays.end()) ? nullptr : &Result->second;
	}

	/** Erase cached PowerData for position */
	void ErasePowerData(const Vector3i & a_Position)
	{
		m_CachedPowerLevels.erase(a_Position);
	}

	cRedstoneHandler::PoweringData ExchangeUpdateOncePowerData(const Vector3i & a_Position, cRedstoneHandler::PoweringData a_PoweringData)
	{
		auto Result = m_CachedPowerLevels.find(a_Position);
		if (Result == m_CachedPowerLevels.end())
		{
			m_CachedPowerLevels[a_Position] = a_PoweringData;
			return cRedstoneHandler::PoweringData();
		}
		std::swap(Result->second, a_PoweringData);
		return a_PoweringData;
	}

	/** Structure storing position of mechanism + it's delay ticks (countdown) & if to power on */
	std::unordered_map<Vector3i, std::pair<int, bool>, VectorHasher<int>> m_MechanismDelays;
	std::unordered_map<Vector3i, bool, VectorHasher<int>> m_UpdateOncePositions;

private:

	cVector3iArray m_ActiveBlocks;

	// TODO: map<Vector3i, int> -> Position of torch + it's heat level

	std::unordered_map<Vector3i, cRedstoneHandler::PoweringData, VectorHasher<int>> m_CachedPowerLevels;

	friend class cRedstoneHandlerFactory;

};