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