]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/pathlib/utility.qc
Make most server includes order insensitive
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / pathlib / utility.qc
1 #include "utility.qh"
2
3 #include "pathlib.qh"
4
5 float fsnap(float val,float fsize)
6 {
7     return rint(val / fsize) * fsize;
8 }
9
10 vector vsnap(vector point,float fsize)
11 {
12     vector vret;
13
14     vret.x = rint(point.x / fsize) * fsize;
15     vret.y = rint(point.y / fsize) * fsize;
16     vret.z = ceil(point.z / fsize) * fsize;
17
18     return vret;
19 }
20
21 float location_isok(vector point, float water_isok, float air_isok)
22 {
23     float pc,pc2;
24
25     if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
26         return 0;
27
28     pc  = pointcontents(point);
29     pc2 = pointcontents(point - '0 0 1');
30
31     switch(pc)
32     {
33         case CONTENT_SOLID:
34             break;
35
36         case CONTENT_SLIME:
37             break;
38
39         case CONTENT_LAVA:
40             break;
41
42         case CONTENT_SKY:
43             break;
44
45         case CONTENT_EMPTY:
46             if (pc2 == CONTENT_SOLID)
47                 return 1;
48
49             if (pc2 == CONTENT_EMPTY)
50                 if(air_isok)
51                     return 1;
52
53             if (pc2 == CONTENT_WATER)
54                 if(water_isok)
55                     return 1;
56
57             break;
58
59         case CONTENT_WATER:
60             if (water_isok)
61                 return 1;
62
63             break;
64     }
65
66     return 0;
67 }
68
69 entity pathlib_nodeatpoint(vector where)
70 {
71     entity node;
72
73     ++pathlib_searched_cnt;
74
75     where.x = fsnap(where.x,pathlib_gridsize);
76     where.y = fsnap(where.y,pathlib_gridsize);
77
78     node = findradius(where,pathlib_gridsize * 0.5);
79     while(node)
80     {
81         if(node.is_path_node == true)
82             return node;
83
84         node = node.chain;
85     }
86
87     return world;
88 }
89
90 float tile_check_cross(vector where)
91 {
92     vector p,f,r;
93
94     f = PLIB_FORWARD * tile_check_size;
95     r = PLIB_RIGHT   * tile_check_size;
96
97
98     // forward-right
99     p = where + f + r;
100     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
101     if (!location_isok(trace_endpos, 1, 0))
102         return 0;
103
104     // Forward-left
105     p = where + f - r;
106     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
107     if (!location_isok(trace_endpos, 1, 0))
108         return 0;
109
110     // Back-right
111     p = where - f + r;
112     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
113     if (!location_isok(trace_endpos, 1 ,0))
114         return 0;
115
116     //Back-left
117     p = where - f - r;
118     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
119     if (!location_isok(trace_endpos, 1, 0))
120         return 0;
121
122     return 1;
123 }
124
125 float tile_check_plus(vector where)
126 {
127     vector p,f,r;
128
129     f = PLIB_FORWARD * tile_check_size;
130     r = PLIB_RIGHT   * tile_check_size;
131
132     // forward
133     p = where + f;
134     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
135     if (!location_isok(trace_endpos,1,0))
136         return 0;
137
138
139     //left
140     p = where - r;
141     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
142     if (!location_isok(trace_endpos,1,0))
143         return 0;
144
145     // Right
146     p = where + r;
147     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
148     if (!location_isok(trace_endpos,1,0))
149         return 0;
150
151     //Back
152     p = where - f;
153     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
154     if (!location_isok(trace_endpos,1,0))
155         return 0;
156
157     return 1;
158 }
159
160 float tile_check_plus2(vector where)
161 {
162     vector p,f,r;
163     float i = 0, e = 0;
164
165     f = PLIB_FORWARD * pathlib_gridsize;
166     r = PLIB_RIGHT   * pathlib_gridsize;
167
168 //#define pathlib_node_edgeflag_left    2
169 //#define pathlib_node_edgeflag_right   4
170 //#define pathlib_node_edgeflag_forward 8
171 //#define pathlib_node_edgeflag_back    16
172
173     // forward
174     p = where + f;
175     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
176     if (location_isok(trace_endpos,1,0))
177     {
178        ++i;
179        e |= pathlib_node_edgeflag_forward;
180     }
181
182
183     //left
184     p = where - r;
185     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
186     if (location_isok(trace_endpos,1,0))
187     {
188        ++i;
189        e |= pathlib_node_edgeflag_left;
190     }
191
192
193     // Right
194     p = where + r;
195     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
196     if (location_isok(trace_endpos,1,0))
197     {
198        ++i;
199        e |= pathlib_node_edgeflag_right;
200     }
201
202     //Back
203     p = where - f;
204     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,self);
205     if (location_isok(trace_endpos,1,0))
206     {
207        ++i;
208        e |= pathlib_node_edgeflag_back;
209     }
210
211     // forward-right
212     p = where + f + r;
213     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
214     if (location_isok(trace_endpos, 1, 0))
215     {
216        ++i;
217        e |= pathlib_node_edgeflag_forwardright;
218     }
219
220     // Forward-left
221     p = where + f - r;
222     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
223     if (location_isok(trace_endpos, 1, 0))
224     {
225        ++i;
226        e |= pathlib_node_edgeflag_forwardleft;
227     }
228
229     // Back-right
230     p = where - f + r;
231     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
232     if (location_isok(trace_endpos, 1 ,0))
233     {
234        ++i;
235        e |= pathlib_node_edgeflag_backright;
236     }
237
238     //Back-left
239     p = where - f - r;
240     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, self);
241     if (location_isok(trace_endpos, 1, 0))
242     {
243        ++i;
244        e |= pathlib_node_edgeflag_backleft;
245     }
246
247
248     if(i == 0)
249         e = pathlib_node_edgeflag_none;
250
251     return e;
252 }
253
254 float tile_check_star(vector where)
255 {
256     if(tile_check_plus(where))
257         return tile_check_cross(where);
258
259     return 0;
260 }
261