Scripting a Fight

Lighthouse
I'm struggling to create a fight in my text based game. I don't want anything complicated: if you've ever played Zork, you know what I'm talking about. Just hitting the opponent until he dies, while receiving damage yourself. I don't even know if there is a way to script what I want to do. Right now, I have created a series of "first time" commands that give a probability of the opponent hitting you or missing you. The problem is, the fight is always the same amount of turns until it ends. It doesn't end when the opponents health is depleted, which is what I want. Also, it means that the opponent will only return an attack, so if you take a look at the room while in the fight your opponent will not attack. Is there a way to script what I want? Is there a way to give other entities health that can be decreased? Please help.

The Pixie
Well first I am going to pimp my RPG library. It may be more than you want, but might be more than writing it all yourself.
viewtopic.php?f=18&t=4886

Assuming you want to do it all yourself...

You need an attribute, let us call it "hitpoints". You need to set it up on the player and any opponents. You can do that on the Attributes tab (as long as this is with the off-line editor). Attributes are at the bottom, just click add, and type the name. Then set it to be Integer (i.e., a whole number), and set the value.

Sound like you already have an attack command. You can randomise it with the GetRandomInt function:
http://docs.textadventures.co.uk/quest/ ... omint.html

For the opponent, you need a "turn script". A turn script will fire every turn, so the opponent can attack even if the player is not attacking. There are various ways to set it up. Probably the easiest is to create a turn script for each opponent, and only set it going when that opponent starts to attack. Each turn the opponent will attack, just remember to stop it when the opponent is dead.

That is a brief overview that will hopefully get you started, but if you want more, we can see what we can do.

XanMag
Here I am again to offer my foolishness!! You'll probably find Pixie's suggestions more to your liking but who knows...

Here's my method and probably what I would toy with as a non-coder. I'd create, say, 4-5 duplicate enemies. Give each a percent of successful hit under an 'attack enemy' command and then create nested 'IF' statements that raise flags for a counter attack with a percentage success that decrease players health. If your attack is successful, I'd move the hit mob, possibly after a counter attack, out of the room and replace it with an identical mob (maybe changing the name from mighty troll warrior to staggering troll warrior to bloody troll warrior to dead troll warrior (or whatever :)) with a similar successful hit-flag-counter-replace script. The last mob after a successful hit would leave the room to be replaced by a dead mob. All hit mobs and replacing mobs would be stored in a dead room. Use the move object option.

This way, you could say a mob is killed after 4-5 successful hit attempts and each mob could potentially decrease your health rating WITHOUT dealing with creating HP's for one character/monster. I'm terrible with integers and attributes and getting random values so I've established some non-conventional/non-coding work arounds. Moving sh!t into and out of rooms has been a life saver to my game creation!! :)

Good luck and happy gaming.

HegemonKhan
Pixie's Simple Combat Library is best for people new to this stuff (and~or want an 'open' combat system), but if you're more interested in a 'script-locked' (you're stuck in the battle sequence until you win, you lose, you run away, or monster runs away) combat system, you can take a look at my (using a variation of Pertex' Combat Library code ~ all credit goes to Pertex) combat code, though it is a bit more complex in design, but at least maybe it can help you see and get some ideas at doing combat:

viewtopic.php?f=10&t=3348&start=120#p22483

and just search my posts for~on 'attributes' and~or 'if' scripts, as I got a lot of posts explaining these things, lol.

-----------

conceptually:

1. both characters ( 'player' Player Object and 'orc' Object) need a 'life' Integer Attribute

2. both characters ( 'player' Player Object and 'orc' Object) need a 'damage' Integer Attribute

3. the 'orc' Object needs a 'dead' Boolean Attribute that is set to Value: false

4. the 'orc' Object needs a 'fight' Verb

5. inside the 'orc' Object's 'fight' Verb, you want this (in code) setup:

with my comments:

// you check if the 'orc' is not dead (if so: subsequently do battling scripts) or dead (if so: subsequently msg that the orc is already dead ~ see the very bottom of this code block):
if (orc.dead = false) {
// you go first, attacking the orc:
orc.life = orc.life - player.damage
msg ("You attack the orc, hitting it for " + player.damage + " damage, leaving it with only " + orc.life + " life left.")
// you check if the orc is dead (via if it has 0 or less life):
if (orc.life <= 0) {
// you alter~re-set the 'orc' Object's 'dead' Boolean Attribute to now having the Value of 'true', making it be 'dead' :
orc.dead = true
msg ("You killed the orc.")
// if the orc is not dead (if it has more than 0 life remaining), it now attacks you:
} else {
player.life = player.life - orc.damage
msg ("The orc attacks you for " + orc.damage + " damage, leaving you with only " + player.life + " left.")
// we check if you're dead (via if your life is 0 or less), and if you are, we msg it and that the game is over, and lastly, we end the game:
if (player.life <= 0) {
msg ("You were killed by the orc.")
msg ("GAME OVER")
finish
// this below is to loop~continue the combat sequence until you or the orc is killed, locking you into the battle sequence, preventing you from doing anything else, until the battle is over:
} else {
// this entire 'fight' scripting block would have to be put inside of a Function (which the Function would be called upon from within your 'orc' Object's 'fight' Verb), so that you can then use this here code line to 'call upon' this Function, to loop~run through it again
}
}
// remember, we got to deal with if the orc is already dead (see the very top comment):
} else if (orc.dead = true) {
msg ("The orc is already dead, silly. You can't fight a dead rotting corpse!")
}


without my comments, full code (using looping ~ as a Function):

the 'orc' Object's 'fight' Script Attribute (GUI~Editor: Verb) and the 'fight' Verb (the GUI~Editor automatically creates this for you):

<object name="orc">
<inherit name="editor_object" />
<parent type="object">room</parent>
<fight type="script">
orc_fight_function
</fight>
</object>

<verb>
<property>fight</property>
<pattern>fight</pattern>
<defaultexpression>You can't fight that!</defaultexpression>
</verb>

the 'orc_fight_function' Function:

<function name="orc_fight_function">
if (orc.dead = false) {
orc.life = orc.life - player.damage
msg ("You attack the orc, hitting it for " + player.damage + " damage, leaving it with only " + orc.life + " life left.")
if (orc.life <= 0) {
orc.dead = true
msg ("You killed the orc.")
} else {
player.life = player.life - orc.damage
msg ("The orc attacks you for " + orc.damage + " damage, leaving you with only " + player.life + " left.")
if (player.life <= 0) {
msg ("You were killed by the orc.")
msg ("GAME OVER")
finish
} else {
orc_fight_function
}
}
} else {
msg ("The orc is already dead, silly. You can't fight a dead rotting corpse!")
}
</function>

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums