character walk random in walkspace x

Started by YotamElal, Tue 01/03/2005 14:49:41

Previous topic - Next topic

YotamElal

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)

Ashen

#1
Try putting this in the rep_ex of the room:
Code: ags

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:
Code: ags

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);
}
I know what you're thinking ... Don't think that.

YotamElal

  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?

Gilbert

I think you mean 0-319 and 40-199, right?

In that case just do:
  int walkx = Random (319);
  int walky = Random (159) + 40;

strazer

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.

Gilbert

But because that was not my codes originally.

strazer

Oops, I meant Ashen of course. Sorry. :)

YotamElal

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);
}


?

Ashen

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.
I know what you're thinking ... Don't think that.

YotamElal

it works without the limits...

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

?

Ashen

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?
I know what you're thinking ... Don't think that.

YotamElal

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).

Ashen

So, it walks around, but isn't stuck on walkable 1? Odd, works OK for me.

I suppose this should do it:
Code: ags

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.
I know what you're thinking ... Don't think that.

YotamElal

seem to work fine..

thnx

Kweepa

Quote from: Ashen on Wed 02/03/2005 11:35:54
Code: ags

  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.
Still waiting for Purity of the Surf II

SMF spam blocked by CleanTalk