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