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