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