2016-12-01 04:41:04 +02:00
-- control.lua
2019-03-02 04:07:24 +02:00
-- Mar 2019
2016-12-01 04:41:04 +02:00
2016-11-10 12:22:32 +02:00
-- Oarc's Separated Spawn Scenario
2019-07-24 13:09:55 +02:00
--
2016-12-01 04:41:04 +02:00
-- I wanted to create a scenario that allows you to spawn in separate locations
-- From there, I ended up adding a bunch of other minor/major features
2019-07-24 13:09:55 +02:00
--
2016-12-01 04:41:04 +02:00
-- Credit:
2019-07-24 13:09:55 +02:00
-- Tags - Taken from WOGs scenario
2016-12-01 04:41:04 +02:00
-- Rocket Silo - Taken from Frontier as an idea
--
-- Feel free to re-use anything you want. It would be nice to give me credit
-- if you can.
2017-07-26 00:12:55 +02:00
2016-11-10 12:22:32 +02:00
2016-11-16 14:43:03 +02:00
2019-03-02 04:07:24 +02:00
-- To keep the scenario more manageable (for myself) I have done the following:
2016-12-01 04:41:04 +02:00
-- 1. Keep all event calls in control.lua (here)
2019-04-10 19:59:57 +02:00
-- 2. Put all config options in config.lua and provided an example-config.lua file too.
2019-03-03 03:51:17 +02:00
-- 3. Put other stuff into their own files where possible.
2019-04-10 19:59:57 +02:00
-- 4. Put all other files into lib folder
-- 5. Provided an examples folder for example/recommended map gen settings
2016-11-16 14:43:03 +02:00
2016-11-10 12:22:32 +02:00
2016-12-28 18:33:09 +02:00
-- Generic Utility Includes
2019-03-02 04:07:24 +02:00
require ( " lib/oarc_utils " )
2019-03-03 03:51:17 +02:00
-- Other soft-mod type features.
2019-03-02 06:05:23 +02:00
require ( " lib/frontier_silo " )
require ( " lib/tag " )
2019-03-02 04:07:24 +02:00
require ( " lib/game_opts " )
2019-03-03 03:51:17 +02:00
require ( " lib/player_list " )
2019-04-28 20:13:46 +02:00
require ( " lib/rocket_launch " )
2019-05-12 15:59:54 +02:00
require ( " lib/admin_commands " )
2019-09-24 16:50:48 +02:00
require ( " lib/regrowth_map " )
2020-02-04 21:12:52 +02:00
require ( " lib/shared_chests " )
2020-02-06 07:56:40 +02:00
require ( " lib/notepad " )
2020-04-03 05:42:18 +02:00
require ( " lib/map_features " )
2020-05-09 21:39:32 +02:00
require ( " lib/oarc_buy " )
2020-05-12 22:06:04 +02:00
require ( " lib/auto_decon_miners " )
2018-02-01 06:31:33 +02:00
-- For Philip. I currently do not use this and need to add proper support for
-- commands like this in the future.
2019-03-03 03:51:17 +02:00
-- require("lib/rgcommand")
-- require("lib/helper_commands")
2016-12-28 18:33:09 +02:00
-- Main Configuration File
2016-12-01 04:41:04 +02:00
require ( " config " )
2019-04-10 19:59:57 +02:00
-- Save all config settings to global table.
require ( " lib/oarc_global_cfg.lua " )
2016-12-28 18:33:09 +02:00
-- Scenario Specific Includes
2019-03-02 04:07:24 +02:00
require ( " lib/separate_spawns " )
require ( " lib/separate_spawns_guis " )
2020-02-16 05:03:25 +02:00
require ( " lib/oarc_enemies " )
2019-08-01 05:47:39 +02:00
require ( " lib/oarc_gui_tabs " )
2019-12-19 23:00:30 +02:00
-- compatibility with mods
require ( " compat/factoriomaps " )
2019-03-19 22:46:31 +02:00
-- Create a new surface so we can modify map settings at the start.
GAME_SURFACE_NAME = " oarc "
2016-11-10 12:22:32 +02:00
2019-09-24 16:50:48 +02:00
commands.add_command ( " trigger-map-cleanup " ,
" Force immediate removal of all expired chunks (unused chunk removal mod) " ,
2020-06-19 06:12:42 +02:00
RegrowthForceRemoveChunksCmd )
2019-09-24 16:50:48 +02:00
2016-12-01 04:41:04 +02:00
--------------------------------------------------------------------------------
-- ALL EVENT HANLDERS ARE HERE IN ONE PLACE!
--------------------------------------------------------------------------------
----------------------------------------
2019-07-24 13:09:55 +02:00
-- On Init - only runs once the first
2016-12-01 04:41:04 +02:00
-- time the game starts
----------------------------------------
script.on_init ( function ( event )
2017-11-29 21:39:09 +02:00
2019-04-10 19:59:57 +02:00
-- FIRST
InitOarcConfig ( )
2019-09-24 16:50:48 +02:00
-- Regrowth (always init so we can enable during play.)
RegrowthInit ( )
2019-03-19 22:46:31 +02:00
-- Create new game surface
CreateGameSurface ( )
2019-03-11 15:29:00 +02:00
2019-03-19 22:46:31 +02:00
-- MUST be before other stuff, but after surface creation.
2019-04-11 03:03:02 +02:00
InitSpawnGlobalsAndForces ( )
2016-12-01 04:41:04 +02:00
2019-04-28 20:58:59 +02:00
-- Frontier Silo Area Generation
2020-06-23 03:31:05 +02:00
if ( global.ocfg . frontier_rocket_silo and not global.ocfg . enable_magic_factories ) then
2019-04-28 20:58:59 +02:00
SpawnSilosAndGenerateSiloAreas ( )
2019-03-02 06:05:23 +02:00
end
2019-05-14 07:09:01 +02:00
-- Everyone do the shuffle. Helps avoid always starting at the same location.
2020-05-01 20:59:47 +02:00
-- Needs to be done after the silo spawning.
if ( global.ocfg . enable_vanilla_spawns ) then
global.vanillaSpawns = FYShuffle ( global.vanillaSpawns )
log ( " Vanilla spawns: " )
log ( serpent.block ( global.vanillaSpawns ) )
end
2019-12-19 23:00:30 +02:00
Compat.handle_factoriomaps ( )
2020-02-06 07:56:40 +02:00
if ( global.ocfg . enable_chest_sharing ) then
SharedChestInitItems ( )
end
2020-03-03 20:44:44 +02:00
2020-05-01 20:59:47 +02:00
if ( global.ocfg . enable_magic_factories ) then
MagicFactoriesInit ( )
end
2020-04-03 05:42:18 +02:00
2020-05-12 22:06:04 +02:00
OarcMapFeatureInitGlobalCounters ( )
OarcAutoDeconOnInit ( )
2020-03-03 20:44:44 +02:00
-- Display starting point text as a display of dominance.
RenderPermanentGroundText ( game.surfaces [ GAME_SURFACE_NAME ] , { x =- 29 , y =- 30 } , 40 , " OARC " , { 0.9 , 0.7 , 0.3 , 0.8 } )
2020-05-28 02:40:51 +02:00
rendering.draw_sprite { sprite = " item/coin " ,
render_layer = 30 ,
target = { 0 , 0 } ,
surface = game.surfaces [ GAME_SURFACE_NAME ] ,
tint = { r = 0.1 , g = 0.1 , b = 0.1 , a = 0.1 } ,
x_scale = 80 ,
y_scale = 80 }
2019-12-19 23:00:30 +02:00
end )
script.on_load ( function ( )
Compat.handle_factoriomaps ( )
2016-12-01 04:41:04 +02:00
end )
----------------------------------------
2019-04-28 20:13:46 +02:00
-- Rocket launch event
-- Used for end game win conditions / unlocking late game stuff
2016-12-01 04:41:04 +02:00
----------------------------------------
2019-03-02 06:05:23 +02:00
script.on_event ( defines.events . on_rocket_launched , function ( event )
2020-04-30 02:33:07 +02:00
RocketLaunchEvent ( event )
2019-03-02 06:05:23 +02:00
end )
2016-12-01 04:41:04 +02:00
----------------------------------------
-- Chunk Generation
----------------------------------------
script.on_event ( defines.events . on_chunk_generated , function ( event )
2019-03-15 22:15:01 +02:00
2020-04-30 02:33:07 +02:00
if ( event.surface . name ~= GAME_SURFACE_NAME ) then return end
2019-09-24 16:50:48 +02:00
if global.ocfg . enable_regrowth then
RegrowthChunkGenerate ( event )
end
2020-06-19 05:18:29 +02:00
2019-04-11 02:40:34 +02:00
if global.ocfg . enable_undecorator then
2019-03-02 06:05:23 +02:00
UndecorateOnChunkGenerate ( event )
end
2017-12-16 15:46:06 +02:00
2019-04-11 03:03:02 +02:00
SeparateSpawnsGenerateChunk ( event )
2017-05-13 04:33:57 +02:00
2020-02-07 08:15:38 +02:00
CreateHoldingPen ( event.surface , event.area )
2016-12-01 04:41:04 +02:00
end )
----------------------------------------
-- Gui Click
----------------------------------------
script.on_event ( defines.events . on_gui_click , function ( event )
2019-07-31 20:53:21 +02:00
-- Don't interfere with other mod related stuff.
if ( event.element . get_mod ( ) ~= nil ) then return end
2019-04-11 02:40:34 +02:00
if global.ocfg . enable_tags then
2019-03-02 06:05:23 +02:00
TagGuiClick ( event )
end
2016-12-01 04:41:04 +02:00
2019-04-11 03:03:02 +02:00
WelcomeTextGuiClick ( event )
SpawnOptsGuiClick ( event )
SpawnCtrlGuiClick ( event )
SharedSpwnOptsGuiClick ( event )
BuddySpawnOptsGuiClick ( event )
BuddySpawnWaitMenuClick ( event )
BuddySpawnRequestMenuClick ( event )
SharedSpawnJoinWaitMenuClick ( event )
2016-12-19 23:35:36 +02:00
2019-07-31 20:53:21 +02:00
ClickOarcGuiButton ( event )
2020-05-09 21:39:32 +02:00
ClickOarcStoreButton ( event )
2019-07-31 20:53:21 +02:00
2019-03-02 04:07:24 +02:00
GameOptionsGuiClick ( event )
2016-12-01 04:41:04 +02:00
end )
2017-12-15 21:45:01 +02:00
script.on_event ( defines.events . on_gui_checked_state_changed , function ( event )
2019-04-11 03:03:02 +02:00
SpawnOptsRadioSelect ( event )
SpawnCtrlGuiOptionsSelect ( event )
2017-12-15 21:45:01 +02:00
end )
2019-07-31 20:53:21 +02:00
script.on_event ( defines.events . on_gui_selected_tab_changed , function ( event )
TabChangeOarcGui ( event )
2020-05-09 21:39:32 +02:00
TabChangeOarcStore ( event )
2019-07-31 20:53:21 +02:00
end )
2016-12-01 04:41:04 +02:00
----------------------------------------
-- Player Events
----------------------------------------
script.on_event ( defines.events . on_player_joined_game , function ( event )
PlayerJoinedMessages ( event )
2019-06-08 05:52:14 +02:00
ServerWriteFile ( " player_events " , game.players [ event.player_index ] . name .. " joined the game. " .. " \n " )
2016-12-01 04:41:04 +02:00
end )
script.on_event ( defines.events . on_player_created , function ( event )
2019-07-31 20:53:21 +02:00
local player = game.players [ event.player_index ]
2019-07-24 13:09:55 +02:00
2017-04-28 03:48:28 +02:00
-- Move the player to the game surface immediately.
2019-07-31 20:53:21 +02:00
player.teleport ( { x = 0 , y = 0 } , GAME_SURFACE_NAME )
2017-04-27 02:59:48 +02:00
2019-04-11 02:40:34 +02:00
if global.ocfg . enable_long_reach then
2019-07-31 20:53:21 +02:00
GivePlayerLongReach ( player )
2019-03-09 03:29:47 +02:00
end
2020-06-19 20:25:03 +02:00
SeparateSpawnsPlayerCreated ( event.player_index , true )
2020-05-12 22:06:04 +02:00
2019-08-01 05:53:31 +02:00
InitOarcGuiTabs ( player )
2020-05-09 21:39:32 +02:00
InitOarcStoreGuiTabs ( player )
2016-12-01 04:41:04 +02:00
end )
script.on_event ( defines.events . on_player_respawned , function ( event )
2019-04-11 03:03:02 +02:00
SeparateSpawnsPlayerRespawned ( event )
2019-07-24 13:09:55 +02:00
2017-05-12 01:42:23 +02:00
PlayerRespawnItems ( event )
2016-12-14 23:45:45 +02:00
2019-04-11 02:40:34 +02:00
if global.ocfg . enable_long_reach then
2019-03-09 03:29:47 +02:00
GivePlayerLongReach ( game.players [ event.player_index ] )
end
2016-12-01 04:41:04 +02:00
end )
2016-12-02 23:26:49 +02:00
script.on_event ( defines.events . on_player_left_game , function ( event )
2019-06-08 05:52:14 +02:00
ServerWriteFile ( " player_events " , game.players [ event.player_index ] . name .. " left the game. " .. " \n " )
2020-06-20 20:13:44 +02:00
local player = game.players [ event.player_index ]
2020-06-19 20:25:03 +02:00
-- If players leave early, say goodbye.
2020-06-20 20:13:44 +02:00
if ( player and ( player.online_time < ( global.ocfg . minimum_online_time * TICKS_PER_MINUTE ) ) ) then
2020-06-20 20:26:20 +02:00
log ( " Player left early: " .. player.name )
2020-06-20 20:13:44 +02:00
SendBroadcastMsg ( player.name .. " 's base was marked for immediate clean up because they left within " .. global.ocfg . minimum_online_time .. " minutes of joining. " )
2020-06-20 21:06:08 +02:00
RemoveOrResetPlayer ( player , true , true , true , true )
2020-06-19 20:25:03 +02:00
end
2016-12-02 23:26:49 +02:00
end )
2020-05-13 17:50:22 +02:00
-- script.on_event(defines.events.on_player_removed, function(event)
-- Player is already deleted when this is called.
-- end)
2020-05-12 22:06:04 +02:00
2017-07-26 02:20:35 +02:00
----------------------------------------
2019-07-03 01:13:48 +02:00
-- On tick events. Stuff that needs to happen at regular intervals.
2019-07-31 20:53:21 +02:00
-- Delayed events, delayed spawns, ...
2017-07-26 02:20:35 +02:00
----------------------------------------
2017-04-30 15:22:45 +02:00
script.on_event ( defines.events . on_tick , function ( event )
2019-09-24 16:50:48 +02:00
if global.ocfg . enable_regrowth then
RegrowthOnTick ( )
RegrowthForceRemovalOnTick ( )
end
2017-07-29 00:02:45 +02:00
2019-04-11 03:03:02 +02:00
DelayedSpawnOnTick ( )
2018-01-25 07:04:07 +02:00
2020-02-04 22:10:13 +02:00
if global.ocfg . enable_chest_sharing then
SharedChestsOnTick ( )
end
2020-02-06 07:56:40 +02:00
2020-05-01 20:59:47 +02:00
if global.ocfg . enable_magic_factories then
MagicFactoriesOnTick ( )
end
2020-04-03 05:42:18 +02:00
2020-02-06 07:56:40 +02:00
TimeoutSpeechBubblesOnTick ( )
FadeoutRenderOnTick ( )
2020-05-12 22:06:04 +02:00
if global.ocfg . enable_miner_decon then
OarcAutoDeconOnTick ( )
end
2017-04-30 15:22:45 +02:00
end )
2017-11-29 21:39:09 +02:00
2020-06-19 05:18:29 +02:00
script.on_event ( defines.events . on_sector_scanned , function ( event )
2019-09-24 16:50:48 +02:00
if global.ocfg . enable_regrowth then
RegrowthSectorScan ( event )
end
end )
2019-07-31 20:53:21 +02:00
2017-11-29 21:39:09 +02:00
2017-07-29 00:02:45 +02:00
----------------------------------------
2020-05-09 21:39:32 +02:00
-- Various on "built" events
2017-07-29 00:02:45 +02:00
----------------------------------------
2020-05-09 21:39:32 +02:00
script.on_event ( defines.events . on_built_entity , function ( event )
if global.ocfg . enable_autofill then
Autofill ( event )
end
if global.ocfg . enable_regrowth then
2020-06-19 05:18:29 +02:00
if ( event.created_entity . surface.name ~= GAME_SURFACE_NAME ) then return end
2020-06-19 06:12:42 +02:00
RegrowthMarkAreaSafeGivenTilePos ( event.created_entity . position , 2 , false )
2020-05-09 21:39:32 +02:00
end
if global.ocfg . enable_anti_grief then
SetItemBlueprintTimeToLive ( event )
end
if global.ocfg . frontier_rocket_silo then
BuildSiloAttempt ( event )
end
end )
2019-04-10 19:59:57 +02:00
script.on_event ( defines.events . on_robot_built_entity , function ( event )
2019-09-24 16:50:48 +02:00
if global.ocfg . enable_regrowth then
2020-06-19 05:18:29 +02:00
if ( event.created_entity . surface.name ~= GAME_SURFACE_NAME ) then return end
2020-06-19 06:12:42 +02:00
RegrowthMarkAreaSafeGivenTilePos ( event.created_entity . position , 2 , false )
2019-09-24 16:50:48 +02:00
end
2019-05-12 16:00:56 +02:00
if global.ocfg . frontier_rocket_silo then
BuildSiloAttempt ( event )
end
2019-04-10 19:59:57 +02:00
end )
2019-05-12 16:00:56 +02:00
2019-09-29 20:32:20 +02:00
script.on_event ( defines.events . on_player_built_tile , function ( event )
if global.ocfg . enable_regrowth then
2020-06-19 05:18:29 +02:00
if ( game.surfaces [ event.surface_index ] . name ~= GAME_SURFACE_NAME ) then return end
2019-09-29 20:32:20 +02:00
for k , v in pairs ( event.tiles ) do
2020-06-19 06:12:42 +02:00
RegrowthMarkAreaSafeGivenTilePos ( v.position , 2 , false )
2019-09-29 20:32:20 +02:00
end
end
end )
2020-05-09 21:39:32 +02:00
----------------------------------------
-- On script_raised_built. This should help catch mods that
-- place items that don't count as player_built and robot_built.
-- Specifically FARL.
----------------------------------------
script.on_event ( defines.events . script_raised_built , function ( event )
if global.ocfg . enable_regrowth then
2020-06-19 05:18:29 +02:00
if ( event.entity . surface.name ~= GAME_SURFACE_NAME ) then return end
2020-06-19 06:12:42 +02:00
RegrowthMarkAreaSafeGivenTilePos ( event.entity . position , 2 , false )
2020-05-09 21:39:32 +02:00
end
end )
2017-08-08 01:27:15 +02:00
2017-07-26 02:20:35 +02:00
----------------------------------------
-- Shared chat, so you don't have to type /s
2019-04-29 05:37:12 +02:00
-- But you do lose your player colors across forces.
2017-07-26 02:20:35 +02:00
----------------------------------------
script.on_event ( defines.events . on_console_chat , function ( event )
2019-06-08 05:52:14 +02:00
if ( event.player_index ) then
ServerWriteFile ( " server_chat " , game.players [ event.player_index ] . name .. " : " .. event.message .. " \n " )
end
2019-04-11 02:40:34 +02:00
if ( global.ocfg . enable_shared_chat ) then
2017-08-09 20:11:15 +02:00
if ( event.player_index ~= nil ) then
2017-08-08 01:27:15 +02:00
ShareChatBetweenForces ( game.players [ event.player_index ] , event.message )
end
2017-07-26 02:20:35 +02:00
end
end )
2016-12-02 23:26:49 +02:00
2016-12-01 04:41:04 +02:00
----------------------------------------
-- On Research Finished
2017-04-28 03:48:28 +02:00
-- This is where you can permanently remove researched techs
2016-12-01 04:41:04 +02:00
----------------------------------------
2019-03-02 06:05:23 +02:00
script.on_event ( defines.events . on_research_finished , function ( event )
2019-07-24 13:09:55 +02:00
2019-04-28 20:13:46 +02:00
-- Never allows players to build rocket-silos in "frontier" mode.
2019-05-15 08:40:32 +02:00
if global.ocfg . frontier_rocket_silo and not global.ocfg . frontier_allow_build then
RemoveRecipe ( event.research . force , " rocket-silo " )
end
2019-04-28 20:13:46 +02:00
2019-07-24 13:09:55 +02:00
if global.ocfg . lock_goodies_rocket_launch and
2020-06-19 05:18:29 +02:00
( not global.ocore . satellite_sent or not global.ocore . satellite_sent [ event.research . force.name ] ) then
2020-03-03 20:44:44 +02:00
for _ , v in ipairs ( LOCKED_RECIPES ) do
RemoveRecipe ( event.research . force , v.r )
end
2019-04-28 20:13:46 +02:00
end
2019-05-12 16:00:56 +02:00
2019-06-03 07:10:19 +02:00
if global.ocfg . enable_loaders then
2019-05-12 16:00:56 +02:00
EnableLoaders ( event )
end
2019-03-02 06:05:23 +02:00
end )
2016-12-19 23:35:36 +02:00
2019-03-11 15:29:00 +02:00
----------------------------------------
2019-07-03 01:13:48 +02:00
-- On Entity Spawned and On Biter Base Built
2019-03-11 15:29:00 +02:00
-- This is where I modify biter spawning based on location and other factors.
----------------------------------------
script.on_event ( defines.events . on_entity_spawned , function ( event )
2019-04-11 02:40:34 +02:00
if ( global.ocfg . modified_enemy_spawning ) then
2019-03-11 15:29:00 +02:00
ModifyEnemySpawnsNearPlayerStartingAreas ( event )
end
end )
2019-07-03 01:13:48 +02:00
script.on_event ( defines.events . on_biter_base_built , function ( event )
if ( global.ocfg . modified_enemy_spawning ) then
ModifyEnemySpawnsNearPlayerStartingAreas ( event )
end
end )
2019-05-03 23:52:38 +02:00
2020-02-16 05:03:25 +02:00
----------------------------------------
-- On unit group finished gathering
-- This is where I remove biter waves on offline players
----------------------------------------
script.on_event ( defines.events . on_unit_group_finished_gathering , function ( event )
2020-03-03 20:44:44 +02:00
if ( global.ocfg . enable_offline_protect ) then
OarcModifyEnemyGroup ( event.group )
end
2020-02-16 05:03:25 +02:00
end )
2019-05-03 23:52:38 +02:00
----------------------------------------
-- On Corpse Timed Out
-- Save player's stuff so they don't lose it if they can't get to the corpse fast enough.
----------------------------------------
script.on_event ( defines.events . on_character_corpse_expired , function ( event )
DropGravestoneChestFromCorpse ( event.corpse )
end )
2020-02-06 07:56:40 +02:00
----------------------------------------
-- On Gui Text Change
-- For capturing text entry.
----------------------------------------
script.on_event ( defines.events . on_gui_text_changed , function ( event )
NotepadOnGuiTextChange ( event )
2020-03-03 22:53:01 +02:00
end )
----------------------------------------
-- On Gui Closed
-- For capturing player escaping custom GUI so we can close it using ESC key.
----------------------------------------
script.on_event ( defines.events . on_gui_closed , function ( event )
OarcGuiOnGuiClosedEvent ( event )
2020-05-09 21:39:32 +02:00
OarcStoreOnGuiClosedEvent ( event )
2020-04-03 20:15:39 +02:00
end )
----------------------------------------
-- On enemies killed
-- For coin generation and stuff
----------------------------------------
2020-05-12 22:06:04 +02:00
script.on_event ( defines.events . on_post_entity_died , function ( event )
if ( game.surfaces [ event.surface_index ] . name ~= GAME_SURFACE_NAME ) then return end
CoinsFromEnemiesOnPostEntityDied ( event )
end ,
2020-05-13 17:50:22 +02:00
{ { filter = " type " , type = " unit " } , { filter = " type " , type = " unit-spawner " } , { filter = " type " , type = " turret " } } )
2020-05-12 22:06:04 +02:00
----------------------------------------
-- Scripted auto decon for miners...
----------------------------------------
script.on_event ( defines.events . on_resource_depleted , function ( event )
if global.ocfg . enable_miner_decon then
OarcAutoDeconOnResourceDepleted ( event )
end
end )