summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaChunkStay.cpp
blob: 01136fb3e2aad65272bfea2e7aafe46d599147f8 (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

// LuaChunkStay.cpp

// Implements the cLuaChunkStay class representing a cChunkStay binding for plugins, used by cWorld:ChunkStay() Lua API

#include "Globals.h"
#include "LuaChunkStay.h"
#include "PluginLua.h"





cLuaChunkStay::cLuaChunkStay() {}





bool cLuaChunkStay::AddChunks(const cLuaState::cStackTable & a_ChunkCoordsTable)
{
	// This function is expected to be called just once, with all the coords in a table
	ASSERT(m_Chunks.empty());

	// Add each set of coords:
	a_ChunkCoordsTable.ForEachArrayElement(
		[=](cLuaState & a_LuaState, int a_Index)
		{
			if (!lua_istable(a_LuaState, -1))
			{
				LOGWARNING(
					"%s: Element #%d is not a table (got %s). Ignoring the element.",
					__FUNCTION__,
					a_Index,
					lua_typename(a_LuaState, -1)
				);
				a_LuaState.LogStackTrace();
				lua_pop(a_LuaState, 1);
				return false;
			}
			AddChunkCoord(a_LuaState, a_Index);
			return false;
		}
	);

	// If there are no chunks, log a warning and return failure:
	if (m_Chunks.empty())
	{
		LOGWARNING("%s: No valid chunk coords.", __FUNCTION__);
		a_ChunkCoordsTable.GetLuaState().LogStackTrace();
		return false;
	}

	// All ok
	return true;
}





void cLuaChunkStay::AddChunkCoord(cLuaState & L, int a_Index)
{
	// Check that the element has 2 coords:
	int NumCoords = luaL_getn(L, -1);
	if (NumCoords != 2)
	{
		LOGWARNING(
			"%s: Element #%d doesn't contain 2 coords (got %d). Ignoring the element.",
			__FUNCTION__,
			a_Index,
			NumCoords
		);
		return;
	}

	// Read the two coords from the element:
	lua_rawgeti(L, -1, 1);
	lua_rawgeti(L, -2, 2);
	int ChunkX = luaL_checkint(L, -2);
	int ChunkZ = luaL_checkint(L, -1);
	lua_pop(L, 2);

	// Check that a coord is not yet present:
	for (cChunkCoordsVector::iterator itr = m_Chunks.begin(), end = m_Chunks.end(); itr != end; ++itr)
	{
		if ((itr->m_ChunkX == ChunkX) && (itr->m_ChunkZ == ChunkZ))
		{
			LOGWARNING("%s: Element #%d is a duplicate, ignoring it.", __FUNCTION__, a_Index);
			return;
		}
	}  // for itr - m_Chunks[]

	m_Chunks.emplace_back(ChunkX, ChunkZ);
}





void cLuaChunkStay::Enable(
	cChunkMap & a_ChunkMap,
	cLuaState::cCallbackPtr a_OnChunkAvailable,
	cLuaState::cCallbackPtr a_OnAllChunksAvailable
)
{
	m_OnChunkAvailable = std::move(a_OnChunkAvailable);
	m_OnAllChunksAvailable = std::move(a_OnAllChunksAvailable);

	// Enable the ChunkStay:
	Super::Enable(a_ChunkMap);
}





void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ)
{
	if (m_OnChunkAvailable != nullptr)
	{
		m_OnChunkAvailable->Call(a_ChunkX, a_ChunkZ);
	}
}





bool cLuaChunkStay::OnAllChunksAvailable(void)
{
	if (m_OnAllChunksAvailable != nullptr)
	{
		// Call the callback:
		m_OnAllChunksAvailable->Call();

		// Remove the callback references - they won't be needed anymore
		m_OnChunkAvailable.reset();
		m_OnAllChunksAvailable.reset();
	}

	// Disable the ChunkStay by returning true
	return true;
}





void cLuaChunkStay::OnDisabled(void)
{
	// This object is no longer needed, delete it
	delete this;
}