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