fix a crash MrBougo found by preventing clientcommand spam
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 entity nagger;
2 float readycount;
3 float Nagger_SendEntity(entity to, float sendflags)
4 {
5         float nags, i, f, b;
6         entity e;
7         WriteByte(MSG_ENTITY, ENT_CLIENT_NAGGER);
8
9         nags = 0;
10         if(readycount)
11         {
12                 nags |= 1;
13                 if(to.ready == 0)
14                         nags |= 2;
15         }
16         if(votecalled)
17         {
18                 nags |= 4;
19                 if(to.vote_vote == 0)
20                         nags |= 8;
21         }
22         if(inWarmupStage)
23                 nags |= 16;
24
25         if(sendflags & 128)
26                 nags |= 128;
27
28         WriteByte(MSG_ENTITY, nags);
29
30         if(nags & 128)
31         {
32                 if(votecalled)
33                         WriteString(MSG_ENTITY, votecalledvote_display);
34                 else
35                         WriteString(MSG_ENTITY, "");
36         }
37
38         if(nags & 1)
39         {
40                 for(i = 1; i <= maxclients; i += 8)
41                 {
42                         for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
43                                 if(clienttype(e) != CLIENTTYPE_REAL || e.ready)
44                                         f |= b;
45                         WriteByte(MSG_ENTITY, f);
46                 }
47         }
48
49         return TRUE;
50 }
51 void Nagger_Init()
52 {
53         Net_LinkEntity(nagger = spawn(), FALSE, 0, Nagger_SendEntity);
54 }
55 void Nagger_VoteChanged()
56 {
57         if(nagger)
58                 nagger.SendFlags |= 128;
59 }
60 void Nagger_VoteCountChanged()
61 {
62         if(nagger)
63                 nagger.SendFlags |= 1;
64 }
65 void Nagger_ReadyCounted()
66 {
67         if(nagger)
68                 nagger.SendFlags |= 1;
69 }
70
71 void ReadyCount();
72 string MapVote_Suggest(string m);
73
74 entity GetPlayer(string name)
75 {
76         float num;
77         entity e;
78         string ns;
79
80         if(substring(name, 0, 1) == "#") {
81                 num = stof(substring(name, 1, 999));
82                 if(num >= 1 && num <= maxclients) {
83                         for((e = world); num > 0; --num, (e = nextent(e)))
84                                 ;
85                         //if(clienttype(e) == CLIENTTYPE_REAL)
86                         if(e.classname == "player")
87                                 return e;
88                 }
89         } else {
90                 ns = strdecolorize(name);
91                 FOR_EACH_REALPLAYER(e) {
92                         if(!strcasecmp(strdecolorize(e.netname), ns)) {
93                                 return e;
94                         }
95                 }
96         }
97         return world;
98 }
99
100 float drag_lastcnt;
101 void DragBox_Think()
102 {
103         if(self.aiment && self.enemy)
104         {
105                 self.origin_x = (self.aiment.origin_x + self.enemy.origin_x) * 0.5;
106                 self.origin_y = (self.aiment.origin_y + self.enemy.origin_y) * 0.5;
107                 self.origin_z = (self.aiment.origin_z + self.enemy.origin_z) * 0.5;
108                 self.maxs_x = fabs(self.aiment.origin_x - self.enemy.origin_x) * 0.5;
109                 self.maxs_y = fabs(self.aiment.origin_y - self.enemy.origin_y) * 0.5;
110                 self.maxs_z = fabs(self.aiment.origin_z - self.enemy.origin_z) * 0.5;
111                 self.mins = -1 * self.maxs;
112                 setorigin(self, self.origin); setsize(self, self.mins, self.maxs); // link edict
113         }
114
115         if(self.cnt == -1) // actually race_place -1
116         {
117                 // show "10 10" for qualifying spawns
118                 setmodel(self.killindicator, "models/sprites/10.spr32");
119                 setmodel(self.killindicator.killindicator, "models/sprites/10.spr32");
120         }
121         else if(self.cnt == -2) // actually race_place 0
122         {
123                 // show "10 0" for loser spawns
124                 setmodel(self.killindicator, "models/sprites/10.spr32");
125                 setmodel(self.killindicator.killindicator, "models/sprites/0.spr32");
126         }
127         else
128         {
129                 setmodel(self.killindicator, strcat("models/sprites/", ftos(mod(self.cnt, 10)), ".spr32"));
130                 setmodel(self.killindicator.killindicator, strcat("models/sprites/", ftos(floor(self.cnt / 10)), ".spr32"));
131         }
132
133         self.nextthink = time;
134 }
135
136 //float ctf_clientcommand();
137 float readyrestart_happened;
138 .float lms_spectate_warning;
139 void spawnfunc_func_breakable();
140
141 .float cmd_floodtime;
142 .float cmd_floodcount;
143 float cmd_floodcheck()
144 {
145         if (timeoutStatus != 2)
146         {
147                 if(time == self.cmd_floodtime)
148                 {
149                         self.cmd_floodcount += 1;
150                         if(self.cmd_floodcount > 8)
151                                 return TRUE;
152                 }
153                 else
154                 {
155                         self.cmd_floodtime = time;
156                         self.cmd_floodcount = 1;
157                 }
158         }
159         return FALSE;
160 }
161
162 void SV_ParseClientCommand(string s) {
163         string cmd;
164         float tokens, f, effectnum;
165         vector start, end;
166         entity e, oldself;
167
168         tokens = tokenize_console(s);
169
170         cmd = argv(0);
171         if(cmd != "reportcvar")
172         if(cmd != "sentcvar")
173         if(cmd != "pause")
174         if(cmd != "prespawn")
175         if(cmd != "spawn")
176         if(cmd != "begin")
177         {
178                 if(cmd_floodcheck())
179                         return;
180         }
181
182         if(GameCommand_Vote(s, self)) {
183                 return;
184         } else if(GameCommand_MapVote(argv(0))) {
185                 return;
186         } else if(cmd == "autoswitch") {
187                 // be backwards compatible with older clients (enabled)
188                 self.autoswitch = ("0" != argv(1));
189                 local string autoswitchmsg;
190                 if (self.autoswitch) {
191                         autoswitchmsg = "on";
192                 } else {
193                         autoswitchmsg = "off";
194                 }
195                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
196         } else if(cmd == "clientversion") {
197                 if not(self.flags & FL_CLIENT)
198                         return;
199                 if (argv(1) == "$gameversion") {
200                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
201                         // either that or someone wants to be funny
202                         self.version = 1;
203                 } else {
204                         self.version = stof(argv(1));
205                 }
206                 if(self.version != cvar("gameversion"))
207                 {
208                         self.classname = "observer";
209                         self.version_mismatch = 1;
210                         PutClientInServer();
211                 } else if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force")) {
212                         //JoinBestTeam(self, FALSE, TRUE);
213                 } else if(teams_matter && !cvar("sv_spectate")) {
214                         self.classname = "observer";
215                         stuffcmd(self,"menu_showteamselect\n");
216                 }
217         } else if(cmd == "reportcvar") { // old system
218                 if(substring(argv(2), 0, 1) == "$") // undefined cvar: use the default value on the server then
219                 {
220                         s = strcat(substring(s, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
221                         tokens = tokenize_console(s);
222                 }
223                 GetCvars(1);
224         } else if(cmd == "sentcvar") { // new system
225                 if(tokens == 2) // undefined cvar: use the default value on the server then
226                 {
227                         s = strcat(substring(s, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
228                         tokens = tokenize_console(s);
229                 }
230                 GetCvars(1);
231         } else if(cmd == "spectate") {
232                 if(cmd_floodcheck())
233                         return;
234                 if not(self.flags & FL_CLIENT)
235                         return;
236                 if(g_arena)
237                         return;
238                 if(g_lms)
239                 {
240                         if(self.lms_spectate_warning)
241                         {
242                                 // mark player as spectator
243                                 PlayerScore_Add(self, SP_LMS_RANK, 666 - PlayerScore_Add(self, SP_LMS_RANK, 0));
244                         }
245                         else
246                         {
247                                 self.lms_spectate_warning = 1;
248                                 sprint(self, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
249                                 return;
250                         }
251                 }
252                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
253                         if(self.flagcarried)
254                                 DropFlag(self.flagcarried, world, world);
255                         if(self.ballcarried)
256                                 DropBall(self.ballcarried, self.origin, self.velocity);
257                         kh_Key_DropAll(self, TRUE);
258                         WaypointSprite_PlayerDead();
259                         self.classname = "observer";
260                         if(blockSpectators)
261                                 sprint(self, strcat("^7You have to become a player within the next ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds, otherwise you will be kicked, because spectators aren't allowed at this time!\n"));
262                         PutClientInServer();
263                 }
264         } else if(cmd == "join") {
265                 if not(self.flags & FL_CLIENT)
266                         return;
267                 if(!g_arena)
268                 if (self.classname != "player" && !lockteams)
269                 {
270                         if(isJoinAllowed()) {
271                                 self.classname = "player";
272                                 PlayerScore_Clear(self);
273                                 bprint ("^4", self.netname, "^4 is playing now\n");
274                                 PutClientInServer();
275                                 if(cvar("g_campaign"))
276                                         campaign_bots_may_start = 1;
277                         }
278                         else {
279                                 //player may not join because of g_maxplayers is set
280                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
281                         }
282                 }
283         } else if( cmd == "selectteam" ) {
284                 if not(self.flags & FL_CLIENT)
285                         return;
286                 if( !teams_matter ) {
287                         sprint( self, "selecteam can only be used in teamgames\n");
288                 } else if(cvar("g_campaign")) {
289                         //JoinBestTeam(self, 0);
290                 } else if(lockteams) {
291                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
292                 } else if( argv(1) == "red" ) {
293                         DoTeamChange(COLOR_TEAM1);
294                 } else if( argv(1) == "blue" ) {
295                         DoTeamChange(COLOR_TEAM2);
296                 } else if( argv(1) == "yellow" ) {
297                         DoTeamChange(COLOR_TEAM3);
298                 } else if( argv(1) == "pink" ) {
299                         DoTeamChange(COLOR_TEAM4);
300                 } else if( argv(1) == "auto" ) {
301                         DoTeamChange(-1);
302                 } else {
303                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
304                 }
305         } else if(cmd == "ready") {
306                 if not(self.flags & FL_CLIENT)
307                         return;
308
309                 if((inWarmupStage && 0 >= g_warmup_limit) // with unlimited warmup players have to be able to restart
310                    || cvar("sv_ready_restart") || g_race_qualifying == 2)
311                 {
312                         if(!readyrestart_happened || cvar("sv_ready_restart_repeatable"))
313                         {
314                                 if (self.ready) // toggle
315                                 {
316                                         self.ready = FALSE;
317                                         bprint(self.netname, "^2 is ^1NOT^2 ready\n");
318                                 }
319                                 else
320                                 {
321                                         self.ready = TRUE;
322                                         bprint(self.netname, "^2 is ready\n");
323                                 }
324
325                                 // cannot reset the game while a timeout is active!
326                                 if(!timeoutStatus)
327                                         ReadyCount();
328                         } else {
329                                 sprint(self, "^1Game has already been restarted\n");
330                         }
331                 }
332         } else if(cmd == "maplist") {
333                 sprint(self, maplist_reply);
334         } else if(cmd == "lsmaps") {
335                 sprint(self, lsmaps_reply);
336         } else if(cmd == "records") {
337                 sprint(self, records_reply);
338         } else if(cmd == "voice") {
339                 if(tokens >= 3)
340                         VoiceMessage(argv(1), substring(s, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
341                 else
342                         VoiceMessage(argv(1), "");
343         } else if(cmd == "say") {
344                 if(tokens >= 2)
345                         Say(self, FALSE, world, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
346                 //clientcommand(self, formatmessage(s));
347         } else if(cmd == "say_team") {
348                 if(tokens >= 2)
349                         Say(self, TRUE, world, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
350                 //clientcommand(self, formatmessage(s));
351         } else if(cmd == "tell") {
352                 e = GetCommandPlayerSlotTargetFromTokenizedCommand(tokens, 1);
353                 if(e && tokens > ParseCommandPlayerSlotTarget_firsttoken)
354                 {
355                         Say(self, FALSE, e, substring(s, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken)), TRUE);
356                 }
357                 else
358                 {
359                         if(tokens > ParseCommandPlayerSlotTarget_firsttoken)
360                                 trigger_magicear_processmessage_forallears(self, -1, world, substring(s, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken)));
361                         sprint(self, "ERROR: usage: tell # playerid text...\n");
362                 }
363                 //clientcommand(self, formatmessage(s));
364         } else if(cmd == "info") {
365                 cmd = cvar_string(strcat("sv_info_", argv(1)));
366                 if(cmd == "")
367                         sprint(self, "ERROR: unsupported info command\n");
368                 else
369                         wordwrap_sprint(cmd, 1111);
370         } else if(cmd == "suggestmap") {
371                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
372         } else if(cmd == "timeout") {
373                 if not(self.flags & FL_CLIENT)
374                         return;
375                 if(cvar("sv_timeout")) {
376                         if(self.classname == "player") {
377                                 if(votecalled)
378                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
379                                 else
380                                         evaluateTimeout();
381                         }
382                         else
383                                 sprint(self, "^7Error: only players can call a timeout!\n");
384                 }
385         } else if(cmd == "timein") {
386                 if not(self.flags & FL_CLIENT)
387                         return;
388                 if(cvar("sv_timeout")) {
389                         evaluateTimein();
390                 }
391         } else if(cmd == "teamstatus") {
392                 Score_NicePrint(self);
393         } else if(cmd == "cvar_changes") {
394                 sprint(self, cvar_changes);
395         } else if(cmd == "pointparticles") {
396                 if((sv_cheats || self.maycheat) && tokens == 5)
397                 {
398                         // arguments:
399                         //   effectname
400                         //   origin (0..1, on crosshair line)
401                         //   velocity
402                         //   howmany
403                         effectnum = particleeffectnum(argv(1));
404                         f = stof(argv(2));
405                         start = (1-f) * self.origin + f * self.cursor_trace_endpos;
406                         end = stov(argv(3));
407                         f = stof(argv(4));
408                         pointparticles(effectnum, start, end, f);
409                 }
410                 else
411                         sprint(self, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
412         } else if(cmd == "trailparticles") {
413                 if((sv_cheats || self.maycheat) && tokens == 2)
414                 {
415                         // arguments:
416                         //   effectname
417                         effectnum = particleeffectnum(argv(1));
418                         W_SetupShot(self, FALSE, FALSE, "",0);
419                         traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
420                         trailparticles(self, effectnum, w_shotorg, trace_endpos);
421                 }
422                 else
423                         sprint(self, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
424         } else if(cmd == "make") {
425                 if((sv_cheats || self.maycheat) && tokens == 3)
426                 {
427                         // arguments:
428                         //   modelname mode
429                         f = stof(argv(2));
430                         W_SetupShot(self, FALSE, FALSE, "", 0);
431                         traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, self);
432                         if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
433                         {
434                                 sprint(self, "cannot make stuff there (bad surface)\n");
435                         }
436                         else
437                         {
438                                 oldself = self;
439                                 self = spawn();
440                                 self.model = strzone(argv(1));
441                                 self.mdl = "rocket_explode";
442                                 self.health = 1000;
443                                 setorigin(self, trace_endpos);
444                                 self.effects = EF_NOMODELFLAGS;
445                                 if(f == 1)
446                                 {
447                                         self.angles = fixedvectoangles2(trace_plane_normal, v_forward);
448                                         self.angles = AnglesTransform_Multiply(self.angles, '-90 0 0'); // so unrotated models work
449                                 }
450                                 spawnfunc_func_breakable();
451                                 // now, is it valid?
452                                 if(f == 0)
453                                 {
454                                         tracebox(self.origin, self.mins, self.maxs, self.origin, MOVE_NORMAL, self);
455                                         if(trace_startsolid)
456                                         {
457                                                 sprint(oldself, "cannot make stuff there (no space)\n");
458                                                 remove(self);
459                                         }
460                                 }
461                                 self = oldself;
462                         }
463                 }
464                 else
465                         sprint(self, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
466         } else if(cmd == "penalty") {
467                 if((sv_cheats || self.maycheat) && tokens == 3)
468                         race_ImposePenaltyTime(self, stof(argv(1)), argv(2));
469                 else
470                         sprint(self, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
471         } else if(cmd == "dragbox_spawn") {
472                 if(sv_cheats || self.maycheat)
473                 {
474                         e = spawn();
475                         e.classname = "dragbox_box";
476                         e.think = DragBox_Think;
477                         e.nextthink = time;
478                         e.solid = -1; // black
479                         setmodel(e, "null"); // network it
480                         if(tokens == 4)
481                                 e.cnt = stof(argv(1));
482                         else
483                                 e.cnt = max(0, drag_lastcnt);
484
485                         e.aiment = spawn();
486                         e.aiment.classname = "dragbox_corner_1";
487                         e.aiment.owner = e;
488                         setmodel(e.aiment, "models/marker.md3");
489                         e.aiment.skin = 0;
490                         setsize(e.aiment, '0 0 0', '0 0 0');
491                         if(tokens == 4)
492                                 setorigin(e.aiment, stov(argv(2)));
493                         else
494                                 setorigin(e.aiment, self.cursor_trace_endpos);
495
496                         e.enemy = spawn();
497                         e.enemy.classname = "dragbox_corner_2";
498                         e.enemy.owner = e;
499                         setmodel(e.enemy, "models/marker.md3");
500                         e.enemy.skin = 1;
501                         setsize(e.enemy, '0 0 0', '0 0 0');
502                         end = normalize(self.cursor_trace_start - e.aiment.origin);
503                         end_x = (end_x > 0) * 2 - 1;
504                         end_y = (end_y > 0) * 2 - 1;
505                         end_z = (end_z > 0) * 2 - 1;
506                         if(tokens == 4)
507                                 setorigin(e.enemy, stov(argv(3)));
508                         else
509                                 setorigin(e.enemy, e.aiment.origin + 32 * end);
510
511                         e.killindicator = spawn();
512                         e.killindicator.classname = "drag_digit";
513                         e.killindicator.owner = e;
514                         setattachment(e.killindicator, e, "");
515                         setorigin(e.killindicator, '0 0 -8');
516                         e.killindicator.killindicator = spawn();
517                         e.killindicator.killindicator.classname = "drag_digit";
518                         e.killindicator.killindicator.owner = e;
519                         setattachment(e.killindicator.killindicator, e, "");
520                         setorigin(e.killindicator.killindicator, '0 0 8');
521                 }
522                 else
523                         sprint(self, "Usage: sv_cheats 1; r_showbboxes 1.5; restart; cmd dragbox_spawn\n");
524         } else if(cmd == "dragpoint_spawn") {
525                 if(sv_cheats || self.maycheat)
526                 {
527                         e = spawn();
528                         e.classname = "dragpoint";
529                         e.think = DragBox_Think;
530                         e.nextthink = time;
531                         e.solid = 0; // nothing special
532                         setmodel(e, "models/marker.md3");
533                         setsize(e, PL_MIN, PL_MAX);
534                         e.skin = 2;
535                         if(tokens == 3)
536                                 e.cnt = stof(argv(1));
537                         else
538                                 e.cnt = drag_lastcnt;
539                         if(tokens == 3)
540                                 setorigin(e, stov(argv(2)));
541                         else
542                         {
543                                 setorigin(e, self.cursor_trace_endpos + normalize(self.cursor_trace_start - self.cursor_trace_endpos));
544                                 move_out_of_solid(e);
545                         }
546
547                         e.killindicator = spawn();
548                         e.killindicator.classname = "drag_digit";
549                         e.killindicator.owner = e;
550                         setattachment(e.killindicator, e, "");
551                         setorigin(e.killindicator, '0 0 40');
552                         e.killindicator.killindicator = spawn();
553                         e.killindicator.killindicator.classname = "drag_digit";
554                         e.killindicator.killindicator.owner = e;
555                         setattachment(e.killindicator.killindicator, e, "");
556                         setorigin(e.killindicator.killindicator, '0 0 56');
557                 }
558                 else
559                         sprint(self, "Usage: sv_cheats 1; r_showbboxes 1.5; restart; cmd dragbox_spawn\n");
560         } else if(cmd == "drag_remove") {
561                 if(sv_cheats || self.maycheat)
562                 {
563                         RandomSelection_Init();
564                         for(e = world; (e = find(e, classname, "dragbox_box")); )
565                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
566                         for(e = world; (e = find(e, classname, "dragpoint")); )
567                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
568                         if(RandomSelection_chosen_ent)
569                         {
570                                 remove(RandomSelection_chosen_ent.killindicator.killindicator);
571                                 remove(RandomSelection_chosen_ent.killindicator);
572                                 if(RandomSelection_chosen_ent.aiment)
573                                         remove(RandomSelection_chosen_ent.aiment);
574                                 if(RandomSelection_chosen_ent.enemy)
575                                         remove(RandomSelection_chosen_ent.enemy);
576                                 remove(RandomSelection_chosen_ent);
577                         }
578                 }
579                 else
580                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_remove\n");
581         } else if(cmd == "drag_setcnt") {
582                 if((sv_cheats || self.maycheat) && tokens >= 2)
583                 {
584                         RandomSelection_Init();
585                         for(e = world; (e = find(e, classname, "dragbox_box")); )
586                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
587                         for(e = world; (e = find(e, classname, "dragpoint")); )
588                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
589                         if(RandomSelection_chosen_ent)
590                         {
591                                 if(substring(argv(1), 0, 1) == "*")
592                                         RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
593                                 else
594                                         RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
595                         }
596                 }
597                 else
598                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
599         } else if(cmd == "drag_save") {
600                 if((sv_cheats || self.maycheat) && tokens >= 2)
601                 {
602                         f = fopen(argv(1), FILE_WRITE);
603                         fputs(f, "cmd drag_clear\n");
604                         for(e = world; (e = find(e, classname, "dragbox_box")); )
605                         {
606                                 fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
607                         }
608                         for(e = world; (e = find(e, classname, "dragpoint")); )
609                         {
610                                 fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
611                         }
612                         fclose(f);
613                 }
614                 else
615                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
616         } else if(cmd == "drag_saveraceent") {
617                 if((sv_cheats || self.maycheat) && tokens >= 2)
618                 {
619                         f = fopen(argv(1), FILE_WRITE);
620                         for(e = world; (e = find(e, classname, "dragbox_box")); )
621                         {
622                                 fputs(f, "{\n");
623                                 fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
624                                 fputs(f, strcat("\"origin\" \"", ftos(e.absmin_x), " ", ftos(e.absmin_y), " ", ftos(e.absmin_z), "\"\n"));
625                                 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"));
626                                 fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
627                                 fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
628                                 fputs(f, "}\n");
629                         }
630                         for(e = world; (e = find(e, classname, "dragpoint")); )
631                         {
632                                 start = '0 0 0';
633                                 effectnum = 0;
634                                 for(oldself = world; (oldself = find(oldself, classname, "dragbox_box")); )
635                                 {
636                                         if(e.cnt <= 0 && oldself.cnt == 0 || e.cnt == oldself.cnt)
637                                         {
638                                                 start = start + oldself.origin;
639                                                 ++effectnum;
640                                         }
641                                 }
642                                 start *= 1 / effectnum;
643                                 fputs(f, "{\n");
644                                 fputs(f, "\"classname\" \"info_player_race\"\n");
645                                 fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
646                                 fputs(f, strcat("\"origin\" \"", ftos(e.origin_x), " ", ftos(e.origin_y), " ", ftos(e.origin_z), "\"\n"));
647                                 if(e.cnt == -2)
648                                 {
649                                         fputs(f, "\"target\" \"checkpoint0\"\n");
650                                         fputs(f, "\"race_place\" \"0\"\n");
651                                 }
652                                 else if(e.cnt == -1)
653                                 {
654                                         fputs(f, "\"target\" \"checkpoint0\"\n");
655                                         fputs(f, "\"race_place\" \"-1\"\n");
656                                 }
657                                 else
658                                 {
659                                         fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
660                                         if(e.cnt == 0)
661                                         {
662                                                 // these need race_place
663                                                 // counting...
664                                                 effectnum = 1;
665                                                 for(oldself = world; (oldself = find(oldself, classname, "dragpoint")); )
666                                                 if(oldself.cnt == 0)
667                                                 {
668                                                         if(vlen(oldself.origin - start) < vlen(e.origin - start))
669                                                                 ++effectnum;
670                                                         else if(vlen(oldself.origin - start) == vlen(e.origin - start) && num_for_edict(oldself) < num_for_edict(e))
671                                                                 ++effectnum;
672                                                 }
673                                                 fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
674                                         }
675                                 }
676                                 fputs(f, "}\n");
677                         }
678                         fclose(f);
679                 }
680                 else
681                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
682         } else if(cmd == "drag_clear") {
683                 if(sv_cheats || self.maycheat)
684                 {
685                         for(e = world; (e = find(e, classname, "dragbox_box")); )
686                                 remove(e);
687                         for(e = world; (e = find(e, classname, "dragbox_corner_1")); )
688                                 remove(e);
689                         for(e = world; (e = find(e, classname, "dragbox_corner_2")); )
690                                 remove(e);
691                         for(e = world; (e = find(e, classname, "dragpoint")); )
692                                 remove(e);
693                         for(e = world; (e = find(e, classname, "drag_digit")); )
694                                 remove(e);
695                 }
696                 else
697                         sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_clear\n");
698         } else {
699                 //if(ctf_clientcommand())
700                 //      return;
701                 // grep for Cmd_AddCommand_WithClientCommand to find them all
702                 if(cmd != "status")
703                 if(cmd != "max")
704                 if(cmd != "monster")
705                 if(cmd != "scrag")
706                 if(cmd != "wraith")
707                 if(cmd != "gimme")
708                 if(cmd != "god")
709                 if(cmd != "notarget")
710                 if(cmd != "fly")
711                 if(cmd != "noclip")
712                 if(cmd != "give")
713                 //if(cmd != "say") // handled above
714                 //if(cmd != "say_team") // handled above
715                 if(cmd != "kill")
716                 if(cmd != "pause")
717                 if(cmd != "ping")
718                 if(cmd != "name")
719                 if(cmd != "color")
720                 if(cmd != "rate")
721                 if(cmd != "pmodel")
722                 if(cmd != "playermodel")
723                 if(cmd != "playerskin")
724                 if(cmd != "prespawn")
725                 if(cmd != "spawn")
726                 if(cmd != "begin")
727                 if(cmd != "pings")
728                 if(cmd != "sv_startdownload")
729                 if(cmd != "download")
730                 {
731                         print("WARNING: Invalid clientcommand by ", self.netname, ": ", s, "\n");
732                         return;
733                 }
734
735                 if(self.jointime > 0 && time > self.jointime + 10 && time > self.nickspamtime) // allow any changes in the first 10 seconds since joining
736                 if(cmd == "name" || cmd == "playermodel") // TODO also playerskin and color?
737                 {
738                         if(self.nickspamtime == 0 || time > self.nickspamtime + cvar("g_nick_flood_timeout"))
739                                 // good, no serious flood
740                                 self.nickspamcount = 1;
741                         else
742                                 self.nickspamcount += 1;
743                         self.nickspamtime = time + cvar("g_nick_flood_penalty");
744
745                         if (timeoutStatus == 2) //when game is paused, no flood protection
746                                 self.nickspamcount = self.nickspamtime = 0;
747                 }
748
749                 clientcommand(self,s);
750         }
751 }
752
753 void ReadyRestartForce()
754 {
755         local entity e;
756
757         bprint("^1Server is restarting...\n");
758
759         VoteReset();
760
761         // clear overtime
762         if (checkrules_overtimesadded > 0 && g_race_qualifying != 2) {
763                 //we have to decrease timelimit to its original value again!!
764                 float newTL;
765                 newTL = cvar("timelimit");
766                 newTL -= checkrules_overtimesadded * cvar("timelimit_overtime");
767                 cvar_set("timelimit", ftos(newTL));
768         }
769
770         checkrules_suddendeathend = checkrules_overtimesadded = checkrules_suddendeathwarning = 0;
771
772
773         readyrestart_happened = 1;
774         game_starttime = time + RESTART_COUNTDOWN;
775         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
776
777         inWarmupStage = 0; //once the game is restarted the game is in match stage
778
779         //reset the .ready status of all players (also spectators)
780         FOR_EACH_CLIENTSLOT(e)
781                 e.ready = 0;
782         readycount = 0;
783         Nagger_ReadyCounted(); // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
784
785         if(cvar("teamplay_lockonrestart") && teams_matter) {
786                 lockteams = 1;
787                 bprint("^1The teams are now locked.\n");
788         }
789
790         //initiate the restart-countdown-announcer entity
791         if(cvar("sv_ready_restart_after_countdown"))
792         {
793                 restartTimer = spawn();
794                 restartTimer.think = restartTimer_Think;
795                 restartTimer.nextthink = game_starttime;
796         }
797
798         //after a restart every players number of allowed timeouts gets reset, too
799         if(cvar("sv_timeout"))
800         {
801                 FOR_EACH_REALPLAYER(e)
802                         e.allowedTimeouts = cvar("sv_timeout_number");
803         }
804
805         //reset map immediately if this cvar is not set
806         if (!cvar("sv_ready_restart_after_countdown"))
807                 reset_map(TRUE);
808
809         if(cvar("sv_eventlog"))
810                 GameLogEcho(":restart");
811 }
812
813 void ReadyRestart()
814 {
815         // no arena, assault support yet...
816         if(g_arena | g_assault | gameover | intermission_running | race_completing)
817                 localcmd("restart\n");
818         else
819                 localcmd("\nsv_hook_gamerestart;");
820
821         ReadyRestartForce();
822
823         // reset ALL scores, but only do that at the beginning
824         //of the countdown if sv_ready_restart_after_countdown is off!
825         //Otherwise scores could be manipulated during the countdown!
826         if (!cvar("sv_ready_restart_after_countdown"))
827                 Score_ClearAll();
828 }
829
830 /**
831  * Counts how many players are ready. If not enough players are ready, the function
832  * does nothing. If all players are ready, the timelimit will be extended and the
833  * restart_countdown variable is set to allow other functions like PlayerPostThink
834  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
835  * is not set the map will be resetted.
836  *
837  * Function is called after the server receives a 'ready' sign from a player.
838  */
839 void ReadyCount()
840 {
841         local entity e;
842         local float r, p;
843
844         r = p = 0;
845
846         FOR_EACH_REALPLAYER(e)
847         {
848                 p += 1;
849                 if(e.ready)
850                         r += 1;
851         }
852
853         readycount = r;
854
855         Nagger_ReadyCounted();
856
857         if(r) // at least one is ready
858         if(r == p) // and, everyone is ready
859                 ReadyRestart();
860 }
861
862 /**
863  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
864  * is set)
865  */
866 void restartTimer_Think() {
867         restart_mapalreadyrestarted = 1;
868         reset_map(TRUE);
869         Score_ClearAll();
870         remove(self);
871         return;
872 }
873
874 /**
875  * Checks whether the player who calls the timeout is allowed to do so.
876  * If so, it initializes the timeout countdown. It also checks whether another
877  * timeout was already running at this time and reacts correspondingly.
878  *
879  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
880  *                          timeoutInitiator, timeoutStatus, timeoutHandler
881  *
882  * This function is called when a player issues the calltimeout command.
883  */
884 void evaluateTimeout() {
885         if (inWarmupStage && !g_warmup_allow_timeout)
886                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
887         if (time < game_starttime )
888                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
889         if (timeoutStatus != 2) {
890                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
891                 if (cvar("timelimit")) {
892                         //a timelimit was used
893                         local float myTl;
894                         myTl = cvar("timelimit");
895
896                         local float lastPossibleTimeout;
897                         lastPossibleTimeout = (myTl*60) - cvar("sv_timeout_leadtime") - 1;
898
899                         if (lastPossibleTimeout < time - game_starttime)
900                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
901                 }
902         }
903         //player may not call a timeout if he has no calls left
904         if (self.allowedTimeouts < 1)
905                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
906         //now all required checks are passed
907         self.allowedTimeouts -= 1;
908         bprint(self.netname, " ^7called a timeout (", ftos(self.allowedTimeouts), " timeouts left)!\n"); //write a bprint who started the timeout (and how many he has left)
909         remainingTimeoutTime = cvar("sv_timeout_length");
910         remainingLeadTime = cvar("sv_timeout_leadtime");
911         timeoutInitiator = self;
912         if (timeoutStatus == 0) { //if another timeout was already active, don't change its status (which was 1 or 2) to 1, only change it to 1 if no timeout was active yet
913                 timeoutStatus = 1;
914                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
915                 timeoutHandler = spawn();
916                 timeoutHandler.think = timeoutHandler_Think;
917         }
918         timeoutHandler.nextthink = time; //always let the entity think asap
919
920         //inform all connected clients about the timeout call
921         play2all("announcer/robotic/timeoutcalled.wav");
922 }
923
924 /**
925  * Checks whether a player is allowed to resume the game. If he is allowed to do it,
926  * and the lead time for the timeout is still active, this countdown just will be aborted (the
927  * game will never be paused). Otherwise the remainingTimeoutTime will be set to the corresponding
928  * value of the cvar sv_timeout_resumetime.
929  *
930  * This function is called when a player issues the resumegame command.
931  */
932 void evaluateTimein() {
933         if (!timeoutStatus)
934                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
935         if (self != timeoutInitiator)
936                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
937         if (timeoutStatus == 1) {
938                 remainingTimeoutTime = timeoutStatus = 0;
939                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
940                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
941         }
942         else if (timeoutStatus == 2) {
943                 //only shorten the remainingTimeoutTime if it makes sense
944                 if( remainingTimeoutTime > (cvar("sv_timeout_resumetime") + 1) ) {
945                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
946                         remainingTimeoutTime = cvar("sv_timeout_resumetime");
947                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
948                 }
949                 else
950                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
951
952         }
953 }