6 #include "tolua++/include/tolua++.h"
7 #include "../BlockArea.h"
9 #include "ManualBindings.h"
12 #include "../WorldStorage/SchematicFileSerializer.h"
22 bool (SELF::*DoWithFn)(int, int, int,
cFunctionRef<bool(ITEM &)>),
23 bool (SELF::*CoordCheckFn)(int, int, int)
const
39 SELF * Self =
nullptr;
51 return L.
ApiParamError(
"Expected a valid callback function for parameter #5");
53 if (!(Self->*CoordCheckFn)(BlockX, BlockY, BlockZ))
55 return L.
ApiParamError(fmt::format(FMT_STRING(
"The provided coordinates ({0}) are not valid"),
61 bool res = (Self->*DoWithFn)(BlockX, BlockY, BlockZ, [&](ITEM & a_Item)
93 return a_LuaState.
ApiParamError(
"Cannot read the bounds parameters, expected 6 numbers");
95 return a_StartParam + 6;
103 return a_LuaState.
ApiParamError(
"Cannot read the bounds parameter, expected a cCuboid instance");
106 return a_StartParam + 1;
113 return a_LuaState.
ApiParamError(
"Cannot read the bounds parameter, expected two Vector3i instances");
115 return a_StartParam + 2;
135 if (!a_LuaState.
GetStackValues(a_StartParam, a_Coords.
x, a_Coords.
y, a_Coords.
z))
137 return a_LuaState.
ApiParamError(fmt::format(FMT_STRING(
"Cannot read the {}, expected 3 numbers"), a_ParamName));
139 return a_StartParam + 3;
146 return a_LuaState.
ApiParamError(fmt::format(FMT_STRING(
"Cannot read the {}, expected a Vector3i instance"), a_ParamName));
148 return a_StartParam + 1;
180 return L.
ApiParamError(fmt::format(FMT_STRING(
"Invalid combination of baDataTypes specified (0x{:02x})"), dataTypes));
184 if ((size.
x <= 0) || (size.
y <= 0) || (size.
z <= 0))
186 return L.
ApiParamError(fmt::format(FMT_STRING(
"Invalid sizes, must be greater than zero, got {}"), size));
189 self->Create(size, dataTypes);
222 if (!(self->IsValidRelCoords(bounds.
p1) && self->IsValidRelCoords(bounds.
p2)))
225 FMT_STRING(
"The bounds ({0} - {1}) are out of range ({2} - {3})"),
229 (self->GetSize() -
Vector3i{1, 1, 1})
234 NIBBLETYPE blockMeta = 0, blockLight = 0, blockSkyLight = 0x0f;
237 return L.
ApiParamError(
"Cannot read the datatypes or block type params");
239 L.
GetStackValues(nextIdx + 2, blockMeta, blockLight, blockSkyLight);
242 return L.
ApiParamError(fmt::format(FMT_STRING(
"Invalid baDataTypes combination (0x{:02x})"), dataTypes));
246 self->FillRelCuboid(bounds, dataTypes, blockType, blockMeta, blockLight, blockSkyLight);
276 if (!self->IsValidCoords(coords))
278 return L.
ApiParamError(fmt::format(FMT_STRING(
"Coords ({0}) out of range ({1} - {2})"),
279 coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() -
Vector3i{1, 1, 1}
284 self->GetBlockTypeMeta(coords.
x, coords.
y, coords.
z, blockType, blockMeta);
285 L.
Push(blockType, blockMeta);
316 L.
Push(self->GetSizeX() - 1, self->GetSizeY() - 1, self->GetSizeZ() - 1);
345 if (!self->HasBlockTypes())
347 return L.
ApiParamError(
"The area doesn't contain baTypes datatype");
351 int minRelX, minRelY, minRelZ, maxRelX, maxRelY, maxRelZ;
352 self->GetNonAirCropRelCoords(minRelX, minRelY, minRelZ, maxRelX, maxRelY, maxRelZ, ignoreBlockType);
355 L.
Push(minRelX, minRelY, minRelZ, maxRelX, maxRelY, maxRelZ);
387 L.
Push(self->GetOriginX(), self->GetOriginY(), self->GetOriginZ());
415 if (!self->HasBlockTypes())
417 return L.
ApiParamError(
"The area doesn't contain baTypes datatype");
419 if (!self->HasBlockMetas())
421 return L.
ApiParamError(
"The area doesn't contain baMetas datatype");
426 if (!self->IsValidRelCoords(coords))
428 return L.
ApiParamError(fmt::format(FMT_STRING(
"The coords ({0}) are out of range (max {1})"),
429 coords, (self->GetSize() -
Vector3i{1, 1, 1})
434 self->GetRelBlockTypeMeta(coords.
x, coords.
y, coords.
z, blockType, blockMeta);
435 L.
Push(blockType, blockMeta);
466 L.
Push(self->GetSizeX(), self->GetSizeY(), self->GetSizeZ());
503 catch (
const std::exception & Oops)
545 catch (
const std::exception & Oops)
578 if (world ==
nullptr)
580 return L.
ApiParamError(
"Invalid world instance. The world must be not nil");
590 return L.
ApiParamError(fmt::format(FMT_STRING(
"Invalid baDataTypes combination (0x{:02x})"), dataTypes));
596 return L.
ApiParamError(fmt::format(FMT_STRING(
"Coordinates {0} - {1} exceed world bounds"), bounds.
p1, bounds.
p2));
602 L.
Push(self->Read(*world, bounds, dataTypes));
635 NIBBLETYPE blockMeta, blockLight, blockSkyLight;
636 L.
GetStackValues(idx, dataTypes, blockType, blockMeta, blockLight, blockSkyLight);
639 return L.
ApiParamError(fmt::format(FMT_STRING(
"Invalid baDataTypes combination (0x{:02x})"), dataTypes));
641 if ((self->GetDataTypes() & dataTypes) != dataTypes)
644 FMT_STRING(
"Requested datatypes not present in the cBlockArea. Got only 0x{:02x}, requested 0x{:02x}"),
645 self->GetDataTypes(), dataTypes
650 self->RelLine(p1, p2, dataTypes, blockType, blockMeta, blockLight, blockSkyLight);
687 catch (
const std::exception & Oops)
727 catch (
const std::exception & Oops)
759 if (world ==
nullptr)
761 return L.
ApiParamError(
"Invalid world instance. The world must be not nil");
775 return L.
ApiParamError(fmt::format(FMT_STRING(
"Invalid datatype combination (0x{:02x})"), dataTypes));
777 if ((self->GetDataTypes() & dataTypes) != dataTypes)
780 FMT_STRING(
"Requesting datatypes not present in the cBlockArea. Got only 0x{:02x}, requested 0x{:02x}"),
781 self->GetDataTypes(), dataTypes
793 LOGWARNING(
"cBlockArea:Write(): MinBlockY less than zero, adjusting to zero");
799 LOGWARNING(
"cBlockArea:Write(): MinBlockY + m_SizeY more than chunk height, adjusting to chunk height");
807 L.
Push(self->Write(*world, coords, dataTypes));
811 L.
Push(self->Write(*world, coords));
828 DataType (
cBlockArea::*Fn)(int, int, int)
const
847 if ((self->GetDataTypes() & DataTypeFlag) == 0)
849 return L.
ApiParamError(fmt::format(FMT_STRING(
"The area doesn't contain the datatype (0x{:02x})"), DataTypeFlag));
855 if (!self->IsValidCoords(coords))
857 return L.
ApiParamError(fmt::format(FMT_STRING(
"The coords ({0}) are out of range ({1} - {2})"),
858 coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() -
Vector3i{1, 1, 1}
863 L.
Push((self->*Fn)(coords.
x, coords.
y, coords.
z));
879 DataType (
cBlockArea::*Fn)(int, int, int)
const
898 if ((self->GetDataTypes() & DataTypeFlag) == 0)
900 return L.
ApiParamError(fmt::format(FMT_STRING(
"The area doesn't contain the datatype (0x{:02x})"), DataTypeFlag));
906 if (!self->IsValidRelCoords(coords))
908 return L.
ApiParamError(fmt::format(FMT_STRING(
"The coords ({0}) are out of range ({1})"),
909 coords, (self->GetSize() -
Vector3i(1, 1, 1))
914 L.
Push((self->*Fn)(coords.
x, coords.
y, coords.
z));
930 void (
cBlockArea::*Fn)(int, int, int, DataType)
949 if ((self->GetDataTypes() & DataTypeFlag) == 0)
951 return L.
ApiParamError(fmt::format(FMT_STRING(
"The area doesn't contain the datatype (0x{:02x})"), DataTypeFlag));
957 if (!self->IsValidCoords(coords))
959 return L.
ApiParamError(fmt::format(FMT_STRING(
"The coords ({0}) are out of range ({1} - {2})"),
960 coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() -
Vector3i{1, 1, 1}
967 (
self->*Fn)(coords.
x, coords.
y, coords.
z, data);
983 void (
cBlockArea::*Fn)(int, int, int, DataType)
1002 if ((self->GetDataTypes() & DataTypeFlag) == 0)
1004 return L.
ApiParamError(fmt::format(FMT_STRING(
"The area doesn't contain the datatype (0x{:02x})"), DataTypeFlag));
1010 if (!self->IsValidRelCoords(coords))
1013 FMT_STRING(
"The coords ({0}) are out of range ({1})"),
1014 coords, (self->GetSize() -
Vector3i(1, 1, 1))
1021 (
self->*Fn)(coords.
x, coords.
y, coords.
z, data);
1046 if (!self->HasBlockTypes() || !self->HasBlockMetas())
1048 return L.
ApiParamError(
"The area doesn't contain the datatypes baTypes and baMetas");
1054 if (!self->IsValidCoords(coords))
1056 return L.
ApiParamError(fmt::format(FMT_STRING(
"The coords ({0}) are out of range ({1} - {2})"),
1057 coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() -
Vector3i{1, 1, 1}
1065 return L.
ApiParamError(
"Bad number for block type or meta type");
1069 self->SetBlockTypeMeta(coords.
x, coords.
y, coords.
z, block, meta);
1094 if (!self->HasBlockTypes() || !self->HasBlockMetas())
1096 return L.
ApiParamError(fmt::format(FMT_STRING(
"The area doesn't contain the baTypes or baMetas datatypes (0x{:02x})"), self->GetDataTypes()));
1102 if (!self->IsValidRelCoords(coords))
1104 return L.
ApiParamError(fmt::format(FMT_STRING(
"The coords ({0}) are out of range ({1})"),
1105 coords, (self->GetSize() -
Vector3i(1, 1, 1))
1113 return L.
ApiParamError(
"Bad number for block type or meta type");
1117 self->SetRelBlockTypeMeta(coords.
x, coords.
y, coords.
z, block, meta);
1125 void cManualBindings::BindBlockArea(lua_State * a_LuaState)
1127 tolua_beginmodule(a_LuaState,
nullptr);
1128 tolua_beginmodule(a_LuaState,
"cBlockArea");
1130 tolua_function(a_LuaState,
"DoWithBlockEntityAt", DoWithXYZ<cBlockArea, cBlockEntity, &cBlockArea::DoWithBlockEntityAt, &cBlockArea::IsValidCoords>);
1131 tolua_function(a_LuaState,
"DoWithBlockEntityRelAt", DoWithXYZ<cBlockArea, cBlockEntity, &cBlockArea::DoWithBlockEntityRelAt, &cBlockArea::IsValidRelCoords>);
1133 tolua_function(a_LuaState,
"ForEachBlockEntity", ForEach< cBlockArea, cBlockEntity, &cBlockArea::ForEachBlockEntity>);
1134 tolua_function(a_LuaState,
"GetBlockLight", GetBlock<NIBBLETYPE, cBlockArea::baLight, &cBlockArea::GetRelBlockLight>);
1135 tolua_function(a_LuaState,
"GetBlockMeta", GetBlock<NIBBLETYPE, cBlockArea::baMetas, &cBlockArea::GetRelBlockMeta>);
1136 tolua_function(a_LuaState,
"GetBlockSkyLight", GetBlock<NIBBLETYPE, cBlockArea::baSkyLight, &cBlockArea::GetRelBlockSkyLight>);
1137 tolua_function(a_LuaState,
"GetBlockType", GetBlock<BLOCKTYPE, cBlockArea::baTypes, &cBlockArea::GetRelBlockType>);
1142 tolua_function(a_LuaState,
"GetRelBlockLight", GetRelBlock<NIBBLETYPE, cBlockArea::baLight, &cBlockArea::GetRelBlockLight>);
1143 tolua_function(a_LuaState,
"GetRelBlockMeta", GetRelBlock<NIBBLETYPE, cBlockArea::baMetas, &cBlockArea::GetRelBlockMeta>);
1144 tolua_function(a_LuaState,
"GetRelBlockSkyLight", GetRelBlock<NIBBLETYPE, cBlockArea::baSkyLight, &cBlockArea::GetRelBlockSkyLight>);
1145 tolua_function(a_LuaState,
"GetRelBlockType", GetRelBlock<BLOCKTYPE, cBlockArea::baTypes, &cBlockArea::GetRelBlockType>);
1154 tolua_function(a_LuaState,
"SetBlockType", SetBlock<BLOCKTYPE, cBlockArea::baTypes, &cBlockArea::SetRelBlockType>);
1155 tolua_function(a_LuaState,
"SetBlockMeta", SetBlock<NIBBLETYPE, cBlockArea::baMetas, &cBlockArea::SetRelBlockMeta>);
1156 tolua_function(a_LuaState,
"SetBlockLight", SetBlock<NIBBLETYPE, cBlockArea::baLight, &cBlockArea::SetRelBlockLight>);
1157 tolua_function(a_LuaState,
"SetBlockSkyLight", SetBlock<NIBBLETYPE, cBlockArea::baSkyLight, &cBlockArea::SetRelBlockSkyLight>);
1159 tolua_function(a_LuaState,
"SetRelBlockType", SetRelBlock<BLOCKTYPE, cBlockArea::baTypes, &cBlockArea::SetRelBlockType>);
1160 tolua_function(a_LuaState,
"SetRelBlockMeta", SetRelBlock<NIBBLETYPE, cBlockArea::baMetas, &cBlockArea::SetRelBlockMeta>);
1161 tolua_function(a_LuaState,
"SetRelBlockLight", SetRelBlock<NIBBLETYPE, cBlockArea::baLight, &cBlockArea::SetRelBlockLight>);
1162 tolua_function(a_LuaState,
"SetRelBlockSkyLight", SetRelBlock<NIBBLETYPE, cBlockArea::baSkyLight, &cBlockArea::SetRelBlockSkyLight>);
1165 tolua_endmodule(a_LuaState);
1166 tolua_endmodule(a_LuaState);
static int SetRelBlock(lua_State *a_LuaState)
Templated bindings for the SetRelBlock___() functions.
static int tolua_cBlockArea_SetRelBlockTypeMeta(lua_State *a_LuaState)
static int tolua_cBlockArea_GetNonAirCropRelCoords(lua_State *a_LuaState)
static int tolua_cBlockArea_RelLine(lua_State *a_LuaState)
Bindings for the cBlockArea:RelLine() functions.
static int tolua_cBlockArea_Create(lua_State *a_LuaState)
Binding for the cBlockArea::Create() functions.
static int tolua_cBlockArea_GetOrigin(lua_State *a_LuaState)
static int tolua_cBlockArea_LoadFromSchematicFile(lua_State *a_LuaState)
static int tolua_cBlockArea_SaveToSchematicString(lua_State *a_LuaState)
static int tolua_cBlockArea_FillRelCuboid(lua_State *a_LuaState)
Bindings for the cBlockArea:FillRelCuboid() functions.
static int SetBlock(lua_State *a_LuaState)
Templated bindings for the SetBlock___() functions.
static int GetRelBlock(lua_State *a_LuaState)
Templated bindings for the GetRelBlock___() functions.
static int tolua_cBlockArea_SaveToSchematicFile(lua_State *a_LuaState)
static int tolua_cBlockArea_GetBlockTypeMeta(lua_State *a_LuaState)
static int tolua_cBlockArea_GetRelBlockTypeMeta(lua_State *a_LuaState)
static int tolua_cBlockArea_Read(lua_State *a_LuaState)
Bindings for the cBlockArea:Read() functions.
static int readVector3iOverloadParams(cLuaState &a_LuaState, int a_StartParam, Vector3i &a_Coords, const char *a_ParamName)
Reads params that together form a Vector3i.
static int tolua_cBlockArea_SetBlockTypeMeta(lua_State *a_LuaState)
static int tolua_cBlockArea_LoadFromSchematicString(lua_State *a_LuaState)
static int tolua_cBlockArea_GetCoordRange(lua_State *a_LuaState)
static int readCuboidOverloadParams(cLuaState &a_LuaState, int a_StartParam, cCuboid &a_Cuboid)
Reads params that together form a Cuboid.
static int DoWithXYZ(lua_State *tolua_S)
Template for the bindings for the DoWithXYZAt(X, Y, Z) functions that need to check their coords.
static int tolua_cBlockArea_Write(lua_State *a_LuaState)
Bindings for the cBlockArea:Write() functions.
static int tolua_cBlockArea_GetSize(lua_State *a_LuaState)
static int GetBlock(lua_State *a_LuaState)
Templated bindings for the GetBlock___() functions.
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
unsigned char BLOCKTYPE
The datatype used by blockdata.
std::basic_string< std::byte > ContiguousByteBuffer
void LOGWARNING(std::string_view a_Format, const Args &... args)
Encapsulates a Lua state and provides some syntactic sugar for common operations.
bool CheckParamFunction(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are functions; also logs warning if not.
bool CheckParamUserType(int a_StartParam, const char *a_UserType, int a_EndParam=-1)
Returns true if the specified parameters on the stack are of the specified usertype; also logs warnin...
void Push(Arg1 &&a_Arg1, Arg2 &&a_Arg2, Args &&... a_Args)
Pushes multiple arguments onto the Lua stack.
void LogStackTrace(int a_StartingDepth=0)
Logs all items in the current stack trace to the server console.
bool CheckParamEnd(int a_Param)
Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters)
bool IsParamUserType(int a_ParamIdx, const AString &a_UserType)
Returns true if the specified parameter is of the specified class.
bool Call(const FnT &a_Function, Args &&... args)
Call the specified Lua function.
bool CheckParamSelf(const char *a_SelfClassName)
Returns true if the first parameter is an instance of the expected class name.
bool CheckParamString(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are strings; also logs warning if not.
bool CheckParamNumber(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are numbers; also logs warning if not.
bool GetStackValue(int a_StackPos, AString &a_Value)
int ApiParamError(std::string_view a_Msg)
Prints the message, prefixed with the current function name, then logs the stack contents and raises ...
bool GetStackValues(int a_StartStackPos, Arg1 &&a_Arg1, Args &&... args)
Retrieves a list of values from the Lua stack, starting at the specified index.
bool IsParamNumber(int a_ParamIdx)
Returns true if the specified parameter is a number.
Used for storing references to object in the global registry.
bool IsValid(void) const
Returns true if the reference is valid.
static bool IsValidDataTypeCombination(int a_DataTypes)
Returns true if the datatype combination is valid.
static bool IsValidHeight(Vector3i a_BlockPosition)
Validates a height-coordinate.
ContiguousByteBufferView GetView() const
Returns a view (of type std::byte) of the internal store.
static void SaveToSchematicFile(const cBlockArea &a_BlockArea, const std::string &a_FileName)
Saves the area into a .schematic file.
static void LoadFromSchematicString(cBlockArea &a_BlockArea, ContiguousByteBufferView a_SchematicData)
Loads an area from a string containing the .schematic file data.
static Compression::Result SaveToSchematicString(const cBlockArea &a_BlockArea)
Saves the area into a string containing the .schematic file data.
static void LoadFromSchematicFile(cBlockArea &a_BlockArea, const std::string &a_FileName)
Loads an area from a .schematic file.