]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/Main.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / Main.qc
1 // --------------------------------------------------------------------------
2 // BEGIN REQUIRED CSQC FUNCTIONS
3 //include "main.qh"
4
5 #define DP_CSQC_ENTITY_REMOVE_IS_B0RKED
6 void menu_show_error()
7 {
8         drawstring('0 200 0', _("ERROR - MENU IS VISIBLE BUT NO MENU WAS DEFINED!"), '8 8 0', '1 0 0', 1, 0);
9 }
10
11 // CSQC_Init : Called every time the CSQC code is initialized (essentially at map load)
12 // Useful for precaching things
13
14 void menu_sub_null()
15 {
16 }
17
18 #ifdef USE_FTE
19 float __engine_check;
20 #endif
21
22 string forcefog;
23 void WaypointSprite_Load();
24 void ConsoleCommand_macro_init();
25 void CSQC_Init(void)
26 {
27         prvm_language = cvar_string("prvm_language");
28
29 #ifdef USE_FTE
30 #pragma target ID
31         __engine_check = checkextension("DP_SV_WRITEPICTURE");
32         if(!__engine_check)
33         {
34                 print(_("^3Your engine build is outdated\n^3This Server uses a newer QC VM. Please update!\n"));
35                 localcmd("\ndisconnect\n");
36                 return;
37         }
38 #pragma target FTE
39 #endif
40
41         check_unacceptable_compiler_bugs();
42
43 #ifdef WATERMARK
44         print(sprintf(_("^4CSQC Build information: ^1%s\n"), WATERMARK()));
45 #endif
46
47         float i;
48
49 #ifdef COMPAT_XON050_ENGINE
50         // old engine lacks implementation of player_localnum
51         player_localnum = player_localentnum - 1;
52 #endif
53
54         binddb = db_create();
55         tempdb = db_create();
56         ClientProgsDB = db_load("client.db");
57         compressShortVector_init();
58
59         drawfont = FONT_USER+1;
60         menu_visible = FALSE;
61         menu_show = menu_show_error;
62         menu_action = menu_sub_null;
63
64         for(i = 0; i < 255; ++i)
65                 if(getplayerkeyvalue(i, "viewentity") == "")
66                         break;
67         maxclients = i;
68
69         //registercommand("hud_configure");
70         //registercommand("hud_save");
71         //registercommand("menu_action");
72         
73         ConsoleCommand_macro_init();
74
75         registercvar("hud_usecsqc", "1");
76         registercvar("scoreboard_columns", "default");
77
78         gametype = 0;
79
80         // hud_fields uses strunzone on the titles!
81         for(i = 0; i < MAX_HUD_FIELDS; ++i)
82                 hud_title[i] = strzone("(null)");
83
84         postinit = false;
85
86         calledhooks = 0;
87
88         teams = Sort_Spawn();
89         players = Sort_Spawn();
90
91         GetTeam(COLOR_SPECTATOR, true); // add specs first
92
93         RegisterWeapons();
94
95         WaypointSprite_Load();
96
97         // precaches
98         precache_model("null");
99         precache_sound("misc/hit.wav");
100         precache_sound("misc/typehit.wav");
101         Projectile_Precache();
102         Hook_Precache();
103         GibSplash_Precache();
104         Casings_Precache();
105         DamageInfo_Precache();
106         Vehicles_Precache();
107         turrets_precache();
108   Announcer_Precache();
109         Tuba_Precache();
110         
111         if(autocvar_cl_reticle)
112         {
113                 if(autocvar_cl_reticle_item_normal) { precache_pic("gfx/reticle_normal"); }
114                 if(autocvar_cl_reticle_item_nex) { precache_pic("gfx/reticle_nex"); }
115         }
116         
117         get_mi_min_max_texcoords(1); // try the CLEVER way first
118         minimapname = strcat("gfx/", mi_shortname, "_radar.tga");
119         shortmapname = mi_shortname;
120
121         if(precache_pic(minimapname) == "")
122         {
123                 // but maybe we have a non-clever minimap
124                 minimapname = strcat("gfx/", mi_shortname, "_mini.tga");
125                 if(precache_pic(minimapname) == "")
126                         minimapname = ""; // FAIL
127                 else
128                         get_mi_min_max_texcoords(0); // load new texcoords
129         }
130
131         mi_center = (mi_min + mi_max) * 0.5;
132         mi_scale = mi_max - mi_min;
133         minimapname = strzone(minimapname);
134
135         WarpZone_Init();
136
137         hud_configure_prev = -1;
138         tab_panel = -1;
139 }
140
141 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)
142 float cvar_clientsettemp_restore();
143 void CSQC_Shutdown(void)
144 {
145 #ifdef USE_FTE
146 #pragma TARGET id
147         if(!__engine_check)
148                 return 0;
149 #pragma TARGET fte
150 #endif
151
152         WarpZone_Shutdown();
153
154         remove(teams);
155         remove(players);
156         db_close(binddb);
157         db_close(tempdb);
158         if(autocvar_cl_db_saveasdump)
159                 db_dump(ClientProgsDB, "client.db");
160         else
161                 db_save(ClientProgsDB, "client.db");
162         db_close(ClientProgsDB);
163
164         cvar_clientsettemp_restore();
165
166         if(camera_active)
167                 cvar_set("chase_active",ftos(chase_active_backup));
168
169         // unset the event chasecam's chase_active
170         if(autocvar_chase_active < 0)
171                 cvar_set("chase_active", "0");
172
173         if not(isdemo())
174         {
175                 if not(calledhooks & HOOK_START)
176                         localcmd("\n_cl_hook_gamestart nop\n");
177                 if not(calledhooks & HOOK_END)
178                         localcmd("\ncl_hook_gameend\n");
179         }
180 }
181
182 .float has_team;
183 float SetTeam(entity o, float Team)
184 {
185         entity tm;
186         if(teamplay)
187         {
188                 switch(Team)
189                 {
190                         case -1:
191                         case COLOR_TEAM1:
192                         case COLOR_TEAM2:
193                         case COLOR_TEAM3:
194                         case COLOR_TEAM4:
195                                 break;
196                         default:
197                                 if(GetTeam(Team, false) == world)
198                                 {
199                                         print(sprintf(_("trying to switch to unsupported team %d\n"), Team));
200                                         Team = COLOR_SPECTATOR;
201                                 }
202                                 break;
203                 }
204         }
205         else
206         {
207                 switch(Team)
208                 {
209                         case -1:
210                         case 0:
211                                 break;
212                         default:
213                                 if(GetTeam(Team, false) == world)
214                                 {
215                                         print(sprintf(_("trying to switch to unsupported team %d\n"), Team));
216                                         Team = COLOR_SPECTATOR;
217                                 }
218                                 break;
219                 }
220         }
221         if(Team == -1) // leave
222         {
223                 if(o.has_team)
224                 {
225                         tm = GetTeam(o.team, false);
226                         tm.team_size -= 1;
227                         o.has_team = 0;
228                         return TRUE;
229                 }
230         }
231         else
232         {
233                 if not(o.has_team)
234                 {
235                         o.team = Team;
236                         tm = GetTeam(Team, true);
237                         tm.team_size += 1;
238                         o.has_team = 1;
239                         return TRUE;
240                 }
241                 else if(Team != o.team)
242                 {
243                         tm = GetTeam(o.team, false);
244                         tm.team_size -= 1;
245                         o.team = Team;
246                         tm = GetTeam(Team, true);
247                         tm.team_size += 1;
248                         return TRUE;
249                 }
250         }
251         return FALSE;
252 }
253
254 void Playerchecker_Think()
255 {
256         float i;
257         entity e;
258         for(i = 0; i < maxclients; ++i)
259         {
260                 e = playerslots[i];
261                 if(GetPlayerName(i) == "")
262                 {
263                         if(e.sort_prev)
264                         {
265                                 // player disconnected
266                                 SetTeam(e, -1);
267                                 RemovePlayer(e);
268                                 e.sort_prev = world;
269                                 //e.gotscores = 0;
270                         }
271                 }
272                 else
273                 {
274                         if not(e.sort_prev)
275                         {
276                                 // player connected
277                                 if not(e)
278                                         playerslots[i] = e = spawn();
279                                 e.sv_entnum = i;
280                                 e.ping = 0;
281                                 e.ping_packetloss = 0;
282                                 e.ping_movementloss = 0;
283                                 //e.gotscores = 0; // we might already have the scores...
284                                 SetTeam(e, GetPlayerColor(i)); // will not hurt; later updates come with HUD_UpdatePlayerTeams
285                                 RegisterPlayer(e);
286                                 HUD_UpdatePlayerPos(e);
287                         }
288                 }
289         }
290         self.nextthink = time + 0.2;
291 }
292
293 void Porto_Init();
294 void TrueAim_Init();
295 void PostInit(void)
296 {
297         localcmd(strcat("\nscoreboard_columns_set ", autocvar_scoreboard_columns, ";\n"));
298
299         entity playerchecker;
300         playerchecker = spawn();
301         playerchecker.think = Playerchecker_Think;
302         playerchecker.nextthink = time + 0.2;
303
304         Porto_Init();
305         TrueAim_Init();
306
307         postinit = true;
308 }
309
310 float button_zoom;
311
312 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.
313 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.
314 // All keys are in ascii.
315 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.
316 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.
317 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.
318 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)
319 {
320         float bSkipKey;
321         bSkipKey = false;
322
323         if (HUD_Panel_InputEvent(bInputType, nPrimary, nSecondary))
324                 return true;
325
326         if (MapVote_InputEvent(bInputType, nPrimary, nSecondary))
327                 return true;
328
329         if(menu_visible)
330                 if(menu_action(bInputType, nPrimary, nSecondary))
331                         return TRUE;
332
333         return bSkipKey;
334 }
335
336 // END REQUIRED CSQC FUNCTIONS
337 // --------------------------------------------------------------------------
338
339 // --------------------------------------------------------------------------
340 // BEGIN OPTIONAL CSQC FUNCTIONS
341 void Ent_RemoveEntCS()
342 {
343         entcs_receiver[self.sv_entnum] = world;
344 }
345 void Ent_ReadEntCS()
346 {
347         float sf;
348         InterpolateOrigin_Undo();
349
350         self.classname = "entcs_receiver";
351         sf = ReadByte();
352
353         if(sf & 1)
354                 self.sv_entnum = ReadByte();
355         if(sf & 2)
356         {
357                 self.origin_x = ReadShort();
358                 self.origin_y = ReadShort();
359                 self.origin_z = ReadShort();
360         }
361         if(sf & 4)
362         {
363                 self.angles_y = ReadByte() * 360.0 / 256;
364                 self.angles_x = self.angles_z = 0;
365         }
366         if(sf & 8)
367                 self.healthvalue = ReadByte() * 10;
368         if(sf & 16)
369                 self.armorvalue = ReadByte() * 10;
370
371         entcs_receiver[self.sv_entnum] = self;
372         self.entremove = Ent_RemoveEntCS;
373
374         InterpolateOrigin_Note();
375 }
376
377 void Ent_Remove();
378
379 void Ent_RemovePlayerScore()
380 {
381         float i;
382
383         if(self.owner)
384         {
385                 SetTeam(self.owner, -1);
386                 self.owner.gotscores = 0;
387                 for(i = 0; i < MAX_SCORE; ++i)
388                         self.owner.(scores[i]) = 0; // clear all scores
389         }
390 }
391
392 void Ent_ReadPlayerScore()
393 {
394         float i, n;
395         float isNew;
396         entity o;
397
398         // damnit -.- don't want to go change every single .sv_entnum in hud.qc AGAIN
399         // (no I've never heard of M-x replace-string, sed, or anything like that)
400         isNew = !self.owner; // workaround for DP bug
401         n = ReadByte()-1;
402
403 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
404         if(!isNew && n != self.sv_entnum)
405         {
406                 //print(_("A CSQC entity changed its owner!\n"));
407                 print(sprintf(_("A CSQC entity changed its owner! (edict: %d, classname: %s)\n"), num_for_edict(self), self.classname));
408                 isNew = true;
409                 Ent_Remove();
410                 self.enttype = ENT_CLIENT_SCORES;
411         }
412 #endif
413
414         self.sv_entnum = n;
415
416         if not(playerslots[self.sv_entnum])
417                 playerslots[self.sv_entnum] = spawn();
418         o = self.owner = playerslots[self.sv_entnum];
419         o.sv_entnum = self.sv_entnum;
420         o.gotscores = 1;
421
422         //if not(o.sort_prev)
423         //      RegisterPlayer(o);
424         //playerchecker will do this for us later, if it has not already done so
425
426         float sf, lf;
427 #if MAX_SCORE <= 8
428         sf = ReadByte();
429         lf = ReadByte();
430 #else
431         sf = ReadShort();
432         lf = ReadShort();
433 #endif
434         float p;
435         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
436                 if(sf & p)
437                 {
438                         if(lf & p)
439                                 o.(scores[i]) = ReadInt24_t();
440                         else
441                                 o.(scores[i]) = ReadChar();
442                 }
443
444         if(o.sort_prev)
445                 HUD_UpdatePlayerPos(o); // if not registered, we cannot do this yet!
446
447         self.entremove = Ent_RemovePlayerScore;
448 }
449
450 void Ent_ReadTeamScore()
451 {
452         float i;
453         entity o;
454
455         self.team = ReadByte();
456         o = self.owner = GetTeam(self.team, true); // these team numbers can always be trusted
457
458         float sf, lf;
459 #if MAX_TEAMSCORE <= 8
460         sf = ReadByte();
461         lf = ReadByte();
462 #else
463         sf = ReadShort();
464         lf = ReadShort();
465 #endif
466         float p;
467         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
468                 if(sf & p)
469                 {
470                         if(lf & p)
471                                 o.(teamscores[i]) = ReadInt24_t();
472                         else
473                                 o.(teamscores[i]) = ReadChar();
474                 }
475
476         HUD_UpdateTeamPos(o);
477 }
478
479 void Ent_ClientData()
480 {
481         float f;
482         float newspectatee_status;
483
484         f = ReadByte();
485
486         scoreboard_showscores_force = (f & 1);
487
488         if(f & 2)
489         {
490                 newspectatee_status = ReadByte();
491                 if(newspectatee_status == player_localnum + 1)
492                         newspectatee_status = -1; // observing
493         }
494         else
495                 newspectatee_status = 0;
496
497         spectatorbutton_zoom = (f & 4);
498
499         if(f & 8)
500         {
501                 angles_held_status = 1;
502                 angles_held_x = ReadAngle();
503                 angles_held_y = ReadAngle();
504                 angles_held_z = 0;
505         }
506         else
507                 angles_held_status = 0;
508
509         if(newspectatee_status != spectatee_status)
510         {
511                 // clear race stuff
512                 race_laptime = 0;
513                 race_checkpointtime = 0;
514         }
515         if (autocvar_hud_panel_healtharmor_progressbar_gfx)
516         {
517                 if ( (spectatee_status == -1 && newspectatee_status > 0) //before observing, now spectating
518                   || (spectatee_status > 0 && newspectatee_status > 0 && spectatee_status != newspectatee_status) //changed spectated player
519                 )
520                         prev_p_health = -1;
521                 else if(spectatee_status && !newspectatee_status) //before observing/spectating, now playing
522                         prev_health = -1;
523         }
524         spectatee_status = newspectatee_status;
525
526         // non-COMPAT_XON050_ENGINE: we could get rid of spectatee_status, and derive it from player_localentnum and player_localnum
527 }
528
529 void Ent_Nagger()
530 {
531         float nags, i, j, b, f;
532
533         nags = ReadByte(); // NAGS NAGS NAGS NAGS NAGS NAGS NADZ NAGS NAGS NAGS
534
535         if(!(nags & 4))
536         {
537                 if(vote_called_vote)
538                         strunzone(vote_called_vote);
539                 vote_called_vote = string_null;
540                 vote_active = 0;
541         }
542         else
543         {
544                 vote_active = 1;
545         }
546
547         if(nags & 64)
548         {
549                 vote_yescount = ReadByte();
550                 vote_nocount = ReadByte();
551                 vote_needed = ReadByte();
552                 vote_highlighted = ReadChar();
553         }
554
555         if(nags & 128)
556         {
557                 if(vote_called_vote)
558                         strunzone(vote_called_vote);
559                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));
560         }
561
562         if(nags & 1)
563         {
564                 for(j = 0; j < maxclients; ++j)
565                         if(playerslots[j])
566                                 playerslots[j].ready = 1;
567                 for(i = 1; i <= maxclients; i += 8)
568                 {
569                         f = ReadByte();
570                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
571                                 if not(f & b)
572                                         if(playerslots[j])
573                                                 playerslots[j].ready = 0;
574                 }
575         }
576
577         ready_waiting = (nags & 1);
578         ready_waiting_for_me = (nags & 2);
579         vote_waiting = (nags & 4);
580         vote_waiting_for_me = (nags & 8);
581         warmup_stage = (nags & 16);
582 }
583
584 void Ent_RandomSeed()
585 {
586         float s;
587         prandom_debug();
588         s = ReadShort();
589         psrandom(s);
590 }
591
592 void Ent_ReadAccuracy(void)
593 {
594         float sf, f, w, b;
595         sf = ReadInt24_t();
596         if(sf == 0)
597         {
598                 for(w = 0; w <= WEP_LAST - WEP_FIRST; ++w)
599                         weapon_accuracy[w] = -1;
600                 return;
601         }
602
603         for(w = 0, f = 1; w <= WEP_LAST - WEP_FIRST; ++w, f *= 2)
604         {
605                 if(sf & f)
606                 {
607                         b = ReadByte();
608                         if(b == 0)
609                                 weapon_accuracy[w] = -1;
610                         else if(b == 255)
611                                 weapon_accuracy[w] = 1.0; // no better error handling yet, sorry
612                         else
613                                 weapon_accuracy[w] = (b - 1.0) / 100.0;
614                 }
615         }
616 }
617
618 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.
619 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.
620 void Ent_RadarLink();
621 void Ent_Init();
622 void Ent_ScoresInfo();
623 void CSQC_Ent_Update(float bIsNewEntity)
624 {
625         float t;
626         float savetime;
627         t = ReadByte();
628
629         // set up the "time" global for received entities to be correct for interpolation purposes
630         savetime = time;
631         if(servertime)
632         {
633                 time = servertime;
634         }
635         else
636         {
637                 serverprevtime = time;
638                 serverdeltatime = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
639                 time = serverprevtime + serverdeltatime;
640         }
641
642 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
643         if(self.enttype)
644         {
645                 if(t != self.enttype || bIsNewEntity)
646                 {
647                         //print(_("A CSQC entity changed its type!\n"));
648                         print(sprintf(_("A CSQC entity changed its type! (edict: %d, server: %d, type: %d -> %d)\n"), num_for_edict(self), self.entnum, self.enttype, t));
649                         Ent_Remove();
650                         bIsNewEntity = 1;
651                 }
652         }
653         else
654         {
655                 if(!bIsNewEntity)
656                 {
657                         print(sprintf(_("A CSQC entity appeared out of nowhere! (edict: %d, server: %d, type: %d)\n"), num_for_edict(self), self.entnum, t));
658                         bIsNewEntity = 1;
659                 }
660         }
661 #endif
662         self.enttype = t;
663         switch(t)
664         {
665                 case ENT_CLIENT_ENTCS: Ent_ReadEntCS(); break;
666                 case ENT_CLIENT_SCORES: Ent_ReadPlayerScore(); break;
667                 case ENT_CLIENT_TEAMSCORES: Ent_ReadTeamScore(); break;
668                 case ENT_CLIENT_POINTPARTICLES: Ent_PointParticles(); break;
669                 case ENT_CLIENT_RAINSNOW: Ent_RainOrSnow(); break;
670                 case ENT_CLIENT_LASER: Ent_Laser(); break;
671                 case ENT_CLIENT_NAGGER: Ent_Nagger(); break;
672                 case ENT_CLIENT_WAYPOINT: Ent_WaypointSprite(); break;
673                 case ENT_CLIENT_RADARLINK: Ent_RadarLink(); break;
674                 case ENT_CLIENT_PROJECTILE: Ent_Projectile(); break;
675                 case ENT_CLIENT_GIBSPLASH: Ent_GibSplash(bIsNewEntity); break;
676                 case ENT_CLIENT_DAMAGEINFO: Ent_DamageInfo(bIsNewEntity); break;
677                 case ENT_CLIENT_CASING: Ent_Casing(bIsNewEntity); break;
678                 case ENT_CLIENT_INIT: Ent_Init(); break;
679                 case ENT_CLIENT_SCORES_INFO: Ent_ScoresInfo(); break;
680                 case ENT_CLIENT_MAPVOTE: Ent_MapVote(); break;
681                 case ENT_CLIENT_CLIENTDATA: Ent_ClientData(); break;
682                 case ENT_CLIENT_RANDOMSEED: Ent_RandomSeed(); break;
683                 case ENT_CLIENT_WALL: Ent_Wall(); break;
684                 case ENT_CLIENT_MODELEFFECT: Ent_ModelEffect(bIsNewEntity); break;
685                 case ENT_CLIENT_TUBANOTE: Ent_TubaNote(bIsNewEntity); break;
686                 case ENT_CLIENT_WARPZONE: WarpZone_Read(bIsNewEntity); break;
687                 case ENT_CLIENT_WARPZONE_CAMERA: WarpZone_Camera_Read(bIsNewEntity); break;
688                 case ENT_CLIENT_WARPZONE_TELEPORTED: WarpZone_Teleported_Read(bIsNewEntity); break;
689                 case ENT_CLIENT_TRIGGER_MUSIC: Ent_ReadTriggerMusic(); break;
690                 case ENT_CLIENT_HOOK: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_HOOK); break;
691                 case ENT_CLIENT_LGBEAM: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_LGBEAM); break;
692                 case ENT_CLIENT_GAUNTLET: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_GAUNTLET); break;
693                 case ENT_CLIENT_ACCURACY: Ent_ReadAccuracy(); break;
694                 case ENT_CLIENT_AUXILIARYXHAIR: Net_AuXair2(bIsNewEntity); break;
695                 case ENT_CLIENT_TURRET: ent_turret(); break; 
696                 default:
697                         //error(strcat(_("unknown entity type in CSQC_Ent_Update: %d\n"), self.enttype));
698                         error(sprintf(_("Unknown entity type in CSQC_Ent_Update (enttype: %d, edict: %d, classname: %s)\n"), self.enttype, num_for_edict(self), self.classname));
699                         break;
700         }
701
702         time = savetime;
703 }
704 // Destructor, but does NOT deallocate the entity by calling remove(). Also
705 // used when an entity changes its type. For an entity that someone interacts
706 // with others, make sure it can no longer do so.
707 void Ent_Remove()
708 {
709         if(self.entremove)
710                 self.entremove();
711
712         self.enttype = 0;
713         self.classname = "";
714         self.draw = menu_sub_null;
715         self.entremove = menu_sub_null;
716         // TODO possibly set more stuff to defaults
717 }
718 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.
719 void CSQC_Ent_Remove()
720 {
721         if(self.enttype)
722                 Ent_Remove();
723         remove(self);
724 }
725
726 void Gamemode_Init()
727 {
728         if not(isdemo())
729         {
730                 localcmd("\n_cl_hook_gamestart ", GametypeNameFromType(gametype), "\n");
731                 calledhooks |= HOOK_START;
732         }
733 }
734 // CSQC_Parse_StuffCmd : Provides the stuffcmd string in the first parameter that the server provided.  To execute standard behavior, simply execute localcmd with the string.
735 void CSQC_Parse_StuffCmd(string strMessage)
736 {
737         localcmd(strMessage);
738 }
739 // CSQC_Parse_Print : Provides the print string in the first parameter that the server provided.  To execute standard behavior, simply execute print with the string.
740 void CSQC_Parse_Print(string strMessage)
741 {
742         print(ColorTranslateRGB(strMessage));
743 }
744
745 // CSQC_Parse_CenterPrint : Provides the centerprint_hud string in the first parameter that the server provided.
746 void CSQC_Parse_CenterPrint(string strMessage)
747 {
748         centerprint_hud(strMessage);
749 }
750
751 string notranslate_fogcmd1 = "\nfog ";
752 string notranslate_fogcmd2 = "\nr_fog_exp2 0\nr_drawfog 1\n";
753 void Fog_Force()
754 {
755         // TODO somehow thwart prvm_globalset client ...
756
757         if(forcefog != "")
758                 localcmd(strcat(notranslate_fogcmd1, forcefog, notranslate_fogcmd2));
759 }
760
761 void Gamemode_Init();
762 void Ent_ScoresInfo()
763 {
764         float i;
765         self.classname = "ent_client_scores_info";
766         gametype = ReadByte();
767         for(i = 0; i < MAX_SCORE; ++i)
768         {
769                 scores_label[i] = strzone(ReadString());
770                 scores_flags[i] = ReadByte();
771         }
772         for(i = 0; i < MAX_TEAMSCORE; ++i)
773         {
774                 teamscores_label[i] = strzone(ReadString());
775                 teamscores_flags[i] = ReadByte();
776         }
777         HUD_InitScores();
778         Gamemode_Init();
779 }
780
781 void Ent_Init()
782 {
783         self.classname = "ent_client_init";
784
785         nb_pb_period = ReadByte() / 32; //Accuracy of 1/32th
786
787         hook_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
788         hook_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
789         hook_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
790         hook_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
791         electro_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
792         electro_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
793         electro_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
794         electro_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
795         gauntlet_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
796         gauntlet_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
797         gauntlet_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
798         gauntlet_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
799
800         if(forcefog)
801                 strunzone(forcefog);
802         forcefog = strzone(ReadString());
803
804         armorblockpercent = ReadByte() / 255.0;
805
806         g_weaponswitchdelay = ReadByte() / 255.0;
807
808         g_balance_grenadelauncher_bouncefactor = ReadCoord();
809         g_balance_grenadelauncher_bouncestop = ReadCoord();
810         g_balance_electro_secondary_bouncefactor = ReadCoord();
811         g_balance_electro_secondary_bouncestop = ReadCoord();
812
813         nex_scope = !ReadByte();
814         rifle_scope = !ReadByte();
815
816         serverflags = ReadByte();
817
818         minelayer_maxmines = ReadByte();
819
820         hagar_maxrockets = ReadByte();
821
822         g_trueaim_minrange = ReadCoord();
823
824         if(!postinit)
825                 PostInit();
826 }
827
828 void Net_ReadRace()
829 {
830         float b;
831
832         b = ReadByte();
833
834         switch(b)
835         {
836                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:
837                         race_checkpoint = ReadByte();
838                         race_time = ReadInt24_t();
839                         race_previousbesttime = ReadInt24_t();
840                         if(race_previousbestname)
841                                 strunzone(race_previousbestname);
842                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));
843
844                         race_checkpointtime = time;
845
846                         if(race_checkpoint == 0 || race_checkpoint == 254)
847                         {
848                                 race_penaltyaccumulator = 0;
849                                 race_laptime = time; // valid
850                         }
851
852                         break;
853
854                 case RACE_NET_CHECKPOINT_CLEAR:
855                         race_laptime = 0;
856                         race_checkpointtime = 0;
857                         break;
858
859                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:
860                         race_laptime = ReadCoord();
861                         race_checkpointtime = -99999;
862                         // fall through
863                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:
864                         race_nextcheckpoint = ReadByte();
865
866                         race_nextbesttime = ReadInt24_t();
867                         if(race_nextbestname)
868                                 strunzone(race_nextbestname);
869                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));
870                         break;
871
872                 case RACE_NET_CHECKPOINT_HIT_RACE:
873                         race_mycheckpoint = ReadByte();
874                         race_mycheckpointtime = time;
875                         race_mycheckpointdelta = ReadInt24_t();
876                         race_mycheckpointlapsdelta = ReadByte();
877                         if(race_mycheckpointlapsdelta >= 128)
878                                 race_mycheckpointlapsdelta -= 256;
879                         if(race_mycheckpointenemy)
880                                 strunzone(race_mycheckpointenemy);
881                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
882                         break;
883
884                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:
885                         race_othercheckpoint = ReadByte();
886                         race_othercheckpointtime = time;
887                         race_othercheckpointdelta = ReadInt24_t();
888                         race_othercheckpointlapsdelta = ReadByte();
889                         if(race_othercheckpointlapsdelta >= 128)
890                                 race_othercheckpointlapsdelta -= 256;
891                         if(race_othercheckpointenemy)
892                                 strunzone(race_othercheckpointenemy);
893                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
894                         break;
895
896                 case RACE_NET_PENALTY_RACE:
897                         race_penaltyeventtime = time;
898                         race_penaltytime = ReadShort();
899                         //race_penaltyaccumulator += race_penaltytime;
900                         if(race_penaltyreason)
901                                 strunzone(race_penaltyreason);
902                         race_penaltyreason = strzone(ReadString());
903                         break;
904
905                 case RACE_NET_PENALTY_QUALIFYING:
906                         race_penaltyeventtime = time;
907                         race_penaltytime = ReadShort();
908                         race_penaltyaccumulator += race_penaltytime;
909                         if(race_penaltyreason)
910                                 strunzone(race_penaltyreason);
911                         race_penaltyreason = strzone(ReadString());
912                         break;
913
914                 case RACE_NET_SERVER_RECORD:
915                         race_server_record = ReadInt24_t();
916                         break;
917                 case RACE_NET_SPEED_AWARD:
918                         race_speedaward = ReadInt24_t();
919                         if(race_speedaward_holder)
920                                 strunzone(race_speedaward_holder);
921                         race_speedaward_holder = strzone(ReadString());
922                         break;
923                 case RACE_NET_SPEED_AWARD_BEST:
924                         race_speedaward_alltimebest = ReadInt24_t();
925                         if(race_speedaward_alltimebest_holder)
926                                 strunzone(race_speedaward_alltimebest_holder);
927                         race_speedaward_alltimebest_holder = strzone(ReadString());
928                         break;
929                 case RACE_NET_SERVER_RANKINGS:
930                         float pos, prevpos, del;
931                         pos = ReadShort();
932                         prevpos = ReadShort();
933                         del = ReadShort();
934
935                         // move other rankings out of the way
936                         float i;
937                         if (prevpos) {
938                                 for (i=prevpos-1;i>pos-1;--i) {
939                                         grecordtime[i] = grecordtime[i-1];
940                                         if(grecordholder[i])
941                                                 strunzone(grecordholder[i]);
942                                         grecordholder[i] = strzone(grecordholder[i-1]);
943                                 }
944                         } else if (del) { // a record has been deleted by the admin
945                                 for (i=pos-1; i<= RANKINGS_CNT-1; ++i) {
946                                         if (i == RANKINGS_CNT-1) { // clear out last record
947                                                 grecordtime[i] = 0;
948                                                 if (grecordholder[i])
949                                                         strunzone(grecordholder[i]);
950                                                 grecordholder[i] = string_null;
951                                         }
952                                         else {
953                                                 grecordtime[i] = grecordtime[i+1];
954                                                 if (grecordholder[i])
955                                                         strunzone(grecordholder[i]);
956                                                 grecordholder[i] = strzone(grecordholder[i+1]);
957                                         }
958                                 }
959                         } else { // player has no ranked record yet
960                                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
961                                         grecordtime[i] = grecordtime[i-1];
962                                         if(grecordholder[i])
963                                                 strunzone(grecordholder[i]);
964                                         grecordholder[i] = strzone(grecordholder[i-1]);
965                                 }
966                         }
967
968                         // store new ranking
969                         if(grecordholder[pos-1] != "")
970                                 strunzone(grecordholder[pos-1]);
971                         grecordholder[pos-1] = strzone(ReadString());
972                         grecordtime[pos-1] = ReadInt24_t();
973                         if(grecordholder[pos-1] == GetPlayerName(player_localnum))
974                                 race_myrank = pos;
975                         break;
976                 case RACE_NET_SERVER_STATUS:
977                         race_status = ReadShort();
978                         if(race_status_name)
979                                 strunzone(race_status_name);
980                         race_status_name = strzone(ReadString());
981         }
982 }
983
984 void Net_ReadSpawn()
985 {
986         zoomin_effect = 1;
987         current_viewzoom = 0.6;
988 }
989
990 void Net_TeamNagger()
991 {
992         teamnagger = 1;
993 }
994
995 void Net_ReadPingPLReport()
996 {
997         float e, pi, pl, ml;
998         e = ReadByte();
999         pi = ReadShort();
1000         pl = ReadByte();
1001         ml = ReadByte();
1002         if not(playerslots[e])
1003                 return;
1004         playerslots[e].ping = pi;
1005         playerslots[e].ping_packetloss = pl / 255.0;
1006         playerslots[e].ping_movementloss = ml / 255.0;
1007 }
1008
1009 void Net_WeaponComplain() {
1010         complain_weapon = ReadByte();
1011
1012         if(complain_weapon_name)
1013                 strunzone(complain_weapon_name);
1014         complain_weapon_name = strzone(ReadString());
1015
1016         complain_weapon_type = ReadByte();
1017
1018         complain_weapon_time = time;
1019         weapontime = time; // ping the weapon panel
1020 }
1021
1022 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.
1023 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.
1024 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.
1025 float CSQC_Parse_TempEntity()
1026 {
1027         float bHandled;
1028                 bHandled  = true;
1029         // Acquire TE ID
1030         float nTEID;
1031                 nTEID = ReadByte();
1032
1033                 // NOTE: Could just do return instead of break...
1034         switch(nTEID)
1035         {
1036                 case TE_CSQC_TARGET_MUSIC:
1037                         Net_TargetMusic();
1038                         bHandled = true;
1039                         break;
1040                 case TE_CSQC_PICTURE:
1041                         Net_MapVote_Picture();
1042                         bHandled = true;
1043                         break;
1044                 case TE_CSQC_RACE:
1045                         Net_ReadRace();
1046                         bHandled = true;
1047                         break;
1048                 case TE_CSQC_SPAWN:
1049                         Net_ReadSpawn();
1050                         bHandled = true;
1051                         break;
1052                 case TE_CSQC_ZCURVEPARTICLES:
1053                         Net_ReadZCurveParticles();
1054                         bHandled = true;
1055                         break;
1056                 case TE_CSQC_NEXGUNBEAMPARTICLE:
1057                         Net_ReadNexgunBeamParticle();
1058                         bHandled = true;
1059                         break;
1060                 case TE_CSQC_TEAMNAGGER:
1061                         Net_TeamNagger();
1062                         bHandled = true;
1063                         break;
1064                 case TE_CSQC_LIGHTNINGARC:
1065                         Net_ReadLightningarc();
1066                         bHandled = true;
1067                         break;
1068                 case TE_CSQC_PINGPLREPORT:
1069                         Net_ReadPingPLReport();
1070                         bHandled = true;
1071                         break;
1072                 case TE_CSQC_ANNOUNCE:
1073                         Announcer_Play(ReadString());
1074                         bHandled = true;
1075                         break;
1076                 case TE_CSQC_KILLNOTIFY:
1077                         HUD_KillNotify(ReadString(), ReadString(), ReadString(), ReadShort(), ReadByte());
1078                         bHandled = true;
1079                         break;
1080                 case TE_CSQC_KILLCENTERPRINT:
1081                         HUD_KillCenterprint(ReadString(), ReadString(), ReadShort(), ReadByte());
1082                         bHandled = true;
1083                         break;
1084                 case TE_CSQC_CENTERPRINT_GENERIC:
1085                         float id;
1086                         string s;
1087                         id = ReadByte();
1088                         s = ReadString();
1089                         if (id != 0 && s != "")
1090                                 centerprint_generic(id, s, ReadByte(), ReadByte());
1091                         else
1092                                 centerprint_generic(id, s, 0, 0);
1093                         bHandled = true;
1094                         break;
1095                 case TE_CSQC_WEAPONCOMPLAIN:
1096                         Net_WeaponComplain();
1097                         bHandled = true;
1098                         break;
1099                 case TE_CSQC_VEHICLESETUP:
1100                         Net_VehicleSetup();
1101                         bHandled = true;
1102                         break;
1103                 default:
1104                         // No special logic for this temporary entity; return 0 so the engine can handle it
1105                         bHandled = false;
1106                         break;
1107         }
1108
1109         return bHandled;
1110 }
1111
1112 string getcommandkey(string text, string command)
1113 {
1114         string keys;
1115         float n, j, k, l;
1116
1117         if (!autocvar_hud_showbinds)
1118                 return text;
1119
1120         keys = db_get(binddb, command);
1121         if (!keys)
1122         {
1123                 n = tokenize(findkeysforcommand(command, 0)); // uses '...' strings
1124                 for(j = 0; j < n; ++j)
1125                 {
1126                         k = stof(argv(j));
1127                         if(k != -1)
1128                         {
1129                                 if ("" == keys)
1130                                         keys = keynumtostring(k);
1131                                 else
1132                                         keys = strcat(keys, ", ", keynumtostring(k));
1133
1134                                 ++l;
1135                                 if (autocvar_hud_showbinds_limit > 0 && autocvar_hud_showbinds_limit >= l) break;
1136                         }
1137
1138                 }
1139                 db_put(binddb, command, keys);
1140         }
1141
1142         if ("" == keys) {
1143                 if (autocvar_hud_showbinds > 1)
1144                         return sprintf(_("%s (not bound)"), text);
1145                 else
1146                         return text;
1147         }
1148         else if (autocvar_hud_showbinds > 1)
1149                 return sprintf(_("%s (%s)"), text, keys);
1150         else
1151                 return keys;
1152 }