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];
68 recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
69 recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
71 if(recordholder == e.netname)
76 WRITESPECTATABLE_MSG_ONE({
77 WriteByte(MSG_ONE, SVC_TEMPENTITY);
78 WriteByte(MSG_ONE, TE_CSQC_RACE);
81 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
82 WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
85 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
86 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
87 WriteShort(MSG_ONE, recordtime);
88 WriteString(MSG_ONE, recordholder);
92 void race_InitSpectator()
95 if(msg_entity.enemy.race_laptime)
96 race_SendNextCheckpoint(msg_entity.enemy, 1);
99 void race_SendTime(entity e, float cp, float t, float tvalid)
104 if(g_race_qualifying)
105 t += e.race_penalty_accumulator;
107 t = floor(0.4 + 10 * t); // make integer
108 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
111 if(cp == race_timed_checkpoint) // finish line
112 if not(e.race_completed)
115 if(g_race_qualifying)
117 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
119 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
123 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
124 snew = floor(0.4 + 10 * (time - game_starttime));
125 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
126 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
128 if(cvar("fraglimit"))
129 if(l >= cvar("fraglimit"))
133 if(p.deadflag != DEAD_NO)
134 race_AbandonRaceCheck(p);
139 e.race_completed = 1;
140 MAKE_INDEPENDENT_PLAYER(e);
141 bprint(e.netname, "^7 has finished the race.\n");
150 if(g_race_qualifying)
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)
160 if(t < recordtime || recordtime == 0)
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)
169 string grecordholder;
170 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
171 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
172 if(grecordholder == e.netname)
176 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
177 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
178 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
179 write_recordmarker(e, time - t/10, t/10);
181 else if(t < grecordtime)
183 if(grecordholder == "")
184 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
186 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
187 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
188 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
189 write_recordmarker(e, time - t/10, t/10);
193 if(grecordholder == "")
194 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
196 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
200 if(g_race_qualifying)
202 FOR_EACH_REALPLAYER(p)
203 if(p.race_checkpoint == cp)
204 race_SendNextCheckpoint(p, 0);
217 if(g_race_qualifying)
219 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
220 WriteByte(MSG_ONE, SVC_TEMPENTITY);
221 WriteByte(MSG_ONE, TE_CSQC_RACE);
222 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
223 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
224 WriteShort(MSG_ONE, t); // time to that intermediate
225 WriteShort(MSG_ONE, recordtime); // previously best time
226 WriteString(MSG_ONE, recordholder); // record holder
230 else // RACE! Not Qualifying
232 float lself, lother, othtime;
234 oth = race_checkpoint_lastplayers[cp];
237 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
238 lother = race_checkpoint_lastlaps[cp];
239 othtime = race_checkpoint_lasttimes[cp];
242 lself = lother = othtime = 0;
245 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
246 WriteByte(MSG_ONE, SVC_TEMPENTITY);
247 WriteByte(MSG_ONE, TE_CSQC_RACE);
248 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
249 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
252 WriteShort(MSG_ONE, 0);
253 WriteByte(MSG_ONE, 0);
254 WriteString(MSG_ONE, "");
258 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
259 WriteByte(MSG_ONE, lself - lother);
260 WriteString(MSG_ONE, oth.netname); // record holder
264 race_checkpoint_lastplayers[cp] = e;
265 race_checkpoint_lasttimes[cp] = time;
266 race_checkpoint_lastlaps[cp] = lself;
269 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
270 WriteByte(MSG_ONE, SVC_TEMPENTITY);
271 WriteByte(MSG_ONE, TE_CSQC_RACE);
272 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
273 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
276 WriteShort(MSG_ONE, 0);
277 WriteByte(MSG_ONE, 0);
278 WriteString(MSG_ONE, "");
282 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
283 WriteByte(MSG_ONE, lother - lself);
284 WriteString(MSG_ONE, e.netname); // record holder
290 void race_ClearTime(entity e)
292 e.race_checkpoint = -1;
294 e.race_penalty_accumulator = 0;
295 e.race_lastpenalty = world;
298 WRITESPECTATABLE_MSG_ONE({
299 WriteByte(MSG_ONE, SVC_TEMPENTITY);
300 WriteByte(MSG_ONE, TE_CSQC_RACE);
301 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
305 void dumpsurface(entity e)
309 print("Surfaces of ", etos(e), ":\n");
311 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
315 n = getsurfacenumpoints(e, si);
318 print(" Surface ", ftos(si), ":\n");
319 norm = getsurfacenormal(e, si);
320 print(" Normal = ", vtos(norm), "\n");
321 for(ni = 0; ni < n; ++ni)
323 vec = getsurfacepoint(e, si, ni);
324 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
329 void checkpoint_passed()
333 if(other.classname == "porto")
335 // do not allow portalling through checkpoints
336 trace_plane_normal = normalize(-1 * other.velocity);
345 if not(self.spawnflags & 2)
348 oldmsg = self.message;
351 self.message = oldmsg;
354 if(other.classname != "player")
358 * Remove unauthorized equipment
360 Portal_ClearAll(other);
362 other.porto_forbidden = 2; // decreased by 1 each StartFrame
364 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
366 if(self.race_penalty)
368 if(other.race_lastpenalty != self)
370 other.race_lastpenalty = self;
371 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
375 if(other.race_penalty)
381 if(self.spawnflags & 2)
384 oldmsg = self.message;
387 self.message = oldmsg;
390 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
392 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
394 if(!self.race_checkpoint) // start line
396 other.race_laptime = time;
397 other.race_penalty_accumulator = 0;
398 other.race_lastpenalty = world;
401 if(g_race_qualifying)
402 race_SendNextCheckpoint(other, 0);
404 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
410 if(self.spawnflags & 4)
411 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
415 void checkpoint_touch()
421 void checkpoint_use()
423 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
430 float race_waypointsprite_visible_for_player(entity e)
432 if(e.race_checkpoint == -1)
434 else if(e.race_checkpoint == self.owner.race_checkpoint)
441 void trigger_race_checkpoint_verify()
451 qual = g_race_qualifying;
455 self.classname = "player";
457 for(i = 0; i <= race_highest_checkpoint; ++i)
459 self.race_checkpoint = race_NextCheckpoint(i);
461 // race only (middle of the race)
462 g_race_qualifying = 0;
464 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
465 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
470 g_race_qualifying = 1;
471 self.race_place = race_lowest_place_spawn;
472 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
473 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
475 // race only (initial spawn)
476 g_race_qualifying = 0;
477 for(p = 1; p <= race_highest_place_spawn; ++p)
480 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
481 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
486 g_race_qualifying = qual;
488 if(race_timed_checkpoint)
489 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
492 if(cp.race_checkpoint == 0)
493 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
494 else if(cp.race_checkpoint == race_timed_checkpoint)
495 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
502 void spawnfunc_trigger_race_checkpoint()
505 if(!g_race && !g_cts)
513 self.use = checkpoint_use;
514 if not(self.spawnflags & 1)
515 self.touch = checkpoint_touch;
517 o = (self.absmin + self.absmax) * 0.5;
518 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
519 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
520 self.nearestwaypointtimeout = time + 1000000000;
523 self.message = "went backwards";
525 self.message2 = "was pushed backwards by";
526 if (!self.race_penalty_reason)
527 self.race_penalty_reason = "missing a checkpoint";
529 self.race_checkpoint = self.cnt;
531 if(self.race_checkpoint > race_highest_checkpoint)
533 race_highest_checkpoint = self.race_checkpoint;
534 if(self.spawnflags & 8)
535 race_timed_checkpoint = self.race_checkpoint;
537 race_timed_checkpoint = 0;
540 if(!self.race_penalty)
542 if(self.race_checkpoint)
543 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
545 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
548 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
550 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
553 void race_AbandonRaceCheck(entity p)
555 if(race_completing && !p.race_completed)
557 p.race_completed = 1;
558 MAKE_INDEPENDENT_PLAYER(p);
559 bprint(p.netname, "^7 has abandoned the race.\n");
564 void race_PreparePlayer()
566 race_ClearTime(self);
570 void race_RetractPlayer()
572 if(!g_race && !g_cts)
574 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
575 if(self.race_checkpoint == 0)
577 race_ClearTime(self);
578 self.race_checkpoint = 0;
584 if(!g_race && !g_cts)
587 race_AbandonRaceCheck(self);
592 if(!g_race && !g_cts)
594 if(self.killcount == -666 || g_race_qualifying)
595 race_PreparePlayer();
597 race_AbandonRaceCheck(self);
600 void race_PostSpawn(entity spot)
602 if(!g_race && !g_cts)
604 if(self.killcount != -666 && !g_race_qualifying)
606 if(spot.target == "")
607 // let the player run without timing, if he did not spawn at a targetting spawnpoint
608 race_PreparePlayer();
610 race_RetractPlayer();
613 if(spot.target != "" && self.race_checkpoint == -1)
614 self.race_checkpoint = 0;
619 void race_PreSpawnObserver()
621 if(!g_race && !g_cts)
623 race_PreparePlayer();
626 void spawnfunc_info_player_race (void)
628 if(!g_race && !g_cts)
634 spawnfunc_info_player_deathmatch();
636 if(self.race_place > race_highest_place_spawn)
637 race_highest_place_spawn = self.race_place;
638 if(self.race_place < race_lowest_place_spawn)
639 race_lowest_place_spawn = self.race_place;
642 void race_ClearRecords()
647 for(i = 0; i < MAX_CHECKPOINTS; ++i)
649 race_checkpoint_records[i] = 0;
650 if(race_checkpoint_recordholders[i])
651 strunzone(race_checkpoint_recordholders[i]);
652 race_checkpoint_recordholders[i] = string_null;
659 void race_ReadyRestart()
661 Score_NicePrint(world);
664 PlayerScore_Sort(race_place);
668 print(e.netname, " = ", ftos(e.race_place), "\n");
670 if(g_race_qualifying == 2)
672 g_race_qualifying = 0;
673 independent_players = 0;
674 cvar_set("fraglimit", ftos(race_fraglimit));
675 cvar_set("leadlimit", ftos(race_leadlimit));
676 cvar_set("timelimit", ftos(race_timelimit));
681 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
683 if(g_race_qualifying)
685 pl.race_penalty_accumulator += penalty;
687 WRITESPECTATABLE_MSG_ONE({
688 WriteByte(MSG_ONE, SVC_TEMPENTITY);
689 WriteByte(MSG_ONE, TE_CSQC_RACE);
690 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
691 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
692 WriteString(MSG_ONE, reason);
697 pl.race_penalty = time + penalty;
699 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
700 WriteByte(MSG_ONE, SVC_TEMPENTITY);
701 WriteByte(MSG_ONE, TE_CSQC_RACE);
702 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
703 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
704 WriteString(MSG_ONE, reason);
712 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
718 if(other.race_lastpenalty != self)
720 other.race_lastpenalty = self;
721 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
725 void spawnfunc_trigger_race_penalty()
729 self.use = penalty_use;
730 if not(self.spawnflags & 1)
731 self.touch = penalty_touch;
733 if (!self.race_penalty_reason)
734 self.race_penalty_reason = "missing a checkpoint";
735 if (!self.race_penalty)
736 self.race_penalty = 5;