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, float msg)
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);
105 void race_SendTime(entity e, float cp, float t, float tvalid)
110 if(g_race_qualifying)
111 t += e.race_penalty_accumulator;
113 t = TIME_ENCODE(t); // make integer
114 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
117 if(cp == race_timed_checkpoint) // finish line
118 if not(e.race_completed)
121 if(g_race_qualifying)
123 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
125 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
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);
134 if(cvar("fraglimit"))
135 if(l >= cvar("fraglimit"))
136 race_StartCompleting();
140 e.race_completed = 1;
141 MAKE_INDEPENDENT_PLAYER(e);
142 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, recorddifference;
175 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
176 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname"));
177 if(grecordholder == e.netname)
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);
187 else if(t < grecordtime)
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");
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);
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");
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");
209 if(g_race_qualifying)
211 FOR_EACH_REALPLAYER(p)
212 if(p.race_checkpoint == cp)
213 race_SendNextCheckpoint(p, 0);
226 if(g_race_qualifying)
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
239 else // RACE! Not Qualifying
241 float lself, lother, othtime;
243 oth = race_checkpoint_lastplayers[cp];
246 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
247 lother = race_checkpoint_lastlaps[cp];
248 othtime = race_checkpoint_lasttimes[cp];
251 lself = lother = othtime = 0;
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
261 WriteInt24_t(MSG_ONE, 0);
262 WriteByte(MSG_ONE, 0);
263 WriteString(MSG_ONE, "");
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
273 race_checkpoint_lastplayers[cp] = e;
274 race_checkpoint_lasttimes[cp] = time;
275 race_checkpoint_lastlaps[cp] = lself;
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
285 WriteInt24_t(MSG_ONE, 0);
286 WriteByte(MSG_ONE, 0);
287 WriteString(MSG_ONE, "");
291 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
292 WriteByte(MSG_ONE, lother - lself);
293 WriteString(MSG_ONE, e.netname); // record holder
299 void race_ClearTime(entity e)
301 e.race_checkpoint = -1;
303 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
304 e.race_penalty_accumulator = 0;
305 e.race_lastpenalty = world;
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
315 void dumpsurface(entity e)
319 print("Surfaces of ", etos(e), ":\n");
321 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
325 n = getsurfacenumpoints(e, si);
328 print(" Surface ", ftos(si), ":\n");
329 norm = getsurfacenormal(e, si);
330 print(" Normal = ", vtos(norm), "\n");
331 for(ni = 0; ni < n; ++ni)
333 vec = getsurfacepoint(e, si, ni);
334 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
339 void checkpoint_passed()
343 if(other.classname == "porto")
345 // do not allow portalling through checkpoints
346 trace_plane_normal = normalize(-1 * other.velocity);
355 if not((self.spawnflags & 2) && (other.classname == "player"))
358 oldmsg = self.message;
361 self.message = oldmsg;
364 if(other.classname != "player")
368 * Remove unauthorized equipment
370 Portal_ClearAll(other);
372 other.porto_forbidden = 2; // decreased by 1 each StartFrame
374 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
376 if(self.race_penalty)
378 if(other.race_lastpenalty != self)
380 other.race_lastpenalty = self;
381 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
385 if(other.race_penalty)
391 if(self.spawnflags & 2)
394 oldmsg = self.message;
397 self.message = oldmsg;
400 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
402 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
404 if(!self.race_checkpoint) // start line
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;
412 if(g_race_qualifying)
413 race_SendNextCheckpoint(other, 0);
415 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
421 if(self.spawnflags & 4)
422 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
426 void checkpoint_touch()
432 void checkpoint_use()
434 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
441 float race_waypointsprite_visible_for_player(entity e)
443 if(e.race_checkpoint == -1)
445 else if(e.race_checkpoint == self.owner.race_checkpoint)
452 void trigger_race_checkpoint_verify()
462 qual = g_race_qualifying;
466 self.classname = "player";
470 for(i = 0; i <= race_highest_checkpoint; ++i)
472 self.race_checkpoint = race_NextCheckpoint(i);
474 // race only (middle of the race)
475 g_race_qualifying = 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"));
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"));
488 // race only (initial spawn)
489 g_race_qualifying = 0;
490 for(p = 1; p <= race_highest_place_spawn; ++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"));
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"));
509 g_race_qualifying = qual;
511 if(race_timed_checkpoint)
512 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
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", "", "");
525 void spawnfunc_trigger_race_checkpoint()
528 if(!g_race && !g_cts)
536 self.use = checkpoint_use;
537 if not(self.spawnflags & 1)
538 self.touch = checkpoint_touch;
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;
546 self.message = "went backwards";
548 self.message2 = "was pushed backwards by";
549 if (!self.race_penalty_reason)
550 self.race_penalty_reason = "missing a checkpoint";
552 self.race_checkpoint = self.cnt;
554 if(self.race_checkpoint > race_highest_checkpoint)
556 race_highest_checkpoint = self.race_checkpoint;
557 if(self.spawnflags & 8)
558 race_timed_checkpoint = self.race_checkpoint;
560 race_timed_checkpoint = 0;
563 if(!self.race_penalty)
565 if(self.race_checkpoint)
566 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
568 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
571 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
573 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
576 void race_AbandonRaceCheck(entity p)
578 if(race_completing && !p.race_completed)
580 p.race_completed = 1;
581 MAKE_INDEPENDENT_PLAYER(p);
582 bprint(p.netname, "^7 has abandoned the race.\n");
587 void race_StartCompleting()
592 if(p.deadflag != DEAD_NO)
593 race_AbandonRaceCheck(p);
596 void race_PreparePlayer()
598 race_ClearTime(self);
602 void race_RetractPlayer()
604 if(!g_race && !g_cts)
606 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
607 if(self.race_checkpoint == 0)
609 race_ClearTime(self);
610 self.race_checkpoint = 0;
616 if(!g_race && !g_cts)
619 race_AbandonRaceCheck(self);
624 if(!g_race && !g_cts)
626 if(self.killcount == -666 || g_race_qualifying)
627 race_PreparePlayer();
629 race_AbandonRaceCheck(self);
632 void race_PostSpawn(entity spot)
634 if(!g_race && !g_cts)
636 if(self.killcount != -666 && !g_race_qualifying)
638 if(spot.target == "")
639 // let the player run without timing, if he did not spawn at a targetting spawnpoint
640 race_PreparePlayer();
642 race_RetractPlayer();
645 if(spot.target != "" && self.race_checkpoint == -1)
646 self.race_checkpoint = 0;
651 void race_PreSpawnObserver()
653 if(!g_race && !g_cts)
655 race_PreparePlayer();
658 void spawnfunc_info_player_race (void)
660 if(!g_race && !g_cts)
666 spawnfunc_info_player_deathmatch();
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;
674 void race_ClearRecords()
679 for(i = 0; i < MAX_CHECKPOINTS; ++i)
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;
691 void race_ReadyRestart()
695 Score_NicePrint(world);
698 PlayerScore_Sort(race_place);
705 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
709 print(e.netname, " = ", ftos(e.race_place), "\n");
712 if(g_race_qualifying == 2)
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));
723 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
725 if(g_race_qualifying)
727 pl.race_penalty_accumulator += penalty;
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);
739 pl.race_penalty = time + penalty;
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);
754 if(other.race_lastpenalty != self)
756 other.race_lastpenalty = self;
757 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
763 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
766 void spawnfunc_trigger_race_penalty()
770 self.use = penalty_use;
771 if not(self.spawnflags & 1)
772 self.touch = penalty_touch;
774 if (!self.race_penalty_reason)
775 self.race_penalty_reason = "missing a checkpoint";
776 if (!self.race_penalty)
777 self.race_penalty = 5;