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