]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
race: round record times consistently
[divverent/nexuiz.git] / data / qcsrc / server / race.qc
1 #define MAX_CHECKPOINTS 255
2
3 .float race_checkpoint; // player: next checkpoint that has to be reached
4 .float race_laptime;
5
6 .entity sprite;
7
8 float race_checkpoint_records[MAX_CHECKPOINTS];
9 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
10 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
11 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
12 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
13
14 float race_highest_checkpoint;
15
16 float race_NextCheckpoint(float f)
17 {
18         if(f >= race_highest_checkpoint)
19                 return 0;
20         else
21                 return f + 1;
22 }
23
24 float race_PreviousCheckpoint(float f)
25 {
26         if(f == -1)
27                 return 0;
28         else if(f == 0)
29                 return race_highest_checkpoint;
30         else
31                 return f - 1;
32 }
33
34 void race_SendNextCheckpoint(entity e, float spec)
35 {
36         float recordtime;
37         string recordholder;
38         float cp;
39
40         if(!e.race_laptime)
41                 return;
42
43         cp = e.race_checkpoint;
44         recordtime = race_checkpoint_records[cp];
45         recordholder = race_checkpoint_recordholders[cp];
46         /*
47         recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
48         recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
49         */
50         if(recordholder == e.netname)
51                 recordholder = "";
52
53         if(!spec)
54                 msg_entity = e;
55         WRITESPECTATABLE_MSG_ONE({
56                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
57                 WriteByte(MSG_ONE, TE_CSQC_RACE);
58                 if(spec)
59                 {
60                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
61                         WriteCoord(MSG_ONE, e.race_laptime);
62                 }
63                 else
64                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
65                 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
66                 WriteShort(MSG_ONE, recordtime);
67                 WriteString(MSG_ONE, recordholder);
68         });
69 }
70
71 void race_InitSpectator()
72 {
73         if(g_race_qualifying)
74                 if(msg_entity.enemy.race_laptime)
75                         race_SendNextCheckpoint(msg_entity.enemy, 1);
76 }
77
78 void race_SendTime(entity e, float cp, float t, float tvalid)
79 {
80         float snew, l;
81         entity p;
82
83         t = floor(0.4 + 10 * t); // make integer
84         // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
85
86         if(tvalid)
87         if(cp == 0) // finish line
88         if not(e.race_completed)
89         {
90                 float s;
91                 if(g_race_qualifying)
92                 {
93                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
94                         if(!s || t < s)
95                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
96                 }
97                 else
98                 {
99                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
100                         snew = floor(0.5 + 10 * (time - game_starttime));
101                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
102                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
103
104                         if(cvar("fraglimit"))
105                                 if(l >= cvar("fraglimit"))
106                                 {
107                                         race_completing = 1;
108                                         FOR_EACH_PLAYER(p)
109                                                 if(p.deadflag != DEAD_NO)
110                                                         race_AbandonRaceCheck(p);
111                                 }
112
113                         if(race_completing)
114                         {
115                                 e.race_completed = 1;
116                                 MAKE_INDEPENDENT_PLAYER(e);
117                                 bprint(e.netname, "^7 has finished the race.\n");
118                                 // TODO support spectators with this (e.g. set a flag for forced sbar)
119                                 if(clienttype(e) == CLIENTTYPE_REAL)
120                                 {
121                                         msg_entity = e;
122                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
123                                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
124                                         WriteByte(MSG_ONE, 1);
125                                         // he can still move, but will see the scoreboard now
126                                 }
127                         }
128                 }
129         }
130
131         float recordtime;
132         string recordholder;
133
134         if(g_race_qualifying)
135         {
136                 if(tvalid)
137                 {
138                         recordtime = race_checkpoint_records[cp];
139                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
140                         if(recordholder == e.netname)
141                                 recordholder = "";
142
143                         if(t != 0)
144                         if(t < recordtime || recordtime == 0)
145                         {
146                                 race_checkpoint_records[cp] = t;
147                                 if(race_checkpoint_recordholders[cp])
148                                         strunzone(race_checkpoint_recordholders[cp]);
149                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
150                                 if(cp == 0)
151                                 {
152                                         float grecordtime;
153                                         string grecordholder;
154                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
155                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
156                                         if(grecordholder == e.netname)
157                                                 grecordholder = "";
158                                         if(grecordtime == 0)
159                                         {
160                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
161                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
162                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
163                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
164                                         }
165                                         else if(t < grecordtime)
166                                         {
167                                                 if(grecordholder == "")
168                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
169                                                 else
170                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
171                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
172                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
173                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
174                                         }
175                                         else
176                                         {
177                                                 if(grecordholder == "")
178                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
179                                                 else
180                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
181                                         }
182                                 }
183
184                                 if(g_race_qualifying)
185                                 {
186                                         FOR_EACH_REALPLAYER(p)
187                                                 if(p.race_checkpoint == cp)
188                                                         race_SendNextCheckpoint(p, 0);
189                                 }
190                         }
191                 }
192                 else
193                 {
194                         // dummies
195                         t = 0;
196                         recordtime = 0;
197                         recordholder = "";
198                 }
199
200                 msg_entity = e;
201                 if(g_race_qualifying)
202                 {
203                         WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
204                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
205                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
206                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
207                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
208                                 WriteShort(MSG_ONE, t); // time to that intermediate
209                                 WriteShort(MSG_ONE, recordtime); // previously best time
210                                 WriteString(MSG_ONE, recordholder); // record holder
211                         });
212                 }
213         }
214         else // RACE! Not Qualifying
215         {
216                 float lself, lother, othtime;
217                 entity oth;
218                 oth = race_checkpoint_lastplayers[cp];
219                 if(oth)
220                 {
221                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
222                         lother = race_checkpoint_lastlaps[cp];
223                         othtime = race_checkpoint_lasttimes[cp];
224                 }
225                 else
226                         lself = lother = othtime = 0;
227
228                 msg_entity = e;
229                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
230                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
231                         WriteByte(MSG_ONE, TE_CSQC_RACE);
232                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
233                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
234                         if(e == oth)
235                         {
236                                 WriteShort(MSG_ONE, 0);
237                                 WriteByte(MSG_ONE, 0);
238                                 WriteString(MSG_ONE, "");
239                         }
240                         else
241                         {
242                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
243                                 WriteByte(MSG_ONE, lself - lother);
244                                 WriteString(MSG_ONE, oth.netname); // record holder
245                         }
246                 });
247
248                 race_checkpoint_lastplayers[cp] = e;
249                 race_checkpoint_lasttimes[cp] = time;
250                 race_checkpoint_lastlaps[cp] = lself;
251
252                 msg_entity = oth;
253                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
254                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
255                         WriteByte(MSG_ONE, TE_CSQC_RACE);
256                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
257                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
258                         if(e == oth)
259                         {
260                                 WriteShort(MSG_ONE, 0);
261                                 WriteByte(MSG_ONE, 0);
262                                 WriteString(MSG_ONE, "");
263                         }
264                         else
265                         {
266                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
267                                 WriteByte(MSG_ONE, lother - lself);
268                                 WriteString(MSG_ONE, e.netname); // record holder
269                         }
270                 });
271         }
272 }
273
274 void race_ClearTime(entity e)
275 {
276         e.race_checkpoint = -1;
277         e.race_laptime = 0;
278
279         msg_entity = e;
280         WRITESPECTATABLE_MSG_ONE({
281                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
282                 WriteByte(MSG_ONE, TE_CSQC_RACE);
283                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
284         });
285 }
286
287 void dumpsurface(entity e)
288 {
289         float n, si, ni;
290         vector norm, vec;
291         print("Surfaces of ", etos(e), ":\n");
292
293         print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
294
295         for(si = 0; ; ++si)
296         {
297                 n = getsurfacenumpoints(e, si);
298                 if(n <= 0)
299                         break;
300                 print("  Surface ", ftos(si), ":\n");
301                 norm = getsurfacenormal(e, si);
302                 print("    Normal = ", vtos(norm), "\n");
303                 for(ni = 0; ni < n; ++ni)
304                 {
305                         vec = getsurfacepoint(e, si, ni);
306                         print("    Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
307                 }
308         }
309 }
310
311 void checkpoint_passed()
312 {
313         string oldmsg;
314
315         if(other.classname == "porto")
316         {
317                 // do not allow portalling through checkpoints
318                 trace_plane_normal = normalize(-1 * other.velocity);
319                 self = other;
320                 W_Porto_Fail(0);
321                 return;
322         }
323
324         /*
325          * Trigger targets
326          */
327         if not(self.spawnflags & 2)
328         {
329                 activator = other;
330                 oldmsg = self.message;
331                 self.message = "";
332                 SUB_UseTargets();
333                 self.message = oldmsg;
334         }
335
336         if(other.classname != "player")
337                 return;
338
339         /*
340          * Remove unauthorized equipment
341          */
342         Portal_ClearAll(other);
343
344         other.porto_forbidden = 2; // decreased by 1 each StartFrame
345
346         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
347         {
348                 /*
349                  * Trigger targets
350                  */
351                 if(self.spawnflags & 2)
352                 {
353                         activator = other;
354                         oldmsg = self.message;
355                         self.message = "";
356                         SUB_UseTargets();
357                         self.message = oldmsg;
358                 }
359
360                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
361
362                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
363
364                 if(!self.race_checkpoint) // finish line
365                         other.race_laptime = time;
366
367                 if(g_race_qualifying)
368                         race_SendNextCheckpoint(other, 0);
369         }
370         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
371         {
372                 // ignored
373         }
374         else
375         {
376                 if(self.spawnflags & 4)
377                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
378         }
379 }
380
381 void checkpoint_touch()
382 {
383         EXACTTRIGGER_TOUCH;
384         checkpoint_passed();
385 }
386
387 void checkpoint_use()
388 {
389         other = activator;
390         checkpoint_passed();
391 }
392
393 float race_waypointsprite_visible_for_player(entity e)
394 {
395         if(e.race_checkpoint == -1)
396                 return TRUE;
397         else if(e.race_checkpoint == self.owner.race_checkpoint)
398                 return TRUE;
399         else
400                 return FALSE;
401 }
402
403 void spawnfunc_trigger_race_checkpoint()
404 {
405         vector o;
406         if(!g_race)
407         {
408                 remove(self);
409                 return;
410         }
411
412         EXACTTRIGGER_INIT;
413
414         self.use = checkpoint_use;
415         if not(self.spawnflags & 1)
416                 self.touch = checkpoint_touch;
417
418         o = (self.absmin + self.absmax) * 0.5;
419         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
420         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
421         self.nearestwaypointtimeout = time + 1000000000;
422
423         if(!self.message)
424                 self.message = "went backwards";
425         if (!self.message2)
426                 self.message2 = "was pushed backwards by";
427         
428         self.race_checkpoint = self.cnt;
429
430         if(self.race_checkpoint > race_highest_checkpoint)
431                 race_highest_checkpoint = self.race_checkpoint;
432
433         if(self.race_checkpoint)
434         {
435                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
436         }
437         else
438         {
439                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
440         }
441         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
442 }
443
444 void race_AbandonRaceCheck(entity p)
445 {
446         if(race_completing && !p.race_completed)
447         {
448                 p.race_completed = 1;
449                 MAKE_INDEPENDENT_PLAYER(p);
450                 bprint(p.netname, "^7 has abandoned the race.\n");
451                 // TODO support spectators with this (e.g. set a flag for forced sbar)
452                 if(clienttype(p) == CLIENTTYPE_REAL)
453                 {
454                         msg_entity = p;
455                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
456                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
457                         WriteByte(MSG_ONE, 1);
458                         // he can still move, but will see the scoreboard now
459                 }
460         }
461 }
462
463 void race_PreparePlayer()
464 {
465         race_ClearTime(self);
466         self.race_place = 0;
467 }
468
469 void race_RetractPlayer()
470 {
471         if(!g_race)
472                 return;
473         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
474         if(self.race_checkpoint == 0)
475         {
476                 race_ClearTime(self);
477                 self.race_checkpoint = 0;
478         }
479 }
480
481 void race_PreDie()
482 {
483         if(!g_race)
484                 return;
485
486         race_AbandonRaceCheck(self);
487 }
488
489 void race_PreSpawn()
490 {
491         if(!g_race)
492                 return;
493         if(self.killcount == -666 || g_race_qualifying)
494                 race_PreparePlayer();
495
496         race_AbandonRaceCheck(self);
497 }
498
499 void race_PostSpawn(entity spot)
500 {
501         if(!g_race)
502                 return;
503         if(self.killcount != -666 && !g_race_qualifying)
504         {
505                 if(spot.target == "")
506                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
507                         race_PreparePlayer();
508                 else
509                         race_RetractPlayer();
510         }
511
512         if(spot.target != "" && self.race_checkpoint == -1)
513                 self.race_checkpoint = 0;
514
515         self.race_place = 0;
516 }
517
518 void race_PreSpawnObserver()
519 {
520         if(!g_race)
521                 return;
522         race_PreparePlayer();
523 }
524
525 void spawnfunc_info_player_race (void)
526 {
527         if(!g_race)
528         {
529                 remove(self);
530                 return;
531         }
532         ++race_spawns;
533         spawnfunc_info_player_deathmatch();
534
535         if(self.race_place > race_highest_place_spawn)
536                 race_highest_place_spawn = self.race_place;
537         if(self.race_place < race_lowest_place_spawn)
538                 race_lowest_place_spawn = self.race_place;
539 }
540
541 void race_ClearRecords()
542 {
543         float i;
544         entity e;
545
546         for(i = 0; i < MAX_CHECKPOINTS; ++i)
547         {
548                 race_checkpoint_records[i] = 0;
549                 if(race_checkpoint_recordholders[i])
550                         strunzone(race_checkpoint_recordholders[i]);
551                 race_checkpoint_recordholders[i] = string_null;
552         }
553
554         FOR_EACH_CLIENT(e)
555                 race_ClearTime(e);
556 }
557
558 void race_ReadyRestart()
559 {
560         race_ClearRecords();
561
562         if(g_race_qualifying == 2)
563         {
564                 g_race_qualifying = 0;
565                 independent_players = 0;
566                 cvar_set("fraglimit", ftos(race_fraglimit));
567                 ScoreRules_race();
568         }
569
570         PlayerScore_Sort(race_place);
571 }