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)
81 t = floor(0.5 + 10 * t); // make integer
84 if(cp == 0) // finish line
85 if not(e.race_completed)
90 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
92 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
96 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
97 snew = floor(0.5 + 10 * (time - restart_countdown));
98 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
99 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
101 if(cvar("fraglimit"))
102 if(l >= cvar("fraglimit"))
107 e.race_completed = 1;
108 MAKE_INDEPENDENT_PLAYER(e);
109 bprint(e.netname, "^7 has finished the race.\n");
110 // TODO support spectators with this (e.g. set a flag for forced sbar)
111 if(clienttype(e) == CLIENTTYPE_REAL)
114 WriteByte(MSG_ONE, SVC_TEMPENTITY);
115 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
116 WriteByte(MSG_ONE, 1);
117 // he can still move, but will see the scoreboard now
126 if(g_race_qualifying)
130 recordtime = race_checkpoint_records[cp];
131 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
132 if(recordholder == e.netname)
135 if(t < recordtime || recordtime == 0)
137 race_checkpoint_records[cp] = t;
138 if(race_checkpoint_recordholders[cp])
139 strunzone(race_checkpoint_recordholders[cp]);
140 race_checkpoint_recordholders[cp] = strzone(e.netname);
144 string grecordholder;
145 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
146 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
147 if(grecordholder == e.netname)
151 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
152 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
153 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
154 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
156 else if(t < grecordtime)
158 if(grecordholder == "")
159 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
161 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
162 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
163 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
164 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
168 if(grecordholder == "")
169 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
171 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
175 if(g_race_qualifying)
178 FOR_EACH_REALPLAYER(p)
179 if(p.race_checkpoint == cp)
180 race_SendNextCheckpoint(p, 0);
193 if(g_race_qualifying)
195 WRITESPECTATABLE_MSG_ONE({
196 WriteByte(MSG_ONE, SVC_TEMPENTITY);
197 WriteByte(MSG_ONE, TE_CSQC_RACE);
198 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
199 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
200 WriteShort(MSG_ONE, t); // time to that intermediate
201 WriteShort(MSG_ONE, recordtime); // previously best time
202 WriteString(MSG_ONE, recordholder); // record holder
206 else // RACE! Not Qualifying
208 float lself, lother, othtime;
210 oth = race_checkpoint_lastplayers[cp];
213 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
214 lother = race_checkpoint_lastlaps[cp];
215 othtime = race_checkpoint_lasttimes[cp];
218 lself = lother = othtime = 0;
221 WRITESPECTATABLE_MSG_ONE({
222 WriteByte(MSG_ONE, SVC_TEMPENTITY);
223 WriteByte(MSG_ONE, TE_CSQC_RACE);
224 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
225 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
228 WriteShort(MSG_ONE, 0);
229 WriteByte(MSG_ONE, 0);
230 WriteString(MSG_ONE, "");
234 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
235 WriteByte(MSG_ONE, lself - lother);
236 WriteString(MSG_ONE, oth.netname); // record holder
240 race_checkpoint_lastplayers[cp] = e;
241 race_checkpoint_lasttimes[cp] = time;
242 race_checkpoint_lastlaps[cp] = lself;
245 WRITESPECTATABLE_MSG_ONE({
246 WriteByte(MSG_ONE, SVC_TEMPENTITY);
247 WriteByte(MSG_ONE, TE_CSQC_RACE);
248 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
249 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
252 WriteShort(MSG_ONE, 0);
253 WriteByte(MSG_ONE, 0);
254 WriteString(MSG_ONE, "");
258 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
259 WriteByte(MSG_ONE, lother - lself);
260 WriteString(MSG_ONE, e.netname); // record holder
266 void race_ClearTime(entity e)
268 e.race_checkpoint = -1;
272 WRITESPECTATABLE_MSG_ONE({
273 WriteByte(MSG_ONE, SVC_TEMPENTITY);
274 WriteByte(MSG_ONE, TE_CSQC_RACE);
275 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
279 void dumpsurface(entity e)
283 print("Surfaces of ", etos(e), ":\n");
285 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
289 n = getsurfacenumpoints(e, si);
292 print(" Surface ", ftos(si), ":\n");
293 norm = getsurfacenormal(e, si);
294 print(" Normal = ", vtos(norm), "\n");
295 for(ni = 0; ni < n; ++ni)
297 vec = getsurfacepoint(e, si, ni);
298 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
303 void checkpoint_passed()
308 if(other.classname == "porto")
310 // do not allow portalling through checkpoints
311 trace_plane_normal = normalize(-1 * other.velocity);
320 if not(self.spawnflags & 2)
323 oldmsg = self.message;
326 self.message = oldmsg;
329 if(other.classname != "player")
333 * Remove unauthorized equipment
335 Portal_ClearAll(other);
336 if(other.porto_current)
339 self = other.porto_current;
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()
393 float race_waypointsprite_for_player(entity e)
395 if(e.race_checkpoint == -1)
396 return self.modelindex;
397 else if(e.race_checkpoint == self.owner.race_checkpoint)
398 return self.modelindex;
403 void spawnfunc_trigger_race_checkpoint()
414 self.use = checkpoint_use;
415 if not(self.spawnflags & 1)
416 self.touch = checkpoint_touch;
418 o = (self.absmin + self.absmax) * 0.5;
419 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
420 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
421 self.nearestwaypointtimeout = time + 1000000000;
424 self.message = "went backwards";
426 self.race_checkpoint = self.cnt;
428 if(self.race_checkpoint > race_highest_checkpoint)
429 race_highest_checkpoint = self.race_checkpoint;
431 if(self.race_checkpoint)
433 precache_model("models/sprites/race-checkpoint.sp2");
434 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
438 precache_model("models/sprites/race-finish.sp2");
439 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
441 self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
444 void race_PreparePlayer()
446 race_ClearTime(self);
450 void race_RetractPlayer()
454 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
455 if(self.race_checkpoint == 0)
457 race_ClearTime(self);
458 self.race_checkpoint = 0;
466 if(self.killcount == -666 || g_race_qualifying)
467 race_PreparePlayer();
471 self.race_completed = 1;
472 MAKE_INDEPENDENT_PLAYER(self);
473 bprint(self.netname, "^7 has abandoned the race.\n");
474 // TODO support spectators with this (e.g. set a flag for forced sbar)
475 if(clienttype(self) == CLIENTTYPE_REAL)
478 WriteByte(MSG_ONE, SVC_TEMPENTITY);
479 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
480 WriteByte(MSG_ONE, 1);
481 // he can still move, but will see the scoreboard now
486 void race_PostSpawn(entity spot)
490 if(self.killcount != -666 && !g_race_qualifying)
492 if(spot.target == "")
493 // let the player run without timing, if he did not spawn at a targetting spawnpoint
494 race_PreparePlayer();
496 race_RetractPlayer();
499 if(spot.target != "" && self.race_checkpoint == -1)
500 self.race_checkpoint = 0;
505 void race_PreSpawnObserver()
509 race_PreparePlayer();
512 void spawnfunc_info_player_race (void)
520 spawnfunc_info_player_deathmatch();
522 if(self.race_place > race_highest_place_spawn)
523 race_highest_place_spawn = self.race_place;
524 if(self.race_place < race_lowest_place_spawn)
525 race_lowest_place_spawn = self.race_place;
528 void race_ClearRecords()
533 for(i = 0; i < MAX_CHECKPOINTS; ++i)
535 race_checkpoint_records[i] = 0;
536 if(race_checkpoint_recordholders[i])
537 strunzone(race_checkpoint_recordholders[i]);
538 race_checkpoint_recordholders[i] = string_null;
545 void race_ReadyRestart()
549 if(g_race_qualifying == 2)
551 g_race_qualifying = 0;
552 independent_players = 0;
553 cvar_set("fraglimit", ftos(race_fraglimit));
557 PlayerScore_Sort(race_place);