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_highest_place_spawn;
17 float race_NextCheckpoint(float f)
19 if(f >= race_highest_checkpoint)
25 float race_PreviousCheckpoint(float f)
30 return race_highest_checkpoint;
35 void race_SendNextCheckpoint(entity e)
41 if(clienttype(e) != CLIENTTYPE_REAL)
47 cp = e.race_checkpoint;
48 recordtime = race_checkpoint_records[cp];
49 recordholder = race_checkpoint_recordholders[cp];
51 recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
52 recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
54 if(recordholder == e.netname)
58 WriteByte(MSG_ONE, SVC_TEMPENTITY);
59 WriteByte(MSG_ONE, TE_CSQC_RACE);
60 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
61 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
62 WriteShort(MSG_ONE, recordtime);
63 WriteString(MSG_ONE, recordholder);
66 void race_SendTime(entity e, float cp, float t, float tvalid)
69 t = floor(0.5 + 10 * t); // make integer
72 if(cp == 0) // finish line
73 if not(e.race_completed)
78 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
80 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
84 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
85 snew = floor(0.5 + 10 * (time - restart_countdown));
86 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
87 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
90 if(l >= cvar("fraglimit"))
96 MAKE_INDEPENDENT_PLAYER(e);
97 bprint(e.netname, "^7 has finished the race.\n");
98 if(clienttype(e) == CLIENTTYPE_REAL)
101 WriteByte(MSG_ONE, SVC_TEMPENTITY);
102 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
103 // he can still move, but will see the scoreboard now
112 if(g_race_qualifying)
116 recordtime = race_checkpoint_records[cp];
117 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
118 if(recordholder == e.netname)
121 if(t < recordtime || recordtime == 0)
123 race_checkpoint_records[cp] = t;
124 if(race_checkpoint_recordholders[cp])
125 strunzone(race_checkpoint_recordholders[cp]);
126 race_checkpoint_recordholders[cp] = strzone(e.netname);
130 string grecordholder;
131 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
132 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
133 if(grecordholder == e.netname)
137 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
138 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
139 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
141 else if(t < grecordtime)
143 if(grecordholder == "")
144 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
146 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
147 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
148 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
152 if(grecordholder == "")
153 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
155 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
159 if(g_race_qualifying)
162 FOR_EACH_REALPLAYER(p)
163 if(p.race_checkpoint == cp)
164 race_SendNextCheckpoint(p);
176 if(clienttype(e) == CLIENTTYPE_REAL)
179 if(g_race_qualifying)
181 WriteByte(MSG_ONE, SVC_TEMPENTITY);
182 WriteByte(MSG_ONE, TE_CSQC_RACE);
183 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
184 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
185 WriteShort(MSG_ONE, t); // time to that intermediate
186 WriteShort(MSG_ONE, recordtime); // previously best time
187 WriteString(MSG_ONE, recordholder); // record holder
191 else // RACE! Not Qualifying
193 float lself, lother, othtime;
195 oth = race_checkpoint_lastplayers[cp];
198 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
199 lother = race_checkpoint_lastlaps[cp];
200 othtime = race_checkpoint_lasttimes[cp];
203 lself = lother = othtime = 0;
205 if(clienttype(e) == CLIENTTYPE_REAL)
208 WriteByte(MSG_ONE, SVC_TEMPENTITY);
209 WriteByte(MSG_ONE, TE_CSQC_RACE);
210 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
211 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
214 WriteShort(MSG_ONE, 0);
215 WriteByte(MSG_ONE, 0);
216 WriteString(MSG_ONE, "");
220 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
221 WriteByte(MSG_ONE, lself - lother);
222 WriteString(MSG_ONE, oth.netname); // record holder
226 race_checkpoint_lastplayers[cp] = e;
227 race_checkpoint_lasttimes[cp] = time;
228 race_checkpoint_lastlaps[cp] = lself;
230 if(clienttype(oth) == CLIENTTYPE_REAL)
233 WriteByte(MSG_ONE, SVC_TEMPENTITY);
234 WriteByte(MSG_ONE, TE_CSQC_RACE);
235 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
236 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
239 WriteShort(MSG_ONE, 0);
240 WriteByte(MSG_ONE, 0);
241 WriteString(MSG_ONE, "");
245 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
246 WriteByte(MSG_ONE, lother - lself);
247 WriteString(MSG_ONE, e.netname); // record holder
253 void race_ClearTime(entity e)
255 e.race_checkpoint = -1;
258 if(clienttype(e) != CLIENTTYPE_REAL)
262 WriteByte(MSG_ONE, SVC_TEMPENTITY);
263 WriteByte(MSG_ONE, TE_CSQC_RACE);
264 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
267 void checkpoint_touch()
269 if(other.classname != "player")
272 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
274 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
276 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
278 if(!self.race_checkpoint) // finish line
279 other.race_laptime = time;
281 if(g_race_qualifying)
282 race_SendNextCheckpoint(other);
284 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
290 if(self.spawnflags & 4)
291 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
295 void checkpoint_use()
301 float race_waypointsprite_for_player(entity e)
303 if(e.race_checkpoint == -1)
304 return self.modelindex;
305 else if(e.race_checkpoint == self.owner.race_checkpoint)
306 return self.modelindex;
311 void spawnfunc_trigger_race_checkpoint()
320 self.use = checkpoint_use;
321 if not(self.spawnflags & 1)
322 self.touch = checkpoint_touch;
324 o = (self.absmin + self.absmax) * 0.5;
325 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
326 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
327 self.nearestwaypointtimeout = time + 1000000000;
330 self.message = "went backwards";
332 self.race_checkpoint = self.cnt;
334 if(self.race_checkpoint > race_highest_checkpoint)
335 race_highest_checkpoint = self.race_checkpoint;
337 if(self.race_checkpoint)
339 precache_model("models/sprites/race-checkpoint.sp2");
340 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
344 precache_model("models/sprites/race-finish.sp2");
345 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
347 self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
350 void race_PreparePlayer()
352 race_ClearTime(self);
356 void race_RetractPlayer()
360 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
361 if(self.race_checkpoint == 0)
363 race_ClearTime(self);
364 self.race_checkpoint = 0;
372 if(self.killcount == -666 || g_race_qualifying)
373 race_PreparePlayer();
377 self.race_completed = 1;
378 MAKE_INDEPENDENT_PLAYER(self);
379 bprint(self.netname, "^7 has abandoned the race.\n");
380 if(clienttype(self) == CLIENTTYPE_REAL)
383 WriteByte(MSG_ONE, SVC_TEMPENTITY);
384 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
385 // he can still move, but will see the scoreboard now
390 void race_PostSpawn(entity spot)
394 if(self.killcount != -666 && !g_race_qualifying)
396 if(spot.target == "")
397 // let the player run without timing, if he did not spawn at a targetting spawnpoint
398 race_PreparePlayer();
400 race_RetractPlayer();
403 if(spot.target != "" && self.race_checkpoint == -1)
404 self.race_checkpoint = 0;
409 void race_PreSpawnObserver()
413 race_PreparePlayer();
416 void spawnfunc_info_player_race (void)
424 spawnfunc_info_player_deathmatch();
426 if(self.race_place > race_highest_place_spawn)
427 race_highest_place_spawn = self.race_place;
430 void race_ClearRecords()
435 for(i = 0; i < MAX_CHECKPOINTS; ++i)
437 race_checkpoint_records[i] = 0;
438 if(race_checkpoint_recordholders[i])
439 strunzone(race_checkpoint_recordholders[i]);
440 race_checkpoint_recordholders[i] = string_null;
447 void race_ReadyRestart()
451 if(g_race_qualifying == 2)
453 g_race_qualifying = 0;
454 independent_players = 0;
455 cvar_set("fraglimit", ftos(race_fraglimit));
459 PlayerScore_Sort(race_place);