centerprints now are managed independently per line
[divverent/nexuiz.git] / data / 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 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.flagcarried.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                 // give credit to the individual player
241                 other.frags = other.frags + cvar("g_ctf_flagscore_capture");//FLAGSCORE_CAPTURE;
242                 // give credit to all players of the team (rewards large teams)
243                 // NOTE: this defaults to 0
244                 head = find(head, classname, "player");
245                 while (head)
246                 {
247                         if (head.team == self.team)
248                                 head.frags = head.frags + cvar("g_ctf_flagscore_capture_team");//FLAGSCORE_CAPTURE_TEAM;
249                         head = find(head, classname, "player");
250                 }
251                 sound (self, CHAN_AUTO, self.noise2, 1, ATTN_NONE);
252                 RegenFlag (other.flagcarried);
253                 other.flagcarried = world;
254                 other.next_take_time = time + 1;
255         }
256         if (self.cnt == FLAG_BASE)
257         if (other.team == 5 || other.team == 14) // only red and blue team can steal flags
258         if (other.team != self.team)
259         if (!other.flagcarried)
260         {
261                 if (other.next_take_time > time)
262                         return;
263                 // pick up
264                 self.flagpickuptime = time; // used for timing runs
265                 self.solid = SOLID_NOT;
266                 setorigin(self, self.origin); // relink
267                 self.owner = other;
268                 other.flagcarried = self;
269                 self.cnt = FLAG_CARRY;
270                 bprint(other.netname, "^7 got the ", self.netname, "\n");
271                 other.frags = other.frags + cvar("g_ctf_flagscore_pickup");//FLAGSCORE_PICKUP;
272                 LogCTF("steal", self.team, other);
273                 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
274
275                 player = find(world, classname, "player");
276                 while(player) {
277                         if(player.team == self.team) centermsg_setfor(player, CENTERMSG_CTF, "The enemy got your flag! Retrieve it!");
278                         player = find(player, classname, "player");
279                 }
280
281                 return;
282         }
283
284         if (self.cnt == FLAG_DROPPED)
285         {
286                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
287                 if (other.team == self.team || (other.team != 5 && other.team != 14))
288                 {
289                         // return flag
290                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
291                         if (other.team == 5 || other.team == 14)
292                                 other.frags = other.frags + cvar("g_ctf_flagscore_return");//FLAGSCORE_RETURN;
293                         else
294                                 other.frags = other.frags + cvar("g_ctf_flagscore_return_rogue");//FLAGSCORE_RETURNROGUE;
295                         LogCTF("return", self.team, other);
296                         sound (self, CHAN_AUTO, self.noise1, 1, ATTN_NONE);
297                         ReturnFlag(self);
298                 }
299                 else if (!other.flagcarried)
300                 {
301                         // pick up
302                         self.solid = SOLID_NOT;
303                         setorigin(self, self.origin); // relink
304                         self.owner = other;
305                         other.flagcarried = self;
306                         self.cnt = FLAG_CARRY;
307                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
308                         other.frags = other.frags + cvar("g_ctf_flagscore_pickup");//FLAGSCORE_PICKUP;
309                         LogCTF("pickup", self.team, other);
310                         sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
311
312                         player = find(world, classname, "player");
313                         while(player) {
314                                 if(player.team == self.team) centermsg_setfor(player, CENTERMSG_CTF, "The enemy got your flag! Retrieve it!");
315                                 player = find(player, classname, "player");
316                         }
317                 }
318         }
319 };
320
321 /*QUAKED info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
322 CTF Starting point for a player
323 in team one (Red).
324
325 Keys:
326 "angle"
327  viewing angle when spawning
328 */
329 void() info_player_team1 =
330 {
331         self.classname = "info_player_deathmatch";
332         self.team = 5; // red
333         relocate_spawnpoint();
334 };
335 //self.team = 4;self.classname = "info_player_start";info_player_start();};
336
337 /*QUAKED info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
338 CTF Starting point for a player in
339 team two (Blue).
340
341 Keys:
342 "angle"
343  viewing angle when spawning
344 */
345 void() info_player_team2 =
346 {
347         self.classname = "info_player_deathmatch";
348         self.team = 14; // blue
349         relocate_spawnpoint();
350 };
351 //self.team = 13;self.classname = "info_player_start";info_player_start();};
352
353 /*QUAKED info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
354 CTF Starting point for a player in
355 team three (Magenta).
356
357 Keys:
358 "angle"
359  viewing angle when spawning
360 */
361 void() info_player_team3 =
362 {
363         self.classname = "info_player_deathmatch";
364         self.team = 10; // purple
365         relocate_spawnpoint();
366 };
367
368
369 /*QUAKED info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
370 CTF Starting point for a player in
371 team four (Yellow).
372
373 Keys:
374 "angle"
375  viewing angle when spawning
376 */
377 void() info_player_team4 =
378 {
379         self.classname = "info_player_deathmatch";
380         self.team = 13; // yellow
381         relocate_spawnpoint();
382 };
383
384
385
386
387 /*QUAKED item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
388 CTF flag for team one (Red).
389 Multiple are allowed.
390
391 Keys:
392 "angle"
393  Angle the flag will point
394 (minus 90 degrees)
395 "model"
396  model to use, note this needs red and blue as skins 0 and 1
397  (default models/ctf/flag.md3)
398 "noise"
399  sound played when flag is picked up
400  (default ctf/take.wav)
401 "noise1"
402  sound played when flag is returned by a teammate
403  (default ctf/return.wav)
404 "noise2"
405  sound played when flag is captured
406  (default ctf/capture.wav)
407 "noise3"
408  sound played when flag is lost in the field and respawns itself
409  (default ctf/respawn.wav)
410 */
411
412 void() item_flag_team1 =
413 {
414         if (!cvar("g_ctf"))
415                 return;
416         //if(!cvar("teamplay"))
417         //      cvar_set("teamplay", "3");
418
419         self.team = 5; // color 4 team (red)
420         self.items = IT_KEY2; // gold key (redish enough)
421         self.netname = "^1RED^7 flag";
422         self.target = "###item###";
423         self.skin = 0;
424         if (!self.model)
425                 self.model = "models/ctf/flag_red.md3";
426         if (!self.noise)
427                 self.noise = "ctf/take.wav";
428         if (!self.noise1)
429                 self.noise1 = "ctf/return.wav";
430         if (!self.noise2)
431                 self.noise2 = "ctf/capture.wav";
432         if (!self.noise3)
433                 self.noise3 = "ctf/respawn.wav";
434         precache_model (self.model);
435         setmodel (self, self.model);
436         precache_sound (self.noise);
437         precache_sound (self.noise1);
438         precache_sound (self.noise2);
439         precache_sound (self.noise3);
440         setsize(self, '-16 -16 -37', '16 16 37');
441         setorigin(self, self.origin + '0 0 37');
442         self.nextthink = time + 0.2; // start after doors etc
443         self.think = place_flag;
444
445         if(!self.scale)
446                 self.scale = 0.6;
447         //if(!self.glow_size)
448         //      self.glow_size = 50;
449
450         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
451 };
452
453 /*QUAKED item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
454 CTF flag for team two (Blue).
455 Multiple are allowed.
456
457 Keys:
458 "angle"
459  Angle the flag will point
460 (minus 90 degrees)
461
462 */
463
464 void() item_flag_team2 =
465 {
466         if (!cvar("g_ctf"))
467                 return;
468         //if(!cvar("teamplay"))
469         //      cvar_set("teamplay", "3");
470
471         self.team = 14; // color 13 team (blue)
472         self.items = IT_KEY1; // silver key (bluish enough)
473         self.netname = "^4BLUE^7 flag";
474         self.target = "###item###";
475         self.skin = 0;
476         if (!self.model)
477                 self.model = "models/ctf/flag_blue.md3";
478         if (!self.noise)
479                 self.noise = "ctf/take.wav";
480         if (!self.noise1)
481                 self.noise1 = "ctf/return.wav";
482         if (!self.noise2)
483                 self.noise2 = "ctf/capture.wav";
484         if (!self.noise3)
485                 self.noise3 = "ctf/respawn.wav";
486         precache_model (self.model);
487         setmodel (self, self.model);
488         precache_sound (self.noise);
489         precache_sound (self.noise1);
490         precache_sound (self.noise2);
491         precache_sound (self.noise3);
492         setsize(self, '-16 -16 -37', '16 16 37');
493         setorigin(self, self.origin + '0 0 37');
494         self.nextthink = time + 0.2; // start after doors etc
495         self.think = place_flag;
496
497         if(!self.scale)
498                 self.scale = 0.6;
499         //if(!self.glow_size)
500         //      self.glow_size = 50;
501
502         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
503 };
504
505
506 /*QUAKED ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
507 Team declaration for CTF gameplay, this allows you to decide what team
508 names and control point models are used in your map.
509
510 Note: If you use ctf_team entities you must define at least 2!  However, unlike
511 domination, you don't need to make a blank one too.
512
513 Keys:
514 "netname"
515  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
516 "cnt"
517  Scoreboard color of the team (for example 4 is red and 13 is blue)
518
519 */
520
521 void() ctf_team =
522 {
523         self.classname = "ctf_team";
524         self.team = self.cnt + 1;
525 };
526
527 // code from here on is just to support maps that don't have control point and team entities
528 void ctf_spawnteam (string teamname, float teamcolor)
529 {
530         local entity oldself;
531         oldself = self;
532         self = spawn();
533         self.classname = "ctf_team";
534         self.netname = teamname;
535         self.cnt = teamcolor;
536
537         ctf_team();
538
539         self = oldself;
540 };
541
542 // spawn some default teams if the map is not set up for ctf
543 void() ctf_spawnteams =
544 {
545         float numteams;
546
547         numteams = 2;//cvar("g_ctf_default_teams");
548
549         ctf_spawnteam("Red", 4);
550         ctf_spawnteam("Blue", 13);
551 };
552
553 void() ctf_delayedinit =
554 {
555         self.think = SUB_Remove;
556         self.nextthink = time;
557         // if no teams are found, spawn defaults
558         if (find(world, classname, "ctf_team") == world)
559                 ctf_spawnteams();
560 };
561
562 void() ctf_init =
563 {
564         local entity e;
565         e = spawn();
566         e.think = ctf_delayedinit;
567         e.nextthink = time + 0.1;
568 };
569