]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/miscfunctions.qc
Add a mutator hook to indicate whether the physics HUD panel should be shown when...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / miscfunctions.qc
1 #include "miscfunctions.qh"
2
3 #include "autocvars.qh"
4 #include "defs.qh"
5 #include "hud/_mod.qh"
6
7 #include <common/command/_mod.qh>
8
9 #include <common/teams.qh>
10
11 #include <lib/csqcmodel/cl_model.qh>
12
13
14 void AuditLists()
15 {
16         entity e;
17         entity prev;
18
19         prev = players;
20         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
21         {
22                 if(prev != e.sort_prev)
23                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
24         }
25
26         prev = teams;
27         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
28         {
29                 if(prev != e.sort_prev)
30                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
31         }
32 }
33
34
35 float RegisterPlayer(entity player)
36 {
37         entity pl;
38         AuditLists();
39         for(pl = players.sort_next; pl; pl = pl.sort_next)
40                 if(pl == player)
41                         error("Player already registered!");
42         player.sort_next = players.sort_next;
43         player.sort_prev = players;
44         if(players.sort_next)
45                 players.sort_next.sort_prev = player;
46         players.sort_next = player;
47         AuditLists();
48         return true;
49 }
50
51 void RemovePlayer(entity player)
52 {
53         entity pl, parent;
54         AuditLists();
55         parent = players;
56         for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
57                 parent = pl;
58
59         if(!pl)
60         {
61                 error("Trying to remove a player which is not in the playerlist!");
62                 return;
63         }
64         parent.sort_next = player.sort_next;
65         if(player.sort_next)
66                 player.sort_next.sort_prev = parent;
67         AuditLists();
68 }
69
70 void MoveToLast(entity e)
71 {
72         AuditLists();
73         entity ent = e.sort_next;
74         while(ent)
75         {
76                 SORT_SWAP(ent, e);
77                 ent = e.sort_next;
78         }
79         AuditLists();
80 }
81
82 float RegisterTeam(entity Team)
83 {
84         assert_once(Team.team, eprint(Team));
85         entity tm;
86         AuditLists();
87         for(tm = teams.sort_next; tm; tm = tm.sort_next)
88                 if(tm == Team)
89                         error("Team already registered!");
90         Team.sort_next = teams.sort_next;
91         Team.sort_prev = teams;
92         if(teams.sort_next)
93                 teams.sort_next.sort_prev = Team;
94         teams.sort_next = Team;
95         if(Team.team && Team.team != NUM_SPECTATOR)
96                 ++team_count;
97         AuditLists();
98         return true;
99 }
100
101 void RemoveTeam(entity Team)
102 {
103         entity tm, parent;
104         AuditLists();
105         parent = teams;
106         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
107                 parent = tm;
108
109         if(!tm)
110         {
111                 LOG_INFO(_("Trying to remove a team which is not in the teamlist!"));
112                 return;
113         }
114         parent.sort_next = Team.sort_next;
115         if(Team.sort_next)
116                 Team.sort_next.sort_prev = parent;
117         if(Team.team && Team.team != NUM_SPECTATOR)
118                 --team_count;
119         AuditLists();
120 }
121
122 entity GetTeam(int Team, bool add)
123 {
124         TC(int, Team); TC(bool, add);
125         int num = (Team == NUM_SPECTATOR) ? 16 : Team;
126         if(teamslots[num])
127                 return teamslots[num];
128         if (!add)
129                 return NULL;
130         entity tm = new_pure(team);
131         tm.team = Team;
132         teamslots[num] = tm;
133         RegisterTeam(tm);
134         return tm;
135 }
136
137 vector HUD_GetFontsize(string cvarname)
138 {
139         vector v;
140         v = stov(cvar_string(cvarname));
141         if(v.x == 0)
142                 v = '8 8 0';
143         if(v.y == 0)
144                 v.y = v.x;
145         v.z = 0;
146         return v;
147 }
148
149 float PreviewExists(string name)
150 {
151         if(autocvar_cl_readpicture_force)
152                 return false;
153
154         if (fexists(strcat(name, ".tga"))) return true;
155         if (fexists(strcat(name, ".png"))) return true;
156         if (fexists(strcat(name, ".jpg"))) return true;
157         if (fexists(strcat(name, ".pcx"))) return true;
158
159         return false;
160 }
161
162 // decolorizes and team colors the player name when needed
163 string playername(string thename, float teamid)
164 {
165     TC(int, teamid);
166     string t;
167     if (teamplay)
168     {
169         t = Team_ColorCode(teamid);
170         return strcat(t, strdecolorize(thename));
171     }
172     else
173         return strdecolorize(thename);
174 }
175
176 float cvar_or(string cv, float v)
177 {
178         string s;
179         s = cvar_string(cv);
180         if(s == "")
181                 return v;
182         else
183                 return stof(s);
184 }
185
186 vector project_3d_to_2d(vector vec)
187 {
188         vec = cs_project(vec);
189         if(cs_project_is_b0rked > 0)
190         {
191                 vec.x *= vid_conwidth / vid_width;
192                 vec.y *= vid_conheight / vid_height;
193         }
194         return vec;
195 }
196
197 bool projected_on_screen(vector screen_pos)
198 {
199         return screen_pos.z >= 0
200                 && screen_pos.x >= 0
201                 && screen_pos.y >= 0
202                 && screen_pos.x < vid_conwidth
203                 && screen_pos.y < vid_conheight;
204 }
205
206 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
207 {
208         return 1.2 / (1.2 - fadelerp);
209 }
210
211 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
212 {
213         boxsize.x *= boxxsizefactor; // easier interface for text
214         return boxsize * (0.5 * (1 - sz));
215 }
216
217 // NOTE base is the central value
218 // freq: circle frequency, = 2*pi*frequency in hertz
219 // start_pos:
220 //  -1 start from the lower value
221 //   0 start from the base value
222 //   1 start from the higher value
223 float blink_synced(float base, float range, float freq, float start_time, int start_pos)
224 {
225         // note:
226         //   RMS = sqrt(base^2 + 0.5 * range^2)
227         // thus
228         //   base = sqrt(RMS^2 - 0.5 * range^2)
229         // ensure RMS == 1
230
231         return base + range * sin((time - start_time - (M_PI / 2) * start_pos) * freq);
232 }
233
234 float blink(float base, float range, float freq)
235 {
236         return blink_synced(base, range, freq, 0, 0);
237 }
238
239 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
240 {
241         vector line_dim = '0 0 0';
242
243         // left and right lines
244         pos.x -= thickness;
245         line_dim.x = thickness;
246         line_dim.y = dim.y;
247         drawfill(pos, line_dim, color, theAlpha, drawflag);
248         drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
249
250         // upper and lower lines
251         pos.y -= thickness;
252         line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
253         line_dim.y = thickness;
254         drawfill(pos, line_dim, color, theAlpha, drawflag);
255         drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
256 }
257
258 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
259 {
260         pos = HUD_Shift(pos);
261         sz = HUD_Scale(sz);
262         area = HUD_Scale(area);
263
264         vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
265         end_pos = pos + area;
266
267         current_pos.y = pos.y;
268         while (current_pos.y < end_pos.y)
269         {
270                 current_pos.x = pos.x;
271                 while (current_pos.x < end_pos.x)
272                 {
273                         new_size.x = min(sz.x, end_pos.x - current_pos.x);
274                         new_size.y = min(sz.y, end_pos.y - current_pos.y);
275                         ratio.x = new_size.x / sz.x;
276                         ratio.y = new_size.y / sz.y;
277                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
278                         current_pos.x += sz.x;
279                 }
280                 current_pos.y += sz.y;
281         }
282 }
283
284 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
285 {
286         float sz;
287         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
288
289         drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
290 }
291
292 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
293 {
294         drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
295         drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
296 }
297
298 void HUD_Scale_Disable()
299 {
300         hud_scale = '1 1 0';
301         hud_shift = '0 0 0';
302         drawfontscale = hud_scale;
303 }
304
305 void HUD_Scale_Enable()
306 {
307         hud_scale = hud_scale_current;
308         hud_shift = hud_shift_current;
309         drawfontscale = hud_scale;
310 }
311
312 vector HUD_Scale(vector v)
313 {
314         v.x = HUD_ScaleX(v.x);
315         v.y = HUD_ScaleY(v.y);
316         return v;
317 }
318
319 vector HUD_Shift(vector v)
320 {
321         v.x = HUD_ShiftX(v.x);
322         v.y = HUD_ShiftY(v.y);
323         return v;
324 }
325
326 float stringwidth(string text, float handleColors, vector sz)
327 {
328         vector dfs = drawfontscale;
329         drawfontscale = '1 1 0';
330         float r = stringwidth_builtin(text, handleColors, sz);
331         drawfontscale = dfs;
332         return r;
333 }
334
335 #define SET_POS_AND_SZ_Y_ASPECT(allow_colors) MACRO_BEGIN \
336         float textaspect, oldsz; \
337         vector dfs = drawfontscale; \
338         drawfontscale = '1 1 0'; \
339         textaspect = stringwidth(text, allow_colors, '1 1 1' * sz.y) / sz.y; \
340         drawfontscale = dfs; \
341         if(sz.x/sz.y > textaspect) { \
342                 oldsz = sz.x; \
343                 sz.x = sz.y * textaspect; \
344                 pos.x += (oldsz - sz.x) * 0.5; \
345         } else { \
346                 oldsz = sz.y; \
347                 sz.y = sz.x / textaspect; \
348                 pos.y += (oldsz - sz.y) * 0.5; \
349         } \
350 MACRO_END
351
352 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
353 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
354         SET_POS_AND_SZ_Y_ASPECT(false);
355         drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
356 }
357
358 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
359 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
360         SET_POS_AND_SZ_Y_ASPECT(true);
361         drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
362 }
363
364 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
365 {
366         float sz;
367         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
368
369         drawfontscale = hud_scale * sz;
370         vector dfs = drawfontscale;
371         drawfontscale = sz * '1 1 0';
372         float textaspect = stringwidth_builtin(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz);
373         drawfontscale = dfs;
374         drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, textaspect), text, HUD_Scale(theScale * (sz / drawfontscale.x)), rgb, theAlpha * (1 - fadelerp), flag);
375         // width parameter:
376         //    (scale_x * sz / drawfontscale.x) * drawfontscale.x * SIZE1 / (scale_x * sz)
377         //    SIZE1
378         drawfontscale = hud_scale;
379 }
380
381 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
382 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
383         SET_POS_AND_SZ_Y_ASPECT(false);
384         drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
385 }
386
387 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
388 {
389         float sz;
390         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
391
392         drawfontscale = hud_scale * sz;
393         // eventually replace with drawcolorcodedstring
394         drawcolorcodedstring(position + expandingbox_resize_centered_box_offset(sz, theScale, stringwidth_builtin(text, true, theScale * (sz / drawfontscale.x)) / (theScale.x * sz)), text, theScale * (sz / drawfontscale.x), theAlpha * (1 - fadelerp), flag);
395         drawfontscale = hud_scale;
396 }
397
398 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
399         SET_POS_AND_SZ_Y_ASPECT(true);
400         drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
401 }
402
403 void update_mousepos()
404 {
405         mousepos += getmousepos() * autocvar_menu_mouse_speed;
406         mousepos.x = bound(0, mousepos.x, vid_conwidth);
407         mousepos.y = bound(0, mousepos.y, vid_conheight);
408 }
409
410 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
411 float PolyDrawModelSurface(entity e, float i_s)
412 {
413         float i_t;
414         float n_t;
415         vector tri;
416         string tex;
417         tex = getsurfacetexture(e, i_s);
418         if (!tex)
419                 return 0; // this is beyond the last one
420         n_t = getsurfacenumtriangles(e, i_s);
421         for(i_t = 0; i_t < n_t; ++i_t)
422         {
423                 tri = getsurfacetriangle(e, i_s, i_t);
424                 R_BeginPolygon(tex, 0, false);
425                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
426                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
427                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
428                 R_EndPolygon();
429         }
430         return 1;
431 }
432 void PolyDrawModel(entity e)
433 {
434         float i_s;
435         for(i_s = 0; ; ++i_s)
436                 if(!PolyDrawModelSurface(e, i_s))
437                         break;
438 }
439
440 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
441 {
442         vector ringsize, v, t;
443         ringsize = radi * '1 1 0';
444         centre = HUD_Shift(centre);
445         ringsize = HUD_Scale(ringsize);
446
447         if(f >= 1)
448         {
449                 // draw full rectangle
450                 R_BeginPolygon(pic, drawflag, true);
451                         v = centre;                     t = '0.5 0.5 0';
452                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
453                         R_PolygonVertex(v, t, rgb, a);
454
455                         v = centre;                     t = '0.5 0.5 0';
456                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
457                         R_PolygonVertex(v, t, rgb, a);
458
459                         v = centre;                     t = '0.5 0.5 0';
460                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
461                         R_PolygonVertex(v, t, rgb, a);
462
463                         v = centre;                     t = '0.5 0.5 0';
464                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
465                         R_PolygonVertex(v, t, rgb, a);
466                 R_EndPolygon();
467                 return;  // Complete rectangle, nothing more needed.
468         }
469
470         float co = cos(f * 2 * M_PI);
471         float si = sin(f * 2 * M_PI);
472         float q = fabs(co) + fabs(si);
473         co /= q;
474         si /= q;
475
476         if(f > 0.75)
477         {
478                 // draw upper half in full
479                 R_BeginPolygon(pic, drawflag, true);
480                         v = centre;                     t = '0.5 0.5 0';
481                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
482                         R_PolygonVertex(v, t, rgb, a);
483
484                         v = centre;                     t = '0.5 0.5 0';
485                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
486                         R_PolygonVertex(v, t, rgb, a);
487
488                         v = centre;                     t = '0.5 0.5 0';
489                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
490                         R_PolygonVertex(v, t, rgb, a);
491                 R_EndPolygon();
492                 // draw clipped lower half as a quad
493                 R_BeginPolygon(pic, drawflag, true);
494                         v = centre;                     t = '0.5 0.5 0';
495                         R_PolygonVertex(v, t, rgb, a);
496
497                         v = centre;                     t = '0.5 0.5 0';
498                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
499                         R_PolygonVertex(v, t, rgb, a);
500
501                         v = centre;                     t = '0.5 0.5 0';
502                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
503                         R_PolygonVertex(v, t, rgb, a);
504         }
505         else if(f > 0.5)
506         {
507                 // draw upper half in full
508                 R_BeginPolygon(pic, drawflag, true);
509                         v = centre;                     t = '0.5 0.5 0';
510                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
511                         R_PolygonVertex(v, t, rgb, a);
512
513                         v = centre;                     t = '0.5 0.5 0';
514                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
515                         R_PolygonVertex(v, t, rgb, a);
516
517                         v = centre;                     t = '0.5 0.5 0';
518                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
519                         R_PolygonVertex(v, t, rgb, a);
520                 R_EndPolygon();
521                 // draw clipped lower half as a triangle
522                 R_BeginPolygon(pic, drawflag, true);
523                         v = centre;                     t = '0.5 0.5 0';
524                         R_PolygonVertex(v, t, rgb, a);
525
526                         v = centre;                     t = '0.5 0.5 0';
527                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
528                         R_PolygonVertex(v, t, rgb, a);
529         }
530         else if(f > 0.25)
531         {
532                 // draw clipped lower half as a quad
533                 R_BeginPolygon(pic, drawflag, true);
534                         v = centre;                     t = '0.5 0.5 0';
535                         R_PolygonVertex(v, t, rgb, a);
536
537                         v = centre;                     t = '0.5 0.5 0';
538                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
539                         R_PolygonVertex(v, t, rgb, a);
540
541                         v = centre;                     t = '0.5 0.5 0';
542                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
543                         R_PolygonVertex(v, t, rgb, a);
544         }
545         else if (f > 0)
546         {
547                 // draw clipped lower half as a triangle
548                 R_BeginPolygon(pic, drawflag, true);
549                         v = centre;                     t = '0.5 0.5 0';
550                         R_PolygonVertex(v, t, rgb, a);
551
552                         v = centre;                     t = '0.5 0.5 0';
553                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
554                         R_PolygonVertex(v, t, rgb, a);
555         }
556         else
557         {
558                 // Nothing to draw.
559                 return;
560         }
561
562         // The last, moving vertex.
563                 v = centre;                     t = '0.5 0.5 0';
564                 v.x += co * 0.5 * ringsize.x;   t += co * '0.5 0.5 0';
565                 v.y += si * 0.5 * ringsize.y;   t += si * '0.5 -0.5 0';
566                 R_PolygonVertex(v, t, rgb, a);
567         R_EndPolygon();
568 }
569
570 /** engine callback */
571 void URI_Get_Callback(int id, int status, string data)
572 {
573         TC(int, id); TC(int, status);
574         if(url_URI_Get_Callback(id, status, data))
575         {
576                 // handled
577         }
578         else if (id == URI_GET_DISCARD)
579         {
580                 // discard
581         }
582         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
583         {
584                 // sv_cmd curl
585                 Curl_URI_Get_Callback(id, status, data);
586         }
587         else
588         {
589                 LOG_INFOF("Received HTTP request data for an invalid id %d.", id);
590         }
591 }
592
593 void Accuracy_LoadLevels()
594 {
595         if(autocvar_accuracy_color_levels != acc_color_levels)
596         {
597                 strcpy(acc_color_levels, autocvar_accuracy_color_levels);
598                 acc_levels = tokenize_console(acc_color_levels);
599                 if(acc_levels > MAX_ACCURACY_LEVELS)
600                         acc_levels = MAX_ACCURACY_LEVELS;
601                 if(acc_levels < 2)
602                         LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values");
603
604                 int i;
605                 for(i = 0; i < acc_levels; ++i)
606                         acc_lev[i] = stof(argv(i)) / 100.0;
607         }
608 }
609
610 void Accuracy_LoadColors()
611 {
612         if(time > acc_col_loadtime)
613         if(acc_levels >= 2)
614         {
615                 int i;
616                 for(i = 0; i < acc_levels; ++i)
617                         acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
618                 acc_col_loadtime = time + 2;
619         }
620 }
621
622 vector Accuracy_GetColor(float accuracy)
623 {
624         float factor;
625         vector color;
626         if(acc_levels < 2)
627                 return '0 0 0'; // return black, can't determine the right color
628
629         // find the max level lower than acc
630         int j = acc_levels-1;
631         while(j && accuracy < acc_lev[j])
632                 --j;
633
634         // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
635         factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
636         color = acc_col[j];
637         color = color + factor * (acc_col[j+1] - color);
638         return color;
639 }