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