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