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