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