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_INTERMISSION);
102 // he can still move, but will see the scoreboard now
111 if(g_race_qualifying)
115 recordtime = race_checkpoint_records[cp];
116 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
117 if(recordholder == e.netname)
120 if(t < recordtime || recordtime == 0)
122 race_checkpoint_records[cp] = t;
123 if(race_checkpoint_recordholders[cp])
124 strunzone(race_checkpoint_recordholders[cp]);
125 race_checkpoint_recordholders[cp] = strzone(e.netname);
129 string grecordholder;
130 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
131 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
132 if(grecordholder == e.netname)
136 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
137 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
138 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
140 else if(t < grecordtime)
142 if(grecordholder == "")
143 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
145 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
146 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
147 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
151 if(grecordholder == "")
152 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
154 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
158 if(g_race_qualifying)
161 FOR_EACH_REALPLAYER(p)
162 if(p.race_checkpoint == cp)
163 race_SendNextCheckpoint(p);
175 if(clienttype(e) == CLIENTTYPE_REAL)
178 if(g_race_qualifying)
180 WriteByte(MSG_ONE, SVC_TEMPENTITY);
181 WriteByte(MSG_ONE, TE_CSQC_RACE);
182 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
183 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
184 WriteShort(MSG_ONE, t); // time to that intermediate
185 WriteShort(MSG_ONE, recordtime); // previously best time
186 WriteString(MSG_ONE, recordholder); // record holder
190 else // RACE! Not Qualifying
192 float lself, lother, othtime;
194 oth = race_checkpoint_lastplayers[cp];
197 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
198 lother = race_checkpoint_lastlaps[cp];
199 othtime = race_checkpoint_lasttimes[cp];
202 lself = lother = othtime = 0;
204 if(clienttype(e) == CLIENTTYPE_REAL)
207 WriteByte(MSG_ONE, SVC_TEMPENTITY);
208 WriteByte(MSG_ONE, TE_CSQC_RACE);
209 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
210 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
213 WriteShort(MSG_ONE, 0);
214 WriteByte(MSG_ONE, 0);
215 WriteString(MSG_ONE, "");
219 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
220 WriteByte(MSG_ONE, lself - lother);
221 WriteString(MSG_ONE, oth.netname); // record holder
225 race_checkpoint_lastplayers[cp] = e;
226 race_checkpoint_lasttimes[cp] = time;
227 race_checkpoint_lastlaps[cp] = lself;
229 if(clienttype(oth) == CLIENTTYPE_REAL)
232 WriteByte(MSG_ONE, SVC_TEMPENTITY);
233 WriteByte(MSG_ONE, TE_CSQC_RACE);
234 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
235 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
238 WriteShort(MSG_ONE, 0);
239 WriteByte(MSG_ONE, 0);
240 WriteString(MSG_ONE, "");
244 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
245 WriteByte(MSG_ONE, lother - lself);
246 WriteString(MSG_ONE, e.netname); // record holder
252 void race_ClearTime(entity e)
254 e.race_checkpoint = -1;
257 if(clienttype(e) != CLIENTTYPE_REAL)
261 WriteByte(MSG_ONE, SVC_TEMPENTITY);
262 WriteByte(MSG_ONE, TE_CSQC_RACE);
263 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
266 void checkpoint_touch()
268 if(other.classname != "player")
271 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
273 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
275 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
277 if(!self.race_checkpoint) // finish line
278 other.race_laptime = time;
280 if(g_race_qualifying)
281 race_SendNextCheckpoint(other);
283 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
289 if(self.spawnflags & 4)
290 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
294 void checkpoint_use()
300 float race_waypointsprite_for_player(entity e)
302 if(e.race_checkpoint == -1)
303 return self.modelindex;
304 else if(e.race_checkpoint == self.owner.race_checkpoint)
305 return self.modelindex;
310 void spawnfunc_trigger_race_checkpoint()
319 self.use = checkpoint_use;
320 if not(self.spawnflags & 1)
321 self.touch = checkpoint_touch;
323 o = (self.absmin + self.absmax) * 0.5;
324 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
325 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
326 self.nearestwaypointtimeout = time + 1000000000;
329 self.message = "went backwards";
331 self.race_checkpoint = self.cnt;
333 if(self.race_checkpoint > race_highest_checkpoint)
334 race_highest_checkpoint = self.race_checkpoint;
336 if(self.race_checkpoint)
338 precache_model("models/sprites/race-checkpoint.sp2");
339 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
343 precache_model("models/sprites/race-finish.sp2");
344 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
346 self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
349 void race_PreparePlayer()
351 race_ClearTime(self);
355 void race_RetractPlayer()
359 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
360 if(self.race_checkpoint == 0)
362 race_ClearTime(self);
363 self.race_checkpoint = 0;
371 if(self.killcount == -666 || g_race_qualifying)
372 race_PreparePlayer();
375 void race_PostSpawn(entity spot)
379 if(self.killcount != -666 && !g_race_qualifying)
381 if(spot.target == "")
382 // let the player run without timing, if he did not spawn at a targetting spawnpoint
383 race_PreparePlayer();
385 race_RetractPlayer();
388 if(spot.target != "" && self.race_checkpoint == -1)
389 self.race_checkpoint = 0;
394 void race_PreSpawnObserver()
398 race_PreparePlayer();
401 void spawnfunc_info_player_race (void)
409 spawnfunc_info_player_deathmatch();
411 if(self.race_place > race_highest_place_spawn)
412 race_highest_place_spawn = self.race_place;
415 void race_ClearRecords()
420 for(i = 0; i < MAX_CHECKPOINTS; ++i)
422 race_checkpoint_records[i] = 0;
423 if(race_checkpoint_recordholders[i])
424 strunzone(race_checkpoint_recordholders[i]);
425 race_checkpoint_recordholders[i] = string_null;
432 void race_ReadyRestart()
436 if(g_race_qualifying == 2)
438 g_race_qualifying = 0;
439 independent_players = 0;
440 cvar_set("fraglimit", ftos(race_fraglimit));
444 PlayerScore_Sort(race_place);