]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/pathlib/path_waypoint.qc
Merge commit 'origin/tzork/pathlib'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / pathlib / path_waypoint.qc
1 .entity list;
2
3 entity pathlib_wpp_goal;
4 entity pathlib_wpp_start;
5
6 void pathlib_wpp_close(entity wp)
7 {
8     --pathlib_open_cnt;
9     ++pathlib_closed_cnt;
10
11     wp.list = closedlist;
12     wp.colormod = '0 1 1';
13
14     if(wp == best_open_node)
15         best_open_node = world;
16
17     if(wp == pathlib_wpp_goal)
18         pathlib_foundgoal = TRUE;
19 }
20
21 float pathlib_wpp_open(entity wp, entity child, float cost)
22 {
23     float g, h, f;
24
25     if(child.list == closedlist)
26         return FALSE;
27
28     g = wp.pathlib_node_g + vlen(child.origin - wp.origin); //cost;
29     h = vlen(child.origin - pathlib_wpp_goal.origin);
30     f = g + h;
31
32
33     child.colormod = '1 0 1';
34     child.path_prev = wp;
35     child.list = openlist;
36     child.pathlib_node_g = g;
37     child.pathlib_node_h = h;
38     child.pathlib_node_f = f;
39
40     ++pathlib_open_cnt;
41
42     if(best_open_node.pathlib_node_f > f)
43         best_open_node = child;
44
45     return TRUE;
46 }
47
48 float pathlib_wpp_expand(entity wp)
49 {
50
51     if(wp.wp00) pathlib_wpp_open(wp,wp.wp00,wp.wp00mincost); else return 0;
52     if(wp.wp01) pathlib_wpp_open(wp,wp.wp01,wp.wp01mincost); else return 1;
53     if(wp.wp02) pathlib_wpp_open(wp,wp.wp02,wp.wp02mincost); else return 2;
54     if(wp.wp03) pathlib_wpp_open(wp,wp.wp03,wp.wp03mincost); else return 3;
55     if(wp.wp04) pathlib_wpp_open(wp,wp.wp04,wp.wp04mincost); else return 4;
56     if(wp.wp05) pathlib_wpp_open(wp,wp.wp05,wp.wp05mincost); else return 5;
57     if(wp.wp06) pathlib_wpp_open(wp,wp.wp06,wp.wp06mincost); else return 6;
58     if(wp.wp07) pathlib_wpp_open(wp,wp.wp07,wp.wp07mincost); else return 7;
59     if(wp.wp08) pathlib_wpp_open(wp,wp.wp08,wp.wp08mincost); else return 8;
60     if(wp.wp09) pathlib_wpp_open(wp,wp.wp09,wp.wp09mincost); else return 9;
61     if(wp.wp10) pathlib_wpp_open(wp,wp.wp10,wp.wp10mincost); else return 10;
62     if(wp.wp11) pathlib_wpp_open(wp,wp.wp11,wp.wp11mincost); else return 11;
63     if(wp.wp12) pathlib_wpp_open(wp,wp.wp12,wp.wp12mincost); else return 12;
64     if(wp.wp13) pathlib_wpp_open(wp,wp.wp13,wp.wp13mincost); else return 13;
65     if(wp.wp14) pathlib_wpp_open(wp,wp.wp14,wp.wp14mincost); else return 14;
66     if(wp.wp15) pathlib_wpp_open(wp,wp.wp15,wp.wp15mincost); else return 15;
67     if(wp.wp16) pathlib_wpp_open(wp,wp.wp16,wp.wp16mincost); else return 16;
68     if(wp.wp17) pathlib_wpp_open(wp,wp.wp17,wp.wp17mincost); else return 17;
69     if(wp.wp18) pathlib_wpp_open(wp,wp.wp18,wp.wp18mincost); else return 18;
70     if(wp.wp19) pathlib_wpp_open(wp,wp.wp19,wp.wp19mincost); else return 19;
71     if(wp.wp20) pathlib_wpp_open(wp,wp.wp20,wp.wp20mincost); else return 20;
72     if(wp.wp21) pathlib_wpp_open(wp,wp.wp21,wp.wp21mincost); else return 21;
73     if(wp.wp22) pathlib_wpp_open(wp,wp.wp22,wp.wp22mincost); else return 22;
74     if(wp.wp23) pathlib_wpp_open(wp,wp.wp23,wp.wp23mincost); else return 23;
75     if(wp.wp24) pathlib_wpp_open(wp,wp.wp24,wp.wp24mincost); else return 24;
76     if(wp.wp25) pathlib_wpp_open(wp,wp.wp25,wp.wp25mincost); else return 25;
77     if(wp.wp26) pathlib_wpp_open(wp,wp.wp26,wp.wp26mincost); else return 26;
78     if(wp.wp27) pathlib_wpp_open(wp,wp.wp27,wp.wp27mincost); else return 27;
79     if(wp.wp28) pathlib_wpp_open(wp,wp.wp28,wp.wp28mincost); else return 28;
80     if(wp.wp29) pathlib_wpp_open(wp,wp.wp29,wp.wp29mincost); else return 29;
81     if(wp.wp30) pathlib_wpp_open(wp,wp.wp30,wp.wp30mincost); else return 30;
82     if(wp.wp31) pathlib_wpp_open(wp,wp.wp31,wp.wp31mincost); else return 31;
83
84 }
85
86 entity pathlib_wpp_bestopen()
87 {
88     entity n, best;
89
90     if(best_open_node)
91         return best_open_node;
92
93     n = findchainentity(list, openlist);
94     /*
95     if not(n)
96     {
97         dprint("pathlib_waypointpath: No more open nodes, path fail.\n");
98         return world;
99     }
100     */
101
102     best = n;
103     while(n)
104     {
105         if(n.pathlib_node_f < best.pathlib_node_f)
106             best = n;
107
108         n = n.chain;
109     }
110
111     return best;
112
113 }
114
115 entity pathlib_waypointpath(entity from, entity to)
116 {
117     entity n;
118     float ptime;
119
120
121     ptime = gettime(GETTIME_REALTIME);
122     pathlib_starttime = ptime;
123
124     if not(openlist)
125         openlist       = spawn();
126
127     if not(closedlist)
128         closedlist     = spawn();
129
130     pathlib_closed_cnt       = 0;
131     pathlib_open_cnt         = 0;
132     pathlib_searched_cnt     = 0;
133
134     pathlib_foundgoal      = FALSE;
135
136
137     dprint("pathlib_waypointpath init\n");
138
139     // Initialize waypoint grid
140     n = findchainentity(owner, waypoint_master);
141     while (n)
142     {
143         n.list = world;
144         n.pathlib_node_g = 0;
145         n.pathlib_node_f = 0;
146         n.pathlib_node_h = 0;
147
148         setmodel(n, "models/runematch/rune.mdl");
149         n.effects = EF_LOWPRECISION;
150         n.colormod = '0 0 0';
151         n.scale = 1;
152
153         n = n.chain;
154     }
155
156     pathlib_wpp_goal  = to;
157     pathlib_wpp_start = from;
158
159     from.list = closedlist;
160     bprint("Expanding ",ftos(pathlib_wpp_expand(from))," links\n");
161     if(pathlib_open_cnt <= 0)
162     {
163         dprint("pathlib_waypointpath: Start waypoint not linked! aborting.\n");
164         return world;
165     }
166
167     return world;
168 }
169
170 entity pathlib_waypointpath_step()
171 {
172     entity n;
173
174
175     /*
176     if((gettime(GETTIME_REALTIME) - pathlib_starttime) > pathlib_maxtime)
177     {
178         dprint("pathlib_waypointpath: Path took to long to compute!\n");
179         return world;
180     }
181     */
182
183     n = pathlib_wpp_bestopen();
184     if(!n)
185     {
186         bprint("Cannot find best open node, abort.\n");
187         return world;
188     }
189     pathlib_wpp_close(n);
190
191     if(pathlib_foundgoal)
192     {
193         bprint("Target found. Rebuilding and filtering path...\n");
194         // to-do rebuild (follow path_prev from n untill it hits from) and return path.
195
196         n = pathlib_wpp_goal;
197         while(n != pathlib_wpp_start)
198         {
199             n.scale = 3;
200             n = n.path_prev;
201         }
202         pathlib_wpp_start.colormod = '0 0 1';
203         pathlib_wpp_goal.colormod = '1 0 0';
204
205         return n;
206     }
207
208     bprint("Expanding ",ftos(pathlib_wpp_expand(n))," links\n");
209
210
211     return world;
212 }
213
214 void plas_think()
215 {
216     pathlib_waypointpath_step();
217     if(pathlib_foundgoal)
218         return;
219     self.nextthink = time + 0.1;
220 }
221
222 void pathlib_waypointpath_autostep()
223 {
224     entity n;
225     n = spawn();
226     n.think = plas_think;
227     n.nextthink = time + 0.1;
228 }
229
230 /*
231 entity pathlib_wpp_goal;
232 entity pathlib_wpp_start;
233 entity pathlib_waypointpath(entity from, entity to)
234 {
235     entity path, start, end, open, n, ln;
236     float ptime, ftime, ctime;
237
238
239     ptime = gettime(GETTIME_REALTIME);
240     pathlib_starttime = ptime;
241
242     if not(openlist)
243         openlist       = spawn();
244
245     if not(closedlist)
246         closedlist     = spawn();
247
248     pathlib_closed_cnt       = 0;
249     pathlib_open_cnt         = 0;
250     pathlib_searched_cnt     = 0;
251
252     pathlib_foundgoal      = FALSE;
253
254
255     dprint("pathlib_waypointpath init\n");
256
257     // Initialize waypoint grid
258     n = findchainentity(owner, waypoint_master);
259     while (n)
260     {
261         n.list = world;
262         n.pathlib_node_g = 0;
263         n.pathlib_node_f = 0;
264         n.pathlib_node_h = 0;
265         setmodel(n, "models/runematch/rune.mdl");
266         n.effects = EF_LOWPRECISION;
267         n.colormod = '1 1 1';
268         n.scale = 1;
269     }
270
271     pathlib_wpp_goal = to;
272     pathlib_wpp_start = from;
273
274     from.list = closedlist;
275     pathlib_wpp_expand(from);
276     if(pathlib_open_cnt <= 0)
277     {
278         dprint("pathlib_waypointpath: Start waypoint not linked! aborting.\n");
279         return world;
280     }
281
282     while(pathlib_open_cnt)
283     {
284         if((gettime(GETTIME_REALTIME) - pathlib_starttime) > pathlib_maxtime)
285         {
286             dprint("pathlib_waypointpath: Path took to long to compute!\n");
287             return world;
288         }
289
290         n = findentity(world, list, openlist);
291         if not(n)
292         {
293             dprint("pathlib_waypointpath: No more open nodes, path fail.\n");
294             return world;
295         }
296
297         pathlib_wpp_close(n);
298         pathlib_wpp_expand(n);
299
300         if(pathlib_foundgoal)
301         {
302             dprint("Target found. Rebuilding and filtering path...\n");
303             // to-do rebuild (follow path_prev from n untill it hits from) and return path.
304             n = to;
305             do
306             {
307                 n.scale = 3;
308                 n = n.path_prev;
309             }while(n != from);
310
311             return world;
312         }
313     }
314
315     return world;
316 }
317 */