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)
369 if(self.spawnflags & 2)
372 oldmsg = self.message;
375 self.message = oldmsg;
378 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
380 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
382 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
384 if(!self.race_checkpoint) // start line
386 other.race_laptime = time;
387 other.race_penalty_accumulator = 0;
388 other.race_lastpenalty = world;
391 if(g_race_qualifying)
392 race_SendNextCheckpoint(other, 0);
394 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
400 if(self.spawnflags & 4)
401 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
405 void checkpoint_touch()
411 void checkpoint_use()
413 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
420 float race_waypointsprite_visible_for_player(entity e)
422 if(e.race_checkpoint == -1)
424 else if(e.race_checkpoint == self.owner.race_checkpoint)
431 void trigger_race_checkpoint_verify()
441 qual = g_race_qualifying;
445 self.classname = "player";
447 for(i = 0; i <= race_highest_checkpoint; ++i)
449 self.race_checkpoint = race_NextCheckpoint(i);
451 // race only (middle of the race)
452 g_race_qualifying = 0;
454 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
455 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
460 g_race_qualifying = 1;
461 self.race_place = race_lowest_place_spawn;
462 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
463 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
465 // race only (initial spawn)
466 g_race_qualifying = 0;
467 for(p = 1; p <= race_highest_place_spawn; ++p)
470 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
471 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
476 g_race_qualifying = qual;
478 if(race_timed_checkpoint)
479 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
482 if(cp.race_checkpoint == 0)
483 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
484 else if(cp.race_checkpoint == race_timed_checkpoint)
485 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
492 void spawnfunc_trigger_race_checkpoint()
503 self.use = checkpoint_use;
504 if not(self.spawnflags & 1)
505 self.touch = checkpoint_touch;
507 o = (self.absmin + self.absmax) * 0.5;
508 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
509 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
510 self.nearestwaypointtimeout = time + 1000000000;
513 self.message = "went backwards";
515 self.message2 = "was pushed backwards by";
516 if (!self.race_penalty_reason)
517 self.race_penalty_reason = "missing a checkpoint";
519 self.race_checkpoint = self.cnt;
521 if(self.race_checkpoint > race_highest_checkpoint)
523 race_highest_checkpoint = self.race_checkpoint;
524 if(self.spawnflags & 8)
525 race_timed_checkpoint = self.race_checkpoint;
527 race_timed_checkpoint = 0;
530 if(!self.race_penalty)
532 if(self.race_checkpoint)
533 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
535 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
538 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
540 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
543 void race_AbandonRaceCheck(entity p)
545 if(race_completing && !p.race_completed)
547 p.race_completed = 1;
548 MAKE_INDEPENDENT_PLAYER(p);
549 bprint(p.netname, "^7 has abandoned the race.\n");
554 void race_PreparePlayer()
556 race_ClearTime(self);
560 void race_RetractPlayer()
564 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
565 if(self.race_checkpoint == 0)
567 race_ClearTime(self);
568 self.race_checkpoint = 0;
577 race_AbandonRaceCheck(self);
584 if(self.killcount == -666 || g_race_qualifying)
585 race_PreparePlayer();
587 race_AbandonRaceCheck(self);
590 void race_PostSpawn(entity spot)
594 if(self.killcount != -666 && !g_race_qualifying)
596 if(spot.target == "")
597 // let the player run without timing, if he did not spawn at a targetting spawnpoint
598 race_PreparePlayer();
600 race_RetractPlayer();
603 if(spot.target != "" && self.race_checkpoint == -1)
604 self.race_checkpoint = 0;
609 void race_PreSpawnObserver()
613 race_PreparePlayer();
616 void spawnfunc_info_player_race (void)
624 spawnfunc_info_player_deathmatch();
626 if(self.race_place > race_highest_place_spawn)
627 race_highest_place_spawn = self.race_place;
628 if(self.race_place < race_lowest_place_spawn)
629 race_lowest_place_spawn = self.race_place;
632 void race_ClearRecords()
637 for(i = 0; i < MAX_CHECKPOINTS; ++i)
639 race_checkpoint_records[i] = 0;
640 if(race_checkpoint_recordholders[i])
641 strunzone(race_checkpoint_recordholders[i]);
642 race_checkpoint_recordholders[i] = string_null;
649 void race_ReadyRestart()
651 Score_NicePrint(world);
654 PlayerScore_Sort(race_place);
658 print(e.netname, " = ", ftos(e.race_place), "\n");
660 if(g_race_qualifying == 2)
662 g_race_qualifying = 0;
663 independent_players = 0;
664 cvar_set("fraglimit", ftos(race_fraglimit));
665 cvar_set("leadlimit", ftos(race_leadlimit));
670 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
672 if(g_race_qualifying)
674 pl.race_penalty_accumulator += penalty;
676 WRITESPECTATABLE_MSG_ONE({
677 WriteByte(MSG_ONE, SVC_TEMPENTITY);
678 WriteByte(MSG_ONE, TE_CSQC_RACE);
679 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
680 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
681 WriteString(MSG_ONE, reason);
686 pl.race_penalty = time + penalty;
688 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
689 WriteByte(MSG_ONE, SVC_TEMPENTITY);
690 WriteByte(MSG_ONE, TE_CSQC_RACE);
691 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
692 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
693 WriteString(MSG_ONE, reason);
701 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
707 if(other.race_lastpenalty != self)
709 other.race_lastpenalty = self;
710 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
714 void spawnfunc_trigger_race_penalty()
718 self.use = penalty_use;
719 if not(self.spawnflags & 1)
720 self.touch = penalty_touch;
722 if (!self.race_penalty_reason)
723 self.race_penalty_reason = "missing a checkpoint";
724 if (!self.race_penalty)
725 self.race_penalty = 5;