3 #include <common/deathtypes/all.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <common/gamemodes/rules.qh>
6 #include <common/mapobjects/subs.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
9 #include <common/net_linked.qh>
10 #include <common/notifications/all.qh>
11 #include <common/state.qh>
12 #include <common/stats.qh>
13 #include <common/vehicles/sv_vehicles.qh>
14 #include <common/weapons/_all.qh>
15 #include <common/weapons/weapon/porto.qh>
16 #include <lib/warpzone/common.qh>
17 #include <lib/warpzone/util_server.qh>
18 #include <server/bot/api.qh>
19 #include <server/cheats.qh>
20 #include <server/client.qh>
21 #include <server/command/getreplies.qh>
22 #include <server/damage.qh>
23 #include <server/gamelog.qh>
24 #include <server/intermission.qh>
25 #include <server/main.qh>
26 #include <server/mutators/_mod.qh>
27 #include <server/portals.qh>
28 #include <server/scores.qh>
29 #include <server/spawnpoints.qh>
30 #include <server/weapons/common.qh>
31 #include <server/world.qh>
33 .string stored_netname; // TODO: store this information independently of race-based gamemodes
35 string uid2name(string myuid)
37 string s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
39 // FIXME remove this later after 0.6 release
40 // convert old style broken records to correct style
43 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
46 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
47 db_remove(ServerProgsDB, strcat("uid2name", myuid));
52 s = "^1Unregistered Player";
56 void write_recordmarker(entity pl, float tstart, float dt)
58 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
60 // also write a marker into demo files for demotc-race-record-extractor to find
63 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
64 " ", ftos(tstart), " ", ftos(dt), "\n"));
67 IntrusiveList g_race_targets;
68 IntrusiveList g_racecheckpoints;
71 g_race_targets = IL_NEW();
72 g_racecheckpoints = IL_NEW();
75 void race_InitSpectator()
78 if(msg_entity.enemy.race_laptime)
79 race_SendNextCheckpoint(msg_entity.enemy, 1);
82 float race_readTime(string map, float pos)
84 return stof(db_get(ServerProgsDB, strcat(map, record_type, "time", ftos(pos))));
87 string race_readUID(string map, float pos)
89 return db_get(ServerProgsDB, strcat(map, record_type, "crypto_idfp", ftos(pos)));
92 float race_readPos(string map, float t)
94 for(int i = 1; i <= RANKINGS_CNT; ++i)
96 int mytime = race_readTime(map, i);
97 if(!mytime || mytime > t)
101 return 0; // pos is zero if unranked
104 void race_writeTime(string map, float t, string myuid)
107 newpos = race_readPos(map, t);
109 float i, prevpos = 0;
110 for(i = 1; i <= RANKINGS_CNT; ++i)
112 if(race_readUID(map, i) == myuid)
117 // player improved his existing record, only have to iterate on ranks between new and old recs
118 for (i = prevpos; i > newpos; --i)
120 db_put(ServerProgsDB, strcat(map, record_type, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
121 db_put(ServerProgsDB, strcat(map, record_type, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
126 // player has no ranked record yet
127 for (i = RANKINGS_CNT; i > newpos; --i)
129 float other_time = race_readTime(map, i - 1);
131 db_put(ServerProgsDB, strcat(map, record_type, "time", ftos(i)), ftos(other_time));
132 db_put(ServerProgsDB, strcat(map, record_type, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
137 // store new time itself
138 db_put(ServerProgsDB, strcat(map, record_type, "time", ftos(newpos)), ftos(t));
139 db_put(ServerProgsDB, strcat(map, record_type, "crypto_idfp", ftos(newpos)), myuid);
142 string race_readName(string map, float pos)
144 return uid2name(db_get(ServerProgsDB, strcat(map, record_type, "crypto_idfp", ftos(pos))));
147 void race_checkAndWriteName(entity player)
149 if(CS_CVAR(player).cvar_cl_allow_uidtracking == 1 && CS_CVAR(player).cvar_cl_allow_uid2name == 1)
151 if (!player.stored_netname)
152 player.stored_netname = strzone(uid2name(player.crypto_idfp));
153 if(player.stored_netname != player.netname)
155 db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
156 strcpy(player.stored_netname, player.netname);
162 const float MAX_CHECKPOINTS = 255;
165 .float race_penalty_accumulator;
166 .string race_penalty_reason;
167 .float race_checkpoint; // player: next checkpoint that has to be reached
168 .entity race_lastpenalty;
172 float race_checkpoint_records[MAX_CHECKPOINTS];
173 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
174 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
175 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
176 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
178 .float race_checkpoint_record[MAX_CHECKPOINTS];
180 float race_highest_checkpoint;
181 float race_timed_checkpoint;
184 float defragcpexists;
186 float race_NextCheckpoint(float f)
188 if(f >= race_highest_checkpoint)
194 float race_PreviousCheckpoint(float f)
199 return race_highest_checkpoint;
205 // 0 = common start/finish
208 float race_CheckpointNetworkID(float f)
210 if(race_timed_checkpoint)
214 else if(f == race_timed_checkpoint)
215 return 255; // finish
220 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
225 int cp = e.race_checkpoint;
226 float recordtime = race_checkpoint_records[cp];
227 float myrecordtime = e.race_checkpoint_record[cp];
228 string recordholder = race_checkpoint_recordholders[cp];
229 if(recordholder == e.netname)
232 if(!IS_REAL_CLIENT(e))
237 WRITESPECTATABLE_MSG_ONE(msg_entity, {
238 WriteHeader(MSG_ONE, TE_CSQC_RACE);
241 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
242 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
243 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
246 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
247 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
248 WriteInt24_t(MSG_ONE, recordtime);
250 WriteInt24_t(MSG_ONE, myrecordtime);
251 WriteString(MSG_ONE, recordholder);
255 void race_send_recordtime(float msg)
257 // send the server best time
258 WriteHeader(msg, TE_CSQC_RACE);
259 WriteByte(msg, RACE_NET_SERVER_RECORD);
260 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
263 void race_send_speedaward(float msg)
265 // send the best speed of the round
266 WriteHeader(msg, TE_CSQC_RACE);
267 WriteByte(msg, RACE_NET_SPEED_AWARD);
268 WriteInt24_t(msg, floor(speedaward_speed+0.5));
269 WriteString(msg, speedaward_holder);
272 void race_send_speedaward_alltimebest(float msg)
274 // send the best speed
275 WriteHeader(msg, TE_CSQC_RACE);
276 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
277 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
278 WriteString(msg, speedaward_alltimebest_holder);
281 void race_send_rankings_cnt(float msg)
283 WriteHeader(msg, TE_CSQC_RACE);
284 WriteByte(msg, RACE_NET_RANKINGS_CNT);
285 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
289 void race_SendRanking(float pos, float prevpos, float del, float msg)
291 WriteHeader(msg, TE_CSQC_RACE);
292 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
293 WriteShort(msg, pos);
294 WriteShort(msg, prevpos);
295 WriteShort(msg, del);
296 WriteString(msg, race_readName(GetMapname(), pos));
297 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
300 void race_SpeedAwardFrame(entity player)
302 if (IS_OBSERVER(player))
305 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
307 speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
308 speedaward_holder = player.netname;
309 speedaward_uid = player.crypto_idfp;
310 speedaward_lastupdate = time;
312 if (speedaward_speed > speedaward_lastsent && (time - speedaward_lastupdate > 1 || intermission_running))
314 race_send_speedaward(MSG_ALL);
315 speedaward_lastsent = speedaward_speed;
316 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
318 speedaward_alltimebest = speedaward_speed;
319 speedaward_alltimebest_holder = speedaward_holder;
320 speedaward_alltimebest_uid = speedaward_uid;
321 db_put(ServerProgsDB, strcat(GetMapname(), record_type, "speed/speed"), ftos(speedaward_alltimebest));
322 db_put(ServerProgsDB, strcat(GetMapname(), record_type, "speed/crypto_idfp"), speedaward_alltimebest_uid);
323 race_send_speedaward_alltimebest(MSG_ALL);
328 void race_SendAll(entity player, bool only_rankings)
330 if(!IS_REAL_CLIENT(player))
336 race_send_recordtime(MSG_ONE);
337 race_send_speedaward(MSG_ONE);
339 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), record_type, "speed/speed")));
340 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), record_type, "speed/crypto_idfp")));
341 race_send_speedaward_alltimebest(MSG_ONE);
344 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
345 race_send_rankings_cnt(MSG_ONE);
346 for (int i = 1; i <= m; ++i)
347 race_SendRanking(i, 0, 0, MSG_ONE);
350 void race_SendStatus(float id, entity e)
352 if(!IS_REAL_CLIENT(e))
361 WRITESPECTATABLE_MSG_ONE(msg_entity, {
362 WriteHeader(msg, TE_CSQC_RACE);
363 WriteByte(msg, RACE_NET_SERVER_STATUS);
365 WriteString(msg, e.netname);
369 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
371 // netname only used TEMPORARILY for printing
372 int newpos = race_readPos(map, t);
374 int player_prevpos = 0;
375 for(int i = 1; i <= RANKINGS_CNT; ++i)
377 if(race_readUID(map, i) == myuid)
382 string oldrec_holder;
383 if (player_prevpos && (player_prevpos < newpos || !newpos))
385 oldrec = race_readTime(GetMapname(), player_prevpos);
386 race_SendStatus(0, e); // "fail"
388 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
393 // no ranking, time worse than the worst ranked
394 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
395 race_SendStatus(0, e); // "fail"
397 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
401 // if we didn't hit a return yet, we have a new record!
403 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
407 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
411 if(uid2name(myuid) == "^1Unregistered Player")
414 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
418 oldrec = race_readTime(GetMapname(), newpos);
419 oldrec_holder = race_readName(GetMapname(), newpos);
422 race_writeTime(GetMapname(), t, myuid);
424 if (newpos == 1 && showmessage)
426 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
427 race_send_recordtime(MSG_ALL);
430 race_SendRanking(newpos, player_prevpos, 0, MSG_ALL);
431 strcpy(rankings_reply, getrankings());
433 if(newpos == player_prevpos)
436 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
437 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
438 else { race_SendStatus(1, e); } // "new time"
443 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
444 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
445 else { race_SendStatus(2, e); } // "new rank"
450 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
451 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
452 else { race_SendStatus(2, e); } // "new rank"
456 void race_deleteTime(string map, float pos)
458 for(int i = pos; i <= RANKINGS_CNT; ++i)
460 string therank = ftos(i);
461 if (i == RANKINGS_CNT)
463 db_remove(ServerProgsDB, strcat(map, record_type, "time", therank));
464 db_remove(ServerProgsDB, strcat(map, record_type, "crypto_idfp", therank));
468 db_put(ServerProgsDB, strcat(map, record_type, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
469 db_put(ServerProgsDB, strcat(map, record_type, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
473 race_SendRanking(pos, 0, 1, MSG_ALL);
475 race_send_recordtime(MSG_ALL);
477 strcpy(rankings_reply, getrankings());
480 void race_SendTime(entity e, float cp, float t, float tvalid)
484 if(g_race_qualifying)
485 t += e.race_penalty_accumulator;
487 t = TIME_ENCODE(t); // make integer
490 if(cp == race_timed_checkpoint) // finish line
491 if (!CS(e).race_completed)
494 if(g_race_qualifying)
496 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
498 GameRules_scoring_add(e, RACE_FASTEST, t - s);
502 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
504 GameRules_scoring_add(e, RACE_FASTEST, t - s);
506 s = GameRules_scoring_add(e, RACE_TIME, 0);
507 snew = TIME_ENCODE(time - game_starttime);
508 GameRules_scoring_add(e, RACE_TIME, snew - s);
509 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
511 if(autocvar_fraglimit)
512 if(l >= autocvar_fraglimit)
513 race_StartCompleting();
517 CS(e).race_completed = 1;
518 MAKE_INDEPENDENT_PLAYER(e);
519 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
525 if(g_race_qualifying)
532 recordtime = race_checkpoint_records[cp];
533 float myrecordtime = e.race_checkpoint_record[cp];
534 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
535 if(recordholder == e.netname)
540 if(cp == race_timed_checkpoint)
542 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
543 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
545 if(t < myrecordtime || myrecordtime == 0)
546 e.race_checkpoint_record[cp] = t; // resending done below
548 if(t < recordtime || recordtime == 0)
550 race_checkpoint_records[cp] = t;
551 strcpy(race_checkpoint_recordholders[cp], e.netname);
552 if(g_race_qualifying)
553 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
566 if(IS_REAL_CLIENT(e))
568 if(g_race_qualifying)
570 FOREACH_CLIENT(IS_REAL_CLIENT(it),
572 if(it == e || (IS_SPEC(it) && it.enemy == e))
575 WriteHeader(MSG_ONE, TE_CSQC_RACE);
576 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
577 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
578 WriteInt24_t(MSG_ONE, t); // time to that intermediate
579 WriteInt24_t(MSG_ONE, recordtime); // previously best time
580 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
581 WriteString(MSG_ONE, recordholder); // record holder
587 else // RACE! Not Qualifying
589 float mylaps, lother, othtime;
590 entity oth = race_checkpoint_lastplayers[cp];
593 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
594 lother = race_checkpoint_lastlaps[cp];
595 othtime = race_checkpoint_lasttimes[cp];
598 mylaps = lother = othtime = 0;
600 if(IS_REAL_CLIENT(e))
603 WRITESPECTATABLE_MSG_ONE(msg_entity, {
604 WriteHeader(MSG_ONE, TE_CSQC_RACE);
605 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
606 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
609 WriteInt24_t(MSG_ONE, 0);
610 WriteByte(MSG_ONE, 0);
611 WriteByte(MSG_ONE, 0);
615 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
616 WriteByte(MSG_ONE, mylaps - lother);
617 WriteByte(MSG_ONE, etof(oth)); // record holder
622 race_checkpoint_lastplayers[cp] = e;
623 race_checkpoint_lasttimes[cp] = time;
624 race_checkpoint_lastlaps[cp] = mylaps;
626 if(IS_REAL_CLIENT(oth))
629 WRITESPECTATABLE_MSG_ONE(msg_entity, {
630 WriteHeader(MSG_ONE, TE_CSQC_RACE);
631 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
632 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
635 WriteInt24_t(MSG_ONE, 0);
636 WriteByte(MSG_ONE, 0);
637 WriteByte(MSG_ONE, 0);
641 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
642 WriteByte(MSG_ONE, lother - mylaps);
643 WriteByte(MSG_ONE, etof(e) - 1); // record holder
650 void race_ClearTime(entity e)
652 e.race_checkpoint = 0;
654 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
655 e.race_penalty_accumulator = 0;
656 e.race_lastpenalty = NULL;
658 if(!IS_REAL_CLIENT(e))
662 WRITESPECTATABLE_MSG_ONE(msg_entity, {
663 WriteHeader(MSG_ONE, TE_CSQC_RACE);
664 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
668 void checkpoint_passed(entity this, entity player)
670 if(IS_VEHICLE(player) && player.owner)
671 player = player.owner;
673 if(player.personal && autocvar_g_allow_checkpoints)
674 return; // practice mode!
676 if(player.classname == "porto")
678 // do not allow portalling through checkpoints
679 trace_plane_normal = normalize(-1 * player.velocity);
680 W_Porto_Fail(player, 0);
684 string oldmsg; // used twice
689 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
691 oldmsg = this.message;
693 SUB_UseTargets(this, player, player);
694 this.message = oldmsg;
697 if (!IS_PLAYER(player))
701 * Remove unauthorized equipment
703 Portal_ClearAll(player);
705 player.porto_forbidden = 2; // decreased by 1 each StartFrame
709 if(this.race_checkpoint == -2)
711 this.race_checkpoint = player.race_checkpoint;
714 int cp_amount = 0, largest_cp_id = 0;
715 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
718 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
722 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
724 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
729 largest_cp_id = it.race_checkpoint;
730 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
732 it.race_checkpoint = largest_cp_id + 1; // finish line
734 race_highest_checkpoint = largest_cp_id + 1;
735 race_timed_checkpoint = largest_cp_id + 1;
741 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
743 it.race_checkpoint = 1;
745 race_highest_checkpoint = 1;
746 race_timed_checkpoint = 1;
750 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
752 if(this.race_penalty)
754 if(player.race_lastpenalty != this)
756 player.race_lastpenalty = this;
757 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
761 if(player.race_penalty)
767 if(this.spawnflags & 2)
769 oldmsg = this.message;
771 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
772 this.message = oldmsg;
775 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
776 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
777 player.race_respawn_checkpoint = this.race_checkpoint;
778 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
779 player.race_started = 1;
781 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
783 if(!this.race_checkpoint) // start line
785 player.race_laptime = time;
786 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
787 player.race_penalty_accumulator = 0;
788 player.race_lastpenalty = NULL;
791 if(g_race_qualifying)
792 race_SendNextCheckpoint(player, 0);
794 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
797 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
800 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
802 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
808 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
814 if(this.spawnflags & 4)
815 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
819 void checkpoint_touch(entity this, entity toucher)
821 EXACTTRIGGER_TOUCH(this, toucher);
822 checkpoint_passed(this, toucher);
825 void checkpoint_use(entity this, entity actor, entity trigger)
827 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
830 checkpoint_passed(this, actor);
833 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
835 entity own = this.owner;
837 own = this.realowner; // target support
839 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
841 else if(view.race_checkpoint == own.race_checkpoint)
847 void trigger_race_checkpoint_verify(entity this)
849 static bool have_verified;
850 if (have_verified) return;
851 have_verified = true;
853 bool qual = g_race_qualifying;
855 int pl_race_checkpoint = 0;
856 int pl_race_place = 0;
859 for (int i = 0; i <= race_highest_checkpoint; ++i) {
860 pl_race_checkpoint = race_NextCheckpoint(i);
862 // race only (middle of the race)
863 g_race_qualifying = false;
865 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
866 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
871 g_race_qualifying = 1;
872 pl_race_place = race_lowest_place_spawn;
873 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
874 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
877 // race only (initial spawn)
878 g_race_qualifying = 0;
879 for (int p = 1; p <= race_highest_place_spawn; ++p) {
881 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
882 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
887 } else if (!defrag_ents) {
889 pl_race_checkpoint = race_NextCheckpoint(0);
890 g_race_qualifying = 1;
891 pl_race_place = race_lowest_place_spawn;
892 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
893 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
896 pl_race_checkpoint = race_NextCheckpoint(0);
897 g_race_qualifying = 1;
898 pl_race_place = 0; // there's only one spawn on defrag maps
900 // check if a defragcp file already exists, then read it and apply the checkpoint order
905 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
907 while ((l = fgets(fh))) {
908 len = tokenize_console(l);
910 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
913 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
914 if (argv(0) == cp.targetname) {
915 cp.race_checkpoint = stof(argv(1));
923 g_race_qualifying = qual;
925 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
927 if(it.targetname == "" || !it.targetname) // somehow this is a case...
930 FOREACH_ENTITY_STRING(target, cpt.targetname,
932 vector org = (it.absmin + it.absmax) * 0.5;
933 if(cpt.race_checkpoint == 0)
934 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
936 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
938 it.sprite.realowner = cpt;
939 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
943 if (race_timed_checkpoint) {
945 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
948 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
949 if(it.targetname == "" || !it.targetname) // somehow this is a case...
951 FOREACH_ENTITY_STRING(target, cpt.targetname, {
953 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
956 if(it.classname == "target_checkpoint") {
957 if(it.race_checkpoint == -2)
958 defragcpexists = -1; // 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
961 if (defragcpexists != -1) {
962 float largest_cp_id = 0;
963 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
964 if (cp.race_checkpoint > largest_cp_id) {
965 largest_cp_id = cp.race_checkpoint;
968 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
969 cp.race_checkpoint = largest_cp_id + 1; // finish line
971 race_highest_checkpoint = largest_cp_id + 1;
972 race_timed_checkpoint = largest_cp_id + 1;
974 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
975 cp.race_checkpoint = 255; // finish line
977 race_highest_checkpoint = 255;
978 race_timed_checkpoint = 255;
981 IL_EACH(g_racecheckpoints, it.sprite,
983 if (it.race_checkpoint == 0) {
984 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
985 } else if (it.race_checkpoint == race_timed_checkpoint) {
986 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
993 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
994 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
995 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
1001 // These just make the game crash on some maps with oddly shaped triggers.
1002 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
1003 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
1004 // (happened on item triggers too)
1009 //setsize(targ, trigger.mins, trigger.maxs);
1010 //setorigin(targ, trigger.origin);
1018 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
1020 if(g_race_qualifying)
1023 if(this.race_checkpoint != 0)
1025 if(spot.race_place != race_lowest_place_spawn)
1030 if(this.race_checkpoint != player.race_respawn_checkpoint)
1032 // try reusing the previous spawn
1033 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
1034 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
1035 if(this.race_checkpoint == 0)
1037 int pl = player.race_place;
1038 if(pl > race_highest_place_spawn)
1040 if(pl == 0 && !player.race_started)
1041 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
1042 if(spot.race_place != pl)
1049 spawnfunc(trigger_race_checkpoint)
1052 if(!g_race && !g_cts) { delete(this); return; }
1056 this.use = checkpoint_use;
1057 if (!(this.spawnflags & 1))
1058 settouch(this, checkpoint_touch);
1060 o = (this.absmin + this.absmax) * 0.5;
1061 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
1062 waypoint_spawnforitem_force(this, trace_endpos);
1063 this.nearestwaypointtimeout = -1;
1065 if(this.message == "")
1066 this.message = "went backwards";
1067 if (this.message2 == "")
1068 this.message2 = "was pushed backwards by";
1069 if (this.race_penalty_reason == "")
1070 this.race_penalty_reason = "missing a checkpoint";
1072 this.race_checkpoint = this.cnt;
1074 if(this.race_checkpoint > race_highest_checkpoint)
1076 race_highest_checkpoint = this.race_checkpoint;
1077 if(this.spawnflags & 8)
1078 race_timed_checkpoint = this.race_checkpoint;
1080 race_timed_checkpoint = 0;
1083 if(!this.race_penalty)
1085 if(this.race_checkpoint)
1086 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1088 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1091 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1092 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1094 IL_PUSH(g_racecheckpoints, this);
1096 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1099 void target_checkpoint_setup(entity this)
1101 if(!g_race && !g_cts) { delete(this); return; }
1104 // if this is targeted, then it probably isn't a trigger
1105 bool is_trigger = this.targetname == "";
1110 this.use = checkpoint_use;
1111 if (is_trigger && !(this.spawnflags & 1))
1112 settouch(this, checkpoint_touch);
1114 vector org = this.origin;
1116 // bots should only pathfind to this if it is a valid touchable trigger
1119 org = (this.absmin + this.absmax) * 0.5;
1120 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1121 waypoint_spawnforitem_force(this, trace_endpos);
1122 this.nearestwaypointtimeout = -1;
1125 if(this.message == "")
1126 this.message = "went backwards";
1127 if (this.message2 == "")
1128 this.message2 = "was pushed backwards by";
1129 if (this.race_penalty_reason == "")
1130 this.race_penalty_reason = "missing a checkpoint";
1132 if(this.classname == "target_startTimer")
1133 this.race_checkpoint = 0;
1135 this.race_checkpoint = -2;
1137 race_timed_checkpoint = 1;
1139 IL_PUSH(g_race_targets, this);
1141 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1144 spawnfunc(target_checkpoint)
1146 // xonotic defrag entity
1147 target_checkpoint_setup(this);
1150 // compatibility entity names
1151 spawnfunc(target_startTimer) { target_checkpoint_setup(this); }
1152 spawnfunc(target_stopTimer) { target_checkpoint_setup(this); }
1154 void race_AbandonRaceCheck(entity p)
1156 if(race_completing && !CS(p).race_completed)
1158 CS(p).race_completed = 1;
1159 MAKE_INDEPENDENT_PLAYER(p);
1160 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1161 ClientData_Touch(p);
1165 void race_StartCompleting()
1167 race_completing = 1;
1168 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1171 void race_PreparePlayer(entity this)
1173 race_ClearTime(this);
1174 this.race_place = 0;
1175 this.race_started = 0;
1176 this.race_respawn_checkpoint = 0;
1177 this.race_respawn_spotref = NULL;
1180 void race_RetractPlayer(entity this)
1182 if(!g_race && !g_cts)
1184 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1185 race_ClearTime(this);
1186 this.race_checkpoint = this.race_respawn_checkpoint;
1189 spawnfunc(info_player_race)
1191 if(!g_race && !g_cts) { delete(this); return; }
1193 spawnfunc_info_player_deathmatch(this);
1195 if(this.race_place > race_highest_place_spawn)
1196 race_highest_place_spawn = this.race_place;
1197 if(this.race_place < race_lowest_place_spawn)
1198 race_lowest_place_spawn = this.race_place;
1201 void race_ClearRecords()
1203 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1205 race_checkpoint_records[j] = 0;
1206 strfree(race_checkpoint_recordholders[j]);
1209 FOREACH_CLIENT(true, {
1210 float p = it.race_place;
1211 race_PreparePlayer(it);
1216 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1218 if(g_race_qualifying)
1220 pl.race_penalty_accumulator += penalty;
1221 if(IS_REAL_CLIENT(pl))
1224 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1225 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1226 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1227 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1228 WriteString(MSG_ONE, reason);
1234 pl.race_penalty = time + penalty;
1235 if(IS_REAL_CLIENT(pl))
1238 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1239 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1240 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1241 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1242 WriteString(MSG_ONE, reason);
1248 void penalty_touch(entity this, entity toucher)
1250 EXACTTRIGGER_TOUCH(this, toucher);
1251 if(toucher.race_lastpenalty != this)
1253 toucher.race_lastpenalty = this;
1254 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1258 void penalty_use(entity this, entity actor, entity trigger)
1260 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1263 spawnfunc(trigger_race_penalty)
1265 // TODO: find out why this wasnt done:
1266 //if(!g_cts && !g_race) { remove(this); return; }
1270 this.use = penalty_use;
1271 if (!(this.spawnflags & 1))
1272 settouch(this, penalty_touch);
1274 if (this.race_penalty_reason == "")
1275 this.race_penalty_reason = "missing a checkpoint";
1276 if (!this.race_penalty)
1277 this.race_penalty = 5;
1280 float race_GetFractionalLapCount(entity e)
1282 // interesting metrics (idea by KrimZon) to maybe sort players in the
1283 // scoreboard, immediately updates when overtaking
1285 // requires the track to be built so you never get farther away from the
1286 // next checkpoint, though, and current Xonotic race maps are not built that
1289 // also, this code is slow and would need optimization (i.e. "next CP"
1290 // links on CP entities)
1293 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1294 if(CS(e).race_completed)
1295 return l; // not fractional
1298 float bestfraction, fraction;
1300 float nextcpindex, lastcpindex;
1302 nextcpindex = max(e.race_checkpoint, 0);
1303 lastcpindex = e.race_respawn_checkpoint;
1304 lastcp = e.race_respawn_spotref;
1306 if(nextcpindex == lastcpindex)
1310 IL_EACH(g_racecheckpoints, true,
1312 if(it.race_checkpoint != lastcpindex)
1317 o0 = (it.absmin + it.absmax) * 0.5;
1318 IL_EACH(g_racecheckpoints, true,
1320 if(it.race_checkpoint != nextcpindex)
1322 o1 = (it.absmin + it.absmax) * 0.5;
1325 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1326 if(fraction < bestfraction)
1327 bestfraction = fraction;
1331 // we are at CP "nextcpindex - bestfraction"
1332 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1333 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1335 nc = race_highest_checkpoint + 1;
1336 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;