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)
83 t = floor(0.4 + 10 * t); // make integer
84 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
87 if(cp == 0) // finish line
88 if not(e.race_completed)
93 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
95 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
99 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
100 snew = floor(0.5 + 10 * (time - game_starttime));
101 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
102 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
104 if(cvar("fraglimit"))
105 if(l >= cvar("fraglimit"))
109 if(p.deadflag != DEAD_NO)
110 race_AbandonRaceCheck(p);
115 e.race_completed = 1;
116 MAKE_INDEPENDENT_PLAYER(e);
117 bprint(e.netname, "^7 has finished the race.\n");
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)
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)
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_VARNAME(dummy1, {
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_VARNAME(dummy2, {
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_VARNAME(dummy3, {
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()
307 if(other.classname == "porto")
309 // do not allow portalling through checkpoints
310 trace_plane_normal = normalize(-1 * other.velocity);
319 if not(self.spawnflags & 2)
322 oldmsg = self.message;
325 self.message = oldmsg;
328 if(other.classname != "player")
332 * Remove unauthorized equipment
334 Portal_ClearAll(other);
336 other.porto_forbidden = 2; // decreased by 1 each StartFrame
338 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
343 if(self.spawnflags & 2)
346 oldmsg = self.message;
349 self.message = oldmsg;
352 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
354 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
356 if(!self.race_checkpoint) // finish line
357 other.race_laptime = time;
359 if(g_race_qualifying)
360 race_SendNextCheckpoint(other, 0);
362 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
368 if(self.spawnflags & 4)
369 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
373 void checkpoint_touch()
379 void checkpoint_use()
381 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
388 float race_waypointsprite_visible_for_player(entity e)
390 if(e.race_checkpoint == -1)
392 else if(e.race_checkpoint == self.owner.race_checkpoint)
398 void spawnfunc_trigger_race_checkpoint()
409 self.use = checkpoint_use;
410 if not(self.spawnflags & 1)
411 self.touch = checkpoint_touch;
413 o = (self.absmin + self.absmax) * 0.5;
414 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
415 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
416 self.nearestwaypointtimeout = time + 1000000000;
419 self.message = "went backwards";
421 self.message2 = "was pushed backwards by";
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 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
434 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
436 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
439 void race_AbandonRaceCheck(entity p)
441 if(race_completing && !p.race_completed)
443 p.race_completed = 1;
444 MAKE_INDEPENDENT_PLAYER(p);
445 bprint(p.netname, "^7 has abandoned the race.\n");
450 void race_PreparePlayer()
452 race_ClearTime(self);
456 void race_RetractPlayer()
460 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
461 if(self.race_checkpoint == 0)
463 race_ClearTime(self);
464 self.race_checkpoint = 0;
473 race_AbandonRaceCheck(self);
480 if(self.killcount == -666 || g_race_qualifying)
481 race_PreparePlayer();
483 race_AbandonRaceCheck(self);
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 PlayerScore_Sort(race_place);
551 if(g_race_qualifying == 2)
553 g_race_qualifying = 0;
554 independent_players = 0;
555 cvar_set("fraglimit", ftos(race_fraglimit));