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