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