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