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