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