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