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