refactor lua scripting a bit
This commit is contained in:
@@ -335,7 +335,7 @@ static int l_audio_get_duration(lua::State* L) {
|
||||
static int l_audio_get_position(lua::State* L) {
|
||||
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
|
||||
if (speaker != nullptr) {
|
||||
return lua::pushvec3_stack(L, speaker->getPosition());
|
||||
return lua::pushvec_stack(L, speaker->getPosition());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -344,7 +344,7 @@ static int l_audio_get_position(lua::State* L) {
|
||||
static int l_audio_get_velocity(lua::State* L) {
|
||||
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
|
||||
if (speaker != nullptr) {
|
||||
return lua::pushvec3_stack(L, speaker->getVelocity());
|
||||
return lua::pushvec_stack(L, speaker->getVelocity());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ static int l_is_extended(lua::State* L) {
|
||||
|
||||
static int l_get_size(lua::State* L) {
|
||||
if (auto def = require_block(L)) {
|
||||
return lua::pushivec3_stack(L, def->size.x, def->size.y, def->size.z);
|
||||
return lua::pushivec_stack(L, glm::ivec3(def->size));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -76,7 +76,7 @@ static int l_seek_origin(lua::State* L) {
|
||||
auto z = lua::tointeger(L, 3);
|
||||
auto vox = level->chunks->get(x, y, z);
|
||||
auto& def = indices->blocks.require(vox->id);
|
||||
return lua::pushivec3_stack(
|
||||
return lua::pushivec_stack(
|
||||
L, level->chunks->seekOrigin({x, y, z}, def, vox->state)
|
||||
);
|
||||
}
|
||||
@@ -117,14 +117,14 @@ static int l_get_x(lua::State* L) {
|
||||
auto z = lua::tointeger(L, 3);
|
||||
auto vox = level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return lua::pushivec3_stack(L, 1, 0, 0);
|
||||
return lua::pushivec_stack(L, glm::ivec3(1, 0, 0));
|
||||
}
|
||||
auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||
const auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||
if (!def.rotatable) {
|
||||
return lua::pushivec3_stack(L, 1, 0, 0);
|
||||
return lua::pushivec_stack(L, glm::ivec3(1, 0, 0));
|
||||
} else {
|
||||
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
|
||||
return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
|
||||
return lua::pushivec_stack(L, rot.axisX);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,14 +134,14 @@ static int l_get_y(lua::State* L) {
|
||||
auto z = lua::tointeger(L, 3);
|
||||
auto vox = level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return lua::pushivec3_stack(L, 0, 1, 0);
|
||||
return lua::pushivec_stack(L, glm::ivec3(0, 1, 0));
|
||||
}
|
||||
auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||
const auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||
if (!def.rotatable) {
|
||||
return lua::pushivec3_stack(L, 0, 1, 0);
|
||||
return lua::pushivec_stack(L, glm::ivec3(0, 1, 0));
|
||||
} else {
|
||||
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
|
||||
return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
|
||||
return lua::pushivec_stack(L, rot.axisY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,14 +151,14 @@ static int l_get_z(lua::State* L) {
|
||||
auto z = lua::tointeger(L, 3);
|
||||
auto vox = level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return lua::pushivec3_stack(L, 0, 0, 1);
|
||||
return lua::pushivec_stack(L, glm::ivec3(0, 0, 1));
|
||||
}
|
||||
auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||
const auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||
if (!def.rotatable) {
|
||||
return lua::pushivec3_stack(L, 0, 0, 1);
|
||||
return lua::pushivec_stack(L, glm::ivec3(0, 0, 1));
|
||||
} else {
|
||||
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
|
||||
return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
|
||||
return lua::pushivec_stack(L, rot.axisZ);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,9 +49,9 @@ static int l_mul(lua::State* L) {
|
||||
switch (argc) {
|
||||
case 2: {
|
||||
if (len2 == 4) {
|
||||
return lua::pushvec4_stack(L, matrix1 * lua::tovec4(L, 2));
|
||||
return lua::pushvec(L, matrix1 * lua::tovec4(L, 2));
|
||||
} else if (len2 == 3) {
|
||||
return lua::pushvec3_stack(
|
||||
return lua::pushvec(
|
||||
L, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ inline std::shared_ptr<Player> get_player(lua::State* L, int idx) {
|
||||
|
||||
static int l_get_pos(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushvec3_stack(L, player->getPosition());
|
||||
return lua::pushvec_stack(L, player->getPosition());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -37,7 +37,7 @@ static int l_set_pos(lua::State* L) {
|
||||
static int l_get_vel(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
if (auto hitbox = player->getHitbox()) {
|
||||
return lua::pushvec3_stack(L, hitbox->velocity);
|
||||
return lua::pushvec_stack(L, hitbox->velocity);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -59,7 +59,7 @@ static int l_set_vel(lua::State* L) {
|
||||
|
||||
static int l_get_rot(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushvec3_stack(L, player->cam);
|
||||
return lua::pushvec_stack(L, player->cam);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -133,7 +133,7 @@ static int l_get_selected_block(lua::State* L) {
|
||||
if (player->selection.vox.id == BLOCK_VOID) {
|
||||
return 0;
|
||||
}
|
||||
return lua::pushivec3_stack(L, player->selection.position);
|
||||
return lua::pushivec_stack(L, player->selection.position);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -149,15 +149,13 @@ static int l_get_selected_entity(lua::State* L) {
|
||||
|
||||
static int l_get_spawnpoint(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushvec3_stack(L, player->getSpawnPoint());
|
||||
return lua::pushvec_stack(L, player->getSpawnPoint());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_spawnpoint(lua::State* L) {
|
||||
auto player = get_player(L, 1);
|
||||
|
||||
if (player) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
auto x = lua::tonumber(L, 2);
|
||||
auto y = lua::tonumber(L, 3);
|
||||
auto z = lua::tonumber(L, 4);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "lua_commons.hpp"
|
||||
#include "lua_wrapper.hpp"
|
||||
#include "lua_custom_types.hpp"
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
@@ -18,68 +18,6 @@ namespace lua {
|
||||
|
||||
std::string env_name(int env);
|
||||
|
||||
template <lua_CFunction func>
|
||||
int wrap(lua_State* L) {
|
||||
int result = 0;
|
||||
try {
|
||||
result = func(L);
|
||||
}
|
||||
// transform exception with description into lua_error
|
||||
catch (std::exception& e) {
|
||||
luaL_error(L, e.what());
|
||||
}
|
||||
// Rethrow any other exception (lua error for example)
|
||||
catch (...) {
|
||||
throw;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void pop(lua::State* L, int n = 1) {
|
||||
lua_pop(L, n);
|
||||
}
|
||||
inline void insert(lua::State* L, int idx) {
|
||||
lua_insert(L, idx);
|
||||
}
|
||||
inline void remove(lua::State* L, int idx) {
|
||||
lua_remove(L, idx);
|
||||
}
|
||||
inline int gettop(lua::State* L) {
|
||||
return lua_gettop(L);
|
||||
}
|
||||
inline size_t objlen(lua::State* L, int idx) {
|
||||
return lua_objlen(L, idx);
|
||||
}
|
||||
inline int next(lua::State* L, int idx) {
|
||||
return lua_next(L, idx);
|
||||
}
|
||||
inline int type(lua::State* L, int idx) {
|
||||
return lua_type(L, idx);
|
||||
}
|
||||
inline const char* type_name(lua::State* L, int idx) {
|
||||
return lua_typename(L, idx);
|
||||
}
|
||||
inline int rawget(lua::State* L, int idx = -2) {
|
||||
lua_rawget(L, idx);
|
||||
return 1;
|
||||
}
|
||||
inline int rawgeti(lua::State* L, int n, int idx = -1) {
|
||||
lua_rawgeti(L, idx, n);
|
||||
return 1;
|
||||
}
|
||||
inline void rawseti(lua::State* L, int n, int idx = -2) {
|
||||
lua_rawseti(L, idx, n);
|
||||
}
|
||||
|
||||
inline int createtable(lua::State* L, int narr, int nrec) {
|
||||
lua_createtable(L, narr, nrec);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline bool isnil(lua::State* L, int idx) {
|
||||
return lua_isnil(L, idx);
|
||||
}
|
||||
|
||||
inline bool getglobal(lua::State* L, const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (isnil(L, -1)) {
|
||||
@@ -107,25 +45,8 @@ namespace lua {
|
||||
return true;
|
||||
}
|
||||
|
||||
// function wrappers with number of pushed values as return value
|
||||
|
||||
inline int pushnil(lua::State* L) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushinteger(lua::State* L, lua::Integer x) {
|
||||
lua_pushinteger(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushnumber(lua::State* L, lua::Number x) {
|
||||
lua_pushnumber(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <int n>
|
||||
inline int pushvec(lua::State* L, glm::vec<n, float> vec) {
|
||||
inline int pushvec(lua::State* L, const glm::vec<n, float>& vec) {
|
||||
createtable(L, n, 0);
|
||||
for (int i = 0; i < n; i++) {
|
||||
pushnumber(L, vec[i]);
|
||||
@@ -135,7 +56,7 @@ namespace lua {
|
||||
}
|
||||
|
||||
template <int n>
|
||||
inline int pushivec(lua::State* L, glm::vec<n, int> vec) {
|
||||
inline int pushivec(lua::State* L, const glm::vec<n, int>& vec) {
|
||||
createtable(L, n, 0);
|
||||
for (int i = 0; i < n; i++) {
|
||||
pushinteger(L, vec[i]);
|
||||
@@ -144,34 +65,20 @@ namespace lua {
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushivec3_stack(
|
||||
lua::State* L, lua::Integer x, lua::Integer y, lua::Integer z
|
||||
) {
|
||||
pushinteger(L, x);
|
||||
pushinteger(L, y);
|
||||
pushinteger(L, z);
|
||||
return 3;
|
||||
template<int n>
|
||||
inline int pushvec_stack(lua::State* L, const glm::vec<n, float>& vec) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
pushnumber(L, vec[i]);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
inline int pushivec3_stack(lua::State* L, glm::ivec3 vec) {
|
||||
pushinteger(L, vec.x);
|
||||
pushinteger(L, vec.y);
|
||||
pushinteger(L, vec.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec3_stack(lua::State* L, glm::vec3 vec) {
|
||||
pushnumber(L, vec.x);
|
||||
pushnumber(L, vec.y);
|
||||
pushnumber(L, vec.z);
|
||||
return 3;
|
||||
}
|
||||
inline int pushvec4_stack(lua::State* L, glm::vec4 vec) {
|
||||
pushnumber(L, vec.x);
|
||||
pushnumber(L, vec.y);
|
||||
pushnumber(L, vec.z);
|
||||
pushnumber(L, vec.w);
|
||||
return 4;
|
||||
template<int n>
|
||||
inline int pushivec_stack(lua::State* L, const glm::vec<n, int>& vec) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
pushinteger(L, vec[i]);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
inline void setmetatable(lua::State* L, int idx = -2) {
|
||||
@@ -637,4 +544,40 @@ namespace lua {
|
||||
luaL_setfuncs(L, libfuncs, 0);
|
||||
setglobal(L, name);
|
||||
}
|
||||
|
||||
inline int requirefield(lua::State* L, const std::string& name, int idx = -1) {
|
||||
if (getfield(L, name, idx)) {
|
||||
return 1;
|
||||
}
|
||||
throw std::runtime_error("object has no member '"+name+"'");
|
||||
}
|
||||
|
||||
inline const char* require_string_field(
|
||||
lua::State* L, const std::string& name, int idx=-1
|
||||
) {
|
||||
requirefield(L, name, idx);
|
||||
auto value = require_string(L, -1);
|
||||
lua::pop(L);
|
||||
return value;
|
||||
}
|
||||
|
||||
inline Integer require_integer_field(
|
||||
lua::State* L, const std::string& name, int idx=-1
|
||||
) {
|
||||
requirefield(L, name, idx);
|
||||
auto value = tointeger(L, -1);
|
||||
lua::pop(L);
|
||||
return value;
|
||||
}
|
||||
|
||||
inline bool get_boolean_field(
|
||||
lua::State* L, const std::string& name, bool def, int idx=-1
|
||||
) {
|
||||
if (getfield(L, name, idx)) {
|
||||
bool value = toboolean(L, -1);
|
||||
pop(L);
|
||||
return value;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
#include "lua_commons.hpp"
|
||||
|
||||
namespace lua {
|
||||
template <lua_CFunction func>
|
||||
int wrap(lua_State* L) {
|
||||
int result = 0;
|
||||
try {
|
||||
result = func(L);
|
||||
}
|
||||
// transform exception with description into lua_error
|
||||
catch (std::exception& e) {
|
||||
luaL_error(L, e.what());
|
||||
}
|
||||
// Rethrow any other exception (lua error for example)
|
||||
catch (...) {
|
||||
throw;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void pop(lua::State* L, int n = 1) {
|
||||
lua_pop(L, n);
|
||||
}
|
||||
inline void insert(lua::State* L, int idx) {
|
||||
lua_insert(L, idx);
|
||||
}
|
||||
inline void remove(lua::State* L, int idx) {
|
||||
lua_remove(L, idx);
|
||||
}
|
||||
inline int gettop(lua::State* L) {
|
||||
return lua_gettop(L);
|
||||
}
|
||||
inline size_t objlen(lua::State* L, int idx) {
|
||||
return lua_objlen(L, idx);
|
||||
}
|
||||
inline int next(lua::State* L, int idx) {
|
||||
return lua_next(L, idx);
|
||||
}
|
||||
inline int type(lua::State* L, int idx) {
|
||||
return lua_type(L, idx);
|
||||
}
|
||||
inline const char* type_name(lua::State* L, int idx) {
|
||||
return lua_typename(L, idx);
|
||||
}
|
||||
inline int rawget(lua::State* L, int idx = -2) {
|
||||
lua_rawget(L, idx);
|
||||
return 1;
|
||||
}
|
||||
inline int rawgeti(lua::State* L, int n, int idx = -1) {
|
||||
lua_rawgeti(L, idx, n);
|
||||
return 1;
|
||||
}
|
||||
inline void rawseti(lua::State* L, int n, int idx = -2) {
|
||||
lua_rawseti(L, idx, n);
|
||||
}
|
||||
|
||||
inline int createtable(lua::State* L, int narr, int nrec) {
|
||||
lua_createtable(L, narr, nrec);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline bool isnil(lua::State* L, int idx) {
|
||||
return lua_isnil(L, idx);
|
||||
}
|
||||
|
||||
// function wrappers with number of pushed values as return value
|
||||
|
||||
inline int pushnil(lua::State* L) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushinteger(lua::State* L, lua::Integer x) {
|
||||
lua_pushinteger(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushnumber(lua::State* L, lua::Number x) {
|
||||
lua_pushnumber(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user