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