2024-09-10 12:29:44 +02:00
-- This file is part of thesixthroc's Pirate Ship softmod, licensed under GPLv3 and stored at https://github.com/ComfyFactory/ComfyFactorio and https://github.com/danielmartin0/ComfyFactorio-Pirates.
2021-10-13 10:21:53 +02:00
local Memory = require ' maps.pirates.memory '
local Common = require ' maps.pirates.common '
local CoreData = require ' maps.pirates.coredata '
local Utils = require ' maps.pirates.utils_local '
local Math = require ' maps.pirates.math '
2022-03-19 23:20:55 +02:00
-- local Surfaces = require 'maps.pirates.surfaces.surfaces'
2023-02-02 21:44:44 +02:00
local Roles = require ' maps.pirates.roles.roles '
2021-10-13 10:21:53 +02:00
local Crew = require ' maps.pirates.crew '
local Progression = require ' maps.pirates.progression '
2022-03-19 23:20:55 +02:00
-- local Structures = require 'maps.pirates.structures.structures'
local _inspect = require ' utils.inspect ' . inspect
2021-10-13 10:21:53 +02:00
local Boats = require ' maps.pirates.structures.boats.boats '
local GuiCommon = require ' maps.pirates.gui.common '
2022-03-19 23:20:55 +02:00
-- local Server = require 'utils.server'
2021-10-13 10:21:53 +02:00
local Public = { }
local window_name = ' runs '
local function flow_add_proposal_slider ( flow , name , displayname , indices_count , starting_index , tooltip )
local flow2 , flow3 , flow4
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
flow2 = flow.add ( {
name = name ,
type = ' flow ' ,
direction = ' vertical ' ,
} )
flow2.style . horizontal_align = ' left '
flow2.style . width = 130
flow3 = flow2.add ( {
type = ' label ' ,
caption = displayname ,
} )
flow3.style . font = ' heading-3 '
flow3.style . height = 20
flow3.style . margin = 0
flow3.style . padding = 0
flow3.style . top_padding = - 4
flow3.style . bottom_margin = 0
flow3.style . font_color = GuiCommon.subsection_header_font_color
flow3.tooltip = tooltip
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = name ,
type = ' flow ' ,
direction = ' vertical ' ,
} )
flow3.style . horizontal_align = ' center '
flow3.style . width = 130
flow4 = flow3.add ( {
name = ' slider ' ,
type = ' slider ' ,
value_step = 1 ,
minimum_value = 1 ,
maximum_value = indices_count ,
value = starting_index ,
discrete_values = true ,
discrete_slider = true ,
} )
flow4.style . width = 100
flow4.style . margin = 0
flow4.tooltip = tooltip
flow4 = flow3.add ( {
name = ' readoff_text ' ,
type = ' label ' ,
caption = ' ' ,
} )
flow4.style . font = ' default-listbox '
flow4.style . height = 20
flow4.style . margin = 0
flow4.style . padding = 0
flow4.style . top_padding = 0
flow4.style . bottom_margin = 16
flow4.tooltip = tooltip
flow2 = flow.add ( {
name = name .. ' _readoff_icon ' ,
type = ' sprite-button ' ,
enabled = false ,
} )
flow2.style . width = 48
flow2.style . height = 48
flow2.tooltip = tooltip
return flow2
end
2022-03-19 23:20:55 +02:00
-- commented out for luacheck:
-- local function flow_add_proposal_switch(flow, name, displayname, starting_position, tooltip)
-- local flow2, flow3, flow4
-- flow2 = flow.add({
-- name = name,
-- type = 'flow',
-- direction = 'vertical',
-- })
-- flow2.style.horizontal_align = 'left'
-- flow2.style.width = 130
-- flow3 = flow2.add({
-- type = 'label',
-- caption = displayname,
-- })
-- flow3.style.font = 'heading-3'
-- flow3.style.height = 20
-- flow3.style.margin = 0
-- flow3.style.padding = 0
-- flow3.style.top_padding = -4
-- flow3.style.bottom_margin = 0
-- flow3.style.font_color = GuiCommon.subsection_header_font_color
-- flow3.tooltip = tooltip
-- flow3 = flow2.add({
-- name = name,
-- type = 'flow',
-- direction = 'vertical',
-- })
-- flow3.style.horizontal_align = 'center'
-- flow3.style.width = 130
-- flow4 = flow3.add({
-- name = 'switch',
-- type = 'switch',
-- switch_state = starting_position,
-- })
-- -- flow4.style.width = 80
-- -- flow4.style.height = 40
-- flow4.style.margin = 0
-- flow4.tooltip = tooltip
-- flow4 = flow3.add({
-- name = 'readoff_text',
-- type = 'label',
-- caption = '',
-- })
-- flow4.style.font = 'default-listbox'
-- flow4.style.height = 20
-- flow4.style.margin = 0
-- flow4.style.padding = 0
-- flow4.style.top_padding = 0
-- flow4.style.bottom_margin = 16
-- flow4.tooltip = tooltip
-- flow2 = flow.add({
-- name = name .. '_readoff_icon',
-- type = 'sprite-button',
-- enabled = false,
-- })
-- flow2.style.width = 48
-- flow2.style.height = 48
-- flow2.tooltip = tooltip
-- return flow2
-- end
2021-10-13 10:21:53 +02:00
function Public . toggle_window ( player )
2022-10-10 19:39:08 +02:00
local window
2022-03-19 23:20:55 +02:00
local flow , flow2 , flow3 , flow4 , flow5
2021-10-13 10:21:53 +02:00
--*** OVERALL FLOW ***--
if player.gui . screen [ window_name .. ' _piratewindow ' ] then player.gui . screen [ window_name .. ' _piratewindow ' ] . destroy ( ) return end
2022-03-19 23:20:55 +02:00
2022-10-10 19:39:08 +02:00
window = GuiCommon.new_window ( player , window_name )
window.caption = { ' pirates.gui_runs_play ' }
flow = window.add {
type = ' scroll-pane ' ,
name = ' scroll_pane ' ,
direction = ' vertical ' ,
horizontal_scroll_policy = ' never ' ,
2023-02-02 21:44:44 +02:00
vertical_scroll_policy = ' auto-and-reserve-space '
2022-10-10 19:39:08 +02:00
}
flow.style . maximal_height = 500
2023-02-01 22:17:22 +02:00
flow.style . bottom_margin = 10
2021-10-13 10:21:53 +02:00
--*** ONGOING RUNS ***--
2022-05-29 13:36:27 +02:00
flow2 = GuiCommon.flow_add_section ( flow , ' ongoing_runs ' , { ' pirates.gui_runs_ongoing_runs ' } )
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = ' helpful_tip ' ,
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_ongoing_runs_helpful_tip ' } ,
2021-10-13 10:21:53 +02:00
} )
flow3.style . font_color = { r = 0.90 , g = 0.90 , b = 0.90 }
flow3.style . single_line = false
flow3.style . maximal_width = 160
flow3 = flow2.add ( {
name = ' ongoing_runs_listbox ' ,
type = ' list-box ' ,
} )
flow3.style . margin = 2
2023-02-02 21:44:44 +02:00
flow3.style . right_margin = 5
2021-10-13 10:21:53 +02:00
flow3.style . horizontally_stretchable = true
2022-10-10 19:21:14 +02:00
flow3 = flow2.add ( {
2023-02-02 21:44:44 +02:00
name = ' join_protected_crew_info ' ,
type = ' label ' ,
caption = { ' pirates.gui_join_protected_run_info ' , 0 , 0 , 0 } ,
visible = false ,
} )
flow3.style . single_line = false
flow3 = flow2.add ( {
name = ' join_private_crew_info ' ,
2022-10-10 19:21:14 +02:00
type = ' label ' ,
caption = { ' pirates.gui_join_private_run_info ' , 0 , 0 , 0 } ,
visible = false ,
} )
flow3.style . single_line = false
flow3 = flow2.add ( {
name = ' password_namefield ' ,
type = ' textfield ' ,
text = ' ' ,
visible = false ,
} )
flow3.style . width = 150
flow3.style . height = 24
flow3.style . top_margin = - 3
flow3.style . bottom_margin = 3
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = ' flow_buttons ' ,
type = ' flow ' ,
direction = ' horizontal ' ,
} )
flow4 = flow3.add ( {
name = ' join_spectators ' ,
type = ' button ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_ongoing_runs_spectate ' } ,
2021-10-13 10:21:53 +02:00
} )
flow4.style . minimal_width = 95
flow4.style . font = ' default-bold '
flow4.style . font_color = { r = 0.10 , g = 0.10 , b = 0.10 }
flow4 = flow3.add ( {
name = ' join_crew ' ,
type = ' button ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_ongoing_runs_join_crew ' } ,
2021-10-13 10:21:53 +02:00
} )
flow4.style . minimal_width = 95
flow4.style . font = ' default-bold '
flow4.style . font_color = { r = 0.10 , g = 0.10 , b = 0.10 }
flow4 = flow3.add ( {
name = ' leave_spectators ' ,
type = ' button ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_ongoing_runs_return_to_lobby ' } ,
2021-10-13 10:21:53 +02:00
} )
flow4.style . minimal_width = 95
flow4.style . font = ' default-bold '
flow4.style . font_color = { r = 0.10 , g = 0.10 , b = 0.10 }
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = ' wait_to_join ' ,
type = ' label ' ,
} )
flow3.style . left_margin = 5
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = ' leaving_prompt ' ,
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_ongoing_runs_hop_on_board ' } ,
2021-10-13 10:21:53 +02:00
} )
flow3.style . left_margin = 5
-- PROPOSALS --
2022-05-29 13:36:27 +02:00
flow2 = GuiCommon.flow_add_section ( flow , ' proposals ' , { ' pirates.gui_runs_proposals ' } )
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = ' proposals_listbox ' ,
type = ' list-box ' ,
} )
flow3.style . margin = 2
2023-02-02 21:44:44 +02:00
flow3.style . right_margin = 5
flow3.style . horizontally_stretchable = true
2021-10-13 10:21:53 +02:00
flow3 = flow2.add ( {
name = ' flow_buttons ' ,
type = ' flow ' ,
direction = ' horizontal ' ,
} )
flow4 = flow3.add ( {
name = ' abandon_proposal ' ,
type = ' button ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_proposals_abandon_proposal ' } ,
2021-10-13 10:21:53 +02:00
} )
flow4.style . minimal_width = 150
flow4.style . font = ' default-bold '
flow4.style . font_color = { r = 0.10 , g = 0.10 , b = 0.10 }
flow4.style . bottom_margin = 9
-- PROPOSAL MAKER --
flow3 = GuiCommon.flow_add_subpanel ( flow2 , ' proposal_maker ' )
flow4 = flow3.add ( {
name = ' body ' ,
type = ' flow ' ,
direction = ' vertical ' ,
} )
flow4.style . horizontal_align = ' center '
flow4.style . vertical_align = ' center '
flow5 = flow4.add ( {
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_proposal_maker_run_name ' } ,
2021-10-13 10:21:53 +02:00
} )
flow5.style . font = ' heading-3 '
flow5 = flow4.add ( {
name = ' namefield ' ,
type = ' textfield ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_proposal_maker_run_name_2 ' } ,
2021-10-13 10:21:53 +02:00
text = ' ' ,
} )
flow5.style . width = 150
flow5.style . height = 24
flow5.style . top_margin = - 3
flow5.style . bottom_margin = 3
2023-02-02 21:44:44 +02:00
-- PROTECTED RUN ELEMENTS --
flow4.add ( {
name = ' protected_checkbox ' ,
type = ' checkbox ' ,
caption = { ' pirates.gui_runs_proposal_maker_protected ' } ,
state = false ,
tooltip = { ' pirates.gui_runs_proposal_maker_protected_tooltip ' , CoreData.protected_run_lock_amount_hr }
} )
2022-10-10 19:21:14 +02:00
-- PRIVATE RUN ELEMENTS --
2022-10-28 15:22:59 +02:00
flow4.add ( {
2022-10-10 19:21:14 +02:00
name = ' private_checkbox ' ,
type = ' checkbox ' ,
caption = { ' pirates.gui_runs_proposal_maker_private ' } ,
state = false ,
tooltip = { ' pirates.gui_runs_proposal_maker_private_tooltip ' , CoreData.private_run_lock_amount_hr }
} )
flow5 = flow4.add ( {
name = ' password_label ' ,
type = ' label ' ,
caption = { ' pirates.gui_runs_proposal_maker_password ' } ,
} )
flow5.style . font = ' heading-3 '
flow5 = flow4.add ( {
name = ' password_namefield ' ,
type = ' textfield ' ,
text = ' ' ,
} )
flow5.style . width = 150
flow5.style . height = 24
flow5.style . top_margin = - 3
flow5.style . bottom_margin = 3
flow5 = flow4.add ( {
name = ' confirm_password_label ' ,
type = ' label ' ,
caption = { ' pirates.gui_runs_proposal_maker_confirm_password ' } ,
} )
flow5.style . font = ' heading-3 '
flow5 = flow4.add ( {
name = ' confirm_password_namefield ' ,
type = ' textfield ' ,
text = ' ' ,
} )
flow5.style . width = 150
flow5.style . height = 24
flow5.style . top_margin = - 3
flow5.style . bottom_margin = 3
-- CREW SIZE LIMIT SLIDER --
2021-10-13 10:21:53 +02:00
flow5 = flow4.add ( {
name = ' options ' ,
type = ' table ' ,
column_count = 2 ,
} )
flow5.style . width = 200
flow5.style . margin = 0
2022-05-29 13:36:27 +02:00
flow_add_proposal_slider ( flow5 , ' capacity ' , { ' pirates.gui_runs_proposal_maker_capacity ' } , # CoreData.capacity_options , 5 , { ' pirates.capacity_tooltip ' } )
2021-10-13 10:21:53 +02:00
-- flow_add_proposal_slider(flow5, 'difficulty', 'Difficulty', #CoreData.difficulty_options, 2, {'pirates.difficulty_tooltip'})
-- flow_add_proposal_switch(flow5, 'mode', 'Mode', 'left', {'pirates.mode_tooltip'})
-- flow5 = flow4.add({
-- name = 'proposal_cant_do_infinity_mode',
-- type = 'label',
-- caption = 'Infinity mode isn\'t available at the moment.',
-- })
-- flow5.style.single_line = false
-- flow5.style.maximal_width = 200
flow5 = flow4.add ( {
name = ' proposal_disabled_low_crew_caps ' ,
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_proposal_maker_capacity_disabled ' } ,
2021-10-13 10:21:53 +02:00
} )
flow5.style . single_line = false
flow5.style . maximal_width = 200
flow5 = flow4.add ( {
name = ' propose_crew ' ,
type = ' button ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_proposal_maker_propose ' } ,
2021-10-13 10:21:53 +02:00
} )
2022-03-19 23:20:55 +02:00
flow5.style . minimal_width = 75
flow5.style . font = ' default-bold '
flow5.style . font_color = { r = 0.10 , g = 0.10 , b = 0.10 }
2021-10-13 10:21:53 +02:00
-- LAUNCH YOUR PROPOSAL --
flow3 = flow2.add ( {
name = ' flow_proposal_launch ' ,
type = ' flow ' ,
direction = ' vertical ' ,
} )
2022-03-11 00:11:51 +02:00
flow4 = flow3.add ( {
name = ' proposal_crew_count_capped ' ,
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_launch_error_2 ' } ,
2022-03-11 00:11:51 +02:00
} )
flow4.style . single_line = false
2021-10-13 10:21:53 +02:00
flow4 = flow3.add ( {
name = ' proposal_insufficient_player_capacity ' ,
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_launch_error_3 ' } ,
2021-10-13 10:21:53 +02:00
} )
2022-03-11 00:11:51 +02:00
flow4.style . single_line = false
2021-10-13 10:21:53 +02:00
flow4 = flow3.add ( {
name = ' proposal_insufficient_sloops ' ,
type = ' label ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_launch_error_4 ' } ,
2021-10-13 10:21:53 +02:00
} )
2022-03-11 00:11:51 +02:00
flow4.style . single_line = false
2021-10-13 10:21:53 +02:00
flow4 = flow3.add ( {
name = ' launch_crew ' ,
type = ' button ' ,
2022-05-29 13:36:27 +02:00
caption = { ' pirates.gui_runs_launch ' } ,
2021-10-13 10:21:53 +02:00
} )
flow4.style . minimal_width = 150
flow4.style . font = ' default-bold '
flow4.style . font_color = { r = 0.10 , g = 0.10 , b = 0.10 }
2023-02-01 22:17:22 +02:00
GuiCommon.flow_add_close_button ( window , window_name .. ' _piratebutton ' )
2021-10-13 10:21:53 +02:00
end
2022-03-19 23:20:55 +02:00
-- function Public.regular_update(player)
2021-10-13 10:21:53 +02:00
2022-03-19 23:20:55 +02:00
-- end
2022-03-13 03:44:32 +02:00
function Public . full_update ( player )
2022-03-20 13:41:23 +02:00
if Public.regular_update then Public.regular_update ( player ) end
2021-10-13 10:21:53 +02:00
local global_memory = Memory.get_global_memory ( )
local memory = Memory.get_crew_memory ( )
if not player.gui . screen [ ' runs_piratewindow ' ] then return end
2022-10-10 19:39:08 +02:00
local flow = player.gui . screen [ ' runs_piratewindow ' ] . scroll_pane
2022-03-19 23:20:55 +02:00
local playercrew_status = GuiCommon.crew_overall_state_bools ( player.index )
2021-10-13 10:21:53 +02:00
if not playercrew_status then return end
--*** WHAT TO SHOW ***--
flow.ongoing_runs . visible = ( # global_memory.crew_active_ids > 0 )
if flow.ongoing_runs . visible then
local bool1 = ( not playercrew_status.leaving ) and ( not playercrew_status.adventuring ) and ( not playercrew_status.spectating ) and ( flow.ongoing_runs . body.ongoing_runs_listbox . selected_index ~= 0 )
local selected_joinable_bool = false
local crewid
if bool1 then
crewid = tonumber ( ( flow.ongoing_runs . body.ongoing_runs_listbox . get_item ( flow.ongoing_runs . body.ongoing_runs_listbox . selected_index ) ) [ 2 ] )
2024-09-07 23:26:53 +02:00
selected_joinable_bool = bool1 and crewid ~= nil and global_memory.crew_memories [ crewid ] ~= nil and ( global_memory.crew_memories [ crewid ] . crewstatus == Crew.enum . ADVENTURING )
2021-10-13 10:21:53 +02:00
end
flow.ongoing_runs . body.helpful_tip . visible = not ( playercrew_status.leaving or playercrew_status.adventuring or playercrew_status.spectating )
flow.ongoing_runs . body.flow_buttons . visible = selected_joinable_bool or playercrew_status.spectating
flow.ongoing_runs . body.flow_buttons . join_spectators.visible = selected_joinable_bool
flow.ongoing_runs . body.flow_buttons . leave_spectators.visible = playercrew_status.spectating
flow.ongoing_runs . body.flow_buttons . join_crew.visible = selected_joinable_bool and ( not ( crewid and global_memory.crew_memories [ crewid ] and ( global_memory.crew_memories [ crewid ] . crewstatus == Crew.enum . LEAVING_INITIAL_DOCK or # global_memory.crew_memories [ crewid ] . crewplayerindices >= global_memory.crew_memories [ crewid ] . capacity or ( global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data and global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data [ player.index ] and game.tick < global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data [ player.index ] + Common.ban_from_rejoining_crew_ticks ) ) ) )
flow.ongoing_runs . body.wait_to_join . visible = selected_joinable_bool and crewid and global_memory.crew_memories [ crewid ] and ( global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data and global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data [ player.index ] and game.tick < global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data [ player.index ] + Common.ban_from_rejoining_crew_ticks ) and ( not ( global_memory.crew_memories [ crewid ] . crewstatus == Crew.enum . LEAVING_INITIAL_DOCK or # global_memory.crew_memories [ crewid ] . crewplayerindices >= global_memory.crew_memories [ crewid ] . capacity ) )
if flow.ongoing_runs . body.wait_to_join . visible then
2022-05-29 13:36:27 +02:00
flow.ongoing_runs . body.wait_to_join . caption = { ' pirates.gui_runs_wait_to_join ' , Math.ceil ( ( ( global_memory.crew_memories [ crewid ] . tempbanned_from_joining_data [ player.index ] - ( game.tick - Common.ban_from_rejoining_crew_ticks ) ) ) / 60 ) }
2021-10-13 10:21:53 +02:00
end
if not selected_joinable_bool then flow.ongoing_runs . body.ongoing_runs_listbox . selected_index = 0 end
flow.ongoing_runs . body.leaving_prompt . visible = playercrew_status.leaving
2022-10-10 19:21:14 +02:00
2023-02-02 21:44:44 +02:00
local show_protected_info = crewid and global_memory.crew_memories [ crewid ] . run_is_protected
flow.ongoing_runs . body.join_protected_crew_info . visible = show_protected_info
2022-10-10 19:21:14 +02:00
2023-02-02 21:44:44 +02:00
local show_private_info = crewid and global_memory.crew_memories [ crewid ] . run_is_private
flow.ongoing_runs . body.join_private_crew_info . visible = show_private_info
flow.ongoing_runs . body.password_namefield . visible = show_private_info
2021-10-13 10:21:53 +02:00
end
flow.proposals . visible = ( memory.crewstatus == nil and not playercrew_status.leaving )
if flow.proposals . visible then
if playercrew_status.proposing then
flow.proposals . body.proposals_listbox . selected_index = 0
flow.proposals . body.proposals_listbox . selected_index = 0
end
flow.proposals . body.proposals_listbox . visible = ( not playercrew_status.leaving ) and ( # global_memory.crewproposals > 0 )
2022-03-19 23:20:55 +02:00
2024-09-09 15:47:43 +02:00
flow.proposals . body.flow_buttons . abandon_proposal.visible = ( not playercrew_status.leaving ) and playercrew_status.created_crew and playercrew_status.proposing and ( # global_memory.crewproposals > 0 )
2021-10-13 10:21:53 +02:00
2024-09-09 15:47:43 +02:00
flow.proposals . body.proposal_maker . visible = ( not playercrew_status.leaving ) and ( not playercrew_status.created_crew )
2021-10-13 10:21:53 +02:00
flow.proposals . body.flow_proposal_launch . proposal_insufficient_sloops.visible = playercrew_status.sloops_full
flow.proposals . body.flow_proposal_launch . proposal_insufficient_player_capacity.visible = playercrew_status.needs_more_capacity
2022-03-11 00:11:51 +02:00
flow.proposals . body.flow_proposal_launch . proposal_crew_count_capped.visible = playercrew_status.crew_count_capped
2021-10-13 10:21:53 +02:00
-- flow.proposals.body.proposal_maker.body.proposal_cant_do_infinity_mode.visible = (flow.proposals.body.proposal_maker.body.options.mode.mode.switch.switch_state == 'right')
-- flow.proposals.body.proposal_maker.body.proposal_disabled_low_crew_caps.visible = false
2022-05-14 00:35:12 +02:00
flow.proposals . body.proposal_maker . body.proposal_disabled_low_crew_caps . visible = ( flow.proposals . body.proposal_maker . body.options . capacity.capacity . slider.slider_value < global_memory.minimumCapacitySliderValue )
2021-10-13 10:21:53 +02:00
flow.proposals . body.proposal_maker . body.propose_crew . visible = ( flow.proposals . body.proposal_maker . body.proposal_disabled_low_crew_caps . visible == false )
-- flow.proposals.body.proposal_maker.body.propose_crew.visible = (flow.proposals.body.proposal_maker.body.proposal_cant_do_infinity_mode.visible == false) and (flow.proposals.body.proposal_maker.body.proposal_disabled_low_crew_caps.visible == false)
2022-03-11 00:11:51 +02:00
flow.proposals . body.flow_proposal_launch . launch_crew.visible = playercrew_status.proposal_can_launch
2021-10-13 10:21:53 +02:00
2022-10-10 19:21:14 +02:00
local checkbox_state = flow.proposals . body.proposal_maker . body.private_checkbox . state
flow.proposals . body.proposal_maker . body.password_label . visible = checkbox_state
flow.proposals . body.proposal_maker . body.password_namefield . visible = checkbox_state
flow.proposals . body.proposal_maker . body.confirm_password_label . visible = checkbox_state
flow.proposals . body.proposal_maker . body.confirm_password_namefield . visible = checkbox_state
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
--*** UPDATE CONTENT ***--
if flow.ongoing_runs . visible then
local wrappedmemories = { }
for _ , mem in pairs ( global_memory.crew_memories ) do
local count = 0
if mem.crewstatus and mem.crewstatus == Crew.enum . LEAVING_INITIAL_DOCK then
count = Boats.players_on_boat_count ( mem.boat )
elseif mem.crewplayerindices then
count = # mem.crewplayerindices
end
2022-05-30 17:51:08 +02:00
wrappedmemories [ # wrappedmemories + 1 ] = { ' pirates.run_displayform ' , mem.id , { ' ' , mem.name .. ' , ' , CoreData.difficulty_options [ mem.difficulty_option ] . text , ' , [item=light-armor] ' .. count .. CoreData.capacity_options [ mem.capacity_option ] . text2 .. ' , [item=rail] ' .. ( mem.overworldx or 0 ) } }
2021-10-13 10:21:53 +02:00
-- wrappedmemories[#wrappedmemories + 1] = {'pirates.run_displayform', mem.id, mem.name, Utils.spritepath_to_richtext(CoreData.difficulty_options[mem.difficulty_option].icon), count, CoreData.capacity_options[mem.capacity_option].text2, ' [item=rail] ', mem.overworldx or 0}
end
GuiCommon.update_listbox ( flow.ongoing_runs . body.ongoing_runs_listbox , wrappedmemories )
2022-10-10 19:21:14 +02:00
local crewid = nil
local bool1 = ( not playercrew_status.leaving ) and ( not playercrew_status.adventuring ) and ( not playercrew_status.spectating ) and ( flow.ongoing_runs . body.ongoing_runs_listbox . selected_index ~= 0 )
if bool1 then
crewid = tonumber ( ( flow.ongoing_runs . body.ongoing_runs_listbox . get_item ( flow.ongoing_runs . body.ongoing_runs_listbox . selected_index ) ) [ 2 ] )
end
2023-02-02 21:44:44 +02:00
-- Update timer when captain protection expires
if crewid and flow.ongoing_runs . body.join_protected_crew_info . visible then
local lock_timer = global_memory.crew_memories [ crewid ] . protected_run_lock_timer
local sec = Math.floor ( ( lock_timer / ( 60 ) ) % 60 )
local min = Math.floor ( ( lock_timer / ( 60 * 60 ) ) % 60 )
local hrs = Math.floor ( ( lock_timer / ( 60 * 60 * 60 ) ) % 60 )
flow.ongoing_runs . body.join_protected_crew_info . caption = { ' pirates.gui_join_protected_run_info ' , hrs , min , sec }
end
2022-10-10 19:21:14 +02:00
-- Update timer when run will become public
2023-02-02 21:44:44 +02:00
if crewid and flow.ongoing_runs . body.join_private_crew_info . visible then
2022-10-10 19:21:14 +02:00
local lock_timer = global_memory.crew_memories [ crewid ] . private_run_lock_timer
local sec = Math.floor ( ( lock_timer / ( 60 ) ) % 60 )
local min = Math.floor ( ( lock_timer / ( 60 * 60 ) ) % 60 )
local hrs = Math.floor ( ( lock_timer / ( 60 * 60 * 60 ) ) % 60 )
2023-02-02 21:44:44 +02:00
flow.ongoing_runs . body.join_private_crew_info . caption = { ' pirates.gui_join_private_run_info ' , hrs , min , sec }
2022-10-10 19:21:14 +02:00
end
2021-10-13 10:21:53 +02:00
end
if flow.proposals . visible then
local wrappedproposals = { }
for _ , proposal in pairs ( global_memory.crewproposals ) do
wrappedproposals [ # wrappedproposals + 1 ] = { ' pirates.proposal_displayform ' , proposal.name , Utils.spritepath_to_richtext ( CoreData.capacity_options [ proposal.capacity_option ] . icon ) }
-- wrappedproposals[#wrappedproposals + 1] = {'pirates.proposal_displayform', proposal.name, Utils.spritepath_to_richtext(CoreData.difficulty_options[proposal.difficulty_option].icon), Utils.spritepath_to_richtext(CoreData.capacity_options[proposal.capacity_option].icon)}
end
GuiCommon.update_listbox ( flow.proposals . body.proposals_listbox , wrappedproposals )
end
-- update proposal maker
if flow.proposals . body.proposal_maker . visible then
local capacity_slider_value = flow.proposals . body.proposal_maker . body.options . capacity.capacity . slider.slider_value
for i , opt in pairs ( CoreData.capacity_options ) do
if capacity_slider_value == i then
flow.proposals . body.proposal_maker . body.options . capacity.capacity . readoff_text.caption = opt.text
flow.proposals . body.proposal_maker . body.options . capacity_readoff_icon.sprite = opt.icon
end
end
2022-05-29 13:36:27 +02:00
if flow.proposals . body.proposal_maker . body.options . capacity.capacity . readoff_text.caption == ' ∞ ' then flow.proposals . body.proposal_maker . body.options . capacity.capacity . readoff_text.caption = { ' pirates.gui_runs_proposal_maker_no_limit ' } end
2021-10-13 10:21:53 +02:00
-- local difficulty_slider_value = flow.proposals.body.proposal_maker.body.options.difficulty.difficulty.slider.slider_value
-- for i, opt in pairs(CoreData.difficulty_options) do
-- if difficulty_slider_value == i then
-- flow.proposals.body.proposal_maker.body.options.difficulty.difficulty.readoff_text.caption = opt.text
-- flow.proposals.body.proposal_maker.body.options.difficulty_readoff_icon.sprite = opt.icon
-- end
-- end
-- local mode_switch_state = flow.proposals.body.proposal_maker.body.options.mode.mode.switch.switch_state
-- for i, opt in pairs(CoreData.mode_options) do
-- if mode_switch_state == i then
-- flow.proposals.body.proposal_maker.body.options.mode.mode.readoff_text.caption = opt.text
-- flow.proposals.body.proposal_maker.body.options.mode_readoff_icon.sprite = opt.icon
-- end
-- end
end
end
function Public . click ( event )
2022-10-10 19:21:14 +02:00
if not event.element then return end
if not event.element . valid then return end
2021-10-13 10:21:53 +02:00
local player = game.players [ event.element . player_index ]
local eventname = event.element . name
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
if not player.gui . screen [ window_name .. ' _piratewindow ' ] then return end
2022-10-10 19:39:08 +02:00
local flow = player.gui . screen [ window_name .. ' _piratewindow ' ] . scroll_pane
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
local global_memory = Memory.get_global_memory ( )
2022-03-19 23:20:55 +02:00
-- local memory = Memory.get_crew_memory()
2021-10-13 10:21:53 +02:00
if eventname == ' join_spectators ' then
local listbox = flow.ongoing_runs . body.ongoing_runs_listbox
2023-06-26 20:51:55 +02:00
-- It was observed that "listbox.get_item(listbox.selected_index)" can produce "Index out of range error"
-- This is to prevent that error.
if listbox.selected_index >= 1 and listbox.selected_index <= # listbox.items then
Crew.join_spectators ( player , tonumber ( listbox.get_item ( listbox.selected_index ) [ 2 ] ) )
end
2021-10-13 10:21:53 +02:00
return
end
if eventname == ' leave_spectators ' then
Crew.leave_spectators ( player )
return
end
if eventname == ' join_crew ' then
local listbox = flow.ongoing_runs . body.ongoing_runs_listbox
2022-10-10 19:21:14 +02:00
2023-06-24 19:55:43 +02:00
-- It was observed that "listbox.get_item(listbox.selected_index)" can produce "Index out of range error"
-- This is to prevent that error.
if listbox.selected_index >= 1 and listbox.selected_index <= # listbox.items then
local crewid = tonumber ( listbox.get_item ( listbox.selected_index ) [ 2 ] )
2023-06-26 20:51:55 +02:00
Memory.set_working_id ( crewid )
local memory = Memory.get_crew_memory ( )
2023-06-24 19:55:43 +02:00
-- If run is private
2023-06-26 20:51:55 +02:00
if memory.run_is_private then
if memory.private_run_password == flow.ongoing_runs . body.password_namefield . text then
Crew.join_crew ( player )
2023-06-24 19:55:43 +02:00
flow.ongoing_runs . body.join_private_crew_info . visible = false
flow.ongoing_runs . body.password_namefield . visible = false
if memory.run_is_protected and ( not Roles.captain_exists ( ) ) then
Common.notify_player_expected ( player , { ' pirates.player_joins_protected_run_with_no_captain ' } )
Common.notify_player_expected ( player , { ' pirates.create_new_crew_tip ' } )
end
else
Common.notify_player_error ( player , { ' pirates.gui_join_private_run_error_wrong_password ' } )
end
else
2023-06-26 20:51:55 +02:00
Crew.join_crew ( player )
2023-02-02 21:44:44 +02:00
if memory.run_is_protected and ( not Roles.captain_exists ( ) ) then
2023-02-20 18:56:41 +02:00
Common.notify_player_expected ( player , { ' pirates.player_joins_protected_run_with_no_captain ' } )
Common.notify_player_expected ( player , { ' pirates.create_new_crew_tip ' } )
2023-02-02 21:44:44 +02:00
end
2022-10-10 19:21:14 +02:00
end
2023-02-02 21:44:44 +02:00
2023-06-24 19:55:43 +02:00
return
2022-10-10 19:21:14 +02:00
end
2021-10-13 10:21:53 +02:00
end
if eventname == ' propose_crew ' then
2024-09-09 13:20:30 +02:00
if # global_memory.crew_active_ids >= global_memory.active_crews_cap_memory then
2023-02-01 22:17:22 +02:00
Common.notify_player_error ( player , { ' pirates.gui_runs_launch_error_5 ' } )
return
end
2023-02-02 21:44:44 +02:00
-- Count private runs
local private_run_count = 0
for _ , id in pairs ( global_memory.crew_active_ids ) do
if global_memory.crew_memories [ id ] . run_is_private then
private_run_count = private_run_count + 1
2022-10-10 19:21:14 +02:00
end
2023-02-02 21:44:44 +02:00
end
2022-10-10 19:21:14 +02:00
2023-02-02 21:44:44 +02:00
-- Count protected but not private runs
local protected_but_not_private_run_count = 0
for _ , id in pairs ( global_memory.crew_active_ids ) do
if global_memory.crew_memories [ id ] . run_is_protected and ( not global_memory.crew_memories [ id ] . run_is_private ) then
protected_but_not_private_run_count = protected_but_not_private_run_count + 1
end
end
local run_is_protected = flow.proposals . body.proposal_maker . body.protected_checkbox . state
local run_is_private = flow.proposals . body.proposal_maker . body.private_checkbox . state
if run_is_private then
2022-10-10 19:21:14 +02:00
-- Make sure private run can be created
if private_run_count >= global_memory.private_run_cap then
Common.notify_player_error ( player , { ' pirates.gui_runs_proposal_maker_error_private_run_limit ' } )
return
end
-- Check if passwords match
if flow.proposals . body.proposal_maker . body.password_namefield . text ~= flow.proposals . body.proposal_maker . body.confirm_password_namefield . text then
Common.notify_player_error ( player , { ' pirates.gui_runs_proposal_maker_error_private_run_password_no_match ' } )
return
end
-- Check if passwords aren't empty
if flow.proposals . body.proposal_maker . body.password_namefield . text == ' ' then
Common.notify_player_error ( player , { ' pirates.gui_runs_proposal_maker_error_private_run_password_empty ' } )
return
end
2023-02-02 21:44:44 +02:00
elseif run_is_protected then
-- Make sure protected run can be created
2024-09-07 23:44:15 +02:00
if protected_but_not_private_run_count >= global_memory.protected_but_not_private_run_cap then
2023-02-02 21:44:44 +02:00
Common.notify_player_error ( player , { ' pirates.gui_runs_proposal_maker_error_protected_run_limit ' } )
return
end
2022-10-10 19:21:14 +02:00
end
local private_run_password = flow.proposals . body.proposal_maker . body.password_namefield . text
2021-10-13 10:21:53 +02:00
local proposal_name = flow.proposals . body.proposal_maker . body.namefield . text
-- local proposal_name = string.sub(flow.proposals.body.proposal_maker.body.namefield.text, 1, 30)
local capacity_option = flow.proposals . body.proposal_maker . body.options . capacity.capacity . slider.slider_value
2022-03-05 21:56:41 +02:00
local difficulty_option = 1
2021-10-13 10:21:53 +02:00
-- local difficulty_option = flow.proposals.body.proposal_maker.body.options.difficulty.difficulty.slider.slider_value
-- local mode_option = flow.proposals.body.proposal_maker.body.options.mode.mode.switch.switch_state
2022-06-03 10:53:14 +02:00
if ( not proposal_name ) or ( proposal_name == ' ' ) then proposal_name = ' NoName ' end
2021-10-13 10:21:53 +02:00
-- make name unique
-- local unique, changed = true, false
-- local check_unique = function(name)
-- unique = true
-- for _, proposal in pairs(global_memory.crewproposals) do
-- if name == proposal.name then
-- unique = false
-- changed = true
-- break
-- end
-- end
-- end
-- local i = 0
-- check_unique()
-- while i < 10 and not unique do
-- check_unique(proposal_name .. i)
-- i = i + 1
-- end
-- if not unique then return end
-- if changed then proposal_name = proposal_name .. i end
2022-03-19 23:20:55 +02:00
2021-10-13 10:21:53 +02:00
local unique = true
for _ , proposal in pairs ( global_memory.crewproposals ) do
if proposal_name == proposal.name then
unique = false
break
end
end
if not unique then return end
local proposal = {
name = proposal_name ,
difficulty_option = difficulty_option ,
capacity_option = capacity_option ,
-- mode_option = mode_option,
2024-09-09 15:47:43 +02:00
created_by_player = player.index ,
2023-02-02 21:44:44 +02:00
run_is_protected = run_is_protected ,
2022-10-10 19:21:14 +02:00
run_is_private = run_is_private ,
private_run_password = private_run_password ,
2021-10-13 10:21:53 +02:00
}
global_memory.crewproposals [ # global_memory.crewproposals + 1 ] = proposal
2022-10-10 19:21:14 +02:00
if run_is_private then
Common.notify_lobby ( { ' pirates.proposal_propose_private ' , player.name , proposal_name , CoreData.capacity_options [ capacity_option ] . text3 } )
else
Common.notify_lobby ( { ' pirates.proposal_propose ' , player.name , proposal_name , CoreData.capacity_options [ capacity_option ] . text3 } )
end
2022-05-29 13:36:27 +02:00
2021-10-13 10:21:53 +02:00
-- local message = player.name .. ' proposed the run ' .. proposal_name .. ' (difficulty ' .. CoreData.difficulty_options[difficulty_option].text .. ', capacity ' .. CoreData.capacity_options[capacity_option].text3 .. ').'
return
end
if eventname == ' abandon_proposal ' then
Crew.player_abandon_proposal ( player )
return
end
if eventname == ' launch_crew ' then
2022-03-19 23:20:55 +02:00
if GuiCommon.crew_overall_state_bools ( player.index ) . proposal_can_launch then --double check
2022-03-11 00:11:51 +02:00
for k , proposal in pairs ( global_memory.crewproposals ) do
2024-09-09 15:47:43 +02:00
if proposal.created_by_player == player.index then
2022-10-10 19:21:14 +02:00
-- Make sure private run can be created
if proposal.run_is_private then
-- NOTE: I didn't want to add this check in "proposal_can_launch", because different error message would get displayed (I think?).
local private_run_count = 0
for _ , id in pairs ( global_memory.crew_active_ids ) do
if global_memory.crew_memories [ id ] . run_is_private then
private_run_count = private_run_count + 1
end
end
if private_run_count >= global_memory.private_run_cap then
Common.notify_player_error ( player , { ' pirates.gui_runs_proposal_maker_error_private_run_limit ' } )
return
end
2023-02-02 21:44:44 +02:00
elseif proposal.run_is_protected then
local protected_but_not_private_run_count = 0
for _ , id in pairs ( global_memory.crew_active_ids ) do
if global_memory.crew_memories [ id ] . run_is_protected and ( not global_memory.crew_memories [ id ] . run_is_private ) then
protected_but_not_private_run_count = protected_but_not_private_run_count + 1
end
end
2024-09-07 23:44:15 +02:00
if protected_but_not_private_run_count >= global_memory.protected_but_not_private_run_cap then
2023-02-02 21:44:44 +02:00
Common.notify_player_error ( player , { ' pirates.gui_runs_proposal_maker_error_protected_run_limit ' } )
return
end
2022-10-10 19:21:14 +02:00
end
2021-10-13 10:21:53 +02:00
Crew.initialise_crew ( proposal )
global_memory.crewproposals [ k ] = nil
Progression.set_off_from_starting_dock ( )
return
end
end
end
end
end
return Public