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