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