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