#Stuck Pathfinding

1 messages · Page 1 of 1 (latest)

hybrid acorn
#

I'm creating an AI with pathfinding in my game, but I'm going a bit CRAZY because I can't find a way to make it redo the path when the point of new_x and new_y are inside a wall or in an inaccessible room (I think this is the problem)

#

CREATE EVENT:
`randomize();
pdead = false;
image_xscale = 1;
image_yscale = 1;
velocity = random_range(0.4, 0.65);
initial_velocity = velocity;
spot_velocity = random_range(0.15, 0.3);
hp = irandom_range(2,3);
last_x = x;
last_y = y;
if (!variable_global_exists("enemygrid")) {
var w = room_width div 16;
var h = room_height div 16;
global.enemygrid = mp_grid_create(0, 0, w, h, 16, 16);
mp_grid_add_instances(global.enemygrid, obj_solid, true);
}

path = path_add();

target_x = obj_player.x;
target_y = obj_player.y;

alarm[0] = 1;
p_run = 8;
immortality = false;
memory = 0;
max_memory = irandom_range(200, 300);

wander_target_x = x;
wander_target_y = y;
wander_distance = 80;
rst = irandom_range(20, 45);
spot_time = rst;

role = choose("bruiser", "gunner");
if role == "gunner" {
gun = instance_create_depth(x,y, 1, obj_enemygun);
gun.enemy = id;
}

if role == "bruiser" {
distance = 2;
} else {
distance = 10;
}

stuntimer = 0;`

#

ALARM 0
`path_delete(path);
path = path_add();

mp_grid_clear_all(global.enemygrid);
mp_grid_add_instances(global.enemygrid, obj_solid, true);

if (memory > 0 && global.hp > 0) {
if role == "bruiser" {
distance = 2;
} else {
distance = 10;
}
if (distance_to_object(obj_player) > distance) {
if (mp_grid_path(global.enemygrid, path, x, y, target_x, target_y, true)) {
path_start(path, velocity, path_action_stop, true);
}
}
} else {

if (point_distance(x, y, wander_target_x, wander_target_y) < 8) && (memory == 0) {
var angle = irandom(359);
var new_x = x + lengthdir_x(wander_distance, angle);
var new_y = y + lengthdir_y(wander_distance, angle);

    if (!position_meeting(new_x, new_y, obj_solid)) {
        wander_target_x = clamp(new_x, 0, room_width);
        wander_target_y = clamp(new_y, 0, room_height);
    }
}

if (mp_grid_path(global.enemygrid, path, x, y, wander_target_x, wander_target_y, true)) {
    path_start(path, velocity, path_action_stop, true);

}
}

alarm_set(0, 8);`

grizzled topaz
#

You can simply cancel starting new path if there are solid objects in the way. collision_line() should do the trick:

if (point_distance(x, y, wander_target_x, wander_target_y) < 8) && (memory == 0) {
var angle = irandom(359);
var new_x = x + lengthdir_x(wander_distance, angle);
var new_y = y + lengthdir_y(wander_distance, angle);

if (collision_line(x, y, new_x, new_y, obj_solid, 0, 0) != noone) {
    alarm_set(0, 8);
    exit;
}
    
    if (!position_meeting(new_x, new_y, obj_solid)) {
        wander_target_x = clamp(new_x, 0, room_width);
        wander_target_y = clamp(new_y, 0, room_height);
    }
}
#

You might also want to take a look at mp_potential_path_object() function

hollow lily
#

from my experience mp_potential_path_object() gets stuck in corners, so the path thing is better in my oppinion

#

oh i'm late excuse me

hybrid acorn
#

and sometimes they even flash when they walk, I don't know why, in the sense that either when they go down or up they flip continuously, or it's as if they "vibrate" when they walk

grizzled topaz
hybrid acorn