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, float spec)
44 cp = e.race_checkpoint;
45 recordtime = race_checkpoint_records[cp];
46 recordholder = race_checkpoint_recordholders[cp];
48 recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
49 recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
51 if(recordholder == e.netname)
56 WRITESPECTATABLE_MSG_ONE({
57 WriteByte(MSG_ONE, SVC_TEMPENTITY);
58 WriteByte(MSG_ONE, TE_CSQC_RACE);
61 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
62 WriteCoord(MSG_ONE, e.race_laptime);
65 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
66 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
67 WriteShort(MSG_ONE, recordtime);
68 WriteString(MSG_ONE, recordholder);
72 void race_InitSpectator()
75 if(msg_entity.enemy.race_laptime)
76 race_SendNextCheckpoint(msg_entity.enemy, 1);
79 void race_SendTime(entity e, float cp, float t, float tvalid)
82 t = floor(0.5 + 10 * t); // make integer
85 if(cp == 0) // finish line
86 if not(e.race_completed)
91 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
93 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
97 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
98 snew = floor(0.5 + 10 * (time - restart_countdown));
99 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
100 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
102 if(cvar("fraglimit"))
103 if(l >= cvar("fraglimit"))
108 e.race_completed = 1;
109 MAKE_INDEPENDENT_PLAYER(e);
110 bprint(e.netname, "^7 has finished the race.\n");
111 // TODO support spectators with this (e.g. set a flag for forced sbar)
112 if(clienttype(e) == CLIENTTYPE_REAL)
115 WriteByte(MSG_ONE, SVC_TEMPENTITY);
116 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
117 WriteByte(MSG_ONE, 1);
118 // he can still move, but will see the scoreboard now
127 if(g_race_qualifying)
131 recordtime = race_checkpoint_records[cp];
132 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
133 if(recordholder == e.netname)
136 if(t < recordtime || recordtime == 0)
138 race_checkpoint_records[cp] = t;
139 if(race_checkpoint_recordholders[cp])
140 strunzone(race_checkpoint_recordholders[cp]);
141 race_checkpoint_recordholders[cp] = strzone(e.netname);
145 string grecordholder;
146 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
147 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
148 if(grecordholder == e.netname)
152 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
153 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
154 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
155 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
157 else if(t < grecordtime)
159 if(grecordholder == "")
160 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
162 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
163 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
164 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
165 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
169 if(grecordholder == "")
170 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
172 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
176 if(g_race_qualifying)
179 FOR_EACH_REALPLAYER(p)
180 if(p.race_checkpoint == cp)
181 race_SendNextCheckpoint(p, 0);
194 if(g_race_qualifying)
196 WRITESPECTATABLE_MSG_ONE({
197 WriteByte(MSG_ONE, SVC_TEMPENTITY);
198 WriteByte(MSG_ONE, TE_CSQC_RACE);
199 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
200 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
201 WriteShort(MSG_ONE, t); // time to that intermediate
202 WriteShort(MSG_ONE, recordtime); // previously best time
203 WriteString(MSG_ONE, recordholder); // record holder
207 else // RACE! Not Qualifying
209 float lself, lother, othtime;
211 oth = race_checkpoint_lastplayers[cp];
214 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
215 lother = race_checkpoint_lastlaps[cp];
216 othtime = race_checkpoint_lasttimes[cp];
219 lself = lother = othtime = 0;
222 WRITESPECTATABLE_MSG_ONE({
223 WriteByte(MSG_ONE, SVC_TEMPENTITY);
224 WriteByte(MSG_ONE, TE_CSQC_RACE);
225 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
226 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
229 WriteShort(MSG_ONE, 0);
230 WriteByte(MSG_ONE, 0);
231 WriteString(MSG_ONE, "");
235 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
236 WriteByte(MSG_ONE, lself - lother);
237 WriteString(MSG_ONE, oth.netname); // record holder
241 race_checkpoint_lastplayers[cp] = e;
242 race_checkpoint_lasttimes[cp] = time;
243 race_checkpoint_lastlaps[cp] = lself;
246 WRITESPECTATABLE_MSG_ONE({
247 WriteByte(MSG_ONE, SVC_TEMPENTITY);
248 WriteByte(MSG_ONE, TE_CSQC_RACE);
249 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
250 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
253 WriteShort(MSG_ONE, 0);
254 WriteByte(MSG_ONE, 0);
255 WriteString(MSG_ONE, "");
259 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
260 WriteByte(MSG_ONE, lother - lself);
261 WriteString(MSG_ONE, e.netname); // record holder
267 void race_ClearTime(entity e)
269 e.race_checkpoint = -1;
273 WRITESPECTATABLE_MSG_ONE({
274 WriteByte(MSG_ONE, SVC_TEMPENTITY);
275 WriteByte(MSG_ONE, TE_CSQC_RACE);
276 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
280 void dumpsurface(entity e)
284 print("Surfaces of ", etos(e), ":\n");
286 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
290 n = getsurfacenumpoints(e, si);
293 print(" Surface ", ftos(si), ":\n");
294 norm = getsurfacenormal(e, si);
295 print(" Normal = ", vtos(norm), "\n");
296 for(ni = 0; ni < n; ++ni)
298 vec = getsurfacepoint(e, si, ni);
299 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
304 void checkpoint_touch()
311 if(other.classname == "porto")
313 // do not allow portalling through checkpoints
314 trace_plane_normal = normalize(-1 * other.velocity);
323 if not(self.spawnflags & 2)
326 oldmsg = self.message;
329 self.message = oldmsg;
332 if(other.classname != "player")
336 * Remove unauthorized equipment
338 Portal_ClearAll(other);
339 if(other.porto_current)
342 self = other.porto_current;
347 other.porto_forbidden = 2; // decreased by 1 each StartFrame
349 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
354 if(self.spawnflags & 2)
357 oldmsg = self.message;
360 self.message = oldmsg;
363 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
365 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
367 if(!self.race_checkpoint) // finish line
368 other.race_laptime = time;
370 if(g_race_qualifying)
371 race_SendNextCheckpoint(other, 0);
373 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
379 if(self.spawnflags & 4)
380 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
384 void checkpoint_use()
390 float race_waypointsprite_for_player(entity e)
392 if(e.race_checkpoint == -1)
393 return self.modelindex;
394 else if(e.race_checkpoint == self.owner.race_checkpoint)
395 return self.modelindex;
400 void spawnfunc_trigger_race_checkpoint()
411 self.use = checkpoint_use;
412 if not(self.spawnflags & 1)
413 self.touch = checkpoint_touch;
415 o = (self.absmin + self.absmax) * 0.5;
416 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
417 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
418 self.nearestwaypointtimeout = time + 1000000000;
421 self.message = "went backwards";
423 self.race_checkpoint = self.cnt;
425 if(self.race_checkpoint > race_highest_checkpoint)
426 race_highest_checkpoint = self.race_checkpoint;
428 if(self.race_checkpoint)
430 precache_model("models/sprites/race-checkpoint.sp2");
431 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
435 precache_model("models/sprites/race-finish.sp2");
436 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
438 self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
441 void race_PreparePlayer()
443 race_ClearTime(self);
447 void race_RetractPlayer()
451 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
452 if(self.race_checkpoint == 0)
454 race_ClearTime(self);
455 self.race_checkpoint = 0;
463 if(self.killcount == -666 || g_race_qualifying)
464 race_PreparePlayer();
468 self.race_completed = 1;
469 MAKE_INDEPENDENT_PLAYER(self);
470 bprint(self.netname, "^7 has abandoned the race.\n");
471 // TODO support spectators with this (e.g. set a flag for forced sbar)
472 if(clienttype(self) == CLIENTTYPE_REAL)
475 WriteByte(MSG_ONE, SVC_TEMPENTITY);
476 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
477 WriteByte(MSG_ONE, 1);
478 // he can still move, but will see the scoreboard now
483 void race_PostSpawn(entity spot)
487 if(self.killcount != -666 && !g_race_qualifying)
489 if(spot.target == "")
490 // let the player run without timing, if he did not spawn at a targetting spawnpoint
491 race_PreparePlayer();
493 race_RetractPlayer();
496 if(spot.target != "" && self.race_checkpoint == -1)
497 self.race_checkpoint = 0;
502 void race_PreSpawnObserver()
506 race_PreparePlayer();
509 void spawnfunc_info_player_race (void)
517 spawnfunc_info_player_deathmatch();
519 if(self.race_place > race_highest_place_spawn)
520 race_highest_place_spawn = self.race_place;
523 void race_ClearRecords()
528 for(i = 0; i < MAX_CHECKPOINTS; ++i)
530 race_checkpoint_records[i] = 0;
531 if(race_checkpoint_recordholders[i])
532 strunzone(race_checkpoint_recordholders[i]);
533 race_checkpoint_recordholders[i] = string_null;
540 void race_ReadyRestart()
544 if(g_race_qualifying == 2)
546 g_race_qualifying = 0;
547 independent_players = 0;
548 cvar_set("fraglimit", ftos(race_fraglimit));
552 PlayerScore_Sort(race_place);