Search code examples
artificial-intelligencepath-findinggame-maker

Game Maker: Studio - Make objects avoid other instances of same object with A* pathfinding


I have a game where there are multiple enemies who must chase a single player. I have pathfinding set up using the GML A* pathfinding with mp_grid and a path. However, these enemies can walk on top of each other when seeking the player. To fix this, I also told the path to ignore enemies as well with mp_grid_add_instances, but then they stop moving altogether because they see themselves as obstacles, thus trapping themselves within a bounding box. Is there a way I can add "all other enemies BUT self" with mp_grid_add_instances?

Here's my grid creation code (in a CONTROLS class for initializing variables):

global.zombie_ai_grid = mp_grid_create(0, 0, room_width / 50, (room_height - sp_dashboard.sprite_height) / 50, 50, 50);
mp_grid_add_instances(global.zombie_ai_grid, obj_obstacle, false);

This is my path initialization code (in Zombie class):

path = path_add();
alarm[0] = 5;

This is my path creation code in Alarm 0 (path updates every 2 seconds):

mp_grid_path(global.zombie_ai_grid, path, x, y, nearest.x, nearest.y, true);
path_set_kind(path, 1);
path_set_precision(path, 8);

path_end();
path_start(path, MAX_SPEED, 0, true);

alarm[0] = room_speed * 2;

Solution

  • You need to clear the cell for each enemy before calculating a path and set back after. Like this:

    mp_grid_clear_cell(global.zombie_ai_grid, x div CELL_SIZE, y div CELL_SIZE);
    mp_grid_path(global.zombie_ai_grid, path, x, y, nearest.x, nearest.y, true);
    mp_grid_add_cell(global.zombie_ai_grid, x div CELL_SIZE, y div CELL_SIZE);
    

    But note that the two enemies must not have collision (located in the same cell), otherwise that the enemy will not be marked as an obstacle.


    Other way:

    mp_grid_clear_all(global.zombie_ai_grid);
    with (obj_obstacle)
    {
        if (id != other.id)
        {
            mp_grid_add_instances(global.zombie_ai_grid, id, false);
        }
    }
    

    If this will work too slowly, then you need to make a queue and every step you need to calculate the path for only a few zombies.