playerdemos - recording actions of a player and playing them back using bots, to...
[divverent/nexuiz.git] / data / qcsrc / server / gamecommand.qc
1 string GotoMap(string m);
2
3 float FullTraceFraction(vector a, vector mi, vector ma, vector b)
4 {
5         vector c;
6         float white, black;
7
8         white = 0.001;
9         black = 0.001;
10
11         c = a;
12
13         float n, m;
14         n = m = 0;
15
16         while(vlen(c - b) > 1)
17         {
18                 ++m;
19
20                 tracebox(c, mi, ma, b, MOVE_WORLDONLY, world);
21                 ++n;
22
23                 if(!trace_startsolid)
24                 {
25                         black += vlen(trace_endpos - c);
26                         c = trace_endpos;
27                 }
28
29                 n += tracebox_inverted(c, mi, ma, b, MOVE_WORLDONLY, world);
30
31                 white += vlen(trace_endpos - c);
32                 c = trace_endpos;
33         }
34
35         if(n > 200)
36                 dprint("HOLY SHIT! FullTraceFraction: ", ftos(n), " total traces, ", ftos(m), " iterations\n");
37
38         return white / (black + white);
39 }
40
41 float RadarMapAtPoint_Trace(float x, float y, float w, float h, float zmin, float zsize, float q)
42 {
43         vector a, b, mi, ma;
44
45         mi = '0 0 0';
46         ma = '1 0 0' * w + '0 1 0' * h;
47         a = '1 0 0' * x + '0 1 0' * y + '0 0 1' * zmin;
48         b = '1 0 0' * x + '0 1 0' * y + '0 0 1' * (zsize + zmin);
49
50         return FullTraceFraction(a, mi, ma, b);
51 }
52 float RadarMapAtPoint_Block(float x, float y, float w, float h, float zmin, float zsize, float q)
53 {
54         vector o, mi, ma;
55         float i, r;
56         vector dz;
57
58         q = 256 * q - 1;
59         // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
60
61         mi = '0 0 0';
62         dz = (zsize / q) * '0 0 1';
63         ma = '1 0 0' * w + '0 1 0' * h + dz;
64         o = '1 0 0' * x + '0 1 0' * y + '0 0 1' * zmin;
65
66         if(x < world.absmin_x - w)
67                 return 0;
68         if(y < world.absmin_y - h)
69                 return 0;
70         if(x > world.absmax_x)
71                 return 0;
72         if(y > world.absmax_y)
73                 return 0;
74
75         r = 0;
76         for(i = 0; i < q; ++i)
77         {
78                 tracebox(o + dz * i, mi, ma, o + dz * i, MOVE_WORLDONLY, world);
79                 if(trace_startsolid)
80                         ++r;
81         }
82         return r / q;
83 }
84 float RadarMapAtPoint_Sample(float x, float y, float w, float h, float zmin, float zsize, float q)
85 {
86         vector a, b, mi, ma;
87
88         q *= 4; // choose q so it matches the regular algorithm in speed
89
90         q = 256 * q - 1;
91         // 256q-1 is the ideal sample count to map equal amount of sample values to one pixel value
92
93         mi = '0 0 0';
94         ma = '1 0 0' * w + '0 1 0' * h;
95         a = '1 0 0' * x + '0 1 0' * y + '0 0 1' * zmin;
96         b = '1 0 0' * w + '0 1 0' * h + '0 0 1' * zsize;
97
98         float c, i;
99         c = 0;
100
101         for(i = 0; i < q; ++i)
102         {
103                 vector v;
104                 v_x = a_x + random() * b_x;
105                 v_y = a_y + random() * b_y;
106                 v_z = a_z + random() * b_z;
107                 traceline(v, v, MOVE_WORLDONLY, world);
108                 if(trace_startsolid)
109                         ++c;
110         }
111
112         return c / q;
113 }
114
115 // FF is contained twice, to map 256 to FF too
116 // removes the need to bound()
117 string doublehex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFFFF";
118
119 float RADAR_WIDTH_MAX = 2048;
120 float RADAR_HEIGHT_MAX = 2048;
121 float sharpen_buffer[RADAR_WIDTH_MAX * 3];
122
123 void sharpen_set(float x, float v)
124 {
125         sharpen_buffer[x + 2 * RADAR_WIDTH_MAX] = v;
126 }
127
128 float sharpen_getpixel(float x, float y)
129 {
130         if(x < 0)
131                 return 0;
132         if(x >= RADAR_WIDTH_MAX)
133                 return 0;
134         if(y < 0)
135                 return 0;
136         if(y > 2)
137                 return 0;
138         return sharpen_buffer[x + y * RADAR_WIDTH_MAX];
139 }
140
141 float sharpen_get(float x, float a)
142 {
143         float sum;
144         sum = sharpen_getpixel(x, 1);
145         if(a == 0)
146                 return sum;
147         sum *= (8 + 1/a);
148         sum -= sharpen_getpixel(x - 1, 0);
149         sum -= sharpen_getpixel(x - 1, 1);
150         sum -= sharpen_getpixel(x - 1, 2);
151         sum -= sharpen_getpixel(x + 1, 0);
152         sum -= sharpen_getpixel(x + 1, 1);
153         sum -= sharpen_getpixel(x + 1, 2);
154         sum -= sharpen_getpixel(x, 0);
155         sum -= sharpen_getpixel(x, 2);
156         return bound(0, sum * a, 1);
157 }
158
159 void sharpen_shift(float w)
160 {
161         float i;
162         for(i = 0; i < w; ++i)
163         {
164                 sharpen_buffer[i] = sharpen_buffer[i + RADAR_WIDTH_MAX];
165                 sharpen_buffer[i + RADAR_WIDTH_MAX] = sharpen_buffer[i + 2 * RADAR_WIDTH_MAX];
166                 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
167         }
168 }
169
170 void sharpen_init(float w)
171 {
172         float i;
173         for(i = 0; i < w; ++i)
174         {
175                 sharpen_buffer[i] = 0;
176                 sharpen_buffer[i + RADAR_WIDTH_MAX] = 0;
177                 sharpen_buffer[i + 2 * RADAR_WIDTH_MAX] = 0;
178         }
179 }
180
181 entity radarmapper;
182 void RadarMap_Next()
183 {
184         if(radarmapper.count & 4)
185         {
186                 localcmd("quit\n");
187         }
188         else if(radarmapper.count & 2)
189         {
190                 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"));
191                 GotoNextMap();
192         }
193         remove(radarmapper);
194         radarmapper = world;
195 }
196
197 // rough map entity
198 //   cnt: current line
199 //   size: pixel width/height
200 //   maxs: cell width/height
201 //   frame: counter
202 void RadarMap_Think()
203 {
204         float i, x, l;
205         string si;
206
207         if(self.frame == 0)
208         {
209                 // initialize
210                 get_mi_min_max_texcoords(1);
211                 self.mins = mi_picmin;
212                 self.maxs_x = (mi_picmax_x - mi_picmin_x) / self.size_x;
213                 self.maxs_y = (mi_picmax_y - mi_picmin_y) / self.size_y;
214                 self.maxs_z = mi_max_z - mi_min_z;
215                 print("Picture mins/maxs: ", ftos(self.maxs_x), " and ", ftos(self.maxs_y), " should match\n");
216                 self.netname = strzone(strcat("gfx/", mi_shortname, "_radar.xpm"));
217                 if(!(self.count & 1))
218                 {
219                         self.cnt = fopen(self.netname, FILE_READ);
220                         if(self.cnt < 0)
221                                 self.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.tga"), FILE_READ);
222                         if(self.cnt < 0)
223                                 self.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.png"), FILE_READ);
224                         if(self.cnt < 0)
225                                 self.cnt = fopen(strcat("gfx/", mi_shortname, "_radar.jpg"), FILE_READ);
226                         if(self.cnt < 0)
227                                 self.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.tga"), FILE_READ);
228                         if(self.cnt < 0)
229                                 self.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.png"), FILE_READ);
230                         if(self.cnt < 0)
231                                 self.cnt = fopen(strcat("gfx/", mi_shortname, "_mini.jpg"), FILE_READ);
232                         if(self.cnt >= 0)
233                         {
234                                 fclose(self.cnt);
235
236                                 print(self.netname, " already exists, aborting (you may want to specify --force)\n");
237                                 RadarMap_Next();
238                                 return;
239                         }
240                 }
241                 self.cnt = fopen(self.netname, FILE_WRITE);
242                 if(self.cnt < 0)
243                 {
244                         print("Error writing ", self.netname, "\n");
245                         remove(self);
246                         radarmapper = world;
247                         return;
248                 }
249                 print("Writing to ", self.netname, "...\n");
250                 fputs(self.cnt, "/* XPM */\n");
251                 fputs(self.cnt, "static char *RadarMap[] = {\n");
252                 fputs(self.cnt, "/* columns rows colors chars-per-pixel */\n");
253                 fputs(self.cnt, strcat("\"", ftos(self.size_x), " ", ftos(self.size_y), " 256 2\",\n"));
254                 for(i = 0; i < 256; ++i)
255                 {
256                         si = substring(doublehex, i*2, 2);
257                         fputs(self.cnt, strcat("\"", si, " c #", si, si, si, "\",\n"));
258                 }
259                 self.frame += 1;
260                 self.nextthink = time;
261                 sharpen_init(self.size_x);
262         }
263         else if(self.frame <= self.size_y)
264         {
265                 // fill the sharpen buffer with this line
266                 sharpen_shift(self.size_x);
267                 i = self.count & 24;
268
269                 switch(i)
270                 {
271                         case 0:
272                         default:
273                                 for(x = 0; x < self.size_x; ++x)
274                                 {
275                                         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);
276                                         sharpen_set(x, l);
277                                 }
278                                 break;
279                         case 8:
280                                 for(x = 0; x < self.size_x; ++x)
281                                 {
282                                         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);
283                                         sharpen_set(x, l);
284                                 }
285                                 break;
286                         case 16:
287                                 for(x = 0; x < self.size_x; ++x)
288                                 {
289                                         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);
290                                         sharpen_set(x, l);
291                                 }
292                                 break;
293                 }
294
295                 // do we have enough lines?
296                 if(self.frame >= 2)
297                 {
298                         // write a pixel line
299                         fputs(self.cnt, "\"");
300                         for(x = 0; x < self.size_x; ++x)
301                         {
302                                 l = sharpen_get(x, self.ltime);
303                                 fputs(self.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
304                         }
305                         if(self.frame == self.size_y)
306                                 fputs(self.cnt, "\"\n");
307                         else
308                         {
309                                 fputs(self.cnt, "\",\n");
310                                 print(ftos(self.size_y - self.frame), " lines left\n");
311                         }
312                 }
313
314                 // is this the last line? then write back the missing line
315                 if(self.frame == self.size_y)
316                 {
317                         sharpen_shift(self.size_x);
318                         // write a pixel line
319                         fputs(self.cnt, "\"");
320                         for(x = 0; x < self.size_x; ++x)
321                         {
322                                 l = sharpen_get(x, self.ltime);
323                                 fputs(self.cnt, substring(doublehex, 2 * floor(l * 256.0), 2));
324                         }
325                         if(self.frame == self.size_y)
326                                 fputs(self.cnt, "\"\n");
327                         else
328                         {
329                                 fputs(self.cnt, "\",\n");
330                                 print(ftos(self.size_y - self.frame), " lines left\n");
331                         }
332                 }
333
334                 self.frame += 1;
335                 self.nextthink = time;
336         }
337         else
338         {
339                 // close the file
340                 fputs(self.cnt, "};\n");
341                 fclose(self.cnt);
342                 print("Finished. Please edit data/", self.netname, " with an image editing application and place it in the TGA format in the gfx folder.\n");
343                 RadarMap_Next();
344         }
345 }
346
347 void RadarMap(float argc)
348 {
349         if(radarmapper)
350                 return;
351         float i;
352         radarmapper = spawn();
353         radarmapper.classname = "radarmapper";
354         radarmapper.think = RadarMap_Think;
355         radarmapper.nextthink = time;
356         radarmapper.count = 8; // default to the --trace method, as it is faster now
357         radarmapper.ltime = 1;
358         radarmapper.size_x = 512;
359         radarmapper.size_y = 512;
360         radarmapper.size_z = 1;
361
362         for(i = 1; i < argc; ++i)
363         {
364                 if(argv(i) == "--force")
365                         radarmapper.count |= 1;
366                 else if(argv(i) == "--loop")
367                         radarmapper.count |= 2;
368                 else if(argv(i) == "--quit")
369                         radarmapper.count |= 4;
370                 else if(argv(i) == "--block")
371                 {
372                         radarmapper.count &~= 24;
373                 }
374                 else if(argv(i) == "--trace")
375                 {
376                         radarmapper.count &~= 24;
377                         radarmapper.count |= 8;
378                 }
379                 else if(argv(i) == "--sample")
380                 {
381                         radarmapper.count &~= 24;
382                         radarmapper.count |= 16;
383                 }
384                 else if(argv(i) == "--flags") // for the recursive call
385                 {
386                         ++i;
387                         radarmapper.count = stof(argv(i));
388                 }
389                 else if(argv(i) == "--sharpen") // for the recursive call
390                 {
391                         ++i;
392                         radarmapper.ltime = stof(argv(i));
393                 }
394                 else if(argv(i) == "--res") // resolution
395                 {
396                         ++i;
397                         radarmapper.size_x = stof(argv(i));
398                         ++i;
399                         radarmapper.size_y = stof(argv(i));
400                 }
401                 else if(argv(i) == "--qual") // quality multiplier
402                 {
403                         ++i;
404                         radarmapper.size_z = stof(argv(i));
405                 }
406                 else
407                 {
408                         remove(radarmapper);
409                         radarmapper = world;
410                         print("Usage: sv_cmd radarmap [--force] [--loop] [--quit] [--block | --trace | --sample] [--sharpen N] [--res W H] [--qual Q]\n");
411                         print("The quality factor Q is roughly proportional to the time taken.\n");
412                         print("--trace supports no quality factor; its result should look like --block with infinite quality factor.\n");
413                         print("--block \n");
414                         return;
415                 }
416         }
417
418         print("Radarmap entity spawned.\n");
419 }
420
421 void BBox()
422 {
423         print("Original size: ", ftos(world.absmin_x), " ", ftos(world.absmin_y), " ", ftos(world.absmin_z));
424         print(" ", ftos(world.absmax_x), " ", ftos(world.absmax_y), " ", ftos(world.absmax_z), "\n");
425         print("Currently set size: ", ftos(world.mins_x), " ", ftos(world.mins_y), " ", ftos(world.mins_z));
426         print(" ", ftos(world.maxs_x), " ", ftos(world.maxs_y), " ", ftos(world.maxs_z), "\n");
427         print("Solid bounding box size:");
428
429         tracebox('1 0 0' * world.absmin_x,
430                  '0 1 0' * world.absmin_y + '0 0 1' * world.absmin_z,
431                  '0 1 0' * world.absmax_y + '0 0 1' * world.absmax_z,
432                  '1 0 0' * world.absmax_x,
433                          MOVE_WORLDONLY,
434                          world);
435         if(trace_startsolid)
436                 print(" ", ftos(world.absmin_x));
437         else
438                 print(" ", ftos(trace_endpos_x));
439
440         tracebox('0 1 0' * world.absmin_y,
441                  '1 0 0' * world.absmin_x + '0 0 1' * world.absmin_z,
442                  '1 0 0' * world.absmax_x + '0 0 1' * world.absmax_z,
443                  '0 1 0' * world.absmax_y,
444                          MOVE_WORLDONLY,
445                          world);
446         if(trace_startsolid)
447                 print(" ", ftos(world.absmin_y));
448         else
449                 print(" ", ftos(trace_endpos_y));
450
451         tracebox('0 0 1' * world.absmin_z,
452                  '1 0 0' * world.absmin_x + '0 1 0' * world.absmin_y,
453                  '1 0 0' * world.absmax_x + '0 1 0' * world.absmax_y,
454                  '0 0 1' * world.absmax_z,
455                          MOVE_WORLDONLY,
456                          world);
457         if(trace_startsolid)
458                 print(" ", ftos(world.absmin_z));
459         else
460                 print(" ", ftos(trace_endpos_z));
461
462         tracebox('1 0 0' * world.absmax_x,
463                  '0 1 0' * world.absmin_y + '0 0 1' * world.absmin_z,
464                  '0 1 0' * world.absmax_y + '0 0 1' * world.absmax_z,
465                  '1 0 0' * world.absmin_x,
466                          MOVE_WORLDONLY,
467                          world);
468         if(trace_startsolid)
469                 print(" ", ftos(world.absmax_x));
470         else
471                 print(" ", ftos(trace_endpos_x));
472
473         tracebox('0 1 0' * world.absmax_y,
474                  '1 0 0' * world.absmin_x + '0 0 1' * world.absmin_z,
475                  '1 0 0' * world.absmax_x + '0 0 1' * world.absmax_z,
476                  '0 1 0' * world.absmin_y,
477                          MOVE_WORLDONLY,
478                          world);
479         if(trace_startsolid)
480                 print(" ", ftos(world.absmax_y));
481         else
482                 print(" ", ftos(trace_endpos_y));
483
484         tracebox('0 0 1' * world.absmax_z,
485                  '1 0 0' * world.absmin_x + '0 1 0' * world.absmin_y,
486                  '1 0 0' * world.absmax_x + '0 1 0' * world.absmax_y,
487                  '0 0 1' * world.absmin_z,
488                          MOVE_WORLDONLY,
489                          world);
490         if(trace_startsolid)
491                 print(" ", ftos(world.absmax_z));
492         else
493                 print(" ", ftos(trace_endpos_z));
494
495         print("\n");
496 }
497
498 void EffectIndexDump()
499 {
500         float d;
501         float fh;
502         string s;
503
504         d = db_create();
505
506         print("begin of effects list\n");
507         db_put(d, "TE_GUNSHOT", "1"); print("effect TE_GUNSHOT is ", ftos(particleeffectnum("TE_GUNSHOT")), "\n");
508         db_put(d, "TE_GUNSHOTQUAD", "1"); print("effect TE_GUNSHOTQUAD is ", ftos(particleeffectnum("TE_GUNSHOTQUAD")), "\n");
509         db_put(d, "TE_SPIKE", "1"); print("effect TE_SPIKE is ", ftos(particleeffectnum("TE_SPIKE")), "\n");
510         db_put(d, "TE_SPIKEQUAD", "1"); print("effect TE_SPIKEQUAD is ", ftos(particleeffectnum("TE_SPIKEQUAD")), "\n");
511         db_put(d, "TE_SUPERSPIKE", "1"); print("effect TE_SUPERSPIKE is ", ftos(particleeffectnum("TE_SUPERSPIKE")), "\n");
512         db_put(d, "TE_SUPERSPIKEQUAD", "1"); print("effect TE_SUPERSPIKEQUAD is ", ftos(particleeffectnum("TE_SUPERSPIKEQUAD")), "\n");
513         db_put(d, "TE_WIZSPIKE", "1"); print("effect TE_WIZSPIKE is ", ftos(particleeffectnum("TE_WIZSPIKE")), "\n");
514         db_put(d, "TE_KNIGHTSPIKE", "1"); print("effect TE_KNIGHTSPIKE is ", ftos(particleeffectnum("TE_KNIGHTSPIKE")), "\n");
515         db_put(d, "TE_EXPLOSION", "1"); print("effect TE_EXPLOSION is ", ftos(particleeffectnum("TE_EXPLOSION")), "\n");
516         db_put(d, "TE_EXPLOSIONQUAD", "1"); print("effect TE_EXPLOSIONQUAD is ", ftos(particleeffectnum("TE_EXPLOSIONQUAD")), "\n");
517         db_put(d, "TE_TAREXPLOSION", "1"); print("effect TE_TAREXPLOSION is ", ftos(particleeffectnum("TE_TAREXPLOSION")), "\n");
518         db_put(d, "TE_TELEPORT", "1"); print("effect TE_TELEPORT is ", ftos(particleeffectnum("TE_TELEPORT")), "\n");
519         db_put(d, "TE_LAVASPLASH", "1"); print("effect TE_LAVASPLASH is ", ftos(particleeffectnum("TE_LAVASPLASH")), "\n");
520         db_put(d, "TE_SMALLFLASH", "1"); print("effect TE_SMALLFLASH is ", ftos(particleeffectnum("TE_SMALLFLASH")), "\n");
521         db_put(d, "TE_FLAMEJET", "1"); print("effect TE_FLAMEJET is ", ftos(particleeffectnum("TE_FLAMEJET")), "\n");
522         db_put(d, "EF_FLAME", "1"); print("effect EF_FLAME is ", ftos(particleeffectnum("EF_FLAME")), "\n");
523         db_put(d, "TE_BLOOD", "1"); print("effect TE_BLOOD is ", ftos(particleeffectnum("TE_BLOOD")), "\n");
524         db_put(d, "TE_SPARK", "1"); print("effect TE_SPARK is ", ftos(particleeffectnum("TE_SPARK")), "\n");
525         db_put(d, "TE_PLASMABURN", "1"); print("effect TE_PLASMABURN is ", ftos(particleeffectnum("TE_PLASMABURN")), "\n");
526         db_put(d, "TE_TEI_G3", "1"); print("effect TE_TEI_G3 is ", ftos(particleeffectnum("TE_TEI_G3")), "\n");
527         db_put(d, "TE_TEI_SMOKE", "1"); print("effect TE_TEI_SMOKE is ", ftos(particleeffectnum("TE_TEI_SMOKE")), "\n");
528         db_put(d, "TE_TEI_BIGEXPLOSION", "1"); print("effect TE_TEI_BIGEXPLOSION is ", ftos(particleeffectnum("TE_TEI_BIGEXPLOSION")), "\n");
529         db_put(d, "TE_TEI_PLASMAHIT", "1"); print("effect TE_TEI_PLASMAHIT is ", ftos(particleeffectnum("TE_TEI_PLASMAHIT")), "\n");
530         db_put(d, "EF_STARDUST", "1"); print("effect EF_STARDUST is ", ftos(particleeffectnum("EF_STARDUST")), "\n");
531         db_put(d, "TR_ROCKET", "1"); print("effect TR_ROCKET is ", ftos(particleeffectnum("TR_ROCKET")), "\n");
532         db_put(d, "TR_GRENADE", "1"); print("effect TR_GRENADE is ", ftos(particleeffectnum("TR_GRENADE")), "\n");
533         db_put(d, "TR_BLOOD", "1"); print("effect TR_BLOOD is ", ftos(particleeffectnum("TR_BLOOD")), "\n");
534         db_put(d, "TR_WIZSPIKE", "1"); print("effect TR_WIZSPIKE is ", ftos(particleeffectnum("TR_WIZSPIKE")), "\n");
535         db_put(d, "TR_SLIGHTBLOOD", "1"); print("effect TR_SLIGHTBLOOD is ", ftos(particleeffectnum("TR_SLIGHTBLOOD")), "\n");
536         db_put(d, "TR_KNIGHTSPIKE", "1"); print("effect TR_KNIGHTSPIKE is ", ftos(particleeffectnum("TR_KNIGHTSPIKE")), "\n");
537         db_put(d, "TR_VORESPIKE", "1"); print("effect TR_VORESPIKE is ", ftos(particleeffectnum("TR_VORESPIKE")), "\n");
538         db_put(d, "TR_NEHAHRASMOKE", "1"); print("effect TR_NEHAHRASMOKE is ", ftos(particleeffectnum("TR_NEHAHRASMOKE")), "\n");
539         db_put(d, "TR_NEXUIZPLASMA", "1"); print("effect TR_NEXUIZPLASMA is ", ftos(particleeffectnum("TR_NEXUIZPLASMA")), "\n");
540         db_put(d, "TR_GLOWTRAIL", "1"); print("effect TR_GLOWTRAIL is ", ftos(particleeffectnum("TR_GLOWTRAIL")), "\n");
541         db_put(d, "SVC_PARTICLE", "1"); print("effect SVC_PARTICLE is ", ftos(particleeffectnum("SVC_PARTICLE")), "\n");
542
543         fh = fopen("effectinfo.txt", FILE_READ);
544         while((s = fgets(fh)))
545         {
546                 tokenize(s); // tokenize_console would hit the loop counter :(
547                 if(argv(0) == "effect")
548                 {
549                         if(db_get(d, argv(1)) != "1")
550                         {
551                                 if(particleeffectnum(argv(1)) >= 0)
552                                         print("effect ", argv(1), " is ", ftos(particleeffectnum(argv(1))), "\n");
553                                 db_put(d, argv(1), "1");
554                         }
555                 }
556         }
557         print("end of effects list\n");
558
559         db_close(d);
560 }
561
562 void make_mapinfo_Think()
563 {
564         if(MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, 0, 1))
565         {
566                 print("Done rebuiling mapinfos.\n");
567                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
568                 remove(self);
569         }
570         else
571         {
572                 self.think = make_mapinfo_Think;
573                 self.nextthink = time;
574         }
575 }
576
577 void GameCommand(string command)
578 {
579         float argc;
580         entity client, e;
581         vector v;
582         float entno, i;
583         string s;
584         argc = tokenize_console(command);
585
586         if(argv(0) == "help" || argc == 0)
587         {
588                 print("Usage: sv_cmd COMMAND..., where possible commands are:\n");
589                 print("  adminmsg clientnumber \"message\"\n");
590                 print("  teamstatus\n");
591                 print("  printstats\n");
592                 print("  make_mapinfo\n");
593                 print("  gametype dm|ctf|...\n");
594                 print("  savedb filename\n");
595                 print("  dumpdb filename\n");
596                 print("  loaddb filename\n");
597                 print("  allready\n");
598                 print("  effectindexdump\n");
599                 print("  radarmap [--force] [--quit | --loop] [sharpness]\n");
600                 print("  bbox\n");
601                 print("  cvar_changes\n");
602                 print("  find classname\n");
603                 GameCommand_Vote("help", world);
604                 GameCommand_Ban("help");
605                 GameCommand_Generic("help");
606                 return;
607         }
608
609         if(GameCommand_Vote(command, world))
610                 return;
611
612         if(GameCommand_Ban(command))
613                 return;
614
615         if(GameCommand_Generic(command))
616                 return;
617
618         if(argv(0) == "printstats")
619         {
620                 DumpStats(FALSE);
621                 return;
622         }
623
624         if(argv(0) == "make_mapinfo")
625         {
626                 e = spawn();
627                 e.classname = "make_mapinfo";
628                 e.think = make_mapinfo_Think;
629                 e.nextthink = time;
630                 MapInfo_Enumerate();
631                 return;
632         }
633
634         if(argv(0) == "warp") if(argc == 2) if(cvar("g_campaign"))
635         {
636                 CampaignLevelWarp(stof(argv(1)));
637                 return;
638         }
639
640         if(argv(0) == "gotomap") if(argc == 2)
641         {
642                 print(GotoMap(argv(1)), "\n");
643                 return;
644         }
645
646         if(argv(0) == "gametype") if(argc == 2)
647         {
648                 float t, tsave;
649                 s = argv(1);
650                 t = MapInfo_Type_FromString(s);
651                 tsave = MapInfo_CurrentGametype();
652                 if(t)
653                 {
654                         MapInfo_SwitchGameType(t);
655                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
656                         if(MapInfo_count > 0)
657                         {
658                                 bprint("Game type successfully switched to ", s, "\n");
659                         }
660                         else
661                         {
662                                 bprint("Cannot use this game type: no map for it found\n");
663                                 MapInfo_SwitchGameType(tsave);
664                                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
665                         }
666                 }
667                 else
668                         bprint("Game type switch to ", s, " failed: this type does not exist!\n");
669                 return;
670         }
671
672         if(argv(0) == "adminmsg")
673         if(argc == 3)
674         {
675                 entno = stof(argv(1));
676
677                 if((entno < 1) | (entno > maxclients)) {
678                         print("Player ", argv(1), " doesn't exist\n");
679                         return;
680                 }
681
682                 client = edict_num(entno);
683
684                 if(client.flags & FL_CLIENT)
685                 {
686                         centerprint_atprio(client, CENTERPRIO_ADMIN, strcat("^3", admin_name(), ":\n\n^7", argv(2)));
687                         sprint(client, strcat("\{1}\{13}^3", admin_name(), "^7: ", argv(2), "\n"));
688                         print("Message sent to ", client.netname, "\n");
689                 }
690                 else
691                         print("Client not found\n");
692
693                 return;
694         }
695
696         if(argv(0) == "savedb") if(argc == 2)
697         {
698                 db_save(ServerProgsDB, argv(1));
699                 print("DB saved.\n");
700                 return;
701         }
702
703         if(argv(0) == "dumpdb") if(argc == 2)
704         {
705                 db_dump(ServerProgsDB, argv(1));
706                 print("DB dumped.\n");
707                 return;
708         }
709
710         if(argv(0) == "loaddb") if(argc == 2)
711         {
712                 db_close(ServerProgsDB);
713                 ServerProgsDB = db_load(argv(1));
714                 print("DB loaded.\n");
715                 return;
716         }
717
718         if (argv(0) == "nospectators")
719         {
720                 blockSpectators = 1;
721                 local entity plr;
722                 FOR_EACH_CLIENT(plr) //give every spectator <g_maxplayers_spectator_blocktime> seconds time to become a player
723                 {
724                         if(plr.classname == "spectator" || plr.classname == "observer")
725                         {
726                                 plr.spectatortime = time;
727                                 sprint(plr, strcat("^7You have to become a player within the next ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds, otherwise you will be kicked, because spectators aren't allowed at this time!\n"));
728                         }
729                 }
730                 bprint(strcat("^7All spectators will be automatically kicked when not joining the game after ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds!\n"));
731                 return;
732         }
733
734         if (argv(0) == "lockteams")
735         {
736                 if(teams_matter)
737                 {
738                         lockteams = 1;
739                         bprint("^1The teams are now locked.\n");
740                 }
741                 else
742                         bprint("That command can only be used in a team-based gamemode.\n");
743                 return;
744         }
745
746         if (argv(0) == "unlockteams")
747         {
748                 if(teams_matter)
749                 {
750                         lockteams = 0;
751                         bprint("^1The teams are now unlocked.\n");
752                 }
753                 else
754                         bprint("That command can only be used in a team-based gamemode.\n");
755                 return;
756         }
757         if (argv(0) == "movetoteam") if(argc == 3)
758         {
759                 entno = stof(argv(1));
760                 client = world;
761                 if(entno <= maxclients)
762                         client = edict_num(entno);
763                 if(client.flags & FL_CLIENT)
764                 {
765                         float lt;
766                         lt = lockteams;
767                         lockteams = 0;
768
769                         self = client;
770                         SV_ParseClientCommand(strcat("selectteam ", argv(2)));
771
772                         lockteams = lt;
773                 }
774                 else
775                         print("Client not found\n");
776                 return;
777         }
778         if (argv(0) == "teamstatus")
779         {
780                 Score_NicePrint(world);
781                 return;
782         }
783         if (argv(0) == "allready")
784         {
785                 ReadyRestart();
786                 return;
787         }
788         if (argv(0) == "effectindexdump")
789         {
790                 EffectIndexDump();
791                 return;
792         }
793         if (argv(0) == "radarmap")
794         {
795                 RadarMap(argc);
796                 return;
797         }
798         if (argv(0) == "bbox")
799         {
800                 BBox();
801                 return;
802         }
803         if (argv(0) == "cvar_changes")
804         {
805                 print(cvar_changes);
806                 return;
807         }
808         if (argv(0) == "find") if(argc == 2)
809         {
810                 for(client = world; (client = find(client, classname, argv(1))); )
811                         print(etos(client), "\n");
812                 return;
813         }
814         if (argv(0) == "records")
815         {
816                 strunzone(records_reply);
817                 records_reply = strzone(getrecords());
818                 print(records_reply);
819                 return;
820         }
821
822         if(argv(0) == "cointoss")
823         {
824                 bprint("^3Throwing coin... Result: ");
825                 if (random() > 0.5)
826                         bprint("^1heads ^3!\n");
827                 else
828                         bprint("^1tails ^3!\n");
829                 return;
830         }
831
832         if(argv(0) == "__FORCE_READY_RESTART")
833         {
834                 reset_map(FALSE);
835                 return;
836         }
837
838         if(argv(0) == "debug_shotorg")
839         {
840                 debug_shotorg = stov(argv(1));
841                 return;
842         }
843
844         if(argv(0) == "gettaginfo") if(argc >= 4)
845         {
846                 e = spawn();
847                 if(argv(1) == "w")
848                         setmodel(e, (nextent(world)).weaponentity.model);
849                 else
850                         setmodel(e, argv(1));
851                 e.frame = stof(argv(2));
852                 i = gettagindex(e, argv(3));
853                 if(i)
854                 {
855                         v = gettaginfo(e, i);
856                         print("model ", e.model, " frame ", ftos(e.frame), " tag ", argv(3));
857                         print(" index = ", ftos(i));
858                         print(" vector = ", ftos(v_x), " ", ftos(v_y), " ", ftos(v_z), "\n");
859                         if(argc >= 6)
860                         {
861                                 v_y = -v_y;
862                                 localcmd(strcat(argv(4), vtos(v), argv(5), "\n"));
863                         }
864                 }
865                 else
866                         print("bone not found\n");
867                 remove(e);
868                 return;
869         }
870
871         if(argv(0) == "time")
872         {
873                 print("time = ", ftos(time), "\n");
874                 print("frame start = ", ftos(gettime(GETTIME_FRAMESTART)), "\n");
875                 print("realtime = ", ftos(gettime(GETTIME_REALTIME)), "\n");
876                 print("hires = ", ftos(gettime(GETTIME_HIRES)), "\n");
877                 print("uptime = ", ftos(gettime(GETTIME_UPTIME)), "\n");
878                 print("localtime = ", strftime(TRUE, "%a %b %e %H:%M:%S %Z %Y"), "\n");
879                 print("gmtime = ", strftime(FALSE, "%a %b %e %H:%M:%S %Z %Y"), "\n");
880                 return;
881         }
882
883         if(argv(0) == "tracebug")
884         {
885                 print("TEST CASE. If this returns the runaway loop counter error, possibly everything is oaky.\n");
886                 for(;;)
887                 {
888                         vector org, delta, start, end, p, pos;
889                         float safe, unsafe;
890
891                         org = world.mins;
892                         delta = world.maxs - world.mins;
893
894                         start_x = org_x + random() * delta_x;
895                         start_y = org_y + random() * delta_y;
896                         start_z = org_z + random() * delta_z;
897
898                         end_x = org_x + random() * delta_x;
899                         end_y = org_y + random() * delta_y;
900                         end_z = org_z + random() * delta_z;
901
902                         start = stov(vtos(start));
903                         end = stov(vtos(end));
904
905                         tracebox(start, PL_MIN, PL_MAX, end, MOVE_NOMONSTERS, world);
906                         if(!trace_startsolid)
907                         {
908                                 p = trace_endpos;
909                                 tracebox(p, PL_MIN, PL_MAX, p, MOVE_NOMONSTERS, world);
910                                 if(trace_startsolid)
911                                 {
912                                         rint(42); // do an engine breakpoint on VM_rint so you can get the trace that errnoeously returns startsolid
913                                         tracebox(start, PL_MIN, PL_MAX, end, MOVE_NOMONSTERS, world);
914                                         tracebox(p, PL_MIN, PL_MAX, p, MOVE_NOMONSTERS, world);
915
916                                         // how much do we need to back off?
917                                         safe = 1;
918                                         unsafe = 0;
919                                         for(;;)
920                                         {
921                                                 pos = p * (1 - (safe + unsafe) * 0.5) + start * ((safe + unsafe) * 0.5);
922                                                 tracebox(pos, PL_MIN, PL_MAX, pos, MOVE_NOMONSTERS, world);
923                                                 if(trace_startsolid)
924                                                 {
925                                                         if((safe + unsafe) * 0.5 == unsafe)
926                                                                 break;
927                                                         unsafe = (safe + unsafe) * 0.5;
928                                                 }
929                                                 else
930                                                 {
931                                                         if((safe + unsafe) * 0.5 == safe)
932                                                                 break;
933                                                         safe = (safe + unsafe) * 0.5;
934                                                 }
935                                         }
936
937                                         print("safe distance to back off: ", ftos(safe * vlen(p - start)), "qu\n");
938                                         print("unsafe distance to back off: ", ftos(unsafe * vlen(p - start)), "qu\n");
939
940                                         tracebox(p, PL_MIN + '0.1 0.1 0.1', PL_MAX - '0.1 0.1 0.1', p, MOVE_NOMONSTERS, world);
941                                         if(trace_startsolid)
942                                                 print("trace_endpos much in solid when tracing from ", vtos(start), " to ", vtos(end), " endpos ", vtos(trace_endpos), "\n");
943                                         else
944                                                 print("trace_endpos just in solid when tracing from ", vtos(start), " to ", vtos(end), " endpos ", vtos(trace_endpos), "\n");
945                                         break;
946                                 }
947                         }
948                 }
949         }
950
951         if(argv(0) == "tracewalk")
952         {
953                 e = nextent(world);
954                 if(tracewalk(e, stov(argv(1)), e.mins, e.maxs, stov(argv(2)), MOVE_NORMAL))
955                         print("can walk\n");
956                 else
957                         print("cannot walk\n");
958                 return;
959         }
960
961         if(argv(0) == "onslaught_updatelinks")
962         {
963                 onslaught_updatelinks();
964                 print("ONS links updated\n");
965                 return;
966         }
967
968         if(argv(0) == "bot_cmd")
969         {
970                 local entity bot;
971
972                 if(argv(1) == "help")
973                 {
974                         if(argc==2)
975                         {
976                                 bot_list_commands();
977                                 print("\nsv_cmd bot_cmd reset          #Clear the cmd queues of all bots\n");
978                                 print("sv_cmd bot_cmd load <file>    #Load script file\n");
979                                 print("\nUse sv_cmd bot_cmd help <command> for more\n\n");
980                                 return;
981                         }
982
983                         bot_cmdhelp(argv(2));
984                         return;
985                 }
986
987                 // Clear all bot queues
988                 if(argv(1) == "reset")
989                 {
990                         bot_resetqueues();
991                         return;
992                 }
993
994                 // Load cmds from file
995                 if(argv(1) == "load" && argc == 3)
996                 {
997                         float fh;
998                         fh = fopen(argv(2), FILE_READ);
999                         if(fh < 0)
1000                         {
1001                                 print("cannot open the file\n");
1002                                 return;
1003                         }
1004
1005                         i = 0;
1006                         while((s = fgets(fh)))
1007                         {
1008                                 argc = tokenize_console(s);
1009
1010                                 if(argc >= 3 && argv(0) == "sv_cmd" && argv(1) == "bot_cmd")
1011                                 {
1012                                         // let's start at token 2 so we can skip sv_cmd bot_cmd
1013                                         bot = find_bot_by_number(stof(argv(2)));
1014                                         if(bot == world)
1015                                                 bot = find_bot_by_name(argv(2));
1016                                         if(bot)
1017                                                 bot_queuecommand(bot, strcat(argv(3), " ", argv(4)));
1018                                 }
1019                                 else
1020                                         localcmd(strcat(s, "\n"));
1021
1022                                 ++i;
1023                         }
1024
1025                         print(ftos(i), " commands read\n");
1026
1027                         fclose(fh);
1028
1029                         return;
1030                 }
1031
1032                 if(argc < 3)
1033                 {
1034                         print("Usage: sv_cmd bot_cmd <bot name or number> <command> [argument]\n");
1035                         print("Examples: bot_cmd <id> cc \"say something\"\n");
1036                         print("          bot_cmd <id> presskey jump\n");
1037                         print("          .. or sv_cmd bot_cmd help <command> for more\n");
1038                         return;
1039                 }
1040
1041                 bot = find_bot_by_number(stof(argv(1)));
1042                 if(bot == world)
1043                         bot = find_bot_by_name(argv(1));
1044
1045                 if(bot)
1046                         bot_queuecommand(bot, strcat(argv(2), " ", argv(3)));
1047                 else
1048                         print(strcat("Error: Unable to find a bot with the name or number '",argv(1),"'\n"));
1049
1050                 return;
1051         }
1052
1053         if(argv(0) == "playerdemo")
1054         {
1055                 if(argv(1) == "load")
1056                 {
1057                         entno = stof(argv(2));
1058                         if((entno < 1) | (entno > maxclients)) {
1059                                 print("Player ", argv(2), " doesn't exist\n");
1060                                 return;
1061                         }
1062                         client = edict_num(entno);
1063                         if(clienttype(client) != CLIENTTYPE_BOT) {
1064                                 print("Player ", client.netname, " is not a bot\n");
1065                                 return;
1066                         }
1067                         self = client;
1068                         playerdemo_open_read(argv(3));
1069                         return;
1070                 }
1071                 else if(argv(1) == "write")
1072                 {
1073                         entno = stof(argv(2));
1074                         if((entno < 1) | (entno > maxclients)) {
1075                                 print("Player ", argv(2), " doesn't exist\n");
1076                                 return;
1077                         }
1078                         client = edict_num(entno);
1079                         self = client;
1080                         playerdemo_open_write(argv(3));
1081                         return;
1082                 }
1083         }
1084
1085         print("Invalid command. For a list of supported commands, try sv_cmd help.\n");
1086 }
1087