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