Free Code for Combat Systems in ChoiceScript

Code for basic combat. Feel free to use it in your story or modify it.

Copy this into your startup.txt

*comment attribute
*create strength 20
*create agility 20

*comment skills
*create combat 20
*create weapons 20
*create stealth 20

*comment status
*create health 100

*comment booleans
*create has_dagger false
*create has_bow false
*create has_poison false

*comment fight
*create opp_awareness 0
*create weapon ""
*create armor ""
*create weapon_range 0
*create weapon_atk 0
*create armor_def 0
*create weapon_speed 0
*create armor_weight 0

*create opp_health 0
*create opp_strength 0
*create opp_agility 0
*create opp_strength 0
*create opp_alerted false
*create opp_name ""
*create opp_dist 0

Make a file called combat_util.txt in the same folder as your startup.txt, then copy this bit in to combat_util.txt

*label check_health
*if health <= 0
  *set health 0
  Your health is depleted, you died.
  *goto_scene ending
*return



*label switchweapon
*if (weapon="dagger")
  *set weapon_range 5
  *set weapon_atk 5
*if (weapon="bow")
  *set weapon_range 50
  *set weapon_atk 4
*if (weapon="longsword")
  *set weapon_range 10
  *set weapon_atk 7
*if (weapon="unequipped")
  *set weapon_range 5
  *set weapon_atk 0
*return



*label fight
*temp won false
*temp escaped false
*temp attack ((strength/10)+weapon_atk)
*temp def (strength/10)+armor_def
*temp opp_health_max opp_health
*temp opp_health_percent 0
*temp player_actions agility
*temp opp_actions opp_agility
*if (player_actions>opp_actions)
  *set player_actions  (player_actions/opp_actions)
  *set opp_actions 1
*if (opp_actions>player_actions)
  *set opp_actions (opp_actions/player_actions)
  *set player_actions 1
*set player_actions (player_actions*10)
*set opp_actions (opp_actions*10)
*temp bow_stage 0
*temp weapon_poisoned false
*temp facing_opp true
*temp fight_msg "You have encountered an enemy"
*temp player_action_point player_actions
*temp opp_action_point opp_actions
*gosub display_fight_stats
*page_break
*goto turn
*return

*label turn
*set player_action_point player_actions
*set opp_action_point opp_actions
*gosub opp_turn
*gosub player_turn
*if (won)
  *page_break
  You are victorious.
  *return
*if (escaped)
  *page_break
  You have escaped.
  *return
*goto turn
*return



*label display_fight_stats
*set opp_health_percent (opp_health/opp_health_max)*100
${fight_msg}

You are fighting ${opp_name}

Distance: ${opp_dist}

Enemy Attack: ${opp_strength}

Enemy Action: ${opp_action_point}

Enemy Health:
*stat_chart
  percent opp_health_percent Health

Your Attack: ${attack}

Your Actions: ${player_action_point}

Your Health:
*stat_chart
  percent health Health
*return



*label opp_turn
*page_break
*if (opp_action_point=0)
  *set fight_msg "Your turn"
  *return
*if (opp_alerted = false)
  *set fight_msg "The enemy doesn't see you"
  *return
*if (opp_action_point<10)
  opp_action_point = 0
*if (opp_dist<=5)
  *set opp_action_point - 10
  *set health - opp_strength
  *gosub check_health
  *set fight_msg "Enemy attacks you bare handed"
*if (opp_dist>5)
  *set opp_action_point - 10
  *if (opp_dist<opp_agility)
    *set opp_dist 5
  *if (opp_dist>opp_agility)
    *set opp_dist - opp_agility
  *set fight_msg "Enemy approaches you"
*gosub display_fight_stats
*goto opp_turn
*return



*label player_turn
*page_break
*if (player_action_point<=0)
  *gosub display_fight_stats
  *return
*if (opp_health<1)
  *set won true
  *return
*temp attack ((strength/10)+(weapon_atk*(1+(weapons/100))))
*gosub display_fight_stats
*temp can_poison_weapon
*fake_choice
  #Maneuvers
    *set fight_msg "Choose a manuver"
    *set player_action_point - 10
    *gosub display_fight_stats
    *choice
      #Walk
        *set fight_msg "You walked forward"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness+10))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>(agility*0.5))
            *set opp_dist - (agility*0.5)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + (agility*0.5)
        *goto player_turn
      #Backstep
        *set fight_msg "You take a few steps back"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness+5))
          *set opp_alerted true
        *if (facing_opp)
          *set opp_dist + (agility*0.33)
          *goto player_turn
        *elseif (opp_dist>(agility*0.33))
          *set opp_dist - (agility*0.33)
          *goto player_turn
        *else
          *set opp_dist 0
        *goto player_turn
      #Turn around
        *set fight_msg "You make a quick turn"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness+5))
          *set opp_alerted true
        *if (facing_opp)
          *set facing_opp false
          *goto player_turn
        *else
          *set facing_opp true
        *goto player_turn
      #Run
        *set fight_msg "You run as fast as you could"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness+20))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>(agility*0.7))
            *set opp_dist - (agility*0.7)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + (agility*0.7)
        *goto player_turn
      *selectable_if (agility>=20) #Sprint
        *set fight_msg "You sprinted at your top speed, making clacking cacophonies with your footwear"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness+40))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>agility)
            *set opp_dist - agility
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + agility
        *goto player_turn
      *selectable_if (stealth>=20) #Leopard crawl
        *set fight_msg "Like a militia you went down on your front and crawled awkwardly"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>(agility*0.33))
            *set opp_dist - (agility*0.33)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + (agility*0.33)
        *goto player_turn
      *selectable_if (agility>=80) #Dash
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness + 30))
          *set opp_alerted true
        *if (facing_opp)
          *set fight_msg "In the blink of an eye you dashed towards your opponent, leaving a trail of blurry shadows behind you"
          *if (opp_dist>(agility * 1.5))
            *set opp_dist - (agility * 1.5)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set fight_msg "In the blink of an eye you dashed away from your opponent, leaving a trail of blurry shadows behind you"
          *set opp_dist + (agility * 1.5)
        *goto player_turn
      *selectable_if (stealth>=40) #Cat walk
        *set fight_msg "Footsteps as light as a cat, you slipped across"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>(agility*0.5))
            *set opp_dist - (agility*0.5)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + (agility*0.5)
        *goto player_turn
      *selectable_if (stealth>=50) #Efficient silent strides
        *set fight_msg "You silently but swiftly strode"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>(agility/1.5))
            *set opp_dist - (agility/1.5)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + (agility/1.5)
        *goto player_turn
      *selectable_if ((agility>=20) and (stealth>=60)) #Silent run
        *set fight_msg "You run stealthily, without losing any pace"
        *if (((1 + (stealth / 100)) *opp_dist) < (opp_awareness))
          *set opp_alerted true
        *if (facing_opp)
          *if (opp_dist>(agility*0.7))
            *set opp_dist - (agility*0.7)
            *goto player_turn
          *else
            *set opp_dist 0
            *goto player_turn
        *else
          *set opp_dist + (agility*0.7)
        *goto player_turn
      *selectable_if ((agility>=60) and (stealth>=60)) #Evade
        *set fight_msg "The opponent is unable to track your position through sight alone, to them, you have faded from existence"
        *set opp_alerted false
        *if (facing_opp)
          *set opp_dist + (agility*0.33)
          *goto player_turn
        *elseif (opp_dist>(agility*0.33))
          *set opp_dist - (agility*0.33)
          *goto player_turn
        *else
          *set opp_dist 0
        *goto player_turn
      *selectable_if ((agility>=80) and (stealth>=80)) #Blink
        *set opp_alerted false
        *if (facing_opp)
          *set fight_msg "With this overpowered maneuver, you can instantly arrive at the opponent's position without them even realising"
          *set opp_dist 0
          *goto player_turn
        *else
          *set fight_msg "The prerequisite for flight is blinking away without your opponent noticing, but it is also excellent for ranged retaliation"
          *set opp_dist + (agility*2)
        *goto player_turn
      #Go back
        *set player_action_point +10
        *goto player_turn
  *selectable_if (opp_dist<=5) #Unarmed combat
    *set opp_alerted true
    *set fight_msg "Choose an unarmed melee move"
    *gosub display_fight_stats
    *choice
      #Throw in a jab to the chest
        *set opp_health - (attack-weapon_atk)
        *set player_action_point - 5
        *set fight_msg "You punched your opponent in the chest"
        *goto player_turn
      *selectable_if (combat>=20) #Throw a hook to the face
        *set opp_health - ((attack-weapon_atk)+(combat/10))
        *set player_action_point - 5
        *set fight_msg "You punched your opponent in the face"
        *goto player_turn
      *selectable_if (combat>=40) #Turning side kick to the head
        *set opp_health - ((attack-weapon_atk)+(combat/7))
        *set player_action_point - 10
        *set fight_msg "You kicked your opponent in the face"
        *goto player_turn
      *selectable_if (combat>=50) #Roundhouse kick to the neck
        *set opp_health - ((attack-weapon_atk)+(combat/5))
        *set player_action_point - 15
        *set fight_msg "You roundhouse kicked your opponent in the back of their neck"
        *goto player_turn
      *selectable_if (combat>=60) #Flying reverse roundhouse kick
        *set opp_health - (((attack-weapon_atk)+10)+(combat/5))
        *set player_action_point - 15
        *set fight_msg "You roundhouse kicked your opponent in the face"
        *goto player_turn
      *selectable_if (combat>=70) #Quick combo: jab, hook, side kick
        *set opp_health - ((attack-weapon_atk)+2)
        *set opp_health - ((attack-weapon_atk)+(combat/10))
        *set opp_health - ((attack-weapon_atk)+(combat/7))
        *set player_action_point - 15
        *set fight_msg "You performed a quick set of combos"
        *goto player_turn
      *selectable_if (combat>=80) #Forearm headlock, then repetitive knee strikes to the head 
        *temp luck 0
        *rand luck 0 8
        *set opp_health - (((attack-weapon_atk)+(combat/10))*luck)
        *set player_action_point - 15
        *set fight_msg "You kneed the opponent"
        *goto player_turn
      *selectable_if (combat>=90) #Master combo: reverse wristlock, downward wrist breaker, elbow head strike, face jab, double left right side kick, flying reverse roundhouse kick, headscissors
        *set opp_health - ((attack-weapon_atk)+(combat/1.5))
        *set player_action_point - 30
  #Change weapon
    *set fight_msg "Choose your main equipement"
    *gosub display_fight_stats
    *choice
      #Unequip
        *set weapon "unequipped"
        *set player_action_point - 5
        *gosub switchweapon
        *set fight_msg "You equipped fist"
        *goto player_turn
      *if (has_dagger) #Dagger
        *set opp_alerted true
        *set weapon "dagger"
        *set player_action_point - 5
        *gosub switchweapon
        *set fight_msg "You equipped dagger"
        *goto player_turn
      *if (has_bow) #Bow
        *set weapon "bow"
        *set player_action_point - 5
        *gosub switchweapon
        *set fight_msg "You equipped bow"
        *goto player_turn
      #Go back
        *goto player_turn
  *if (weapon="dagger")
    *selectable_if ((opp_dist<=weapon_range) and (player_action_point>=5)) #Use dagger
      *set fight_msg "How would you like to use your dagger?"
      *gosub display_fight_stats
      *choice
        #Stab whereever
          *set opp_alerted true
          *if (weapon_poisoned)
            *set attack + 10
          *set opp_health - (attack+(weapons/5))
          *set player_action_point - 5
          *set fight_msg "You stabbed the opponent in the arm"
          *goto player_turn
        *if (weapons >= 20)
          #Lateral cut
            *set opp_alerted true
            *if (weapon_poisoned)
              *set attack + 10
            *set opp_health - ((attack+(weapons/5))*1.5)
            *set player_action_point - 5
            *set fight_msg "You cut the opponent's chest, they're bleeding badly"
            *goto player_turn
        *if (weapons >= 50)
          #Neck stab
            *set opp_alerted true
            *if (weapon_poisoned)
              *set attack + 10
            *set opp_health - ((attack+(weapons/5))*2)
            *set player_action_point - 5
            *set fight_msg "You stabbed the opponent's neck, what a savage move"
            *goto player_turn
        *if (weapons >= 80)
          #Slice and dice
            *set opp_alerted true
            *if (weapon_poisoned)
              *set attack + 10
            *set opp_health - ((attack+(weapons/5))*3)
            *set player_action_point - 5
            *set fight_msg "After a sequence of flashes in the air, your opponent gained numerous wounds"
            *goto player_turn
        #Go back
          *set fight_msg "You didn't know what you wanted to do with your dagger"
          *goto player_turn
  *if ((weapon="bow") and (bow_stage=0))
    *selectable_if (player_action_point>=10) #Load bow
      *set bow_stage 1
      *set player_action_point - 10
      *set fight_msg "You loaded an arrow"
      *goto player_turn
  *if ((weapon="bow") and (bow_stage=1))
    *selectable_if (player_action_point>=10) #Draw bow
      *set bow_stage 2
      *set player_action_point - 10
      *set fight_msg "You aim and draw"
      *goto player_turn
  *if ((weapon="bow") and (bow_stage=2))
    *selectable_if ((opp_dist<=weapon_range) and (player_action_point>=10)) #Fire arrow
      *set opp_alerted true
      *set bow_stage 0
      *if (weapon_poisoned)
        *set attack + 10
        *set weapon_poisoned false
      *set opp_health - attack
      *set player_action_point - 10
      *set weapon_poisoned false
      *set fight_msg "You fired an arrow"
      *goto player_turn
  *if (has_poison)
    *selectable_if ((weapon != "unequipped") and (player_action_point>=10)) #Poison weapon
      *set weapon_poisoned true
      *set fight_msg "You poisoned your weapon"
      *set player_action_point - 10
      *goto player_turn
  #Do nothing
    *set player_action_point 0
    *set fight_msg "You did nothing"
  #Escape
    *temp luck 0
    *rand luck 0 6
    *if (((luck) + (agility/opp_agility))>6)
      *set escaped true
      *return
    *else
      *set fight_msg "You failed to escape"
      *set player_action_point - 10
*goto player_turn
*return

Then just put this bit of code into where you want to have the fight. Change these variables or rid of them as you will.

*set combat 40
*set agility 40
*set stealth 40
*set weapons 40
*set has_dagger true
*set has_bow true
*set has_poison true
*set weapon "unequipped"
*gosub_scene fighter switchweapon
*set opp_awareness 100
*set opp_name "Guard"
*set opp_health 200
*set opp_agility 20
*set opp_strength 10
*set opp_alerted false
*set opp_health 200
*set opp_dist 50
*gosub_scene combat_util fight
14 Likes

Holy shit. All this code for just 1 fight? Is it a minimum or as much optional as we can get with cog?

Mind i ask a question. Can we change impact depending on someone having a formation? Like in great tournament when we can make wall of phalanx .is there a way to determine by that or we have to set if formation then damage.also can we have different damage done by different weapons ?as sword at one time is better then spear. So person can choose what to use. But all weapons needs to be trained separately. So if sword mastery is 5 and spear is 2. But opponent is on horse. Naturally spear is better.but with sword we being more skilled.how can we determine the damage caused? It should
Make actual difference rather then just saying sword or spear. Is there any way?

I know writimg a code to show me will be too much. But if someone can explain in any way possible. I’ll be thankful

The amount of code is really too much if you want a fight scene and I would argue that writing a description of the fight would be better. The only thing is doing that takes effort, copying and pasting this code takes no effort.

Adding weapon proficiency is of course doable, create the variables and use them to multiply the damage when they’re used, and to %+ them whenever they are used. I didn’t do this because stats screen don’t need that much information, so I called weapons proficiency in general “weapons”.

In startup

*create prof_sword 131
*create prof_spear 193

When they are used

#Use sword
  *set attack (weapon_damage * (prof_sword / 100))
  *set opp_health - attack
#Use spear
  *set attack (weapon_damage * (prof_spear / 100)
  *if formation = "No formation"
    *set attack + 5
  *if formation = "Phalanx"
    *set attack + 20
  *set opp_health - attack

Formations is useful when there is combat between multiple people but that can be done like in great tournament just by hard coding them since they only happen once. Don’t need a reusable code for that. Just do

*create formation "No formation"

When weapons are used in formation

*if formation = "No formation"
  *set attack + 5
*if formation = "Phalanx"
  *set attack + 20
2 Likes

Thank you so much for answering so fast. May i ask a question? Mostly people ask for help and then people answer.here you yourself posted it. So did you gave it to help new comers so they can copy paste?

Your reply has actually given me some help idea

Yeah I meant it that way. Glad to be of help.

Could i in future maybe ask a bit help for combat coding? Including formation and mastery level. So i can use it few times?
I have never done it before. If yes I’ll pm you some time in future

Yes of course.

@zenovarse I hope you don’t mind,
I coded a simpler one myself
you guys free to copy or modify it

copy this into startup file

*create boss1health 10
*create boss_attk 35
*create boss_dex 30
*create boss_title "The Great"
*create boss1_level 9
*create dmg_attk ""
*create bossmainattk ""
*create bossspecattk ""
*create has_attack 0

then add this into your scene

*label zz_fight
*if boss1health <= 0
    *finish
*if health_points <= 0
    *finish
*if has_attack = 0
 *line_break
 Red dragon
 *line_break
 ${boss1health} HP
 *line_break
 ${boss_title}
 *line_break
 Level ${boss1_level}
 *line_break
 ${health_points} Your HP
 *choice
  #melee attack
   *if strength >= 11
       *rand dmg_attk 8 10
       *if dmg_attk = 8
        *set boss1health -8
        *set has_attack 1
        *goto zz_fight
       *elseif dmg_attk = 9
        *set boss1health -9
        *set has_attack 1
        *goto zz_fight
       *elseif dmg_attk = 10
        *set boss1health -10
        *set has_attack 1
        *goto zz_fight
   *elseif strength <= 10
       *rand dmg_attk 1 3
       *if dmg_attk = 1
        *set boss1health -1
        *set has_attack 1
        *goto zz_fight
       *elseif dmg_attk = 2
        *set boss1health -2
        *set has_attack 1
        *goto zz_fight
       *elseif dmg_attk = 3
        *set boss1health -3
        *set has_attack 1
        *goto zz_fight
   *goto zz_fight
 
*elseif has_attack = 1 
 
 
 
 
 
 
 *if boss_attk >= 30
 *if boss_dex >= 30
  *rand bossmainattk 8 12
  *if bossmainattk = 8
        *set health_points -8
        *set has_attack 0
        *goto zz_fight
  *if bossmainattk = 9
        *set health_points -9
        *set has_attack 0
        *goto zz_fight
  *if bossmainattk = 10
        *set health_points -10
        *set has_attack 0
        *goto zz_fight
  *if bossmainattk = 11
        *set health_points -11
        *set has_attack 0
        *goto zz_fight
  *if bossmainattk = 12
        *set health_points -12
        *set has_attack 0
        *goto zz_fight
 *if boss_attk <= 30
 *if boss_dex <= 30
  *rand bossmainattk 3 5
  *if bossmainattk = 3
        *set health_points -3
        *set has_attack 0
        *goto zz_fight
  *if bossmainattk = 4
        *set health_points -4
        *set has_attack 0
        *goto zz_fight
  *if bossmainattk = 5
        *set health_points -5
        *set has_attack 0
        *goto zz_fight