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