content-pack specific variables PACK_ENV and PACK_ID
This commit is contained in:
@@ -274,7 +274,7 @@ void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) {
|
|||||||
void ContentLoader::load(ContentBuilder& builder) {
|
void ContentLoader::load(ContentBuilder& builder) {
|
||||||
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
|
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
|
||||||
|
|
||||||
auto runtime = new ContentPackRuntime(*pack, scripting::create_environment());
|
auto runtime = new ContentPackRuntime(*pack, scripting::create_pack_environment(*pack));
|
||||||
builder.add(runtime);
|
builder.add(runtime);
|
||||||
env = runtime->getEnvironment()->getId();
|
env = runtime->getEnvironment()->getId();
|
||||||
|
|
||||||
|
|||||||
@@ -181,6 +181,11 @@ int lua::LuaState::pushenv(int env) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lua::LuaState::pushvalue(int idx) {
|
||||||
|
lua_pushvalue(L, idx);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int lua::LuaState::pushglobals() {
|
int lua::LuaState::pushglobals() {
|
||||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ namespace lua {
|
|||||||
int pushnumber(luanumber x);
|
int pushnumber(luanumber x);
|
||||||
int pushstring(const std::string& str);
|
int pushstring(const std::string& str);
|
||||||
int pushenv(int env);
|
int pushenv(int env);
|
||||||
|
int pushvalue(int idx);
|
||||||
int pushnil();
|
int pushnil();
|
||||||
int pushglobals();
|
int pushglobals();
|
||||||
void pop(int n=1);
|
void pop(int n=1);
|
||||||
|
|||||||
@@ -95,6 +95,16 @@ std::unique_ptr<Environment> scripting::create_environment(int parent) {
|
|||||||
return std::make_unique<Environment>(state->createEnvironment(parent));
|
return std::make_unique<Environment>(state->createEnvironment(parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Environment> scripting::create_pack_environment(const ContentPack& pack) {
|
||||||
|
int id = state->createEnvironment(0);
|
||||||
|
state->pushenv(id);
|
||||||
|
state->pushvalue(-1);
|
||||||
|
state->setfield("PACK_ENV");
|
||||||
|
state->pushstring(pack.id);
|
||||||
|
state->setfield("PACK_ID");
|
||||||
|
return std::make_unique<Environment>(id);
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
||||||
scripting::level = level;
|
scripting::level = level;
|
||||||
scripting::content = level->content;
|
scripting::content = level->content;
|
||||||
@@ -230,8 +240,6 @@ bool register_event(int env, const std::string& name, const std::string& id) {
|
|||||||
// remove previous name
|
// remove previous name
|
||||||
state->pushnil();
|
state->pushnil();
|
||||||
state->setfield(name, -3);
|
state->setfield(name, -3);
|
||||||
|
|
||||||
std::cout << id << std::endl;
|
|
||||||
// add new global name
|
// add new global name
|
||||||
state->setglobal(id);
|
state->setglobal(id);
|
||||||
state->pop();
|
state->pop();
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class LuaState;
|
|||||||
|
|
||||||
class Engine;
|
class Engine;
|
||||||
class Content;
|
class Content;
|
||||||
|
class ContentPack;
|
||||||
class ContentIndices;
|
class ContentIndices;
|
||||||
class Level;
|
class Level;
|
||||||
class Block;
|
class Block;
|
||||||
@@ -52,6 +53,7 @@ namespace scripting {
|
|||||||
);
|
);
|
||||||
|
|
||||||
std::unique_ptr<Environment> create_environment(int parent=0);
|
std::unique_ptr<Environment> create_environment(int parent=0);
|
||||||
|
std::unique_ptr<Environment> create_pack_environment(const ContentPack& pack);
|
||||||
|
|
||||||
void on_world_load(Level* level, BlocksController* blocks);
|
void on_world_load(Level* level, BlocksController* blocks);
|
||||||
void on_world_save();
|
void on_world_save();
|
||||||
|
|||||||
Reference in New Issue
Block a user