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 void race_send_recordtime();
23 float race_NextCheckpoint(float f)
25 if(f >= race_highest_checkpoint)
31 float race_PreviousCheckpoint(float f)
36 return race_highest_checkpoint;
42 // 0 = common start/finish
45 float race_CheckpointNetworkID(float f)
47 if(race_timed_checkpoint)
51 else if(f == race_timed_checkpoint)
57 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
66 cp = e.race_checkpoint;
67 recordtime = race_checkpoint_records[cp];
68 recordholder = race_checkpoint_recordholders[cp];
69 if(recordholder == e.netname)
74 WRITESPECTATABLE_MSG_ONE({
75 WriteByte(MSG_ONE, SVC_TEMPENTITY);
76 WriteByte(MSG_ONE, TE_CSQC_RACE);
79 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
80 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
81 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
84 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
85 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
86 WriteInt24_t(MSG_ONE, recordtime);
87 WriteString(MSG_ONE, recordholder);
91 void race_InitSpectator()
94 if(msg_entity.enemy.race_laptime)
95 race_SendNextCheckpoint(msg_entity.enemy, 1);
98 void race_SendTime(entity e, float cp, float t, float tvalid)
103 if(g_race_qualifying)
104 t += e.race_penalty_accumulator;
106 t = TIME_ENCODE(t); // make integer
107 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
110 if(cp == race_timed_checkpoint) // finish line
111 if not(e.race_completed)
114 if(g_race_qualifying)
116 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
118 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
122 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
123 snew = TIME_ENCODE(time - game_starttime);
124 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
125 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
127 if(cvar("fraglimit"))
128 if(l >= cvar("fraglimit"))
129 race_StartCompleting();
133 e.race_completed = 1;
134 MAKE_INDEPENDENT_PLAYER(e);
135 bprint(e.netname, "^7 has finished the race.\n");
143 if(g_race_qualifying)
147 recordtime = race_checkpoint_records[cp];
148 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
149 if(recordholder == e.netname)
153 if(t < recordtime || recordtime == 0)
155 race_checkpoint_records[cp] = t;
156 if(race_checkpoint_recordholders[cp])
157 strunzone(race_checkpoint_recordholders[cp]);
158 race_checkpoint_recordholders[cp] = strzone(e.netname);
159 if(cp == race_timed_checkpoint)
162 string grecordholder;
168 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
169 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname"));
170 if(grecordholder == e.netname)
174 bprint(e.netname, "^7 set the all-time fastest lap record with ", TIME_ENCODED_TOSTRING(t), "\n");
175 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
176 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
177 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
178 race_send_recordtime();
180 else if(t < grecordtime)
182 if(grecordholder == "")
183 bprint(e.netname, "^7 broke his all-time fastest lap record with ", TIME_ENCODED_TOSTRING(t), "\n");
185 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", TIME_ENCODED_TOSTRING(t), "\n");
186 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
187 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
188 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
189 race_send_recordtime();
193 if(grecordholder == "")
194 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", TIME_ENCODED_TOSTRING(grecordtime), "\n");
196 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", TIME_ENCODED_TOSTRING(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 WriteInt24_t(MSG_ONE, t); // time to that intermediate
225 WriteInt24_t(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 WriteInt24_t(MSG_ONE, 0);
253 WriteByte(MSG_ONE, 0);
254 WriteString(MSG_ONE, "");
258 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
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 WriteInt24_t(MSG_ONE, 0);
277 WriteByte(MSG_ONE, 0);
278 WriteString(MSG_ONE, "");
282 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
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_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
295 e.race_penalty_accumulator = 0;
296 e.race_lastpenalty = world;
299 WRITESPECTATABLE_MSG_ONE({
300 WriteByte(MSG_ONE, SVC_TEMPENTITY);
301 WriteByte(MSG_ONE, TE_CSQC_RACE);
302 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
306 void dumpsurface(entity e)
310 print("Surfaces of ", etos(e), ":\n");
312 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
316 n = getsurfacenumpoints(e, si);
319 print(" Surface ", ftos(si), ":\n");
320 norm = getsurfacenormal(e, si);
321 print(" Normal = ", vtos(norm), "\n");
322 for(ni = 0; ni < n; ++ni)
324 vec = getsurfacepoint(e, si, ni);
325 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
330 void checkpoint_passed()
334 if(other.classname == "porto")
336 // do not allow portalling through checkpoints
337 trace_plane_normal = normalize(-1 * other.velocity);
346 if not(self.spawnflags & 2)
349 oldmsg = self.message;
352 self.message = oldmsg;
355 if(other.classname != "player")
359 * Remove unauthorized equipment
361 Portal_ClearAll(other);
363 other.porto_forbidden = 2; // decreased by 1 each StartFrame
365 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
367 if(self.race_penalty)
369 if(other.race_lastpenalty != self)
371 other.race_lastpenalty = self;
372 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
376 if(other.race_penalty)
382 if(self.spawnflags & 2)
385 oldmsg = self.message;
388 self.message = oldmsg;
391 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
393 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
395 if(!self.race_checkpoint) // start line
397 other.race_laptime = time;
398 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
399 other.race_penalty_accumulator = 0;
400 other.race_lastpenalty = world;
403 if(g_race_qualifying)
404 race_SendNextCheckpoint(other, 0);
406 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
412 if(self.spawnflags & 4)
413 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
417 void checkpoint_touch()
423 void checkpoint_use()
425 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
432 float race_waypointsprite_visible_for_player(entity e)
434 if(e.race_checkpoint == -1)
436 else if(e.race_checkpoint == self.owner.race_checkpoint)
443 void trigger_race_checkpoint_verify()
453 qual = g_race_qualifying;
457 self.classname = "player";
461 for(i = 0; i <= race_highest_checkpoint; ++i)
463 self.race_checkpoint = race_NextCheckpoint(i);
465 // race only (middle of the race)
466 g_race_qualifying = 0;
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 respawning in race) - bailing out"));
474 g_race_qualifying = 1;
475 self.race_place = race_lowest_place_spawn;
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 qualifying) - bailing out"));
479 // race only (initial spawn)
480 g_race_qualifying = 0;
481 for(p = 1; p <= race_highest_place_spawn; ++p)
484 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
485 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
493 self.race_checkpoint = race_NextCheckpoint(0);
494 g_race_qualifying = 1;
495 self.race_place = race_lowest_place_spawn;
496 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
497 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
500 g_race_qualifying = qual;
502 if(race_timed_checkpoint)
503 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
506 if(cp.race_checkpoint == 0)
507 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
508 else if(cp.race_checkpoint == race_timed_checkpoint)
509 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
516 void spawnfunc_trigger_race_checkpoint()
519 if(!g_race && !g_cts)
527 self.use = checkpoint_use;
528 if not(self.spawnflags & 1)
529 self.touch = checkpoint_touch;
531 o = (self.absmin + self.absmax) * 0.5;
532 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
533 waypoint_spawnforitem_force(self, trace_endpos);
534 self.nearestwaypointtimeout = time + 1000000000;
537 self.message = "went backwards";
539 self.message2 = "was pushed backwards by";
540 if (!self.race_penalty_reason)
541 self.race_penalty_reason = "missing a checkpoint";
543 self.race_checkpoint = self.cnt;
545 if(self.race_checkpoint > race_highest_checkpoint)
547 race_highest_checkpoint = self.race_checkpoint;
548 if(self.spawnflags & 8)
549 race_timed_checkpoint = self.race_checkpoint;
551 race_timed_checkpoint = 0;
554 if(!self.race_penalty)
556 if(self.race_checkpoint)
557 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
559 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
562 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
564 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
567 void race_AbandonRaceCheck(entity p)
569 if(race_completing && !p.race_completed)
571 p.race_completed = 1;
572 MAKE_INDEPENDENT_PLAYER(p);
573 bprint(p.netname, "^7 has abandoned the race.\n");
578 void race_StartCompleting()
583 if(p.deadflag != DEAD_NO)
584 race_AbandonRaceCheck(p);
587 void race_PreparePlayer()
589 race_ClearTime(self);
593 void race_RetractPlayer()
595 if(!g_race && !g_cts)
597 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
598 if(self.race_checkpoint == 0)
600 race_ClearTime(self);
601 self.race_checkpoint = 0;
607 if(!g_race && !g_cts)
610 race_AbandonRaceCheck(self);
615 if(!g_race && !g_cts)
617 if(self.killcount == -666 || g_race_qualifying)
618 race_PreparePlayer();
620 race_AbandonRaceCheck(self);
623 void race_PostSpawn(entity spot)
625 if(!g_race && !g_cts)
627 if(self.killcount != -666 && !g_race_qualifying)
629 if(spot.target == "")
630 // let the player run without timing, if he did not spawn at a targetting spawnpoint
631 race_PreparePlayer();
633 race_RetractPlayer();
636 if(spot.target != "" && self.race_checkpoint == -1)
637 self.race_checkpoint = 0;
642 void race_PreSpawnObserver()
644 if(!g_race && !g_cts)
646 race_PreparePlayer();
649 void spawnfunc_info_player_race (void)
651 if(!g_race && !g_cts)
657 spawnfunc_info_player_deathmatch();
659 if(self.race_place > race_highest_place_spawn)
660 race_highest_place_spawn = self.race_place;
661 if(self.race_place < race_lowest_place_spawn)
662 race_lowest_place_spawn = self.race_place;
665 void race_ClearRecords()
670 for(i = 0; i < MAX_CHECKPOINTS; ++i)
672 race_checkpoint_records[i] = 0;
673 if(race_checkpoint_recordholders[i])
674 strunzone(race_checkpoint_recordholders[i]);
675 race_checkpoint_recordholders[i] = string_null;
682 void race_ReadyRestart()
686 Score_NicePrint(world);
689 PlayerScore_Sort(race_place);
696 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
700 print(e.netname, " = ", ftos(e.race_place), "\n");
703 if(g_race_qualifying == 2)
705 g_race_qualifying = 0;
706 independent_players = 0;
707 cvar_set("fraglimit", ftos(race_fraglimit));
708 cvar_set("leadlimit", ftos(race_leadlimit));
709 cvar_set("timelimit", ftos(race_timelimit));
714 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
716 if(g_race_qualifying)
718 pl.race_penalty_accumulator += penalty;
720 WRITESPECTATABLE_MSG_ONE({
721 WriteByte(MSG_ONE, SVC_TEMPENTITY);
722 WriteByte(MSG_ONE, TE_CSQC_RACE);
723 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
724 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
725 WriteString(MSG_ONE, reason);
730 pl.race_penalty = time + penalty;
732 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
733 WriteByte(MSG_ONE, SVC_TEMPENTITY);
734 WriteByte(MSG_ONE, TE_CSQC_RACE);
735 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
736 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
737 WriteString(MSG_ONE, reason);
745 if(other.race_lastpenalty != self)
747 other.race_lastpenalty = self;
748 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
754 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
757 void spawnfunc_trigger_race_penalty()
761 self.use = penalty_use;
762 if not(self.spawnflags & 1)
763 self.touch = penalty_touch;
765 if (!self.race_penalty_reason)
766 self.race_penalty_reason = "missing a checkpoint";
767 if (!self.race_penalty)
768 self.race_penalty = 5;
771 void race_send_recordtime()
779 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
780 // send the server best time
781 WriteByte(MSG_ONE, SVC_TEMPENTITY);
782 WriteByte(MSG_ONE, TE_CSQC_RACE);
783 WriteByte(MSG_ONE, RACE_NET_SERVER_RECORD);
784 WriteInt24_t(MSG_ONE, grecordtime);