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;
16 float race_NextCheckpoint(float f)
18 if(f >= race_highest_checkpoint)
24 float race_PreviousCheckpoint(float f)
29 return race_highest_checkpoint;
34 void race_SendNextCheckpoint(entity e, float spec)
43 cp = e.race_checkpoint;
44 recordtime = race_checkpoint_records[cp];
45 recordholder = race_checkpoint_recordholders[cp];
47 recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
48 recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
50 if(recordholder == e.netname)
55 WRITESPECTATABLE_MSG_ONE({
56 WriteByte(MSG_ONE, SVC_TEMPENTITY);
57 WriteByte(MSG_ONE, TE_CSQC_RACE);
60 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
61 WriteCoord(MSG_ONE, e.race_laptime);
64 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
65 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
66 WriteShort(MSG_ONE, recordtime);
67 WriteString(MSG_ONE, recordholder);
71 void race_InitSpectator()
74 if(msg_entity.enemy.race_laptime)
75 race_SendNextCheckpoint(msg_entity.enemy, 1);
78 void race_SendTime(entity e, float cp, float t, float tvalid)
83 t = floor(0.4 + 10 * t); // make integer
84 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
87 if(cp == 0) // finish line
88 if not(e.race_completed)
93 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
95 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
99 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
100 snew = floor(0.5 + 10 * (time - game_starttime));
101 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
102 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
104 if(cvar("fraglimit"))
105 if(l >= cvar("fraglimit"))
109 if(p.deadflag != DEAD_NO)
110 race_AbandonRaceCheck(p);
115 e.race_completed = 1;
116 MAKE_INDEPENDENT_PLAYER(e);
117 bprint(e.netname, "^7 has finished the race.\n");
118 // TODO support spectators with this (e.g. set a flag for forced sbar)
119 if(clienttype(e) == CLIENTTYPE_REAL)
122 WriteByte(MSG_ONE, SVC_TEMPENTITY);
123 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
124 WriteByte(MSG_ONE, 1);
125 // he can still move, but will see the scoreboard now
134 if(g_race_qualifying)
138 recordtime = race_checkpoint_records[cp];
139 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
140 if(recordholder == e.netname)
144 if(t < recordtime || recordtime == 0)
146 race_checkpoint_records[cp] = t;
147 if(race_checkpoint_recordholders[cp])
148 strunzone(race_checkpoint_recordholders[cp]);
149 race_checkpoint_recordholders[cp] = strzone(e.netname);
153 string grecordholder;
154 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
155 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
156 if(grecordholder == e.netname)
160 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
161 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
162 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
163 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
165 else if(t < grecordtime)
167 if(grecordholder == "")
168 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
170 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
171 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
172 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
173 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
177 if(grecordholder == "")
178 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
180 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
184 if(g_race_qualifying)
186 FOR_EACH_REALPLAYER(p)
187 if(p.race_checkpoint == cp)
188 race_SendNextCheckpoint(p, 0);
201 if(g_race_qualifying)
203 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
204 WriteByte(MSG_ONE, SVC_TEMPENTITY);
205 WriteByte(MSG_ONE, TE_CSQC_RACE);
206 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
207 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
208 WriteShort(MSG_ONE, t); // time to that intermediate
209 WriteShort(MSG_ONE, recordtime); // previously best time
210 WriteString(MSG_ONE, recordholder); // record holder
214 else // RACE! Not Qualifying
216 float lself, lother, othtime;
218 oth = race_checkpoint_lastplayers[cp];
221 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
222 lother = race_checkpoint_lastlaps[cp];
223 othtime = race_checkpoint_lasttimes[cp];
226 lself = lother = othtime = 0;
229 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
230 WriteByte(MSG_ONE, SVC_TEMPENTITY);
231 WriteByte(MSG_ONE, TE_CSQC_RACE);
232 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
233 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
236 WriteShort(MSG_ONE, 0);
237 WriteByte(MSG_ONE, 0);
238 WriteString(MSG_ONE, "");
242 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
243 WriteByte(MSG_ONE, lself - lother);
244 WriteString(MSG_ONE, oth.netname); // record holder
248 race_checkpoint_lastplayers[cp] = e;
249 race_checkpoint_lasttimes[cp] = time;
250 race_checkpoint_lastlaps[cp] = lself;
253 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
254 WriteByte(MSG_ONE, SVC_TEMPENTITY);
255 WriteByte(MSG_ONE, TE_CSQC_RACE);
256 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
257 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
260 WriteShort(MSG_ONE, 0);
261 WriteByte(MSG_ONE, 0);
262 WriteString(MSG_ONE, "");
266 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
267 WriteByte(MSG_ONE, lother - lself);
268 WriteString(MSG_ONE, e.netname); // record holder
274 void race_ClearTime(entity e)
276 e.race_checkpoint = -1;
280 WRITESPECTATABLE_MSG_ONE({
281 WriteByte(MSG_ONE, SVC_TEMPENTITY);
282 WriteByte(MSG_ONE, TE_CSQC_RACE);
283 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
287 void dumpsurface(entity e)
291 print("Surfaces of ", etos(e), ":\n");
293 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
297 n = getsurfacenumpoints(e, si);
300 print(" Surface ", ftos(si), ":\n");
301 norm = getsurfacenormal(e, si);
302 print(" Normal = ", vtos(norm), "\n");
303 for(ni = 0; ni < n; ++ni)
305 vec = getsurfacepoint(e, si, ni);
306 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
311 void checkpoint_passed()
315 if(other.classname == "porto")
317 // do not allow portalling through checkpoints
318 trace_plane_normal = normalize(-1 * other.velocity);
327 if not(self.spawnflags & 2)
330 oldmsg = self.message;
333 self.message = oldmsg;
336 if(other.classname != "player")
340 * Remove unauthorized equipment
342 Portal_ClearAll(other);
344 other.porto_forbidden = 2; // decreased by 1 each StartFrame
346 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
351 if(self.spawnflags & 2)
354 oldmsg = self.message;
357 self.message = oldmsg;
360 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
362 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
364 if(!self.race_checkpoint) // finish line
365 other.race_laptime = time;
367 if(g_race_qualifying)
368 race_SendNextCheckpoint(other, 0);
370 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
376 if(self.spawnflags & 4)
377 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
381 void checkpoint_touch()
387 void checkpoint_use()
389 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
396 float race_waypointsprite_visible_for_player(entity e)
398 if(e.race_checkpoint == -1)
400 else if(e.race_checkpoint == self.owner.race_checkpoint)
406 void spawnfunc_trigger_race_checkpoint()
417 self.use = checkpoint_use;
418 if not(self.spawnflags & 1)
419 self.touch = checkpoint_touch;
421 o = (self.absmin + self.absmax) * 0.5;
422 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
423 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
424 self.nearestwaypointtimeout = time + 1000000000;
427 self.message = "went backwards";
429 self.message2 = "was pushed backwards by";
431 self.race_checkpoint = self.cnt;
433 if(self.race_checkpoint > race_highest_checkpoint)
434 race_highest_checkpoint = self.race_checkpoint;
436 if(self.race_checkpoint)
438 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
442 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
444 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
447 void race_AbandonRaceCheck(entity p)
449 if(race_completing && !p.race_completed)
451 p.race_completed = 1;
452 MAKE_INDEPENDENT_PLAYER(p);
453 bprint(p.netname, "^7 has abandoned the race.\n");
454 // TODO support spectators with this (e.g. set a flag for forced sbar)
455 if(clienttype(p) == CLIENTTYPE_REAL)
458 WriteByte(MSG_ONE, SVC_TEMPENTITY);
459 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
460 WriteByte(MSG_ONE, 1);
461 // he can still move, but will see the scoreboard now
466 void race_PreparePlayer()
468 race_ClearTime(self);
472 void race_RetractPlayer()
476 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
477 if(self.race_checkpoint == 0)
479 race_ClearTime(self);
480 self.race_checkpoint = 0;
489 race_AbandonRaceCheck(self);
496 if(self.killcount == -666 || g_race_qualifying)
497 race_PreparePlayer();
499 race_AbandonRaceCheck(self);
502 void race_PostSpawn(entity spot)
506 if(self.killcount != -666 && !g_race_qualifying)
508 if(spot.target == "")
509 // let the player run without timing, if he did not spawn at a targetting spawnpoint
510 race_PreparePlayer();
512 race_RetractPlayer();
515 if(spot.target != "" && self.race_checkpoint == -1)
516 self.race_checkpoint = 0;
521 void race_PreSpawnObserver()
525 race_PreparePlayer();
528 void spawnfunc_info_player_race (void)
536 spawnfunc_info_player_deathmatch();
538 if(self.race_place > race_highest_place_spawn)
539 race_highest_place_spawn = self.race_place;
540 if(self.race_place < race_lowest_place_spawn)
541 race_lowest_place_spawn = self.race_place;
544 void race_ClearRecords()
549 for(i = 0; i < MAX_CHECKPOINTS; ++i)
551 race_checkpoint_records[i] = 0;
552 if(race_checkpoint_recordholders[i])
553 strunzone(race_checkpoint_recordholders[i]);
554 race_checkpoint_recordholders[i] = string_null;
561 void race_ReadyRestart()
565 PlayerScore_Sort(race_place);
567 if(g_race_qualifying == 2)
569 g_race_qualifying = 0;
570 independent_players = 0;
571 cvar_set("fraglimit", ftos(race_fraglimit));