From 6eb6574cc4232b0bb5d734700e75cb9d4d9f651a Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Sat, 12 Oct 2019 13:29:11 +0200 Subject: [PATCH] Refactoring + Changes from #996 --- map_gen/maps/space_race/config.lua | 71 ++++ map_gen/maps/space_race/gui/join_gui.lua | 126 ++++++++ map_gen/maps/space_race/gui/load_gui.lua | 90 ++++++ map_gen/maps/space_race/gui/wait_gui.lua | 92 ++++++ map_gen/maps/space_race/gui/won_gui.lua | 89 +++++ map_gen/maps/space_race/lobby.lua | 323 ++---------------- map_gen/maps/space_race/map.lua | 4 - map_gen/maps/space_race/map_info.lua | 30 +- map_gen/maps/space_race/market_handler.lua | 248 ++++++++++++-- map_gen/maps/space_race/market_items.lua | 17 +- map_gen/maps/space_race/scenario.lua | 359 ++++----------------- 11 files changed, 803 insertions(+), 646 deletions(-) create mode 100644 map_gen/maps/space_race/config.lua create mode 100644 map_gen/maps/space_race/gui/join_gui.lua create mode 100644 map_gen/maps/space_race/gui/load_gui.lua create mode 100644 map_gen/maps/space_race/gui/wait_gui.lua create mode 100644 map_gen/maps/space_race/gui/won_gui.lua diff --git a/map_gen/maps/space_race/config.lua b/map_gen/maps/space_race/config.lua new file mode 100644 index 000000000..835fbd653 --- /dev/null +++ b/map_gen/maps/space_race/config.lua @@ -0,0 +1,71 @@ +local Config = { + version = 'v0.2', + players_needed_to_start_game = 4, + bootstrap_period = 60 * 60 * 10, -- 10 minutes + player_kill_reward = 25, + entity_kill_rewards = { + ['default'] = 1, + ['wooden-chest'] = 0, + ['gun-turret'] = 5, + ['laser-turret'] = 10, + ['flamethrower-turret'] = 8, + ['artillery-turret'] = 50, + ['artillery-wagon'] = 30, + ['locomotive'] = 10, + ['cargo-wagon'] = 5, + ['fluid-wagon'] = 5, + ['radar'] = 5 + }, + disabled_research = { + ['military'] = {player = 2, entity = 8}, + ['military-2'] = {player = 6, entity = 24, unlocks = 'military'}, + ['military-3'] = {player = 12, entity = 48, unlocks = 'military-2'}, + ['military-4'] = {player = 24, entity = 96, unlocks = 'military-3'}, + ['stone-walls'] = {player = 2, entity = 8, invert = true}, + ['heavy-armor'] = {player = 12, entity = 48, invert = true}, + ['artillery-shell-range-1'] = nil + }, + disabled_recipes = { + 'tank', + 'rocket-silo' + }, + entity_drop_amount = { + --NEEDS BALANCING! + ['biter-spawner'] = {low = 2, high = 10, chance = 1}, + ['spitter-spawner'] = {low = 2, high = 10, chance = 1}, + ['small-worm-turret'] = {low = 2, high = 5, chance = 0.5}, + ['medium-worm-turret'] = {low = 5, high = 7, chance = 0.5}, + ['big-worm-turret'] = {low = 5, high = 10, chance = 0.5}, + ['behemoth-worm-turret'] = {low = 5, high = 15, chance = 0.4}, + -- default is 0, no chance of coins dropping from biters/spitters + ['small-biter'] = {low = 1, high = 2, chance = 0.05}, + ['small-spitter'] = {low = 2, high = 3, chance = 0.05}, + ['medium-spitter'] = {low = 3, high = 6, chance = 0.05}, + ['big-spitter'] = {low = 5, high = 15, chance = 0.05}, + ['behemoth-spitter'] = {low = 20, high = 30, chance = 0.05}, + ['medium-biter'] = {low = 3, high = 5, chance = 0.05}, + ['big-biter'] = {low = 3, high = 8, chance = 0.05}, + ['behemoth-biter'] = {low = 8, high = 10, chance = 0.05} + }, + turret_active_delays = { + ['ammo-turret'] = 60 * 3, + ['electric-turret'] = 60 * 10, + ['fluid-turret'] = 60 * 5, + ['artillery-turret'] = 60 * 60 + }, + warning_on_built = { + ['artillery-turret'] = true, + ['artillery-wagon'] = true, + ['tank'] = true + }, + neutral_entities = { + ['wooden-chest'] = true + }, + snake = { + size = 45, + max_food = 8, + speed = 30 + } +} + +return Config diff --git a/map_gen/maps/space_race/gui/join_gui.lua b/map_gen/maps/space_race/gui/join_gui.lua new file mode 100644 index 000000000..c003909f6 --- /dev/null +++ b/map_gen/maps/space_race/gui/join_gui.lua @@ -0,0 +1,126 @@ +local Gui = require 'utils.gui' + +local Public = {} + +-- + +local join_USA = Gui.uid_name() +local join_USSR = Gui.uid_name() + +function Public.show_gui(event) + local frame + local player = game.get_player(event.player_index) + local center = player.gui.center + local gui = center['Space-Race-Lobby'] + if (gui) then + Gui.destroy(gui) + end + + frame = player.gui.center.add {name = 'Space-Race-Lobby', type = 'frame', direction = 'vertical', style = 'captionless_frame'} + + frame.style.minimal_width = 300 + + --Header + local top_flow = frame.add {type = 'flow', direction = 'horizontal'} + top_flow.style.horizontal_align = 'center' + top_flow.style.horizontally_stretchable = true + + local title_flow = top_flow.add {type = 'flow'} + title_flow.style.horizontal_align = 'center' + title_flow.style.top_padding = 8 + title_flow.style.horizontally_stretchable = false + + local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} + title.style.font = 'default-large-bold' + + --Body + + local content_flow = frame.add {type = 'flow', direction = 'horizontal'} + content_flow.style.top_padding = 8 + content_flow.style.bottom_padding = 16 + content_flow.style.left_padding = 24 + content_flow.style.right_padding = 24 + content_flow.style.horizontal_align = 'center' + content_flow.style.horizontally_stretchable = true + + local label_flow = content_flow.add {type = 'flow'} + label_flow.style.horizontal_align = 'center' + label_flow.style.horizontally_stretchable = true + + local label = label_flow.add {type = 'label', caption = 'Feel free to pick a side!'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' + + --Footer + local button_flow = frame.add {type = 'flow'} + button_flow.style.horizontal_align = 'center' + button_flow.style.horizontally_stretchable = true + + local usa_button_flow = button_flow.add {type = 'flow', direction = 'vertical'} + usa_button_flow.style.horizontally_stretchable = true + usa_button_flow.style.horizontal_align = 'center' + + local ussr_button_flow = button_flow.add {type = 'flow', direction = 'vertical'} + ussr_button_flow.style.horizontally_stretchable = true + ussr_button_flow.style.horizontal_align = 'center' + + local teams = remote.call('space-race', 'get_teams') + + local force_USSR = teams[2] + local force_USA = teams[1] + + local usa_players = #force_USA.players + local ussr_players = #force_USSR.players + + local usa_connected = #force_USA.connected_players + local ussr_connected = #force_USSR.connected_players + + label = usa_button_flow.add {type = 'label', caption = usa_connected .. ' online / ' .. usa_players .. ' total'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' + + local join_usa_button = usa_button_flow.add {type = 'button', name = join_USA, caption = 'Join United Factory Workers'} + + label = ussr_button_flow.add {type = 'label', caption = ussr_connected .. ' online / ' .. ussr_players .. ' total'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' + local join_ussr_button = ussr_button_flow.add {type = 'button', name = join_USSR, caption = 'Join Union of Factory Employees'} + + Gui.set_data(join_usa_button, frame) + Gui.set_data(join_ussr_button, frame) +end + +Gui.on_click( + join_USA, + function(event) + local frame = Gui.get_data(event.element) + local player = event.player + + if remote.call('space-race', 'join_usa', nil, player) then + Gui.remove_data_recursively(frame) + frame.destroy() + Public.update_gui() + end + end +) + +Gui.on_click( + join_USSR, + function(event) + local frame = Gui.get_data(event.element) + local player = event.player + + if remote.call('space-race', 'join_ussr', nil, player) then + Gui.remove_data_recursively(frame) + frame.destroy() + Public.update_gui() + end + end +) + +-- + +return Public diff --git a/map_gen/maps/space_race/gui/load_gui.lua b/map_gen/maps/space_race/gui/load_gui.lua new file mode 100644 index 000000000..d222a274a --- /dev/null +++ b/map_gen/maps/space_race/gui/load_gui.lua @@ -0,0 +1,90 @@ +local Gui = require 'utils.gui' +local Color = require 'resources.color_presets' + +local Public = {} + +-- + +function Public.show_gui(event) + local frame + local player = game.get_player(event.player_index) + local center = player.gui.center + local gui = center['Space-Race-Waiting'] + if (gui) then + Gui.destroy(gui) + end + + frame = player.gui.center.add {name = 'Space-Race-Waiting', type = 'frame', direction = 'vertical', style = 'captionless_frame'} + + frame.style.minimal_width = 300 + + --Header + local top_flow = frame.add {type = 'flow', direction = 'horizontal'} + top_flow.style.horizontal_align = 'center' + top_flow.style.horizontally_stretchable = true + + local title_flow = top_flow.add {type = 'flow'} + title_flow.style.horizontal_align = 'center' + title_flow.style.top_padding = 8 + title_flow.style.horizontally_stretchable = false + + local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} + title.style.font = 'default-large-bold' + + --Body + + local content_flow = frame.add {type = 'flow'} + content_flow.style.top_padding = 8 + content_flow.style.horizontal_align = 'center' + content_flow.style.horizontally_stretchable = true + + local label_flow = content_flow.add {type = 'flow', direction = 'vertical'} + label_flow.style.horizontal_align = 'center' + + label_flow.style.horizontally_stretchable = true + local label = label_flow.add {type = 'label', caption = 'Waiting for map to generate\nPlease wait\n'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' + label.style.font_color = Color.yellow + + local started_tick = remote.call('space-race', 'get_started_tick') + local time = game.tick - started_tick + + if time > 60 then + local minutes = (time / 3600) + minutes = minutes - minutes % 1 + time = time - (minutes * 3600) + local seconds = (time / 60) + seconds = seconds - seconds % 1 + time = minutes .. ' minutes and ' .. seconds .. ' seconds' + else + local seconds = (time - (time % 60)) / 60 + time = seconds .. ' seconds' + end + + label = label_flow.add {type = 'label', caption = '[color=blue]Time elapsed: ' .. time .. ' [/color]'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' +end + +-- + +function Public.show_gui_to_all() + for _, player in pairs(game.connected_players) do + Public.show_gui({player_index = player.index}) + end +end + +function Public.remove_gui() + for _, player in pairs(game.connected_players) do + local center = player.gui.center + local gui = center['Space-Race-Waiting'] + if (gui) then + Gui.destroy(gui) + end + end +end + +return Public diff --git a/map_gen/maps/space_race/gui/wait_gui.lua b/map_gen/maps/space_race/gui/wait_gui.lua new file mode 100644 index 000000000..7cde61ef9 --- /dev/null +++ b/map_gen/maps/space_race/gui/wait_gui.lua @@ -0,0 +1,92 @@ +local Gui = require 'utils.gui' +local snake_game = require 'features.snake.game' + +local config = require 'map_gen.maps.space_race.config' +local players_needed = config.players_needed_to_start_game + +local Public = {} + +-- + +local waiting_close_name = Gui.uid_name() + +function Public.show_gui(event) + local frame + local player = game.get_player(event.player_index) + local center = player.gui.center + local gui = center['Space-Race-Lobby'] + if (gui) then + Gui.destroy(gui) + end + + local snake_button_text + + if snake_game.is_running() then + snake_button_text = 'Play Snake' + else + snake_button_text = '... Loading Snake ...' + end + frame = player.gui.center.add {name = 'Space-Race-Lobby', type = 'frame', direction = 'vertical', style = 'captionless_frame'} + + frame.style.minimal_width = 300 + + --Header + local top_flow = frame.add {type = 'flow', direction = 'horizontal'} + top_flow.style.horizontal_align = 'center' + top_flow.style.horizontally_stretchable = true + + local title_flow = top_flow.add {type = 'flow'} + title_flow.style.horizontal_align = 'center' + title_flow.style.top_padding = 8 + title_flow.style.horizontally_stretchable = false + + local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} + title.style.font = 'default-large-bold' + + --Body + + local content_flow = frame.add {type = 'flow', direction = 'horizontal'} + content_flow.style.top_padding = 8 + content_flow.style.bottom_padding = 16 + content_flow.style.left_padding = 24 + content_flow.style.right_padding = 24 + content_flow.style.horizontal_align = 'center' + content_flow.style.horizontally_stretchable = true + + local label_flow = content_flow.add {type = 'flow'} + label_flow.style.horizontal_align = 'center' + + label_flow.style.horizontally_stretchable = true + local label = label_flow.add {type = 'label', caption = #game.connected_players .. ' out of ' .. players_needed .. ' players needed to begin!'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' + + --Footer + local ok_button_flow = frame.add {type = 'flow'} + ok_button_flow.style.horizontally_stretchable = true + ok_button_flow.style.horizontal_align = 'center' + + local ok_button = ok_button_flow.add {type = 'button', name = waiting_close_name, caption = snake_button_text} + Gui.set_data(ok_button, frame) +end + +Gui.on_click( + waiting_close_name, + function(event) + if snake_game.is_running() then + local frame = Gui.get_data(event.element) + local player = event.player + + game.permissions.get_group('lobby').remove_player(player) + snake_game.new_snake(player) + + Gui.remove_data_recursively(frame) + frame.destroy() + end + end +) + +-- + +return Public diff --git a/map_gen/maps/space_race/gui/won_gui.lua b/map_gen/maps/space_race/gui/won_gui.lua new file mode 100644 index 000000000..a2e92b282 --- /dev/null +++ b/map_gen/maps/space_race/gui/won_gui.lua @@ -0,0 +1,89 @@ +local Gui = require 'utils.gui' +local snake_game = require 'features.snake.game' + +local Public = {} + +-- + +local won_close_name = Gui.uid_name() + +function Public.show_gui(event, force) + local frame + local player = game.get_player(event.player_index) + local center = player.gui.center + local gui = center['Space-Race-Lobby'] + if (gui) then + Gui.destroy(gui) + end + + local snake_button_text + + if snake_game.is_running() then + snake_button_text = 'Play Snake' + else + snake_button_text = '... Loading Snake ...' + end + frame = player.gui.center.add {name = 'Space-Race-Lobby', type = 'frame', direction = 'vertical', style = 'captionless_frame'} + + frame.style.minimal_width = 300 + + --Header + local top_flow = frame.add {type = 'flow', direction = 'horizontal'} + top_flow.style.horizontal_align = 'center' + top_flow.style.horizontally_stretchable = true + + local title_flow = top_flow.add {type = 'flow'} + title_flow.style.horizontal_align = 'center' + title_flow.style.top_padding = 8 + title_flow.style.horizontally_stretchable = false + + local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} + title.style.font = 'default-large-bold' + + --Body + + local content_flow = frame.add {type = 'flow', direction = 'horizontal'} + content_flow.style.top_padding = 8 + content_flow.style.bottom_padding = 16 + content_flow.style.left_padding = 24 + content_flow.style.right_padding = 24 + content_flow.style.horizontal_align = 'center' + content_flow.style.horizontally_stretchable = true + + local label_flow = content_flow.add {type = 'flow'} + label_flow.style.horizontal_align = 'center' + + label_flow.style.horizontally_stretchable = true + local label = label_flow.add {type = 'label', caption = force.name .. ' have won the game!\nWaiting for map restart\n\nPlay some snake while we wait!'} + label.style.horizontal_align = 'center' + label.style.single_line = false + label.style.font = 'default' + + --Footer + local ok_button_flow = frame.add {type = 'flow'} + ok_button_flow.style.horizontally_stretchable = true + ok_button_flow.style.horizontal_align = 'center' + + local ok_button = ok_button_flow.add {type = 'button', name = won_close_name, caption = snake_button_text} + Gui.set_data(ok_button, frame) +end + +Gui.on_click( + won_close_name, + function(event) + if snake_game.is_running() then + local frame = Gui.get_data(event.element) + local player = event.player + + game.permissions.get_group('lobby').remove_player(player) + snake_game.new_snake(player) + + Gui.remove_data_recursively(frame) + frame.destroy() + end + end +) + +-- + +return Public diff --git a/map_gen/maps/space_race/lobby.lua b/map_gen/maps/space_race/lobby.lua index 3308be89b..eb941af0d 100644 --- a/map_gen/maps/space_race/lobby.lua +++ b/map_gen/maps/space_race/lobby.lua @@ -1,33 +1,46 @@ local Event = require 'utils.event' local Token = require 'utils.token' local Task = require 'utils.task' -local Gui = require 'utils.gui' local b = require 'map_gen.shared.builders' local snake_game = require 'features.snake.game' +local config = require 'map_gen.maps.space_race.config' + +--Guis +local join_gui = require 'map_gen.maps.space_race.gui.join_gui' +local wait_gui = require 'map_gen.maps.space_race.gui.wait_gui' +local won_gui = require 'map_gen.maps.space_race.gui.won_gui' local floor = math.floor local Public = {} +local size = config.snake.size +local snake_check_x = (size + 5) * 2 +local snake_check_y = size + 5 + --Lobby Map local Generate = require 'map_gen.shared.generate' -local shape = b.rectangle(45, 4) +local shape = b.rectangle(size, 4) shape = b.change_tile(shape, true, 'concrete') Generate.get_surfaces()['snake'] = shape local delay_snake_checker -local players_needed = 4 +local players_needed = config.players_needed_to_start_game -local size = 45 +local function check_snake_map_gen() + local surface = game.get_surface('snake') + return surface.get_tile({snake_check_x, -snake_check_y}).name == 'out-of-map' + and surface.get_tile({snake_check_x, snake_check_y}).name == 'out-of-map' +end local snake_generate = Token.register( function() local surface = game.get_surface('snake') - if surface.get_tile({100, -50}).name == 'out-of-map' and surface.get_tile({100, 50}).name == 'out-of-map' then + if check_snake_map_gen() then local position = {x = -floor(size), y = 5} - local max_food = 8 - local speed = 30 + local max_food = config.snake.max_food + local speed = config.snake.speed snake_game.start_game(surface, position, size, speed, max_food) else Task.set_timeout_in_ticks(5, delay_snake_checker) @@ -40,8 +53,7 @@ check_map_gen_is_done = Token.register( function() if not snake_game.is_running() then - local surface = game.get_surface('snake') - if surface.get_tile({100, -50}).name == 'out-of-map' and surface.get_tile({100, 50}).name == 'out-of-map' then + if check_snake_map_gen() then Event.remove_removable_nth_tick(60, check_map_gen_is_done) Token.get(snake_generate)() Public.update_gui() @@ -72,310 +84,23 @@ Event.on_init( end ) --- - -local waiting_close_name = Gui.uid_name() - -local function show_waiting_gui(event) - local frame - local player = game.get_player(event.player_index) - local center = player.gui.center - local gui = center['Space-Race-Lobby'] - if (gui) then - Gui.destroy(gui) - end - - local snake_button_text - - if snake_game.is_running() then - snake_button_text = 'Play Snake' - else - snake_button_text = '... Loading Snake ...' - end - frame = player.gui.center.add {name = 'Space-Race-Lobby', type = 'frame', direction = 'vertical', style = 'captionless_frame'} - - frame.style.minimal_width = 300 - - --Header - local top_flow = frame.add {type = 'flow', direction = 'horizontal'} - top_flow.style.horizontal_align = 'center' - top_flow.style.horizontally_stretchable = true - - local title_flow = top_flow.add {type = 'flow'} - title_flow.style.horizontal_align = 'center' - title_flow.style.top_padding = 8 - title_flow.style.horizontally_stretchable = false - - local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} - title.style.font = 'default-large-bold' - - --Body - - local content_flow = frame.add {type = 'flow', direction = 'horizontal'} - content_flow.style.top_padding = 8 - content_flow.style.bottom_padding = 16 - content_flow.style.left_padding = 24 - content_flow.style.right_padding = 24 - content_flow.style.horizontal_align = 'center' - content_flow.style.horizontally_stretchable = true - - local label_flow = content_flow.add {type = 'flow'} - label_flow.style.horizontal_align = 'center' - - label_flow.style.horizontally_stretchable = true - local label = label_flow.add {type = 'label', caption = #game.connected_players .. ' out of ' .. players_needed .. ' players needed to begin!'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' - - --Footer - local ok_button_flow = frame.add {type = 'flow'} - ok_button_flow.style.horizontally_stretchable = true - ok_button_flow.style.horizontal_align = 'center' - - local ok_button = ok_button_flow.add {type = 'button', name = waiting_close_name, caption = snake_button_text} - Gui.set_data(ok_button, frame) -end - -Gui.on_click( - waiting_close_name, - function(event) - if snake_game.is_running() then - local frame = Gui.get_data(event.element) - local player = event.player - - game.permissions.get_group('lobby').remove_player(player) - snake_game.new_snake(player) - - Gui.remove_data_recursively(frame) - frame.destroy() - end - end -) - --- - --- - -local join_USA = Gui.uid_name() -local join_USSR = Gui.uid_name() - -local function show_join_gui(event) - local frame - local player = game.get_player(event.player_index) - local center = player.gui.center - local gui = center['Space-Race-Lobby'] - if (gui) then - Gui.destroy(gui) - end - - frame = player.gui.center.add {name = 'Space-Race-Lobby', type = 'frame', direction = 'vertical', style = 'captionless_frame'} - - frame.style.minimal_width = 300 - - --Header - local top_flow = frame.add {type = 'flow', direction = 'horizontal'} - top_flow.style.horizontal_align = 'center' - top_flow.style.horizontally_stretchable = true - - local title_flow = top_flow.add {type = 'flow'} - title_flow.style.horizontal_align = 'center' - title_flow.style.top_padding = 8 - title_flow.style.horizontally_stretchable = false - - local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} - title.style.font = 'default-large-bold' - - --Body - - local content_flow = frame.add {type = 'flow', direction = 'horizontal'} - content_flow.style.top_padding = 8 - content_flow.style.bottom_padding = 16 - content_flow.style.left_padding = 24 - content_flow.style.right_padding = 24 - content_flow.style.horizontal_align = 'center' - content_flow.style.horizontally_stretchable = true - - local label_flow = content_flow.add {type = 'flow'} - label_flow.style.horizontal_align = 'center' - label_flow.style.horizontally_stretchable = true - - local label = label_flow.add {type = 'label', caption = 'Feel free to pick a side!'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' - - --Footer - local button_flow = frame.add {type = 'flow'} - button_flow.style.horizontal_align = 'center' - button_flow.style.horizontally_stretchable = true - - local usa_button_flow = button_flow.add {type = 'flow', direction = 'vertical'} - usa_button_flow.style.horizontally_stretchable = true - usa_button_flow.style.horizontal_align = 'center' - - local ussr_button_flow = button_flow.add {type = 'flow', direction = 'vertical'} - ussr_button_flow.style.horizontally_stretchable = true - ussr_button_flow.style.horizontal_align = 'center' - - local teams = remote.call('space-race', 'get_teams') - - local force_USSR = teams[2] - local force_USA = teams[1] - - local usa_players = #force_USA.players - local ussr_players = #force_USSR.players - - local usa_connected = #force_USA.connected_players - local ussr_connected = #force_USSR.connected_players - - label = usa_button_flow.add {type = 'label', caption = usa_connected .. ' online / ' .. usa_players .. ' total'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' - - local join_usa_button = usa_button_flow.add {type = 'button', name = join_USA, caption = 'Join United Factory Workers'} - - label = ussr_button_flow.add {type = 'label', caption = ussr_connected .. ' online / ' .. ussr_players .. ' total'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' - local join_ussr_button = ussr_button_flow.add {type = 'button', name = join_USSR, caption = 'Join Union of Factory Employees'} - - Gui.set_data(join_usa_button, frame) - Gui.set_data(join_ussr_button, frame) -end - -Gui.on_click( - join_USA, - function(event) - local frame = Gui.get_data(event.element) - local player = event.player - - if remote.call('space-race', 'join_usa', nil, player) then - Gui.remove_data_recursively(frame) - frame.destroy() - Public.update_gui() - end - end -) - -Gui.on_click( - join_USSR, - function(event) - local frame = Gui.get_data(event.element) - local player = event.player - - if remote.call('space-race', 'join_ussr', nil, player) then - Gui.remove_data_recursively(frame) - frame.destroy() - Public.update_gui() - end - end -) - --- - --- - -local won_close_name = Gui.uid_name() - -local function show_won_gui(event, force) - local frame - local player = game.get_player(event.player_index) - local center = player.gui.center - local gui = center['Space-Race-Lobby'] - if (gui) then - Gui.destroy(gui) - end - - local snake_button_text - - if snake_game.is_running() then - snake_button_text = 'Play Snake' - else - snake_button_text = '... Loading Snake ...' - end - frame = player.gui.center.add {name = 'Space-Race-Lobby', type = 'frame', direction = 'vertical', style = 'captionless_frame'} - - frame.style.minimal_width = 300 - - --Header - local top_flow = frame.add {type = 'flow', direction = 'horizontal'} - top_flow.style.horizontal_align = 'center' - top_flow.style.horizontally_stretchable = true - - local title_flow = top_flow.add {type = 'flow'} - title_flow.style.horizontal_align = 'center' - title_flow.style.top_padding = 8 - title_flow.style.horizontally_stretchable = false - - local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} - title.style.font = 'default-large-bold' - - --Body - - local content_flow = frame.add {type = 'flow', direction = 'horizontal'} - content_flow.style.top_padding = 8 - content_flow.style.bottom_padding = 16 - content_flow.style.left_padding = 24 - content_flow.style.right_padding = 24 - content_flow.style.horizontal_align = 'center' - content_flow.style.horizontally_stretchable = true - - local label_flow = content_flow.add {type = 'flow'} - label_flow.style.horizontal_align = 'center' - - label_flow.style.horizontally_stretchable = true - local label = label_flow.add {type = 'label', caption = force.name .. ' have won the game!\nWaiting for map restart\n\nPlay some snake while we wait!'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' - - --Footer - local ok_button_flow = frame.add {type = 'flow'} - ok_button_flow.style.horizontally_stretchable = true - ok_button_flow.style.horizontal_align = 'center' - - local ok_button = ok_button_flow.add {type = 'button', name = won_close_name, caption = snake_button_text} - Gui.set_data(ok_button, frame) -end - -Gui.on_click( - won_close_name, - function(event) - if snake_game.is_running() then - local frame = Gui.get_data(event.element) - local player = event.player - - game.permissions.get_group('lobby').remove_player(player) - snake_game.new_snake(player) - - Gui.remove_data_recursively(frame) - frame.destroy() - end - end -) - --- - function Public.show_gui(event) if #game.connected_players < players_needed and (not remote.call('space-race', 'get_game_status')) then if not snake_game.is_running() and game.tick > 60 * 55 then Token.get(snake_generate)() end game.forces.enemy.evolution_factor = 0 - show_waiting_gui(event) + wait_gui.show_gui(event) return end local won = remote.call('space-race', 'get_won') if won then - show_won_gui(event, won) + won_gui.show_gui(event, won) else if snake_game.is_running() then snake_game.end_game() end - show_join_gui(event) + join_gui.show_gui(event) end end diff --git a/map_gen/maps/space_race/map.lua b/map_gen/maps/space_race/map.lua index d0f9afd3d..f3c8c8f79 100644 --- a/map_gen/maps/space_race/map.lua +++ b/map_gen/maps/space_race/map.lua @@ -206,7 +206,3 @@ local wilderness = b.any({wilderness_shallow_water, wilderness_ditch_left, wilde local map = b.if_else(wilderness, b.full_shape) return map - ---[[local function dummy_map(x, y, world) - return map(x, y, world) -end]] diff --git a/map_gen/maps/space_race/map_info.lua b/map_gen/maps/space_race/map_info.lua index 9e67df654..574cbca36 100644 --- a/map_gen/maps/space_race/map_info.lua +++ b/map_gen/maps/space_race/map_info.lua @@ -1,4 +1,8 @@ local ScenarioInfo = require 'features.gui.info' +local config = require 'map_gen.maps.space_race.config' + +local entity_kill_rewards = config.entity_kill_rewards +local entity_drop_amount = config.entity_drop_amount ScenarioInfo.set_map_name('RedMew - Space Race') --ScenarioInfo.set_map_description('World at War! This is not a cold war, it is a radioative hot war') @@ -13,14 +17,21 @@ ScenarioInfo.add_map_extra_info( - Explore, exploit and defend the wilderness for extra resources and coins - Engage in PVP and PVE to earn coins to enable research - Enemy structures: (balance coming soon) - Turrets 1 coin - Radars 1 coin - Others 1 coin - - Enemy players: 25 coins + Turrets ]] .. entity_kill_rewards['gun-turret'] .. '-' .. entity_kill_rewards['artillery-turret'] .. ' coins\n' .. +[[ + Radars ]] .. entity_kill_rewards['radar'] .. ' coins\n' .. +[[ + Others ]] .. entity_kill_rewards['default'] .. ' coin\n' .. +[[ + - Enemy players: ]] .. config.player_kill_reward .. ' coins\n' .. +[[ - Natives: - Units 1-15 coins, 5% chance - Worms 2-15 coins, 50% chance - Spawners 2-10 coins, 100 % chance + Units ]] .. entity_drop_amount['small-biter'].low .. '-' .. entity_drop_amount['behemoth-spitter'].high .. ' coins ' .. entity_drop_amount['small-biter'].chance * 100 .. '% chance\n' .. +[[ + Worms ]] .. entity_drop_amount['small-worm-turret'].low .. '-' .. entity_drop_amount['behemoth-worm-turret'].high .. ' coins ' .. entity_drop_amount['small-worm-turret'].chance * 100 .. '% chance\n' .. +[[ + Spawners ]] .. entity_drop_amount['spitter-spawner'].low .. '-' .. entity_drop_amount['biter-spawner'].high .. ' coins ' .. entity_drop_amount['biter-spawner'].chance * 100 .. '% chance\n' .. +[[ - Sabotage enemy wilderness structures to slow the enemy team Coming Soon: @@ -38,6 +49,11 @@ Current Version: v0.2 ScenarioInfo.set_new_info( [[ +2019-10-12 v0.3 +- Market prices changed +- Wooden chests are now minable for both forces +- Refactoring of code +- Added central config file 2019-10-11 v0.2 - Added water near spawn - Changed biter coin drop rate diff --git a/map_gen/maps/space_race/market_handler.lua b/map_gen/maps/space_race/market_handler.lua index 19c751909..4722a4a8d 100644 --- a/map_gen/maps/space_race/market_handler.lua +++ b/map_gen/maps/space_race/market_handler.lua @@ -2,6 +2,39 @@ local Retailer = require 'features.retailer' local Events = require 'utils.event' local Token = require 'utils.token' local Task = require 'utils.task' +local Global = require 'utils.global' +local ScoreTracker = require 'utils.score_tracker' + +local unlock_progress = { + force_USA = { + players_killed = 0, + entities_killed = 0 + }, + force_USSR = { + players_killed = 0, + entities_killed = 0 + } +} + +Global.register( + { + unlock_progress = unlock_progress + }, + function(tbl) + unlock_progress = tbl.unlock_progress + end +) + + + +local config = require 'map_gen.maps.space_race.config' + +local entity_kill_rewards = config.entity_kill_rewards +local entity_kill_rewards_default = entity_kill_rewards['default'] + +local entity_drop_amount = config.entity_drop_amount + +local player_kill_reward = config.player_kill_reward local function on_market_purchase(event) local item = event.item @@ -33,26 +66,119 @@ local spill_items = local random = math.random -local entity_drop_amount = {--NEEDS BALANCING! - ['biter-spawner'] = {low = 2, high = 10, chance = 1}, - ['spitter-spawner'] = {low = 2, high = 10, chance = 1}, - ['small-worm-turret'] = {low = 2, high = 5, chance = 0.5}, - ['medium-worm-turret'] = {low = 5, high = 7, chance = 0.5}, - ['big-worm-turret'] = {low = 5, high = 10, chance = 0.5}, - ['behemoth-worm-turret'] = {low = 5, high = 15, chance = 0.4}, - -- default is 0, no chance of coins dropping from biters/spitters - ['small-biter'] = {low = 1, high = 2, chance = 0.05}, - ['small-spitter'] = {low = 2, high = 3, chance = 0.05}, - ['medium-spitter'] = {low = 3, high = 6, chance = 0.05}, - ['big-spitter'] = {low = 5, high = 15, chance = 0.05}, - ['behemoth-spitter'] = {low = 20, high = 30, chance = 0.05}, - ['medium-biter'] = {low = 3, high = 5, chance = 0.05}, - ['big-biter'] = {low = 3, high = 8, chance = 0.05}, - ['behemoth-biter'] = {low = 8, high = 10, chance = 0.05} +local function invert_force(force) + local teams = remote.call('space-race', 'get_teams') + + local force_USSR = teams[2] + local force_USA = teams[1] + + if force == force_USA then + return force_USSR + elseif force == force_USSR then + return force_USA + end +end + +local unlock_reasons = { + player_killed = 1, + entity_killed = 2 } +local function unlock_market_item(force, item_name) + local group_name + + local teams = remote.call('space-race', 'get_teams') + + local force_USSR = teams[2] + local force_USA = teams[1] + + if force == force_USA then + group_name = 'USA_market' + elseif force == force_USSR then + group_name = 'USSR_market' + end + if group_name then + Retailer.enable_item(group_name, item_name) + if not (item_name == 'tank') then + Debug.print('Unlocked: ' .. item_name .. ' | For: ' .. group_name) + end + end +end + +local function check_for_market_unlocks(force) + local teams = remote.call('space-race', 'get_teams') + + local force_USSR = teams[2] + local force_USA = teams[1] + + for research, conditions in pairs(config.disabled_research) do + local _force = force + local inverted = conditions.invert + local unlocks = conditions.unlocks + + if inverted then + _force = invert_force(_force) + end + + if force == force_USA then + if conditions.player <= unlock_progress.force_USA.players_killed or conditions.entity <= unlock_progress.force_USA.entities_killed then + unlock_market_item(_force, research) + if unlocks then + unlock_market_item(invert_force(_force), unlocks) + end + end + elseif force == force_USSR then + if conditions.player <= unlock_progress.force_USSR.players_killed or conditions.entity <= unlock_progress.force_USSR.entities_killed then + unlock_market_item(_force, research) + if unlocks then + unlock_market_item(invert_force(_force), unlocks) + end + end + end + end + + if force_USA.technologies.tanks.researched then + unlock_market_item(force_USA, 'tank') + end + if force_USSR.technologies.tanks.researched then + unlock_market_item(force_USSR, 'tank') + end +end + +local function update_unlock_progress(force, unlock_reason) + local players_killed + local entities_killed + + local teams = remote.call('space-race', 'get_teams') + + local force_USSR = teams[2] + local force_USA = teams[1] + + if force == force_USA then + players_killed = unlock_progress.force_USA.players_killed + entities_killed = unlock_progress.force_USA.entities_killed + if unlock_reason == unlock_reasons.player_killed then + unlock_progress.force_USA.players_killed = players_killed + 1 + elseif unlock_reason == unlock_reasons.entity_killed then + unlock_progress.force_USA.entities_killed = entities_killed + 1 + end + elseif force == force_USSR then + players_killed = unlock_progress.force_USSR.players_killed + entities_killed = unlock_progress.force_USSR.entities_killed + if unlock_reason == unlock_reasons.player_killed then + unlock_progress.force_USSR.players_killed = players_killed + 1 + elseif unlock_reason == unlock_reasons.entity_killed then + unlock_progress.force_USSR.entities_killed = entities_killed + 1 + end + else + return + end + + check_for_market_unlocks(force) +end + -- Determines how many coins to drop when enemy entity dies based upon the entity_drop_amount table in config.lua -local function fish_drop_entity_died(event) +local function spill_coins(event) local entity = event.entity if not entity or not entity.valid then return @@ -85,15 +211,85 @@ local function fish_drop_entity_died(event) end end -Events.add(defines.events.on_entity_died, fish_drop_entity_died) +local function insert_coins(event) + local entity = event.entity + + if entity.type == 'character' then + return + end + + local force = entity.force + local name = entity.name + if name == 'rocket-silo' then + remote.call('space-race', 'lost', force) + end ---[[ + local teams = remote.call('space-race', 'get_teams') - raise_event(Retailer.events.on_market_purchase, { - item = item, - count = stack_count, - player = player, - group_name = market_group, - }) + local force_USSR = teams[2] + local force_USA = teams[1] + + if force ~= force_USSR and force ~= force_USA then + return + end + + local cause = event.cause + local cause_force = event.force + + local count = config.entity_kill_rewards[name] or entity_kill_rewards_default + + if cause_force then + if not (cause and cause.valid) then + if not (force == force_USA or force == force_USSR) then + return + end + Task.set_timeout_in_ticks( + 1, + spill_items, + { + count = math.floor(count/2), + surface = entity.surface, + position = entity.position + } + ) + end + end + + if cause and cause.valid then + if cause.prototype.name == 'character' then + cause_force = cause.force + if not (force == cause_force) then + local coins_inserted = cause.insert({name = 'coin', count = count}) + ScoreTracker.change_for_player(cause.index, 'coins-earned', coins_inserted) + update_unlock_progress(cause_force, unlock_reasons.entity_killed) + end + end + end +end + +local function on_entity_died(event) + spill_coins(event) + insert_coins(event) +end + +Events.add(defines.events.on_entity_died, on_entity_died) + +local function on_player_died(event) + local cause = event.cause + if cause and cause.valid and cause.type == 'character' then + local cause_force = cause.force + if not (game.get_player(event.player_index).force == cause_force) then + cause.insert({name = 'coin', count = player_kill_reward}) + update_unlock_progress(cause_force, unlock_reasons.player_killed) + end + end +end + +Events.add(defines.events.on_player_died, on_player_died) + +local function on_research_finished(event) + check_for_market_unlocks(event.research.force) + remote.call('space-race', 'remove_recipes') +end -]] +Events.add(defines.events.on_research_finished, on_research_finished) diff --git a/map_gen/maps/space_race/market_items.lua b/map_gen/maps/space_race/market_items.lua index 9e62d732d..cba1ac2df 100644 --- a/map_gen/maps/space_race/market_items.lua +++ b/map_gen/maps/space_race/market_items.lua @@ -6,7 +6,7 @@ return { description = {'', 'Unlocks stone walls research'}, sprite = 'technology/stone-walls', stack_limit = 1, - price = 250, + price = 500, disabled = true, disabled_reason = {'', 'DISABLED'} }, @@ -17,7 +17,7 @@ return { description = {'', 'Unlocks heavy armor research'}, sprite = 'technology/heavy-armor', stack_limit = 1, - price = 400, + price = 1000, disabled = true, disabled_reason = {'', 'DISABLED'} }, @@ -28,7 +28,7 @@ return { description = {'', 'Unlocks military research'}, sprite = 'technology/military', stack_limit = 1, - price = 100, + price = 1000, disabled = true, disabled_reason = {'', 'DISABLED'} }, @@ -39,7 +39,7 @@ return { description = {'', 'Unlocks military 2 research'}, sprite = 'technology/military-2', stack_limit = 1, - price = 1000, + price = 10000, disabled = true, disabled_reason = {'', 'DISABLED'} }, @@ -50,7 +50,7 @@ return { description = {'', 'Unlocks military 3 research'}, sprite = 'technology/military-3', stack_limit = 1, - price = 10000, + price = 25000, disabled = true, disabled_reason = {'', 'DISABLED'} }, @@ -61,7 +61,7 @@ return { description = {'', 'Unlocks military 4 research'}, sprite = 'technology/military-3', stack_limit = 1, - price = 10000, + price = 50000, disabled = true, disabled_reason = {'', 'DISABLED'} }, @@ -72,9 +72,10 @@ return { description = {'', 'Tanks can only be purchased'}, sprite = 'item/tank', stack_limit = 1, - price = 200, + price = 2000, disabled = true, disabled_reason = {'', 'Research tanks to unlock'} }, - {price = 1, name = 'raw-fish'}, + {price = 10, name = 'raw-fish'}, + {price = 10, name = 'repair-pack'} } diff --git a/map_gen/maps/space_race/scenario.lua b/map_gen/maps/space_race/scenario.lua index 42fdb71dc..dc5f9c528 100644 --- a/map_gen/maps/space_race/scenario.lua +++ b/map_gen/maps/space_race/scenario.lua @@ -7,6 +7,8 @@ local Retailer = require 'features.retailer' local Market_Items = require 'map_gen.maps.space_race.market_items' local Token = require 'utils.token' local Task = require 'utils.task' +local config = require 'map_gen.maps.space_race.config' +local Table = require 'utils.table' local floor = math.floor @@ -14,50 +16,42 @@ require 'map_gen.maps.space_race.map_info' require 'map_gen.maps.space_race.market_handler' local Lobby = require 'map_gen.maps.space_race.lobby' +-- GUIs + +local load_gui = require 'map_gen.maps.space_race.gui.load_gui' + local Public = {} -local config = global.config +local redmew_config = global.config -config.market.enabled = false -config.score.enabled = false -config.player_rewards.enabled = false -config.apocalypse.enabled = false -config.turret_active_delay.turret_types = { +redmew_config.market.enabled = false +redmew_config.score.enabled = false +redmew_config.player_rewards.enabled = false +redmew_config.apocalypse.enabled = false +redmew_config.turret_active_delay.turret_types = { ['ammo-turret'] = 60 * 3, ['electric-turret'] = 60 * 10, ['fluid-turret'] = 60 * 5, ['artillery-turret'] = 60 * 60 } -config.turret_active_delay.techs = {} -config.player_create.show_info_at_start = false -config.camera.enabled = false +redmew_config.turret_active_delay.techs = {} +redmew_config.player_create.show_info_at_start = false +redmew_config.camera.enabled = false -local players_needed = 4 -local player_kill_reward = 25 -local entity_kill_reward = 1 -local startup_timer = 60 * 60 * 2 +local players_needed = config.players_needed_to_start_game +local player_kill_reward = config.player_kill_reward +local startup_timer = config.bootstrap_period local player_ports = { USA = {{x = -409, y = 0}, {x = -380, y = 0}}, USSR = {{x = 409, y = 0}, {x = 380, y = 0}} } -local disabled_research = { - ['military'] = {player = 2, entity = 8}, - ['military-2'] = {player = 6, entity = 24, unlocks = 'military'}, - ['military-3'] = {player = 12, entity = 48, unlocks = 'military-2'}, - ['military-4'] = {player = 24, entity = 96, unlocks = 'military-3'}, - ['stone-walls'] = {player = 2, entity = 8, invert = true}, - ['heavy-armor'] = {player = 12, entity = 48, invert = true}, - ['artillery-shell-range-1'] = nil -} +local disabled_research = config.disabled_research local researched_tech = {} -local disabled_recipes = { - 'tank', - 'rocket-silo' -} +local disabled_recipes = config.disabled_recipes local primitives = { game_started = false, @@ -69,29 +63,16 @@ local primitives = { won = nil } -local unlock_progress = { - force_USA = { - players_killed = 0, - entities_killed = 0 - }, - force_USSR = { - players_killed = 0, - entities_killed = 0 - } -} - Global.register( { primitives = primitives, - unlock_progress = unlock_progress }, function(tbl) primitives = tbl.primitives - unlock_progress = tbl.unlock_progress end ) -local function remove_recipes() +function Public.remove_recipes() local USA_recipe = primitives.force_USA.recipes local USSR_recipe = primitives.force_USSR.recipes for _, recipe in pairs(disabled_recipes) do @@ -107,19 +88,6 @@ local remove_permission_group = end ) -local function deepcopy(orig) - local copy - if type(orig) == 'table' then - copy = {} - for orig_key, orig_value in pairs(orig) do - copy[deepcopy(orig_key)] = deepcopy(orig_value) - end - else - copy = orig - end - return copy -end - Event.on_init( function() game.difficulty_settings.technology_price_multiplier = 0.5 @@ -135,11 +103,14 @@ Event.on_init( force_USSR.laboratory_speed_modifier = 1 force_USA.laboratory_speed_modifier = 1 + force_USSR.research_queue_enabled = true + force_USA.research_queue_enabled = true + local lobby_permissions = game.permissions.create_group('lobby') lobby_permissions.set_allows_action(defines.input_action.start_walking, false) - force_USSR.chart(RS.get_surface(), {{x = 380, y = 16}, {x = 420, y = -16}}) - force_USA.chart(RS.get_surface(), {{x = -380, y = 16}, {x = -420, y = -16}}) + force_USSR.chart(RS.get_surface(), {{x = 380, y = 64}, {x = 420, y = -64}}) + force_USA.chart(RS.get_surface(), {{x = -380, y = 64}, {x = -420, y = -64}}) --game.forces.player.chart(RS.get_surface(), {{x = 400, y = 65}, {x = -400, y = -33}}) local silo @@ -168,7 +139,7 @@ Event.on_init( Retailer.add_market('USA_market', market) if table.size(Retailer.get_items('USSR_market')) == 0 then - local items = deepcopy(Market_Items) + local items = Table.deep_copy(Market_Items) for _, prototype in pairs(items) do local name = prototype.name prototype.price = (disabled_research[name] and disabled_research[name].player) and disabled_research[name].player * player_kill_reward or prototype.price @@ -185,7 +156,7 @@ Event.on_init( end if table.size(Retailer.get_items('USA_market')) == 0 then - local items = deepcopy(Market_Items) + local items = Table.deep_copy(Market_Items) for _, prototype in pairs(items) do local name = prototype.name prototype.price = (disabled_research[name] and disabled_research[name].player) and disabled_research[name].player * player_kill_reward or prototype.price @@ -255,106 +226,10 @@ Event.on_init( primitives.lobby_permissions = lobby_permissions - remove_recipes() + Public.remove_recipes() end ) -local function invert_force(force) - local force_USA = primitives.force_USA - local force_USSR = primitives.force_USSR - if force == force_USA then - return force_USSR - elseif force == force_USSR then - return force_USA - end -end - -local unlock_reasons = { - player_killed = 1, - entity_killed = 2 -} - -local function unlock_market_item(force, item_name) - local group_name - if force == primitives.force_USA then - group_name = 'USA_market' - elseif force == primitives.force_USSR then - group_name = 'USSR_market' - end - if group_name then - Retailer.enable_item(group_name, item_name) - if not (item_name == 'tank') then - Debug.print('Unlocked: ' .. item_name .. ' | For: ' .. group_name) - end - end -end - -local function check_for_market_unlocks(force) - local force_USA = primitives.force_USA - local force_USSR = primitives.force_USSR - - for research, conditions in pairs(disabled_research) do - local _force = force - local inverted = conditions.invert - local unlocks = conditions.unlocks - - if inverted then - _force = invert_force(_force) - end - - if force == force_USA then - if conditions.player <= unlock_progress.force_USA.players_killed or conditions.entity <= unlock_progress.force_USA.entities_killed then - unlock_market_item(_force, research) - if unlocks then - unlock_market_item(invert_force(_force), unlocks) - end - end - elseif force == force_USSR then - if conditions.player <= unlock_progress.force_USSR.players_killed or conditions.entity <= unlock_progress.force_USSR.entities_killed then - unlock_market_item(_force, research) - if unlocks then - unlock_market_item(invert_force(_force), unlocks) - end - end - end - end - - if force_USA.technologies.tanks.researched then - unlock_market_item(force_USA, 'tank') - end - if force_USSR.technologies.tanks.researched then - unlock_market_item(force_USSR, 'tank') - end -end - -local function update_unlock_progress(force, unlock_reason) - local players_killed - local entities_killed - local force_USA = primitives.force_USA - local force_USSR = primitives.force_USSR - if force == force_USA then - players_killed = unlock_progress.force_USA.players_killed - entities_killed = unlock_progress.force_USA.entities_killed - if unlock_reason == unlock_reasons.player_killed then - unlock_progress.force_USA.players_killed = players_killed + 1 - elseif unlock_reason == unlock_reasons.entity_killed then - unlock_progress.force_USA.entities_killed = entities_killed + 1 - end - elseif force == force_USSR then - players_killed = unlock_progress.force_USSR.players_killed - entities_killed = unlock_progress.force_USSR.entities_killed - if unlock_reason == unlock_reasons.player_killed then - unlock_progress.force_USSR.players_killed = players_killed + 1 - elseif unlock_reason == unlock_reasons.entity_killed then - unlock_progress.force_USSR.entities_killed = entities_killed + 1 - end - else - return - end - - check_for_market_unlocks(force) -end - local function restore_character(player) if primitives.game_started then player.set_controller {type = defines.controllers.god} @@ -385,7 +260,7 @@ local function victory(force) Lobby.all_to_lobby() end -local function lost(force) +function Public.lost(force) local force_USA = primitives.force_USA if force == force_USA then victory(primitives.force_USSR) @@ -394,67 +269,11 @@ local function lost(force) end end -local function on_entity_died(event) - local entity = event.entity - - if entity.type == 'character' then - return - end - - local force = entity.force - if entity.name == 'rocket-silo' then - lost(force) - end - - if force == 'player' or force == 'neutral' or force == 'enemy' then - return - end - - local cause = event.cause - local cause_force = event.force - - if cause_force then - if not (cause and cause.valid) then - local force_USA = primitives.force_USA - cause_force = (force == force_USA) and primitives.force_USSR or force_USA - cause = entity.surface.find_entities_filtered {position = entity.position, radius = 50, type = 'character', force = cause_force, limit = 1}[1] - end - end - - if cause and cause.valid then - if cause.prototype.name == 'character' then - cause_force = cause.force - if not (force == cause_force) then - cause.insert({name = 'coin', count = entity_kill_reward}) - update_unlock_progress(cause_force, unlock_reasons.entity_killed) - end - end - end -end - local function on_rocket_launched(event) victory(event.entity.force) end -local function on_research_finished(event) - check_for_market_unlocks(event.research.force) - remove_recipes() -end - -Event.add(defines.events.on_entity_died, on_entity_died) Event.add(defines.events.on_rocket_launched, on_rocket_launched) -Event.add(defines.events.on_research_finished, on_research_finished) - -local function on_player_died(event) - local cause = event.cause - if cause and cause.valid and cause.type == 'character' then - local cause_force = cause.force - if not (game.get_player(event.player_index).force == cause_force) then - cause.insert({name = 'coin', count = player_kill_reward}) - update_unlock_progress(cause_force, unlock_reasons.player_killed) - end - end -end local function on_built_entity(event) local entity = event.created_entity @@ -464,13 +283,18 @@ local function on_built_entity(event) end local name = entity.name - if name == 'artillery-turret' or name == 'artillery-wagon' or name == 'tank' then + + if config.neutral_entities[name] then + entity.force = 'neutral' + return + end + + if config.warning_on_built[name] then local position = entity.position game.print({'', '[gps=' .. floor(position.x) .. ', ' .. floor(position.y) .. '] [color=yellow]Warning! ', {'entity-name.' .. name}, ' has been deployed![/color]'}) end end -Event.add(defines.events.on_player_died, on_player_died) Event.add(defines.events.on_built_entity, on_built_entity) local function allow_teleport(force, position) @@ -479,7 +303,7 @@ local function allow_teleport(force, position) elseif force == primitives.force_USSR and position.x < 0 then return false end - return math.abs(position.x) > 377 and math.abs(position.x) < 400 and position.y > -10 and position.y < 10 + return math.abs(position.x) > 377 and math.abs(position.x) < 410 and position.y > -10 and position.y < 10 end local function get_teleport_location(force, to_safe_zone) @@ -535,87 +359,6 @@ end Command.add('warp', {description = 'Use to switch between PVP and Safe-zone in Space Race', capture_excess_arguments = false, allowed_by_server = false}, teleport) -local Gui = require 'utils.gui' - -local function loading_gui(event) - local frame - local player = game.get_player(event.player_index) - local center = player.gui.center - local gui = center['Space-Race-Waiting'] - if (gui) then - Gui.destroy(gui) - end - - frame = player.gui.center.add {name = 'Space-Race-Waiting', type = 'frame', direction = 'vertical', style = 'captionless_frame'} - - frame.style.minimal_width = 300 - - --Header - local top_flow = frame.add {type = 'flow', direction = 'horizontal'} - top_flow.style.horizontal_align = 'center' - top_flow.style.horizontally_stretchable = true - - local title_flow = top_flow.add {type = 'flow'} - title_flow.style.horizontal_align = 'center' - title_flow.style.top_padding = 8 - title_flow.style.horizontally_stretchable = false - - local title = title_flow.add {type = 'label', caption = 'Welcome to Space Race'} - title.style.font = 'default-large-bold' - - --Body - - local content_flow = frame.add {type = 'flow'} - content_flow.style.top_padding = 8 - content_flow.style.horizontal_align = 'center' - content_flow.style.horizontally_stretchable = true - - local label_flow = content_flow.add {type = 'flow', direction = 'vertical'} - label_flow.style.horizontal_align = 'center' - - label_flow.style.horizontally_stretchable = true - local label = label_flow.add {type = 'label', caption = 'Waiting for map to generate\nPlease wait\n'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' - label.style.font_color = Color.yellow - - local time = game.tick - primitives.started_tick - - if time > 60 then - local minutes = (time / 3600) - minutes = minutes - minutes % 1 - time = time - (minutes * 3600) - local seconds = (time / 60) - seconds = seconds - seconds % 1 - time = minutes .. ' minutes and ' .. seconds .. ' seconds' - else - local seconds = (time - (time % 60)) / 60 - time = seconds .. ' seconds' - end - - label = label_flow.add {type = 'label', caption = '[color=blue]Time elapsed: ' .. time .. ' [/color]'} - label.style.horizontal_align = 'center' - label.style.single_line = false - label.style.font = 'default' -end - -local function show_waiting_gui() - for _, player in pairs(game.connected_players) do - loading_gui({player_index = player.index}) - end -end - -local function remove_waiting_gui() - for _, player in pairs(game.connected_players) do - local center = player.gui.center - local gui = center['Space-Race-Waiting'] - if (gui) then - Gui.destroy(gui) - end - end -end - local check_map_gen_is_done check_map_gen_is_done = Token.register( @@ -625,24 +368,24 @@ check_map_gen_is_done = local num_players = num_usa_players + num_ussr_players if not primitives.game_started and num_players >= players_needed then local surface = RS.get_surface() - if surface.get_tile({388.5, 0}).name == 'landfill' and surface.get_tile({-388.5, 0}).name == 'landfill' then + if surface.get_tile({388.5, 0}).name == 'landfill' and surface.get_tile({-388.5, 0}).name == 'landfill' and surface.get_tile({388.5, 60}).name == 'out-of-map' and surface.get_tile({-388.5, 60}).name == 'out-of-map' then primitives.started_tick = 0 - remove_waiting_gui() + load_gui.remove_gui() Event.remove_removable_nth_tick(60, check_map_gen_is_done) start_game() return end - show_waiting_gui() + load_gui.show_gui_to_all() else primitives.started_tick = 0 - remove_waiting_gui() + load_gui.remove_gui() Event.remove_removable_nth_tick(60, check_map_gen_is_done) end end ) local function check_ready_to_start() - if not primitives.game_started then + if primitives.game_started then return end local num_usa_players = #primitives.force_USA.connected_players @@ -725,6 +468,16 @@ end Command.add('join-UFE', {description = 'Use to join Union of Factory Employees in Space Race', capture_excess_arguments = false, allowed_by_server = false}, Public.join_ussr) +local function print_player_num(_, player) + local force_USA = primitives.force_USA + local force_USSR = primitives.force_USSR + + player.print('West: ' .. #force_USA.connected_players ..' online /' .. #force_USA.players .. ' total') + player.print('East: ' .. #force_USSR.connected_players ..' online /' .. #force_USSR.players .. ' total') +end + +Command.add('team_players', {description = 'Use to get number of players on both teams', capture_excess_arguments = false, allowed_by_server = false}, print_player_num) + function Public.get_won() return primitives.won end @@ -737,12 +490,14 @@ function Public.get_game_status() return primitives.game_started end +function Public.get_started_tick() + return primitives.started_tick +end + remote.add_interface('space-race', Public) --[[TODO -Introduction / Map information - Starting trees!