2 #include <common/command/_mod.qh>
5 #include "../g_world.qh"
6 #include "../g_subs.qh"
8 #include <common/util.qh>
10 #include <lib/csqcmodel/sv_model.qh>
12 // ===============================================
13 // Generates radar map images for use in the HUD
14 // ===============================================
16 float FullTraceFraction(vector a, vector mi, vector ma, vector b)
29 while (vdist(c - b, >, 1))
33 tracebox(c, mi, ma, b, MOVE_WORLDONLY, NULL);
36 if (!trace_startsolid)
38 black += vlen(trace_endpos - c);
42 n += tracebox_inverted(c, mi, ma, b, MOVE_WORLDONLY, NULL, false, NULL);
44 white += vlen(trace_endpos - c);
48 if (n > 200) LOG_TRACE("HOLY SHIT! FullTraceFraction: ", ftos(n), " total traces, ", ftos(m), " iterations");
50 return white / (black + white);
52 float RadarMapAtPoint_Trace(float e, float f, float w, float h, float zmin, float zsize, float q)
57 ma = '1 0 0' * w + '0 1 0' * h;
58 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
59 b = '1 0 0' * e + '0 1 0' * f + '0 0 1' * (zsize + zmin);
61 return FullTraceFraction(a, mi, ma, b);
63 float RadarMapAtPoint_LineBlock(float e, float f, float w, float h, float zmin, float zsize, float q)
70 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
73 dz = (zsize / q) * '0 0 1';
74 ma = '1 0 0' * w + '0 1 0' * h + dz;
75 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
77 if (e < world.absmin.x - w) return 0;
78 if (f < world.absmin.y - h) return 0;
79 if (e > world.absmax.x) return 0;
80 if (f > world.absmax.y) return 0;
83 for (i = 0; i < q; ++i)
86 v1 = v2 = o + dz * i + mi;
87 v1_x += random() * (ma.x - mi.x);
88 v1_y += random() * (ma.y - mi.y);
89 v1_z += random() * (ma.z - mi.z);
90 v2_x += random() * (ma.x - mi.x);
91 v2_y += random() * (ma.y - mi.y);
92 v2_z += random() * (ma.z - mi.z);
93 traceline(v1, v2, MOVE_WORLDONLY, NULL);
94 if (trace_startsolid || trace_fraction < 1) ++r;
98 float RadarMapAtPoint_Block(float e, float f, float w, float h, float zmin, float zsize, float q)
105 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
108 dz = (zsize / q) * '0 0 1';
109 ma = '1 0 0' * w + '0 1 0' * h + dz;
110 o = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
112 if (e < world.absmin.x - w) return 0;
113 if (f < world.absmin.y - h) return 0;
114 if (e > world.absmax.x) return 0;
115 if (f > world.absmax.y) return 0;
118 for (i = 0; i < q; ++i)
120 tracebox(o + dz * i, mi, ma, o + dz * i, MOVE_WORLDONLY, NULL);
121 if (trace_startsolid) ++r;
125 float RadarMapAtPoint_Sample(float e, float f, float w, float h, float zmin, float zsize, float q)
129 q *= 4; // choose q so it matches the regular algorithm in speed
132 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
135 ma = '1 0 0' * w + '0 1 0' * h;
136 a = '1 0 0' * e + '0 1 0' * f + '0 0 1' * zmin;
137 b = '1 0 0' * w + '0 1 0' * h + '0 0 1' * zsize;
142 for (i = 0; i < q; ++i)
145 v.x = a.x + random() * b.x;
146 v.y = a.y + random() * b.y;
147 v.z = a.z + random() * b.z;
148 traceline(v, v, MOVE_WORLDONLY, NULL);
149 if (trace_startsolid) ++c;
154 void sharpen_set(int b, float v)
156 sharpen_buffer[b + 2 * RADAR_WIDTH_MAX] = v;
158 float sharpen_getpixel(int b, int c)
161 if (b >= RADAR_WIDTH_MAX) return 0;
164 return sharpen_buffer[b + c * RADAR_WIDTH_MAX];
166 float sharpen_get(float b, float a)
168 float sum = sharpen_getpixel(b, 1);
169 if (a == 0) return sum;
171 sum -= sharpen_getpixel(b - 1, 0);
172 sum -= sharpen_getpixel(b - 1, 1);
173 sum -= sharpen_getpixel(b - 1, 2);
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, 0);
178 sum -= sharpen_getpixel(b, 2);
179 return bound(0, sum * a, 1);
181 void sharpen_shift(int w)
183 for (int i = 0; i < w; ++i)
185 sharpen_buffer[i] = sharpen_buffer[i + RADAR_WIDTH_MAX];
186 sharpen_buffer[i + RADAR_WIDTH_MAX] = sharpen_buffer[i + 2 * RADAR_WIDTH_MAX];
187 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
190 void sharpen_init(int w)
192 for (int i = 0; i < w; ++i)
194 sharpen_buffer[i] = 0;
195 sharpen_buffer[i + RADAR_WIDTH_MAX] = 0;
196 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
201 if (radarmapper.count & 4)
205 else if (radarmapper.count & 2)
207 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"));
213 void RadarMap_Think(entity this)
217 // size: pixel width/height
218 // maxs: cell width/height
227 get_mi_min_max_texcoords(1);
228 this.mins = mi_picmin;
229 this.maxs_x = (mi_picmax.x - mi_picmin.x) / this.size.x;
230 this.maxs_y = (mi_picmax.y - mi_picmin.y) / this.size.y;
231 this.maxs_z = mi_max.z - mi_min.z;
232 LOG_INFO("Picture mins/maxs: ", ftos(this.maxs.x), " and ", ftos(this.maxs.y), " should match\n");
233 this.netname = strzone(strcat("gfx/", mi_shortname, "_radar.xpm"));
234 if (!(this.count & 1))
236 this.cnt = fopen(this.netname, FILE_READ);
237 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.tga"), FILE_READ);
238 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.png"), FILE_READ);
239 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.jpg"), FILE_READ);
240 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.tga"), FILE_READ);
241 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.png"), FILE_READ);
242 if (this.cnt < 0) this.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.jpg"), FILE_READ);
247 LOG_INFO(this.netname, " already exists, aborting (you may want to specify --force)\n");
252 this.cnt = fopen(this.netname, FILE_WRITE);
255 LOG_INFO("Error writing ", this.netname, "\n");
260 LOG_INFO("Writing to ", this.netname, "...\n");
261 fputs(this.cnt, "/* XPM */\n");
262 fputs(this.cnt, "static char *RadarMap[] = {\n");
263 fputs(this.cnt, "/* columns rows colors chars-per-pixel */\n");
264 fputs(this.cnt, strcat("\"", ftos(this.size.x), " ", ftos(this.size.y), " 256 2\",\n"));
265 for (i = 0; i < 256; ++i)
267 si = substring(doublehex, i * 2, 2);
268 fputs(this.cnt, strcat("\"", si, " c #", si, si, si, "\",\n"));
271 this.nextthink = time;
272 sharpen_init(this.size.x);
274 else if (this.frame <= this.size.y)
276 // fill the sharpen buffer with this line
277 sharpen_shift(this.size.x);
284 for (x = 0; x < this.size.x; ++x)
286 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);
291 for (x = 0; x < this.size.x; ++x)
293 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);
298 for (x = 0; x < this.size.x; ++x)
300 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);
305 for (x = 0; x < this.size.x; ++x)
307 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);
313 // do we have enough lines?
316 // write a pixel line
317 fputs(this.cnt, "\"");
318 for (x = 0; x < this.size.x; ++x)
320 l = sharpen_get(x, this.ltime);
321 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
323 if (this.frame == this.size.y)
325 fputs(this.cnt, "\"\n");
329 fputs(this.cnt, "\",\n");
330 LOG_INFO(ftos(this.size.y - this.frame), " lines left\n");
334 // is this the last line? then write back the missing line
335 if (this.frame == this.size.y)
337 sharpen_shift(this.size.x);
338 // write a pixel line
339 fputs(this.cnt, "\"");
340 for (x = 0; x < this.size.x; ++x)
342 l = sharpen_get(x, this.ltime);
343 fputs(this.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
345 if (this.frame == this.size.y)
347 fputs(this.cnt, "\"\n");
351 fputs(this.cnt, "\",\n");
352 LOG_INFO(ftos(this.size.y - this.frame), " lines left\n");
357 this.nextthink = time;
362 fputs(this.cnt, "};\n");
364 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");
369 float RadarMap_Make(float argc)
375 radarmapper = new(radarmapper);
376 setthink(radarmapper, RadarMap_Think);
377 radarmapper.nextthink = time;
378 radarmapper.count = 8; // default to the --trace method, as it is faster now
379 radarmapper.ltime = 1;
380 radarmapper.size = '512 512 1';
381 for (i = 1; i < argc; ++i)
386 { radarmapper.count |= 1;
390 { radarmapper.count |= 2;
394 { radarmapper.count |= 4;
398 { radarmapper.count &= ~24;
402 { radarmapper.count &= ~24;
403 radarmapper.count |= 8;
407 { radarmapper.count &= ~24;
408 radarmapper.count |= 16;
412 { radarmapper.count |= 24;
417 radarmapper.count = stof(argv(i));
419 } // for the recursive call
422 radarmapper.ltime = stof(argv(i));
424 } // for the recursive call
425 case "--res": // minor alias
428 radarmapper.size_x = stof(argv(i));
430 radarmapper.size_y = stof(argv(i));
433 case "--qual": // minor alias
436 radarmapper.size_z = stof(argv(i));
448 if (radarmapper) // after doing the arguments, see if we successfully went forward.
450 LOG_INFO("Radarmap entity spawned.\n");
451 return true; // if so, don't print usage.