From 8765654e4678874761e6154fdac6362d84420b85 Mon Sep 17 00:00:00 2001 From: Xeno333 Date: Mon, 16 Jun 2025 19:09:07 -0500 Subject: [PATCH] working flags --- builtin/mainmenu/dlg_create_world.lua | 35 ++++++++++++++++++++++++--- src/content/mods.cpp | 16 +++++++++--- src/content/mods.h | 1 + src/content/subgames.cpp | 4 +++ src/script/lua_api/l_mainmenu.cpp | 14 +++++++++++ 5 files changed, 63 insertions(+), 7 deletions(-) diff --git a/builtin/mainmenu/dlg_create_world.lua b/builtin/mainmenu/dlg_create_world.lua index a629c0dd0..ea23ee16d 100644 --- a/builtin/mainmenu/dlg_create_world.lua +++ b/builtin/mainmenu/dlg_create_world.lua @@ -187,6 +187,10 @@ local function create_world_formspec(dialogdata) if is_lua_mapgen and lua_mapgens[current_mapgen].mg_flags then lua_mapgen_allowed_mg_flags = lua_mapgens[current_mapgen].mg_flags end + local lua_mapgen_flags = {} + if is_lua_mapgen and lua_mapgens[current_mapgen].lmg_flags then + lua_mapgen_flags = lua_mapgens[current_mapgen].lmg_flags + end -- The logic of the flag element IDs is as follows: -- "flag_main_foo-bar-baz" controls dialogdata.flags["main"]["foo_bar_baz"] @@ -265,13 +269,27 @@ local function create_world_formspec(dialogdata) end local mg_specific_flags = function(mapgen, y) - if not flag_checkboxes[mapgen] then + if not is_lua_mapgen and not flag_checkboxes[mapgen] then return "", y end if disallowed_mapgen_settings["mg"..mapgen.."_spflags"] then return "", y end + local form = "" + + if is_lua_mapgen then + for tab, _ in pairs(lua_mapgen_flags) do + local id = "flag_"..mapgen.."_"..tab:gsub("_", "-") + form = form .. ("checkbox[0,%f;%s;%s;%s]"): + format(y, id, tab, "true") + + y = y + 0.5 + end + + return form, y + end + for _, tab in pairs(flag_checkboxes[mapgen]) do local id = "flag_"..mapgen.."_"..tab[1]:gsub("_", "-") form = form .. ("checkbox[0,%f;%s;%s;%s]"): @@ -335,7 +353,7 @@ local function create_world_formspec(dialogdata) y_start = 0.0 end y = y_start + 0.3 - str_spflags = mg_specific_flags(current_mapgen_internal, y) + str_spflags = mg_specific_flags(current_mapgen, y) if str_spflags ~= "" then label_spflags = "label[0,"..y_start..";" .. fgettext("Mapgen-specific flags") .. "]" end @@ -455,8 +473,8 @@ local function create_world_buttonhandler(this, fields) end end + local lua_mapgens = core.get_lua_mapgens() if not is_internal_mapgen then - local lua_mapgens = core.get_lua_mapgens() for name, v in pairs(lua_mapgens) do if v.title == this.data.mg or (v.title == nil and name == this.data.mg) then mapgen_internal = "singlenode" @@ -480,6 +498,12 @@ local function create_world_buttonhandler(this, fields) mgvalleys_spflags = table_to_flags(this.data.flags.valleys), mgflat_spflags = table_to_flags(this.data.flags.flat), } + -- Add the lua-defined mapgen flags + for mg, v in pairs(lua_mapgens) do + if this.data.flags[mg] ~= nil then + settings["lmg_" .. mg .. "_flags"] = table_to_flags(this.data.flags[mg]) + end + end message = core.create_world(worldname, game.id, settings) end @@ -564,6 +588,11 @@ function create_create_world_dlg() flat = core.settings:get_flags("mgflat_spflags"), } } + for mg, tab in pairs(core.get_lua_mapgens()) do + if retval.data.flags[mg] == nil then + retval.data.flags[mg] = tab.lmg_flags + end + end return retval end diff --git a/src/content/mods.cpp b/src/content/mods.cpp index 737c4f6fc..c6f5387fc 100644 --- a/src/content/mods.cpp +++ b/src/content/mods.cpp @@ -148,13 +148,21 @@ bool parseModContents(ModSpec &spec) } } else { if (info.exists("mg_flags")) { - std::string dep = info.get("mg_flags"); - dep.erase(std::remove_if(dep.begin(), dep.end(), - static_cast(&std::isspace)), dep.end()); - for (const auto &flag : str_split(dep, ',')) { + std::string mg_flags = info.get("mg_flags"); + mg_flags.erase(std::remove_if(mg_flags.begin(), mg_flags.end(), + static_cast(&std::isspace)), mg_flags.end()); + for (const auto &flag : str_split(mg_flags, ',')) { spec.mg_flags.insert(flag); } } + if (info.exists("lmg_flags")) { + std::string lmg_flags = info.get("lmg_flags"); + lmg_flags.erase(std::remove_if(lmg_flags.begin(), lmg_flags.end(), + static_cast(&std::isspace)), lmg_flags.end()); + for (const auto &flag : str_split(lmg_flags, ',')) { + spec.lmg_flags.insert(flag); + } + } } if (info.exists("description")) diff --git a/src/content/mods.h b/src/content/mods.h index 1aef0a3a0..5c2d572e8 100644 --- a/src/content/mods.h +++ b/src/content/mods.h @@ -41,6 +41,7 @@ struct ModSpec // lua-defined mapgen only bool is_mapgen = false; std::unordered_set mg_flags; + std::unordered_set lmg_flags; /** * A constructed canonical path to represent this mod's location. diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp index 0610d16b0..5003eb59b 100644 --- a/src/content/subgames.cpp +++ b/src/content/subgames.cpp @@ -416,6 +416,10 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name, mgr.setMapSetting("seed", g_settings->get("fixed_map_seed")); + // If there is a lua-defined mapgen and it has flags then add its flags + if (lua_mapgen != "" && g_settings->exists("lmg_" + lua_mapgen + "_flags")) + mgr.setMapSetting("lmg_" + lua_mapgen + "_flags", g_settings->get("lmg_" + lua_mapgen + "_flags"), true); + mgr.makeMapgenParams(); mgr.saveMapMeta(); } diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index e1d9f58c8..bee19e994 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -718,6 +718,20 @@ int ModApiMainMenu::l_get_lua_mapgens(lua_State *L) } lua_settable(L, top_lvl2); + lua_pushstring(L, "lmg_flags"); + if (!mod.lmg_flags.empty()) { + lua_newtable(L); + int lmg_flags_top = lua_gettop(L); + for (const auto &flag : mod.lmg_flags) { + lua_pushstring(L, flag.c_str()); + lua_pushboolean(L, true); + lua_settable(L, lmg_flags_top); + } + } else { + lua_pushnil(L); + } + lua_settable(L, top_lvl2); + lua_settable(L, top); } }