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