Update to sol3, vec3 customization, C++ 17 now required.

master
Nicole Collings 2020-07-23 18:54:11 -07:00
parent c7c2e38c38
commit d56d6f890a
69 changed files with 487 additions and 408 deletions

View File

@ -3,6 +3,16 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set warnings
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
add_compile_options(
-Werror -Wall -Wextra -pedantic -pedantic-errors
-Wnon-virtual-dtor -Wmisleading-indentation -Wlogical-op -Wnull-dereference
-Wno-unused-parameter -Wno-reorder -Wno-sign-compare)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options(/permissive /W4 /w14640)
endif()
set(PROJECT_NAME "Zepha")
set(MAIN_EXEC_NAME "Zepha")
set(TEST_EXEC_NAME "ZephaTest")
@ -16,7 +26,7 @@ find_path(LUA_HEADERS lua.hpp
/usr/local/include/lua5.1)
find_path(ASSIMP_HEADERS assimp/Importer.hpp)
find_path(ENET_HEADERS enet/enet.h)
find_path(NOISE_HEADERS libnoise/noise.h)
#find_path(NOISE_HEADERS libnoise/noise.h)
find_path(GLM_HEADERS glm/glm.hpp)
find_path(PTHREAD_HEADERS pthread.h)
@ -34,11 +44,8 @@ else()
find_library(PTHREAD_LIB pthread)
endif()
if (WIN32)
set(SOL_HEADERS lib/header/sol2/include)
else()
set(SOL_HEADERS lib/header/sol2/include_linux)
endif()
set(SOL_HEADERS lib/header/sol/include)
set(NOISE_HEADERS lib/static/noise/include)
include_directories(
${GLM_HEADERS}
@ -69,9 +76,6 @@ if(WIN32)
target_link_libraries(${MAIN_EXEC_NAME} winmm ws2_32)
endif()
# Enable Safeties
target_compile_definitions(${MAIN_EXEC_NAME} PUBLIC SOL_ALL_SAFETIES_ON)
# Test Build
add_subdirectory(test)
add_executable(${TEST_EXEC_NAME} test/Main.cpp)
@ -79,7 +83,6 @@ target_link_libraries(${TEST_EXEC_NAME} Zepha_Core)
target_link_libraries(${TEST_EXEC_NAME} Zepha_Test)
target_include_directories(${TEST_EXEC_NAME} PRIVATE ${GLFW_HEADERS})
target_compile_definitions(${TEST_EXEC_NAME} PUBLIC SOL_ALL_SAFETIES_ON)
target_link_libraries(${TEST_EXEC_NAME} ${LUA_LIB})
target_link_libraries (${TEST_EXEC_NAME} z)
target_link_libraries(${TEST_EXEC_NAME} ${ENET_LIB})

View File

@ -8,38 +8,57 @@ _G["vector"] = {}
-- x, y, and z must all be numbers.
local function create_vector(x, y, z)
local v = {x, y, z}
setmetatable(v, vector._mt)
setmetatable(v, vector)
return v
end
-- vector.equal
-- Return a boolean indicating if v1 == v2
function vector.equal(v1, v2)
assert(vector.is_vector(v1) and vector.is_vector(v2))
return (rawget(v1, 1) == rawget(v2, 1) and rawget(v1, 2) == rawget(v2, 2) and rawget(v1, 3) == rawget(v2, 3))
end
vector.__eq = vector.equal
-- vector.copy
-- Copies v and returns a new vector
function vector.copy(v)
assert(vector.is_vector(v))
return create_vector(rawget(v, 1), rawget(v, 2), rawget(v, 3))
end
-- vector.add
-- Add two vectors or a vector and number
function vector.add(v1, v2)
if not vector.is_vector(v1) then return end
if vector.is_vector(v2) then
return create_vector(rawget(v1, 1) + rawget(v2, 1), rawget(v1, 2) + rawget(v2, 2), rawget(v1, 3) + rawget(v2, 3))
elseif type(v2) == "number" then
return create_vector(rawget(v1, 1) + v2, rawget(v1, 2) + v2, rawget(v1, 3) + v2)
end
assert(vector.is_vector(v1))
if vector.is_vector(v2) then return create_vector(rawget(v1, 1) + rawget(v2, 1), rawget(v1, 2) + rawget(v2, 2), rawget(v1, 3) + rawget(v2, 3))
elseif type(v2) == "number" then return create_vector(rawget(v1, 1) + v2, rawget(v1, 2) + v2, rawget(v1, 3) + v2) end
end
vector.__add = vector.add
-- vector.negative
-- Flips a vector's content's signs
function vector.negative(v)
if not vector.is_vector(v) then return end
assert(vector.is_vector(v))
return create_vector(-rawget(v, 1), -rawget(v, 2), -rawget(v, 3))
end
vector.__unm = vector.negative
-- vector.subtract
-- Subtract v2 from v1
function vector.subtract(v1, v2)
return vector.add(v1, vector.negative(v2))
end
vector.__sub = vector.subtract
-- vector.multiply
-- Multiply v1 by a vector or number
function vector.multiply(v1, m)
if not vector.is_vector(v1) then return end
assert(vector.is_vector(v1))
if vector.is_vector(m) then
return create_vector(rawget(v1, 1) * rawget(m, 1), rawget(v1, 2) * rawget(m, 2), rawget(v1, 3) * rawget(m, 3))
elseif type(m) == "number" then
@ -47,10 +66,12 @@ function vector.multiply(v1, m)
end
end
vector.__mul = vector.multiply
-- vector.divide
-- Divice v1 by a vector or number
function vector.divide(v1, m)
if not vector.is_vector(v1) then return end
assert(vector.is_vector(v1))
if vector.is_vector(m) then
return create_vector(rawget(v1, 1) / rawget(m, 1), rawget(v1, 2) / rawget(m, 2), rawget(v1, 3) / rawget(m, 3))
elseif type(m) == "number" then
@ -58,134 +79,107 @@ function vector.divide(v1, m)
end
end
vector.__div = vector.divide
-- vector.pow
-- Return v to the power of p
function vector.pow(v, m)
if not vector.is_vector(v) then return end
assert(vector.is_vector(v))
local res = create_vector(rawget(v, 1), rawget(v, 2), rawget(v, 3))
for i = 1, power - 1 do res = res * v end
return res
end
-- vector.equal
-- Return a boolean indicating if v1 == v2
function vector.equal(v1, v2)
if not vector.is_vector(v1) or not vector.is_vector(v2) then return end
return (rawget(v1, 1) == rawget(v2, 1) and rawget(v1, 2) == rawget(v2, 2) and rawget(v1, 3) == rawget(v2, 3))
end
vector.__pow = vector.pow
-- vector.abs
-- Return the absolute value of v
function vector.abs(v)
if not vector.is_vector(v) then return end
assert(vector.is_vector(v))
return create_vector(math.abs(rawget(v, 1)), math.abs(rawget(v, 2)), math.abs(rawget(v, 3)))
end
-- vector.round
-- Round each vector value to the nearest integer
function vector.round(v)
if not vector.is_vector(v) then return end
assert(vector.is_vector(v))
return create_vector(math.round(rawget(v, 1)), math.round(rawget(v, 2)), math.round(rawget(v, 3)))
end
-- vector.floor
-- Floor each vector value to the lowest integer
function vector.floor(v)
if not vector.is_vector(v) then return end
assert(vector.is_vector(v))
return create_vector(math.floor(rawget(v, 1)), math.floor(rawget(v, 2)), math.floor(rawget(v, 3)))
end
-- vector.ceil
-- Ceil each vector value to the highest integer
function vector.ceil(v)
if not vector.is_vector(v) then return end
assert(vector.is_vector(v))
return create_vector(math.ceil(rawget(v, 1)), math.ceil(rawget(v, 2)), math.ceil(rawget(v, 3)))
end
-- vector.distance_squared
-- vector.length2
-- Get the square of the length of a vector
function vector.length2(v)
assert(vector.is_vector(v))
return rawget(v, 1) ^ 2 + rawget(v, 2) ^ 2 + rawget(v, 3) ^ 2
end
-- vector.length
-- Get the length of a vector
function vector.length(v)
assert(vector.is_vector(v))
return math.sqrt(vector.length2(v))
end
-- vector.distance2
-- Get the square of the distance between two vectors
-- This function is faster if you only need to compare two distances
function vector.distance_squared(v1, v2)
if not vector.is_vector(v1) or not vector.is_vector(v2) then return end
local diff = vector.abs(vector.subtract(v1, v2))
return diff.x ^ 2 + diff.y ^ 2 + diff.z ^ 2
function vector.distance2(v1, v2)
assert(vector.is_vector(v1) and vector.is_vector(v2))
return vector.length2(vector.abs(vector.subtract(v1, v2)))
end
-- vector.distance
-- Get the distance between two vectors
function vector.distance(v1, v2)
if not vector.is_vector(v1) or not vector.is_vector(v2) then return end
return math.sqrt(vector.distance_squared(v1, v2))
assert(vector.is_vector(v1) and vector.is_vector(v2))
return math.sqrt(vector.distance2(v1, v2))
end
-- Vector metatable
-- A metatable to be assigned to vectors which applies mathematic operators
vector._mt = {
__is_vector = true,
-- Alias x, y, z to 1, 2, 3
-- Value manipulation functions
__index = function(tbl, key)
if key == "x" then return rawget(tbl, 1) end
if key == "y" then return rawget(tbl, 2) end
if key == "z" then return rawget(tbl, 3) end
function vector:__index(key)
if key == "x" then return rawget(self, 1) end
if key == "y" then return rawget(self, 2) end
if key == "z" then return rawget(self, 3) end
return getmetatable(self)[key]
end
local val = rawget(tbl, key)
if val == nil then val = rawget(getmetatable(tbl), key) end
return val
end,
__newindex = function(tbl, key, val)
if key == "x" or key == 0 then rawset(tbl, 1, val)
elseif key == "y" or key == 1 then rawset(tbl, 2, val)
elseif key == "z" or key == 2 then rawset(tbl, 3, val) end
end,
__tostring = function(tbl)
return table.concat({
"{ ",
tostring(tbl[1]), ", ",
tostring(tbl[2]), ", ",
tostring(tbl[3]), " }"
})
end,
__eq = function(tbl, o)
return vector.equal(tbl, o)
end,
function vector:__newindex(key, val)
if key == "x" then rawset(self, 1, val)
elseif key == "y" then rawset(self, 2, val)
elseif key == "z" then rawset(self, 3, val) end
end
-- Arithmetic functions
__unm = function(tbl)
return vector.negative(tbl)
end,
__add = function(tbl, o)
return vector.add(tbl, o)
end,
__sub = function(tbl, o)
return vector.subtract(tbl, o)
end,
__mul = function(tbl, o)
return vector.multiply(tbl, o)
end,
__div = function(tbl, o)
return vector.divide(tbl, o)
end,
__pow = function(tbl, power)
return vector.pow(tbl, o)
end,
function vector:__tostring()
return table.concat({
"{ ",
tostring(rawget(self, 1)), ", ",
tostring(rawget(self, 2)), ", ",
tostring(rawget(self, 3)), " }"
})
end
-- Higher level methods
abs = vector.abs,
round = vector.round,
floor = vector.floor,
ceil = vector.ceil,
dist = vector.distance,
distance, vector.distance,
dist2 = vector.distance_squared,
distance_squared = vector.distance_squared
}
function vector.__concat(a, b)
return tostring(a) .. tostring(b)
end
-- vector.is_vector
-- Checks if a value is a vector.
function vector.is_vector(v)
if type(v) ~= "table" or not v.__is_vector then return false end
return true
return getmetatable(v) == vector
end
-- vector.new
@ -193,7 +187,7 @@ end
-- Table constructor works with 1-3 values
vector.new = function(x, y, z)
-- Blank new constructor, return empty vector
if x == nil then return create_vector(0, 0, 0)
if x == nil or (type(x) == "table" and x[1] == nil) then return create_vector(0, 0, 0)
-- Invalid type passed to function, return nil
elseif type(x) ~= "number" and type(x) ~= "table" then return nil
-- Passed a table as x with at least x and y parameters, z will be set to table value or 0

View File

@ -319,6 +319,6 @@ set(ZEPHA_SRC
def/gen/MapGenProps.cpp
def/gen/MapGenProps.h
lua/api/class/LuaGuiElement.cpp
lua/api/class/LuaGuiElement.h world/Dimension.cpp world/Dimension.h world/fs/FileManipulator.cpp world/fs/FileManipulator.h def/item/BlockModel.cpp net/server/world/ServerPacketStream.cpp net/server/world/ServerPacketStream.h lua/register/CreateBlockModel.cpp lua/register/CreateBlockModel.h)
lua/api/class/LuaGuiElement.h world/Dimension.cpp world/Dimension.h world/fs/FileManipulator.cpp world/fs/FileManipulator.h def/item/BlockModel.cpp net/server/world/ServerPacketStream.cpp net/server/world/ServerPacketStream.h lua/register/CreateBlockModel.cpp lua/register/CreateBlockModel.h lua/customization/vec3.hpp)
add_library (Zepha_Core ${ZEPHA_SRC})

View File

@ -20,6 +20,18 @@
#include "StartGame.h"
int main(int argc, char* argv[]) {
// sol::state lua;
// lua.open_libraries(sol::lib::base);
//
// lua.script(R"(
// number = 32
// )");
//
// unsigned short n = lua.get<unsigned short>("number");
// std::cout << n << std::endl;
return StartGame(argc, argv);
}

View File

@ -5,12 +5,12 @@
#pragma once
#include <string>
#include <sol2/sol.hpp>
#include "../ItemDef.h"
#include "BlockModel.h"
#include "SelectionBox.h"
#include "../../lua/Lua.h"
#include "../../util/Util.h"
#include "../../lua/Callback.h"

View File

@ -6,6 +6,7 @@
#include <memory>
#include <vector>
#include <functional>
#include <glm/vec2.hpp>
#include <glm/mat4x4.hpp>
#include <assimp/scene.h>

View File

@ -23,14 +23,14 @@ std::shared_ptr<GuiComponent> GameGuiBuilder::createComponent(LuaGuiElement& ele
elem.updateFunction = std::bind(&GameGuiBuilder::elementUpdated, this);
if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) {
elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); });
if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) {
elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); });
if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) {
elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); });
// if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) {
// elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); });
//
// if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) {
// elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); });
//
// if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) {
// elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); });
return c;
}

View File

@ -75,14 +75,14 @@ std::shared_ptr<GuiComponent> GuiBuilder::createComponent(LuaGuiElement& elem, g
elem.updateFunction = std::bind(&GuiBuilder::elementUpdated, this);
if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) {
elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); });
if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) {
elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); });
if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) {
elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); });
// if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) {
// elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); });
//
// if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) {
// elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); });
//
// if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) {
// elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); });
return c;
}

View File

@ -15,7 +15,6 @@
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>
#include <sol2/sol.hpp>
#include "../../util/Any.h"
#include "../../lua/api/class/LuaGuiElement.h"

View File

@ -7,6 +7,7 @@
#include <list>
#include <memory>
#include <functional>
#include "../../entity/Entity.h"
@ -42,6 +43,7 @@ public:
return std::static_pointer_cast<T>(it);
}
}
return nullptr;
};
std::shared_ptr<GuiComponent> insert(unsigned int index, std::shared_ptr<GuiComponent> component);

View File

@ -266,10 +266,10 @@ void InventoryList::setStack(unsigned short i, const ItemStack &stack) {
}
}
void InventoryList::setLuaCallback(InventoryList::Callback type, sol::function cb) {
void InventoryList::setLuaCallback(InventoryList::Callback type, sol::safe_function cb) {
luaCallbacks[static_cast<size_t>(type)] = cb;
}
sol::function InventoryList::getLuaCallback(InventoryList::Callback type) {
sol::safe_function InventoryList::getLuaCallback(InventoryList::Callback type) {
return luaCallbacks[static_cast<size_t>(type)];
}

View File

@ -5,8 +5,8 @@
#pragma once
#include <string>
#include <sol2/sol.hpp>
#include "../../lua/Lua.h"
#include "ItemStack.h"
class DefinitionAtlas;
@ -53,7 +53,7 @@ public:
void setStack(unsigned short i, const ItemStack& stack);
sol::function getLuaCallback(Callback type);
void setLuaCallback(Callback type, sol::function cb);
void setLuaCallback(Callback type, sol::safe_function cb);
DefinitionAtlas& defs;

View File

@ -28,6 +28,7 @@ bool ServerInventoryList::addWatcher(unsigned int cid) {
watchers.push_back(cid);
sendTo(client);
return true;
}
bool ServerInventoryList::removeWatcher(unsigned int cid) {

View File

@ -62,12 +62,12 @@ void LocalWorld::blockPlace(glm::vec3 pos, unsigned int block) {
if (block == LocalDefinitionAtlas::AIR) {
auto def = defs.defs.blockFromId(getBlock(pos));
if (def.callbacks.count(Callback::BREAK_CLIENT))
defs.parser.safe_function(def.callbacks[Callback::BREAK_CLIENT], defs.parser.luaVec(pos));
defs.parser.safe_function(def.callbacks[Callback::BREAK_CLIENT], pos);
}
else {
auto def = defs.defs.blockFromId(block);
if (def.callbacks.count(Callback::PLACE_CLIENT))
defs.parser.safe_function(def.callbacks[Callback::PLACE_CLIENT], defs.parser.luaVec(pos));
defs.parser.safe_function(def.callbacks[Callback::PLACE_CLIENT], pos);
}
net->blockPlace(pos, block);
@ -82,7 +82,7 @@ void LocalWorld::blockInteract(PointedThing &thing) {
auto def = defs.defs.blockFromId(getBlock(thing.target.block.pos));
if (def.callbacks.count(Callback::INTERACT_CLIENT))
defs.parser.safe_function(def.callbacks[Callback::INTERACT_CLIENT], defs.parser.luaVec(thing.target.block.pos));
defs.parser.safe_function(def.callbacks[Callback::INTERACT_CLIENT], thing.target.block.pos);
net->blockInteract(thing.target.block.pos);
}

6
src/lua/Lua.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include "customization/vec3.hpp"

View File

@ -6,7 +6,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "Lua.h"
class LocalLuaParser;

View File

@ -13,21 +13,10 @@ void LuaParser::update(double delta) {
DelayedFunction& f = *it;
f.timeout -= delta;
if (f.timeout <= 0) {
if (f.function(sol::as_args(f.args))) {
f.timeout = f.initial_timeout;
} else {
it = delayed_functions.erase(it);
continue;
}
sol::optional<bool> res = f.function(sol::as_args(f.args));
if (res && *res) f.timeout = f.initial_timeout;
else { it = delayed_functions.erase(it); continue; }
}
it++;
}
}
sol::table LuaParser::luaVec(glm::vec3 vec) {
return lua["vector"]["new"](vec.x, vec.y, vec.z);
}
sol::table LuaParser::luaVec(sol::state_view s, glm::vec3 vec) {
return s["vector"]["new"](vec.x, vec.y, vec.z);
}
}

View File

@ -8,16 +8,14 @@
#include <list>
#include <glm/vec3.hpp>
#include <sol2/sol.hpp>
#include "Lua.h"
class LuaParser {
public:
constexpr static double UPDATE_STEP {1 / 60.0};
virtual void update(double delta);
sol::table luaVec(glm::vec3 vec);
static sol::table luaVec(sol::state_view s, glm::vec3 vec);
sol::state lua;
sol::table core;

View File

@ -4,13 +4,12 @@
#include "LocalLuaAnimationManager.h"
#include "../../Lua.h"
#include "../../../game/entity/Entity.h"
#include "../../../game/entity/AnimationSegment.h"
LocalLuaAnimationManager::LocalLuaAnimationManager(Entity &entity) :
entity(entity) {
}
entity(entity) {}
void LocalLuaAnimationManager::define(sol::table anims) {
std::vector<AnimationSegment> animations;

View File

@ -4,7 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
class Entity;

View File

@ -4,35 +4,34 @@
#include "LocalLuaEntity.h"
#include "../../Lua.h"
#include "../../LuaParser.h"
#include "../../../def/ClientGame.h"
#include "../../../def/item/BlockDef.h"
#include "../../../def/item/CraftItemDef.h"
void LocalLuaEntity::snap_pos(const sol::table &pos) {
entity->setPos({pos[1], pos[2], pos[3]});
void LocalLuaEntity::snap_pos(glm::vec3 pos) {
entity->setPos(pos);
}
void LocalLuaEntity::set_pos(const sol::table &pos) {
entity->interpPos({pos[1], pos[2], pos[3]});
void LocalLuaEntity::set_pos(glm::vec3 pos) {
entity->interpPos(pos);
}
sol::table LocalLuaEntity::get_pos(sol::this_state s) {
glm::vec3 pos = entity->getPos();
return LuaParser::luaVec(sol::state_view(s), pos);
glm::vec3 LocalLuaEntity::get_pos() {
return entity->getPos();
}
void LocalLuaEntity::snap_visual_offset(const sol::table &vs) {
entity->setVisualOffset({vs["x"], vs["y"], vs["z"]});
void LocalLuaEntity::snap_visual_offset(glm::vec3 vs) {
entity->setVisualOffset(vs);
}
void LocalLuaEntity::set_visual_offset(const sol::table &pos) {
entity->interpVisualOffset({pos[1], pos[2], pos[3]});
void LocalLuaEntity::set_visual_offset(glm::vec3 vs) {
entity->interpVisualOffset(vs);
}
sol::table LocalLuaEntity::get_visual_offset(sol::this_state s) {
glm::vec3 v = entity->getVisualOffset();
return LuaParser::luaVec(sol::state_view(s), v);
glm::vec3 LocalLuaEntity::get_visual_offset() {
return entity->getVisualOffset();
}
void LocalLuaEntity::snap_pitch(float rot) {

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "LocalLuaAnimationManager.h"
#include "../../../game/entity/Entity.h"
@ -22,13 +21,13 @@ public:
LocalLuaAnimationManager manager;
void snap_pos(const sol::table& pos);
void set_pos(const sol::table& pos);
sol::table get_pos(sol::this_state s);
void snap_pos(glm::vec3 pos);
void set_pos(glm::vec3 pos);
glm::vec3 get_pos();
void snap_visual_offset(const sol::table& vs);
void set_visual_offset(const sol::table& pos);
sol::table get_visual_offset(sol::this_state s);
void snap_visual_offset(glm::vec3 vs);
void set_visual_offset(glm::vec3 vs);
glm::vec3 get_visual_offset();
void snap_pitch(float rot);
void set_pitch(float rot);

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../../../game/inventory/LocalInventory.h"
class LocalLuaInventory {

View File

@ -10,27 +10,24 @@
#include "LocalLuaInventoryList.h"
#include "../../../game/scene/world/Player.h"
sol::table LocalLuaPlayer::get_pos(sol::this_state s) {
glm::vec3 pos = player.getPos();
return LuaParser::luaVec(sol::state_view(s), pos);
glm::vec3 LocalLuaPlayer::get_pos() {
return player.getPos();
}
sol::table LocalLuaPlayer::get_block_pos(sol::this_state s) {
glm::vec3 pos = glm::floor(player.getPos());
return LuaParser::luaVec(sol::state_view(s), pos);
glm::vec3 LocalLuaPlayer::get_block_pos() {
return glm::floor(player.getPos());
}
void LocalLuaPlayer::set_pos(const sol::table &pos) {
player.setPos({pos[1], pos[2], pos[3]}, true);
void LocalLuaPlayer::set_pos(glm::vec3 pos) {
player.setPos(pos, true);
}
sol::table LocalLuaPlayer::get_vel(sol::this_state s) {
glm::vec3 vel = player.getVel();
return LuaParser::luaVec(sol::state_view(s), vel);
glm::vec3 LocalLuaPlayer::get_vel() {
return player.getVel();
}
void LocalLuaPlayer::set_vel(const sol::table &vel) {
player.setVel({vel[1], vel[2], vel[3]}, true);
void LocalLuaPlayer::set_vel(glm::vec3 vel) {
player.setVel(vel, true);
}
float LocalLuaPlayer::get_look_yaw() {

View File

@ -4,7 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
//#include <sol/forward.hpp>
#include "LocalLuaInventory.h"
@ -17,12 +17,12 @@ public:
Player& player;
sol::table get_pos(sol::this_state s);
sol::table get_block_pos(sol::this_state s);
void set_pos(const sol::table& pos);
glm::vec3 get_pos();
glm::vec3 get_block_pos();
void set_pos(glm::vec3 pos);
sol::table get_vel(sol::this_state s);
void set_vel(const sol::table& vel);
glm::vec3 get_vel();
void set_vel(glm::vec3 vel);
float get_look_yaw();
void set_look_yaw(float rot);

View File

@ -171,5 +171,6 @@ Any LuaGuiElement::getAsAny(const std::string &key) const {
return Any::from<glm::vec4>(values);
}
}
else throw std::runtime_error("Invalid type requested in getAsAny");
throw std::runtime_error("Invalid type requested in getAsAny");
}

View File

@ -5,8 +5,8 @@
#pragma once
#include <list>
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../../../util/Any.h"
class LuaGuiElement {

View File

@ -4,6 +4,7 @@
#include "LuaItemStack.h"
#include "../../Lua.h"
#include "../../../def/ItemDef.h"
#include "../../../def/DefinitionAtlas.h"
#include "../../../game/inventory/ItemStack.h"

View File

@ -4,7 +4,8 @@
#pragma once
#include <sol2/sol.hpp>
#include <string>
#include <sol/forward.hpp>
class DefinitionAtlas;
class ItemStack;

View File

@ -8,30 +8,28 @@
#include "../../../def/ItemDef.h"
#include "../../../def/ServerGame.h"
void ServerLuaEntity::snap_pos(const sol::table &pos) {
entity->setPos({pos[1], pos[2], pos[3]});
void ServerLuaEntity::snap_pos(glm::vec3 pos) {
entity->setPos(pos);
}
void ServerLuaEntity::set_pos(const sol::table &pos) {
entity->setPos({pos[1], pos[2], pos[3]});
void ServerLuaEntity::set_pos(glm::vec3 pos) {
entity->setPos(pos);
}
sol::table ServerLuaEntity::get_pos(sol::this_state s) {
glm::vec3 pos = entity->getPos();
return LuaParser::luaVec(sol::state_view(s), pos);
glm::vec3 ServerLuaEntity::get_pos() {
return entity->getPos();
}
void ServerLuaEntity::snap_visual_offset(const sol::table &vs) {
entity->setVisualOffset({vs[1], vs[2], vs[3]});
void ServerLuaEntity::snap_visual_offset(glm::vec3 vs) {
entity->setVisualOffset(vs);
}
void ServerLuaEntity::set_visual_offset(const sol::table &pos) {
entity->setVisualOffset({pos[1], pos[2], pos[3]});
void ServerLuaEntity::set_visual_offset(glm::vec3 vs) {
entity->setVisualOffset(vs);
}
sol::table ServerLuaEntity::get_visual_offset(sol::this_state s) {
glm::vec3 v = entity->getVisualOffset();
return LuaParser::luaVec(sol::state_view(s), v);
glm::vec3 ServerLuaEntity::get_visual_offset() {
return entity->getVisualOffset();
}
void ServerLuaEntity::snap_pitch(float rot) {

View File

@ -5,8 +5,8 @@
#pragma once
#include <memory>
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../../../net/server/world/ServerEntity.h"
class ServerGame;
@ -20,13 +20,13 @@ public:
unsigned int id;
ServerGame& defs;
void snap_pos(const sol::table& pos);
void set_pos(const sol::table& pos);
sol::table get_pos(sol::this_state s);
void snap_pos(glm::vec3 pos);
void set_pos(glm::vec3 pos);
glm::vec3 get_pos();
void snap_visual_offset(const sol::table& vs);
void set_visual_offset(const sol::table& pos);
sol::table get_visual_offset(sol::this_state s);
void snap_visual_offset(glm::vec3 vs);
void set_visual_offset(glm::vec3 vs);
glm::vec3 get_visual_offset();
void snap_pitch(float rot);
void set_pitch(float rot);

View File

@ -28,5 +28,5 @@ void ServerLuaInventory::set_default_list(sol::object list) {
sol::object ServerLuaInventory::get_default_list(sol::this_state s) {
if (inventory.getDefaultList() == "") return sol::nil;
else get_list(s, inventory.getDefaultList());
else return get_list(s, inventory.getDefaultList());
}

View File

@ -44,7 +44,7 @@ void ServerLuaInventoryList::set_stack(unsigned short i, LuaItemStack stack) {
void ServerLuaInventoryList::set_stack(unsigned short i, sol::table stack) {
if (i < 1 || i > list.getLength()) throw "index is outside of list bounds.";
list.setStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2)));
list.setStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack[2]));
}
LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, LuaItemStack stack) {
@ -54,7 +54,7 @@ LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, LuaItemStack
LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, sol::table stack) {
if (i < 1 || i > list.getLength()) throw "index is outside of list bounds.";
return LuaItemStack(list.placeStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2))), list.defs);
return LuaItemStack(list.placeStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack[2])), list.defs);
}
LuaItemStack ServerLuaInventoryList::split_stack(unsigned short i) {
@ -67,7 +67,7 @@ LuaItemStack ServerLuaInventoryList::add_stack(LuaItemStack stack) {
}
LuaItemStack ServerLuaInventoryList::add_stack(sol::table stack) {
return LuaItemStack(list.addStack(ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2))), list.defs);
return LuaItemStack(list.addStack(ItemStack(list.defs.fromStr(stack[1]).index, stack[2])), list.defs);
}
int ServerLuaInventoryList::stack_fits(LuaItemStack stack) {
@ -75,7 +75,7 @@ int ServerLuaInventoryList::stack_fits(LuaItemStack stack) {
}
int ServerLuaInventoryList::stack_fits(sol::table stack) {
return list.stackFits(ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2)));
return list.stackFits(ItemStack(list.defs.fromStr(stack[1]).index, stack[2]));
}
LuaItemStack ServerLuaInventoryList::take_stack(LuaItemStack request) {
@ -83,7 +83,7 @@ LuaItemStack ServerLuaInventoryList::take_stack(LuaItemStack request) {
}
LuaItemStack ServerLuaInventoryList::take_stack(sol::table request) {
return LuaItemStack(list.takeStack(ItemStack(list.defs.fromStr(request[1]).index, request.get<unsigned short>(2))), list.defs);
return LuaItemStack(list.takeStack(ItemStack(list.defs.fromStr(request[1]).index, request[2])), list.defs);
}
LuaItemStack ServerLuaInventoryList::remove_stack(unsigned short i, unsigned short count) {
@ -91,10 +91,10 @@ LuaItemStack ServerLuaInventoryList::remove_stack(unsigned short i, unsigned sho
return LuaItemStack(list.removeStack(i - 1, count), list.defs);
}
void ServerLuaInventoryList::set_callback(ServerInventoryList::Callback t, sol::function fun) {
void ServerLuaInventoryList::set_callback(ServerInventoryList::Callback t, sol::safe_function fun) {
list.setLuaCallback(t, fun);
}
sol::function ServerLuaInventoryList::get_callback(ServerInventoryList::Callback t) {
sol::safe_function ServerLuaInventoryList::get_callback(ServerInventoryList::Callback t) {
return list.getLuaCallback(t);
}

View File

@ -45,6 +45,6 @@ public:
LuaItemStack remove_stack(unsigned short ind, unsigned short count);
void set_callback(ServerInventoryList::Callback t, sol::function fun);
sol::function get_callback(ServerInventoryList::Callback t);
void set_callback(ServerInventoryList::Callback t, sol::safe_function fun);
sol::safe_function get_callback(ServerInventoryList::Callback t);
};

View File

@ -20,27 +20,24 @@ std::string ServerLuaPlayer::get_address() {
return NetHandler::intToIPString(player.address.host) + ":" + std::to_string(player.address.port);
}
sol::table ServerLuaPlayer::get_pos(sol::this_state s) {
glm::vec3 pos = player.getPos();
return LuaParser::luaVec(sol::state_view(s), pos);
glm::vec3 ServerLuaPlayer::get_pos() {
return player.getPos();
}
sol::table ServerLuaPlayer::get_block_pos(sol::this_state s) {
glm::vec3 pos = glm::floor(player.getPos());
return LuaParser::luaVec(sol::state_view(s), pos);
glm::vec3 ServerLuaPlayer::get_block_pos() {
return glm::floor(player.getPos());
}
void ServerLuaPlayer::set_pos(const sol::table &pos) {
player.setPos({pos[1], pos[2], pos[3]}, true);
void ServerLuaPlayer::set_pos(glm::vec3 pos) {
player.setPos(pos, true);
}
sol::table ServerLuaPlayer::get_vel(sol::this_state s) {
glm::vec3 vel = player.getVel();
return LuaParser::luaVec(sol::state_view(s), vel);
glm::vec3 ServerLuaPlayer::get_vel() {
return player.getVel();
}
void ServerLuaPlayer::set_vel(const sol::table &vel) {
player.setVel({vel[1], vel[2], vel[3]}, true);
void ServerLuaPlayer::set_vel(glm::vec3 vel) {
player.setVel(vel, true);
}
float ServerLuaPlayer::get_look_yaw() {

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "ServerLuaInventory.h"
#include "../../../net/server/conn/ServerClient.h"
@ -20,12 +19,12 @@ public:
unsigned int get_cid();
std::string get_address();
sol::table get_pos(sol::this_state s);
sol::table get_block_pos(sol::this_state s);
void set_pos(const sol::table& pos);
glm::vec3 get_pos();
glm::vec3 get_block_pos();
void set_pos(glm::vec3 pos);
sol::table get_vel(sol::this_state s);
void set_vel(const sol::table& vel);
glm::vec3 get_vel();
void set_vel(glm::vec3 vel);
float get_look_yaw();
void set_look_yaw(float rot);

View File

@ -4,7 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
namespace Api {
static void trigger_event(sol::state& lua) {
@ -19,14 +19,5 @@ namespace Api {
end
end
)");
lua.script(R"(
zepha.__builtin.add_player = function(player)
table.insert(zepha.players, player)
zepha.__builtin.trigger_event("new_player", player)
zepha.__builtin.trigger_event("player_join", player)
end
)");
}
}

View File

@ -4,7 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
namespace Api {
static void update_entities(sol::state& lua) {

View File

@ -5,8 +5,8 @@
#pragma once
#include <list>
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../../LuaParser.h"
namespace MenuApi {

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../../../game/hud/GuiBuilder.h"
class LuaGuiElement;

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../../../game/ClientState.h"
namespace MenuApi {

View File

@ -17,15 +17,12 @@ namespace Api {
static void add_entity_c(sol::state& lua, sol::table& core, ClientGame& defs, LocalWorld& world) {
core["entities"] = lua.create_table();
core.set_function("add_entity", [&](sol::optional<sol::table> pos, sol::optional<std::string> identifier, sol::object staticData) {
if (!identifier || !identifier->length()) throw std::runtime_error("Expected an identifier as the second argument.");
if (!pos) throw std::runtime_error("Expected a position as the first argument.");
if (core["registered_entities"][*identifier] == sol::nil) throw std::runtime_error("identifier '" + *identifier + "' is not a valid entity identifier.");
sol::table entityDef = core["registered_entities"][*identifier];
core.set_function("add_entity", [&](glm::vec3 pos, std::string identifier, sol::object staticData) {
if (core["registered_entities"][identifier] == sol::nil) throw std::runtime_error("identifier '" + identifier + "' is not a valid entity identifier.");
sol::table entityDef = core["registered_entities"][identifier];
auto entity = std::make_unique<Entity>();
entity->setPos({pos->get<float>("x"), pos->get<float>("y"), pos->get<float>("z")});
entity->setPos(pos);
auto entityRef = std::make_shared<LocalLuaEntity>(std::move(entity), entities_ind++, defs);
sol::table luaEntity = lua.create_table();
@ -37,9 +34,9 @@ namespace Api {
auto displayObject = luaEntity.get<sol::optional<std::string>>("display_object");
auto displayTexture = luaEntity.get<sol::optional<std::string>>("display_texture");
if (!displayType) throw std::runtime_error("entity '" + *identifier + "' is missing the display property.");
if (!displayObject) throw std::runtime_error("entity '" + *identifier + "' is missing the display_object property.");
if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + *identifier + "' is missing the display_texture property.");
if (!displayType) throw std::runtime_error("entity '" + identifier + "' is missing the display property.");
if (!displayObject) throw std::runtime_error("entity '" + identifier + "' is missing the display_object property.");
if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + identifier + "' is missing the display_texture property.");
entityRef->set_display_type(*displayType, *displayObject, displayTexture);
@ -55,15 +52,12 @@ namespace Api {
static void add_entity_s(sol::state& lua, sol::table& core, ServerGame& defs, ServerWorld& world) {
core["entities"] = lua.create_table();
core.set_function("add_entity", [&](sol::optional<sol::table> pos, sol::optional<std::string> identifier, sol::object staticData) {
if (!identifier || !identifier->length()) throw std::runtime_error("Expected an identifier as the second argument.");
if (!pos) throw std::runtime_error("Expected a position as the first argument.");
if (core["registered_entities"][*identifier] == sol::nil) throw std::runtime_error("identifier '" + *identifier + "' is not a valid entity identifier.");
sol::table entityDef = core["registered_entities"][*identifier];
core.set_function("add_entity", [&](glm::vec3 pos, std::string identifier, sol::object staticData) {
if (core["registered_entities"][identifier] == sol::nil) throw std::runtime_error("identifier '" + identifier + "' is not a valid entity identifier.");
sol::table entityDef = core["registered_entities"][identifier];
auto entity = std::make_unique<ServerEntity>(entities_ind);
entity->setPos({pos->get<float>("x"), pos->get<float>("y"), pos->get<float>("z")});
entity->setPos(pos);
auto entityRef = std::make_shared<ServerLuaEntity>(std::move(entity), entities_ind++, defs);
sol::table luaEntity = lua.create_table();
@ -75,9 +69,9 @@ namespace Api {
auto displayObject = luaEntity.get<sol::optional<std::string>>("display_object");
auto displayTexture = luaEntity.get<sol::optional<std::string>>("display_texture");
if (!displayType) throw std::runtime_error("entity '" + *identifier + "' is missing the display property.");
if (!displayObject) throw std::runtime_error("entity '" + *identifier + "' is missing the display_object property.");
if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + *identifier + "' is missing the display_texture property.");
if (!displayType) throw std::runtime_error("entity '" + identifier + "' is missing the display property.");
if (!displayObject) throw std::runtime_error("entity '" + identifier + "' is missing the display_object property.");
if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + identifier + "' is missing the display_texture property.");
entityRef->set_display_type(*displayType, *displayObject, displayTexture);

View File

@ -11,7 +11,7 @@ namespace Api {
core.set_function("create_structure", [&](sol::optional<sol::table> data) {
if (!data) throw "expected a table as the first argument.";
auto origin = data->get<sol::optional<sol::table>>("origin");
auto origin = data->get<sol::optional<glm::vec3>>("origin");
auto schematic = data->get<sol::optional<sol::table>>("schematic");
if (!origin) throw std::runtime_error("expected a table as the first argument.");
@ -26,7 +26,7 @@ namespace Api {
s->dimensions = {xWid, yWid, zWid};
s->stringData.resize(xWid * yWid * zWid);
s->origin = {origin->get<unsigned int>(1), origin->get<unsigned int>(2), origin->get<unsigned int>(3)};
s->origin = origin ? glm::ivec3{*origin} : glm::ivec3 {};
for (unsigned int y = 1; y <= yWid; y++) {
for (unsigned int z = 1; z <= zWid; z++) {

View File

@ -9,11 +9,8 @@
namespace Api {
static void get_block(sol::table &core, DefinitionAtlas& defs, World& world) {
core.set_function("get_block", [&](sol::table pos) -> std::string {
if (!pos.get<sol::optional<float>>("x") || !pos.get<sol::optional<float>>("y") || !pos.get<sol::optional<float>>("z"))
throw std::runtime_error("Expected a vector as the first argument.");
return defs.fromId(world.getBlock({pos.get<float>("x"), pos.get<float>("y"), pos.get<float>("z")})).identifier;
core.set_function("get_block", [&](glm::ivec3 pos) {
return defs.fromId(world.getBlock(pos)).identifier;
});
}
}

View File

@ -9,10 +9,9 @@
namespace Api {
static void set_block(sol::table &core, DefinitionAtlas& defs, World& world) {
core.set_function("set_block", [&](sol::table pos, std::string identifier) {
if (!pos["x"] || !pos["y"] || !pos["z"]) throw std::runtime_error("expected a vector as the first argument.");
core.set_function("set_block", [&](glm::vec3 pos, std::string identifier) {
auto& block = defs.fromStr(identifier);
world.setBlock({pos.get<float>("x"), pos.get<float>("y"), pos.get<float>("z")}, block.index);
world.setBlock(pos, block.index);
});
}
}

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../class/LocalLuaInventory.h"
#include "../class/LocalLuaInventoryList.h"

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../class/LuaItemStack.h"
namespace ClientApi {

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../class/LocalLuaEntity.h"
#include "../../../game/scene/world/LocalWorld.h"

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../class/ServerLuaInventory.h"
#include "../class/ServerLuaInventoryList.h"

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../../Lua.h"
#include "../class/ServerLuaEntity.h"
namespace ServerApi {

View File

@ -0,0 +1,129 @@
//
// Vec3 to Vector table customization.
// Allows sol to transfer glm::vec3 and glm::vec3 to a Lua vector table and back.
// This customization was based off of code provided by rubenwardy.
// Created by aurailus on 2020-07-22.
//
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedStructInspection"
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#pragma once
#include <glm/vec3.hpp>
#include "../../util/Util.h"
namespace sol {
// Float
template <>
struct lua_size<glm::vec3> : std::integral_constant<int, 1> {};
template <>
struct lua_type_of<glm::vec3> : std::integral_constant<type, type::table> {};
template <typename Handler>
inline bool sol_lua_check(types<glm::vec3>, lua_State* L, int index, Handler&& handler, stack::record& tracking) {
int absoluteIndex = lua_absindex(L, index);
if (!stack::check<table>(L, absoluteIndex, handler)) {
tracking.use(1);
return false;
}
stack::get_field(L, 1, absoluteIndex);
bool x = stack::check<float>(L, -1);
stack::get_field(L, 2, absoluteIndex);
bool y = stack::check<float>(L, -1);
stack::get_field(L, 3, absoluteIndex);
bool z = stack::check<float>(L, -1);
stack::pop_n(L, 3);
tracking.use(1);
return x && y && z;
}
inline glm::vec3 sol_lua_get(types<glm::vec3>, lua_State* L, int index, stack::record& tracking) {
int absoluteIndex = lua_absindex(L, index);
sol::table table = stack::get<sol::table>(L, absoluteIndex);
float x = table[1];
float y = table[2];
float z = table[3];
tracking.use(1);
return glm::vec3 { x, y, z };
}
inline int sol_lua_push(sol::types<glm::vec3>, lua_State* L, const glm::vec3& vec) {
sol::state_view lua(L);
lua_createtable(L, 3, 0);
sol::stack_table tbl(L, -1);
tbl[1] = vec.x; tbl[2] = vec.y; tbl[3] = vec.z;
tbl[sol::metatable_key] = lua["vector"];
return 1;
}
// Float
template <>
struct lua_size<glm::ivec3> : std::integral_constant<int, 1> {};
template <>
struct lua_type_of<glm::ivec3> : std::integral_constant<type, type::table> {};
template <typename Handler>
inline bool sol_lua_check(types<glm::ivec3>, lua_State* L, int index, Handler&& handler, stack::record& tracking) {
int absoluteIndex = lua_absindex(L, index);
if (!stack::check<table>(L, absoluteIndex, handler)) {
tracking.use(1);
return false;
}
stack::get_field(L, 1, absoluteIndex);
bool x = stack::check<int>(L, -1);
stack::get_field(L, 2, absoluteIndex);
bool y = stack::check<int>(L, -1);
stack::get_field(L, 3, absoluteIndex);
bool z = stack::check<int>(L, -1);
stack::pop_n(L, 3);
tracking.use(1);
return x && y && z;
}
inline glm::ivec3 sol_lua_get(types<glm::ivec3>, lua_State* L, int index, stack::record& tracking) {
int absoluteIndex = lua_absindex(L, index);
sol::table table = stack::get<sol::table>(L, absoluteIndex);
int x = table[1];
int y = table[2];
int z = table[3];
tracking.use(1);
return glm::ivec3 { x, y, z };
}
inline int sol_lua_push(sol::types<glm::ivec3>, lua_State* L, const glm::ivec3& vec) {
sol::state_view lua(L);
lua_createtable(L, 3, 0);
sol::stack_table tbl(L, -1);
tbl[1] = vec.x; tbl[2] = vec.y; tbl[3] = vec.z;
tbl[sol::metatable_key] = lua["vector"];
return 1;
}
}
#pragma clang diagnostic pop

View File

@ -123,33 +123,36 @@ sol::protected_function_result LocalLuaParser::errorCallback(sol::protected_func
sol::error err = errPfr;
std::string errString = err.what();
std::string::size_type slash = errString.find('/');
assert(slash != std::string::npos);
try {
std::string::size_type slash = errString.find('/');
if (slash != std::string::npos) throw "npos";
std::string modString = errString.substr(0, slash);
std::string modString = errString.substr(0, slash);
std::string::size_type lineNumStart = errString.find(':', slash);
assert(lineNumStart != std::string::npos);
std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1);
assert(lineNumEnd != std::string::npos);
std::string::size_type lineNumStart = errString.find(':', slash);
if (lineNumStart != std::string::npos) throw "lineNumStart";
std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1);
if (lineNumStart != std::string::npos) throw "lineNumEnd";
std::string fileName = errString.substr(0, lineNumStart);
int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1));
std::string fileName = errString.substr(0, lineNumStart);
int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1));
for (const auto& mod : handler.cGetMods()) {
if (mod.config.name == modString) {
for (auto& file : mod.files) {
if (file.path == fileName) {
std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl;
exit(1);
for (const auto& mod : handler.cGetMods()) {
if (mod.config.name == modString) {
for (auto& file : mod.files) {
if (file.path == fileName) {
std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl;
break;
}
}
break;
}
break;
}
}
std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:"
<< std::endl << errString << Log::endl;
catch (...) {
std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:"
<< std::endl << std::endl << errString << Log::endl;
}
exit(1);
return errPfr;

View File

@ -76,7 +76,12 @@ void ServerLuaParser::sendModsPacket(ENetPeer* peer) const {
void ServerLuaParser::playerConnected(std::shared_ptr<ServerClient> client) {
auto players = core.get<sol::table>("players");
safe_function(core["__builtin"]["add_player"], ServerLuaPlayer(*client));
players.add(ServerLuaPlayer(*client));
sol::object player = players[players.size()];
safe_function(core["__builtin"]["trigger_event"], "new_player", player);
safe_function(core["__builtin"]["trigger_event"], "player_join", player);
}
void ServerLuaParser::playerDisconnected(std::shared_ptr<ServerClient> client) {
@ -148,33 +153,36 @@ sol::protected_function_result ServerLuaParser::errorCallback(sol::protected_fun
sol::error err = errPfr;
std::string errString = err.what();
std::string::size_type slash = errString.find_first_of("/");
assert(slash != std::string::npos);
try {
std::string::size_type slash = errString.find_first_of("/");
if (slash != std::string::npos) throw "npos";
std::string modString = errString.substr(0, slash);
std::string modString = errString.substr(0, slash);
std::string::size_type lineNumStart = errString.find(':', slash);
assert(lineNumStart != std::string::npos);
std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1);
assert(lineNumEnd != std::string::npos);
std::string::size_type lineNumStart = errString.find(':', slash);
if (lineNumStart != std::string::npos) throw "lineNumStart";
std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1);
if (lineNumStart != std::string::npos) throw "lineNumEnd";
std::string fileName = errString.substr(0, lineNumStart);
int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1));
std::string fileName = errString.substr(0, lineNumStart);
int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1));
for (auto& mod : handler.cGetMods()) {
if (mod.config.name == modString) {
for (auto& file : mod.files) {
if (file.path == fileName) {
std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl;
exit(1);
for (auto& mod : handler.cGetMods()) {
if (mod.config.name == modString) {
for (auto& file : mod.files) {
if (file.path == fileName) {
std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl;
break;
}
}
break;
}
break;
}
}
std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:"
<< std::endl << std::endl << errString << Log::endl;
catch (...) {
std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:"
<< std::endl << std::endl << errString << Log::endl;
}
exit(1);
return errPfr;

View File

@ -4,11 +4,11 @@
#pragma once
#include <sol2/sol.hpp>
#include <noise/module/modulebase.h>
#include <noise/module/add.h>
#include <noise/module/module.h>
#include "../Lua.h"
#include "../../util/Util.h"
#include "../../def/ClientGame.h"
#include "../../def/ServerGame.h"
@ -241,6 +241,7 @@ namespace RegisterBiomes {
modules.push_back(module);
return module;
}
throw std::runtime_error("Invalid noise module specified.");
}
static void registerBiomes(sol::table source, DefinitionAtlas& defs, BiomeAtlas& biomes) {

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../Lua.h"
#include "../Callback.h"
#include "../../def/ItemDef.h"
#include "../../def/ClientGame.h"

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../Lua.h"
#include "../../def/ClientGame.h"
#include "../../def/ServerGame.h"
#include "../../def/gen/BiomeDef.h"

View File

@ -4,8 +4,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "../Lua.h"
#include "../../def/ClientGame.h"
#include "../../def/ServerGame.h"
#include "../../def/gen/BiomeDef.h"

View File

@ -6,6 +6,7 @@
#include <string>
#include <vector>
#include <stdexcept>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
@ -14,7 +15,7 @@ public:
Deserializer(const std::string& data) : data(&data[0]), len(data.length()) {};
Deserializer(const char* start, size_t len) : data(start), len(len) {};
template <typename T> inline T read() { assert(false); };
template <typename T> inline T read() { throw std::runtime_error("Tried to append a non-serializable type"); };
template <typename T> inline Deserializer& read(T& ref) {
ref = read<T>();
return *this;

View File

@ -6,6 +6,7 @@
#include <string>
#include <vector>
#include <stdexcept>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
@ -16,7 +17,7 @@ class Serializer {
public:
std::string data {};
template <typename T> inline Serializer& append(const T& elem) { assert(false); };
template <typename T> inline Serializer& append(const T& elem) { throw std::runtime_error("Tried to append a non-serializable type"); };
Packet packet(PacketType p, bool reliable = true) {
Packet packet(p, reliable);

View File

@ -130,52 +130,38 @@ void Server::handlePlayerPacket(ServerClient& client, PacketView& p) {
if (block == DefinitionAtlas::AIR) {
auto def = defs.defs.blockFromId(worldBlock);
if (def.callbacks.count(Callback::BREAK)) {
defs.parser.safe_function(def.callbacks[Callback::BREAK],
defs.parser.luaVec(pos), ServerLuaPlayer(client));
}
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"],
"break", defs.parser.luaVec(pos), ServerLuaPlayer(client));
//TODO: stop casting to float vec3
if (def.callbacks.count(Callback::BREAK)) defs.parser.safe_function(def.callbacks[Callback::BREAK], glm::vec3(pos), ServerLuaPlayer(client));
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "break", glm::vec3(pos), ServerLuaPlayer(client));
}
else {
auto def = defs.defs.blockFromId(block);
if (def.callbacks.count(Callback::PLACE)) {
defs.parser.safe_function(def.callbacks[Callback::PLACE],
defs.parser.luaVec(pos), ServerLuaPlayer(client));
}
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"],
"place", defs.parser.luaVec(pos), ServerLuaPlayer(client));
if (def.callbacks.count(Callback::PLACE))
defs.parser.safe_function(def.callbacks[Callback::PLACE], pos, ServerLuaPlayer(client));
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "place", pos, ServerLuaPlayer(client));
}
world.setBlock(pos, block);
if (block == DefinitionAtlas::AIR) {
auto def = defs.defs.blockFromId(worldBlock);
if (def.callbacks.count(Callback::AFTER_BREAK)) {
defs.parser.safe_function(def.callbacks[Callback::AFTER_BREAK],
defs.parser.luaVec(pos), ServerLuaPlayer(client));
}
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"],
"after_break", defs.parser.luaVec(pos), ServerLuaPlayer(client));
if (def.callbacks.count(Callback::AFTER_BREAK))
defs.parser.safe_function(def.callbacks[Callback::AFTER_BREAK], pos, ServerLuaPlayer(client));
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "after_break", pos, ServerLuaPlayer(client));
}
else {
auto def = defs.defs.blockFromId(block);
if (def.callbacks.count(Callback::AFTER_PLACE)) {
defs.parser.safe_function(def.callbacks[Callback::AFTER_PLACE],
defs.parser.luaVec(pos), ServerLuaPlayer(client));
}
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"],
"after_place", defs.parser.luaVec(pos), ServerLuaPlayer(client));
if (def.callbacks.count(Callback::AFTER_PLACE))
defs.parser.safe_function(def.callbacks[Callback::AFTER_PLACE], pos, ServerLuaPlayer(client));
defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "after_place", pos, ServerLuaPlayer(client));
}
break;
}
case PacketType::BLOCK_INTERACT: {
glm::ivec3 pos = p.d.read<glm::ivec3>();
auto def = defs.defs.blockFromId(world.getBlock(pos));
if (def.callbacks.count(Callback::INTERACT)) {
defs.parser.safe_function(def.callbacks[Callback::INTERACT],
defs.parser.luaVec(pos), ServerLuaPlayer(client));
}
if (def.callbacks.count(Callback::INTERACT))
defs.parser.safe_function(def.callbacks[Callback::INTERACT], pos, ServerLuaPlayer(client));
break;
}
case PacketType::INV_WATCH: {

View File

@ -218,15 +218,11 @@ void ServerWorld::setBlock(glm::ivec3 pos, unsigned int block) {
if (block == DefinitionAtlas::AIR) {
auto def = game.defs.blockFromId(oldBlock);
if (def.callbacks.count(Callback::DESTRUCT)) {
def.callbacks[Callback::DESTRUCT](game.parser.luaVec(pos));
}
if (def.callbacks.count(Callback::DESTRUCT)) def.callbacks[Callback::DESTRUCT](pos);
}
else {
auto def = game.defs.blockFromId(block);
if (def.callbacks.count(Callback::CONSTRUCT)) {
def.callbacks[Callback::CONSTRUCT](game.parser.luaVec(pos));
}
if (def.callbacks.count(Callback::CONSTRUCT)) def.callbacks[Callback::CONSTRUCT](pos);
}
dimension.setBlock(pos, block);
@ -248,13 +244,11 @@ void ServerWorld::setBlock(glm::ivec3 pos, unsigned int block) {
if (block == DefinitionAtlas::AIR) {
auto def = game.defs.blockFromId(oldBlock);
if (def.callbacks.count(Callback::AFTER_DESTRUCT))
def.callbacks[Callback::AFTER_DESTRUCT](game.parser.luaVec(pos));
if (def.callbacks.count(Callback::AFTER_DESTRUCT)) def.callbacks[Callback::AFTER_DESTRUCT](pos);
}
else {
auto def = game.defs.blockFromId(block);
if (def.callbacks.count(Callback::AFTER_CONSTRUCT))
def.callbacks[Callback::AFTER_CONSTRUCT](game.parser.luaVec(pos));
if (def.callbacks.count(Callback::AFTER_CONSTRUCT)) def.callbacks[Callback::AFTER_CONSTRUCT](pos);
}
}

View File

@ -2,11 +2,11 @@ _G["crafting"] = {}
crafting.registered_recipes = {}
crafting.register_recipe = function(tbl)
function crafting.register_recipe(tbl)
table.insert(crafting.registered_recipes, tbl)
end
crafting.bind = function(craft_input, craft_output)
function crafting.bind(craft_input, craft_output)
local width = craft_input.width
local length = craft_input.length

View File

@ -73,7 +73,7 @@ zepha.register_entity("@aurailus:item_collection:dropped_item", {
end,
check_collect = function(self)
for _,p in pairs(zepha.players) do
if p.pos:dist(self.object.pos) < 2 then
if p.pos:distance(self.object.pos) < 2 then
self.object.pos = p.pos + V{0, 0.90, 0}
self.scooping = true

View File

@ -17,6 +17,4 @@ zepha.register_entity("zeus:default:test", {
end
})
if zepha.client then
local entity = zepha.add_entity(V{}, "zeus:default:test")
end
if zepha.client then entity = zepha.add_entity(V(), "zeus:default:test") end

View File

@ -28,11 +28,4 @@ zepha.register_keybind("zeus:default:double_jump_fly", {
if press - last_press < 0.25 then toggleFlying() end
last_press = press;
end
})
-- if (zepha.client) then
-- zepha.delay(function()
-- zepha.get_hit_impact(zepha.player, "zeus:default:stone")
-- return true
-- end, 1)
-- end
})

View File

@ -31,7 +31,7 @@
local shrub = zepha.create_structure({
origin = V{},
origin = V{1, 2, 3},
schematic = {
{{"zeus:default:tallgrass_4"}}
}

View File

@ -5,7 +5,7 @@
#pragma clang diagnostic pop
#include <catch2/catch.hpp>
#include <sol2/sol.hpp>
#include <sol/sol.hpp>
TEST_CASE("Catch2 Library", "[core]") {
REQUIRE(true);

View File

@ -3,7 +3,6 @@
//
#include <catch2/catch.hpp>
#include <iostream>
#include "../../src/world/chunk/Chunk.h"
#pragma clang diagnostic push

View File

@ -4,8 +4,8 @@
#include <catch2/catch.hpp>
#include <iostream>
#include "../../src/util/net/Serializer.h"
#include "../../src/util/net/Deserializer.h"
#include "../../src/net/Serializer.h"
#include "../../src/net/Deserializer.h"
TEST_CASE("Serialization", "[net]") {
Serializer s;