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