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