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