2021-03-24 16:46:00 +01:00
require ' modules.custom_death_messages '
require ' modules.flashlight_toggle_button '
require ' modules.global_chat_toggle '
require ' modules.biters_yield_coins '
2022-09-30 22:23:37 +02:00
require ' maps.scrap_towny_ffa.reset '
require ' maps.scrap_towny_ffa.mining '
require ' maps.scrap_towny_ffa.building '
require ' maps.scrap_towny_ffa.spaceship '
require ' maps.scrap_towny_ffa.town_center '
require ' maps.scrap_towny_ffa.market '
require ' maps.scrap_towny_ffa.slots '
require ' maps.scrap_towny_ffa.wreckage_yields_scrap '
require ' maps.scrap_towny_ffa.rocks_yield_ore_veins '
2022-10-04 16:03:00 +01:00
require ' maps.scrap_towny_ffa.worms_create_oil_patches '
2022-09-30 22:23:37 +02:00
require ' maps.scrap_towny_ffa.spawners_contain_biters '
require ' maps.scrap_towny_ffa.explosives_are_explosive '
require ' maps.scrap_towny_ffa.fluids_are_explosive '
require ' maps.scrap_towny_ffa.trap '
require ' maps.scrap_towny_ffa.turrets_drop_ammo '
require ' maps.scrap_towny_ffa.vehicles '
2022-10-19 22:51:01 -04:00
require ' maps.scrap_towny_ffa.suicide '
2021-03-24 16:46:00 +01:00
2022-09-30 22:23:37 +02:00
local Event = require ' utils.event '
2022-10-21 21:49:17 +02:00
local Alert = require ' utils.alert '
2022-03-01 18:55:02 +01:00
local Autostash = require ' modules.autostash '
2022-09-30 22:23:37 +02:00
local MapDefaults = require ' maps.scrap_towny_ffa.map_defaults '
2022-04-05 19:28:08 +02:00
local BottomFrame = require ' utils.gui.bottom_frame '
2022-09-30 22:23:37 +02:00
local ScenarioTable = require ' maps.scrap_towny_ffa.table '
local Nauvis = require ' maps.scrap_towny_ffa.nauvis '
local Biters = require ' maps.scrap_towny_ffa.biters '
local Pollution = require ' maps.scrap_towny_ffa.pollution '
local Fish = require ' maps.scrap_towny_ffa.fish_reproduction '
local Team = require ' maps.scrap_towny_ffa.team '
local Radar = require ' maps.scrap_towny_ffa.limited_radar '
local Limbo = require ' maps.scrap_towny_ffa.limbo '
local Evolution = require ' maps.scrap_towny_ffa.evolution '
2022-09-22 20:19:19 +02:00
local mod_gui = require ( ' mod-gui ' )
2022-09-22 20:42:46 +02:00
local Gui = require ' utils.gui '
local Color = require ' utils.color_presets '
2022-10-21 21:49:17 +02:00
local Server = require ' utils.server '
2022-09-26 19:48:50 +02:00
local Where = require ' utils.commands.where '
local Inventory = require ' modules.show_inventory '
2021-03-24 16:46:00 +01:00
2022-10-21 21:49:17 +02:00
local function spairs ( t )
2022-09-22 20:19:19 +02:00
local keys = { }
for k in pairs ( t ) do
keys [ # keys + 1 ] = k
end
2022-10-21 21:49:17 +02:00
table.sort (
keys ,
function ( a , b )
return t [ b ] < t [ a ]
end
)
2022-09-22 20:19:19 +02:00
local i = 0
return function ( )
i = i + 1
if keys [ i ] then
return keys [ i ] , t [ keys [ i ] ]
end
end
end
local function init_score_board ( player )
2022-09-30 22:23:37 +02:00
local this = ScenarioTable.get_table ( )
local saved_frame = this.score_gui_frame [ player.index ]
2022-09-22 22:14:23 +02:00
if saved_frame and saved_frame.valid then
return
end
2022-09-22 20:19:19 +02:00
local flow = mod_gui.get_frame_flow ( player )
local frame = flow.add { type = ' frame ' , style = mod_gui.frame_style , caption = ' Town survival ' , direction = ' vertical ' }
frame.style . vertically_stretchable = false
2022-09-30 22:23:37 +02:00
this.score_gui_frame [ player.index ] = frame
2022-09-22 20:19:19 +02:00
end
local function update_score ( )
2022-09-30 22:23:37 +02:00
local this = ScenarioTable.get_table ( )
2022-09-22 20:19:19 +02:00
2022-10-21 21:49:17 +02:00
if this.winner then
if not this.game_won then
this.game_won = true
this.game_reset_tick = 5400
Alert.alert_all_players ( 900 , ' Winner winner chicken dinner! \n [color=red] ' .. this.winner . name .. ' [/color] has won the game! ' , Color.white , ' restart_required ' , 1.0 )
for _ , player in pairs ( game.connected_players ) do
player.play_sound { path = ' utility/game_won ' , volume_modifier = 0.75 }
end
local winner = this.winner
local message = {
title = ' Game over ' ,
description = ' Town statistics is below ' ,
color = ' success ' ,
field1 = {
text1 = ' Town won: ' ,
text2 = winner.name ,
inline = ' false '
} ,
field2 = {
text1 = ' Town researched: ' ,
text2 = winner.name .. ' with a score of ' .. winner.research_counter .. ' techs! ' ,
inline = ' false '
} ,
field3 = {
text1 = ' Town upgrades: ' ,
text2 = winner.name .. ' upgraded their town with: \n Crafting speed: ' .. winner.upgrades . crafting_speed .. ' \n Mining speed: ' .. winner.upgrades . mining_speed ,
inline = ' false '
} ,
field4 = {
text1 = ' Town health: ' ,
text2 = winner.name .. ' had a health of ' .. winner.health .. ' left! ' ,
inline = ' false '
} ,
field5 = {
text1 = ' Town coins: ' ,
text2 = winner.name .. ' had ' .. winner.coin_balance .. ' coins stashed! ' ,
inline = ' false '
}
}
Server.to_discord_embed_parsed ( message )
end
return
end
2022-09-22 20:19:19 +02:00
for _ , player in pairs ( game.connected_players ) do
2022-09-30 22:23:37 +02:00
local frame = this.score_gui_frame [ player.index ]
2022-09-22 20:19:19 +02:00
if not ( frame and frame.valid ) then
init_score_board ( player )
end
2022-09-22 20:24:55 +02:00
if frame and frame.valid then
frame.clear ( )
2022-09-22 20:19:19 +02:00
2022-09-22 20:24:55 +02:00
local inner_frame = frame.add { type = ' frame ' , style = ' inside_shallow_frame ' , direction = ' vertical ' }
2022-09-22 20:19:19 +02:00
2022-09-22 20:24:55 +02:00
local subheader = inner_frame.add { type = ' frame ' , style = ' subheader_frame ' }
subheader.style . horizontally_stretchable = true
subheader.style . vertical_align = ' center '
2022-09-22 20:19:19 +02:00
2022-09-28 15:01:04 +01:00
subheader.add { type = ' label ' , style = ' subheader_label ' , caption = { ' ' , ' Survive 2 days (48h) to win! ' } }
2022-09-22 20:19:19 +02:00
2022-09-22 20:24:55 +02:00
if not next ( subheader.children ) then
subheader.destroy ( )
2022-09-22 20:19:19 +02:00
end
2022-09-22 20:24:55 +02:00
local information_table = inner_frame.add { type = ' table ' , column_count = 3 , style = ' bordered_table ' }
information_table.style . margin = 4
information_table.style . column_alignments [ 3 ] = ' right '
2022-09-22 20:19:19 +02:00
2022-09-28 10:27:58 +01:00
for _ , caption in pairs ( { ' Rank ' , ' Town (players online/total) ' , ' Survival time ' } ) do
2022-09-22 20:24:55 +02:00
local label = information_table.add { type = ' label ' , caption = caption }
label.style . font = ' default-bold '
2022-09-22 20:19:19 +02:00
end
2022-09-22 20:24:55 +02:00
local town_ages = { }
2022-09-30 22:23:37 +02:00
for _ , town_center in pairs ( this.town_centers ) do
2022-09-22 20:24:55 +02:00
if town_center ~= nil then
local age = game.tick - town_center.creation_tick
town_ages [ town_center ] = age
end
2022-09-22 20:19:19 +02:00
end
2022-09-22 20:24:55 +02:00
local rank = 1
2022-10-21 21:49:17 +02:00
for town_center , age in spairs ( town_ages ) do
2022-09-22 20:24:55 +02:00
local position = information_table.add { type = ' label ' , caption = ' # ' .. rank }
2022-09-30 22:23:37 +02:00
if town_center == this.town_centers [ player.force . name ] then
2022-09-22 20:24:55 +02:00
position.style . font = ' default-semibold '
position.style . font_color = { r = 1 , g = 1 }
end
2022-09-30 22:23:37 +02:00
local label =
information_table.add {
type = ' label ' ,
caption = town_center.town_name .. ' ( ' .. # town_center.market . force.connected_players .. ' / ' .. # town_center.market . force.players .. ' ) '
}
2022-09-22 20:24:55 +02:00
label.style . font = ' default-semibold '
label.style . font_color = town_center.color
local age_hours = age / 60 / 3600
2022-10-21 21:49:17 +02:00
local total_age = string.format ( ' %.1f ' , age_hours )
information_table.add { type = ' label ' , caption = total_age .. ' h ' }
2022-09-22 20:24:55 +02:00
rank = rank + 1
2022-10-21 21:49:17 +02:00
if tonumber ( total_age ) >= this.required_time_to_win then
this.winner = {
name = town_center.town_name ,
research_counter = town_center.research_counter ,
upgrades = town_center.upgrades ,
health = town_center.health ,
coin_balance = town_center.coin_balance
}
end
2022-09-22 20:24:55 +02:00
end
2022-09-28 10:27:58 +01:00
-- Outlander section
information_table.add { type = ' label ' , caption = ' - ' }
2022-09-30 22:23:37 +02:00
local outlander_on = # game.forces [ ' player ' ] . connected_players + # game.forces [ ' rogue ' ] . connected_players
local outlander_total = # game.forces [ ' player ' ] . players + # game.forces [ ' rogue ' ] . players
2022-09-28 10:27:58 +01:00
2022-09-30 22:23:37 +02:00
local label =
information_table.add {
type = ' label ' ,
caption = ' Outlanders ' .. ' ( ' .. outlander_on .. ' / ' .. outlander_total .. ' ) '
}
2022-09-28 10:27:58 +01:00
label.style . font_color = { 170 , 170 , 170 }
information_table.add { type = ' label ' , caption = ' - ' }
2021-03-24 16:46:00 +01:00
end
end
2021-02-06 12:42:10 -08:00
end
local function on_init ( )
2022-03-01 18:55:02 +01:00
Autostash.insert_into_furnace ( true )
2022-10-22 22:16:21 +02:00
Autostash.insert_to_neutral_chests ( true )
2022-03-01 18:55:02 +01:00
Autostash.insert_into_wagon ( true )
Autostash.bottom_button ( true )
BottomFrame.reset ( )
BottomFrame.activate_custom_buttons ( true )
2022-09-26 19:48:50 +02:00
Where.module_disabled ( true )
Inventory.module_disabled ( true )
2022-03-01 18:55:02 +01:00
2021-03-24 16:46:00 +01:00
--log("on_init")
game.enemy_has_vision_on_land_mines = false
game.draw_resource_selection = true
game.disable_tutorial_triggers ( )
2021-02-06 12:42:10 -08:00
2022-09-30 22:23:37 +02:00
MapDefaults.initialize ( )
Limbo.initialize ( )
2021-03-24 16:46:00 +01:00
Nauvis.initialize ( )
Team.initialize ( )
2021-02-06 12:42:10 -08:00
end
local tick_actions = {
2021-03-24 16:46:00 +01:00
[ 60 * 0 ] = Radar.reset , -- each minute, at 00 seconds
[ 60 * 5 ] = Team.update_town_chart_tags , -- each minute, at 05 seconds
[ 60 * 10 ] = Team.set_all_player_colors , -- each minute, at 10 seconds
[ 60 * 15 ] = Fish.reproduce , -- each minute, at 15 seconds
[ 60 * 25 ] = Biters.unit_groups_start_moving , -- each minute, at 25 seconds
[ 60 * 30 ] = Radar.reset , -- each minute, at 30 seconds
[ 60 * 45 ] = Biters.validate_swarms , -- each minute, at 45 seconds
[ 60 * 50 ] = Biters.swarm , -- each minute, at 50 seconds
[ 60 * 55 ] = Pollution.market_scent -- each minute, at 55 seconds
2021-02-06 12:42:10 -08:00
}
local function on_nth_tick ( event )
2021-03-24 16:46:00 +01:00
-- run each second
local tick = event.tick
local seconds = tick % 3600 -- tick will recycle minute
if not tick_actions [ seconds ] then
return
end
2022-03-01 18:55:02 +01:00
--game.surfaces['nauvis'].play_sound({path = 'utility/alert_destroyed', volume_modifier = 1})
--log('seconds = ' .. seconds)
2021-03-24 16:46:00 +01:00
tick_actions [ seconds ] ( )
2021-02-06 12:42:10 -08:00
end
2022-10-21 21:54:06 +02:00
local function handle_changes ( )
ScenarioTable.set ( ' restart ' , true )
ScenarioTable.set ( ' soft_reset ' , false )
print ( ' Received new changes from backend. ' )
end
2022-09-22 20:19:19 +02:00
local function ui_smell_evolution ( )
for _ , player in pairs ( game.connected_players ) do
-- Only for non-townies
if player.force . index == game.forces . player.index or player.force . index == game.forces [ ' rogue ' ] . index then
local e = Evolution.get_evolution ( player.position )
local extra
if e < 0.1 then
extra = ' A good place to found a town. Build a furnace to get started. '
else
extra = ' Not good to start a new town. Maybe somewhere else? '
end
player.create_local_flying_text (
{
position = { x = player.position . x , y = player.position . y } ,
text = ' You smell the evolution around here: ' .. string.format ( ' %.0f ' , e * 100 ) .. ' %. ' .. extra ,
color = { r = 1 , g = 1 , b = 1 }
}
)
end
end
end
2021-02-06 12:42:10 -08:00
Event.on_init ( on_init )
2021-03-24 16:46:00 +01:00
Event.on_nth_tick ( 60 , on_nth_tick ) -- once every second
2022-09-22 20:19:19 +02:00
Event.on_nth_tick ( 60 * 30 , ui_smell_evolution )
Event.on_nth_tick ( 60 , update_score )
2022-10-21 21:54:06 +02:00
Event.add ( Server.events . on_changes_detected , handle_changes )
2022-09-22 20:42:46 +02:00
2022-10-21 21:49:17 +02:00
--Disable the comfy main gui since we got too many goodies there.
2022-09-22 20:42:46 +02:00
Event.add (
defines.events . on_gui_click ,
function ( event )
local element = event.element
if not element or not element.valid then
return
end
local fish_button = Gui.top_main_gui_button
local main_frame_name = Gui.main_frame_name
local player = game.get_player ( event.player_index )
if not player or not player.valid then
return
end
if element.name == fish_button then
if not player.admin then
if player.gui . left [ main_frame_name ] and player.gui . left [ main_frame_name ] . valid then
player.gui . left [ main_frame_name ] . destroy ( )
end
return player.print ( ' Comfy panel is disabled in this scenario. ' , Color.fail )
end
end
end
)
2022-09-30 22:23:37 +02:00
require ' maps.scrap_towny_ffa.scrap_towny_ffa_layout '