mp_grid - Closest point on a allowed cell to a forbidden cell

Bingdom

Googledom
Hello GMC!

Today, i have encountered a really tough problem and i need help figuring it out.
So basically i currently have set up my AI path finding using mp_grid, and everything is working fine EXCEPT when i try to set a path to a forbidden cell. I want to know how to make the AI to move to the closest available cell after when the player has clicked on a forbidden cell, i'm unsure how to program an efficient method.

This is what im currently doing for the AI path finding.
Code:
with(OBJ_Troop) {
var t_path = path_add();
        if mp_grid_path(other.map_ai, t_path, x, y, mouse_x, mouse_y, true) {
            path_assign(path, t_path);
            path_start(path,3,path_action_stop,false);
        }
path_delete(t_path);
}
If you're unsure what i mean here is a picture (excuse my bad writing :D)
upload_2016-9-3_22-27-30.png
Also, the tiles are stored in a ds_grid too, in case if it's needed. ;)
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
If I remember correctly, when mp_grid_path returns false, it still builds a path, albeit the one that will go through "solid" cells at some point(s). So you could handle that case and then find the last valid point on it with a loop or during movement itself.
 

Bingdom

Googledom
If I remember correctly, when mp_grid_path returns false, it still builds a path, albeit the one that will go through "solid" cells at some point(s). So you could handle that case and then find the last valid point on it with a loop or during movement itself.
Thanks for your response.

I tried doing what you said, it doesn't seem like it generates a path. Here is the code i tried in case if i made any mistakes (I don't think there is any, just mp_grid_path not generating a path when its false).

Code:
        var t_path = path_add();
        if !mp_grid_path(other.map_ai, t_path, x, y, mouse_x, mouse_y, true) {
        var xx,yy,stop;
        stop = false;
            do {
                xx = path_get_x(t_path,1) div CELL_SIZE;
                yy = path_get_y(t_path,1) div CELL_SIZE;
                show_debug_message("Point in grid: " + string(xx) + "x " + string(yy) + "y");
                if mp_grid_get_cell(other.map_ai,xx,yy) == -1 {
                    path_delete_point(t_path,path_get_number(t_path) - 1);
                        //Failsafe if the path becomes nothing
                        if path_get_length(t_path) <= 0 {
                            stop = true;
                        }
                } else {
                    stop = true;
                }
            } until stop
        }
            path_assign(path, t_path);
            path_start(path,3,path_action_stop,false);
            path_delete(t_path);
        }
The debug message keeps telling me that it reads at point 0, 0 on the mp_grid.
 
T

TimothyAllen

Guest
Best way is to probably write your own A*. Then you have several options:
1: Record the node with the lowest heuristic score and return path to it if no path to the goal is found. (Possibly less effiencent but more simple)

2: (just a thought of mine, never actaully tried this)
Start the pathfinding at the goal node instead of the start node. (A) If no free children nodes, perform A* outward until you find the first (or first set) of free nodes. Select the lowest estimated score of these nodes and restart your original A* from this free node. If no path found, pull the next lowest estimated score for the "free set" of nodes. If this list is depleted, start over at (A).
 
Top