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