block ui test
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
#include "LuaState.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "lua_util.h"
|
||||
#include "api_lua.h"
|
||||
#include "libgui.h"
|
||||
#include "../../../util/stringutil.h"
|
||||
|
||||
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
|
||||
}
|
||||
|
||||
lua::LuaState::LuaState() {
|
||||
L = luaL_newstate();
|
||||
if (L == nullptr) {
|
||||
throw lua::luaerror("could not to initialize Lua");
|
||||
}
|
||||
// Allowed standard libraries
|
||||
luaopen_base(L);
|
||||
luaopen_math(L);
|
||||
luaopen_string(L);
|
||||
luaopen_table(L);
|
||||
luaopen_debug(L);
|
||||
luaopen_jit(L);
|
||||
luaopen_bit(L);
|
||||
|
||||
std::cout << LUA_VERSION << std::endl;
|
||||
std::cout << LUAJIT_VERSION << std::endl;
|
||||
|
||||
createFuncs();
|
||||
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
setglobal(envName(0));
|
||||
}
|
||||
|
||||
const std::string lua::LuaState::envName(int env) const {
|
||||
return "_ENV"+util::mangleid(env);
|
||||
}
|
||||
|
||||
lua::LuaState::~LuaState() {
|
||||
lua_close(L);
|
||||
}
|
||||
|
||||
void lua::LuaState::logError(const std::string& text) {
|
||||
std::cerr << text << std::endl;
|
||||
}
|
||||
|
||||
void lua::LuaState::addfunc(const std::string& name, lua_CFunction func) {
|
||||
lua_pushcfunction(L, func);
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
|
||||
bool lua::LuaState::getglobal(const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (lua_isnil(L, lua_gettop(L))) {
|
||||
lua_pop(L, lua_gettop(L));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lua::LuaState::hasglobal(const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (lua_isnil(L, lua_gettop(L))) {
|
||||
lua_pop(L, lua_gettop(L));
|
||||
return false;
|
||||
}
|
||||
lua_pop(L, lua_gettop(L));
|
||||
return true;
|
||||
}
|
||||
|
||||
void lua::LuaState::setglobal(const std::string& name) {
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
|
||||
bool lua::LuaState::rename(const std::string& from, const std::string& to) {
|
||||
const char* src = from.c_str();
|
||||
lua_getglobal(L, src);
|
||||
if (lua_isnil(L, lua_gettop(L))) {
|
||||
lua_pop(L, lua_gettop(L));
|
||||
return false;
|
||||
}
|
||||
lua_setglobal(L, to.c_str());
|
||||
|
||||
// remove previous
|
||||
lua_pushnil(L);
|
||||
lua_setglobal(L, src);
|
||||
return true;
|
||||
}
|
||||
|
||||
void lua::LuaState::remove(const std::string& name) {
|
||||
lua_pushnil(L);
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
|
||||
void lua::LuaState::createFuncs() {
|
||||
openlib("pack", packlib, 0);
|
||||
openlib("world", worldlib, 0);
|
||||
openlib("player", playerlib, 0);
|
||||
openlib("inventory", inventorylib, 0);
|
||||
openlib("item", itemlib, 0);
|
||||
openlib("time", timelib, 0);
|
||||
openlib("file", filelib, 0);
|
||||
openlib("gui", guilib, 0);
|
||||
|
||||
addfunc("print", l_print);
|
||||
|
||||
addfunc("block_index", l_block_index);
|
||||
addfunc("block_name", l_block_name);
|
||||
addfunc("blocks_count", l_blocks_count);
|
||||
addfunc("is_solid_at", l_is_solid_at);
|
||||
addfunc("is_replaceable_at", l_is_replaceable_at);
|
||||
addfunc("set_block", l_set_block);
|
||||
addfunc("get_block", l_get_block);
|
||||
addfunc("get_block_X", l_get_block_x);
|
||||
addfunc("get_block_Y", l_get_block_y);
|
||||
addfunc("get_block_Z", l_get_block_z);
|
||||
addfunc("get_block_states", l_get_block_states);
|
||||
addfunc("get_block_user_bits", l_get_block_user_bits);
|
||||
addfunc("set_block_user_bits", l_set_block_user_bits);
|
||||
}
|
||||
|
||||
void lua::LuaState::loadbuffer(int env, const std::string& src, const std::string& file) {
|
||||
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) {
|
||||
throw lua::luaerror(lua_tostring(L, -1));
|
||||
}
|
||||
if (env && getglobal(envName(env))) {
|
||||
lua_setfenv(L, -2);
|
||||
}
|
||||
}
|
||||
|
||||
int lua::LuaState::call(int argc) {
|
||||
if (lua_pcall(L, argc, LUA_MULTRET, 0)) {
|
||||
throw lua::luaerror(lua_tostring(L, -1));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::callNoThrow(int argc) {
|
||||
if (lua_pcall(L, argc, LUA_MULTRET, 0)) {
|
||||
logError(lua_tostring(L, -1));
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::eval(int env, const std::string& src, const std::string& file) {
|
||||
auto srcText = "return ("+src+")";
|
||||
loadbuffer(env, srcText, file);
|
||||
return call(0);
|
||||
}
|
||||
|
||||
int lua::LuaState::execute(int env, const std::string& src, const std::string& file) {
|
||||
loadbuffer(env, src, file);
|
||||
return callNoThrow(0);
|
||||
}
|
||||
|
||||
int lua::LuaState::gettop() const {
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
int lua::LuaState::pushinteger(luaint x) {
|
||||
lua_pushinteger(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushnumber(luanumber x) {
|
||||
lua_pushnumber(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushivec3(luaint x, luaint y, luaint z) {
|
||||
lua::pushivec3(L, x, y, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushstring(const std::string& str) {
|
||||
lua_pushstring(L, str.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushenv(int env) {
|
||||
if (getglobal(envName(env))) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushvalue(int idx) {
|
||||
lua_pushvalue(L, idx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushglobals() {
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void lua::LuaState::pop(int n) {
|
||||
lua_pop(L, n);
|
||||
}
|
||||
|
||||
int lua::LuaState::pushnil() {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool lua::LuaState::getfield(const std::string& name) {
|
||||
lua_getfield(L, -1, name.c_str());
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, -1);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void lua::LuaState::setfield(const std::string& name, int idx) {
|
||||
lua_setfield(L, idx, name.c_str());
|
||||
}
|
||||
|
||||
bool lua::LuaState::toboolean(int idx) {
|
||||
return lua_toboolean(L, idx);
|
||||
}
|
||||
|
||||
lua::luaint lua::LuaState::tointeger(int idx) {
|
||||
return lua_tointeger(L, idx);
|
||||
}
|
||||
|
||||
lua::luanumber lua::LuaState::tonumber(int idx) {
|
||||
return lua_tonumber(L, idx);
|
||||
}
|
||||
|
||||
void lua::LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs, int nup) {
|
||||
lua_newtable(L);
|
||||
luaL_setfuncs(L, libfuncs, nup);
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
|
||||
const std::string lua::LuaState::storeAnonymous() {
|
||||
auto funcId = uintptr_t(lua_topointer(L, lua_gettop(L)));
|
||||
auto funcName = "F$"+util::mangleid(funcId);
|
||||
lua_setglobal(L, funcName.c_str());
|
||||
return funcName;
|
||||
}
|
||||
|
||||
int lua::LuaState::createEnvironment(int parent) {
|
||||
int id = nextEnvironment++;
|
||||
|
||||
// local env = {}
|
||||
lua_createtable(L, 0, 1);
|
||||
|
||||
// setmetatable(env, {__index=_G})
|
||||
lua_createtable(L, 0, 1);
|
||||
if (parent == 0 || true) {
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
} else {
|
||||
if (pushenv(parent) == 0) {
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
}
|
||||
}
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
// envname = env
|
||||
setglobal(envName(id));
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
void lua::LuaState::removeEnvironment(int id) {
|
||||
lua_pushnil(L);
|
||||
setglobal(envName(id));
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#ifndef LOGIC_SCRIPTING_LUA_STATE_H_
|
||||
#define LOGIC_SCRIPTING_LUA_STATE_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifndef LUAJIT_VERSION
|
||||
#error LuaJIT required
|
||||
#endif
|
||||
|
||||
namespace lua {
|
||||
using luaint = lua_Integer;
|
||||
using luanumber = lua_Number;
|
||||
|
||||
class luaerror : public std::runtime_error {
|
||||
public:
|
||||
luaerror(const std::string& message);
|
||||
};
|
||||
|
||||
class LuaState {
|
||||
lua_State* L;
|
||||
int nextEnvironment = 1;
|
||||
|
||||
void logError(const std::string& text);
|
||||
void initEnvironment();
|
||||
public:
|
||||
LuaState();
|
||||
~LuaState();
|
||||
|
||||
const std::string envName(int env) const;
|
||||
void loadbuffer(int env, const std::string& src, const std::string& file);
|
||||
int gettop() const;
|
||||
int pushivec3(luaint x, luaint y, luaint z);
|
||||
int pushinteger(luaint x);
|
||||
int pushnumber(luanumber x);
|
||||
int pushstring(const std::string& str);
|
||||
int pushenv(int env);
|
||||
int pushvalue(int idx);
|
||||
int pushnil();
|
||||
int pushglobals();
|
||||
void pop(int n=1);
|
||||
bool getfield(const std::string& name);
|
||||
void setfield(const std::string& name, int idx=-2);
|
||||
bool toboolean(int idx);
|
||||
luaint tointeger(int idx);
|
||||
luanumber tonumber(int idx);
|
||||
int call(int argc);
|
||||
int callNoThrow(int argc);
|
||||
int execute(int env, const std::string& src, const std::string& file="<string>");
|
||||
int eval(int env, const std::string& src, const std::string& file="<eval>");
|
||||
void openlib(const std::string& name, const luaL_Reg* libfuncs, int nup);
|
||||
void addfunc(const std::string& name, lua_CFunction func);
|
||||
bool getglobal(const std::string& name);
|
||||
void setglobal(const std::string& name);
|
||||
bool hasglobal(const std::string& name);
|
||||
bool rename(const std::string& from, const std::string& to);
|
||||
void remove(const std::string& name);;
|
||||
void createFuncs();
|
||||
int createEnvironment(int parent);
|
||||
void removeEnvironment(int id);
|
||||
const std::string storeAnonymous();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_LUA_STATE_H_
|
||||
@@ -0,0 +1,494 @@
|
||||
#include "api_lua.h"
|
||||
|
||||
#include "../scripting.h"
|
||||
#include "lua_util.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include "../../../files/files.h"
|
||||
#include "../../../physics/Hitbox.h"
|
||||
#include "../../../objects/Player.h"
|
||||
#include "../../../world/Level.h"
|
||||
#include "../../../world/World.h"
|
||||
#include "../../../content/Content.h"
|
||||
#include "../../../voxels/Block.h"
|
||||
#include "../../../voxels/Chunks.h"
|
||||
#include "../../../voxels/voxel.h"
|
||||
#include "../../../items/ItemDef.h"
|
||||
#include "../../../items/ItemStack.h"
|
||||
#include "../../../items/Inventory.h"
|
||||
#include "../../../items/Inventories.h"
|
||||
#include "../../../lighting/Lighting.h"
|
||||
#include "../../../logic/BlocksController.h"
|
||||
#include "../../../window/Window.h"
|
||||
#include "../../../engine.h"
|
||||
|
||||
fs::path resolve_path(lua_State* L, const std::string& path) {
|
||||
try {
|
||||
return scripting::engine->getPaths()->resolve(path);
|
||||
} catch (const files_access_error& err) {
|
||||
luaL_error(L, err.what());
|
||||
abort(); // unreachable
|
||||
}
|
||||
}
|
||||
|
||||
/* == file library == */
|
||||
int l_file_resolve(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
lua_pushstring(L, path.u8string().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_file_read(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
if (fs::is_regular_file(path)) {
|
||||
lua_pushstring(L, files::read_string(path).c_str());
|
||||
return 1;
|
||||
}
|
||||
return luaL_error(L, "file does not exists '%s'", path.u8string().c_str());
|
||||
}
|
||||
|
||||
int l_file_write(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
const char* text = lua_tostring(L, 2);
|
||||
files::write_string(path, text);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_file_exists(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
lua_pushboolean(L, fs::exists(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_file_isfile(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
lua_pushboolean(L, fs::is_regular_file(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_file_isdir(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
lua_pushboolean(L, fs::is_directory(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_file_length(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
if (fs::exists(path)){
|
||||
lua_pushinteger(L, fs::file_size(path));
|
||||
} else {
|
||||
lua_pushinteger(L, -1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_file_mkdir(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
lua_pushboolean(L, fs::create_directory(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == time library == */
|
||||
int l_time_uptime(lua_State* L) {
|
||||
lua_pushnumber(L, Window::time());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_time_delta(lua_State* L) {
|
||||
lua_pushnumber(L, scripting::engine->getDelta());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == pack library == */
|
||||
int l_pack_get_folder(lua_State* L) {
|
||||
std::string packName = lua_tostring(L, 1);
|
||||
if (packName == "core") {
|
||||
auto folder = scripting::engine->getPaths()
|
||||
->getResources().u8string()+"/";
|
||||
lua_pushstring(L, folder.c_str());
|
||||
return 1;
|
||||
}
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
if (pack.id == packName) {
|
||||
lua_pushstring(L, (pack.folder.u8string()+"/").c_str());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
lua_pushstring(L, "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == world library == */
|
||||
int l_world_get_total_time(lua_State* L) {
|
||||
lua_pushnumber(L, scripting::level->world->totalTime);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_world_get_day_time(lua_State* L) {
|
||||
lua_pushnumber(L, scripting::level->world->daytime);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_world_set_day_time(lua_State* L) {
|
||||
double value = lua_tonumber(L, 1);
|
||||
scripting::level->world->daytime = fmod(value, 1.0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_world_get_seed(lua_State* L) {
|
||||
lua_pushinteger(L, scripting::level->world->getSeed());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == player library ==*/
|
||||
int l_player_get_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
glm::vec3 pos = scripting::level->player->hitbox->position;
|
||||
lua_pushnumber(L, pos.x);
|
||||
lua_pushnumber(L, pos.y);
|
||||
lua_pushnumber(L, pos.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int l_player_get_rot(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
glm::vec2 rot = scripting::level->player->cam;
|
||||
lua_pushnumber(L, rot.x);
|
||||
lua_pushnumber(L, rot.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int l_player_set_rot(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
lua::luanumber x = lua_tonumber(L, 2);
|
||||
lua::luanumber y = lua_tonumber(L, 3);
|
||||
glm::vec2& cam = scripting::level->player->cam;
|
||||
cam.x = x;
|
||||
cam.y = y;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_player_set_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
lua::luanumber x = lua_tonumber(L, 2);
|
||||
lua::luanumber y = lua_tonumber(L, 3);
|
||||
lua::luanumber z = lua_tonumber(L, 4);
|
||||
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_player_get_inv(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
Player* player = scripting::level->player;
|
||||
lua_pushinteger(L, player->getInventory()->getId());
|
||||
lua_pushinteger(L, player->getChosenSlot());
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void validate_itemid(lua_State* L, itemid_t id) {
|
||||
if (id >= scripting::indices->countItemDefs()) {
|
||||
luaL_error(L, "invalid item id");
|
||||
}
|
||||
}
|
||||
|
||||
/* == inventory library == */
|
||||
int l_inventory_get(lua_State* L) {
|
||||
lua::luaint invid = lua_tointeger(L, 1);
|
||||
lua::luaint slotid = lua_tointeger(L, 2);
|
||||
auto inv = scripting::level->inventories->get(invid);
|
||||
if (inv == nullptr) {
|
||||
luaL_error(L, "inventory does not exists in runtime: %d", invid);
|
||||
}
|
||||
if (slotid < 0 || uint64_t(slotid) >= inv->size()) {
|
||||
luaL_error(L, "slot index is out of range [0, inventory.size(invid)]");
|
||||
}
|
||||
ItemStack& item = inv->getSlot(slotid);
|
||||
lua_pushinteger(L, item.getItemId());
|
||||
lua_pushinteger(L, item.getCount());
|
||||
return 2;
|
||||
}
|
||||
|
||||
int l_inventory_set(lua_State* L) {
|
||||
lua::luaint invid = lua_tointeger(L, 1);
|
||||
lua::luaint slotid = lua_tointeger(L, 2);
|
||||
lua::luaint itemid = lua_tointeger(L, 3);
|
||||
lua::luaint count = lua_tointeger(L, 4);
|
||||
validate_itemid(L, itemid);
|
||||
|
||||
auto inv = scripting::level->inventories->get(invid);
|
||||
if (inv == nullptr) {
|
||||
luaL_error(L, "inventory does not exists in runtime: %d", invid);
|
||||
}
|
||||
if (slotid < 0 || uint64_t(slotid) >= inv->size()) {
|
||||
luaL_error(L, "slot index is out of range [0, inventory.size(invid)]");
|
||||
}
|
||||
ItemStack& item = inv->getSlot(slotid);
|
||||
item.set(ItemStack(itemid, count));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_inventory_size(lua_State* L) {
|
||||
lua::luaint invid = lua_tointeger(L, 1);
|
||||
auto inv = scripting::level->inventories->get(invid);
|
||||
if (inv == nullptr) {
|
||||
luaL_error(L, "inventory does not exists in runtime: %d", invid);
|
||||
}
|
||||
lua_pushinteger(L, inv->size());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_inventory_add(lua_State* L) {
|
||||
lua::luaint invid = lua_tointeger(L, 1);
|
||||
lua::luaint itemid = lua_tointeger(L, 2);
|
||||
lua::luaint count = lua_tointeger(L, 3);
|
||||
validate_itemid(L, itemid);
|
||||
|
||||
auto inv = scripting::level->inventories->get(invid);
|
||||
if (inv == nullptr) {
|
||||
luaL_error(L, "inventory does not exists in runtime: %d", invid);
|
||||
}
|
||||
ItemStack item(itemid, count);
|
||||
inv->move(item, scripting::indices);
|
||||
lua_pushinteger(L, item.getCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_inventory_get_block(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
int64_t id = scripting::blocks->createBlockInventory(x, y, z);
|
||||
lua_pushinteger(L, id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == item library == */
|
||||
int l_item_name(lua_State* L) {
|
||||
auto indices = scripting::content->getIndices();
|
||||
lua::luaint id = lua_tointeger(L, 1);
|
||||
if (id < 0 || size_t(id) >= indices->countItemDefs()) {
|
||||
return 0;
|
||||
}
|
||||
auto def = indices->getItemDef(id);
|
||||
lua_pushstring(L, def->name.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_item_index(lua_State* L) {
|
||||
auto name = lua_tostring(L, 1);
|
||||
lua_pushinteger(L, scripting::content->requireItem(name).rt.id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_item_stack_size(lua_State* L) {
|
||||
auto indices = scripting::content->getIndices();
|
||||
lua::luaint id = lua_tointeger(L, 1);
|
||||
if (id < 0 || size_t(id) >= indices->countItemDefs()) {
|
||||
return 0;
|
||||
}
|
||||
auto def = indices->getItemDef(id);
|
||||
lua_pushinteger(L, def->stackSize);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_item_defs_count(lua_State* L) {
|
||||
lua_pushinteger(L, scripting::indices->countItemDefs());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == blocks-related functions == */
|
||||
int l_block_name(lua_State* L) {
|
||||
auto indices = scripting::content->getIndices();
|
||||
lua::luaint id = lua_tointeger(L, 1);
|
||||
if (id < 0 || size_t(id) >= indices->countBlockDefs()) {
|
||||
return 0;
|
||||
}
|
||||
auto def = indices->getBlockDef(id);
|
||||
lua_pushstring(L, def->name.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_is_solid_at(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
|
||||
lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_blocks_count(lua_State* L) {
|
||||
lua_pushinteger(L, scripting::indices->countBlockDefs());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_block_index(lua_State* L) {
|
||||
auto name = lua_tostring(L, 1);
|
||||
lua_pushinteger(L, scripting::content->requireBlock(name).rt.id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_set_block(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
lua::luaint id = lua_tointeger(L, 4);
|
||||
lua::luaint states = lua_tointeger(L, 5);
|
||||
bool noupdate = lua_toboolean(L, 6);
|
||||
if (id < 0 || size_t(id) >= scripting::indices->countBlockDefs()) {
|
||||
return 0;
|
||||
}
|
||||
scripting::level->chunks->set(x, y, z, id, states);
|
||||
scripting::level->lighting->onBlockSet(x,y,z, id);
|
||||
if (!noupdate)
|
||||
scripting::blocks->updateSides(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_get_block(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
int id = vox == nullptr ? -1 : vox->id;
|
||||
lua_pushinteger(L, id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_get_block_x(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return lua::pushivec3(L, 1, 0, 0);
|
||||
}
|
||||
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (!def->rotatable) {
|
||||
return lua::pushivec3(L, 1, 0, 0);
|
||||
} else {
|
||||
const CoordSystem& rot = def->rotations.variants[vox->rotation()];
|
||||
return lua::pushivec3(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
|
||||
}
|
||||
}
|
||||
|
||||
int l_get_block_y(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return lua::pushivec3(L, 0, 1, 0);
|
||||
}
|
||||
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (!def->rotatable) {
|
||||
return lua::pushivec3(L, 0, 1, 0);
|
||||
} else {
|
||||
const CoordSystem& rot = def->rotations.variants[vox->rotation()];
|
||||
return lua::pushivec3(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
|
||||
}
|
||||
}
|
||||
|
||||
int l_get_block_z(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return lua::pushivec3(L, 0, 0, 1);
|
||||
}
|
||||
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (!def->rotatable) {
|
||||
return lua::pushivec3(L, 0, 0, 1);
|
||||
} else {
|
||||
const CoordSystem& rot = def->rotations.variants[vox->rotation()];
|
||||
return lua::pushivec3(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
|
||||
}
|
||||
}
|
||||
|
||||
int l_get_block_states(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
int states = vox == nullptr ? 0 : vox->states;
|
||||
lua_pushinteger(L, states);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_get_block_user_bits(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
lua::luaint offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET;
|
||||
lua::luaint bits = lua_tointeger(L, 5);
|
||||
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
lua_pushinteger(L, 0);
|
||||
return 1;
|
||||
}
|
||||
uint mask = ((1 << bits) - 1) << offset;
|
||||
uint data = (vox->states & mask) >> offset;
|
||||
lua_pushinteger(L, data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_set_block_user_bits(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
lua::luaint offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET;
|
||||
lua::luaint bits = lua_tointeger(L, 5);
|
||||
|
||||
uint mask = ((1 << bits) - 1) << offset;
|
||||
lua::luaint value = (lua_tointeger(L, 6) << offset) & mask;
|
||||
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
vox->states = (vox->states & (~mask)) | value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_is_replaceable_at(lua_State* L) {
|
||||
int x = lua_tointeger(L, 1);
|
||||
int y = lua_tointeger(L, 2);
|
||||
int z = lua_tointeger(L, 3);
|
||||
|
||||
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_print(lua_State* L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_getglobal(L, "tostring");
|
||||
for (int i=1; i<=n; i++) {
|
||||
lua_pushvalue(L, -1); /* function to be called */
|
||||
lua_pushvalue(L, i); /* value to print */
|
||||
lua_call(L, 1, 1);
|
||||
const char* s = lua_tostring(L, -1); /* get result */
|
||||
if (s == NULL)
|
||||
return luaL_error(L, LUA_QL("tostring") " must return a string to "
|
||||
LUA_QL("print"));
|
||||
if (i > 1)
|
||||
std::cout << "\t";
|
||||
std::cout << s;
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
#ifndef LOGIC_SCRIPTING_API_LUA_H_
|
||||
#define LOGIC_SCRIPTING_API_LUA_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
/* == file library == */
|
||||
extern int l_file_resolve(lua_State* L);
|
||||
extern int l_file_read(lua_State* L);
|
||||
extern int l_file_write(lua_State* L);
|
||||
extern int l_file_exists(lua_State* L);
|
||||
extern int l_file_isfile(lua_State* L);
|
||||
extern int l_file_isdir(lua_State* L);
|
||||
extern int l_file_length(lua_State* L);
|
||||
extern int l_file_mkdir(lua_State* L);
|
||||
|
||||
static const luaL_Reg filelib [] = {
|
||||
{"resolve", l_file_resolve},
|
||||
{"read", l_file_read},
|
||||
{"write", l_file_write},
|
||||
{"exists", l_file_exists},
|
||||
{"isfile", l_file_isfile},
|
||||
{"isdir", l_file_isdir},
|
||||
{"length", l_file_length},
|
||||
{"mkdir", l_file_mkdir},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == time library == */
|
||||
extern int l_time_uptime(lua_State* L);
|
||||
extern int l_time_delta(lua_State* L);
|
||||
|
||||
static const luaL_Reg timelib [] = {
|
||||
{"uptime", l_time_uptime},
|
||||
{"delta", l_time_delta},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == pack library == */
|
||||
extern int l_pack_get_folder(lua_State* L);
|
||||
|
||||
static const luaL_Reg packlib [] = {
|
||||
{"get_folder", l_pack_get_folder},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == world library == */
|
||||
extern int l_world_get_total_time(lua_State* L);
|
||||
extern int l_world_get_day_time(lua_State* L);
|
||||
extern int l_world_set_day_time(lua_State* L);
|
||||
extern int l_world_get_seed(lua_State* L);
|
||||
|
||||
static const luaL_Reg worldlib [] = {
|
||||
{"get_total_time", l_world_get_total_time},
|
||||
{"get_day_time", l_world_get_day_time},
|
||||
{"set_day_time", l_world_set_day_time},
|
||||
{"get_seed", l_world_get_seed},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == player library ==*/
|
||||
extern int l_player_get_pos(lua_State* L);
|
||||
extern int l_player_get_rot(lua_State* L);
|
||||
extern int l_player_set_rot(lua_State* L);
|
||||
extern int l_player_set_pos(lua_State* L);
|
||||
extern int l_player_get_inv(lua_State* L);
|
||||
|
||||
static const luaL_Reg playerlib [] = {
|
||||
{"get_pos", l_player_get_pos},
|
||||
{"set_pos", l_player_set_pos},
|
||||
{"get_rot", l_player_get_rot},
|
||||
{"set_rot", l_player_set_rot},
|
||||
{"get_inventory", l_player_get_inv},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == inventory library == */
|
||||
extern int l_inventory_get(lua_State* L);
|
||||
extern int l_inventory_set(lua_State* L);
|
||||
extern int l_inventory_size(lua_State* L);
|
||||
extern int l_inventory_add(lua_State* L);
|
||||
extern int l_inventory_get_block(lua_State* L);
|
||||
|
||||
static const luaL_Reg inventorylib [] = {
|
||||
{"get", l_inventory_get},
|
||||
{"set", l_inventory_set},
|
||||
{"size", l_inventory_size},
|
||||
{"add", l_inventory_add},
|
||||
{"get_block", l_inventory_get_block},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == item library == */
|
||||
extern int l_item_name(lua_State* L);
|
||||
extern int l_item_index(lua_State* L);
|
||||
extern int l_item_stack_size(lua_State* L);
|
||||
extern int l_item_defs_count(lua_State* L);
|
||||
|
||||
static const luaL_Reg itemlib [] = {
|
||||
{"index", l_item_index},
|
||||
{"name", l_item_name},
|
||||
{"stack_size", l_item_stack_size},
|
||||
{"defs_count", l_item_defs_count},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == blocks-related functions == */
|
||||
extern int l_block_name(lua_State* L);
|
||||
extern int l_is_solid_at(lua_State* L);
|
||||
extern int l_blocks_count(lua_State* L);
|
||||
extern int l_block_index(lua_State* L);
|
||||
extern int l_set_block(lua_State* L);
|
||||
extern int l_get_block(lua_State* L);
|
||||
extern int l_get_block_x(lua_State* L);
|
||||
extern int l_get_block_y(lua_State* L);
|
||||
extern int l_get_block_z(lua_State* L);
|
||||
extern int l_get_block_states(lua_State* L);
|
||||
extern int l_get_block_user_bits(lua_State* L);
|
||||
extern int l_set_block_user_bits(lua_State* L);
|
||||
extern int l_is_replaceable_at(lua_State* L);
|
||||
|
||||
// Modified version of luaB_print from lbaselib.c
|
||||
extern int l_print(lua_State* L);
|
||||
|
||||
#endif // LOGIC_SCRIPTING_API_LUA_H_
|
||||
@@ -0,0 +1,106 @@
|
||||
#include "libgui.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "../scripting.h"
|
||||
#include "lua_util.h"
|
||||
|
||||
#include "../../../engine.h"
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../../frontend/gui/UINode.h"
|
||||
#include "../../../frontend/gui/controls.h"
|
||||
#include "../../../frontend/UiDocument.h"
|
||||
#include "../../../util/stringutil.h"
|
||||
|
||||
static gui::UINode* getDocumentNode(lua_State* L, const std::string& name, const std::string& nodeName) {
|
||||
auto doc = scripting::engine->getAssets()->getLayout(name);
|
||||
if (doc == nullptr) {
|
||||
luaL_error(L, "document '%s' not found", name.c_str());
|
||||
}
|
||||
auto node = doc->get(nodeName);
|
||||
if (node == nullptr) {
|
||||
luaL_error(L, "document '%s' has no element with id '%s'", name.c_str(), nodeName.c_str());
|
||||
}
|
||||
return node.get();
|
||||
}
|
||||
|
||||
static bool getattr(lua_State* L, gui::Button* button, const std::string& attr) {
|
||||
if (button == nullptr)
|
||||
return false;
|
||||
if (attr == "text") {
|
||||
lua_pushstring(L, util::wstr2str_utf8(button->getText()).c_str());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool getattr(lua_State* L, gui::Label* label, const std::string& attr) {
|
||||
if (label == nullptr)
|
||||
return false;
|
||||
if (attr == "text") {
|
||||
lua_pushstring(L, util::wstr2str_utf8(label->getText()).c_str());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) {
|
||||
if (button == nullptr)
|
||||
return false;
|
||||
if (attr == "text") {
|
||||
button->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool setattr(lua_State* L, gui::Label* label, const std::string& attr) {
|
||||
if (label == nullptr)
|
||||
return false;
|
||||
if (attr == "text") {
|
||||
label->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int l_gui_getattr(lua_State* L) {
|
||||
auto docname = lua_tostring(L, 1);
|
||||
auto element = lua_tostring(L, 2);
|
||||
const std::string attr = lua_tostring(L, 3);
|
||||
auto node = getDocumentNode(L, docname, element);
|
||||
|
||||
if (attr == "color") {
|
||||
return lua::pushcolor_arr(L, node->getColor());
|
||||
} else if (attr == "coord") {
|
||||
return lua::pushvec2_arr(L, node->getCoord());
|
||||
} else if (attr == "size") {
|
||||
return lua::pushvec2_arr(L, node->getSize());
|
||||
}
|
||||
|
||||
if (getattr(L, dynamic_cast<gui::Button*>(node), attr))
|
||||
return 1;
|
||||
if (getattr(L, dynamic_cast<gui::Label*>(node), attr))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_gui_getviewport(lua_State* L) {
|
||||
lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_gui_setattr(lua_State* L) {
|
||||
auto docname = lua_tostring(L, 1);
|
||||
auto element = lua_tostring(L, 2);
|
||||
const std::string attr = lua_tostring(L, 3);
|
||||
|
||||
auto node = getDocumentNode(L, docname, element);
|
||||
|
||||
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
|
||||
return 0;
|
||||
if (setattr(L, dynamic_cast<gui::Label*>(node), attr))
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||
#define LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
extern int l_gui_getviewport(lua_State* L);
|
||||
extern int l_gui_getattr(lua_State* L);
|
||||
extern int l_gui_setattr(lua_State* L);
|
||||
|
||||
static const luaL_Reg guilib [] = {
|
||||
{"get_viewport", l_gui_getviewport},
|
||||
{"getattr", l_gui_getattr},
|
||||
{"setattr", l_gui_setattr},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#endif // LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "libhud.h"
|
||||
#include "LuaState.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../scripting.h"
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../../frontend/hud.h"
|
||||
#include "../../../world/Level.h"
|
||||
#include "../../../voxels/Chunks.h"
|
||||
#include "../../../voxels/voxel.h"
|
||||
#include "../../../voxels/Block.h"
|
||||
#include "../../../content/Content.h"
|
||||
#include "../../../logic/BlocksController.h"
|
||||
#include "../../../items/Inventories.h"
|
||||
#include "../../../engine.h"
|
||||
|
||||
namespace scripting {
|
||||
extern Hud* hud;
|
||||
}
|
||||
|
||||
int l_hud_open_inventory(lua_State* L) {
|
||||
scripting::hud->openInventory();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_hud_close_inventory(lua_State* L) {
|
||||
scripting::hud->closeInventory();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_hud_open_block(lua_State* L) {
|
||||
lua::luaint x = lua_tointeger(L, 1);
|
||||
lua::luaint y = lua_tointeger(L, 2);
|
||||
lua::luaint z = lua_tointeger(L, 3);
|
||||
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
luaL_error(L, "block does not exists at %d %d %d", x, y, z);
|
||||
}
|
||||
auto def = scripting::content->getIndices()->getBlockDef(vox->id);
|
||||
auto assets = scripting::engine->getAssets();
|
||||
auto layout = assets->getLayout(def->uiLayout);
|
||||
if (layout == nullptr) {
|
||||
luaL_error(L, "block '%s' has no ui layout", def->name.c_str());
|
||||
}
|
||||
|
||||
auto id = scripting::blocks->createBlockInventory(x, y, z);
|
||||
if (id == 0) {
|
||||
luaL_error(L,
|
||||
"block '%s' at %d %d %d has no inventory",
|
||||
def->name.c_str(),
|
||||
x, y, z
|
||||
);
|
||||
}
|
||||
|
||||
scripting::hud->openInventory(
|
||||
glm::ivec3(x, y, z), layout, scripting::level->inventories->get(id)
|
||||
);
|
||||
|
||||
lua_pushinteger(L, id);
|
||||
lua_pushstring(L, def->uiLayout.c_str());
|
||||
return 2;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef LOGIC_SCRIPTING_API_LIBHUD_H_
|
||||
#define LOGIC_SCRIPTING_API_LIBHUD_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
extern int l_hud_open_inventory(lua_State* L);
|
||||
extern int l_hud_close_inventory(lua_State* L);
|
||||
extern int l_hud_open_block(lua_State* L);
|
||||
|
||||
static const luaL_Reg hudlib [] = {
|
||||
{"open_inventory", l_hud_open_inventory},
|
||||
{"close_inventory", l_hud_close_inventory},
|
||||
{"open_block", l_hud_open_block},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#endif // LOGIC_SCRIPTING_API_LIBHUD_H_
|
||||
@@ -0,0 +1,90 @@
|
||||
#ifndef LOGIC_SCRIPTING_LUA_UTIL_H_
|
||||
#define LOGIC_SCRIPTING_LUA_UTIL_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include "LuaState.h"
|
||||
|
||||
namespace lua {
|
||||
inline int pushivec3(lua_State* L, luaint x, luaint y, luaint z) {
|
||||
lua_pushinteger(L, x);
|
||||
lua_pushinteger(L, y);
|
||||
lua_pushinteger(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec3(lua_State* L, glm::vec3 vec) {
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_pushnumber(L, vec.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec4(lua_State* L, glm::vec4 vec) {
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_pushnumber(L, vec.w);
|
||||
return 4;
|
||||
}
|
||||
|
||||
inline int pushvec2_arr(lua_State* L, glm::vec2 vec) {
|
||||
lua_createtable(L, 2, 0);
|
||||
lua_getglobal(L, "vec2_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushvec3_arr(lua_State* L, glm::vec3 vec) {
|
||||
lua_createtable(L, 3, 0);
|
||||
lua_getglobal(L, "vec3_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushvec4_arr(lua_State* L, glm::vec4 vec) {
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_getglobal(L, "vec4_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_pushnumber(L, vec.w);
|
||||
lua_rawseti(L, -2, 4);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushcolor_arr(lua_State* L, glm::vec4 vec) {
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_getglobal(L, "color_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushinteger(L, vec.x*255);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushinteger(L, vec.y*255);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushinteger(L, vec.z*255);
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_pushinteger(L, vec.w*255);
|
||||
lua_rawseti(L, -2, 4);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_LUA_UTIL_H_
|
||||
Reference in New Issue
Block a user