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