1 #include "miscfunctions.qh"
3 #include "autocvars.qh"
7 #include <common/command/_mod.qh>
9 #include <common/teams.qh>
11 #include <lib/csqcmodel/cl_model.qh>
20 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
22 if(prev != e.sort_prev)
23 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
27 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
29 if(prev != e.sort_prev)
30 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
35 float RegisterPlayer(entity player)
39 for(pl = players.sort_next; pl; pl = pl.sort_next)
41 error("Player already registered!");
42 player.sort_next = players.sort_next;
43 player.sort_prev = players;
45 players.sort_next.sort_prev = player;
46 players.sort_next = player;
51 void RemovePlayer(entity player)
56 for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
61 error("Trying to remove a player which is not in the playerlist!");
64 parent.sort_next = player.sort_next;
66 player.sort_next.sort_prev = parent;
70 void MoveToLast(entity e)
73 entity ent = e.sort_next;
82 float RegisterTeam(entity Team)
84 assert_once(Team.team, eprint(Team));
87 for(tm = teams.sort_next; tm; tm = tm.sort_next)
89 error("Team already registered!");
90 Team.sort_next = teams.sort_next;
91 Team.sort_prev = teams;
93 teams.sort_next.sort_prev = Team;
94 teams.sort_next = Team;
95 if(Team.team && Team.team != NUM_SPECTATOR)
101 void RemoveTeam(entity Team)
106 for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
111 LOG_INFO(_("Trying to remove a team which is not in the teamlist!"));
114 parent.sort_next = Team.sort_next;
116 Team.sort_next.sort_prev = parent;
117 if(Team.team && Team.team != NUM_SPECTATOR)
122 entity GetTeam(int Team, bool add)
124 TC(int, Team); TC(bool, add);
125 int num = (Team == NUM_SPECTATOR) ? 16 : Team;
127 return teamslots[num];
130 entity tm = new_pure(team);
137 vector HUD_GetFontsize(string cvarname)
140 v = stov(cvar_string(cvarname));
149 float PreviewExists(string name)
151 if(autocvar_cl_readpicture_force)
154 if (fexists(strcat(name, ".tga"))) return true;
155 if (fexists(strcat(name, ".png"))) return true;
156 if (fexists(strcat(name, ".jpg"))) return true;
157 if (fexists(strcat(name, ".pcx"))) return true;
162 // decolorizes and team colors the player name when needed
163 string playername(string thename, float teamid)
169 t = Team_ColorCode(teamid);
170 return strcat(t, strdecolorize(thename));
173 return strdecolorize(thename);
176 float cvar_or(string cv, float v)
186 vector project_3d_to_2d(vector vec)
188 vec = cs_project(vec);
189 if(cs_project_is_b0rked > 0)
191 vec.x *= vid_conwidth / vid_width;
192 vec.y *= vid_conheight / vid_height;
197 bool projected_on_screen(vector screen_pos)
199 return screen_pos.z >= 0
202 && screen_pos.x < vid_conwidth
203 && screen_pos.y < vid_conheight;
206 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
208 return 1.2 / (1.2 - fadelerp);
211 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
213 boxsize.x *= boxxsizefactor; // easier interface for text
214 return boxsize * (0.5 * (1 - sz));
217 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
219 vector line_dim = '0 0 0';
221 // left and right lines
223 line_dim.x = thickness;
225 drawfill(pos, line_dim, color, theAlpha, drawflag);
226 drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
228 // upper and lower lines
230 line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
231 line_dim.y = thickness;
232 drawfill(pos, line_dim, color, theAlpha, drawflag);
233 drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
236 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
238 pos = HUD_Shift(pos);
240 area = HUD_Scale(area);
242 vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
243 end_pos = pos + area;
245 current_pos.y = pos.y;
246 while (current_pos.y < end_pos.y)
248 current_pos.x = pos.x;
249 while (current_pos.x < end_pos.x)
251 new_size.x = min(sz.x, end_pos.x - current_pos.x);
252 new_size.y = min(sz.y, end_pos.y - current_pos.y);
253 ratio.x = new_size.x / sz.x;
254 ratio.y = new_size.y / sz.y;
255 drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
256 current_pos.x += sz.x;
258 current_pos.y += sz.y;
262 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
265 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
267 drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
270 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
272 drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
273 drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
276 void HUD_Scale_Disable()
280 drawfontscale = hud_scale;
283 void HUD_Scale_Enable()
285 hud_scale = hud_scale_current;
286 hud_shift = hud_shift_current;
287 drawfontscale = hud_scale;
290 vector HUD_Scale(vector v)
292 v.x = HUD_ScaleX(v.x);
293 v.y = HUD_ScaleY(v.y);
297 vector HUD_Shift(vector v)
299 v.x = HUD_ShiftX(v.x);
300 v.y = HUD_ShiftY(v.y);
304 float stringwidth(string text, float handleColors, vector sz)
306 vector dfs = drawfontscale;
307 drawfontscale = '1 1 0';
308 float r = stringwidth_builtin(text, handleColors, sz);
313 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
314 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
315 SET_POS_AND_SZ_Y_ASPECT(false);
316 drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
319 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
320 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
321 SET_POS_AND_SZ_Y_ASPECT(true);
322 drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
325 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
328 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
330 drawfontscale = hud_scale * sz;
331 vector dfs = drawfontscale;
332 drawfontscale = sz * '1 1 0';
333 float textaspect = stringwidth_builtin(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz);
335 drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, textaspect), text, HUD_Scale(theScale * (sz / drawfontscale.x)), rgb, theAlpha * (1 - fadelerp), flag);
337 // (scale_x * sz / drawfontscale.x) * drawfontscale.x * SIZE1 / (scale_x * sz)
339 drawfontscale = hud_scale;
342 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
343 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
344 SET_POS_AND_SZ_Y_ASPECT(false);
345 drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
348 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
351 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
353 drawfontscale = hud_scale * sz;
354 // eventually replace with drawcolorcodedstring
355 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);
356 drawfontscale = hud_scale;
359 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
360 SET_POS_AND_SZ_Y_ASPECT(true);
361 drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
364 void update_mousepos()
366 mousepos += getmousepos() * autocvar_menu_mouse_speed;
367 mousepos.x = bound(0, mousepos.x, vid_conwidth);
368 mousepos.y = bound(0, mousepos.y, vid_conheight);
371 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
372 float PolyDrawModelSurface(entity e, float i_s)
378 tex = getsurfacetexture(e, i_s);
380 return 0; // this is beyond the last one
381 n_t = getsurfacenumtriangles(e, i_s);
382 for(i_t = 0; i_t < n_t; ++i_t)
384 tri = getsurfacetriangle(e, i_s, i_t);
385 R_BeginPolygon(tex, 0, false);
386 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
387 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
388 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
393 void PolyDrawModel(entity e)
396 for(i_s = 0; ; ++i_s)
397 if(!PolyDrawModelSurface(e, i_s))
401 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
403 vector ringsize, v, t;
404 ringsize = radi * '1 1 0';
405 centre = HUD_Shift(centre);
406 ringsize = HUD_Scale(ringsize);
408 float co = cos(f * 2 * M_PI);
409 float si = sin(f * 2 * M_PI);
410 float q = fabs(co) + fabs(si);
416 // draw full rectangle
417 R_BeginPolygon(pic, drawflag, true);
418 v = centre; t = '0.5 0.5 0';
419 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
420 R_PolygonVertex(v, t, rgb, a);
422 v = centre; t = '0.5 0.5 0';
423 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
424 R_PolygonVertex(v, t, rgb, a);
426 v = centre; t = '0.5 0.5 0';
427 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
428 R_PolygonVertex(v, t, rgb, a);
430 v = centre; t = '0.5 0.5 0';
431 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
432 R_PolygonVertex(v, t, rgb, a);
434 return; // Complete rectangle, nothing more needed.
438 // draw upper half in full
439 R_BeginPolygon(pic, drawflag, true);
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);
444 v = centre; t = '0.5 0.5 0';
445 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
446 R_PolygonVertex(v, t, rgb, a);
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);
452 // draw clipped lower half as a quad
453 R_BeginPolygon(pic, drawflag, true);
454 v = centre; t = '0.5 0.5 0';
455 R_PolygonVertex(v, t, rgb, a);
457 v = centre; t = '0.5 0.5 0';
458 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
459 R_PolygonVertex(v, t, rgb, a);
461 v = centre; t = '0.5 0.5 0';
462 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
463 R_PolygonVertex(v, t, rgb, a);
467 // draw upper half in full
468 R_BeginPolygon(pic, drawflag, true);
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);
473 v = centre; t = '0.5 0.5 0';
474 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
475 R_PolygonVertex(v, t, rgb, a);
477 v = centre; t = '0.5 0.5 0';
478 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
479 R_PolygonVertex(v, t, rgb, a);
481 // draw clipped lower half as a triangle
482 R_BeginPolygon(pic, drawflag, true);
483 v = centre; t = '0.5 0.5 0';
484 R_PolygonVertex(v, t, rgb, a);
486 v = centre; t = '0.5 0.5 0';
487 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
488 R_PolygonVertex(v, t, rgb, a);
492 // draw clipped lower half as a quad
493 R_BeginPolygon(pic, drawflag, true);
494 v = centre; t = '0.5 0.5 0';
495 R_PolygonVertex(v, t, rgb, a);
497 v = centre; t = '0.5 0.5 0';
498 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
499 R_PolygonVertex(v, t, rgb, a);
501 v = centre; t = '0.5 0.5 0';
502 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
503 R_PolygonVertex(v, t, rgb, a);
507 // draw clipped lower half as a triangle
508 R_BeginPolygon(pic, drawflag, true);
509 v = centre; t = '0.5 0.5 0';
510 R_PolygonVertex(v, t, rgb, a);
512 v = centre; t = '0.5 0.5 0';
513 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
514 R_PolygonVertex(v, t, rgb, a);
522 // The last, moving vertex.
523 v = centre; t = '0.5 0.5 0';
524 v.x += co * 0.5 * ringsize.x; t += co * '0.5 0.5 0';
525 v.y += si * 0.5 * ringsize.y; t += si * '0.5 -0.5 0';
526 R_PolygonVertex(v, t, rgb, a);
530 /** engine callback */
531 void URI_Get_Callback(int id, int status, string data)
533 TC(int, id); TC(int, status);
534 if(url_URI_Get_Callback(id, status, data))
538 else if (id == URI_GET_DISCARD)
542 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
545 Curl_URI_Get_Callback(id, status, data);
549 LOG_INFOF("Received HTTP request data for an invalid id %d.", id);
553 void Accuracy_LoadLevels()
555 if(autocvar_accuracy_color_levels != acc_color_levels)
557 strcpy(acc_color_levels, autocvar_accuracy_color_levels);
558 acc_levels = tokenize_console(acc_color_levels);
559 if(acc_levels > MAX_ACCURACY_LEVELS)
560 acc_levels = MAX_ACCURACY_LEVELS;
562 LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values");
565 for(i = 0; i < acc_levels; ++i)
566 acc_lev[i] = stof(argv(i)) / 100.0;
570 void Accuracy_LoadColors()
572 if(time > acc_col_loadtime)
576 for(i = 0; i < acc_levels; ++i)
577 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
578 acc_col_loadtime = time + 2;
582 vector Accuracy_GetColor(float accuracy)
587 return '0 0 0'; // return black, can't determine the right color
589 // find the max level lower than acc
590 int j = acc_levels-1;
591 while(j && accuracy < acc_lev[j])
594 // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
595 factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
597 color = color + factor * (acc_col[j+1] - color);