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