T
trentallain
Guest
What I was previously doing was generating a path between an AI unit and an opposing unit, however I now need to make the AI path smarter by making it so:
1. The number of path points isn't greater than the unit's MP (MP is the maximum amount of squares it can move).
2. The AI unit should try and get as close as possible to its closest target (even if the target is surrounded already) (-1 on global.grid signifies that it is a free position, anything other than that means the position is not free).
3. The AI unit shouldn't move any closer if its target is already within its range
If any of these issues occur in the path, I store that position so that I can delete all the path points after it occurs. It should find the first point where the path breaks.
This is my code of me trying to do it:
This code works as expected for generating a basic path, but only if I remove the parts where I find if the path breaks and where I delete the remaining points.
But when the full code is run, including those parts, my AI doesn't do anything at all.
And if I only use the i > mp part and not the other 2 situations, then my AI can move diagonally and doesn't appear to consider the amount of MP.
Could someone help me try and solve this please? So that it will cut the path where any of those 3 things occur and then deletes the remaining points.
1. The number of path points isn't greater than the unit's MP (MP is the maximum amount of squares it can move).
2. The AI unit should try and get as close as possible to its closest target (even if the target is surrounded already) (-1 on global.grid signifies that it is a free position, anything other than that means the position is not free).
3. The AI unit shouldn't move any closer if its target is already within its range
If any of these issues occur in the path, I store that position so that I can delete all the path points after it occurs. It should find the first point where the path breaks.
This is my code of me trying to do it:
Code:
// Clear path points
path_clear_points(ai_path);
// Create the path regardless if you can fully traverse it
mp_grid_path(_ai_mp_grid, ai_path, grid_x * grid.size + 10, grid_y * grid.size + 10, _ai_target.grid_x * grid.size + 10, _ai_target.grid_y * grid.size + 10, false)
// Delete end point of the path (because this is where the target is)
path_delete_point(ai_path, path_get_number(ai_path) - 1);
// Delete points that are either exceeding MP or are blocked
var _path_end = -1;
// Find where/if the path breaks
for (var i = 0; i < path_get_number(ai_path); ++i) {
// Get path's grid coordinates at this position
var _xpos = path_get_point_x(ai_path, i) div grid.size;
var _ypos = path_get_point_y(ai_path, i) div grid.size;
// Check if target is in range yet
var _in_range = point_distance(_xpos, _ypos, ai_target.x, ai_target.y) < range;
// If the path is larger than MP, or the position is taken on the grid, or the target is now within range
if i > mp || global.grid[# _xpos, _ypos] != -1 || _in_range {
// Set this point as the point to end the path at
_path_end = i;
break;
}
}
// If the path has broken somewhere
if _path_end != -1 {
for (var i = _path_end; i < path_get_number(ai_path); ++i) {
// Delete all remaining points
path_delete_point(ai_path, i);
}
}
But when the full code is run, including those parts, my AI doesn't do anything at all.
And if I only use the i > mp part and not the other 2 situations, then my AI can move diagonally and doesn't appear to consider the amount of MP.
Could someone help me try and solve this please? So that it will cut the path where any of those 3 things occur and then deletes the remaining points.