6ef82679444fc021f0e4d53b42bf278333cf9b8f
[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, "",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, "", 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, "flmodey 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 Drag_Begin(entity dragger, entity draggee, vector touchpoint);
676 void Drag_Finish(entity dragger);
677 float Drag_IsDraggable(entity draggee);
678 float Drag_MayChangeAngles(entity draggee);
679 void Drag_MoveForward(entity dragger);
680 void Drag_SetSpeed(entity dragger, float s);
681 void Drag_MoveBackward(entity dragger);
682 void Drag_Update(entity dragger);
683 float Drag_CanDrag(entity dragger);
684 float Drag_IsDragging(entity dragger);
685 void Drag_MoveDrag(entity from, entity to);
686 .entity dragentity;
687
688 float CheatFrame()
689 {
690         BEGIN_CHEAT_FUNCTION();
691
692         if(Drag_IsDragging(self))
693         {
694                 if(self.BUTTON_DRAG)
695                 {
696                         if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
697                         {
698                                 Drag_MoveForward(self);
699                                 self.impulse = 0;
700                         }
701                         else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19)
702                         {
703                                 Drag_MoveBackward(self);
704                                 self.impulse = 0;
705                         }
706                         else if(self.impulse >= 1 && self.impulse <= 9)
707                         {
708                                 Drag_SetSpeed(self, self.impulse - 1);
709                         }
710                         else if(self.impulse == 14)
711                         {
712                                 Drag_SetSpeed(self, 9);
713                         }
714
715                         if(frametime)
716                                 Drag_Update(self);
717                 }
718                 else
719                 {
720                         Drag_Finish(self);
721                 }
722         }
723         else
724         {
725                 if(Drag_CanDrag(self))
726                         if(self.BUTTON_DRAG)
727                         {
728                                 crosshair_trace(self);
729                                 if(trace_ent)
730                                         if(Drag_IsDraggable(trace_ent))
731                                                 switch(0)
732                                                 {
733                                                         default:
734                                                                 IS_CHEAT(0, 0, CHRAME_DRAG);
735                                                                 if(trace_ent.draggedby)
736                                                                         Drag_Finish(trace_ent.draggedby);
737                                                                 if(trace_ent.tag_entity)
738                                                                         detach_sameorigin(trace_ent);
739                                                                 Drag_Begin(self, trace_ent, trace_endpos);
740                                                                 DID_CHEAT();
741                                                                 break;
742                                                 }
743                         }
744         }
745
746         END_CHEAT_FUNCTION();
747 }
748
749
750
751
752
753 // ENTITY DRAGGING
754
755 // on dragger:
756 .float draggravity;
757 .float dragspeed; // speed of mouse wheel action
758 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
759 .vector draglocalvector; // local attachment vector of the dragentity
760 .float draglocalangle;
761 // on draggee:
762 .entity draggedby;
763 .float dragmovetype;
764 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
765 {
766         float tagscale;
767
768         draggee.dragmovetype = draggee.movetype;
769         draggee.draggravity = draggee.gravity;
770         draggee.movetype = MOVETYPE_WALK;
771         draggee.gravity = 0.00001;
772         draggee.flags &~= FL_ONGROUND;
773         draggee.draggedby = dragger;
774
775         dragger.dragentity = draggee;
776
777         dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
778         dragger.draglocalangle = draggee.angles_y - dragger.v_angle_y;
779         touchpoint = touchpoint - gettaginfo(draggee, 0);
780         tagscale = pow(vlen(v_forward), -2);
781         dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
782         dragger.draglocalvector_y = touchpoint * v_right * tagscale;
783         dragger.draglocalvector_z = touchpoint * v_up * tagscale;
784
785         dragger.dragspeed = 64;
786 }
787
788 void Drag_Finish(entity dragger)
789 {
790         entity draggee;
791         draggee = dragger.dragentity;
792         if(dragger)
793                 dragger.dragentity = world;
794         draggee.draggedby = world;
795         draggee.movetype = draggee.dragmovetype;
796         draggee.gravity = draggee.draggravity;
797
798         switch(draggee.movetype)
799         {
800                 case MOVETYPE_TOSS:
801                 case MOVETYPE_WALK:
802                 case MOVETYPE_STEP:
803                 case MOVETYPE_FLYMISSILE:
804                 case MOVETYPE_BOUNCE:
805                 case MOVETYPE_BOUNCEMISSILE:
806                 case MOVETYPE_PHYSICS:
807                         break;
808                 default:
809                         draggee.velocity = '0 0 0';
810                         break;
811         }
812
813         if((draggee.flags & FL_ITEM) && (vlen(draggee.velocity) < 32))
814         {
815                 draggee.velocity = '0 0 0';
816                 draggee.flags |= FL_ONGROUND; // floating items are FUN
817         }
818 }
819
820 float Drag_IsDraggable(entity draggee)
821 {
822         // TODO add more checks for bad stuff here
823         if(draggee.classname == "func_bobbing")
824                 return FALSE;
825         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)
826                 return FALSE;
827         if(draggee.classname == "plat")
828                 return FALSE;
829         if(draggee.classname == "func_button")
830                 return FALSE;
831         if(draggee.model == "")
832                 return FALSE;
833         if(draggee.classname == "spectator")
834                 return FALSE;
835         if(draggee.classname == "observer")
836                 return FALSE;
837         if(draggee.classname == "exteriorweaponentity")
838                 return FALSE;
839
840         return TRUE;
841 }
842
843 float Drag_MayChangeAngles(entity draggee)
844 {
845         // TODO add more checks for bad stuff here
846         if(substring(draggee.model, 0, 1) == "*")
847                 return FALSE;
848         return TRUE;
849 }
850
851 void Drag_MoveForward(entity dragger)
852 {
853         dragger.dragdistance += dragger.dragspeed;
854 }
855
856 void Drag_SetSpeed(entity dragger, float s)
857 {
858         dragger.dragspeed = pow(2, s);
859 }
860
861 void Drag_MoveBackward(entity dragger)
862 {
863         dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
864 }
865
866 void Drag_Update(entity dragger)
867 {
868         vector curorigin, neworigin, goodvelocity;
869         float f;
870         entity draggee;
871
872         draggee = dragger.dragentity;
873         draggee.flags &~= FL_ONGROUND;
874
875         curorigin = gettaginfo(draggee, 0);
876         curorigin = curorigin + v_forward * dragger.draglocalvector_x + v_right * dragger.draglocalvector_y + v_up * dragger.draglocalvector_z;
877         makevectors(dragger.v_angle);
878         neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
879         goodvelocity = (neworigin - curorigin) * (1 / frametime);
880
881         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle > 180)
882                 dragger.draglocalangle += 360;
883         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle <= -180)
884                 dragger.draglocalangle -= 360;
885
886         f = min(frametime * 10, 1);
887         draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
888
889         if(Drag_MayChangeAngles(draggee))
890                 draggee.angles_y = draggee.angles_y * (1 - f) + (dragger.v_angle_y + dragger.draglocalangle) * f;
891
892         draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
893
894         te_lightning1(dragger, dragger.origin + dragger.view_ofs, curorigin);
895 }
896
897 float Drag_CanDrag(entity dragger)
898 {
899         return (dragger.deadflag == DEAD_NO) || (dragger.classname == "player");
900 }
901
902 float Drag_IsDragging(entity dragger)
903 {
904         if(!dragger.dragentity)
905                 return FALSE;
906         if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
907         {
908                 dragger.dragentity = world;
909                 return FALSE;
910         }
911         if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity))
912         {
913                 Drag_Finish(dragger);
914                 return FALSE;
915         }
916         return TRUE;
917 }
918
919 void Drag_MoveDrag(entity from, entity to)
920 {
921         if(from.draggedby)
922         {
923                 to.draggedby = from.draggedby;
924                 to.draggedby.dragentity = to;
925                 from.draggedby = world;
926         }
927 }
928
929
930
931
932
933
934 void DragBox_Think()
935 {
936         if(self.aiment && self.enemy)
937         {
938                 self.origin_x = (self.aiment.origin_x + self.enemy.origin_x) * 0.5;
939                 self.origin_y = (self.aiment.origin_y + self.enemy.origin_y) * 0.5;
940                 self.origin_z = (self.aiment.origin_z + self.enemy.origin_z) * 0.5;
941                 self.maxs_x = fabs(self.aiment.origin_x - self.enemy.origin_x) * 0.5;
942                 self.maxs_y = fabs(self.aiment.origin_y - self.enemy.origin_y) * 0.5;
943                 self.maxs_z = fabs(self.aiment.origin_z - self.enemy.origin_z) * 0.5;
944                 self.mins = -1 * self.maxs;
945                 setorigin(self, self.origin); setsize(self, self.mins, self.maxs); // link edict
946         }
947
948         if(self.cnt == -1) // actually race_place -1
949         {
950                 // show "10 10" for qualifying spawns
951                 setmodel(self.killindicator, "models/sprites/10.spr32");
952                 setmodel(self.killindicator.killindicator, "models/sprites/10.spr32");
953         }
954         else if(self.cnt == -2) // actually race_place 0
955         {
956                 // show "10 0" for loser spawns
957                 setmodel(self.killindicator, "models/sprites/10.spr32");
958                 setmodel(self.killindicator.killindicator, "models/sprites/0.spr32");
959         }
960         else
961         {
962                 setmodel(self.killindicator, strcat("models/sprites/", ftos(mod(self.cnt, 10)), ".spr32"));
963                 setmodel(self.killindicator.killindicator, strcat("models/sprites/", ftos(floor(self.cnt / 10)), ".spr32"));
964         }
965
966         self.nextthink = time;
967 }
968
969 #endif