content-pack specific variables PACK_ENV and PACK_ID

This commit is contained in:
MihailRis 2024-02-12 03:22:08 +03:00
parent 49fe2108d3
commit 617017d979
5 changed files with 19 additions and 3 deletions

View File

@ -274,7 +274,7 @@ void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) {
void ContentLoader::load(ContentBuilder& builder) {
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);
env = runtime->getEnvironment()->getId();

View File

@ -181,6 +181,11 @@ int lua::LuaState::pushenv(int env) {
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;

View File

@ -32,6 +32,7 @@ namespace lua {
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);

View File

@ -95,6 +95,16 @@ std::unique_ptr<Environment> scripting::create_environment(int 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) {
scripting::level = level;
scripting::content = level->content;
@ -230,8 +240,6 @@ bool register_event(int env, const std::string& name, const std::string& id) {
// remove previous name
state->pushnil();
state->setfield(name, -3);
std::cout << id << std::endl;
// add new global name
state->setglobal(id);
state->pop();

View File

@ -9,6 +9,7 @@ class LuaState;
class Engine;
class Content;
class ContentPack;
class ContentIndices;
class Level;
class Block;
@ -52,6 +53,7 @@ namespace scripting {
);
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_save();