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