Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: YotamElal on Tue 01/03/2005 14:49:41

Title: character walk random in walkspace x
Post by: YotamElal on Tue 01/03/2005 14:49:41
How can I make a NPC character walk around random in walkspace 1?

I have other walkable areas that are enabled for my player character.

? :o)
Title: Re: character walk random in walkspace x
Post by: Ashen on Tue 01/03/2005 15:50:42
Try putting this in the rep_ex of the room:

if (character[NPC].walking == 0) {
  int walkx = Random (320);
  int walky = Random (240); // or 200, depending on what resolution you're using
  if (GetWalkableAreaAt (walkx, walky) == 1) MoveCharacter (NPC, walkx, walky);
}


EDIT:
Or not.
Since GetWalkableAreaAt uses screen co-ordinates, this won't work too well (if at all) in scrolling rooms. For that, you'd need to track the absolute (room) co-ords versus the screen co-ords:

if (character[NPC].walking == 0) {
  int walkx = Random (ROOMWIDTH);
  int walky = Random (ROOMHEIGHT);
  int abswalkx;
  int abswalky;
  if (walkx >= GetViewportX ()) abswalkx = (walkx - GetViewportX ());
  else abswalkx = -(GetViewportX () - walkx);
  if (walky >= GetViewportX ()) abswalky = (walky - GetViewportY ());
  else abswalky = -(GetViewportY () - walky);
  if (GetWalkableAreaAt (abswalkx, abswalky) == 1) MoveCharacter (NPC, walkx, walky);
}
Title: ??? there is only 1 x & 1 y
Post by: YotamElal on Wed 02/03/2005 05:18:53
  int walkx = Random (ROOMWIDTH);
  int walky = Random (ROOMHEIGHT);

the room width is from 0 to 320
but the walkable are is from 40 to 200

here I can only put in one X intiger??????
what2do?
Title: Re: character walk random in walkspace x
Post by: Gilbert on Wed 02/03/2005 05:39:38
I think you mean 0-319 and 40-199, right?

In that case just do:
  int walkx = Random (319);
  int walky = Random (159) + 40;
Title: Re: character walk random in walkspace x
Post by: strazer on Wed 02/03/2005 05:44:12
Gilbert, just a note to say that you can use the game.room_width and game.room_height variables so users don't have to manually enter the ROOMWIDTH and ROOMHEIGHT values.
Title: Re: character walk random in walkspace x
Post by: Gilbert on Wed 02/03/2005 05:53:47
But because that was not my codes originally.
Title: Re: character walk random in walkspace x
Post by: strazer on Wed 02/03/2005 06:02:05
Oops, I meant Ashen of course. Sorry. :)
Title: character isn't moving????
Post by: YotamElal on Wed 02/03/2005 06:24:51
the character isn't moving???

if (character[FRA].walking == 0) {
  int walkx = Random (156) + 104;
  int walky = Random (40) + 198;
  int abswalkx;
  int abswalky;
  if (walkx >= GetViewportX ()) abswalkx = (walkx - GetViewportX ());
  else abswalkx = -(GetViewportX () - walkx);
  if (walky >= GetViewportX ()) abswalky = (walky - GetViewportY ());
  else abswalky = -(GetViewportY () - walky);
  if (GetWalkableAreaAt (abswalkx, abswalky) == 1) MoveCharacter (FRA, walkx, walky);
}


?
Title: Re: character walk random in walkspace x
Post by: Ashen on Wed 02/03/2005 09:58:34
strazer:
I realised that after I'd typed it, but it didn't seem worth changing for one specific room. If it needed to be more generic then, yes, that would be the best way.

YotamElal:
Does it work with out the limits?
(i.e change int walkx=Random (156) + 104;  to int walkx = Random (game.room_width);, and  int walky=Random (40) + 198;  to int walky = Random (game.room_height);)
Depending on how your room is set up, it's possible those values never return as on Walkable Area 1. The limits aren't really needed, as the if (GetWalkableAreaAt (abswalkx, abswalky) == 1) stops FRA from moving if the co-ords are out of bounds.
Title: it works without the limits...
Post by: YotamElal on Wed 02/03/2005 11:07:53
it works without the limits...

I need the character to walk aroun either in walkable area 1 or in specific x,y cordinates..

?
Title: Re: character walk random in walkspace x
Post by: Ashen on Wed 02/03/2005 11:18:22
I don't understand. What do you mean by "or in specific x,y cordinates"?
Couldn't you just include those cordinates in walkable area 1?
Title: i mean...
Post by: YotamElal on Wed 02/03/2005 11:26:42
what I mean is

I need the character to walk around random in walkable area 1.
(NOT SOLVED)

if thats not possible.. maybe it's possible instead to walk around in area
x(from 20 to 160) y(from 200 to 230).
Title: Re: character walk random in walkspace x
Post by: Ashen on Wed 02/03/2005 11:35:54
So, it walks around, but isn't stuck on walkable 1? Odd, works OK for me.

I suppose this should do it:

if (character[FRA].walking == 0) {
  int walk x = Random (141) + 20; // since 140 would give a maximum of 159, not 160
  int walky =  Random (31) +  200;
  MoveCharacter (FRA, walkx, walky);
}

But that's basically the simple way I originally posted.
Title: seem to work fine..
Post by: YotamElal on Wed 02/03/2005 13:01:19
seem to work fine..

thnx
Title: Re: character walk random in walkspace x
Post by: Kweepa on Wed 02/03/2005 23:42:57
Quote from: Ashen on Wed 02/03/2005 11:35:54

  int walk x = Random (141) + 20; // since 140 would give a maximum of 159, not 160

Actually Random(n) gives a random integer in the inclusive range [0..n].
e.g., Random(3) may return 0, 1, 2, or 3.
Hence Gil's code.