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