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