This section will explain how basic script mechanics work. If you are familiar with Pfhortran scripting or assembly-style coding mechanics, you can skip this section and go straight to Procedures.
To write effective Pfhortran scripts, it is important to understand how execution works. Execution is the carrying-out of an instruction, and thereby changing something. Pfhortran scripts execute one instruction per frame, meaning that normally you can only perform one operation per frame. There are times when this is not true, but we'll get into such examples later.
Let's examine a sample script.
repeat: inflict_damage 10
jump repeat
This script inflicts 10 points of damage onto the player every other frame until the player dies. The script is a loop... it will repeat it's instructions over and over and over again. The loop is built around two separate pieces: a "label" and a jump statement. A label is simply a way of naming a particular line of code. In this case, we have named the first line "repeat". The name can be anything, as long as it ends with a colon. The use of labels allows us to refer to specific lines in a Pfhortran script very easily. In this case, the "repeat" label is used by the jump instruction. Jump simply tells the script to move to the label specified and continue. So in this case, jump tells Pfhortran to go back to the line named "repeat" and execute it again. This way an infinite loop is generated: damage is inflicted to the player (via the inflict_damage command on the first line), the jump instruction tells Pfhortran to move back to the first line, damage is inflicted on to the player, jump tells Pfhortran to move back to the first line, etc, etc. You can see that this particular script will never end. You might also notice that the player will only take damage every other frame because it takes one frame to preform the jump statement.
Lets make this script a little more interesting... maybe give the player a chance at survival.
repeat: inflict_damage 10
wait_ticks 230
add_item energy_powerup
jump repeat
Ok, we have added a few lines of code, but the basic loop has not changed. However, after the player is damaged once, the script will wait 230 ticks, a few seconds, and then give the player an energy powerup before repeating. So what is likely to happen is the player takes some damage, but after a few seconds their life is restored via a powerup. This is going to be pretty annoying for the player, as their screen will be flashing red and green all the time, but it illustrates how loops can be used to generate interesting effects.
Now let's make things really interesting. The code is starting to get a bit long, so I am going to add line numbers to the left of this document so that it is easier to refer to specific lines. Note that these line numbers should be left out of actual Pfhortran scripts--I am just using them here to help explain how each script works.
01
02
03
04
05
06
07
|
add_item flamethrower
select_weapon flamethrower
repeat: inflict_damage 10
wait_ticks 20
add_item flamethrower_canister
set_life 15
jump repeat
|
The script has evolved into something we might actually want to use... if we were out to really annoy the player. The script starts out by giving the player the flamethrower and forcing them to select it. Then the loop begins with the familiar inflict_damage instruction. Within the loop we wait a tiny bit (20 ticks), give the player some more flamethrower ammo, and set their life to a mere 15 points. The result of this script will be that the player has a flamethrower with unlimited ammo, but their life will almost never rise above 5 points (very low indeed). Though their life is restored to 15 each time the script loops, the beginning of the loop takes 10 points away from that, leaving the player 20 ticks to fend for himself with five points of life. This might be called the "berserker rage flame script" or some such, as most players will just try to take out as many opponents as possible before being hit.
If you understand Pfhortran so far, you should have no trouble with the next few sections. If you got lost somewhere along the way, try reading it again. If that doesn't help, skim the next few sections and them jump straight into the tutorial below. If you are still totally lost, let me know.
|