Save creative_mode and enable_damage setting for each world in world.mt
authorfz72 <fz72@gmx.de>
Sun, 15 Mar 2015 13:52:10 +0000 (14:52 +0100)
committerLoic Blot <loic.blot@unix-experience.fr>
Wed, 18 Mar 2015 08:53:24 +0000 (09:53 +0100)
Create Parameters on world initialisation and set settings of old worlds

builtin/mainmenu/common.lua
builtin/mainmenu/modmgr.lua
builtin/mainmenu/tab_server.lua
builtin/mainmenu/tab_singleplayer.lua
src/subgame.cpp

index f32d77f2a4a03c11c85782517e8b19c2eb3f5b46..c1e8ee9c46d220ed1ac423a1319128c1b5aa14e8 100644 (file)
@@ -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
index f2938685eef93c8cd05119c6a1a3f43e6c1334f0..f6b9b4814c7cf6ef6e1eb7be59e6cdbf6a15152d 100644 (file)
@@ -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
 
index 1ae2a065675131daccb570337ce51c76d3dfb80a..d78af768fdd3c7b848a75417faf5d6409928104f 100644 (file)
@@ -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
 
index 9dc377a8ff5545fdb87dc7ddcdccbebf024b3b34..c2b850e449507c4394ff31bc1617d950eb0b4078 100644 (file)
@@ -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
 
index 4e8777d13a7ef30691fad1e6efa7e59c9469ee07..a3edcda2eb294a08cc9c396891f6d5fd078663eb 100644 (file)
@@ -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;