Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / pathlib / path_waypoint.qc
1 #include "../bot/waypoints.qh"
2
3 #include "pathlib.qh"
4 #include "main.qh"
5
6 var float pathlib_wpp_open(entity wp, entity child, float cost);
7
8 void pathlib_wpp_close(entity wp)
9 {
10     --pathlib_open_cnt;
11     ++pathlib_closed_cnt;
12
13     wp.pathlib_list = closedlist;
14
15     if(wp == best_open_node)
16         best_open_node = world;
17
18     if(wp == goal_node)
19         pathlib_foundgoal = true;
20 }
21
22 float pathlib_wpp_opencb(entity wp, entity child, float cost)
23 {
24
25     if(child.pathlib_list == closedlist)
26         return false;
27
28         // FIXME! wp.wp##mincost is NOT distance. Make it distance or add a field for distance to be used here (for better speed)
29         cost = vlen(child.origin - wp.origin);
30
31     child.path_prev      = wp;
32     child.pathlib_list   = openlist;
33     child.pathlib_node_g = wp.pathlib_node_g + cost;
34     child.pathlib_node_h = pathlib_heuristic(child.origin, goal_node.origin);
35     child.pathlib_node_c = pathlib_wpp_waypointcallback(child, wp);
36     child.pathlib_node_f = child.pathlib_node_g + child.pathlib_node_h + child.pathlib_node_c;
37
38
39     if(child == goal_node)
40         pathlib_foundgoal = true;
41
42     ++pathlib_open_cnt;
43
44     if(best_open_node.pathlib_node_f > child.pathlib_node_f)
45         best_open_node = child;
46
47     return true;
48 }
49
50 float pathlib_wpp_openncb(entity wp, entity child, float cost)
51 {
52
53     if(child.pathlib_list == closedlist)
54         return false;
55
56         // FIXME! wp.wp##mincost is NOT distance. Make it distance or add a field for distance to be used here (for better speed)
57         cost = vlen(child.origin - wp.origin);
58
59     child.path_prev      = wp;
60     child.pathlib_list   = openlist;
61     child.pathlib_node_g = wp.pathlib_node_g + cost;
62     child.pathlib_node_h = pathlib_heuristic(child.origin, goal_node.origin);
63     child.pathlib_node_f = child.pathlib_node_g + child.pathlib_node_h;
64
65     if(child == goal_node)
66         pathlib_foundgoal = true;
67
68     ++pathlib_open_cnt;
69
70     if(best_open_node.pathlib_node_f > child.pathlib_node_f)
71         best_open_node = child;
72
73     return true;
74 }
75
76 float pathlib_wpp_expand(entity wp)
77 {
78     if(wp.wp00) pathlib_wpp_open(wp,wp.wp00,wp.wp00mincost); else return 0;
79     if(wp.wp01) pathlib_wpp_open(wp,wp.wp01,wp.wp01mincost); else return 1;
80     if(wp.wp02) pathlib_wpp_open(wp,wp.wp02,wp.wp02mincost); else return 2;
81     if(wp.wp03) pathlib_wpp_open(wp,wp.wp03,wp.wp03mincost); else return 3;
82     if(wp.wp04) pathlib_wpp_open(wp,wp.wp04,wp.wp04mincost); else return 4;
83     if(wp.wp05) pathlib_wpp_open(wp,wp.wp05,wp.wp05mincost); else return 5;
84     if(wp.wp06) pathlib_wpp_open(wp,wp.wp06,wp.wp06mincost); else return 6;
85     if(wp.wp07) pathlib_wpp_open(wp,wp.wp07,wp.wp07mincost); else return 7;
86     if(wp.wp08) pathlib_wpp_open(wp,wp.wp08,wp.wp08mincost); else return 8;
87     if(wp.wp09) pathlib_wpp_open(wp,wp.wp09,wp.wp09mincost); else return 9;
88     if(wp.wp10) pathlib_wpp_open(wp,wp.wp10,wp.wp10mincost); else return 10;
89     if(wp.wp11) pathlib_wpp_open(wp,wp.wp11,wp.wp11mincost); else return 11;
90     if(wp.wp12) pathlib_wpp_open(wp,wp.wp12,wp.wp12mincost); else return 12;
91     if(wp.wp13) pathlib_wpp_open(wp,wp.wp13,wp.wp13mincost); else return 13;
92     if(wp.wp14) pathlib_wpp_open(wp,wp.wp14,wp.wp14mincost); else return 14;
93     if(wp.wp15) pathlib_wpp_open(wp,wp.wp15,wp.wp15mincost); else return 15;
94     if(wp.wp16) pathlib_wpp_open(wp,wp.wp16,wp.wp16mincost); else return 16;
95     if(wp.wp17) pathlib_wpp_open(wp,wp.wp17,wp.wp17mincost); else return 17;
96     if(wp.wp18) pathlib_wpp_open(wp,wp.wp18,wp.wp18mincost); else return 18;
97     if(wp.wp19) pathlib_wpp_open(wp,wp.wp19,wp.wp19mincost); else return 19;
98     if(wp.wp20) pathlib_wpp_open(wp,wp.wp20,wp.wp20mincost); else return 20;
99     if(wp.wp21) pathlib_wpp_open(wp,wp.wp21,wp.wp21mincost); else return 21;
100     if(wp.wp22) pathlib_wpp_open(wp,wp.wp22,wp.wp22mincost); else return 22;
101     if(wp.wp23) pathlib_wpp_open(wp,wp.wp23,wp.wp23mincost); else return 23;
102     if(wp.wp24) pathlib_wpp_open(wp,wp.wp24,wp.wp24mincost); else return 24;
103     if(wp.wp25) pathlib_wpp_open(wp,wp.wp25,wp.wp25mincost); else return 25;
104     if(wp.wp26) pathlib_wpp_open(wp,wp.wp26,wp.wp26mincost); else return 26;
105     if(wp.wp27) pathlib_wpp_open(wp,wp.wp27,wp.wp27mincost); else return 27;
106     if(wp.wp28) pathlib_wpp_open(wp,wp.wp28,wp.wp28mincost); else return 28;
107     if(wp.wp29) pathlib_wpp_open(wp,wp.wp29,wp.wp29mincost); else return 29;
108     if(wp.wp30) pathlib_wpp_open(wp,wp.wp30,wp.wp30mincost); else return 30;
109     if(wp.wp31) pathlib_wpp_open(wp,wp.wp31,wp.wp31mincost); else return 31;
110
111     return 32;
112 }
113
114 entity pathlib_wpp_bestopen()
115 {
116     entity n, best;
117
118     if(best_open_node)
119         return best_open_node;
120
121     n = findchainentity(pathlib_list, openlist);
122     best = n;
123     while(n)
124     {
125         if(n.pathlib_node_f < best.pathlib_node_f)
126             best = n;
127
128         n = n.chain;
129     }
130
131     return best;
132
133 }
134
135 entity pathlib_waypointpath(entity wp_from, entity wp_to, float callback)
136 {
137     entity n;
138     float ptime;
139
140     ptime                                       = gettime(GETTIME_REALTIME);
141     pathlib_starttime           = ptime;
142         pathlib_movecost                = 300;
143         pathlib_movecost_diag   = vlen('1 1 0' * pathlib_movecost);
144
145         if (!pathlib_wpp_waypointcallback)
146                 callback = false;
147
148         if (callback)
149                 pathlib_wpp_open = pathlib_wpp_opencb;
150         else
151                 pathlib_wpp_open = pathlib_wpp_openncb;
152
153         pathlib_heuristic = pathlib_h_none;
154
155     if (!openlist)
156         openlist       = spawn();
157
158     if (!closedlist)
159         closedlist     = spawn();
160
161     pathlib_closed_cnt       = 0;
162     pathlib_open_cnt         = 0;
163     pathlib_searched_cnt     = 0;
164     pathlib_foundgoal      = false;
165
166     LOG_TRACE("pathlib_waypointpath init\n");
167
168     // Initialize waypoint grid
169     // FIXME! presisted chain for better preformance
170     for(n = findchain(classname, "waypoint"); n; n = n.chain)
171     {
172         n.pathlib_list = world;
173         n.pathlib_node_g = 0;
174         n.pathlib_node_f = 0;
175         n.pathlib_node_h = 0;
176
177         //setmodel(n, "models/runematch/rune.mdl");
178         //n.effects = EF_LOWPRECISION;
179         //n.colormod = '0 0 0';
180         //n.scale = 1;
181
182     }
183
184     goal_node  = wp_to;
185     start_node = wp_from;
186
187     start_node.pathlib_list = closedlist;
188     LOG_TRACE("Expanding ",ftos(pathlib_wpp_expand(start_node))," links\n");
189     if(pathlib_open_cnt <= 0)
190     {
191         LOG_TRACE("pathlib_waypointpath: Start waypoint not linked! aborting.\n");
192         return world;
193     }
194
195     return world;
196 }
197
198 entity pathlib_waypointpath_step()
199 {
200     entity n;
201
202     n = pathlib_wpp_bestopen();
203     if(!n)
204     {
205         LOG_TRACE("Cannot find best open node, abort.\n");
206         return world;
207     }
208     pathlib_wpp_close(n);
209         LOG_TRACE("Expanding ",ftos(pathlib_wpp_expand(n))," links\n");
210
211     if(pathlib_foundgoal)
212     {
213         entity start, end, open, ln;
214
215         LOG_TRACE("Target found. Rebuilding and filtering path...\n");
216
217                 buildpath_nodefilter = buildpath_nodefilter_none;
218                 start = path_build(world, start_node.origin, world, world);
219                 end   = path_build(world, goal_node.origin, world, start);
220                 ln    = end;
221
222                 for(open = goal_node; open.path_prev != start_node; open = open.path_prev)
223                 {
224                         n    = path_build(ln,open.origin,open.path_prev,start);
225                         ln.path_prev = n;
226                         ln = n;
227                 }
228                 start.path_next = n;
229                 n.path_prev = start;
230
231         return start;
232     }
233
234     return world;
235 }
236 void plas_think()
237 {
238     pathlib_waypointpath_step();
239     if(pathlib_foundgoal)
240         return;
241     self.nextthink = time + 0.1;
242 }
243
244 void pathlib_waypointpath_autostep()
245 {
246     entity n;
247     n = spawn();
248     n.think = plas_think;
249     n.nextthink = time + 0.1;
250 }