From d2ee5b254ace004e70e8ec907e05243a611e170e Mon Sep 17 00:00:00 2001 From: MewMew Date: Fri, 29 Nov 2019 05:56:24 +0100 Subject: [PATCH] map settings tab for bb --- maps/biter_battles_v2/config.lua | 8 -- maps/biter_battles_v2/difficulty_vote.lua | 6 +- maps/biter_battles_v2/gui.lua | 2 +- maps/biter_battles_v2/main.lua | 22 +++- maps/biter_battles_v2/map_settings_tab.lua | 121 +++++++++++++++++++++ 5 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 maps/biter_battles_v2/map_settings_tab.lua diff --git a/maps/biter_battles_v2/config.lua b/maps/biter_battles_v2/config.lua index d33b9371..fbb7aee2 100644 --- a/maps/biter_battles_v2/config.lua +++ b/maps/biter_battles_v2/config.lua @@ -1,18 +1,10 @@ --BITER BATTLES CONFIG-- local bb_config = { - --TEAM SETTINGS-- - ["team_balancing"] = true, --Should players only be able to join a team that has less or equal members than the opposing team? - ["only_admins_vote"] = false, --Are only admins able to vote on the global difficulty? - --Optional custom team names, can also be modified via "Team Manager" ["north_side_team_name"] = "Team North", ["south_side_team_name"] = "Team South", - --GENERAL SETTINGS-- - ["blueprint_library_importing"] = false, --Allow the importing of blueprints from the blueprint library? - ["blueprint_string_importing"] = false, --Allow the importing of blueprints via blueprint strings? - --MAP PREGENERATION-- ["map_pregeneration_radius"] = 0, --3 horizontal radiuses in chunks to pregenerate at the start of the map. ["on_init_pregen"] = false, --Generate some chunks on_init? diff --git a/maps/biter_battles_v2/difficulty_vote.lua b/maps/biter_battles_v2/difficulty_vote.lua index 69adc9d5..4d7a2809 100644 --- a/maps/biter_battles_v2/difficulty_vote.lua +++ b/maps/biter_battles_v2/difficulty_vote.lua @@ -29,7 +29,7 @@ end local function poll_difficulty(player) if player.gui.center["difficulty_poll"] then player.gui.center["difficulty_poll"].destroy() return end - if bb_config.only_admins_vote or global.tournament_mode then + if global.bb_settings.only_admins_vote or global.tournament_mode then if not player.admin then return end end @@ -86,7 +86,7 @@ local function on_player_joined_game(event) local player = game.players[event.player_index] if game.tick < timeout then if not global.difficulty_player_votes[player.name] then - if bb_config.only_admins_vote or global.tournament_mode then + if global.bb_settings.only_admins_vote or global.tournament_mode then if player.admin then poll_difficulty(player) end else poll_difficulty(player) @@ -122,7 +122,7 @@ local function on_gui_click(event) if game.tick > timeout then event.element.parent.destroy() return end local i = tonumber(event.element.name) - if bb_config.only_admins_vote or global.tournament_mode then + if global.bb_settings.only_admins_vote or global.tournament_mode then if player.admin then game.print(player.name .. " has voted for " .. difficulties[i].name .. " difficulty!", difficulties[i].print_color) global.difficulty_player_votes[player.name] = i diff --git a/maps/biter_battles_v2/gui.lua b/maps/biter_battles_v2/gui.lua index 27ab22e5..31023321 100644 --- a/maps/biter_battles_v2/gui.lua +++ b/maps/biter_battles_v2/gui.lua @@ -285,7 +285,7 @@ function join_team(player, force_name, forced_join) local enemy_team = "south" if force_name == "south" then enemy_team = "north" end - if not global.training_mode and bb_config.team_balancing then + if not global.training_mode and global.bb_settings.team_balancing then if not forced_join then if #game.forces[force_name].connected_players > #game.forces[enemy_team].connected_players then if not global.chosen_team[player.name] then diff --git a/maps/biter_battles_v2/main.lua b/maps/biter_battles_v2/main.lua index e1bb201a..5567351d 100644 --- a/maps/biter_battles_v2/main.lua +++ b/maps/biter_battles_v2/main.lua @@ -1,12 +1,25 @@ -- Biter Battles v2 -- by MewMew -require "on_tick_schedule" local bb_config = require "maps.biter_battles_v2.config" +require "on_tick_schedule" +require "maps.biter_battles_v2.map_settings_tab" require "modules.spawners_contain_biters" require "modules.mineable_wreckage_yields_scrap" local event = require 'utils.event' +local function init_settings() + global.bb_settings = { + --TEAM SETTINGS-- + ["team_balancing"] = true, --Should players only be able to join a team that has less or equal members than the opposing team? + ["only_admins_vote"] = false, --Are only admins able to vote on the global difficulty? + + --GENERAL SETTINGS-- + ["blueprint_library_importing"] = false, --Allow the importing of blueprints from the blueprint library? + ["blueprint_string_importing"] = false, --Allow the importing of blueprints via blueprint strings? + } +end + local function init_surface() local map_gen_settings = {} map_gen_settings.water = math.random(15, 65) * 0.01 @@ -88,10 +101,10 @@ local function init_forces() f.set_cease_fire('south', true) f.share_chart = false - if not bb_config.blueprint_library_importing then + if not global.bb_settings.blueprint_library_importing then game.permissions.get_group("Default").set_allows_action(defines.input_action.grab_blueprint_record, false) end - if not bb_config.blueprint_string_importing then + if not global.bb_settings.blueprint_string_importing then game.permissions.get_group("Default").set_allows_action(defines.input_action.import_blueprint_string, false) game.permissions.get_group("Default").set_allows_action(defines.input_action.import_blueprint, false) end @@ -179,7 +192,8 @@ local function on_player_joined_game(event) end end -local function on_init(surface) +local function on_init() + init_settings() init_surface() init_forces() global.gui_refresh_delay = 0 diff --git a/maps/biter_battles_v2/map_settings_tab.lua b/maps/biter_battles_v2/map_settings_tab.lua new file mode 100644 index 00000000..a733f55b --- /dev/null +++ b/maps/biter_battles_v2/map_settings_tab.lua @@ -0,0 +1,121 @@ +-- config tab -- + +local Tabs = require 'comfy_panel.main' + +local functions = { + ["map_settings_blueprint_toggle"] = function(event) + if event.element.switch_state == "left" then + game.permissions.get_group("Default").set_allows_action(defines.input_action.grab_blueprint_record, true) + game.permissions.get_group("Default").set_allows_action(defines.input_action.import_blueprint_string, true) + game.permissions.get_group("Default").set_allows_action(defines.input_action.import_blueprint, true) + global.bb_settings.blueprint_library_importing = true + game.print("The blueprint library has been enabled!") + else + game.permissions.get_group("Default").set_allows_action(defines.input_action.grab_blueprint_record, false) + game.permissions.get_group("Default").set_allows_action(defines.input_action.import_blueprint_string, false) + game.permissions.get_group("Default").set_allows_action(defines.input_action.import_blueprint, false) + global.bb_settings.blueprint_library_importing = false + game.print("The blueprint library has been disabled!") + end + end, + + ["map_settings_team_balancing_toggle"] = function(event) + if event.element.switch_state == "left" then + global.bb_settings.team_balancing = true + game.print("Team balancing has been enabled!") + else + global.bb_settings.team_balancing = false + game.print("Team balancing has been disabled!") + end + end, + + ["map_settings_only_admins_vote"] = function(event) + if event.element.switch_state == "left" then + global.bb_settings.only_admins_vote = true + global.difficulty_player_votes = {} + game.print("Admin-only difficulty voting has been enabled!") + else + global.bb_settings.only_admins_vote = false + game.print("Admin-only difficulty voting has been disabled!") + end + end, +} + +local function add_switch(element, switch_state, name, description_main, description) + local t = element.add({type = "table", column_count = 5}) + local label = t.add({type = "label", caption = "ON"}) + label.style.padding = 0 + label.style.left_padding= 10 + label.style.font_color = {0.77, 0.77, 0.77} + local switch = t.add({type = "switch", name = name}) + switch.switch_state = switch_state + switch.style.padding = 0 + switch.style.margin = 0 + local label = t.add({type = "label", caption = "OFF"}) + label.style.padding = 0 + label.style.font_color = {0.70, 0.70, 0.70} + + local label = t.add({type = "label", caption = description_main}) + label.style.padding = 2 + label.style.left_padding= 10 + label.style.minimal_width = 120 + label.style.font = "heading-2" + label.style.font_color = {0.88, 0.88, 0.99} + + local label = t.add({type = "label", caption = description}) + label.style.padding = 2 + label.style.left_padding= 10 + label.style.single_line = false + label.style.font = "heading-3" + label.style.font_color = {0.85, 0.85, 0.85} + + return switch +end + +local build_config_gui = (function (player, frame) + frame.clear() + + local admin = player.admin + local line_elements = {} + local switch_label_elements = {} + local label_elements = {} + + line_elements[#line_elements + 1] = frame.add({type = "line"}) + + local switch_state = "right" + if global.bb_settings.blueprint_library_importing then switch_state = "left" end + local switch = add_switch(frame, switch_state, "map_settings_blueprint_toggle", "Blueprints", "Enables or disables the usage of blueprint strings and the library.") + if not admin then switch.ignored_by_interaction = true end + + line_elements[#line_elements + 1] = frame.add({type = "line"}) + + local switch_state = "right" + if global.bb_settings.team_balancing then switch_state = "left" end + local switch = add_switch(frame, switch_state, "map_settings_team_balancing_toggle", "Team Balancing", "Players can only join a team that has less or equal players than the opposing.") + if not admin then switch.ignored_by_interaction = true end + + line_elements[#line_elements + 1] = frame.add({type = "line"}) + + local switch_state = "right" + if global.bb_settings.only_admins_vote then switch_state = "left" end + local switch = add_switch(frame, switch_state, "map_settings_only_admins_vote", "Admin Vote", "Only admins can vote for map difficulty. Clears all currently existing votes.") + if not admin then switch.ignored_by_interaction = true end + + line_elements[#line_elements + 1] = frame.add({type = "line"}) + +end) + +local function on_gui_switch_state_changed(event) + if not event.element then return end + if not event.element.valid then return end + if functions[event.element.name] then + functions[event.element.name](event) + return + end +end + +comfy_panel_tabs["MapSettings"] = build_config_gui + + +local event = require 'utils.event' +event.add(defines.events.on_gui_switch_state_changed, on_gui_switch_state_changed) \ No newline at end of file