Jump to content

World Options and Mod Changes


zarklord_klei
 Share

Recommended Posts

The final solution to the question:
How does a mod change Pre/Post settings on world-generation, without forcing them on every load is the following code.

We (penguin0616 and I) spent over 50 hours for this, while it would be a "one liner" for Klei to simply add modsupport, so mods can easily change them... And my teleportato mod is still not completely made compatible to that QoL update, guess it will take several more hours.. now you see why I thought about abandoning my mods...)
In modworldgenmain.lua:

local overrides_forest = {dropeverythingondespawn = "always"} -- put your Pre/Post settings you would like to load here
local overrides_cave = {dropeverythingondespawn = "always"}

local WSO = require("worldsettings_overrides")
local savedata = nil
local new_game = true
local function GetSaveData()
    if new_game then
        if savedata then
            return savedata
        end
        local i = 1
        local stack_i = 3 -- we increment this in case another mod is overwriting a function, to make sure getlocal still works.
        while true do
            if stack_i>20 then --give up
                return nil
            end
            local name, value = GLOBAL.debug.getlocal(stack_i, i) -- Level 1 is where we are now, Level 2 is our Pre function calling this, level 3 is gamelogic.lua:362 or gamelogic.lua:367 depending on the savedata.
            if name then
                if name == "savedata" then
                    if value.meta.SERP_MOD_HAS_OVERRIDEN then -- only do it once per world
                        new_game = false -- do not try it again, it is no new game, so no need to find savedata again.
                        return nil
                    end
                    savedata = value
                    savedata.meta.SERP_MOD_HAS_OVERRIDEN = true
                    return savedata
                end
                i = i+1
            else
                stack_i = stack_i+1
                i = 1
            end
        end
    end
end
for i,PrePost in ipairs({"Pre","Post"}) do
    for name, fn in pairs(WSO[PrePost]) do
        if overrides_forest[name] or overrides_cave[name] then
            local old_fn = WSO[PrePost][name]
            WSO[PrePost][name] = function(difficulty,...)
                if new_game then
                    if not savedata then -- only when it is a new game
                        GetSaveData()
                    end
                    if savedata then
                        if savedata.map.prefab=="forest"  then
                            if overrides_forest[name] then
                                savedata.map.topology.overrides[name] = overrides_forest[name]
                                difficulty = overrides_forest[name]
                            end
                        elseif savedata.map.prefab=="cave" then
                            if overrides_cave[name] then
                                savedata.map.topology.overrides[name] = overrides_cave[name]
                                difficulty = overrides_cave[name]
                            end
                        end
                    end
                end
                if old_fn then
                    return old_fn(difficulty,...)
                end
            end
        end
    end
end


 

Edited by Serpens
  • Like 1
Link to comment
Share on other sites

Feature Request:
Could Klei please add a way for mods to change existing settings? For example add more season-length options to the season settings.
Currently this is not possible without again alot of work and overwriting game functions, while it would be so damn easy for Klei to add this option.
I could write you the few lines code you need to add, to also allow replacing/merging existing settings.

edit:
was not too complicated, but only thanks to upvaluehacker. Following code in modworldgenmain.lua:

-- ## Add more options to existing game settings.
-- this is an example to add 3 and 6 days to all season lengths. Requires upvaluehacker (google for it if you dont have it)
local UpvalueHacker = GLOBAL.require("upvaluehacker")
local customize = GLOBAL.require("map/customize")
local WSO = require("worldsettings_overrides")
local RefreshWorldTabs = UpvalueHacker.GetUpvalue(customize.RemoveCustomizeGroup, "RefreshWorldTabs")
local customize_descriptions = UpvalueHacker.GetUpvalue(customize.GetDescription, "descriptions")
	local new_seasonlengths = {{ text = "3 days", data = "3__daysseason", pos=1 },{ text = "6 days", data = "6__daysseason", pos=2 }}
for _,length in ipairs(new_seasonlengths) do
    table.insert(customize_descriptions.season_length_descriptions,length.pos,length)
end
UpvalueHacker.SetUpvalue(customize.GetDescription, customize_descriptions, "descriptions")
RefreshWorldTabs() -- refresh to display new settings
	local seasons = {"autumn","summer","spring","winter"}
for i,season in ipairs(seasons) do
    local old_fn = WSO.Post[season]
    WSO.Post[season] = function(difficulty,...)
        if string.find(difficulty,"__") then
            local diff_split = difficulty:split("__") -- this way we det the desired length number from aboves data string
            GLOBAL.TheWorld:PushEvent("ms_setseasonlength", {season = season, length = diff_split[1]})
        elseif old_fn~=nil then
            return old_fn(difficulty,...)
        end
    end
end
---------------------------
Edited by Serpens
Link to comment
Share on other sites

@zarklord_klei

modservercreationmain.lua

FrontEndAssets = {
    Asset("ATLAS", "images/blackbear.xml"),
    Asset("IMAGE", "images/blackbear.tex"),
}

local itemsettingsblackbear = {
    value = "default",
    atlas = "images/blackbear.xml",
    image = "blackbear.tex",
    masteroption = true,
    master_controlled = true,
    order = 1
}

AddCustomizeItem(category, groupname, itemnameblackbear, itemsettingsblackbear)

The image not shown. Looks like modservercreationmain.lua didn't work. Please fixed it.

client_log.txt

[00:02:41]: FrontendLoadMod	workshop-2494474238	
[00:02:41]: loaded mod_config_data/modconfiguration_workshop-2494474238	
[00:02:41]: Fontend-Loading mod: workshop-2494474238 (Myth Patches) Version:1.1	
[00:02:41]: Mod: workshop-2494474238 (Myth Patches)	Loading modservercreationmain.lua	
[00:02:41]: WARNING! Invalid resource handle for atlas '../mods/workshop-2494474238/images/blackbear.xml', did you remember to load the asset?
[00:02:41]: Looking for default texture '' from atlas '../mods/workshop-2494474238/images/blackbear.xml'.
[00:02:41]: Error Looking for default texture in from atlas '../mods/workshop-2494474238/images/blackbear.xml'.

 

20210907133718.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...