]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
add reverse ctf
[divverent/nexuiz.git] / data / qcsrc / server / ctf.qc
1 .entity sprite;
2 entity ctf_worldflaglist; // CTF flags in the map
3 .entity ctf_worldflagnext;
4 .float dropperid;
5 .float ctf_droptime;
6
7 .float next_take_time;                  // the next time a player can pick up a flag (time + blah)
8                                                                 /// I used this, in part, to fix the looping score bug. - avirox
9 //float FLAGSCORE_PICKUP        =  1;
10 //float FLAGSCORE_RETURN        =  5; // returned by owner team
11 //float FLAGSCORE_RETURNROGUE   = 10; // returned by rogue team
12 //float FLAGSCORE_CAPTURE       =  5;
13
14 #define FLAG_CARRY_POS '-15 0 7'
15
16 float ctf_score_value(string parameter)
17 {
18         if(g_ctf_win_mode != 2)
19                 return cvar(strcat("g_ctf_personal", parameter));
20         else
21                 return cvar(strcat("g_ctf_flag", parameter));
22 }
23
24 void FakeTimeLimit(entity e, float t)
25 {
26         msg_entity = e;
27         WriteByte(MSG_ONE, 3); // svc_updatestat
28         WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
29         if(t < 0)
30                 WriteCoord(MSG_ONE, cvar("timelimit"));
31         else
32                 WriteCoord(MSG_ONE, (t + 1) / 60);
33 }
34
35 float   flagcaptimerecord;
36 .float  flagpickuptime;
37 //.float  iscommander;
38 //.float  ctf_state;
39
40 void() FlagThink;
41 void() FlagTouch;
42
43 void place_flag()
44 {
45         if(!self.t_width)
46                 self.t_width = 0.1; // frame animation rate
47         if(!self.t_length)
48                 self.t_length = 119; // maximum frame
49
50         setattachment(self, world, "");
51         self.mdl = self.model;
52         self.flags = FL_ITEM;
53         self.solid = SOLID_TRIGGER;
54         self.movetype = MOVETYPE_NONE;
55         self.velocity = '0 0 0';
56         self.origin_z = self.origin_z + 6;
57         self.think = FlagThink;
58         self.touch = FlagTouch;
59         self.nextthink = time + 0.1;
60         self.cnt = FLAG_BASE;
61         self.mangle = self.angles;
62         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
63         //self.effects = self.effects | EF_DIMLIGHT;
64         if(self.noalign)
65         {
66                 self.dropped_origin = self.origin;
67         }
68         else
69         {
70                 droptofloor();
71                 self.movetype = MOVETYPE_TOSS;
72         }
73 };
74
75 void LogCTF(string mode, float flagteam, entity actor)
76 {
77         string s;
78         if(!cvar("sv_eventlog"))
79                 return;
80         s = strcat(":ctf:", mode);
81         s = strcat(s, ":", ftos(flagteam));
82         if(actor != world)
83                 s = strcat(s, ":", ftos(actor.playerid));
84         GameLogEcho(s);
85 }
86
87 void RegenFlag(entity e)
88 {
89         setattachment(e, world, "");
90         e.damageforcescale = 0;
91         e.movetype = MOVETYPE_NONE;
92         if(!self.noalign)
93                 e.movetype = MOVETYPE_TOSS;
94         e.solid = SOLID_TRIGGER;
95         // TODO: play a sound here
96         setorigin(e, e.dropped_origin);
97         e.angles = e.mangle;
98         e.cnt = FLAG_BASE;
99         e.owner = world;
100         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
101 };
102
103 void ReturnFlag(entity e)
104 {
105         if (e.owner)
106         if (e.owner.flagcarried == e)
107         {
108                 WaypointSprite_DetachCarrier(e.owner);
109                 e.owner.flagcarried = world;
110
111                 if(e.speedrunning)
112                         FakeTimeLimit(e.owner, -1);
113         }
114         e.owner = world;
115         RegenFlag(e);
116 };
117
118 void DropFlag(entity e, entity penalty_receiver, entity attacker)
119 {
120         local entity p;
121
122         if(e.speedrunning)
123         {
124                 ReturnFlag(e);
125                 return;
126         }
127
128         if (!e.owner)
129         {
130                 dprint("FLAG: drop - no owner?!?!\n");
131                 return;
132         }
133         p = e.owner;
134         if (p.flagcarried != e)
135         {
136                 dprint("FLAG: drop - owner is not carrying this flag??\n");
137                 return;
138         }
139         bprint(p.netname, "^7 lost the ", e.netname, "\n");
140
141         if(penalty_receiver)
142                 UpdateFrags(penalty_receiver, -ctf_score_value("penalty_suicidedrop"));
143         else
144                 UpdateFrags(p, -ctf_score_value("penalty_drop"));
145         PlayerScore_Add(p, SP_CTF_DROPS, +1);
146         e.playerid = attacker.playerid;
147         e.ctf_droptime = time;
148         
149         WaypointSprite_Ping(p.waypointsprite_attachedforcarrier);
150         WaypointSprite_DetachCarrier(p);
151         LogCTF("dropped", p.team, p);
152
153         setattachment(e, world, "");
154         e.damageforcescale = cvar("g_balance_ctf_damageforcescale");
155
156         if (p.flagcarried == e)
157                 p.flagcarried = world;
158         e.owner = world;
159
160         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
161         e.solid = SOLID_TRIGGER;
162         e.movetype = MOVETYPE_TOSS;
163         // setsize(e, '-16 -16 0', '16 16 74');
164         setorigin(e, p.origin - '0 0 24' + '0 0 37');
165         e.cnt = FLAG_DROPPED;
166         e.velocity = '0 0 300';
167         e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
168
169         trace_startsolid = FALSE;
170         tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
171         if(trace_startsolid)
172                 dprint("FLAG FALLTHROUGH will happen SOON\n");
173 };
174
175 void AnimateFlag()
176 {
177         if(self.delay > time)
178                 return;
179         self.delay = time + self.t_width;
180         if(self.nextthink > self.delay)
181                 self.nextthink = self.delay;
182
183         self.frame = self.frame + 1;
184         if(self.frame > self.t_length)
185                 self.frame = 0;
186 }
187
188 void FlagThink()
189 {
190         local entity e;
191
192         self.nextthink = time + 0.1;
193
194         AnimateFlag();
195
196         if(self.speedrunning)
197         if(self.cnt == FLAG_CARRY)
198         {
199                 if(self.owner)
200                 if(flagcaptimerecord)
201                 if(time >= self.flagpickuptime + flagcaptimerecord)
202                 {
203                         bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
204
205                         self.owner.impulse = 77; // returning!
206                         e = self;
207                         self = self.owner;
208                         ReturnFlag(e);
209                         ImpulseCommands();
210                         self = e;
211                         return;
212                 }
213         }
214
215         if (self.cnt == FLAG_BASE)
216                 return;
217
218         if (self.cnt == FLAG_DROPPED)
219         {
220                 // flag fallthrough? FIXME remove this if bug is really fixed now
221                 if(self.origin_z < -131072)
222                 {
223                         dprint("FLAG FALLTHROUGH just happened\n");
224                         self.pain_finished = 0;
225                 }
226                 setattachment(self, world, "");
227                 if (time > self.pain_finished)
228                 {
229                         bprint("The ", self.netname, " has returned to base\n");
230                         sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
231                         LogCTF("returned", self.team, world);
232                         ReturnFlag(self);
233                 }
234                 return;
235         }
236
237         e = self.owner;
238         if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
239         {
240                 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
241                 DropFlag(self, world, world);
242                 return;
243         }
244
245         if(cvar("g_ctf_allow_drop"))
246         if(e.BUTTON_USE)
247                 DropFlag(self, e, world);
248 };
249
250 void FlagTouch()
251 {
252         if(gameover) return;
253
254         local float t;
255         local entity player;
256         local string s, s0, h0, h1;
257         if (other.classname != "player")
258                 return;
259         if (other.health < 1) // ignore dead players
260                 return;
261
262         if (self.cnt == FLAG_CARRY)
263                 return;
264
265         if (self.cnt == FLAG_BASE)
266         if (other.team == self.team)
267         if (other.flagcarried) // he's got a flag
268         if (other.flagcarried.team != self.team) // capture
269         {
270                 if (other.flagcarried == world)
271                 {
272                         return;
273                 }
274                 t = time - other.flagcarried.flagpickuptime;
275                 s = ftos_decimals(t, 2);
276                 s0 = ftos_decimals(flagcaptimerecord, 2);
277                 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
278                 h1 = other.netname;
279                 if(h0 == h1)
280                         h0 = "his";
281                 else
282                         h0 = strcat(h0, "^7's"); // h0: display text for previous netname
283                 if (flagcaptimerecord == 0)
284                 {
285                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
286                         flagcaptimerecord = t;
287                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
288                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
289                         GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
290                 }
291                 else if (t < flagcaptimerecord)
292                 {
293                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
294                         flagcaptimerecord = t;
295                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
296                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
297                         GameLogEcho(strcat(":recordset:", ftos(other.playerid), ":", ftos(t)));
298                 }
299                 else
300                 {
301                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
302                 }
303
304                 PlayerTeamScore_Add(other, SP_CTF_CAPS, ST_CTF_CAPS, 1);
305                 LogCTF("capture", other.flagcarried.team, other);
306                 // give credit to the individual player
307                 UpdateFrags(other, ctf_score_value("score_capture"));
308
309                 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
310                 WaypointSprite_DetachCarrier(other);
311                 if(self.speedrunning)
312                         FakeTimeLimit(other, -1);
313                 RegenFlag (other.flagcarried);
314                 other.flagcarried = world;
315                 other.next_take_time = time + 1;
316         }
317         if (self.cnt == FLAG_BASE)
318         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
319         if (other.team != self.team)
320         if (!other.flagcarried)
321         {
322                 if (other.next_take_time > time)
323                         return;
324                 // pick up
325                 self.flagpickuptime = time; // used for timing runs
326                 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
327                 if(other.speedrunning)
328                 if(flagcaptimerecord)
329                         FakeTimeLimit(other, time + flagcaptimerecord);
330                 self.solid = SOLID_NOT;
331                 setorigin(self, self.origin); // relink
332                 self.owner = other;
333                 other.flagcarried = self;
334                 self.cnt = FLAG_CARRY;
335                 self.angles = '0 0 0';
336                 bprint(other.netname, "^7 got the ", self.netname, "\n");
337                 UpdateFrags(other, ctf_score_value("score_pickup_base"));
338                 self.dropperid = other.playerid;
339                 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
340                 LogCTF("steal", self.team, other);
341                 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
342
343                 FOR_EACH_PLAYER(player)
344                         if(player.team == self.team)
345                                 centerprint(player, "The enemy got your flag! Retrieve it!");
346
347                 self.movetype = MOVETYPE_NONE;
348                 setorigin(self, FLAG_CARRY_POS);
349                 setattachment(self, other, "");
350                 WaypointSprite_AttachCarrier("flagcarrier", other);
351                 WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 1 0');
352                 WaypointSprite_Ping(self.sprite);
353
354                 return;
355         }
356
357         if (self.cnt == FLAG_DROPPED)
358         {
359                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
360                 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
361                 {
362                         // return flag
363                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
364
365                         // punish the player who last had it
366                         FOR_EACH_PLAYER(player)
367                                 if(player.playerid == self.dropperid)
368                                         PlayerScore_Add(player, SP_SCORE, -ctf_score_value("penalty_returned"));
369
370                         // punish the team who was last carrying it
371                         if(self.team == COLOR_TEAM1)
372                                 TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, -ctf_score_value("penalty_returned"));
373                         else
374                                 TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, -ctf_score_value("penalty_returned"));
375
376                         // reward the player who returned it
377                         if(other.playerid == self.playerid) // is this the guy who killed the FC last?
378                         {
379                                 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
380                                         UpdateFrags(other, ctf_score_value("score_return_by_killer"));
381                                 else
382                                         UpdateFrags(other, ctf_score_value("score_return_rogue_by_killer"));
383                         }
384                         else
385                         {
386                                 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
387                                         UpdateFrags(other, ctf_score_value("score_return"));
388                                 else
389                                         UpdateFrags(other, ctf_score_value("score_return_rogue"));
390                         }
391                         PlayerScore_Add(other, SP_CTF_RETURNS, 1);
392                         LogCTF("return", self.team, other);
393                         sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
394                         ReturnFlag(self);
395                 }
396                 else if (!other.flagcarried && (other.playerid != self.dropperid || time > self.ctf_droptime + cvar("g_balance_ctf_delay_collect")))
397                 {
398                         // pick up
399                         self.solid = SOLID_NOT;
400                         setorigin(self, self.origin); // relink
401                         self.owner = other;
402                         other.flagcarried = self;
403                         self.cnt = FLAG_CARRY;
404                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
405
406                         float f;
407                         f = bound(0, (self.pain_finished - time) / cvar("g_ctf_flag_returntime"), 1);
408                         //print("factor is ", ftos(f), "\n");
409                         f = ctf_score_value("score_pickup_dropped_late") * (1-f)
410                           + ctf_score_value("score_pickup_dropped_early") * f;
411                         f = floor(f + 0.5);
412                         self.dropperid = other.playerid;
413                         //print("score is ", ftos(f), "\n");
414
415                         UpdateFrags(other, f);
416                         PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
417                         LogCTF("pickup", self.team, other);
418                         sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
419
420                         FOR_EACH_PLAYER(player)
421                                 if(player.team == self.team)
422                                         centerprint(player, "The enemy got your flag! Retrieve it!");
423
424                         self.movetype = MOVETYPE_NONE;  // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
425                         setorigin(self, FLAG_CARRY_POS);
426                         setattachment(self, other, "");
427                         self.damageforcescale = 0;
428                         WaypointSprite_AttachCarrier("flagcarrier", other);
429                         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 1 0');
430                 }
431         }
432 };
433
434 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
435 CTF Starting point for a player
436 in team one (Red).
437
438 Keys:
439 "angle"
440  viewing angle when spawning
441 */
442 void spawnfunc_info_player_team1()
443 {
444         self.team = COLOR_TEAM1; // red
445         spawnfunc_info_player_deathmatch();
446 };
447 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
448
449 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
450 CTF Starting point for a player in
451 team two (Blue).
452
453 Keys:
454 "angle"
455  viewing angle when spawning
456 */
457 void spawnfunc_info_player_team2()
458 {
459         self.team = COLOR_TEAM2; // blue
460         spawnfunc_info_player_deathmatch();
461 };
462 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
463
464 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
465 CTF Starting point for a player in
466 team three (Magenta).
467
468 Keys:
469 "angle"
470  viewing angle when spawning
471 */
472 void spawnfunc_info_player_team3()
473 {
474         self.team = COLOR_TEAM3; // purple
475         spawnfunc_info_player_deathmatch();
476 };
477
478
479 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
480 CTF Starting point for a player in
481 team four (Yellow).
482
483 Keys:
484 "angle"
485  viewing angle when spawning
486 */
487 void spawnfunc_info_player_team4()
488 {
489         self.team = COLOR_TEAM4; // yellow
490         spawnfunc_info_player_deathmatch();
491 };
492
493
494
495
496 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
497 CTF flag for team one (Red).
498 Multiple are allowed.
499
500 Keys:
501 "angle"
502  Angle the flag will point
503 (minus 90 degrees)
504 "model"
505  model to use, note this needs red and blue as skins 0 and 1
506  (default models/ctf/flag.md3)
507 "noise"
508  sound played when flag is picked up
509  (default ctf/take.wav)
510 "noise1"
511  sound played when flag is returned by a teammate
512  (default ctf/return.wav)
513 "noise2"
514  sound played when flag is captured
515  (default ctf/redcapture.wav)
516 "noise3"
517  sound played when flag is lost in the field and respawns itself
518  (default ctf/respawn.wav)
519 */
520
521 void spawnfunc_item_flag_team1()
522 {
523         if (!g_ctf)
524         {
525                 remove(self);
526                 return;
527         }
528
529         //if(!cvar("teamplay"))
530         //      cvar_set("teamplay", "3");
531
532         // link flag into ctf_worldflaglist
533         self.ctf_worldflagnext = ctf_worldflaglist;
534         ctf_worldflaglist = self;
535
536         self.classname = "item_flag_team";
537         if(g_ctf_reverse)
538         {
539                 self.team = COLOR_TEAM2; // color 13 team (blue)
540                 self.items = IT_KEY1; // silver key (bluish enough)
541         }
542         else
543         {
544                 self.team = COLOR_TEAM1; // color 4 team (red)
545                 self.items = IT_KEY2; // gold key (redish enough)
546         }
547         self.netname = "^1RED^7 flag";
548         self.target = "###item###";
549         self.skin = 0;
550         if(self.spawnflags & 1)
551                 self.noalign = 1;
552         if (!self.model)
553                 self.model = "models/ctf/flag_red.md3";
554         if (!self.noise)
555                 self.noise = "ctf/take.wav";
556         if (!self.noise1)
557                 self.noise1 = "ctf/return.wav";
558         if (!self.noise2)
559                 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
560         if (!self.noise3)
561                 self.noise3 = "ctf/respawn.wav";
562         precache_model (self.model);
563         setmodel (self, self.model); // precision set below
564         precache_sound (self.noise);
565         precache_sound (self.noise1);
566         precache_sound (self.noise2);
567         precache_sound (self.noise3);
568         //setsize(self, '-16 -16 -37', '16 16 37');
569         setsize(self, PL_MIN + '0 0 -13', PL_MAX + '0 0 -13');
570         setorigin(self, self.origin + '0 0 37');
571         self.nextthink = time + 0.2; // start after doors etc
572         self.think = place_flag;
573
574         if(!self.scale)
575                 self.scale = 0.6;
576         //if(!self.glow_size)
577         //      self.glow_size = 50;
578
579         self.effects = self.effects | EF_LOWPRECISION;
580         if(cvar("g_ctf_fullbrightflags"))
581                 self.effects |= EF_FULLBRIGHT;
582
583         waypoint_spawnforitem(self);
584
585         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
586         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, '1 0 0');
587 };
588
589 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
590 CTF flag for team two (Blue).
591 Multiple are allowed.
592
593 Keys:
594 "angle"
595  Angle the flag will point
596 (minus 90 degrees)
597 "model"
598  model to use, note this needs red and blue as skins 0 and 1
599  (default models/ctf/flag.md3)
600 "noise"
601  sound played when flag is picked up
602  (default ctf/take.wav)
603 "noise1"
604  sound played when flag is returned by a teammate
605  (default ctf/return.wav)
606 "noise2"
607  sound played when flag is captured
608  (default ctf/bluecapture.wav)
609 "noise3"
610  sound played when flag is lost in the field and respawns itself
611  (default ctf/respawn.wav)
612 */
613
614 void spawnfunc_item_flag_team2()
615 {
616         if (!g_ctf)
617         {
618                 remove(self);
619                 return;
620         }
621         //if(!cvar("teamplay"))
622         //      cvar_set("teamplay", "3");
623
624         // link flag into ctf_worldflaglist
625         self.ctf_worldflagnext = ctf_worldflaglist;
626         ctf_worldflaglist = self;
627
628         self.classname = "item_flag_team";
629         if(g_ctf_reverse)
630         {
631                 self.team = COLOR_TEAM1; // color 4 team (red)
632                 self.items = IT_KEY2; // gold key (redish enough)
633         }
634         else
635         {
636                 self.team = COLOR_TEAM2; // color 13 team (blue)
637                 self.items = IT_KEY1; // silver key (bluish enough)
638         }
639         self.netname = "^4BLUE^7 flag";
640         self.target = "###item###";
641         self.skin = 0;
642         if(self.spawnflags & 1)
643                 self.noalign = 1;
644         if (!self.model)
645                 self.model = "models/ctf/flag_blue.md3";
646         if (!self.noise)
647                 self.noise = "ctf/take.wav";
648         if (!self.noise1)
649                 self.noise1 = "ctf/return.wav";
650         if (!self.noise2)
651                 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
652         if (!self.noise3)
653                 self.noise3 = "ctf/respawn.wav";
654         precache_model (self.model);
655         setmodel (self, self.model); // precision set below
656         precache_sound (self.noise);
657         precache_sound (self.noise1);
658         precache_sound (self.noise2);
659         precache_sound (self.noise3);
660         //setsize(self, '-16 -16 -37', '16 16 37');
661         setsize(self, PL_MIN + '0 0 -13', PL_MAX + '0 0 -13');
662         setorigin(self, self.origin + '0 0 37');
663         self.nextthink = time + 0.2; // start after doors etc
664         self.think = place_flag;
665
666         if(!self.scale)
667                 self.scale = 0.6;
668         //if(!self.glow_size)
669         //      self.glow_size = 50;
670
671         self.effects = self.effects | EF_LOWPRECISION;
672         if(cvar("g_ctf_fullbrightflags"))
673                 self.effects |= EF_FULLBRIGHT;
674
675         waypoint_spawnforitem(self);
676
677         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
678         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, '0 0 1');
679 };
680
681
682 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
683 Team declaration for CTF gameplay, this allows you to decide what team
684 names and control point models are used in your map.
685
686 Note: If you use spawnfunc_ctf_team entities you must define at least 2!  However, unlike
687 domination, you don't need to make a blank one too.
688
689 Keys:
690 "netname"
691  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
692 "cnt"
693  Scoreboard color of the team (for example 4 is red and 13 is blue)
694
695 */
696
697 void spawnfunc_ctf_team()
698 {
699         if (!g_ctf)
700         {
701                 remove(self);
702                 return;
703         }
704         self.classname = "ctf_team";
705         self.team = self.cnt + 1;
706 };
707
708 // code from here on is just to support maps that don't have control point and team entities
709 void ctf_spawnteam (string teamname, float teamcolor)
710 {
711         local entity oldself;
712         oldself = self;
713         self = spawn();
714         self.classname = "ctf_team";
715         self.netname = teamname;
716         self.cnt = teamcolor;
717
718         spawnfunc_ctf_team();
719
720         self = oldself;
721 };
722
723 // spawn some default teams if the map is not set up for ctf
724 void ctf_spawnteams()
725 {
726         float numteams;
727
728         numteams = 2;//cvar("g_ctf_default_teams");
729
730         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
731         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
732 };
733
734 void ctf_delayedinit()
735 {
736         // if no teams are found, spawn defaults
737         if (find(world, classname, "ctf_team") == world)
738                 ctf_spawnteams();
739
740         ScoreRules_ctf();
741 };
742
743 void ctf_init()
744 {
745         InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
746         flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
747 };
748
749 void ctf_setstatus2(entity flag, float shift)
750 {
751         if (flag.cnt == FLAG_CARRY)
752                 if (flag.owner == self)
753                         self.items |= shift * 3;
754                 else
755                         self.items |= shift * 1;
756         else if (flag.cnt == FLAG_DROPPED)
757                 self.items |= shift * 2;
758         else
759         {
760                 // no status bits
761         }
762 };
763
764 void ctf_setstatus()
765 {
766         self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
767         self.items = self.items - (self.items & IT_RED_FLAG_LOST);
768         self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
769         self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
770
771         if (g_ctf) {
772                 local entity flag;
773                 float redflags, blueflags;
774
775                 redflags = 0;
776                 blueflags = 0;
777
778                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
779                 {
780                         if(flag.team == COLOR_TEAM1)
781                                 ++redflags;
782                         else if(flag.team == COLOR_TEAM2)
783                                 ++blueflags;
784                 }
785
786                 // blinking magic: if there is more than one flag, show one of these in a clever way
787                 if(redflags)
788                         redflags = mod(floor(time * redflags * 0.75), redflags);
789                 if(blueflags)
790                         blueflags = mod(floor(time * blueflags * 0.75), blueflags);
791
792                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
793                 {
794                         if(flag.team == COLOR_TEAM1)
795                         {
796                                 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
797                                         ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
798                         }
799                         else if(flag.team == COLOR_TEAM2)
800                         {
801                                 if(--blueflags == -1) // happens exactly once
802                                         ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
803                         }
804                 }
805         }
806 };
807 /*
808 entity(float cteam) ctf_team_has_commander =
809 {
810         entity pl;
811         if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
812                 return world;
813         
814         FOR_EACH_REALPLAYER(pl) {
815                 if(pl.team == cteam && pl.iscommander) {
816                         return pl;
817                 }
818         }
819         return world;
820 };
821
822 void(entity e, float st) ctf_setstate =
823 {
824         e.ctf_state = st;
825         ++e.version;
826 };
827
828 void(float cteam) ctf_new_commander =
829 {
830         entity pl, plmax;
831         
832         plmax = world;
833         FOR_EACH_REALPLAYER(pl) {
834                 if(pl.team == cteam) {
835                         if(pl.iscommander) { // don't reassign if alreay there
836                                 return;
837                         }
838                         if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
839                                 plmax = pl;
840                 }
841         }
842         if(plmax == world) {
843                 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
844                 return;
845         }
846
847         plmax.iscommander = TRUE;
848         ctf_setstate(plmax, 3);
849         sprint(plmax, "^3You're the commander now!\n");
850         centerprint(plmax, "^3You're the commander now!\n");
851 };
852
853 void() ctf_clientconnect =
854 {
855         self.iscommander = FALSE;
856         
857         if(!self.team || self.classname != "player") {
858                 ctf_setstate(self, -1);
859         } else
860                 ctf_setstate(self, 0);
861
862         self.team_saved = self.team;
863         
864         if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
865                 ctf_new_commander(self.team);
866         }
867 };
868
869 void() ctf_playerchanged =
870 {
871         if(!self.team || self.classname != "player") {
872                 ctf_setstate(self, -1);
873         } else if(self.ctf_state < 0 && self.classname == "player") {
874                 ctf_setstate(self, 0);
875         }
876
877         if(self.iscommander &&
878            (self.classname != "player" || self.team != self.team_saved)
879                 )
880         {
881                 self.iscommander = FALSE;
882                 if(self.classname == "player")
883                         ctf_setstate(self, 0);
884                 else
885                         ctf_setstate(self, -1);
886                 ctf_new_commander(self.team_saved);
887         }
888         
889         self.team_saved = self.team;
890         
891         ctf_new_commander(self.team);
892 };
893
894 void() ctf_clientdisconnect =
895 {
896         if(self.iscommander)
897         {
898                 ctf_new_commander(self.team);
899         }
900 };
901
902 entity GetPlayer(string);
903 float() ctf_clientcommand =
904 {
905         entity e;
906         if(argv(0) == "order") {
907                 if(!g_ctf) {
908                         sprint(self, "This command is not supported in this gamemode.\n");
909                         return TRUE;
910                 }
911                 if(!self.iscommander) {
912                         sprint(self, "^1You are not the commander!\n");
913                         return TRUE;
914                 }
915                 if(argv(2) == "") {
916                         sprint(self, "Usage: order #player status   - (playernumber as in status)\n");
917                         return TRUE;
918                 }
919                 e = GetPlayer(argv(1));
920                 if(e == world) {
921                         sprint(self, "Invalid player.\nUsage: order #player status   - (playernumber as in status)\n");
922                         return TRUE;
923                 }
924                 if(e.team != self.team) {
925                         sprint(self, "^3You can only give orders to your own team!\n");
926                         return TRUE;
927                 }
928                 if(argv(2) == "attack") {
929                         sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
930                         sprint(e, "^1Attack!\n");
931                         centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
932                         ctf_setstate(e, 1);
933                 } else if(argv(2) == "defend") {
934                         sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
935                         sprint(e, "^Defend!\n");
936                         centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
937                         ctf_setstate(e, 2);
938                 } else {
939                         sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");
940                 }
941                 return TRUE;
942         }
943         return FALSE;
944 };
945 */