From 895bac9b96bbb21b063f52fb35048afe63ea7982 Mon Sep 17 00:00:00 2001 From: bi Date: Sun, 6 May 2018 20:13:28 +0300 Subject: [PATCH] Mutators: added precalculated compatibility function --- .../vmf/modules/ui/mutators/mutator_gui.lua | 2 +- .../modules/ui/mutators/mutator_manager.lua | 62 +++++++++++++++++-- vmf/scripts/mods/vmf/vmf_loader.lua | 2 + 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_gui.lua b/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_gui.lua index c6984e1..2eaa556 100644 --- a/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_gui.lua +++ b/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_gui.lua @@ -317,7 +317,7 @@ local mutators_view = { -- Creates and return text for checkbox tooltip generate_tooltip_for = function(self, mutator) - local config = vmf.get_mutator_config(mutator) + local config = mutator:get_config() local text = "" -- Show supported difficulty when can't be enabled due to difficulty level diff --git a/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_manager.lua b/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_manager.lua index 6a07e4d..af7fef8 100644 --- a/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_manager.lua +++ b/vmf/scripts/mods/vmf/modules/ui/mutators/mutator_manager.lua @@ -104,6 +104,55 @@ local function is_compatible(mutator, other_mutator) return compatible end +-- Creates 'compatibility' entry for the mutator, checks compatibility of given mutator with all other mutators. +-- 'compatibility.mostly_compatible' is 'true' when mutator is not specifically set to be incompatible with +-- all other mutators. All the incompatible mutators will be added to 'compatibility.except'. And vice versa, +-- if 'mostly_compatible' is 'false', all the compatible mutators will be added to 'except'. +local function update_compatibility(mutator) + + -- Create default 'compatibility' entry + local config = mutator:get_config() + config.compatibility = {} + local compatibility = config.compatibility + compatibility.mostly_compatible = not config.incompatible_with_all + compatibility.except = {} + + local mostly_compatible = compatibility.mostly_compatible + local except = compatibility.except + + for _, other_mutator in ipairs(_MUTATORS) do + + local other_config = other_mutator:get_config() + local other_mostly_compatible = other_config.compatibility.mostly_compatible + local other_except = other_config.compatibility.except + + if is_compatible(mutator, other_mutator) then + if not mostly_compatible then except[other_mutator] = true end + if not other_mostly_compatible then other_except[mutator] = true end + else + if mostly_compatible then except[other_mutator] = true end + if other_mostly_compatible then other_except[mutator] = true end + end + end +end + +function vmf.temp_show_mutator_compatibility() + + print("MUTATORS COMPATIBILITY:") + + for _, mutator in ipairs(_MUTATORS) do + local compatibility = mutator:get_config().compatibility + + print("\n" .. mutator:get_readable_name() .. (compatibility.mostly_compatible and "[+]" or "[-]") .. ":") + + local ident = compatibility.mostly_compatible and " - " or " + " + + for other_mutator in pairs(compatibility.except) do + print(ident .. other_mutator:get_readable_name()) + end + end +end + -- Called after mutator is enabled local function on_enabled(mutator) local config = mutator:get_config() @@ -321,13 +370,9 @@ end -- Turns a mod into a mutator function vmf.register_mod_as_mutator(mod, config) + -- Form config config = config or {} - - table.insert(_MUTATORS, mod) - - -- Save config - mod._data.config = table.clone(_DEFAULT_CONFIG) - local _config = mod:get_config() + local _config = table.clone(_DEFAULT_CONFIG) for k, _ in pairs(_config) do if config[k] ~= nil then _config[k] = config[k] @@ -335,7 +380,10 @@ function vmf.register_mod_as_mutator(mod, config) end if _config.short_title == "" then _config.short_title = nil end + -- Save config inside the mod data + mod._data.config = _config + update_compatibility(mod) local mod_name = mod:get_name() @@ -350,6 +398,8 @@ function vmf.register_mod_as_mutator(mod, config) end end + table.insert(_MUTATORS, mod) + _MUTATORS_SORTED = false _MUTATORS_VIEW:update_mutator_list() diff --git a/vmf/scripts/mods/vmf/vmf_loader.lua b/vmf/scripts/mods/vmf/vmf_loader.lua index c46e627..fba5907 100644 --- a/vmf/scripts/mods/vmf/vmf_loader.lua +++ b/vmf/scripts/mods/vmf/vmf_loader.lua @@ -60,6 +60,8 @@ return { object.vmf.create_network_dictionary() object.vmf.ping_vmf_users() + object.vmf.temp_show_mutator_compatibility() + object.vmf.all_mods_loaded_event() object.vmf.all_mods_were_loaded = true