*comment This displays a 1-liner stat bar
*label show_stats
*gosub calc_adj_stats

Power: [b]${adj_power}[/b] (${stat_power}) | Technique: [b]${adj_technique}[/b] (${stat_technique}) | Toughness: [b]${adj_toughness}[/b] (${stat_toughness}) | Mental: [b]${adj_mental}[/b] (${stat_mental}) | Energy: [b]${stat_energy}%[/b]
*return

*comment calculate the stats after adjustment for less than 100% energy
*label calc_adj_stats
*gosub clamp_energy
*temp energy_adjustment (1 - ((100 - stat_energy) * 0.005))
*set adj_power round(stat_power * energy_adjustment)
*set adj_technique round(stat_technique * energy_adjustment)
*set adj_toughness round(stat_toughness * energy_adjustment)
*set adj_mental round(stat_mental * energy_adjustment)
*return

*comment clamp energy values at 0-100
*label clamp_energy
*if (stat_energy > 100)
   *set stat_energy 100
*if (stat_energy < 0)
   *set stat_energy 0
*return

*label increase_analysis
*set trait_analysis +1
*gosub update_highest_trait
*return

*label increase_composure
*set trait_composure +1
*gosub update_highest_trait
*return

*label increase_empathy
*set trait_empathy +1
*gosub update_highest_trait
*return

*label update_highest_trait
*temp highest_value 0
*if (trait_analysis > highest_value)
    *set highest_value trait_analysis
*if (trait_composure > highest_value)
    *set highest_value trait_composure
*if (trait_empathy > highest_value)
    *set highest_value trait_empathy
*if (trait_analysis = highest_value)
    *set highest_trait "analysis"
*elseif (trait_composure = highest_value)
    *set highest_trait "composure"
*else
    *set highest_trait "empathy"
*set highest_trait_num highest_value
*return

*label update_highest_friendship
*temp highest_value 0
*if (eric_friendship >= highest_value)
    *set highest_value eric_friendship
*if (katie_friendship >= highest_value)
    *set highest_value katie_friendship
*if (danny_friendship >= highest_value)
    *set highest_value danny_friendship
*if (chloe_friendship >= highest_value)
    *set highest_value chloe_friendship
*if (eric_friendship = highest_value)
    *set highest_friendship "Eric"
*if (katie_friendship = highest_value)
    *set highest_friendship "Katie"
*if (danny_friendship = highest_value)
    *set highest_friendship "Danny"
*if (chloe_friendship = highest_value)
    *set highest_friendship "Chloe"
*set highest_friendship_num highest_value
*return



*comment sets trait_bonus_applied if the highest trait is the same as the param
*label calculate_trait_bonus
*params trait_type
*if (trait_type = "analysis")
    *if (highest_trait = "analysis")
        *set trait_bonus_applied trait_bonus_amount
    *else
        *set trait_bonus_applied 0
*elseif (trait_type = "composure")
    *if (highest_trait = "composure")
        *set trait_bonus_applied trait_bonus_amount
    *else
        *set trait_bonus_applied 0
*elseif (trait_type = "empathy")
    *if (highest_trait = "empathy")
        *set trait_bonus_applied trait_bonus_amount
    *else
        *set trait_bonus_applied 0
*else
    *bug Incorrect trait parameter passed
*return

*label tick_character_cooldowns

*set eric_plot_timer -1
*if (eric_plot_timer < 0)
    *set eric_plot_timer 0
*set katie_plot_timer -1
*if (katie_plot_timer < 0)
    *set katie_plot_timer 0
*set danny_plot_timer -1
*if (danny_plot_timer < 0)
    *set danny_plot_timer 0
*set chloe_plot_timer -1
*if (chloe_plot_timer < 0)
    *set chloe_plot_timer 0
*return

*label fortnight_choice
*if (stat_energy < 30)

    You've been training too hard, and you're in no condition to fight. You'll need to rest and recover.

*elseif (stat_energy < 60)

    You've been training hard. Rest and recovery would be helpful.

*choice
    #Train power with heavy bag work.
        *gosub_scene training_montages power
        *set stat_power %+8
        *set stat_energy -20
        *gosub_scene utils clamp_energy
    #Refine boxing technique with precision drills.
        *gosub_scene training_montages technique
        *set stat_technique %+8
        *set stat_energy -20
        *gosub_scene utils clamp_energy
    #Build stamina with running and conditioning.
        *gosub_scene training_montages toughness
        *set stat_toughness %+8
        *set stat_energy -20
        *gosub_scene utils clamp_energy
    #Practise visualisation and fight strategy.
        *gosub_scene training_montages mental
        *set stat_mental %+8
        *set stat_energy -20
        *gosub_scene utils clamp_energy
    *if (unlocked_eric_training) #Practise energy meditation, refining technique and focus.
        *gosub_scene training_montages eric
        *set stat_technique %+4
        *set stat_mental %+2
        *set stat_energy -10
        *gosub_scene utils clamp_energy
    *if (unlocked_katie_training) #Train pressure point resistance, building toughness and focus.
        *gosub_scene training_montages katie
        *set stat_toughness %+4
        *set stat_mental %+2
        *set stat_energy -10
        *gosub_scene utils clamp_energy
    *if (unlocked_danny_training) #Refine power and develop awareness through daily activity.
        *gosub_scene training_montages danny
        *set stat_power %+4
        *set stat_mental %+2
        *set stat_energy -10
        *gosub_scene utils clamp_energy
    *if (unlocked_chloe_training) #Practise reading opponents and reacting to unpredictability.
        *gosub_scene training_montages chloe
        *set stat_technique %+3
        *set stat_mental %+3
        *set stat_energy -10
        *gosub_scene utils clamp_energy        
    *if (stat_energy >= 90) #Rest and recover (not needed).
        *gosub_scene training_montages rest
        *set stat_energy +30    
        *gosub_scene utils clamp_energy
    *if ((stat_energy >= 60) and (stat_energy < 90)) #Rest and recover.
        *gosub_scene training_montages rest
        *set stat_energy +30    
        *gosub_scene utils clamp_energy
    *if ((stat_energy >= 30) and (stat_energy < 60)) #Rest and recover (recommended).
        *gosub_scene training_montages rest
        *set stat_energy +30    
        *gosub_scene utils clamp_energy
    *if (stat_energy < 30) #Rest and recover [b](highly recommended)[/b].
        *gosub_scene training_montages rest
        *set stat_energy +30    
        *gosub_scene utils clamp_energy
*page_break
*return

*label character_visit_options
*comment reduce the character cooldowns first. This is the first subroutine visited by the chapters
*if (eric_subplot_status = "available")
    *gosub_scene eric_subplot scene_introduction

*if (katie_subplot_status = "available")
    *gosub_scene katie_subplot scene_introduction

*if (danny_subplot_status = "available")
    *gosub_scene danny_subplot scene_introduction

*if (chloe_subplot_status = "available")
    *gosub_scene chloe_subplot scene_introduction
*return

*label character_visit_choice
*if (eric_plot_timer = 0)
    *set eric_unvisited_timer +1
*if (katie_plot_timer = 0)
    *set katie_unvisited_timer +1
*if (danny_plot_timer = 0)
    *set danny_unvisited_timer +1
*if (chloe_plot_timer = 0)
    *set chloe_unvisited_timer +1
*temp any_character_available false
*temp visit_eric_available false
*temp visit_katie_available false
*temp search_danny_available false
*temp visit_danny_available false
*temp visit_chloe_available false
*if ((eric_subplot_status = "available") and (eric_plot_timer = 0))
    *set visit_eric_available true
    *set any_character_available true
*if ((katie_subplot_status = "available") and (katie_plot_timer = 0))
    *set visit_katie_available true
    *set any_character_available true
*if ((danny_subplot_status = "available") and ((danny_plot_timer = 0) and (danny_subplot_scene = 1)))
    *set search_danny_available true
    *set any_character_available true
*if (current_month = end_month)
    *if ((danny_subplot_status = "available") and ((danny_plot_timer = 0) and not(danny_subplot_scene = 1)))
        *set visit_danny_available true
        *set any_character_available true
*elseif ((not(danny_subplot_scene = 1)) and (not(danny_subplot_scene = 5)))
    *if ((danny_subplot_status = "available") and (danny_plot_timer = 0))
        *set visit_danny_available true
        *set any_character_available true
*if ((chloe_subplot_status = "available") and (chloe_plot_timer = 0))
    *set visit_chloe_available true
    *set any_character_available true
*if (not(any_character_available))
    No one is available this fortnight.

    *page_break
    *return
*label character_visit_choice_option
*choice
    *if (visit_eric_available) #Visit Eric.
        *gosub_scene eric_subplot scene_choice
        *set eric_subplot_scene + 1
        *set eric_unvisited_timer 0
        *set eric_almost_lapsed false

        *if (eric_subplot_scene >= 7)
            *page_break End of Eric's story.
        *else
            *page_break End scene.
    *if (visit_katie_available) #Visit Katie.
        *gosub_scene katie_subplot scene_choice
        *set katie_subplot_scene + 1
        *set katie_unvisited_timer 0
        *set katie_almost_lapsed false

        *if (katie_subplot_scene >= 7)
            *page_break End of Katie's story.
        *else
            *page_break End scene.
    *if (search_danny_available) #Search for Danny.
        *gosub_scene danny_subplot scene_choice
        *set danny_subplot_scene +1
        *set danny_unvisited_timer 0
        *set danny_almost_lapsed false

        *page_break End scene.
    *if (visit_danny_available) #Visit Danny.
        *gosub_scene danny_subplot scene_choice
        *set danny_subplot_scene +1
        *set danny_unvisited_timer 0
        *set danny_almost_lapsed false

        *if (danny_subplot_scene >= 6)
            *page_break End of Danny's story.
        *else
            *page_break End scene.
    *if (visit_chloe_available) #Visit Chloe.
        *gosub_scene chloe_subplot scene_choice
        *set chloe_subplot_scene + 1
        *set chloe_unvisited_timer 0
        *set chloe_almost_lapsed false

        *if (chloe_subplot_scene >= 7)
            *page_break End of Chloe's story.
        *else
            *page_break End scene.
    #Don't visit anyone.
        There are still people you could see. Opportunities may not stay open forever.

        *choice
            #Keep to yourself this fortnight.
                You decide to keep to yourself this fortnight.

                *page_break
            #Reconsider.
                *goto character_visit_choice_option
*return

*label character_unvisited_check
*comment if you don't visit a character for too long, they are no longer available for visiting
*comment They will wait forever if you are their partner though
*comment Danny is more patient, Chloe is less patient
*comment this runs before character_visit_choice
*if ((eric_subplot_status = "available") and (not(eric_partner)))
    *if (eric_subplot_scene = 1) and (eric_unvisited_timer = 1)
        *set eric_almost_lapsed true
    *elseif (eric_subplot_scene = 1) and (eric_unvisited_timer >= 2)
        *set eric_subplot_status "lapsed"
        *set eric_almost_lapsed false
    *elseif (eric_subplot_scene > 1) and (eric_unvisited_timer = 2)
        *set eric_almost_lapsed true
    *elseif (eric_subplot_scene > 1) and (eric_unvisited_timer >= 3)
        *set eric_subplot_status "lapsed"
        *set eric_almost_lapsed false
*if ((katie_subplot_status = "available") and (not(katie_partner)))
    *if (katie_subplot_scene = 1) and (katie_unvisited_timer = 1)
        *set katie_almost_lapsed true
    *elseif (katie_subplot_scene = 1) and (katie_unvisited_timer >= 2)
        *set katie_subplot_status "lapsed"
        *set katie_almost_lapsed false
    *elseif (katie_subplot_scene > 1) and (katie_unvisited_timer = 2)
        *set katie_almost_lapsed true
    *elseif (katie_subplot_scene > 1) and (katie_unvisited_timer >= 3)
        *set katie_subplot_status "lapsed"
        *set katie_almost_lapsed false
*if (danny_subplot_status = "available")
    *if (danny_subplot_scene = 1) and (danny_unvisited_timer = 2)
        *set danny_almost_lapsed true
    *elseif (danny_subplot_scene = 1) and (danny_unvisited_timer >= 3)
        *set danny_subplot_status "lapsed"
        *set danny_almost_lapsed false
    *elseif (danny_subplot_scene = 5)
        *set danny_almost_lapsed false
    *elseif (danny_subplot_scene > 1) and (danny_unvisited_timer = 3)
        *set danny_almost_lapsed true
    *elseif (danny_subplot_scene > 1) and (danny_unvisited_timer >= 4)
        *set danny_subplot_status "lapsed"
        *set danny_almost_lapsed false
*if ((chloe_subplot_status = "available") and (not(chloe_partner)))
    *if (chloe_subplot_scene = 1) and (chloe_unvisited_timer = 1)
        *set chloe_almost_lapsed true
    *elseif (chloe_subplot_scene = 1) and (chloe_unvisited_timer >= 2)
        *set chloe_subplot_status "lapsed"
        *set chloe_almost_lapsed false
    *elseif (chloe_subplot_scene > 1) and (chloe_unvisited_timer = 1)
        *set chloe_almost_lapsed true
    *elseif (chloe_subplot_scene > 1) and (chloe_unvisited_timer >= 2)
        *set chloe_subplot_status "lapsed"
        *set chloe_almost_lapsed false
*return

*label show_fight_momentum
*if (fight_momentum <= 0)
    [i]You're one step from defeat.[/i]
*elseif (fight_momentum = 1)
    [i]You're barely holding on.[/i]
*elseif (fight_momentum = 2)
    [i]${fight_opponent_name}'s pressuring you hard.[/i]
*elseif (fight_momentum = 3)
    [i]${fight_opponent_name} has the edge.[/i]
*elseif (fight_momentum = 4)
    [i]${fight_opponent_name} has a slight edge.[/i]
*elseif (fight_momentum = 5)
    [i]The fight is even.[/i]
*elseif (fight_momentum = 6)
    [i]You have a slight edge.[/i]
*elseif (fight_momentum = 7)
    [i]You have the edge.[/i]
*elseif (fight_momentum = 8)
    [i]You have control of the fight.[/i]
*elseif (fight_momentum = 9)
    [i]${fight_opponent_name} is barely holding on.[/i]
*elseif (fight_momentum >= 10)
    [i]${fight_opponent_name} is one step from defeat.[/i]
*else
    *bug Invalid fight_momentum
*return

*comment display the fighting style in choicescript_stats based on the stat distribution
*label display_fighting_style
*comment display texts
*temp power_text "Your fighting style resembles a slugger: landing decisive blows through raw power."
*temp technique_text "Your fighting style resembles an out-fighter: controlling distance, setting the pace of the fight."
*temp toughness_text "Your fighting style resembles a pressure fighter: absorbing hits to force mistakes."
*temp mental_text "Your fighting style resembles a tactician: identifying and capitalising on predictable patterns."
*temp power_technique_text "Your fighting style resembles a boxer-puncher: clean combinations that come with stopping power."
*temp power_toughness_text "Your fighting style resembles a brawler: shrugging off damage to force heavy exchanges."
*temp power_mental_text "Your fighting style resembles a power counterpuncher: baiting attacks and punishing overextensions."
*temp technique_toughness_text "Your fighting style resembles an inside fighter: staying close, working efficiently under pressure."
*temp technique_mental_text "Your fighting style resembles a stylist: foiling the opponent with feints, angles, and timing."
*temp toughness_mental_text "Your fighting style resembles a defensive specialist: reducing damage through anticipation and resilience."
*temp all_rounded_text "Your fighting style is well-rounded: adaptable, with no dominant style."
*temp total_stats 0
*temp pct_power 0
*temp pct_technique 0
*temp pct_toughness 0
*temp pct_mental 0
*temp highest_stat ""
*temp second_highest_stat ""
*temp third_highest_stat ""
*temp second_highest_stat_near false
*temp third_highest_stat_near false
*set total_stats (stat_power + stat_technique) + (stat_toughness + stat_mental)
*set pct_power (stat_power * 100) / total_stats
*set pct_technique (stat_technique * 100) / total_stats
*set pct_toughness (stat_toughness * 100) / total_stats
*set pct_mental (stat_mental * 100) / total_stats
*temp a pct_power
*temp b pct_technique
*temp c pct_toughness
*temp d pct_mental
*temp highest_pct 0
*if (a > highest_pct)
    *set highest_pct a
    *set highest_stat "power"
*if (b > highest_pct)
    *set highest_pct b
    *set highest_stat "technique"
*if (c > highest_pct)
    *set highest_pct c
    *set highest_stat "toughness"
*if (d > highest_pct)
    *set highest_pct d
    *set highest_stat "mental"
*comment remove highest
*if highest_stat = "power"
    *set a 0
*elseif highest_stat = "technique"
    *set b 0
*elseif highest_stat = "toughness"
    *set c 0
*elseif highest_stat = "mental"
    *set d 0
*temp second_highest_pct 0
*if (a > second_highest_pct)
    *set second_highest_pct a
    *set second_highest_stat "power"
*if (b > second_highest_pct)
    *set second_highest_pct b
    *set second_highest_stat "technique"
*if (c > second_highest_pct)
    *set second_highest_pct c
    *set second_highest_stat "toughness"
*if (d > second_highest_pct)
    *set second_highest_pct d
    *set second_highest_stat "mental"
*comment remove second highest
*if second_highest_stat = "power"
    *set a 0
*elseif second_highest_stat = "technique"
    *set b 0
*elseif second_highest_stat = "toughness"
    *set c 0
*elseif second_highest_stat = "mental"
    *set d 0
*temp third_highest_pct 0
*if (a > third_highest_pct)
    *set third_highest_pct a
    *set third_highest_stat "power"
*if (b > third_highest_pct)
    *set third_highest_pct b
    *set third_highest_stat "technique"
*if (c > third_highest_pct)
    *set third_highest_pct c
    *set third_highest_stat "toughness"
*if (d > third_highest_pct)
    *set third_highest_pct d
    *set third_highest_stat "mental"
*comment closeness within 5%
*if (highest_pct - second_highest_pct) < 5
    *set second_highest_stat_near true
*if (second_highest_pct - third_highest_pct) < 5
    *set third_highest_stat_near true
*if (second_highest_stat_near)
    *if (third_highest_stat_near)
        ${all_rounded_text}
    *elseif (highest_stat = "power")
        *if (second_highest_stat = "technique")
            ${power_technique_text}
        *elseif (second_highest_stat = "toughness")
            ${power_toughness_text}
        *elseif (second_highest_stat = "mental")
            ${power_mental_text}
        *else
            *bug invalid second_highest_stat
    *elseif (highest_stat = "technique")
        *if (second_highest_stat = "power")
            ${power_technique_text}
        *elseif (second_highest_stat = "toughness")
            ${technique_toughness_text}
        *elseif (second_highest_stat = "mental")
            ${technique_mental_text}
        *else
            *bug invalid second_highest_stat
    *elseif (highest_stat = "toughness")
        *if (second_highest_stat = "power")
            ${power_toughness_text}
        *elseif (second_highest_stat = "technique")
            ${technique_toughness_text}
        *elseif (second_highest_stat = "mental")
            ${toughness_mental_text}
        *else
            *bug invalid second_highest_stat
    *elseif (highest_stat = "mental")
        *if (second_highest_stat = "power")
            ${power_mental_text}
        *elseif (second_highest_stat = "technique")
            ${technique_mental_text}
        *elseif (second_highest_stat = "toughness")
            ${toughness_mental_text}
        *else
            *bug invalid second_highest_stat
    *else
        *bug invalid highest_stat
*else
    *if (highest_stat = "power")
        ${power_text}
    *elseif (highest_stat = "technique")
        ${technique_text}
    *elseif (highest_stat = "toughness")
        ${toughness_text}
    *elseif (highest_stat = "mental")
        ${mental_text}
    *else
        *bug invalid highest_stat
*return

*label check_and_show_achievements
*check_achievements
*if (eric_subplot_scene = 7)
    *if (not(choice_achieved_eric))
        *achieve eric
    *if (eric_harmony > 50)
        *if (not(choice_achieved_eric_harmony))
            *achieve eric_harmony
    *elseif (not(choice_achieved_eric_ambition))
        *achieve eric_ambition
*if (katie_subplot_scene = 7)
    *if (not(choice_achieved_katie))
        *achieve katie
    *if (katie_masking > 50)
        *if (not(choice_achieved_katie_masking))
            *achieve katie_masking
    *elseif (not(choice_achieved_katie_authenticity))
        *achieve katie_authenticity
*if (danny_subplot_scene = 6)
    *if (not(choice_achieved_danny))
        *achieve danny
    *if (not(choice_achieved_danny_refusal))
        *achieve danny_refusal
*if (chloe_subplot_scene = 7)
    *if (not(choice_achieved_chloe))
        *achieve chloe
    *if (chloe_acceptance > 50)
        *if (not(choice_achieved_chloe_acceptance))
            *achieve chloe_acceptance
    *elseif (not(choice_achieved_chloe_protection))
        *achieve chloe_protection
*return

*label first_loss_notification
*if ((record_losses > 0) and (not(first_loss_notification_shown)))
    *set first_loss_notification_shown true

    [i]Defeat does not end the game. Losses are valid outcomes, and the story continues.[/i]
*return