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