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