mutators: comments; split tests

This commit is contained in:
UnShame 2018-02-22 02:25:31 +03:00
parent bc4377a4c3
commit d581e0a9ce
6 changed files with 149 additions and 96 deletions

View file

@ -1,11 +1,15 @@
--[[ Add additional dice to end game roll --]]
local manager = get_mod("vmf_mutator_manager") local manager = get_mod("vmf_mutator_manager")
-- List of all die types
local missions = { local missions = {
"bonus_dice_hidden_mission", "bonus_dice_hidden_mission",
"tome_bonus_mission", "tome_bonus_mission",
"grimoire_hidden_mission" "grimoire_hidden_mission"
} }
-- Amounts of additional dice to be added at level completion
local num_dice_per_mission = { local num_dice_per_mission = {
bonus_dice_hidden_mission = 0, bonus_dice_hidden_mission = 0,
tome_bonus_mission = 0, tome_bonus_mission = 0,
@ -18,6 +22,7 @@ manager:hook("GameModeManager.complete_level", function(func, self)
local mission_system = Managers.state.entity:system("mission_system") local mission_system = Managers.state.entity:system("mission_system")
local active_mission = mission_system.active_missions local active_mission = mission_system.active_missions
-- Add additional dice
for _, mission in ipairs(missions) do for _, mission in ipairs(missions) do
for _ = 1, num_dice_per_mission[mission] do for _ = 1, num_dice_per_mission[mission] do
mission_system:request_mission(mission, nil, Network.peer_id()) mission_system:request_mission(mission, nil, Network.peer_id())
@ -25,12 +30,14 @@ manager:hook("GameModeManager.complete_level", function(func, self)
end end
end end
-- Get total number of dice
for name, obj in pairs(active_mission) do for name, obj in pairs(active_mission) do
if table.has_item(missions, name) then if table.has_item(missions, name) then
num_dice = num_dice + obj.current_amount num_dice = num_dice + obj.current_amount
end end
end end
-- Remove excess dice
for _, mission in ipairs(missions) do for _, mission in ipairs(missions) do
if active_mission[mission] then if active_mission[mission] then
for _ = 1, active_mission[mission].current_amount do for _ = 1, active_mission[mission].current_amount do
@ -47,6 +54,7 @@ manager:hook("GameModeManager.complete_level", function(func, self)
return func(self) return func(self)
end) end)
-- Adds/remove dice
local function adjustDice(grims, tomes, bonus, multiplier) local function adjustDice(grims, tomes, bonus, multiplier)
if grims then num_dice_per_mission.grimoire_hidden_mission = num_dice_per_mission.grimoire_hidden_mission + grims * multiplier end if grims then num_dice_per_mission.grimoire_hidden_mission = num_dice_per_mission.grimoire_hidden_mission + grims * multiplier end
if tomes then num_dice_per_mission.tome_bonus_mission = num_dice_per_mission.tome_bonus_mission + tomes * multiplier end if tomes then num_dice_per_mission.tome_bonus_mission = num_dice_per_mission.tome_bonus_mission + tomes * multiplier end

View file

@ -1,3 +1,5 @@
--[[ Add mutators panel to the map view --]]
local manager = get_mod("vmf_mutator_manager") local manager = get_mod("vmf_mutator_manager")
local mutators = manager.mutators local mutators = manager.mutators
@ -149,28 +151,40 @@ local mutators_view = {
end end
end, end,
-- Sets appropriate text and style to checkboxes, hides/shows them as needed
update_checkboxes = function(self) update_checkboxes = function(self)
local widgets = self.map_view.normal_settings_widget_types local widgets = self.map_view.normal_settings_widget_types
for i = 1, PER_PAGE do for i = 1, PER_PAGE do
local current_index = PER_PAGE * (self.current_page - 1) + i local current_index = PER_PAGE * (self.current_page - 1) + i
local checkbox = self.mutator_checkboxes[i] local checkbox = self.mutator_checkboxes[i]
local hotspot = checkbox.content.button_hotspot local hotspot = checkbox.content.button_hotspot
-- Hide if fewer mutators shown than there are checkboxes
if #self.mutators_sorted < current_index then if #self.mutators_sorted < current_index then
checkbox.content.setting_text = "" checkbox.content.setting_text = ""
checkbox.content.tooltip_text = "" checkbox.content.tooltip_text = ""
-- Remove from render lists
widgets.adventure["mutator_checkbox_" .. i] = nil widgets.adventure["mutator_checkbox_" .. i] = nil
widgets.survival["mutator_checkbox_" .. i] = nil widgets.survival["mutator_checkbox_" .. i] = nil
else else
local mutator_info = self.mutators_sorted[current_index] local mutator_info = self.mutators_sorted[current_index]
local mutator = get_mod(mutator_info[1]) local mutator = get_mod(mutator_info[1])
-- Set text and tooltip
checkbox.content.setting_text = mutator_info[2] checkbox.content.setting_text = mutator_info[2]
checkbox.content.tooltip_text = self:generate_tooltip_for(mutator) checkbox.content.tooltip_text = self:generate_tooltip_for(mutator)
-- Add to render lists
widgets.adventure["mutator_checkbox_" .. i] = checkbox widgets.adventure["mutator_checkbox_" .. i] = checkbox
widgets.survival["mutator_checkbox_" .. i] = checkbox widgets.survival["mutator_checkbox_" .. i] = checkbox
-- Set colors based on whether mutator can be enabled
local active = mutator:can_be_enabled() local active = mutator:can_be_enabled()
local color = active and "cheeseburger" or "slate_gray" local color = active and "cheeseburger" or "slate_gray"
local color_hover = active and "white" or "slate_gray" local color_hover = active and "white" or "slate_gray"
@ -178,10 +192,12 @@ local mutators_view = {
checkbox.style.setting_text_hover.text_color = Colors.get_color_table_with_alpha(color_hover, 255) checkbox.style.setting_text_hover.text_color = Colors.get_color_table_with_alpha(color_hover, 255)
checkbox.style.checkbox_style.color = Colors.get_color_table_with_alpha(color_hover, 255) checkbox.style.checkbox_style.color = Colors.get_color_table_with_alpha(color_hover, 255)
-- Sound on hover
if hotspot.on_hover_enter then if hotspot.on_hover_enter then
self.map_view:play_sound("Play_hud_hover") self.map_view:play_sound("Play_hud_hover")
end end
-- Click event
if hotspot.on_release then if hotspot.on_release then
self.map_view:play_sound("Play_hud_hover") self.map_view:play_sound("Play_hud_hover")
if mutator:is_enabled() then if mutator:is_enabled() then
@ -190,11 +206,13 @@ local mutators_view = {
mutator:enable() mutator:enable()
end end
end end
checkbox.content.selected = mutator:is_enabled() checkbox.content.selected = mutator:is_enabled()
end end
end end
end, end,
-- Activate on button click or map open
activate = function(self) activate = function(self)
if not self.initialized or not self.map_view.active or self.active then return end if not self.initialized or not self.map_view.active or self.active then return end
@ -225,6 +243,7 @@ local mutators_view = {
--print("ACTIVE!") --print("ACTIVE!")
end, end,
-- Deactivate on button click or map close
deactivate = function(self) deactivate = function(self)
if not self.initialized or not self.active then return end if not self.initialized or not self.active then return end
@ -258,6 +277,7 @@ local mutators_view = {
--print("DEACTIVE") --print("DEACTIVE")
end, end,
-- Changes which muttators are displayed
on_mutators_page_change = function(self, index_change) on_mutators_page_change = function(self, index_change)
if not self.initialized then return end if not self.initialized then return end
@ -278,11 +298,14 @@ local mutators_view = {
end end
end, end,
-- Creates and return text for checkbox tooltip
generate_tooltip_for = function(self, mutator) generate_tooltip_for = function(self, mutator)
-- Description
local config = mutator:get_config() local config = mutator:get_config()
local text = config.description local text = config.description
local supports_difficulty = mutator:supports_current_difficulty()
-- Show supported difficulty when can't be enabled due to difficulty level
local supports_difficulty = mutator:supports_current_difficulty()
if not supports_difficulty then if not supports_difficulty then
text = text .. "\nSupported difficulty levels:" text = text .. "\nSupported difficulty levels:"
for i, difficulty in ipairs(config.difficulty_levels) do for i, difficulty in ipairs(config.difficulty_levels) do
@ -290,13 +313,19 @@ local mutators_view = {
end end
end end
-- Show enabled incompatible
local incompatible_mutators = mutator:get_incompatible_mutators(true) local incompatible_mutators = mutator:get_incompatible_mutators(true)
local currently_compatible = #incompatible_mutators == 0 local currently_compatible = #incompatible_mutators == 0
-- Or all incompatible if difficulty is compatible
if supports_difficulty and #incompatible_mutators == 0 then if supports_difficulty and #incompatible_mutators == 0 then
incompatible_mutators = mutator:get_incompatible_mutators() incompatible_mutators = mutator:get_incompatible_mutators()
end end
if #incompatible_mutators > 0 then if #incompatible_mutators > 0 then
if currently_compatible and config.incompatible_with_all or #incompatible_mutators == #mutators - 1 then if currently_compatible and config.incompatible_with_all or #incompatible_mutators == #mutators - 1 then
-- Show special message when incompatible with all
text = text .. "\nIncompatible with all other mutators" text = text .. "\nIncompatible with all other mutators"
else else
text = text .. "\nIncompatible with:" text = text .. "\nIncompatible with:"
@ -305,10 +334,13 @@ local mutators_view = {
text = text .. (i == 1 and " " or ", ") .. name text = text .. (i == 1 and " " or ", ") .. name
end end
end end
elseif config.compatible_with_all then elseif config.compatible_with_all then
-- Special message when compatible with all
text = text .. "\nCompatible with all other mutators" text = text .. "\nCompatible with all other mutators"
end end
-- Special message if switched to unsupported difficulty level
if mutator:is_enabled() and not supports_difficulty then if mutator:is_enabled() and not supports_difficulty then
text = text .. "\nWill be disabled when Play is pressed" text = text .. "\nWill be disabled when Play is pressed"
end end
@ -383,4 +415,16 @@ local mutators_view = {
end end
} }
-- Initialize mutators view after map view
manager:hook("MapView.init", function(func, self, ...)
func(self, ...)
manager:pcall(function() mutators_view:init(self) end)
end)
-- Destroy mutators view after map view
manager:hook("MapView.destroy", function(func, ...)
mutators_view:deinitialize()
func(...)
end)
return mutators_view return mutators_view

View file

@ -1,8 +1,10 @@
local definitions = local_require("scripts/ui/views/map_view_definitions") local definitions = local_require("scripts/ui/views/map_view_definitions")
local scenegraph_definition = definitions.scenegraph_definition local scenegraph_definition = definitions.scenegraph_definition
-- Mutators to show per page
definitions.PER_PAGE = 6 definitions.PER_PAGE = 6
-- Button to toggle mutators view
scenegraph_definition.mutators_button = { scenegraph_definition.mutators_button = {
vertical_alignment = "bottom", vertical_alignment = "bottom",
parent = "banner_party", parent = "banner_party",
@ -17,6 +19,8 @@ scenegraph_definition.mutators_button = {
1 1
} }
} }
-- This will replace the Mission text
scenegraph_definition.banner_mutators_text = { scenegraph_definition.banner_mutators_text = {
vertical_alignment = "center", vertical_alignment = "center",
parent = "banner_level", parent = "banner_level",
@ -34,30 +38,35 @@ scenegraph_definition.banner_mutators_text = {
local new_widgets = { local new_widgets = {
-- This will replace the banner behind the Mission text
banner_mutators_widget = UIWidgets.create_texture_with_text_and_tooltip("title_bar", "Mutators", "Enable and disable mutators", "banner_level", "banner_mutators_text", { banner_mutators_widget = UIWidgets.create_texture_with_text_and_tooltip("title_bar", "Mutators", "Enable and disable mutators", "banner_level", "banner_mutators_text", {
vertical_alignment = "center", vertical_alignment = "center",
scenegraph_id = "banner_mutators_text", scenegraph_id = "banner_mutators_text",
localize = false, localize = false,
font_size = 28, font_size = 28,
horizontal_alignment = "center", horizontal_alignment = "center",
font_type = "hell_shark", font_type = "hell_shark",
text_color = Colors.get_color_table_with_alpha("cheeseburger", 255) text_color = Colors.get_color_table_with_alpha("cheeseburger", 255)
}, },
{ {
font_size = 24, font_size = 24,
max_width = 500, max_width = 500,
localize = false, localize = false,
horizontal_alignment = "left", horizontal_alignment = "left",
vertical_alignment = "top", vertical_alignment = "top",
font_type = "hell_shark", font_type = "hell_shark",
text_color = Colors.get_color_table_with_alpha("white", 255), text_color = Colors.get_color_table_with_alpha("white", 255),
line_colors = {}, line_colors = {},
offset = { offset = {
0, 0,
0, 0,
50 50
}
} }
}), ),
-- Button to toggle mutators view
mutators_button_widget = { mutators_button_widget = {
element = UIElements.ToggleIconButton, element = UIElements.ToggleIconButton,
content = { content = {
@ -172,6 +181,7 @@ local new_widgets = {
} }
} }
-- Checkboxes
for i = 1, definitions.PER_PAGE do for i = 1, definitions.PER_PAGE do
new_widgets["mutator_checkbox_" .. i] = { new_widgets["mutator_checkbox_" .. i] = {
scenegraph_id = "mutator_checkbox_" .. i, scenegraph_id = "mutator_checkbox_" .. i,

View file

@ -1,16 +1,19 @@
--[[ Notify players of enabled mutators via chat and tab menu --]]
local manager = get_mod("vmf_mutator_manager") local manager = get_mod("vmf_mutator_manager")
local mutators = manager.mutators local mutators = manager.mutators
local were_enabled_before = false local were_enabled_before = false
local function get_enabled_mutators_names(short) -- Assembles a list of enabled mutators
local function get_enabled_mutators_names(separator, short)
local name = nil local name = nil
for _, mutator in ipairs(mutators) do for _, mutator in ipairs(mutators) do
local config = mutator:get_config() local config = mutator:get_config()
if mutator:is_enabled() then if mutator:is_enabled() then
local added_name = (short and config.short_title or config.title or mutator:get_name()) local added_name = (short and config.short_title or config.title or mutator:get_name())
if name then if name then
name = name .. " " .. added_name name = name .. separator .. added_name
else else
name = added_name name = added_name
end end
@ -19,11 +22,12 @@ local function get_enabled_mutators_names(short)
return name return name
end end
-- Sets the lobby name
local function set_lobby_data() local function set_lobby_data()
if not Managers.matchmaking then return end if not Managers.matchmaking then return end
local name = get_enabled_mutators_names(true) local name = get_enabled_mutators_names(" ", true)
local default_name = LobbyAux.get_unique_server_name() local default_name = LobbyAux.get_unique_server_name()
if name then if name then
@ -38,6 +42,8 @@ local function set_lobby_data()
Managers.matchmaking.lobby:set_lobby_data(lobby_data) Managers.matchmaking.lobby:set_lobby_data(lobby_data)
end end
-- Return a function for chat system to only send messages to specific client
-- TODO: test if this works
local function get_member_func(client_cookie) local function get_member_func(client_cookie)
local peer_id = tostring(client_cookie) local peer_id = tostring(client_cookie)
for _ = 1, 3 do for _ = 1, 3 do
@ -58,19 +64,21 @@ local function get_member_func(client_cookie)
end end
end end
-- Set difficulty in the tab menu
-- TODO: see if this can be set every time a mutator is enabled/disable
manager:hook("IngamePlayerListUI.set_difficulty_name", function(func, self, name) manager:hook("IngamePlayerListUI.set_difficulty_name", function(func, self, name)
local mutators_name = get_enabled_mutators_names(true) local mutators_name = get_enabled_mutators_names(" ", true)
if mutators_name then if mutators_name then
name = name .. " " .. mutators_name name = name .. " " .. mutators_name
end end
self.headers.content.game_difficulty = name self.headers.content.game_difficulty = name
end) end)
-- Notify everybody about enabled/disabled mutators when Play button is pressed on the map screen
manager:hook("MatchmakingStateHostGame.host_game", function(func, self, ...) manager:hook("MatchmakingStateHostGame.host_game", function(func, self, ...)
func(self, ...) func(self, ...)
set_lobby_data() set_lobby_data()
local names = get_enabled_mutators_names() local names = get_enabled_mutators_names(", ")
if names then if names then
Managers.chat:send_system_chat_message(1, "ENABLED MUTATORS: " .. names, 0, true) Managers.chat:send_system_chat_message(1, "ENABLED MUTATORS: " .. names, 0, true)
were_enabled_before = true were_enabled_before = true
@ -80,8 +88,10 @@ manager:hook("MatchmakingStateHostGame.host_game", function(func, self, ...)
end end
end) end)
-- Send special messages with enabled mutators list to players just joining the lobby
-- TODO: test if this works
manager:hook("MatchmakingManager.rpc_matchmaking_request_join_lobby", function(func, self, sender, client_cookie, host_cookie, lobby_id, friend_join) manager:hook("MatchmakingManager.rpc_matchmaking_request_join_lobby", function(func, self, sender, client_cookie, host_cookie, lobby_id, friend_join)
local name = get_enabled_mutators_names() local name = get_enabled_mutators_names(", ")
if name then if name then
local message = "[Automated message] This lobby has the following difficulty mod active : " .. name local message = "[Automated message] This lobby has the following difficulty mod active : " .. name
manager:hook("Managers.chat.channels[1].members_func", get_member_func(client_cookie)) manager:hook("Managers.chat.channels[1].members_func", get_member_func(client_cookie))
@ -91,4 +101,4 @@ manager:hook("MatchmakingManager.rpc_matchmaking_request_join_lobby", function(f
func(self, sender, client_cookie, host_cookie, lobby_id, friend_join) func(self, sender, client_cookie, host_cookie, lobby_id, friend_join)
end) end)
return set_lobby_data return set_lobby_data

View file

@ -1,3 +1,5 @@
--[[ Add ability to turn mods into mutators --]]
local manager = new_mod("vmf_mutator_manager") local manager = new_mod("vmf_mutator_manager")
manager:localization("localization/mutator_manager") manager:localization("localization/mutator_manager")
@ -103,8 +105,8 @@ manager.disable_impossible_mutators = function(notify, everybody)
end end
if #disabled_mutators > 0 and notify then if #disabled_mutators > 0 and notify then
local message = everybody and "MUTATORS DISABLED DUE TO DIFFICULTY CHANGE:" or "Mutators disabled due to difficulty change:" local message = everybody and "MUTATORS DISABLED DUE TO DIFFICULTY CHANGE:" or "Mutators disabled due to difficulty change:"
for _, mutator in ipairs(disabled_mutators) do for i, mutator in ipairs(disabled_mutators) do
message = message .. " " .. (mutator:get_config().title or mutator:get_name()) message = message .. (i == 1 and " " or ", ") .. (mutator:get_config().title or mutator:get_name())
end end
if everybody then if everybody then
Managers.chat:send_system_chat_message(1, message, 0, true) Managers.chat:send_system_chat_message(1, message, 0, true)
@ -275,14 +277,14 @@ local function disable_mutator(self)
manager:pcall(function() set_mutator_state(self, false) end) manager:pcall(function() set_mutator_state(self, false) end)
end end
-- Checks current difficulty and map selection screen settings to determine if a mutator can be enabled -- Checks current difficulty, map selection screen settings (optionally) and incompatible mutators to determine if a mutator can be enabled
local function can_be_enabled(self, ignore_map) local function can_be_enabled(self, ignore_map)
if #self:get_incompatible_mutators(true) > 0 then return false end if #self:get_incompatible_mutators(true) > 0 then return false end
return self:supports_current_difficulty(ignore_map) return self:supports_current_difficulty(ignore_map)
end end
-- Only checks difficulty
local function supports_current_difficulty(self, ignore_map) local function supports_current_difficulty(self, ignore_map)
local mutator_difficulty_levels = self:get_config().difficulty_levels local mutator_difficulty_levels = self:get_config().difficulty_levels
local actual_difficulty = Managers.state and Managers.state.difficulty:get_difficulty() local actual_difficulty = Managers.state and Managers.state.difficulty:get_difficulty()
@ -308,6 +310,7 @@ local function get_config(self)
return mutators_config[self:get_name()] return mutators_config[self:get_name()]
end end
-- Returns a list of incompatible with self mutators, all or only enabled ones
local function get_incompatible_mutators(self, enabled_only) local function get_incompatible_mutators(self, enabled_only)
local incompatible_mutators = {} local incompatible_mutators = {}
for _, other_mutator in ipairs(mutators) do for _, other_mutator in ipairs(mutators) do
@ -382,71 +385,14 @@ manager:hook("DifficultyManager.set_difficulty", function(func, self, difficulty
end) end)
-- Initialize mutators view after map view --[[
manager:hook("MapView.init", function(func, self, ...) INITIALIZE
func(self, ...) --]]
manager:pcall(function() mutators_view:init(self) end)
end)
-- Destroy mutators view after map view
manager:hook("MapView.destroy", function(func, ...)
mutators_view:deinitialize()
func(...)
end)
-- Initialize mutators view when map_view has been initialized already -- Initialize mutators view when map_view has been initialized already
manager:pcall(function() mutators_view:init(mutators_view:get_map_view()) end) manager:pcall(function() mutators_view:init(mutators_view:get_map_view()) end)
--[[ --[[
Testing Testing
--]] --]]
local mutator2 = new_mod("mutator2") manager:dofile("scripts/mods/vmf/modules/mutators/mutator_test")
local mutator3 = new_mod("mutator3")
local mutator555 = new_mod("mutator555")
mutator555:register_as_mutator({
incompatible_with_all = true
})
mutator555:create_options({}, true, "mutator555", "mutator555 description")
mutator555.on_enabled = function() end
mutator555.on_disabled = function() end
mutator3:register_as_mutator({
incompatible_with = {
"mutator4"
}
})
mutator3.on_enabled = function() end
mutator3.on_disabled = function() end
mutator2:register_as_mutator({
compatible_with_all = true,
difficulty_levels = {
"hardest"
}
})
mutator2.on_enabled = function() end
mutator2.on_disabled = function() end
--[[for i=4,17 do
local mutator = new_mod("mutator" .. i)
mutator:register_as_mutator({})
mutator.on_enabled = function() end
mutator.on_disabled = function() end
end--]]

View file

@ -0,0 +1,35 @@
local mutator2 = new_mod("mutator2")
local mutator3 = new_mod("mutator3")
local mutator555 = new_mod("mutator555")
mutator555:register_as_mutator({
incompatible_with_all = true
})
mutator555:create_options({}, true, "mutator555", "mutator555 description")
mutator555.on_enabled = function() end
mutator555.on_disabled = function() end
mutator3:register_as_mutator({
incompatible_with = {
"mutator4"
}
})
mutator3.on_enabled = function() end
mutator3.on_disabled = function() end
mutator2:register_as_mutator({
compatible_with_all = true,
difficulty_levels = {
"hardest"
}
})
mutator2.on_enabled = function() end
mutator2.on_disabled = function() end
--[[for i=4,17 do
local mutator = new_mod("mutator" .. i)
mutator:register_as_mutator({})
mutator.on_enabled = function() end
mutator.on_disabled = function() end
end--]]