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) // qualifying only
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);
79 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
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);
89 void race_InitSpectator()
92 if(msg_entity.enemy.race_laptime)
93 race_SendNextCheckpoint(msg_entity.enemy, 1);
96 void race_send_recordtime(float t)
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);
107 void race_SendTime(entity e, float cp, float t, float tvalid)
112 if(g_race_qualifying)
113 t += e.race_penalty_accumulator;
115 t = TIME_ENCODE(t); // make integer
116 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
119 if(cp == race_timed_checkpoint) // finish line
120 if not(e.race_completed)
123 if(g_race_qualifying)
125 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
127 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
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);
136 if(cvar("fraglimit"))
137 if(l >= cvar("fraglimit"))
138 race_StartCompleting();
142 e.race_completed = 1;
143 MAKE_INDEPENDENT_PLAYER(e);
144 bprint(e.netname, "^7 has finished the race.\n");
152 if(g_race_qualifying)
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)
162 if(t < recordtime || recordtime == 0)
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)
171 string grecordholder, recorddifference;
177 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
178 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname"));
179 if(grecordholder == e.netname)
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);
189 else if(t < grecordtime)
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");
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);
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");
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");
211 if(g_race_qualifying)
213 FOR_EACH_REALPLAYER(p)
214 if(p.race_checkpoint == cp)
215 race_SendNextCheckpoint(p, 0);
228 if(g_race_qualifying)
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
241 else // RACE! Not Qualifying
243 float lself, lother, othtime;
245 oth = race_checkpoint_lastplayers[cp];
248 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
249 lother = race_checkpoint_lastlaps[cp];
250 othtime = race_checkpoint_lasttimes[cp];
253 lself = lother = othtime = 0;
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
263 WriteInt24_t(MSG_ONE, 0);
264 WriteByte(MSG_ONE, 0);
265 WriteString(MSG_ONE, "");
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
275 race_checkpoint_lastplayers[cp] = e;
276 race_checkpoint_lasttimes[cp] = time;
277 race_checkpoint_lastlaps[cp] = lself;
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
287 WriteInt24_t(MSG_ONE, 0);
288 WriteByte(MSG_ONE, 0);
289 WriteString(MSG_ONE, "");
293 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
294 WriteByte(MSG_ONE, lother - lself);
295 WriteString(MSG_ONE, e.netname); // record holder
301 void race_ClearTime(entity e)
303 e.race_checkpoint = -1;
305 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
306 e.race_penalty_accumulator = 0;
307 e.race_lastpenalty = world;
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
317 void dumpsurface(entity e)
321 print("Surfaces of ", etos(e), ":\n");
323 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
327 n = getsurfacenumpoints(e, si);
330 print(" Surface ", ftos(si), ":\n");
331 norm = getsurfacenormal(e, si);
332 print(" Normal = ", vtos(norm), "\n");
333 for(ni = 0; ni < n; ++ni)
335 vec = getsurfacepoint(e, si, ni);
336 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
341 void checkpoint_passed()
345 if(other.classname == "porto")
347 // do not allow portalling through checkpoints
348 trace_plane_normal = normalize(-1 * other.velocity);
357 if not((self.spawnflags & 2) && (other.classname == "player"))
360 oldmsg = self.message;
363 self.message = oldmsg;
366 if(other.classname != "player")
370 * Remove unauthorized equipment
372 Portal_ClearAll(other);
374 other.porto_forbidden = 2; // decreased by 1 each StartFrame
376 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
378 if(self.race_penalty)
380 if(other.race_lastpenalty != self)
382 other.race_lastpenalty = self;
383 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
387 if(other.race_penalty)
393 if(self.spawnflags & 2)
396 oldmsg = self.message;
399 self.message = oldmsg;
402 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
404 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
406 if(!self.race_checkpoint) // start line
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;
414 if(g_race_qualifying)
415 race_SendNextCheckpoint(other, 0);
417 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
423 if(self.spawnflags & 4)
424 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
428 void checkpoint_touch()
434 void checkpoint_use()
436 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
443 float race_waypointsprite_visible_for_player(entity e)
445 if(e.race_checkpoint == -1)
447 else if(e.race_checkpoint == self.owner.race_checkpoint)
454 void trigger_race_checkpoint_verify()
464 qual = g_race_qualifying;
468 self.classname = "player";
472 for(i = 0; i <= race_highest_checkpoint; ++i)
474 self.race_checkpoint = race_NextCheckpoint(i);
476 // race only (middle of the race)
477 g_race_qualifying = 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"));
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"));
490 // race only (initial spawn)
491 g_race_qualifying = 0;
492 for(p = 1; p <= race_highest_place_spawn; ++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"));
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"));
511 g_race_qualifying = qual;
513 if(race_timed_checkpoint)
514 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
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", "", "");
527 void spawnfunc_trigger_race_checkpoint()
530 if(!g_race && !g_cts)
538 self.use = checkpoint_use;
539 if not(self.spawnflags & 1)
540 self.touch = checkpoint_touch;
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;
548 self.message = "went backwards";
550 self.message2 = "was pushed backwards by";
551 if (!self.race_penalty_reason)
552 self.race_penalty_reason = "missing a checkpoint";
554 self.race_checkpoint = self.cnt;
556 if(self.race_checkpoint > race_highest_checkpoint)
558 race_highest_checkpoint = self.race_checkpoint;
559 if(self.spawnflags & 8)
560 race_timed_checkpoint = self.race_checkpoint;
562 race_timed_checkpoint = 0;
565 if(!self.race_penalty)
567 if(self.race_checkpoint)
568 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
570 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
573 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
575 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
578 void race_AbandonRaceCheck(entity p)
580 if(race_completing && !p.race_completed)
582 p.race_completed = 1;
583 MAKE_INDEPENDENT_PLAYER(p);
584 bprint(p.netname, "^7 has abandoned the race.\n");
589 void race_StartCompleting()
594 if(p.deadflag != DEAD_NO)
595 race_AbandonRaceCheck(p);
598 void race_PreparePlayer()
600 race_ClearTime(self);
604 void race_RetractPlayer()
606 if(!g_race && !g_cts)
608 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
609 if(self.race_checkpoint == 0)
611 race_ClearTime(self);
612 self.race_checkpoint = 0;
618 if(!g_race && !g_cts)
621 race_AbandonRaceCheck(self);
626 if(!g_race && !g_cts)
628 if(self.killcount == -666 || g_race_qualifying)
629 race_PreparePlayer();
631 race_AbandonRaceCheck(self);
634 void race_PostSpawn(entity spot)
636 if(!g_race && !g_cts)
638 if(self.killcount != -666 && !g_race_qualifying)
640 if(spot.target == "")
641 // let the player run without timing, if he did not spawn at a targetting spawnpoint
642 race_PreparePlayer();
644 race_RetractPlayer();
647 if(spot.target != "" && self.race_checkpoint == -1)
648 self.race_checkpoint = 0;
653 void race_PreSpawnObserver()
655 if(!g_race && !g_cts)
657 race_PreparePlayer();
660 void spawnfunc_info_player_race (void)
662 if(!g_race && !g_cts)
668 spawnfunc_info_player_deathmatch();
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;
676 void race_ClearRecords()
681 for(i = 0; i < MAX_CHECKPOINTS; ++i)
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;
693 void race_ReadyRestart()
697 Score_NicePrint(world);
700 PlayerScore_Sort(race_place);
707 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
711 print(e.netname, " = ", ftos(e.race_place), "\n");
714 if(g_race_qualifying == 2)
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));
725 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
727 if(g_race_qualifying)
729 pl.race_penalty_accumulator += penalty;
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);
741 pl.race_penalty = time + penalty;
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);
756 if(other.race_lastpenalty != self)
758 other.race_lastpenalty = self;
759 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
765 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
768 void spawnfunc_trigger_race_penalty()
772 self.use = penalty_use;
773 if not(self.spawnflags & 1)
774 self.touch = penalty_touch;
776 if (!self.race_penalty_reason)
777 self.race_penalty_reason = "missing a checkpoint";
778 if (!self.race_penalty)
779 self.race_penalty = 5;