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