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