world.lua script

This commit is contained in:
MihailRis 2024-01-26 18:37:10 +03:00
parent 5d6c2b9b8e
commit abc3e47feb
7 changed files with 68 additions and 2 deletions

View File

@ -276,6 +276,12 @@ void ContentLoader::load(ContentBuilder* builder) {
fixPackIndices();
auto folder = pack->folder;
fs::path scriptFile = folder/fs::path("scripts/world.lua");
if (fs::is_regular_file(scriptFile)) {
scripting::load_world_script(pack->id, scriptFile);
}
if (!fs::is_regular_file(pack->getContentFile()))
return;
auto root = files::read_json(pack->getContentFile());

View File

@ -123,8 +123,8 @@ void Engine::mainloop() {
}
Engine::~Engine() {
scripting::close();
screen = nullptr;
scripting::close();
Audio::finalize();

View File

@ -100,8 +100,10 @@ LevelScreen::LevelScreen(Engine* engine, Level* level)
LevelScreen::~LevelScreen() {
std::cout << "-- writing world" << std::endl;
controller->onWorldSave();
auto world = level->getWorld();
world->write(level.get());
controller->onWorldQuit();
}
void LevelScreen::updateHotkeys() {

View File

@ -17,7 +17,6 @@ LevelController::LevelController(EngineSettings& settings, Level* level)
}
LevelController::~LevelController() {
scripting::on_world_quit();
}
void LevelController::update(float delta, bool input, bool pause) {
@ -26,3 +25,11 @@ void LevelController::update(float delta, bool input, bool pause) {
chunks->update(settings.chunks.loadSpeed);
blocks->update(delta);
}
void LevelController::onWorldSave() {
scripting::on_world_save();
}
void LevelController::onWorldQuit() {
scripting::on_world_quit();
}

View File

@ -29,6 +29,9 @@ public:
void update(float delta,
bool input,
bool pause);
void onWorldSave();
void onWorldQuit();
};
#endif // LOGIC_LEVEL_CONTROLLER_H_

View File

@ -4,6 +4,7 @@
#include <stdexcept>
#include <lua.hpp>
#include "../../content/ContentPack.h"
#include "../../files/engine_paths.h"
#include "../../files/files.h"
#include "../../util/timeutil.h"
@ -105,9 +106,40 @@ void scripting::on_world_load(Level* level, BlocksController* blocks) {
scripting::content = level->content;
scripting::blocks = blocks;
load_script("world.lua");
for (auto& pack : scripting::engine->getContentPacks()) {
std::string name = pack.id+".worldopen";
lua_getglobal(L, name.c_str());
if (lua_isnil(L, lua_gettop(L))) {
lua_pop(L, lua_gettop(L));
continue;
}
call_func(L, 0, name);
}
}
void scripting::on_world_save() {
for (auto& pack : scripting::engine->getContentPacks()) {
std::string name = pack.id+".worldsave";
lua_getglobal(L, name.c_str());
if (lua_isnil(L, lua_gettop(L))) {
lua_pop(L, lua_gettop(L));
continue;
}
call_func(L, 0, name);
}
}
void scripting::on_world_quit() {
for (auto& pack : scripting::engine->getContentPacks()) {
std::string name = pack.id+".worldquit";
lua_getglobal(L, name.c_str());
if (lua_isnil(L, lua_gettop(L))) {
lua_pop(L, lua_gettop(L));
continue;
}
call_func(L, 0, name);
}
scripting::level = nullptr;
scripting::content = nullptr;
}
@ -211,6 +243,20 @@ void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_s
funcsset->on_block_break_by=rename_global(L, "on_block_break_by", (prefix+".blockbreakby").c_str());
}
void scripting::load_world_script(std::string prefix, fs::path file) {
std::string src = files::read_string(file);
std::cout << "loading script " << file.u8string() << std::endl;
if (luaL_loadbuffer(L, src.c_str(), src.size(), file.string().c_str())) {
handleError(L);
return;
}
call_func(L, 0, "<script>");
rename_global(L, "init", (prefix+".init").c_str());
rename_global(L, "on_world_open", (prefix+".worldopen").c_str());
rename_global(L, "on_world_save", (prefix+".worldsave").c_str());
rename_global(L, "on_world_quit", (prefix+".worldquit").c_str());
}
void scripting::close() {
lua_close(L);

View File

@ -21,6 +21,7 @@ namespace scripting {
void initialize(Engine* engine);
void on_world_load(Level* level, BlocksController* blocks);
void on_world_save();
void on_world_quit();
void on_blocks_tick(const Block* block, int tps);
void update_block(const Block* block, int x, int y, int z);
@ -32,5 +33,6 @@ namespace scripting {
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
void load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset);
void load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset);
void load_world_script(std::string prefix, fs::path file);
void close();
}