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