Use new cvar check URL; kill CVAR_POPCON.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cheats.qc
1 void CopyBody(float keepvelocity);
2
3 #ifdef NOCHEATS
4
5 float CheatImpulse(float i) { return 0; }
6 float CheatCommand(float argc) { return 0; }
7 float CheatFrame() { return 0; }
8 void CheatInit() { cheatcount_total = world.cheatcount; }
9 void CheatShutdown() { }
10 void CheatInitClient() { }
11 void CheatShutdownClient() { }
12 void Drag_MoveDrag(entity from, entity to) { }
13
14 #else
15
16 .float maycheat;
17 float gamestart_sv_cheats;
18
19 #define CHIMPULSE_SPEEDRUN_INIT 30
20 #define CHIMPULSE_GIVE_ALL 99
21 #define CHIMPULSE_CLONE_MOVING 140
22 #define CHIMPULSE_SPEEDRUN 141
23 #define CHIMPULSE_CLONE_STANDING 142
24 #define CHIMPULSE_TELEPORT 143
25 #define CHIMPULSE_R00T 148
26
27 #define CHRAME_DRAG 8
28
29 void CheatInit()
30 {
31         gamestart_sv_cheats = autocvar_sv_cheats;
32 }
33
34 void CheatShutdown()
35 {
36 }
37
38 void CheatInitClient()
39 {
40 }
41
42 void CheatShutdownClient()
43 {
44 }
45
46 float CheatsAllowed(float i, float argc, float fr) // the cheat gets passed as argument for possible future ACL checking
47 {
48         // dead people cannot cheat
49         if(self.deadflag != DEAD_NO)
50                 return 0;
51         if(gamestart_sv_cheats < 2 && !IS_PLAYER(self))
52                 return 0;
53         
54         // sv_clones
55         if(i == CHIMPULSE_CLONE_MOVING || i == CHIMPULSE_CLONE_STANDING)
56                 if(self.lip < sv_clones)
57                         return 1;
58
59         // haha
60         if(self.maycheat)
61                 return 1;
62         
63         // sv_cheats
64         if(gamestart_sv_cheats && autocvar_sv_cheats)
65                 return 1;
66
67         // if we get here, player is not allowed to cheat. Log it.
68         if(i)
69                 bprint(sprintf("Player %s^7 tried to use cheat 'impulse %d'\n", self.netname, i));
70         else if(argc)
71                 bprint(sprintf("Player %s^7 tried to use cheat '%s'\n", self.netname, argv(0)));
72         else if(fr)
73                 bprint(sprintf("Player %s^7 tried to use cheat frame %d\n", self.netname, fr));
74         else
75                 bprint(sprintf("Player %s^7 tried to use an unknown cheat\n", self.netname));
76
77         return 0;
78 }
79
80 #define BEGIN_CHEAT_FUNCTION() \
81         float cheating, attempting; \
82         cheating = 0; attempting = 0
83 #define DID_CHEAT() \
84         ++cheating
85 #define ADD_CHEATS(e,n) \
86         cheatcount_total += n; \
87         e.cheatcount += n
88 #define END_CHEAT_FUNCTION() \
89         ADD_CHEATS(self,cheating); \
90         return attempting
91 #define IS_CHEAT(i,argc,fr) \
92         if((++attempting, !CheatsAllowed(i,argc,fr))) \
93                 break
94
95 float num_autoscreenshot;
96 void info_autoscreenshot_findtarget()
97 {
98         entity e;
99         e = find(world, targetname, self.target);
100         if(!e)
101         {
102                 objerror("Missing target. FAIL!");
103                 return;
104         }
105         vector a = vectoangles(e.origin - self.origin);
106         a_x = -a_x; // don't ask
107         self.angles_x = a_x;
108         self.angles_y = a_y;
109         // we leave Rick Roll alone
110 }
111 void spawnfunc_info_autoscreenshot()
112 {
113         if(++num_autoscreenshot > autocvar_g_max_info_autoscreenshot)
114         {
115                 objerror("Too many info_autoscreenshot entitites. FAIL!");
116                 return;
117         }
118         if(self.target != "")
119                 InitializeEntity(self, info_autoscreenshot_findtarget, INITPRIO_FINDTARGET);
120         // this one just has to exist
121 }
122
123 float CheatImpulse(float i)
124 {
125         BEGIN_CHEAT_FUNCTION();
126         switch(i)
127         {
128                 entity e, e2;
129
130                 case CHIMPULSE_SPEEDRUN_INIT: // deploy personal waypoint
131                         // shared with regular waypoint init, so this is not a cheat by itself
132                         if(!self.personal)
133                         {
134                                 self.personal = spawn();
135                                 self.personal.classname = "personal_wp";
136                         }
137                         self.personal.origin = self.origin;
138                         self.personal.v_angle = self.v_angle;
139                         self.personal.velocity = self.velocity;
140                         self.personal.ammo_rockets = self.ammo_rockets;
141                         self.personal.ammo_nails = self.ammo_nails;
142                         self.personal.ammo_cells = self.ammo_cells;
143                         self.personal.ammo_shells = self.ammo_shells;
144                         self.personal.ammo_fuel = self.ammo_fuel;
145                         self.personal.health = self.health;
146                         self.personal.armorvalue = self.armorvalue;
147                         self.personal.weapons = self.weapons;
148                         self.personal.items = self.items;
149                         self.personal.pauserotarmor_finished = self.pauserotarmor_finished;
150                         self.personal.pauserothealth_finished = self.pauserothealth_finished;
151                         self.personal.pauserotfuel_finished = self.pauserotfuel_finished;
152                         self.personal.pauseregen_finished = self.pauseregen_finished;
153                         self.personal.strength_finished = self.strength_finished;
154                         self.personal.invincible_finished = self.invincible_finished;
155                         self.personal.teleport_time = time;
156                         break; // this part itself doesn't cheat, so let's not count this
157                 case CHIMPULSE_CLONE_MOVING:
158                         IS_CHEAT(i, 0, 0);
159                         makevectors (self.v_angle);
160                         self.velocity = self.velocity + v_forward * 300;
161                         CopyBody(1);
162                         self.lip += 1;
163                         self.velocity = self.velocity - v_forward * 300;
164                         DID_CHEAT();
165                         break;
166                 case CHIMPULSE_CLONE_STANDING:
167                         IS_CHEAT(i, 0, 0);
168                         CopyBody(0);
169                         self.lip += 1;
170                         DID_CHEAT();
171                         break;
172                 case CHIMPULSE_GIVE_ALL:
173                         IS_CHEAT(i, 0, 0);
174                         CheatCommand(tokenize_console("give all"));
175                         break; // already counted as cheat
176                 case CHIMPULSE_SPEEDRUN:
177                         IS_CHEAT(i, 0, 0);
178                         if(self.personal)
179                         {
180                                 self.speedrunning = TRUE;
181                                 tracebox(self.personal.origin, self.mins, self.maxs, self.personal.origin, MOVE_WORLDONLY, self);
182                                 if(trace_startsolid)
183                                 {
184                                         sprint(self, "Cannot move there, cheater - only waypoints set using g_waypointsprite_personal work\n");
185                                 }
186                                 else
187                                 {
188                                         // Abort speedrun, teleport back
189                                         setorigin(self, self.personal.origin);
190                                         self.oldvelocity = self.velocity = self.personal.velocity;
191                                         self.angles = self.personal.v_angle;
192                                         self.fixangle = TRUE;
193                                         
194                                         MUTATOR_CALLHOOK(AbortSpeedrun);
195                                 }
196
197                                 self.ammo_rockets = self.personal.ammo_rockets;
198                                 self.ammo_nails = self.personal.ammo_nails;
199                                 self.ammo_cells = self.personal.ammo_cells;
200                                 self.ammo_shells = self.personal.ammo_shells;
201                                 self.ammo_fuel = self.personal.ammo_fuel;
202                                 self.health = self.personal.health;
203                                 self.armorvalue = self.personal.armorvalue;
204                                 self.weapons = self.personal.weapons;
205                                 self.items = self.personal.items;
206                                 self.pauserotarmor_finished = time + self.personal.pauserotarmor_finished - self.personal.teleport_time;
207                                 self.pauserothealth_finished = time + self.personal.pauserothealth_finished - self.personal.teleport_time;
208                                 self.pauserotfuel_finished = time + self.personal.pauserotfuel_finished - self.personal.teleport_time;
209                                 self.pauseregen_finished = time + self.personal.pauseregen_finished - self.personal.teleport_time;
210                                 self.strength_finished = time + self.personal.strength_finished - self.personal.teleport_time;
211                                 self.invincible_finished = time + self.personal.invincible_finished - self.personal.teleport_time;
212
213                                 DID_CHEAT();
214                                 break;
215                         }
216                         if(self.deadflag != DEAD_NO)
217                                 sprint(self, "UR DEAD AHAHAH))\n");
218                         else
219                                 sprint(self, "No waypoint set, cheater (use g_waypointsprite_personal to set one)\n");
220                         break;
221                 case CHIMPULSE_TELEPORT:
222                         IS_CHEAT(i, 0, 0);
223                         if(self.movetype == MOVETYPE_NOCLIP)
224                         {
225                                 e = find(world, classname, "info_autoscreenshot");
226                                 if(e)
227                                 {
228                                         sprint(self, "Emergency teleport used info_autoscreenshot location\n");
229                                         setorigin(self, e.origin);
230                                         self.angles = e.angles;
231                                         remove(e);
232                                         // should we? self.angles_x = -self.angles_x;
233                                         self.fixangle = TRUE;
234                                         self.velocity = '0 0 0';
235                                         DID_CHEAT();
236                                         break;
237                                 }
238                         }
239                         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, ((gamestart_sv_cheats < 2) ? 100 : 100000), 384, 384))
240                         {
241                                 sprint(self, "Emergency teleport used random location\n");
242                                 self.angles_x = -self.angles_x;
243                                 self.fixangle = TRUE;
244                                 self.velocity = '0 0 0';
245                                 DID_CHEAT();
246                                 break;
247                         }
248                         sprint(self, "Emergency teleport could not find a good location, forget it!\n");
249                         break;
250                 case CHIMPULSE_R00T:
251                         IS_CHEAT(i, 0, 0);
252                         RandomSelection_Init();
253                         FOR_EACH_PLAYER(e)
254                                 if(e.deadflag == DEAD_NO)
255                                         if(DIFF_TEAM(e, self))
256                                                 RandomSelection_Add(e, 0, string_null, 1, 1);
257                         if(RandomSelection_chosen_ent)
258                                 e = RandomSelection_chosen_ent;
259                         else
260                                 e = self;
261
262                         pointparticles(particleeffectnum("rocket_explode"), e.origin, '0 0 0', 1);
263                         sound(e, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
264
265                         e2 = spawn();
266                         setorigin(e2, e.origin);
267                         RadiusDamage(e2, self, 1000, 0, 128, world, 500, DEATH_CHEAT, e);
268                         remove(e2);
269
270                         print("404 Sportsmanship not found.\n");
271                         DID_CHEAT();
272                         break;
273         }
274
275         END_CHEAT_FUNCTION();
276 }
277
278 void DragBox_Think();
279 float drag_lastcnt;
280 float CheatCommand(float argc)
281 {
282         BEGIN_CHEAT_FUNCTION();
283         string cmd;
284         cmd = argv(0);
285         switch(cmd)
286         {
287                 entity e;
288                 float effectnum, f;
289                 vector start, end;
290                 entity oldself;
291
292                 case "pointparticles":
293                         IS_CHEAT(0, argc, 0);
294                         if(argc == 5)
295                         {
296                                 // arguments:
297                                 //   effectname
298                                 //   origin (0..1, on crosshair line)
299                                 //   velocity
300                                 //   howmany
301                                 effectnum = particleeffectnum(argv(1));
302                                 f = stof(argv(2));
303                                 crosshair_trace(self);
304                                 start = (1-f) * self.origin + f * trace_endpos;
305                                 end = stov(argv(3));
306                                 f = stof(argv(4));
307                                 pointparticles(effectnum, start, end, f);
308                                 DID_CHEAT();
309                                 break;
310                         }
311                         sprint(self, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
312                         break;
313                 case "trailparticles":
314                         IS_CHEAT(0, argc, 0);
315                         if(argc == 2)
316                         {
317                                 // arguments:
318                                 //   effectname
319                                 effectnum = particleeffectnum(argv(1));
320                                 W_SetupShot(self, FALSE, FALSE, "", CH_WEAPON_A, 0);
321                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
322                                 trailparticles(self, effectnum, w_shotorg, trace_endpos);
323                                 DID_CHEAT();
324                                 break;
325                         }
326                         sprint(self, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
327                         break;
328                 case "make":
329                         IS_CHEAT(0, argc, 0);
330                         if(argc == 3)
331                         {
332                                 // arguments:
333                                 //   modelname mode
334                                 f = stof(argv(2));
335                                 W_SetupShot(self, FALSE, FALSE, "", CH_WEAPON_A, 0);
336                                 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, self);
337                                 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
338                                 {
339                                         sprint(self, "cannot make stuff there (bad surface)\n");
340                                 }
341                                 else
342                                 {
343                                         e = spawn();
344                                         e.model = strzone(argv(1));
345                                         e.mdl = "rocket_explode";
346                                         e.health = 1000;
347                                         setorigin(e, trace_endpos);
348                                         e.effects = EF_NOMODELFLAGS;
349                                         if(f == 1)
350                                         {
351                                                 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
352                                                 e.angles = AnglesTransform_ApplyToAngles(e.angles, '-90 0 0'); // so unrotated models work
353                                         }
354                                         oldself = self;
355                                         self = e;
356                                         spawnfunc_func_breakable();
357                                         self = oldself;
358                                         // now, is it valid?
359                                         if(f == 0)
360                                         {
361                                                 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
362                                                 if(trace_startsolid)
363                                                 {
364                                                         remove(e);
365                                                         sprint(self, "cannot make stuff there (no space)\n");
366                                                 }
367                                                 else
368                                                         DID_CHEAT();
369                                         }
370                                         else
371                                                 DID_CHEAT();
372                                 }
373                         }
374                         else
375                                 sprint(self, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
376                         break;
377                 case "penalty":
378                         IS_CHEAT(0, argc, 0);
379                         if(argc == 3)
380                         {
381                                 race_ImposePenaltyTime(self, stof(argv(1)), argv(2));
382                                 DID_CHEAT();
383                                 break;
384                         }
385                         sprint(self, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
386                         break;
387                 case "dragbox_spawn":
388                         IS_CHEAT(0, argc, 0);
389                         e = spawn();
390                         e.classname = "dragbox_box";
391                         e.think = DragBox_Think;
392                         e.nextthink = time;
393                         e.solid = -1; // black
394                         setmodel(e, "null"); // network it
395                         if(argc == 4)
396                                 e.cnt = stof(argv(1));
397                         else
398                                 e.cnt = max(0, drag_lastcnt);
399
400                         e.aiment = spawn();
401                         e.aiment.classname = "dragbox_corner_1";
402                         e.aiment.owner = e;
403                         setmodel(e.aiment, "models/marker.md3");
404                         e.aiment.skin = 0;
405                         setsize(e.aiment, '0 0 0', '0 0 0');
406                         if(argc == 4)
407                                 setorigin(e.aiment, stov(argv(2)));
408                         else
409                         {
410                                 crosshair_trace(self);
411                                 setorigin(e.aiment, trace_endpos);
412                         }
413
414                         e.enemy = spawn();
415                         e.enemy.classname = "dragbox_corner_2";
416                         e.enemy.owner = e;
417                         setmodel(e.enemy, "models/marker.md3");
418                         e.enemy.skin = 1;
419                         setsize(e.enemy, '0 0 0', '0 0 0');
420                         end = normalize(self.origin + self.view_ofs - e.aiment.origin);
421                         end_x = (end_x > 0) * 2 - 1;
422                         end_y = (end_y > 0) * 2 - 1;
423                         end_z = (end_z > 0) * 2 - 1;
424                         if(argc == 4)
425                                 setorigin(e.enemy, stov(argv(3)));
426                         else
427                                 setorigin(e.enemy, e.aiment.origin + 32 * end);
428
429                         e.killindicator = spawn();
430                         e.killindicator.classname = "drag_digit";
431                         e.killindicator.owner = e;
432                         setattachment(e.killindicator, e, "");
433                         setorigin(e.killindicator, '0 0 -8');
434                         e.killindicator.killindicator = spawn();
435                         e.killindicator.killindicator.classname = "drag_digit";
436                         e.killindicator.killindicator.owner = e;
437                         setattachment(e.killindicator.killindicator, e, "");
438                         setorigin(e.killindicator.killindicator, '0 0 8');
439                         DID_CHEAT();
440                         break;
441                 case "dragpoint_spawn":
442                         IS_CHEAT(0, argc, 0);
443                         e = spawn();
444                         e.classname = "dragpoint";
445                         e.think = DragBox_Think;
446                         e.nextthink = time;
447                         e.solid = 0; // nothing special
448                         setmodel(e, "models/marker.md3");
449                         setsize(e, PL_MIN, PL_MAX);
450                         e.skin = 2;
451                         if(argc == 3)
452                                 e.cnt = stof(argv(1));
453                         else
454                                 e.cnt = drag_lastcnt;
455                         if(argc == 3)
456                                 setorigin(e, stov(argv(2)));
457                         else
458                         {
459                                 crosshair_trace(self);
460                                 setorigin(e, trace_endpos + normalize(self.origin + self.view_ofs - trace_endpos));
461                                 move_out_of_solid(e);
462                         }
463
464                         e.killindicator = spawn();
465                         e.killindicator.classname = "drag_digit";
466                         e.killindicator.owner = e;
467                         setattachment(e.killindicator, e, "");
468                         setorigin(e.killindicator, '0 0 40');
469                         e.killindicator.killindicator = spawn();
470                         e.killindicator.killindicator.classname = "drag_digit";
471                         e.killindicator.killindicator.owner = e;
472                         setattachment(e.killindicator.killindicator, e, "");
473                         setorigin(e.killindicator.killindicator, '0 0 56');
474                         DID_CHEAT();
475                         break;
476                 case "drag_remove":
477                         IS_CHEAT(0, argc, 0);
478                         RandomSelection_Init();
479                         crosshair_trace(self);
480                         for(e = world; (e = find(e, classname, "dragbox_box")); )
481                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
482                         for(e = world; (e = find(e, classname, "dragpoint")); )
483                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
484                         if(RandomSelection_chosen_ent)
485                         {
486                                 remove(RandomSelection_chosen_ent.killindicator.killindicator);
487                                 remove(RandomSelection_chosen_ent.killindicator);
488                                 if(RandomSelection_chosen_ent.aiment)
489                                         remove(RandomSelection_chosen_ent.aiment);
490                                 if(RandomSelection_chosen_ent.enemy)
491                                         remove(RandomSelection_chosen_ent.enemy);
492                                 remove(RandomSelection_chosen_ent);
493                         }
494                         DID_CHEAT();
495                         break;
496                 case "drag_setcnt":
497                         IS_CHEAT(0, argc, 0);
498                         if(argc == 2)
499                         {
500                                 RandomSelection_Init();
501                                 crosshair_trace(self);
502                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
503                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
504                                 for(e = world; (e = find(e, classname, "dragpoint")); )
505                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
506                                 if(RandomSelection_chosen_ent)
507                                 {
508                                         if(substring(argv(1), 0, 1) == "*")
509                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
510                                         else
511                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
512                                 }
513                                 DID_CHEAT();
514                                 break;
515                         }
516                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
517                         break;
518                 case "drag_save":
519                         IS_CHEAT(0, argc, 0);
520                         if(argc == 2)
521                         {
522                                 f = fopen(argv(1), FILE_WRITE);
523                                 fputs(f, "cmd drag_clear\n");
524                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
525                                 {
526                                         fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
527                                 }
528                                 for(e = world; (e = find(e, classname, "dragpoint")); )
529                                 {
530                                         fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
531                                 }
532                                 fclose(f);
533                                 DID_CHEAT();
534                                 break;
535                         }
536                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
537                         break;
538                 case "drag_saveraceent":
539                         IS_CHEAT(0, argc, 0);
540                         if(argc == 2)
541                         {
542                                 f = fopen(argv(1), FILE_WRITE);
543                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
544                                 {
545                                         fputs(f, "{\n");
546                                         fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
547                                         fputs(f, strcat("\"origin\" \"", ftos(e.absmin_x), " ", ftos(e.absmin_y), " ", ftos(e.absmin_z), "\"\n"));
548                                         fputs(f, strcat("\"maxs\" \"", ftos(e.absmax_x - e.absmin_x), " ", ftos(e.absmax_y - e.absmin_y), " ", ftos(e.absmax_z - e.absmin_z), "\"\n"));
549                                         fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
550                                         fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
551                                         fputs(f, "}\n");
552                                 }
553                                 for(e = world; (e = find(e, classname, "dragpoint")); )
554                                 {
555                                         start = '0 0 0';
556                                         effectnum = 0;
557                                         for(oldself = world; (oldself = find(oldself, classname, "dragbox_box")); )
558                                         {
559                                                 if(e.cnt <= 0 && oldself.cnt == 0 || e.cnt == oldself.cnt)
560                                                 {
561                                                         start = start + oldself.origin;
562                                                         ++effectnum;
563                                                 }
564                                         }
565                                         start *= 1 / effectnum;
566                                         fputs(f, "{\n");
567                                         fputs(f, "\"classname\" \"info_player_race\"\n");
568                                         fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
569                                         fputs(f, strcat("\"origin\" \"", ftos(e.origin_x), " ", ftos(e.origin_y), " ", ftos(e.origin_z), "\"\n"));
570                                         if(e.cnt == -2)
571                                         {
572                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
573                                                 fputs(f, "\"race_place\" \"0\"\n");
574                                         }
575                                         else if(e.cnt == -1)
576                                         {
577                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
578                                                 fputs(f, "\"race_place\" \"-1\"\n");
579                                         }
580                                         else
581                                         {
582                                                 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
583                                                 if(e.cnt == 0)
584                                                 {
585                                                         // these need race_place
586                                                         // counting...
587                                                         effectnum = 1;
588                                                         for(oldself = world; (oldself = find(oldself, classname, "dragpoint")); )
589                                                         if(oldself.cnt == 0)
590                                                         {
591                                                                 if(vlen(oldself.origin - start) < vlen(e.origin - start))
592                                                                         ++effectnum;
593                                                                 else if(vlen(oldself.origin - start) == vlen(e.origin - start) && num_for_edict(oldself) < num_for_edict(e))
594                                                                         ++effectnum;
595                                                         }
596                                                         fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
597                                                 }
598                                         }
599                                         fputs(f, "}\n");
600                                 }
601                                 fclose(f);
602                                 DID_CHEAT();
603                                 break;
604                         }
605                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
606                         break;
607                 case "drag_clear":
608                         IS_CHEAT(0, argc, 0);
609                         for(e = world; (e = find(e, classname, "dragbox_box")); )
610                                 remove(e);
611                         for(e = world; (e = find(e, classname, "dragbox_corner_1")); )
612                                 remove(e);
613                         for(e = world; (e = find(e, classname, "dragbox_corner_2")); )
614                                 remove(e);
615                         for(e = world; (e = find(e, classname, "dragpoint")); )
616                                 remove(e);
617                         for(e = world; (e = find(e, classname, "drag_digit")); )
618                                 remove(e);
619                         DID_CHEAT();
620                         break;
621                 case "god":
622                         IS_CHEAT(0, argc, 0);
623                         BITXOR_ASSIGN(self.flags, FL_GODMODE);
624                         if(self.flags & FL_GODMODE)
625                         {
626                                 sprint(self, "godmode ON\n");
627                                 DID_CHEAT();
628                         }
629                         else
630                                 sprint(self, "godmode OFF\n");
631                         break;
632                 case "notarget":
633                         IS_CHEAT(0, argc, 0);
634                         BITXOR_ASSIGN(self.flags, FL_NOTARGET);
635                         if(self.flags & FL_NOTARGET)
636                         {
637                                 sprint(self, "notarget ON\n");
638                                 DID_CHEAT();
639                         }
640                         else
641                                 sprint(self, "notarget OFF\n");
642                         break;
643                 case "noclip":
644                         IS_CHEAT(0, argc, 0);
645                         if(self.movetype != MOVETYPE_NOCLIP)
646                         {
647                                 self.movetype = MOVETYPE_NOCLIP;
648                                 sprint(self, "noclip ON\n");
649                                 DID_CHEAT();
650                         }
651                         else
652                         {
653                                 self.movetype = MOVETYPE_WALK;
654                                 sprint(self, "noclip OFF\n");
655                         }
656                         break;
657                 case "fly":
658                         IS_CHEAT(0, argc, 0);
659                         if(self.movetype != MOVETYPE_FLY)
660                         {
661                                 self.movetype = MOVETYPE_FLY;
662                                 sprint(self, "flymode ON\n");
663                                 DID_CHEAT();
664                         }
665                         else
666                         {
667                                 self.movetype = MOVETYPE_WALK;
668                                 sprint(self, "flymode OFF\n");
669                         }
670                         break;
671                 case "give":
672                         IS_CHEAT(0, argc, 0);
673                         if(GiveItems(self, 1, argc))
674                                 DID_CHEAT();
675                         break;
676                 case "usetarget":
677                         IS_CHEAT(0, argc, 0);
678                         e = self;
679                         self = spawn();
680                         self.target = argv(1);
681                         activator = e;
682                         SUB_UseTargets();
683                         remove(self);
684                         self = e;
685                         DID_CHEAT();
686                         break;
687                 case "killtarget":
688                         IS_CHEAT(0, argc, 0);
689                         e = self;
690                         self = spawn();
691                         self.killtarget = argv(1);
692                         activator = e;
693                         SUB_UseTargets();
694                         remove(self);
695                         self = e;
696                         DID_CHEAT();
697                         break;
698                 case "teleporttotarget":
699                         IS_CHEAT(0, argc, 0);
700                         e = self;
701                         self = spawn();
702                         setorigin(self, self.origin);
703                         self.classname = "cheattriggerteleport";
704                         self.target = argv(1);
705                         teleport_findtarget();
706                         if(!wasfreed(self))
707                         {
708                                 Simple_TeleportPlayer(self, e);
709                                 remove(self);
710                                 self = e;
711                                 DID_CHEAT();
712                         }
713                         else
714                                 self = e;
715                         break;
716         }
717
718         END_CHEAT_FUNCTION();
719 }
720
721 float Drag(entity e, float grab, float ischeat);
722 void Drag_Begin(entity dragger, entity draggee, vector touchpoint);
723 void Drag_Finish(entity dragger);
724 float Drag_IsDraggable(entity draggee);
725 float Drag_MayChangeAngles(entity draggee);
726 void Drag_MoveForward(entity dragger);
727 void Drag_SetSpeed(entity dragger, float s);
728 void Drag_MoveBackward(entity dragger);
729 void Drag_Update(entity dragger);
730 float Drag_CanDrag(entity dragger);
731 float Drag_IsDragging(entity dragger);
732 void Drag_MoveDrag(entity from, entity to);
733 .entity dragentity;
734
735 float CheatFrame()
736 {
737         BEGIN_CHEAT_FUNCTION();
738
739         // Dragging can be used as either a cheat, or a function for some objects. If sv_cheats is active,
740         // the cheat dragging is used (unlimited pickup range and any entity can be carried). If sv_cheats
741         // is disabled, normal dragging is used (limited pickup range and only dragable objects can be carried),
742         // grabbing itself no longer being accounted as cheating.
743
744         switch(0)
745         {
746                 default:
747                         if(self.maycheat || (gamestart_sv_cheats && autocvar_sv_cheats))
748                         {
749                                 // use cheat dragging if cheats are enabled
750                                 crosshair_trace_plusvisibletriggers(self);
751                                 Drag(trace_ent, TRUE, TRUE);
752                         }
753                         else
754                         {
755                                 // drag is TRUE if the object can be picked up. While an object is being carried, the Drag() function
756                                 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
757                                 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
758                                 // it goes out of range while slinging it around.
759
760                                 float drag;
761                                 crosshair_trace_plusvisibletriggers(self);
762                                 drag = FALSE;
763                                 if(vlen(self.origin - trace_ent.origin) <= autocvar_g_grab_range)
764                                 {
765                                         switch(trace_ent.grab)
766                                         {
767                                                 case 0: // can't grab
768                                                         break;
769                                                 case 1: // owner can grab
770                                                         if(trace_ent.owner == self || trace_ent.realowner == self)
771                                                                 drag = TRUE;
772                                                         break;
773                                                 case 2: // owner and team mates can grab
774                                                         if(SAME_TEAM(trace_ent.owner, self) || SAME_TEAM(trace_ent.realowner, self) || trace_ent.team == self.team)
775                                                                 drag = TRUE;
776                                                         break;
777                                                 case 3: // anyone can grab
778                                                         drag = TRUE;
779                                                         break;
780                                                 default:
781                                                         break;
782                                         }
783                                 }
784                                 Drag(trace_ent, drag, FALSE); // execute dragging
785                         }
786                         break;
787         }
788
789         END_CHEAT_FUNCTION();
790 }
791
792
793
794
795
796 // ENTITY DRAGGING
797
798 float Drag(entity e, float pick, float ischeat)
799 {
800         BEGIN_CHEAT_FUNCTION();
801
802         // returns TRUE when an entity has been picked up
803         // If pick is TRUE, the object can also be picked up if it's not being held already
804         // If pick is FALSE, only keep dragging the object if it's already being held
805
806         switch(0)
807         {
808                 default:
809                         if(Drag_IsDragging(self))
810                         {
811                                 if(self.BUTTON_DRAG)
812                                 {
813                                         if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
814                                         {
815                                                 Drag_MoveForward(self);
816                                                 self.impulse = 0;
817                                         }
818                                         else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19)
819                                         {
820                                                 Drag_MoveBackward(self);
821                                                 self.impulse = 0;
822                                         }
823                                         else if(self.impulse >= 1 && self.impulse <= 9)
824                                         {
825                                                 Drag_SetSpeed(self, self.impulse - 1);
826                                         }
827                                         else if(self.impulse == 14)
828                                         {
829                                                 Drag_SetSpeed(self, 9);
830                                         }
831
832                                         if(frametime)
833                                                 Drag_Update(self);
834                                 }
835                                 else
836                                 {
837                                         Drag_Finish(self);
838                                 }
839                         }
840                         else
841                         {
842                                 if(Drag_CanDrag(self))
843                                         if(self.BUTTON_DRAG && pick)
844                                         {
845                                                 if(e)
846                                                         if(Drag_IsDraggable(e))
847                                                         {
848                                                                 if(ischeat)
849                                                                         IS_CHEAT(0, 0, CHRAME_DRAG);
850                                                                 if(e.draggedby)
851                                                                         Drag_Finish(e.draggedby);
852                                                                 if(e.tag_entity)
853                                                                         detach_sameorigin(e);
854                                                                 Drag_Begin(self, e, trace_endpos);
855                                                                 if(ischeat)
856                                                                         DID_CHEAT();
857                                                                 return TRUE;
858                                                         }
859                                         }
860                         }
861                         break;
862         }
863         return FALSE;
864 }
865
866 // on dragger:
867 .float draggravity;
868 .float dragspeed; // speed of mouse wheel action
869 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
870 .vector draglocalvector; // local attachment vector of the dragentity
871 .float draglocalangle;
872 // on draggee:
873 .entity draggedby;
874 .float dragmovetype;
875 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
876 {
877         float tagscale;
878
879         draggee.dragmovetype = draggee.movetype;
880         draggee.draggravity = draggee.gravity;
881         draggee.movetype = MOVETYPE_WALK;
882         draggee.gravity = 0.00001;
883         draggee.flags &= ~FL_ONGROUND;
884         draggee.draggedby = dragger;
885
886         dragger.dragentity = draggee;
887
888         dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
889         dragger.draglocalangle = draggee.angles_y - dragger.v_angle_y;
890         touchpoint = touchpoint - gettaginfo(draggee, 0);
891         tagscale = pow(vlen(v_forward), -2);
892         dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
893         dragger.draglocalvector_y = touchpoint * v_right * tagscale;
894         dragger.draglocalvector_z = touchpoint * v_up * tagscale;
895
896         dragger.dragspeed = 64;
897 }
898
899 void Drag_Finish(entity dragger)
900 {
901         entity draggee;
902         draggee = dragger.dragentity;
903         if(dragger)
904                 dragger.dragentity = world;
905         draggee.draggedby = world;
906         draggee.movetype = draggee.dragmovetype;
907         draggee.gravity = draggee.draggravity;
908
909         switch(draggee.movetype)
910         {
911                 case MOVETYPE_TOSS:
912                 case MOVETYPE_WALK:
913                 case MOVETYPE_STEP:
914                 case MOVETYPE_FLYMISSILE:
915                 case MOVETYPE_BOUNCE:
916                 case MOVETYPE_BOUNCEMISSILE:
917                 case MOVETYPE_PHYSICS:
918                         break;
919                 default:
920                         draggee.velocity = '0 0 0';
921                         break;
922         }
923
924         if((draggee.flags & FL_ITEM) && (vlen(draggee.velocity) < 32))
925         {
926                 draggee.velocity = '0 0 0';
927                 draggee.flags |= FL_ONGROUND; // floating items are FUN
928         }
929 }
930
931 float Drag_IsDraggable(entity draggee)
932 {
933         // TODO add more checks for bad stuff here
934         if(draggee == world)
935                 return FALSE;
936         if(draggee.classname == "func_bobbing")
937                 return FALSE;
938         if(draggee.classname == "door") // FIXME find out why these must be excluded, or work around the problem (trying to drag these causes like 4 fps)
939                 return FALSE;
940         if(draggee.classname == "plat")
941                 return FALSE;
942         if(draggee.classname == "func_button")
943                 return FALSE;
944 //      if(draggee.model == "")
945 //              return FALSE;
946         if(IS_SPEC(draggee))
947                 return FALSE;
948         if(IS_OBSERVER(draggee))
949                 return FALSE;
950         if(draggee.classname == "exteriorweaponentity")
951                 return FALSE;
952         if(draggee.classname == "weaponentity")
953                 return FALSE;
954
955         return TRUE;
956 }
957
958 float Drag_MayChangeAngles(entity draggee)
959 {
960         // TODO add more checks for bad stuff here
961         if(substring(draggee.model, 0, 1) == "*")
962                 return FALSE;
963         return TRUE;
964 }
965
966 void Drag_MoveForward(entity dragger)
967 {
968         dragger.dragdistance += dragger.dragspeed;
969 }
970
971 void Drag_SetSpeed(entity dragger, float s)
972 {
973         dragger.dragspeed = pow(2, s);
974 }
975
976 void Drag_MoveBackward(entity dragger)
977 {
978         dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
979 }
980
981 void Drag_Update(entity dragger)
982 {
983         vector curorigin, neworigin, goodvelocity;
984         float f;
985         entity draggee;
986
987         draggee = dragger.dragentity;
988         draggee.flags &= ~FL_ONGROUND;
989
990         curorigin = gettaginfo(draggee, 0);
991         curorigin = curorigin + v_forward * dragger.draglocalvector_x + v_right * dragger.draglocalvector_y + v_up * dragger.draglocalvector_z;
992         makevectors(dragger.v_angle);
993         neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
994         goodvelocity = (neworigin - curorigin) * (1 / frametime);
995
996         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle > 180)
997                 dragger.draglocalangle += 360;
998         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle <= -180)
999                 dragger.draglocalangle -= 360;
1000
1001         f = min(frametime * 10, 1);
1002         draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
1003
1004         if(Drag_MayChangeAngles(draggee))
1005                 draggee.angles_y = draggee.angles_y * (1 - f) + (dragger.v_angle_y + dragger.draglocalangle) * f;
1006
1007         draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
1008
1009         te_lightning1(dragger, dragger.origin + dragger.view_ofs, curorigin);
1010 }
1011
1012 float Drag_CanDrag(entity dragger)
1013 {
1014         return (dragger.deadflag == DEAD_NO) || (IS_PLAYER(dragger));
1015 }
1016
1017 float Drag_IsDragging(entity dragger)
1018 {
1019         if(!dragger.dragentity)
1020                 return FALSE;
1021         if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
1022         {
1023                 dragger.dragentity = world;
1024                 return FALSE;
1025         }
1026         if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity))
1027         {
1028                 Drag_Finish(dragger);
1029                 return FALSE;
1030         }
1031         return TRUE;
1032 }
1033
1034 void Drag_MoveDrag(entity from, entity to)
1035 {
1036         if(from.draggedby)
1037         {
1038                 to.draggedby = from.draggedby;
1039                 to.draggedby.dragentity = to;
1040                 from.draggedby = world;
1041         }
1042 }
1043
1044 void DragBox_Think()
1045 {
1046         if(self.aiment && self.enemy)
1047         {
1048                 self.origin_x = (self.aiment.origin_x + self.enemy.origin_x) * 0.5;
1049                 self.origin_y = (self.aiment.origin_y + self.enemy.origin_y) * 0.5;
1050                 self.origin_z = (self.aiment.origin_z + self.enemy.origin_z) * 0.5;
1051                 self.maxs_x = fabs(self.aiment.origin_x - self.enemy.origin_x) * 0.5;
1052                 self.maxs_y = fabs(self.aiment.origin_y - self.enemy.origin_y) * 0.5;
1053                 self.maxs_z = fabs(self.aiment.origin_z - self.enemy.origin_z) * 0.5;
1054                 self.mins = -1 * self.maxs;
1055                 setorigin(self, self.origin); setsize(self, self.mins, self.maxs); // link edict
1056         }
1057
1058         if(self.cnt == -1) // actually race_place -1
1059         {
1060                 // show "10 10" for qualifying spawns
1061                 setmodel(self.killindicator, "models/sprites/10.spr32");
1062                 setmodel(self.killindicator.killindicator, "models/sprites/10.spr32");
1063         }
1064         else if(self.cnt == -2) // actually race_place 0
1065         {
1066                 // show "10 0" for loser spawns
1067                 setmodel(self.killindicator, "models/sprites/10.spr32");
1068                 setmodel(self.killindicator.killindicator, "models/sprites/0.spr32");
1069         }
1070         else
1071         {
1072                 setmodel(self.killindicator, strcat("models/sprites/", ftos(mod(self.cnt, 10)), ".spr32"));
1073                 setmodel(self.killindicator.killindicator, strcat("models/sprites/", ftos(floor(self.cnt / 10)), ".spr32"));
1074         }
1075
1076         self.nextthink = time;
1077 }
1078
1079 #endif