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