]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/pathlib/utility.qc
Minor code cleanup in pathlib
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / pathlib / utility.qc
1 #include "utility.qh"
2
3 #include "pathlib.qh"
4
5 bool location_isok(vector point, bool waterok, bool air_isok)
6 {
7     if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
8         return false;
9
10     int pc = pointcontents(point);
11     int pc2 = pointcontents(point - '0 0 1');
12
13     if(pc == CONTENT_EMPTY && pc2 == CONTENT_SOLID)
14         return true;
15     if(pc == CONTENT_EMPTY && pc2 == CONTENT_WATER && waterok)
16         return true;
17     if(pc == CONTENT_EMPTY && pc2 == CONTENT_EMPTY && air_isok)
18         return true;
19     if(pc == CONTENT_WATER && waterok)
20         return true;
21     return false;
22 }
23
24 entity pathlib_nodeatpoint(vector where)
25 {
26     ++pathlib_searched_cnt;
27
28     where.x = fsnap(where.x,pathlib_gridsize);
29     where.y = fsnap(where.y,pathlib_gridsize);
30
31     FOREACH_ENTITY_RADIUS(where, pathlib_gridsize * 0.5, it.is_path_node,
32     {
33         return it;
34     });
35
36     return NULL;
37 }
38
39 bool tile_check_cross(entity this, vector where)
40 {
41         vector p;
42     vector f = PLIB_FORWARD * tile_check_size;
43     vector r = PLIB_RIGHT   * tile_check_size;
44
45
46     // forward-right
47     p = where + f + r;
48     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
49     if (!location_isok(trace_endpos, 1, 0))
50         return false;
51
52     // Forward-left
53     p = where + f - r;
54     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
55     if (!location_isok(trace_endpos, 1, 0))
56         return false;
57
58     // Back-right
59     p = where - f + r;
60     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
61     if (!location_isok(trace_endpos, 1 ,0))
62         return false;
63
64     //Back-left
65     p = where - f - r;
66     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
67     if (!location_isok(trace_endpos, 1, 0))
68         return false;
69
70     return true;
71 }
72
73 bool tile_check_plus(entity this, vector where)
74 {
75     vector p;
76
77     vector f = PLIB_FORWARD * tile_check_size;
78     vector r = PLIB_RIGHT   * tile_check_size;
79
80     // forward
81     p = where + f;
82     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
83     if (!location_isok(trace_endpos,1,0))
84         return false;
85
86
87     //left
88     p = where - r;
89     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
90     if (!location_isok(trace_endpos,1,0))
91         return false;
92
93     // Right
94     p = where + r;
95     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
96     if (!location_isok(trace_endpos,1,0))
97         return false;
98
99     //Back
100     p = where - f;
101     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
102     if (!location_isok(trace_endpos,1,0))
103         return false;
104
105     return true;
106 }
107
108 float tile_check_plus2(entity this, vector where)
109 {
110     vector p;
111     int j = 0, e = 0;
112
113     vector f = PLIB_FORWARD * pathlib_gridsize;
114     vector r = PLIB_RIGHT   * pathlib_gridsize;
115
116 //#define pathlib_node_edgeflag_left    2
117 //#define pathlib_node_edgeflag_right   4
118 //#define pathlib_node_edgeflag_forward 8
119 //#define pathlib_node_edgeflag_back    16
120
121     // forward
122     p = where + f;
123     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
124     if (location_isok(trace_endpos,1,0))
125     {
126        ++j;
127        e |= pathlib_node_edgeflag_forward;
128     }
129
130
131     //left
132     p = where - r;
133     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
134     if (location_isok(trace_endpos,1,0))
135     {
136        ++j;
137        e |= pathlib_node_edgeflag_left;
138     }
139
140
141     // Right
142     p = where + r;
143     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
144     if (location_isok(trace_endpos,1,0))
145     {
146        ++j;
147        e |= pathlib_node_edgeflag_right;
148     }
149
150     //Back
151     p = where - f;
152     traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this);
153     if (location_isok(trace_endpos,1,0))
154     {
155        ++j;
156        e |= pathlib_node_edgeflag_back;
157     }
158
159     // forward-right
160     p = where + f + r;
161     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
162     if (location_isok(trace_endpos, 1, 0))
163     {
164        ++j;
165        e |= pathlib_node_edgeflag_forwardright;
166     }
167
168     // Forward-left
169     p = where + f - r;
170     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
171     if (location_isok(trace_endpos, 1, 0))
172     {
173        ++j;
174        e |= pathlib_node_edgeflag_forwardleft;
175     }
176
177     // Back-right
178     p = where - f + r;
179     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
180     if (location_isok(trace_endpos, 1 ,0))
181     {
182        ++j;
183        e |= pathlib_node_edgeflag_backright;
184     }
185
186     //Back-left
187     p = where - f - r;
188     traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this);
189     if (location_isok(trace_endpos, 1, 0))
190     {
191        ++j;
192        e |= pathlib_node_edgeflag_backleft;
193     }
194
195
196     if(j == 0)
197         e = pathlib_node_edgeflag_none;
198
199     return e;
200 }
201
202 bool tile_check_star(entity this, vector where)
203 {
204     if(tile_check_plus(this, where))
205         return tile_check_cross(this, where);
206
207     return false;
208 }
209