mutators: fool proof 'enable_before_these'/'enable_after_these'

This commit is contained in:
UnShame 2018-02-19 21:45:55 +03:00
parent 388574ba1e
commit 4d5a0e64e2

View file

@ -4,16 +4,24 @@ local mutators = {}
local mutators_sequence = {} local mutators_sequence = {}
local mutators_sorted = false local mutators_sorted = false
local function update_mutators_sequence(mod_name, load_these_after) local function update_mutators_sequence(mutator_name, load_these_after)
if not mutators_sequence[mod_name] then if not mutators_sequence[mutator_name] then
mutators_sequence[mod_name] = {} mutators_sequence[mutator_name] = {}
end end
for _, mutator_name in ipairs(load_these_after) do for _, other_mutator_name in ipairs(load_these_after) do
if not table.has_item(mutators_sequence[mod_name], mutator_name) then
table.insert(mutators_sequence[mod_name], mutator_name) if mutators_sequence[other_mutator_name] and table.has_item(mutators_sequence[other_mutator_name], mutator_name) then
vmf:error("Mutators '" .. mutator_name .. "' and '" .. other_mutator_name .. "' are both set to load after the other one.")
elseif not table.has_item(mutators_sequence[mutator_name], other_mutator_name) then
table.insert(mutators_sequence[mutator_name], other_mutator_name)
end end
end end
table.combine(mutators_sequence[mod_name], load_these_after) table.combine(mutators_sequence[mutator_name], load_these_after)
end
local function error_endless_loop()
vmf:error("Mutators: too many iterations. Check for loops in 'enable_before_these'/'enable_after_these'.")
end end
local function sort_mutators() local function sort_mutators()
@ -24,6 +32,9 @@ local function sort_mutators()
end end
print("-----------") print("-----------")
local maxIter = #mutators * #mutators * #mutators
local numIter = 0
local i = 2 local i = 2
while i <= #mutators do while i <= #mutators do
local mutator = mutators[i] local mutator = mutators[i]
@ -39,9 +50,15 @@ local function sort_mutators()
i = i - 1 i = i - 1
end end
j = j - 1 j = j - 1
numIter = numIter + 1
if numIter > maxIter then return error_endless_loop() end
end end
i = i + 1 i = i + 1
numIter = numIter + 1
if numIter > maxIter then return error_endless_loop() end
end end
mutators_sorted = true mutators_sorted = true
@ -132,39 +149,61 @@ VMFMod.register_as_mutator = function(self, config)
end end
-- Testing -- Testing
local mutator1 = new_mod("mutator1") local mutation = new_mod("mutation")
local mutator2 = new_mod("mutator2") local deathwish = new_mod("deathwish")
local mutator3 = new_mod("mutator3") local mutator3 = new_mod("mutator3")
local mutator555 = new_mod("mutator555") local mutator555 = new_mod("mutator555")
local mutator_whatever = new_mod("mutator_whatever") local mutator_whatever = new_mod("mutator_whatever")
mutator555:register_as_mutator({ mutator555:register_as_mutator({
enable_after_these = { enable_after_these = {
"mutator1" "mutation"
} }
}) })
mutator555:create_options({}, true, "mutator555", "mutator555 description") mutator555:create_options({}, true, "mutator555", "mutator555 description")
mutator555.on_enabled = function() end mutator555.on_enabled = function() end
mutator555.on_disabled = function() end mutator555.on_disabled = function() end
mutator2:register_as_mutator({
deathwish:register_as_mutator({
enable_after_these = {
"mutation"
},
enable_before_these = { enable_before_these = {
"mutator555", "mutator555",
"mutator3" "mutator3",
"mutation"
} }
}) })
mutator2:create_options({}, true, "mutator2", "mutator2 description") deathwish:create_options({}, true, "deathwish", "deathwish description")
mutator2.on_enabled = function() end deathwish.on_enabled = function()
mutator2.on_disabled = function() end print(tostring(Breeds.skaven_gutter_runner == Breeds.skaven_pack_master))
end
deathwish.on_disabled = function() end
mutator1:register_as_mutator({
enable_before_these = { -------------------------------
"mutator2" local breeds
mutation:register_as_mutator({
enable_after_these = {
"deathwish"
} }
}) })
mutator1:create_options({}, true, "mutator1", "mutator1 description") mutation:create_options({}, true, "mutation", "mutation description")
mutator1.on_enabled = function() end mutation.on_enabled = function()
mutator1.on_disabled = function() end breeds = table.clone(Breeds)
Breeds.skaven_slave = Breeds.skaven_clan_rat
Breeds.skaven_clan_rat = Breeds.skaven_storm_vermin_commander
Breeds.skaven_gutter_runner = Breeds.skaven_rat_ogre
Breeds.skaven_pack_master = Breeds.skaven_rat_ogre
Breeds.skaven_poison_wind_globadier = Breeds.skaven_rat_ogre
Breeds.skaven_ratling_gunner = Breeds.skaven_rat_ogre
end
mutation.on_disabled = function(initial)
if not initial then Breeds = breeds end
end
-------------------------------
mutator3:register_as_mutator({ mutator3:register_as_mutator({
enable_before_these = { enable_before_these = {