]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
race: completed spectator support
[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(self.enemy.race_laptime)
75                 race_SendNextCheckpoint(self.enemy, 1);
76 }
77
78 void race_SendTime(entity e, float cp, float t, float tvalid)
79 {
80         float snew, l;
81         t = floor(0.5 + 10 * t); // make integer
82
83         if(tvalid)
84         if(cp == 0) // finish line
85         if not(e.race_completed)
86         {
87                 float s;
88                 if(g_race_qualifying)
89                 {
90                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
91                         if(!s || t < s)
92                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
93                 }
94                 else
95                 {
96                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
97                         snew = floor(0.5 + 10 * (time - restart_countdown));
98                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
99                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
100
101                         if(cvar("fraglimit"))
102                                 if(l >= cvar("fraglimit"))
103                                         race_completing = 1;
104
105                         if(race_completing)
106                         {
107                                 e.race_completed = 1;
108                                 MAKE_INDEPENDENT_PLAYER(e);
109                                 bprint(e.netname, "^7 has finished the race.\n");
110                                 // TODO support spectators with this (e.g. set a flag for forced sbar)
111                                 if(clienttype(e) == CLIENTTYPE_REAL)
112                                 {
113                                         msg_entity = e;
114                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
115                                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
116                                         WriteByte(MSG_ONE, 1);
117                                         // he can still move, but will see the scoreboard now
118                                 }
119                         }
120                 }
121         }
122
123         float recordtime;
124         string recordholder;
125
126         if(g_race_qualifying)
127         {
128                 if(tvalid)
129                 {
130                         recordtime = race_checkpoint_records[cp];
131                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
132                         if(recordholder == e.netname)
133                                 recordholder = "";
134
135                         if(t < recordtime || recordtime == 0)
136                         {
137                                 race_checkpoint_records[cp] = t;
138                                 if(race_checkpoint_recordholders[cp])
139                                         strunzone(race_checkpoint_recordholders[cp]);
140                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
141                                 if(cp == 0)
142                                 {
143                                         float grecordtime;
144                                         string grecordholder;
145                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
146                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
147                                         if(grecordholder == e.netname)
148                                                 grecordholder = "";
149                                         if(grecordtime == 0)
150                                         {
151                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
152                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
153                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
154                                         }
155                                         else if(t < grecordtime)
156                                         {
157                                                 if(grecordholder == "")
158                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
159                                                 else
160                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's 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                                         }
164                                         else
165                                         {
166                                                 if(grecordholder == "")
167                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
168                                                 else
169                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
170                                         }
171                                 }
172
173                                 if(g_race_qualifying)
174                                 {
175                                         entity p;
176                                         FOR_EACH_REALPLAYER(p)
177                                                 if(p.race_checkpoint == cp)
178                                                         race_SendNextCheckpoint(p, 0);
179                                 }
180                         }
181                 }
182                 else
183                 {
184                         // dummies
185                         t = 0;
186                         recordtime = 0;
187                         recordholder = "";
188                 }
189
190                 msg_entity = e;
191                 if(g_race_qualifying)
192                 {
193                         WRITESPECTATABLE_MSG_ONE({
194                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
195                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
196                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
197                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
198                                 WriteShort(MSG_ONE, t); // time to that intermediate
199                                 WriteShort(MSG_ONE, recordtime); // previously best time
200                                 WriteString(MSG_ONE, recordholder); // record holder
201                         });
202                 }
203         }
204         else // RACE! Not Qualifying
205         {
206                 float lself, lother, othtime;
207                 entity oth;
208                 oth = race_checkpoint_lastplayers[cp];
209                 if(oth)
210                 {
211                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
212                         lother = race_checkpoint_lastlaps[cp];
213                         othtime = race_checkpoint_lasttimes[cp];
214                 }
215                 else
216                         lself = lother = othtime = 0;
217
218                 msg_entity = e;
219                 WRITESPECTATABLE_MSG_ONE({
220                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
221                         WriteByte(MSG_ONE, TE_CSQC_RACE);
222                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
223                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
224                         if(e == oth)
225                         {
226                                 WriteShort(MSG_ONE, 0);
227                                 WriteByte(MSG_ONE, 0);
228                                 WriteString(MSG_ONE, "");
229                         }
230                         else
231                         {
232                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
233                                 WriteByte(MSG_ONE, lself - lother);
234                                 WriteString(MSG_ONE, oth.netname); // record holder
235                         }
236                 });
237
238                 race_checkpoint_lastplayers[cp] = e;
239                 race_checkpoint_lasttimes[cp] = time;
240                 race_checkpoint_lastlaps[cp] = lself;
241
242                 msg_entity = oth;
243                 WRITESPECTATABLE_MSG_ONE({
244                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
245                         WriteByte(MSG_ONE, TE_CSQC_RACE);
246                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
247                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
248                         if(e == oth)
249                         {
250                                 WriteShort(MSG_ONE, 0);
251                                 WriteByte(MSG_ONE, 0);
252                                 WriteString(MSG_ONE, "");
253                         }
254                         else
255                         {
256                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
257                                 WriteByte(MSG_ONE, lother - lself);
258                                 WriteString(MSG_ONE, e.netname); // record holder
259                         }
260                 });
261         }
262 }
263
264 void race_ClearTime(entity e)
265 {
266         e.race_checkpoint = -1;
267         e.race_laptime = 0;
268
269         msg_entity = e;
270         WRITESPECTATABLE_MSG_ONE({
271                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
272                 WriteByte(MSG_ONE, TE_CSQC_RACE);
273                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
274         });
275 }
276
277 void checkpoint_touch()
278 {
279         if(other.classname != "player")
280                 return;
281
282         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
283         {
284                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
285
286                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
287
288                 if(!self.race_checkpoint) // finish line
289                         other.race_laptime = time;
290
291                 if(g_race_qualifying)
292                         race_SendNextCheckpoint(other, 0);
293         }
294         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
295         {
296                 // ignored
297         }
298         else
299         {
300                 if(self.spawnflags & 4)
301                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
302         }
303 }
304
305 void checkpoint_use()
306 {
307         other = activator;
308         checkpoint_touch();
309 }
310
311 float race_waypointsprite_for_player(entity e)
312 {
313         if(e.race_checkpoint == -1)
314                 return self.modelindex;
315         else if(e.race_checkpoint == self.owner.race_checkpoint)
316                 return self.modelindex;
317         else
318                 return FALSE;
319 }
320
321 void spawnfunc_trigger_race_checkpoint()
322 {
323         vector o;
324         if(!g_race)
325         {
326                 remove(self);
327                 return;
328         }
329         InitTrigger();
330         self.use = checkpoint_use;
331         if not(self.spawnflags & 1)
332                 self.touch = checkpoint_touch;
333
334         o = (self.absmin + self.absmax) * 0.5;
335         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
336         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
337         self.nearestwaypointtimeout = time + 1000000000;
338
339         if(!self.message)
340                 self.message = "went backwards";
341         
342         self.race_checkpoint = self.cnt;
343
344         if(self.race_checkpoint > race_highest_checkpoint)
345                 race_highest_checkpoint = self.race_checkpoint;
346
347         if(self.race_checkpoint)
348         {
349                 precache_model("models/sprites/race-checkpoint.sp2");
350                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
351         }
352         else
353         {
354                 precache_model("models/sprites/race-finish.sp2");
355                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
356         }
357         self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
358 }
359
360 void race_PreparePlayer()
361 {
362         race_ClearTime(self);
363         self.race_place = 0;
364 }
365
366 void race_RetractPlayer()
367 {
368         if(!g_race)
369                 return;
370         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
371         if(self.race_checkpoint == 0)
372         {
373                 race_ClearTime(self);
374                 self.race_checkpoint = 0;
375         }
376 }
377
378 void race_PreSpawn()
379 {
380         if(!g_race)
381                 return;
382         if(self.killcount == -666 || g_race_qualifying)
383                 race_PreparePlayer();
384
385         if(race_completing)
386         {
387                 self.race_completed = 1;
388                 MAKE_INDEPENDENT_PLAYER(self);
389                 bprint(self.netname, "^7 has abandoned the race.\n");
390                 // TODO support spectators with this (e.g. set a flag for forced sbar)
391                 if(clienttype(self) == CLIENTTYPE_REAL)
392                 {
393                         msg_entity = self;
394                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
395                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
396                         WriteByte(MSG_ONE, 1);
397                         // he can still move, but will see the scoreboard now
398                 }
399         }
400 }
401
402 void race_PostSpawn(entity spot)
403 {
404         if(!g_race)
405                 return;
406         if(self.killcount != -666 && !g_race_qualifying)
407         {
408                 if(spot.target == "")
409                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
410                         race_PreparePlayer();
411                 else
412                         race_RetractPlayer();
413         }
414
415         if(spot.target != "" && self.race_checkpoint == -1)
416                 self.race_checkpoint = 0;
417
418         self.race_place = 0;
419 }
420
421 void race_PreSpawnObserver()
422 {
423         if(!g_race)
424                 return;
425         race_PreparePlayer();
426 }
427
428 void spawnfunc_info_player_race (void)
429 {
430         if(!g_race)
431         {
432                 remove(self);
433                 return;
434         }
435         ++race_spawns;
436         spawnfunc_info_player_deathmatch();
437
438         if(self.race_place > race_highest_place_spawn)
439                 race_highest_place_spawn = self.race_place;
440 }
441
442 void race_ClearRecords()
443 {
444         float i;
445         entity e;
446
447         for(i = 0; i < MAX_CHECKPOINTS; ++i)
448         {
449                 race_checkpoint_records[i] = 0;
450                 if(race_checkpoint_recordholders[i])
451                         strunzone(race_checkpoint_recordholders[i]);
452                 race_checkpoint_recordholders[i] = string_null;
453         }
454
455         FOR_EACH_CLIENT(e)
456                 race_ClearTime(e);
457 }
458
459 void race_ReadyRestart()
460 {
461         race_ClearRecords();
462
463         if(g_race_qualifying == 2)
464         {
465                 g_race_qualifying = 0;
466                 independent_players = 0;
467                 cvar_set("fraglimit", ftos(race_fraglimit));
468                 ScoreRules_race();
469         }
470
471         PlayerScore_Sort(race_place);
472 }