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

// LuaChunkStay.cpp

// Implements the cLuaChunkStay class representing a cChunkStay binding for plugins

#include "Globals.h"
#include "LuaChunkStay.h"
#include "../World.h"





cLuaChunkStay::cLuaChunkStay(void) :
	m_LuaState((lua_State *)NULL)  // Want a detached Lua state by default
{
}





void cLuaChunkStay::Enable(
	cWorld & a_World, lua_State * a_LuaState,
	int a_OnChunkAvailableStackPos, int a_OnAllChunksAvailableStackPos
)
{
	if (m_LuaState.IsValid())
	{
		LOGWARNING("LuaChunkStay: Already enabled. Ignoring this call.");
		cLuaState::LogStackTrace(a_LuaState);
		return;
	}
	m_LuaState.Attach(a_LuaState);
	m_OnChunkAvailable.RefStack(m_LuaState, a_OnChunkAvailableStackPos);
	m_OnAllChunksAvailable.RefStack(m_LuaState, a_OnAllChunksAvailableStackPos);
	super::Enable(*a_World.GetChunkMap());
}





void cLuaChunkStay::Disable(void)
{
	super::Disable();

	// If the state was valid (bound to Lua functions), unbind those functions:
	if (!m_LuaState.IsValid())
	{
		return;
	}
	m_OnAllChunksAvailable.UnRef();
	m_OnChunkAvailable.UnRef();
	m_LuaState.Detach();
}





void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ)
{
	m_LuaState.Call(m_OnChunkAvailable, a_ChunkX, a_ChunkZ);
}





void cLuaChunkStay::OnAllChunksAvailable(void)
{
	m_LuaState.Call(m_OnAllChunksAvailable);
}