Attacking Companions (Script Written) [UNSOLVED]

So I'm wanting enemies in general to randomly attack companions (if/when they are present) as well as the Player. I have this script but I was wondering how I might go about adding companions to it.

ORIGINAL TURNSCRIPT

if (not game.notarealturn) {
  foreach (o, GetDirectChildren(player.parent)) {
    if (GetBoolean(o, "attacking_player")) {
      if (o.health > 1) {
        if (HasScript(o, "specialattack")) {
          if (RandomChance(5)) {
            do (o, "specialattack")
          }
          else {
            do (o, "attackplayer")
          }
        }
        else {
          do (o, "attackplayer")
        }
      }
    }
  }
}
if (not game.notarealturn) {
  game.turncount = game.turncount + 1
}
game.notarealturn = False

AND this was the other script on the Monster Object.

bonus = this.damagebonus - player.agility / 2
damage = this.damage - player.defense / 3
if (damage < 1) {
  if (player.ghost=True) {
    if (RandomChance(47)) {
      damage = 1
    }
    else {
      damage = 0
    }
  }
  else {
    if (RandomChance(50)) {
      damage = 1
    }
    else {
      damage = 0
    }
  }
}
if (player.ghost=False) {
  if (RandomChance(40 + bonus * 5)) {
    msg (this.attackplayerhit)
    player.health = player.health - damage
  }
  else {
    msg (this.attackplayermissed)
  }
}
else {
  if (RandomChance(37 + bonus * 5)) {
    msg (this.attackplayerhit)
    player.health = player.health - damage
  }
  else {
    msg (this.attackplayermissed)
  }
}

Thank you so much ahead of time for any help in this.

Anonynn.


My first thought would be to put at the start of the actual attack script something like:

targets = some function that gets a list of companions
list add (targets, player)
target = PickOneObject (targets)
game.text_processor_variables = QuickParams ("target", target)

and then for the remainder of the script refer to target.agility, target.defense, target.heath, etc.

I added it to the taxt_processor_variables so that a monster's attack message can be something like "It lumbers forward and takes a swing at {object:target}." to show the name of the companion it's attacking (or 'you' for the player).


(Sorry, still behind on the other script. About all the coding I've done lately is things that I can finish in a single session, because memory issues are starting to bite again)


Io

Mrangel's idea is sort of what I would recommend too. Remember, you can nest multiple attributes in each other. You can have Enemy.Target as an object, so you can easily do something like, say, an enemy striking their target, but reducing the damage based on their armor:

 Enemy.Target.Health=Enemy.Target.Health-(Enemy.Strength/Enemy.Target.Armor)

I wouldn't suggest nesting attributes for something like this.

In this case, it's picking a target each time the enemy attacks, so there's no benefit to saving it. The target only needs to be a local variable (which are quicker to access).

If you wanted your enemy to keep attacking the same target on consecutive turns, you'd have something like:

targets = some function that gets a list of companions currently alive/targetable
list add (targets, player)
if (HasObject (this, "last_attacked") and ListContains (targets, this.last_attacked) and RandomChance (85)) {
  target = this.last_attacked
}
else {
  target = PickOneObject (targets)
  this.last_attacked = target
}
game.text_processor_variables = QuickParams ("target", target)
// and the code here to actually do the attack

That's still using a local variable, but stores it in an attribute between turns. More efficient from a computation perspective, and the code seems easier to read, for me at least.


Io

@MrAngel, ah I was thinking if he had multiple enemies.


Spam Removed


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

Support

Forums