4 #include <common/command/_mod.qh>
6 #include "../g_world.qh"
7 #include "../g_subs.qh"
9 #include <common/util.qh>
11 #include <lib/csqcmodel/sv_model.qh>
13 // ===============================================
14 // Generates radar map images for use in the HUD
15 // ===============================================
17 float FullTraceFraction(vector a, vector mi, vector ma, vector b)
30 while (vdist(c - b, >, 1))
34 tracebox(c, mi, ma, b, MOVE_WORLDONLY, NULL);
37 if (!trace_startsolid)
39 black += vlen(trace_endpos - c);
43 n += tracebox_inverted(c, mi, ma, b, MOVE_WORLDONLY, NULL, false, NULL);
45 white += vlen(trace_endpos - c);
49 if (n > 200) LOG_TRACE("HOLY SHIT! FullTraceFraction: ", ftos(n), " total traces, ", ftos(m), " iterations");
51 return white / (black + white);
53 float RadarMapAtPoint_Trace(float e, float f, float w, float h, float zmin, float zsize, float q)
58 ma = '1 0 0' * w + '0 1 0' * h;
59 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
60 b = '1 0 0' * e + '0 1 0' * f + '0 0 1' * (zsize + zmin);
62 return FullTraceFraction(a, mi, ma, b);
64 float RadarMapAtPoint_LineBlock(float e, float f, float w, float h, float zmin, float zsize, float q)
71 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
74 dz = (zsize / q) * '0 0 1';
75 ma = '1 0 0' * w + '0 1 0' * h + dz;
76 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
78 if (e < world.absmin.x - w) return 0;
79 if (f < world.absmin.y - h) return 0;
80 if (e > world.absmax.x) return 0;
81 if (f > world.absmax.y) return 0;
84 for (i = 0; i < q; ++i)
87 v1 = v2 = o + dz * i + mi;
88 v1_x += random() * (ma.x - mi.x);
89 v1_y += random() * (ma.y - mi.y);
90 v1_z += random() * (ma.z - mi.z);
91 v2_x += random() * (ma.x - mi.x);
92 v2_y += random() * (ma.y - mi.y);
93 v2_z += random() * (ma.z - mi.z);
94 traceline(v1, v2, MOVE_WORLDONLY, NULL);
95 if (trace_startsolid || trace_fraction < 1) ++r;
99 float RadarMapAtPoint_Block(float e, float f, float w, float h, float zmin, float zsize, float q)
106 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
109 dz = (zsize / q) * '0 0 1';
110 ma = '1 0 0' * w + '0 1 0' * h + dz;
111 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
113 if (e < world.absmin.x - w) return 0;
114 if (f < world.absmin.y - h) return 0;
115 if (e > world.absmax.x) return 0;
116 if (f > world.absmax.y) return 0;
119 for (i = 0; i < q; ++i)
121 tracebox(o + dz * i, mi, ma, o + dz * i, MOVE_WORLDONLY, NULL);
122 if (trace_startsolid) ++r;
126 float RadarMapAtPoint_Sample(float e, float f, float w, float h, float zmin, float zsize, float q)
130 q *= 4; // choose q so it matches the regular algorithm in speed
133 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
136 ma = '1 0 0' * w + '0 1 0' * h;
137 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
138 b = '1 0 0' * w + '0 1 0' * h + '0 0 1' * zsize;
143 for (i = 0; i < q; ++i)
146 v.x = a.x + random() * b.x;
147 v.y = a.y + random() * b.y;
148 v.z = a.z + random() * b.z;
149 traceline(v, v, MOVE_WORLDONLY, NULL);
150 if (trace_startsolid) ++c;
155 void sharpen_set(int b, float v)
157 sharpen_buffer[b + 2 * RADAR_WIDTH_MAX] = v;
159 float sharpen_getpixel(int b, int c)
162 if (b >= RADAR_WIDTH_MAX) return 0;
165 return sharpen_buffer[b + c * RADAR_WIDTH_MAX];
167 float sharpen_get(float b, float a)
169 float sum = sharpen_getpixel(b, 1);
170 if (a == 0) return sum;
172 sum -= sharpen_getpixel(b - 1, 0);
173 sum -= sharpen_getpixel(b - 1, 1);
174 sum -= sharpen_getpixel(b - 1, 2);
175 sum -= sharpen_getpixel(b + 1, 0);
176 sum -= sharpen_getpixel(b + 1, 1);
177 sum -= sharpen_getpixel(b + 1, 2);
178 sum -= sharpen_getpixel(b, 0);
179 sum -= sharpen_getpixel(b, 2);
180 return bound(0, sum * a, 1);
182 void sharpen_shift(int w)
184 for (int i = 0; i < w; ++i)
186 sharpen_buffer[i] = sharpen_buffer[i + RADAR_WIDTH_MAX];
187 sharpen_buffer[i + RADAR_WIDTH_MAX] = sharpen_buffer[i + 2 * RADAR_WIDTH_MAX];
188 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
191 void sharpen_init(int w)
193 for (int i = 0; i < w; ++i)
195 sharpen_buffer[i] = 0;
196 sharpen_buffer[i + RADAR_WIDTH_MAX] = 0;
197 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
202 if (radarmapper.count & 4)
206 else if (radarmapper.count & 2)
208 localcmd(strcat("defer 1 \"sv_cmd radarmap --flags ", ftos(radarmapper.count), strcat(" --res ", ftos(radarmapper.size.x), " ", ftos(radarmapper.size.y), " --sharpen ", ftos(radarmapper.ltime), " --qual ", ftos(radarmapper.size.z)), "\"\n"));
214 void RadarMap_Think(entity this)
218 // size: pixel width/height
219 // maxs: cell width/height
228 get_mi_min_max_texcoords(1);
229 this.mins = mi_picmin;
230 this.maxs_x = (mi_picmax.x - mi_picmin.x) / this.size.x;
231 this.maxs_y = (mi_picmax.y - mi_picmin.y) / this.size.y;
232 this.maxs_z = mi_max.z - mi_min.z;
233 LOG_INFO("Picture mins/maxs: ", ftos(this.maxs.x), " and ", ftos(this.maxs.y), " should match\n");
234 this.netname = strzone(strcat("gfx/", mi_shortname, "_radar.xpm"));
235 if (!(this.count & 1))
237 this.cnt = fopen(this.netname, FILE_READ);
238 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.tga"), FILE_READ);
239 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.png"), FILE_READ);
240 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.jpg"), FILE_READ);
241 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.tga"), FILE_READ);
242 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.png"), FILE_READ);
243 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.jpg"), FILE_READ);
248 LOG_INFO(this.netname, " already exists, aborting (you may want to specify --force)\n");
253 this.cnt = fopen(this.netname, FILE_WRITE);
256 LOG_INFO("Error writing ", this.netname, "\n");
261 LOG_INFO("Writing to ", this.netname, "...\n");
262 fputs(this.cnt, "/* XPM */\n");
263 fputs(this.cnt, "static char *RadarMap[] = {\n");
264 fputs(this.cnt, "/* columns rows colors chars-per-pixel */\n");
265 fputs(this.cnt, strcat("\"", ftos(this.size.x), " ", ftos(this.size.y), " 256 2\",\n"));
266 for (i = 0; i < 256; ++i)
268 si = substring(doublehex, i * 2, 2);
269 fputs(this.cnt, strcat("\"", si, " c #", si, si, si, "\",\n"));
272 this.nextthink = time;
273 sharpen_init(this.size.x);
275 else if (this.frame <= this.size.y)
277 // fill the sharpen buffer with this line
278 sharpen_shift(this.size.x);
285 for (x = 0; x < this.size.x; ++x)
287 l = RadarMapAtPoint_Block(this.mins.x + x * this.maxs.x, this.mins.y + (this.size.y - this.frame) * this.maxs.y, this.maxs.x, this.maxs.y, this.mins.z, this.maxs.z, this.size.z);
292 for (x = 0; x < this.size.x; ++x)
294 l = RadarMapAtPoint_Trace(this.mins.x + x * this.maxs.x, this.mins.y + (this.size.y - this.frame) * this.maxs.y, this.maxs.x, this.maxs.y, this.mins.z, this.maxs.z, this.size.z);
299 for (x = 0; x < this.size.x; ++x)
301 l = RadarMapAtPoint_Sample(this.mins.x + x * this.maxs.x, this.mins.y + (this.size.y - this.frame) * this.maxs.y, this.maxs.x, this.maxs.y, this.mins.z, this.maxs.z, this.size.z);
306 for (x = 0; x < this.size.x; ++x)
308 l = RadarMapAtPoint_LineBlock(this.mins.x + x * this.maxs.x, this.mins.y + (this.size.y - this.frame) * this.maxs.y, this.maxs.x, this.maxs.y, this.mins.z, this.maxs.z, this.size.z);
314 // do we have enough lines?
317 // write a pixel line
318 fputs(this.cnt, "\"");
319 for (x = 0; x < this.size.x; ++x)
321 l = sharpen_get(x, this.ltime);
322 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
324 if (this.frame == this.size.y)
326 fputs(this.cnt, "\"\n");
330 fputs(this.cnt, "\",\n");
331 LOG_INFO(ftos(this.size.y - this.frame), " lines left\n");
335 // is this the last line? then write back the missing line
336 if (this.frame == this.size.y)
338 sharpen_shift(this.size.x);
339 // write a pixel line
340 fputs(this.cnt, "\"");
341 for (x = 0; x < this.size.x; ++x)
343 l = sharpen_get(x, this.ltime);
344 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
346 if (this.frame == this.size.y)
348 fputs(this.cnt, "\"\n");
352 fputs(this.cnt, "\",\n");
353 LOG_INFO(ftos(this.size.y - this.frame), " lines left\n");
358 this.nextthink = time;
363 fputs(this.cnt, "};\n");
365 LOG_INFO("Finished. Please edit data/", this.netname, " with an image editing application and place it in the TGA format in the gfx folder.\n");
370 bool RadarMap_Make(float argc)
376 radarmapper = new(radarmapper);
377 setthink(radarmapper, RadarMap_Think);
378 radarmapper.nextthink = time;
379 radarmapper.count = 8; // default to the --trace method, as it is faster now
380 radarmapper.ltime = 1;
381 radarmapper.size = '512 512 1';
382 for (i = 1; i < argc; ++i)
387 { radarmapper.count |= 1;
391 { radarmapper.count |= 2;
395 { radarmapper.count |= 4;
399 { radarmapper.count &= ~24;
403 { radarmapper.count &= ~24;
404 radarmapper.count |= 8;
408 { radarmapper.count &= ~24;
409 radarmapper.count |= 16;
413 { radarmapper.count |= 24;
418 radarmapper.count = stof(argv(i));
420 } // for the recursive call
423 radarmapper.ltime = stof(argv(i));
425 } // for the recursive call
426 case "--res": // minor alias
429 radarmapper.size_x = stof(argv(i));
431 radarmapper.size_y = stof(argv(i));
434 case "--qual": // minor alias
437 radarmapper.size_z = stof(argv(i));
449 if (radarmapper) // after doing the arguments, see if we successfully went forward.
451 LOG_INFO("Radarmap entity spawned.\n");
452 return true; // if so, don't print usage.