From 3183e6a69a80bff5743b5dd8185957d130fa2e11 Mon Sep 17 00:00:00 2001 From: MewMew Date: Wed, 8 May 2019 15:38:53 +0200 Subject: [PATCH] spectator mode --- maps/wave_of_death/WoD.lua | 49 ++++++++++++++++++++++++++++++++- maps/wave_of_death/init.lua | 29 +++++++++++++++++++ maps/wave_of_death/spectate.lua | 7 +++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 maps/wave_of_death/spectate.lua diff --git a/maps/wave_of_death/WoD.lua b/maps/wave_of_death/WoD.lua index ecadd790..9d956530 100644 --- a/maps/wave_of_death/WoD.lua +++ b/maps/wave_of_death/WoD.lua @@ -19,6 +19,28 @@ function soft_teleport(player, destination) player.teleport(pos, surface) end +local function spectate_button(player) + if player.gui.top.spectate_button then return end + local button = player.gui.top.add({type = "button", name = "spectate_button", caption = "Spectate"}) + button.style.font = "default-bold" + button.style.font_color = {r = 0.0, g = 0.0, b = 0.0} + button.style.minimal_height = 38 + button.style.minimal_width = 38 + button.style.top_padding = 2 + button.style.left_padding = 4 + button.style.right_padding = 4 + button.style.bottom_padding = 2 +end + +local function create_spectate_confirmation(player) + if player.gui.center.spectate_confirmation_frame then return end + local frame = player.gui.center.add({type = "frame", name = "spectate_confirmation_frame", caption = "Are you sure you want to spectate? This can not be undone."}) + frame.style.font = "default" + frame.style.font_color = {r = 0.3, g = 0.65, b = 0.3} + frame.add({type = "button", name = "confirm_spectate", caption = "Spectate"}) + frame.add({type = "button", name = "cancel_spectate", caption = "Cancel"}) +end + local function autojoin_lane(player) local lowest_player_count = 256 local lane_number = 1 @@ -38,8 +60,9 @@ end local function on_player_joined_game(event) init() - + local player = game.players[event.player_index] + spectate_button(player) if player.online_time == 0 then autojoin_lane(player) return end if global.wod_lane[tonumber(player.force.name)].game_lost == true then @@ -71,6 +94,30 @@ local function on_tick(event) game_status.restart_server() end +local function on_gui_click(event) + if not event then return end + if not event.element then return end + if not event.element.valid then return end + local player = game.players[event.element.player_index] + if event.element.name == "cancel_spectate" then player.gui.center["spectate_confirmation_frame"].destroy() return end + if event.element.name == "confirm_spectate" then + player.gui.center["spectate_confirmation_frame"].destroy() + game.permissions.get_group("spectator").add_player(player) + player.force = game.forces.player + if player.character then player.character.die() end + return + end + if event.element.name == "spectate_button" then + if player.gui.center["spectate_confirmation_frame"] then + player.gui.center["spectate_confirmation_frame"].destroy() + else + create_spectate_confirmation(player) + end + return + end +end + +event.add(defines.events.on_gui_click, on_gui_click) event.add(defines.events.on_tick, on_tick) event.add(defines.events.on_chunk_generated, on_chunk_generated) event.add(defines.events.on_entity_damaged, on_entity_damaged) diff --git a/maps/wave_of_death/init.lua b/maps/wave_of_death/init.lua index de7c8355..ca44742c 100644 --- a/maps/wave_of_death/init.lua +++ b/maps/wave_of_death/init.lua @@ -20,6 +20,7 @@ local function init_surface() game.map_settings.enemy_evolution.pollution_factor = 0 game.map_settings.pollution.enabled = false game.map_settings.enemy_expansion.enabled = false + game.difficulty_settings.technology_price_multiplier = 0.8 return surface end @@ -47,6 +48,34 @@ local function init_forces(surface) end end end + + for i = 1, 4, 1 do + game.forces[i].set_friend("player", true) + game.forces["player"].set_friend(game.forces[i].name, true) + end + game.forces["player"].set_spawn_position({x = 32, y = 0}, surface) + + local p = game.permissions.create_group("spectator") + for action_name, _ in pairs(defines.input_action) do + p.set_allows_action(defines.input_action[action_name], false) + end + local defs = { + defines.input_action.write_to_console, + defines.input_action.gui_click, + defines.input_action.gui_selection_state_changed, + defines.input_action.gui_checked_state_changed , + defines.input_action.gui_elem_changed, + defines.input_action.gui_text_changed, + defines.input_action.gui_value_changed, + defines.input_action.start_walking, + defines.input_action.open_kills_gui, + defines.input_action.open_character_gui, + defines.input_action.edit_permission_group, + defines.input_action.toggle_show_entity_info, + defines.input_action.rotate_entity, + defines.input_action.start_research + } + for _, d in pairs(defs) do p.set_allows_action(d, true) end end local function init_globals() diff --git a/maps/wave_of_death/spectate.lua b/maps/wave_of_death/spectate.lua new file mode 100644 index 00000000..250c3bb8 --- /dev/null +++ b/maps/wave_of_death/spectate.lua @@ -0,0 +1,7 @@ +local function spectate_button() +end + +local function on_gui_click() +end + +event.add(defines.events.on_gui_click, on_gui_click)