Wonder around aimlessly?

Started by PIT, Fri 19/01/2007 05:58:20

Previous topic - Next topic

PIT

Here I am, back with yet ANOTHER question.

How would I script a character (MER1 by the way) to wonder around the whole game world (all the rooms) aimlessly.

I tried this going for the current room with this:
Code: ags

if (cMer1.Room == player.Room) {
cMer1.Walk(Random(320), Random(200), eNoBlock, eWalkableAreas);
}

But of course he went spastic, trying to go everywhere at once.

Then I tried this:
Code: ags

if (cMer1.Room == player.Room) {
cMer1.Walk(Random(1), Random(1), eNoBlock, eWalkableAreas);
}

but he just stated walking, but not moving.

I'm completely stumped  ???

Fornax

#1
You're on the right track.

Try this in your 'Runs Constantly' GLOBAL event (or whatever it's called):

Code: ags


    // If character isn't moving it's time to make a new destination choice
    if (Not cMer1.Moving) {

        // Now, walk to random spot.
        // NOTE: We use WalkStraight so he stops as soon as he hits a non-walkable area.
        cMer1.WalkStraight (Random(320), Random(200), eNoBlock);

    }



That'll get your character walking about the current room semi-intelligently.

Now, if you've set up the borders of the room to point to other rooms your character SHOULD autonomously walk into those rooms and continue gamboling around. And he should be able to come back into the room you're in. Anything could happen!


EDIT: Just tried this myself and there are a few quirks to work out:

1. The room borders don't work on a non-player characters. Might have to do some coding to get him to change rooms.

2. AGS has a fit when the other character is in a different room. I'm guessing this is because the walkable areas are no longer loaded so WalkStraight can't function. Might be best to not walk around if not in the same room, but randomly decide whether to appear in the player's room.

3. You might want to force the other character (at random times) to walk right up to the screen edge so that he does change rooms (once #1 is working). At the moment he wombles randomly around but seldom reaches the screen edges.

4. You might want to introduce a random tea break now and then for realism.

Khris

There's no simple solution.

cMer1 is supposed to enter and leave the current room, so AGS has to "know" all room exists and where they lead to.
You could establish a database holding all relevant data, then use it to make both the player and cMer1 change rooms.

It is even possible then to simulate cMer1 wandering around in other rooms by making him transparent and walk around in the current room, ignoring the walkable areas. (Provided all rooms have the same dimensions.)

Which method are you using? Regions? Yellow borders?

Akatosh

If I was you, I'd use regions and use the following type of code in repeatedly_execute (the room one):

(no idea if region[0] is correct, though)

Code: ags

if (Region.GetAtXY(cMer1.x, cMer1.y)==region[0]) cMer1.ChangeRoom(room,x,y); //replace room,x,y with your targets
else if (Region.GetAtXY(cMer1.x, cMer1.y)==region [1]) cMer1.ChangeRoom(room,x,y); //same here
//and so on.


Of course, never place him on another room-change region.

----------

Another way would be to get the ccs (Character Control System) plugin, which provides good functionality for handling NPCs.

Mr Flibble

Couldn't you just put a script in the Before Fade in for every single room the player enters? Do some kind of random number check (I'd suggest a small range) and if you get for example 1, then you could move MER into that room and have him walk around.
Ah! There is no emoticon for what I'm feeling!

monkey0506

#5
Going off of what Flibble said:

Code: ags
// global script

// on event
if (event == eEventEnterRoomBeforeFadein) if (!Random(5)) cMer1.ChangeRoom(data); // (!Random(5)) == (Random(5) == 0) == 16.66% chance

// rep_ex_always
if ((cMer1.Room == player.Room) && (!cMer1.Moving) && (Random(4) > 0)) cMer1.WalkStraight(Random(Room.Width), Random(Room.Height), eNoBlock); // (Random(4) > 0) == 80% chance


I've randomized it so that there's a 16.66% chance on room load that MER1 will be placed in the room. There's also only an 80% chance that once he has stopped moving he will immediately be given new walk-to coordinates. However since that 80% chance is redetermined every game loop, you may want to recalculate that a bit. I've also used Room.Height and Room.Width so that MER1 can be told to walk to any possible coordinate in the room, instead of just inside the 320x200 range (which if all your rooms are 320x200 won't make a difference, but if you have a scrolling room it would).

PIT

#6
Quote
1. The room borders don't work on a non-player characters. Might have to do some coding to get him to change rooms.

2. AGS has a fit when the other character is in a different room. I'm guessing this is because the walkable areas are no longer loaded so WalkStraight can't function. Might be best to not walk around if not in the same room, but randomly decide whether to appear in the player's room.

3. You might want to force the other character (at random times) to walk right up to the screen edge so that he does change rooms (once #1 is working). At the moment he wombles randomly around but seldom reaches the screen edges.

1. I use the rooms repeatedly_execute for this eg.
Code: ags

if (cMer1.x < 2) {
  cMer1.ChangeRoom(int Room, int x, int y);
}

you know what I'm on about.

2. I'm downloading one of the modules as I type for this problem.
EDIT: I found the module to hard to figure out, so this question will still be in the dark for now.

3. I know, just how?

about changing rooms that are accesed by doors:
Code: ags

if ((cMer1.x == 230) && (cMer1.y == 130) {
  cMer1.ChangeRoom(int Room, int x, int y);
}


I just need the
Code: ags

SetTimer(int Number, int Loops);

thing worked out

Ashen

Quote
3. I know, just how?

Might be a bit sloppy, but how about something like:
Code: ags

if ((cMer1.Room == player.Room) && (!cMer1.Moving) && (Random(4) > 0))  {// (Random(4) > 0) == 80% chance
  int Temp = Random(9);
  if (Temp == 0) cMer1.WalkStraight(0, Random(Room.Height)); 
  else cMer1.WalkStraight(Random(Room.Width), Random(Room.Height), eNoBlock);
  // I.e. a 10% chance of walking to some point on the left edge, 90% of walking to some other random point
}


Adjust the probability as you like, and obviously add more conditions depending on how many of the room edges will take the Character to another room.


What do you need to figure out about Timers?
I know what you're thinking ... Don't think that.

PIT

#8
Never mind about the timers Ashen (I needed to read the maual  :P).

When ever I am in the sam room as cMer1 He walks around, and says a randome message.

eg.
Code: ags

if (cMer1.Room != player.Room) {
return;
}
int ran2 = Random(9);
else {
if (cMer1.Moving == false) {
cMer1.WalkStraight(Random(320), Random(200), eNoBlock);
}
if (cMer1.Room == 1) {
  if (ran2 == 0) {
cMer1.Walk(0, cMer1.y, eNoBlock);
}
else if (ran2 == 1) {
  cMer1.Walk(230, 130, eNoBlock);
}

and that's how I have it for all my rooms.

For talking it's:
Code: ags

function game_start {

SetTimer(1, 80);

}
--------------------------
function repeatedly_execute {

int ran3 = Random(24);
if (IsTimerExpired(1)) {
if (ran3 == 0) {
  cMer1.SayBackground("Blah Blah Blah");
}
if (ran3 == 1) {
  cMer1.SayBackgorund("Yadda Yadda Yadda");
}
SetTimer(1, 80); //So it resets the timer to make him say it again, otherwise he'll never speak
}

You know what I'm on about don't you?

Ashen

#9
That looks pretty solid - are you having any problems with it, or have you just posted it for reference? There's a typo in the first sample, I think (it says if (ccMer1.Room == 1) - should be CMer1?), and that condition looks to be missing a closing brace - but they might be mistakes with copying it to here, and not in the actual code.

Like I said, though, all those Room dependant conditions might get a little sloppy - if you can figure out KhrisMUC's code from this thread, it might give you a slightly neater way to do it (replace 'doors' and 'door coords' with 'edges' and 'edge boundaries').
I know what you're thinking ... Don't think that.

PIT

#10
Yes just for refrence.
I'll fix up ccMer1.
It doens't have a closing brace because there's more code.
For Room 3, 4 and 5 and when more rooms come along I'll add more.
The room Code ends when the Talking code starts.
It all works fine  ;D

Didn't you read my reply in this thread?  :D
this thread


SMF spam blocked by CleanTalk