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