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