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) {
225 if (worst_time && t > worst_time)
229 string recorddifference;
231 pos = race_GetPos(t);
234 prevpos = race_CheckName(e.netname);
236 prevpos = race_CheckUID(e.uid, e.netname);
238 if (prevpos && (prevpos < pos || !pos))
240 oldrec = race_GetTime(prevpos);
241 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
242 bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
245 } else if (!pos) { // no ranking, time worse than the worst ranked
246 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
247 bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
251 // move other rankings out of the way
253 if (prevpos) { // player improved his existing record
255 oldrec = grecordtime[pos-1];
257 oldrec = grecordtime[pos-1];
258 for (i=prevpos-1;i>pos-1;--i) {
259 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
260 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
262 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
264 grecordtime[i] = grecordtime[i-1];
266 if (grecordholder[i])
267 strunzone(grecordholder[i]);
268 grecordholder[i] = strzone(grecordholder[i-1]);
271 strunzone(grecorduid[i]);
272 grecorduid[i] = strzone(grecorduid[i-1]);
275 } else { // player has no ranked record yet
276 oldrec = grecordtime[pos-1];
277 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
278 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
279 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
281 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
283 grecordtime[i] = grecordtime[i-1];
285 if (grecordholder[i])
286 strunzone(grecordholder[i]);
287 grecordholder[i] = strzone(grecordholder[i-1]);
290 strunzone(grecorduid[i]);
291 grecorduid[i] = strzone(grecorduid[i-1]);
298 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
299 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
301 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), e.uid);
306 if (grecordholder[0])
307 strunzone(grecordholder[0]);
308 grecordholder[0] = strzone(e.netname);
311 strunzone(grecorduid[0]);
312 grecorduid[0] = strzone(e.uid);
314 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
315 race_send_recordtime(MSG_ALL);
317 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
318 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
320 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(pos-1)), e.uid);
323 grecordtime[pos-1] = t;
325 if (grecordholder[pos-1])
326 strunzone(grecordholder[pos-1]);
327 grecordholder[pos-1] = strzone(e.netname);
329 if (grecorduid[pos-1])
330 strunzone(grecorduid[pos-1]);
331 grecorduid[pos-1] = strzone(e.uid);
335 if (pos == RANKINGS_CNT)
338 race_SendRankings(pos, prevpos, 0, MSG_ALL);
340 strunzone(rankings_reply);
341 rankings_reply = strzone(getrankings());
344 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
345 bprint(e.netname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
346 race_SendStatus(2, e); // "new record"
348 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
349 bprint(e.netname, "^1 broke ", grecordholder[pos], "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
350 race_SendStatus(2, e); // "new record"
354 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
355 bprint(e.netname, "^3 improved their ", race_PlaceName(pos), " ^3place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
356 race_SendStatus(0, e); // "new time"
357 } else if (oldrec == 0) {
358 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
359 race_SendStatus(1, e); // "new rank"
361 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
362 bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
363 race_SendStatus(1, e); // "new rank"
368 void race_DeleteTime(float pos) {
371 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
373 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
374 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
376 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), grecorduid[1]);
378 grecordtime[0] = grecordtime[1];
379 if (grecordholder[i])
380 strunzone(grecordholder[0]);
381 grecordholder[0] = strzone(grecordholder[1]);
385 strunzone(grecorduid[0]);
386 grecorduid[0] = strzone(grecorduid[1]);
389 else if (i == RANKINGS_CNT-1) {
390 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
391 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
393 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), string_null);
396 if (grecordholder[i])
397 strunzone(grecordholder[i]);
398 grecordholder[i] = string_null;
402 strunzone(grecorduid[i]);
403 grecorduid[i] = string_null;
407 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
408 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
410 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i+1]);
412 grecordtime[i] = grecordtime[i+1];
413 if (grecordholder[i])
414 strunzone(grecordholder[i]);
415 grecordholder[i] = strzone(grecordholder[i+1]);
419 strunzone(grecorduid[i]);
420 grecorduid[i] = strzone(grecorduid[i+1]);
425 race_SendRankings(pos, 0, 1, MSG_ALL);
427 race_send_recordtime(MSG_ALL);
430 strunzone(rankings_reply);
431 rankings_reply = strzone(getrankings());
436 void race_SendTime(entity e, float cp, float t, float tvalid)
441 if(g_race_qualifying)
442 t += e.race_penalty_accumulator;
444 t = TIME_ENCODE(t); // make integer
445 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
448 if(cp == race_timed_checkpoint) // finish line
449 if not(e.race_completed)
452 if(g_race_qualifying)
454 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
456 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
460 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
461 snew = TIME_ENCODE(time - game_starttime);
462 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
463 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
465 if(cvar("fraglimit"))
466 if(l >= cvar("fraglimit"))
467 race_StartCompleting();
471 e.race_completed = 1;
472 MAKE_INDEPENDENT_PLAYER(e);
473 bprint(e.netname, "^7 has finished the race.\n");
481 if(g_race_qualifying)
485 recordtime = race_checkpoint_records[cp];
486 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
487 if(recordholder == e.netname)
491 if(t < worst_time || worst_time == 0)
493 if(cp == race_timed_checkpoint)
498 if(t < recordtime || recordtime == 0)
500 race_checkpoint_records[cp] = t;
501 if(race_checkpoint_recordholders[cp])
502 strunzone(race_checkpoint_recordholders[cp]);
503 race_checkpoint_recordholders[cp] = strzone(e.netname);
504 if(g_race_qualifying)
506 FOR_EACH_REALPLAYER(p)
507 if(p.race_checkpoint == cp)
508 race_SendNextCheckpoint(p, 0);
522 if(g_race_qualifying)
524 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
525 WriteByte(MSG_ONE, SVC_TEMPENTITY);
526 WriteByte(MSG_ONE, TE_CSQC_RACE);
527 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
528 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
529 WriteInt24_t(MSG_ONE, t); // time to that intermediate
530 WriteInt24_t(MSG_ONE, recordtime); // previously best time
531 WriteString(MSG_ONE, recordholder); // record holder
535 else // RACE! Not Qualifying
537 float lself, lother, othtime;
539 oth = race_checkpoint_lastplayers[cp];
542 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
543 lother = race_checkpoint_lastlaps[cp];
544 othtime = race_checkpoint_lasttimes[cp];
547 lself = lother = othtime = 0;
550 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
551 WriteByte(MSG_ONE, SVC_TEMPENTITY);
552 WriteByte(MSG_ONE, TE_CSQC_RACE);
553 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
554 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
557 WriteInt24_t(MSG_ONE, 0);
558 WriteByte(MSG_ONE, 0);
559 WriteString(MSG_ONE, "");
563 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
564 WriteByte(MSG_ONE, lself - lother);
565 WriteString(MSG_ONE, oth.netname); // record holder
569 race_checkpoint_lastplayers[cp] = e;
570 race_checkpoint_lasttimes[cp] = time;
571 race_checkpoint_lastlaps[cp] = lself;
574 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
575 WriteByte(MSG_ONE, SVC_TEMPENTITY);
576 WriteByte(MSG_ONE, TE_CSQC_RACE);
577 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
578 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
581 WriteInt24_t(MSG_ONE, 0);
582 WriteByte(MSG_ONE, 0);
583 WriteString(MSG_ONE, "");
587 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
588 WriteByte(MSG_ONE, lother - lself);
589 WriteString(MSG_ONE, e.netname); // record holder
595 void race_ClearTime(entity e)
597 e.race_checkpoint = 0;
599 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
600 e.race_penalty_accumulator = 0;
601 e.race_lastpenalty = world;
604 WRITESPECTATABLE_MSG_ONE({
605 WriteByte(MSG_ONE, SVC_TEMPENTITY);
606 WriteByte(MSG_ONE, TE_CSQC_RACE);
607 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
611 void dumpsurface(entity e)
615 print("Surfaces of ", etos(e), ":\n");
617 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
621 n = getsurfacenumpoints(e, si);
624 print(" Surface ", ftos(si), ":\n");
625 norm = getsurfacenormal(e, si);
626 print(" Normal = ", vtos(norm), "\n");
627 for(ni = 0; ni < n; ++ni)
629 vec = getsurfacepoint(e, si, ni);
630 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
635 void checkpoint_passed()
640 if(other.classname == "porto")
642 // do not allow portalling through checkpoints
643 trace_plane_normal = normalize(-1 * other.velocity);
652 if not((self.spawnflags & 2) && (other.classname == "player"))
655 oldmsg = self.message;
658 self.message = oldmsg;
661 if(other.classname != "player")
665 * Remove unauthorized equipment
667 Portal_ClearAll(other);
669 other.porto_forbidden = 2; // decreased by 1 each StartFrame
672 if(self.race_checkpoint == -2)
674 self.race_checkpoint = other.race_checkpoint;
679 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
681 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
683 largest_cp_id = cp.race_checkpoint;
684 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
685 cp.race_checkpoint = largest_cp_id + 1; // finish line
686 race_highest_checkpoint = largest_cp_id + 1;
687 race_timed_checkpoint = largest_cp_id + 1;
689 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
690 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
696 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
697 cp.race_checkpoint = 1;
698 race_highest_checkpoint = 1;
699 race_timed_checkpoint = 1;
703 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
705 if(self.race_penalty)
707 if(other.race_lastpenalty != self)
709 other.race_lastpenalty = self;
710 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
714 if(other.race_penalty)
720 if(self.spawnflags & 2)
723 oldmsg = self.message;
726 self.message = oldmsg;
729 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
730 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
731 other.race_respawn_checkpoint = self.race_checkpoint;
732 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
733 other.race_started = 1;
735 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
737 if(!self.race_checkpoint) // start line
739 other.race_laptime = time;
740 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
741 other.race_penalty_accumulator = 0;
742 other.race_lastpenalty = world;
745 if(g_race_qualifying)
746 race_SendNextCheckpoint(other, 0);
748 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
751 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
754 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
755 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
760 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
766 if(self.spawnflags & 4)
767 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
771 void checkpoint_touch()
777 void checkpoint_use()
779 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
786 float race_waypointsprite_visible_for_player(entity e)
788 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
790 else if(e.race_checkpoint == self.owner.race_checkpoint)
797 void trigger_race_checkpoint_verify()
807 qual = g_race_qualifying;
811 self.classname = "player";
815 for(i = 0; i <= race_highest_checkpoint; ++i)
817 self.race_checkpoint = race_NextCheckpoint(i);
819 // race only (middle of the race)
820 g_race_qualifying = 0;
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 respawning in race) - bailing out"));
828 g_race_qualifying = 1;
829 self.race_place = race_lowest_place_spawn;
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 qualifying) - bailing out"));
833 // race only (initial spawn)
834 g_race_qualifying = 0;
835 for(p = 1; p <= race_highest_place_spawn; ++p)
838 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
839 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
844 else if(!defrag_ents)
847 self.race_checkpoint = race_NextCheckpoint(0);
848 g_race_qualifying = 1;
849 self.race_place = race_lowest_place_spawn;
850 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
851 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
855 self.race_checkpoint = race_NextCheckpoint(0);
856 g_race_qualifying = 1;
857 self.race_place = 0; // there's only one spawn on defrag maps
859 // check if a defragcp file already exists, then read it and apply the checkpoint order
864 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
867 while((l = fgets(fh)))
869 len = tokenize_console(l);
871 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
874 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
875 if(argv(0) == cp.targetname)
876 cp.race_checkpoint = stof(argv(1));
882 g_race_qualifying = qual;
884 if(race_timed_checkpoint) {
886 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
887 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
888 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
889 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
891 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
892 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
896 if(defragcpexists != -1){
898 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
899 if(cp.race_checkpoint > largest_cp_id)
900 largest_cp_id = cp.race_checkpoint;
901 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
902 cp.race_checkpoint = largest_cp_id + 1; // finish line
903 race_highest_checkpoint = largest_cp_id + 1;
904 race_timed_checkpoint = largest_cp_id + 1;
906 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
907 cp.race_checkpoint = 255; // finish line
908 race_highest_checkpoint = 255;
909 race_timed_checkpoint = 255;
913 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
916 if(cp.race_checkpoint == 0)
917 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
918 else if(cp.race_checkpoint == race_timed_checkpoint)
919 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
925 entity trigger, targ;
926 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
927 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
928 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
932 setsize(targ, trigger.mins, trigger.maxs);
933 setorigin(targ, trigger.origin);
941 void spawnfunc_trigger_race_checkpoint()
944 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 self.race_checkpoint = self.cnt;
970 if(self.race_checkpoint > race_highest_checkpoint)
972 race_highest_checkpoint = self.race_checkpoint;
973 if(self.spawnflags & 8)
974 race_timed_checkpoint = self.race_checkpoint;
976 race_timed_checkpoint = 0;
979 if(!self.race_penalty)
981 if(self.race_checkpoint)
982 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
984 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
987 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
989 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
992 void spawnfunc_target_checkpoint() // defrag entity
995 if(!g_race && !g_cts)
1004 self.use = checkpoint_use;
1005 if not(self.spawnflags & 1)
1006 self.touch = checkpoint_touch;
1008 o = (self.absmin + self.absmax) * 0.5;
1009 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
1010 waypoint_spawnforitem_force(self, trace_endpos);
1011 self.nearestwaypointtimeout = time + 1000000000;
1014 self.message = "went backwards";
1016 self.message2 = "was pushed backwards by";
1017 if (!self.race_penalty_reason)
1018 self.race_penalty_reason = "missing a checkpoint";
1020 if(self.classname == "target_startTimer")
1021 self.race_checkpoint = 0;
1023 self.race_checkpoint = -2;
1025 race_timed_checkpoint = 1;
1027 if(self.race_checkpoint == 0)
1028 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
1030 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
1032 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1034 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1037 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1038 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1040 void race_AbandonRaceCheck(entity p)
1042 if(race_completing && !p.race_completed)
1044 p.race_completed = 1;
1045 MAKE_INDEPENDENT_PLAYER(p);
1046 bprint(p.netname, "^7 has abandoned the race.\n");
1047 ClientData_Touch(p);
1051 void race_StartCompleting()
1054 race_completing = 1;
1056 if(p.deadflag != DEAD_NO)
1057 race_AbandonRaceCheck(p);
1060 void race_PreparePlayer()
1062 race_ClearTime(self);
1063 self.race_place = 0;
1064 self.race_started = 0;
1065 self.race_respawn_checkpoint = 0;
1066 self.race_respawn_spotref = world;
1069 void race_RetractPlayer()
1071 if(!g_race && !g_cts)
1073 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1074 race_ClearTime(self);
1075 self.race_checkpoint = self.race_respawn_checkpoint;
1080 if(!g_race && !g_cts)
1083 race_AbandonRaceCheck(self);
1086 void race_PreSpawn()
1088 if(!g_race && !g_cts)
1090 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1091 race_PreparePlayer();
1093 race_RetractPlayer();
1095 race_AbandonRaceCheck(self);
1098 void race_PostSpawn(entity spot)
1100 if(!g_race && !g_cts)
1103 if(spot.target == "")
1104 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1105 race_PreparePlayer();
1107 // if we need to respawn, do it right
1108 self.race_respawn_checkpoint = self.race_checkpoint;
1109 self.race_respawn_spotref = spot;
1111 self.race_place = 0;
1114 void race_PreSpawnObserver()
1116 if(!g_race && !g_cts)
1118 race_PreparePlayer();
1119 self.race_checkpoint = -1;
1122 void spawnfunc_info_player_race (void)
1124 if(!g_race && !g_cts)
1130 spawnfunc_info_player_deathmatch();
1132 if(self.race_place > race_highest_place_spawn)
1133 race_highest_place_spawn = self.race_place;
1134 if(self.race_place < race_lowest_place_spawn)
1135 race_lowest_place_spawn = self.race_place;
1138 void race_ClearRecords()
1143 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1145 race_checkpoint_records[i] = 0;
1146 if(race_checkpoint_recordholders[i])
1147 strunzone(race_checkpoint_recordholders[i]);
1148 race_checkpoint_recordholders[i] = string_null;
1152 FOR_EACH_CLIENT(self)
1155 p = self.race_place;
1156 race_PreparePlayer();
1157 self.race_place = p;
1162 void race_ReadyRestart()
1166 Score_NicePrint(world);
1168 race_ClearRecords();
1169 PlayerScore_Sort(race_place);
1176 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1180 print(e.netname, " = ", ftos(e.race_place), "\n");
1183 if(g_race_qualifying == 2)
1185 g_race_qualifying = 0;
1186 independent_players = 0;
1187 cvar_set("fraglimit", ftos(race_fraglimit));
1188 cvar_set("leadlimit", ftos(race_leadlimit));
1189 cvar_set("timelimit", ftos(race_timelimit));
1194 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1196 if(g_race_qualifying)
1198 pl.race_penalty_accumulator += penalty;
1200 WRITESPECTATABLE_MSG_ONE({
1201 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1202 WriteByte(MSG_ONE, TE_CSQC_RACE);
1203 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1204 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1205 WriteString(MSG_ONE, reason);
1210 pl.race_penalty = time + penalty;
1212 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1213 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1214 WriteByte(MSG_ONE, TE_CSQC_RACE);
1215 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1216 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1217 WriteString(MSG_ONE, reason);
1222 void penalty_touch()
1225 if(other.race_lastpenalty != self)
1227 other.race_lastpenalty = self;
1228 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1234 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1237 void spawnfunc_trigger_race_penalty()
1241 self.use = penalty_use;
1242 if not(self.spawnflags & 1)
1243 self.touch = penalty_touch;
1245 if (!self.race_penalty_reason)
1246 self.race_penalty_reason = "missing a checkpoint";
1247 if (!self.race_penalty)
1248 self.race_penalty = 5;