4 #include <common/command/_mod.qh>
5 #include <common/mapobjects/triggers.qh>
9 #include <server/intermission.qh>
11 #include <common/util.qh>
13 #include <lib/csqcmodel/sv_model.qh>
15 // ===============================================
16 // Generates radar map images for use in the HUD
17 // ===============================================
19 float FullTraceFraction(vector a, vector mi, vector ma, vector b)
32 while (vdist(c - b, >, 1))
36 tracebox(c, mi, ma, b, MOVE_WORLDONLY, NULL);
39 if (!trace_startsolid)
41 black += vlen(trace_endpos - c);
45 n += tracebox_inverted(c, mi, ma, b, MOVE_WORLDONLY, NULL, false, NULL);
47 white += vlen(trace_endpos - c);
51 if (n > 200) LOG_TRACE("HOLY SHIT! FullTraceFraction: ", ftos(n), " total traces, ", ftos(m), " iterations");
53 return white / (black + white);
55 float RadarMapAtPoint_Trace(float e, float f, float w, float h, float zmin, float zsize, float q)
60 ma = '1 0 0' * w + '0 1 0' * h;
61 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
62 b = '1 0 0' * e + '0 1 0' * f + '0 0 1' * (zsize + zmin);
64 return FullTraceFraction(a, mi, ma, b);
66 float RadarMapAtPoint_LineBlock(float e, float f, float w, float h, float zmin, float zsize, float q)
73 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
76 dz = (zsize / q) * '0 0 1';
77 ma = '1 0 0' * w + '0 1 0' * h + dz;
78 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
80 if (e < world.absmin.x - w) return 0;
81 if (f < world.absmin.y - h) return 0;
82 if (e > world.absmax.x) return 0;
83 if (f > world.absmax.y) return 0;
86 for (i = 0; i < q; ++i)
89 v1 = v2 = o + dz * i + mi;
90 v1_x += random() * (ma.x - mi.x);
91 v1_y += random() * (ma.y - mi.y);
92 v1_z += random() * (ma.z - mi.z);
93 v2_x += random() * (ma.x - mi.x);
94 v2_y += random() * (ma.y - mi.y);
95 v2_z += random() * (ma.z - mi.z);
96 traceline(v1, v2, MOVE_WORLDONLY, NULL);
97 if (trace_startsolid || trace_fraction < 1) ++r;
101 float RadarMapAtPoint_Block(float e, float f, float w, float h, float zmin, float zsize, float q)
108 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
111 dz = (zsize / q) * '0 0 1';
112 ma = '1 0 0' * w + '0 1 0' * h + dz;
113 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
115 if (e < world.absmin.x - w) return 0;
116 if (f < world.absmin.y - h) return 0;
117 if (e > world.absmax.x) return 0;
118 if (f > world.absmax.y) return 0;
121 for (i = 0; i < q; ++i)
123 tracebox(o + dz * i, mi, ma, o + dz * i, MOVE_WORLDONLY, NULL);
124 if (trace_startsolid) ++r;
128 float RadarMapAtPoint_Sample(float e, float f, float w, float h, float zmin, float zsize, float q)
132 q *= 4; // choose q so it matches the regular algorithm in speed
135 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
138 ma = '1 0 0' * w + '0 1 0' * h;
139 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
140 b = '1 0 0' * w + '0 1 0' * h + '0 0 1' * zsize;
145 for (i = 0; i < q; ++i)
148 v.x = a.x + random() * b.x;
149 v.y = a.y + random() * b.y;
150 v.z = a.z + random() * b.z;
151 traceline(v, v, MOVE_WORLDONLY, NULL);
152 if (trace_startsolid) ++c;
157 void sharpen_set(int b, float v)
159 sharpen_buffer[b + 2 * RADAR_WIDTH_MAX] = v;
161 float sharpen_getpixel(int b, int c)
164 if (b >= RADAR_WIDTH_MAX) return 0;
167 return sharpen_buffer[b + c * RADAR_WIDTH_MAX];
169 float sharpen_get(float b, float a)
171 float sum = sharpen_getpixel(b, 1);
172 if (a == 0) return sum;
174 sum -= sharpen_getpixel(b - 1, 0);
175 sum -= sharpen_getpixel(b - 1, 1);
176 sum -= sharpen_getpixel(b - 1, 2);
177 sum -= sharpen_getpixel(b + 1, 0);
178 sum -= sharpen_getpixel(b + 1, 1);
179 sum -= sharpen_getpixel(b + 1, 2);
180 sum -= sharpen_getpixel(b, 0);
181 sum -= sharpen_getpixel(b, 2);
182 return bound(0, sum * a, 1);
184 void sharpen_shift(int w)
186 for (int i = 0; i < w; ++i)
188 sharpen_buffer[i] = sharpen_buffer[i + RADAR_WIDTH_MAX];
189 sharpen_buffer[i + RADAR_WIDTH_MAX] = sharpen_buffer[i + 2 * RADAR_WIDTH_MAX];
190 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
193 void sharpen_init(int w)
195 for (int i = 0; i < w; ++i)
197 sharpen_buffer[i] = 0;
198 sharpen_buffer[i + RADAR_WIDTH_MAX] = 0;
199 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
204 if (radarmapper.count & 4)
208 else if (radarmapper.count & 2)
210 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"));
216 void RadarMap_Think(entity this)
220 // size: pixel width/height
221 // maxs: cell width/height
230 get_mi_min_max_texcoords(1);
231 this.mins = mi_picmin;
232 this.maxs_x = (mi_picmax.x - mi_picmin.x) / this.size.x;
233 this.maxs_y = (mi_picmax.y - mi_picmin.y) / this.size.y;
234 this.maxs_z = mi_max.z - mi_min.z;
235 LOG_INFO("Picture mins/maxs: ", ftos(this.maxs.x), " and ", ftos(this.maxs.y), " should match");
236 this.netname = strzone(strcat("gfx/", mi_shortname, "_radar.xpm"));
237 if (!(this.count & 1))
239 this.cnt = fopen(this.netname, FILE_READ);
240 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.tga"), FILE_READ);
241 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.png"), FILE_READ);
242 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.jpg"), FILE_READ);
243 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.tga"), FILE_READ);
244 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.png"), FILE_READ);
245 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.jpg"), FILE_READ);
250 LOG_INFO(this.netname, " already exists, aborting (you may want to specify --force)");
255 this.cnt = fopen(this.netname, FILE_WRITE);
258 LOG_INFO("Error writing ", this.netname);
263 LOG_INFO("Writing to ", this.netname, "...");
264 fputs(this.cnt, "/* XPM */\n");
265 fputs(this.cnt, "static char *RadarMap[] = {\n");
266 fputs(this.cnt, "/* columns rows colors chars-per-pixel */\n");
267 fputs(this.cnt, strcat("\"", ftos(this.size.x), " ", ftos(this.size.y), " 256 2\",\n"));
268 for (i = 0; i < 256; ++i)
270 si = substring(doublehex, i * 2, 2);
271 fputs(this.cnt, strcat("\"", si, " c #", si, si, si, "\",\n"));
274 this.nextthink = time;
275 sharpen_init(this.size.x);
277 else if (this.frame <= this.size.y)
279 // fill the sharpen buffer with this line
280 sharpen_shift(this.size.x);
287 for (x = 0; x < this.size.x; ++x)
289 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);
294 for (x = 0; x < this.size.x; ++x)
296 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);
301 for (x = 0; x < this.size.x; ++x)
303 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);
308 for (x = 0; x < this.size.x; ++x)
310 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);
316 // do we have enough lines?
319 // write a pixel line
320 fputs(this.cnt, "\"");
321 for (x = 0; x < this.size.x; ++x)
323 l = sharpen_get(x, this.ltime);
324 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
326 if (this.frame == this.size.y)
328 fputs(this.cnt, "\"\n");
332 fputs(this.cnt, "\",\n");
333 LOG_INFO(ftos(this.size.y - this.frame), " lines left");
337 // is this the last line? then write back the missing line
338 if (this.frame == this.size.y)
340 sharpen_shift(this.size.x);
341 // write a pixel line
342 fputs(this.cnt, "\"");
343 for (x = 0; x < this.size.x; ++x)
345 l = sharpen_get(x, this.ltime);
346 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
348 if (this.frame == this.size.y)
350 fputs(this.cnt, "\"\n");
354 fputs(this.cnt, "\",\n");
355 LOG_INFO(ftos(this.size.y - this.frame), " lines left");
360 this.nextthink = time;
365 fputs(this.cnt, "};\n");
367 LOG_INFO("Finished. Please edit data/", this.netname, " with an image editing application and place it in the TGA format in the gfx folder.");
372 bool RadarMap_Make(int argc)
378 radarmapper = new(radarmapper);
379 setthink(radarmapper, RadarMap_Think);
380 radarmapper.nextthink = time;
381 radarmapper.count = 8; // default to the --trace method, as it is faster now
382 radarmapper.ltime = 1;
383 radarmapper.size = '512 512 1';
384 for (i = 1; i < argc; ++i)
389 { radarmapper.count |= 1;
393 { radarmapper.count |= 2;
397 { radarmapper.count |= 4;
401 { radarmapper.count &= ~24;
405 { radarmapper.count &= ~24;
406 radarmapper.count |= 8;
410 { radarmapper.count &= ~24;
411 radarmapper.count |= 16;
415 { radarmapper.count |= 24;
420 radarmapper.count = stof(argv(i));
422 } // for the recursive call
425 radarmapper.ltime = stof(argv(i));
427 } // for the recursive call
428 case "--res": // minor alias
431 radarmapper.size_x = stof(argv(i));
433 radarmapper.size_y = stof(argv(i));
436 case "--qual": // minor alias
439 radarmapper.size_z = stof(argv(i));
451 if (radarmapper) // after doing the arguments, see if we successfully went forward.
453 LOG_INFO("Radarmap entity spawned.");
454 return true; // if so, don't print usage.