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