]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ctf.qc
revert that CTF change. Instead, don't load/save the DB _at all_ with sv_cheats.
[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                 h0 = db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"));
252                 if(h0 == other.netname)
253                         h0 = "his";
254                 else
255                         h0 = strcat(h0, "^7's"); 
256                 if (flagcaptimerecord == 0)
257                 {
258                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, " seconds\n");
259                         flagcaptimerecord = t;
260                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
261                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), other.netname);
262                 }
263                 else if (t < flagcaptimerecord)
264                 {
265                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", breaking ", strcat(h0, " previous record of ", s0, " seconds\n"));
266                         flagcaptimerecord = t;
267                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time"), ftos(t));
268                         db_put(ServerProgsDB, strcat(GetMapname(), "/captimerecord/netname"), other.netname);
269                 }
270                 else
271                 {
272                         bprint(other.netname, "^7 captured the ", other.flagcarried.netname, " in ", s, ", failing to break ", strcat(h0, " record of ", s0, " seconds\n"));
273                 }
274
275                 LogCTF("capture", other.flagcarried.team, other);
276                 // give credit to the individual player
277                 UpdateFrags(other, cvar("g_ctf_flagscore_capture"));
278
279                 // give credit to all players of the team (rewards large teams)
280                 // NOTE: this defaults to 0
281                 FOR_EACH_PLAYER(head)
282                         if (head.team == self.team)
283                                 UpdateFrags(head, cvar("g_ctf_flagscore_capture_team"));
284
285                 sound (self, CHAN_AUTO, self.noise2, 1, ATTN_NONE);
286                 WaypointSprite_DetachCarrier(other);
287                 if(self.speedrunning)
288                         FakeTimeLimit(other, -1);
289                 RegenFlag (other.flagcarried);
290                 other.flagcarried = world;
291                 other.next_take_time = time + 1;
292         }
293         if (self.cnt == FLAG_BASE)
294         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2) // only red and blue team can steal flags
295         if (other.team != self.team)
296         if (!other.flagcarried)
297         {
298                 if (other.next_take_time > time)
299                         return;
300                 // pick up
301                 self.flagpickuptime = time; // used for timing runs
302                 self.speedrunning = other.speedrunning; // if speedrunning, flag will self-return and teleport the owner back after the record
303                 if(other.speedrunning)
304                 if(flagcaptimerecord)
305                         FakeTimeLimit(other, time + flagcaptimerecord);
306                 self.solid = SOLID_NOT;
307                 setorigin(self, self.origin); // relink
308                 self.owner = other;
309                 other.flagcarried = self;
310                 self.cnt = FLAG_CARRY;
311                 self.angles = '0 0 0';
312                 bprint(other.netname, "^7 got the ", self.netname, "\n");
313                 UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
314                 LogCTF("steal", self.team, other);
315                 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
316
317                 FOR_EACH_PLAYER(player)
318                         if(player.team == self.team)
319                                 centerprint(player, "The enemy got your flag! Retrieve it!");
320
321                 self.movetype = MOVETYPE_NONE;
322                 setorigin(self, FLAG_CARRY_POS);
323                 setattachment(self, other, "");
324                 WaypointSprite_AttachCarrier("flagcarrier", other);
325
326                 return;
327         }
328
329         if (self.cnt == FLAG_DROPPED)
330         {
331                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
332                 if (other.team == self.team || (other.team != COLOR_TEAM1 && other.team != COLOR_TEAM2))
333                 {
334                         // return flag
335                         bprint(other.netname, "^7 returned the ", self.netname, "\n");
336                         if (other.team == COLOR_TEAM1 || other.team == COLOR_TEAM2)
337                                 UpdateFrags(other, cvar("g_ctf_flagscore_return"));
338                         else
339                                 UpdateFrags(other, cvar("g_ctf_flagscore_return_rogue"));
340                         LogCTF("return", self.team, other);
341                         sound (self, CHAN_AUTO, self.noise1, 1, ATTN_NONE);
342                         ReturnFlag(self);
343                 }
344                 else if (!other.flagcarried)
345                 {
346                         // pick up
347                         self.solid = SOLID_NOT;
348                         setorigin(self, self.origin); // relink
349                         self.owner = other;
350                         other.flagcarried = self;
351                         self.cnt = FLAG_CARRY;
352                         bprint(other.netname, "^7 picked up the ", self.netname, "\n");
353                         UpdateFrags(other, cvar("g_ctf_flagscore_pickup"));
354                         LogCTF("pickup", self.team, other);
355                         sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
356
357                         FOR_EACH_PLAYER(player)
358                                 if(player.team == self.team)
359                                         centerprint(player, "The enemy got your flag! Retrieve it!");
360
361                         self.movetype = MOVETYPE_NONE;  // flag must have MOVETYPE_NONE here, otherwise it will drop through the floor...
362                         setorigin(self, FLAG_CARRY_POS);
363                         setattachment(self, other, "");
364                         WaypointSprite_AttachCarrier("flagcarrier", other);
365                 }
366         }
367 };
368
369 /*QUAKED info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
370 CTF Starting point for a player
371 in team one (Red).
372
373 Keys:
374 "angle"
375  viewing angle when spawning
376 */
377 void() info_player_team1 =
378 {
379         self.team = COLOR_TEAM1; // red
380         info_player_deathmatch();
381 };
382 //self.team = 4;self.classname = "info_player_start";info_player_start();};
383
384 /*QUAKED info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
385 CTF Starting point for a player in
386 team two (Blue).
387
388 Keys:
389 "angle"
390  viewing angle when spawning
391 */
392 void() info_player_team2 =
393 {
394         self.team = COLOR_TEAM2; // blue
395         info_player_deathmatch();
396 };
397 //self.team = 13;self.classname = "info_player_start";info_player_start();};
398
399 /*QUAKED info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
400 CTF Starting point for a player in
401 team three (Magenta).
402
403 Keys:
404 "angle"
405  viewing angle when spawning
406 */
407 void() info_player_team3 =
408 {
409         self.team = COLOR_TEAM3; // purple
410         info_player_deathmatch();
411 };
412
413
414 /*QUAKED info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
415 CTF Starting point for a player in
416 team four (Yellow).
417
418 Keys:
419 "angle"
420  viewing angle when spawning
421 */
422 void() info_player_team4 =
423 {
424         self.team = COLOR_TEAM4; // yellow
425         info_player_deathmatch();
426 };
427
428
429
430
431 /*QUAKED item_flag_team1 (0 0.5 0.8) (-48 -48 -37) (48 48 37)
432 CTF flag for team one (Red).
433 Multiple are allowed.
434
435 Keys:
436 "angle"
437  Angle the flag will point
438 (minus 90 degrees)
439 "model"
440  model to use, note this needs red and blue as skins 0 and 1
441  (default models/ctf/flag.md3)
442 "noise"
443  sound played when flag is picked up
444  (default ctf/take.wav)
445 "noise1"
446  sound played when flag is returned by a teammate
447  (default ctf/return.wav)
448 "noise2"
449  sound played when flag is captured
450  (default ctf/capture.wav)
451 "noise3"
452  sound played when flag is lost in the field and respawns itself
453  (default ctf/respawn.wav)
454 */
455
456 void() item_flag_team1 =
457 {
458         if (!g_ctf)
459                 return;
460         //if(!cvar("teamplay"))
461         //      cvar_set("teamplay", "3");
462
463         self.classname = "item_flag_team1";
464         self.team = COLOR_TEAM1; // color 4 team (red)
465         self.items = IT_KEY2; // gold key (redish enough)
466         self.netname = "^1RED^7 flag";
467         self.target = "###item###";
468         self.skin = 0;
469         if(self.spawnflags & 1)
470                 self.noalign = 1;
471         if (!self.model)
472                 self.model = "models/ctf/flag_red.md3";
473         if (!self.noise)
474                 self.noise = "ctf/take.wav";
475         if (!self.noise1)
476                 self.noise1 = "ctf/return.wav";
477         if (!self.noise2)
478                 self.noise2 = "ctf/capture.wav";
479         if (!self.noise3)
480                 self.noise3 = "ctf/respawn.wav";
481         precache_model (self.model);
482         setmodel (self, self.model); // precision set below
483         precache_sound (self.noise);
484         precache_sound (self.noise1);
485         precache_sound (self.noise2);
486         precache_sound (self.noise3);
487         setsize(self, '-16 -16 -37', '16 16 37');
488         setorigin(self, self.origin + '0 0 37');
489         self.nextthink = time + 0.2; // start after doors etc
490         self.think = place_flag;
491
492         if(!self.scale)
493                 self.scale = 0.6;
494         //if(!self.glow_size)
495         //      self.glow_size = 50;
496
497         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
498         if(!self.noalign)
499                 droptofloor();
500
501         WaypointSprite_SpawnFixed("redbase", self.origin + '0 0 37', self, enemy);
502 };
503
504 /*QUAKED item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
505 CTF flag for team two (Blue).
506 Multiple are allowed.
507
508 Keys:
509 "angle"
510  Angle the flag will point
511 (minus 90 degrees)
512
513 */
514
515 void() item_flag_team2 =
516 {
517         if (!g_ctf)
518                 return;
519         //if(!cvar("teamplay"))
520         //      cvar_set("teamplay", "3");
521
522         self.classname = "item_flag_team2";
523         self.team = COLOR_TEAM2; // color 13 team (blue)
524         self.items = IT_KEY1; // silver key (bluish enough)
525         self.netname = "^4BLUE^7 flag";
526         self.target = "###item###";
527         self.skin = 0;
528         if(self.spawnflags & 1)
529                 self.noalign = 1;
530         if (!self.model)
531                 self.model = "models/ctf/flag_blue.md3";
532         if (!self.noise)
533                 self.noise = "ctf/take.wav";
534         if (!self.noise1)
535                 self.noise1 = "ctf/return.wav";
536         if (!self.noise2)
537                 self.noise2 = "ctf/capture.wav";
538         if (!self.noise3)
539                 self.noise3 = "ctf/respawn.wav";
540         precache_model (self.model);
541         setmodel (self, self.model); // precision set below
542         precache_sound (self.noise);
543         precache_sound (self.noise1);
544         precache_sound (self.noise2);
545         precache_sound (self.noise3);
546         setsize(self, '-16 -16 -37', '16 16 37');
547         setorigin(self, self.origin + '0 0 37');
548         self.nextthink = time + 0.2; // start after doors etc
549         self.think = place_flag;
550
551         if(!self.scale)
552                 self.scale = 0.6;
553         //if(!self.glow_size)
554         //      self.glow_size = 50;
555
556         self.effects = self.effects | EF_FULLBRIGHT | EF_LOWPRECISION;
557         if(!self.noalign)
558                 droptofloor();
559
560         WaypointSprite_SpawnFixed("bluebase", self.origin + '0 0 37', self, enemy);
561 };
562
563
564 /*QUAKED ctf_team (0 .5 .8) (-16 -16 -24) (16 16 32)
565 Team declaration for CTF gameplay, this allows you to decide what team
566 names and control point models are used in your map.
567
568 Note: If you use ctf_team entities you must define at least 2!  However, unlike
569 domination, you don't need to make a blank one too.
570
571 Keys:
572 "netname"
573  Name of the team (for example Red, Blue, Green, Yellow, Life, Death, Offense, Defense, etc)
574 "cnt"
575  Scoreboard color of the team (for example 4 is red and 13 is blue)
576
577 */
578
579 void() ctf_team =
580 {
581         self.classname = "ctf_team";
582         self.team = self.cnt + 1;
583 };
584
585 // code from here on is just to support maps that don't have control point and team entities
586 void ctf_spawnteam (string teamname, float teamcolor)
587 {
588         local entity oldself;
589         oldself = self;
590         self = spawn();
591         self.classname = "ctf_team";
592         self.netname = teamname;
593         self.cnt = teamcolor;
594
595         ctf_team();
596
597         self = oldself;
598 };
599
600 // spawn some default teams if the map is not set up for ctf
601 void() ctf_spawnteams =
602 {
603         float numteams;
604
605         numteams = 2;//cvar("g_ctf_default_teams");
606
607         ctf_spawnteam("Red", COLOR_TEAM1 - 1);
608         ctf_spawnteam("Blue", COLOR_TEAM2 - 1);
609 };
610
611 void() ctf_delayedinit =
612 {
613         self.think = SUB_Remove;
614         self.nextthink = time;
615         // if no teams are found, spawn defaults
616         if (find(world, classname, "ctf_team") == world)
617                 ctf_spawnteams();
618 };
619
620 void() ctf_init =
621 {
622         local entity e;
623         e = spawn();
624         e.think = ctf_delayedinit;
625         e.nextthink = time + 0.1;
626         flagcaptimerecord = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/captimerecord/time")));
627 };
628
629 void(entity flag) ctf_setstatus2 =
630 {
631         if (flag) {
632                 local float shift;
633                 if (flag.team == COLOR_TEAM1) shift = IT_RED_FLAG_TAKEN;
634                 else if (flag.team == COLOR_TEAM2) shift = IT_BLUE_FLAG_TAKEN;
635                 else shift = 0;
636
637                 local float status;
638                 if (flag.cnt == FLAG_CARRY)
639                         if (flag.owner == self) status = 3;
640                         else status = 1;
641                 else if (flag.cnt == FLAG_DROPPED) status = 2;
642                 else status = 0;
643
644                 self.items = self.items | (shift * status);
645         }
646 };
647
648 void() ctf_setstatus =
649 {
650         self.items = self.items - (self.items & IT_RED_FLAG_TAKEN);
651         self.items = self.items - (self.items & IT_RED_FLAG_LOST);
652         self.items = self.items - (self.items & IT_BLUE_FLAG_TAKEN);
653         self.items = self.items - (self.items & IT_BLUE_FLAG_LOST);
654
655         if (g_ctf) {
656                 local entity flag;
657                 flag = find(world, classname, "item_flag_team1");
658                 ctf_setstatus2(flag);
659                 flag = find(world, classname, "item_flag_team2");
660                 ctf_setstatus2(flag);
661         }
662 };