]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/miscfunctions.qc
Merge branch 'terencehill/menu_optimization' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / miscfunctions.qc
1 #include "miscfunctions.qh"
2
3 #include "hud/all.qh"
4
5 #include <common/command/generic.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         other = e.sort_next;
72         while(other)
73         {
74                 SORT_SWAP(other, e);
75                 other = 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         int num = (Team == NUM_SPECTATOR) ? 16 : Team;
123         if(teamslots[num])
124                 return teamslots[num];
125         if (!add)
126                 return world;
127         entity tm = new(team);
128         make_pure(tm);
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     string t;
164     if (teamplay)
165     {
166         t = Team_ColorCode(teamid);
167         return strcat(t, strdecolorize(thename));
168     }
169     else
170         return strdecolorize(thename);
171 }
172
173 float cvar_or(string cv, float v)
174 {
175         string s;
176         s = cvar_string(cv);
177         if(s == "")
178                 return v;
179         else
180                 return stof(s);
181 }
182
183 vector project_3d_to_2d(vector vec)
184 {
185         vec = cs_project(vec);
186         if(cs_project_is_b0rked > 0)
187         {
188                 vec.x *= vid_conwidth / vid_width;
189                 vec.y *= vid_conheight / vid_height;
190         }
191         return vec;
192 }
193
194 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
195 {
196         return 1.2 / (1.2 - fadelerp);
197 }
198
199 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
200 {
201         boxsize.x *= boxxsizefactor; // easier interface for text
202         return boxsize * (0.5 * (1 - sz));
203 }
204
205 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
206 {
207         vector line_dim = '0 0 0';
208
209         // left and right lines
210         pos.x -= thickness;
211         line_dim.x = thickness;
212         line_dim.y = dim.y;
213         drawfill(pos, line_dim, color, theAlpha, drawflag);
214         drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
215
216         // upper and lower lines
217         pos.y -= thickness;
218         line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
219         line_dim.y = thickness;
220         drawfill(pos, line_dim, color, theAlpha, drawflag);
221         drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
222 }
223
224 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
225 {
226         vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
227         end_pos = pos + area;
228
229         current_pos.y = pos.y;
230         while (current_pos.y < end_pos.y)
231         {
232                 current_pos.x = pos.x;
233                 while (current_pos.x < end_pos.x)
234                 {
235                         new_size.x = min(sz.x, end_pos.x - current_pos.x);
236                         new_size.y = min(sz.y, end_pos.y - current_pos.y);
237                         ratio.x = new_size.x / sz.x;
238                         ratio.y = new_size.y / sz.y;
239                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
240                         current_pos.x += sz.x;
241                 }
242                 current_pos.y += sz.y;
243         }
244 }
245
246 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
247 {
248         float sz;
249         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
250
251         drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
252 }
253
254 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
255 {
256         drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
257         drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
258 }
259
260 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
261 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
262         SET_POS_AND_SZ_Y_ASPECT(false);
263         drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
264 }
265
266 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
267 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
268         SET_POS_AND_SZ_Y_ASPECT(true);
269         drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
270 }
271
272 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
273 {
274         float sz;
275         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
276
277         drawfontscale = sz * '1 1 0';
278         drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, stringwidth(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz)), text, theScale * (sz / drawfontscale.x), rgb, theAlpha * (1 - fadelerp), flag);
279         // width parameter:
280         //    (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
281         //    SIZE1
282         drawfontscale = '1 1 0';
283 }
284
285 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
286 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
287         SET_POS_AND_SZ_Y_ASPECT(false);
288         drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
289 }
290
291 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
292 {
293         float sz;
294         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
295
296         drawfontscale = sz * '1 1 0';
297         drawcolorcodedstring(position + expandingbox_resize_centered_box_offset(sz, theScale, stringwidth(text, true, theScale * (sz / drawfontscale.x)) / (theScale.x * sz)), text, theScale * (sz / drawfontscale.x), theAlpha * (1 - fadelerp), flag);
298         drawfontscale = '1 1 0';
299 }
300
301 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
302         SET_POS_AND_SZ_Y_ASPECT(true);
303         drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
304 }
305
306 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
307 float PolyDrawModelSurface(entity e, float i_s)
308 {
309         float i_t;
310         float n_t;
311         vector tri;
312         string tex;
313         tex = getsurfacetexture(e, i_s);
314         if (!tex)
315                 return 0; // this is beyond the last one
316         n_t = getsurfacenumtriangles(e, i_s);
317         for(i_t = 0; i_t < n_t; ++i_t)
318         {
319                 tri = getsurfacetriangle(e, i_s, i_t);
320                 R_BeginPolygon(tex, 0);
321                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
322                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
323                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
324                 R_EndPolygon();
325         }
326         return 1;
327 }
328 void PolyDrawModel(entity e)
329 {
330         float i_s;
331         for(i_s = 0; ; ++i_s)
332                 if(!PolyDrawModelSurface(e, i_s))
333                         break;
334 }
335
336 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
337 {
338         float x, y, q, d;
339         vector ringsize, v, t;
340         ringsize = radi * '1 1 0';
341
342         x = cos(f * 2 * M_PI);
343         y = sin(f * 2 * M_PI);
344         q = fabs(x) + fabs(y);
345         x /= q;
346         y /= q;
347
348         if(f >= 1)
349         {
350                 // draw full rectangle
351                 R_BeginPolygon(pic, drawflag);
352                         v = centre;                     t = '0.5 0.5 0';
353                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
354                         R_PolygonVertex(v, t, rgb, a);
355
356                         v = centre;                     t = '0.5 0.5 0';
357                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
358                         R_PolygonVertex(v, t, rgb, a);
359
360                         v = centre;                     t = '0.5 0.5 0';
361                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
362                         R_PolygonVertex(v, t, rgb, a);
363
364                         v = centre;                     t = '0.5 0.5 0';
365                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
366                         R_PolygonVertex(v, t, rgb, a);
367                 R_EndPolygon();
368
369                 d = q - 1;
370                 if(d > 0)
371                 {
372                         R_BeginPolygon(pic, drawflag);
373                                 v = centre;                     t = '0.5 0.5 0';
374                                 R_PolygonVertex(v, t, rgb, a);
375
376                                 v = centre;                     t = '0.5 0.5 0';
377                                 v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
378                                 R_PolygonVertex(v, t, rgb, a);
379                 }
380         }
381         else if(f > 0.75)
382         {
383                 // draw upper and first triangle
384                 R_BeginPolygon(pic, drawflag);
385                         v = centre;                     t = '0.5 0.5 0';
386                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
387                         R_PolygonVertex(v, t, rgb, a);
388
389                         v = centre;                     t = '0.5 0.5 0';
390                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
391                         R_PolygonVertex(v, t, rgb, a);
392
393                         v = centre;                     t = '0.5 0.5 0';
394                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
395                         R_PolygonVertex(v, t, rgb, a);
396                 R_EndPolygon();
397                 R_BeginPolygon(pic, drawflag);
398                         v = centre;                     t = '0.5 0.5 0';
399                         R_PolygonVertex(v, t, rgb, a);
400
401                         v = centre;                     t = '0.5 0.5 0';
402                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
403                         R_PolygonVertex(v, t, rgb, a);
404
405                         v = centre;                     t = '0.5 0.5 0';
406                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
407                         R_PolygonVertex(v, t, rgb, a);
408
409                 d = q - 0.75;
410                 if(d <= 0)
411                         R_EndPolygon();
412         }
413         else if(f > 0.5)
414         {
415                 // draw upper triangle
416                 R_BeginPolygon(pic, drawflag);
417                         v = centre;                     t = '0.5 0.5 0';
418                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
419                         R_PolygonVertex(v, t, rgb, a);
420
421                         v = centre;                     t = '0.5 0.5 0';
422                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
423                         R_PolygonVertex(v, t, rgb, a);
424
425                         v = centre;                     t = '0.5 0.5 0';
426                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
427                         R_PolygonVertex(v, t, rgb, a);
428                 R_EndPolygon();
429
430                 d = q - 0.5;
431                 if(d > 0)
432                 {
433                         R_BeginPolygon(pic, drawflag);
434                                 v = centre;                     t = '0.5 0.5 0';
435                                 R_PolygonVertex(v, t, rgb, a);
436
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         }
442         else if(f > 0.25)
443         {
444                 // draw first triangle
445                 R_BeginPolygon(pic, drawflag);
446                         v = centre;                     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.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
451                         R_PolygonVertex(v, t, rgb, a);
452
453                         v = centre;                     t = '0.5 0.5 0';
454                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
455                         R_PolygonVertex(v, t, rgb, a);
456
457                 d = q - 0.25;
458                 if(d <= 0)
459                         R_EndPolygon();
460         }
461         else
462         {
463                 d = q;
464                 if(d > 0)
465                 {
466                         R_BeginPolygon(pic, drawflag);
467                                 v = centre;                     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.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
472                                 R_PolygonVertex(v, t, rgb, a);
473                 }
474         }
475
476         if(d > 0)
477         {
478                         v = centre;                     t = '0.5 0.5 0';
479                         v.x += x * 0.5 * ringsize.x;    t += x * '0.5 0.5 0';
480                         v.y += y * 0.5 * ringsize.y;    t += y * '0.5 -0.5 0';
481                         R_PolygonVertex(v, t, rgb, a);
482                 R_EndPolygon();
483         }
484 }
485
486 /** engine callback */
487 void URI_Get_Callback(int id, float status, string data)
488 {
489         if(url_URI_Get_Callback(id, status, data))
490         {
491                 // handled
492         }
493         else if (id == URI_GET_DISCARD)
494         {
495                 // discard
496         }
497         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
498         {
499                 // sv_cmd curl
500                 Curl_URI_Get_Callback(id, status, data);
501         }
502         else
503         {
504                 LOG_INFOF("Received HTTP request data for an invalid id %d.\n", id);
505         }
506 }
507
508 void Accuracy_LoadLevels()
509 {
510         if(autocvar_accuracy_color_levels != acc_color_levels)
511         {
512                 if(acc_color_levels)
513                         strunzone(acc_color_levels);
514                 acc_color_levels = strzone(autocvar_accuracy_color_levels);
515                 acc_levels = tokenize_console(acc_color_levels);
516                 if(acc_levels > MAX_ACCURACY_LEVELS)
517                         acc_levels = MAX_ACCURACY_LEVELS;
518                 if(acc_levels < 2)
519                         LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values\n");
520
521                 int i;
522                 for(i = 0; i < acc_levels; ++i)
523                         acc_lev[i] = stof(argv(i)) / 100.0;
524         }
525 }
526
527 void Accuracy_LoadColors()
528 {
529         if(time > acc_col_loadtime)
530         if(acc_levels >= 2)
531         {
532                 int i;
533                 for(i = 0; i < acc_levels; ++i)
534                         acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
535                 acc_col_loadtime = time + 2;
536         }
537 }
538
539 vector Accuracy_GetColor(float accuracy)
540 {
541         float factor;
542         vector color;
543         if(acc_levels < 2)
544                 return '0 0 0'; // return black, can't determine the right color
545
546         // find the max level lower than acc
547         int j = acc_levels-1;
548         while(j && accuracy < acc_lev[j])
549                 --j;
550
551         // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
552         factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
553         color = acc_col[j];
554         color = color + factor * (acc_col[j+1] - color);
555         return color;
556 }
557