Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: jfarwyke on Wed 15/04/2009 17:53:24

Title: want character to move away from playable character if too close (SOLVED)
Post by: jfarwyke on Wed 15/04/2009 17:53:24
Thought I could use FolllowCharacter with special values to have character move away instead of follow. Basically, as my playable character approaches the other character, I want the other character to move away. I didn't want to set coordinates though, but instead have him move a few steps away if my playable character gets too close. Is this possible? I've searched the manual and forum to no end. thanks.
Title: Re: want character to move away from playable character if too close
Post by: Matti on Wed 15/04/2009 21:37:09
I'd suggest simple checks to determine whether the player character is too close or not. Something like the following:


if (player.x < cBla.x && player.x > cBla.x - 50) cBla.walk (cBla.x+50, cBla.y);
if (player.x > cBla.x && player.x < cBla.x + 50) cBla.walk (cBla.x-50, cBla.y);
if (player.y < cBla.y && player.y > cBla.y - 50) cBla.walk (cBla.x, cBla.y+50);
if (player.y > cBla.y && player.y < cBla.y + 50) cBla.walk (cBla.x, cBla.y-50);


I don't know the situation in your game but I guess you would have to check too if the character is next to a wall / the border of the walking area. In that case he would have to walk around the player character. I'm doing that in my current game, it's an annoying list of coordinate checks, but it's possible.

I don't know if there's an easier way...
Title: Re: want character to move away from playable character if too close
Post by: Khris on Wed 15/04/2009 22:20:24
That code won't work as intended (since x & y are handled separately, the y walk will always cancel the x walk).

I'd put this in rep_ex:

  Character*c = cOtherChar;  // replace cOtherChar
  int x = c.x - player.x;
  int y = c.y - player.y;
  int dist = 30;   // walk away if distance is below 30
  if (!c.Moving && x*x + y*y < dist*dist) {
    x = (x*5)/3; y = (y*5)/3;    // move away to a distance of ~50
    c.Walk(c.x + x, c.y +y);    // walk away directly from player
  }


Tested and more or less working. If the other character is trapped, it will often start moving and turning on the spot.
Title: Re: want character to move away from playable character if too close
Post by: jfarwyke on Thu 16/04/2009 05:39:03
Thanks guys! That code does the trick Khris, it looks great, just what I wanted. But let's say I wanted to apply this to the other characters in the room too. I tried using the same code with the other character names, but it wants me to change the variables, x, y, dist....If I change those variables, the code won't work.
Title: Re: want character to move away from playable character if too close
Post by: Trent R on Thu 16/04/2009 05:48:24
Copy the code again: (I think I changed all the vars)  Character*c1 = cOtherChar;  // replace cOtherChar
  int x1 = c1.x - player.x;
  int y1 = c1.y - player.y;
  int dist1 = 30;   // walk away if distance is below 30
  if (!c1.Moving && x1*x1 + y1*y1 < dist1*dist1) {
    x1 = (x1*5)/3; y = (y1*5)/3;    // move away to a distance of ~50
    c1.Walk(c1.x + x1, c1.y +y1);    // walk away directly from player
  }

  Character*c2 = cAnotherChar;  // replace
  int x2 = c2.x - player.x;
  int y2 = c2.y - player.y;
  int dist2 = 30;   // walk away if distance is below 30
  if (!c2.Moving && x2*x2 + y2*y2 < dist2*dist2) {
    x2 = (x2*5)/3; y = (y2*5)/3;    // move away to a distance of ~50
    c2.Walk(c2.x + x2, c2.y +y2);    // walk away directly from player
  }



~Trent
Title: Re: want character to move away from playable character if too close
Post by: jfarwyke on Thu 16/04/2009 07:02:54
Wow, thanks man. I should of thought of that. You guys are smart.
Title: Re: want character to move away from playable character if too close
Post by: Khris on Thu 16/04/2009 09:24:00
Put this above rep_ex in the global script:
function Flee(this Character*, int dist) {
  if (this.Room != player.Room) return;
  int x = this.x - player.x;
  int y = this.y - player.y;
  if (!this.Moving && x*x + y*y < dist*dist) {
    x = (x*5)/3; y = (y*5)/3;    // move away to a distance of ~50                          Edit: REPLACED dist BY 3
    this.Walk(this.x + x, this.y + y);    // walk away directly from player
  }
}


Call this inside rep_ex:
  cShyguy1.Flee(30);
  cShyguy2.Flee(30);
  ...


Edit: corrected line
Title: Re: want character to move away from playable character if too close
Post by: jfarwyke on Thu 16/04/2009 14:32:58
I can't thank you enough. One day I hope to be able to understand all that code and why it works. I tried the scripts tutorial, but for some reason I can't make sense of variables. But I'll get it sooner or later. Thanks again.
Title: Re: want character to move away from playable character if too close
Post by: Khris on Thu 16/04/2009 15:13:48
I can break it down for you:

I have used an extender function, so 'this' represents the current character that's supposed to move away from the player

  if (this.Room != player.Room) return;
If the character isn't in the current (player's) room, exit the function.

  int x = this.x - player.x;
  int y = this.y - player.y;

Create two variables, type int (= integer = whole number), and store the x and y distance to the player in them.

  if (!this.Moving && x*x + y*y < dist*dist) {
If the character currently isn't moving AND the distance to the player is below the submitted distance (30)...

    x = (x*5)/3; y = (y*5)/3;    // move away to a distance of ~50
...increase the length of the vector from player to character by 40%...

    this.Walk(this.x + x, this.y + y);    // walk away directly from player
...and send them on their way
Title: Re: want character to move away from playable character if too close
Post by: jfarwyke on Fri 17/04/2009 19:34:36
Thanks again!
Title: Re: want character to move away from playable character if too close
Post by: jfarwyke on Sun 19/04/2009 10:24:26
Thanks again!