2014-02-09 12:56:16 -05:00
|
|
|
|
|
|
|
// LuaChunkStay.cpp
|
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// Implements the cLuaChunkStay class representing a cChunkStay binding for plugins, used by cWorld:ChunkStay() Lua API
|
2014-02-09 12:56:16 -05:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "LuaChunkStay.h"
|
2014-02-10 16:47:10 -05:00
|
|
|
#include "PluginLua.h"
|
2014-02-09 12:56:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-14 10:26:31 -04:00
|
|
|
cLuaChunkStay::cLuaChunkStay()
|
2014-02-09 14:39:45 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-14 10:26:31 -04:00
|
|
|
bool cLuaChunkStay::AddChunks(const cLuaState::cStackTable & a_ChunkCoordsTable)
|
2014-02-09 12:56:16 -05:00
|
|
|
{
|
2014-02-10 16:47:10 -05:00
|
|
|
// This function is expected to be called just once, with all the coords in a table
|
|
|
|
ASSERT(m_Chunks.empty());
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// Add each set of coords:
|
2016-08-14 10:26:31 -04:00
|
|
|
a_ChunkCoordsTable.ForEachArrayElement([=](cLuaState & a_LuaState, int a_Index)
|
2014-02-10 16:47:10 -05:00
|
|
|
{
|
2016-08-14 10:26:31 -04:00
|
|
|
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;
|
2014-02-10 16:47:10 -05:00
|
|
|
}
|
2016-08-14 10:26:31 -04:00
|
|
|
);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// If there are no chunks, log a warning and return failure:
|
|
|
|
if (m_Chunks.empty())
|
|
|
|
{
|
2016-08-14 10:26:31 -04:00
|
|
|
LOGWARNING("%s: No valid chunk coords.", __FUNCTION__);
|
|
|
|
a_ChunkCoordsTable.GetLuaState().LogStackTrace();
|
2014-02-10 16:47:10 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// All ok
|
|
|
|
return true;
|
2014-02-09 12:56:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
void cLuaChunkStay::AddChunkCoord(cLuaState & L, int a_Index)
|
2014-02-09 14:39:45 -05:00
|
|
|
{
|
2014-02-10 16:47:10 -05:00
|
|
|
// Check that the element has 2 coords:
|
|
|
|
int NumCoords = luaL_getn(L, -1);
|
|
|
|
if (NumCoords != 2)
|
2014-02-09 14:39:45 -05:00
|
|
|
{
|
2014-02-10 16:47:10 -05:00
|
|
|
LOGWARNING("%s: Element #%d doesn't contain 2 coords (got %d). Ignoring the element.",
|
|
|
|
__FUNCTION__, a_Index, NumCoords
|
|
|
|
);
|
2014-02-09 14:39:45 -05:00
|
|
|
return;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// 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);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// 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[]
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2021-03-07 11:31:43 -05:00
|
|
|
m_Chunks.emplace_back(ChunkX, ChunkZ);
|
2014-02-10 16:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-12 10:53:24 -04:00
|
|
|
void cLuaChunkStay::Enable(cChunkMap & a_ChunkMap, cLuaState::cCallbackPtr a_OnChunkAvailable, cLuaState::cCallbackPtr a_OnAllChunksAvailable)
|
2014-02-10 16:47:10 -05:00
|
|
|
{
|
2016-06-27 14:49:59 -04:00
|
|
|
m_OnChunkAvailable = std::move(a_OnChunkAvailable);
|
|
|
|
m_OnAllChunksAvailable = std::move(a_OnAllChunksAvailable);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// Enable the ChunkStay:
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::Enable(a_ChunkMap);
|
2014-02-09 14:39:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-09 12:56:16 -05:00
|
|
|
void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ)
|
|
|
|
{
|
2016-06-12 10:53:24 -04:00
|
|
|
if (m_OnChunkAvailable != nullptr)
|
2014-10-17 07:17:56 -04:00
|
|
|
{
|
2016-06-12 10:53:24 -04:00
|
|
|
m_OnChunkAvailable->Call(a_ChunkX, a_ChunkZ);
|
2014-10-17 07:17:56 -04:00
|
|
|
}
|
2014-02-09 12:56:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
bool cLuaChunkStay::OnAllChunksAvailable(void)
|
2014-02-09 12:56:16 -05:00
|
|
|
{
|
2016-06-12 10:53:24 -04:00
|
|
|
if (m_OnAllChunksAvailable != nullptr)
|
2014-02-10 16:47:10 -05:00
|
|
|
{
|
|
|
|
// Call the callback:
|
2016-06-12 10:53:24 -04:00
|
|
|
m_OnAllChunksAvailable->Call();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// Remove the callback references - they won't be needed anymore
|
2016-06-12 10:53:24 -04:00
|
|
|
m_OnChunkAvailable.reset();
|
|
|
|
m_OnAllChunksAvailable.reset();
|
2014-02-10 16:47:10 -05:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
// Disable the ChunkStay by returning true
|
|
|
|
return true;
|
2014-02-09 12:56:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-10 16:47:10 -05:00
|
|
|
void cLuaChunkStay::OnDisabled(void)
|
|
|
|
{
|
|
|
|
// This object is no longer needed, delete it
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|