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.5 + 10 * t); // make integer
86 if(cp == 0) // finish line
87 if not(e.race_completed)
92 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
94 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
98 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
99 snew = floor(0.5 + 10 * (time - restart_countdown));
100 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
101 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
103 if(cvar("fraglimit"))
104 if(l >= cvar("fraglimit"))
108 if(p.deadflag != DEAD_NO)
109 race_AbandonRaceCheck(p);
114 e.race_completed = 1;
115 MAKE_INDEPENDENT_PLAYER(e);
116 bprint(e.netname, "^7 has finished the race.\n");
117 // TODO support spectators with this (e.g. set a flag for forced sbar)
118 if(clienttype(e) == CLIENTTYPE_REAL)
121 WriteByte(MSG_ONE, SVC_TEMPENTITY);
122 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
123 WriteByte(MSG_ONE, 1);
124 // he can still move, but will see the scoreboard now
133 if(g_race_qualifying)
137 recordtime = race_checkpoint_records[cp];
138 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
139 if(recordholder == e.netname)
142 if(t < recordtime || recordtime == 0)
144 race_checkpoint_records[cp] = t;
145 if(race_checkpoint_recordholders[cp])
146 strunzone(race_checkpoint_recordholders[cp]);
147 race_checkpoint_recordholders[cp] = strzone(e.netname);
151 string grecordholder;
152 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
153 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
154 if(grecordholder == e.netname)
158 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
159 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
160 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
161 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
163 else if(t < grecordtime)
165 if(grecordholder == "")
166 bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
168 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
169 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
170 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
171 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
175 if(grecordholder == "")
176 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
178 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
182 if(g_race_qualifying)
184 FOR_EACH_REALPLAYER(p)
185 if(p.race_checkpoint == cp)
186 race_SendNextCheckpoint(p, 0);
199 if(g_race_qualifying)
201 WRITESPECTATABLE_MSG_ONE({
202 WriteByte(MSG_ONE, SVC_TEMPENTITY);
203 WriteByte(MSG_ONE, TE_CSQC_RACE);
204 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
205 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
206 WriteShort(MSG_ONE, t); // time to that intermediate
207 WriteShort(MSG_ONE, recordtime); // previously best time
208 WriteString(MSG_ONE, recordholder); // record holder
212 else // RACE! Not Qualifying
214 float lself, lother, othtime;
216 oth = race_checkpoint_lastplayers[cp];
219 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
220 lother = race_checkpoint_lastlaps[cp];
221 othtime = race_checkpoint_lasttimes[cp];
224 lself = lother = othtime = 0;
227 WRITESPECTATABLE_MSG_ONE({
228 WriteByte(MSG_ONE, SVC_TEMPENTITY);
229 WriteByte(MSG_ONE, TE_CSQC_RACE);
230 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
231 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
234 WriteShort(MSG_ONE, 0);
235 WriteByte(MSG_ONE, 0);
236 WriteString(MSG_ONE, "");
240 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
241 WriteByte(MSG_ONE, lself - lother);
242 WriteString(MSG_ONE, oth.netname); // record holder
246 race_checkpoint_lastplayers[cp] = e;
247 race_checkpoint_lasttimes[cp] = time;
248 race_checkpoint_lastlaps[cp] = lself;
251 WRITESPECTATABLE_MSG_ONE({
252 WriteByte(MSG_ONE, SVC_TEMPENTITY);
253 WriteByte(MSG_ONE, TE_CSQC_RACE);
254 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
255 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
258 WriteShort(MSG_ONE, 0);
259 WriteByte(MSG_ONE, 0);
260 WriteString(MSG_ONE, "");
264 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
265 WriteByte(MSG_ONE, lother - lself);
266 WriteString(MSG_ONE, e.netname); // record holder
272 void race_ClearTime(entity e)
274 e.race_checkpoint = -1;
278 WRITESPECTATABLE_MSG_ONE({
279 WriteByte(MSG_ONE, SVC_TEMPENTITY);
280 WriteByte(MSG_ONE, TE_CSQC_RACE);
281 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
285 void dumpsurface(entity e)
289 print("Surfaces of ", etos(e), ":\n");
291 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
295 n = getsurfacenumpoints(e, si);
298 print(" Surface ", ftos(si), ":\n");
299 norm = getsurfacenormal(e, si);
300 print(" Normal = ", vtos(norm), "\n");
301 for(ni = 0; ni < n; ++ni)
303 vec = getsurfacepoint(e, si, ni);
304 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
309 void checkpoint_passed()
314 if(other.classname == "porto")
316 // do not allow portalling through checkpoints
317 trace_plane_normal = normalize(-1 * other.velocity);
326 if not(self.spawnflags & 2)
329 oldmsg = self.message;
332 self.message = oldmsg;
335 if(other.classname != "player")
339 * Remove unauthorized equipment
341 Portal_ClearAll(other);
342 if(other.porto_current)
345 self = other.porto_current;
350 other.porto_forbidden = 2; // decreased by 1 each StartFrame
352 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
357 if(self.spawnflags & 2)
360 oldmsg = self.message;
363 self.message = oldmsg;
366 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
368 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
370 if(!self.race_checkpoint) // finish line
371 other.race_laptime = time;
373 if(g_race_qualifying)
374 race_SendNextCheckpoint(other, 0);
376 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
382 if(self.spawnflags & 4)
383 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
387 void checkpoint_touch()
393 void checkpoint_use()
399 float race_waypointsprite_for_player(entity e)
401 if(e.race_checkpoint == -1)
402 return self.modelindex;
403 else if(e.race_checkpoint == self.owner.race_checkpoint)
404 return self.modelindex;
409 void spawnfunc_trigger_race_checkpoint()
420 self.use = checkpoint_use;
421 if not(self.spawnflags & 1)
422 self.touch = checkpoint_touch;
424 o = (self.absmin + self.absmax) * 0.5;
425 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
426 self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
427 self.nearestwaypointtimeout = time + 1000000000;
430 self.message = "went backwards";
432 self.race_checkpoint = self.cnt;
434 if(self.race_checkpoint > race_highest_checkpoint)
435 race_highest_checkpoint = self.race_checkpoint;
437 if(self.race_checkpoint)
439 precache_model("models/sprites/race-checkpoint.sp2");
440 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
444 precache_model("models/sprites/race-finish.sp2");
445 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
447 self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
450 void race_AbandonRaceCheck(entity p)
452 if(race_completing && !p.race_completed)
454 p.race_completed = 1;
455 MAKE_INDEPENDENT_PLAYER(p);
456 bprint(p.netname, "^7 has abandoned the race.\n");
457 // TODO support spectators with this (e.g. set a flag for forced sbar)
458 if(clienttype(p) == CLIENTTYPE_REAL)
461 WriteByte(MSG_ONE, SVC_TEMPENTITY);
462 WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
463 WriteByte(MSG_ONE, 1);
464 // he can still move, but will see the scoreboard now
469 void race_PreparePlayer()
471 race_ClearTime(self);
475 void race_RetractPlayer()
479 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
480 if(self.race_checkpoint == 0)
482 race_ClearTime(self);
483 self.race_checkpoint = 0;
492 race_AbandonRaceCheck(self);
499 if(self.killcount == -666 || g_race_qualifying)
500 race_PreparePlayer();
502 race_AbandonRaceCheck(self);
505 void race_PostSpawn(entity spot)
509 if(self.killcount != -666 && !g_race_qualifying)
511 if(spot.target == "")
512 // let the player run without timing, if he did not spawn at a targetting spawnpoint
513 race_PreparePlayer();
515 race_RetractPlayer();
518 if(spot.target != "" && self.race_checkpoint == -1)
519 self.race_checkpoint = 0;
524 void race_PreSpawnObserver()
528 race_PreparePlayer();
531 void spawnfunc_info_player_race (void)
539 spawnfunc_info_player_deathmatch();
541 if(self.race_place > race_highest_place_spawn)
542 race_highest_place_spawn = self.race_place;
543 if(self.race_place < race_lowest_place_spawn)
544 race_lowest_place_spawn = self.race_place;
547 void race_ClearRecords()
552 for(i = 0; i < MAX_CHECKPOINTS; ++i)
554 race_checkpoint_records[i] = 0;
555 if(race_checkpoint_recordholders[i])
556 strunzone(race_checkpoint_recordholders[i]);
557 race_checkpoint_recordholders[i] = string_null;
564 void race_ReadyRestart()
568 if(g_race_qualifying == 2)
570 g_race_qualifying = 0;
571 independent_players = 0;
572 cvar_set("fraglimit", ftos(race_fraglimit));
576 PlayerScore_Sort(race_place);