-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwayfinder.nut
343 lines (272 loc) · 8.97 KB
/
wayfinder.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//////////////////////////////////////////////////////////////////////
// //
// CluelessPlus - an noai-AI //
// //
//////////////////////////////////////////////////////////////////////
//
// Author: Zuu (Leif Linse), user Zuu @ tt-forums.net
// Purpose: To play around with the noai framework.
// - Not to make the next big thing.
// Copyright: Leif Linse - 2008-2011
// License: GNU GPL - version 2
// WayFinder is an unfinished attempt to create a path finder that tries
// to identify obstacles and then tries to find a way inbetween the
// obstacles.
//// CLASS: WayFinder ////
/*class WFPathNode
{
parent = null;
tile = null;
constructor(tile, parent)
{
this.tile = tile;
this.parent = parent;
}
function GetParent();
function GetTile();
}
function WFPathNode::GetParent()
{
return parent;
}
function WFPathNode::GetTile()
{
return tile;
}*/
class WFLink
{
from = null;
to = null;
has_obstacle = false;
tiles = null;
constructor(from, to, hasObstacle)
{
this.from = from;
this.to = to;
this.has_obstacle = hasObstacle;
this.tiles = [];
}
}
//// CLASS: WayFinder ////
class WayFinder
{
from = null;
to = null;
from_x = null;
from_y = null;
to_x = null;
to_y = null;
pf_state = null;
PF_STATE_FIND_OBSTACLES = null;
PF_STATE_FIND_PATH = null;
transport_mode = null;
TM_RAIL = null;
TM_ROAD = null;
obstacles = null;
link_list = null;
scan_area = null;
scan_area_from_tile = null;
scan_area_to_tile = null;
num_optimal_path_tiles = null;
num_obstacle_tiles = null;
find_obstacles_data = null;
constructor(transportMode)
{
this.from = -1;
this.to = -1;
this.PF_STATE_FIND_OBSTACLES = "find obstacles";
this.PF_STATE_FIND_PATH = "find path";
this.pf_state = PF_STATE_FIND_OBSTACLES;
this.TM_ROAD = "road";
this.TM_RAIL = "rail";
this.transport_mode = transportMode;
this.obstacles = [];
this.link_list = [];
this.scan_area = null;
this.scan_area_from_tile = -1;
this.scan_area_to_tile = -1;
this.num_optimal_path_tiles = 0;
this.num_obstacle_tiles = 0;
this.find_obstacles_data = null; // for storing a table with data used in PF_STATE_FIND_OBSTACLES
}
function InitializePath(from, to);
function FindPath(ticks);
}
function WayFinder::InitializePath(from, to)
{
Helper.ClearAllSigns();
// set from/to
this.from = from[0];
this.to = to[0];
// Create scan_area tile rect
from_x = AIMap.GetTileX(this.from);
to_x = AIMap.GetTileX(this.to);
from_y = AIMap.GetTileY(this.from);
to_y = AIMap.GetTileY(this.to);
local x_min = Helper.Min(from_x, to_x);
local y_min = Helper.Min(from_y, to_y);
local x_max = Helper.Max(from_x, to_x);
local y_max = Helper.Max(from_y, to_y);
// increase the rect by 10 in each direction, but not beyond the map edge
local enlarge_by = 10;
x_min = Helper.Clamp(x_min - enlarge_by, 0, AIMap.GetMapSizeX() - 1);
x_max = Helper.Clamp(x_max + enlarge_by, 0, AIMap.GetMapSizeX() - 1);
y_min = Helper.Clamp(y_min - enlarge_by, 0, AIMap.GetMapSizeY() - 1);
y_max = Helper.Clamp(y_max + enlarge_by, 0, AIMap.GetMapSizeY() - 1);
AILog.Info("x_min = " + x_min);
AILog.Info("x_max = " + x_max);
AILog.Info("y_min = " + y_min);
AILog.Info("y_max = " + y_max);
scan_area_from_tile = AIMap.GetTileIndex(x_min, y_min);
scan_area_to_tile = AIMap.GetTileIndex(x_max, y_max);
scan_area = AITileList();
scan_area.AddRectangle(scan_area_from_tile, scan_area_to_tile);
// set pf_state
pf_state = PF_STATE_FIND_OBSTACLES;
// debug output
Helper.SetSign(this.from, "From");
Helper.SetSign(this.to, "To");
Helper.SetSign(AIMap.GetTileIndex(x_min, y_min), "<>");
Helper.SetSign(AIMap.GetTileIndex(x_max, y_min), "<>");
Helper.SetSign(AIMap.GetTileIndex(x_min, y_max), "<>");
Helper.SetSign(AIMap.GetTileIndex(x_max, y_max), "<>");
AILog.Info("WayFinder initialized");
}
function IsObstacleTile(tile)
{
// Rail or road tiles are not seen as obstacles as they can usually be bridged/tunneled
return !AITile.IsBuildable(tile) && !AIRoad.IsRoadTile(tile) && !AIRail.IsRailTile(tile);
}
function WayFinder::FindObstacles_NextTile(next_tile, debug_sign_label)
{
// Move prev + curr tile forward
find_obstacles_data.prev_tile = find_obstacles_data.curr_tile;
find_obstacles_data.curr_tile = next_tile;
// Display debug sign
Helper.SetSign( find_obstacles_data.curr_tile, debug_sign_label );
// Has the obstacle state (existence of an obstacle at tile) of current tile changed since last tile?
local is_obstacle = IsObstacleTile(find_obstacles_data.curr_tile);
if(is_obstacle != find_obstacles_data.in_obstacle)
{
// Yes - end current link and start a new one
local new_link = WFLink(null, null, is_obstacle);
if(find_obstacles_data.in_obstacle)
{
// Exiting obstacle -> put link connection point at current tile (just after obstacle)
find_obstacles_data.curr_link.to = find_obstacles_data.curr_tile;
find_obstacles_data.curr_link.tiles.append(find_obstacles_data.curr_tile);
new_link.from = find_obstacles_data.curr_tile;
}
else
{
// Entering an obstacle -> put link connection point before current tile (just before obstacle)
find_obstacles_data.curr_link.to = find_obstacles_data.prev_tile;
new_link.from = find_obstacles_data.prev_tile;
new_link.tiles.append(find_obstacles_data.prev_tile);
}
new_link.tiles.append(find_obstacles_data.curr_tile);
link_list.append(find_obstacles_data.curr_link);
find_obstacles_data.curr_link = new_link;
find_obstacles_data.in_obstacle = is_obstacle;
}
else
{
// No obstacle state change -> Just continue on current link by adding curr_tile to list of all tiles in that link
find_obstacles_data.curr_link.tiles.append(find_obstacles_data.curr_tile);
}
// Count number of tiles + obstacle tiles
find_obstacles_data.num_tiles++;
if(is_obstacle) find_obstacles_data.num_obstacle_tiles++;
if(is_obstacle) Helper.SetSign( find_obstacles_data.curr_tile, "o" );
}
function WayFinder::FindPath(ticks)
{
local start_tick = AIController.GetTick();
while(ticks == -1 || start_tick + ticks > AIController.GetTick())
{
if(pf_state == PF_STATE_FIND_OBSTACLES)
{
// Walk through the optimal path
if(find_obstacles_data == null)
{
// Init data structure for this PF state
find_obstacles_data = {
dir_x = from_x < to_x? 1 : -1,
dir_y = from_y < to_y? 1 : -1,
x = from_x,
y = from_y,
prev_tile = -1,
curr_tile = AIMap.GetTileIndex(from_x, from_y),
in_obstacle = null,
curr_link = null,
num_tiles = 0,
num_obstacle_tiles = 0};
find_obstacles_data.in_obstacle = IsObstacleTile(find_obstacles_data.curr_tile);
find_obstacles_data.curr_link = WFLink(find_obstacles_data.curr_tile, null, find_obstacles_data.in_obstacle);
// Add start tile to tile list of first link
find_obstacles_data.curr_link.tiles.append(find_obstacles_data.curr_link.from);
}
// Get the optimal path (only consider obstacles on the optimal path)
//local optimal_path_tiles = AITileList();
// shorter data table pointer name
local data = find_obstacles_data;
while(data.x != to_x)
{
data.x += data.dir_x;
FindObstacles_NextTile(AIMap.GetTileIndex(data.x, data.y), "x");
// Rail can go diagonal
if(this.transport_mode == TM_RAIL && data.y != to_y)
{
data.y += data.dir_y;
FindObstacles_NextTile(AIMap.GetTileIndex(data.x, data.y), "y2");
}
}
while(data.y != to_y)
{
data.y += data.dir_y;
FindObstacles_NextTile(AIMap.GetTileIndex(data.x, data.y), "y");
}
// Add last tile and terminate last link
FindObstacles_NextTile(AIMap.GetTileIndex(data.x, data.y), ".");
data.curr_link.to = data.curr_tile;
link_list.append(data.curr_link);
data.curr_link = null;
AILog.Info("Num tiles " + data.num_tiles);
AILog.Info("Num obstacle tiles " + data.num_obstacle_tiles);
AILog.Info("Num links " + link_list.len());
/*
// Check which optimal path tiles that have an obstacle
local obstacle_tiles = AITileList();
obstacle_tiles.AddList(optimal_path_tiles);
obstacle_tiles.Valuate(IsObstacleTile);
obstacle_tiles.KeepValue(0);
*/
foreach(link in this.link_list)
{
AILog.Info("Link start: " + Tile.GetTileString(link.from));
AILog.Info("Link to: " + Tile.GetTileString(link.to));
AILog.Info("Link has_obstacle: " + link.has_obstacle);
local obstacle_observed = false;
foreach(tile in link.tiles)
{
Helper.SetSign(tile, ".");
if(IsObstacleTile(tile))
{
obstacle_observed = true;
//break;
}
}
Helper.SetSign(link.from, "f");
Helper.SetSign(link.to, "t");
AILog.Info("Link obstacle_observed: " + link.has_obstacle);
AILog.Info("----------------------------------------------");
}
pf_state = PF_STATE_FIND_PATH;
find_obstacles_data = null; // allow the memory used for this pf_state to be freed.
}
else if(pf_state == PF_STATE_FIND_PATH)
{
}
}
return true;
}