1 #include "miscfunctions.qh"
5 #include <common/command/_mod.qh>
7 #include <common/teams.qh>
9 #include <lib/csqcmodel/cl_model.qh>
18 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
20 if(prev != e.sort_prev)
21 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
25 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
27 if(prev != e.sort_prev)
28 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
33 float RegisterPlayer(entity player)
37 for(pl = players.sort_next; pl; pl = pl.sort_next)
39 error("Player already registered!");
40 player.sort_next = players.sort_next;
41 player.sort_prev = players;
43 players.sort_next.sort_prev = player;
44 players.sort_next = player;
49 void RemovePlayer(entity player)
54 for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
59 error("Trying to remove a player which is not in the playerlist!");
62 parent.sort_next = player.sort_next;
64 player.sort_next.sort_prev = parent;
68 void MoveToLast(entity e)
71 entity ent = e.sort_next;
80 float RegisterTeam(entity Team)
82 assert_once(Team.team, eprint(Team));
85 for(tm = teams.sort_next; tm; tm = tm.sort_next)
87 error("Team already registered!");
88 Team.sort_next = teams.sort_next;
89 Team.sort_prev = teams;
91 teams.sort_next.sort_prev = Team;
92 teams.sort_next = Team;
93 if(Team.team && Team.team != NUM_SPECTATOR)
99 void RemoveTeam(entity Team)
104 for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
109 LOG_INFO(_("Trying to remove a team which is not in the teamlist!"));
112 parent.sort_next = Team.sort_next;
114 Team.sort_next.sort_prev = parent;
115 if(Team.team && Team.team != NUM_SPECTATOR)
120 entity GetTeam(int Team, bool add)
122 TC(int, Team); TC(bool, add);
123 int num = (Team == NUM_SPECTATOR) ? 16 : Team;
125 return teamslots[num];
128 entity tm = new_pure(team);
135 vector HUD_GetFontsize(string cvarname)
138 v = stov(cvar_string(cvarname));
147 float PreviewExists(string name)
149 if(autocvar_cl_readpicture_force)
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;
160 // decolorizes and team colors the player name when needed
161 string playername(string thename, float teamid)
167 t = Team_ColorCode(teamid);
168 return strcat(t, strdecolorize(thename));
171 return strdecolorize(thename);
174 float cvar_or(string cv, float v)
184 vector project_3d_to_2d(vector vec)
186 vec = cs_project(vec);
187 if(cs_project_is_b0rked > 0)
189 vec.x *= vid_conwidth / vid_width;
190 vec.y *= vid_conheight / vid_height;
195 bool projected_on_screen(vector screen_pos)
197 return screen_pos.z >= 0
200 && screen_pos.x < vid_conwidth
201 && screen_pos.y < vid_conheight;
204 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
206 return 1.2 / (1.2 - fadelerp);
209 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
211 boxsize.x *= boxxsizefactor; // easier interface for text
212 return boxsize * (0.5 * (1 - sz));
215 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
217 vector line_dim = '0 0 0';
219 // left and right lines
221 line_dim.x = thickness;
223 drawfill(pos, line_dim, color, theAlpha, drawflag);
224 drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
226 // upper and lower lines
228 line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
229 line_dim.y = thickness;
230 drawfill(pos, line_dim, color, theAlpha, drawflag);
231 drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
234 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
236 pos = HUD_Shift(pos);
238 area = HUD_Scale(area);
240 vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
241 end_pos = pos + area;
243 current_pos.y = pos.y;
244 while (current_pos.y < end_pos.y)
246 current_pos.x = pos.x;
247 while (current_pos.x < end_pos.x)
249 new_size.x = min(sz.x, end_pos.x - current_pos.x);
250 new_size.y = min(sz.y, end_pos.y - current_pos.y);
251 ratio.x = new_size.x / sz.x;
252 ratio.y = new_size.y / sz.y;
253 drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
254 current_pos.x += sz.x;
256 current_pos.y += sz.y;
260 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
263 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
265 drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
268 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
270 drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
271 drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
274 void HUD_Scale_Disable()
278 drawfontscale = hud_scale;
281 void HUD_Scale_Enable()
283 hud_scale = hud_scale_current;
284 hud_shift = hud_shift_current;
285 drawfontscale = hud_scale;
288 vector HUD_Scale(vector v)
290 v.x = HUD_ScaleX(v.x);
291 v.y = HUD_ScaleY(v.y);
295 vector HUD_Shift(vector v)
297 v.x = HUD_ShiftX(v.x);
298 v.y = HUD_ShiftY(v.y);
302 float stringwidth(string text, float handleColors, vector sz)
304 vector dfs = drawfontscale;
305 drawfontscale = '1 1 0';
306 float r = stringwidth_builtin(text, handleColors, sz);
311 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
312 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
313 SET_POS_AND_SZ_Y_ASPECT(false);
314 drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
317 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
318 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
319 SET_POS_AND_SZ_Y_ASPECT(true);
320 drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
323 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
326 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
328 drawfontscale = hud_scale * sz;
329 vector dfs = drawfontscale;
330 drawfontscale = sz * '1 1 0';
331 float textaspect = stringwidth_builtin(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz);
333 drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, textaspect), text, HUD_Scale(theScale * (sz / drawfontscale.x)), rgb, theAlpha * (1 - fadelerp), flag);
335 // (scale_x * sz / drawfontscale.x) * drawfontscale.x * SIZE1 / (scale_x * sz)
337 drawfontscale = hud_scale;
340 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
341 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
342 SET_POS_AND_SZ_Y_ASPECT(false);
343 drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
346 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
349 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
351 drawfontscale = hud_scale * sz;
352 // eventually replace with drawcolorcodedstring
353 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);
354 drawfontscale = hud_scale;
357 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
358 SET_POS_AND_SZ_Y_ASPECT(true);
359 drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
362 void update_mousepos()
364 mousepos += getmousepos() * autocvar_menu_mouse_speed;
365 mousepos.x = bound(0, mousepos.x, vid_conwidth);
366 mousepos.y = bound(0, mousepos.y, vid_conheight);
369 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
370 float PolyDrawModelSurface(entity e, float i_s)
376 tex = getsurfacetexture(e, i_s);
378 return 0; // this is beyond the last one
379 n_t = getsurfacenumtriangles(e, i_s);
380 for(i_t = 0; i_t < n_t; ++i_t)
382 tri = getsurfacetriangle(e, i_s, i_t);
383 R_BeginPolygon(tex, 0);
384 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
385 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
386 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
391 void PolyDrawModel(entity e)
394 for(i_s = 0; ; ++i_s)
395 if(!PolyDrawModelSurface(e, i_s))
399 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
402 vector ringsize, v, t;
403 ringsize = radi * '1 1 0';
404 centre = HUD_Shift(centre);
405 ringsize = HUD_Scale(ringsize);
407 float co = cos(f * 2 * M_PI);
408 float si = sin(f * 2 * M_PI);
409 float q = fabs(co) + fabs(si);
415 // draw full rectangle
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);
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);
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);
429 v = centre; t = '0.5 0.5 0';
430 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
431 R_PolygonVertex(v, t, rgb, a);
437 R_BeginPolygon(pic, drawflag);
438 v = centre; t = '0.5 0.5 0';
439 R_PolygonVertex(v, t, rgb, a);
441 v = centre; t = '0.5 0.5 0';
442 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
443 R_PolygonVertex(v, t, rgb, a);
448 // draw upper and first triangle
449 R_BeginPolygon(pic, drawflag);
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);
454 v = centre; t = '0.5 0.5 0';
455 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
456 R_PolygonVertex(v, t, rgb, a);
458 v = centre; t = '0.5 0.5 0';
459 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
460 R_PolygonVertex(v, t, rgb, a);
462 R_BeginPolygon(pic, drawflag);
463 v = centre; t = '0.5 0.5 0';
464 R_PolygonVertex(v, t, rgb, a);
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);
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);
480 // draw upper triangle
481 R_BeginPolygon(pic, drawflag);
482 v = centre; t = '0.5 0.5 0';
483 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
484 R_PolygonVertex(v, t, rgb, a);
486 v = centre; t = '0.5 0.5 0';
487 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
488 R_PolygonVertex(v, t, rgb, a);
490 v = centre; t = '0.5 0.5 0';
491 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
492 R_PolygonVertex(v, t, rgb, a);
498 R_BeginPolygon(pic, drawflag);
499 v = centre; t = '0.5 0.5 0';
500 R_PolygonVertex(v, t, rgb, a);
502 v = centre; t = '0.5 0.5 0';
503 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
504 R_PolygonVertex(v, t, rgb, a);
509 // draw first triangle
510 R_BeginPolygon(pic, drawflag);
511 v = centre; t = '0.5 0.5 0';
512 R_PolygonVertex(v, t, rgb, a);
514 v = centre; t = '0.5 0.5 0';
515 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
516 R_PolygonVertex(v, t, rgb, a);
518 v = centre; t = '0.5 0.5 0';
519 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
520 R_PolygonVertex(v, t, rgb, a);
531 R_BeginPolygon(pic, drawflag);
532 v = centre; t = '0.5 0.5 0';
533 R_PolygonVertex(v, t, rgb, a);
535 v = centre; t = '0.5 0.5 0';
536 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
537 R_PolygonVertex(v, t, rgb, a);
543 v = centre; t = '0.5 0.5 0';
544 v.x += co * 0.5 * ringsize.x; t += co * '0.5 0.5 0';
545 v.y += si * 0.5 * ringsize.y; t += si * '0.5 -0.5 0';
546 R_PolygonVertex(v, t, rgb, a);
551 /** engine callback */
552 void URI_Get_Callback(int id, int status, string data)
554 TC(int, id); TC(int, status);
555 if(url_URI_Get_Callback(id, status, data))
559 else if (id == URI_GET_DISCARD)
563 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
566 Curl_URI_Get_Callback(id, status, data);
570 LOG_INFOF("Received HTTP request data for an invalid id %d.\n", id);
574 void Accuracy_LoadLevels()
576 if(autocvar_accuracy_color_levels != acc_color_levels)
579 strunzone(acc_color_levels);
580 acc_color_levels = strzone(autocvar_accuracy_color_levels);
581 acc_levels = tokenize_console(acc_color_levels);
582 if(acc_levels > MAX_ACCURACY_LEVELS)
583 acc_levels = MAX_ACCURACY_LEVELS;
585 LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values\n");
588 for(i = 0; i < acc_levels; ++i)
589 acc_lev[i] = stof(argv(i)) / 100.0;
593 void Accuracy_LoadColors()
595 if(time > acc_col_loadtime)
599 for(i = 0; i < acc_levels; ++i)
600 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
601 acc_col_loadtime = time + 2;
605 vector Accuracy_GetColor(float accuracy)
610 return '0 0 0'; // return black, can't determine the right color
612 // find the max level lower than acc
613 int j = acc_levels-1;
614 while(j && accuracy < acc_lev[j])
617 // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
618 factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
620 color = color + factor * (acc_col[j+1] - color);