Nyymi
Member
I'm trying to create a path finding function to get a list of provinces (tile_id) from start to target. This one works fine until you add actual obstacles when will loop forever. I've tried to rewrite this many times but just can't wrap my mind around it.
I've been using point_distance to jump in the next one but this obviously doesn't work with barriers. My attempts to filter out dead ends haven't worked. Any advice on how to approach this?
About the provinces:
Each province has x, y and a unique string identifier stored as string (tile_id) in a JSON file. They're actually all a top of each other on the room.
I've been using point_distance to jump in the next one but this obviously doesn't work with barriers. My attempts to filter out dead ends haven't worked. Any advice on how to approach this?
About the provinces:
Each province has x, y and a unique string identifier stored as string (tile_id) in a JSON file. They're actually all a top of each other on the room.
GML:
function get_path_to_province(_tile_id, _target_tile_id, _war_list, _name){ // tile id as string
var path_to_target_lis = ds_list_create();
var _target_x = global.data[? _target_tile_id][? "x"],
_target_y = global.data[? _target_tile_id][? "y"],
_found = false;
while (!_found){
var _lis = global.data[? _tile_id][? "path"],
_lis_len = ds_list_size(_lis),
_last_distance = 0,
_next_tile = _lis[| 0];
for (var i = 0; i < _lis_len; i++;){
var tile = _lis[| i],
_id = global.province_id_list[? tile];
if _id.owner == _name or ds_list_find_index(_war_list, _id.owner) != -1{//check if can move to province
var _distance = point_distance(global.data[? tile][? "x"], global.data[? tile][? "y"], _target_x, _target_y);
if _last_distance > _distance or _last_distance == 0{
_last_distance = _distance;
_next_tile = tile;
}
if tile == _target_tile_id{
_next_tile = tile;
_found = true;
break;
}
}
// Change province to new tile for next loop
}//end for
if _tile_id != _next_tile{
_tile_id = _next_tile;
ds_list_add(path_to_target_lis, _next_tile);
}else{// no next step was found
_found = true;
break;
}
}//end of while
return path_to_target_lis;
}
Attachments
-
22.3 KB Views: 14