2022-06-01 20:50:36 +02:00
-- This file is part of thesixthroc's Pirate Ship softmod, licensed under GPLv3 and stored at https://github.com/danielmartin0/ComfyFactorio-Pirates.
2021-10-13 10:21:53 +02:00
2022-02-26 20:25:48 +02:00
--[[
2022-03-29 01:36:38 +02:00
Pirate Ship is maintained by thesixthroc and hosted by Comfy .
2022-02-26 20:25:48 +02:00
Want to host it ? Ask us at getcomfy.eu / discord !
] ]
2022-03-11 12:40:55 +02:00
--[[
2022-03-29 01:36:38 +02:00
2022-03-11 12:40:55 +02:00
== Tips for Developers ! ==
2022-03-29 01:36:38 +02:00
The scenario is quite complex , but there are ways to get started , even if you don ' t know much Lua. Some ideas (incomplete):
• Go to pirates / surfaces / islands / first and edit stuff there to see the effect it has on the first island
• Ask thesixthroc for access to the ToDo list on Github Projects , to see what needs doing
2022-05-29 13:36:27 +02:00
• Make sure to use debug = true in control.lua
2022-03-11 12:40:55 +02:00
] ]
2022-05-29 13:36:27 +02:00
--[[
personal note for thesixthroc looking at this in XX years : my design notes are all in Obsidian ( edit : mostly moved to Github Projects )
] ]
--[[
Convention for Factorio blueprints in this folder : Use Snap to grid -> Relative , Offset of zeroes .
We record tiles and entities separately . For tiles , we use the factorio dev approved ' concrete trick ' , painting each tile type separately as concrete . The concrete BP will typically need an offset , since it doesn ' t remember the center of the entities BP — we configure this offset in the Lua rather than the BP itself, since it ' s easier to edit that way .
] ]
2022-04-08 15:49:53 +02:00
2022-04-08 16:13:28 +02:00
2021-10-13 10:21:53 +02:00
-- require 'modules.biters_yield_coins'
require ' modules.biter_noms_you '
require ' modules.no_deconstruction_of_neutral_entities '
2022-03-29 01:36:38 +02:00
require ' maps.pirates.custom_events ' --probably do this before anything else
2022-03-13 03:44:32 +02:00
2022-03-19 23:20:55 +02:00
require ' utils.server '
local _inspect = require ' utils.inspect ' . inspect
2021-10-13 10:21:53 +02:00
-- local Modifers = require 'player_modifiers'
2022-04-08 15:49:53 +02:00
local BottomFrame = require ' utils.gui.bottom_frame '
2022-02-25 02:01:28 +02:00
local Autostash = require ' modules.autostash '
2022-03-19 23:20:55 +02:00
require ' modules.inserter_drops_pickup '
2021-10-13 10:21:53 +02:00
2022-02-26 20:25:48 +02:00
2022-05-29 13:36:27 +02:00
local PiratesApiOnTick = require ' maps.pirates.api_on_tick '
local ClassPiratesApiOnTick = require ' maps.pirates.roles.tick_functions '
2021-10-13 10:21:53 +02:00
2022-03-19 23:20:55 +02:00
require ' maps.pirates.commands '
require ' maps.pirates.math '
2021-10-13 10:21:53 +02:00
local Memory = require ' maps.pirates.memory '
2022-03-19 23:20:55 +02:00
require ' maps.pirates.gui.gui '
2021-10-13 10:21:53 +02:00
local Common = require ' maps.pirates.common '
local CoreData = require ' maps.pirates.coredata '
2022-03-19 23:20:55 +02:00
require ' maps.pirates.utils_local '
2021-10-13 10:21:53 +02:00
local Balance = require ' maps.pirates.balance '
local Crew = require ' maps.pirates.crew '
local Roles = require ' maps.pirates.roles.roles '
local Structures = require ' maps.pirates.structures.structures '
local Surfaces = require ' maps.pirates.surfaces.surfaces '
2022-07-30 11:29:17 +02:00
local Kraken = require ' maps.pirates.surfaces.sea.kraken '
2022-05-29 13:36:27 +02:00
local PiratesApiEvents = require ' maps.pirates.api_events '
2022-03-19 23:20:55 +02:00
require ' maps.pirates.structures.boats.boats '
2021-10-13 10:21:53 +02:00
local Progression = require ' maps.pirates.progression '
2022-05-29 13:36:27 +02:00
local QuestStructures = require ' maps.pirates.structures.quest_structures.quest_structures '
2021-10-13 10:21:53 +02:00
local Ai = require ' maps.pirates.ai '
2022-03-19 23:20:55 +02:00
require ' maps.pirates.ores '
require ' maps.pirates.quest '
require ' maps.pirates.parrot '
require ' maps.pirates.shop.shop '
require ' maps.pirates.boat_upgrades '
2021-10-13 10:21:53 +02:00
local Token = require ' utils.token '
local Task = require ' utils.task '
2022-07-30 22:31:00 +02:00
local Server = require ' utils.server '
2021-10-13 10:21:53 +02:00
2023-01-12 18:58:53 +02:00
local Math = require ' maps.pirates.math '
2022-03-13 12:55:04 +02:00
require ' utils.profiler '
2021-10-13 10:21:53 +02:00
local Public = { }
2022-02-26 20:25:48 +02:00
-- parrot sprites from https://elthen.itch.io/2d-pixel-art-parrot-sprites, licensed appropriately
2021-10-13 10:21:53 +02:00
local jetty_delayed = Token.register (
2022-03-19 23:20:55 +02:00
-- function(data)
function ( )
2021-10-13 10:21:53 +02:00
Surfaces.Lobby . place_lobby_jetty_and_boats ( )
end
)
local function on_init ( )
Memory.global_reset_memory ( )
local global_memory = Memory.get_global_memory ( )
game.reset_time_played ( )
-- local spectator = game.create_force('spectator')
-- local spectator_permissions = game.permissions.create_group('spectator')
-- spectator_permissions.set_allows_action(defines.input_action.start_walking,false)
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
Autostash.insert_into_furnace ( true )
-- Autostash.insert_into_wagon(true)
Autostash.bottom_button ( true )
BottomFrame.reset ( )
BottomFrame.activate_custom_buttons ( true )
2021-10-13 15:56:49 +02:00
-- BottomFrame.bottom_right(true)
2021-10-13 10:21:53 +02:00
local mgs = game.surfaces [ ' nauvis ' ] . map_gen_settings
mgs.width = 16
mgs.height = 16
game.surfaces [ ' nauvis ' ] . map_gen_settings = mgs
game.surfaces [ ' nauvis ' ] . clear ( )
game.create_surface ( ' piratedev1 ' , Common.default_map_gen_settings ( 100 , 100 ) )
2022-03-16 17:01:50 +02:00
game.surfaces [ ' piratedev1 ' ] . clear ( )
2021-10-13 10:21:53 +02:00
Common.init_game_settings ( Balance.technology_price_multiplier )
2022-03-19 23:20:55 +02:00
2022-05-14 00:35:12 +02:00
global_memory.active_crews_cap = Common.activeCrewsCap
2022-10-10 19:21:14 +02:00
global_memory.private_run_cap = Common.private_run_cap
2022-10-28 15:22:59 +02:00
2022-05-14 00:35:12 +02:00
global_memory.minimumCapacitySliderValue = Common.minimumCapacitySliderValue
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
Surfaces.Lobby . create_starting_dock_surface ( )
local lobby = game.surfaces [ CoreData.lobby_surface_name ]
game.forces . player.set_spawn_position ( Common.lobby_spawnpoint , lobby )
2022-06-02 21:08:55 +02:00
-- game.forces.player.character_running_speed_modifier = Balance.base_extra_character_speed
2021-10-13 10:21:53 +02:00
2022-03-19 23:20:55 +02:00
game.create_force ( ' environment ' )
2021-10-13 10:21:53 +02:00
for id = 1 , 3 , 1 do
2022-06-15 23:00:18 +02:00
game.create_force ( Common.get_enemy_force_name ( id ) )
game.create_force ( Common.get_ancient_friendly_force_name ( id ) )
game.create_force ( Common.get_ancient_hostile_force_name ( id ) )
2022-03-19 23:20:55 +02:00
2022-10-29 12:30:06 +02:00
game.create_force ( Common.get_crew_force_name ( id ) )
2021-10-13 10:21:53 +02:00
Crew.reset_crew_and_enemy_force ( id )
end
-- Delay.global_add(Delay.global_enum.PLACE_LOBBY_JETTY_AND_BOATS)
Task.set_timeout_in_ticks ( 2 , jetty_delayed , { } )
2022-02-26 20:25:48 +02:00
2021-10-13 10:21:53 +02:00
end
local event = require ' utils.event '
event.on_init ( on_init )
local function crew_tick ( )
local memory = Memory.get_crew_memory ( )
local destination = Common.current_destination ( )
local tick = game.tick
if memory.age and memory.overworldx and memory.overworldx > 0 then
memory.age = memory.age + 5
end
if memory.real_age then
memory.real_age = memory.real_age + 5
end
2022-06-01 23:13:09 +02:00
PiratesApiOnTick.boat_movement_tick ( 5 ) --arguments are tick intervals
-- PiratesApiOnTick.parrot_tick(5)
2022-02-26 20:25:48 +02:00
2022-06-01 23:13:09 +02:00
PiratesApiOnTick.quest_progress_tick ( 5 )
PiratesApiOnTick.strobe_player_colors ( 5 )
2022-06-02 13:40:32 +02:00
2021-10-13 10:21:53 +02:00
if tick % 10 == 0 then
2022-06-01 23:13:09 +02:00
PiratesApiOnTick.prevent_disembark ( 10 )
PiratesApiOnTick.prevent_unbarreling_off_ship ( 10 )
2022-07-02 17:42:22 +02:00
-- PiratesApiOnTick.shop_ratelimit_tick(10)
2022-05-29 13:36:27 +02:00
PiratesApiOnTick.pick_up_tick ( 10 )
2022-07-02 17:42:22 +02:00
QuestStructures.tick_quest_structure_entry_price_check ( )
2022-06-01 23:13:09 +02:00
PiratesApiOnTick.update_boat_stored_resources ( 10 )
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if tick % 30 == 0 then
PiratesApiOnTick.silo_update ( 30 )
PiratesApiOnTick.buried_treasure_check ( 30 )
ClassPiratesApiOnTick.update_character_properties ( 30 )
ClassPiratesApiOnTick.class_update_auxiliary_data ( 30 )
ClassPiratesApiOnTick.class_renderings ( 30 )
if tick % 60 == 0 then
PiratesApiOnTick.captain_warn_afk ( 60 )
PiratesApiOnTick.ship_deplete_fuel ( 60 )
2022-07-26 22:59:42 +02:00
PiratesApiOnTick.crowsnest_natural_move ( 60 )
2022-06-01 23:13:09 +02:00
PiratesApiOnTick.slower_boat_tick ( 60 )
PiratesApiOnTick.raft_raids ( 60 )
PiratesApiOnTick.place_cached_structures ( 60 )
2022-06-08 15:26:49 +02:00
PiratesApiOnTick.update_alert_sound_frequency_tracker ( )
2022-06-15 23:00:18 +02:00
PiratesApiOnTick.check_for_cliff_explosives_in_hold_wooden_chests ( )
2022-07-02 17:42:22 +02:00
PiratesApiOnTick.equalise_fluid_storages ( ) -- Made the update less often for small performance gain, but frequency can be increased if players complain
2022-07-08 22:06:35 +02:00
PiratesApiOnTick.revealed_buried_treasure_distance_check ( )
2022-10-10 19:21:14 +02:00
PiratesApiOnTick.update_private_run_lock_timer ( 60 )
2022-07-30 15:02:24 +02:00
PiratesApiOnTick.victory_continue_reminder ( )
2022-07-30 11:29:17 +02:00
Kraken.overall_kraken_tick ( )
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if destination.dynamic_data . timer then
destination.dynamic_data . timer = destination.dynamic_data . timer + 1
end
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if memory.captain_acceptance_timer then
memory.captain_acceptance_timer = memory.captain_acceptance_timer - 1
if memory.captain_acceptance_timer == 0 then
Roles.assign_captain_based_on_priorities ( )
end
end
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if memory.captain_accrued_time_data and memory.playerindex_captain and memory.overworldx and memory.overworldx > 0 and memory.overworldx < CoreData.victory_x then --only count time in the 'main game'
local player = game.players [ memory.playerindex_captain ]
if player and player.name then
if ( not memory.captain_accrued_time_data [ player.name ] ) then memory.captain_accrued_time_data [ player.name ] = 0 end
memory.captain_accrued_time_data [ player.name ] = memory.captain_accrued_time_data [ player.name ] + 1
end
end
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if destination.dynamic_data . time_remaining and destination.dynamic_data . time_remaining > 0 then
destination.dynamic_data . time_remaining = destination.dynamic_data . time_remaining - 1
if destination.dynamic_data . time_remaining == 0 then
if memory.boat and memory.boat . surface_name then
local surface_name_decoded = Surfaces.SurfacesCommon . decode_surface_name ( memory.boat . surface_name )
local type = surface_name_decoded.type
if type == Surfaces.enum . ISLAND then
2023-01-29 15:02:06 +02:00
if destination.static_params and destination.static_params . base_cost_to_undock and Balance.need_resources_to_undock ( Common.overworldx ( ) ) == true then
Crew.try_lose ( { ' pirates.loss_resources_were_not_collected_in_time ' } )
else
Progression.retreat_from_island ( false )
end
2022-06-01 23:13:09 +02:00
elseif type == Surfaces.enum . DOCK then
Progression.undock_from_dock ( false )
end
end
end
end
2022-03-19 23:20:55 +02:00
2023-01-12 18:58:53 +02:00
if destination.dynamic_data . disabled_wave_timer then
destination.dynamic_data . disabled_wave_timer = Math.max ( 0 , destination.dynamic_data . disabled_wave_timer - 1 )
end
2022-06-01 23:13:09 +02:00
if tick % 120 == 0 then
Ai.Tick_actions ( 120 )
if tick % 240 == 0 then
2023-01-29 15:02:06 +02:00
-- PiratesApiOnTick.check_all_spawners_dead(240) -- incentivises killing all spawners too much
2022-06-01 23:13:09 +02:00
if memory.max_players_recorded then
local count_now = # Common.crew_get_crew_members ( )
if count_now and count_now > memory.max_players_recorded then
memory.max_players_recorded = count_now
end
end
PiratesApiOnTick.Kraken_Destroyed_Backup_check ( 240 )
PiratesApiOnTick.LOS_tick ( 240 )
end
end
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if tick % 300 == 0 then
PiratesApiOnTick.periodic_free_resources ( 300 )
PiratesApiOnTick.update_recentcrewmember_list ( 300 )
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if tick % 1800 == 0 then
PiratesApiOnTick.transfer_pollution ( 1800 )
2021-10-13 10:21:53 +02:00
2022-06-01 23:13:09 +02:00
if tick % 3600 == 0 then
PiratesApiOnTick.prune_offline_characters_list ( 3600 )
end
2021-10-13 10:21:53 +02:00
end
end
2022-06-02 13:40:32 +02:00
2022-02-28 18:36:46 +02:00
2022-06-02 21:08:55 +02:00
if tick % ( 60 * Balance.class_reward_tick_rate_in_seconds ) == 0 then
ClassPiratesApiOnTick.class_rewards_tick ( 60 * Balance.class_reward_tick_rate_in_seconds )
2022-06-01 23:13:09 +02:00
end
2022-02-28 18:36:46 +02:00
end
end
end
2022-03-04 19:57:58 +02:00
2022-06-01 23:13:09 +02:00
if tick % 60 == 15 or tick % 60 == 45 then
2022-07-26 22:59:42 +02:00
-- @TODO move this ugly check to function?
2022-06-01 23:13:09 +02:00
if memory.boat and memory.boat . state == Structures.Boats . enum_state.ATSEA_SAILING then
PiratesApiOnTick.overworld_check_collisions ( 120 )
end
2021-10-13 10:21:53 +02:00
end
2022-06-01 23:13:09 +02:00
if tick % 60 == 30 then
2022-07-26 22:59:42 +02:00
PiratesApiOnTick.crowsnest_steer ( 120 )
2021-10-13 10:21:53 +02:00
end
2022-06-01 23:13:09 +02:00
if tick % Common.loading_interval == 0 then
PiratesApiOnTick.loading_update ( Common.loading_interval )
2022-03-09 01:36:03 +02:00
end
2022-07-30 22:31:00 +02:00
if memory.crew_disband_tick_message then
if memory.crew_disband_tick_message < tick then
memory.crew_disband_tick_message = nil
2022-07-31 11:23:59 +02:00
local message1 = { ' pirates.crew_disband_tick_message ' , 30 }
2022-10-02 23:37:54 +02:00
2022-07-30 22:31:00 +02:00
Common.notify_force ( memory.force , message1 )
2022-10-02 23:37:54 +02:00
2022-07-30 22:31:00 +02:00
Server.to_discord_embed_raw ( { ' ' , ' [ ' .. memory.name .. ' ] ' , message1 } , true )
end
end
2021-10-13 10:21:53 +02:00
if memory.crew_disband_tick then
if memory.crew_disband_tick < tick then
memory.crew_disband_tick = nil
Crew.disband_crew ( )
end
end
end
local function global_tick ( )
local global_memory = Memory.get_global_memory ( )
local tick = game.tick
if tick % 60 == 0 then
2022-05-29 13:36:27 +02:00
PiratesApiOnTick.update_players_second ( )
2021-10-13 10:21:53 +02:00
end
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
if tick % 30 == 0 then
for _ , player in pairs ( game.connected_players ) do
2022-06-15 23:00:18 +02:00
local crew_id = Common.get_id_from_force_name ( player.force . name )
2021-10-13 10:21:53 +02:00
Memory.set_working_id ( crew_id )
Roles.update_tags ( player )
end
end
for _ , id in pairs ( global_memory.crew_active_ids ) do
Memory.set_working_id ( id )
crew_tick ( )
end
2022-05-29 13:36:27 +02:00
PiratesApiOnTick.update_player_guis ( 5 )
2021-10-13 10:21:53 +02:00
end
event.on_nth_tick ( 5 , global_tick )
local function instatick ( )
local global_memory = Memory.get_global_memory ( )
for _ , id in pairs ( global_memory.crew_active_ids ) do
Memory.set_working_id ( id )
2022-05-29 13:36:27 +02:00
PiratesApiOnTick.minimap_jam ( 1 )
PiratesApiOnTick.silo_insta_update ( )
2021-10-13 10:21:53 +02:00
end
end
event.on_nth_tick ( 1 , instatick )
----- FOR BUGFIXING HARD CRASHES (segfaults) ------
2022-03-09 01:36:03 +02:00
-- often, segfaults are due to an error during chunk generation (as of 1.1.0 or so, anyway.)
2021-10-13 10:21:53 +02:00
-- to help debug, comment this out, and instead use the command /chnk to generate some chunks manually
2022-05-29 13:36:27 +02:00
event.add ( defines.events . on_chunk_generated , PiratesApiEvents.event_on_chunk_generated )
2021-10-13 10:21:53 +02:00
2022-02-27 01:33:19 +02:00
----- FOR DESYNC BUGFIXING -----
2022-03-09 01:36:03 +02:00
local gMeta = getmetatable ( _ENV )
if not gMeta then
gMeta = { }
setmetatable ( _ENV , gMeta )
end
gMeta.__newindex = function ( _ , n , v )
log ( ' Desync warning: attempt to write to undeclared var ' .. n )
global [ n ] = v
end
gMeta.__index = function ( _ , n )
return global [ n ]
end
2021-10-13 10:21:53 +02:00
return Public