0104f89b3f2bcfe5742fc0abb5ee6582880d5642
[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 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         sv_cheats = cvar("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(self.classname != "player")
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(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         ++attempting; \
93         if(!CheatsAllowed(i,argc,fr)) \
94                 break
95
96 float CheatImpulse(float i)
97 {
98         BEGIN_CHEAT_FUNCTION();
99         switch(i)
100         {
101                 entity e, e2;
102                 vector org;
103
104                 case CHIMPULSE_SPEEDRUN_INIT: // deploy personal waypoint
105                         // shared with regular waypoint init, so this is not a cheat by itself
106                         if(!self.personal)
107                         {
108                                 self.personal = spawn();
109                                 self.personal.classname = "personal_wp";
110                         }
111                         self.personal.origin = self.origin;
112                         self.personal.v_angle = self.v_angle;
113                         self.personal.velocity = self.velocity;
114                         self.personal.ammo_rockets = self.ammo_rockets;
115                         self.personal.ammo_nails = self.ammo_nails;
116                         self.personal.ammo_cells = self.ammo_cells;
117                         self.personal.ammo_shells = self.ammo_shells;
118                         self.personal.ammo_fuel = self.ammo_fuel;
119                         self.personal.health = self.health;
120                         self.personal.armorvalue = self.armorvalue;
121                         self.personal.weapons = self.weapons;
122                         self.personal.items = self.items;
123                         self.personal.pauserotarmor_finished = self.pauserotarmor_finished;
124                         self.personal.pauserothealth_finished = self.pauserothealth_finished;
125                         self.personal.pauserotfuel_finished = self.pauserotfuel_finished;
126                         self.personal.pauseregen_finished = self.pauseregen_finished;
127                         self.personal.strength_finished = self.strength_finished;
128                         self.personal.invincible_finished = self.invincible_finished;
129                         self.personal.teleport_time = time;
130                         break; // this part itself doesn't cheat, so let's not count this
131                 case CHIMPULSE_CLONE_MOVING:
132                         IS_CHEAT(i, 0, 0);
133                         makevectors (self.v_angle);
134                         self.velocity = self.velocity + v_forward * 300;
135                         CopyBody(1);
136                         self.lip += 1;
137                         self.velocity = self.velocity - v_forward * 300;
138                         DID_CHEAT();
139                         break;
140                 case CHIMPULSE_CLONE_STANDING:
141                         IS_CHEAT(i, 0, 0);
142                         CopyBody(0);
143                         self.lip += 1;
144                         DID_CHEAT();
145                         break;
146                 case CHIMPULSE_GIVE_ALL:
147                         IS_CHEAT(i, 0, 0);
148                         CheatCommand(tokenize_console("give all"));
149                         break; // already counted as cheat
150                 case CHIMPULSE_SPEEDRUN:
151                         IS_CHEAT(i, 0, 0);
152                         if(self.personal)
153                         {
154                                 self.speedrunning = TRUE;
155                                 tracebox(self.personal.origin, self.mins, self.maxs, self.personal.origin, MOVE_WORLDONLY, self);
156                                 if(trace_startsolid)
157                                 {
158                                         sprint(self, "Cannot move there, cheater - only waypoints set using g_waypointsprite_personal work\n");
159                                 }
160                                 else
161                                 {
162                                         // Abort speedrun, teleport back
163                                         setorigin(self, self.personal.origin);
164                                         self.oldvelocity = self.velocity = self.personal.velocity;
165                                         self.angles = self.personal.v_angle;
166                                         self.fixangle = TRUE;
167                                         if(self.flagcarried)
168                                         {
169                                                 bprint("The ", self.flagcarried.netname, " was returned to base by its carrier\n");
170                                                 ReturnFlag(self.flagcarried);
171                                         }
172                                 }
173                                 if(g_ctf)
174                                 {
175                                         self.ammo_rockets = 999;
176                                         self.ammo_nails = 999;
177                                         self.ammo_cells = 999;
178                                         self.ammo_shells = 999;
179                                         self.ammo_fuel = 999;
180                                         self.health = start_health;
181                                         self.armorvalue = start_armorvalue;
182                                         self.weapons |= weaponsInMap;
183                                         self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
184                                         self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
185                                         self.pauserotfuel_finished = time + cvar("g_balance_pause_fuel_rot_spawn");
186                                         self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
187                                         self.strength_finished = 0;
188                                         self.invincible_finished = 0;
189                                 }
190                                 else
191                                 {
192                                         self.ammo_rockets = self.personal.ammo_rockets;
193                                         self.ammo_nails = self.personal.ammo_nails;
194                                         self.ammo_cells = self.personal.ammo_cells;
195                                         self.ammo_shells = self.personal.ammo_shells;
196                                         self.ammo_fuel = self.personal.ammo_fuel;
197                                         self.health = self.personal.health;
198                                         self.armorvalue = self.personal.armorvalue;
199                                         self.weapons = self.personal.weapons;
200                                         self.items = self.personal.items;
201                                         self.pauserotarmor_finished = time + self.personal.pauserotarmor_finished - self.personal.teleport_time;
202                                         self.pauserothealth_finished = time + self.personal.pauserothealth_finished - self.personal.teleport_time;
203                                         self.pauserotfuel_finished = time + self.personal.pauserotfuel_finished - self.personal.teleport_time;
204                                         self.pauseregen_finished = time + self.personal.pauseregen_finished - self.personal.teleport_time;
205                                         self.strength_finished = time + self.personal.strength_finished - self.personal.teleport_time;
206                                         self.invincible_finished = time + self.personal.invincible_finished - self.personal.teleport_time;
207                                 }
208                                 DID_CHEAT();
209                                 break;
210                         }
211                         if(self.deadflag != DEAD_NO)
212                                 sprint(self, "UR DEAD AHAHAH))\n");
213                         else
214                                 sprint(self, "No waypoint set, cheater (use g_waypointsprite_personal to set one)\n");
215                         break;
216                 case CHIMPULSE_TELEPORT:
217                         IS_CHEAT(i, 0, 0);
218                         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, ((sv_cheats >= 2) ? 100000 : 100), 1024, 256))
219                         {
220                                 self.angles_x = -self.angles_x;
221                                 self.fixangle = TRUE;
222                                 self.velocity = '0 0 0';
223                                 DID_CHEAT();
224                                 break;
225                         }
226                         sprint(self, "Emergency teleport could not find a good location, forget it!\n");
227                         break;
228                 case CHIMPULSE_R00T:
229                         IS_CHEAT(i, 0, 0);
230                         FOR_EACH_PLAYER(e)
231                         {
232                                 get_model_parameters(e.playermodel, e.skinindex);
233                                 if(get_model_parameters_sex == "Female")
234                                 {
235                                         makevectors(e.angles);
236                                         traceline(e.origin, e.origin + v_right * 256, MOVE_NORMAL, e);
237                                 }
238                                 else
239                                 {
240                                         org_x = random();
241                                         org_y = random();
242                                         org_z = 0;
243                                         org = normalize(org);
244                                         traceline(e.origin, e.origin + org * 256, MOVE_NORMAL, e); // random direction
245                                 }
246
247                                 org = findbetterlocation(trace_endpos, 12);
248
249                                 e2 = spawn();
250                                 setorigin(e2, org);
251                                 pointparticles(particleeffectnum("rocket_explode"), org, '0 0 0', 1);
252                                 sound(e2, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
253                                 RadiusDamage(e2, e, 1000, 0, 128, e, 500, DEATH_CHEAT, world);
254                                 remove(e2);
255                         }
256                         print("404 Sportsmanship not found.\n");
257                         DID_CHEAT();
258                         break;
259         }
260
261         END_CHEAT_FUNCTION();
262 }
263
264 void DragBox_Think();
265 float drag_lastcnt;
266 float CheatCommand(float argc)
267 {
268         BEGIN_CHEAT_FUNCTION();
269         string cmd;
270         cmd = argv(0);
271         switch(cmd)
272         {
273                 entity e;
274                 float effectnum, f;
275                 vector start, end;
276                 entity oldself;
277
278                 case "pointparticles":
279                         IS_CHEAT(0, argc, 0);
280                         if(argc == 5)
281                         {
282                                 // arguments:
283                                 //   effectname
284                                 //   origin (0..1, on crosshair line)
285                                 //   velocity
286                                 //   howmany
287                                 effectnum = particleeffectnum(argv(1));
288                                 f = stof(argv(2));
289                                 crosshair_trace(self);
290                                 start = (1-f) * self.origin + f * trace_endpos;
291                                 end = stov(argv(3));
292                                 f = stof(argv(4));
293                                 pointparticles(effectnum, start, end, f);
294                                 DID_CHEAT();
295                                 break;
296                         }
297                         sprint(self, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
298                         break;
299                 case "trailparticles":
300                         IS_CHEAT(0, argc, 0);
301                         if(argc == 2)
302                         {
303                                 // arguments:
304                                 //   effectname
305                                 effectnum = particleeffectnum(argv(1));
306                                 W_SetupShot(self, FALSE, FALSE, "", CHAN_WEAPON, 0);
307                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
308                                 trailparticles(self, effectnum, w_shotorg, trace_endpos);
309                                 DID_CHEAT();
310                                 break;
311                         }
312                         sprint(self, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
313                         break;
314                 case "make":
315                         IS_CHEAT(0, argc, 0);
316                         if(argc == 3)
317                         {
318                                 // arguments:
319                                 //   modelname mode
320                                 f = stof(argv(2));
321                                 W_SetupShot(self, FALSE, FALSE, "", CHAN_WEAPON, 0);
322                                 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, self);
323                                 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
324                                 {
325                                         sprint(self, "cannot make stuff there (bad surface)\n");
326                                 }
327                                 else
328                                 {
329                                         e = spawn();
330                                         e.model = strzone(argv(1));
331                                         e.mdl = "rocket_explode";
332                                         e.health = 1000;
333                                         setorigin(e, trace_endpos);
334                                         e.effects = EF_NOMODELFLAGS;
335                                         if(f == 1)
336                                         {
337                                                 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
338                                                 e.angles = AnglesTransform_ApplyToAngles(e.angles, '-90 0 0'); // so unrotated models work
339                                         }
340                                         oldself = self;
341                                         self = e;
342                                         spawnfunc_func_breakable();
343                                         self = oldself;
344                                         // now, is it valid?
345                                         if(f == 0)
346                                         {
347                                                 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
348                                                 if(trace_startsolid)
349                                                 {
350                                                         remove(e);
351                                                         sprint(self, "cannot make stuff there (no space)\n");
352                                                 }
353                                                 else
354                                                         DID_CHEAT();
355                                         }
356                                         else
357                                                 DID_CHEAT();
358                                 }
359                         }
360                         else
361                                 sprint(self, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
362                         break;
363                 case "penalty":
364                         IS_CHEAT(0, argc, 0);
365                         if(argc == 3)
366                         {
367                                 race_ImposePenaltyTime(self, stof(argv(1)), argv(2));
368                                 DID_CHEAT();
369                                 break;
370                         }
371                         sprint(self, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
372                         break;
373                 case "dragbox_spawn":
374                         IS_CHEAT(0, argc, 0);
375                         e = spawn();
376                         e.classname = "dragbox_box";
377                         e.think = DragBox_Think;
378                         e.nextthink = time;
379                         e.solid = -1; // black
380                         setmodel(e, "null"); // network it
381                         if(argc == 4)
382                                 e.cnt = stof(argv(1));
383                         else
384                                 e.cnt = max(0, drag_lastcnt);
385
386                         e.aiment = spawn();
387                         e.aiment.classname = "dragbox_corner_1";
388                         e.aiment.owner = e;
389                         setmodel(e.aiment, "models/marker.md3");
390                         e.aiment.skin = 0;
391                         setsize(e.aiment, '0 0 0', '0 0 0');
392                         if(argc == 4)
393                                 setorigin(e.aiment, stov(argv(2)));
394                         else
395                         {
396                                 crosshair_trace(self);
397                                 setorigin(e.aiment, trace_endpos);
398                         }
399
400                         e.enemy = spawn();
401                         e.enemy.classname = "dragbox_corner_2";
402                         e.enemy.owner = e;
403                         setmodel(e.enemy, "models/marker.md3");
404                         e.enemy.skin = 1;
405                         setsize(e.enemy, '0 0 0', '0 0 0');
406                         end = normalize(self.origin + self.view_ofs - e.aiment.origin);
407                         end_x = (end_x > 0) * 2 - 1;
408                         end_y = (end_y > 0) * 2 - 1;
409                         end_z = (end_z > 0) * 2 - 1;
410                         if(argc == 4)
411                                 setorigin(e.enemy, stov(argv(3)));
412                         else
413                                 setorigin(e.enemy, e.aiment.origin + 32 * end);
414
415                         e.killindicator = spawn();
416                         e.killindicator.classname = "drag_digit";
417                         e.killindicator.owner = e;
418                         setattachment(e.killindicator, e, "");
419                         setorigin(e.killindicator, '0 0 -8');
420                         e.killindicator.killindicator = spawn();
421                         e.killindicator.killindicator.classname = "drag_digit";
422                         e.killindicator.killindicator.owner = e;
423                         setattachment(e.killindicator.killindicator, e, "");
424                         setorigin(e.killindicator.killindicator, '0 0 8');
425                         DID_CHEAT();
426                         break;
427                 case "dragpoint_spawn":
428                         IS_CHEAT(0, argc, 0);
429                         e = spawn();
430                         e.classname = "dragpoint";
431                         e.think = DragBox_Think;
432                         e.nextthink = time;
433                         e.solid = 0; // nothing special
434                         setmodel(e, "models/marker.md3");
435                         setsize(e, PL_MIN, PL_MAX);
436                         e.skin = 2;
437                         if(argc == 3)
438                                 e.cnt = stof(argv(1));
439                         else
440                                 e.cnt = drag_lastcnt;
441                         if(argc == 3)
442                                 setorigin(e, stov(argv(2)));
443                         else
444                         {
445                                 crosshair_trace(self);
446                                 setorigin(e, trace_endpos + normalize(self.origin + self.view_ofs - trace_endpos));
447                                 move_out_of_solid(e);
448                         }
449
450                         e.killindicator = spawn();
451                         e.killindicator.classname = "drag_digit";
452                         e.killindicator.owner = e;
453                         setattachment(e.killindicator, e, "");
454                         setorigin(e.killindicator, '0 0 40');
455                         e.killindicator.killindicator = spawn();
456                         e.killindicator.killindicator.classname = "drag_digit";
457                         e.killindicator.killindicator.owner = e;
458                         setattachment(e.killindicator.killindicator, e, "");
459                         setorigin(e.killindicator.killindicator, '0 0 56');
460                         DID_CHEAT();
461                         break;
462                 case "drag_remove":
463                         IS_CHEAT(0, argc, 0);
464                         RandomSelection_Init();
465                         crosshair_trace(self);
466                         for(e = world; (e = find(e, classname, "dragbox_box")); )
467                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
468                         for(e = world; (e = find(e, classname, "dragpoint")); )
469                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
470                         if(RandomSelection_chosen_ent)
471                         {
472                                 remove(RandomSelection_chosen_ent.killindicator.killindicator);
473                                 remove(RandomSelection_chosen_ent.killindicator);
474                                 if(RandomSelection_chosen_ent.aiment)
475                                         remove(RandomSelection_chosen_ent.aiment);
476                                 if(RandomSelection_chosen_ent.enemy)
477                                         remove(RandomSelection_chosen_ent.enemy);
478                                 remove(RandomSelection_chosen_ent);
479                         }
480                         DID_CHEAT();
481                         break;
482                 case "drag_setcnt":
483                         IS_CHEAT(0, argc, 0);
484                         if(argc == 2)
485                         {
486                                 RandomSelection_Init();
487                                 crosshair_trace(self);
488                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
489                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
490                                 for(e = world; (e = find(e, classname, "dragpoint")); )
491                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
492                                 if(RandomSelection_chosen_ent)
493                                 {
494                                         if(substring(argv(1), 0, 1) == "*")
495                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
496                                         else
497                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
498                                 }
499                                 DID_CHEAT();
500                                 break;
501                         }
502                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
503                         break;
504                 case "drag_save":
505                         IS_CHEAT(0, argc, 0);
506                         if(argc == 2)
507                         {
508                                 f = fopen(argv(1), FILE_WRITE);
509                                 fputs(f, "cmd drag_clear\n");
510                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
511                                 {
512                                         fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
513                                 }
514                                 for(e = world; (e = find(e, classname, "dragpoint")); )
515                                 {
516                                         fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
517                                 }
518                                 fclose(f);
519                                 DID_CHEAT();
520                                 break;
521                         }
522                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
523                         break;
524                 case "drag_saveraceent":
525                         IS_CHEAT(0, argc, 0);
526                         if(argc == 2)
527                         {
528                                 f = fopen(argv(1), FILE_WRITE);
529                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
530                                 {
531                                         fputs(f, "{\n");
532                                         fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
533                                         fputs(f, strcat("\"origin\" \"", ftos(e.absmin_x), " ", ftos(e.absmin_y), " ", ftos(e.absmin_z), "\"\n"));
534                                         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"));
535                                         fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
536                                         fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
537                                         fputs(f, "}\n");
538                                 }
539                                 for(e = world; (e = find(e, classname, "dragpoint")); )
540                                 {
541                                         start = '0 0 0';
542                                         effectnum = 0;
543                                         for(oldself = world; (oldself = find(oldself, classname, "dragbox_box")); )
544                                         {
545                                                 if(e.cnt <= 0 && oldself.cnt == 0 || e.cnt == oldself.cnt)
546                                                 {
547                                                         start = start + oldself.origin;
548                                                         ++effectnum;
549                                                 }
550                                         }
551                                         start *= 1 / effectnum;
552                                         fputs(f, "{\n");
553                                         fputs(f, "\"classname\" \"info_player_race\"\n");
554                                         fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
555                                         fputs(f, strcat("\"origin\" \"", ftos(e.origin_x), " ", ftos(e.origin_y), " ", ftos(e.origin_z), "\"\n"));
556                                         if(e.cnt == -2)
557                                         {
558                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
559                                                 fputs(f, "\"race_place\" \"0\"\n");
560                                         }
561                                         else if(e.cnt == -1)
562                                         {
563                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
564                                                 fputs(f, "\"race_place\" \"-1\"\n");
565                                         }
566                                         else
567                                         {
568                                                 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
569                                                 if(e.cnt == 0)
570                                                 {
571                                                         // these need race_place
572                                                         // counting...
573                                                         effectnum = 1;
574                                                         for(oldself = world; (oldself = find(oldself, classname, "dragpoint")); )
575                                                         if(oldself.cnt == 0)
576                                                         {
577                                                                 if(vlen(oldself.origin - start) < vlen(e.origin - start))
578                                                                         ++effectnum;
579                                                                 else if(vlen(oldself.origin - start) == vlen(e.origin - start) && num_for_edict(oldself) < num_for_edict(e))
580                                                                         ++effectnum;
581                                                         }
582                                                         fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
583                                                 }
584                                         }
585                                         fputs(f, "}\n");
586                                 }
587                                 fclose(f);
588                                 DID_CHEAT();
589                                 break;
590                         }
591                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
592                         break;
593                 case "drag_clear":
594                         IS_CHEAT(0, argc, 0);
595                         for(e = world; (e = find(e, classname, "dragbox_box")); )
596                                 remove(e);
597                         for(e = world; (e = find(e, classname, "dragbox_corner_1")); )
598                                 remove(e);
599                         for(e = world; (e = find(e, classname, "dragbox_corner_2")); )
600                                 remove(e);
601                         for(e = world; (e = find(e, classname, "dragpoint")); )
602                                 remove(e);
603                         for(e = world; (e = find(e, classname, "drag_digit")); )
604                                 remove(e);
605                         DID_CHEAT();
606                         break;
607                 case "warp":
608                         IS_CHEAT(0, argc, 0);
609                         if(argc == 2) if(cvar("g_campaign"))
610                         {
611                                 CampaignLevelWarp(stof(argv(1)));
612                                 DID_CHEAT();
613                         }
614                         break;
615                 case "god":
616                         IS_CHEAT(0, argc, 0);
617                         BITXOR_ASSIGN(self.flags, FL_GODMODE);
618                         if(self.flags & FL_GODMODE)
619                         {
620                                 sprint(self, "godmode ON\n");
621                                 DID_CHEAT();
622                         }
623                         else
624                                 sprint(self, "godmode OFF\n");
625                         break;
626                 case "notarget":
627                         IS_CHEAT(0, argc, 0);
628                         BITXOR_ASSIGN(self.flags, FL_NOTARGET);
629                         if(self.flags & FL_NOTARGET)
630                         {
631                                 sprint(self, "notarget ON\n");
632                                 DID_CHEAT();
633                         }
634                         else
635                                 sprint(self, "notarget OFF\n");
636                         break;
637                 case "noclip":
638                         IS_CHEAT(0, argc, 0);
639                         if(self.movetype != MOVETYPE_NOCLIP)
640                         {
641                                 self.movetype = MOVETYPE_NOCLIP;
642                                 sprint(self, "noclip ON\n");
643                                 DID_CHEAT();
644                         }
645                         else
646                         {
647                                 self.movetype = MOVETYPE_WALK;
648                                 sprint(self, "noclip OFF\n");
649                         }
650                         break;
651                 case "fly":
652                         IS_CHEAT(0, argc, 0);
653                         if(self.movetype != MOVETYPE_FLY)
654                         {
655                                 self.movetype = MOVETYPE_FLY;
656                                 sprint(self, "flymode ON\n");
657                                 DID_CHEAT();
658                         }
659                         else
660                         {
661                                 self.movetype = MOVETYPE_WALK;
662                                 sprint(self, "flymode OFF\n");
663                         }
664                         break;
665                 case "give":
666                         IS_CHEAT(0, argc, 0);
667                         if(GiveItems(self, 1, argc))
668                                 DID_CHEAT();
669                         break;
670         }
671
672         END_CHEAT_FUNCTION();
673 }
674
675 void crosshair_trace_plusvisibletriggers(entity pl);
676 void Drag_Begin(entity dragger, entity draggee, vector touchpoint);
677 void Drag_Finish(entity dragger);
678 float Drag_IsDraggable(entity draggee);
679 float Drag_MayChangeAngles(entity draggee);
680 void Drag_MoveForward(entity dragger);
681 void Drag_SetSpeed(entity dragger, float s);
682 void Drag_MoveBackward(entity dragger);
683 void Drag_Update(entity dragger);
684 float Drag_CanDrag(entity dragger);
685 float Drag_IsDragging(entity dragger);
686 void Drag_MoveDrag(entity from, entity to);
687 .entity dragentity;
688
689 float CheatFrame()
690 {
691         BEGIN_CHEAT_FUNCTION();
692
693         if(Drag_IsDragging(self))
694         {
695                 if(self.BUTTON_DRAG)
696                 {
697                         if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
698                         {
699                                 Drag_MoveForward(self);
700                                 self.impulse = 0;
701                         }
702                         else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19)
703                         {
704                                 Drag_MoveBackward(self);
705                                 self.impulse = 0;
706                         }
707                         else if(self.impulse >= 1 && self.impulse <= 9)
708                         {
709                                 Drag_SetSpeed(self, self.impulse - 1);
710                         }
711                         else if(self.impulse == 14)
712                         {
713                                 Drag_SetSpeed(self, 9);
714                         }
715
716                         if(frametime)
717                                 Drag_Update(self);
718                 }
719                 else
720                 {
721                         Drag_Finish(self);
722                 }
723         }
724         else
725         {
726                 if(Drag_CanDrag(self))
727                         if(self.BUTTON_DRAG)
728                         {
729                                 crosshair_trace_plusvisibletriggers(self);
730                                 if(trace_ent)
731                                         if(Drag_IsDraggable(trace_ent))
732                                                 switch(0)
733                                                 {
734                                                         default:
735                                                                 IS_CHEAT(0, 0, CHRAME_DRAG);
736                                                                 if(trace_ent.draggedby)
737                                                                         Drag_Finish(trace_ent.draggedby);
738                                                                 if(trace_ent.tag_entity)
739                                                                         detach_sameorigin(trace_ent);
740                                                                 Drag_Begin(self, trace_ent, trace_endpos);
741                                                                 DID_CHEAT();
742                                                                 break;
743                                                 }
744                         }
745         }
746
747         END_CHEAT_FUNCTION();
748 }
749
750
751
752
753
754 // ENTITY DRAGGING
755
756 void crosshair_trace_plusvisibletriggers(entity pl)
757 {
758         entity first;
759         entity e;
760         first = findchainfloat(solid, SOLID_TRIGGER);
761
762         for (e = first; e; e = e.chain)
763                 if (e.model != "")
764                         e.solid = SOLID_BSP;
765
766         crosshair_trace(pl);
767
768         for (e = first; e; e = e.chain)
769                 e.solid = SOLID_TRIGGER;
770 }
771
772 // on dragger:
773 .float draggravity;
774 .float dragspeed; // speed of mouse wheel action
775 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
776 .vector draglocalvector; // local attachment vector of the dragentity
777 .float draglocalangle;
778 // on draggee:
779 .entity draggedby;
780 .float dragmovetype;
781 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
782 {
783         float tagscale;
784
785         draggee.dragmovetype = draggee.movetype;
786         draggee.draggravity = draggee.gravity;
787         draggee.movetype = MOVETYPE_WALK;
788         draggee.gravity = 0.00001;
789         draggee.flags &~= FL_ONGROUND;
790         draggee.draggedby = dragger;
791
792         dragger.dragentity = draggee;
793
794         dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
795         dragger.draglocalangle = draggee.angles_y - dragger.v_angle_y;
796         touchpoint = touchpoint - gettaginfo(draggee, 0);
797         tagscale = pow(vlen(v_forward), -2);
798         dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
799         dragger.draglocalvector_y = touchpoint * v_right * tagscale;
800         dragger.draglocalvector_z = touchpoint * v_up * tagscale;
801
802         dragger.dragspeed = 64;
803 }
804
805 void Drag_Finish(entity dragger)
806 {
807         entity draggee;
808         draggee = dragger.dragentity;
809         if(dragger)
810                 dragger.dragentity = world;
811         draggee.draggedby = world;
812         draggee.movetype = draggee.dragmovetype;
813         draggee.gravity = draggee.draggravity;
814
815         switch(draggee.movetype)
816         {
817                 case MOVETYPE_TOSS:
818                 case MOVETYPE_WALK:
819                 case MOVETYPE_STEP:
820                 case MOVETYPE_FLYMISSILE:
821                 case MOVETYPE_BOUNCE:
822                 case MOVETYPE_BOUNCEMISSILE:
823                 case MOVETYPE_PHYSICS:
824                         break;
825                 default:
826                         draggee.velocity = '0 0 0';
827                         break;
828         }
829
830         if((draggee.flags & FL_ITEM) && (vlen(draggee.velocity) < 32))
831         {
832                 draggee.velocity = '0 0 0';
833                 draggee.flags |= FL_ONGROUND; // floating items are FUN
834         }
835 }
836
837 float Drag_IsDraggable(entity draggee)
838 {
839         // TODO add more checks for bad stuff here
840         if(draggee.classname == "func_bobbing")
841                 return FALSE;
842         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)
843                 return FALSE;
844         if(draggee.classname == "plat")
845                 return FALSE;
846         if(draggee.classname == "func_button")
847                 return FALSE;
848 //      if(draggee.model == "")
849 //              return FALSE;
850         if(draggee.classname == "spectator")
851                 return FALSE;
852         if(draggee.classname == "observer")
853                 return FALSE;
854         if(draggee.classname == "exteriorweaponentity")
855                 return FALSE;
856         if(draggee.classname == "weaponentity")
857                 return FALSE;
858
859         return TRUE;
860 }
861
862 float Drag_MayChangeAngles(entity draggee)
863 {
864         // TODO add more checks for bad stuff here
865         if(substring(draggee.model, 0, 1) == "*")
866                 return FALSE;
867         return TRUE;
868 }
869
870 void Drag_MoveForward(entity dragger)
871 {
872         dragger.dragdistance += dragger.dragspeed;
873 }
874
875 void Drag_SetSpeed(entity dragger, float s)
876 {
877         dragger.dragspeed = pow(2, s);
878 }
879
880 void Drag_MoveBackward(entity dragger)
881 {
882         dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
883 }
884
885 void Drag_Update(entity dragger)
886 {
887         vector curorigin, neworigin, goodvelocity;
888         float f;
889         entity draggee;
890
891         draggee = dragger.dragentity;
892         draggee.flags &~= FL_ONGROUND;
893
894         curorigin = gettaginfo(draggee, 0);
895         curorigin = curorigin + v_forward * dragger.draglocalvector_x + v_right * dragger.draglocalvector_y + v_up * dragger.draglocalvector_z;
896         makevectors(dragger.v_angle);
897         neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
898         goodvelocity = (neworigin - curorigin) * (1 / frametime);
899
900         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle > 180)
901                 dragger.draglocalangle += 360;
902         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle <= -180)
903                 dragger.draglocalangle -= 360;
904
905         f = min(frametime * 10, 1);
906         draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
907
908         if(Drag_MayChangeAngles(draggee))
909                 draggee.angles_y = draggee.angles_y * (1 - f) + (dragger.v_angle_y + dragger.draglocalangle) * f;
910
911         draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
912
913         te_lightning1(dragger, dragger.origin + dragger.view_ofs, curorigin);
914 }
915
916 float Drag_CanDrag(entity dragger)
917 {
918         return (dragger.deadflag == DEAD_NO) || (dragger.classname == "player");
919 }
920
921 float Drag_IsDragging(entity dragger)
922 {
923         if(!dragger.dragentity)
924                 return FALSE;
925         if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
926         {
927                 dragger.dragentity = world;
928                 return FALSE;
929         }
930         if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity))
931         {
932                 Drag_Finish(dragger);
933                 return FALSE;
934         }
935         return TRUE;
936 }
937
938 void Drag_MoveDrag(entity from, entity to)
939 {
940         if(from.draggedby)
941         {
942                 to.draggedby = from.draggedby;
943                 to.draggedby.dragentity = to;
944                 from.draggedby = world;
945         }
946 }
947
948
949
950
951
952
953 void DragBox_Think()
954 {
955         if(self.aiment && self.enemy)
956         {
957                 self.origin_x = (self.aiment.origin_x + self.enemy.origin_x) * 0.5;
958                 self.origin_y = (self.aiment.origin_y + self.enemy.origin_y) * 0.5;
959                 self.origin_z = (self.aiment.origin_z + self.enemy.origin_z) * 0.5;
960                 self.maxs_x = fabs(self.aiment.origin_x - self.enemy.origin_x) * 0.5;
961                 self.maxs_y = fabs(self.aiment.origin_y - self.enemy.origin_y) * 0.5;
962                 self.maxs_z = fabs(self.aiment.origin_z - self.enemy.origin_z) * 0.5;
963                 self.mins = -1 * self.maxs;
964                 setorigin(self, self.origin); setsize(self, self.mins, self.maxs); // link edict
965         }
966
967         if(self.cnt == -1) // actually race_place -1
968         {
969                 // show "10 10" for qualifying spawns
970                 setmodel(self.killindicator, "models/sprites/10.spr32");
971                 setmodel(self.killindicator.killindicator, "models/sprites/10.spr32");
972         }
973         else if(self.cnt == -2) // actually race_place 0
974         {
975                 // show "10 0" for loser spawns
976                 setmodel(self.killindicator, "models/sprites/10.spr32");
977                 setmodel(self.killindicator.killindicator, "models/sprites/0.spr32");
978         }
979         else
980         {
981                 setmodel(self.killindicator, strcat("models/sprites/", ftos(mod(self.cnt, 10)), ".spr32"));
982                 setmodel(self.killindicator.killindicator, strcat("models/sprites/", ftos(floor(self.cnt / 10)), ".spr32"));
983         }
984
985         self.nextthink = time;
986 }
987
988 #endif