Added debugging mode, enabled by navnodeedit_debug 1 in console
[divverent/nexuiz.git] / navnodeedit / src / shit.qc
1 float(vector smins, vector smaxs, vector bmins, vector bmaxs) boxenclosed = {return smins_x >= bmins_x && smaxs_x <= bmaxs_x && smins_y >= bmins_y && smaxs_y <= bmaxs_y && smins_z >= bmins_z && smaxs_z <= bmaxs_z;};\r
2 \r
3 vector(vector point, entity current_space, entity goal_space) ClampPointToSpace =\r
4 {\r
5         local float North, South, West, East, Up, Down;\r
6         local float f, f2;\r
7         local vector ret_point, tvec;\r
8         local entity e;\r
9 \r
10         if (!goal_space)\r
11                 goal_space = current_space;\r
12 \r
13         North = min(current_space.origin_y + current_space.maxs_y, goal_space.origin_y + goal_space.maxs_y);\r
14         South = max(current_space.origin_y + current_space.mins_y, goal_space.origin_y + goal_space.mins_y);\r
15         East = min(current_space.origin_x + current_space.maxs_x, goal_space.origin_x + goal_space.maxs_x);\r
16         West = max(current_space.origin_x + current_space.mins_x, goal_space.origin_x + goal_space.mins_x);\r
17         Up = min(current_space.origin_z + current_space.maxs_z, goal_space.origin_z + goal_space.maxs_z);\r
18         Down = max(current_space.origin_z + current_space.mins_z, goal_space.origin_z + goal_space.mins_z);\r
19 \r
20         f = (East + West) * 0.5;\r
21         f2 = East - self.maxs_x;\r
22         East = max(f, f2);\r
23         f2 = West - self.mins_x;\r
24         West = min(f, f2);\r
25         f = (North + South) * 0.5;\r
26         f2 = North - self.maxs_y;\r
27         North = max(f, f2);\r
28         f2 = South - self.mins_y;\r
29         South = min(f, f2);\r
30         f = (Up + Down) * 0.5;\r
31         f2 = Up - self.maxs_z;\r
32         Up = max(f, f2);\r
33         f2 = Down - self.mins_z;\r
34         Down = min(f, f2);\r
35 \r
36         ret_point_x = bound(West, point_x, East);\r
37         ret_point_y = bound(South, point_y, North);\r
38         ret_point_z = bound(Down, point_z, Up);\r
39 \r
40         e = goal_space.plane_chain;\r
41         while (e)\r
42         {\r
43                 tvec = self.maxs;\r
44                 if (e.mangle_x < 0)\r
45                         tvec_x = self.mins_x;\r
46                 if (e.mangle_y < 0)\r
47                         tvec_y = self.mins_y;\r
48                 if (e.mangle_z < 0)\r
49                         tvec_z = self.mins_z;\r
50                 tvec += ret_point;\r
51                 f = tvec*e.mangle - goal_space.origin*e.mangle-e.delay;\r
52                 if (f > 0)\r
53                         ret_point = ret_point - f*e.mangle;\r
54                 e = e.list;\r
55         }\r
56         return ret_point;\r
57 };\r
58 \r
59 float(entity node) CheckNode =\r
60 {\r
61         local float f, result;\r
62         local vector tvec;\r
63         local entity plane;\r
64 \r
65         if (boxenclosed(self.origin + self.mins, self.origin + self.maxs, node.origin + node.mins, node.origin + node.maxs))\r
66         {\r
67                 result = TRUE;\r
68                 plane = node.plane_chain;\r
69                 while (plane)\r
70                 {\r
71                         tvec = self.maxs;\r
72                         if (plane.mangle_x < 0)\r
73                                 tvec_x = self.mins_x;\r
74                         if (plane.mangle_y < 0)\r
75                                 tvec_y = self.mins_y;\r
76                         if (plane.mangle_z < 0)\r
77                                 tvec_z = self.mins_z;\r
78                         tvec += self.origin;\r
79                         f = tvec*plane.mangle - node.origin*plane.mangle-plane.delay;\r
80                         if (f > 0)\r
81                                 result = FALSE;\r
82                         plane = plane.list;\r
83                 }\r
84         }\r
85         return result;\r
86 };\r
87 \r
88 float(entity currentnode, entity goalnode, float flight) AdvancedTraceWalk =\r
89 {\r
90         local float f, t, bump;\r
91         local float result;\r
92         local float stepdist, fulldist, totaldist;\r
93         local vector goalpoint, oldorg, move;\r
94         local entity oldself, debugpoint;\r
95 \r
96         oldself = self;\r
97         stepdist = cvar("navnodeedit_tracewalk_stepdist");\r
98         fulldist = cvar("navnodeedit_tracewalk_fulldist");\r
99         result = FALSE;\r
100         self = spawn();\r
101         setsize(self, '-16 -16 -24', '16 16 45');\r
102         tracebox(self.origin, self.mins, self.maxs, currentnode.origin + '0 0 -65536', TRUE, self);\r
103         setorigin(self, trace_endpos);\r
104         setsize(self, '-18 -18 -26', '18 18 47');\r
105         goalpoint = ClampPointToSpace(self.origin, currentnode, goalnode);\r
106         setsize(self, '-16 -16 -24', '16 16 45');\r
107         while(vlen(self.origin - oldorg) > 1 && totaldist < fulldist)\r
108         {\r
109                 oldorg = self.origin;\r
110                 if (!flight)\r
111                         tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 18', TRUE, self);\r
112                 setorigin(self, trace_endpos);\r
113                 move = self.origin - goalpoint;\r
114                 if (!flight)\r
115                         move_z = 0;\r
116                 f = vlen(self.origin - goalpoint);\r
117                 f = min(f, stepdist);\r
118                 self.velocity = normalize(move)*f;\r
119                 if (!flight)\r
120                         self.velocity_z = (f*2)*-1;\r
121                 t = 1;\r
122                 bump = 0;\r
123                 while (bump < 16)\r
124                 {\r
125                         bump = bump + 1;\r
126                         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * t, TRUE, self);\r
127                         if (trace_fraction == 1)\r
128                         {\r
129                                 setorigin(self, trace_endpos);\r
130                                 break;\r
131                         }\r
132                         if (self.velocity * trace_plane_normal < 0)\r
133                                 self.velocity = self.velocity - (trace_plane_normal * (trace_plane_normal * self.velocity));\r
134                         if (trace_fraction >= 0.0001)\r
135                         {\r
136                                 t = t * (1 - trace_fraction);\r
137                                 setorigin(self, trace_endpos);\r
138                         }\r
139                 }\r
140                 if (!flight)\r
141                 {\r
142                         tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', TRUE, self);\r
143                         setorigin(self, trace_endpos);\r
144                 }\r
145                 if (debug)\r
146                 {\r
147                         debugpoint = spawn();\r
148                         debugpoint.effects = EF_NODEPTHTEST;\r
149                         setmodel(debugpoint, "progs/point.spr");\r
150                         setorigin(debugpoint, self.origin);\r
151                         debugpoint.list = dasdebugpoint;\r
152                         dasdebugpoint = debugpoint;\r
153                 }\r
154                 if (CheckNode(goalnode))\r
155                 {\r
156                         result = TRUE;\r
157                         break;\r
158                 }\r
159                 totaldist = totaldist + vlen(self.origin - oldorg);\r
160         }\r
161         remove(self);\r
162         self = oldself;\r
163         return result;\r
164 };\r
165 \r
166 void() FlaggerThink =\r
167 {\r
168         local entity e;\r
169 \r
170         self.nextthink = time;\r
171         if (!dasdebugpoint)\r
172         {\r
173                 if (dasnode)\r
174                 {\r
175                         if (!(dasnode.lflags0 & LF_REMOTE))\r
176                         if (!AdvancedTraceWalk(dasnode, dasnode.link0, FALSE))\r
177                         {\r
178                                 dasnode.lflags0 = LF_NOWALK;\r
179                                 if (!AdvancedTraceWalk(dasnode, dasnode.link0, TRUE))\r
180                                         dasnode.lflags0 = LF_NOLINK;\r
181                         }\r
182                         if (dasnode.link1)\r
183                         {\r
184                                 if (!(dasnode.lflags1 & LF_REMOTE))\r
185                                 if (!AdvancedTraceWalk(dasnode, dasnode.link1, FALSE))\r
186                                 {\r
187                                         dasnode.lflags1 = LF_NOWALK;\r
188                                         if (!AdvancedTraceWalk(dasnode, dasnode.link1, TRUE))\r
189                                                 dasnode.lflags1 = LF_NOLINK;\r
190                                 }\r
191                                 if (dasnode.link2)\r
192                                 {\r
193                                         if (!(dasnode.lflags2 & LF_REMOTE))\r
194                                         if (!AdvancedTraceWalk(dasnode, dasnode.link2, FALSE))\r
195                                         {\r
196                                                 dasnode.lflags2 = LF_NOWALK;\r
197                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link2, TRUE))\r
198                                                         dasnode.lflags2 = LF_NOLINK;\r
199                                         }\r
200                                         if (dasnode.link3)\r
201                                         {\r
202                                                 if (!(dasnode.lflags3 & LF_REMOTE))\r
203                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link3, FALSE))\r
204                                                 {\r
205                                                         dasnode.lflags3 = LF_NOWALK;\r
206                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link3, TRUE))\r
207                                                                 dasnode.lflags3 = LF_NOLINK;\r
208                                                 }\r
209                                                 if (dasnode.link4)\r
210                                                 {\r
211                                                         if (!(dasnode.lflags4 & LF_REMOTE))\r
212                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link4, FALSE))\r
213                                                         {\r
214                                                                 dasnode.lflags4 = LF_NOWALK;\r
215                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link4, TRUE))\r
216                                                                         dasnode.lflags4 = LF_NOLINK;\r
217                                                         }\r
218                                                         if (dasnode.link5)\r
219                                                         {\r
220                                                                 if (!(dasnode.lflags5 & LF_REMOTE))\r
221                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link5, FALSE))\r
222                                                                 {\r
223                                                                         dasnode.lflags5 = LF_NOWALK;\r
224                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link5, TRUE))\r
225                                                                                 dasnode.lflags5 = LF_NOLINK;\r
226                                                                 }\r
227                                                                 if (dasnode.link6)\r
228                                                                 {\r
229                                                                         if (!(dasnode.lflags6 & LF_REMOTE))\r
230                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link6, FALSE))\r
231                                                                         {\r
232                                                                                 dasnode.lflags6 = LF_NOWALK;\r
233                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link6, TRUE))\r
234                                                                                         dasnode.lflags6 = LF_NOLINK;\r
235                                                                         }\r
236                                                                         if (dasnode.link7)\r
237                                                                         {\r
238                                                                                 if (!(dasnode.lflags7 & LF_REMOTE))\r
239                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link7, FALSE))\r
240                                                                                 {\r
241                                                                                         dasnode.lflags7 = LF_NOWALK;\r
242                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link7, TRUE))\r
243                                                                                                 dasnode.lflags7 = LF_NOLINK;\r
244                                                                                 }\r
245                                                                                 if (dasnode.link8)\r
246                                                                                 {\r
247                                                                                         if (!(dasnode.lflags8 & LF_REMOTE))\r
248                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link8, FALSE))\r
249                                                                                         {\r
250                                                                                                 dasnode.lflags8 = LF_NOWALK;\r
251                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link8, TRUE))\r
252                                                                                                         dasnode.lflags8 = LF_NOLINK;\r
253                                                                                         }\r
254                                                                                         if (dasnode.link9)\r
255                                                                                         {\r
256                                                                                                 if (!(dasnode.lflags9 & LF_REMOTE))\r
257                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link9, FALSE))\r
258                                                                                                 {\r
259                                                                                                         dasnode.lflags9 = LF_NOWALK;\r
260                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link9, TRUE))\r
261                                                                                                                 dasnode.lflags9 = LF_NOLINK;\r
262                                                                                                 }\r
263                                                                                                 if (dasnode.link10)\r
264                                                                                                 {\r
265                                                                                                         if (!(dasnode.lflags10 & LF_REMOTE))\r
266                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link10, FALSE))\r
267                                                                                                         {\r
268                                                                                                                 dasnode.lflags10 = LF_NOWALK;\r
269                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link10, TRUE))\r
270                                                                                                                         dasnode.lflags10 = LF_NOLINK;\r
271                                                                                                         }\r
272                                                                                                         if (dasnode.link11)\r
273                                                                                                         {\r
274                                                                                                                 if (!(dasnode.lflags11 & LF_REMOTE))\r
275                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link11, FALSE))\r
276                                                                                                                 {\r
277                                                                                                                         dasnode.lflags11 = LF_NOWALK;\r
278                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link11, TRUE))\r
279                                                                                                                                 dasnode.lflags11 = LF_NOLINK;\r
280                                                                                                                 }\r
281                                                                                                                 if (dasnode.link12)\r
282                                                                                                                 {\r
283                                                                                                                         if (!(dasnode.lflags12 & LF_REMOTE))\r
284                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link12, FALSE))\r
285                                                                                                                         {\r
286                                                                                                                                 dasnode.lflags12 = LF_NOWALK;\r
287                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link12, TRUE))\r
288                                                                                                                                         dasnode.lflags12 = LF_NOLINK;\r
289                                                                                                                         }\r
290                                                                                                                         if (dasnode.link13)\r
291                                                                                                                         {\r
292                                                                                                                                 if (!(dasnode.lflags13 & LF_REMOTE))\r
293                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link13, FALSE))\r
294                                                                                                                                 {\r
295                                                                                                                                         dasnode.lflags13 = LF_NOWALK;\r
296                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link13, TRUE))\r
297                                                                                                                                                 dasnode.lflags13 = LF_NOLINK;\r
298                                                                                                                                 }\r
299                                                                                                                                 if (dasnode.link14)\r
300                                                                                                                                 {\r
301                                                                                                                                         if (!(dasnode.lflags14 & LF_REMOTE))\r
302                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link14, FALSE))\r
303                                                                                                                                         {\r
304                                                                                                                                                 dasnode.lflags14 = LF_NOWALK;\r
305                                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link14, TRUE))\r
306                                                                                                                                                         dasnode.lflags14 = LF_NOLINK;\r
307                                                                                                                                         }\r
308                                                                                                                                         if (dasnode.link15)\r
309                                                                                                                                         {\r
310                                                                                                                                                 if (!(dasnode.lflags15 & LF_REMOTE))\r
311                                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link15, FALSE))\r
312                                                                                                                                                 {\r
313                                                                                                                                                         dasnode.lflags15 = LF_NOWALK;\r
314                                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link15, TRUE))\r
315                                                                                                                                                                 dasnode.lflags15 = LF_NOLINK;\r
316                                                                                                                                                 }\r
317                                                                                                                                                 if (dasnode.link16)\r
318                                                                                                                                                 {\r
319                                                                                                                                                         if (!(dasnode.lflags16 & LF_REMOTE))\r
320                                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link16, FALSE))\r
321                                                                                                                                                         {\r
322                                                                                                                                                                 dasnode.lflags16 = LF_NOWALK;\r
323                                                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link16, TRUE))\r
324                                                                                                                                                                         dasnode.lflags16 = LF_NOLINK;\r
325                                                                                                                                                         }\r
326                                                                                                                                                         if (dasnode.link17)\r
327                                                                                                                                                         {\r
328                                                                                                                                                                 if (!(dasnode.lflags17 & LF_REMOTE))\r
329                                                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link17, FALSE))\r
330                                                                                                                                                                 {\r
331                                                                                                                                                                         dasnode.lflags17 = LF_NOWALK;\r
332                                                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link17, TRUE))\r
333                                                                                                                                                                                 dasnode.lflags17 = LF_NOLINK;\r
334                                                                                                                                                                 }\r
335                                                                                                                                                                 if (dasnode.link18)\r
336                                                                                                                                                                 {\r
337                                                                                                                                                                         if (!(dasnode.lflags18 & LF_REMOTE))\r
338                                                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link18, FALSE))\r
339                                                                                                                                                                         {\r
340                                                                                                                                                                                 dasnode.lflags18 = LF_NOWALK;\r
341                                                                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link18, TRUE))\r
342                                                                                                                                                                                         dasnode.lflags18 = LF_NOLINK;\r
343                                                                                                                                                                         }\r
344                                                                                                                                                                         if (dasnode.link19)\r
345                                                                                                                                                                         {\r
346                                                                                                                                                                                 if (!(dasnode.lflags19 & LF_REMOTE))\r
347                                                                                                                                                                                 if (!AdvancedTraceWalk(dasnode, dasnode.link19, FALSE))\r
348                                                                                                                                                                                 {\r
349                                                                                                                                                                                         dasnode.lflags19 = LF_NOWALK;\r
350                                                                                                                                                                                         if (!AdvancedTraceWalk(dasnode, dasnode.link19, TRUE))\r
351                                                                                                                                                                                                 dasnode.lflags19 = LF_NOLINK;\r
352                                                                                                                                                                                 }\r
353                                                                                                                                                                         }\r
354                                                                                                                                                                 }\r
355                                                                                                                                                         }\r
356                                                                                                                                                 }\r
357                                                                                                                                         }\r
358                                                                                                                                 }\r
359                                                                                                                         }\r
360                                                                                                                 }\r
361                                                                                                         }\r
362                                                                                                 }\r
363                                                                                         }\r
364                                                                                 }\r
365                                                                         }\r
366                                                                 }\r
367                                                         }\r
368                                                 }\r
369                                         }\r
370                                 }\r
371                         }\r
372                         dasnode = dasnode.list;\r
373                         if (debug)\r
374                         {\r
375                                 e = findchainflags(flags, FL_CLIENT);\r
376                                 while (e)\r
377                                 {\r
378                                         e.confirm = 0;\r
379                                         e = e.chain;            \r
380                                 }\r
381                         }\r
382                 }\r
383                 else\r
384                 {\r
385                         self.think = SUB_Remove;\r
386                         e = findchainflags(flags, FL_CLIENT);\r
387                         while (e)\r
388                         {\r
389                                 e.confirm = 0;\r
390                                 e = e.chain;            \r
391                         }\r
392                 }\r
393         }\r
394 };\r
395 \r
396 void() AutoFlagger =\r
397 {\r
398         local entity e;\r
399 \r
400         e = dasdebugpoint;\r
401         if (e)\r
402         {\r
403                 while (e)\r
404                 {\r
405                         e.think = SUB_Remove;\r
406                         e.nextthink = time;\r
407                         e = e.list;\r
408                 }\r
409         }\r
410         else\r
411         {\r
412                 dasnode = navnode_chain;\r
413                 e = spawn();\r
414                 e.think = FlaggerThink;\r
415                 e.nextthink = time;\r
416         }\r
417 };