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