]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/main.qc
Simplify headers
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / main.qc
1 #include "main.qh"
2 #include "_all.qh"
3
4 #include "casings.qh"
5 #include "csqcmodel_hooks.qh"
6 #include "damage.qh"
7 #include "effects.qh"
8 #include "gibs.qh"
9 #include "hook.qh"
10 #include "hud.qh"
11 #include "hud_config.qh"
12 #include "laser.qh"
13 #include "mapvoting.qh"
14 #include "modeleffects.qh"
15 #include "particles.qh"
16 #include "prandom.qh"
17 #include "scoreboard.qh"
18 #include "shownames.qh"
19 #include "sortlist.qh"
20 #include "tturrets.qh"
21 #include "tuba.qh"
22 #include "t_items.qh"
23 #include "wall.qh"
24 #include "waypointsprites.qh"
25
26 #include "../common/vehicles/all.qh"
27
28 #include "weapons/projectile.qh"
29
30 #include "../common/buffs.qh"
31 #include "../common/deathtypes.qh"
32 #include "../common/effects.qh"
33 #include "../common/mapinfo.qh"
34 #include "../common/monsters/all.qh"
35 #include "../common/nades.qh"
36 #include "../common/net_notice.qh"
37 #include "../common/notifications.qh"
38 #include "../common/stats.qh"
39 #include "../common/teams.qh"
40
41 #include "../common/items/all.qh"
42
43 #include "../common/mutators/base.qh"
44
45 #include "../common/weapons/all.qh"
46
47 #include "../csqcmodellib/cl_model.qh"
48 #include "../csqcmodellib/interpolate.qh"
49
50 #include "../common/triggers/include.qh"
51
52 #include "../warpzonelib/client.qh"
53
54 // --------------------------------------------------------------------------
55 // BEGIN REQUIRED CSQC FUNCTIONS
56 //include "main.qh"
57
58 entity clearentity_ent;
59 void clearentity(entity e)
60 {
61         if (!clearentity_ent)
62         {
63                 clearentity_ent = spawn();
64                 clearentity_ent.classname = "clearentity";
65         }
66         int n = e.entnum;
67         copyentity(clearentity_ent, e);
68         e.entnum = n;
69 }
70
71 #define DP_CSQC_ENTITY_REMOVE_IS_B0RKED
72 void menu_show_error()
73 {
74         drawstring('0 200 0', _("ERROR - MENU IS VISIBLE BUT NO MENU WAS DEFINED!"), '8 8 0', '1 0 0', 1, 0);
75 }
76
77 // CSQC_Init : Called every time the CSQC code is initialized (essentially at map load)
78 // Useful for precaching things
79
80 void menu_sub_null()
81 {
82 }
83
84 string forcefog;
85 void WaypointSprite_Load();
86 void ConsoleCommand_macro_init();
87 void CSQC_Init(void)
88 {
89         prvm_language = cvar_string("prvm_language");
90
91 #ifdef WATERMARK
92         dprintf("^4CSQC Build information: ^1%s\n", WATERMARK);
93 #endif
94
95         int i;
96
97         binddb = db_create();
98         tempdb = db_create();
99         ClientProgsDB = db_load("client.db");
100         compressShortVector_init();
101
102         draw_endBoldFont();
103         menu_visible = false;
104         menu_show = menu_show_error;
105         menu_action = func_null;
106
107         for(i = 0; i < 255; ++i)
108                 if(getplayerkeyvalue(i, "viewentity") == "")
109                         break;
110         maxclients = i;
111
112         //registercommand("hud_configure");
113         //registercommand("hud_save");
114         //registercommand("menu_action");
115
116         ConsoleCommand_macro_init();
117
118         registercvar("hud_usecsqc", "1");
119         registercvar("scoreboard_columns", "default");
120
121         registercvar("cl_nade_type", "3");
122         registercvar("cl_pokenade_type", "zombie");
123
124         registercvar("cl_jumpspeedcap_min", "");
125         registercvar("cl_jumpspeedcap_max", "");
126
127         gametype = 0;
128
129         // hud_fields uses strunzone on the titles!
130         for(i = 0; i < MAX_HUD_FIELDS; ++i)
131                 hud_title[i] = strzone("(null)");
132
133         Cmd_HUD_SetFields(0);
134
135         postinit = false;
136
137         calledhooks = 0;
138
139         teams = Sort_Spawn();
140         players = Sort_Spawn();
141
142         GetTeam(NUM_SPECTATOR, true); // add specs first
143
144         // needs to be done so early because of the constants they create
145         static_init();
146         CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
147         CALL_ACCUMULATED_FUNCTION(RegisterDeathtypes);
148         CALL_ACCUMULATED_FUNCTION(RegisterHUD_Panels);
149         CALL_ACCUMULATED_FUNCTION(RegisterEffects);
150         CALL_ACCUMULATED_FUNCTION(RegisterVehicles);
151
152         WaypointSprite_Load();
153
154         // precaches
155         precache_model("null");
156         precache_sound("misc/hit.wav");
157         precache_sound("misc/typehit.wav");
158
159         Projectile_Precache();
160         Hook_Precache();
161         GibSplash_Precache();
162         Casings_Precache();
163         turrets_precache();
164         Tuba_Precache();
165         CSQCPlayer_Precache();
166
167         if(autocvar_cl_reticle)
168         {
169                 precache_pic("gfx/reticle_normal");
170                 // weapon reticles are precached in weapon files
171         }
172
173         get_mi_min_max_texcoords(1); // try the CLEVER way first
174         minimapname = strcat("gfx/", mi_shortname, "_radar.tga");
175         shortmapname = mi_shortname;
176
177         if(precache_pic(minimapname) == "")
178         {
179                 // but maybe we have a non-clever minimap
180                 minimapname = strcat("gfx/", mi_shortname, "_mini.tga");
181                 if(precache_pic(minimapname) == "")
182                         minimapname = ""; // FAIL
183                 else
184                         get_mi_min_max_texcoords(0); // load new texcoords
185         }
186
187         mi_center = (mi_min + mi_max) * 0.5;
188         mi_scale = mi_max - mi_min;
189         minimapname = strzone(minimapname);
190
191         WarpZone_Init();
192
193         hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
194         draw_currentSkin = strzone(strcat("gfx/menu/", cvar_string("menu_skin")));
195 }
196
197 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)
198 void Shutdown(void)
199 {
200         WarpZone_Shutdown();
201
202         remove(teams);
203         remove(players);
204         db_close(binddb);
205         db_close(tempdb);
206         if(autocvar_cl_db_saveasdump)
207                 db_dump(ClientProgsDB, "client.db");
208         else
209                 db_save(ClientProgsDB, "client.db");
210         db_close(ClientProgsDB);
211
212         if(camera_active)
213                 cvar_set("chase_active",ftos(chase_active_backup));
214
215         // unset the event chasecam's chase_active
216         if(autocvar_chase_active < 0)
217                 cvar_set("chase_active", "0");
218
219         if (!isdemo())
220         {
221                 if (!(calledhooks & HOOK_START))
222                         localcmd("\n_cl_hook_gamestart nop\n");
223                 if (!(calledhooks & HOOK_END))
224                         localcmd("\ncl_hook_gameend\n");
225         }
226 }
227
228 .float has_team;
229 float SetTeam(entity o, int Team)
230 {
231         entity tm;
232         if(teamplay)
233         {
234                 switch(Team)
235                 {
236                         case -1:
237                         case NUM_TEAM_1:
238                         case NUM_TEAM_2:
239                         case NUM_TEAM_3:
240                         case NUM_TEAM_4:
241                                 break;
242                         default:
243                                 if(GetTeam(Team, false) == world)
244                                 {
245                                         dprintf("trying to switch to unsupported team %d\n", Team);
246                                         Team = NUM_SPECTATOR;
247                                 }
248                                 break;
249                 }
250         }
251         else
252         {
253                 switch(Team)
254                 {
255                         case -1:
256                         case 0:
257                                 break;
258                         default:
259                                 if(GetTeam(Team, false) == world)
260                                 {
261                                         dprintf("trying to switch to unsupported team %d\n", Team);
262                                         Team = NUM_SPECTATOR;
263                                 }
264                                 break;
265                 }
266         }
267         if(Team == -1) // leave
268         {
269                 if(o.has_team)
270                 {
271                         tm = GetTeam(o.team, false);
272                         tm.team_size -= 1;
273                         o.has_team = 0;
274                         return true;
275                 }
276         }
277         else
278         {
279                 if (!o.has_team)
280                 {
281                         o.team = Team;
282                         tm = GetTeam(Team, true);
283                         tm.team_size += 1;
284                         o.has_team = 1;
285                         return true;
286                 }
287                 else if(Team != o.team)
288                 {
289                         tm = GetTeam(o.team, false);
290                         tm.team_size -= 1;
291                         o.team = Team;
292                         tm = GetTeam(Team, true);
293                         tm.team_size += 1;
294                         return true;
295                 }
296         }
297         return false;
298 }
299
300 void Playerchecker_Think()
301 {
302     int i;
303         entity e;
304         for(i = 0; i < maxclients; ++i)
305         {
306                 e = playerslots[i];
307                 if(GetPlayerName(i) == "")
308                 {
309                         if(e.sort_prev)
310                         {
311                                 // player disconnected
312                                 SetTeam(e, -1);
313                                 RemovePlayer(e);
314                                 e.sort_prev = world;
315                                 //e.gotscores = 0;
316                         }
317                 }
318                 else
319                 {
320                         if (!e.sort_prev)
321                         {
322                                 // player connected
323                                 if (!e)
324                                         playerslots[i] = e = spawn();
325                                 e.sv_entnum = i;
326                                 e.ping = 0;
327                                 e.ping_packetloss = 0;
328                                 e.ping_movementloss = 0;
329                                 //e.gotscores = 0; // we might already have the scores...
330                                 SetTeam(e, GetPlayerColor(i)); // will not hurt; later updates come with HUD_UpdatePlayerTeams
331                                 RegisterPlayer(e);
332                                 HUD_UpdatePlayerPos(e);
333                         }
334                 }
335         }
336         self.nextthink = time + 0.2;
337 }
338
339 void Porto_Init();
340 void TrueAim_Init();
341 void PostInit(void)
342 {
343         entity playerchecker;
344         playerchecker = spawn();
345         playerchecker.think = Playerchecker_Think;
346         playerchecker.nextthink = time + 0.2;
347
348         Porto_Init();
349         TrueAim_Init();
350
351         postinit = true;
352 }
353
354 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.
355 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.
356 // All keys are in ascii.
357 // bInputType = 0 is key pressed, 1 is key released, 2 and 3 are mouse input.
358 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.
359 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.
360 // In the case of mouse input after a setcursormode(1) call, nPrimary is xpos, nSecondary is ypos.
361 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)
362 {
363         float bSkipKey;
364         bSkipKey = false;
365
366         if (HUD_Panel_InputEvent(bInputType, nPrimary, nSecondary))
367                 return true;
368
369         if (MapVote_InputEvent(bInputType, nPrimary, nSecondary))
370                 return true;
371
372         if(menu_visible && menu_action)
373                 if(menu_action(bInputType, nPrimary, nSecondary))
374                         return true;
375
376         return bSkipKey;
377 }
378
379 // END REQUIRED CSQC FUNCTIONS
380 // --------------------------------------------------------------------------
381
382 // --------------------------------------------------------------------------
383 // BEGIN OPTIONAL CSQC FUNCTIONS
384
385 void Ent_RemoveEntCS()
386 {
387         entcs_receiver[self.sv_entnum] = world;
388 }
389 void Ent_ReadEntCS()
390 {
391     int sf;
392         InterpolateOrigin_Undo();
393
394         self.classname = "entcs_receiver";
395         sf = ReadByte();
396
397         if(sf & 1)
398                 self.sv_entnum = ReadByte();
399         if(sf & 2)
400         {
401                 self.origin_x = ReadShort();
402                 self.origin_y = ReadShort();
403                 self.origin_z = ReadShort();
404                 setorigin(self, self.origin);
405         }
406         if(sf & 4)
407         {
408                 self.angles_y = ReadByte() * 360.0 / 256;
409                 self.angles_x = self.angles_z = 0;
410         }
411         if(sf & 8)
412                 self.healthvalue = ReadByte() * 10;
413         if(sf & 16)
414                 self.armorvalue = ReadByte() * 10;
415
416         entcs_receiver[self.sv_entnum] = self;
417         self.entremove = Ent_RemoveEntCS;
418         self.iflags |= IFLAG_ORIGIN;
419
420         InterpolateOrigin_Note();
421 }
422
423 void Ent_Remove();
424
425 void Ent_RemovePlayerScore()
426 {
427         if(self.owner) {
428                 SetTeam(self.owner, -1);
429                 self.owner.gotscores = 0;
430                 for(int i = 0; i < MAX_SCORE; ++i) {
431                         self.owner.(scores[i]) = 0; // clear all scores
432                 }
433         }
434 }
435
436 void Ent_ReadPlayerScore()
437 {
438         int i, n;
439         bool isNew;
440         entity o;
441
442         // damnit -.- don't want to go change every single .sv_entnum in hud.qc AGAIN
443         // (no I've never heard of M-x replace-string, sed, or anything like that)
444         isNew = !self.owner; // workaround for DP bug
445         n = ReadByte()-1;
446
447 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
448         if(!isNew && n != self.sv_entnum)
449         {
450                 //print("A CSQC entity changed its owner!\n");
451                 printf("A CSQC entity changed its owner! (edict: %d, classname: %s)\n", num_for_edict(self), self.classname);
452                 isNew = true;
453                 Ent_Remove();
454                 self.enttype = ENT_CLIENT_SCORES;
455         }
456 #endif
457
458         self.sv_entnum = n;
459
460         if (!(playerslots[self.sv_entnum]))
461                 playerslots[self.sv_entnum] = spawn();
462         o = self.owner = playerslots[self.sv_entnum];
463         o.sv_entnum = self.sv_entnum;
464         o.gotscores = 1;
465
466         //if (!o.sort_prev)
467         //      RegisterPlayer(o);
468         //playerchecker will do this for us later, if it has not already done so
469
470     int sf, lf;
471 #if MAX_SCORE <= 8
472         sf = ReadByte();
473         lf = ReadByte();
474 #else
475         sf = ReadShort();
476         lf = ReadShort();
477 #endif
478     int p;
479         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
480                 if(sf & p)
481                 {
482                         if(lf & p)
483                                 o.(scores[i]) = ReadInt24_t();
484                         else
485                                 o.(scores[i]) = ReadChar();
486                 }
487
488         if(o.sort_prev)
489                 HUD_UpdatePlayerPos(o); // if not registered, we cannot do this yet!
490
491         self.entremove = Ent_RemovePlayerScore;
492 }
493
494 void Ent_ReadTeamScore()
495 {
496         int i;
497         entity o;
498
499         self.team = ReadByte();
500         o = self.owner = GetTeam(self.team, true); // these team numbers can always be trusted
501
502     int sf, lf;
503 #if MAX_TEAMSCORE <= 8
504         sf = ReadByte();
505         lf = ReadByte();
506 #else
507         sf = ReadShort();
508         lf = ReadShort();
509 #endif
510         int p;
511         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
512                 if(sf & p)
513                 {
514                         if(lf & p)
515                                 o.(teamscores[i]) = ReadInt24_t();
516                         else
517                                 o.(teamscores[i]) = ReadChar();
518                 }
519
520         HUD_UpdateTeamPos(o);
521 }
522
523 void Ent_ClientData()
524 {
525         float newspectatee_status;
526
527     int f = ReadByte();
528
529         scoreboard_showscores_force = (f & 1);
530
531         if(f & 2)
532         {
533                 newspectatee_status = ReadByte();
534                 if(newspectatee_status == player_localnum + 1)
535                         newspectatee_status = -1; // observing
536         }
537         else
538                 newspectatee_status = 0;
539
540         spectatorbutton_zoom = (f & 4);
541
542         if(f & 8)
543         {
544                 angles_held_status = 1;
545                 angles_held.x = ReadAngle();
546                 angles_held.y = ReadAngle();
547                 angles_held.z = 0;
548         }
549         else
550                 angles_held_status = 0;
551
552         if(newspectatee_status != spectatee_status)
553         {
554                 // clear race stuff
555                 race_laptime = 0;
556                 race_checkpointtime = 0;
557         }
558         if (autocvar_hud_panel_healtharmor_progressbar_gfx)
559         {
560                 if ( (spectatee_status == -1 && newspectatee_status > 0) //before observing, now spectating
561                   || (spectatee_status > 0 && newspectatee_status > 0 && spectatee_status != newspectatee_status) //changed spectated player
562                 )
563                         prev_p_health = -1;
564                 else if(spectatee_status && !newspectatee_status) //before observing/spectating, now playing
565                         prev_health = -1;
566         }
567         spectatee_status = newspectatee_status;
568
569         // we could get rid of spectatee_status, and derive it from player_localentnum and player_localnum
570 }
571
572 void Ent_Nagger()
573 {
574     int i, j, b, f;
575
576     int nags = ReadByte(); // NAGS NAGS NAGS NAGS NAGS NAGS NADZ NAGS NAGS NAGS
577
578         if(!(nags & 4))
579         {
580                 if(vote_called_vote)
581                         strunzone(vote_called_vote);
582                 vote_called_vote = string_null;
583                 vote_active = 0;
584         }
585         else
586         {
587                 vote_active = 1;
588         }
589
590         if(nags & 64)
591         {
592                 vote_yescount = ReadByte();
593                 vote_nocount = ReadByte();
594                 vote_needed = ReadByte();
595                 vote_highlighted = ReadChar();
596         }
597
598         if(nags & 128)
599         {
600                 if(vote_called_vote)
601                         strunzone(vote_called_vote);
602                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));
603         }
604
605         if(nags & 1)
606         {
607                 for(j = 0; j < maxclients; ++j)
608                         if(playerslots[j])
609                                 playerslots[j].ready = 1;
610                 for(i = 1; i <= maxclients; i += 8)
611                 {
612                         f = ReadByte();
613                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
614                                 if (!(f & b))
615                                         if(playerslots[j])
616                                                 playerslots[j].ready = 0;
617                 }
618         }
619
620         ready_waiting = (nags & 1);
621         ready_waiting_for_me = (nags & 2);
622         vote_waiting = (nags & 4);
623         vote_waiting_for_me = (nags & 8);
624         warmup_stage = (nags & 16);
625 }
626
627 void Ent_EliminatedPlayers()
628 {
629     int i, j, b, f;
630
631     int sf = ReadByte();
632         if(sf & 1)
633         {
634                 for(j = 0; j < maxclients; ++j)
635                         if(playerslots[j])
636                                 playerslots[j].eliminated = 1;
637                 for(i = 1; i <= maxclients; i += 8)
638                 {
639                         f = ReadByte();
640                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
641                                 if (!(f & b))
642                                         if(playerslots[j])
643                                                 playerslots[j].eliminated = 0;
644                 }
645         }
646 }
647
648 void Ent_RandomSeed()
649 {
650         float s;
651         prandom_debug();
652         s = ReadShort();
653         psrandom(s);
654 }
655
656 void Ent_ReadAccuracy(void)
657 {
658     int f, w;
659     int sf = ReadInt24_t();
660         if(sf == 0)
661         {
662                 for(w = 0; w <= WEP_LAST - WEP_FIRST; ++w)
663                         weapon_accuracy[w] = -1;
664                 return;
665         }
666
667         for(w = 0, f = 1; w <= WEP_LAST - WEP_FIRST; ++w)
668         {
669                 if(sf & f)
670                 {
671             int b = ReadByte();
672                         if(b == 0)
673                                 weapon_accuracy[w] = -1;
674                         else if(b == 255)
675                                 weapon_accuracy[w] = 1.0; // no better error handling yet, sorry
676                         else
677                                 weapon_accuracy[w] = (b - 1.0) / 100.0;
678                 }
679                 if(f == 0x800000)
680                         f = 1;
681                 else
682                         f *= 2;
683         }
684 }
685
686 void Spawn_Draw(void)
687 {
688         pointparticles(self.cnt, self.origin + '0 0 28', '0 0 2', bound(0, frametime, 0.1));
689 }
690
691 void Ent_ReadSpawnPoint(float is_new) // entity for spawnpoint
692 {
693         float teamnum = (ReadByte() - 1);
694         vector spn_origin;
695         spn_origin.x = ReadShort();
696         spn_origin.y = ReadShort();
697         spn_origin.z = ReadShort();
698
699         if(is_new)
700         {
701                 self.origin = spn_origin;
702                 setsize(self, PL_MIN_CONST, PL_MAX_CONST);
703                 droptofloor();
704
705                 /*if(autocvar_cl_spawn_point_model) // needs a model first
706                 {
707                         self.mdl = "models/spawnpoint.md3";
708                         self.colormod = Team_ColorRGB(teamnum);
709                         precache_model(self.mdl);
710                         setmodel(self, self.mdl);
711                         self.drawmask = MASK_NORMAL;
712                         //self.movetype = MOVETYPE_NOCLIP;
713                         //self.draw = Spawn_Draw;
714                 }*/
715                 if(autocvar_cl_spawn_point_particles)
716                 {
717                         if((serverflags & SERVERFLAG_TEAMPLAY))
718                         {
719                                 switch(teamnum)
720                                 {
721                                         case NUM_TEAM_1: self.cnt = particleeffectnum("spawn_point_red"); break;
722                                         case NUM_TEAM_2: self.cnt = particleeffectnum("spawn_point_blue"); break;
723                                         case NUM_TEAM_3: self.cnt = particleeffectnum("spawn_point_yellow"); break;
724                                         case NUM_TEAM_4: self.cnt = particleeffectnum("spawn_point_pink"); break;
725                                         default: self.cnt = particleeffectnum("spawn_point_neutral"); break;
726                                 }
727                         }
728                         else { self.cnt = particleeffectnum("spawn_point_neutral"); }
729
730                         self.draw = Spawn_Draw;
731                 }
732         }
733
734         //printf("Ent_ReadSpawnPoint(is_new = %d); origin = %s, team = %d, effect = %d\n", is_new, vtos(self.origin), teamnum, self.cnt);
735 }
736
737 void Ent_ReadSpawnEvent(float is_new)
738 {
739         // If entnum is 0, ONLY do the local spawn actions
740         // this way the server can disable the sending of
741         // spawn origin or such to clients if wanted.
742         float entnum = ReadByte();
743
744         if(entnum)
745         {
746                 self.origin_x = ReadShort();
747                 self.origin_y = ReadShort();
748                 self.origin_z = ReadShort();
749
750                 if(is_new)
751                 {
752                         float teamnum = GetPlayerColor(entnum - 1);
753
754                         if(autocvar_cl_spawn_event_particles)
755                         {
756                                 switch(teamnum)
757                                 {
758                                         case NUM_TEAM_1: pointparticles(particleeffectnum("spawn_event_red"), self.origin, '0 0 0', 1); break;
759                                         case NUM_TEAM_2: pointparticles(particleeffectnum("spawn_event_blue"), self.origin, '0 0 0', 1); break;
760                                         case NUM_TEAM_3: pointparticles(particleeffectnum("spawn_event_yellow"), self.origin, '0 0 0', 1); break;
761                                         case NUM_TEAM_4: pointparticles(particleeffectnum("spawn_event_pink"), self.origin, '0 0 0', 1); break;
762                                         default: pointparticles(particleeffectnum("spawn_event_neutral"), self.origin, '0 0 0', 1); break;
763                                 }
764                         }
765                         if(autocvar_cl_spawn_event_sound)
766                         {
767                                 sound(self, CH_TRIGGER, "misc/spawn.wav", VOL_BASE, ATTEN_NORM);
768                         }
769                 }
770         }
771
772         // local spawn actions
773         if(is_new && (!entnum || (entnum == player_localentnum)))
774         {
775                 zoomin_effect = 1;
776                 current_viewzoom = (1 / bound(1, autocvar_cl_spawnzoom_factor, 16));
777
778                 if(autocvar_cl_unpress_zoom_on_spawn)
779                 {
780                         localcmd("-zoom\n");
781                         button_zoom = false;
782                 }
783         }
784
785         //printf("Ent_ReadSpawnEvent(is_new = %d); origin = %s, entnum = %d, localentnum = %d\n", is_new, vtos(self.origin), entnum, player_localentnum);
786 }
787
788 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.
789 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.
790 void Ent_RadarLink();
791 void Ent_Init();
792 void Ent_ScoresInfo();
793 void CSQC_Ent_Update(float bIsNewEntity)
794 {
795         float t;
796         float savetime;
797         t = ReadByte();
798
799         if(autocvar_developer_csqcentities)
800                 printf("CSQC_Ent_Update(%d) with self=%i self.entnum=%d self.enttype=%d t=%d\n", bIsNewEntity, self, self.entnum, self.enttype, t);
801
802         // set up the "time" global for received entities to be correct for interpolation purposes
803         savetime = time;
804         if(servertime)
805         {
806                 time = servertime;
807         }
808         else
809         {
810                 serverprevtime = time;
811                 serverdeltatime = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
812                 time = serverprevtime + serverdeltatime;
813         }
814
815 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
816         if(self.enttype)
817         {
818                 if(t != self.enttype || bIsNewEntity)
819                 {
820                         //print("A CSQC entity changed its type!\n");
821                         printf("A CSQC entity changed its type! (edict: %d, server: %d, type: %d -> %d)\n", num_for_edict(self), self.entnum, self.enttype, t);
822                         Ent_Remove();
823                         clearentity(self);
824                         bIsNewEntity = 1;
825                 }
826         }
827         else
828         {
829                 if(!bIsNewEntity)
830                 {
831                         printf("A CSQC entity appeared out of nowhere! (edict: %d, server: %d, type: %d)\n", num_for_edict(self), self.entnum, t);
832                         bIsNewEntity = 1;
833                 }
834         }
835 #endif
836         self.enttype = t;
837         switch(t)
838         {
839                 case ENT_CLIENT_ENTCS: Ent_ReadEntCS(); break;
840                 case ENT_CLIENT_SCORES: Ent_ReadPlayerScore(); break;
841                 case ENT_CLIENT_TEAMSCORES: Ent_ReadTeamScore(); break;
842                 case ENT_CLIENT_POINTPARTICLES: Ent_PointParticles(); break;
843                 case ENT_CLIENT_RAINSNOW: Ent_RainOrSnow(); break;
844                 case ENT_CLIENT_LASER: Ent_Laser(); break;
845                 case ENT_CLIENT_NAGGER: Ent_Nagger(); break;
846                 case ENT_CLIENT_ELIMINATEDPLAYERS: Ent_EliminatedPlayers(); break;
847                 case ENT_CLIENT_WAYPOINT: Ent_WaypointSprite(); break;
848                 case ENT_CLIENT_RADARLINK: Ent_RadarLink(); break;
849                 case ENT_CLIENT_PROJECTILE: Ent_Projectile(); break;
850                 case ENT_CLIENT_GIBSPLASH: Ent_GibSplash(bIsNewEntity); break;
851                 case ENT_CLIENT_DAMAGEINFO: Ent_DamageInfo(bIsNewEntity); break;
852                 case ENT_CLIENT_CASING: Ent_Casing(bIsNewEntity); break;
853                 case ENT_CLIENT_INIT: Ent_Init(); break;
854                 case ENT_CLIENT_SCORES_INFO: Ent_ScoresInfo(); break;
855                 case ENT_CLIENT_MAPVOTE: Ent_MapVote(); break;
856                 case ENT_CLIENT_CLIENTDATA: Ent_ClientData(); break;
857                 case ENT_CLIENT_RANDOMSEED: Ent_RandomSeed(); break;
858                 case ENT_CLIENT_WALL: Ent_Wall(); break;
859                 case ENT_CLIENT_MODELEFFECT: Ent_ModelEffect(bIsNewEntity); break;
860                 case ENT_CLIENT_TUBANOTE: Ent_TubaNote(bIsNewEntity); break;
861                 case ENT_CLIENT_WARPZONE: WarpZone_Read(bIsNewEntity); break;
862                 case ENT_CLIENT_WARPZONE_CAMERA: WarpZone_Camera_Read(bIsNewEntity); break;
863                 case ENT_CLIENT_WARPZONE_TELEPORTED: WarpZone_Teleported_Read(bIsNewEntity); break;
864                 case ENT_CLIENT_TRIGGER_MUSIC: Ent_ReadTriggerMusic(); break;
865                 case ENT_CLIENT_HOOK: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_HOOK); break;
866                 case ENT_CLIENT_INVENTORY: Inventory_Read(self); break;
867                 case ENT_CLIENT_ARC_BEAM: Ent_ReadArcBeam(bIsNewEntity); break;
868                 case ENT_CLIENT_ACCURACY: Ent_ReadAccuracy(); break;
869                 case ENT_CLIENT_AUXILIARYXHAIR: Net_AuXair2(bIsNewEntity); break;
870                 case ENT_CLIENT_TURRET: ent_turret(); break;
871                 case ENT_CLIENT_MODEL: CSQCModel_Read(bIsNewEntity); break;
872                 case ENT_CLIENT_ITEM: ItemRead(bIsNewEntity); break;
873                 case ENT_CLIENT_BUMBLE_RAYGUN: bumble_raygun_read(bIsNewEntity); break;
874                 case ENT_CLIENT_SPAWNPOINT: Ent_ReadSpawnPoint(bIsNewEntity); break;
875                 case ENT_CLIENT_SPAWNEVENT: Ent_ReadSpawnEvent(bIsNewEntity); break;
876                 case ENT_CLIENT_NOTIFICATION: Read_Notification(bIsNewEntity); break;
877                 case ENT_CLIENT_HEALING_ORB: ent_healer(); break;
878                 case ENT_CLIENT_VIEWLOC: ent_viewloc(); break;
879                 case ENT_CLIENT_VIEWLOC_TRIGGER: ent_viewloc_trigger(); break;
880                 case ENT_CLIENT_LADDER: ent_func_ladder(); break;
881                 case ENT_CLIENT_TRIGGER_PUSH: ent_trigger_push(); break;
882                 case ENT_CLIENT_TARGET_PUSH: ent_target_push(); break;
883                 case ENT_CLIENT_CONVEYOR: ent_conveyor(); break;
884                 case ENT_CLIENT_DOOR: ent_door(); break;
885                 case ENT_CLIENT_PLAT: ent_plat(); break;
886                 case ENT_CLIENT_SWAMP: ent_swamp(); break;
887                 case ENT_CLIENT_CORNER: ent_corner(); break;
888                 case ENT_CLIENT_KEYLOCK: ent_keylock(); break;
889                 case ENT_CLIENT_TRAIN: ent_train(); break;
890                 case ENT_CLIENT_TRIGGER_IMPULSE: ent_trigger_impulse(); break;
891                 case ENT_CLIENT_EFFECT: Read_Effect(bIsNewEntity); break;
892
893                 default:
894                         //error(strcat(_("unknown entity type in CSQC_Ent_Update: %d\n"), self.enttype));
895                         error(sprintf("Unknown entity type in CSQC_Ent_Update (enttype: %d, edict: %d, classname: %s)\n", self.enttype, num_for_edict(self), self.classname));
896                         break;
897         }
898
899         time = savetime;
900 }
901 // Destructor, but does NOT deallocate the entity by calling remove(). Also
902 // used when an entity changes its type. For an entity that someone interacts
903 // with others, make sure it can no longer do so.
904 void Ent_Remove()
905 {
906         if(self.entremove)
907                 self.entremove();
908
909         if(self.skeletonindex)
910         {
911                 skel_delete(self.skeletonindex);
912                 self.skeletonindex = 0;
913         }
914
915         if(self.snd_looping > 0)
916         {
917                 sound(self, self.snd_looping, "misc/null.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
918                 self.snd_looping = 0;
919         }
920
921         self.enttype = 0;
922         self.classname = "";
923         self.draw = menu_sub_null;
924         self.entremove = menu_sub_null;
925         // TODO possibly set more stuff to defaults
926 }
927 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.
928 void CSQC_Ent_Remove()
929 {
930         if(autocvar_developer_csqcentities)
931                 printf("CSQC_Ent_Remove() with self=%i self.entnum=%d self.enttype=%d\n", self, self.entnum, self.enttype);
932
933         if(wasfreed(self))
934         {
935                 print("WARNING: CSQC_Ent_Remove called for already removed entity. Packet loss?\n");
936                 return;
937         }
938         if(self.enttype)
939                 Ent_Remove();
940         remove(self);
941 }
942
943 void Gamemode_Init()
944 {
945         if (!isdemo())
946         {
947                 if(!(calledhooks & HOOK_START))
948                         localcmd("\n_cl_hook_gamestart ", MapInfo_Type_ToString(gametype), "\n");
949                 calledhooks |= HOOK_START;
950         }
951 }
952 // 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.
953 void CSQC_Parse_StuffCmd(string strMessage)
954 {
955         if(autocvar_developer_csqcentities)
956                 printf("CSQC_Parse_StuffCmd(\"%s\")\n", strMessage);
957
958         localcmd(strMessage);
959 }
960 // 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.
961 void CSQC_Parse_Print(string strMessage)
962 {
963         if(autocvar_developer_csqcentities)
964                 printf("CSQC_Parse_Print(\"%s\")\n", strMessage);
965
966         print(ColorTranslateRGB(strMessage));
967 }
968
969 // CSQC_Parse_CenterPrint : Provides the centerprint_hud string in the first parameter that the server provided.
970 void CSQC_Parse_CenterPrint(string strMessage)
971 {
972         if(autocvar_developer_csqcentities)
973                 printf("CSQC_Parse_CenterPrint(\"%s\")\n", strMessage);
974
975         centerprint_hud(strMessage);
976 }
977
978 string notranslate_fogcmd1 = "\nfog ";
979 string notranslate_fogcmd2 = "\nr_fog_exp2 0\nr_drawfog 1\n";
980 void Fog_Force()
981 {
982         // TODO somehow thwart prvm_globalset client ...
983
984         if(autocvar_cl_orthoview && autocvar_cl_orthoview_nofog)
985                 { localcmd("\nr_drawfog 0\n"); }
986         else if(forcefog != "")
987                 { localcmd(strcat(notranslate_fogcmd1, forcefog, notranslate_fogcmd2)); }
988 }
989
990 void Gamemode_Init();
991 void Ent_ScoresInfo()
992 {
993     int i;
994         self.classname = "ent_client_scores_info";
995         gametype = ReadInt24_t();
996         HUD_ModIcons_SetFunc();
997         for(i = 0; i < MAX_SCORE; ++i)
998         {
999                 if(scores_label[i])
1000                         strunzone(scores_label[i]);
1001                 scores_label[i] = strzone(ReadString());
1002                 scores_flags[i] = ReadByte();
1003         }
1004         for(i = 0; i < MAX_TEAMSCORE; ++i)
1005         {
1006                 if(teamscores_label[i])
1007                         strunzone(teamscores_label[i]);
1008                 teamscores_label[i] = strzone(ReadString());
1009                 teamscores_flags[i] = ReadByte();
1010         }
1011         HUD_InitScores();
1012         Gamemode_Init();
1013 }
1014
1015 void Ent_Init()
1016 {
1017         self.classname = "ent_client_init";
1018
1019         nb_pb_period = ReadByte() / 32; //Accuracy of 1/32th
1020
1021         hook_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
1022         hook_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
1023         hook_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
1024         hook_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
1025         arc_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
1026         arc_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
1027         arc_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
1028         arc_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
1029
1030         if(forcefog)
1031                 strunzone(forcefog);
1032         forcefog = strzone(ReadString());
1033
1034         armorblockpercent = ReadByte() / 255.0;
1035
1036         g_balance_mortar_bouncefactor = ReadCoord();
1037         g_balance_mortar_bouncestop = ReadCoord();
1038         g_balance_electro_secondary_bouncefactor = ReadCoord();
1039         g_balance_electro_secondary_bouncestop = ReadCoord();
1040
1041         vortex_scope = !ReadByte();
1042         rifle_scope = !ReadByte();
1043
1044         serverflags = ReadByte();
1045
1046         minelayer_maxmines = ReadByte();
1047
1048         hagar_maxrockets = ReadByte();
1049
1050         g_trueaim_minrange = ReadCoord();
1051         g_balance_porto_secondary = ReadByte();
1052
1053         if(!postinit)
1054                 PostInit();
1055 }
1056
1057 void Net_ReadRace()
1058 {
1059         float b;
1060
1061         b = ReadByte();
1062
1063         switch(b)
1064         {
1065                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:
1066                         race_checkpoint = ReadByte();
1067                         race_time = ReadInt24_t();
1068                         race_previousbesttime = ReadInt24_t();
1069                         if(race_previousbestname)
1070                                 strunzone(race_previousbestname);
1071                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));
1072
1073                         race_checkpointtime = time;
1074
1075                         if(race_checkpoint == 0 || race_checkpoint == 254)
1076                         {
1077                                 race_penaltyaccumulator = 0;
1078                                 race_laptime = time; // valid
1079                         }
1080
1081                         break;
1082
1083                 case RACE_NET_CHECKPOINT_CLEAR:
1084                         race_laptime = 0;
1085                         race_checkpointtime = 0;
1086                         break;
1087
1088                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:
1089                         race_laptime = ReadCoord();
1090                         race_checkpointtime = -99999;
1091                         // fall through
1092                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:
1093                         race_nextcheckpoint = ReadByte();
1094
1095                         race_nextbesttime = ReadInt24_t();
1096                         if(race_nextbestname)
1097                                 strunzone(race_nextbestname);
1098                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));
1099                         break;
1100
1101                 case RACE_NET_CHECKPOINT_HIT_RACE:
1102                         race_mycheckpoint = ReadByte();
1103                         race_mycheckpointtime = time;
1104                         race_mycheckpointdelta = ReadInt24_t();
1105                         race_mycheckpointlapsdelta = ReadByte();
1106                         if(race_mycheckpointlapsdelta >= 128)
1107                                 race_mycheckpointlapsdelta -= 256;
1108                         if(race_mycheckpointenemy)
1109                                 strunzone(race_mycheckpointenemy);
1110                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1111                         break;
1112
1113                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:
1114                         race_othercheckpoint = ReadByte();
1115                         race_othercheckpointtime = time;
1116                         race_othercheckpointdelta = ReadInt24_t();
1117                         race_othercheckpointlapsdelta = ReadByte();
1118                         if(race_othercheckpointlapsdelta >= 128)
1119                                 race_othercheckpointlapsdelta -= 256;
1120                         if(race_othercheckpointenemy)
1121                                 strunzone(race_othercheckpointenemy);
1122                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1123                         break;
1124
1125                 case RACE_NET_PENALTY_RACE:
1126                         race_penaltyeventtime = time;
1127                         race_penaltytime = ReadShort();
1128                         //race_penaltyaccumulator += race_penaltytime;
1129                         if(race_penaltyreason)
1130                                 strunzone(race_penaltyreason);
1131                         race_penaltyreason = strzone(ReadString());
1132                         break;
1133
1134                 case RACE_NET_PENALTY_QUALIFYING:
1135                         race_penaltyeventtime = time;
1136                         race_penaltytime = ReadShort();
1137                         race_penaltyaccumulator += race_penaltytime;
1138                         if(race_penaltyreason)
1139                                 strunzone(race_penaltyreason);
1140                         race_penaltyreason = strzone(ReadString());
1141                         break;
1142
1143                 case RACE_NET_SERVER_RECORD:
1144                         race_server_record = ReadInt24_t();
1145                         break;
1146                 case RACE_NET_SPEED_AWARD:
1147                         race_speedaward = ReadInt24_t();
1148                         if(race_speedaward_holder)
1149                                 strunzone(race_speedaward_holder);
1150                         race_speedaward_holder = strzone(ReadString());
1151                         break;
1152                 case RACE_NET_SPEED_AWARD_BEST:
1153                         race_speedaward_alltimebest = ReadInt24_t();
1154                         if(race_speedaward_alltimebest_holder)
1155                                 strunzone(race_speedaward_alltimebest_holder);
1156                         race_speedaward_alltimebest_holder = strzone(ReadString());
1157                         break;
1158                 case RACE_NET_SERVER_RANKINGS:
1159                         float prevpos, del;
1160             int pos = ReadShort();
1161                         prevpos = ReadShort();
1162                         del = ReadShort();
1163
1164                         // move other rankings out of the way
1165             int i;
1166                         if (prevpos) {
1167                                 for (i=prevpos-1;i>pos-1;--i) {
1168                                         grecordtime[i] = grecordtime[i-1];
1169                                         if(grecordholder[i])
1170                                                 strunzone(grecordholder[i]);
1171                                         grecordholder[i] = strzone(grecordholder[i-1]);
1172                                 }
1173                         } else if (del) { // a record has been deleted by the admin
1174                                 for (i=pos-1; i<= RANKINGS_CNT-1; ++i) {
1175                                         if (i == RANKINGS_CNT-1) { // clear out last record
1176                                                 grecordtime[i] = 0;
1177                                                 if (grecordholder[i])
1178                                                         strunzone(grecordholder[i]);
1179                                                 grecordholder[i] = string_null;
1180                                         }
1181                                         else {
1182                                                 grecordtime[i] = grecordtime[i+1];
1183                                                 if (grecordholder[i])
1184                                                         strunzone(grecordholder[i]);
1185                                                 grecordholder[i] = strzone(grecordholder[i+1]);
1186                                         }
1187                                 }
1188                         } else { // player has no ranked record yet
1189                                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
1190                                         grecordtime[i] = grecordtime[i-1];
1191                                         if(grecordholder[i])
1192                                                 strunzone(grecordholder[i]);
1193                                         grecordholder[i] = strzone(grecordholder[i-1]);
1194                                 }
1195                         }
1196
1197                         // store new ranking
1198                         if(grecordholder[pos-1] != "")
1199                                 strunzone(grecordholder[pos-1]);
1200                         grecordholder[pos-1] = strzone(ReadString());
1201                         grecordtime[pos-1] = ReadInt24_t();
1202                         if(grecordholder[pos-1] == GetPlayerName(player_localnum))
1203                                 race_myrank = pos;
1204                         break;
1205                 case RACE_NET_SERVER_STATUS:
1206                         race_status = ReadShort();
1207                         if(race_status_name)
1208                                 strunzone(race_status_name);
1209                         race_status_name = strzone(ReadString());
1210         }
1211 }
1212
1213 void Net_TeamNagger()
1214 {
1215         teamnagger = 1;
1216 }
1217
1218 void Net_ReadPingPLReport()
1219 {
1220         int e, pi, pl, ml;
1221         e = ReadByte();
1222         pi = ReadShort();
1223         pl = ReadByte();
1224         ml = ReadByte();
1225         if (!(playerslots[e]))
1226                 return;
1227         playerslots[e].ping = pi;
1228         playerslots[e].ping_packetloss = pl / 255.0;
1229         playerslots[e].ping_movementloss = ml / 255.0;
1230 }
1231
1232 void Net_WeaponComplain()
1233 {
1234         complain_weapon = ReadByte();
1235
1236         if(complain_weapon_name)
1237                 strunzone(complain_weapon_name);
1238         complain_weapon_name = strzone(WEP_NAME(complain_weapon));
1239
1240         complain_weapon_type = ReadByte();
1241
1242         complain_weapon_time = time;
1243         weapontime = time; // ping the weapon panel
1244
1245         switch(complain_weapon_type)
1246         {
1247                 case 0: Local_Notification(MSG_MULTI, ITEM_WEAPON_NOAMMO, complain_weapon); break;
1248                 case 1: Local_Notification(MSG_MULTI, ITEM_WEAPON_DONTHAVE, complain_weapon); break;
1249                 default: Local_Notification(MSG_MULTI, ITEM_WEAPON_UNAVAILABLE, complain_weapon); break;
1250         }
1251 }
1252
1253 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.
1254 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.
1255 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.
1256 float CSQC_Parse_TempEntity()
1257 {
1258         float bHandled;
1259                 bHandled  = true;
1260         // Acquire TE ID
1261         float nTEID;
1262                 nTEID = ReadByte();
1263
1264         if(autocvar_developer_csqcentities)
1265                 printf("CSQC_Parse_TempEntity() with nTEID=%d\n", nTEID);
1266
1267                 // NOTE: Could just do return instead of break...
1268         switch(nTEID)
1269         {
1270                 case TE_CSQC_TARGET_MUSIC:
1271                         Net_TargetMusic();
1272                         bHandled = true;
1273                         break;
1274                 case TE_CSQC_PICTURE:
1275                         Net_MapVote_Picture();
1276                         bHandled = true;
1277                         break;
1278                 case TE_CSQC_RACE:
1279                         Net_ReadRace();
1280                         bHandled = true;
1281                         break;
1282                 case TE_CSQC_VORTEXBEAMPARTICLE:
1283                         Net_ReadVortexBeamParticle();
1284                         bHandled = true;
1285                         break;
1286                 case TE_CSQC_TEAMNAGGER:
1287                         Net_TeamNagger();
1288                         bHandled = true;
1289                         break;
1290                 case TE_CSQC_ARC:
1291                         Net_ReadArc();
1292                         bHandled = true;
1293                         break;
1294                 case TE_CSQC_PINGPLREPORT:
1295                         Net_ReadPingPLReport();
1296                         bHandled = true;
1297                         break;
1298                 case TE_CSQC_WEAPONCOMPLAIN:
1299                         Net_WeaponComplain();
1300                         bHandled = true;
1301                         break;
1302                 case TE_CSQC_VEHICLESETUP:
1303                         Net_VehicleSetup();
1304                         bHandled = true;
1305                         break;
1306                 case TE_CSQC_SVNOTICE:
1307                         cl_notice_read();
1308                         bHandled = true;
1309                         break;
1310                 case TE_CSQC_SHOCKWAVEPARTICLE:
1311                         Net_ReadShockwaveParticle();
1312                         bHandled = true;
1313                         break;
1314                 default:
1315                         // No special logic for this temporary entity; return 0 so the engine can handle it
1316                         bHandled = false;
1317                         break;
1318         }
1319
1320         return bHandled;
1321 }
1322
1323 string getcommandkey(string text, string command)
1324 {
1325         string keys;
1326         float n, j, k, l = 0;
1327
1328         if (!autocvar_hud_showbinds)
1329                 return text;
1330
1331         keys = db_get(binddb, command);
1332         if (keys == "")
1333         {
1334                 n = tokenize(findkeysforcommand(command, 0)); // uses '...' strings
1335                 for(j = 0; j < n; ++j)
1336                 {
1337                         k = stof(argv(j));
1338                         if(k != -1)
1339                         {
1340                                 if ("" == keys)
1341                                         keys = keynumtostring(k);
1342                                 else
1343                                         keys = strcat(keys, ", ", keynumtostring(k));
1344
1345                                 ++l;
1346                                 if (autocvar_hud_showbinds_limit > 0 && autocvar_hud_showbinds_limit <= l)
1347                                         break;
1348                         }
1349
1350                 }
1351                 if (keys == "")
1352                         keys = "NO_KEY";
1353                 db_put(binddb, command, keys);
1354         }
1355
1356         if (keys == "NO_KEY") {
1357                 if (autocvar_hud_showbinds > 1)
1358                         return sprintf(_("%s (not bound)"), text);
1359                 else
1360                         return text;
1361         }
1362         else if (autocvar_hud_showbinds > 1)
1363                 return sprintf("%s (%s)", text, keys);
1364         else
1365                 return keys;
1366 }