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