Merge branch 'master' into Mario/vaporizer_damage
[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.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 {
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 {
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 {
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 {
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 {
305         BEGIN_CHEAT_FUNCTION();
306         string cmd;
307         cmd = argv(0);
308         switch(cmd)
309         {
310                 entity e;
311                 float effectnum, f;
312                 vector start, end;
313                 entity oldself;
314
315                 case "pointparticles":
316                         IS_CHEAT(0, argc, 0);
317                         if(argc == 5)
318                         {
319                                 // arguments:
320                                 //   effectname
321                                 //   origin (0..1, on crosshair line)
322                                 //   velocity
323                                 //   howmany
324                                 f = stof(argv(2));
325                                 crosshair_trace(self);
326                                 start = (1-f) * self.origin + f * trace_endpos;
327                                 end = stov(argv(3));
328                                 f = stof(argv(4));
329                                 Send_Effect_(argv(1), start, end, f);
330                                 DID_CHEAT();
331                                 break;
332                         }
333                         sprint(self, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
334                         break;
335                 case "trailparticles":
336                         IS_CHEAT(0, argc, 0);
337                         if(argc == 2)
338                         {
339                                 // arguments:
340                                 //   effectname
341                                 effectnum = _particleeffectnum(argv(1));
342                                 W_SetupShot(self, false, false, "", CH_WEAPON_A, 0);
343                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
344                                 trailparticles(self, effectnum, w_shotorg, trace_endpos);
345                                 DID_CHEAT();
346                                 break;
347                         }
348                         sprint(self, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
349                         break;
350                 case "make":
351                         IS_CHEAT(0, argc, 0);
352                         if(argc == 3)
353                         {
354                                 // arguments:
355                                 //   modelname mode
356                                 f = stof(argv(2));
357                                 W_SetupShot(self, false, false, "", CH_WEAPON_A, 0);
358                                 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, self);
359                                 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
360                                 {
361                                         sprint(self, "cannot make stuff there (bad surface)\n");
362                                 }
363                                 else
364                                 {
365                                         e = spawn();
366                                         e.model = strzone(argv(1));
367                                         e.mdl = "rocket_explode";
368                                         e.health = 1000;
369                                         setorigin(e, trace_endpos);
370                                         e.effects = EF_NOMODELFLAGS;
371                                         if(f == 1)
372                                         {
373                                                 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
374                                                 e.angles = AnglesTransform_ApplyToAngles(e.angles, '-90 0 0'); // so unrotated models work
375                                         }
376                                         oldself = self;
377                                         self = e;
378                                         spawnfunc_func_breakable();
379                                         self = oldself;
380                                         // now, is it valid?
381                                         if(f == 0)
382                                         {
383                                                 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
384                                                 if(trace_startsolid)
385                                                 {
386                                                         remove(e);
387                                                         sprint(self, "cannot make stuff there (no space)\n");
388                                                 }
389                                                 else
390                                                         DID_CHEAT();
391                                         }
392                                         else
393                                                 DID_CHEAT();
394                                 }
395                         }
396                         else
397                                 sprint(self, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
398                         break;
399                 case "penalty":
400                         IS_CHEAT(0, argc, 0);
401                         if(argc == 3)
402                         {
403                                 race_ImposePenaltyTime(self, stof(argv(1)), argv(2));
404                                 DID_CHEAT();
405                                 break;
406                         }
407                         sprint(self, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
408                         break;
409                 case "dragbox_spawn":
410                         IS_CHEAT(0, argc, 0);
411                         e = spawn();
412                         e.classname = "dragbox_box";
413                         e.think = DragBox_Think;
414                         e.nextthink = time;
415                         e.solid = -1; // black
416                         setmodel(e, "null"); // network it
417                         if(argc == 4)
418                                 e.cnt = stof(argv(1));
419                         else
420                                 e.cnt = max(0, drag_lastcnt);
421
422                         e.aiment = spawn();
423                         e.aiment.classname = "dragbox_corner_1";
424                         e.aiment.owner = e;
425                         setmodel(e.aiment, "models/marker.md3");
426                         e.aiment.skin = 0;
427                         setsize(e.aiment, '0 0 0', '0 0 0');
428                         if(argc == 4)
429                                 setorigin(e.aiment, stov(argv(2)));
430                         else
431                         {
432                                 crosshair_trace(self);
433                                 setorigin(e.aiment, trace_endpos);
434                         }
435
436                         e.enemy = spawn();
437                         e.enemy.classname = "dragbox_corner_2";
438                         e.enemy.owner = e;
439                         setmodel(e.enemy, "models/marker.md3");
440                         e.enemy.skin = 1;
441                         setsize(e.enemy, '0 0 0', '0 0 0');
442                         end = normalize(self.origin + self.view_ofs - e.aiment.origin);
443                         end.x = (end.x > 0) * 2 - 1;
444                         end.y = (end.y > 0) * 2 - 1;
445                         end.z = (end.z > 0) * 2 - 1;
446                         if(argc == 4)
447                                 setorigin(e.enemy, stov(argv(3)));
448                         else
449                                 setorigin(e.enemy, e.aiment.origin + 32 * end);
450
451                         e.killindicator = spawn();
452                         e.killindicator.classname = "drag_digit";
453                         e.killindicator.owner = e;
454                         setattachment(e.killindicator, e, "");
455                         setorigin(e.killindicator, '0 0 -8');
456                         e.killindicator.killindicator = spawn();
457                         e.killindicator.killindicator.classname = "drag_digit";
458                         e.killindicator.killindicator.owner = e;
459                         setattachment(e.killindicator.killindicator, e, "");
460                         setorigin(e.killindicator.killindicator, '0 0 8');
461                         DID_CHEAT();
462                         break;
463                 case "dragpoint_spawn":
464                         IS_CHEAT(0, argc, 0);
465                         e = spawn();
466                         e.classname = "dragpoint";
467                         e.think = DragBox_Think;
468                         e.nextthink = time;
469                         e.solid = 0; // nothing special
470                         setmodel(e, "models/marker.md3");
471                         setsize(e, PL_MIN, PL_MAX);
472                         e.skin = 2;
473                         if(argc == 3)
474                                 e.cnt = stof(argv(1));
475                         else
476                                 e.cnt = drag_lastcnt;
477                         if(argc == 3)
478                                 setorigin(e, stov(argv(2)));
479                         else
480                         {
481                                 crosshair_trace(self);
482                                 setorigin(e, trace_endpos + normalize(self.origin + self.view_ofs - trace_endpos));
483                                 move_out_of_solid(e);
484                         }
485
486                         e.killindicator = spawn();
487                         e.killindicator.classname = "drag_digit";
488                         e.killindicator.owner = e;
489                         setattachment(e.killindicator, e, "");
490                         setorigin(e.killindicator, '0 0 40');
491                         e.killindicator.killindicator = spawn();
492                         e.killindicator.killindicator.classname = "drag_digit";
493                         e.killindicator.killindicator.owner = e;
494                         setattachment(e.killindicator.killindicator, e, "");
495                         setorigin(e.killindicator.killindicator, '0 0 56');
496                         DID_CHEAT();
497                         break;
498                 case "drag_remove":
499                         IS_CHEAT(0, argc, 0);
500                         RandomSelection_Init();
501                         crosshair_trace(self);
502                         for(e = world; (e = find(e, classname, "dragbox_box")); )
503                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
504                         for(e = world; (e = find(e, classname, "dragpoint")); )
505                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
506                         if(RandomSelection_chosen_ent)
507                         {
508                                 remove(RandomSelection_chosen_ent.killindicator.killindicator);
509                                 remove(RandomSelection_chosen_ent.killindicator);
510                                 if(RandomSelection_chosen_ent.aiment)
511                                         remove(RandomSelection_chosen_ent.aiment);
512                                 if(RandomSelection_chosen_ent.enemy)
513                                         remove(RandomSelection_chosen_ent.enemy);
514                                 remove(RandomSelection_chosen_ent);
515                         }
516                         DID_CHEAT();
517                         break;
518                 case "drag_setcnt":
519                         IS_CHEAT(0, argc, 0);
520                         if(argc == 2)
521                         {
522                                 RandomSelection_Init();
523                                 crosshair_trace(self);
524                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
525                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
526                                 for(e = world; (e = find(e, classname, "dragpoint")); )
527                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
528                                 if(RandomSelection_chosen_ent)
529                                 {
530                                         if(substring(argv(1), 0, 1) == "*")
531                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
532                                         else
533                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
534                                 }
535                                 DID_CHEAT();
536                                 break;
537                         }
538                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
539                         break;
540                 case "drag_save":
541                         IS_CHEAT(0, argc, 0);
542                         if(argc == 2)
543                         {
544                                 f = fopen(argv(1), FILE_WRITE);
545                                 fputs(f, "cmd drag_clear\n");
546                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
547                                 {
548                                         fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
549                                 }
550                                 for(e = world; (e = find(e, classname, "dragpoint")); )
551                                 {
552                                         fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
553                                 }
554                                 fclose(f);
555                                 DID_CHEAT();
556                                 break;
557                         }
558                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
559                         break;
560                 case "drag_saveraceent":
561                         IS_CHEAT(0, argc, 0);
562                         if(argc == 2)
563                         {
564                                 f = fopen(argv(1), FILE_WRITE);
565                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
566                                 {
567                                         fputs(f, "{\n");
568                                         fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
569                                         fputs(f, strcat("\"origin\" \"", ftos(e.absmin.x), " ", ftos(e.absmin.y), " ", ftos(e.absmin.z), "\"\n"));
570                                         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"));
571                                         fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
572                                         fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
573                                         fputs(f, "}\n");
574                                 }
575                                 for(e = world; (e = find(e, classname, "dragpoint")); )
576                                 {
577                                         start = '0 0 0';
578                                         effectnum = 0;
579                                         for(oldself = world; (oldself = find(oldself, classname, "dragbox_box")); )
580                                         {
581                                                 if(e.cnt <= 0 && oldself.cnt == 0 || e.cnt == oldself.cnt)
582                                                 {
583                                                         start = start + oldself.origin;
584                                                         ++effectnum;
585                                                 }
586                                         }
587                                         start *= 1 / effectnum;
588                                         fputs(f, "{\n");
589                                         fputs(f, "\"classname\" \"info_player_race\"\n");
590                                         fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
591                                         fputs(f, strcat("\"origin\" \"", ftos(e.origin.x), " ", ftos(e.origin.y), " ", ftos(e.origin.z), "\"\n"));
592                                         if(e.cnt == -2)
593                                         {
594                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
595                                                 fputs(f, "\"race_place\" \"0\"\n");
596                                         }
597                                         else if(e.cnt == -1)
598                                         {
599                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
600                                                 fputs(f, "\"race_place\" \"-1\"\n");
601                                         }
602                                         else
603                                         {
604                                                 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
605                                                 if(e.cnt == 0)
606                                                 {
607                                                         // these need race_place
608                                                         // counting...
609                                                         effectnum = 1;
610                                                         for(oldself = world; (oldself = find(oldself, classname, "dragpoint")); )
611                                                         if(oldself.cnt == 0)
612                                                         {
613                                                                 if(vlen(oldself.origin - start) < vlen(e.origin - start))
614                                                                         ++effectnum;
615                                                                 else if(vlen(oldself.origin - start) == vlen(e.origin - start) && num_for_edict(oldself) < num_for_edict(e))
616                                                                         ++effectnum;
617                                                         }
618                                                         fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
619                                                 }
620                                         }
621                                         fputs(f, "}\n");
622                                 }
623                                 fclose(f);
624                                 DID_CHEAT();
625                                 break;
626                         }
627                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
628                         break;
629                 case "drag_clear":
630                         IS_CHEAT(0, argc, 0);
631                         for(e = world; (e = find(e, classname, "dragbox_box")); )
632                                 remove(e);
633                         for(e = world; (e = find(e, classname, "dragbox_corner_1")); )
634                                 remove(e);
635                         for(e = world; (e = find(e, classname, "dragbox_corner_2")); )
636                                 remove(e);
637                         for(e = world; (e = find(e, classname, "dragpoint")); )
638                                 remove(e);
639                         for(e = world; (e = find(e, classname, "drag_digit")); )
640                                 remove(e);
641                         DID_CHEAT();
642                         break;
643                 case "god":
644                         IS_CHEAT(0, argc, 0);
645                         BITXOR_ASSIGN(self.flags, FL_GODMODE);
646                         if(self.flags & FL_GODMODE)
647                         {
648                                 sprint(self, "godmode ON\n");
649                                 DID_CHEAT();
650                         }
651                         else
652                                 sprint(self, "godmode OFF\n");
653                         break;
654                 case "notarget":
655                         IS_CHEAT(0, argc, 0);
656                         BITXOR_ASSIGN(self.flags, FL_NOTARGET);
657                         if(self.flags & FL_NOTARGET)
658                         {
659                                 sprint(self, "notarget ON\n");
660                                 DID_CHEAT();
661                         }
662                         else
663                                 sprint(self, "notarget OFF\n");
664                         break;
665                 case "noclip":
666                         IS_CHEAT(0, argc, 0);
667                         if(self.movetype != MOVETYPE_NOCLIP)
668                         {
669                                 self.movetype = MOVETYPE_NOCLIP;
670                                 sprint(self, "noclip ON\n");
671                                 DID_CHEAT();
672                         }
673                         else
674                         {
675                                 self.movetype = MOVETYPE_WALK;
676                                 sprint(self, "noclip OFF\n");
677                         }
678                         break;
679                 case "fly":
680                         IS_CHEAT(0, argc, 0);
681                         if(self.movetype != MOVETYPE_FLY)
682                         {
683                                 self.movetype = MOVETYPE_FLY;
684                                 sprint(self, "flymode ON\n");
685                                 DID_CHEAT();
686                         }
687                         else
688                         {
689                                 self.movetype = MOVETYPE_WALK;
690                                 sprint(self, "flymode OFF\n");
691                         }
692                         break;
693                 case "give":
694                         IS_CHEAT(0, argc, 0);
695                         if(GiveItems(self, 1, argc))
696                                 DID_CHEAT();
697                         break;
698                 case "usetarget":
699                         IS_CHEAT(0, argc, 0);
700                         e = self;
701                         self = spawn();
702                         self.target = argv(1);
703                         activator = e;
704                         SUB_UseTargets();
705                         remove(self);
706                         self = e;
707                         DID_CHEAT();
708                         break;
709                 case "killtarget":
710                         IS_CHEAT(0, argc, 0);
711                         e = self;
712                         self = spawn();
713                         self.killtarget = argv(1);
714                         activator = e;
715                         SUB_UseTargets();
716                         remove(self);
717                         self = e;
718                         DID_CHEAT();
719                         break;
720                 case "teleporttotarget":
721                         IS_CHEAT(0, argc, 0);
722                         e = self;
723                         self = spawn();
724                         setorigin(self, self.origin);
725                         self.classname = "cheattriggerteleport";
726                         self.target = argv(1);
727                         teleport_findtarget();
728                         if(!wasfreed(self))
729                         {
730                                 Simple_TeleportPlayer(self, e);
731                                 remove(self);
732                                 self = e;
733                                 DID_CHEAT();
734                         }
735                         else
736                                 self = e;
737                         break;
738         }
739
740         END_CHEAT_FUNCTION();
741 }
742
743 float Drag(float force_allow_pick, float ischeat);
744 void Drag_Begin(entity dragger, entity draggee, vector touchpoint);
745 void Drag_Finish(entity dragger);
746 float Drag_IsDraggable(entity draggee);
747 float Drag_MayChangeAngles(entity draggee);
748 void Drag_MoveForward(entity dragger);
749 void Drag_SetSpeed(entity dragger, float s);
750 void Drag_MoveBackward(entity dragger);
751 void Drag_Update(entity dragger);
752 float Drag_CanDrag(entity dragger);
753 float Drag_IsDragging(entity dragger);
754 void Drag_MoveDrag(entity from, entity to);
755 .entity dragentity;
756
757 float CheatFrame()
758 {
759         BEGIN_CHEAT_FUNCTION();
760
761         // Dragging can be used as either a cheat, or a function for some objects. If sv_cheats is active,
762         // the cheat dragging is used (unlimited pickup range and any entity can be carried). If sv_cheats
763         // is disabled, normal dragging is used (limited pickup range and only dragable objects can be carried),
764         // grabbing itself no longer being accounted as cheating.
765
766         switch(0)
767         {
768                 default:
769                         if(self.maycheat || (gamestart_sv_cheats && autocvar_sv_cheats))
770                         {
771                                 // use cheat dragging if cheats are enabled
772                                 crosshair_trace_plusvisibletriggers(self);
773                                 Drag(true, true);
774                         }
775                         else
776                         {
777                                 Drag(false, false); // execute dragging
778                         }
779                         break;
780         }
781
782         END_CHEAT_FUNCTION();
783 }
784
785
786
787
788
789 // ENTITY DRAGGING
790
791 // on dragger:
792 .float draggravity;
793 .float dragspeed; // speed of mouse wheel action
794 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
795 .vector draglocalvector; // local attachment vector of the dragentity
796 .float draglocalangle;
797 // on draggee:
798 .entity draggedby;
799 .float dragmovetype;
800
801 float Drag(float force_allow_pick, float ischeat)
802 {
803         BEGIN_CHEAT_FUNCTION();
804
805         // returns true when an entity has been picked up
806         // If pick is true, the object can also be picked up if it's not being held already
807         // If pick is false, only keep dragging the object if it's already being held
808
809         switch(0)
810         {
811                 default:
812                         if(Drag_IsDragging(self))
813                         {
814                                 if(self.BUTTON_DRAG)
815                                 {
816                                         if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
817                                         {
818                                                 Drag_MoveForward(self);
819                                                 self.impulse = 0;
820                                         }
821                                         else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19)
822                                         {
823                                                 Drag_MoveBackward(self);
824                                                 self.impulse = 0;
825                                         }
826                                         else if(self.impulse >= 1 && self.impulse <= 9)
827                                         {
828                                                 Drag_SetSpeed(self, self.impulse - 1);
829                                         }
830                                         else if(self.impulse == 14)
831                                         {
832                                                 Drag_SetSpeed(self, 9);
833                                         }
834
835                                         if(frametime)
836                                                 Drag_Update(self);
837                                 }
838                                 else
839                                 {
840                                         Drag_Finish(self);
841                                 }
842                         }
843                         else
844                         {
845                                 if(Drag_CanDrag(self))
846                                         if(self.BUTTON_DRAG)
847                                         {
848                                                 crosshair_trace_plusvisibletriggers(self);
849                                                 entity e = trace_ent;
850                                                 float pick = force_allow_pick;
851                                                 if (e && !pick)
852                                                 {
853                                                         // pick is true if the object can be picked up. While an object is being carried, the Drag() function
854                                                         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
855                                                         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
856                                                         // it goes out of range while slinging it around.
857
858                                                         if(vlen(self.origin - e.origin) <= autocvar_g_grab_range)
859                                                         {
860                                                                 switch(e.grab)
861                                                                 {
862                                                                         case 0: // can't grab
863                                                                                 break;
864                                                                         case 1: // owner can grab
865                                                                                 if(e.owner == self || e.realowner == self)
866                                                                                         pick = true;
867                                                                                 break;
868                                                                         case 2: // owner and team mates can grab
869                                                                                 if(SAME_TEAM(e.owner, self) || SAME_TEAM(e.realowner, self) || e.team == self.team)
870                                                                                         pick = true;
871                                                                                 break;
872                                                                         case 3: // anyone can grab
873                                                                                 pick = true;
874                                                                                 break;
875                                                                         default:
876                                                                                 break;
877                                                                 }
878                                                         }
879                                                 }
880                                                 // Find e and pick
881                                                 if(e && pick)
882                                                         if(Drag_IsDraggable(e))
883                                                         {
884                                                                 if(ischeat)
885                                                                         IS_CHEAT(0, 0, CHRAME_DRAG);
886                                                                 if(e.draggedby)
887                                                                         Drag_Finish(e.draggedby);
888                                                                 if(e.tag_entity)
889                                                                         detach_sameorigin(e);
890                                                                 Drag_Begin(self, e, trace_endpos);
891                                                                 if(ischeat)
892                                                                         DID_CHEAT();
893                                                                 return true;
894                                                         }
895                                         }
896                         }
897                         break;
898         }
899         return false;
900 }
901
902 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
903 {
904         float tagscale;
905
906         draggee.dragmovetype = draggee.movetype;
907         draggee.draggravity = draggee.gravity;
908         draggee.movetype = MOVETYPE_WALK;
909         draggee.gravity = 0.00001;
910         draggee.flags &= ~FL_ONGROUND;
911         draggee.draggedby = dragger;
912
913         dragger.dragentity = draggee;
914
915         dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
916         dragger.draglocalangle = draggee.angles.y - dragger.v_angle.y;
917         touchpoint = touchpoint - gettaginfo(draggee, 0);
918         tagscale = pow(vlen(v_forward), -2);
919         dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
920         dragger.draglocalvector_y = touchpoint * v_right * tagscale;
921         dragger.draglocalvector_z = touchpoint * v_up * tagscale;
922
923         dragger.dragspeed = 64;
924 }
925
926 void Drag_Finish(entity dragger)
927 {
928         entity draggee;
929         draggee = dragger.dragentity;
930         if(dragger)
931                 dragger.dragentity = world;
932         draggee.draggedby = world;
933         draggee.movetype = draggee.dragmovetype;
934         draggee.gravity = draggee.draggravity;
935
936         switch(draggee.movetype)
937         {
938                 case MOVETYPE_TOSS:
939                 case MOVETYPE_WALK:
940                 case MOVETYPE_STEP:
941                 case MOVETYPE_FLYMISSILE:
942                 case MOVETYPE_BOUNCE:
943                 case MOVETYPE_BOUNCEMISSILE:
944                 case MOVETYPE_PHYSICS:
945                         break;
946                 default:
947                         draggee.velocity = '0 0 0';
948                         break;
949         }
950
951         if((draggee.flags & FL_ITEM) && (vlen(draggee.velocity) < 32))
952         {
953                 draggee.velocity = '0 0 0';
954                 draggee.flags |= FL_ONGROUND; // floating items are FUN
955         }
956 }
957
958 float Drag_IsDraggable(entity draggee)
959 {
960         // TODO add more checks for bad stuff here
961         if(draggee == world)
962                 return false;
963         if(draggee.classname == "func_bobbing")
964                 return false;
965         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)
966                 return false;
967         if(draggee.classname == "plat")
968                 return false;
969         if(draggee.classname == "func_button")
970                 return false;
971 //      if(draggee.model == "")
972 //              return false;
973         if(IS_SPEC(draggee))
974                 return false;
975         if(IS_OBSERVER(draggee))
976                 return false;
977         if(draggee.classname == "exteriorweaponentity")
978                 return false;
979         if(draggee.classname == "weaponentity")
980                 return false;
981
982         return true;
983 }
984
985 float Drag_MayChangeAngles(entity draggee)
986 {
987         // TODO add more checks for bad stuff here
988         if(substring(draggee.model, 0, 1) == "*")
989                 return false;
990         return true;
991 }
992
993 void Drag_MoveForward(entity dragger)
994 {
995         dragger.dragdistance += dragger.dragspeed;
996 }
997
998 void Drag_SetSpeed(entity dragger, float s)
999 {
1000         dragger.dragspeed = pow(2, s);
1001 }
1002
1003 void Drag_MoveBackward(entity dragger)
1004 {
1005         dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
1006 }
1007
1008 void Drag_Update(entity dragger)
1009 {
1010         vector curorigin, neworigin, goodvelocity;
1011         float f;
1012         entity draggee;
1013
1014         draggee = dragger.dragentity;
1015         draggee.flags &= ~FL_ONGROUND;
1016
1017         curorigin = gettaginfo(draggee, 0);
1018         curorigin = curorigin + v_forward * dragger.draglocalvector.x + v_right * dragger.draglocalvector.y + v_up * dragger.draglocalvector.z;
1019         makevectors(dragger.v_angle);
1020         neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
1021         goodvelocity = (neworigin - curorigin) * (1 / frametime);
1022
1023         while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle > 180)
1024                 dragger.draglocalangle += 360;
1025         while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle <= -180)
1026                 dragger.draglocalangle -= 360;
1027
1028         f = min(frametime * 10, 1);
1029         draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
1030
1031         if(Drag_MayChangeAngles(draggee))
1032                 draggee.angles_y = draggee.angles.y * (1 - f) + (dragger.v_angle.y + dragger.draglocalangle) * f;
1033
1034         draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
1035
1036         vector vecs = '0 0 0';
1037         if(dragger.weaponentity.movedir_x > 0)
1038                 vecs = dragger.weaponentity.movedir;
1039
1040         vector dv = v_right * -vecs_y + v_up * vecs_z;
1041
1042         te_lightning1(draggee, dragger.origin + dragger.view_ofs + dv, curorigin);
1043 }
1044
1045 float Drag_CanDrag(entity dragger)
1046 {
1047         return (dragger.deadflag == DEAD_NO) || (IS_PLAYER(dragger));
1048 }
1049
1050 float Drag_IsDragging(entity dragger)
1051 {
1052         if(!dragger.dragentity)
1053                 return false;
1054         if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
1055         {
1056                 dragger.dragentity = world;
1057                 return false;
1058         }
1059         if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity))
1060         {
1061                 Drag_Finish(dragger);
1062                 return false;
1063         }
1064         return true;
1065 }
1066
1067 void Drag_MoveDrag(entity from, entity to)
1068 {
1069         if(from.draggedby)
1070         {
1071                 to.draggedby = from.draggedby;
1072                 to.draggedby.dragentity = to;
1073                 from.draggedby = world;
1074         }
1075 }
1076
1077 void DragBox_Think()
1078 {
1079         if(self.aiment && self.enemy)
1080         {
1081                 self.origin_x = (self.aiment.origin.x + self.enemy.origin.x) * 0.5;
1082                 self.origin_y = (self.aiment.origin.y + self.enemy.origin.y) * 0.5;
1083                 self.origin_z = (self.aiment.origin.z + self.enemy.origin.z) * 0.5;
1084                 self.maxs_x = fabs(self.aiment.origin.x - self.enemy.origin.x) * 0.5;
1085                 self.maxs_y = fabs(self.aiment.origin.y - self.enemy.origin.y) * 0.5;
1086                 self.maxs_z = fabs(self.aiment.origin.z - self.enemy.origin.z) * 0.5;
1087                 self.mins = -1 * self.maxs;
1088                 setorigin(self, self.origin); setsize(self, self.mins, self.maxs); // link edict
1089         }
1090
1091         if(self.cnt == -1) // actually race_place -1
1092         {
1093                 // show "10 10" for qualifying spawns
1094                 setmodel(self.killindicator, "models/sprites/10.spr32");
1095                 setmodel(self.killindicator.killindicator, "models/sprites/10.spr32");
1096         }
1097         else if(self.cnt == -2) // actually race_place 0
1098         {
1099                 // show "10 0" for loser spawns
1100                 setmodel(self.killindicator, "models/sprites/10.spr32");
1101                 setmodel(self.killindicator.killindicator, "models/sprites/0.spr32");
1102         }
1103         else
1104         {
1105                 setmodel(self.killindicator, strcat("models/sprites/", ftos(self.cnt % 10), ".spr32"));
1106                 setmodel(self.killindicator.killindicator, strcat("models/sprites/", ftos(floor(self.cnt / 10)), ".spr32"));
1107         }
1108
1109         self.nextthink = time;
1110 }
1111
1112 #endif