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