Hero Revival: An Excercise in Revision Part 2

Thus begins the legend of The Two Triggers. The story behind the creation of hero revival code continues.

Posted by on

My code wasn't good enough. Or rather, it wasn't cool enough; it functioned just fine. My desire to make an infinitely more spectacular means of hero revival spurred me into enduring an all-night Mexican stand-off with the World Editor and it's wiley trigger editor. But the lack of sleep was worth it, as, many ridiculous hours of revision in, I finally completed a working trigger that functioned exactly as I'd dreamed.

We need to cover one little topic of programming before we can move forward: For loops. For loops repeat a set of actions a set number of times. This is useful in all kinds of ways, but for me, it was useful for reviving my fallen heroes. A For loop begins like this:

actionscript code:
For each (Integer A) from 1 to 5, do (Actions)
Loop - Actions

Integer A is simply the name of the For loop, present to differentiate it from other For loops potentially running simultaneously. From integer to integer is how many times the For loop will repeat itself before moving on to further actions beyond the For loop. The actions within the For loop are nested within that "Loop - Actions" line.

My goal was to simplify my code as much as possible. I still needed two triggers, however. If you reference back to the previous post, the first trigger may seem extremely similar by comparison, but there is one big difference that is resolved in the second trigger.

You may notice that the If/Then/Else statement is missing from the bottom. The statement reset RedHeroesDeadCount to zero once it had become greater than five. No more! Know, all this trigger does is set each dying hero to a different array spot in the unit variable RedHeroesDead[]. It then sets the cooldown integer for the dying hero to zero, since he or she has just died.

The second trigger is where this gets interesting.

This trigger begins the same way, fired whenever player red's turn comes around. But then all heck breaks loose.

First, I set several new variables, all integers. RedRevBaseInt is set to one, and keeps track of which hero is being revived. RedRevReset begins as zero and resets each unit to a previous spot in the array when as the lower numbered units are revived. Finally, we have RedRevIntLoop. It is set to RedHeroesDeadCount, or, how many of Red's heroes are currently not amongst the living. This variable is needed so that I do not override RedHeroesDeadCount, which would throw off everything.

Then, I jump immediately into a For loop. The For loop runs from 1 to RedRevIntLoop, which you may recall is the number of units are currently dead. If three of Red's heroes are dead, this loop will run three times. All of the rest of the actions within this trigger are nested within the Integer A For loop.

The first action within the For loop ups the revival cooldown by one for RedRevBaseInt (which is initially 1, or the hero who had died first). Then the If/Then/Else statement begins. If the revival cooldown for RedRevBaseInt (the first hero who had died) is equal to three, that is, if the first hero has been dead for three turns, then run through the Then actions. If not, if the hero has been dead for two or fewer turns, then jump to the Else actions.

Let's assume the first hero who had died, who is RedHeroesDead[1], is ready to be revived. Well, then we'll instantly revive the hero at the Frontlines.

Next, I reset RedHeroesDead[RedRevReset] to the value of RedHeroesDead[RedRevReset + 1]. The first time this loop is run, this line of code looks like so:

RedHeroesDead[2] has now been copied and pasted onto RedHeroesDead[1], overriding the recently revived hero. Similarly, the next line resets the cooldown of the first RedHeroesDead to that of the second. And then the For Loop Integer B runs again. This time moving a potentially third dead hero to RedHeroesDead[2]. And again, if there is a fourth hero dead, he or she will be moved to RedHeroesDead[3]. Get the picture?

We're out of For loop Integer B. Phew! But this is still work to be done.

Continuing on, we reset RedHeroesDeadCount, subtracting one from the number of red heroes who are currently dead. We also reset RedRevBaseInt to 1. This is because all units have now been moved down a notch in the array. Meaning that next time we run the For loop Integer A, the cooldown for the second most recently dead hero is now housed without RedRevivalCooldown[1]. We also reset RedRevReset=0 for Integer B loop later on.