]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
forgot this one
[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
643
644
645 /*QUAKED spawnfunc_item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
646 CTF flag for team one (Red).
647 Multiple are allowed.
648
649 Keys:
650 "angle"
651  Angle the flag will point
652 (minus 90 degrees)
653 "model"
654  model to use, note this needs red and blue as skins 0 and 1
655  (default models/ctf/flag.md3)
656 "noise"
657  sound played when flag is picked up
658  (default ctf/take.wav)
659 "noise1"
660  sound played when flag is returned by a teammate
661  (default ctf/return.wav)
662 "noise2"
663  sound played when flag is captured
664  (default ctf/redcapture.wav)
665 "noise3"
666  sound played when flag is lost in the field and respawns itself
667  (default ctf/respawn.wav)
668 */
669
670 void spawnfunc_item_flag_team1()
671 {
672         if (!g_ctf)
673         {
674                 remove(self);
675                 return;
676         }
677
678         //if(!cvar("teamplay"))
679         //      cvar_set("teamplay", "3");
680
681         // link flag into ctf_worldflaglist
682         self.ctf_worldflagnext = ctf_worldflaglist;
683         ctf_worldflaglist = self;
684
685         self.classname = "item_flag_team";
686         if(g_ctf_reverse)
687         {
688                 self.team = COLOR_TEAM2; // color 13 team (blue)
689                 self.items = IT_KEY1; // silver key (bluish enough)
690         }
691         else
692         {
693                 self.team = COLOR_TEAM1; // color 4 team (red)
694                 self.items = IT_KEY2; // gold key (redish enough)
695         }
696         self.netname = "^1RED^7 flag";
697         self.target = "###item###";
698         self.skin = 0;
699         if(self.spawnflags & 1)
700                 self.noalign = 1;
701         if (!self.model)
702                 self.model = "models/ctf/flags.md3";
703         if (!self.noise)
704                 self.noise = "ctf/take.wav";
705         if (!self.noise1)
706                 self.noise1 = "ctf/return.wav";
707         if (!self.noise2)
708                 self.noise2 = "ctf/redcapture.wav"; // blue team scores by capturing the red flag
709         if (!self.noise3)
710                 self.noise3 = "ctf/respawn.wav";
711         precache_model (self.model);
712         setmodel (self, self.model); // precision set below
713         precache_sound (self.noise);
714         precache_sound (self.noise1);
715         precache_sound (self.noise2);
716         precache_sound (self.noise3);
717         //setsize(self, '-16 -16 -37', '16 16 37');
718         setsize(self, PL_MIN + '0 0 -13', PL_MAX + '0 0 -13');
719         setorigin(self, self.origin + '0 0 37');
720         self.nextthink = time + 0.2; // start after doors etc
721         self.think = place_flag;
722
723         if(!self.scale)
724                 self.scale = 0.6;
725         //if(!self.glow_size)
726         //      self.glow_size = 50;
727
728         self.effects = self.effects | EF_LOWPRECISION;
729         if(cvar("g_ctf_fullbrightflags"))
730                 self.effects |= EF_FULLBRIGHT;
731         if(cvar("g_ctf_dynamiclights"))
732                 self.effects |= EF_RED;
733
734         waypoint_spawnforitem(self);
735
736         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, sprite);
737         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, '1 0 0');
738
739         precache_model("models/onslaught/generator_shield.md3");
740 };
741
742 /*QUAKED spawnfunc_item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
743 CTF flag for team two (Blue).
744 Multiple are allowed.
745
746 Keys:
747 "angle"
748  Angle the flag will point
749 (minus 90 degrees)
750 "model"
751  model to use, note this needs red and blue as skins 0 and 1
752  (default models/ctf/flag.md3)
753 "noise"
754  sound played when flag is picked up
755  (default ctf/take.wav)
756 "noise1"
757  sound played when flag is returned by a teammate
758  (default ctf/return.wav)
759 "noise2"
760  sound played when flag is captured
761  (default ctf/bluecapture.wav)
762 "noise3"
763  sound played when flag is lost in the field and respawns itself
764  (default ctf/respawn.wav)
765 */
766
767 void spawnfunc_item_flag_team2()
768 {
769         if (!g_ctf)
770         {
771                 remove(self);
772                 return;
773         }
774         //if(!cvar("teamplay"))
775         //      cvar_set("teamplay", "3");
776
777         // link flag into ctf_worldflaglist
778         self.ctf_worldflagnext = ctf_worldflaglist;
779         ctf_worldflaglist = self;
780
781         self.classname = "item_flag_team";
782         if(g_ctf_reverse)
783         {
784                 self.team = COLOR_TEAM1; // color 4 team (red)
785                 self.items = IT_KEY2; // gold key (redish enough)
786         }
787         else
788         {
789                 self.team = COLOR_TEAM2; // color 13 team (blue)
790                 self.items = IT_KEY1; // silver key (bluish enough)
791         }
792         self.netname = "^4BLUE^7 flag";
793         self.target = "###item###";
794         self.skin = 1;
795         if(self.spawnflags & 1)
796                 self.noalign = 1;
797         if (!self.model)
798                 self.model = "models/ctf/flags.md3";
799         if (!self.noise)
800                 self.noise = "ctf/take.wav";
801         if (!self.noise1)
802                 self.noise1 = "ctf/return.wav";
803         if (!self.noise2)
804                 self.noise2 = "ctf/bluecapture.wav"; // red team scores by capturing the blue flag
805         if (!self.noise3)
806                 self.noise3 = "ctf/respawn.wav";
807         precache_model (self.model);
808         setmodel (self, self.model); // precision set below
809         precache_sound (self.noise);
810         precache_sound (self.noise1);
811         precache_sound (self.noise2);
812         precache_sound (self.noise3);
813         //setsize(self, '-16 -16 -37', '16 16 37');
814         setsize(self, PL_MIN + '0 0 -13', PL_MAX + '0 0 -13');
815         setorigin(self, self.origin + '0 0 37');
816         self.nextthink = time + 0.2; // start after doors etc
817         self.think = place_flag;
818
819         if(!self.scale)
820                 self.scale = 0.6;
821         //if(!self.glow_size)
822         //      self.glow_size = 50;
823
824         self.effects = self.effects | EF_LOWPRECISION;
825         if(cvar("g_ctf_fullbrightflags"))
826                 self.effects |= EF_FULLBRIGHT;
827         if(cvar("g_ctf_dynamiclights"))
828                 self.effects |= EF_BLUE;
829
830         waypoint_spawnforitem(self);
831
832         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, sprite);
833         WaypointSprite_UpdateTeamRadar(self.sprite, RADARICON_FLAG, '0 0 1');
834
835         precache_model("models/onslaught/generator_shield.md3");
836 };
837
838
839 /*QUAKED spawnfunc_ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
840 Team declaration for CTF gameplay, this allows you to decide what team
841 names and control point models are used in your map.
842
843 Note: If you use spawnfunc_ctf_team entities you must define at least 2!  However, unlike
844 domination, you don't need to make a blank one too.
845
846 Keys:
847 "netname"
848  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
849 "cnt"
850  Scoreboard color of the team (for example 4 is red and 13 is blue)
851
852 */
853
854 void spawnfunc_ctf_team()
855 {
856         if (!g_ctf)
857         {
858                 remove(self);
859                 return;
860         }
861         self.classname = "ctf_team";
862         self.team = self.cnt + 1;
863 };
864
865 // code from here on is just to support maps that don't have control point and team entities
866 void ctf_spawnteam (string teamname, float teamcolor)
867 {
868         local entity oldself;
869         oldself = self;
870         self = spawn();
871         self.classname = "ctf_team";
872         self.netname = teamname;
873         self.cnt = teamcolor;
874
875         spawnfunc_ctf_team();
876
877         self = oldself;
878 };
879
880 // spawn some default teams if the map is not set up for ctf
881 void ctf_spawnteams()
882 {
883         float numteams;
884
885         numteams = 2;//cvar("g_ctf_default_teams");
886
887         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
888         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
889 };
890
891 void ctf_delayedinit()
892 {
893         // if no teams are found, spawn defaults
894         if (find(world, classname, "ctf_team") == world)
895                 ctf_spawnteams();
896
897         ScoreRules_ctf();
898 };
899
900 void ctf_init()
901 {
902         InitializeEntity(world, ctf_delayedinit, INITPRIO_GAMETYPE);
903         flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
904
905         captureshield_min_negscore = cvar("g_ctf_shield_min_negscore");
906         captureshield_max_ratio = cvar("g_ctf_shield_max_ratio");
907         captureshield_force = cvar("g_ctf_shield_force");
908 };
909
910 void ctf_setstatus2(entity flag, float shift)
911 {
912         if (flag.cnt == FLAG_CARRY)
913                 if (flag.owner == self)
914                         self.items |= shift * 3;
915                 else
916                         self.items |= shift * 1;
917         else if (flag.cnt == FLAG_DROPPED)
918                 self.items |= shift * 2;
919         else
920         {
921                 // no status bits
922         }
923 };
924
925 void ctf_setstatus()
926 {
927         self.items &~= IT_RED_FLAG_TAKEN;
928         self.items &~= IT_RED_FLAG_LOST;
929         self.items &~= IT_BLUE_FLAG_TAKEN;
930         self.items &~= IT_BLUE_FLAG_LOST;
931         self.items &~= IT_CTF_SHIELDED;
932
933         if (g_ctf) {
934                 local entity flag;
935                 float redflags, blueflags;
936
937                 if(self.ctf_captureshielded)
938                         self.items |= IT_CTF_SHIELDED;
939
940                 redflags = 0;
941                 blueflags = 0;
942
943                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
944                 {
945                         if(flag.items & IT_KEY2) // blue
946                                 ++redflags;
947                         else if(flag.items & IT_KEY1) // red
948                                 ++blueflags;
949                 }
950
951                 // blinking magic: if there is more than one flag, show one of these in a clever way
952                 if(redflags)
953                         redflags = mod(floor(time * redflags * 0.75), redflags);
954                 if(blueflags)
955                         blueflags = mod(floor(time * blueflags * 0.75), blueflags);
956
957                 for (flag = ctf_worldflaglist; flag; flag = flag.ctf_worldflagnext) if(flag.cnt != FLAG_BASE)
958                 {
959                         if(flag.items & IT_KEY2) // blue
960                         {
961                                 if(--redflags == -1) // happens exactly once (redflags is in 0..count-1, and will --'ed count times)
962                                         ctf_setstatus2(flag, IT_RED_FLAG_TAKEN);
963                         }
964                         else if(flag.items & IT_KEY1) // red
965                         {
966                                 if(--blueflags == -1) // happens exactly once
967                                         ctf_setstatus2(flag, IT_BLUE_FLAG_TAKEN);
968                         }
969                 }
970         }
971 };
972 /*
973 entity(float cteam) ctf_team_has_commander =
974 {
975         entity pl;
976         if(cteam != COLOR_TEAM1 || cteam != COLOR_TEAM2)
977                 return world;
978         
979         FOR_EACH_REALPLAYER(pl) {
980                 if(pl.team == cteam && pl.iscommander) {
981                         return pl;
982                 }
983         }
984         return world;
985 };
986
987 void(entity e, float st) ctf_setstate =
988 {
989         e.ctf_state = st;
990         ++e.version;
991 };
992
993 void(float cteam) ctf_new_commander =
994 {
995         entity pl, plmax;
996         
997         plmax = world;
998         FOR_EACH_REALPLAYER(pl) {
999                 if(pl.team == cteam) {
1000                         if(pl.iscommander) { // don't reassign if alreay there
1001                                 return;
1002                         }
1003                         if(plmax == world || plmax.frags < pl.frags) <<<<<<<<<<<<<<<<< BROKEN in new scoring system
1004                                 plmax = pl;
1005                 }
1006         }
1007         if(plmax == world) {
1008                 bprint(strcat(ColoredTeamName(cteam), " Team has no Commander!\n"));
1009                 return;
1010         }
1011
1012         plmax.iscommander = TRUE;
1013         ctf_setstate(plmax, 3);
1014         sprint(plmax, "^3You're the commander now!\n");
1015         centerprint(plmax, "^3You're the commander now!\n");
1016 };
1017
1018 void() ctf_clientconnect =
1019 {
1020         self.iscommander = FALSE;
1021         
1022         if(!self.team || self.classname != "player") {
1023                 ctf_setstate(self, -1);
1024         } else
1025                 ctf_setstate(self, 0);
1026
1027         self.team_saved = self.team;
1028         
1029         if(self.team != 0 && self.classname == "player" && !ctf_team_has_commander(self.team)) {
1030                 ctf_new_commander(self.team);
1031         }
1032 };
1033
1034 void() ctf_playerchanged =
1035 {
1036         if(!self.team || self.classname != "player") {
1037                 ctf_setstate(self, -1);
1038         } else if(self.ctf_state < 0 && self.classname == "player") {
1039                 ctf_setstate(self, 0);
1040         }
1041
1042         if(self.iscommander &&
1043            (self.classname != "player" || self.team != self.team_saved)
1044                 )
1045         {
1046                 self.iscommander = FALSE;
1047                 if(self.classname == "player")
1048                         ctf_setstate(self, 0);
1049                 else
1050                         ctf_setstate(self, -1);
1051                 ctf_new_commander(self.team_saved);
1052         }
1053         
1054         self.team_saved = self.team;
1055         
1056         ctf_new_commander(self.team);
1057 };
1058
1059 void() ctf_clientdisconnect =
1060 {
1061         if(self.iscommander)
1062         {
1063                 ctf_new_commander(self.team);
1064         }
1065 };
1066
1067 entity GetPlayer(string);
1068 float() ctf_clientcommand =
1069 {
1070         entity e;
1071         if(argv(0) == "order") {
1072                 if(!g_ctf) {
1073                         sprint(self, "This command is not supported in this gamemode.\n");
1074                         return TRUE;
1075                 }
1076                 if(!self.iscommander) {
1077                         sprint(self, "^1You are not the commander!\n");
1078                         return TRUE;
1079                 }
1080                 if(argv(2) == "") {
1081                         sprint(self, "Usage: order #player status   - (playernumber as in status)\n");
1082                         return TRUE;
1083                 }
1084                 e = GetPlayer(argv(1));
1085                 if(e == world) {
1086                         sprint(self, "Invalid player.\nUsage: order #player status   - (playernumber as in status)\n");
1087                         return TRUE;
1088                 }
1089                 if(e.team != self.team) {
1090                         sprint(self, "^3You can only give orders to your own team!\n");
1091                         return TRUE;
1092                 }
1093                 if(argv(2) == "attack") {
1094                         sprint(self, strcat("Ordering ", e.netname, " to attack!\n"));
1095                         sprint(e, "^1Attack!\n");
1096                         centerprint(e, "^7You've been ordered to^9\n^1Attack!\n");
1097                         ctf_setstate(e, 1);
1098                 } else if(argv(2) == "defend") {
1099                         sprint(self, strcat("Ordering ", e.netname, " to defend!\n"));
1100                         sprint(e, "^Defend!\n");
1101                         centerprint(e, "^7You've been ordered to^9\n^2Defend!\n");
1102                         ctf_setstate(e, 2);
1103                 } else {
1104                         sprint(self, "^7Invalid command, use ^3attack^7, or ^3defend^7.\n");
1105                 }
1106                 return TRUE;
1107         }
1108         return FALSE;
1109 };
1110 */