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