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