1 #define MAX_CHECKPOINTS 255
3 void spawnfunc_target_checkpoint();
6 .float race_penalty_accumulator;
7 .string race_penalty_reason;
8 .float race_checkpoint; // player: next checkpoint that has to be reached
10 .entity race_lastpenalty;
14 float race_checkpoint_records[MAX_CHECKPOINTS];
15 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
16 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
17 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
18 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
20 float race_highest_checkpoint;
21 float race_timed_checkpoint;
26 float race_NextCheckpoint(float f)
28 if(f >= race_highest_checkpoint)
34 float race_PreviousCheckpoint(float f)
39 return race_highest_checkpoint;
45 // 0 = common start/finish
48 float race_CheckpointNetworkID(float f)
50 if(race_timed_checkpoint)
54 else if(f == race_timed_checkpoint)
60 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
69 cp = e.race_checkpoint;
70 recordtime = race_checkpoint_records[cp];
71 recordholder = race_checkpoint_recordholders[cp];
72 if(recordholder == e.netname)
77 WRITESPECTATABLE_MSG_ONE({
78 WriteByte(MSG_ONE, SVC_TEMPENTITY);
79 WriteByte(MSG_ONE, TE_CSQC_RACE);
82 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
83 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
84 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
87 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
88 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
89 WriteInt24_t(MSG_ONE, recordtime);
90 WriteString(MSG_ONE, recordholder);
94 void race_InitSpectator()
97 if(msg_entity.enemy.race_laptime)
98 race_SendNextCheckpoint(msg_entity.enemy, 1);
102 float grecordtime[RANKINGS_CNT];
103 string grecordholder[RANKINGS_CNT];
104 string grecorduid[RANKINGS_CNT];
105 float worst_time; // last ranked time
106 float have_recs; // have we already read the records from the database before?
107 float race_GetTime(float pos) {
113 if (!have_recs) { // I guess this is better than checking if the first array item is empty, rumor has it that arrays are slow
115 for(i=0;i<RANKINGS_CNT;++i) {
116 grecordtime[i] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i))));
117 grecordholder[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i))));
118 grecorduid[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i))));
120 grecordtime[0] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
121 grecordholder[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname")));
122 grecorduid[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp")));
123 worst_time = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, strcat("time", ftos(RANKINGS_CNT-1)))));
127 return grecordtime[pos-1];
130 string race_GetName(float pos) { // these other functions assume that race_GetTime has been called >= once before
131 return grecordholder[pos-1];
134 float race_CheckUID(string myuid, string net_name) { // return existing UID or player name ranking pos, else 0
138 for (i=RANKINGS_CNT-1;i>=0;--i)
139 if(grecorduid[i] == myuid)
142 for (i=RANKINGS_CNT-1;i>=0;--i)
144 if(grecordholder[i] == net_name)
149 float race_GetPos(float t) {
153 for (i=0;i<RANKINGS_CNT;++i)
154 if (grecordtime[i] == 0 || grecordtime[i] > t)
157 for (i=0;i<RANKINGS_CNT;++i)
158 if (grecordtime[i] > t)
163 void race_send_recordtime(float msg)
165 // send the server best time
166 WriteByte(msg, SVC_TEMPENTITY);
167 WriteByte(msg, TE_CSQC_RACE);
168 WriteByte(msg, RACE_NET_SERVER_RECORD);
169 WriteInt24_t(msg, race_GetTime(1));
172 void race_SendRankings(float pos, float prevpos, float del, float msg)
174 WriteByte(msg, SVC_TEMPENTITY);
175 WriteByte(msg, TE_CSQC_RACE);
176 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
177 WriteShort(msg, pos);
178 WriteShort(msg, prevpos);
179 WriteShort(msg, del);
180 WriteString(msg, race_GetName(pos));
181 WriteInt24_t(msg, race_GetTime(pos));
184 void race_SendStatus(float id, entity e)
192 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
193 WriteByte(msg, SVC_TEMPENTITY);
194 WriteByte(msg, TE_CSQC_RACE);
195 WriteByte(msg, RACE_NET_SERVER_STATUS);
197 WriteString(msg, e.netname);
201 string race_PlaceName(float pos) {
209 return strcat(ftos(pos), "th");
212 void race_SetTime(entity e, float t, float match_rec) {
214 pos = race_GetPos(t);
215 prevpos = race_CheckUID(e.crypto_idfp, e.netname);
218 string recorddifference;
219 if (prevpos && (prevpos < pos || !pos))
221 oldrec = race_GetTime(prevpos);
222 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
223 bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
224 race_SendStatus(0, e); // "fail"
226 } else if (!pos) { // no ranking, time worse than the worst ranked
227 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
228 bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
229 race_SendStatus(0, e); // "fail"
233 oldrec = grecordtime[pos-1];
235 // move other rankings out of the way
237 if (prevpos) { // player improved his existing record
238 for (i=prevpos-1;i>pos-1;--i) {
239 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
240 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
241 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i-1]);
242 grecordtime[i] = grecordtime[i-1];
244 if (grecordholder[i])
245 strunzone(grecordholder[i]);
246 grecordholder[i] = strzone(grecordholder[i-1]);
248 strunzone(grecorduid[i]);
249 grecorduid[i] = strzone(grecorduid[i-1]);
251 } else { // player has no ranked record yet
252 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
253 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
254 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
255 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i-1]);
256 grecordtime[i] = grecordtime[i-1];
258 if (grecordholder[i])
259 strunzone(grecordholder[i]);
260 grecordholder[i] = strzone(grecordholder[i-1]);
262 strunzone(grecorduid[i]);
263 grecorduid[i] = strzone(grecorduid[i-1]);
269 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
270 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
271 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp"), e.crypto_idfp);
275 if (grecordholder[0])
276 strunzone(grecordholder[0]);
277 grecordholder[0] = strzone(e.netname);
279 strunzone(grecorduid[0]);
280 grecorduid[0] = strzone(e.crypto_idfp);
281 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
282 race_send_recordtime(MSG_ALL);
284 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
285 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
286 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(pos-1)), e.crypto_idfp);
288 grecordtime[pos-1] = t;
290 if (grecordholder[pos-1])
291 strunzone(grecordholder[pos-1]);
292 grecordholder[pos-1] = strzone(e.netname);
293 if (grecorduid[pos-1])
294 strunzone(grecorduid[pos-1]);
295 grecorduid[pos-1] = strzone(e.crypto_idfp);
298 if (pos == RANKINGS_CNT)
301 race_SendRankings(pos, prevpos, 0, MSG_ALL);
303 strunzone(rankings_reply);
304 rankings_reply = strzone(getrankings());
307 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
308 bprint(e.netname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
309 } else if (oldrec == 0) {
310 bprint(e.netname, "^1 set the 1st place record with ", TIME_ENCODED_TOSTRING(t), "\n");
312 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
313 bprint(e.netname, "^1 broke ", grecordholder[pos], "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
315 race_SendStatus(3, e); // "new server record"
318 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
319 bprint(e.netname, "^5 improved their ", race_PlaceName(pos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
320 race_SendStatus(1, e); // "new time"
321 } else if (oldrec == 0) {
322 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
323 race_SendStatus(2, e); // "new rank"
325 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
326 bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
327 race_SendStatus(2, e); // "new rank"
332 void race_DeleteTime(float pos) {
335 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
337 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
338 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
339 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp"), grecorduid[1]);
340 grecordtime[0] = grecordtime[1];
341 if (grecordholder[i])
342 strunzone(grecordholder[0]);
343 grecordholder[0] = strzone(grecordholder[1]);
346 strunzone(grecorduid[0]);
347 grecorduid[0] = strzone(grecorduid[1]);
349 else if (i == RANKINGS_CNT-1) {
350 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
351 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
352 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), string_null);
354 if (grecordholder[i])
355 strunzone(grecordholder[i]);
356 grecordholder[i] = string_null;
359 strunzone(grecorduid[i]);
360 grecorduid[i] = string_null;
363 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
364 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
365 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i+1]);
366 grecordtime[i] = grecordtime[i+1];
367 if (grecordholder[i])
368 strunzone(grecordholder[i]);
369 grecordholder[i] = strzone(grecordholder[i+1]);
372 strunzone(grecorduid[i]);
373 grecorduid[i] = strzone(grecorduid[i+1]);
377 race_SendRankings(pos, 0, 1, MSG_ALL);
379 race_send_recordtime(MSG_ALL);
382 strunzone(rankings_reply);
383 rankings_reply = strzone(getrankings());
388 void race_SendTime(entity e, float cp, float t, float tvalid)
393 if(g_race_qualifying)
394 t += e.race_penalty_accumulator;
396 t = TIME_ENCODE(t); // make integer
397 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
400 if(cp == race_timed_checkpoint) // finish line
401 if not(e.race_completed)
404 if(g_race_qualifying)
406 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
408 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
412 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
413 snew = TIME_ENCODE(time - game_starttime);
414 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
415 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
417 if(cvar("fraglimit"))
418 if(l >= cvar("fraglimit"))
419 race_StartCompleting();
423 e.race_completed = 1;
424 MAKE_INDEPENDENT_PLAYER(e);
425 bprint(e.netname, "^7 has finished the race.\n");
433 if(g_race_qualifying)
437 recordtime = race_checkpoint_records[cp];
438 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
439 if(recordholder == e.netname)
443 if(cp == race_timed_checkpoint)
444 race_SetTime(e, t, recordtime);
446 if(t < recordtime || recordtime == 0)
448 race_checkpoint_records[cp] = t;
449 if(race_checkpoint_recordholders[cp])
450 strunzone(race_checkpoint_recordholders[cp]);
451 race_checkpoint_recordholders[cp] = strzone(e.netname);
452 if(g_race_qualifying)
454 FOR_EACH_REALPLAYER(p)
455 if(p.race_checkpoint == cp)
456 race_SendNextCheckpoint(p, 0);
470 if(g_race_qualifying)
472 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
473 WriteByte(MSG_ONE, SVC_TEMPENTITY);
474 WriteByte(MSG_ONE, TE_CSQC_RACE);
475 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
476 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
477 WriteInt24_t(MSG_ONE, t); // time to that intermediate
478 WriteInt24_t(MSG_ONE, recordtime); // previously best time
479 WriteString(MSG_ONE, recordholder); // record holder
483 else // RACE! Not Qualifying
485 float lself, lother, othtime;
487 oth = race_checkpoint_lastplayers[cp];
490 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
491 lother = race_checkpoint_lastlaps[cp];
492 othtime = race_checkpoint_lasttimes[cp];
495 lself = lother = othtime = 0;
498 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
499 WriteByte(MSG_ONE, SVC_TEMPENTITY);
500 WriteByte(MSG_ONE, TE_CSQC_RACE);
501 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
502 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
505 WriteInt24_t(MSG_ONE, 0);
506 WriteByte(MSG_ONE, 0);
507 WriteString(MSG_ONE, "");
511 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
512 WriteByte(MSG_ONE, lself - lother);
513 WriteString(MSG_ONE, oth.netname); // record holder
517 race_checkpoint_lastplayers[cp] = e;
518 race_checkpoint_lasttimes[cp] = time;
519 race_checkpoint_lastlaps[cp] = lself;
522 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
523 WriteByte(MSG_ONE, SVC_TEMPENTITY);
524 WriteByte(MSG_ONE, TE_CSQC_RACE);
525 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
526 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
529 WriteInt24_t(MSG_ONE, 0);
530 WriteByte(MSG_ONE, 0);
531 WriteString(MSG_ONE, "");
535 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
536 WriteByte(MSG_ONE, lother - lself);
537 WriteString(MSG_ONE, e.netname); // record holder
543 void race_ClearTime(entity e)
545 e.race_checkpoint = 0;
547 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
548 e.race_penalty_accumulator = 0;
549 e.race_lastpenalty = world;
552 WRITESPECTATABLE_MSG_ONE({
553 WriteByte(MSG_ONE, SVC_TEMPENTITY);
554 WriteByte(MSG_ONE, TE_CSQC_RACE);
555 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
559 void dumpsurface(entity e)
563 print("Surfaces of ", etos(e), ":\n");
565 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
569 n = getsurfacenumpoints(e, si);
572 print(" Surface ", ftos(si), ":\n");
573 norm = getsurfacenormal(e, si);
574 print(" Normal = ", vtos(norm), "\n");
575 for(ni = 0; ni < n; ++ni)
577 vec = getsurfacepoint(e, si, ni);
578 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
583 void checkpoint_passed()
588 if(other.classname == "porto")
590 // do not allow portalling through checkpoints
591 trace_plane_normal = normalize(-1 * other.velocity);
600 if not((self.spawnflags & 2) && (other.classname == "player"))
603 oldmsg = self.message;
606 self.message = oldmsg;
609 if(other.classname != "player")
613 * Remove unauthorized equipment
615 Portal_ClearAll(other);
617 other.porto_forbidden = 2; // decreased by 1 each StartFrame
620 if(self.race_checkpoint == -2)
622 self.race_checkpoint = other.race_checkpoint;
627 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
629 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
631 largest_cp_id = cp.race_checkpoint;
632 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
633 cp.race_checkpoint = largest_cp_id + 1; // finish line
634 race_highest_checkpoint = largest_cp_id + 1;
635 race_timed_checkpoint = largest_cp_id + 1;
637 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
638 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
644 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
645 cp.race_checkpoint = 1;
646 race_highest_checkpoint = 1;
647 race_timed_checkpoint = 1;
651 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
653 if(self.race_penalty)
655 if(other.race_lastpenalty != self)
657 other.race_lastpenalty = self;
658 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
662 if(other.race_penalty)
668 if(self.spawnflags & 2)
671 oldmsg = self.message;
674 self.message = oldmsg;
677 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
678 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
679 other.race_respawn_checkpoint = self.race_checkpoint;
680 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
681 other.race_started = 1;
683 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
685 if(!self.race_checkpoint) // start line
687 other.race_laptime = time;
688 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
689 other.race_penalty_accumulator = 0;
690 other.race_lastpenalty = world;
693 if(g_race_qualifying)
694 race_SendNextCheckpoint(other, 0);
696 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
699 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
702 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
703 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
708 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
714 if(self.spawnflags & 4)
715 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
719 void checkpoint_touch()
725 void checkpoint_use()
727 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
734 float race_waypointsprite_visible_for_player(entity e)
736 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
738 else if(e.race_checkpoint == self.owner.race_checkpoint)
745 void trigger_race_checkpoint_verify()
755 qual = g_race_qualifying;
759 self.classname = "player";
763 for(i = 0; i <= race_highest_checkpoint; ++i)
765 self.race_checkpoint = race_NextCheckpoint(i);
767 // race only (middle of the race)
768 g_race_qualifying = 0;
770 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
771 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
776 g_race_qualifying = 1;
777 self.race_place = race_lowest_place_spawn;
778 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
779 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
781 // race only (initial spawn)
782 g_race_qualifying = 0;
783 for(p = 1; p <= race_highest_place_spawn; ++p)
786 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
787 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
792 else if(!defrag_ents)
795 self.race_checkpoint = race_NextCheckpoint(0);
796 g_race_qualifying = 1;
797 self.race_place = race_lowest_place_spawn;
798 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
799 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
803 self.race_checkpoint = race_NextCheckpoint(0);
804 g_race_qualifying = 1;
805 self.race_place = 0; // there's only one spawn on defrag maps
807 // check if a defragcp file already exists, then read it and apply the checkpoint order
812 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
815 while((l = fgets(fh)))
817 len = tokenize_console(l);
819 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
822 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
823 if(argv(0) == cp.targetname)
824 cp.race_checkpoint = stof(argv(1));
830 g_race_qualifying = qual;
832 if(race_timed_checkpoint) {
834 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
835 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
836 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
837 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
839 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
840 if(cp.race_checkpoint == -2) // something's wrong with the defrag cp file or it has not been written yet, set defragcpexists to -1 so that it will be rewritten when someone finishes
844 if(defragcpexists != -1){
846 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
847 if(cp.race_checkpoint > largest_cp_id)
848 largest_cp_id = cp.race_checkpoint;
849 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
850 cp.race_checkpoint = largest_cp_id + 1; // finish line
851 race_highest_checkpoint = largest_cp_id + 1;
852 race_timed_checkpoint = largest_cp_id + 1;
854 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
855 cp.race_checkpoint = 255; // finish line
856 race_highest_checkpoint = 255;
857 race_timed_checkpoint = 255;
861 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
864 if(cp.race_checkpoint == 0)
865 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
866 else if(cp.race_checkpoint == race_timed_checkpoint)
867 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
873 entity trigger, targ;
874 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
875 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
876 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
880 setsize(targ, trigger.mins, trigger.maxs);
881 setorigin(targ, trigger.origin);
889 void spawnfunc_trigger_race_checkpoint()
892 if(!g_race && !g_cts)
900 self.use = checkpoint_use;
901 if not(self.spawnflags & 1)
902 self.touch = checkpoint_touch;
904 o = (self.absmin + self.absmax) * 0.5;
905 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
906 waypoint_spawnforitem_force(self, trace_endpos);
907 self.nearestwaypointtimeout = time + 1000000000;
910 self.message = "went backwards";
912 self.message2 = "was pushed backwards by";
913 if (!self.race_penalty_reason)
914 self.race_penalty_reason = "missing a checkpoint";
916 self.race_checkpoint = self.cnt;
918 if(self.race_checkpoint > race_highest_checkpoint)
920 race_highest_checkpoint = self.race_checkpoint;
921 if(self.spawnflags & 8)
922 race_timed_checkpoint = self.race_checkpoint;
924 race_timed_checkpoint = 0;
927 if(!self.race_penalty)
929 if(self.race_checkpoint)
930 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
932 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
935 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
937 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
940 void spawnfunc_target_checkpoint() // defrag entity
943 if(!g_race && !g_cts)
952 self.use = checkpoint_use;
953 if not(self.spawnflags & 1)
954 self.touch = checkpoint_touch;
956 o = (self.absmin + self.absmax) * 0.5;
957 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
958 waypoint_spawnforitem_force(self, trace_endpos);
959 self.nearestwaypointtimeout = time + 1000000000;
962 self.message = "went backwards";
964 self.message2 = "was pushed backwards by";
965 if (!self.race_penalty_reason)
966 self.race_penalty_reason = "missing a checkpoint";
968 if(self.classname == "target_startTimer")
969 self.race_checkpoint = 0;
971 self.race_checkpoint = -2;
973 race_timed_checkpoint = 1;
975 if(self.race_checkpoint == 0)
976 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
978 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
980 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
982 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
985 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
986 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
988 void race_AbandonRaceCheck(entity p)
990 if(race_completing && !p.race_completed)
992 p.race_completed = 1;
993 MAKE_INDEPENDENT_PLAYER(p);
994 bprint(p.netname, "^7 has abandoned the race.\n");
999 void race_StartCompleting()
1002 race_completing = 1;
1004 if(p.deadflag != DEAD_NO)
1005 race_AbandonRaceCheck(p);
1008 void race_PreparePlayer()
1010 race_ClearTime(self);
1011 self.race_place = 0;
1012 self.race_started = 0;
1013 self.race_respawn_checkpoint = 0;
1014 self.race_respawn_spotref = world;
1017 void race_RetractPlayer()
1019 if(!g_race && !g_cts)
1021 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1022 race_ClearTime(self);
1023 self.race_checkpoint = self.race_respawn_checkpoint;
1028 if(!g_race && !g_cts)
1031 race_AbandonRaceCheck(self);
1034 void race_PreSpawn()
1036 if(!g_race && !g_cts)
1038 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1039 race_PreparePlayer();
1041 race_RetractPlayer();
1043 race_AbandonRaceCheck(self);
1046 void race_PostSpawn(entity spot)
1048 if(!g_race && !g_cts)
1051 if(spot.target == "")
1052 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1053 race_PreparePlayer();
1055 // if we need to respawn, do it right
1056 self.race_respawn_checkpoint = self.race_checkpoint;
1057 self.race_respawn_spotref = spot;
1059 self.race_place = 0;
1062 void race_PreSpawnObserver()
1064 if(!g_race && !g_cts)
1066 race_PreparePlayer();
1067 self.race_checkpoint = -1;
1070 void spawnfunc_info_player_race (void)
1072 if(!g_race && !g_cts)
1078 spawnfunc_info_player_deathmatch();
1080 if(self.race_place > race_highest_place_spawn)
1081 race_highest_place_spawn = self.race_place;
1082 if(self.race_place < race_lowest_place_spawn)
1083 race_lowest_place_spawn = self.race_place;
1086 void race_ClearRecords()
1091 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1093 race_checkpoint_records[i] = 0;
1094 if(race_checkpoint_recordholders[i])
1095 strunzone(race_checkpoint_recordholders[i]);
1096 race_checkpoint_recordholders[i] = string_null;
1100 FOR_EACH_CLIENT(self)
1103 p = self.race_place;
1104 race_PreparePlayer();
1105 self.race_place = p;
1110 void race_ReadyRestart()
1114 Score_NicePrint(world);
1116 race_ClearRecords();
1117 PlayerScore_Sort(race_place);
1124 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1128 print(e.netname, " = ", ftos(e.race_place), "\n");
1131 if(g_race_qualifying == 2)
1133 g_race_qualifying = 0;
1134 independent_players = 0;
1135 cvar_set("fraglimit", ftos(race_fraglimit));
1136 cvar_set("leadlimit", ftos(race_leadlimit));
1137 cvar_set("timelimit", ftos(race_timelimit));
1142 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1144 if(g_race_qualifying)
1146 pl.race_penalty_accumulator += penalty;
1148 WRITESPECTATABLE_MSG_ONE({
1149 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1150 WriteByte(MSG_ONE, TE_CSQC_RACE);
1151 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1152 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1153 WriteString(MSG_ONE, reason);
1158 pl.race_penalty = time + penalty;
1160 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1161 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1162 WriteByte(MSG_ONE, TE_CSQC_RACE);
1163 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1164 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1165 WriteString(MSG_ONE, reason);
1170 void penalty_touch()
1173 if(other.race_lastpenalty != self)
1175 other.race_lastpenalty = self;
1176 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1182 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1185 void spawnfunc_trigger_race_penalty()
1189 self.use = penalty_use;
1190 if not(self.spawnflags & 1)
1191 self.touch = penalty_touch;
1193 if (!self.race_penalty_reason)
1194 self.race_penalty_reason = "missing a checkpoint";
1195 if (!self.race_penalty)
1196 self.race_penalty = 5;
1199 float race_GetFractionalLapCount(entity e)
1201 // interesting metrics (idea by KrimZon) to maybe sort players in the
1202 // scoreboard, immediately updates when overtaking
1204 // requires the track to be built so you never get farther away from the
1205 // next checkpoint, though, and current Xonotic race maps are not built that
1208 // also, this code is slow and would need optimization (i.e. "next CP"
1209 // links on CP entities)
1212 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1213 if(e.race_completed)
1214 return l; // not fractional
1217 float bestfraction, fraction;
1218 entity lastcp, cp0, cp1;
1219 float nextcpindex, lastcpindex;
1221 nextcpindex = max(e.race_checkpoint, 0);
1222 lastcpindex = e.race_respawn_checkpoint;
1223 lastcp = e.race_respawn_spotref;
1225 if(nextcpindex == lastcpindex)
1229 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1231 if(cp0.race_checkpoint != lastcpindex)
1236 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1237 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1239 if(cp1.race_checkpoint != nextcpindex)
1241 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1244 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1245 if(fraction < bestfraction)
1246 bestfraction = fraction;
1250 // we are at CP "nextcpindex - bestfraction"
1251 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1252 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1254 nc = race_highest_checkpoint + 1;
1255 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;