X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fclient%2Fmiscfunctions.qc;h=a653f4bf1e5ca0af486b06dd58d6f61aafd143a1;hb=HEAD;hp=a7af09c7c82a146fcc259558ae8c46e9bd789a2c;hpb=e824d4612617171b932bba08f1a5a50500d4a603;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/client/miscfunctions.qc b/qcsrc/client/miscfunctions.qc deleted file mode 100644 index a7af09c7c..000000000 --- a/qcsrc/client/miscfunctions.qc +++ /dev/null @@ -1,621 +0,0 @@ -#include "miscfunctions.qh" - -#include "autocvars.qh" -#include "defs.qh" -#include "hud/_mod.qh" - -#include - -#include - -#include - - -void AuditLists() -{ - entity e; - entity prev; - - prev = players; - for(e = prev.sort_next; e; prev = e, e = e.sort_next) - { - if(prev != e.sort_prev) - error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers")); - } - - prev = teams; - for(e = prev.sort_next; e; prev = e, e = e.sort_next) - { - if(prev != e.sort_prev) - error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers")); - } -} - - -float RegisterPlayer(entity player) -{ - entity pl; - AuditLists(); - for(pl = players.sort_next; pl; pl = pl.sort_next) - if(pl == player) - error("Player already registered!"); - player.sort_next = players.sort_next; - player.sort_prev = players; - if(players.sort_next) - players.sort_next.sort_prev = player; - players.sort_next = player; - AuditLists(); - return true; -} - -void RemovePlayer(entity player) -{ - entity pl, parent; - AuditLists(); - parent = players; - for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next) - parent = pl; - - if(!pl) - { - error("Trying to remove a player which is not in the playerlist!"); - return; - } - parent.sort_next = player.sort_next; - if(player.sort_next) - player.sort_next.sort_prev = parent; - AuditLists(); -} - -void MoveToLast(entity e) -{ - AuditLists(); - entity ent = e.sort_next; - while(ent) - { - SORT_SWAP(ent, e); - ent = e.sort_next; - } - AuditLists(); -} - -float RegisterTeam(entity Team) -{ - assert_once(Team.team, eprint(Team)); - entity tm; - AuditLists(); - for(tm = teams.sort_next; tm; tm = tm.sort_next) - if(tm == Team) - error("Team already registered!"); - Team.sort_next = teams.sort_next; - Team.sort_prev = teams; - if(teams.sort_next) - teams.sort_next.sort_prev = Team; - teams.sort_next = Team; - if(Team.team && Team.team != NUM_SPECTATOR) - ++team_count; - AuditLists(); - return true; -} - -void RemoveTeam(entity Team) -{ - entity tm, parent; - AuditLists(); - parent = teams; - for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next) - parent = tm; - - if(!tm) - { - LOG_INFO(_("Trying to remove a team which is not in the teamlist!")); - return; - } - parent.sort_next = Team.sort_next; - if(Team.sort_next) - Team.sort_next.sort_prev = parent; - if(Team.team && Team.team != NUM_SPECTATOR) - --team_count; - AuditLists(); -} - -entity GetTeam(int Team, bool add) -{ - TC(int, Team); TC(bool, add); - int num = (Team == NUM_SPECTATOR) ? 16 : Team; - if(teamslots[num]) - return teamslots[num]; - if (!add) - return NULL; - entity tm = new_pure(team); - tm.team = Team; - teamslots[num] = tm; - RegisterTeam(tm); - return tm; -} - -vector HUD_GetFontsize(string cvarname) -{ - vector v; - v = stov(cvar_string(cvarname)); - if(v.x == 0) - v = '8 8 0'; - if(v.y == 0) - v.y = v.x; - v.z = 0; - return v; -} - -float PreviewExists(string name) -{ - if(autocvar_cl_readpicture_force) - return false; - - if (fexists(strcat(name, ".tga"))) return true; - if (fexists(strcat(name, ".png"))) return true; - if (fexists(strcat(name, ".jpg"))) return true; - if (fexists(strcat(name, ".pcx"))) return true; - - return false; -} - -// decolorizes and team colors the player name when needed -string playername(string thename, float teamid) -{ - TC(int, teamid); - string t; - if (teamplay) - { - t = Team_ColorCode(teamid); - return strcat(t, strdecolorize(thename)); - } - else - return strdecolorize(thename); -} - -float cvar_or(string cv, float v) -{ - string s; - s = cvar_string(cv); - if(s == "") - return v; - else - return stof(s); -} - -vector project_3d_to_2d(vector vec) -{ - vec = cs_project(vec); - if(cs_project_is_b0rked > 0) - { - vec.x *= vid_conwidth / vid_width; - vec.y *= vid_conheight / vid_height; - } - return vec; -} - -bool projected_on_screen(vector screen_pos) -{ - return screen_pos.z >= 0 - && screen_pos.x >= 0 - && screen_pos.y >= 0 - && screen_pos.x < vid_conwidth - && screen_pos.y < vid_conheight; -} - -float expandingbox_sizefactor_from_fadelerp(float fadelerp) -{ - return 1.2 / (1.2 - fadelerp); -} - -vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor) -{ - boxsize.x *= boxxsizefactor; // easier interface for text - return boxsize * (0.5 * (1 - sz)); -} - -// NOTE base is the central value -// freq: circle frequency, = 2*pi*frequency in hertz -// start_pos: -// -1 start from the lower value -// 0 start from the base value -// 1 start from the higher value -float blink_synced(float base, float range, float freq, float start_time, int start_pos) -{ - // note: - // RMS = sqrt(base^2 + 0.5 * range^2) - // thus - // base = sqrt(RMS^2 - 0.5 * range^2) - // ensure RMS == 1 - - return base + range * sin((time - start_time - (M_PI / 2) * start_pos) * freq); -} - -float blink(float base, float range, float freq) -{ - return blink_synced(base, range, freq, 0, 0); -} - -void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag) -{ - vector line_dim = '0 0 0'; - - // left and right lines - pos.x -= thickness; - line_dim.x = thickness; - line_dim.y = dim.y; - drawfill(pos, line_dim, color, theAlpha, drawflag); - drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag); - - // upper and lower lines - pos.y -= thickness; - line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer - line_dim.y = thickness; - drawfill(pos, line_dim, color, theAlpha, drawflag); - drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag); -} - -void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag) -{ - pos = HUD_Shift(pos); - sz = HUD_Scale(sz); - area = HUD_Scale(area); - - vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0'; - end_pos = pos + area; - - current_pos.y = pos.y; - while (current_pos.y < end_pos.y) - { - current_pos.x = pos.x; - while (current_pos.x < end_pos.x) - { - new_size.x = min(sz.x, end_pos.x - current_pos.x); - new_size.y = min(sz.y, end_pos.y - current_pos.y); - ratio.x = new_size.x / sz.x; - ratio.y = new_size.y / sz.y; - drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag); - current_pos.x += sz.x; - } - current_pos.y += sz.y; - } -} - -void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp) -{ - float sz; - sz = expandingbox_sizefactor_from_fadelerp(fadelerp); - - drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag); -} - -void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp) -{ - drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp); - drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag); -} - -void HUD_Scale_Disable() -{ - hud_scale = '1 1 0'; - hud_shift = '0 0 0'; - drawfontscale = hud_scale; -} - -void HUD_Scale_Enable() -{ - hud_scale = hud_scale_current; - hud_shift = hud_shift_current; - drawfontscale = hud_scale; -} - -vector HUD_Scale(vector v) -{ - v.x = HUD_ScaleX(v.x); - v.y = HUD_ScaleY(v.y); - return v; -} - -vector HUD_Shift(vector v) -{ - v.x = HUD_ShiftX(v.x); - v.y = HUD_ShiftY(v.y); - return v; -} - -float stringwidth(string text, float handleColors, vector sz) -{ - vector dfs = drawfontscale; - drawfontscale = '1 1 0'; - float r = stringwidth_builtin(text, handleColors, sz); - drawfontscale = dfs; - return r; -} - -// drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box -void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) { - SET_POS_AND_SZ_Y_ASPECT(false); - drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag); -} - -// drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box -void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) { - SET_POS_AND_SZ_Y_ASPECT(true); - drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag); -} - -void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp) -{ - float sz; - sz = expandingbox_sizefactor_from_fadelerp(fadelerp); - - drawfontscale = hud_scale * sz; - vector dfs = drawfontscale; - drawfontscale = sz * '1 1 0'; - float textaspect = stringwidth_builtin(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz); - drawfontscale = dfs; - drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, textaspect), text, HUD_Scale(theScale * (sz / drawfontscale.x)), rgb, theAlpha * (1 - fadelerp), flag); - // width parameter: - // (scale_x * sz / drawfontscale.x) * drawfontscale.x * SIZE1 / (scale_x * sz) - // SIZE1 - drawfontscale = hud_scale; -} - -// drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box -void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) { - SET_POS_AND_SZ_Y_ASPECT(false); - drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp); -} - -void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp) -{ - float sz; - sz = expandingbox_sizefactor_from_fadelerp(fadelerp); - - drawfontscale = hud_scale * sz; - // eventually replace with drawcolorcodedstring - 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); - drawfontscale = hud_scale; -} - -void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) { - SET_POS_AND_SZ_Y_ASPECT(true); - drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp); -} - -void update_mousepos() -{ - mousepos += getmousepos() * autocvar_menu_mouse_speed; - mousepos.x = bound(0, mousepos.x, vid_conwidth); - mousepos.y = bound(0, mousepos.y, vid_conheight); -} - -// this draws the triangles of a model DIRECTLY. Don't expect high performance, really... -float PolyDrawModelSurface(entity e, float i_s) -{ - float i_t; - float n_t; - vector tri; - string tex; - tex = getsurfacetexture(e, i_s); - if (!tex) - return 0; // this is beyond the last one - n_t = getsurfacenumtriangles(e, i_s); - for(i_t = 0; i_t < n_t; ++i_t) - { - tri = getsurfacetriangle(e, i_s, i_t); - R_BeginPolygon(tex, 0, false); - R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1); - R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1); - R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1); - R_EndPolygon(); - } - return 1; -} -void PolyDrawModel(entity e) -{ - float i_s; - for(i_s = 0; ; ++i_s) - if(!PolyDrawModelSurface(e, i_s)) - break; -} - -void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag) -{ - vector ringsize, v, t; - ringsize = radi * '1 1 0'; - centre = HUD_Shift(centre); - ringsize = HUD_Scale(ringsize); - - float co = cos(f * 2 * M_PI); - float si = sin(f * 2 * M_PI); - float q = fabs(co) + fabs(si); - co /= q; - si /= q; - - if(f >= 1) - { - // draw full rectangle - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - v.x += 0.5 * ringsize.x; t += '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - R_EndPolygon(); - return; // Complete rectangle, nothing more needed. - } - else if(f > 0.75) - { - // draw upper half in full - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - v.x += 0.5 * ringsize.x; t += '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - R_EndPolygon(); - // draw clipped lower half as a quad - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - } - else if(f > 0.5) - { - // draw upper half in full - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - v.x += 0.5 * ringsize.x; t += '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - R_EndPolygon(); - // draw clipped lower half as a triangle - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - } - else if(f > 0.25) - { - // draw clipped lower half as a quad - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x += 0.5 * ringsize.x; t += '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - } - else if (f > 0) - { - // draw clipped lower half as a triangle - R_BeginPolygon(pic, drawflag, true); - v = centre; t = '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - - v = centre; t = '0.5 0.5 0'; - v.x += 0.5 * ringsize.x; t += '0.5 0.5 0'; - R_PolygonVertex(v, t, rgb, a); - } - else - { - // Nothing to draw. - return; - } - - // The last, moving vertex. - v = centre; t = '0.5 0.5 0'; - v.x += co * 0.5 * ringsize.x; t += co * '0.5 0.5 0'; - v.y += si * 0.5 * ringsize.y; t += si * '0.5 -0.5 0'; - R_PolygonVertex(v, t, rgb, a); - R_EndPolygon(); -} - -/** engine callback */ -void URI_Get_Callback(int id, int status, string data) -{ - TC(int, id); TC(int, status); - if(url_URI_Get_Callback(id, status, data)) - { - // handled - } - else if (id == URI_GET_DISCARD) - { - // discard - } - else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END) - { - // sv_cmd curl - Curl_URI_Get_Callback(id, status, data); - } - else - { - LOG_INFOF("Received HTTP request data for an invalid id %d.", id); - } -} - -void Accuracy_LoadLevels() -{ - if(autocvar_accuracy_color_levels != acc_color_levels) - { - strcpy(acc_color_levels, autocvar_accuracy_color_levels); - acc_levels = tokenize_console(acc_color_levels); - if(acc_levels > MAX_ACCURACY_LEVELS) - acc_levels = MAX_ACCURACY_LEVELS; - if(acc_levels < 2) - LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values"); - - int i; - for(i = 0; i < acc_levels; ++i) - acc_lev[i] = stof(argv(i)) / 100.0; - } -} - -void Accuracy_LoadColors() -{ - if(time > acc_col_loadtime) - if(acc_levels >= 2) - { - int i; - for(i = 0; i < acc_levels; ++i) - acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i)))); - acc_col_loadtime = time + 2; - } -} - -vector Accuracy_GetColor(float accuracy) -{ - float factor; - vector color; - if(acc_levels < 2) - return '0 0 0'; // return black, can't determine the right color - - // find the max level lower than acc - int j = acc_levels-1; - while(j && accuracy < acc_lev[j]) - --j; - - // inject color j+1 in color j, how much depending on how much accuracy is higher than level j - factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]); - color = acc_col[j]; - color = color + factor * (acc_col[j+1] - color); - return color; -}