2021-05-09 15:47:39 +02:00
--[[
Journey , launch a rocket in increasingly harder getting worlds . - MewMew
] ] --
2021-05-25 22:12:08 +02:00
require ' modules.rocket_launch_always_yields_science '
local Server = require ' utils.server '
2021-05-09 15:47:39 +02:00
local Constants = require ' maps.journey.constants '
local Functions = require ' maps.journey.functions '
2021-05-21 01:01:48 +02:00
local Unique_modifiers = require ' maps.journey.unique_modifiers '
2021-05-09 15:47:39 +02:00
local Map = require ' modules.map_info '
local Global = require ' utils.global '
2023-08-14 09:44:12 +02:00
local Token = require ' utils.token '
local Event = require ' utils.event '
2023-08-15 01:57:39 +02:00
local Vacants = require ' modules.clear_vacant_players '
2021-05-09 15:47:39 +02:00
2023-04-07 00:30:44 +02:00
local journey = {
announce_capsules = true
}
2023-08-14 09:44:12 +02:00
local events = {
import = Event.generate_event_name ( ' import ' ) ,
}
2021-05-09 15:47:39 +02:00
Global.register (
journey ,
function ( tbl )
journey = tbl
end
)
2023-08-14 09:44:12 +02:00
journey.import = Token.register (
function ( data )
if not data then
return
end
script.raise_event ( events.import , data )
end
)
2021-05-09 15:47:39 +02:00
local function on_chunk_generated ( event )
local surface = event.surface
2023-04-07 00:30:44 +02:00
2021-05-21 01:01:48 +02:00
if surface.index == 1 then
2021-05-25 22:12:08 +02:00
Functions.place_mixed_ore ( event , journey )
2021-05-21 01:01:48 +02:00
local unique_modifier = Unique_modifiers [ journey.world_trait ]
if unique_modifier.on_chunk_generated then unique_modifier.on_chunk_generated ( event , journey ) end
return
end
2023-04-07 00:30:44 +02:00
2023-08-15 01:57:39 +02:00
if surface.name ~= ' mothership ' then return end
2021-05-09 15:47:39 +02:00
Functions.on_mothership_chunk_generated ( event )
end
2021-05-28 22:19:24 +02:00
local function on_console_chat ( event )
if not event.player_index then return end
local message = event.message
message = string.lower ( message )
2023-08-15 01:57:39 +02:00
local a = string.find ( message , ' ? ' , 1 , true )
2021-05-28 22:19:24 +02:00
if not a then return end
2023-08-15 01:57:39 +02:00
local b = string.find ( message , ' mother ' , 1 , true )
if not b then return end
2021-05-28 22:19:24 +02:00
local answer = Constants.mothership_messages . answers [ math.random ( 1 , # Constants.mothership_messages . answers ) ]
if math.random ( 1 , 4 ) == 1 then
2023-08-15 01:57:39 +02:00
for _ = 1 , math.random ( 2 , 5 ) , 1 do table.insert ( journey.mothership_messages , ' ' ) end
table.insert ( journey.mothership_messages , ' ... ' )
2021-05-28 22:19:24 +02:00
end
2023-08-15 01:57:39 +02:00
for _ = 1 , math.random ( 15 , 30 ) , 1 do table.insert ( journey.mothership_messages , ' ' ) end
2021-05-28 22:19:24 +02:00
table.insert ( journey.mothership_messages , answer )
end
2021-05-09 15:47:39 +02:00
local function on_player_joined_game ( event )
local player = game.players [ event.player_index ]
Functions.draw_gui ( journey )
2023-08-14 09:44:12 +02:00
Functions.set_minimum_to_vote ( journey )
2021-05-09 15:47:39 +02:00
2023-08-15 01:57:39 +02:00
if player.surface . name == ' mothership ' then
2021-05-09 15:47:39 +02:00
journey.characters_in_mothership = journey.characters_in_mothership + 1
end
2023-04-07 00:30:44 +02:00
2023-08-15 01:57:39 +02:00
if player.force . name == ' enemy ' then
2021-05-09 15:47:39 +02:00
Functions.clear_player ( player )
player.force = game.forces . player
2023-08-15 01:57:39 +02:00
local position = game.surfaces . nauvis.find_non_colliding_position ( ' character ' , { 0 , 0 } , 32 , 0.5 )
2021-05-21 01:01:48 +02:00
if position then
player.teleport ( position , game.surfaces . nauvis )
else
player.teleport ( { 0 , 0 } , game.surfaces . nauvis )
end
2023-04-07 00:30:44 +02:00
end
2021-05-09 15:47:39 +02:00
end
local function on_player_left_game ( event )
local player = game.players [ event.player_index ]
Functions.draw_gui ( journey )
2023-04-07 00:30:44 +02:00
2023-08-15 01:57:39 +02:00
if player.surface . name == ' mothership ' then
2021-05-09 15:47:39 +02:00
journey.characters_in_mothership = journey.characters_in_mothership - 1
2023-08-15 01:57:39 +02:00
player.clear_items_inside ( )
2021-05-09 15:47:39 +02:00
end
end
local function on_player_changed_position ( event )
local player = game.players [ event.player_index ]
Functions.teleporters ( journey , player )
2021-05-21 01:01:48 +02:00
local unique_modifier = Unique_modifiers [ journey.world_trait ]
if unique_modifier.on_player_changed_position then unique_modifier.on_player_changed_position ( event ) end
2021-05-09 15:47:39 +02:00
end
local function on_built_entity ( event )
Functions.deny_building ( event )
2021-05-28 22:19:24 +02:00
Functions.register_built_silo ( event , journey )
2021-05-21 01:01:48 +02:00
local unique_modifier = Unique_modifiers [ journey.world_trait ]
2021-05-28 22:19:24 +02:00
if unique_modifier.on_built_entity then unique_modifier.on_built_entity ( event , journey ) end
2021-05-09 15:47:39 +02:00
end
local function on_robot_built_entity ( event )
Functions.deny_building ( event )
2021-05-28 22:19:24 +02:00
Functions.register_built_silo ( event , journey )
2021-05-21 01:01:48 +02:00
local unique_modifier = Unique_modifiers [ journey.world_trait ]
2021-05-28 22:19:24 +02:00
if unique_modifier.on_robot_built_entity then unique_modifier.on_robot_built_entity ( event , journey ) end
2021-05-21 01:01:48 +02:00
end
local function on_player_mined_entity ( event )
local unique_modifier = Unique_modifiers [ journey.world_trait ]
2021-05-28 22:19:24 +02:00
if unique_modifier.on_player_mined_entity then unique_modifier.on_player_mined_entity ( event , journey ) end
2021-05-21 01:01:48 +02:00
end
local function on_robot_mined_entity ( event )
local unique_modifier = Unique_modifiers [ journey.world_trait ]
2021-05-28 22:19:24 +02:00
if unique_modifier.on_robot_mined_entity then unique_modifier.on_robot_mined_entity ( event , journey ) end
2021-05-21 01:01:48 +02:00
end
2023-06-11 01:22:53 +02:00
local function on_entity_damaged ( event )
local entity = event.entity
if not entity or not entity.valid then return end
if entity ~= journey.beacon_objective then return end
if event.force and event.force . name == ' enemy ' then
Functions.deal_damage_to_beacon ( journey , event.final_damage_amount )
end
entity.health = 200
end
2021-05-21 01:01:48 +02:00
local function on_entity_died ( event )
local unique_modifier = Unique_modifiers [ journey.world_trait ]
2021-05-28 22:19:24 +02:00
if unique_modifier.on_entity_died then unique_modifier.on_entity_died ( event , journey ) end
2021-05-09 15:47:39 +02:00
end
local function on_rocket_launched ( event )
local rocket_inventory = event.rocket . get_inventory ( defines.inventory . rocket )
local slot = rocket_inventory [ 1 ]
if slot and slot.valid and slot.valid_for_read then
if journey.mothership_cargo [ slot.name ] then
journey.mothership_cargo [ slot.name ] = journey.mothership_cargo [ slot.name ] + slot.count
else
journey.mothership_cargo [ slot.name ] = slot.count
end
2021-05-25 22:12:08 +02:00
if journey.mothership_cargo_space [ slot.name ] then
if journey.mothership_cargo [ slot.name ] > journey.mothership_cargo_space [ slot.name ] then
journey.mothership_cargo [ slot.name ] = journey.mothership_cargo_space [ slot.name ]
2023-04-07 00:30:44 +02:00
end
2023-07-17 19:34:22 +02:00
if slot.name == ' uranium-fuel-cell ' or slot.name == ' nuclear-reactor ' then
Server.to_discord_embed ( ' Refueling progress: ' .. slot.name .. ' : ' .. journey.mothership_cargo [ slot.name ] .. ' / ' .. journey.mothership_cargo_space [ slot.name ] )
2023-08-15 01:57:39 +02:00
elseif journey.speedrun . enabled and slot.name == journey.speedrun . item then
Server.to_discord_embed ( ' Orbital Station delivery: ' .. slot.name .. ' : ' .. journey.mothership_cargo [ slot.name ] .. ' / ' .. journey.mothership_cargo_space [ slot.name ] )
end
2023-04-07 00:30:44 +02:00
end
2021-05-09 15:47:39 +02:00
end
2021-05-25 22:12:08 +02:00
Functions.draw_gui ( journey )
2021-05-09 15:47:39 +02:00
end
2023-08-14 09:44:12 +02:00
local function make_import ( data )
if not data then
return
end
if data.key ~= ' journey_data ' then
return
end
local old_selectors = journey.world_selectors
for key , value in pairs ( data.value ) do
journey [ key ] = value
end
for k , selector in pairs ( old_selectors ) do
journey.world_selectors [ k ] . border = selector.border
journey.world_selectors [ k ] . texts = selector.texts
journey.world_selectors [ k ] . rectangles = selector.rectangles
end
journey.importing = true
game.print ( ' Journey data imported. ' )
journey.game_state = ' importing_world '
end
2021-05-09 15:47:39 +02:00
local function on_nth_tick ( )
Functions [ journey.game_state ] ( journey )
Functions.mothership_message_queue ( journey )
2023-09-18 05:23:53 +02:00
local tick = game.tick
if tick % 3600 == 0 then
Functions.lure_far_biters ( journey )
2023-09-18 22:36:30 +02:00
elseif tick % 600 == 0 then
2023-09-18 05:23:53 +02:00
Functions.lure_biters ( journey )
end
2021-05-09 15:47:39 +02:00
end
local function on_init ( )
local T = Map.Pop_info ( )
2021-07-21 03:13:29 +02:00
T.localised_category = ' journey '
2021-05-09 15:47:39 +02:00
T.main_caption_color = { r = 100 , g = 20 , b = 255 }
T.sub_caption_color = { r = 100 , g = 100 , b = 100 }
2023-04-07 00:30:44 +02:00
2021-05-25 22:12:08 +02:00
game.permissions . get_group ( ' Default ' ) . set_allows_action ( defines.input_action . set_auto_launch_rocket , false )
2023-08-15 01:57:39 +02:00
Vacants.init ( 1 , true )
2021-05-09 15:47:39 +02:00
Functions.hard_reset ( journey )
end
2023-08-14 09:44:12 +02:00
2023-06-11 01:22:53 +02:00
local function cmd_handler ( )
local player = game.player
local p
if not ( player and player.valid ) then
p = log
else
p = player.print
end
if player and not player.admin then
2023-08-15 01:57:39 +02:00
p ( ' You are not an admin! ' )
2023-08-14 09:44:12 +02:00
return false
2023-06-11 01:22:53 +02:00
end
2023-08-14 09:44:12 +02:00
return true , player or { name = ' Server ' } , p
2023-06-11 01:22:53 +02:00
end
2021-05-09 15:47:39 +02:00
commands.add_command (
2023-06-11 01:22:53 +02:00
' journey-reset ' ,
2021-05-09 15:47:39 +02:00
' Fully resets the journey map. ' ,
function ( )
2023-08-14 09:44:12 +02:00
local s , player = cmd_handler ( )
if s then
Functions.hard_reset ( journey )
2023-08-15 01:57:39 +02:00
game.print ( player.name .. ' has reset the map. ' )
2023-08-14 09:44:12 +02:00
end
2021-05-09 15:47:39 +02:00
end
)
2021-05-21 01:01:48 +02:00
commands.add_command (
2023-06-11 01:22:53 +02:00
' journey-skip-world ' ,
2021-05-21 01:01:48 +02:00
' Instantly wins and skips the current world. ' ,
function ( )
2023-08-14 09:44:12 +02:00
local s , _ , p = cmd_handler ( )
if s then
2023-08-15 01:57:39 +02:00
if journey.game_state ~= ' dispatch_goods ' and journey.game_state ~= ' world ' then return end
journey.game_state = ' set_world_selectors '
2023-08-14 09:44:12 +02:00
p ( ' The current world was skipped... ' )
end
2021-05-21 01:01:48 +02:00
end
)
2023-06-11 01:22:53 +02:00
commands.add_command (
' journey-update ' ,
' Restarts the server with newest version of Journey scenario code during next map reset ' ,
function ( )
2023-08-14 09:44:12 +02:00
local s , _ , p = cmd_handler ( )
if s then
journey.restart_from_scenario = not journey.restart_from_scenario
p ( ' Journey marking for full restart with updates on next reset was switched to ' .. tostring ( journey.restart_from_scenario ) )
end
end
)
commands.add_command (
' journey-import ' ,
' Sets the journey gamestate to the last exported one. ' ,
function ( )
local s , _ , p = cmd_handler ( )
if s then
Functions.import_journey ( journey )
p ( ' Journey world settings importing... ' )
end
end
)
commands.add_command (
' journey-export ' ,
' Exports the journey gamestate to the server ' ,
function ( )
local s , _ , p = cmd_handler ( )
if s then
Functions.export_journey ( journey )
p ( ' Journey world settings exporting... ' )
end
2023-06-11 01:22:53 +02:00
end
)
2021-05-09 15:47:39 +02:00
Event.on_init ( on_init )
Event.on_nth_tick ( 10 , on_nth_tick )
Event.add ( defines.events . on_chunk_generated , on_chunk_generated )
Event.add ( defines.events . on_player_joined_game , on_player_joined_game )
Event.add ( defines.events . on_player_left_game , on_player_left_game )
Event.add ( defines.events . on_player_changed_position , on_player_changed_position )
Event.add ( defines.events . on_rocket_launched , on_rocket_launched )
Event.add ( defines.events . on_robot_built_entity , on_robot_built_entity )
2021-05-21 01:01:48 +02:00
Event.add ( defines.events . on_built_entity , on_built_entity )
Event.add ( defines.events . on_robot_mined_entity , on_robot_mined_entity )
Event.add ( defines.events . on_player_mined_entity , on_player_mined_entity )
Event.add ( defines.events . on_entity_died , on_entity_died )
2023-06-11 01:22:53 +02:00
Event.add ( defines.events . on_entity_damaged , on_entity_damaged )
2021-05-28 22:19:24 +02:00
Event.add ( defines.events . on_console_chat , on_console_chat )
2023-08-14 09:44:12 +02:00
Event.add ( events [ ' import ' ] , make_import )