2019-01-17 09:15:35 +01:00
-- launch fish into space to win the game -- by mewmew
2020-11-04 20:53:42 +01:00
local Event = require ' utils.event '
2023-11-13 23:58:43 +01:00
local Gui = require ' utils.gui '
2020-11-04 20:53:42 +01:00
2025-10-27 23:57:30 +01:00
local Public = { }
Gui.mod_gui_button_enabled = true
Gui.button_style = ' mod_gui_button '
Gui.set_mod_gui_top_frame ( true )
local function get_top_frame ( player , id )
if not id then
id = ' fish_in_space_toggle '
2020-11-04 20:53:42 +01:00
end
2025-10-27 23:57:30 +01:00
if Gui.get_mod_gui_top_frame ( ) then
return Gui.get_button_flow ( player ) [ id ]
else
return player.gui . top [ id ]
end
end
local function goals ( )
storage.catplanet_goals =
{
{ goal = 0 , rank = false , achieved = true } ,
{
goal = 100 ,
rank = ' Copper ' ,
color = { r = 201 , g = 133 , b = 6 } ,
msg = ' You have saved the first container of fish! ' ,
msg2 = ' However, this is only the beginning. ' ,
achieved = false
} ,
{
goal = 1000 ,
rank = ' Bronze ' ,
color = { r = 186 , g = 115 , b = 39 } ,
msg = ' Thankful for the fish, they sent back a toy mouse made of solid bronze! ' ,
msg2 = ' They are demanding more. ' ,
achieved = false
} ,
{
goal = 10000 ,
rank = ' Silver ' ,
color = { r = 186 , g = 178 , b = 171 } ,
msg = ' In gratitude for the fish, they left you a silver furball! ' ,
msg2 = ' They are still longing for more. ' ,
achieved = false
} ,
{
goal = 25000 ,
rank = ' Gold ' ,
color = { r = 255 , g = 214 , b = 33 } ,
msg = ' Pleased about the delivery, they sent back a golden audiotape with cat purrs. ' ,
msg2 = ' They still demand more. ' ,
achieved = false
} ,
{
goal = 50000 ,
rank = ' Platinum ' ,
color = { r = 224 , g = 223 , b = 215 } ,
msg = ' To express their infinite love, they sent back a yarnball made of shiny material. ' ,
msg2 = ' Defying all logic, they still demand more fish. ' ,
achieved = false
} ,
{
goal = 100000 ,
rank = ' Diamond ' ,
color = { r = 237 , g = 236 , b = 232 } ,
msg = ' A box arrives with a mewing kitten, it a has a diamond collar. ' ,
msg2 = ' More fish? Why? What.. ' ,
achieved = false
} ,
{
goal = 250000 ,
rank = ' Anti-matter ' ,
color = { r = 100 , g = 100 , b = 245 } ,
msg = ' The obese cat colapses and forms a black hole! ' ,
msg2 = ' :obese: ' ,
achieved = false
} ,
{
goal = 500000 ,
rank = ' Black Hole ' ,
color = { r = 100 , g = 100 , b = 245 } ,
msg = ' A letter arrives, it reads: Go to bed hooman! ' ,
msg2 = ' Not yet... ' ,
achieved = false
} ,
{
goal = 1000000 ,
rank = ' Blue Screen ' ,
color = { r = 100 , g = 100 , b = 245 } ,
msg = ' Cat error #4721 ' ,
msg2 = ' .... ' ,
achieved = false
} ,
{ goal = 10000000 , rank = ' Blue Screen ' , color = { r = 100 , g = 100 , b = 245 } , msg = ' .... ' , msg2 = ' .... ' , achieved = false }
}
2020-11-04 20:53:42 +01:00
end
2019-01-17 09:15:35 +01:00
local function get_rank ( )
2024-09-24 19:37:11 +02:00
if not storage.catplanet_goals then
2020-11-04 20:53:42 +01:00
goals ( )
end
2024-09-24 19:37:11 +02:00
for i = # storage.catplanet_goals , 1 , - 1 do
if storage.fish_in_space >= storage.catplanet_goals [ i ] . goal then
2020-11-04 20:53:42 +01:00
return i
end
end
2019-01-17 09:15:35 +01:00
end
local function fish_in_space_toggle_button ( player )
2025-10-27 23:57:30 +01:00
local button = get_top_frame ( player )
if button then
2020-11-04 20:53:42 +01:00
return
end
2025-10-27 23:57:30 +01:00
if Gui.get_mod_gui_top_frame ( ) then
button = Gui.add_mod_button ( player , { name = ' fish_in_space_toggle ' , type = ' sprite-button ' , sprite = ' item/raw-fish ' , tooltip = ' Fish in Space ' , style = Gui.button_style } )
else
button = player.gui . top.add { name = ' fish_in_space_toggle ' , type = ' sprite-button ' , sprite = ' item/raw-fish ' , tooltip = ' Fish in Space ' , style = Gui.button_style }
end
if button then
button.style . minimal_height = 36
button.style . maximal_height = 36
end
2019-01-17 09:15:35 +01:00
end
2020-11-04 20:53:42 +01:00
local function level_up_popup ( player )
2024-09-24 19:37:11 +02:00
local reward = storage.catplanet_goals [ get_rank ( ) ]
2020-11-04 20:53:42 +01:00
if player.gui . center [ ' level_up_popup ' ] then
player.gui . center [ ' level_up_popup ' ] . destroy ( )
end
2024-09-24 19:37:11 +02:00
local frame = player.gui . center.add ( { type = ' frame ' , name = ' level_up_popup ' , direction = ' vertical ' } )
local label = frame.add ( { type = ' label ' , caption = reward.msg } )
2020-11-04 20:53:42 +01:00
label.style . font = ' default-listbox '
label.style . font_color = reward.color
2024-09-24 19:37:11 +02:00
local button = frame.add ( { type = ' button ' , caption = reward.msg2 , name = ' level_up_popup_close ' } )
2020-11-04 20:53:42 +01:00
button.style . minimal_width = string.len ( reward.msg ) * 7
button.style . font = ' default-listbox '
2024-09-24 19:37:11 +02:00
button.style . font_color = { r = 0.77 , g = 0.77 , b = 0.77 }
2019-01-17 09:15:35 +01:00
end
2020-11-04 20:53:42 +01:00
local function fish_in_space_gui ( player )
2024-09-24 19:37:11 +02:00
if storage.fish_in_space == 0 then
2020-11-04 20:53:42 +01:00
return
end
local i = get_rank ( )
fish_in_space_toggle_button ( player )
if player.gui . left [ ' fish_in_space ' ] then
player.gui . left [ ' fish_in_space ' ] . destroy ( )
end
2024-09-24 19:37:11 +02:00
local frame = player.gui . left.add ( { type = ' frame ' , name = ' fish_in_space ' } )
local label = frame.add ( { type = ' label ' , caption = ' Fish rescued: ' } )
label.style . font_color = { r = 0.11 , g = 0.8 , b = 0.44 }
2023-11-13 23:58:43 +01:00
frame.style . bottom_padding = - 2
2020-11-04 20:53:42 +01:00
2024-10-22 23:49:08 +02:00
frame.style . minimal_height = 40
2024-09-24 19:37:11 +02:00
local progress = storage.fish_in_space / storage.catplanet_goals [ i + 1 ] . goal
2020-11-04 20:53:42 +01:00
if progress > 1 then
progress = 1
end
2024-09-24 19:37:11 +02:00
local progressbar = frame.add ( { type = ' progressbar ' , value = progress } )
2025-10-27 23:57:30 +01:00
---@class LuaGuiElementStyle
2022-09-15 00:01:51 +02:00
progressbar.style = ' achievement_progressbar '
progressbar.style . minimal_width = 96
progressbar.style . maximal_width = 96
progressbar.style . padding = - 1
progressbar.style . top_padding = 1
2024-09-25 20:51:01 +02:00
progressbar.style . height = 20
2020-11-04 20:53:42 +01:00
2024-09-24 19:37:11 +02:00
label = frame.add ( { type = ' label ' , caption = storage.fish_in_space .. ' / ' .. tostring ( storage.catplanet_goals [ i + 1 ] . goal ) } )
label.style . font_color = { r = 0.33 , g = 0.66 , b = 0.9 }
2020-11-04 20:53:42 +01:00
2024-09-24 19:37:11 +02:00
if storage.catplanet_goals [ i ] . rank then
label = frame.add ( { type = ' label ' , caption = ' ~Rank~ ' } )
label.style . font_color = { r = 0.75 , g = 0.75 , b = 0.75 }
label = frame.add ( { type = ' label ' , caption = storage.catplanet_goals [ i ] . rank } )
2020-11-04 20:53:42 +01:00
label.style . font = ' default-bold '
2024-09-24 19:37:11 +02:00
label.style . font_color = storage.catplanet_goals [ i ] . color
2020-11-04 20:53:42 +01:00
end
2019-01-17 09:15:35 +01:00
end
local function fireworks ( entity )
2020-11-04 20:53:42 +01:00
for x = entity.position . x - 32 , entity.position . x + 32 , 1 do
for y = entity.position . y - 32 , entity.position . y + 32 , 1 do
if math.random ( 1 , 150 ) == 1 then
2024-09-24 19:37:11 +02:00
entity.surface . create_entity ( { name = ' big-explosion ' , position = { x = x , y = y } } )
2020-11-04 20:53:42 +01:00
end
if math.random ( 1 , 150 ) == 1 then
2024-09-24 19:37:11 +02:00
entity.surface . create_entity ( { name = ' uranium-cannon-shell-explosion ' , position = { x = x , y = y } } )
2020-11-04 20:53:42 +01:00
end
if math.random ( 1 , 150 ) == 1 then
2024-09-24 19:37:11 +02:00
entity.surface . create_entity ( { name = ' blood-explosion-huge ' , position = { x = x , y = y } } )
2020-11-04 20:53:42 +01:00
end
if math.random ( 1 , 150 ) == 1 then
2024-09-24 19:37:11 +02:00
entity.surface . create_entity ( { name = ' big-artillery-explosion ' , position = { x = x , y = y } } )
2020-11-04 20:53:42 +01:00
end
end
end
2019-01-17 09:15:35 +01:00
end
2020-11-04 20:53:42 +01:00
local function on_rocket_launched ( event )
2024-10-22 23:49:08 +02:00
local rocket_inventory = event.rocket . cargo_pod.get_inventory ( defines.inventory . cargo_unit )
local slot = rocket_inventory [ 1 ]
if slot and slot.valid and slot.valid_for_read then
if slot.name ~= " raw-fish " then
return
end
2020-11-04 20:53:42 +01:00
end
2024-10-22 23:49:08 +02:00
2025-10-27 23:57:30 +01:00
if storage.fish_in_space == 0 then
goals ( )
end
2024-10-22 23:49:08 +02:00
rocket_inventory.clear ( )
rocket_inventory.insert ( { name = ' space-science-pack ' , count = 200 } )
storage.fish_in_space = storage.fish_in_space + slot.count
2020-11-04 20:53:42 +01:00
local i = get_rank ( )
for _ , player in pairs ( game.connected_players ) do
fish_in_space_gui ( player )
end
2024-09-24 19:37:11 +02:00
if not storage.catplanet_goals [ i ] . achieved then
2020-11-04 20:53:42 +01:00
for _ , player in pairs ( game.connected_players ) do
2024-09-24 19:37:11 +02:00
player.play_sound { path = ' utility/game_won ' , volume_modifier = 0.9 }
2020-11-04 20:53:42 +01:00
level_up_popup ( player )
end
2024-09-24 19:37:11 +02:00
storage.catplanet_goals [ i ] . achieved = true
2020-11-04 20:53:42 +01:00
fireworks ( event.rocket_silo )
end
2019-01-17 09:15:35 +01:00
end
local function init ( )
2024-09-24 19:37:11 +02:00
storage.fish_in_space = 0
2019-01-17 09:15:35 +01:00
end
local function on_player_joined_game ( event )
2024-09-24 19:37:11 +02:00
if not storage.fish_in_space then
2020-11-04 20:53:42 +01:00
init ( )
end
local player = game.players [ event.player_index ]
fish_in_space_gui ( player )
2019-01-17 09:15:35 +01:00
end
2020-11-04 20:53:42 +01:00
local function on_gui_click ( event )
if not event then
return
end
if not event.element then
return
end
if not event.element . valid then
return
end
local player = game.players [ event.element . player_index ]
local name = event.element . name
if name == ' fish_in_space_toggle ' then
local frame = player.gui . left [ ' fish_in_space ' ]
if frame then
frame.destroy ( )
else
fish_in_space_gui ( player )
end
end
if name == ' level_up_popup_close ' then
player.gui . center [ ' level_up_popup ' ] . destroy ( )
end
2019-01-17 09:15:35 +01:00
end
2020-09-06 05:52:03 +02:00
local function on_init ( )
2024-09-24 19:37:11 +02:00
storage.fish_autolaunch = true
2020-11-04 20:53:42 +01:00
goals ( )
2024-09-24 19:37:11 +02:00
storage.rocket_silos = { }
2020-09-06 05:52:03 +02:00
end
local function tick ( )
2024-09-24 19:37:11 +02:00
if not storage.fish_autolaunch then
2020-11-04 20:53:42 +01:00
return
end
if game.tick % 6000 == 0 then
local found_silos = { }
for _ , surface in pairs ( game.surfaces ) do
2024-09-24 19:37:11 +02:00
local objects = surface.find_entities_filtered { name = ' rocket-silo ' }
2020-11-04 20:53:42 +01:00
for _ , object in pairs ( objects ) do
table.insert ( found_silos , object )
end
end
2024-09-24 19:37:11 +02:00
storage.rocket_silos = found_silos
2020-11-04 20:53:42 +01:00
end
2024-09-24 19:37:11 +02:00
for index , silo in pairs ( storage.rocket_silos ) do
2020-11-04 20:53:42 +01:00
if silo.valid and silo.name == ' rocket-silo ' then
local rocket_inventory = silo.get_inventory ( defines.inventory . rocket_silo_rocket )
local fish
if rocket_inventory and rocket_inventory.valid then
fish = rocket_inventory [ 1 ]
end
if fish and fish.valid_for_read and fish.count == 100 and fish.name == ' raw-fish ' then
silo.launch_rocket ( )
end
else
2024-09-24 19:37:11 +02:00
storage.rocket_silos [ index ] = nil
2020-11-04 20:53:42 +01:00
end
end
2020-09-06 05:52:03 +02:00
end
2020-11-04 20:53:42 +01:00
Event.on_nth_tick ( 60 , tick )
Event.on_init ( on_init )
Event.add ( defines.events . on_gui_click , on_gui_click )
Event.add ( defines.events . on_player_joined_game , on_player_joined_game )
2024-10-22 23:49:08 +02:00
Event.add ( defines.events . on_rocket_launch_ordered , on_rocket_launched )
2025-10-27 23:57:30 +01:00
Public.get_top_frame = get_top_frame
Public.fish_in_space_toggle_button = fish_in_space_toggle_button
Public.level_up_popup = level_up_popup
Public.fish_in_space_gui = fish_in_space_gui
Public.fireworks = fireworks
return Public