From 8ca08a850ff2494652aa0ac2daa3d00f03aa4e7a Mon Sep 17 00:00:00 2001 From: fz72 Date: Sun, 15 Mar 2015 14:52:10 +0100 Subject: [PATCH] Save creative_mode and enable_damage setting for each world in world.mt Create Parameters on world initialisation and set settings of old worlds --- builtin/mainmenu/common.lua | 20 ++++++++++- builtin/mainmenu/modmgr.lua | 4 ++- builtin/mainmenu/tab_server.lua | 44 ++++++++++++++++++++++++ builtin/mainmenu/tab_singleplayer.lua | 48 +++++++++++++++++++++++++-- src/subgame.cpp | 4 ++- 5 files changed, 115 insertions(+), 5 deletions(-) diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua index f32d77f2a..c1e8ee9c4 100644 --- a/builtin/mainmenu/common.lua +++ b/builtin/mainmenu/common.lua @@ -189,7 +189,6 @@ end -------------------------------------------------------------------------------- function menu_handle_key_up_down(fields,textlist,settingname) - if fields["key_up"] then local oldidx = core.get_textlist_index(textlist) @@ -197,6 +196,16 @@ function menu_handle_key_up_down(fields,textlist,settingname) local newidx = oldidx -1 core.setting_set(settingname, menudata.worldlist:get_raw_index(newidx)) + + local worldconfig = modmgr.get_worldconfig( + menudata.worldlist:get_list()[newidx].path) + + if worldconfig.creative_mode ~= nil then + core.setting_set("creative_mode", worldconfig.creative_mode) + end + if worldconfig.enable_damage ~= nil then + core.setting_set("enable_damage", worldconfig.enable_damage) + end end return true end @@ -208,6 +217,15 @@ function menu_handle_key_up_down(fields,textlist,settingname) local newidx = oldidx + 1 core.setting_set(settingname, menudata.worldlist:get_raw_index(newidx)) + local worldconfig = modmgr.get_worldconfig( + menudata.worldlist:get_list()[newidx].path) + + if worldconfig.creative_mode ~= nil then + core.setting_set("creative_mode", worldconfig.creative_mode) + end + if worldconfig.enable_damage ~= nil then + core.setting_set("enable_damage", worldconfig.enable_damage) + end end return true diff --git a/builtin/mainmenu/modmgr.lua b/builtin/mainmenu/modmgr.lua index f2938685e..f6b9b4814 100644 --- a/builtin/mainmenu/modmgr.lua +++ b/builtin/mainmenu/modmgr.lua @@ -321,8 +321,10 @@ function modmgr.get_worldconfig(worldpath) for key,value in pairs(worldfile:to_table()) do if key == "gameid" then worldconfig.id = value - else + elseif key:sub(0, 9) == "load_mod_" then worldconfig.global_mods[key] = core.is_yes(value) + else + worldconfig[key] = value end end diff --git a/builtin/mainmenu/tab_server.lua b/builtin/mainmenu/tab_server.lua index 1ae2a0656..d78af768f 100644 --- a/builtin/mainmenu/tab_server.lua +++ b/builtin/mainmenu/tab_server.lua @@ -68,6 +68,32 @@ local function main_button_handler(this, fields, name, tabdata) if fields["srv_worlds"] ~= nil then local event = core.explode_textlist_event(fields["srv_worlds"]) + local selected = core.get_textlist_index("srv_worlds") + if selected ~= nil then + local filename = menudata.worldlist:get_list()[selected].path + local worldconfig = modmgr.get_worldconfig(filename) + filename = filename .. DIR_DELIM .. "world.mt" + + if worldconfig.creative_mode ~= nil then + core.setting_set("creative_mode", worldconfig.creative_mode) + else + local worldfile = Settings(filename) + worldfile:set("creative_mode", core.setting_get("creative_mode")) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end + end + if worldconfig.enable_damage ~= nil then + core.setting_set("enable_damage", worldconfig.enable_damage) + else + local worldfile = Settings(filename) + worldfile:set("enable_damage", core.setting_get("enable_damage")) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end + end + end + if event.type == "DCL" then world_doubleclick = true end @@ -84,11 +110,29 @@ local function main_button_handler(this, fields, name, tabdata) if fields["cb_creative_mode"] then core.setting_set("creative_mode", fields["cb_creative_mode"]) + local selected = core.get_textlist_index("srv_worlds") + local filename = menudata.worldlist:get_list()[selected].path .. + DIR_DELIM .. "world.mt" + + local worldfile = Settings(filename) + worldfile:set("creative_mode", fields["cb_creative_mode"]) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end return true end if fields["cb_enable_damage"] then core.setting_set("enable_damage", fields["cb_enable_damage"]) + local selected = core.get_textlist_index("srv_worlds") + local filename = menudata.worldlist:get_list()[selected].path .. + DIR_DELIM .. "world.mt" + + local worldfile = Settings(filename) + worldfile:set("enable_damage", fields["cb_enable_damage"]) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end return true end diff --git a/builtin/mainmenu/tab_singleplayer.lua b/builtin/mainmenu/tab_singleplayer.lua index 9dc377a8f..c2b850e44 100644 --- a/builtin/mainmenu/tab_singleplayer.lua +++ b/builtin/mainmenu/tab_singleplayer.lua @@ -106,13 +106,39 @@ local function main_button_handler(this, fields, name, tabdata) if fields["sp_worlds"] ~= nil then local event = core.explode_textlist_event(fields["sp_worlds"]) + local selected = core.get_textlist_index("sp_worlds") + if selected ~= nil then + local filename = menudata.worldlist:get_list()[selected].path + local worldconfig = modmgr.get_worldconfig(filename) + filename = filename .. DIR_DELIM .. "world.mt" + + if worldconfig.creative_mode ~= nil then + core.setting_set("creative_mode", worldconfig.creative_mode) + else + local worldfile = Settings(filename) + worldfile:set("creative_mode", core.setting_get("creative_mode")) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end + end + if worldconfig.enable_damage ~= nil then + core.setting_set("enable_damage", worldconfig.enable_damage) + else + local worldfile = Settings(filename) + worldfile:set("enable_damage", core.setting_get("enable_damage")) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end + end + end + if event.type == "DCL" then world_doubleclick = true end - if event.type == "CHG" then + if event.type == "CHG" and selected ~= nil then core.setting_set("mainmenu_last_selected_world", - menudata.worldlist:get_raw_index(core.get_textlist_index("sp_worlds"))) + menudata.worldlist:get_raw_index(selected)) return true end end @@ -123,11 +149,29 @@ local function main_button_handler(this, fields, name, tabdata) if fields["cb_creative_mode"] then core.setting_set("creative_mode", fields["cb_creative_mode"]) + local selected = core.get_textlist_index("sp_worlds") + local filename = menudata.worldlist:get_list()[selected].path .. + DIR_DELIM .. "world.mt" + + local worldfile = Settings(filename) + worldfile:set("creative_mode", fields["cb_creative_mode"]) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end return true end if fields["cb_enable_damage"] then core.setting_set("enable_damage", fields["cb_enable_damage"]) + local selected = core.get_textlist_index("sp_worlds") + local filename = menudata.worldlist:get_list()[selected].path .. + DIR_DELIM .. "world.mt" + + local worldfile = Settings(filename) + worldfile:set("enable_damage", fields["cb_enable_damage"]) + if not worldfile:write() then + core.log("error", "Failed to write world config file") + end return true end diff --git a/src/subgame.cpp b/src/subgame.cpp index 4e8777d13..a3edcda2e 100644 --- a/src/subgame.cpp +++ b/src/subgame.cpp @@ -283,7 +283,9 @@ bool initializeWorld(const std::string &path, const std::string &gameid) std::string worldmt_path = path + DIR_DELIM "world.mt"; if (!fs::PathExists(worldmt_path)) { std::ostringstream ss(std::ios_base::binary); - ss << "gameid = " << gameid << "\nbackend = sqlite3\n"; + ss << "gameid = " << gameid << "\nbackend = sqlite3\n" + << "creative_mode = " << g_settings->get("creative_mode") + << "\nenable_damage = " << g_settings->get("enable_damage") << "\n"; if (!fs::safeWriteToFile(worldmt_path, ss.str())) return false;