Help with code where attacks can cause damage to multiple targets but reduces each time?

For my entry in the Halloween Jam. The player is trapped in a barrier and this barrier has five cores that power it.

The player can damage the barrier which in turns damages one of the five cores.

My issue is that some of the possible options to damage it can cause more damage than one core can take (each core has basically 20% hp with the five cores = 100%). So with each attack if the total damage is greater than the hp of the core it should damage the next core (so for the Author’s order option below the damage is 30 which would destroy the first core and then do 10 damage to the next core)

Now for this I am considered using a code like this;

How does Sam attack the antagonist?
    #Use Author's order.
        *set damage 30
        *goto cores
    #Use Fan flames
        *set damage 15
        *goto cores

*label cores
*if (damage > 20)
    *set core1 0
    *set damage (damage - 20)
    *goto next_core
    *set core1 (core1 - damage)
    *goto next_scene

*label next_core
*if (damage > 20)
    *set core2 0
    *set damage (damage - 20)
    *goto next_core
    *set core2 (core2 - damage)
    *goto next_scene

*label next_scene
Code end here.

I’m just wondering whether there would be an easier way of coding this? Has anyone got any suggestions?

Only way I can think of is to have just one damage variable “cores” set to 100 and then slowly subtract damage. For checking what core you’re on it can be something along the lines of

*if cores > 80
   *comment core1 code
*elseif cores > 60
   *comment core 2 code
*elseif cores > 40
   *comment core 3 code
*elseif cores > 20
   *comment core 4 code
*elseif cores > 0
   *comment core 5 code
   *comment all cores destroyed code

this probably wouldn’t work as well if say, you wanted core destroyed text but it doesn’t look like you have that anyways?

If there is something more specific and more complicated you want to do, there probably is a solution but… this is the lowest effort solution I could think of.

1 Like

The above code is just a sample text to show the general idea of the code. I’d not got around to actually coding it in the game itself.

Originally I did just have a barrier stat but switched to a single barrier stat for the player to see and the five separate core stats in the background when I decided on that was how the barrier worked and certain demons can attack a specific core.

Yeah I’m not sure there would be anything that is a lot more efficient, although I guess you could do an array and then loop through to determine where the overflow damage goes? In case it overflows to more than one crystal (like if the one attacked is at 5, the attack does 30 so it kills the one attacked, the next one and damages a third).

1 Like

My first observation is that this only works for the first hit. Subsequent hits should not compare to 20 but to the current health points of the core. My suggestion is to make the subroutine more agnostic of the cores so you can avoid an infinite if ladder. There is also one approach if all cores have the same amount of health points, and another if the cores can have arbitrary amounts of health points.

This would be a simple solution considering:

  • 5 cores
  • total health point of 100
  • each core has the same amount of health points, i.e. 20
  • cores receive damage in order, that is, core 3 is only damaged if 1 and 2 are already dead
*create accumulated_damage 0
*create current_core 0
*create current_core_health 0

*label deal_damage
*params damage_points
*set accumulated_damage +damage_points
*comment The line below clamps the value if it goes above 100.
*set accumulated_damage "@{(accumulated_damage > 100) 100|${accumulated_damage}}"
*comment The following lines will identify the current core.
*comment For example, if the accumulated damage is 45, the current core is number 3 (20 + 20 + 5)
*set current_core (accumulated_damage / 20)
*set current_core (current_core - (current_core modulo 1))
*set current_core "@{(current_core = 5) 5|${current_core + 1}}"
*comment The following line will find the current core's remaining health points
*set current_core_health ((current_core * 20) - accumulated_damage)

Then you can use current_core to produce flavour text and make reasonable assumptions, for example, if you’re currently at core 3, then it means 1 and 2 are busted and 4 and 5 are at full health.

You can use accumulated_damage and current_core_health for checks.

*if (current_core = 3)
    Well, you're halfway through! Keep going!

*if (current_core_health >= 15)
    The core pulses with a dimming light. You know another hit and it will be done for!

*if (accumulated_damage = 100)
    *goto end_fight

I just wrote all of this from the top of my head, there might be a bug somewhere, but the general idea is there.


Yeah that was the issue I noticed myself. I think I’ve got a fix for that but its literally a note on a scrap piece of paper as I had turned my laptop off and was about to go to bed last night when I thought of it. I’ll try your code too.

Thanks for the suggestions so far.

1 Like

This is what I’ve done with the code. I’m just in the process of running some tests on it. But initially it does seem to do what I want.

*label core_check
*if (core1 > 0)
    *set core "core1"
    *goto core_damage
*elseif (core2 > 0)
    *set core "core2"
    *goto core_damage
*elseif (core3 > 0)
    *set core "core3"
    *goto core_damage
*elseif (core4 > 0)
    *set core "core4"
    *goto core_damage
    *set core "core5"
    *goto core_damage

*label core_damage
*if (damage > {core})
    *set damage - {core}
    *set {core} 0
    *if (damage > 0)
        *goto core_check
        *goto end
    *set {core} - damage
    *goto end

*label end

I’ve gone back to the original version of just having a single barrier stat. I couldn’t settle on what exactly I was after with the five cores (I did have an idea but that’s gone from my mind and it was more work for something I was no longer settled on).

What I have done though is set the barrier at a base 50% when the game starts and the player will determine how strong the barrier containing them is. (increasing it to 56% at its weakest to 74% at its strongest followed by another increase depending on the magi you pick (additional increase between 5-20%)

It should allow for a more varied experience while minimising tricky code.

Here is a minimum working example, it should work for any number of cores with any damage amounts since it calls itself recursively if the damage is greater than the HP of the cores. You can test it by copying it as is into a startup.txt and running it in CSIDE. I wrote the code without using arrays, but if you are using the latest version of ChoiceScript, you can use array notation/syntax to simplify it.

Note that the *stat_chart is only there for debugging purposes. Inside the *label barrier you can have whatever text you like and individually tailor text using switches based on the currentCore, the overallHealth, or the individual core healths coreHealth_N where N is an integer.

*title Help with code where attacks can cause damage to multiple targets but reduces each time?
*author Joe S. Fung


*comment note that you can use create array to get the same results
*create coreHealth_1 20
*create coreHealth_2 20
*create coreHealth_3 20
*create coreHealth_4 20
*create coreHealth_5 20
*create coreHealth_count 5

*create damage 0

*comment You can make these temps if not used elsewhere
*create coreName "coreHealth_"
*create currentCore "1"

*label barrier
Core Health Remaining
*temp overallHealth ((((coreHealth_1 + coreHealth_2) + coreHealth_3) + coreHealth_4) + coreHealth_5) 
  text coreHealth_1 Core 1 HP
  percent (coreHealth_1*coreHealth_count) Core 1
  text coreHealth_2 Core 2 HP
  percent (coreHealth_2*coreHealth_count) Core 2
  text coreHealth_3 Core 3 HP
  percent (coreHealth_3*coreHealth_count) Core 3
  text coreHealth_4 Core 4 HP
  percent (coreHealth_4*coreHealth_count) Core 4
  text coreHealth_5 Core 5 HP
  percent (coreHealth_5*coreHealth_count) Core 5
  text overallHealth Barrier HP
  percent overallHealth Barrier

How does Sam attack the antagonist?
  #Use Author's order. [30 damage]
    *set damage 30
    *goto cores
  #Use Fan flames [15 damage]
    *set damage 15
    *goto cores

*label cores
*temp coreHealthName (coreName & currentCore)
*set {coreHealthName} ({coreHealthName} - damage)

*if {coreHealthName} < 0
  *comment Health of current core less than zero
  *set damage (0 - {coreHealthName})
  *set {coreHealthName} 0
  *if currentCore = coreHealth_count
    *comment The last core has less than or equal to zero HP
    *goto destroyed
    *comment partial damage to next core
    *set currentCore + 1
    *goto cores
*elseif {coreHealthName} = 0
  *comment Current core is destroyed
  *set currentCore + 1
  *if currentCore > coreHealth_count
    *goto destroyed
    *goto barrier
  *comment Health of current core greater than 0
  *goto barrier

*label destroyed
All cores were destroyed!

I noticed that you found another solution, but I thought that this might help if someone has a similar situation.