1 #include "../../common/command/command.qh"
5 #include "../g_world.qh"
7 #include "../../common/util.qh"
9 #include "../../csqcmodellib/sv_model.qh"
11 // ===============================================
12 // Generates radar map images for use in the HUD
13 // ===============================================
15 float FullTraceFraction(vector a, vector mi, vector ma, vector b)
28 while(vlen(c - b) > 1)
32 tracebox(c, mi, ma, b, MOVE_WORLDONLY, world);
37 black += vlen(trace_endpos - c);
41 n += tracebox_inverted(c, mi, ma, b, MOVE_WORLDONLY, world, false, world);
43 white += vlen(trace_endpos - c);
48 LOG_TRACE("HOLY SHIT! FullTraceFraction: ", ftos(n), " total traces, ", ftos(m), " iterations\n");
50 return white / (black + white);
52 float RadarMapAtPoint_Trace(float x, float y, 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' * x + '0 1 0' * y + '0 0 1' * zmin;
59 b = '1 0 0' * x + '0 1 0' * y + '0 0 1' * (zsize + zmin);
61 return FullTraceFraction(a, mi, ma, b);
63 float RadarMapAtPoint_LineBlock(float x, float y, 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' * x + '0 1 0' * y + '0 0 1' * zmin;
77 if(x < world.absmin.x - w)
79 if(y < world.absmin.y - h)
81 if(x > world.absmax.x)
83 if(y > world.absmax.y)
87 for(i = 0; i < q; ++i)
90 v1 = v2 = o + dz * i + mi;
91 v1_x += random() * (ma.x - mi.x);
92 v1_y += random() * (ma.y - mi.y);
93 v1_z += random() * (ma.z - mi.z);
94 v2_x += random() * (ma.x - mi.x);
95 v2_y += random() * (ma.y - mi.y);
96 v2_z += random() * (ma.z - mi.z);
97 traceline(v1, v2, MOVE_WORLDONLY, world);
98 if(trace_startsolid || trace_fraction < 1)
103 float RadarMapAtPoint_Block(float x, float y, float w, float h, float zmin, float zsize, float q)
110 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
113 dz = (zsize / q) * '0 0 1';
114 ma = '1 0 0' * w + '0 1 0' * h + dz;
115 o = '1 0 0' * x + '0 1 0' * y + '0 0 1' * zmin;
117 if(x < world.absmin.x - w)
119 if(y < world.absmin.y - h)
121 if(x > world.absmax.x)
123 if(y > world.absmax.y)
127 for(i = 0; i < q; ++i)
129 tracebox(o + dz * i, mi, ma, o + dz * i, MOVE_WORLDONLY, world);
135 float RadarMapAtPoint_Sample(float x, float y, float w, float h, float zmin, float zsize, float q)
139 q *= 4; // choose q so it matches the regular algorithm in speed
142 // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
145 ma = '1 0 0' * w + '0 1 0' * h;
146 a = '1 0 0' * x + '0 1 0' * y + '0 0 1' * zmin;
147 b = '1 0 0' * w + '0 1 0' * h + '0 0 1' * zsize;
152 for(i = 0; i < q; ++i)
155 v.x = a.x + random() * b.x;
156 v.y = a.y + random() * b.y;
157 v.z = a.z + random() * b.z;
158 traceline(v, v, MOVE_WORLDONLY, world);
165 void sharpen_set(int x, float v)
167 sharpen_buffer[x + 2 * RADAR_WIDTH_MAX] = v;
169 float sharpen_getpixel(int x, int y)
173 if(x >= RADAR_WIDTH_MAX)
179 return sharpen_buffer[x + y * RADAR_WIDTH_MAX];
181 float sharpen_get(float x, float a)
183 float sum = sharpen_getpixel(x, 1);
187 sum -= sharpen_getpixel(x - 1, 0);
188 sum -= sharpen_getpixel(x - 1, 1);
189 sum -= sharpen_getpixel(x - 1, 2);
190 sum -= sharpen_getpixel(x + 1, 0);
191 sum -= sharpen_getpixel(x + 1, 1);
192 sum -= sharpen_getpixel(x + 1, 2);
193 sum -= sharpen_getpixel(x, 0);
194 sum -= sharpen_getpixel(x, 2);
195 return bound(0, sum * a, 1);
197 void sharpen_shift(int w)
199 for(int i = 0; i < w; ++i)
201 sharpen_buffer[i] = sharpen_buffer[i + RADAR_WIDTH_MAX];
202 sharpen_buffer[i + RADAR_WIDTH_MAX] = sharpen_buffer[i + 2 * RADAR_WIDTH_MAX];
203 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
206 void sharpen_init(int w)
208 for(int i = 0; i < w; ++i)
210 sharpen_buffer[i] = 0;
211 sharpen_buffer[i + RADAR_WIDTH_MAX] = 0;
212 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
217 if(radarmapper.count & 4)
221 else if(radarmapper.count & 2)
223 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"));
229 void RadarMap_Think()
233 // size: pixel width/height
234 // maxs: cell width/height
243 get_mi_min_max_texcoords(1);
244 self.mins = mi_picmin;
245 self.maxs_x = (mi_picmax.x - mi_picmin.x) / self.size.x;
246 self.maxs_y = (mi_picmax.y - mi_picmin.y) / self.size.y;
247 self.maxs_z = mi_max.z - mi_min.z;
248 LOG_INFO("Picture mins/maxs: ", ftos(self.maxs.x), " and ", ftos(self.maxs.y), " should match\n");
249 self.netname = strzone(strcat("gfx/", mi_shortname, "_radar.xpm"));
250 if(!(self.count & 1))
252 self.cnt = fopen(self.netname, FILE_READ);
254 self.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.tga"), FILE_READ);
256 self.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.png"), FILE_READ);
258 self.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.jpg"), FILE_READ);
260 self.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.tga"), FILE_READ);
262 self.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.png"), FILE_READ);
264 self.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.jpg"), FILE_READ);
269 LOG_INFO(self.netname, " already exists, aborting (you may want to specify --force)\n");
274 self.cnt = fopen(self.netname, FILE_WRITE);
277 LOG_INFO("Error writing ", self.netname, "\n");
282 LOG_INFO("Writing to ", self.netname, "...\n");
283 fputs(self.cnt, "/* XPM */\n");
284 fputs(self.cnt, "static char *RadarMap[] = {\n");
285 fputs(self.cnt, "/* columns rows colors chars-per-pixel */\n");
286 fputs(self.cnt, strcat("\"", ftos(self.size.x), " ", ftos(self.size.y), " 256 2\",\n"));
287 for(i = 0; i < 256; ++i)
289 si = substring(doublehex, i*2, 2);
290 fputs(self.cnt, strcat("\"", si, " c #", si, si, si, "\",\n"));
293 self.nextthink = time;
294 sharpen_init(self.size.x);
296 else if(self.frame <= self.size.y)
298 // fill the sharpen buffer with this line
299 sharpen_shift(self.size.x);
306 for(x = 0; x < self.size.x; ++x)
308 l = RadarMapAtPoint_Block(self.mins.x + x * self.maxs.x, self.mins.y + (self.size.y - self.frame) * self.maxs.y, self.maxs.x, self.maxs.y, self.mins.z, self.maxs.z, self.size.z);
313 for(x = 0; x < self.size.x; ++x)
315 l = RadarMapAtPoint_Trace(self.mins.x + x * self.maxs.x, self.mins.y + (self.size.y - self.frame) * self.maxs.y, self.maxs.x, self.maxs.y, self.mins.z, self.maxs.z, self.size.z);
320 for(x = 0; x < self.size.x; ++x)
322 l = RadarMapAtPoint_Sample(self.mins.x + x * self.maxs.x, self.mins.y + (self.size.y - self.frame) * self.maxs.y, self.maxs.x, self.maxs.y, self.mins.z, self.maxs.z, self.size.z);
327 for(x = 0; x < self.size.x; ++x)
329 l = RadarMapAtPoint_LineBlock(self.mins.x + x * self.maxs.x, self.mins.y + (self.size.y - self.frame) * self.maxs.y, self.maxs.x, self.maxs.y, self.mins.z, self.maxs.z, self.size.z);
335 // do we have enough lines?
338 // write a pixel line
339 fputs(self.cnt, "\"");
340 for(x = 0; x < self.size.x; ++x)
342 l = sharpen_get(x, self.ltime);
343 fputs(self.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
345 if(self.frame == self.size.y)
346 fputs(self.cnt, "\"\n");
349 fputs(self.cnt, "\",\n");
350 LOG_INFO(ftos(self.size.y - self.frame), " lines left\n");
354 // is this the last line? then write back the missing line
355 if(self.frame == self.size.y)
357 sharpen_shift(self.size.x);
358 // write a pixel line
359 fputs(self.cnt, "\"");
360 for(x = 0; x < self.size.x; ++x)
362 l = sharpen_get(x, self.ltime);
363 fputs(self.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
365 if(self.frame == self.size.y)
366 fputs(self.cnt, "\"\n");
369 fputs(self.cnt, "\",\n");
370 LOG_INFO(ftos(self.size.y - self.frame), " lines left\n");
375 self.nextthink = time;
380 fputs(self.cnt, "};\n");
382 LOG_INFO("Finished. Please edit data/", self.netname, " with an image editing application and place it in the TGA format in the gfx folder.\n");
387 float RadarMap_Make(float argc)
393 radarmapper = spawn();
394 radarmapper.classname = "radarmapper";
395 radarmapper.think = RadarMap_Think;
396 radarmapper.nextthink = time;
397 radarmapper.count = 8; // default to the --trace method, as it is faster now
398 radarmapper.ltime = 1;
399 radarmapper.size = '512 512 1';
400 for(i = 1; i < argc; ++i)
404 case "--force": { radarmapper.count |= 1; break; }
405 case "--loop": { radarmapper.count |= 2; break; }
406 case "--quit": { radarmapper.count |= 4; break; }
407 case "--block": { radarmapper.count &= ~24; break; }
408 case "--trace": { radarmapper.count &= ~24; radarmapper.count |= 8; break; }
409 case "--sample": { radarmapper.count &= ~24; radarmapper.count |= 16; break; }
410 case "--lineblock": { radarmapper.count |= 24; break; }
411 case "--flags": { ++i; radarmapper.count = stof(argv(i)); break; } // for the recursive call
412 case "--sharpen": { ++i; radarmapper.ltime = stof(argv(i)); break; } // for the recursive call
413 case "--res": // minor alias
414 case "--resolution": { ++i; radarmapper.size_x = stof(argv(i)); ++i; radarmapper.size_y = stof(argv(i)); break; }
415 case "--qual": // minor alias
416 case "--quality": { ++i; radarmapper.size_z = stof(argv(i)); break; }
426 if(radarmapper) // after doing the arguments, see if we successfully went forward.
428 LOG_INFO("Radarmap entity spawned.\n");
429 return true; // if so, don't print usage.