1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-24 03:47:58 +02:00

462 lines
16 KiB
Lua
Raw Normal View History

2020-01-02 16:09:24 +01:00
local Public = {}
2020-01-04 21:24:19 +01:00
local Server = require 'utils.server'
2020-01-02 16:09:24 +01:00
2019-11-23 11:45:43 +01:00
local bb_config = require "maps.biter_battles_v2.config"
local event = require 'utils.event'
2020-04-18 07:47:56 +02:00
local Functions = require "maps.biter_battles_v2.functions"
2019-03-15 21:50:49 +01:00
local feed_the_biters = require "maps.biter_battles_v2.feeding"
2020-01-01 18:47:08 +01:00
local Tables = require "maps.biter_battles_v2.tables"
local wait_messages = Tables.wait_messages
local food_names = Tables.gui_foods
local math_random = math.random
2019-03-15 03:59:43 +01:00
require "maps.biter_battles_v2.spec_spy"
2019-03-15 20:11:56 +01:00
local gui_values = {
2019-09-09 22:41:25 +02:00
["north"] = {force = "north", biter_force = "north_biters", c1 = bb_config.north_side_team_name, c2 = "JOIN ", n1 = "join_north_button",
t1 = "Evolution of north side biters.",
t2 = "Threat causes biters to attack. Reduces when biters are slain.", color1 = {r = 0.55, g = 0.55, b = 0.99}, color2 = {r = 0.66, g = 0.66, b = 0.99},
tech_spy = "spy-north-tech", prod_spy = "spy-north-prod"},
2019-09-09 22:41:25 +02:00
["south"] = {force = "south", biter_force = "south_biters", c1 = bb_config.south_side_team_name, c2 = "JOIN ", n1 = "join_south_button",
t1 = "Evolution of south side biters.",
t2 = "Threat causes biters to attack. Reduces when biters are slain.", color1 = {r = 0.99, g = 0.33, b = 0.33}, color2 = {r = 0.99, g = 0.44, b = 0.44},
tech_spy = "spy-south-tech", prod_spy = "spy-south-prod"}
}
2019-03-19 21:00:30 +01:00
2019-03-14 22:50:09 +01:00
local function create_sprite_button(player)
if player.gui.top["bb_toggle_button"] then return end
2019-03-16 20:25:21 +01:00
local button = player.gui.top.add({type = "sprite-button", name = "bb_toggle_button", sprite = "entity/big-spitter"})
2019-03-14 22:50:09 +01:00
button.style.font = "default-bold"
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
2019-03-14 22:50:09 +01:00
end
local function create_first_join_gui(player)
if not global.game_lobby_timeout then global.game_lobby_timeout = 5999940 end
if global.game_lobby_timeout - game.tick < 0 then global.game_lobby_active = false end
local frame = player.gui.left.add { type = "frame", name = "bb_main_gui", direction = "vertical" }
2019-03-15 21:50:49 +01:00
local b = frame.add{ type = "label", caption = "Defend your Rocket Silo!" }
b.style.font = "heading-1"
2019-03-15 20:11:56 +01:00
b.style.font_color = {r=0.98, g=0.66, b=0.22}
local b = frame.add { type = "label", caption = "Feed the enemy team's biters to gain advantage!" }
2019-03-15 21:50:49 +01:00
b.style.font = "heading-2"
2019-03-15 20:11:56 +01:00
b.style.font_color = {r=0.98, g=0.66, b=0.22}
2019-09-09 22:41:25 +02:00
frame.add { type = "label", caption = "-----------------------------------------------------------"}
2019-03-15 20:11:56 +01:00
for _, gui_value in pairs(gui_values) do
2019-09-14 04:10:36 +02:00
local t = frame.add { type = "table", column_count = 3 }
local c = gui_value.c1
if global.tm_custom_name[gui_value.force] then c = global.tm_custom_name[gui_value.force] end
local l = t.add { type = "label", caption = c}
2019-03-15 21:50:49 +01:00
l.style.font = "heading-2"
2019-03-15 20:11:56 +01:00
l.style.font_color = gui_value.color1
2019-09-09 22:41:25 +02:00
l.style.single_line = false
l.style.maximal_width = 290
2019-03-15 20:11:56 +01:00
local l = t.add { type = "label", caption = " - "}
local l = t.add { type = "label", caption = #game.forces[gui_value.force].connected_players .. " Players "}
l.style.font_color = { r=0.22, g=0.88, b=0.22}
local c = gui_value.c2
2019-03-15 20:11:56 +01:00
local font_color = gui_value.color1
if global.game_lobby_active then
font_color = {r=0.7, g=0.7, b=0.7}
c = c .. " (waiting for players... "
c = c .. math.ceil((global.game_lobby_timeout - game.tick)/60)
c = c .. ")"
end
local t = frame.add { type = "table", column_count = 4 }
2019-03-16 20:25:21 +01:00
for _, p in pairs(game.forces[gui_value.force].connected_players) do
2019-03-15 20:11:56 +01:00
local l = t.add({type = "label", caption = p.name})
l.style.font_color = {r = p.color.r * 0.6 + 0.4, g = p.color.g * 0.6 + 0.4, b = p.color.b * 0.6 + 0.4, a = 1}
2019-03-15 21:50:49 +01:00
l.style.font = "heading-2"
end
2019-03-15 20:11:56 +01:00
local b = frame.add { type = "sprite-button", name = gui_value.n1, caption = c }
b.style.font = "default-large-bold"
b.style.font_color = font_color
b.style.minimal_width = 350
2019-09-09 22:41:25 +02:00
frame.add { type = "label", caption = "-----------------------------------------------------------"}
end
end
local function add_tech_button(elem, gui_value)
local tech_button = elem.add {
type = "sprite-button",
name = gui_value.tech_spy,
sprite = "item/space-science-pack"
}
tech_button.style.height = 25
tech_button.style.width = 25
tech_button.style.left_margin = 3
end
local function add_prod_button(elem, gui_value)
local prod_button = elem.add {
type = "sprite-button",
name = gui_value.prod_spy,
sprite = "item/assembling-machine-3"
}
prod_button.style.height = 25
prod_button.style.width = 25
2019-03-14 22:50:09 +01:00
end
2020-04-21 15:13:19 +02:00
function Public.create_main_gui(player)
local is_spec = player.force.name == "spectator"
2019-03-14 22:50:09 +01:00
if player.gui.left["bb_main_gui"] then player.gui.left["bb_main_gui"].destroy() end
2019-03-16 07:31:34 +01:00
if global.bb_game_won_by_team then return end
2019-09-01 02:42:14 +02:00
if not global.chosen_team[player.name] then
if not global.tournament_mode then
create_first_join_gui(player)
2019-09-01 02:42:14 +02:00
return
end
end
2019-03-14 22:50:09 +01:00
local frame = player.gui.left.add { type = "frame", name = "bb_main_gui", direction = "vertical" }
-- Science sending GUI
if not is_spec then
2019-03-14 22:50:09 +01:00
frame.add { type = "table", name = "biter_battle_table", column_count = 4 }
local t = frame.biter_battle_table
2020-01-01 18:47:08 +01:00
for food_name, tooltip in pairs(food_names) do
local s = t.add { type = "sprite-button", name = food_name, sprite = "item/" .. food_name }
s.tooltip = tooltip
2019-09-09 22:41:25 +02:00
s.style.minimal_height = 41
s.style.minimal_width = 41
2019-03-15 21:50:49 +01:00
s.style.top_padding = 0
s.style.left_padding = 0
s.style.right_padding = 0
s.style.bottom_padding = 0
2019-03-14 22:50:09 +01:00
end
end
local first_team = true
for k, gui_value in pairs(gui_values) do
-- Line separator
if not first_team then
frame.add { type = "line", caption = "this line", direction = "horizontal" }
else
first_team = false
end
-- Team name & Player count
local t = frame.add { type = "table", column_count = 4 }
-- Team name
2019-09-14 04:10:36 +02:00
local c = gui_value.c1
if global.tm_custom_name[gui_value.force] then c = global.tm_custom_name[gui_value.force] end
local l = t.add { type = "label", caption = c}
2019-03-15 17:56:08 +01:00
l.style.font = "default-bold"
2019-03-15 20:11:56 +01:00
l.style.font_color = gui_value.color1
2019-09-09 22:41:25 +02:00
l.style.single_line = false
l.style.maximal_width = 102
-- Number of players
2019-09-09 22:41:25 +02:00
local l = t.add { type = "label", caption = " - "}
local c = #game.forces[gui_value.force].connected_players .. " Player"
if #game.forces[gui_value.force].connected_players ~= 1 then c = c .. "s" end
local l = t.add { type = "label", caption = c}
l.style.font = "default"
l.style.font_color = { r=0.22, g=0.88, b=0.22}
-- Tech button
if is_spec then
add_tech_button(t, gui_value)
-- add_prod_button(t, gui_value)
end
-- Player list
if global.bb_view_players[player.name] == true then
local t = frame.add { type = "table", column_count = 4 }
2019-03-15 20:11:56 +01:00
for _, p in pairs(game.forces[gui_value.force].connected_players) do
2019-03-15 17:56:08 +01:00
local l = t.add { type = "label", caption = p.name }
2019-03-15 20:11:56 +01:00
l.style.font_color = {r = p.color.r * 0.6 + 0.4, g = p.color.g * 0.6 + 0.4, b = p.color.b * 0.6 + 0.4, a = 1}
2019-03-15 17:56:08 +01:00
end
2019-03-14 22:50:09 +01:00
end
-- Statistics
local t = frame.add { type = "table", name = "stats_" .. gui_value.force, column_count = 5 }
-- Evolution
2019-03-15 21:50:49 +01:00
local l = t.add { type = "label", caption = "Evo:"}
2019-03-15 20:11:56 +01:00
--l.style.minimal_width = 25
local biter_force = game.forces[gui_value.biter_force]
2020-04-21 15:13:19 +02:00
local tooltip = gui_value.t1 .. "\nHealth: " .. Functions.get_health_modifier(biter_force) * 100 .. "%" .. "\nDamage: " .. (biter_force.get_ammo_damage_modifier("melee") + 1) * 100 .. "%"
l.tooltip = tooltip
2019-03-17 21:39:05 +01:00
local evo = math.floor(1000 * global.bb_evolution[gui_value.biter_force]) * 0.1
local l = t.add {type = "label", caption = evo .. "%"}
l.style.minimal_width = 40
2019-03-15 20:11:56 +01:00
l.style.font_color = gui_value.color2
2019-03-15 17:56:08 +01:00
l.style.font = "default-bold"
l.tooltip = tooltip
-- Threat
2019-03-17 21:39:05 +01:00
local l = t.add {type = "label", caption = "Threat: "}
2019-03-15 17:56:08 +01:00
l.style.minimal_width = 25
2019-03-15 20:11:56 +01:00
l.tooltip = gui_value.t2
local l = t.add {type = "label", name = "threat_" .. gui_value.force, caption = math.floor(global.bb_threat[gui_value.biter_force])}
2019-03-15 20:11:56 +01:00
l.style.font_color = gui_value.color2
2019-03-15 17:56:08 +01:00
l.style.font = "default-bold"
l.style.width = 50
2019-03-15 20:11:56 +01:00
l.tooltip = gui_value.t2
2019-03-14 22:50:09 +01:00
end
-- Action frame
2019-03-14 22:50:09 +01:00
local t = frame.add { type = "table", column_count = 2 }
-- Spectate / Rejoin team
if is_spec then
2019-03-14 22:50:09 +01:00
local b = t.add { type = "sprite-button", name = "bb_leave_spectate", caption = "Join Team" }
else
local b = t.add { type = "sprite-button", name = "bb_spectate", caption = "Spectate" }
end
-- Playerlist button
2019-03-14 22:50:09 +01:00
if global.bb_view_players[player.name] == true then
local b = t.add { type = "sprite-button", name = "bb_hide_players", caption = "Playerlist" }
else
local b = t.add { type = "sprite-button", name = "bb_view_players", caption = "Playerlist" }
end
local b_width = is_spec and 97 or 86
-- 111 when prod_spy button will be there
2019-03-14 22:50:09 +01:00
for _, b in pairs(t.children) do
b.style.font = "default-bold"
b.style.font_color = { r=0.98, g=0.66, b=0.22}
b.style.top_padding = 1
b.style.left_padding = 1
b.style.right_padding = 1
b.style.bottom_padding = 1
b.style.maximal_height = 30
b.style.width = b_width
2019-03-14 22:50:09 +01:00
end
end
2020-01-02 16:09:24 +01:00
function Public.refresh()
2019-03-14 22:50:09 +01:00
for _, player in pairs(game.connected_players) do
if player.gui.left["bb_main_gui"] then
2020-04-21 15:13:19 +02:00
Public.create_main_gui(player)
2019-03-14 22:50:09 +01:00
end
end
2019-09-16 18:14:27 +02:00
global.gui_refresh_delay = game.tick + 5
end
2020-01-02 16:28:12 +01:00
function Public.refresh_threat()
2019-09-16 18:14:27 +02:00
if global.gui_refresh_delay > game.tick then return end
for _, player in pairs(game.connected_players) do
if player.gui.left["bb_main_gui"] then
if player.gui.left["bb_main_gui"].stats_north then
player.gui.left["bb_main_gui"].stats_north.threat_north.caption = math.floor(global.bb_threat["north_biters"])
player.gui.left["bb_main_gui"].stats_south.threat_south.caption = math.floor(global.bb_threat["south_biters"])
end
end
end
global.gui_refresh_delay = game.tick + 5
2019-03-14 22:50:09 +01:00
end
2019-09-01 02:42:14 +02:00
function join_team(player, force_name, forced_join)
if not player.character then return end
2019-09-01 02:42:14 +02:00
if not forced_join then
if global.tournament_mode then player.print("The game is set to tournament mode. Teams can only be changed via team manager.", {r = 0.98, g = 0.66, b = 0.22}) return end
2019-09-01 02:42:14 +02:00
end
2019-03-16 03:44:47 +01:00
if not force_name then return end
2019-03-14 22:50:09 +01:00
local surface = player.surface
2019-03-15 01:39:49 +01:00
local enemy_team = "south"
if force_name == "south" then enemy_team = "north" end
2019-11-29 05:56:24 +01:00
if not global.training_mode and global.bb_settings.team_balancing then
2019-09-01 02:42:14 +02:00
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
player.print("Team " .. force_name .. " has too many players currently.", {r = 0.98, g = 0.66, b = 0.22})
return
end
2019-04-28 19:38:44 +02:00
end
2019-03-17 21:39:05 +01:00
end
2019-03-15 01:39:49 +01:00
end
2019-09-01 02:42:14 +02:00
if global.chosen_team[player.name] then
if not forced_join then
if game.tick - global.spectator_rejoin_delay[player.name] < 3600 then
player.print(
"Not ready to return to your team yet. Please wait " .. 60-(math.floor((game.tick - global.spectator_rejoin_delay[player.name])/60)) .. " seconds.",
{r = 0.98, g = 0.66, b = 0.22}
)
return
end
2019-03-15 01:39:49 +01:00
end
2019-11-20 03:40:54 +01:00
local p = surface.find_non_colliding_position("character", game.forces[force_name].get_spawn_position(surface), 8, 1)
player.teleport(p, surface)
2019-03-14 22:50:09 +01:00
player.force = game.forces[force_name]
2019-03-16 03:44:47 +01:00
player.character.destructible = true
2020-01-02 16:09:24 +01:00
Public.refresh()
game.permissions.get_group("Default").add_player(player)
local msg = table.concat({"Team ", player.force.name, " player ", player.name, " is no longer spectating."})
game.print(msg, {r = 0.98, g = 0.66, b = 0.22})
Server.to_discord_bold(msg)
2019-10-01 03:16:56 +02:00
player.spectator = false
2019-03-14 22:50:09 +01:00
return
end
2019-11-20 03:40:54 +01:00
local pos = surface.find_non_colliding_position("character", game.forces[force_name].get_spawn_position(surface), 8, 1)
2019-03-21 10:06:12 +01:00
if not pos then pos = game.forces[force_name].get_spawn_position(surface) end
player.teleport(pos)
2019-03-16 02:36:06 +01:00
player.force = game.forces[force_name]
2019-03-16 03:44:47 +01:00
player.character.destructible = true
game.permissions.get_group("Default").add_player(player)
2019-09-01 02:42:14 +02:00
if not forced_join then
local c = player.force.name
if global.tm_custom_name[player.force.name] then c = global.tm_custom_name[player.force.name] end
2020-01-04 21:52:33 +01:00
local message = table.concat({player.name, " has joined team ", c, "!"})
2020-01-04 21:24:19 +01:00
game.print(message, {r = 0.98, g = 0.66, b = 0.22})
2020-01-06 03:20:48 +01:00
Server.to_discord_bold(message)
2019-09-01 02:42:14 +02:00
end
local i = player.get_inventory(defines.inventory.character_main)
2019-03-15 01:39:49 +01:00
i.clear()
player.insert {name = 'pistol', count = 1}
player.insert {name = 'raw-fish', count = 3}
player.insert {name = 'firearm-magazine', count = 32}
2019-03-16 09:14:41 +01:00
player.insert {name = 'iron-gear-wheel', count = 8}
player.insert {name = 'iron-plate', count = 16}
2019-03-15 21:50:49 +01:00
global.chosen_team[player.name] = force_name
2019-10-01 03:16:56 +02:00
player.spectator = false
2020-01-02 16:09:24 +01:00
Public.refresh()
2019-03-15 01:39:49 +01:00
end
2019-09-01 02:42:14 +02:00
function spectate(player, forced_join)
if not player.character then return end
2019-09-01 02:42:14 +02:00
if not forced_join then
if global.tournament_mode then player.print("The game is set to tournament mode. Teams can only be changed via team manager.", {r = 0.98, g = 0.66, b = 0.22}) return end
2019-09-01 02:42:14 +02:00
end
player.teleport(player.surface.find_non_colliding_position("character", {0,0}, 4, 1))
2019-03-15 01:39:49 +01:00
player.force = game.forces.spectator
2019-03-16 03:44:47 +01:00
player.character.destructible = false
2019-09-01 02:42:14 +02:00
if not forced_join then
local msg = player.name .. " is spectating."
game.print(msg, {r = 0.98, g = 0.66, b = 0.22})
Server.to_discord_bold(msg)
end
game.permissions.get_group("spectator").add_player(player)
2019-03-15 01:39:49 +01:00
global.spectator_rejoin_delay[player.name] = game.tick
2020-04-21 15:13:19 +02:00
Public.create_main_gui(player)
2019-10-01 03:16:56 +02:00
player.spectator = true
2019-03-14 22:50:09 +01:00
end
local function join_gui_click(name, player)
2019-03-14 22:50:09 +01:00
local team = {
["join_north_button"] = "north",
["join_south_button"] = "south"
2019-03-16 20:25:21 +01:00
}
if not team[name] then return end
2019-03-14 22:50:09 +01:00
if global.game_lobby_active then
if player.admin then
join_team(player, team[name])
game.print("Lobby disabled, admin override.", { r=0.98, g=0.66, b=0.22})
global.game_lobby_active = false
return
end
player.print("Waiting for more players, " .. wait_messages[math_random(1, #wait_messages)], { r=0.98, g=0.66, b=0.22})
2019-03-14 22:50:09 +01:00
return
end
join_team(player, team[name])
end
2020-01-02 16:09:24 +01:00
local spy_forces = {{"north", "south"},{"south", "north"}}
function Public.spy_fish()
for _, f in pairs(spy_forces) do
if global.spy_fish_timeout[f[1]] - game.tick > 0 then
local r = 96
local surface = game.surfaces["biter_battles"]
for _, player in pairs(game.forces[f[2]].connected_players) do
game.forces[f[1]].chart(surface, {{player.position.x - r, player.position.y - r}, {player.position.x + r, player.position.y + r}})
end
else
global.spy_fish_timeout[f[1]] = 0
end
end
end
2019-03-14 04:25:54 +01:00
local function on_gui_click(event)
2019-03-14 22:50:09 +01:00
if not event.element then return end
if not event.element.valid then return end
local player = game.players[event.player_index]
local name = event.element.name
if name == "bb_toggle_button" then
if player.gui.left["bb_main_gui"] then
player.gui.left["bb_main_gui"].destroy()
else
2020-04-21 15:13:19 +02:00
Public.create_main_gui(player)
2019-03-14 22:50:09 +01:00
end
return
end
2019-09-01 02:42:14 +02:00
if name == "join_north_button" then join_gui_click(name, player) return end
if name == "join_south_button" then join_gui_click(name, player) return end
2020-04-18 07:47:56 +02:00
if name == "raw-fish" then Functions.spy_fish(player) return end
2019-09-01 02:42:14 +02:00
if food_names[name] then feed_the_biters(player, name) return end
2019-03-15 01:39:49 +01:00
if name == "bb_leave_spectate" then join_team(player, global.chosen_team[player.name]) end
2019-03-14 22:50:09 +01:00
if name == "bb_spectate" then
2019-03-15 01:39:49 +01:00
if player.position.y ^ 2 + player.position.x ^ 2 < 12000 then
spectate(player)
2019-03-14 22:50:09 +01:00
else
player.print("You are too far away from spawn to spectate.",{ r=0.98, g=0.66, b=0.22})
end
2019-03-15 01:39:49 +01:00
return
end
if name == "bb_hide_players" then
global.bb_view_players[player.name] = false
2020-04-21 15:13:19 +02:00
Public.create_main_gui(player)
2019-03-15 01:39:49 +01:00
end
if name == "bb_view_players" then
global.bb_view_players[player.name] = true
2020-04-21 15:13:19 +02:00
Public.create_main_gui(player)
2019-03-16 20:25:21 +01:00
end
2019-03-14 22:50:09 +01:00
end
local function on_player_joined_game(event)
local player = game.players[event.player_index]
2019-03-14 22:50:09 +01:00
if not global.bb_view_players then global.bb_view_players = {} end
2019-03-15 01:39:49 +01:00
if not global.chosen_team then global.chosen_team = {} end
2019-03-14 22:50:09 +01:00
global.bb_view_players[player.name] = false
2019-03-15 03:59:43 +01:00
if #game.connected_players > 1 then
2019-03-17 02:35:58 +01:00
global.game_lobby_timeout = math.ceil(36000 / #game.connected_players)
2019-03-15 03:59:43 +01:00
else
2019-09-09 22:41:25 +02:00
global.game_lobby_timeout = 599940
2019-03-15 03:59:43 +01:00
end
2019-09-01 02:42:14 +02:00
--if not global.chosen_team[player.name] then
-- if global.tournament_mode then
-- player.force = game.forces.spectator
-- else
-- player.force = game.forces.player
-- end
--end
2019-03-14 22:50:09 +01:00
create_sprite_button(player)
2020-04-21 15:13:19 +02:00
Public.create_main_gui(player)
2019-03-14 04:25:54 +01:00
end
event.add(defines.events.on_gui_click, on_gui_click)
2019-03-14 22:50:09 +01:00
event.add(defines.events.on_player_joined_game, on_player_joined_game)
return Public