1 #define MAX_CHECKPOINTS 255
3 .float race_checkpoint; // player: next checkpoint that has to be reached
8 float race_checkpoint_records[MAX_CHECKPOINTS];
9 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
10 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
11 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
12 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
14 float race_highest_checkpoint;
15 float race_timed_checkpoint;
17 float race_NextCheckpoint(float f)
19 if(f >= race_highest_checkpoint)
25 float race_PreviousCheckpoint(float f)
30 return race_highest_checkpoint;
36 // 0 = common start/finish
39 float race_CheckpointNetworkID(float f)
41 if(race_timed_checkpoint)
45 else if(f == race_timed_checkpoint)
51 void race_SendNextCheckpoint(entity e, float spec)
60 cp = e.race_checkpoint;
61 recordtime = race_checkpoint_records[cp];
62 recordholder = race_checkpoint_recordholders[cp];
64 recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
65 recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
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);
81 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
82 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
83 WriteShort(MSG_ONE, recordtime);
84 WriteString(MSG_ONE, recordholder);
88 void race_InitSpectator()
91 if(msg_entity.enemy.race_laptime)
92 race_SendNextCheckpoint(msg_entity.enemy, 1);
95 void race_SendTime(entity e, float cp, float t, float tvalid)
100 t = floor(0.4 + 10 * t); // make integer
101 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
104 if(cp == race_timed_checkpoint) // finish line
105 if not(e.race_completed)
108 if(g_race_qualifying)
110 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
112 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
116 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
117 snew = floor(0.5 + 10 * (time - game_starttime));
118 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
119 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
121 if(cvar("fraglimit"))
122 if(l >= cvar("fraglimit"))
126 if(p.deadflag != DEAD_NO)
127 race_AbandonRaceCheck(p);
132 e.race_completed = 1;
133 MAKE_INDEPENDENT_PLAYER(e);
134 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;
163 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
164 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
165 if(grecordholder == e.netname)
169 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
170 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
171 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
172 write_recordmarker(e, time - t/10, t/10);
174 else if(t < grecordtime)
176 if(grecordholder == "")
177 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
179 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
180 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
181 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
182 write_recordmarker(e, time - t/10, t/10);
186 if(grecordholder == "")
187 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
189 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
193 if(g_race_qualifying)
195 FOR_EACH_REALPLAYER(p)
196 if(p.race_checkpoint == cp)
197 race_SendNextCheckpoint(p, 0);
210 if(g_race_qualifying)
212 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
213 WriteByte(MSG_ONE, SVC_TEMPENTITY);
214 WriteByte(MSG_ONE, TE_CSQC_RACE);
215 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
216 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
217 WriteShort(MSG_ONE, t); // time to that intermediate
218 WriteShort(MSG_ONE, recordtime); // previously best time
219 WriteString(MSG_ONE, recordholder); // record holder
223 else // RACE! Not Qualifying
225 float lself, lother, othtime;
227 oth = race_checkpoint_lastplayers[cp];
230 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
231 lother = race_checkpoint_lastlaps[cp];
232 othtime = race_checkpoint_lasttimes[cp];
235 lself = lother = othtime = 0;
238 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
239 WriteByte(MSG_ONE, SVC_TEMPENTITY);
240 WriteByte(MSG_ONE, TE_CSQC_RACE);
241 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
242 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
245 WriteShort(MSG_ONE, 0);
246 WriteByte(MSG_ONE, 0);
247 WriteString(MSG_ONE, "");
251 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
252 WriteByte(MSG_ONE, lself - lother);
253 WriteString(MSG_ONE, oth.netname); // record holder
257 race_checkpoint_lastplayers[cp] = e;
258 race_checkpoint_lasttimes[cp] = time;
259 race_checkpoint_lastlaps[cp] = lself;
262 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
263 WriteByte(MSG_ONE, SVC_TEMPENTITY);
264 WriteByte(MSG_ONE, TE_CSQC_RACE);
265 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
266 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
269 WriteShort(MSG_ONE, 0);
270 WriteByte(MSG_ONE, 0);
271 WriteString(MSG_ONE, "");
275 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
276 WriteByte(MSG_ONE, lother - lself);
277 WriteString(MSG_ONE, e.netname); // record holder
283 void race_ClearTime(entity e)
285 e.race_checkpoint = -1;
289 WRITESPECTATABLE_MSG_ONE({
290 WriteByte(MSG_ONE, SVC_TEMPENTITY);
291 WriteByte(MSG_ONE, TE_CSQC_RACE);
292 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
296 void dumpsurface(entity e)
300 print("Surfaces of ", etos(e), ":\n");
302 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
306 n = getsurfacenumpoints(e, si);
309 print(" Surface ", ftos(si), ":\n");
310 norm = getsurfacenormal(e, si);
311 print(" Normal = ", vtos(norm), "\n");
312 for(ni = 0; ni < n; ++ni)
314 vec = getsurfacepoint(e, si, ni);
315 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
320 void checkpoint_passed()
324 if(other.classname == "porto")
326 // do not allow portalling through checkpoints
327 trace_plane_normal = normalize(-1 * other.velocity);
336 if not(self.spawnflags & 2)
339 oldmsg = self.message;
342 self.message = oldmsg;
345 if(other.classname != "player")
349 * Remove unauthorized equipment
351 Portal_ClearAll(other);
353 other.porto_forbidden = 2; // decreased by 1 each StartFrame
355 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
360 if(self.spawnflags & 2)
363 oldmsg = self.message;
366 self.message = oldmsg;
369 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
371 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
373 if(!self.race_checkpoint) // start line
374 other.race_laptime = time;
376 if(g_race_qualifying)
377 race_SendNextCheckpoint(other, 0);
379 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
385 if(self.spawnflags & 4)
386 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
390 void checkpoint_touch()
396 void checkpoint_use()
398 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
405 float race_waypointsprite_visible_for_player(entity e)
407 if(e.race_checkpoint == -1)
409 else if(e.race_checkpoint == self.owner.race_checkpoint)
416 void trigger_race_checkpoint_verify()
426 qual = g_race_qualifying;
430 self.classname = "player";
432 for(i = 0; i <= race_highest_checkpoint; ++i)
434 self.race_checkpoint = race_NextCheckpoint(i);
436 // race only (middle of the race)
437 g_race_qualifying = 0;
439 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
440 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
445 g_race_qualifying = 1;
446 self.race_place = race_lowest_place_spawn;
447 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
448 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
450 // race only (initial spawn)
451 g_race_qualifying = 0;
452 for(p = 1; p <= race_highest_place_spawn; ++p)
455 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
456 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
461 g_race_qualifying = qual;
463 if(race_timed_checkpoint)
464 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
465 if(cp.race_checkpoint == 0)
466 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
467 else if(cp.race_checkpoint == race_timed_checkpoint)
468 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
474 void spawnfunc_trigger_race_checkpoint()
485 self.use = checkpoint_use;
486 if not(self.spawnflags & 1)
487 self.touch = checkpoint_touch;
489 o = (self.absmin + self.absmax) * 0.5;
490 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
491 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
492 self.nearestwaypointtimeout = time + 1000000000;
495 self.message = "went backwards";
497 self.message2 = "was pushed backwards by";
499 self.race_checkpoint = self.cnt;
501 if(self.race_checkpoint > race_highest_checkpoint)
503 race_highest_checkpoint = self.race_checkpoint;
504 if(self.spawnflags & 8)
505 race_timed_checkpoint = self.race_checkpoint;
507 race_timed_checkpoint = 0;
510 if(self.race_checkpoint)
511 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
513 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
515 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
517 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
520 void race_AbandonRaceCheck(entity p)
522 if(race_completing && !p.race_completed)
524 p.race_completed = 1;
525 MAKE_INDEPENDENT_PLAYER(p);
526 bprint(p.netname, "^7 has abandoned the race.\n");
531 void race_PreparePlayer()
533 race_ClearTime(self);
537 void race_RetractPlayer()
541 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
542 if(self.race_checkpoint == 0)
544 race_ClearTime(self);
545 self.race_checkpoint = 0;
554 race_AbandonRaceCheck(self);
561 if(self.killcount == -666 || g_race_qualifying)
562 race_PreparePlayer();
564 race_AbandonRaceCheck(self);
567 void race_PostSpawn(entity spot)
571 if(self.killcount != -666 && !g_race_qualifying)
573 if(spot.target == "")
574 // let the player run without timing, if he did not spawn at a targetting spawnpoint
575 race_PreparePlayer();
577 race_RetractPlayer();
580 if(spot.target != "" && self.race_checkpoint == -1)
581 self.race_checkpoint = 0;
586 void race_PreSpawnObserver()
590 race_PreparePlayer();
593 void spawnfunc_info_player_race (void)
601 spawnfunc_info_player_deathmatch();
603 if(self.race_place > race_highest_place_spawn)
604 race_highest_place_spawn = self.race_place;
605 if(self.race_place < race_lowest_place_spawn)
606 race_lowest_place_spawn = self.race_place;
609 void race_ClearRecords()
614 for(i = 0; i < MAX_CHECKPOINTS; ++i)
616 race_checkpoint_records[i] = 0;
617 if(race_checkpoint_recordholders[i])
618 strunzone(race_checkpoint_recordholders[i]);
619 race_checkpoint_recordholders[i] = string_null;
626 void race_ReadyRestart()
628 Score_NicePrint(world);
631 PlayerScore_Sort(race_place);
635 print(e.netname, " = ", ftos(e.race_place), "\n");
637 if(g_race_qualifying == 2)
639 g_race_qualifying = 0;
640 independent_players = 0;
641 cvar_set("fraglimit", ftos(race_fraglimit));
642 cvar_set("leadlimit", ftos(race_leadlimit));