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)
231 if(oth.flags & FL_CLIENT)
234 WriteByte(MSG_ONE, SVC_TEMPENTITY);
235 WriteByte(MSG_ONE, TE_CSQC_RACE);
236 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
237 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
240 WriteShort(MSG_ONE, 0);
241 WriteByte(MSG_ONE, 0);
242 WriteString(MSG_ONE, "");
246 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
247 WriteByte(MSG_ONE, lother - lself);
248 WriteString(MSG_ONE, e.netname); // record holder
254 void race_ClearTime(entity e)
256 e.race_checkpoint = -1;
259 if(clienttype(e) != CLIENTTYPE_REAL)
263 WriteByte(MSG_ONE, SVC_TEMPENTITY);
264 WriteByte(MSG_ONE, TE_CSQC_RACE);
265 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
268 void checkpoint_touch()
270 if(other.classname != "player")
273 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
275 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
277 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
279 if(!self.race_checkpoint) // finish line
280 other.race_laptime = time;
282 if(g_race_qualifying)
283 race_SendNextCheckpoint(other);
285 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
291 if(self.spawnflags & 4)
292 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
296 void checkpoint_use()
302 float race_waypointsprite_for_player(entity e)
304 if(e.race_checkpoint == -1)
305 return self.modelindex;
306 else if(e.race_checkpoint == self.owner.race_checkpoint)
307 return self.modelindex;
312 void spawnfunc_trigger_race_checkpoint()
321 self.use = checkpoint_use;
322 if not(self.spawnflags & 1)
323 self.touch = checkpoint_touch;
325 o = (self.absmin + self.absmax) * 0.5;
326 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
327 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
328 self.nearestwaypointtimeout = time + 1000000000;
331 self.message = "went backwards";
333 self.race_checkpoint = self.cnt;
335 if(self.race_checkpoint > race_highest_checkpoint)
336 race_highest_checkpoint = self.race_checkpoint;
338 if(self.race_checkpoint)
340 precache_model("models/sprites/race-checkpoint.sp2");
341 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
345 precache_model("models/sprites/race-finish.sp2");
346 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
348 self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
351 void race_PreparePlayer()
353 race_ClearTime(self);
357 void race_RetractPlayer()
361 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
362 if(self.race_checkpoint == 0)
364 race_ClearTime(self);
365 self.race_checkpoint = 0;
373 if(self.killcount == -666 || g_race_qualifying)
374 race_PreparePlayer();
378 self.race_completed = 1;
379 MAKE_INDEPENDENT_PLAYER(self);
380 bprint(self.netname, "^7 has abandoned the race.\n");
381 if(clienttype(self) == CLIENTTYPE_REAL)
384 WriteByte(MSG_ONE, SVC_TEMPENTITY);
385 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
386 // he can still move, but will see the scoreboard now
391 void race_PostSpawn(entity spot)
395 if(self.killcount != -666 && !g_race_qualifying)
397 if(spot.target == "")
398 // let the player run without timing, if he did not spawn at a targetting spawnpoint
399 race_PreparePlayer();
401 race_RetractPlayer();
404 if(spot.target != "" && self.race_checkpoint == -1)
405 self.race_checkpoint = 0;
410 void race_PreSpawnObserver()
414 race_PreparePlayer();
417 void spawnfunc_info_player_race (void)
425 spawnfunc_info_player_deathmatch();
427 if(self.race_place > race_highest_place_spawn)
428 race_highest_place_spawn = self.race_place;
431 void race_ClearRecords()
436 for(i = 0; i < MAX_CHECKPOINTS; ++i)
438 race_checkpoint_records[i] = 0;
439 if(race_checkpoint_recordholders[i])
440 strunzone(race_checkpoint_recordholders[i]);
441 race_checkpoint_recordholders[i] = string_null;
448 void race_ReadyRestart()
452 if(g_race_qualifying == 2)
454 g_race_qualifying = 0;
455 independent_players = 0;
456 cvar_set("fraglimit", ftos(race_fraglimit));
460 PlayerScore_Sort(race_place);