]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
Speedrun mode: impulse 77 now:
[divverent/nexuiz.git] / data / qcsrc / server / ctf.qc
1
2 .float next_take_time;                  // the next time a player can pick up a flag (time + blah)
3                                                                 /// I used this, in part, to fix the looping score bug. - avirox
4
5 //float FLAGSCORE_PICKUP        =  1;
6 //float FLAGSCORE_RETURN        =  5; // returned by owner team
7 //float FLAGSCORE_RETURNROGUE   = 10; // returned by rogue team
8 //float FLAGSCORE_CAPTURE       =  5;
9 //float FLAGSCORE_CAPTURE_TEAM  = 20;
10
11 #define FLAG_CARRY_POS '-15 0 7'
12
13 void FakeTimeLimit(entity e, float t)
14 {
15         msg_entity = e;
16         WriteByte(MSG_ONE, 3); // svc_updatestat
17         WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
18         if(t < 0)
19                 WriteCoord(MSG_ONE, cvar("timelimit"));
20         else
21                 WriteCoord(MSG_ONE, (t + 1) / 60);
22 }
23
24 float   flagcaptimerecord;
25 .float  flagpickuptime;
26
27 void() FlagThink;
28 void() FlagTouch;
29
30 void() place_flag =
31 {
32         if(!self.t_width)
33                 self.t_width = 0.1; // frame animation rate
34         if(!self.t_length)
35                 self.t_length = 119; // maximum frame
36
37         setattachment(self, world, "");
38         self.mdl = self.model;
39         self.flags = FL_ITEM;
40         self.solid = SOLID_TRIGGER;
41         self.movetype = MOVETYPE_NONE;
42         self.velocity = '0 0 0';
43         self.origin_z = self.origin_z + 6;
44         self.think = FlagThink;
45         self.touch = FlagTouch;
46         self.nextthink = time + 0.1;
47         self.cnt = FLAG_BASE;
48         self.mangle = self.angles;
49         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
50         //self.effects = self.effects | EF_DIMLIGHT;
51         if(!self.noalign)
52         {
53                 self.movetype = MOVETYPE_TOSS;
54                 if (!droptofloor())
55                 {
56                         dprint("Flag fell out of level at ", vtos(self.origin), "\n");
57                         remove(self);
58                         return;
59                 }
60         }
61         self.oldorigin = self.origin;
62 };
63
64 void LogCTF(string mode, float flagteam, entity actor)
65 {
66         string s;
67         if(!cvar("sv_eventlog"))
68                 return;
69         s = strcat(":ctf:", mode);
70         s = strcat(s, ":", ftos(flagteam));
71         if(actor != world)
72                 s = strcat(s, ":", ftos(actor.playerid));
73         GameLogEcho(s, FALSE);
74 }
75
76 void(entity e) RegenFlag =
77 {
78         setattachment(e, world, "");
79         e.movetype = MOVETYPE_NONE;
80         if(!self.noalign)
81                 e.movetype = MOVETYPE_TOSS;
82         e.solid = SOLID_TRIGGER;
83         // TODO: play a sound here
84         setorigin(e, e.oldorigin);
85         e.angles = e.mangle;
86         e.cnt = FLAG_BASE;
87         e.owner = world;
88         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
89 };
90
91 void(entity e) ReturnFlag =
92 {
93         if (e.owner)
94         if (e.owner.flagcarried == e)
95         {
96                 WaypointSprite_DetachCarrier(e.owner);
97                 e.owner.flagcarried = world;
98
99                 if(e.speedrunning)
100                         FakeTimeLimit(e.owner, -1);
101         }
102         e.owner = world;
103         RegenFlag(e);
104 };
105
106 void(entity e) DropFlag =
107 {
108         local entity p;
109
110         if(e.speedrunning)
111         {
112                 ReturnFlag(e);
113                 return;
114         }
115
116         if (!e.owner)
117         {
118                 dprint("FLAG: drop - no owner?!?!\n");
119                 return;
120         }
121         p = e.owner;
122         if (p.flagcarried != e)
123         {
124                 dprint("FLAG: drop - owner is not carrying this flag??\n");
125                 return;
126         }
127         bprint(p.netname, "^7 lost the ", e.netname, "\n");
128         WaypointSprite_DetachCarrier(p);
129         LogCTF("dropped", p.team, p.flagcarried);
130
131         setattachment(e, world, "");
132
133         if (p.flagcarried == e)
134                 p.flagcarried = world;
135         e.owner = world;
136
137         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
138         e.solid = SOLID_TRIGGER;
139         e.movetype = MOVETYPE_TOSS;
140         // setsize(e, '-16 -16 0', '16 16 74');
141         setorigin(e, p.origin - '0 0 24' + '0 0 37');
142         e.cnt = FLAG_DROPPED;
143         e.velocity = '0 0 300';
144         e.pain_finished = time + cvar("g_ctf_flag_returntime");//30;
145
146         trace_startsolid = FALSE;
147         tracebox(e.origin, e.mins, e.maxs, e.origin, TRUE, e);
148         if(trace_startsolid)
149                 dprint("FLAG FALLTHROUGH will happen SOON\n");
150 };
151
152 void AnimateFlag()
153 {
154         if(self.delay > time)
155                 return;
156         self.delay = time + self.t_width;
157         if(self.nextthink > self.delay)
158                 self.nextthink = self.delay;
159
160         self.frame = self.frame + 1;
161         if(self.frame > self.t_length)
162                 self.frame = 0;
163 }
164
165 void() FlagThink =
166 {
167         local entity e;
168
169         self.nextthink = time + 0.1;
170
171         AnimateFlag();
172
173         if(self.speedrunning)
174         if(self.cnt == FLAG_CARRY)
175         {
176                 if(self.owner)
177                 if(flagcaptimerecord)
178                 if(time >= self.flagpickuptime + flagcaptimerecord)
179                 {
180                         bprint("The ", self.netname, " became impatient after ", ftos_decimals(flagcaptimerecord, 2), " seconds and returned itself\n");
181
182                         self.owner.impulse = 77; // returning!
183                         e = self;
184                         self = self.owner;
185                         ReturnFlag(e);
186                         ImpulseCommands();
187                         self = e;
188                         return;
189                 }
190         }
191
192         if (self.cnt == FLAG_BASE)
193                 return;
194
195         if (self.cnt == FLAG_DROPPED)
196         {
197                 // flag fallthrough? FIXME remove this if bug is really fixed now
198                 if(self.origin_z < -131072)
199                 {
200                         dprint("FLAG FALLTHROUGH just happened\n");
201                         self.pain_finished = 0;
202                 }
203                 setattachment(self, world, "");
204                 if (time > self.pain_finished)
205                 {
206                         bprint("The ", self.netname, " has returned to base\n");
207                         sound (e, CHAN_AUTO, self.noise3, 1, ATTN_NONE);
208                         LogCTF("returned", self.team, world);
209                         ReturnFlag(self);
210                 }
211                 return;
212         }
213
214         e = self.owner;
215         if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
216         {
217                 dprint("CANNOT HAPPEN - player dead and STILL had a flag!\n");
218                 DropFlag(self);
219                 return;
220         }
221 };
222
223 void() FlagTouch =
224 {
225         if(gameover) return;
226
227         local float t;
228         local entity head;
229         local entity player;
230         local string s, s0;
231         if (other.classname != "player")
232                 return;
233         if (other.health < 1) // ignore dead players
234                 return;
235
236         if (self.cnt == FLAG_CARRY)
237                 return;
238
239         if (self.cnt == FLAG_BASE)
240         if (other.team == self.team)
241         if (other.flagcarried) // he's got a flag
242         if (other.flagcarried.team != self.team) // capture
243         {
244                 if (other.flagcarried == world)
245                 {
246                         return;
247                 }
248                 t = time - other.flagcarried.flagpickuptime;
249                 s = ftos_decimals(t, 2);
250                 s0 = ftos_decimals(flagcaptimerecord, 2);
251                 if (flagcaptimerecord == 0)
252                 {
253                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
254                         flagcaptimerecord = t;
255                 }
256                 else if (t < flagcaptimerecord)
257                 {
258                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking the previous record of ", s0, " seconds\n");
259                         flagcaptimerecord = t;
260                 }
261                 else
262                 {
263                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break the previous record of ", s0, " seconds\n");
264                 }
265
266                 LogCTF("capture", other.flagcarried.team, other);
267                 // give credit to the individual player
268                 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
269
270                 // give credit to all players of the team (rewards large teams)
271                 // NOTE: this defaults to 0
272                 FOR_EACH_PLAYER(head)
273                         if (head.team == self.team)
274                                 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
275
276                 sound (self, CHAN_AUTO, self.noise2, 1, ATTN_NONE);
277                 WaypointSprite_DetachCarrier(other);
278                 if(self.speedrunning)
279                         FakeTimeLimit(other, -1);
280                 RegenFlag (other.flagcarried);
281                 other.flagcarried = world;
282                 other.next_take_time = time + 1;
283         }
284         if (self.cnt == FLAG_BASE)
285         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
286         if (other.team != self.team)
287         if (!other.flagcarried)
288         {
289                 if (other.next_take_time > time)
290                         return;
291                 // pick up
292                 self.flagpickuptime = time; // used for timing runs
293                 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
294                 if(other.speedrunning)
295                 if(flagcaptimerecord)
296                         FakeTimeLimit(other, time + flagcaptimerecord);
297                 self.solid = SOLID_NOT;
298                 setorigin(self, self.origin); // relink
299                 self.owner = other;
300                 other.flagcarried = self;
301                 self.cnt = FLAG_CARRY;
302                 self.angles = '0 0 0';
303                 bprint(other.netname, "^7 got the ", self.netname, "\n");
304                 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
305                 LogCTF("steal", self.team, other);
306                 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
307
308                 FOR_EACH_PLAYER(player)
309                         if(player.team == self.team)
310                                 centerprint(player, "The enemy got your flag! Retrieve it!");
311
312                 self.movetype = MOVETYPE_NONE;
313                 setorigin(self, FLAG_CARRY_POS);
314                 setattachment(self, other, "");
315                 WaypointSprite_AttachCarrier("flagcarrier", other);
316
317                 return;
318         }
319
320         if (self.cnt == FLAG_DROPPED)
321         {
322                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
323                 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
324                 {
325                         // return flag
326                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
327                         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
328                                 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
329                         else
330                                 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
331                         LogCTF("return", self.team, other);
332                         sound (self, CHAN_AUTO, self.noise1, 1, ATTN_NONE);
333                         ReturnFlag(self);
334                 }
335                 else if (!other.flagcarried)
336                 {
337                         // pick up
338                         self.solid = SOLID_NOT;
339                         setorigin(self, self.origin); // relink
340                         self.owner = other;
341                         other.flagcarried = self;
342                         self.cnt = FLAG_CARRY;
343                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
344                         UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
345                         LogCTF("pickup", self.team, other);
346                         sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
347
348                         FOR_EACH_PLAYER(player)
349                                 if(player.team == self.team)
350                                         centerprint(player, "The enemy got your flag! Retrieve it!");
351
352                         self.movetype = MOVETYPE_NONE;  // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
353                         setorigin(self, FLAG_CARRY_POS);
354                         setattachment(self, other, "");
355                         WaypointSprite_AttachCarrier("flagcarrier", other);
356                 }
357         }
358 };
359
360 /*QUAKED info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
361 CTF Starting point for a player
362 in team one (Red).
363
364 Keys:
365 "angle"
366  viewing angle when spawning
367 */
368 void() info_player_team1 =
369 {
370         self.team = COLOR_TEAM1; // red
371         info_player_deathmatch();
372 };
373 //self.team = 4;self.classname = "info_player_start";info_player_start();};
374
375 /*QUAKED info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
376 CTF Starting point for a player in
377 team two (Blue).
378
379 Keys:
380 "angle"
381  viewing angle when spawning
382 */
383 void() info_player_team2 =
384 {
385         self.team = COLOR_TEAM2; // blue
386         info_player_deathmatch();
387 };
388 //self.team = 13;self.classname = "info_player_start";info_player_start();};
389
390 /*QUAKED info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
391 CTF Starting point for a player in
392 team three (Magenta).
393
394 Keys:
395 "angle"
396  viewing angle when spawning
397 */
398 void() info_player_team3 =
399 {
400         self.team = COLOR_TEAM3; // purple
401         info_player_deathmatch();
402 };
403
404
405 /*QUAKED info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
406 CTF Starting point for a player in
407 team four (Yellow).
408
409 Keys:
410 "angle"
411  viewing angle when spawning
412 */
413 void() info_player_team4 =
414 {
415         self.team = COLOR_TEAM4; // yellow
416         info_player_deathmatch();
417 };
418
419
420
421
422 /*QUAKED item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
423 CTF flag for team one (Red).
424 Multiple are allowed.
425
426 Keys:
427 "angle"
428  Angle the flag will point
429 (minus 90 degrees)
430 "model"
431  model to use, note this needs red and blue as skins 0 and 1
432  (default models/ctf/flag.md3)
433 "noise"
434  sound played when flag is picked up
435  (default ctf/take.wav)
436 "noise1"
437  sound played when flag is returned by a teammate
438  (default ctf/return.wav)
439 "noise2"
440  sound played when flag is captured
441  (default ctf/capture.wav)
442 "noise3"
443  sound played when flag is lost in the field and respawns itself
444  (default ctf/respawn.wav)
445 */
446
447 void() item_flag_team1 =
448 {
449         if (!g_ctf)
450                 return;
451         //if(!cvar("teamplay"))
452         //      cvar_set("teamplay", "3");
453
454         self.classname = "item_flag_team1";
455         self.team = COLOR_TEAM1; // color 4 team (red)
456         self.items = IT_KEY2; // gold key (redish enough)
457         self.netname = "^1RED^7 flag";
458         self.target = "###item###";
459         self.skin = 0;
460         if(self.spawnflags & 1)
461                 self.noalign = 1;
462         if (!self.model)
463                 self.model = "models/ctf/flag_red.md3";
464         if (!self.noise)
465                 self.noise = "ctf/take.wav";
466         if (!self.noise1)
467                 self.noise1 = "ctf/return.wav";
468         if (!self.noise2)
469                 self.noise2 = "ctf/capture.wav";
470         if (!self.noise3)
471                 self.noise3 = "ctf/respawn.wav";
472         precache_model (self.model);
473         setmodel (self, self.model); // precision set below
474         precache_sound (self.noise);
475         precache_sound (self.noise1);
476         precache_sound (self.noise2);
477         precache_sound (self.noise3);
478         setsize(self, '-16 -16 -37', '16 16 37');
479         setorigin(self, self.origin + '0 0 37');
480         self.nextthink = time + 0.2; // start after doors etc
481         self.think = place_flag;
482
483         if(!self.scale)
484                 self.scale = 0.6;
485         //if(!self.glow_size)
486         //      self.glow_size = 50;
487
488         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
489         if(!self.noalign)
490                 droptofloor();
491
492         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37');
493 };
494
495 /*QUAKED item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
496 CTF flag for team two (Blue).
497 Multiple are allowed.
498
499 Keys:
500 "angle"
501  Angle the flag will point
502 (minus 90 degrees)
503
504 */
505
506 void() item_flag_team2 =
507 {
508         if (!g_ctf)
509                 return;
510         //if(!cvar("teamplay"))
511         //      cvar_set("teamplay", "3");
512
513         self.classname = "item_flag_team2";
514         self.team = COLOR_TEAM2; // color 13 team (blue)
515         self.items = IT_KEY1; // silver key (bluish enough)
516         self.netname = "^4BLUE^7 flag";
517         self.target = "###item###";
518         self.skin = 0;
519         if(self.spawnflags & 1)
520                 self.noalign = 1;
521         if (!self.model)
522                 self.model = "models/ctf/flag_blue.md3";
523         if (!self.noise)
524                 self.noise = "ctf/take.wav";
525         if (!self.noise1)
526                 self.noise1 = "ctf/return.wav";
527         if (!self.noise2)
528                 self.noise2 = "ctf/capture.wav";
529         if (!self.noise3)
530                 self.noise3 = "ctf/respawn.wav";
531         precache_model (self.model);
532         setmodel (self, self.model); // precision set below
533         precache_sound (self.noise);
534         precache_sound (self.noise1);
535         precache_sound (self.noise2);
536         precache_sound (self.noise3);
537         setsize(self, '-16 -16 -37', '16 16 37');
538         setorigin(self, self.origin + '0 0 37');
539         self.nextthink = time + 0.2; // start after doors etc
540         self.think = place_flag;
541
542         if(!self.scale)
543                 self.scale = 0.6;
544         //if(!self.glow_size)
545         //      self.glow_size = 50;
546
547         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
548         if(!self.noalign)
549                 droptofloor();
550
551         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37');
552 };
553
554
555 // spawnfunctions for q3 flags
556 void team_CTF_redflag()
557 {
558         item_flag_team1();
559 }
560
561 void team_CTF_blueflag()
562 {
563         item_flag_team2();
564 }
565
566
567 /*QUAKED ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
568 Team declaration for CTF gameplay, this allows you to decide what team
569 names and control point models are used in your map.
570
571 Note: If you use ctf_team entities you must define at least 2!  However, unlike
572 domination, you don't need to make a blank one too.
573
574 Keys:
575 "netname"
576  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
577 "cnt"
578  Scoreboard color of the team (for example 4 is red and 13 is blue)
579
580 */
581
582 void() ctf_team =
583 {
584         self.classname = "ctf_team";
585         self.team = self.cnt + 1;
586 };
587
588 // code from here on is just to support maps that don't have control point and team entities
589 void ctf_spawnteam (string teamname, float teamcolor)
590 {
591         local entity oldself;
592         oldself = self;
593         self = spawn();
594         self.classname = "ctf_team";
595         self.netname = teamname;
596         self.cnt = teamcolor;
597
598         ctf_team();
599
600         self = oldself;
601 };
602
603 // spawn some default teams if the map is not set up for ctf
604 void() ctf_spawnteams =
605 {
606         float numteams;
607
608         numteams = 2;//cvar("g_ctf_default_teams");
609
610         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
611         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
612 };
613
614 void() ctf_delayedinit =
615 {
616         self.think = SUB_Remove;
617         self.nextthink = time;
618         // if no teams are found, spawn defaults
619         if (find(world, classname, "ctf_team") == world)
620                 ctf_spawnteams();
621 };
622
623 void() ctf_init =
624 {
625         local entity e;
626         e = spawn();
627         e.think = ctf_delayedinit;
628         e.nextthink = time + 0.1;
629 };
630
631 void(entity flag) ctf_setstatus2 =
632 {
633         if (flag) {
634                 local float shift;
635                 if (flag.team == COLOR_TEAM1) shift = IT_RED_FLAG_TAKEN;
636                 else if (flag.team == COLOR_TEAM2) shift = IT_BLUE_FLAG_TAKEN;
637                 else shift = 0;
638
639                 local float status;
640                 if (flag.cnt == FLAG_CARRY)
641                         if (flag.owner == self) status = 3;
642                         else status = 1;
643                 else if (flag.cnt == FLAG_DROPPED) status = 2;
644                 else status = 0;
645
646                 self.items = self.items | (shift * status);
647         }
648 };
649
650 void() ctf_setstatus =
651 {
652         self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
653         self.items = self.items - (self.items & IT_RED_FLAG_LOST);
654         self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
655         self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
656
657         if (g_ctf) {
658                 local entity flag;
659                 flag = find(world, classname, "item_flag_team1");
660                 ctf_setstatus2(flag);
661                 flag = find(world, classname, "item_flag_team2");
662                 ctf_setstatus2(flag);
663         }
664 };