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