]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/miscfunctions.qc
Merge branch 'master' into bones_was_here/q3compat
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / miscfunctions.qc
1 #include "miscfunctions.qh"
2
3 #include "autocvars.qh"
4 #include "hud/_mod.qh"
5 #include "main.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 float cvar_or(string cv, float v)
163 {
164         string s;
165         s = cvar_string(cv);
166         if(s == "")
167                 return v;
168         else
169                 return stof(s);
170 }
171
172 vector project_3d_to_2d(vector vec)
173 {
174         vec = cs_project(vec);
175         if(cs_project_is_b0rked > 0)
176         {
177                 vec.x *= vid_conwidth / vid_width;
178                 vec.y *= vid_conheight / vid_height;
179         }
180         return vec;
181 }
182
183 bool projected_on_screen(vector screen_pos)
184 {
185         return screen_pos.z >= 0
186                 && screen_pos.x >= 0
187                 && screen_pos.y >= 0
188                 && screen_pos.x < vid_conwidth
189                 && screen_pos.y < vid_conheight;
190 }
191
192 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
193 {
194         return 1.2 / (1.2 - fadelerp);
195 }
196
197 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
198 {
199         boxsize.x *= boxxsizefactor; // easier interface for text
200         return boxsize * (0.5 * (1 - sz));
201 }
202
203 // NOTE base is the central value
204 // freq: circle frequency, = 2*pi*frequency in hertz
205 // start_pos:
206 //  -1 start from the lower value
207 //   0 start from the base value
208 //   1 start from the higher value
209 float blink_synced(float base, float range, float freq, float start_time, int start_pos)
210 {
211         // note:
212         //   RMS = sqrt(base^2 + 0.5 * range^2)
213         // thus
214         //   base = sqrt(RMS^2 - 0.5 * range^2)
215         // ensure RMS == 1
216
217         return base + range * sin((time - start_time - (M_PI / 2) * start_pos) * freq);
218 }
219
220 float blink(float base, float range, float freq)
221 {
222         return blink_synced(base, range, freq, 0, 0);
223 }
224
225 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
226 {
227         vector line_dim = '0 0 0';
228
229         // left and right lines
230         pos.x -= thickness;
231         line_dim.x = thickness;
232         line_dim.y = dim.y;
233         drawfill(pos, line_dim, color, theAlpha, drawflag);
234         drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
235
236         // upper and lower lines
237         pos.y -= thickness;
238         line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
239         line_dim.y = thickness;
240         drawfill(pos, line_dim, color, theAlpha, drawflag);
241         drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
242 }
243
244 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
245 {
246         pos = HUD_Shift(pos);
247         sz = HUD_Scale(sz);
248         area = HUD_Scale(area);
249
250         vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
251         end_pos = pos + area;
252
253         current_pos.y = pos.y;
254         while (current_pos.y < end_pos.y)
255         {
256                 current_pos.x = pos.x;
257                 while (current_pos.x < end_pos.x)
258                 {
259                         new_size.x = min(sz.x, end_pos.x - current_pos.x);
260                         new_size.y = min(sz.y, end_pos.y - current_pos.y);
261                         ratio.x = new_size.x / sz.x;
262                         ratio.y = new_size.y / sz.y;
263                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
264                         current_pos.x += sz.x;
265                 }
266                 current_pos.y += sz.y;
267         }
268 }
269
270 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
271 {
272         float sz;
273         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
274
275         drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
276 }
277
278 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
279 {
280         drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
281         drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
282 }
283
284 void HUD_Scale_Disable()
285 {
286         hud_scale = '1 1 0';
287         hud_shift = '0 0 0';
288         drawfontscale = hud_scale;
289 }
290
291 void HUD_Scale_Enable()
292 {
293         hud_scale = hud_scale_current;
294         hud_shift = hud_shift_current;
295         drawfontscale = hud_scale;
296 }
297
298 vector HUD_Scale(vector v)
299 {
300         v.x = HUD_ScaleX(v.x);
301         v.y = HUD_ScaleY(v.y);
302         return v;
303 }
304
305 vector HUD_Shift(vector v)
306 {
307         v.x = HUD_ShiftX(v.x);
308         v.y = HUD_ShiftY(v.y);
309         return v;
310 }
311
312 float stringwidth(string text, float handleColors, vector sz)
313 {
314         vector dfs = drawfontscale;
315         drawfontscale = '1 1 0';
316         float r = stringwidth_builtin(text, handleColors, sz);
317         drawfontscale = dfs;
318         return r;
319 }
320
321 #define SET_POS_AND_SZ_Y_ASPECT(allow_colors) MACRO_BEGIN \
322         float textaspect, oldsz; \
323         vector dfs = drawfontscale; \
324         drawfontscale = '1 1 0'; \
325         textaspect = stringwidth(text, allow_colors, '1 1 1' * sz.y) / sz.y; \
326         drawfontscale = dfs; \
327         if(sz.x/sz.y > textaspect) { \
328                 oldsz = sz.x; \
329                 sz.x = sz.y * textaspect; \
330                 pos.x += (oldsz - sz.x) * 0.5; \
331         } else { \
332                 oldsz = sz.y; \
333                 sz.y = sz.x / textaspect; \
334                 pos.y += (oldsz - sz.y) * 0.5; \
335         } \
336 MACRO_END
337
338 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
339 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
340         SET_POS_AND_SZ_Y_ASPECT(false);
341         drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
342 }
343
344 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
345 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
346         SET_POS_AND_SZ_Y_ASPECT(true);
347         drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
348 }
349
350 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
351 {
352         float sz;
353         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
354
355         drawfontscale = hud_scale * sz;
356         vector dfs = drawfontscale;
357         drawfontscale = sz * '1 1 0';
358         float textaspect = stringwidth_builtin(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz);
359         drawfontscale = dfs;
360         drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, textaspect), text, HUD_Scale(theScale * (sz / drawfontscale.x)), rgb, theAlpha * (1 - fadelerp), flag);
361         // width parameter:
362         //    (scale_x * sz / drawfontscale.x) * drawfontscale.x * SIZE1 / (scale_x * sz)
363         //    SIZE1
364         drawfontscale = hud_scale;
365 }
366
367 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
368 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
369         SET_POS_AND_SZ_Y_ASPECT(false);
370         drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
371 }
372
373 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
374 {
375         float sz;
376         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
377
378         drawfontscale = hud_scale * sz;
379         // eventually replace with drawcolorcodedstring
380         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);
381         drawfontscale = hud_scale;
382 }
383
384 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
385         SET_POS_AND_SZ_Y_ASPECT(true);
386         drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
387 }
388
389 void update_mousepos()
390 {
391         mousepos += getmousepos() * autocvar_menu_mouse_speed;
392         mousepos.x = bound(0, mousepos.x, vid_conwidth);
393         mousepos.y = bound(0, mousepos.y, vid_conheight);
394 }
395
396 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
397 float PolyDrawModelSurface(entity e, float i_s)
398 {
399         float i_t;
400         float n_t;
401         vector tri;
402         string tex;
403         tex = getsurfacetexture(e, i_s);
404         if (!tex)
405                 return 0; // this is beyond the last one
406         n_t = getsurfacenumtriangles(e, i_s);
407         for(i_t = 0; i_t < n_t; ++i_t)
408         {
409                 tri = getsurfacetriangle(e, i_s, i_t);
410                 R_BeginPolygon(tex, 0, false);
411                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
412                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
413                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
414                 R_EndPolygon();
415         }
416         return 1;
417 }
418 void PolyDrawModel(entity e)
419 {
420         float i_s;
421         for(i_s = 0; ; ++i_s)
422                 if(!PolyDrawModelSurface(e, i_s))
423                         break;
424 }
425
426 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
427 {
428         vector ringsize, v, t;
429         ringsize = radi * '1 1 0';
430         centre = HUD_Shift(centre);
431         ringsize = HUD_Scale(ringsize);
432
433         if(f >= 1)
434         {
435                 // draw full rectangle
436                 R_BeginPolygon(pic, drawflag, true);
437                         v = centre;                     t = '0.5 0.5 0';
438                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
439                         R_PolygonVertex(v, t, rgb, a);
440
441                         v = centre;                     t = '0.5 0.5 0';
442                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
443                         R_PolygonVertex(v, t, rgb, a);
444
445                         v = centre;                     t = '0.5 0.5 0';
446                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
447                         R_PolygonVertex(v, t, rgb, a);
448
449                         v = centre;                     t = '0.5 0.5 0';
450                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
451                         R_PolygonVertex(v, t, rgb, a);
452                 R_EndPolygon();
453                 return;  // Complete rectangle, nothing more needed.
454         }
455
456         float co = cos(f * 2 * M_PI);
457         float si = sin(f * 2 * M_PI);
458         float q = fabs(co) + fabs(si);
459         co /= q;
460         si /= q;
461
462         if(f > 0.75)
463         {
464                 // draw upper half in full
465                 R_BeginPolygon(pic, drawflag, true);
466                         v = centre;                     t = '0.5 0.5 0';
467                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
468                         R_PolygonVertex(v, t, rgb, a);
469
470                         v = centre;                     t = '0.5 0.5 0';
471                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
472                         R_PolygonVertex(v, t, rgb, a);
473
474                         v = centre;                     t = '0.5 0.5 0';
475                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
476                         R_PolygonVertex(v, t, rgb, a);
477                 R_EndPolygon();
478                 // draw clipped lower half as a quad
479                 R_BeginPolygon(pic, drawflag, true);
480                         v = centre;                     t = '0.5 0.5 0';
481                         R_PolygonVertex(v, t, rgb, a);
482
483                         v = centre;                     t = '0.5 0.5 0';
484                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
485                         R_PolygonVertex(v, t, rgb, a);
486
487                         v = centre;                     t = '0.5 0.5 0';
488                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
489                         R_PolygonVertex(v, t, rgb, a);
490         }
491         else if(f > 0.5)
492         {
493                 // draw upper half in full
494                 R_BeginPolygon(pic, drawflag, true);
495                         v = centre;                     t = '0.5 0.5 0';
496                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
497                         R_PolygonVertex(v, t, rgb, a);
498
499                         v = centre;                     t = '0.5 0.5 0';
500                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
501                         R_PolygonVertex(v, t, rgb, a);
502
503                         v = centre;                     t = '0.5 0.5 0';
504                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
505                         R_PolygonVertex(v, t, rgb, a);
506                 R_EndPolygon();
507                 // draw clipped lower half as a triangle
508                 R_BeginPolygon(pic, drawflag, true);
509                         v = centre;                     t = '0.5 0.5 0';
510                         R_PolygonVertex(v, t, rgb, a);
511
512                         v = centre;                     t = '0.5 0.5 0';
513                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
514                         R_PolygonVertex(v, t, rgb, a);
515         }
516         else if(f > 0.25)
517         {
518                 // draw clipped lower half as a quad
519                 R_BeginPolygon(pic, drawflag, true);
520                         v = centre;                     t = '0.5 0.5 0';
521                         R_PolygonVertex(v, t, rgb, a);
522
523                         v = centre;                     t = '0.5 0.5 0';
524                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
525                         R_PolygonVertex(v, t, rgb, a);
526
527                         v = centre;                     t = '0.5 0.5 0';
528                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
529                         R_PolygonVertex(v, t, rgb, a);
530         }
531         else if (f > 0)
532         {
533                 // draw clipped lower half as a triangle
534                 R_BeginPolygon(pic, drawflag, true);
535                         v = centre;                     t = '0.5 0.5 0';
536                         R_PolygonVertex(v, t, rgb, a);
537
538                         v = centre;                     t = '0.5 0.5 0';
539                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
540                         R_PolygonVertex(v, t, rgb, a);
541         }
542         else
543         {
544                 // Nothing to draw.
545                 return;
546         }
547
548         // The last, moving vertex.
549                 v = centre;                     t = '0.5 0.5 0';
550                 v.x += co * 0.5 * ringsize.x;   t += co * '0.5 0.5 0';
551                 v.y += si * 0.5 * ringsize.y;   t += si * '0.5 -0.5 0';
552                 R_PolygonVertex(v, t, rgb, a);
553         R_EndPolygon();
554 }
555
556 /** engine callback */
557 void URI_Get_Callback(int id, int status, string data)
558 {
559         TC(int, id); TC(int, status);
560         if(url_URI_Get_Callback(id, status, data))
561         {
562                 // handled
563         }
564         else if (id == URI_GET_DISCARD)
565         {
566                 // discard
567         }
568         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
569         {
570                 // sv_cmd curl
571                 Curl_URI_Get_Callback(id, status, data);
572         }
573         else
574         {
575                 LOG_INFOF("Received HTTP request data for an invalid id %d.", id);
576         }
577 }
578
579 void Accuracy_LoadLevels()
580 {
581         if(autocvar_accuracy_color_levels != acc_color_levels)
582         {
583                 strcpy(acc_color_levels, autocvar_accuracy_color_levels);
584                 acc_levels = tokenize_console(acc_color_levels);
585                 if(acc_levels > MAX_ACCURACY_LEVELS)
586                         acc_levels = MAX_ACCURACY_LEVELS;
587                 if(acc_levels < 2)
588                         LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values");
589
590                 int i;
591                 for(i = 0; i < acc_levels; ++i)
592                         acc_lev[i] = stof(argv(i)) / 100.0;
593         }
594 }
595
596 void Accuracy_LoadColors()
597 {
598         if(time > acc_col_loadtime)
599         if(acc_levels >= 2)
600         {
601                 int i;
602                 for(i = 0; i < acc_levels; ++i)
603                         acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
604                 acc_col_loadtime = time + 2;
605         }
606 }
607
608 vector Accuracy_GetColor(float accuracy)
609 {
610         float factor;
611         vector color;
612         if(acc_levels < 2)
613                 return '0 0 0'; // return black, can't determine the right color
614
615         // find the max level lower than acc
616         int j = acc_levels-1;
617         while(j && accuracy < acc_lev[j])
618                 --j;
619
620         // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
621         factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
622         color = acc_col[j];
623         color = color + factor * (acc_col[j+1] - color);
624         return color;
625 }