]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/ctf.c
New location escapes: %d - location of last death, %y: location of crosshair
[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         relocate_spawnpoint();
360 };
361 //self.team = 4;self.classname = "info_player_start";info_player_start();};
362
363 /*QUAKED info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
364 CTF Starting point for a player in
365 team two (Blue).
366
367 Keys:
368 "angle"
369  viewing angle when spawning
370 */
371 void() info_player_team2 =
372 {
373         self.classname = "info_player_deathmatch";
374         self.team = 14; // blue
375         relocate_spawnpoint();
376 };
377 //self.team = 13;self.classname = "info_player_start";info_player_start();};
378
379 /*QUAKED info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
380 CTF Starting point for a player in
381 team three (Magenta).
382
383 Keys:
384 "angle"
385  viewing angle when spawning
386 */
387 void() info_player_team3 =
388 {
389         self.classname = "info_player_deathmatch";
390         self.team = 10; // purple
391         relocate_spawnpoint();
392 };
393
394
395 /*QUAKED info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
396 CTF Starting point for a player in
397 team four (Yellow).
398
399 Keys:
400 "angle"
401  viewing angle when spawning
402 */
403 void() info_player_team4 =
404 {
405         self.classname = "info_player_deathmatch";
406         self.team = 13; // yellow
407         relocate_spawnpoint();
408 };
409
410
411
412
413 /*QUAKED item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
414 CTF flag for team one (Red).
415 Multiple are allowed.
416
417 Keys:
418 "angle"
419  Angle the flag will point
420 (minus 90 degrees)
421 "model"
422  model to use, note this needs red and blue as skins 0 and 1
423  (default models/ctf/flag.md3)
424 "noise"
425  sound played when flag is picked up
426  (default ctf/take.wav)
427 "noise1"
428  sound played when flag is returned by a teammate
429  (default ctf/return.wav)
430 "noise2"
431  sound played when flag is captured
432  (default ctf/capture.wav)
433 "noise3"
434  sound played when flag is lost in the field and respawns itself
435  (default ctf/respawn.wav)
436 */
437
438 void() item_flag_team1 =
439 {
440         if (!cvar("g_ctf"))
441                 return;
442         //if(!cvar("teamplay"))
443         //      cvar_set("teamplay", "3");
444
445         self.team = 5; // color 4 team (red)
446         self.items = IT_KEY2; // gold key (redish enough)
447         self.netname = "red base";
448         self.target = "###item###";
449         self.skin = 0;
450         if (!self.model)
451                 self.model = "models/ctf/flag_red.md3";
452         if (!self.noise)
453                 self.noise = "ctf/take.wav";
454         if (!self.noise1)
455                 self.noise1 = "ctf/return.wav";
456         if (!self.noise2)
457                 self.noise2 = "ctf/capture.wav";
458         if (!self.noise3)
459                 self.noise3 = "ctf/respawn.wav";
460         precache_model (self.model);
461         setmodel (self, self.model);
462         precache_sound (self.noise);
463         precache_sound (self.noise1);
464         precache_sound (self.noise2);
465         precache_sound (self.noise3);
466         setsize(self, '-16 -16 -37', '16 16 37');
467         setorigin(self, self.origin + '0 0 37');
468         self.nextthink = time + 0.2; // start after doors etc
469         self.think = place_flag;
470
471         if(!self.scale)
472                 self.scale = 0.6;
473         //if(!self.glow_size)
474         //      self.glow_size = 50;
475
476         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
477 };
478
479 /*QUAKED item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
480 CTF flag for team two (Blue).
481 Multiple are allowed.
482
483 Keys:
484 "angle"
485  Angle the flag will point
486 (minus 90 degrees)
487
488 */
489
490 void() item_flag_team2 =
491 {
492         if (!cvar("g_ctf"))
493                 return;
494         //if(!cvar("teamplay"))
495         //      cvar_set("teamplay", "3");
496
497         self.team = 14; // color 13 team (blue)
498         self.items = IT_KEY1; // silver key (bluish enough)
499         self.netname = "blue base";
500         self.target = "###item###";
501         self.skin = 0;
502         if (!self.model)
503                 self.model = "models/ctf/flag_blue.md3";
504         if (!self.noise)
505                 self.noise = "ctf/take.wav";
506         if (!self.noise1)
507                 self.noise1 = "ctf/return.wav";
508         if (!self.noise2)
509                 self.noise2 = "ctf/capture.wav";
510         if (!self.noise3)
511                 self.noise3 = "ctf/respawn.wav";
512         precache_model (self.model);
513         setmodel (self, self.model);
514         precache_sound (self.noise);
515         precache_sound (self.noise1);
516         precache_sound (self.noise2);
517         precache_sound (self.noise3);
518         setsize(self, '-16 -16 -37', '16 16 37');
519         setorigin(self, self.origin + '0 0 37');
520         self.nextthink = time + 0.2; // start after doors etc
521         self.think = place_flag;
522
523         if(!self.scale)
524                 self.scale = 0.6;
525         //if(!self.glow_size)
526         //      self.glow_size = 50;
527
528         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
529 };
530
531
532 /*QUAKED ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
533 Team declaration for CTF gameplay, this allows you to decide what team
534 names and control point models are used in your map.
535
536 Note: If you use ctf_team entities you must define at least 2!  However, unlike
537 domination, you don't need to make a blank one too.
538
539 Keys:
540 "netname"
541  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
542 "cnt"
543  Scoreboard color of the team (for example 4 is red and 13 is blue)
544
545 */
546
547 void() ctf_team =
548 {
549         self.classname = "ctf_team";
550         self.team = self.cnt + 1;
551 };
552
553 // code from here on is just to support maps that don't have control point and team entities
554 void ctf_spawnteam (string teamname, float teamcolor)
555 {
556         local entity oldself;
557         oldself = self;
558         self = spawn();
559         self.classname = "ctf_team";
560         self.netname = teamname;
561         self.cnt = teamcolor;
562
563         ctf_team();
564
565         self = oldself;
566 };
567
568 // spawn some default teams if the map is not set up for ctf
569 void() ctf_spawnteams =
570 {
571         float numteams;
572
573         numteams = 2;//cvar("g_ctf_default_teams");
574
575         ctf_spawnteam("Red", 4);
576         ctf_spawnteam("Blue", 13);
577 };
578
579 void() ctf_delayedinit =
580 {
581         self.think = SUB_Remove;
582         self.nextthink = time;
583         // if no teams are found, spawn defaults
584         if (find(world, classname, "ctf_team") == world)
585                 ctf_spawnteams();
586 };
587
588 void() ctf_init =
589 {
590         local entity e;
591         e = spawn();
592         e.think = ctf_delayedinit;
593         e.nextthink = time + 0.1;
594 };
595