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