2016-11-30 21:41:04 -05:00
-- control.lua
2019-03-01 21:07:24 -05:00
-- Mar 2019
2016-11-30 21:41:04 -05:00
2016-11-10 05:22:32 -05:00
-- Oarc's Separated Spawn Scenario
2019-07-24 06:09:55 -05:00
--
2016-11-30 21:41:04 -05: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 06:09:55 -05:00
--
2016-11-30 21:41:04 -05:00
-- Credit:
2019-07-24 06:09:55 -05:00
-- Tags - Taken from WOGs scenario
2016-11-30 21:41:04 -05: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-25 18:12:55 -04:00
2016-11-10 05:22:32 -05:00
2016-11-16 07:43:03 -05:00
2019-03-01 21:07:24 -05:00
-- To keep the scenario more manageable (for myself) I have done the following:
2016-11-30 21:41:04 -05:00
-- 1. Keep all event calls in control.lua (here)
2019-04-10 13:59:57 -04:00
-- 2. Put all config options in config.lua and provided an example-config.lua file too.
2019-03-02 20:51:17 -05:00
-- 3. Put other stuff into their own files where possible.
2019-04-10 13:59:57 -04:00
-- 4. Put all other files into lib folder
-- 5. Provided an examples folder for example/recommended map gen settings
2016-11-16 07:43:03 -05:00
2016-11-10 05:22:32 -05:00
2016-12-28 08:33:09 -08:00
-- Generic Utility Includes
2019-03-01 21:07:24 -05:00
require ( " lib/oarc_utils " )
2019-03-02 20:51:17 -05:00
-- Other soft-mod type features.
2019-03-01 23:05:23 -05:00
require ( " lib/frontier_silo " )
require ( " lib/tag " )
2019-03-01 21:07:24 -05:00
require ( " lib/game_opts " )
2019-03-02 20:51:17 -05:00
require ( " lib/player_list " )
2019-04-28 14:13:46 -04:00
require ( " lib/rocket_launch " )
2019-05-12 21:59:54 +08:00
require ( " lib/admin_commands " )
2019-09-24 09:50:48 -05:00
require ( " lib/regrowth_map " )
2020-02-04 14:12:52 -05:00
require ( " lib/shared_chests " )
2020-02-06 00:56:40 -05:00
require ( " lib/notepad " )
2020-04-02 23:42:18 -04:00
require ( " lib/map_features " )
2020-05-09 15:39:32 -04:00
require ( " lib/oarc_buy " )
2020-05-12 16:06:04 -04:00
require ( " lib/auto_decon_miners " )
2018-01-31 23:31:33 -05:00
-- For Philip. I currently do not use this and need to add proper support for
-- commands like this in the future.
2019-03-02 20:51:17 -05:00
-- require("lib/rgcommand")
-- require("lib/helper_commands")
2016-12-28 08:33:09 -08:00
-- Main Configuration File
2016-11-30 21:41:04 -05:00
require ( " config " )
2019-04-10 13:59:57 -04:00
-- Save all config settings to global table.
require ( " lib/oarc_global_cfg.lua " )
2016-12-28 08:33:09 -08:00
-- Scenario Specific Includes
2019-03-01 21:07:24 -05:00
require ( " lib/separate_spawns " )
require ( " lib/separate_spawns_guis " )
2020-02-15 22:03:25 -05:00
require ( " lib/oarc_enemies " )
2019-07-31 22:47:39 -05:00
require ( " lib/oarc_gui_tabs " )
2019-12-19 22:00:30 +01:00
-- compatibility with mods
require ( " compat/factoriomaps " )
2019-03-19 16:46:31 -04:00
-- Create a new surface so we can modify map settings at the start.
GAME_SURFACE_NAME = " oarc "
2016-11-10 05:22:32 -05:00
2019-09-24 09:50:48 -05:00
commands.add_command ( " trigger-map-cleanup " ,
" Force immediate removal of all expired chunks (unused chunk removal mod) " ,
2020-06-19 00:12:42 -04:00
RegrowthForceRemoveChunksCmd )
2019-09-24 09:50:48 -05:00
2016-11-30 21:41:04 -05:00
--------------------------------------------------------------------------------
-- ALL EVENT HANLDERS ARE HERE IN ONE PLACE!
--------------------------------------------------------------------------------
----------------------------------------
2019-07-24 06:09:55 -05:00
-- On Init - only runs once the first
2016-11-30 21:41:04 -05:00
-- time the game starts
----------------------------------------
script.on_init ( function ( event )
2017-11-29 14:39:09 -05:00
2019-04-10 13:59:57 -04:00
-- FIRST
InitOarcConfig ( )
2019-09-24 09:50:48 -05:00
-- Regrowth (always init so we can enable during play.)
RegrowthInit ( )
2019-03-19 16:46:31 -04:00
-- Create new game surface
CreateGameSurface ( )
2019-03-11 09:29:00 -04:00
2019-03-19 16:46:31 -04:00
-- MUST be before other stuff, but after surface creation.
2019-04-10 21:03:02 -04:00
InitSpawnGlobalsAndForces ( )
2016-11-30 21:41:04 -05:00
2019-04-28 14:58:59 -04:00
-- Frontier Silo Area Generation
2020-06-22 21:31:05 -04:00
if ( global.ocfg . frontier_rocket_silo and not global.ocfg . enable_magic_factories ) then
2019-04-28 14:58:59 -04:00
SpawnSilosAndGenerateSiloAreas ( )
2019-03-01 23:05:23 -05:00
end
2019-05-14 13:09:01 +08:00
-- Everyone do the shuffle. Helps avoid always starting at the same location.
2020-05-01 14:59:47 -04: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 22:00:30 +01:00
Compat.handle_factoriomaps ( )
2020-02-06 00:56:40 -05:00
if ( global.ocfg . enable_chest_sharing ) then
SharedChestInitItems ( )
end
2020-03-03 13:44:44 -05:00
2020-05-01 14:59:47 -04:00
if ( global.ocfg . enable_magic_factories ) then
MagicFactoriesInit ( )
end
2020-04-02 23:42:18 -04:00
2020-05-12 16:06:04 -04:00
OarcMapFeatureInitGlobalCounters ( )
OarcAutoDeconOnInit ( )
2020-03-03 13:44:44 -05: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-27 20:40:51 -04: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 22:00:30 +01:00
end )
script.on_load ( function ( )
Compat.handle_factoriomaps ( )
2016-11-30 21:41:04 -05:00
end )
----------------------------------------
2019-04-28 14:13:46 -04:00
-- Rocket launch event
-- Used for end game win conditions / unlocking late game stuff
2016-11-30 21:41:04 -05:00
----------------------------------------
2019-03-01 23:05:23 -05:00
script.on_event ( defines.events . on_rocket_launched , function ( event )
2020-04-29 20:33:07 -04:00
RocketLaunchEvent ( event )
2019-03-01 23:05:23 -05:00
end )
2016-11-30 21:41:04 -05:00
----------------------------------------
-- Chunk Generation
----------------------------------------
script.on_event ( defines.events . on_chunk_generated , function ( event )
2019-03-15 16:15:01 -04:00
2020-04-29 20:33:07 -04:00
if ( event.surface . name ~= GAME_SURFACE_NAME ) then return end
2019-09-24 09:50:48 -05:00
if global.ocfg . enable_regrowth then
RegrowthChunkGenerate ( event )
end
2020-06-18 23:18:29 -04:00
2019-04-10 20:40:34 -04:00
if global.ocfg . enable_undecorator then
2019-03-01 23:05:23 -05:00
UndecorateOnChunkGenerate ( event )
end
2017-12-16 08:46:06 -05:00
2019-04-10 21:03:02 -04:00
SeparateSpawnsGenerateChunk ( event )
2017-05-12 22:33:57 -04:00
2020-02-07 01:15:38 -05:00
CreateHoldingPen ( event.surface , event.area )
2016-11-30 21:41:04 -05:00
end )
----------------------------------------
-- Gui Click
----------------------------------------
script.on_event ( defines.events . on_gui_click , function ( event )
2019-07-31 13:53:21 -05:00
-- Don't interfere with other mod related stuff.
if ( event.element . get_mod ( ) ~= nil ) then return end
2019-04-10 20:40:34 -04:00
if global.ocfg . enable_tags then
2019-03-01 23:05:23 -05:00
TagGuiClick ( event )
end
2016-11-30 21:41:04 -05:00
2019-04-10 21:03:02 -04:00
WelcomeTextGuiClick ( event )
SpawnOptsGuiClick ( event )
SpawnCtrlGuiClick ( event )
SharedSpwnOptsGuiClick ( event )
BuddySpawnOptsGuiClick ( event )
BuddySpawnWaitMenuClick ( event )
BuddySpawnRequestMenuClick ( event )
SharedSpawnJoinWaitMenuClick ( event )
2016-12-19 16:35:36 -05:00
2019-07-31 13:53:21 -05:00
ClickOarcGuiButton ( event )
2020-05-09 15:39:32 -04:00
ClickOarcStoreButton ( event )
2019-07-31 13:53:21 -05:00
2019-03-01 21:07:24 -05:00
GameOptionsGuiClick ( event )
2016-11-30 21:41:04 -05:00
end )
2017-12-15 14:45:01 -05:00
script.on_event ( defines.events . on_gui_checked_state_changed , function ( event )
2019-04-10 21:03:02 -04:00
SpawnOptsRadioSelect ( event )
SpawnCtrlGuiOptionsSelect ( event )
2017-12-15 14:45:01 -05:00
end )
2019-07-31 13:53:21 -05:00
script.on_event ( defines.events . on_gui_selected_tab_changed , function ( event )
TabChangeOarcGui ( event )
2020-05-09 15:39:32 -04:00
TabChangeOarcStore ( event )
2019-07-31 13:53:21 -05:00
end )
2016-11-30 21:41:04 -05:00
----------------------------------------
-- Player Events
----------------------------------------
script.on_event ( defines.events . on_player_joined_game , function ( event )
PlayerJoinedMessages ( event )
2019-06-07 22:52:14 -05:00
ServerWriteFile ( " player_events " , game.players [ event.player_index ] . name .. " joined the game. " .. " \n " )
2016-11-30 21:41:04 -05:00
end )
script.on_event ( defines.events . on_player_created , function ( event )
2019-07-31 13:53:21 -05:00
local player = game.players [ event.player_index ]
2019-07-24 06:09:55 -05:00
2017-04-27 21:48:28 -04:00
-- Move the player to the game surface immediately.
2019-07-31 13:53:21 -05:00
player.teleport ( { x = 0 , y = 0 } , GAME_SURFACE_NAME )
2017-04-26 20:59:48 -04:00
2019-04-10 20:40:34 -04:00
if global.ocfg . enable_long_reach then
2019-07-31 13:53:21 -05:00
GivePlayerLongReach ( player )
2019-03-08 20:29:47 -05:00
end
2020-06-19 14:25:03 -04:00
SeparateSpawnsPlayerCreated ( event.player_index , true )
2020-05-12 16:06:04 -04:00
2019-07-31 22:53:31 -05:00
InitOarcGuiTabs ( player )
2020-05-09 15:39:32 -04:00
InitOarcStoreGuiTabs ( player )
2016-11-30 21:41:04 -05:00
end )
script.on_event ( defines.events . on_player_respawned , function ( event )
2019-04-10 21:03:02 -04:00
SeparateSpawnsPlayerRespawned ( event )
2019-07-24 06:09:55 -05:00
2017-05-11 19:42:23 -04:00
PlayerRespawnItems ( event )
2016-12-14 16:45:45 -05:00
2019-04-10 20:40:34 -04:00
if global.ocfg . enable_long_reach then
2019-03-08 20:29:47 -05:00
GivePlayerLongReach ( game.players [ event.player_index ] )
end
2016-11-30 21:41:04 -05:00
end )
2016-12-02 16:26:49 -05:00
script.on_event ( defines.events . on_player_left_game , function ( event )
2019-06-07 22:52:14 -05:00
ServerWriteFile ( " player_events " , game.players [ event.player_index ] . name .. " left the game. " .. " \n " )
2020-06-20 14:13:44 -04:00
local player = game.players [ event.player_index ]
2020-06-19 14:25:03 -04:00
-- If players leave early, say goodbye.
2020-06-20 14:13:44 -04:00
if ( player and ( player.online_time < ( global.ocfg . minimum_online_time * TICKS_PER_MINUTE ) ) ) then
2020-06-20 14:26:20 -04:00
log ( " Player left early: " .. player.name )
2020-06-20 14:13:44 -04: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 15:06:08 -04:00
RemoveOrResetPlayer ( player , true , true , true , true )
2020-06-19 14:25:03 -04:00
end
2016-12-02 16:26:49 -05:00
end )
2020-05-13 11:50:22 -04:00
-- script.on_event(defines.events.on_player_removed, function(event)
-- Player is already deleted when this is called.
-- end)
2020-05-12 16:06:04 -04:00
2017-07-25 20:20:35 -04:00
----------------------------------------
2019-07-02 18:13:48 -05:00
-- On tick events. Stuff that needs to happen at regular intervals.
2019-07-31 13:53:21 -05:00
-- Delayed events, delayed spawns, ...
2017-07-25 20:20:35 -04:00
----------------------------------------
2017-04-30 09:22:45 -04:00
script.on_event ( defines.events . on_tick , function ( event )
2019-09-24 09:50:48 -05:00
if global.ocfg . enable_regrowth then
RegrowthOnTick ( )
RegrowthForceRemovalOnTick ( )
end
2017-07-28 18:02:45 -04:00
2019-04-10 21:03:02 -04:00
DelayedSpawnOnTick ( )
2018-01-25 00:04:07 -05:00
2020-02-04 15:10:13 -05:00
if global.ocfg . enable_chest_sharing then
SharedChestsOnTick ( )
end
2020-02-06 00:56:40 -05:00
2020-05-01 14:59:47 -04:00
if global.ocfg . enable_magic_factories then
MagicFactoriesOnTick ( )
end
2020-04-02 23:42:18 -04:00
2020-02-06 00:56:40 -05:00
TimeoutSpeechBubblesOnTick ( )
FadeoutRenderOnTick ( )
2020-05-12 16:06:04 -04:00
if global.ocfg . enable_miner_decon then
OarcAutoDeconOnTick ( )
end
2017-04-30 09:22:45 -04:00
end )
2017-11-29 14:39:09 -05:00
2020-06-18 23:18:29 -04:00
script.on_event ( defines.events . on_sector_scanned , function ( event )
2019-09-24 09:50:48 -05:00
if global.ocfg . enable_regrowth then
RegrowthSectorScan ( event )
end
end )
2019-07-31 13:53:21 -05:00
2017-11-29 14:39:09 -05:00
2017-07-28 18:02:45 -04:00
----------------------------------------
2020-05-09 15:39:32 -04:00
-- Various on "built" events
2017-07-28 18:02:45 -04:00
----------------------------------------
2020-05-09 15:39:32 -04: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-18 23:18:29 -04:00
if ( event.created_entity . surface.name ~= GAME_SURFACE_NAME ) then return end
2020-06-19 00:12:42 -04:00
RegrowthMarkAreaSafeGivenTilePos ( event.created_entity . position , 2 , false )
2020-05-09 15:39:32 -04: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 13:59:57 -04:00
script.on_event ( defines.events . on_robot_built_entity , function ( event )
2019-09-24 09:50:48 -05:00
if global.ocfg . enable_regrowth then
2020-06-18 23:18:29 -04:00
if ( event.created_entity . surface.name ~= GAME_SURFACE_NAME ) then return end
2020-06-19 00:12:42 -04:00
RegrowthMarkAreaSafeGivenTilePos ( event.created_entity . position , 2 , false )
2019-09-24 09:50:48 -05:00
end
2019-05-12 22:00:56 +08:00
if global.ocfg . frontier_rocket_silo then
BuildSiloAttempt ( event )
end
2019-04-10 13:59:57 -04:00
end )
2019-05-12 22:00:56 +08:00
2019-09-29 13:32:20 -05:00
script.on_event ( defines.events . on_player_built_tile , function ( event )
if global.ocfg . enable_regrowth then
2020-06-18 23:18:29 -04:00
if ( game.surfaces [ event.surface_index ] . name ~= GAME_SURFACE_NAME ) then return end
2019-09-29 13:32:20 -05:00
for k , v in pairs ( event.tiles ) do
2020-06-19 00:12:42 -04:00
RegrowthMarkAreaSafeGivenTilePos ( v.position , 2 , false )
2019-09-29 13:32:20 -05:00
end
end
end )
2020-05-09 15:39:32 -04: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-18 23:18:29 -04:00
if ( event.entity . surface.name ~= GAME_SURFACE_NAME ) then return end
2020-06-19 00:12:42 -04:00
RegrowthMarkAreaSafeGivenTilePos ( event.entity . position , 2 , false )
2020-05-09 15:39:32 -04:00
end
end )
2017-08-07 19:27:15 -04:00
2017-07-25 20:20:35 -04:00
----------------------------------------
-- Shared chat, so you don't have to type /s
2019-04-28 22:37:12 -05:00
-- But you do lose your player colors across forces.
2017-07-25 20:20:35 -04:00
----------------------------------------
script.on_event ( defines.events . on_console_chat , function ( event )
2019-06-07 22:52:14 -05:00
if ( event.player_index ) then
ServerWriteFile ( " server_chat " , game.players [ event.player_index ] . name .. " : " .. event.message .. " \n " )
end
2019-04-10 20:40:34 -04:00
if ( global.ocfg . enable_shared_chat ) then
2017-08-09 14:11:15 -04:00
if ( event.player_index ~= nil ) then
2017-08-07 19:27:15 -04:00
ShareChatBetweenForces ( game.players [ event.player_index ] , event.message )
end
2017-07-25 20:20:35 -04:00
end
end )
2016-12-02 16:26:49 -05:00
2016-11-30 21:41:04 -05:00
----------------------------------------
-- On Research Finished
2017-04-27 21:48:28 -04:00
-- This is where you can permanently remove researched techs
2016-11-30 21:41:04 -05:00
----------------------------------------
2019-03-01 23:05:23 -05:00
script.on_event ( defines.events . on_research_finished , function ( event )
2019-07-24 06:09:55 -05:00
2019-04-28 14:13:46 -04:00
-- Never allows players to build rocket-silos in "frontier" mode.
2019-05-15 14:40:32 +08: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 14:13:46 -04:00
2019-07-24 06:09:55 -05:00
if global.ocfg . lock_goodies_rocket_launch and
2020-06-18 23:18:29 -04:00
( not global.ocore . satellite_sent or not global.ocore . satellite_sent [ event.research . force.name ] ) then
2020-03-03 13:44:44 -05:00
for _ , v in ipairs ( LOCKED_RECIPES ) do
RemoveRecipe ( event.research . force , v.r )
end
2019-04-28 14:13:46 -04:00
end
2019-05-12 22:00:56 +08:00
2019-06-03 00:10:19 -05:00
if global.ocfg . enable_loaders then
2019-05-12 22:00:56 +08:00
EnableLoaders ( event )
end
2019-03-01 23:05:23 -05:00
end )
2016-12-19 16:35:36 -05:00
2019-03-11 09:29:00 -04:00
----------------------------------------
2019-07-02 18:13:48 -05:00
-- On Entity Spawned and On Biter Base Built
2019-03-11 09:29:00 -04: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-10 20:40:34 -04:00
if ( global.ocfg . modified_enemy_spawning ) then
2019-03-11 09:29:00 -04:00
ModifyEnemySpawnsNearPlayerStartingAreas ( event )
end
end )
2019-07-02 18:13:48 -05: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 16:52:38 -05:00
2020-02-15 22:03:25 -05: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 13:44:44 -05:00
if ( global.ocfg . enable_offline_protect ) then
OarcModifyEnemyGroup ( event.group )
end
2020-02-15 22:03:25 -05:00
end )
2019-05-03 16:52:38 -05: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 00:56:40 -05: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 15:53:01 -05: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 15:39:32 -04:00
OarcStoreOnGuiClosedEvent ( event )
2020-04-03 14:15:39 -04:00
end )
----------------------------------------
-- On enemies killed
-- For coin generation and stuff
----------------------------------------
2020-05-12 16:06:04 -04: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 11:50:22 -04:00
{ { filter = " type " , type = " unit " } , { filter = " type " , type = " unit-spawner " } , { filter = " type " , type = " turret " } } )
2020-05-12 16:06:04 -04: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 )