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