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