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)
200 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
201 WriteByte(msg, SVC_TEMPENTITY);
202 WriteByte(msg, TE_CSQC_RACE);
203 WriteByte(msg, RACE_NET_SERVER_STATUS);
208 string race_PlaceName(float pos) {
216 return strcat(ftos(pos), "th");
219 void race_SetTime(entity e, float t) {
220 if (worst_time && t > worst_time)
224 string recorddifference;
226 pos = race_GetPos(t);
229 prevpos = race_CheckName(e.netname);
231 prevpos = race_CheckUID(e.uid, e.netname);
233 if (prevpos && (prevpos < pos || !pos))
235 oldrec = race_GetTime(prevpos);
236 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
237 bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
240 } else if (!pos) { // no ranking, time worse than the worst ranked
241 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
242 bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
246 // move other rankings out of the way
248 if (prevpos) { // player improved his existing record
250 oldrec = grecordtime[pos-1];
252 oldrec = grecordtime[pos-1];
253 for (i=prevpos-1;i>pos-1;--i) {
254 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
255 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
257 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
259 grecordtime[i] = grecordtime[i-1];
261 if (grecordholder[i])
262 strunzone(grecordholder[i]);
263 grecordholder[i] = strzone(grecordholder[i-1]);
266 strunzone(grecorduid[i]);
267 grecorduid[i] = strzone(grecorduid[i-1]);
270 } else { // player has no ranked record yet
271 oldrec = grecordtime[pos-1];
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 race_SendStatus(2, e); // "new record"
343 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
344 bprint(e.netname, "^1 broke ", grecordholder[pos], "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
345 race_SendStatus(2, e); // "new record"
349 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
350 bprint(e.netname, "^3 improved their ", race_PlaceName(pos), " ^3place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
351 race_SendStatus(0, e); // "new time"
352 } else if (oldrec == 0) {
353 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
354 race_SendStatus(1, e); // "new rank"
356 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
357 bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
358 race_SendStatus(1, e); // "new rank"
363 void race_DeleteTime(float pos) {
366 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
368 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
369 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
371 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), grecorduid[1]);
373 grecordtime[0] = grecordtime[1];
374 if (grecordholder[i])
375 strunzone(grecordholder[0]);
376 grecordholder[0] = strzone(grecordholder[1]);
380 strunzone(grecorduid[0]);
381 grecorduid[0] = strzone(grecorduid[1]);
384 else if (i == RANKINGS_CNT-1) {
385 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
386 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
388 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), string_null);
391 if (grecordholder[i])
392 strunzone(grecordholder[i]);
393 grecordholder[i] = string_null;
397 strunzone(grecorduid[i]);
398 grecorduid[i] = string_null;
402 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
403 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
405 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i+1]);
407 grecordtime[i] = grecordtime[i+1];
408 if (grecordholder[i])
409 strunzone(grecordholder[i]);
410 grecordholder[i] = strzone(grecordholder[i+1]);
414 strunzone(grecorduid[i]);
415 grecorduid[i] = strzone(grecorduid[i+1]);
420 race_SendRankings(pos, 0, 1, MSG_ALL);
422 race_send_recordtime(MSG_ALL);
425 strunzone(rankings_reply);
426 rankings_reply = strzone(getrankings());
431 void race_SendTime(entity e, float cp, float t, float tvalid)
436 if(g_race_qualifying)
437 t += e.race_penalty_accumulator;
439 t = TIME_ENCODE(t); // make integer
440 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
443 if(cp == race_timed_checkpoint) // finish line
444 if not(e.race_completed)
447 if(g_race_qualifying)
449 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
451 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
455 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
456 snew = TIME_ENCODE(time - game_starttime);
457 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
458 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
460 if(cvar("fraglimit"))
461 if(l >= cvar("fraglimit"))
462 race_StartCompleting();
466 e.race_completed = 1;
467 MAKE_INDEPENDENT_PLAYER(e);
468 bprint(e.netname, "^7 has finished the race.\n");
476 if(g_race_qualifying)
480 recordtime = race_checkpoint_records[cp];
481 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
482 if(recordholder == e.netname)
486 if(t < worst_time || worst_time == 0)
488 if(cp == race_timed_checkpoint)
493 if(t < recordtime || recordtime == 0)
495 race_checkpoint_records[cp] = t;
496 if(race_checkpoint_recordholders[cp])
497 strunzone(race_checkpoint_recordholders[cp]);
498 race_checkpoint_recordholders[cp] = strzone(e.netname);
499 if(g_race_qualifying)
501 FOR_EACH_REALPLAYER(p)
502 if(p.race_checkpoint == cp)
503 race_SendNextCheckpoint(p, 0);
517 if(g_race_qualifying)
519 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
520 WriteByte(MSG_ONE, SVC_TEMPENTITY);
521 WriteByte(MSG_ONE, TE_CSQC_RACE);
522 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
523 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
524 WriteInt24_t(MSG_ONE, t); // time to that intermediate
525 WriteInt24_t(MSG_ONE, recordtime); // previously best time
526 WriteString(MSG_ONE, recordholder); // record holder
530 else // RACE! Not Qualifying
532 float lself, lother, othtime;
534 oth = race_checkpoint_lastplayers[cp];
537 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
538 lother = race_checkpoint_lastlaps[cp];
539 othtime = race_checkpoint_lasttimes[cp];
542 lself = lother = othtime = 0;
545 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
546 WriteByte(MSG_ONE, SVC_TEMPENTITY);
547 WriteByte(MSG_ONE, TE_CSQC_RACE);
548 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
549 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
552 WriteInt24_t(MSG_ONE, 0);
553 WriteByte(MSG_ONE, 0);
554 WriteString(MSG_ONE, "");
558 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
559 WriteByte(MSG_ONE, lself - lother);
560 WriteString(MSG_ONE, oth.netname); // record holder
564 race_checkpoint_lastplayers[cp] = e;
565 race_checkpoint_lasttimes[cp] = time;
566 race_checkpoint_lastlaps[cp] = lself;
569 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
570 WriteByte(MSG_ONE, SVC_TEMPENTITY);
571 WriteByte(MSG_ONE, TE_CSQC_RACE);
572 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
573 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
576 WriteInt24_t(MSG_ONE, 0);
577 WriteByte(MSG_ONE, 0);
578 WriteString(MSG_ONE, "");
582 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
583 WriteByte(MSG_ONE, lother - lself);
584 WriteString(MSG_ONE, e.netname); // record holder
590 void race_ClearTime(entity e)
592 e.race_checkpoint = 0;
594 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
595 e.race_penalty_accumulator = 0;
596 e.race_lastpenalty = world;
599 WRITESPECTATABLE_MSG_ONE({
600 WriteByte(MSG_ONE, SVC_TEMPENTITY);
601 WriteByte(MSG_ONE, TE_CSQC_RACE);
602 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
606 void dumpsurface(entity e)
610 print("Surfaces of ", etos(e), ":\n");
612 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
616 n = getsurfacenumpoints(e, si);
619 print(" Surface ", ftos(si), ":\n");
620 norm = getsurfacenormal(e, si);
621 print(" Normal = ", vtos(norm), "\n");
622 for(ni = 0; ni < n; ++ni)
624 vec = getsurfacepoint(e, si, ni);
625 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
630 void checkpoint_passed()
635 if(other.classname == "porto")
637 // do not allow portalling through checkpoints
638 trace_plane_normal = normalize(-1 * other.velocity);
647 if not((self.spawnflags & 2) && (other.classname == "player"))
650 oldmsg = self.message;
653 self.message = oldmsg;
656 if(other.classname != "player")
660 * Remove unauthorized equipment
662 Portal_ClearAll(other);
664 other.porto_forbidden = 2; // decreased by 1 each StartFrame
667 if(self.race_checkpoint == -2)
669 self.race_checkpoint = other.race_checkpoint;
674 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
676 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
678 largest_cp_id = cp.race_checkpoint;
679 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
680 cp.race_checkpoint = largest_cp_id + 1; // finish line
681 race_highest_checkpoint = largest_cp_id + 1;
682 race_timed_checkpoint = largest_cp_id + 1;
684 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
685 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
691 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
692 cp.race_checkpoint = 1;
693 race_highest_checkpoint = 1;
694 race_timed_checkpoint = 1;
698 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
700 if(self.race_penalty)
702 if(other.race_lastpenalty != self)
704 other.race_lastpenalty = self;
705 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
709 if(other.race_penalty)
715 if(self.spawnflags & 2)
718 oldmsg = self.message;
721 self.message = oldmsg;
724 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
725 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
726 other.race_respawn_checkpoint = self.race_checkpoint;
727 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
728 other.race_started = 1;
730 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
732 if(!self.race_checkpoint) // start line
734 other.race_laptime = time;
735 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
736 other.race_penalty_accumulator = 0;
737 other.race_lastpenalty = world;
740 if(g_race_qualifying)
741 race_SendNextCheckpoint(other, 0);
743 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
746 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
749 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
750 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
755 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
761 if(self.spawnflags & 4)
762 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
766 void checkpoint_touch()
772 void checkpoint_use()
774 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
781 float race_waypointsprite_visible_for_player(entity e)
783 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
785 else if(e.race_checkpoint == self.owner.race_checkpoint)
792 void trigger_race_checkpoint_verify()
802 qual = g_race_qualifying;
806 self.classname = "player";
810 for(i = 0; i <= race_highest_checkpoint; ++i)
812 self.race_checkpoint = race_NextCheckpoint(i);
814 // race only (middle of the race)
815 g_race_qualifying = 0;
817 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
818 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
823 g_race_qualifying = 1;
824 self.race_place = race_lowest_place_spawn;
825 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
826 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
828 // race only (initial spawn)
829 g_race_qualifying = 0;
830 for(p = 1; p <= race_highest_place_spawn; ++p)
833 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
834 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
839 else if(!defrag_ents)
842 self.race_checkpoint = race_NextCheckpoint(0);
843 g_race_qualifying = 1;
844 self.race_place = race_lowest_place_spawn;
845 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
846 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
850 self.race_checkpoint = race_NextCheckpoint(0);
851 g_race_qualifying = 1;
852 self.race_place = 0; // there's only one spawn on defrag maps
854 // check if a defragcp file already exists, then read it and apply the checkpoint order
859 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
862 while((l = fgets(fh)))
864 len = tokenize_console(l);
866 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
869 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
870 if(argv(0) == cp.targetname)
871 cp.race_checkpoint = stof(argv(1));
877 g_race_qualifying = qual;
879 if(race_timed_checkpoint) {
881 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
882 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
883 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
884 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
886 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
887 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
891 if(defragcpexists != -1){
893 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
894 if(cp.race_checkpoint > largest_cp_id)
895 largest_cp_id = cp.race_checkpoint;
896 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
897 cp.race_checkpoint = largest_cp_id + 1; // finish line
898 race_highest_checkpoint = largest_cp_id + 1;
899 race_timed_checkpoint = largest_cp_id + 1;
901 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
902 cp.race_checkpoint = 255; // finish line
903 race_highest_checkpoint = 255;
904 race_timed_checkpoint = 255;
908 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
911 if(cp.race_checkpoint == 0)
912 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
913 else if(cp.race_checkpoint == race_timed_checkpoint)
914 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
920 entity trigger, targ;
921 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
922 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
923 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
927 setsize(targ, trigger.mins, trigger.maxs);
928 setorigin(targ, trigger.origin);
936 void spawnfunc_trigger_race_checkpoint()
939 if(!g_race && !g_cts)
947 self.use = checkpoint_use;
948 if not(self.spawnflags & 1)
949 self.touch = checkpoint_touch;
951 o = (self.absmin + self.absmax) * 0.5;
952 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
953 waypoint_spawnforitem_force(self, trace_endpos);
954 self.nearestwaypointtimeout = time + 1000000000;
957 self.message = "went backwards";
959 self.message2 = "was pushed backwards by";
960 if (!self.race_penalty_reason)
961 self.race_penalty_reason = "missing a checkpoint";
963 self.race_checkpoint = self.cnt;
965 if(self.race_checkpoint > race_highest_checkpoint)
967 race_highest_checkpoint = self.race_checkpoint;
968 if(self.spawnflags & 8)
969 race_timed_checkpoint = self.race_checkpoint;
971 race_timed_checkpoint = 0;
974 if(!self.race_penalty)
976 if(self.race_checkpoint)
977 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
979 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
982 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
984 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
987 void spawnfunc_target_checkpoint() // defrag entity
990 if(!g_race && !g_cts)
999 self.use = checkpoint_use;
1000 if not(self.spawnflags & 1)
1001 self.touch = checkpoint_touch;
1003 o = (self.absmin + self.absmax) * 0.5;
1004 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
1005 waypoint_spawnforitem_force(self, trace_endpos);
1006 self.nearestwaypointtimeout = time + 1000000000;
1009 self.message = "went backwards";
1011 self.message2 = "was pushed backwards by";
1012 if (!self.race_penalty_reason)
1013 self.race_penalty_reason = "missing a checkpoint";
1015 if(self.classname == "target_startTimer")
1016 self.race_checkpoint = 0;
1018 self.race_checkpoint = -2;
1020 race_timed_checkpoint = 1;
1022 if(self.race_checkpoint == 0)
1023 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
1025 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
1027 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1029 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1032 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1033 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1035 void race_AbandonRaceCheck(entity p)
1037 if(race_completing && !p.race_completed)
1039 p.race_completed = 1;
1040 MAKE_INDEPENDENT_PLAYER(p);
1041 bprint(p.netname, "^7 has abandoned the race.\n");
1042 ClientData_Touch(p);
1046 void race_StartCompleting()
1049 race_completing = 1;
1051 if(p.deadflag != DEAD_NO)
1052 race_AbandonRaceCheck(p);
1055 void race_PreparePlayer()
1057 race_ClearTime(self);
1058 self.race_place = 0;
1059 self.race_started = 0;
1060 self.race_respawn_checkpoint = 0;
1061 self.race_respawn_spotref = world;
1064 void race_RetractPlayer()
1066 if(!g_race && !g_cts)
1068 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1069 race_ClearTime(self);
1070 self.race_checkpoint = self.race_respawn_checkpoint;
1075 if(!g_race && !g_cts)
1078 race_AbandonRaceCheck(self);
1081 void race_PreSpawn()
1083 if(!g_race && !g_cts)
1085 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1086 race_PreparePlayer();
1088 race_RetractPlayer();
1090 race_AbandonRaceCheck(self);
1093 void race_PostSpawn(entity spot)
1095 if(!g_race && !g_cts)
1098 if(spot.target == "")
1099 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1100 race_PreparePlayer();
1102 // if we need to respawn, do it right
1103 self.race_respawn_checkpoint = self.race_checkpoint;
1104 self.race_respawn_spotref = spot;
1106 self.race_place = 0;
1109 void race_PreSpawnObserver()
1111 if(!g_race && !g_cts)
1113 race_PreparePlayer();
1114 self.race_checkpoint = -1;
1117 void spawnfunc_info_player_race (void)
1119 if(!g_race && !g_cts)
1125 spawnfunc_info_player_deathmatch();
1127 if(self.race_place > race_highest_place_spawn)
1128 race_highest_place_spawn = self.race_place;
1129 if(self.race_place < race_lowest_place_spawn)
1130 race_lowest_place_spawn = self.race_place;
1133 void race_ClearRecords()
1138 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1140 race_checkpoint_records[i] = 0;
1141 if(race_checkpoint_recordholders[i])
1142 strunzone(race_checkpoint_recordholders[i]);
1143 race_checkpoint_recordholders[i] = string_null;
1147 FOR_EACH_CLIENT(self)
1150 p = self.race_place;
1151 race_PreparePlayer();
1152 self.race_place = p;
1157 void race_ReadyRestart()
1161 Score_NicePrint(world);
1163 race_ClearRecords();
1164 PlayerScore_Sort(race_place);
1171 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1175 print(e.netname, " = ", ftos(e.race_place), "\n");
1178 if(g_race_qualifying == 2)
1180 g_race_qualifying = 0;
1181 independent_players = 0;
1182 cvar_set("fraglimit", ftos(race_fraglimit));
1183 cvar_set("leadlimit", ftos(race_leadlimit));
1184 cvar_set("timelimit", ftos(race_timelimit));
1189 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1191 if(g_race_qualifying)
1193 pl.race_penalty_accumulator += penalty;
1195 WRITESPECTATABLE_MSG_ONE({
1196 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1197 WriteByte(MSG_ONE, TE_CSQC_RACE);
1198 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1199 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1200 WriteString(MSG_ONE, reason);
1205 pl.race_penalty = time + penalty;
1207 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1208 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1209 WriteByte(MSG_ONE, TE_CSQC_RACE);
1210 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1211 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1212 WriteString(MSG_ONE, reason);
1217 void penalty_touch()
1220 if(other.race_lastpenalty != self)
1222 other.race_lastpenalty = self;
1223 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1229 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1232 void spawnfunc_trigger_race_penalty()
1236 self.use = penalty_use;
1237 if not(self.spawnflags & 1)
1238 self.touch = penalty_touch;
1240 if (!self.race_penalty_reason)
1241 self.race_penalty_reason = "missing a checkpoint";
1242 if (!self.race_penalty)
1243 self.race_penalty = 5;