Page 1 of 1

Ai, like pac-man

PostPosted: Wed Jul 25, 2007 4:38 pm
by Caaz Games
I need an AI code like pac-mans enemy's, like the character needs to move throughout the maze randomly. and there is four different animations faceing North(CopN), East(CopE), South(CopS), West(CopW). But i dont want the cops to move strait towards the Player, i just want them to randomly move around. if you need to see the maze i can post it. i didn't use a path be cuase they move to fast.

PostPosted: Wed Jul 25, 2007 4:56 pm
by Sgt. Sparky
I will make a Demo later. :D
(I must go now. :( )

PostPosted: Wed Jul 25, 2007 4:57 pm
by Caaz Games
ok

PostPosted: Wed Jul 25, 2007 9:28 pm
by Fuzzy
use a 2D array, with each element of the array saying which directions are open to be moved into. Since there are only four, it will be easy.
Code: Select all
 // place in global code
#define NORTH 1
#define SOUTH 2
#define EAST 4
#define WEST 8


See how i doubled each time? that allows no conflict and will let us pic from east and south, for example.

So maybe you have a square where you can go north or south.
Code: Select all
if(CopMap[cop.x,cop.y] == NORTH+SOUTH) // NORTH+SOUTH actually equals 3
{
    do stuff here
}


So the possible moves are...

NORTH+SOUTH+EAST+WEST == 15
SOUTH+EAST+WEST == 14
NORTH+WEST+EAST == 13
WEST+EAST == 12
NORTH+SOUTH+WEST == 11
SOUTH+WEST = 10
NORTH+WEST == 9
WEST == 8
NORTH+SOUTH+EAST == 7
SOUTH+EAST == 6
NORTH+EAST == 5
EAST == 4
NORTH+SOUTH == 3
SOUTH == 2
NORTH == 1

and technically, no move(0), which should be impossible in your game. With 15 combinations, each is represented by a number in an array, and no combination of possible moves matches any others.

From there, pick a random number from 1 to 4, and use that to decide to head NSEW, if its possible from that square. Otherwise, just pick another number.

PostPosted: Wed Jul 25, 2007 9:47 pm
by Caaz Games
So,,, i need boxes in every place on the maze like this? Image except i need them all separate?

PostPosted: Wed Jul 25, 2007 10:20 pm
by Fuzzy
Thats right. You can fill in zero where ever the walls are in the maze. That will automatically tell the cops that they cannot step there. This is a good use for graph paper, if you have any.

Pencil in the walls, then in each remaining white square, figure out the combination of possible moves. From there, you can write in a number. You might be able to pull it off with straight to a text file too.

From there, transform it into a pre filled array.
Code: Select all
char CopMap[16][16] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
}

See? Notice that lack of the last comma.

Thats not tested code. The array might not be quite right. As far as actors go, you dont need one at all the locations. Draw the map as one large actor.

PostPosted: Wed Jul 25, 2007 10:24 pm
by Caaz Games
Ok ill see if this works in a little bit... To Wallmart!xD

PostPosted: Wed Jul 25, 2007 10:47 pm
by Caaz Games
Ok i got it i think, where do i put the codes? Image

PostPosted: Thu Jul 26, 2007 4:14 am
by DilloDude
You need to have a 2 dimensional array with all those numbers stored in it. Once that's done, your cop actor needs to know where it is. That shouldn't be too hard, because you can update it every time you move, so you can keep track of your location in the array. If you know what the initial location is going to be, you can automatically set that on creation, otherwise, calculate it based on coordinates. Store the location in some actor variables: (xloc, yloc). Your cop actor will need a create actor event and a move finish event. They should be axactly the same:
Code: Select all
char movedir;
do
{
    movedir = rand(3.99999);
}while((CopMap[xloc][yloc] & pow(2, movedir)) == 0);
switch(movedir)
{
    case 0: //move north
    MoveTo("Event Actor", 0, -size, velocity, "Event Actor", "");
    //change animation
    break;
 
    case 1: //move south
    MoveTo("Event Actor", 0, size, velocity, "Event Actor", "");
    //change animation
    break;
 
    case  2: //move east
    MoveTo("Event Actor", size, 0, velocity, "Event Actor", "");
    //change animation
    break;
 
    case 3: //move west
    MoveTo("Event Actor", -size, 0, velocity, "Event Actor", "");
    //change animation
    break;
}

Replace 'size' with the distance from the middle of one squqre to the middle of the next. Replace 'velocity' with the speed you want the actor to move. Fill in the appropriarte change animation code.
Wow! I think that's the first time I've used a do...while statement!