]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
more team cleanups by TH
[divverent/nexuiz.git] / data / qcsrc / server / ctf.qc
1 #define FLAG_MIN (PL_MIN + '0 0 -13')
2 #define FLAG_MAX (PL_MAX + '0 0 -13')
3
4 .entity sprite;
5 entity ctf_worldflaglist; // CTF flags in the map
6 .entity ctf_worldflagnext;
7 .float dropperid;
8 .float ctf_droptime;
9
10 .float next_take_time;                  // the next time a player can pick up a flag (time + blah)
11                                                                 /// I used this, in part, to fix the looping score bug. - avirox
12 //float FLAGSCORE_PICKUP        =  1;
13 //float FLAGSCORE_RETURN        =  5; // returned by owner team
14 //float FLAGSCORE_RETURNROGUE   = 10; // returned by rogue team
15 //float FLAGSCORE_CAPTURE       =  5;
16
17 #define FLAG_CARRY_POS '-15 0 7'
18
19 .float ctf_captureshielded; // set to 1 if the player is too bad to be allowed to capture
20
21 float captureshield_min_negscore; // punish at -20 points
22 float captureshield_max_ratio; // punish at most 30% of each team
23 float captureshield_force; // push force of the shield
24
25 float ctf_captureshield_shielded(entity p)
26 {
27         float s, se;
28         entity e;
29         float players_worseeq, players_total;
30
31         if(captureshield_max_ratio <= 0)
32                 return FALSE;
33
34         s = PlayerScore_Add(p, SP_SCORE, 0);
35         if(s >= -captureshield_min_negscore)
36                 return FALSE;
37
38         players_total = players_worseeq = 0;
39         FOR_EACH_PLAYER(e)
40         {
41                 if(e.team != p.team)
42                         continue;
43                 se = PlayerScore_Add(e, SP_SCORE, 0);
44                 if(se <= s)
45                         ++players_worseeq;
46                 ++players_total;
47         }
48
49         // player is in the worse half, if >= half the players are better than him, or consequently, if < half of the players are worse
50         // use this rule here
51         
52         if(players_worseeq >= players_total * captureshield_max_ratio)
53                 return FALSE;
54
55         return TRUE;
56 }
57
58 void ctf_captureshield_update(entity p, float dir)
59 {
60         float should;
61         if(dir == p.ctf_captureshielded) // 0: shield only, 1: unshield only
62         {
63                 should = ctf_captureshield_shielded(p);
64                 if(should != dir)
65                 {
66                         if(should)
67                         {
68                                 centerprint_atprio(p, CENTERPRIO_SHIELDING, "^3You are now ^4shielded^3 from the flag\n^3for ^1too many unsuccessful attempts^3 to capture.\n\n^3Make some defensive scores before trying again.");
69                                 // TODO csqc notifier for this
70                         }
71                         else
72                         {
73                                 centerprint_atprio(p, CENTERPRIO_SHIELDING, "^3You are now free.\n\n^3Feel free to ^1try to capture^3 the flag again\n^3if you think you will succeed.");
74                                 // TODO csqc notifier for this
75                         }
76                         p.ctf_captureshielded = should;
77                 }
78         }
79 }
80
81 float ctf_captureshield_customize()
82 {
83         if not(other.ctf_captureshielded)
84                 return FALSE;
85         if(self.team == other.team)
86                 return FALSE;
87         return TRUE;
88 }
89
90 void ctf_captureshield_touch()
91 {
92         if not(other.ctf_captureshielded)
93                 return;
94         if(self.team == other.team)
95                 return;
96         vector mymid;
97         vector othermid;
98         mymid = (self.absmin + self.absmax) * 0.5;
99         othermid = (other.absmin + other.absmax) * 0.5;
100         Damage(other, self, self, 0, DEATH_HURTTRIGGER, mymid, normalize(othermid - mymid) * captureshield_force);
101         centerprint_atprio(other, CENTERPRIO_SHIELDING, "^3You are ^4shielded^3 from the flag\n^3for ^1too many unsuccessful attempts^3 to capture.\n\n^3Get some defensive scores before trying again.");
102 }
103
104 void ctf_captureshield_spawn()
105 {
106         entity e;
107         e = spawn();
108         e.enemy = self;
109         e.team = self.team;
110         e.touch = ctf_captureshield_touch;
111         e.customizeentityforclient = ctf_captureshield_customize;
112         e.classname = "ctf_captureshield";
113         e.effects = EF_ADDITIVE;
114         e.movetype = MOVETYPE_NOCLIP;
115         e.solid = SOLID_TRIGGER;
116         e.avelocity = '7 0 11';
117         setorigin(e, self.origin);
118         setmodel(e, "models/ctf/shield.md3");
119         e.scale = 0.5;
120         setsize(e, e.scale * e.mins, e.scale * e.maxs);
121 }
122
123 float ctf_score_value(string parameter)
124 {
125         if(g_ctf_win_mode != 2)
126                 return cvar(strcat("g_ctf_personal", parameter));
127         else
128                 return cvar(strcat("g_ctf_flag", parameter));
129 }
130
131 void FakeTimeLimit(entity e, float t)
132 {
133         msg_entity = e;
134         WriteByte(MSG_ONE, 3); // svc_updatestat
135         WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
136         if(t < 0)
137                 WriteCoord(MSG_ONE, cvar("timelimit"));
138         else
139                 WriteCoord(MSG_ONE, (t + 1) / 60);
140 }
141
142 float   flagcaptimerecord;
143 .float  flagpickuptime;
144 //.float  iscommander;
145 //.float  ctf_state;
146
147 void() FlagThink;
148 void() FlagTouch;
149
150 void place_flag()
151 {
152         if(self.classname != "item_flag_team")
153         {
154                 backtrace("PlaceFlag a non-flag");
155                 return;
156         }
157
158         if(!self.t_width)
159                 self.t_width = 0.1; // frame animation rate
160         if(!self.t_length)
161                 self.t_length = 58; // maximum frame
162
163         setattachment(self, world, "");
164         self.mdl = self.model;
165         self.flags = FL_ITEM;
166         self.solid = SOLID_TRIGGER;
167         self.movetype = MOVETYPE_NONE;
168         self.velocity = '0 0 0';
169         self.origin_z = self.origin_z + 6;
170         self.think = FlagThink;
171         self.touch = FlagTouch;
172         self.nextthink = time + 0.1;
173         self.cnt = FLAG_BASE;
174         self.mangle = self.angles;
175         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
176         //self.effects = self.effects | EF_DIMLIGHT;
177         if(self.noalign)
178         {
179                 self.dropped_origin = self.origin;
180         }
181         else
182         {
183                 droptofloor();
184                 self.movetype = MOVETYPE_TOSS;
185         }
186         InitializeEntity(self, ctf_captureshield_spawn, INITPRIO_SETLOCATION);
187 };
188
189 void LogCTF(string mode, float flagteam, entity actor)
190 {
191         string s;
192         if(!cvar("sv_eventlog"))
193                 return;
194         s = strcat(":ctf:", mode);
195         s = strcat(s, ":", ftos(flagteam));
196         if(actor != world)
197                 s = strcat(s, ":", ftos(actor.playerid));
198         GameLogEcho(s);
199 }
200
201 void RegenFlag(entity e)
202 {
203         if(e.classname != "item_flag_team")
204         {
205                 backtrace("RegenFlag a non-flag");
206                 return;
207         }
208
209         setattachment(e, world, "");
210         e.damageforcescale = 0;
211         e.movetype = MOVETYPE_NONE;
212         if(!e.noalign)
213                 e.movetype = MOVETYPE_TOSS;
214         e.velocity = '0 0 0';
215         e.solid = SOLID_TRIGGER;
216         // TODO: play a sound here
217         setorigin(e, e.dropped_origin);
218         e.angles = e.mangle;
219         e.cnt = FLAG_BASE;
220         e.owner = world;
221         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
222 };
223
224 void ReturnFlag(entity e)
225 {
226         if(e.classname != "item_flag_team")
227         {
228                 backtrace("ReturnFlag a non-flag");
229                 return;
230         }
231
232         if (e.owner)
233         if (e.owner.flagcarried == e)
234         {
235                 WaypointSprite_DetachCarrier(e.owner);
236                 e.owner.flagcarried = world;
237
238                 if(e.speedrunning)
239                         FakeTimeLimit(e.owner, -1);
240         }
241         e.owner = world;
242         RegenFlag(e);
243 };
244
245 void DropFlag(entity e, entity penalty_receiver, entity attacker)
246 {
247         local entity p;
248
249         if(e.classname != "item_flag_team")
250         {
251                 backtrace("DropFlag a non-flag");
252                 return;
253         }
254
255         if(e.speedrunning)
256         {
257                 ReturnFlag(e);
258                 return;
259         }
260
261         if (!e.owner)
262         {
263                 dprint("FLAG: drop - no owner?!?!\n");
264                 return;
265         }
266         p = e.owner;
267         if (p.flagcarried != e)
268         {
269                 dprint("FLAG: drop - owner is not carrying this flag??\n");
270                 return;
271         }
272         bprint(p.netname, "^7 lost the ", e.netname, "\n");
273
274         if(penalty_receiver)
275                 UpdateFrags(penalty_receiver, -ctf_score_value("penalty_suicidedrop"));
276         else
277                 UpdateFrags(p, -ctf_score_value("penalty_drop"));
278         PlayerScore_Add(p, SP_CTF_DROPS, +1);
279         ctf_captureshield_update(p, 0); // shield only
280         e.playerid = attacker.playerid;
281         e.ctf_droptime = time;
282         
283         if(p.waypointsprite_attachedforcarrier)
284         {
285                 WaypointSprite_Ping(p.waypointsprite_attachedforcarrier);
286                 WaypointSprite_DetachCarrier(p);
287         }
288         else
289         {
290                 bprint("\{1}^1Flag carrier had no flag sprite?!?\n");
291                 backtrace("Flag carrier had no flag sprite?!?");
292         }
293         LogCTF("dropped", p.team, p);
294         sound (self, CHAN_TRIGGER, self.noise4, VOL_BASE, ATTN_NONE);
295
296         setattachment(e, world, "");
297         e.damageforcescale = cvar("g_balance_ctf_damageforcescale");
298
299         if (p.flagcarried == e)
300                 p.flagcarried = world;
301         e.owner = world;
302
303         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
304         e.solid = SOLID_TRIGGER;
305         e.movetype = MOVETYPE_TOSS;
306         // setsize(e, '-16 -16 0', '16 16 74');
307         setorigin(e, p.origin - '0 0 24' + '0 0 37');
308         e.cnt = FLAG_DROPPED;
309         e.velocity = '0 0 300';
310         e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
311
312         trace_startsolid = FALSE;
313         tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
314         if(trace_startsolid)
315                 dprint("FLAG FALLTHROUGH will happen SOON\n");
316 };
317
318 void AnimateFlag()
319 {
320         if(self.delay > time)
321                 return;
322         self.delay = time + self.t_width;
323         if(self.nextthink > self.delay)
324                 self.nextthink = self.delay;
325
326         self.frame = self.frame + 1;
327         if(self.frame > self.t_length)
328                 self.frame = 0;
329 }
330
331 void FlagThink()
332 {
333         local entity e;
334
335         self.nextthink = time + 0.1;
336
337         // sorry, we have to reset the flag size if it got squished by something
338         if(self.mins != FLAG_MIN || self.maxs != FLAG_MAX)
339         {
340                 // if we can grow back, grow back
341                 tracebox(self.origin, FLAG_MIN, FLAG_MAX, self.origin, MOVE_NOMONSTERS, self);
342                 if(!trace_startsolid)
343                         setsize(self, FLAG_MIN, FLAG_MAX);
344         }
345
346         if(self == ctf_worldflaglist) // only for the first flag
347         {
348                 FOR_EACH_CLIENT(e)
349                         ctf_captureshield_update(e, 1); // release shield only
350         }
351
352         AnimateFlag();
353
354         if(self.speedrunning)
355         if(self.cnt == FLAG_CARRY)
356         {
357                 if(self.owner)
358                 if(flagcaptimerecord)
359                 if(time >= self.flagpickuptime + flagcaptimerecord)
360                 {
361                         bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
362
363                         self.owner.impulse = 77; // returning!
364                         e = self;
365                         self = self.owner;
366                         ReturnFlag(e);
367                         ImpulseCommands();
368                         self = e;
369                         return;
370                 }
371         }
372
373         if (self.cnt == FLAG_BASE)
374                 return;
375
376         if (self.cnt == FLAG_DROPPED)
377         {
378                 // flag fallthrough? FIXME remove this if bug is really fixed now
379                 if(self.origin_z < -131072)
380                 {
381                         dprint("FLAG FALLTHROUGH just happened\n");
382                         self.pain_finished = 0;
383                 }
384                 setattachment(self, world, "");
385                 if (time > self.pain_finished)
386                 {
387                         bprint("The ", self.netname, " has returned to base\n");
388                         sound (self, CHAN_TRIGGER, self.noise3, VOL_BASE, ATTN_NONE);
389                         LogCTF("returned", self.team, world);
390                         ReturnFlag(self);
391                 }
392                 return;
393         }
394
395         e = self.owner;
396         if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
397         {
398                 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
399                 DropFlag(self, world, world);
400                 return;
401         }
402
403         if(cvar("g_ctf_allow_drop"))
404         if(e.BUTTON_USE)
405                 DropFlag(self, e, world);
406 };
407
408 void flag_cap_ring_think()
409 {
410         self.nextthink = time + 0.05;
411         if(self.count > 24)
412         {
413                 self.think = SUB_Remove;
414                 return;
415         }
416
417         self.scale = self.count / 30;
418
419         self.frame = self.count;
420
421         self.count += 1;
422         self.alpha = self.alpha-0.04;
423 };
424
425 void flag_cap_ring_spawn(vector org)
426 {
427         entity e;
428         e = spawn();
429         setmodel(e, "models/ctf/shockwavetransring.md3");
430         org_z=org_z-15;
431         setorigin(e, org);
432
433         e.count = 1;
434         e.alpha = 0.9;
435         
436         e.think = flag_cap_ring_think;
437         e.nextthink = time;
438 };
439
440 void FlagTouch()
441 {
442         if(gameover) return;
443
444         local float t;
445         local entity player;
446         local string s, s0, h0, h1;
447         if (other.classname != "player")
448                 return;
449         if (other.health < 1) // ignore dead players
450                 return;
451
452         if (self.cnt == FLAG_CARRY)
453                 return;
454
455         if (self.cnt == FLAG_BASE)
456         if (other.team == self.team)
457         if (other.flagcarried) // he's got a flag
458         if (other.flagcarried.team != self.team) // capture
459         {
460                 if (other.flagcarried == world)
461                 {
462                         return;
463                 }
464                 if(cvar("g_ctf_captimerecord_allow_assisted") || player_count - currentbots <= 1) // at most one human
465                 {
466                         t = time - other.flagcarried.flagpickuptime;
467                         s = ftos_decimals(t, 2);
468                         s0 = ftos_decimals(flagcaptimerecord, 2);
469                         h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
470                         h1 = other.netname;
471                         if(h0 == h1)
472                                 h0 = "his";
473                         else
474                                 h0 = strcat(h0, "^7's"); // h0: display text for previous netname
475                         if (flagcaptimerecord == 0)
476                         {
477                                 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
478                                 flagcaptimerecord = t;
479                                 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
480                                 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
481                                 write_recordmarker(other, time - t, t);
482                         }
483                         else if (t < flagcaptimerecord)
484                         {
485                                 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
486                                 flagcaptimerecord = t;
487                                 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
488                                 db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), h1);
489                                 write_recordmarker(other, time - t, t);
490                         }
491                         else
492                         {
493                                 bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
494                         }
495                 }
496                 else
497                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, "\n");
498
499                 PlayerTeamScore_Add(other, SP_CTF_CAPS, ST_CTF_CAPS, 1);
500                 LogCTF("capture", other.flagcarried.team, other);
501                 // give credit to the individual player
502                 UpdateFrags(other, ctf_score_value("score_capture"));
503
504                 if (cvar("g_ctf_flag_capture_effects")) {
505                         if (other.team == COLOR_TEAM1) { // red team scores effect
506                                 pointparticles(particleeffectnum("red_ground_quake"), self.origin, '0 0 0', 1);
507                                 flag_cap_ring_spawn(self.origin);
508                         }
509                         if (other.team == COLOR_TEAM2) { // blue team scores effect
510                                 pointparticles(particleeffectnum("blue_ground_quake"), self.origin, '0 0 0', 1);
511                                 flag_cap_ring_spawn(self.origin);
512                         }
513                 }
514
515                 sound (other, CHAN_AUTO, self.noise2, VOL_BASE, ATTN_NONE);
516                 WaypointSprite_DetachCarrier(other);
517                 if(self.speedrunning)
518                         FakeTimeLimit(other, -1);
519                 RegenFlag (other.flagcarried);
520                 other.flagcarried = world;
521                 other.next_take_time = time + 1;
522         }
523         if (self.cnt == FLAG_BASE)
524         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
525         if (other.team != self.team)
526         if (!other.flagcarried)
527         if (!other.ctf_captureshielded)
528         {
529                 if (other.next_take_time > time)
530                         return;
531                         
532                 if (cvar("g_ctf_flag_pickup_effects")) // pickup effect
533                         pointparticles(particleeffectnum("smoke_ring"), 0.5 * (self.absmin + self.absmax), '0 0 0', 1);
534                         
535                 // pick up
536                 self.flagpickuptime = time; // used for timing runs
537                 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
538                 if(other.speedrunning)
539                 if(flagcaptimerecord)
540                         FakeTimeLimit(other, time + flagcaptimerecord);
541                 self.solid = SOLID_NOT;
542                 setorigin(self, self.origin); // relink
543                 self.owner = other;
544                 other.flagcarried = self;
545                 self.cnt = FLAG_CARRY;
546                 self.angles = '0 0 0';
547                 bprint(other.netname, "^7 got the ", self.netname, "\n");
548                 UpdateFrags(other, ctf_score_value("score_pickup_base"));
549                 self.dropperid = other.playerid;
550                 PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
551                 LogCTF("steal", self.team, other);
552                 sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
553
554                 FOR_EACH_PLAYER(player)
555                         if(player.team == self.team)
556                                 centerprint(player, "The enemy got your flag! Retrieve it!");
557
558                 self.movetype = MOVETYPE_NONE;
559                 setorigin(self, FLAG_CARRY_POS);
560                 setattachment(self, other, "");
561                 WaypointSprite_AttachCarrier("flagcarrier", other);
562                 WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 1 0');
563                 WaypointSprite_Ping(self.sprite);
564
565                 return;
566         }
567
568         if (self.cnt == FLAG_DROPPED)
569         {
570                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
571                 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
572                 {
573                         // return flag
574                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
575
576                         // punish the player who last had it
577                         FOR_EACH_PLAYER(player)
578                                 if(player.playerid == self.dropperid)
579                                 {
580                                         PlayerScore_Add(player, SP_SCORE, -ctf_score_value("penalty_returned"));
581                                         ctf_captureshield_update(player, 0); // shield only
582                                 }
583
584                         // punish the team who was last carrying it
585                         if(self.team == COLOR_TEAM1)
586                                 TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, -ctf_score_value("penalty_returned"));
587                         else
588                                 TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, -ctf_score_value("penalty_returned"));
589
590                         // reward the player who returned it
591                         if(other.playerid == self.playerid) // is this the guy who killed the FC last?
592                         {
593                                 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
594                                         UpdateFrags(other, ctf_score_value("score_return_by_killer"));
595                                 else
596                                         UpdateFrags(other, ctf_score_value("score_return_rogue_by_killer"));
597                         }
598                         else
599                         {
600                                 if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
601                                         UpdateFrags(other, ctf_score_value("score_return"));
602                                 else
603                                         UpdateFrags(other, ctf_score_value("score_return_rogue"));
604                         }
605                         PlayerScore_Add(other, SP_CTF_RETURNS, 1);
606                         LogCTF("return", self.team, other);
607                         sound (other, CHAN_AUTO, self.noise1, VOL_BASE, ATTN_NONE);
608                         ReturnFlag(self);
609                 }
610                 else if (!other.flagcarried && (other.playerid != self.dropperid || time > self.ctf_droptime + cvar("g_balance_ctf_delay_collect")))
611                 {
612                         if (cvar("g_ctf_flag_pickup_effects")) // field pickup effect
613                                 pointparticles(particleeffectnum("smoke_ring"), 0.5 * (self.absmin + self.absmax), '0 0 0', 1);
614                         
615                         // pick up
616                         self.solid = SOLID_NOT;
617                         setorigin(self, self.origin); // relink
618                         self.owner = other;
619                         other.flagcarried = self;
620                         self.cnt = FLAG_CARRY;
621                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
622
623                         float f;
624                         f = bound(0, (self.pain_finished - time) / cvar("g_ctf_flag_returntime"), 1);
625                         //print("factor is ", ftos(f), "\n");
626                         f = ctf_score_value("score_pickup_dropped_late") * (1-f)
627                           + ctf_score_value("score_pickup_dropped_early") * f;
628                         f = floor(f + 0.5);
629                         self.dropperid = other.playerid;
630                         //print("score is ", ftos(f), "\n");
631
632                         UpdateFrags(other, f);
633                         PlayerScore_Add(other, SP_CTF_PICKUPS, 1);
634                         LogCTF("pickup", self.team, other);
635                         sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
636
637                         FOR_EACH_PLAYER(player)
638                                 if(player.team == self.team)
639                                         centerprint(player, "The enemy got your flag! Retrieve it!");
640
641                         self.movetype = MOVETYPE_NONE;  // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
642                         setorigin(self, FLAG_CARRY_POS);
643                         setattachment(self, other, "");
644                         self.damageforcescale = 0;
645                         WaypointSprite_AttachCarrier("flagcarrier", other);
646                         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 1 0');
647                 }
648         }
649 };
650
651 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
652 CTF Starting point for a player
653 in team one (Red).
654
655 Keys:
656 "angle"
657  viewing angle when spawning
658 */
659 void spawnfunc_info_player_team1()
660 {
661         if(g_assault)
662         {
663                 remove(self);
664                 return;
665         }
666         self.team = COLOR_TEAM1; // red
667         spawnfunc_info_player_deathmatch();
668 };
669 //self.team = 4;self.classname = "info_player_start";spawnfunc_info_player_start();};
670
671 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
672 CTF Starting point for a player in
673 team two (Blue).
674
675 Keys:
676 "angle"
677  viewing angle when spawning
678 */
679 void spawnfunc_info_player_team2()
680 {
681         if(g_assault)
682         {
683                 remove(self);
684                 return;
685         }
686         self.team = COLOR_TEAM2; // blue
687         spawnfunc_info_player_deathmatch();
688 };
689 //self.team = 13;self.classname = "info_player_start";spawnfunc_info_player_start();};
690
691 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
692 CTF Starting point for a player in
693 team three (Yellow).
694
695 Keys:
696 "angle"
697  viewing angle when spawning
698 */
699 void spawnfunc_info_player_team3()
700 {
701         if(g_assault)
702         {
703                 remove(self);
704                 return;
705         }
706         self.team = COLOR_TEAM3; // yellow
707         spawnfunc_info_player_deathmatch();
708 };
709
710
711 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
712 CTF Starting point for a player in
713 team four (Magenta).
714
715 Keys:
716 "angle"
717  viewing angle when spawning
718 */
719 void spawnfunc_info_player_team4()
720 {
721         if(g_assault)
722         {
723                 remove(self);
724                 return;
725         }
726         self.team = COLOR_TEAM4; // purple
727         spawnfunc_info_player_deathmatch();
728 };
729
730 void item_flag_reset()
731 {
732         DropFlag(self, world, world);
733         ReturnFlag(self);
734 }
735
736 void item_flag_postspawn()
737 { // Check CTF Item Flag Post Spawn
738
739         // Flag Glow Trail Support
740         if(cvar("g_ctf_flag_glowtrails"))
741         { // Provide Flag Glow Trail
742                 if(self.team == COLOR_TEAM1)
743                         // Red
744                         self.glow_color = 251;
745                 else
746                 if(self.team == COLOR_TEAM2)
747                         // Blue
748                         self.glow_color = 210;
749                         
750                 self.glow_size = 25;
751                 self.glow_trail = 1;
752         }
753 };
754
755 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
756 CTF flag for team one (Red).
757 Multiple are allowed.
758
759 Keys:
760 "angle"
761  Angle the flag will point
762 (minus 90 degrees)
763 "model"
764  model to use, note this needs red and blue as skins 0 and 1
765  (default models/ctf/flag.md3)
766 "noise"
767  sound played when flag is picked up
768  (default ctf/take.wav)
769 "noise1"
770  sound played when flag is returned by a teammate
771  (default ctf/return.wav)
772 "noise2"
773  sound played when flag is captured
774  (default ctf/redcapture.wav)
775 "noise3"
776  sound played when flag is lost in the field and respawns itself
777  (default ctf/respawn.wav)
778 */
779
780 void spawnfunc_item_flag_team1()
781 {
782         if (!g_ctf)
783         {
784                 remove(self);
785                 return;
786         }
787
788         //if(!cvar("teamplay"))
789         //      cvar_set("teamplay", "3");
790
791         // link flag into ctf_worldflaglist
792         self.ctf_worldflagnext = ctf_worldflaglist;
793         ctf_worldflaglist = self;
794
795         self.classname = "item_flag_team";
796         if(g_ctf_reverse)
797         {
798                 self.team = COLOR_TEAM2; // color 13 team (blue)
799                 self.items = IT_KEY1; // silver key (bluish enough)
800         }
801         else
802         {
803                 self.team = COLOR_TEAM1; // color 4 team (red)
804                 self.items = IT_KEY2; // gold key (redish enough)
805         }
806         self.netname = "^1RED^7 flag";
807         self.target = "###item###";
808         self.skin = cvar("g_ctf_flag_red_skin");
809         if(self.spawnflags & 1)
810                 self.noalign = 1;
811         if (!self.model)
812                 self.model = cvar_string("g_ctf_flag_red_model");
813         if (!self.noise)
814                 self.noise = "ctf/red_taken.wav";
815         if (!self.noise1)
816                 self.noise1 = "ctf/red_returned.wav";
817         if (!self.noise2)
818                 self.noise2 = "ctf/red_capture.wav"; // blue team scores by capturing the red flag
819         if (!self.noise3)
820                 self.noise3 = "ctf/flag_respawn.wav";
821         if (!self.noise4)
822                 self.noise4 = "ctf/red_dropped.wav";
823         precache_model (self.model);
824         setmodel (self, self.model); // precision set below
825         precache_sound (self.noise);
826         precache_sound (self.noise1);
827         precache_sound (self.noise2);
828         precache_sound (self.noise3);
829         precache_sound (self.noise4);
830         //setsize(self, '-16 -16 -37', '16 16 37');
831         setsize(self, FLAG_MIN, FLAG_MAX);
832         setorigin(self, self.origin + '0 0 37');
833         self.nextthink = time + 0.2; // start after doors etc
834         self.think = place_flag;
835
836         if(!self.scale)
837                 self.scale = 0.6;
838         //if(!self.glow_size)
839         //      self.glow_size = 50;
840
841         self.effects = self.effects | EF_LOWPRECISION;
842         if(cvar("g_ctf_fullbrightflags"))
843                 self.effects |= EF_FULLBRIGHT;
844         if(cvar("g_ctf_dynamiclights"))
845                 self.effects |= EF_RED;
846
847         // From Spidflisk
848         item_flag_postspawn();
849
850         waypoint_spawnforitem(self);
851
852         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
853         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, colormapPaletteColor(COLOR_TEAM1 - 1, FALSE));
854
855         precache_model("models/ctf/shield.md3");
856         precache_model("models/ctf/shockwavetransring.md3");
857
858         self.reset = item_flag_reset;
859 };
860
861 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
862 CTF flag for team two (Blue).
863 Multiple are allowed.
864
865 Keys:
866 "angle"
867  Angle the flag will point
868 (minus 90 degrees)
869 "model"
870  model to use, note this needs red and blue as skins 0 and 1
871  (default models/ctf/flag.md3)
872 "noise"
873  sound played when flag is picked up
874  (default ctf/take.wav)
875 "noise1"
876  sound played when flag is returned by a teammate
877  (default ctf/return.wav)
878 "noise2"
879  sound played when flag is captured
880  (default ctf/bluecapture.wav)
881 "noise3"
882  sound played when flag is lost in the field and respawns itself
883  (default ctf/respawn.wav)
884 */
885
886 void spawnfunc_item_flag_team2()
887 {
888         if (!g_ctf)
889         {
890                 remove(self);
891                 return;
892         }
893         //if(!cvar("teamplay"))
894         //      cvar_set("teamplay", "3");
895
896         // link flag into ctf_worldflaglist
897         self.ctf_worldflagnext = ctf_worldflaglist;
898         ctf_worldflaglist = self;
899
900         self.classname = "item_flag_team";
901         if(g_ctf_reverse)
902         {
903                 self.team = COLOR_TEAM1; // color 4 team (red)
904                 self.items = IT_KEY2; // gold key (redish enough)
905         }
906         else
907         {
908                 self.team = COLOR_TEAM2; // color 13 team (blue)
909                 self.items = IT_KEY1; // silver key (bluish enough)
910         }
911         self.netname = "^4BLUE^7 flag";
912         self.target = "###item###";
913         self.skin = cvar("g_ctf_flag_blue_skin");
914         if(self.spawnflags & 1)
915                 self.noalign = 1;
916         if (!self.model)
917                 self.model = cvar_string("g_ctf_flag_blue_model");
918         if (!self.noise)
919                 self.noise = "ctf/blue_taken.wav";
920         if (!self.noise1)
921                 self.noise1 = "ctf/blue_returned.wav";
922         if (!self.noise2)
923                 self.noise2 = "ctf/blue_capture.wav"; // blue team scores by capturing the red flag
924         if (!self.noise3)
925                 self.noise3 = "ctf/flag_respawn.wav";
926         if (!self.noise4)
927                 self.noise4 = "ctf/blue_dropped.wav";
928         precache_model (self.model);
929         setmodel (self, self.model); // precision set below
930         precache_sound (self.noise);
931         precache_sound (self.noise1);
932         precache_sound (self.noise2);
933         precache_sound (self.noise3);
934         precache_sound (self.noise4);
935         //setsize(self, '-16 -16 -37', '16 16 37');
936         setsize(self, FLAG_MIN, FLAG_MAX);
937         setorigin(self, self.origin + '0 0 37');
938         self.nextthink = time + 0.2; // start after doors etc
939         self.think = place_flag;
940
941         if(!self.scale)
942                 self.scale = 0.6;
943         //if(!self.glow_size)
944         //      self.glow_size = 50;
945
946         self.effects = self.effects | EF_LOWPRECISION;
947         if(cvar("g_ctf_fullbrightflags"))
948                 self.effects |= EF_FULLBRIGHT;
949         if(cvar("g_ctf_dynamiclights"))
950                 self.effects |= EF_BLUE;
951
952         // From Spidflisk
953         item_flag_postspawn();
954
955         waypoint_spawnforitem(self);
956
957         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
958         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, colormapPaletteColor(COLOR_TEAM2 - 1, FALSE));
959
960         precache_model("models/ctf/shield.md3");
961
962         self.reset = item_flag_reset;
963 };
964
965
966 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
967 Team declaration for CTF gameplay, this allows you to decide what team
968 names and control point models are used in your map.
969
970 Note: If you use spawnfunc_ctf_team entities you must define at least 2!  However, unlike
971 domination, you don't need to make a blank one too.
972
973 Keys:
974 "netname"
975  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
976 "cnt"
977  Scoreboard color of the team (for example 4 is red and 13 is blue)
978
979 */
980
981 void spawnfunc_ctf_team()
982 {
983         if (!g_ctf)
984         {
985                 remove(self);
986                 return;
987         }
988         self.classname = "ctf_team";
989         self.team = self.cnt + 1;
990 };
991
992 // code from here on is just to support maps that don't have control point and team entities
993 void ctf_spawnteam (string teamname, float teamcolor)
994 {
995         local entity oldself;
996         oldself = self;
997         self = spawn();
998         self.classname = "ctf_team";
999         self.netname = teamname;
1000         self.cnt = teamcolor;
1001
1002         spawnfunc_ctf_team();
1003
1004         self = oldself;
1005 };
1006
1007 // spawn some default teams if the map is not set up for ctf
1008 void ctf_spawnteams()
1009 {
1010         float numteams;
1011
1012         numteams = 2;//cvar("g_ctf_default_teams");
1013
1014         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
1015         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
1016 };
1017
1018 void ctf_delayedinit()
1019 {
1020         // if no teams are found, spawn defaults
1021         if (find(world, classname, "ctf_team") == world)
1022                 ctf_spawnteams();
1023
1024         ScoreRules_ctf();
1025 };
1026
1027 void ctf_init()
1028 {
1029         InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
1030         flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
1031
1032         captureshield_min_negscore = cvar("g_ctf_shield_min_negscore");
1033         captureshield_max_ratio = cvar("g_ctf_shield_max_ratio");
1034         captureshield_force = cvar("g_ctf_shield_force");
1035 };
1036
1037 void ctf_setstatus2(entity flag, float shift)
1038 {
1039         if (flag.cnt == FLAG_CARRY)
1040                 if (flag.owner == self)
1041                         self.items |= shift * 3;
1042                 else
1043                         self.items |= shift * 1;
1044         else if (flag.cnt == FLAG_DROPPED)
1045                 self.items |= shift * 2;
1046         else
1047         {
1048                 // no status bits
1049         }
1050 };
1051
1052 void ctf_setstatus()
1053 {
1054         self.items &~= IT_RED_FLAG_TAKEN;
1055         self.items &~= IT_RED_FLAG_LOST;
1056         self.items &~= IT_BLUE_FLAG_TAKEN;
1057         self.items &~= IT_BLUE_FLAG_LOST;
1058         self.items &~= IT_CTF_SHIELDED;
1059
1060         if (g_ctf) {
1061                 local entity flag;
1062                 float redflags, blueflags;
1063
1064                 if(self.ctf_captureshielded)
1065                         self.items |= IT_CTF_SHIELDED;
1066
1067                 redflags = 0;
1068                 blueflags = 0;
1069
1070                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
1071                 {
1072                         if(flag.items & IT_KEY2) // blue
1073                                 ++redflags;
1074                         else if(flag.items & IT_KEY1) // red
1075                                 ++blueflags;
1076                 }
1077
1078                 // blinking magic: if there is more than one flag, show one of these in a clever way
1079                 if(redflags)
1080                         redflags = mod(floor(time * redflags * 0.75), redflags);
1081                 if(blueflags)
1082                         blueflags = mod(floor(time * blueflags * 0.75), blueflags);
1083
1084                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
1085                 {
1086                         if(flag.items & IT_KEY2) // blue
1087                         {
1088                                 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
1089                                         ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
1090                         }
1091                         else if(flag.items & IT_KEY1) // red
1092                         {
1093                                 if(--blueflags == -1) // happens exactly once
1094                                         ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
1095                         }
1096                 }
1097         }
1098 };
1099 /*
1100 entity(float cteam) ctf_team_has_commander =
1101 {
1102         entity pl;
1103         if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
1104                 return world;
1105         
1106         FOR_EACH_REALPLAYER(pl) {
1107                 if(pl.team == cteam && pl.iscommander) {
1108                         return pl;
1109                 }
1110         }
1111         return world;
1112 };
1113
1114 void(entity e, float st) ctf_setstate =
1115 {
1116         e.ctf_state = st;
1117         ++e.version;
1118 };
1119
1120 void(float cteam) ctf_new_commander =
1121 {
1122         entity pl, plmax;
1123         
1124         plmax = world;
1125         FOR_EACH_REALPLAYER(pl) {
1126                 if(pl.team == cteam) {
1127                         if(pl.iscommander) { // don't reassign if alreay there
1128                                 return;
1129                         }
1130                         if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
1131                                 plmax = pl;
1132                 }
1133         }
1134         if(plmax == world) {
1135                 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
1136                 return;
1137         }
1138
1139         plmax.iscommander = TRUE;
1140         ctf_setstate(plmax, 3);
1141         sprint(plmax, "^3You're the commander now!\n");
1142         centerprint(plmax, "^3You're the commander now!\n");
1143 };
1144
1145 void() ctf_clientconnect =
1146 {
1147         self.iscommander = FALSE;
1148         
1149         if(!self.team || self.classname != "player") {
1150                 ctf_setstate(self, -1);
1151         } else
1152                 ctf_setstate(self, 0);
1153
1154         self.team_saved = self.team;
1155         
1156         if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
1157                 ctf_new_commander(self.team);
1158         }
1159 };
1160
1161 void() ctf_playerchanged =
1162 {
1163         if(!self.team || self.classname != "player") {
1164                 ctf_setstate(self, -1);
1165         } else if(self.ctf_state < 0 && self.classname == "player") {
1166                 ctf_setstate(self, 0);
1167         }
1168
1169         if(self.iscommander &&
1170            (self.classname != "player" || self.team != self.team_saved)
1171                 )
1172         {
1173                 self.iscommander = FALSE;
1174                 if(self.classname == "player")
1175                         ctf_setstate(self, 0);
1176                 else
1177                         ctf_setstate(self, -1);
1178                 ctf_new_commander(self.team_saved);
1179         }
1180         
1181         self.team_saved = self.team;
1182         
1183         ctf_new_commander(self.team);
1184 };
1185
1186 void() ctf_clientdisconnect =
1187 {
1188         if(self.iscommander)
1189         {
1190                 ctf_new_commander(self.team);
1191         }
1192 };
1193
1194 entity GetPlayer(string);
1195 float() ctf_clientcommand =
1196 {
1197         entity e;
1198         if(argv(0) == "order") {
1199                 if(!g_ctf) {
1200                         sprint(self, "This command is not supported in this gamemode.\n");
1201                         return TRUE;
1202                 }
1203                 if(!self.iscommander) {
1204                         sprint(self, "^1You are not the commander!\n");
1205                         return TRUE;
1206                 }
1207                 if(argv(2) == "") {
1208                         sprint(self, "Usage: order #player status   - (playernumber as in status)\n");
1209                         return TRUE;
1210                 }
1211                 e = GetPlayer(argv(1));
1212                 if(e == world) {
1213                         sprint(self, "Invalid player.\nUsage: order #player status   - (playernumber as in status)\n");
1214                         return TRUE;
1215                 }
1216                 if(e.team != self.team) {
1217                         sprint(self, "^3You can only give orders to your own team!\n");
1218                         return TRUE;
1219                 }
1220                 if(argv(2) == "attack") {
1221                         sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
1222                         sprint(e, "^1Attack!\n");
1223                         centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
1224                         ctf_setstate(e, 1);
1225                 } else if(argv(2) == "defend") {
1226                         sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
1227                         sprint(e, "^Defend!\n");
1228                         centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
1229                         ctf_setstate(e, 2);
1230                 } else {
1231                         sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");
1232                 }
1233                 return TRUE;
1234         }
1235         return FALSE;
1236 };
1237 */