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];
105 string grecorduid[RANKINGS_CNT];
107 float worst_time; // last ranked time
108 float have_recs; // have we already read the records from the database before?
109 float race_GetTime(float pos) {
115 if (!have_recs) { // I guess this is better than checking if the first array item is empty, rumor has it that arrays are slow
117 for(i=0;i<RANKINGS_CNT;++i) {
118 grecordtime[i] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i))));
119 grecordholder[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i))));
121 grecorduid[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i))));
124 grecordtime[0] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
125 grecordholder[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname")));
127 grecorduid[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "uid")));
129 worst_time = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, strcat("time", ftos(RANKINGS_CNT-1)))));
133 return grecordtime[pos-1];
136 string race_GetName(float pos) { // these other functions assume that race_GetTime has been called >= once before
137 return grecordholder[pos-1];
141 float race_CheckUID(string myuid, string net_name) { // return existing UID or player name ranking pos, else 0
143 for (i=RANKINGS_CNT-1;i>=0;--i)
144 if(grecorduid[i] == myuid)
146 for (i=RANKINGS_CNT-1;i>=0;--i)
147 if(grecordholder[i] == net_name)
153 float race_CheckName(string net_name) { // Does the name already exist in rankings? In that case, where? (otherwise 0)
155 for (i=RANKINGS_CNT-1;i>=0;--i)
156 if(grecordholder[i] == net_name)
161 float race_GetPos(float t) {
165 for (i=0;i<RANKINGS_CNT;++i)
166 if (grecordtime[i] == 0 || grecordtime[i] > t)
169 for (i=0;i<RANKINGS_CNT;++i)
170 if (grecordtime[i] > t)
175 void race_send_recordtime(float msg)
177 // send the server best time
178 WriteByte(msg, SVC_TEMPENTITY);
179 WriteByte(msg, TE_CSQC_RACE);
180 WriteByte(msg, RACE_NET_SERVER_RECORD);
181 WriteInt24_t(msg, race_GetTime(1));
184 void race_SendRankings(float pos, float prevpos, float del, float msg)
186 WriteByte(msg, SVC_TEMPENTITY);
187 WriteByte(msg, TE_CSQC_RACE);
188 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
189 WriteShort(msg, pos);
190 WriteShort(msg, prevpos);
191 WriteShort(msg, del);
192 WriteString(msg, race_GetName(pos));
193 WriteInt24_t(msg, race_GetTime(pos));
196 void race_SendStatus(float id, entity e)
204 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
205 WriteByte(msg, SVC_TEMPENTITY);
206 WriteByte(msg, TE_CSQC_RACE);
207 WriteByte(msg, RACE_NET_SERVER_STATUS);
209 WriteString(msg, e.netname);
213 string race_PlaceName(float pos) {
221 return strcat(ftos(pos), "th");
224 void race_SetTime(entity e, float t, float match_rec) {
226 pos = race_GetPos(t);
228 prevpos = race_CheckUID(e.uid, e.netname);
230 prevpos = race_CheckName(e.netname);
234 string recorddifference;
235 if (prevpos && (prevpos < pos || !pos))
237 oldrec = race_GetTime(prevpos);
238 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
239 bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
240 race_SendStatus(0, e); // "fail"
242 } else if (!pos) { // no ranking, time worse than the worst ranked
243 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
244 bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
245 race_SendStatus(0, e); // "fail"
249 oldrec = grecordtime[pos-1];
251 // move other rankings out of the way
253 if (prevpos) { // player improved his existing record
254 for (i=prevpos-1;i>pos-1;--i) {
255 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
256 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
258 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
260 grecordtime[i] = grecordtime[i-1];
262 if (grecordholder[i])
263 strunzone(grecordholder[i]);
264 grecordholder[i] = strzone(grecordholder[i-1]);
267 strunzone(grecorduid[i]);
268 grecorduid[i] = strzone(grecorduid[i-1]);
271 } else { // player has no ranked record yet
272 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
273 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
274 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
276 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
278 grecordtime[i] = grecordtime[i-1];
280 if (grecordholder[i])
281 strunzone(grecordholder[i]);
282 grecordholder[i] = strzone(grecordholder[i-1]);
285 strunzone(grecorduid[i]);
286 grecorduid[i] = strzone(grecorduid[i-1]);
293 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
294 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
296 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), e.uid);
301 if (grecordholder[0])
302 strunzone(grecordholder[0]);
303 grecordholder[0] = strzone(e.netname);
306 strunzone(grecorduid[0]);
307 grecorduid[0] = strzone(e.uid);
309 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
310 race_send_recordtime(MSG_ALL);
312 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
313 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
315 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(pos-1)), e.uid);
318 grecordtime[pos-1] = t;
320 if (grecordholder[pos-1])
321 strunzone(grecordholder[pos-1]);
322 grecordholder[pos-1] = strzone(e.netname);
324 if (grecorduid[pos-1])
325 strunzone(grecorduid[pos-1]);
326 grecorduid[pos-1] = strzone(e.uid);
330 if (pos == RANKINGS_CNT)
333 race_SendRankings(pos, prevpos, 0, MSG_ALL);
335 strunzone(rankings_reply);
336 rankings_reply = strzone(getrankings());
339 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
340 bprint(e.netname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
341 } else if (oldrec == 0) {
342 bprint(e.netname, "^1 set the 1st place record with ", TIME_ENCODED_TOSTRING(t), "\n");
344 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
345 bprint(e.netname, "^1 broke ", grecordholder[pos], "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
347 race_SendStatus(3, e); // "new server record"
350 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
351 bprint(e.netname, "^5 improved their ", race_PlaceName(pos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
352 race_SendStatus(1, e); // "new time"
353 } else if (oldrec == 0) {
354 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
355 race_SendStatus(2, e); // "new rank"
357 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
358 bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
359 race_SendStatus(2, e); // "new rank"
364 void race_DeleteTime(float pos) {
367 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
369 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
370 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
372 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), grecorduid[1]);
374 grecordtime[0] = grecordtime[1];
375 if (grecordholder[i])
376 strunzone(grecordholder[0]);
377 grecordholder[0] = strzone(grecordholder[1]);
381 strunzone(grecorduid[0]);
382 grecorduid[0] = strzone(grecorduid[1]);
385 else if (i == RANKINGS_CNT-1) {
386 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
387 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
389 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), string_null);
392 if (grecordholder[i])
393 strunzone(grecordholder[i]);
394 grecordholder[i] = string_null;
398 strunzone(grecorduid[i]);
399 grecorduid[i] = string_null;
403 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
404 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
406 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i+1]);
408 grecordtime[i] = grecordtime[i+1];
409 if (grecordholder[i])
410 strunzone(grecordholder[i]);
411 grecordholder[i] = strzone(grecordholder[i+1]);
415 strunzone(grecorduid[i]);
416 grecorduid[i] = strzone(grecorduid[i+1]);
421 race_SendRankings(pos, 0, 1, MSG_ALL);
423 race_send_recordtime(MSG_ALL);
426 strunzone(rankings_reply);
427 rankings_reply = strzone(getrankings());
432 void race_SendTime(entity e, float cp, float t, float tvalid)
437 if(g_race_qualifying)
438 t += e.race_penalty_accumulator;
440 t = TIME_ENCODE(t); // make integer
441 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
444 if(cp == race_timed_checkpoint) // finish line
445 if not(e.race_completed)
448 if(g_race_qualifying)
450 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
452 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
456 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
457 snew = TIME_ENCODE(time - game_starttime);
458 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
459 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
461 if(cvar("fraglimit"))
462 if(l >= cvar("fraglimit"))
463 race_StartCompleting();
467 e.race_completed = 1;
468 MAKE_INDEPENDENT_PLAYER(e);
469 bprint(e.netname, "^7 has finished the race.\n");
477 if(g_race_qualifying)
481 recordtime = race_checkpoint_records[cp];
482 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
483 if(recordholder == e.netname)
487 if(cp == race_timed_checkpoint)
488 race_SetTime(e, t, recordtime);
490 if(t < recordtime || recordtime == 0)
492 race_checkpoint_records[cp] = t;
493 if(race_checkpoint_recordholders[cp])
494 strunzone(race_checkpoint_recordholders[cp]);
495 race_checkpoint_recordholders[cp] = strzone(e.netname);
496 if(g_race_qualifying)
498 FOR_EACH_REALPLAYER(p)
499 if(p.race_checkpoint == cp)
500 race_SendNextCheckpoint(p, 0);
514 if(g_race_qualifying)
516 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
517 WriteByte(MSG_ONE, SVC_TEMPENTITY);
518 WriteByte(MSG_ONE, TE_CSQC_RACE);
519 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
520 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
521 WriteInt24_t(MSG_ONE, t); // time to that intermediate
522 WriteInt24_t(MSG_ONE, recordtime); // previously best time
523 WriteString(MSG_ONE, recordholder); // record holder
527 else // RACE! Not Qualifying
529 float lself, lother, othtime;
531 oth = race_checkpoint_lastplayers[cp];
534 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
535 lother = race_checkpoint_lastlaps[cp];
536 othtime = race_checkpoint_lasttimes[cp];
539 lself = lother = othtime = 0;
542 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
543 WriteByte(MSG_ONE, SVC_TEMPENTITY);
544 WriteByte(MSG_ONE, TE_CSQC_RACE);
545 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
546 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
549 WriteInt24_t(MSG_ONE, 0);
550 WriteByte(MSG_ONE, 0);
551 WriteString(MSG_ONE, "");
555 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
556 WriteByte(MSG_ONE, lself - lother);
557 WriteString(MSG_ONE, oth.netname); // record holder
561 race_checkpoint_lastplayers[cp] = e;
562 race_checkpoint_lasttimes[cp] = time;
563 race_checkpoint_lastlaps[cp] = lself;
566 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
567 WriteByte(MSG_ONE, SVC_TEMPENTITY);
568 WriteByte(MSG_ONE, TE_CSQC_RACE);
569 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
570 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
573 WriteInt24_t(MSG_ONE, 0);
574 WriteByte(MSG_ONE, 0);
575 WriteString(MSG_ONE, "");
579 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
580 WriteByte(MSG_ONE, lother - lself);
581 WriteString(MSG_ONE, e.netname); // record holder
587 void race_ClearTime(entity e)
589 e.race_checkpoint = 0;
591 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
592 e.race_penalty_accumulator = 0;
593 e.race_lastpenalty = world;
596 WRITESPECTATABLE_MSG_ONE({
597 WriteByte(MSG_ONE, SVC_TEMPENTITY);
598 WriteByte(MSG_ONE, TE_CSQC_RACE);
599 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
603 void dumpsurface(entity e)
607 print("Surfaces of ", etos(e), ":\n");
609 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
613 n = getsurfacenumpoints(e, si);
616 print(" Surface ", ftos(si), ":\n");
617 norm = getsurfacenormal(e, si);
618 print(" Normal = ", vtos(norm), "\n");
619 for(ni = 0; ni < n; ++ni)
621 vec = getsurfacepoint(e, si, ni);
622 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
627 void checkpoint_passed()
632 if(other.classname == "porto")
634 // do not allow portalling through checkpoints
635 trace_plane_normal = normalize(-1 * other.velocity);
644 if not((self.spawnflags & 2) && (other.classname == "player"))
647 oldmsg = self.message;
650 self.message = oldmsg;
653 if(other.classname != "player")
657 * Remove unauthorized equipment
659 Portal_ClearAll(other);
661 other.porto_forbidden = 2; // decreased by 1 each StartFrame
664 if(self.race_checkpoint == -2)
666 self.race_checkpoint = other.race_checkpoint;
671 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
673 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
675 largest_cp_id = cp.race_checkpoint;
676 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
677 cp.race_checkpoint = largest_cp_id + 1; // finish line
678 race_highest_checkpoint = largest_cp_id + 1;
679 race_timed_checkpoint = largest_cp_id + 1;
681 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
682 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
688 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
689 cp.race_checkpoint = 1;
690 race_highest_checkpoint = 1;
691 race_timed_checkpoint = 1;
695 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
697 if(self.race_penalty)
699 if(other.race_lastpenalty != self)
701 other.race_lastpenalty = self;
702 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
706 if(other.race_penalty)
712 if(self.spawnflags & 2)
715 oldmsg = self.message;
718 self.message = oldmsg;
721 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
722 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
723 other.race_respawn_checkpoint = self.race_checkpoint;
724 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
725 other.race_started = 1;
727 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
729 if(!self.race_checkpoint) // start line
731 other.race_laptime = time;
732 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
733 other.race_penalty_accumulator = 0;
734 other.race_lastpenalty = world;
737 if(g_race_qualifying)
738 race_SendNextCheckpoint(other, 0);
740 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
743 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
746 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
747 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
752 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
758 if(self.spawnflags & 4)
759 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
763 void checkpoint_touch()
769 void checkpoint_use()
771 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
778 float race_waypointsprite_visible_for_player(entity e)
780 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
782 else if(e.race_checkpoint == self.owner.race_checkpoint)
789 void trigger_race_checkpoint_verify()
799 qual = g_race_qualifying;
803 self.classname = "player";
807 for(i = 0; i <= race_highest_checkpoint; ++i)
809 self.race_checkpoint = race_NextCheckpoint(i);
811 // race only (middle of the race)
812 g_race_qualifying = 0;
814 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
815 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
820 g_race_qualifying = 1;
821 self.race_place = race_lowest_place_spawn;
822 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
823 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
825 // race only (initial spawn)
826 g_race_qualifying = 0;
827 for(p = 1; p <= race_highest_place_spawn; ++p)
830 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
831 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
836 else if(!defrag_ents)
839 self.race_checkpoint = race_NextCheckpoint(0);
840 g_race_qualifying = 1;
841 self.race_place = race_lowest_place_spawn;
842 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
843 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
847 self.race_checkpoint = race_NextCheckpoint(0);
848 g_race_qualifying = 1;
849 self.race_place = 0; // there's only one spawn on defrag maps
851 // check if a defragcp file already exists, then read it and apply the checkpoint order
856 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
859 while((l = fgets(fh)))
861 len = tokenize_console(l);
863 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
866 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
867 if(argv(0) == cp.targetname)
868 cp.race_checkpoint = stof(argv(1));
874 g_race_qualifying = qual;
876 if(race_timed_checkpoint) {
878 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
879 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
880 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
881 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
883 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
884 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
888 if(defragcpexists != -1){
890 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
891 if(cp.race_checkpoint > largest_cp_id)
892 largest_cp_id = cp.race_checkpoint;
893 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
894 cp.race_checkpoint = largest_cp_id + 1; // finish line
895 race_highest_checkpoint = largest_cp_id + 1;
896 race_timed_checkpoint = largest_cp_id + 1;
898 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
899 cp.race_checkpoint = 255; // finish line
900 race_highest_checkpoint = 255;
901 race_timed_checkpoint = 255;
905 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
908 if(cp.race_checkpoint == 0)
909 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
910 else if(cp.race_checkpoint == race_timed_checkpoint)
911 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
917 entity trigger, targ;
918 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
919 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
920 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
924 setsize(targ, trigger.mins, trigger.maxs);
925 setorigin(targ, trigger.origin);
933 void spawnfunc_trigger_race_checkpoint()
936 if(!g_race && !g_cts)
944 self.use = checkpoint_use;
945 if not(self.spawnflags & 1)
946 self.touch = checkpoint_touch;
948 o = (self.absmin + self.absmax) * 0.5;
949 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
950 waypoint_spawnforitem_force(self, trace_endpos);
951 self.nearestwaypointtimeout = time + 1000000000;
954 self.message = "went backwards";
956 self.message2 = "was pushed backwards by";
957 if (!self.race_penalty_reason)
958 self.race_penalty_reason = "missing a checkpoint";
960 self.race_checkpoint = self.cnt;
962 if(self.race_checkpoint > race_highest_checkpoint)
964 race_highest_checkpoint = self.race_checkpoint;
965 if(self.spawnflags & 8)
966 race_timed_checkpoint = self.race_checkpoint;
968 race_timed_checkpoint = 0;
971 if(!self.race_penalty)
973 if(self.race_checkpoint)
974 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
976 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
979 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
981 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
984 void spawnfunc_target_checkpoint() // defrag entity
987 if(!g_race && !g_cts)
996 self.use = checkpoint_use;
997 if not(self.spawnflags & 1)
998 self.touch = checkpoint_touch;
1000 o = (self.absmin + self.absmax) * 0.5;
1001 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
1002 waypoint_spawnforitem_force(self, trace_endpos);
1003 self.nearestwaypointtimeout = time + 1000000000;
1006 self.message = "went backwards";
1008 self.message2 = "was pushed backwards by";
1009 if (!self.race_penalty_reason)
1010 self.race_penalty_reason = "missing a checkpoint";
1012 if(self.classname == "target_startTimer")
1013 self.race_checkpoint = 0;
1015 self.race_checkpoint = -2;
1017 race_timed_checkpoint = 1;
1019 if(self.race_checkpoint == 0)
1020 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
1022 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
1024 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1026 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1029 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1030 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1032 void race_AbandonRaceCheck(entity p)
1034 if(race_completing && !p.race_completed)
1036 p.race_completed = 1;
1037 MAKE_INDEPENDENT_PLAYER(p);
1038 bprint(p.netname, "^7 has abandoned the race.\n");
1039 ClientData_Touch(p);
1043 void race_StartCompleting()
1046 race_completing = 1;
1048 if(p.deadflag != DEAD_NO)
1049 race_AbandonRaceCheck(p);
1052 void race_PreparePlayer()
1054 race_ClearTime(self);
1055 self.race_place = 0;
1056 self.race_started = 0;
1057 self.race_respawn_checkpoint = 0;
1058 self.race_respawn_spotref = world;
1061 void race_RetractPlayer()
1063 if(!g_race && !g_cts)
1065 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1066 race_ClearTime(self);
1067 self.race_checkpoint = self.race_respawn_checkpoint;
1072 if(!g_race && !g_cts)
1075 race_AbandonRaceCheck(self);
1078 void race_PreSpawn()
1080 if(!g_race && !g_cts)
1082 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1083 race_PreparePlayer();
1085 race_RetractPlayer();
1087 race_AbandonRaceCheck(self);
1090 void race_PostSpawn(entity spot)
1092 if(!g_race && !g_cts)
1095 if(spot.target == "")
1096 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1097 race_PreparePlayer();
1099 // if we need to respawn, do it right
1100 self.race_respawn_checkpoint = self.race_checkpoint;
1101 self.race_respawn_spotref = spot;
1103 self.race_place = 0;
1106 void race_PreSpawnObserver()
1108 if(!g_race && !g_cts)
1110 race_PreparePlayer();
1111 self.race_checkpoint = -1;
1114 void spawnfunc_info_player_race (void)
1116 if(!g_race && !g_cts)
1122 spawnfunc_info_player_deathmatch();
1124 if(self.race_place > race_highest_place_spawn)
1125 race_highest_place_spawn = self.race_place;
1126 if(self.race_place < race_lowest_place_spawn)
1127 race_lowest_place_spawn = self.race_place;
1130 void race_ClearRecords()
1135 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1137 race_checkpoint_records[i] = 0;
1138 if(race_checkpoint_recordholders[i])
1139 strunzone(race_checkpoint_recordholders[i]);
1140 race_checkpoint_recordholders[i] = string_null;
1144 FOR_EACH_CLIENT(self)
1147 p = self.race_place;
1148 race_PreparePlayer();
1149 self.race_place = p;
1154 void race_ReadyRestart()
1158 Score_NicePrint(world);
1160 race_ClearRecords();
1161 PlayerScore_Sort(race_place);
1168 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1172 print(e.netname, " = ", ftos(e.race_place), "\n");
1175 if(g_race_qualifying == 2)
1177 g_race_qualifying = 0;
1178 independent_players = 0;
1179 cvar_set("fraglimit", ftos(race_fraglimit));
1180 cvar_set("leadlimit", ftos(race_leadlimit));
1181 cvar_set("timelimit", ftos(race_timelimit));
1186 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1188 if(g_race_qualifying)
1190 pl.race_penalty_accumulator += penalty;
1192 WRITESPECTATABLE_MSG_ONE({
1193 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1194 WriteByte(MSG_ONE, TE_CSQC_RACE);
1195 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1196 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1197 WriteString(MSG_ONE, reason);
1202 pl.race_penalty = time + penalty;
1204 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1205 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1206 WriteByte(MSG_ONE, TE_CSQC_RACE);
1207 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1208 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1209 WriteString(MSG_ONE, reason);
1214 void penalty_touch()
1217 if(other.race_lastpenalty != self)
1219 other.race_lastpenalty = self;
1220 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1226 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1229 void spawnfunc_trigger_race_penalty()
1233 self.use = penalty_use;
1234 if not(self.spawnflags & 1)
1235 self.touch = penalty_touch;
1237 if (!self.race_penalty_reason)
1238 self.race_penalty_reason = "missing a checkpoint";
1239 if (!self.race_penalty)
1240 self.race_penalty = 5;
1243 float race_GetFractionalLapCount(entity e)
1245 // interesting metrics (idea by KrimZon) to maybe sort players in the
1246 // scoreboard, immediately updates when overtaking
1248 // requires the track to be built so you never get farther away from the
1249 // next checkpoint, though, and current Xonotic race maps are not built that
1252 // also, this code is slow and would need optimization (i.e. "next CP"
1253 // links on CP entities)
1256 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1257 if(e.race_completed)
1258 return l; // not fractional
1261 float bestfraction, fraction;
1262 entity lastcp, cp0, cp1;
1263 float nextcpindex, lastcpindex;
1265 nextcpindex = max(e.race_checkpoint, 0);
1266 lastcpindex = e.race_respawn_checkpoint;
1267 lastcp = e.race_respawn_spotref;
1269 if(nextcpindex == lastcpindex)
1273 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1275 if(cp0.race_checkpoint != lastcpindex)
1280 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1281 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1283 if(cp1.race_checkpoint != nextcpindex)
1285 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1288 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1289 if(fraction < bestfraction)
1290 bestfraction = fraction;
1294 // we are at CP "nextcpindex - bestfraction"
1295 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1296 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1298 nc = race_highest_checkpoint + 1;
1299 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;