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";
458 for(i = 0; i <= race_highest_checkpoint; ++i)
460 self.race_checkpoint = race_NextCheckpoint(i);
462 // race only (middle of the race)
463 g_race_qualifying = 0;
465 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
466 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
471 g_race_qualifying = 1;
472 self.race_place = race_lowest_place_spawn;
473 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
474 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
476 // race only (initial spawn)
477 g_race_qualifying = 0;
478 for(p = 1; p <= race_highest_place_spawn; ++p)
481 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
482 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
490 self.race_checkpoint = race_NextCheckpoint(0);
491 g_race_qualifying = 1;
492 self.race_place = race_lowest_place_spawn;
493 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
494 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
497 g_race_qualifying = qual;
499 if(race_timed_checkpoint)
500 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
503 if(cp.race_checkpoint == 0)
504 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
505 else if(cp.race_checkpoint == race_timed_checkpoint)
506 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
513 void spawnfunc_trigger_race_checkpoint()
516 if(!g_race && !g_cts)
524 self.use = checkpoint_use;
525 if not(self.spawnflags & 1)
526 self.touch = checkpoint_touch;
528 o = (self.absmin + self.absmax) * 0.5;
529 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
530 waypoint_spawnforitem_force(self, trace_endpos);
531 self.nearestwaypointtimeout = time + 1000000000;
534 self.message = "went backwards";
536 self.message2 = "was pushed backwards by";
537 if (!self.race_penalty_reason)
538 self.race_penalty_reason = "missing a checkpoint";
540 self.race_checkpoint = self.cnt;
542 if(self.race_checkpoint > race_highest_checkpoint)
544 race_highest_checkpoint = self.race_checkpoint;
545 if(self.spawnflags & 8)
546 race_timed_checkpoint = self.race_checkpoint;
548 race_timed_checkpoint = 0;
551 if(!self.race_penalty)
553 if(self.race_checkpoint)
554 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
556 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
559 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
561 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
564 void race_AbandonRaceCheck(entity p)
566 if(race_completing && !p.race_completed)
568 p.race_completed = 1;
569 MAKE_INDEPENDENT_PLAYER(p);
570 bprint(p.netname, "^7 has abandoned the race.\n");
575 void race_StartCompleting()
580 if(p.deadflag != DEAD_NO)
581 race_AbandonRaceCheck(p);
584 void race_PreparePlayer()
586 race_ClearTime(self);
590 void race_RetractPlayer()
592 if(!g_race && !g_cts)
594 self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
595 if(self.race_checkpoint == 0)
597 race_ClearTime(self);
598 self.race_checkpoint = 0;
604 if(!g_race && !g_cts)
607 race_AbandonRaceCheck(self);
612 if(!g_race && !g_cts)
614 if(self.killcount == -666 || g_race_qualifying)
615 race_PreparePlayer();
617 race_AbandonRaceCheck(self);
620 void race_PostSpawn(entity spot)
622 if(!g_race && !g_cts)
624 if(self.killcount != -666 && !g_race_qualifying)
626 if(spot.target == "")
627 // let the player run without timing, if he did not spawn at a targetting spawnpoint
628 race_PreparePlayer();
630 race_RetractPlayer();
633 if(spot.target != "" && self.race_checkpoint == -1)
634 self.race_checkpoint = 0;
639 void race_PreSpawnObserver()
641 if(!g_race && !g_cts)
643 race_PreparePlayer();
646 void spawnfunc_info_player_race (void)
648 if(!g_race && !g_cts)
654 spawnfunc_info_player_deathmatch();
656 if(self.race_place > race_highest_place_spawn)
657 race_highest_place_spawn = self.race_place;
658 if(self.race_place < race_lowest_place_spawn)
659 race_lowest_place_spawn = self.race_place;
662 void race_ClearRecords()
667 for(i = 0; i < MAX_CHECKPOINTS; ++i)
669 race_checkpoint_records[i] = 0;
670 if(race_checkpoint_recordholders[i])
671 strunzone(race_checkpoint_recordholders[i]);
672 race_checkpoint_recordholders[i] = string_null;
679 void race_ReadyRestart()
683 Score_NicePrint(world);
686 PlayerScore_Sort(race_place);
693 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
697 print(e.netname, " = ", ftos(e.race_place), "\n");
700 if(g_race_qualifying == 2)
702 g_race_qualifying = 0;
703 independent_players = 0;
704 cvar_set("fraglimit", ftos(race_fraglimit));
705 cvar_set("leadlimit", ftos(race_leadlimit));
706 cvar_set("timelimit", ftos(race_timelimit));
711 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
713 if(g_race_qualifying)
715 pl.race_penalty_accumulator += penalty;
717 WRITESPECTATABLE_MSG_ONE({
718 WriteByte(MSG_ONE, SVC_TEMPENTITY);
719 WriteByte(MSG_ONE, TE_CSQC_RACE);
720 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
721 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
722 WriteString(MSG_ONE, reason);
727 pl.race_penalty = time + penalty;
729 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
730 WriteByte(MSG_ONE, SVC_TEMPENTITY);
731 WriteByte(MSG_ONE, TE_CSQC_RACE);
732 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
733 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
734 WriteString(MSG_ONE, reason);
742 if(other.race_lastpenalty != self)
744 other.race_lastpenalty = self;
745 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
751 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
754 void spawnfunc_trigger_race_penalty()
758 self.use = penalty_use;
759 if not(self.spawnflags & 1)
760 self.touch = penalty_touch;
762 if (!self.race_penalty_reason)
763 self.race_penalty_reason = "missing a checkpoint";
764 if (!self.race_penalty)
765 self.race_penalty = 5;