Echo Magnum
Member
I've gotten into GML ( Game Maker Language ) thanks to the game makers apprentice and some useful youtube videos and decided to make a complete game with nothing but GML.
I'm doing well apart from the enemy AI. It works for the most part apart from a few bugs that really hurt the gameplay.
I ripped and edited the code from the game makers aprentice, if you have it, it was on the pyramid maze game ( the last game
in GML ).
Here is the script. its in three parts, I'll explain...
Enemy AI : Acts as a control script that controls the behavior of the Enemy depending on the
visibility of the player. If the player is visible then it goes to the AlertAI script, otherwise it
goes to the MovementAI script, or it goes to the AlertAI to look for the player.
Alert AI : This is the script that makes the enemy follow / chase the player if they are visible
MovementAI : general movement script so the enemy can maneuver around walls and obstacles without
getting stuck in a dead end.
Random Direction: This sets the enemy to move to any random direction during a collision with a wall
or obstacle.
The MovementAI script when set to run when the enemy collides with a wall results in them getting stuck at some
point moving forward.
The enemy chases the player for some time and then gets stuck moving in one direction untill a collision with a wall
or some instance.
The enemy can manover through a maze and walls when the enemy AI is set to run during a collision however they
get stuck again during unusual turns
Also if anyone has any advice on programming enemy AI, some tutorials ( preferably for Game Maker 8 ) or anything that might come of use It would be appreciated if you could post a link or reference.
Also ask if you want to take a look at the game and help me debug the AI and I'll send it to you in a PM.
Thanks.
I'm doing well apart from the enemy AI. It works for the most part apart from a few bugs that really hurt the gameplay.
I ripped and edited the code from the game makers aprentice, if you have it, it was on the pyramid maze game ( the last game
in GML ).
Here is the script. its in three parts, I'll explain...
Enemy AI : Acts as a control script that controls the behavior of the Enemy depending on the
visibility of the player. If the player is visible then it goes to the AlertAI script, otherwise it
goes to the MovementAI script, or it goes to the AlertAI to look for the player.
{
if ( !place_snapped(8,8) ) { exit; }
// Calculate distance and visibility
var dist, vis;
dist = point_distance(x,y,player.x,player.y);
vis = !collision_line(x+16,y+16,player.x+16,player.y+16,wall,false,false);
//Executes the rush on order
if ( vis ) { AlertAI(); exit; }
if ( dist>300 ) { MovementAI(); exit; }
if ( random(2)>1 ) { AlertAI(); exit; }
MovementAI();
}
if ( !place_snapped(8,8) ) { exit; }
// Calculate distance and visibility
var dist, vis;
dist = point_distance(x,y,player.x,player.y);
vis = !collision_line(x+16,y+16,player.x+16,player.y+16,wall,false,false);
//Executes the rush on order
if ( vis ) { AlertAI(); exit; }
if ( dist>300 ) { MovementAI(); exit; }
if ( random(2)>1 ) { AlertAI(); exit; }
MovementAI();
}
Alert AI : This is the script that makes the enemy follow / chase the player if they are visible
{
if ( !place_snapped(8,8) ) { exit; }
// find out in which location the player is located
var dir;
dir = point_direction(x,y,player.x,player.y);
dir = round(dir/90);
if ( dir == 4 ) dir = 4;
// the 4 rules that move the enemy in players direction
if ( dir == 0 && direction != 180 && place_free(x+4,y))
{ direction = 0; exit;}
if ( dir == 1 && direction != 270 && place_free(x,y-4))
{ direction = 90; exit;}
if ( dir == 2 && direction != 0 && place_free(x-4,y))
{ direction = 180; exit;}
if ( dir == 3 && direction != 90 && place_free(4,y+4))
{ direction = 270; exit;}
// otherwise do the normal walking behavior
MovementAI();
}
if ( !place_snapped(8,8) ) { exit; }
// find out in which location the player is located
var dir;
dir = point_direction(x,y,player.x,player.y);
dir = round(dir/90);
if ( dir == 4 ) dir = 4;
// the 4 rules that move the enemy in players direction
if ( dir == 0 && direction != 180 && place_free(x+4,y))
{ direction = 0; exit;}
if ( dir == 1 && direction != 270 && place_free(x,y-4))
{ direction = 90; exit;}
if ( dir == 2 && direction != 0 && place_free(x-4,y))
{ direction = 180; exit;}
if ( dir == 3 && direction != 90 && place_free(4,y+4))
{ direction = 270; exit;}
// otherwise do the normal walking behavior
MovementAI();
}
MovementAI : general movement script so the enemy can maneuver around walls and obstacles without
getting stuck in a dead end.
{
var go_ahead, go_left, go_right;
if ( !place_snapped(8,8) ) { exit; }
if ( vspeed == 0 )
{
go_ahead = place_free(x+hspeed,y);
go_left = place_free(x,y+4);
go_right = place_free(x,y-4);
if ( !go_ahead && !go_left && !go_right ) { direction +=180; exit; }
while (true) //forever
{
if ( go_ahead && random(3)<1) {exit;}
if ( go_left && random(3)<1) {direction = 270; exit;}
if ( go_right && random(3)<1) {direction = 90; exit;}
}
}
else
{
go_ahead = place_free(x,y+vspeed);
go_left = place_free(x+4,y);
go_right = place_free(x-4,y);
if ( !go_ahead && !go_left && !go_right ) {vspeed = -vspeed; exit;}
while (true) //forever
{
if ( go_ahead && random(3)<1) {exit;}
if ( go_left && random(3)<1) {direction = 0; exit;}
if ( go_right && random(3)<1) {direction = 180; exit;}
}
}
}
var go_ahead, go_left, go_right;
if ( !place_snapped(8,8) ) { exit; }
if ( vspeed == 0 )
{
go_ahead = place_free(x+hspeed,y);
go_left = place_free(x,y+4);
go_right = place_free(x,y-4);
if ( !go_ahead && !go_left && !go_right ) { direction +=180; exit; }
while (true) //forever
{
if ( go_ahead && random(3)<1) {exit;}
if ( go_left && random(3)<1) {direction = 270; exit;}
if ( go_right && random(3)<1) {direction = 90; exit;}
}
}
else
{
go_ahead = place_free(x,y+vspeed);
go_left = place_free(x+4,y);
go_right = place_free(x-4,y);
if ( !go_ahead && !go_left && !go_right ) {vspeed = -vspeed; exit;}
while (true) //forever
{
if ( go_ahead && random(3)<1) {exit;}
if ( go_left && random(3)<1) {direction = 0; exit;}
if ( go_right && random(3)<1) {direction = 180; exit;}
}
}
}
Random Direction: This sets the enemy to move to any random direction during a collision with a wall
or obstacle.
var set_range;
set_range = choose(0,90,180,270);
// set_range limits direction to only straigth directions
{
motion_set(set_range,2);
exit;
}
set_range = choose(0,90,180,270);
// set_range limits direction to only straigth directions
{
motion_set(set_range,2);
exit;
}
The MovementAI script when set to run when the enemy collides with a wall results in them getting stuck at some
point moving forward.
The enemy chases the player for some time and then gets stuck moving in one direction untill a collision with a wall
or some instance.
The enemy can manover through a maze and walls when the enemy AI is set to run during a collision however they
get stuck again during unusual turns
Also if anyone has any advice on programming enemy AI, some tutorials ( preferably for Game Maker 8 ) or anything that might come of use It would be appreciated if you could post a link or reference.
Also ask if you want to take a look at the game and help me debug the AI and I'll send it to you in a PM.
Thanks.