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