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"))
130 race_StartCompleting();
134 e.race_completed = 1;
135 MAKE_INDEPENDENT_PLAYER(e);
136 bprint(e.netname, "^7 has finished the race.\n");
145 if(g_race_qualifying)
149 recordtime = race_checkpoint_records[cp];
150 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
151 if(recordholder == e.netname)
155 if(t < recordtime || recordtime == 0)
157 race_checkpoint_records[cp] = t;
158 if(race_checkpoint_recordholders[cp])
159 strunzone(race_checkpoint_recordholders[cp]);
160 race_checkpoint_recordholders[cp] = strzone(e.netname);
161 if(cp == race_timed_checkpoint)
164 string grecordholder;
165 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
166 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
167 if(grecordholder == e.netname)
171 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
172 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
173 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
174 write_recordmarker(e, time - t/10, t/10);
176 else if(t < grecordtime)
178 if(grecordholder == "")
179 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
181 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
182 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
183 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
184 write_recordmarker(e, time - t/10, t/10);
188 if(grecordholder == "")
189 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
191 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
195 if(g_race_qualifying)
197 FOR_EACH_REALPLAYER(p)
198 if(p.race_checkpoint == cp)
199 race_SendNextCheckpoint(p, 0);
212 if(g_race_qualifying)
214 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
215 WriteByte(MSG_ONE, SVC_TEMPENTITY);
216 WriteByte(MSG_ONE, TE_CSQC_RACE);
217 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
218 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
219 WriteShort(MSG_ONE, t); // time to that intermediate
220 WriteShort(MSG_ONE, recordtime); // previously best time
221 WriteString(MSG_ONE, recordholder); // record holder
225 else // RACE! Not Qualifying
227 float lself, lother, othtime;
229 oth = race_checkpoint_lastplayers[cp];
232 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
233 lother = race_checkpoint_lastlaps[cp];
234 othtime = race_checkpoint_lasttimes[cp];
237 lself = lother = othtime = 0;
240 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
241 WriteByte(MSG_ONE, SVC_TEMPENTITY);
242 WriteByte(MSG_ONE, TE_CSQC_RACE);
243 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
244 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
247 WriteShort(MSG_ONE, 0);
248 WriteByte(MSG_ONE, 0);
249 WriteString(MSG_ONE, "");
253 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
254 WriteByte(MSG_ONE, lself - lother);
255 WriteString(MSG_ONE, oth.netname); // record holder
259 race_checkpoint_lastplayers[cp] = e;
260 race_checkpoint_lasttimes[cp] = time;
261 race_checkpoint_lastlaps[cp] = lself;
264 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
265 WriteByte(MSG_ONE, SVC_TEMPENTITY);
266 WriteByte(MSG_ONE, TE_CSQC_RACE);
267 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
268 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
271 WriteShort(MSG_ONE, 0);
272 WriteByte(MSG_ONE, 0);
273 WriteString(MSG_ONE, "");
277 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
278 WriteByte(MSG_ONE, lother - lself);
279 WriteString(MSG_ONE, e.netname); // record holder
285 void race_ClearTime(entity e)
287 e.race_checkpoint = -1;
289 e.race_penalty_accumulator = 0;
290 e.race_lastpenalty = world;
293 WRITESPECTATABLE_MSG_ONE({
294 WriteByte(MSG_ONE, SVC_TEMPENTITY);
295 WriteByte(MSG_ONE, TE_CSQC_RACE);
296 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
300 void dumpsurface(entity e)
304 print("Surfaces of ", etos(e), ":\n");
306 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
310 n = getsurfacenumpoints(e, si);
313 print(" Surface ", ftos(si), ":\n");
314 norm = getsurfacenormal(e, si);
315 print(" Normal = ", vtos(norm), "\n");
316 for(ni = 0; ni < n; ++ni)
318 vec = getsurfacepoint(e, si, ni);
319 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
324 void checkpoint_passed()
328 if(other.classname == "porto")
330 // do not allow portalling through checkpoints
331 trace_plane_normal = normalize(-1 * other.velocity);
340 if not(self.spawnflags & 2)
343 oldmsg = self.message;
346 self.message = oldmsg;
349 if(other.classname != "player")
353 * Remove unauthorized equipment
355 Portal_ClearAll(other);
357 other.porto_forbidden = 2; // decreased by 1 each StartFrame
359 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
361 if(self.race_penalty)
363 if(other.race_lastpenalty != self)
365 other.race_lastpenalty = self;
366 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
370 if(other.race_penalty)
376 if(self.spawnflags & 2)
379 oldmsg = self.message;
382 self.message = oldmsg;
385 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
387 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
389 if(!self.race_checkpoint) // start line
391 other.race_laptime = time;
392 other.race_penalty_accumulator = 0;
393 other.race_lastpenalty = world;
396 if(g_race_qualifying)
397 race_SendNextCheckpoint(other, 0);
399 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
405 if(self.spawnflags & 4)
406 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
410 void checkpoint_touch()
416 void checkpoint_use()
418 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
425 float race_waypointsprite_visible_for_player(entity e)
427 if(e.race_checkpoint == -1)
429 else if(e.race_checkpoint == self.owner.race_checkpoint)
436 void trigger_race_checkpoint_verify()
446 qual = g_race_qualifying;
450 self.classname = "player";
452 for(i = 0; i <= race_highest_checkpoint; ++i)
454 self.race_checkpoint = race_NextCheckpoint(i);
456 // race only (middle of the race)
457 g_race_qualifying = 0;
459 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
460 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
465 g_race_qualifying = 1;
466 self.race_place = race_lowest_place_spawn;
467 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
468 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
470 // race only (initial spawn)
471 g_race_qualifying = 0;
472 for(p = 1; p <= race_highest_place_spawn; ++p)
475 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
476 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
481 g_race_qualifying = qual;
483 if(race_timed_checkpoint)
484 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
487 if(cp.race_checkpoint == 0)
488 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
489 else if(cp.race_checkpoint == race_timed_checkpoint)
490 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
497 void spawnfunc_trigger_race_checkpoint()
500 if(!g_race && !g_cts)
508 self.use = checkpoint_use;
509 if not(self.spawnflags & 1)
510 self.touch = checkpoint_touch;
512 o = (self.absmin + self.absmax) * 0.5;
513 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
514 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
515 self.nearestwaypointtimeout = time + 1000000000;
518 self.message = "went backwards";
520 self.message2 = "was pushed backwards by";
521 if (!self.race_penalty_reason)
522 self.race_penalty_reason = "missing a checkpoint";
524 self.race_checkpoint = self.cnt;
526 if(self.race_checkpoint > race_highest_checkpoint)
528 race_highest_checkpoint = self.race_checkpoint;
529 if(self.spawnflags & 8)
530 race_timed_checkpoint = self.race_checkpoint;
532 race_timed_checkpoint = 0;
535 if(!self.race_penalty)
537 if(self.race_checkpoint)
538 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
540 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
543 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
545 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
548 void race_AbandonRaceCheck(entity p)
550 if(race_completing && !p.race_completed)
552 p.race_completed = 1;
553 MAKE_INDEPENDENT_PLAYER(p);
554 bprint(p.netname, "^7 has abandoned the race.\n");
559 void race_StartCompleting()
564 if(p.deadflag != DEAD_NO)
565 race_AbandonRaceCheck(p);
568 void race_PreparePlayer()
570 race_ClearTime(self);
574 void race_RetractPlayer()
576 if(!g_race && !g_cts)
578 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
579 if(self.race_checkpoint == 0)
581 race_ClearTime(self);
582 self.race_checkpoint = 0;
588 if(!g_race && !g_cts)
591 race_AbandonRaceCheck(self);
596 if(!g_race && !g_cts)
598 if(self.killcount == -666 || g_race_qualifying)
599 race_PreparePlayer();
601 race_AbandonRaceCheck(self);
604 void race_PostSpawn(entity spot)
606 if(!g_race && !g_cts)
608 if(self.killcount != -666 && !g_race_qualifying)
610 if(spot.target == "")
611 // let the player run without timing, if he did not spawn at a targetting spawnpoint
612 race_PreparePlayer();
614 race_RetractPlayer();
617 if(spot.target != "" && self.race_checkpoint == -1)
618 self.race_checkpoint = 0;
623 void race_PreSpawnObserver()
625 if(!g_race && !g_cts)
627 race_PreparePlayer();
630 void spawnfunc_info_player_race (void)
632 if(!g_race && !g_cts)
638 spawnfunc_info_player_deathmatch();
640 if(self.race_place > race_highest_place_spawn)
641 race_highest_place_spawn = self.race_place;
642 if(self.race_place < race_lowest_place_spawn)
643 race_lowest_place_spawn = self.race_place;
646 void race_ClearRecords()
651 for(i = 0; i < MAX_CHECKPOINTS; ++i)
653 race_checkpoint_records[i] = 0;
654 if(race_checkpoint_recordholders[i])
655 strunzone(race_checkpoint_recordholders[i]);
656 race_checkpoint_recordholders[i] = string_null;
663 void race_ReadyRestart()
667 Score_NicePrint(world);
670 PlayerScore_Sort(race_place);
677 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
681 print(e.netname, " = ", ftos(e.race_place), "\n");
684 if(g_race_qualifying == 2)
686 g_race_qualifying = 0;
687 independent_players = 0;
688 cvar_set("fraglimit", ftos(race_fraglimit));
689 cvar_set("leadlimit", ftos(race_leadlimit));
690 cvar_set("timelimit", ftos(race_timelimit));
695 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
697 if(g_race_qualifying)
699 pl.race_penalty_accumulator += penalty;
701 WRITESPECTATABLE_MSG_ONE({
702 WriteByte(MSG_ONE, SVC_TEMPENTITY);
703 WriteByte(MSG_ONE, TE_CSQC_RACE);
704 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
705 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
706 WriteString(MSG_ONE, reason);
711 pl.race_penalty = time + penalty;
713 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
714 WriteByte(MSG_ONE, SVC_TEMPENTITY);
715 WriteByte(MSG_ONE, TE_CSQC_RACE);
716 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
717 WriteByte(MSG_ONE, floor(0.4 + 10 * penalty));
718 WriteString(MSG_ONE, reason);
726 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
732 if(other.race_lastpenalty != self)
734 other.race_lastpenalty = self;
735 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
739 void spawnfunc_trigger_race_penalty()
743 self.use = penalty_use;
744 if not(self.spawnflags & 1)
745 self.touch = penalty_touch;
747 if (!self.race_penalty_reason)
748 self.race_penalty_reason = "missing a checkpoint";
749 if (!self.race_penalty)
750 self.race_penalty = 5;