4 #include <common/command/_mod.qh>
7 #include "../g_world.qh"
8 #include "../g_subs.qh"
10 #include <common/util.qh>
12 #include <lib/csqcmodel/sv_model.qh>
14 // ===============================================
15 // Generates radar map images for use in the HUD
16 // ===============================================
18 float FullTraceFraction(vector a, vector mi, vector ma, vector b)
31 while (vdist(c - b, >, 1))
35 tracebox(c, mi, ma, b, MOVE_WORLDONLY, NULL);
38 if (!trace_startsolid)
40 black += vlen(trace_endpos - c);
44 n += tracebox_inverted(c, mi, ma, b, MOVE_WORLDONLY, NULL, false, NULL);
46 white += vlen(trace_endpos - c);
50 if (n > 200) LOG_TRACE("HOLY SHIT! FullTraceFraction: ", ftos(n), " total traces, ", ftos(m), " iterations");
52 return white / (black + white);
54 float RadarMapAtPoint_Trace(float e, float f, float w, float h, float zmin, float zsize, float q)
59 ma = '1 0 0' * w + '0 1 0' * h;
60 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
61 b = '1 0 0' * e + '0 1 0' * f + '0 0 1' * (zsize + zmin);
63 return FullTraceFraction(a, mi, ma, b);
65 float RadarMapAtPoint_LineBlock(float e, float f, float w, float h, float zmin, float zsize, float q)
72 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
75 dz = (zsize / q) * '0 0 1';
76 ma = '1 0 0' * w + '0 1 0' * h + dz;
77 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
79 if (e < world.absmin.x - w) return 0;
80 if (f < world.absmin.y - h) return 0;
81 if (e > world.absmax.x) return 0;
82 if (f > world.absmax.y) return 0;
85 for (i = 0; i < q; ++i)
88 v1 = v2 = o + dz * i + mi;
89 v1_x += random() * (ma.x - mi.x);
90 v1_y += random() * (ma.y - mi.y);
91 v1_z += random() * (ma.z - mi.z);
92 v2_x += random() * (ma.x - mi.x);
93 v2_y += random() * (ma.y - mi.y);
94 v2_z += random() * (ma.z - mi.z);
95 traceline(v1, v2, MOVE_WORLDONLY, NULL);
96 if (trace_startsolid || trace_fraction < 1) ++r;
100 float RadarMapAtPoint_Block(float e, float f, float w, float h, float zmin, float zsize, float q)
107 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
110 dz = (zsize / q) * '0 0 1';
111 ma = '1 0 0' * w + '0 1 0' * h + dz;
112 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
114 if (e < world.absmin.x - w) return 0;
115 if (f < world.absmin.y - h) return 0;
116 if (e > world.absmax.x) return 0;
117 if (f > world.absmax.y) return 0;
120 for (i = 0; i < q; ++i)
122 tracebox(o + dz * i, mi, ma, o + dz * i, MOVE_WORLDONLY, NULL);
123 if (trace_startsolid) ++r;
127 float RadarMapAtPoint_Sample(float e, float f, float w, float h, float zmin, float zsize, float q)
131 q *= 4; // choose q so it matches the regular algorithm in speed
134 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
137 ma = '1 0 0' * w + '0 1 0' * h;
138 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
139 b = '1 0 0' * w + '0 1 0' * h + '0 0 1' * zsize;
144 for (i = 0; i < q; ++i)
147 v.x = a.x + random() * b.x;
148 v.y = a.y + random() * b.y;
149 v.z = a.z + random() * b.z;
150 traceline(v, v, MOVE_WORLDONLY, NULL);
151 if (trace_startsolid) ++c;
156 void sharpen_set(int b, float v)
158 sharpen_buffer[b + 2 * RADAR_WIDTH_MAX] = v;
160 float sharpen_getpixel(int b, int c)
163 if (b >= RADAR_WIDTH_MAX) return 0;
166 return sharpen_buffer[b + c * RADAR_WIDTH_MAX];
168 float sharpen_get(float b, float a)
170 float sum = sharpen_getpixel(b, 1);
171 if (a == 0) return sum;
173 sum -= sharpen_getpixel(b - 1, 0);
174 sum -= sharpen_getpixel(b - 1, 1);
175 sum -= sharpen_getpixel(b - 1, 2);
176 sum -= sharpen_getpixel(b + 1, 0);
177 sum -= sharpen_getpixel(b + 1, 1);
178 sum -= sharpen_getpixel(b + 1, 2);
179 sum -= sharpen_getpixel(b, 0);
180 sum -= sharpen_getpixel(b, 2);
181 return bound(0, sum * a, 1);
183 void sharpen_shift(int w)
185 for (int i = 0; i < w; ++i)
187 sharpen_buffer[i] = sharpen_buffer[i + RADAR_WIDTH_MAX];
188 sharpen_buffer[i + RADAR_WIDTH_MAX] = sharpen_buffer[i + 2 * RADAR_WIDTH_MAX];
189 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
192 void sharpen_init(int w)
194 for (int i = 0; i < w; ++i)
196 sharpen_buffer[i] = 0;
197 sharpen_buffer[i + RADAR_WIDTH_MAX] = 0;
198 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
203 if (radarmapper.count & 4)
207 else if (radarmapper.count & 2)
209 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"));
215 void RadarMap_Think(entity this)
219 // size: pixel width/height
220 // maxs: cell width/height
229 get_mi_min_max_texcoords(1);
230 this.mins = mi_picmin;
231 this.maxs_x = (mi_picmax.x - mi_picmin.x) / this.size.x;
232 this.maxs_y = (mi_picmax.y - mi_picmin.y) / this.size.y;
233 this.maxs_z = mi_max.z - mi_min.z;
234 LOG_INFO("Picture mins/maxs: ", ftos(this.maxs.x), " and ", ftos(this.maxs.y), " should match\n");
235 this.netname = strzone(strcat("gfx/", mi_shortname, "_radar.xpm"));
236 if (!(this.count & 1))
238 this.cnt = fopen(this.netname, FILE_READ);
239 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.tga"), FILE_READ);
240 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.png"), FILE_READ);
241 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.jpg"), FILE_READ);
242 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.tga"), FILE_READ);
243 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.png"), FILE_READ);
244 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.jpg"), FILE_READ);
249 LOG_INFO(this.netname, " already exists, aborting (you may want to specify --force)\n");
254 this.cnt = fopen(this.netname, FILE_WRITE);
257 LOG_INFO("Error writing ", this.netname, "\n");
262 LOG_INFO("Writing to ", this.netname, "...\n");
263 fputs(this.cnt, "/* XPM */\n");
264 fputs(this.cnt, "static char *RadarMap[] = {\n");
265 fputs(this.cnt, "/* columns rows colors chars-per-pixel */\n");
266 fputs(this.cnt, strcat("\"", ftos(this.size.x), " ", ftos(this.size.y), " 256 2\",\n"));
267 for (i = 0; i < 256; ++i)
269 si = substring(doublehex, i * 2, 2);
270 fputs(this.cnt, strcat("\"", si, " c #", si, si, si, "\",\n"));
273 this.nextthink = time;
274 sharpen_init(this.size.x);
276 else if (this.frame <= this.size.y)
278 // fill the sharpen buffer with this line
279 sharpen_shift(this.size.x);
286 for (x = 0; x < this.size.x; ++x)
288 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);
293 for (x = 0; x < this.size.x; ++x)
295 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);
300 for (x = 0; x < this.size.x; ++x)
302 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);
307 for (x = 0; x < this.size.x; ++x)
309 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);
315 // do we have enough lines?
318 // write a pixel line
319 fputs(this.cnt, "\"");
320 for (x = 0; x < this.size.x; ++x)
322 l = sharpen_get(x, this.ltime);
323 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
325 if (this.frame == this.size.y)
327 fputs(this.cnt, "\"\n");
331 fputs(this.cnt, "\",\n");
332 LOG_INFO(ftos(this.size.y - this.frame), " lines left\n");
336 // is this the last line? then write back the missing line
337 if (this.frame == this.size.y)
339 sharpen_shift(this.size.x);
340 // write a pixel line
341 fputs(this.cnt, "\"");
342 for (x = 0; x < this.size.x; ++x)
344 l = sharpen_get(x, this.ltime);
345 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
347 if (this.frame == this.size.y)
349 fputs(this.cnt, "\"\n");
353 fputs(this.cnt, "\",\n");
354 LOG_INFO(ftos(this.size.y - this.frame), " lines left\n");
359 this.nextthink = time;
364 fputs(this.cnt, "};\n");
366 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");
371 bool RadarMap_Make(float argc)
377 radarmapper = new(radarmapper);
378 setthink(radarmapper, RadarMap_Think);
379 radarmapper.nextthink = time;
380 radarmapper.count = 8; // default to the --trace method, as it is faster now
381 radarmapper.ltime = 1;
382 radarmapper.size = '512 512 1';
383 for (i = 1; i < argc; ++i)
388 { radarmapper.count |= 1;
392 { radarmapper.count |= 2;
396 { radarmapper.count |= 4;
400 { radarmapper.count &= ~24;
404 { radarmapper.count &= ~24;
405 radarmapper.count |= 8;
409 { radarmapper.count &= ~24;
410 radarmapper.count |= 16;
414 { radarmapper.count |= 24;
419 radarmapper.count = stof(argv(i));
421 } // for the recursive call
424 radarmapper.ltime = stof(argv(i));
426 } // for the recursive call
427 case "--res": // minor alias
430 radarmapper.size_x = stof(argv(i));
432 radarmapper.size_y = stof(argv(i));
435 case "--qual": // minor alias
438 radarmapper.size_z = stof(argv(i));
450 if (radarmapper) // after doing the arguments, see if we successfully went forward.
452 LOG_INFO("Radarmap entity spawned.\n");
453 return true; // if so, don't print usage.