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