critical lua fixes
This commit is contained in:
@@ -51,6 +51,7 @@ std::shared_ptr<Inventory> Inventories::clone(int64_t id) {
|
|||||||
auto origptr = reinterpret_cast<const Inventory*>(original.get());
|
auto origptr = reinterpret_cast<const Inventory*>(original.get());
|
||||||
auto clone = std::make_shared<Inventory>(*origptr);
|
auto clone = std::make_shared<Inventory>(*origptr);
|
||||||
clone->setId(level.getWorld()->getNextInventoryId());
|
clone->setId(level.getWorld()->getNextInventoryId());
|
||||||
|
store(clone);
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
Inventory::Inventory(int64_t id, size_t size) : id(id), slots(size) {
|
Inventory::Inventory(int64_t id, size_t size) : id(id), slots(size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Inventory::Inventory(const Inventory& orig) {
|
||||||
|
this->slots = orig.slots;
|
||||||
|
}
|
||||||
|
|
||||||
ItemStack& Inventory::getSlot(size_t index) {
|
ItemStack& Inventory::getSlot(size_t index) {
|
||||||
return slots.at(index);
|
return slots.at(index);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ class Inventory : Serializable {
|
|||||||
public:
|
public:
|
||||||
Inventory(int64_t id, size_t size);
|
Inventory(int64_t id, size_t size);
|
||||||
|
|
||||||
|
Inventory(const Inventory& orig);
|
||||||
|
|
||||||
ItemStack& getSlot(size_t index);
|
ItemStack& getSlot(size_t index);
|
||||||
size_t findEmptySlot(size_t begin=0, size_t end=-1) const;
|
size_t findEmptySlot(size_t begin=0, size_t end=-1) const;
|
||||||
size_t findSlotByItem(itemid_t id, size_t begin=0, size_t end=-1);
|
size_t findSlotByItem(itemid_t id, size_t begin=0, size_t end=-1);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ lua::LuaState::LuaState() {
|
|||||||
setglobal(envName(0));
|
setglobal(envName(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string lua::LuaState::envName(int env) const {
|
const std::string lua::LuaState::envName(int env) {
|
||||||
return "_ENV"+util::mangleid(env);
|
return "_ENV"+util::mangleid(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ namespace lua {
|
|||||||
LuaState();
|
LuaState();
|
||||||
~LuaState();
|
~LuaState();
|
||||||
|
|
||||||
const std::string envName(int env) const;
|
static const std::string envName(int env);
|
||||||
void loadbuffer(int env, const std::string& src, const std::string& file);
|
void loadbuffer(int env, const std::string& src, const std::string& file);
|
||||||
int gettop() const;
|
int gettop() const;
|
||||||
int pushivec3(luaint x, luaint y, luaint z);
|
int pushivec3(luaint x, luaint y, luaint z);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../scripting.h"
|
#include "../scripting.h"
|
||||||
#include "lua_util.h"
|
#include "lua_util.h"
|
||||||
|
#include "LuaState.h"
|
||||||
|
|
||||||
#include "../../../engine.h"
|
#include "../../../engine.h"
|
||||||
#include "../../../assets/Assets.h"
|
#include "../../../assets/Assets.h"
|
||||||
@@ -97,9 +98,9 @@ int l_gui_setattr(lua_State* L) {
|
|||||||
|
|
||||||
auto node = getDocumentNode(L, docname, element);
|
auto node = getDocumentNode(L, docname, element);
|
||||||
if (attr == "pos") {
|
if (attr == "pos") {
|
||||||
node->setCoord(lua::tovec2(L, 1));
|
node->setCoord(lua::tovec2(L, 4));
|
||||||
} else if (attr == "size") {
|
} else if (attr == "size") {
|
||||||
node->setSize(lua::tovec2(L, 1));
|
node->setSize(lua::tovec2(L, 4));
|
||||||
} else {
|
} else {
|
||||||
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
|
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
|
||||||
return 0;
|
return 0;
|
||||||
@@ -108,3 +109,13 @@ int l_gui_setattr(lua_State* L) {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int l_gui_get_env(lua_State* L) {
|
||||||
|
auto name = lua_tostring(L, 1);
|
||||||
|
auto doc = scripting::engine->getAssets()->getLayout(name);
|
||||||
|
if (doc == nullptr) {
|
||||||
|
luaL_error(L, "document '%s' not found", name);
|
||||||
|
}
|
||||||
|
lua_getglobal(L, lua::LuaState::envName(doc->getEnvironment()).c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
#ifndef LOGIC_SCRIPTING_API_LIBGUI_H_
|
#ifndef LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||||
#define LOGIC_SCRIPTING_API_LIBGUI_H_
|
#define LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||||
|
|
||||||
#include <lua.hpp>
|
#include "lua_commons.h"
|
||||||
|
|
||||||
extern int l_gui_getviewport(lua_State* L);
|
extern int l_gui_getviewport(lua_State* L);
|
||||||
extern int l_gui_getattr(lua_State* L);
|
extern int l_gui_getattr(lua_State* L);
|
||||||
extern int l_gui_setattr(lua_State* L);
|
extern int l_gui_setattr(lua_State* L);
|
||||||
|
extern int l_gui_get_env(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg guilib [] = {
|
static const luaL_Reg guilib [] = {
|
||||||
{"get_viewport", l_gui_getviewport},
|
{"get_viewport", lua_wrap_errors<l_gui_getviewport>},
|
||||||
{"getattr", l_gui_getattr},
|
{"getattr", lua_wrap_errors<l_gui_getattr>},
|
||||||
{"setattr", l_gui_setattr},
|
{"setattr", lua_wrap_errors<l_gui_setattr>},
|
||||||
|
{"get_env", lua_wrap_errors<l_gui_get_env>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -87,10 +87,12 @@ namespace lua {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline glm::vec2 tovec2(lua_State* L, int idx) {
|
inline glm::vec2 tovec2(lua_State* L, int idx) {
|
||||||
lua_rawgeti(L, idx, 1);
|
lua_pushvalue(L, idx);
|
||||||
|
lua_rawgeti(L, -1, 1);
|
||||||
lua::luanumber x = lua_tonumber(L, -1); lua_pop(L, -1);
|
lua::luanumber x = lua_tonumber(L, -1); lua_pop(L, -1);
|
||||||
lua_rawgeti(L, idx, 1);
|
lua_rawgeti(L, -2, 2);
|
||||||
lua::luanumber y = lua_tonumber(L, -1); lua_pop(L, -1);
|
lua::luanumber y = lua_tonumber(L, -1); lua_pop(L, -1);
|
||||||
|
lua_pop(L, -1);
|
||||||
return glm::vec2(x, y);
|
return glm::vec2(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ runnable scripting::create_runnable(
|
|||||||
const std::string& file
|
const std::string& file
|
||||||
) {
|
) {
|
||||||
return [=](){
|
return [=](){
|
||||||
state->execute(env, src, file);
|
try {
|
||||||
|
state->execute(env, src, file);
|
||||||
|
} catch (const lua::luaerror& err) {
|
||||||
|
std::cerr << err.what() << std::endl;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user