1 #define MAX_CHECKPOINTS 255
4 .float race_penalty_accumulator;
5 .string race_penalty_reason;
6 .float race_checkpoint; // player: next checkpoint that has to be reached
8 .entity race_lastpenalty;
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];
18 float race_highest_checkpoint;
19 float race_timed_checkpoint;
21 float race_NextCheckpoint(float f)
23 if(f >= race_highest_checkpoint)
29 float race_PreviousCheckpoint(float f)
34 return race_highest_checkpoint;
40 // 0 = common start/finish
43 float race_CheckpointNetworkID(float f)
45 if(race_timed_checkpoint)
49 else if(f == race_timed_checkpoint)
55 void race_SendNextCheckpoint(entity e, float spec)
64 cp = e.race_checkpoint;
65 recordtime = race_checkpoint_records[cp];
66 recordholder = race_checkpoint_recordholders[cp];
67 if(recordholder == e.netname)
72 WRITESPECTATABLE_MSG_ONE({
73 WriteByte(MSG_ONE, SVC_TEMPENTITY);
74 WriteByte(MSG_ONE, TE_CSQC_RACE);
77 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
78 WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
81 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
82 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
83 WriteShort(MSG_ONE, recordtime);
84 WriteString(MSG_ONE, recordholder);
88 void race_InitSpectator()
91 if(msg_entity.enemy.race_laptime)
92 race_SendNextCheckpoint(msg_entity.enemy, 1);
95 void race_SendTime(entity e, float cp, float t, float tvalid)
100 if(g_race_qualifying)
101 t += e.race_penalty_accumulator;
103 t = floor(0.4 + 10 * t); // make integer
104 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
107 if(cp == race_timed_checkpoint) // finish line
108 if not(e.race_completed)
111 if(g_race_qualifying)
113 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
115 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
119 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
120 snew = floor(0.4 + 10 * (time - game_starttime));
121 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
122 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
124 if(cvar("fraglimit"))
125 if(l >= cvar("fraglimit"))
126 race_StartCompleting();
130 e.race_completed = 1;
131 MAKE_INDEPENDENT_PLAYER(e);
132 bprint(e.netname, "^7 has finished the race.\n");
141 if(g_race_qualifying)
145 recordtime = race_checkpoint_records[cp];
146 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
147 if(recordholder == e.netname)
151 if(t < recordtime || recordtime == 0)
153 race_checkpoint_records[cp] = t;
154 if(race_checkpoint_recordholders[cp])
155 strunzone(race_checkpoint_recordholders[cp]);
156 race_checkpoint_recordholders[cp] = strzone(e.netname);
157 if(cp == race_timed_checkpoint)
160 string grecordholder;
166 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
167 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname"));
168 if(grecordholder == e.netname)
172 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
173 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
174 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
175 write_recordmarker(e, time - t/10, t/10);
177 else if(t < grecordtime)
179 if(grecordholder == "")
180 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
182 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
183 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
184 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
185 write_recordmarker(e, time - t/10, t/10);
189 if(grecordholder == "")
190 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
192 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
196 if(g_race_qualifying)
198 FOR_EACH_REALPLAYER(p)
199 if(p.race_checkpoint == cp)
200 race_SendNextCheckpoint(p, 0);
213 if(g_race_qualifying)
215 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
216 WriteByte(MSG_ONE, SVC_TEMPENTITY);
217 WriteByte(MSG_ONE, TE_CSQC_RACE);
218 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
219 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
220 WriteShort(MSG_ONE, t); // time to that intermediate
221 WriteShort(MSG_ONE, recordtime); // previously best time
222 WriteString(MSG_ONE, recordholder); // record holder
226 else // RACE! Not Qualifying
228 float lself, lother, othtime;
230 oth = race_checkpoint_lastplayers[cp];
233 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
234 lother = race_checkpoint_lastlaps[cp];
235 othtime = race_checkpoint_lasttimes[cp];
238 lself = lother = othtime = 0;
241 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
242 WriteByte(MSG_ONE, SVC_TEMPENTITY);
243 WriteByte(MSG_ONE, TE_CSQC_RACE);
244 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
245 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
248 WriteShort(MSG_ONE, 0);
249 WriteByte(MSG_ONE, 0);
250 WriteString(MSG_ONE, "");
254 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
255 WriteByte(MSG_ONE, lself - lother);
256 WriteString(MSG_ONE, oth.netname); // record holder
260 race_checkpoint_lastplayers[cp] = e;
261 race_checkpoint_lasttimes[cp] = time;
262 race_checkpoint_lastlaps[cp] = lself;
265 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
266 WriteByte(MSG_ONE, SVC_TEMPENTITY);
267 WriteByte(MSG_ONE, TE_CSQC_RACE);
268 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
269 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
272 WriteShort(MSG_ONE, 0);
273 WriteByte(MSG_ONE, 0);
274 WriteString(MSG_ONE, "");
278 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
279 WriteByte(MSG_ONE, lother - lself);
280 WriteString(MSG_ONE, e.netname); // record holder
286 void race_ClearTime(entity e)
288 e.race_checkpoint = -1;
290 e.race_penalty_accumulator = 0;
291 e.race_lastpenalty = world;
294 WRITESPECTATABLE_MSG_ONE({
295 WriteByte(MSG_ONE, SVC_TEMPENTITY);
296 WriteByte(MSG_ONE, TE_CSQC_RACE);
297 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
301 void dumpsurface(entity e)
305 print("Surfaces of ", etos(e), ":\n");
307 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
311 n = getsurfacenumpoints(e, si);
314 print(" Surface ", ftos(si), ":\n");
315 norm = getsurfacenormal(e, si);
316 print(" Normal = ", vtos(norm), "\n");
317 for(ni = 0; ni < n; ++ni)
319 vec = getsurfacepoint(e, si, ni);
320 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
325 void checkpoint_passed()
329 if(other.classname == "porto")
331 // do not allow portalling through checkpoints
332 trace_plane_normal = normalize(-1 * other.velocity);
341 if not(self.spawnflags & 2)
344 oldmsg = self.message;
347 self.message = oldmsg;
350 if(other.classname != "player")
354 * Remove unauthorized equipment
356 Portal_ClearAll(other);
358 other.porto_forbidden = 2; // decreased by 1 each StartFrame
360 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
362 if(self.race_penalty)
364 if(other.race_lastpenalty != self)
366 other.race_lastpenalty = self;
367 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
371 if(other.race_penalty)
377 if(self.spawnflags & 2)
380 oldmsg = self.message;
383 self.message = oldmsg;
386 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
388 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
390 if(!self.race_checkpoint) // start line
392 other.race_laptime = time;
393 other.race_penalty_accumulator = 0;
394 other.race_lastpenalty = world;
397 if(g_race_qualifying)
398 race_SendNextCheckpoint(other, 0);
400 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
406 if(self.spawnflags & 4)
407 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
411 void checkpoint_touch()
417 void checkpoint_use()
419 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
426 float race_waypointsprite_visible_for_player(entity e)
428 if(e.race_checkpoint == -1)
430 else if(e.race_checkpoint == self.owner.race_checkpoint)
437 void trigger_race_checkpoint_verify()
447 qual = g_race_qualifying;
451 self.classname = "player";
453 for(i = 0; i <= race_highest_checkpoint; ++i)
455 self.race_checkpoint = race_NextCheckpoint(i);
457 // race only (middle of the race)
458 g_race_qualifying = 0;
460 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
461 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
466 g_race_qualifying = 1;
467 self.race_place = race_lowest_place_spawn;
468 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
469 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
471 // race only (initial spawn)
472 g_race_qualifying = 0;
473 for(p = 1; p <= race_highest_place_spawn; ++p)
476 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
477 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
482 g_race_qualifying = qual;
484 if(race_timed_checkpoint)
485 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
488 if(cp.race_checkpoint == 0)
489 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
490 else if(cp.race_checkpoint == race_timed_checkpoint)
491 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
498 void spawnfunc_trigger_race_checkpoint()
501 if(!g_race && !g_cts)
509 self.use = checkpoint_use;
510 if not(self.spawnflags & 1)
511 self.touch = checkpoint_touch;
513 o = (self.absmin + self.absmax) * 0.5;
514 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
515 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
516 self.nearestwaypointtimeout = time + 1000000000;
519 self.message = "went backwards";
521 self.message2 = "was pushed backwards by";
522 if (!self.race_penalty_reason)
523 self.race_penalty_reason = "missing a checkpoint";
525 self.race_checkpoint = self.cnt;
527 if(self.race_checkpoint > race_highest_checkpoint)
529 race_highest_checkpoint = self.race_checkpoint;
530 if(self.spawnflags & 8)
531 race_timed_checkpoint = self.race_checkpoint;
533 race_timed_checkpoint = 0;
536 if(!self.race_penalty)
538 if(self.race_checkpoint)
539 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
541 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
544 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
546 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
549 void race_AbandonRaceCheck(entity p)
551 if(race_completing && !p.race_completed)
553 p.race_completed = 1;
554 MAKE_INDEPENDENT_PLAYER(p);
555 bprint(p.netname, "^7 has abandoned the race.\n");
560 void race_StartCompleting()
565 if(p.deadflag != DEAD_NO)
566 race_AbandonRaceCheck(p);
569 void race_PreparePlayer()
571 race_ClearTime(self);
575 void race_RetractPlayer()
577 if(!g_race && !g_cts)
579 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
580 if(self.race_checkpoint == 0)
582 race_ClearTime(self);
583 self.race_checkpoint = 0;
589 if(!g_race && !g_cts)
592 race_AbandonRaceCheck(self);
597 if(!g_race && !g_cts)
599 if(self.killcount == -666 || g_race_qualifying)
600 race_PreparePlayer();
602 race_AbandonRaceCheck(self);
605 void race_PostSpawn(entity spot)
607 if(!g_race && !g_cts)
609 if(self.killcount != -666 && !g_race_qualifying)
611 if(spot.target == "")
612 // let the player run without timing, if he did not spawn at a targetting spawnpoint
613 race_PreparePlayer();
615 race_RetractPlayer();
618 if(spot.target != "" && self.race_checkpoint == -1)
619 self.race_checkpoint = 0;
624 void race_PreSpawnObserver()
626 if(!g_race && !g_cts)
628 race_PreparePlayer();
631 void spawnfunc_info_player_race (void)
633 if(!g_race && !g_cts)
639 spawnfunc_info_player_deathmatch();
641 if(self.race_place > race_highest_place_spawn)
642 race_highest_place_spawn = self.race_place;
643 if(self.race_place < race_lowest_place_spawn)
644 race_lowest_place_spawn = self.race_place;
647 void race_ClearRecords()
652 for(i = 0; i < MAX_CHECKPOINTS; ++i)
654 race_checkpoint_records[i] = 0;
655 if(race_checkpoint_recordholders[i])
656 strunzone(race_checkpoint_recordholders[i]);
657 race_checkpoint_recordholders[i] = string_null;
664 void race_ReadyRestart()
668 Score_NicePrint(world);
671 PlayerScore_Sort(race_place);
678 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
682 print(e.netname, " = ", ftos(e.race_place), "\n");
685 if(g_race_qualifying == 2)
687 g_race_qualifying = 0;
688 independent_players = 0;
689 cvar_set("fraglimit", ftos(race_fraglimit));
690 cvar_set("leadlimit", ftos(race_leadlimit));
691 cvar_set("timelimit", ftos(race_timelimit));
696 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
698 if(g_race_qualifying)
700 pl.race_penalty_accumulator += penalty;
702 WRITESPECTATABLE_MSG_ONE({
703 WriteByte(MSG_ONE, SVC_TEMPENTITY);
704 WriteByte(MSG_ONE, TE_CSQC_RACE);
705 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
706 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
707 WriteString(MSG_ONE, reason);
712 pl.race_penalty = time + penalty;
714 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
715 WriteByte(MSG_ONE, SVC_TEMPENTITY);
716 WriteByte(MSG_ONE, TE_CSQC_RACE);
717 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
718 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
719 WriteString(MSG_ONE, reason);
727 if(other.race_lastpenalty != self)
729 other.race_lastpenalty = self;
730 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
736 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
739 void spawnfunc_trigger_race_penalty()
743 self.use = penalty_use;
744 if not(self.spawnflags & 1)
745 self.touch = penalty_touch;
747 if (!self.race_penalty_reason)
748 self.race_penalty_reason = "missing a checkpoint";
749 if (!self.race_penalty)
750 self.race_penalty = 5;