1 #define MAX_CHECKPOINTS 255
4 .float race_penalty_accumulator;
5 .string race_penalty_reason;
6 .float race_checkpoint; // player: next checkpoint that has to be reached
8 .entity race_lastpenalty;
12 float race_checkpoint_records[MAX_CHECKPOINTS];
13 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
14 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
15 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
16 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
18 float race_highest_checkpoint;
19 float race_timed_checkpoint;
21 float race_NextCheckpoint(float f)
23 if(f >= race_highest_checkpoint)
29 float race_PreviousCheckpoint(float f)
34 return race_highest_checkpoint;
40 // 0 = common start/finish
43 float race_CheckpointNetworkID(float f)
45 if(race_timed_checkpoint)
49 else if(f == race_timed_checkpoint)
55 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
64 cp = e.race_checkpoint;
65 recordtime = race_checkpoint_records[cp];
66 recordholder = race_checkpoint_recordholders[cp];
67 if(recordholder == e.netname)
72 WRITESPECTATABLE_MSG_ONE({
73 WriteByte(MSG_ONE, SVC_TEMPENTITY);
74 WriteByte(MSG_ONE, TE_CSQC_RACE);
77 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
78 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
79 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
82 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
83 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
84 WriteInt24_t(MSG_ONE, recordtime);
85 WriteString(MSG_ONE, recordholder);
89 void race_InitSpectator()
92 if(msg_entity.enemy.race_laptime)
93 race_SendNextCheckpoint(msg_entity.enemy, 1);
96 void race_SendTime(entity e, float cp, float t, float tvalid)
101 if(g_race_qualifying)
102 t += e.race_penalty_accumulator;
104 t = TIME_ENCODE(t); // make integer
105 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
108 if(cp == race_timed_checkpoint) // finish line
109 if not(e.race_completed)
112 if(g_race_qualifying)
114 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
116 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
120 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
121 snew = TIME_ENCODE(time - game_starttime);
122 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
123 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
125 if(cvar("fraglimit"))
126 if(l >= cvar("fraglimit"))
127 race_StartCompleting();
131 e.race_completed = 1;
132 MAKE_INDEPENDENT_PLAYER(e);
133 bprint(e.netname, "^7 has finished the race.\n");
142 if(g_race_qualifying)
146 recordtime = race_checkpoint_records[cp];
147 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
148 if(recordholder == e.netname)
152 if(t < recordtime || recordtime == 0)
154 race_checkpoint_records[cp] = t;
155 if(race_checkpoint_recordholders[cp])
156 strunzone(race_checkpoint_recordholders[cp]);
157 race_checkpoint_recordholders[cp] = strzone(e.netname);
158 if(cp == race_timed_checkpoint)
161 string grecordholder;
167 grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
168 grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname"));
169 if(grecordholder == e.netname)
173 bprint(e.netname, "^7 set the all-time fastest lap record with ", TIME_ENCODED_TOSTRING(t), "\n");
174 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
175 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
176 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
178 else if(t < grecordtime)
180 if(grecordholder == "")
181 bprint(e.netname, "^7 broke his all-time fastest lap record with ", TIME_ENCODED_TOSTRING(t), "\n");
183 bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", TIME_ENCODED_TOSTRING(t), "\n");
184 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
185 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
186 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
190 if(grecordholder == "")
191 bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", TIME_ENCODED_TOSTRING(grecordtime), "\n");
193 bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", TIME_ENCODED_TOSTRING(grecordtime), "\n");
197 if(g_race_qualifying)
199 FOR_EACH_REALPLAYER(p)
200 if(p.race_checkpoint == cp)
201 race_SendNextCheckpoint(p, 0);
214 if(g_race_qualifying)
216 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
217 WriteByte(MSG_ONE, SVC_TEMPENTITY);
218 WriteByte(MSG_ONE, TE_CSQC_RACE);
219 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
220 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
221 WriteInt24_t(MSG_ONE, t); // time to that intermediate
222 WriteInt24_t(MSG_ONE, recordtime); // previously best time
223 WriteString(MSG_ONE, recordholder); // record holder
227 else // RACE! Not Qualifying
229 float lself, lother, othtime;
231 oth = race_checkpoint_lastplayers[cp];
234 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
235 lother = race_checkpoint_lastlaps[cp];
236 othtime = race_checkpoint_lasttimes[cp];
239 lself = lother = othtime = 0;
242 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
243 WriteByte(MSG_ONE, SVC_TEMPENTITY);
244 WriteByte(MSG_ONE, TE_CSQC_RACE);
245 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
246 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
249 WriteInt24_t(MSG_ONE, 0);
250 WriteByte(MSG_ONE, 0);
251 WriteString(MSG_ONE, "");
255 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
256 WriteByte(MSG_ONE, lself - lother);
257 WriteString(MSG_ONE, oth.netname); // record holder
261 race_checkpoint_lastplayers[cp] = e;
262 race_checkpoint_lasttimes[cp] = time;
263 race_checkpoint_lastlaps[cp] = lself;
266 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
267 WriteByte(MSG_ONE, SVC_TEMPENTITY);
268 WriteByte(MSG_ONE, TE_CSQC_RACE);
269 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
270 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
273 WriteInt24_t(MSG_ONE, 0);
274 WriteByte(MSG_ONE, 0);
275 WriteString(MSG_ONE, "");
279 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
280 WriteByte(MSG_ONE, lother - lself);
281 WriteString(MSG_ONE, e.netname); // record holder
287 void race_ClearTime(entity e)
289 e.race_checkpoint = -1;
291 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
292 e.race_penalty_accumulator = 0;
293 e.race_lastpenalty = world;
296 WRITESPECTATABLE_MSG_ONE({
297 WriteByte(MSG_ONE, SVC_TEMPENTITY);
298 WriteByte(MSG_ONE, TE_CSQC_RACE);
299 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
303 void dumpsurface(entity e)
307 print("Surfaces of ", etos(e), ":\n");
309 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
313 n = getsurfacenumpoints(e, si);
316 print(" Surface ", ftos(si), ":\n");
317 norm = getsurfacenormal(e, si);
318 print(" Normal = ", vtos(norm), "\n");
319 for(ni = 0; ni < n; ++ni)
321 vec = getsurfacepoint(e, si, ni);
322 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
327 void checkpoint_passed()
331 if(other.classname == "porto")
333 // do not allow portalling through checkpoints
334 trace_plane_normal = normalize(-1 * other.velocity);
343 if not(self.spawnflags & 2)
346 oldmsg = self.message;
349 self.message = oldmsg;
352 if(other.classname != "player")
356 * Remove unauthorized equipment
358 Portal_ClearAll(other);
360 other.porto_forbidden = 2; // decreased by 1 each StartFrame
362 if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
364 if(self.race_penalty)
366 if(other.race_lastpenalty != self)
368 other.race_lastpenalty = self;
369 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
373 if(other.race_penalty)
379 if(self.spawnflags & 2)
382 oldmsg = self.message;
385 self.message = oldmsg;
388 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
390 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
392 if(!self.race_checkpoint) // start line
394 other.race_laptime = time;
395 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
396 other.race_penalty_accumulator = 0;
397 other.race_lastpenalty = world;
400 if(g_race_qualifying)
401 race_SendNextCheckpoint(other, 0);
403 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
409 if(self.spawnflags & 4)
410 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
414 void checkpoint_touch()
420 void checkpoint_use()
422 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
429 float race_waypointsprite_visible_for_player(entity e)
431 if(e.race_checkpoint == -1)
433 else if(e.race_checkpoint == self.owner.race_checkpoint)
440 void trigger_race_checkpoint_verify()
450 qual = g_race_qualifying;
454 self.classname = "player";
456 for(i = 0; i <= race_highest_checkpoint; ++i)
458 self.race_checkpoint = race_NextCheckpoint(i);
460 // race only (middle of the race)
461 g_race_qualifying = 0;
463 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
464 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
469 g_race_qualifying = 1;
470 self.race_place = race_lowest_place_spawn;
471 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
472 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
474 // race only (initial spawn)
475 g_race_qualifying = 0;
476 for(p = 1; p <= race_highest_place_spawn; ++p)
479 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
480 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
485 g_race_qualifying = qual;
487 if(race_timed_checkpoint)
488 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
491 if(cp.race_checkpoint == 0)
492 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
493 else if(cp.race_checkpoint == race_timed_checkpoint)
494 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
501 void spawnfunc_trigger_race_checkpoint()
504 if(!g_race && !g_cts)
512 self.use = checkpoint_use;
513 if not(self.spawnflags & 1)
514 self.touch = checkpoint_touch;
516 o = (self.absmin + self.absmax) * 0.5;
517 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
518 waypoint_spawnforitem_force(self, trace_endpos);
521 self.message = "went backwards";
523 self.message2 = "was pushed backwards by";
524 if (!self.race_penalty_reason)
525 self.race_penalty_reason = "missing a checkpoint";
527 self.race_checkpoint = self.cnt;
529 if(self.race_checkpoint > race_highest_checkpoint)
531 race_highest_checkpoint = self.race_checkpoint;
532 if(self.spawnflags & 8)
533 race_timed_checkpoint = self.race_checkpoint;
535 race_timed_checkpoint = 0;
538 if(!self.race_penalty)
540 if(self.race_checkpoint)
541 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
543 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
546 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
548 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
551 void race_AbandonRaceCheck(entity p)
553 if(race_completing && !p.race_completed)
555 p.race_completed = 1;
556 MAKE_INDEPENDENT_PLAYER(p);
557 bprint(p.netname, "^7 has abandoned the race.\n");
562 void race_StartCompleting()
567 if(p.deadflag != DEAD_NO)
568 race_AbandonRaceCheck(p);
571 void race_PreparePlayer()
573 race_ClearTime(self);
577 void race_RetractPlayer()
579 if(!g_race && !g_cts)
581 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
582 if(self.race_checkpoint == 0)
584 race_ClearTime(self);
585 self.race_checkpoint = 0;
591 if(!g_race && !g_cts)
594 race_AbandonRaceCheck(self);
599 if(!g_race && !g_cts)
601 if(self.killcount == -666 || g_race_qualifying)
602 race_PreparePlayer();
604 race_AbandonRaceCheck(self);
607 void race_PostSpawn(entity spot)
609 if(!g_race && !g_cts)
611 if(self.killcount != -666 && !g_race_qualifying)
613 if(spot.target == "")
614 // let the player run without timing, if he did not spawn at a targetting spawnpoint
615 race_PreparePlayer();
617 race_RetractPlayer();
620 if(spot.target != "" && self.race_checkpoint == -1)
621 self.race_checkpoint = 0;
626 void race_PreSpawnObserver()
628 if(!g_race && !g_cts)
630 race_PreparePlayer();
633 void spawnfunc_info_player_race (void)
635 if(!g_race && !g_cts)
641 spawnfunc_info_player_deathmatch();
643 if(self.race_place > race_highest_place_spawn)
644 race_highest_place_spawn = self.race_place;
645 if(self.race_place < race_lowest_place_spawn)
646 race_lowest_place_spawn = self.race_place;
649 void race_ClearRecords()
654 for(i = 0; i < MAX_CHECKPOINTS; ++i)
656 race_checkpoint_records[i] = 0;
657 if(race_checkpoint_recordholders[i])
658 strunzone(race_checkpoint_recordholders[i]);
659 race_checkpoint_recordholders[i] = string_null;
666 void race_ReadyRestart()
670 Score_NicePrint(world);
673 PlayerScore_Sort(race_place);
680 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
684 print(e.netname, " = ", ftos(e.race_place), "\n");
687 if(g_race_qualifying == 2)
689 g_race_qualifying = 0;
690 independent_players = 0;
691 cvar_set("fraglimit", ftos(race_fraglimit));
692 cvar_set("leadlimit", ftos(race_leadlimit));
693 cvar_set("timelimit", ftos(race_timelimit));
698 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
700 if(g_race_qualifying)
702 pl.race_penalty_accumulator += penalty;
704 WRITESPECTATABLE_MSG_ONE({
705 WriteByte(MSG_ONE, SVC_TEMPENTITY);
706 WriteByte(MSG_ONE, TE_CSQC_RACE);
707 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
708 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
709 WriteString(MSG_ONE, reason);
714 pl.race_penalty = time + penalty;
716 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
717 WriteByte(MSG_ONE, SVC_TEMPENTITY);
718 WriteByte(MSG_ONE, TE_CSQC_RACE);
719 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
720 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
721 WriteString(MSG_ONE, reason);
729 if(other.race_lastpenalty != self)
731 other.race_lastpenalty = self;
732 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
738 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
741 void spawnfunc_trigger_race_penalty()
745 self.use = penalty_use;
746 if not(self.spawnflags & 1)
747 self.touch = penalty_touch;
749 if (!self.race_penalty_reason)
750 self.race_penalty_reason = "missing a checkpoint";
751 if (!self.race_penalty)
752 self.race_penalty = 5;