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