]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
make use of EF_TELEPORT_BIT for weapon animation
[divverent/nexuiz.git] / data / qcsrc / server / race.qc
1 #define MAX_CHECKPOINTS 255
2
3 .float race_checkpoint; // player: next checkpoint that has to be reached
4 .float race_laptime;
5
6 .entity sprite;
7
8 float race_checkpoint_records[MAX_CHECKPOINTS];
9 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
10 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
11 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
12 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
13
14 float race_highest_checkpoint;
15
16 float race_NextCheckpoint(float f)
17 {
18         if(f >= race_highest_checkpoint)
19                 return 0;
20         else
21                 return f + 1;
22 }
23
24 float race_PreviousCheckpoint(float f)
25 {
26         if(f == -1)
27                 return 0;
28         else if(f == 0)
29                 return race_highest_checkpoint;
30         else
31                 return f - 1;
32 }
33
34 void race_SendNextCheckpoint(entity e, float spec)
35 {
36         float recordtime;
37         string recordholder;
38         float cp;
39
40         if(!e.race_laptime)
41                 return;
42
43         cp = e.race_checkpoint;
44         recordtime = race_checkpoint_records[cp];
45         recordholder = race_checkpoint_recordholders[cp];
46         /*
47         recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
48         recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
49         */
50         if(recordholder == e.netname)
51                 recordholder = "";
52
53         if(!spec)
54                 msg_entity = e;
55         WRITESPECTATABLE_MSG_ONE({
56                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
57                 WriteByte(MSG_ONE, TE_CSQC_RACE);
58                 if(spec)
59                 {
60                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
61                         WriteCoord(MSG_ONE, e.race_laptime);
62                 }
63                 else
64                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
65                 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
66                 WriteShort(MSG_ONE, recordtime);
67                 WriteString(MSG_ONE, recordholder);
68         });
69 }
70
71 void race_InitSpectator()
72 {
73         if(g_race_qualifying)
74                 if(msg_entity.enemy.race_laptime)
75                         race_SendNextCheckpoint(msg_entity.enemy, 1);
76 }
77
78 void race_SendTime(entity e, float cp, float t, float tvalid)
79 {
80         float snew, l;
81         entity p;
82
83         t = floor(0.4 + 10 * t); // make integer
84         // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
85
86         if(tvalid)
87         if(cp == 0) // finish line
88         if not(e.race_completed)
89         {
90                 float s;
91                 if(g_race_qualifying)
92                 {
93                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
94                         if(!s || t < s)
95                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
96                 }
97                 else
98                 {
99                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
100                         snew = floor(0.5 + 10 * (time - game_starttime));
101                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
102                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
103
104                         if(cvar("fraglimit"))
105                                 if(l >= cvar("fraglimit"))
106                                 {
107                                         race_completing = 1;
108                                         FOR_EACH_PLAYER(p)
109                                                 if(p.deadflag != DEAD_NO)
110                                                         race_AbandonRaceCheck(p);
111                                 }
112
113                         if(race_completing)
114                         {
115                                 e.race_completed = 1;
116                                 MAKE_INDEPENDENT_PLAYER(e);
117                                 bprint(e.netname, "^7 has finished the race.\n");
118                                 ClientData_Touch(e);
119                         }
120                 }
121         }
122
123         float recordtime;
124         string recordholder;
125
126         if(g_race_qualifying)
127         {
128                 if(tvalid)
129                 {
130                         recordtime = race_checkpoint_records[cp];
131                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
132                         if(recordholder == e.netname)
133                                 recordholder = "";
134
135                         if(t != 0)
136                         if(t < recordtime || recordtime == 0)
137                         {
138                                 race_checkpoint_records[cp] = t;
139                                 if(race_checkpoint_recordholders[cp])
140                                         strunzone(race_checkpoint_recordholders[cp]);
141                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
142                                 if(cp == 0)
143                                 {
144                                         float grecordtime;
145                                         string grecordholder;
146                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
147                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
148                                         if(grecordholder == e.netname)
149                                                 grecordholder = "";
150                                         if(grecordtime == 0)
151                                         {
152                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
153                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
154                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
155                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
156                                                 stuffcmd(e, strcat("//RACE RECORD SET ", mmsss(t), "\n"));
157                                         }
158                                         else if(t < grecordtime)
159                                         {
160                                                 if(grecordholder == "")
161                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
162                                                 else
163                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
164                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
165                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
166                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
167                                                 stuffcmd(e, strcat("//RACE RECORD SET ", mmsss(t), "\n"));
168                                         }
169                                         else
170                                         {
171                                                 if(grecordholder == "")
172                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
173                                                 else
174                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
175                                         }
176                                 }
177
178                                 if(g_race_qualifying)
179                                 {
180                                         FOR_EACH_REALPLAYER(p)
181                                                 if(p.race_checkpoint == cp)
182                                                         race_SendNextCheckpoint(p, 0);
183                                 }
184                         }
185                 }
186                 else
187                 {
188                         // dummies
189                         t = 0;
190                         recordtime = 0;
191                         recordholder = "";
192                 }
193
194                 msg_entity = e;
195                 if(g_race_qualifying)
196                 {
197                         WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
198                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
199                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
200                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
201                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
202                                 WriteShort(MSG_ONE, t); // time to that intermediate
203                                 WriteShort(MSG_ONE, recordtime); // previously best time
204                                 WriteString(MSG_ONE, recordholder); // record holder
205                         });
206                 }
207         }
208         else // RACE! Not Qualifying
209         {
210                 float lself, lother, othtime;
211                 entity oth;
212                 oth = race_checkpoint_lastplayers[cp];
213                 if(oth)
214                 {
215                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
216                         lother = race_checkpoint_lastlaps[cp];
217                         othtime = race_checkpoint_lasttimes[cp];
218                 }
219                 else
220                         lself = lother = othtime = 0;
221
222                 msg_entity = e;
223                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
224                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
225                         WriteByte(MSG_ONE, TE_CSQC_RACE);
226                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
227                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
228                         if(e == oth)
229                         {
230                                 WriteShort(MSG_ONE, 0);
231                                 WriteByte(MSG_ONE, 0);
232                                 WriteString(MSG_ONE, "");
233                         }
234                         else
235                         {
236                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
237                                 WriteByte(MSG_ONE, lself - lother);
238                                 WriteString(MSG_ONE, oth.netname); // record holder
239                         }
240                 });
241
242                 race_checkpoint_lastplayers[cp] = e;
243                 race_checkpoint_lasttimes[cp] = time;
244                 race_checkpoint_lastlaps[cp] = lself;
245
246                 msg_entity = oth;
247                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
248                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
249                         WriteByte(MSG_ONE, TE_CSQC_RACE);
250                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
251                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
252                         if(e == oth)
253                         {
254                                 WriteShort(MSG_ONE, 0);
255                                 WriteByte(MSG_ONE, 0);
256                                 WriteString(MSG_ONE, "");
257                         }
258                         else
259                         {
260                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
261                                 WriteByte(MSG_ONE, lother - lself);
262                                 WriteString(MSG_ONE, e.netname); // record holder
263                         }
264                 });
265         }
266 }
267
268 void race_ClearTime(entity e)
269 {
270         e.race_checkpoint = -1;
271         e.race_laptime = 0;
272
273         msg_entity = e;
274         WRITESPECTATABLE_MSG_ONE({
275                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
276                 WriteByte(MSG_ONE, TE_CSQC_RACE);
277                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
278         });
279 }
280
281 void dumpsurface(entity e)
282 {
283         float n, si, ni;
284         vector norm, vec;
285         print("Surfaces of ", etos(e), ":\n");
286
287         print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
288
289         for(si = 0; ; ++si)
290         {
291                 n = getsurfacenumpoints(e, si);
292                 if(n <= 0)
293                         break;
294                 print("  Surface ", ftos(si), ":\n");
295                 norm = getsurfacenormal(e, si);
296                 print("    Normal = ", vtos(norm), "\n");
297                 for(ni = 0; ni < n; ++ni)
298                 {
299                         vec = getsurfacepoint(e, si, ni);
300                         print("    Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
301                 }
302         }
303 }
304
305 void checkpoint_passed()
306 {
307         string oldmsg;
308
309         if(other.classname == "porto")
310         {
311                 // do not allow portalling through checkpoints
312                 trace_plane_normal = normalize(-1 * other.velocity);
313                 self = other;
314                 W_Porto_Fail(0);
315                 return;
316         }
317
318         /*
319          * Trigger targets
320          */
321         if not(self.spawnflags & 2)
322         {
323                 activator = other;
324                 oldmsg = self.message;
325                 self.message = "";
326                 SUB_UseTargets();
327                 self.message = oldmsg;
328         }
329
330         if(other.classname != "player")
331                 return;
332
333         /*
334          * Remove unauthorized equipment
335          */
336         Portal_ClearAll(other);
337
338         other.porto_forbidden = 2; // decreased by 1 each StartFrame
339
340         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
341         {
342                 /*
343                  * Trigger targets
344                  */
345                 if(self.spawnflags & 2)
346                 {
347                         activator = other;
348                         oldmsg = self.message;
349                         self.message = "";
350                         SUB_UseTargets();
351                         self.message = oldmsg;
352                 }
353
354                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
355
356                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
357
358                 if(!self.race_checkpoint) // finish line
359                         other.race_laptime = time;
360
361                 if(g_race_qualifying)
362                         race_SendNextCheckpoint(other, 0);
363         }
364         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
365         {
366                 // ignored
367         }
368         else
369         {
370                 if(self.spawnflags & 4)
371                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
372         }
373 }
374
375 void checkpoint_touch()
376 {
377         EXACTTRIGGER_TOUCH;
378         checkpoint_passed();
379 }
380
381 void checkpoint_use()
382 {
383         if(other.classname == "info_player_deathmatch") // a spawn, a spawn
384                 return;
385
386         other = activator;
387         checkpoint_passed();
388 }
389
390 float race_waypointsprite_visible_for_player(entity e)
391 {
392         if(e.race_checkpoint == -1)
393                 return TRUE;
394         else if(e.race_checkpoint == self.owner.race_checkpoint)
395                 return TRUE;
396         else
397                 return FALSE;
398 }
399
400 float have_verified;
401 void trigger_race_checkpoint_verify()
402 {
403         entity oldself;
404         float i, p;
405         float qual;
406
407         if(have_verified)
408                 return;
409         have_verified = 1;
410         
411         qual = g_race_qualifying;
412
413         oldself = self;
414         self = spawn();
415         self.classname = "player";
416
417         for(i = 0; i <= race_highest_checkpoint; ++i)
418         {
419                 self.race_checkpoint = race_NextCheckpoint(i);
420
421                 // race only (middle of the race)
422                 g_race_qualifying = 0;
423                 self.race_place = 0;
424                 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
425                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
426
427                 if(i == 0)
428                 {
429                         // qualifying only
430                         g_race_qualifying = 1;
431                         self.race_place = race_lowest_place_spawn;
432                         if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
433                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
434                         
435                         // race only (initial spawn)
436                         g_race_qualifying = 0;
437                         for(p = 1; p <= race_highest_place_spawn; ++p)
438                         {
439                                 self.race_place = p;
440                                 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
441                                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
442                         }
443                 }
444         }
445
446         g_race_qualifying = qual;
447
448         remove(self);
449         self = oldself;
450 }
451
452 void spawnfunc_trigger_race_checkpoint()
453 {
454         vector o;
455         if(!g_race)
456         {
457                 remove(self);
458                 return;
459         }
460
461         EXACTTRIGGER_INIT;
462
463         self.use = checkpoint_use;
464         if not(self.spawnflags & 1)
465                 self.touch = checkpoint_touch;
466
467         o = (self.absmin + self.absmax) * 0.5;
468         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
469         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
470         self.nearestwaypointtimeout = time + 1000000000;
471
472         if(!self.message)
473                 self.message = "went backwards";
474         if (!self.message2)
475                 self.message2 = "was pushed backwards by";
476         
477         self.race_checkpoint = self.cnt;
478
479         if(self.race_checkpoint > race_highest_checkpoint)
480                 race_highest_checkpoint = self.race_checkpoint;
481
482         if(self.race_checkpoint)
483         {
484                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
485         }
486         else
487         {
488                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
489         }
490         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
491
492         InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
493 }
494
495 void race_AbandonRaceCheck(entity p)
496 {
497         if(race_completing && !p.race_completed)
498         {
499                 p.race_completed = 1;
500                 MAKE_INDEPENDENT_PLAYER(p);
501                 bprint(p.netname, "^7 has abandoned the race.\n");
502                 ClientData_Touch(p);
503         }
504 }
505
506 void race_PreparePlayer()
507 {
508         race_ClearTime(self);
509         self.race_place = 0;
510 }
511
512 void race_RetractPlayer()
513 {
514         if(!g_race)
515                 return;
516         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
517         if(self.race_checkpoint == 0)
518         {
519                 race_ClearTime(self);
520                 self.race_checkpoint = 0;
521         }
522 }
523
524 void race_PreDie()
525 {
526         if(!g_race)
527                 return;
528
529         race_AbandonRaceCheck(self);
530 }
531
532 void race_PreSpawn()
533 {
534         if(!g_race)
535                 return;
536         if(self.killcount == -666 || g_race_qualifying)
537                 race_PreparePlayer();
538
539         race_AbandonRaceCheck(self);
540 }
541
542 void race_PostSpawn(entity spot)
543 {
544         if(!g_race)
545                 return;
546         if(self.killcount != -666 && !g_race_qualifying)
547         {
548                 if(spot.target == "")
549                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
550                         race_PreparePlayer();
551                 else
552                         race_RetractPlayer();
553         }
554
555         if(spot.target != "" && self.race_checkpoint == -1)
556                 self.race_checkpoint = 0;
557
558         self.race_place = 0;
559 }
560
561 void race_PreSpawnObserver()
562 {
563         if(!g_race)
564                 return;
565         race_PreparePlayer();
566 }
567
568 void spawnfunc_info_player_race (void)
569 {
570         if(!g_race)
571         {
572                 remove(self);
573                 return;
574         }
575         ++race_spawns;
576         spawnfunc_info_player_deathmatch();
577
578         if(self.race_place > race_highest_place_spawn)
579                 race_highest_place_spawn = self.race_place;
580         if(self.race_place < race_lowest_place_spawn)
581                 race_lowest_place_spawn = self.race_place;
582 }
583
584 void race_ClearRecords()
585 {
586         float i;
587         entity e;
588
589         for(i = 0; i < MAX_CHECKPOINTS; ++i)
590         {
591                 race_checkpoint_records[i] = 0;
592                 if(race_checkpoint_recordholders[i])
593                         strunzone(race_checkpoint_recordholders[i]);
594                 race_checkpoint_recordholders[i] = string_null;
595         }
596
597         FOR_EACH_CLIENT(e)
598                 race_ClearTime(e);
599 }
600
601 void race_ReadyRestart()
602 {
603         race_ClearRecords();
604
605         PlayerScore_Sort(race_place);
606
607         if(g_race_qualifying == 2)
608         {
609                 g_race_qualifying = 0;
610                 independent_players = 0;
611                 cvar_set("fraglimit", ftos(race_fraglimit));
612                 ScoreRules_race();
613         }
614 }