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