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 void race_SetTime(entity e, float t, float match_rec) {
203 pos = race_GetPos(t);
204 prevpos = race_CheckUID(e.crypto_idfp, e.netname);
207 string recorddifference;
208 if (prevpos && (prevpos < pos || !pos))
210 oldrec = race_GetTime(prevpos);
211 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
212 bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
213 race_SendStatus(0, e); // "fail"
215 } else if (!pos) { // no ranking, time worse than the worst ranked
216 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
217 bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
218 race_SendStatus(0, e); // "fail"
222 oldrec = grecordtime[pos-1];
224 // move other rankings out of the way
226 if (prevpos) { // player improved his existing record
227 for (i=prevpos-1;i>pos-1;--i) {
228 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
229 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
230 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i-1]);
231 grecordtime[i] = grecordtime[i-1];
233 if (grecordholder[i])
234 strunzone(grecordholder[i]);
235 grecordholder[i] = strzone(grecordholder[i-1]);
237 strunzone(grecorduid[i]);
238 grecorduid[i] = strzone(grecorduid[i-1]);
240 } else { // player has no ranked record yet
241 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
242 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
243 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
244 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i-1]);
245 grecordtime[i] = grecordtime[i-1];
247 if (grecordholder[i])
248 strunzone(grecordholder[i]);
249 grecordholder[i] = strzone(grecordholder[i-1]);
251 strunzone(grecorduid[i]);
252 grecorduid[i] = strzone(grecorduid[i-1]);
258 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
259 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
260 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp"), e.crypto_idfp);
264 if (grecordholder[0])
265 strunzone(grecordholder[0]);
266 grecordholder[0] = strzone(e.netname);
268 strunzone(grecorduid[0]);
269 grecorduid[0] = strzone(e.crypto_idfp);
270 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
271 race_send_recordtime(MSG_ALL);
273 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
274 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
275 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(pos-1)), e.crypto_idfp);
277 grecordtime[pos-1] = t;
279 if (grecordholder[pos-1])
280 strunzone(grecordholder[pos-1]);
281 grecordholder[pos-1] = strzone(e.netname);
282 if (grecorduid[pos-1])
283 strunzone(grecorduid[pos-1]);
284 grecorduid[pos-1] = strzone(e.crypto_idfp);
287 if (pos == RANKINGS_CNT)
290 race_SendRankings(pos, prevpos, 0, MSG_ALL);
292 strunzone(rankings_reply);
293 rankings_reply = strzone(getrankings());
296 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
297 bprint(e.netname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
298 } else if (oldrec == 0) {
299 bprint(e.netname, "^1 set the 1st place record with ", TIME_ENCODED_TOSTRING(t), "\n");
301 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
302 bprint(e.netname, "^1 broke ", grecordholder[pos], "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
304 race_SendStatus(3, e); // "new server record"
307 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
308 bprint(e.netname, "^5 improved their ", race_PlaceName(pos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
309 race_SendStatus(1, e); // "new time"
310 } else if (oldrec == 0) {
311 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
312 race_SendStatus(2, e); // "new rank"
314 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
315 bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
316 race_SendStatus(2, e); // "new rank"
321 void race_DeleteTime(float pos) {
324 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
326 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
327 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
328 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp"), grecorduid[1]);
329 grecordtime[0] = grecordtime[1];
330 if (grecordholder[i])
331 strunzone(grecordholder[0]);
332 grecordholder[0] = strzone(grecordholder[1]);
335 strunzone(grecorduid[0]);
336 grecorduid[0] = strzone(grecorduid[1]);
338 else if (i == RANKINGS_CNT-1) {
339 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
340 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
341 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), string_null);
343 if (grecordholder[i])
344 strunzone(grecordholder[i]);
345 grecordholder[i] = string_null;
348 strunzone(grecorduid[i]);
349 grecorduid[i] = string_null;
352 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
353 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
354 db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i+1]);
355 grecordtime[i] = grecordtime[i+1];
356 if (grecordholder[i])
357 strunzone(grecordholder[i]);
358 grecordholder[i] = strzone(grecordholder[i+1]);
361 strunzone(grecorduid[i]);
362 grecorduid[i] = strzone(grecorduid[i+1]);
366 race_SendRankings(pos, 0, 1, MSG_ALL);
368 race_send_recordtime(MSG_ALL);
371 strunzone(rankings_reply);
372 rankings_reply = strzone(getrankings());
377 void race_SendTime(entity e, float cp, float t, float tvalid)
382 if(g_race_qualifying)
383 t += e.race_penalty_accumulator;
385 t = TIME_ENCODE(t); // make integer
386 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
389 if(cp == race_timed_checkpoint) // finish line
390 if not(e.race_completed)
393 if(g_race_qualifying)
395 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
397 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
401 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
402 snew = TIME_ENCODE(time - game_starttime);
403 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
404 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
406 if(cvar("fraglimit"))
407 if(l >= cvar("fraglimit"))
408 race_StartCompleting();
412 e.race_completed = 1;
413 MAKE_INDEPENDENT_PLAYER(e);
414 bprint(e.netname, "^7 has finished the race.\n");
422 if(g_race_qualifying)
426 recordtime = race_checkpoint_records[cp];
427 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
428 if(recordholder == e.netname)
432 if(cp == race_timed_checkpoint)
434 race_SetTime(e, t, recordtime);
435 if(g_cts && cvar("g_cts_finish_kill_delay"))
437 CTS_ClientKill(cvar("g_cts_finish_kill_delay"));
440 if(t < recordtime || recordtime == 0)
442 race_checkpoint_records[cp] = t;
443 if(race_checkpoint_recordholders[cp])
444 strunzone(race_checkpoint_recordholders[cp]);
445 race_checkpoint_recordholders[cp] = strzone(e.netname);
446 if(g_race_qualifying)
448 FOR_EACH_REALPLAYER(p)
449 if(p.race_checkpoint == cp)
450 race_SendNextCheckpoint(p, 0);
464 if(g_race_qualifying)
466 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
467 WriteByte(MSG_ONE, SVC_TEMPENTITY);
468 WriteByte(MSG_ONE, TE_CSQC_RACE);
469 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
470 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
471 WriteInt24_t(MSG_ONE, t); // time to that intermediate
472 WriteInt24_t(MSG_ONE, recordtime); // previously best time
473 WriteString(MSG_ONE, recordholder); // record holder
477 else // RACE! Not Qualifying
479 float lself, lother, othtime;
481 oth = race_checkpoint_lastplayers[cp];
484 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
485 lother = race_checkpoint_lastlaps[cp];
486 othtime = race_checkpoint_lasttimes[cp];
489 lself = lother = othtime = 0;
492 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
493 WriteByte(MSG_ONE, SVC_TEMPENTITY);
494 WriteByte(MSG_ONE, TE_CSQC_RACE);
495 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
496 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
499 WriteInt24_t(MSG_ONE, 0);
500 WriteByte(MSG_ONE, 0);
501 WriteString(MSG_ONE, "");
505 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
506 WriteByte(MSG_ONE, lself - lother);
507 WriteString(MSG_ONE, oth.netname); // record holder
511 race_checkpoint_lastplayers[cp] = e;
512 race_checkpoint_lasttimes[cp] = time;
513 race_checkpoint_lastlaps[cp] = lself;
516 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
517 WriteByte(MSG_ONE, SVC_TEMPENTITY);
518 WriteByte(MSG_ONE, TE_CSQC_RACE);
519 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
520 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
523 WriteInt24_t(MSG_ONE, 0);
524 WriteByte(MSG_ONE, 0);
525 WriteString(MSG_ONE, "");
529 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
530 WriteByte(MSG_ONE, lother - lself);
531 WriteString(MSG_ONE, e.netname); // record holder
537 void race_ClearTime(entity e)
539 e.race_checkpoint = 0;
541 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
542 e.race_penalty_accumulator = 0;
543 e.race_lastpenalty = world;
546 WRITESPECTATABLE_MSG_ONE({
547 WriteByte(MSG_ONE, SVC_TEMPENTITY);
548 WriteByte(MSG_ONE, TE_CSQC_RACE);
549 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
553 void dumpsurface(entity e)
557 print("Surfaces of ", etos(e), ":\n");
559 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
563 n = getsurfacenumpoints(e, si);
566 print(" Surface ", ftos(si), ":\n");
567 norm = getsurfacenormal(e, si);
568 print(" Normal = ", vtos(norm), "\n");
569 for(ni = 0; ni < n; ++ni)
571 vec = getsurfacepoint(e, si, ni);
572 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
577 void checkpoint_passed()
582 if(other.classname == "porto")
584 // do not allow portalling through checkpoints
585 trace_plane_normal = normalize(-1 * other.velocity);
594 if not((self.spawnflags & 2) && (other.classname == "player"))
597 oldmsg = self.message;
600 self.message = oldmsg;
603 if(other.classname != "player")
607 * Remove unauthorized equipment
609 Portal_ClearAll(other);
611 other.porto_forbidden = 2; // decreased by 1 each StartFrame
614 if(self.race_checkpoint == -2)
616 self.race_checkpoint = other.race_checkpoint;
621 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
623 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
625 largest_cp_id = cp.race_checkpoint;
626 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
627 cp.race_checkpoint = largest_cp_id + 1; // finish line
628 race_highest_checkpoint = largest_cp_id + 1;
629 race_timed_checkpoint = largest_cp_id + 1;
631 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
632 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
638 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
639 cp.race_checkpoint = 1;
640 race_highest_checkpoint = 1;
641 race_timed_checkpoint = 1;
645 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
647 if(self.race_penalty)
649 if(other.race_lastpenalty != self)
651 other.race_lastpenalty = self;
652 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
656 if(other.race_penalty)
662 if(self.spawnflags & 2)
665 oldmsg = self.message;
668 self.message = oldmsg;
671 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
672 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
673 other.race_respawn_checkpoint = self.race_checkpoint;
674 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
675 other.race_started = 1;
677 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
679 if(!self.race_checkpoint) // start line
681 other.race_laptime = time;
682 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
683 other.race_penalty_accumulator = 0;
684 other.race_lastpenalty = world;
687 if(g_race_qualifying)
688 race_SendNextCheckpoint(other, 0);
690 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
693 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
696 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
697 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
702 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
708 if(self.spawnflags & 4)
709 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
713 void checkpoint_touch()
719 void checkpoint_use()
721 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
728 float race_waypointsprite_visible_for_player(entity e)
730 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
732 else if(e.race_checkpoint == self.owner.race_checkpoint)
739 void trigger_race_checkpoint_verify()
749 qual = g_race_qualifying;
753 self.classname = "player";
757 for(i = 0; i <= race_highest_checkpoint; ++i)
759 self.race_checkpoint = race_NextCheckpoint(i);
761 // race only (middle of the race)
762 g_race_qualifying = 0;
764 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
765 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
770 g_race_qualifying = 1;
771 self.race_place = race_lowest_place_spawn;
772 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
773 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
775 // race only (initial spawn)
776 g_race_qualifying = 0;
777 for(p = 1; p <= race_highest_place_spawn; ++p)
780 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
781 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
786 else if(!defrag_ents)
789 self.race_checkpoint = race_NextCheckpoint(0);
790 g_race_qualifying = 1;
791 self.race_place = race_lowest_place_spawn;
792 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
793 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
797 self.race_checkpoint = race_NextCheckpoint(0);
798 g_race_qualifying = 1;
799 self.race_place = 0; // there's only one spawn on defrag maps
801 // check if a defragcp file already exists, then read it and apply the checkpoint order
806 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
809 while((l = fgets(fh)))
811 len = tokenize_console(l);
813 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
816 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
817 if(argv(0) == cp.targetname)
818 cp.race_checkpoint = stof(argv(1));
824 g_race_qualifying = qual;
826 if(race_timed_checkpoint) {
828 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
829 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
830 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
831 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
833 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
834 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
838 if(defragcpexists != -1){
840 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
841 if(cp.race_checkpoint > largest_cp_id)
842 largest_cp_id = cp.race_checkpoint;
843 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
844 cp.race_checkpoint = largest_cp_id + 1; // finish line
845 race_highest_checkpoint = largest_cp_id + 1;
846 race_timed_checkpoint = largest_cp_id + 1;
848 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
849 cp.race_checkpoint = 255; // finish line
850 race_highest_checkpoint = 255;
851 race_timed_checkpoint = 255;
855 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
858 if(cp.race_checkpoint == 0)
859 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
860 else if(cp.race_checkpoint == race_timed_checkpoint)
861 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
867 entity trigger, targ;
868 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
869 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
870 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
874 setsize(targ, trigger.mins, trigger.maxs);
875 setorigin(targ, trigger.origin);
883 void spawnfunc_trigger_race_checkpoint()
886 if(!g_race && !g_cts)
894 self.use = checkpoint_use;
895 if not(self.spawnflags & 1)
896 self.touch = checkpoint_touch;
898 o = (self.absmin + self.absmax) * 0.5;
899 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
900 waypoint_spawnforitem_force(self, trace_endpos);
901 self.nearestwaypointtimeout = time + 1000000000;
904 self.message = "went backwards";
906 self.message2 = "was pushed backwards by";
907 if (!self.race_penalty_reason)
908 self.race_penalty_reason = "missing a checkpoint";
910 self.race_checkpoint = self.cnt;
912 if(self.race_checkpoint > race_highest_checkpoint)
914 race_highest_checkpoint = self.race_checkpoint;
915 if(self.spawnflags & 8)
916 race_timed_checkpoint = self.race_checkpoint;
918 race_timed_checkpoint = 0;
921 if(!self.race_penalty)
923 if(self.race_checkpoint)
924 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
926 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
929 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
931 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
934 void spawnfunc_target_checkpoint() // defrag entity
937 if(!g_race && !g_cts)
946 self.use = checkpoint_use;
947 if not(self.spawnflags & 1)
948 self.touch = checkpoint_touch;
950 o = (self.absmin + self.absmax) * 0.5;
951 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
952 waypoint_spawnforitem_force(self, trace_endpos);
953 self.nearestwaypointtimeout = time + 1000000000;
956 self.message = "went backwards";
958 self.message2 = "was pushed backwards by";
959 if (!self.race_penalty_reason)
960 self.race_penalty_reason = "missing a checkpoint";
962 if(self.classname == "target_startTimer")
963 self.race_checkpoint = 0;
965 self.race_checkpoint = -2;
967 race_timed_checkpoint = 1;
969 if(self.race_checkpoint == 0)
970 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
972 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
974 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
976 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
979 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
980 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
982 void race_AbandonRaceCheck(entity p)
984 if(race_completing && !p.race_completed)
986 p.race_completed = 1;
987 MAKE_INDEPENDENT_PLAYER(p);
988 bprint(p.netname, "^7 has abandoned the race.\n");
993 void race_StartCompleting()
998 if(p.deadflag != DEAD_NO)
999 race_AbandonRaceCheck(p);
1002 void race_PreparePlayer()
1004 race_ClearTime(self);
1005 self.race_place = 0;
1006 self.race_started = 0;
1007 self.race_respawn_checkpoint = 0;
1008 self.race_respawn_spotref = world;
1011 void race_RetractPlayer()
1013 if(!g_race && !g_cts)
1015 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1016 race_ClearTime(self);
1017 self.race_checkpoint = self.race_respawn_checkpoint;
1022 if(!g_race && !g_cts)
1025 race_AbandonRaceCheck(self);
1028 void race_PreSpawn()
1030 if(!g_race && !g_cts)
1032 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1033 race_PreparePlayer();
1035 race_RetractPlayer();
1037 race_AbandonRaceCheck(self);
1040 void race_PostSpawn(entity spot)
1042 if(!g_race && !g_cts)
1045 if(spot.target == "")
1046 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1047 race_PreparePlayer();
1049 // if we need to respawn, do it right
1050 self.race_respawn_checkpoint = self.race_checkpoint;
1051 self.race_respawn_spotref = spot;
1053 self.race_place = 0;
1056 void race_PreSpawnObserver()
1058 if(!g_race && !g_cts)
1060 race_PreparePlayer();
1061 self.race_checkpoint = -1;
1064 void spawnfunc_info_player_race (void)
1066 if(!g_race && !g_cts)
1072 spawnfunc_info_player_deathmatch();
1074 if(self.race_place > race_highest_place_spawn)
1075 race_highest_place_spawn = self.race_place;
1076 if(self.race_place < race_lowest_place_spawn)
1077 race_lowest_place_spawn = self.race_place;
1080 void race_ClearRecords()
1085 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1087 race_checkpoint_records[i] = 0;
1088 if(race_checkpoint_recordholders[i])
1089 strunzone(race_checkpoint_recordholders[i]);
1090 race_checkpoint_recordholders[i] = string_null;
1094 FOR_EACH_CLIENT(self)
1097 p = self.race_place;
1098 race_PreparePlayer();
1099 self.race_place = p;
1104 void race_ReadyRestart()
1108 Score_NicePrint(world);
1110 race_ClearRecords();
1111 PlayerScore_Sort(race_place);
1118 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1122 print(e.netname, " = ", ftos(e.race_place), "\n");
1125 if(g_race_qualifying == 2)
1127 g_race_qualifying = 0;
1128 independent_players = 0;
1129 cvar_set("fraglimit", ftos(race_fraglimit));
1130 cvar_set("leadlimit", ftos(race_leadlimit));
1131 cvar_set("timelimit", ftos(race_timelimit));
1136 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1138 if(g_race_qualifying)
1140 pl.race_penalty_accumulator += penalty;
1142 WRITESPECTATABLE_MSG_ONE({
1143 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1144 WriteByte(MSG_ONE, TE_CSQC_RACE);
1145 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1146 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1147 WriteString(MSG_ONE, reason);
1152 pl.race_penalty = time + penalty;
1154 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1155 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1156 WriteByte(MSG_ONE, TE_CSQC_RACE);
1157 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1158 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1159 WriteString(MSG_ONE, reason);
1164 void penalty_touch()
1167 if(other.race_lastpenalty != self)
1169 other.race_lastpenalty = self;
1170 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1176 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1179 void spawnfunc_trigger_race_penalty()
1183 self.use = penalty_use;
1184 if not(self.spawnflags & 1)
1185 self.touch = penalty_touch;
1187 if (!self.race_penalty_reason)
1188 self.race_penalty_reason = "missing a checkpoint";
1189 if (!self.race_penalty)
1190 self.race_penalty = 5;
1193 float race_GetFractionalLapCount(entity e)
1195 // interesting metrics (idea by KrimZon) to maybe sort players in the
1196 // scoreboard, immediately updates when overtaking
1198 // requires the track to be built so you never get farther away from the
1199 // next checkpoint, though, and current Xonotic race maps are not built that
1202 // also, this code is slow and would need optimization (i.e. "next CP"
1203 // links on CP entities)
1206 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1207 if(e.race_completed)
1208 return l; // not fractional
1211 float bestfraction, fraction;
1212 entity lastcp, cp0, cp1;
1213 float nextcpindex, lastcpindex;
1215 nextcpindex = max(e.race_checkpoint, 0);
1216 lastcpindex = e.race_respawn_checkpoint;
1217 lastcp = e.race_respawn_spotref;
1219 if(nextcpindex == lastcpindex)
1223 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1225 if(cp0.race_checkpoint != lastcpindex)
1230 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1231 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1233 if(cp1.race_checkpoint != nextcpindex)
1235 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1238 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1239 if(fraction < bestfraction)
1240 bestfraction = fraction;
1244 // we are at CP "nextcpindex - bestfraction"
1245 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1246 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1248 nc = race_highest_checkpoint + 1;
1249 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;