diff --git a/inc/common/msg.h b/inc/common/msg.h index 8f70eda40..ddd1396c3 100644 --- a/inc/common/msg.h +++ b/inc/common/msg.h @@ -189,9 +189,9 @@ static inline int MSG_PackSolid16(const vec3_t mins, const vec3_t maxs) int zd = -mins[2] / 8; int zu = (maxs[2] + 32) / 8; - clamp(x, 1, 31); - clamp(zd, 1, 31); - clamp(zu, 1, 63); + x = Q_clip(x, 1, 31); + zd = Q_clip(zd, 1, 31); + zu = Q_clip(zu, 1, 63); return (zu << 10) | (zd << 5) | x; } @@ -202,9 +202,9 @@ static inline uint32_t MSG_PackSolid32_Ver1(const vec3_t mins, const vec3_t maxs int zd = -mins[2]; int zu = maxs[2] + 32768; - clamp(x, 1, 255); - clamp(zd, 0, 255); - clamp(zu, 0, 65535); + x = Q_clip(x, 1, 255); + zd = Q_clip_uint8(zd); + zu = Q_clip_uint16(zu); return ((uint32_t)zu << 16) | (zd << 8) | x; } @@ -216,10 +216,10 @@ static inline uint32_t MSG_PackSolid32_Ver2(const vec3_t mins, const vec3_t maxs int zd = -mins[2]; int zu = maxs[2] + 32; - clamp(x, 1, 255); - clamp(y, 1, 255); - clamp(zd, 0, 255); - clamp(zu, 0, 255); + x = Q_clip(x, 1, 255); + y = Q_clip(y, 1, 255); + zd = Q_clip_uint8(zd); + zu = Q_clip_uint8(zu); return MakeLittleLong(x, y, zd, zu); } diff --git a/inc/shared/shared.h b/inc/shared/shared.h index 6bc113c44..018f0d875 100644 --- a/inc/shared/shared.h +++ b/inc/shared/shared.h @@ -354,12 +354,65 @@ void Q_srand(uint32_t seed); uint32_t Q_rand(void); uint32_t Q_rand_uniform(uint32_t n); -#define clamp(a,b,c) ((a)<(b)?(a)=(b):(a)>(c)?(a)=(c):(a)) -#define cclamp(a,b,c) ((b)>(c)?clamp(a,c,b):clamp(a,b,c)) - static inline int Q_clip(int a, int b, int c) { - return clamp(a, b, c); + if (a < b) + return b; + if (a > c) + return c; + return a; +} + +static inline float Q_clipf(float a, float b, float c) +{ +#if defined(__GNUC__) && defined(__SSE__) + __asm__("maxss %1, %0 \n\t" + "minss %2, %0 \n\t" + : "+&x"(a) : "xm"(b), "xm"(c)); + return a; +#else + if (a < b) + return b; + if (a > c) + return c; + return a; +#endif +} + +static inline float Q_circ_clipf(float a, float b, float c) +{ + return b > c ? Q_clipf(a, c, b) : Q_clipf(a, b, c); +} + +static inline int8_t Q_clip_int8(int a) +{ + return ((a + 0x80U) & ~0xFF) ? (a >> 31) ^ 0x7F : a; +} + +static inline int16_t Q_clip_int16(int a) +{ + return ((a + 0x8000U) & ~0xFFFF) ? (a >> 31) ^ 0x7FFF : a; +} + +static inline int32_t Q_clip_int32(int64_t a) +{ + return ((a + 0x80000000ULL) & ~0xFFFFFFFFULL) ? (a >> 63) ^ 0x7FFFFFFF : a; +} + +#ifdef _LP64 +#define Q_clipl_int32(a) Q_clip_int32(a) +#else +#define Q_clipl_int32(a) (a) +#endif + +static inline uint8_t Q_clip_uint8(int a) +{ + return (a & ~0xFF) ? ~a >> 31 : a; +} + +static inline uint16_t Q_clip_uint16(int a) +{ + return (a & ~0xFFFF) ? ~a >> 31 : a; } #ifndef max @@ -484,6 +537,14 @@ char *Q_strchrnul(const char *s, int c); void *Q_memccpy(void *dst, const void *src, int c, size_t size); size_t Q_strnlen(const char *s, size_t maxlen); +#ifdef _WIN32 +#define Q_atoi(s) atoi(s) +#else +int Q_atoi(const char *s); +#endif + +#define Q_atof(s) strtof(s, NULL) + char *COM_SkipPath(const char *pathname); size_t COM_StripExtension(char *out, const char *in, size_t size); void COM_FilePath(const char *in, char *out, size_t size); diff --git a/src/client/ascii.c b/src/client/ascii.c index 69804574f..122220443 100644 --- a/src/client/ascii.c +++ b/src/client/ascii.c @@ -111,19 +111,19 @@ static void TH_DrawLayoutString(char *dst, const char *s) if (token[0] == 'x') { if (token[1] == 'l') { token = COM_Parse(&s); - x = atoi(token) / 8; + x = Q_atoi(token) / 8; continue; } if (token[1] == 'r') { token = COM_Parse(&s); - x = TH_WIDTH + atoi(token) / 8; + x = TH_WIDTH + Q_atoi(token) / 8; continue; } if (token[1] == 'v') { token = COM_Parse(&s); - x = TH_WIDTH / 2 - 20 + atoi(token) / 8; + x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8; continue; } } @@ -131,19 +131,19 @@ static void TH_DrawLayoutString(char *dst, const char *s) if (token[0] == 'y') { if (token[1] == 't') { token = COM_Parse(&s); - y = atoi(token) / 8; + y = Q_atoi(token) / 8; continue; } if (token[1] == 'b') { token = COM_Parse(&s); - y = TH_HEIGHT + atoi(token) / 8; + y = TH_HEIGHT + Q_atoi(token) / 8; continue; } if (token[1] == 'v') { token = COM_Parse(&s); - y = TH_HEIGHT / 2 - 15 + atoi(token) / 8; + y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8; continue; } } @@ -160,25 +160,25 @@ static void TH_DrawLayoutString(char *dst, const char *s) int score, ping, time; token = COM_Parse(&s); - x = TH_WIDTH / 2 - 20 + atoi(token) / 8; + x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8; token = COM_Parse(&s); - y = TH_HEIGHT / 2 - 15 + atoi(token) / 8; + y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8; token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_CLIENTS) { Com_Error(ERR_DROP, "%s: invalid client index", __func__); } ci = &cl.clientinfo[value]; token = COM_Parse(&s); - score = atoi(token); + score = Q_atoi(token); token = COM_Parse(&s); - ping = atoi(token); + ping = Q_atoi(token); token = COM_Parse(&s); - time = atoi(token); + time = Q_atoi(token); len = strlen(ci->name); TH_DrawString(dst, x + 4, y, ci->name, len); @@ -196,22 +196,22 @@ static void TH_DrawLayoutString(char *dst, const char *s) int score, ping; token = COM_Parse(&s); - x = TH_WIDTH / 2 - 20 + atoi(token) / 8; + x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8; token = COM_Parse(&s); - y = TH_HEIGHT / 2 - 15 + atoi(token) / 8; + y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8; token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_CLIENTS) { Com_Error(ERR_DROP, "%s: invalid client index", __func__); } ci = &cl.clientinfo[value]; token = COM_Parse(&s); - score = atoi(token); + score = Q_atoi(token); token = COM_Parse(&s); - ping = atoi(token); + ping = Q_atoi(token); if (ping > 999) ping = 999; @@ -230,9 +230,9 @@ static void TH_DrawLayoutString(char *dst, const char *s) if (!strcmp(token, "num")) { // draw a number token = COM_Parse(&s); - width = atoi(token); + width = Q_atoi(token); token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid stat index", __func__); } @@ -243,7 +243,7 @@ static void TH_DrawLayoutString(char *dst, const char *s) if (!strcmp(token, "stat_string")) { token = COM_Parse(&s); - index = atoi(token); + index = Q_atoi(token); if (index < 0 || index >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid string index", __func__); } @@ -272,7 +272,7 @@ static void TH_DrawLayoutString(char *dst, const char *s) if (!strcmp(token, "if")) { token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid stat index", __func__); } diff --git a/src/client/console.c b/src/client/console.c index edb434965..28988e161 100644 --- a/src/client/console.c +++ b/src/client/console.c @@ -366,16 +366,12 @@ If the line width has changed, reformat the buffer. */ void Con_CheckResize(void) { - int width; - con.scale = R_ClampScale(con_scale); con.vidWidth = Q_rint(r_config.width * con.scale); con.vidHeight = Q_rint(r_config.height * con.scale); - width = con.vidWidth / CHAR_WIDTH - 2; - - con.linewidth = clamp(width, 0, CON_LINEWIDTH); + con.linewidth = Q_clip(con.vidWidth / CHAR_WIDTH - 2, 0, CON_LINEWIDTH); con.prompt.inputLine.visibleChars = con.linewidth; con.prompt.widthInChars = con.linewidth; con.chatPrompt.inputLine.visibleChars = con.linewidth; diff --git a/src/client/demo.c b/src/client/demo.c index 045902d3d..8b07e85af 100644 --- a/src/client/demo.c +++ b/src/client/demo.c @@ -981,7 +981,7 @@ static void CL_Seek_f(void) return; } - clamp(percent, 0, 100); + percent = Q_clipf(percent, 0, 100); dest = cls.demo.file_offset + cls.demo.file_size * percent / 100; byte_seek = true; diff --git a/src/client/entities.c b/src/client/entities.c index 7cd4d9327..2402749dc 100644 --- a/src/client/entities.c +++ b/src/client/entities.c @@ -1379,8 +1379,7 @@ void CL_AddTestModel(void) VectorCopy(cl_testmodel_position, entity.origin); VectorCopy(cl_testmodel_position, entity.oldorigin); - entity.alpha = cl_testalpha->value; - clamp(entity.alpha, 0.f, 1.f); + entity.alpha = Q_clipf(cl_testalpha->value, 0.f, 1.f); if (entity.alpha < 1.f) entity.flags |= RF_TRANSLUCENT; diff --git a/src/client/input.c b/src/client/input.c index aa3162930..b49bb60f6 100644 --- a/src/client/input.c +++ b/src/client/input.c @@ -249,7 +249,7 @@ static void KeyDown(kbutton_t *b) c = Cmd_Argv(1); if (c[0]) - k = atoi(c); + k = Q_atoi(c); else k = -1; // typed manually at the console for continuous down @@ -270,7 +270,7 @@ static void KeyDown(kbutton_t *b) // save timestamp c = Cmd_Argv(2); - b->downtime = atoi(c); + b->downtime = Q_atoi(c); if (!b->downtime) { b->downtime = com_eventTime - 100; } @@ -286,7 +286,7 @@ static void KeyUp(kbutton_t *b) c = Cmd_Argv(1); if (c[0]) - k = atoi(c); + k = Q_atoi(c); else { // typed manually at the console, assume for unsticking, so clear all b->down[0] = b->down[1] = 0; @@ -308,7 +308,7 @@ static void KeyUp(kbutton_t *b) // save timestamp c = Cmd_Argv(2); - uptime = atoi(c); + uptime = Q_atoi(c); if (!uptime) { b->msec += 10; } else if (uptime > b->downtime) { @@ -384,7 +384,7 @@ static void IN_UseUp(void) static void IN_Impulse(void) { - in_impulse = atoi(Cmd_Argv(1)); + in_impulse = Q_atoi(Cmd_Argv(1)); } static void IN_CenterView(void) @@ -415,7 +415,6 @@ Returns the fraction of the frame that the key was down static float CL_KeyState(kbutton_t *key) { unsigned msec = key->msec; - float val; if (key->state & 1) { // still down @@ -429,9 +428,7 @@ static float CL_KeyState(kbutton_t *key) return (float)(key->state & 1); } - val = (float)msec / cl.cmd.msec; - - return clamp(val, 0, 1); + return Q_clipf((float)msec / cl.cmd.msec, 0, 1); } //========================================================================== @@ -565,11 +562,11 @@ static void CL_BaseMove(vec3_t move) static void CL_ClampSpeed(vec3_t move) { - float speed = 400; // default (maximum) running speed + const float speed = 400; // default (maximum) running speed - clamp(move[0], -speed, speed); - clamp(move[1], -speed, speed); - clamp(move[2], -speed, speed); + move[0] = Q_clipf(move[0], -speed, speed); + move[1] = Q_clipf(move[1], -speed, speed); + move[2] = Q_clipf(move[2], -speed, speed); } static void CL_ClampPitch(void) @@ -584,7 +581,7 @@ static void CL_ClampPitch(void) if (angle > 180) angle -= 360; // wrapped - clamp(angle, -89, 89); + angle = Q_clipf(angle, -89, 89); cl.viewangles[PITCH] = angle - pitch; } diff --git a/src/client/locs.c b/src/client/locs.c index c5aed84f9..63da07ba2 100644 --- a/src/client/locs.c +++ b/src/client/locs.c @@ -93,9 +93,9 @@ void LOC_LoadLocations(void) Com_WPrintf("Line %d is incomplete in %s\n", line, path); } else { loc = LOC_Alloc(Cmd_RawArgsFrom(3)); - loc->origin[0] = atof(Cmd_Argv(0)) * 0.125f; - loc->origin[1] = atof(Cmd_Argv(1)) * 0.125f; - loc->origin[2] = atof(Cmd_Argv(2)) * 0.125f; + loc->origin[0] = Q_atof(Cmd_Argv(0)) * 0.125f; + loc->origin[1] = Q_atof(Cmd_Argv(1)) * 0.125f; + loc->origin[2] = Q_atof(Cmd_Argv(2)) * 0.125f; List_Append(&cl_locations, &loc->entry); count++; } diff --git a/src/client/main.c b/src/client/main.c index da54e0425..9a3d00ac2 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -563,8 +563,7 @@ static void CL_FollowIP_f(void) if (Cmd_Argc() > 1) { // optional second argument references less recent address - j = atoi(Cmd_Argv(1)) + 1; - clamp(j, 1, RECENT_ADDR); + j = Q_clip(Q_atoi(Cmd_Argv(1)), 0, RECENT_ADDR - 1) + 1; } else { j = 1; } @@ -864,8 +863,8 @@ static void CL_ParseStatusResponse(serverStatus_t *status, const char *string) status->numPlayers = 0; while (status->numPlayers < MAX_STATUS_PLAYERS) { player = &status->players[status->numPlayers]; - player->score = atoi(COM_Parse(&s)); - player->ping = atoi(COM_Parse(&s)); + player->score = Q_atoi(COM_Parse(&s)); + player->ping = Q_atoi(COM_Parse(&s)); Q_strlcpy(player->name, COM_Parse(&s), sizeof(player->name)); if (!s) break; @@ -1255,7 +1254,7 @@ static void CL_ConnectionlessPacket(void) return; } - cls.challenge = atoi(Cmd_Argv(1)); + cls.challenge = Q_atoi(Cmd_Argv(1)); cls.state = ca_connecting; cls.connect_time -= CONNECT_INSTANT; // fire immediately //cls.connect_count = 0; @@ -1344,12 +1343,12 @@ static void CL_ConnectionlessPacket(void) if (!strncmp(s, "ac=", 3)) { s += 3; if (*s) { - anticheat = atoi(s); + anticheat = Q_atoi(s); } } else if (!strncmp(s, "nc=", 3)) { s += 3; if (*s) { - type = atoi(s); + type = Q_atoi(s); if (type != NETCHAN_OLD && type != NETCHAN_NEW) { Com_Error(ERR_DISCONNECT, "Server returned invalid netchan type"); @@ -1732,7 +1731,7 @@ static void CL_Precache_f(void) return; } - precache_spawncount = atoi(Cmd_Argv(1)); + precache_spawncount = Q_atoi(Cmd_Argv(1)); CL_ResetPrecacheCheck(); CL_RequestNextDownload(); diff --git a/src/client/parse.c b/src/client/parse.c index 661fa3d65..986455243 100644 --- a/src/client/parse.c +++ b/src/client/parse.c @@ -582,7 +582,7 @@ static void CL_ParseServerData(void) "R1Q2 server reports unsupported protocol version %d.\n" "Assuming it really uses our current client version %d.\n" "Things will break if it does not!\n", i, PROTOCOL_VERSION_R1Q2_CURRENT); - clamp(i, PROTOCOL_VERSION_R1Q2_MINIMUM, PROTOCOL_VERSION_R1Q2_CURRENT); + i = Q_clip(i, PROTOCOL_VERSION_R1Q2_MINIMUM, PROTOCOL_VERSION_R1Q2_CURRENT); } Com_DPrintf("Using minor R1Q2 protocol version %d\n", i); cls.protocolVersion = i; @@ -1176,9 +1176,7 @@ static void CL_ParseZPacket(void) #if USE_FPS static void set_server_fps(int value) { - int framediv = value / BASE_FRAMERATE; - - clamp(framediv, 1, MAX_FRAMEDIV); + int framediv = Q_clip(value / BASE_FRAMERATE, 1, MAX_FRAMEDIV); cl.frametime = BASE_FRAMETIME / framediv; cl.frametime_inv = framediv * BASE_1_FRAMETIME; diff --git a/src/client/precache.c b/src/client/precache.c index 5202c3470..239750e01 100644 --- a/src/client/precache.c +++ b/src/client/precache.c @@ -231,7 +231,7 @@ void CL_RegisterBspModels(void) Com_Error(ERR_DROP, "Couldn't load %s: %s", name, BSP_ErrorString(ret)); } - if (cl.bsp->checksum != atoi(cl.configstrings[cl.csr.mapchecksum])) { + if (cl.bsp->checksum != Q_atoi(cl.configstrings[cl.csr.mapchecksum])) { if (cls.demo.playback) { Com_WPrintf("Local map version differs from demo: %i != %s\n", cl.bsp->checksum, cl.configstrings[cl.csr.mapchecksum]); @@ -427,12 +427,12 @@ void CL_UpdateConfigstring(int index) const char *s = cl.configstrings[index]; if (index == cl.csr.maxclients) { - cl.maxclients = atoi(s); + cl.maxclients = Q_atoi(s); return; } if (index == cl.csr.airaccel) { - cl.pmp.airaccelerate = cl.pmp.qwmode || atoi(s); + cl.pmp.airaccelerate = cl.pmp.qwmode || Q_atoi(s); return; } diff --git a/src/client/screen.c b/src/client/screen.c index 4eb4ff728..ff9acb53a 100644 --- a/src/client/screen.c +++ b/src/client/screen.c @@ -490,8 +490,7 @@ static void SCR_LagDraw(int x, int y) } v &= ~(LAG_WARN_BIT | LAG_CRIT_BIT); - v = (v - v_min) * LAG_HEIGHT / v_range; - clamp(v, 0, LAG_HEIGHT); + v = Q_clip((v - v_min) * LAG_HEIGHT / v_range, 0, LAG_HEIGHT); R_DrawFill8(x + LAG_WIDTH - i - 1, y + LAG_HEIGHT - v, 1, v, c); } @@ -603,8 +602,8 @@ static void SCR_Draw_f(void) flags = UI_IGNORECOLOR; s = Cmd_Argv(1); - x = atoi(Cmd_Argv(2)); - y = atoi(Cmd_Argv(3)); + x = Q_atoi(Cmd_Argv(2)); + y = Q_atoi(Cmd_Argv(3)); if (x < 0) { flags |= UI_RIGHT; @@ -1044,14 +1043,14 @@ static void SCR_Sky_f(void) } if (argc > 2) - rotate = atof(Cmd_Argv(2)); + rotate = Q_atof(Cmd_Argv(2)); else rotate = 0; if (argc == 6) { - axis[0] = atof(Cmd_Argv(3)); - axis[1] = atof(Cmd_Argv(4)); - axis[2] = atof(Cmd_Argv(5)); + axis[0] = Q_atof(Cmd_Argv(3)); + axis[1] = Q_atof(Cmd_Argv(4)); + axis[2] = Q_atof(Cmd_Argv(5)); } else VectorSet(axis, 0, 0, 1); @@ -1547,19 +1546,19 @@ static void SCR_ExecuteLayoutString(const char *s) if (token[0] == 'x') { if (token[1] == 'l') { token = COM_Parse(&s); - x = atoi(token); + x = Q_atoi(token); continue; } if (token[1] == 'r') { token = COM_Parse(&s); - x = scr.hud_width + atoi(token); + x = scr.hud_width + Q_atoi(token); continue; } if (token[1] == 'v') { token = COM_Parse(&s); - x = scr.hud_width / 2 - 160 + atoi(token); + x = scr.hud_width / 2 - 160 + Q_atoi(token); continue; } } @@ -1567,19 +1566,19 @@ static void SCR_ExecuteLayoutString(const char *s) if (token[0] == 'y') { if (token[1] == 't') { token = COM_Parse(&s); - y = atoi(token); + y = Q_atoi(token); continue; } if (token[1] == 'b') { token = COM_Parse(&s); - y = scr.hud_height + atoi(token); + y = scr.hud_height + Q_atoi(token); continue; } if (token[1] == 'v') { token = COM_Parse(&s); - y = scr.hud_height / 2 - 120 + atoi(token); + y = scr.hud_height / 2 - 120 + Q_atoi(token); continue; } } @@ -1588,7 +1587,7 @@ static void SCR_ExecuteLayoutString(const char *s) if (!strcmp(token, "pic")) { // draw a pic from a stat number token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid stat index", __func__); } @@ -1626,25 +1625,25 @@ static void SCR_ExecuteLayoutString(const char *s) int score, ping, time; token = COM_Parse(&s); - x = scr.hud_width / 2 - 160 + atoi(token); + x = scr.hud_width / 2 - 160 + Q_atoi(token); token = COM_Parse(&s); - y = scr.hud_height / 2 - 120 + atoi(token); + y = scr.hud_height / 2 - 120 + Q_atoi(token); token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_CLIENTS) { Com_Error(ERR_DROP, "%s: invalid client index", __func__); } ci = &cl.clientinfo[value]; token = COM_Parse(&s); - score = atoi(token); + score = Q_atoi(token); token = COM_Parse(&s); - ping = atoi(token); + ping = Q_atoi(token); token = COM_Parse(&s); - time = atoi(token); + time = Q_atoi(token); HUD_DrawAltString(x + 32, y, ci->name); HUD_DrawString(x + 32, y + CHAR_HEIGHT, "Score: "); @@ -1667,22 +1666,22 @@ static void SCR_ExecuteLayoutString(const char *s) int score, ping; token = COM_Parse(&s); - x = scr.hud_width / 2 - 160 + atoi(token); + x = scr.hud_width / 2 - 160 + Q_atoi(token); token = COM_Parse(&s); - y = scr.hud_height / 2 - 120 + atoi(token); + y = scr.hud_height / 2 - 120 + Q_atoi(token); token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_CLIENTS) { Com_Error(ERR_DROP, "%s: invalid client index", __func__); } ci = &cl.clientinfo[value]; token = COM_Parse(&s); - score = atoi(token); + score = Q_atoi(token); token = COM_Parse(&s); - ping = atoi(token); + ping = Q_atoi(token); if (ping > 999) ping = 999; @@ -1706,9 +1705,9 @@ static void SCR_ExecuteLayoutString(const char *s) if (!strcmp(token, "num")) { // draw a number token = COM_Parse(&s); - width = atoi(token); + width = Q_atoi(token); token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid stat index", __func__); } @@ -1777,7 +1776,7 @@ static void SCR_ExecuteLayoutString(const char *s) if (!strcmp(token, "stat_string")) { token = COM_Parse(&s); - index = atoi(token); + index = Q_atoi(token); if (index < 0 || index >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid stat index", __func__); } @@ -1815,7 +1814,7 @@ static void SCR_ExecuteLayoutString(const char *s) if (!strcmp(token, "if")) { token = COM_Parse(&s); - value = atoi(token); + value = Q_atoi(token); if (value < 0 || value >= MAX_STATS) { Com_Error(ERR_DROP, "%s: invalid stat index", __func__); } diff --git a/src/client/sound/dma.c b/src/client/sound/dma.c index 41d657b86..b18ec1c0d 100644 --- a/src/client/sound/dma.c +++ b/src/client/sound/dma.c @@ -182,8 +182,8 @@ static void TransferStereo16(samplepair_t *samp, int endtime) // write a linear blast of samples int16_t *out = (int16_t *)dma.buffer + (lpos << 1); for (int i = 0; i < count; i++, samp++, out += 2) { - out[0] = clip16(samp->left); - out[1] = clip16(samp->right); + out[0] = Q_clip_int16(samp->left); + out[1] = Q_clip_int16(samp->right); } ltime += count; @@ -204,7 +204,7 @@ static void TransferStereo(samplepair_t *samp, int endtime) while (count--) { val = *p; p += step; - out[out_idx] = clip16(val); + out[out_idx] = Q_clip_int16(val); out_idx = (out_idx + 1) & out_mask; } } else if (dma.samplebits == 8) { @@ -212,7 +212,7 @@ static void TransferStereo(samplepair_t *samp, int endtime) while (count--) { val = *p; p += step; - out[out_idx] = (clip16(val) >> 8) + 128; + out[out_idx] = (Q_clip_int16(val) >> 8) + 128; out_idx = (out_idx + 1) & out_mask; } } diff --git a/src/client/sound/ogg.c b/src/client/sound/ogg.c index 6baab365d..1e0947df9 100644 --- a/src/client/sound/ogg.c +++ b/src/client/sound/ogg.c @@ -285,7 +285,7 @@ OGG_PlayTrack(const char* track_str) Q_snprintf(ogg.path, sizeof(ogg.path), "%s%s.ogg", ogg.music_dir, (const char*)tracklist[trackindex]); trackindex = (trackindex + 1) % trackcount; } else if (COM_IsUint(track_str)) { - int trackNo = atoi(track_str); + int trackNo = Q_atoi(track_str); get_track_path(ogg.path, sizeof(ogg.path), trackNo); } else { Q_snprintf(ogg.path, sizeof(ogg.path), "%s/%s/music/%s.ogg", sys_basedir->string, *fs_game->string ? fs_game->string : BASEGAME, track_str); diff --git a/src/client/sound/sound.h b/src/client/sound/sound.h index baceb46c8..efa7667a6 100644 --- a/src/client/sound/sound.h +++ b/src/client/sound/sound.h @@ -166,12 +166,6 @@ extern cvar_t *s_show; extern cvar_t *s_underwater; extern cvar_t *s_underwater_gain_hf; -// clip integer to [-0x8000, 0x7FFF] range (stolen from FFmpeg) -static inline int clip16(int v) -{ - return ((v + 0x8000U) & ~0xFFFF) ? (v >> 31) ^ 0x7FFF : v; -} - #define S_IsFullVolume(ch) \ ((ch)->entnum == -1 || (ch)->entnum == listener_entnum || (ch)->dist_mult == 0) diff --git a/src/client/ui/menu.c b/src/client/ui/menu.c index f45937504..e8af4e74a 100644 --- a/src/client/ui/menu.c +++ b/src/client/ui/menu.c @@ -1501,15 +1501,14 @@ static menuSound_t Slider_DoSlide(menuSlider_t *s, int dir); static void Slider_Push(menuSlider_t *s) { s->modified = false; - s->curvalue = s->cvar->value; - cclamp(s->curvalue, s->minvalue, s->maxvalue); + s->curvalue = Q_circ_clipf(s->cvar->value, s->minvalue, s->maxvalue); } static void Slider_Pop(menuSlider_t *s) { if (s->modified) { - cclamp(s->curvalue, s->minvalue, s->maxvalue); - Cvar_SetValue(s->cvar, s->curvalue, FROM_MENU); + float val = Q_circ_clipf(s->curvalue, s->minvalue, s->maxvalue); + Cvar_SetValue(s->cvar, val, FROM_MENU); } } @@ -1539,8 +1538,7 @@ static menuSound_t Slider_Click(menuSlider_t *s) float pos; int x; - pos = (s->curvalue - s->minvalue) / (s->maxvalue - s->minvalue); - clamp(pos, 0, 1); + pos = Q_clipf((s->curvalue - s->minvalue) / (s->maxvalue - s->minvalue), 0, 1); x = CHAR_WIDTH + (SLIDER_RANGE - 1) * CHAR_WIDTH * pos; @@ -1582,9 +1580,8 @@ static menuSound_t Slider_MouseMove(menuSlider_t *s) return QMS_NOTHANDLED; pos = (uis.mouseCoords[0] - (s->generic.x + RCOLUMN_OFFSET + CHAR_WIDTH)) * (1.0f / (SLIDER_RANGE * CHAR_WIDTH)); - clamp(pos, 0, 1); - value = pos * (s->maxvalue - s->minvalue); + value = Q_clipf(pos, 0, 1) * (s->maxvalue - s->minvalue); steps = Q_rint(value / s->step); s->modified = true; @@ -1619,9 +1616,7 @@ Slider_DoSlide static menuSound_t Slider_DoSlide(menuSlider_t *s, int dir) { s->modified = true; - s->curvalue += dir * s->step; - - cclamp(s->curvalue, s->minvalue, s->maxvalue); + s->curvalue = Q_circ_clipf(s->curvalue + dir * s->step, s->minvalue, s->maxvalue); if (s->generic.change) { menuSound_t sound = s->generic.change(&s->generic); @@ -1661,8 +1656,7 @@ static void Slider_Draw(menuSlider_t *s) UI_DrawChar(RCOLUMN_OFFSET + s->generic.x + i * CHAR_WIDTH + CHAR_WIDTH, s->generic.y, flags | UI_LEFT, 130); - pos = (s->curvalue - s->minvalue) / (s->maxvalue - s->minvalue); - clamp(pos, 0, 1); + pos = Q_clipf((s->curvalue - s->minvalue) / (s->maxvalue - s->minvalue), 0, 1); UI_DrawChar(CHAR_WIDTH + RCOLUMN_OFFSET + s->generic.x + (SLIDER_RANGE - 1) * CHAR_WIDTH * pos, s->generic.y, flags | UI_LEFT, 131); diff --git a/src/client/ui/playerconfig.c b/src/client/ui/playerconfig.c index 7f0100319..740820947 100644 --- a/src/client/ui/playerconfig.c +++ b/src/client/ui/playerconfig.c @@ -308,10 +308,10 @@ static bool Push(menuFrameWork_t *self) m_player.hand.curvalue = 0; m_player.aimfix.curvalue = Cvar_VariableInteger("aimfix"); - clamp(m_player.aimfix.curvalue, 0, 1); + m_player.aimfix.curvalue = Q_clip(m_player.aimfix.curvalue, 0, 1); m_player.view.curvalue = Cvar_VariableInteger("cl_player_model"); - clamp(m_player.view.curvalue, 0, 3); + m_player.view.curvalue = Q_clip(m_player.view.curvalue, 0, 3); m_player.menu.banner = R_RegisterPic("m_banner_plauer_setup"); if (m_player.menu.banner) { diff --git a/src/client/ui/script.c b/src/client/ui/script.c index e81f7fd2f..5afb20bc8 100644 --- a/src/client/ui/script.c +++ b/src/client/ui/script.c @@ -237,10 +237,10 @@ static void Parse_Range(menuFrameWork_t *menu) s->generic.name = UI_CopyString(Cmd_Argv(cmd_optind)); s->generic.status = UI_CopyString(status); s->cvar = Cvar_WeakGet(Cmd_Argv(cmd_optind + 1)); - s->minvalue = atof(Cmd_Argv(cmd_optind + 2)); - s->maxvalue = atof(Cmd_Argv(cmd_optind + 3)); + s->minvalue = Q_atof(Cmd_Argv(cmd_optind + 2)); + s->maxvalue = Q_atof(Cmd_Argv(cmd_optind + 3)); if (Cmd_Argc() - cmd_optind > 4) { - s->step = atof(Cmd_Argv(cmd_optind + 4)); + s->step = Q_atof(Cmd_Argv(cmd_optind + 4)); } else { s->step = (s->maxvalue - s->minvalue) / SLIDER_RANGE; } @@ -440,7 +440,7 @@ static void Parse_Toggle(menuFrameWork_t *menu) b++; } if (*b) { - bit = atoi(b); + bit = Q_atoi(b); if (bit < 0 || bit >= 32) { Com_Printf("Invalid bit number: %d\n", bit); return; @@ -491,7 +491,7 @@ static void Parse_Field(menuFrameWork_t *menu) status = cmd_optarg; break; case 'w': - width = atoi(cmd_optarg); + width = Q_atoi(cmd_optarg); if (width < 1 || width > 32) { Com_Printf("Invalid width\n"); return; @@ -642,7 +642,7 @@ static void Parse_Footer(menuFrameWork_t *menu) if (Cmd_Argc() >= 3) { - double scale = atof(Cmd_Argv(2)); + float scale = Q_atof(Cmd_Argv(2)); menu->footer_rc.width = (int)(menu->footer_rc.width * scale); menu->footer_rc.height = (int)(menu->footer_rc.height * scale); } @@ -663,7 +663,7 @@ static void Parse_If(menuFrameWork_t *menu, bool equals) } menu->current_condition.cvar = Cvar_WeakGet(Cmd_Argv(1)); - menu->current_condition.value = atoi(Cmd_Argv(2)); + menu->current_condition.value = Q_atoi(Cmd_Argv(2)); menu->current_condition.equals = equals; } diff --git a/src/client/ui/servers.c b/src/client/ui/servers.c index c94efa9f5..21ad5852f 100644 --- a/src/client/ui/servers.c +++ b/src/client/ui/servers.c @@ -188,10 +188,10 @@ static serverslot_t *FindSlot(const netadr_t *search, int *index_p) static uint32_t ColorForStatus(const serverStatus_t *status, unsigned ping) { - if (atoi(Info_ValueForKey(status->infostring, "needpass")) >= 1) + if (Q_atoi(Info_ValueForKey(status->infostring, "needpass")) >= 1) return uis.color.disabled.u32; - if (atoi(Info_ValueForKey(status->infostring, "anticheat")) >= 2) + if (Q_atoi(Info_ValueForKey(status->infostring, "anticheat")) >= 2) return uis.color.disabled.u32; if (Q_stricmp(Info_ValueForKey(status->infostring, "NoFake"), "ENABLED") == 0) @@ -668,14 +668,13 @@ static void FinishPingStage(void) static void CalcPingRate(void) { extern cvar_t *info_rate; + + // don't allow more than 100 packets/sec int rate = Cvar_ClampInteger(ui_pingrate, 0, 100); // assume average 450 bytes per reply packet if (!rate) - rate = info_rate->integer / 450; - - // don't allow more than 100 packets/sec - clamp(rate, 1, 100); + rate = Q_clip(info_rate->integer / 450, 1, 100); // drop rate by stage m_servers.pingtime = (1000 * PING_STAGES) / (rate * m_servers.pingstage); @@ -777,8 +776,8 @@ static int namecmp(serverslot_t *s1, serverslot_t *s2, int col) static int pingcmp(serverslot_t *s1, serverslot_t *s2) { - int n1 = atoi(UI_GetColumn(s1->name, COL_RTT)); - int n2 = atoi(UI_GetColumn(s2->name, COL_RTT)); + int n1 = Q_atoi(UI_GetColumn(s1->name, COL_RTT)); + int n2 = Q_atoi(UI_GetColumn(s2->name, COL_RTT)); return (n1 - n2) * m_servers.list.sortdir; } diff --git a/src/client/ui/ui.c b/src/client/ui/ui.c index 8c23363fd..3f92f8369 100644 --- a/src/client/ui/ui.c +++ b/src/client/ui/ui.c @@ -401,8 +401,8 @@ UI_MouseEvent */ void UI_MouseEvent(int x, int y) { - clamp(x, 0, r_config.width - 1); - clamp(y, 0, r_config.height - 1); + x = Q_clip(x, 0, r_config.width - 1); + y = Q_clip(y, 0, r_config.height - 1); uis.mouseCoords[0] = Q_rint(x * uis.scale); uis.mouseCoords[1] = Q_rint(y * uis.scale); diff --git a/src/common/bsp.c b/src/common/bsp.c index 9ab5638e0..4bcbf0d5e 100644 --- a/src/common/bsp.c +++ b/src/common/bsp.c @@ -1764,7 +1764,7 @@ mmodel_t *BSP_InlineModel(bsp_t *bsp, const char *name) Q_assert(name); Q_assert(name[0] == '*'); - num = atoi(name + 1); + num = Q_atoi(name + 1); if (num < 1 || num >= bsp->nummodels) { Com_Error(ERR_DROP, "%s: bad number: %d", __func__, num); } diff --git a/src/common/cmd.c b/src/common/cmd.c index 07bfc2f3b..d37195bef 100644 --- a/src/common/cmd.c +++ b/src/common/cmd.c @@ -60,7 +60,7 @@ bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2" */ static void Cmd_Wait_f(void) { - int count = atoi(Cmd_Argv(1)); + int count = Q_atoi(Cmd_Argv(1)); cmd_current->waitCount += max(count, 1); } @@ -613,28 +613,28 @@ static void Cmd_If_f(void) numeric = COM_IsFloat(a) && COM_IsFloat(b); if (!strcmp(op, "==")) { - matched = numeric ? atof(a) == atof(b) : !strcmp(a, b); + matched = numeric ? Q_atof(a) == Q_atof(b) : !strcmp(a, b); } else if (!strcmp(op, "!=") || !strcmp(op, "<>")) { - matched = numeric ? atof(a) != atof(b) : strcmp(a, b); + matched = numeric ? Q_atof(a) != Q_atof(b) : strcmp(a, b); } else if (!strcmp(op, "<")) { if (!numeric) { error: Com_Printf("Can't use '%s' with non-numeric expression(s)\n", op); return; } - matched = atof(a) < atof(b); + matched = Q_atof(a) < Q_atof(b); } else if (!strcmp(op, "<=")) { if (!numeric) goto error; - matched = atof(a) <= atof(b); + matched = Q_atof(a) <= Q_atof(b); } else if (!strcmp(op, ">")) { if (!numeric) goto error; - matched = atof(a) > atof(b); + matched = Q_atof(a) > Q_atof(b); } else if (!strcmp(op, ">=")) { if (!numeric) goto error; - matched = atof(a) >= atof(b); + matched = Q_atof(a) >= Q_atof(b); } else if (!Q_stricmp(op, "isin")) { matched = strstr(b, a) != NULL; } else if (!Q_stricmp(op, "!isin")) { diff --git a/src/common/cmodel.c b/src/common/cmodel.c index d7adcdc85..1bd7ffb15 100644 --- a/src/common/cmodel.c +++ b/src/common/cmodel.c @@ -732,14 +732,17 @@ static void CM_RecursiveHullCheck(mnode_t *node, float p1f, float p2f, const vec frac2 = 0; } + frac = Q_clipf(frac, 0, 1); + frac2 = Q_clipf(frac2, 0, 1); + // move up to the node - midf = p1f + (p2f - p1f) * clamp(frac, 0, 1); + midf = p1f + (p2f - p1f) * frac; LerpVector(p1, p2, frac, mid); CM_RecursiveHullCheck(node->children[side], p1f, midf, p1, mid); // go past the node - midf = p1f + (p2f - p1f) * clamp(frac2, 0, 1); + midf = p1f + (p2f - p1f) * frac2; LerpVector(p1, p2, frac2, mid); CM_RecursiveHullCheck(node->children[side ^ 1], midf, p2f, mid, p2); diff --git a/src/common/cvar.c b/src/common/cvar.c index 315df8dc0..5bd35ad95 100644 --- a/src/common/cvar.c +++ b/src/common/cvar.c @@ -158,11 +158,11 @@ static void parse_string_value(cvar_t *var) if (s[0] == '0' && s[1] == 'x') { long v = strtol(s, NULL, 16); - var->integer = clamp(v, INT_MIN, INT_MAX); + var->integer = Q_clipl_int32(v); var->value = (float)var->integer; } else { - var->integer = atoi(s); - var->value = atof(s); + var->integer = Q_atoi(s); + var->value = Q_atof(s); if (var->value != 0.0f && !isnormal(var->value)) var->value = 0.0f; } @@ -1022,7 +1022,7 @@ static void Cvar_Inc_f(void) value = 1; if (Cmd_Argc() > 2) { - value = atof(Cmd_Argv(2)); + value = Q_atof(Cmd_Argv(2)); } if (!strcmp(Cmd_Argv(0), "dec")) { value = -value; diff --git a/src/common/msg.c b/src/common/msg.c index 71d7c2ecd..b2f9547f6 100644 --- a/src/common/msg.c +++ b/src/common/msg.c @@ -450,11 +450,6 @@ void MSG_WriteDir(const vec3_t dir) MSG_WriteByte(best); } -static inline int clip_byte(int v) -{ - return clamp(v, 0, 255); -} - void MSG_PackEntity(entity_packed_t *out, const entity_state_t *in, const entity_state_extension_t *ext) { // allow 0 to accomodate empty baselines @@ -482,10 +477,10 @@ void MSG_PackEntity(entity_packed_t *out, const entity_state_t *in, const entity out->event = in->event; if (ext) { out->morefx = ext->morefx; - out->alpha = clip_byte(ext->alpha * 255.0f); - out->scale = clip_byte(ext->scale * 16.0f); - out->loop_volume = clip_byte(ext->loop_volume * 255.0f); - out->loop_attenuation = clip_byte(ext->loop_attenuation * 64.0f); + out->alpha = Q_clip_uint8(ext->alpha * 255.0f); + out->scale = Q_clip_uint8(ext->scale * 16.0f); + out->loop_volume = Q_clip_uint8(ext->loop_volume * 255.0f); + out->loop_attenuation = Q_clip_uint8(ext->loop_attenuation * 64.0f); // save network bandwidth if (out->alpha == 255) out->alpha = 0; if (out->scale == 16) out->scale = 0; @@ -780,17 +775,7 @@ void MSG_WriteDeltaEntity(const entity_packed_t *from, MSG_WriteByte(to->scale); } -static inline int OFFSET2CHAR(float x) -{ - int v = x * 4; - return clamp(v, -128, 127); -} - -static inline int BLEND2BYTE(float x) -{ - int v = x * 255; - return clamp(v, 0, 255); -} +#define OFFSET2CHAR(x) Q_clip_int8((x) * 4) void MSG_PackPlayer(player_packed_t *out, const player_state_t *in) { @@ -805,7 +790,7 @@ void MSG_PackPlayer(player_packed_t *out, const player_state_t *in) out->gunindex = in->gunindex; out->gunframe = in->gunframe; for (i = 0; i < 4; i++) - out->blend[i] = BLEND2BYTE(in->blend[i]); + out->blend[i] = Q_clip_uint8(in->blend[i] * 255); out->fov = (int)in->fov; out->rdflags = in->rdflags; for (i = 0; i < MAX_STATS; i++) diff --git a/src/common/pmove.c b/src/common/pmove.c index 6bf42bd2b..cb77416fd 100644 --- a/src/common/pmove.c +++ b/src/common/pmove.c @@ -373,8 +373,8 @@ static void PM_AddCurrents(vec3_t wishvel) wishvel[2] = 0; // limit horizontal speed when on a ladder - clamp(wishvel[0], -25, 25); - clamp(wishvel[1], -25, 25); + wishvel[0] = Q_clipf(wishvel[0], -25, 25); + wishvel[1] = Q_clipf(wishvel[1], -25, 25); } // @@ -1012,7 +1012,7 @@ static void PM_ClampAngles(void) } // don't let the player look up or down more than 90 degrees - clamp(pm->viewangles[PITCH], -89, 89); + pm->viewangles[PITCH] = Q_clipf(pm->viewangles[PITCH], -89, 89); } AngleVectors(pm->viewangles, pml.forward, pml.right, pml.up); } diff --git a/src/common/tests.c b/src/common/tests.c index 4304d536c..01e272ca2 100644 --- a/src/common/tests.c +++ b/src/common/tests.c @@ -48,7 +48,7 @@ static void Com_Freeze_f(void) return; } - seconds = atof(Cmd_Argv(1)); + seconds = Q_atof(Cmd_Argv(1)); if (seconds < 0) { return; } @@ -64,7 +64,7 @@ static void Com_Crash_f(void) { static byte buf1[16]; byte buf2[16], *buf3; - int i = atoi(Cmd_Argv(1)); + int i = Q_atoi(Cmd_Argv(1)); switch (i) { case 1: @@ -109,7 +109,7 @@ static void Com_PrintJunk_f(void) buf[Q_rand() % (sizeof(buf) - 1)] = ' '; if (Cmd_Argc() > 1) - count = atoi(Cmd_Argv(1)); + count = Q_atoi(Cmd_Argv(1)); else count = 1; diff --git a/src/common/utils.c b/src/common/utils.c index 5d32e0fa4..9a2075d24 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -207,7 +207,7 @@ color_index_t Com_ParseColor(const char *s) color_index_t i; if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); if (i < 0 || i >= COLOR_COUNT) { return COLOR_NONE; } diff --git a/src/game/g_cmds.c b/src/game/g_cmds.c index f731d52bf..4c0e1a37e 100644 --- a/src/game/g_cmds.c +++ b/src/game/g_cmds.c @@ -164,7 +164,7 @@ void Cmd_Give_f(edict_t *ent) if (give_all || Q_stricmp(gi.argv(1), "health") == 0) { if (gi.argc() == 3) - ent->health = atoi(gi.argv(2)); + ent->health = Q_atoi(gi.argv(2)); else ent->health = ent->max_health; if (!give_all) @@ -258,7 +258,7 @@ void Cmd_Give_f(edict_t *ent) if (it->flags & IT_AMMO) { if (gi.argc() == 3) - ent->client->pers.inventory[index] = atoi(gi.argv(2)); + ent->client->pers.inventory[index] = Q_atoi(gi.argv(2)); else ent->client->pers.inventory[index] += it->quantity; } else { @@ -691,7 +691,7 @@ void Cmd_Wave_f(edict_t *ent) { int i; - i = atoi(gi.argv(1)); + i = Q_atoi(gi.argv(1)); // can't wave when ducked if (ent->client->ps.pmove.pm_flags & PMF_DUCKED) diff --git a/src/game/g_main.c b/src/game/g_main.c index fc1331ec6..0504bc235 100644 --- a/src/game/g_main.c +++ b/src/game/g_main.c @@ -201,8 +201,7 @@ void InitGame(void) game.helpmessage2[0] = 0; // initialize all entities for this game - game.maxentities = maxentities->value; - clamp(game.maxentities, (int)maxclients->value + 1, game.csr.max_edicts); + game.maxentities = Q_clip(maxentities->value, (int)maxclients->value + 1, game.csr.max_edicts); g_edicts = gi.TagMalloc(game.maxentities * sizeof(g_edicts[0]), TAG_GAME); globals.edicts = g_edicts; globals.max_edicts = game.maxentities; diff --git a/src/game/g_misc.c b/src/game/g_misc.c index de0f1ae1c..5a658d9dd 100644 --- a/src/game/g_misc.c +++ b/src/game/g_misc.c @@ -65,9 +65,9 @@ void VelocityForDamage(int damage, vec3_t v) void ClipGibVelocity(edict_t *ent) { - clamp(ent->velocity[0], -300, 300); - clamp(ent->velocity[1], -300, 300); - clamp(ent->velocity[2], 200, 500); // always some upwards + ent->velocity[0] = Q_clipf(ent->velocity[0], -300, 300); + ent->velocity[1] = Q_clipf(ent->velocity[1], -300, 300); + ent->velocity[2] = Q_clipf(ent->velocity[2], 200, 500); // always some upwards } /* diff --git a/src/game/g_phys.c b/src/game/g_phys.c index 10029b7e3..4ff49d63a 100644 --- a/src/game/g_phys.c +++ b/src/game/g_phys.c @@ -66,13 +66,14 @@ SV_CheckVelocity */ void SV_CheckVelocity(edict_t *ent) { - int i; + float speed = sv_maxvelocity->value; // // bound velocity // - for (i = 0; i < 3; i++) - clamp(ent->velocity[i], -sv_maxvelocity->value, sv_maxvelocity->value); + ent->velocity[0] = Q_clipf(ent->velocity[0], -speed, speed); + ent->velocity[1] = Q_clipf(ent->velocity[1], -speed, speed); + ent->velocity[2] = Q_clipf(ent->velocity[2], -speed, speed); } /* diff --git a/src/game/g_spawn.c b/src/game/g_spawn.c index 35df5fd7f..045a3735b 100644 --- a/src/game/g_spawn.c +++ b/src/game/g_spawn.c @@ -434,13 +434,13 @@ static bool ED_ParseField(const spawn_field_t *fields, const char *key, const ch ((float *)(b + f->ofs))[2] = vec[2]; break; case F_INT: - *(int *)(b + f->ofs) = atoi(value); + *(int *)(b + f->ofs) = Q_atoi(value); break; case F_FLOAT: - *(float *)(b + f->ofs) = atof(value); + *(float *)(b + f->ofs) = Q_atof(value); break; case F_ANGLEHACK: - v = atof(value); + v = Q_atof(value); ((float *)(b + f->ofs))[0] = 0; ((float *)(b + f->ofs))[1] = v; ((float *)(b + f->ofs))[2] = 0; @@ -571,12 +571,11 @@ void SpawnEntities(const char *mapname, const char *entities, const char *spawnp int inhibit; char *com_token; int i; - float skill_level; + int skill_level; - skill_level = floor(skill->value); - clamp(skill_level, 0, 3); + skill_level = Q_clip(skill->value, 0, 3); if (skill->value != skill_level) - gi.cvar_forceset("skill", va("%f", skill_level)); + gi.cvar_forceset("skill", va("%d", skill_level)); SaveClientData(); diff --git a/src/game/g_svcmds.c b/src/game/g_svcmds.c index 65bbe1171..dcc671c55 100644 --- a/src/game/g_svcmds.c +++ b/src/game/g_svcmds.c @@ -89,7 +89,7 @@ static bool StringToFilter(char *s, ipfilter_t *f) num[j++] = *s++; } num[j] = 0; - b.bytes[i] = atoi(num); + b.bytes[i] = Q_atoi(num); if (b.bytes[i] != 0) m.bytes[i] = 255; diff --git a/src/game/g_trigger.c b/src/game/g_trigger.c index d6cddafd0..0c53df2be 100644 --- a/src/game/g_trigger.c +++ b/src/game/g_trigger.c @@ -495,7 +495,7 @@ void SP_trigger_gravity(edict_t *self) } InitTrigger(self); - self->gravity = atoi(st.gravity); + self->gravity = Q_atoi(st.gravity); self->touch = trigger_gravity_touch; } diff --git a/src/game/p_client.c b/src/game/p_client.c index 5ed6bf02e..c7149a322 100644 --- a/src/game/p_client.c +++ b/src/game/p_client.c @@ -1146,7 +1146,7 @@ void PutClientInServer(edict_t *ent) if (deathmatch->value && ((int)dmflags->value & DF_FIXED_FOV)) { client->ps.fov = 90; } else { - client->ps.fov = atoi(Info_ValueForKey(client->pers.userinfo, "fov")); + client->ps.fov = Q_atoi(Info_ValueForKey(client->pers.userinfo, "fov")); if (client->ps.fov < 1) client->ps.fov = 90; else if (client->ps.fov > 160) @@ -1357,7 +1357,7 @@ void ClientUserinfoChanged(edict_t *ent, char *userinfo) if (deathmatch->value && ((int)dmflags->value & DF_FIXED_FOV)) { ent->client->ps.fov = 90; } else { - ent->client->ps.fov = atoi(Info_ValueForKey(userinfo, "fov")); + ent->client->ps.fov = Q_atoi(Info_ValueForKey(userinfo, "fov")); if (ent->client->ps.fov < 1) ent->client->ps.fov = 90; else if (ent->client->ps.fov > 160) @@ -1367,7 +1367,7 @@ void ClientUserinfoChanged(edict_t *ent, char *userinfo) // handedness s = Info_ValueForKey(userinfo, "hand"); if (strlen(s)) { - ent->client->pers.hand = atoi(s); + ent->client->pers.hand = Q_atoi(s); } // save off the userinfo in case we want to check something later diff --git a/src/game/p_view.c b/src/game/p_view.c index 0666258c5..54a0e318c 100644 --- a/src/game/p_view.c +++ b/src/game/p_view.c @@ -138,10 +138,7 @@ void P_DamageFeedback(edict_t *player) if (client->damage_alpha < 0) client->damage_alpha = 0; client->damage_alpha += count * 0.01f; - if (client->damage_alpha < 0.2f) - client->damage_alpha = 0.2f; - if (client->damage_alpha > 0.6f) - client->damage_alpha = 0.6f; // don't go too saturated + client->damage_alpha = Q_clipf(client->damage_alpha, 0.2f, 0.6f); // don't go too saturated // the color of the blend will vary based on how much was absorbed // by different armors @@ -300,11 +297,9 @@ void SV_CalcViewOffset(edict_t *ent) // absolutely bound offsets // so the view can never be outside the player box - clamp(v[0], -14, 14); - clamp(v[1], -14, 14); - clamp(v[2], -22, 30); - - VectorCopy(v, ent->client->ps.viewoffset); + ent->client->ps.viewoffset[0] = Q_clipf(v[0], -14, 14); + ent->client->ps.viewoffset[1] = Q_clipf(v[1], -14, 14); + ent->client->ps.viewoffset[2] = Q_clipf(v[2], -22, 30); } /* @@ -334,7 +329,7 @@ void SV_CalcGunOffset(edict_t *ent) delta -= 360; if (delta < -180) delta += 360; - clamp(delta, -45, 45); + delta = Q_clipf(delta, -45, 45); if (i == YAW) ent->client->ps.gunangles[ROLL] += 0.1f * delta; ent->client->ps.gunangles[i] += 0.2f * delta; diff --git a/src/refresh/gl/mesh.c b/src/refresh/gl/mesh.c index 3e2ab4719..e3429cd4a 100644 --- a/src/refresh/gl/mesh.c +++ b/src/refresh/gl/mesh.c @@ -390,9 +390,9 @@ static void setup_color(void) } } - for (i = 0; i < 3; i++) { - clamp(color[i], 0, 1); - } + color[0] = Q_clipf(color[0], 0, 1); + color[1] = Q_clipf(color[1], 0, 1); + color[2] = Q_clipf(color[2], 0, 1); } if (flags & RF_TRANSLUCENT) { diff --git a/src/refresh/gl/texture.c b/src/refresh/gl/texture.c index 282171825..577d92e23 100644 --- a/src/refresh/gl/texture.c +++ b/src/refresh/gl/texture.c @@ -844,7 +844,7 @@ static void GL_InitParticleTexture(void) dst[0] = 255; dst[1] = 255; dst[2] = 255; - dst[3] = 255 * clamp(f, 0, 1 - shape * 0.2f); + dst[3] = 255 * Q_clipf(f, 0, 1 - shape * 0.2f); dst += 4; } } @@ -897,7 +897,7 @@ static void GL_InitBeamTexture(void) dst[0] = 255; dst[1] = 255; dst[2] = 255; - dst[3] = 255 * clamp(f, 0, 1); + dst[3] = 255 * Q_clipf(f, 0, 1); dst += 4; } } diff --git a/src/refresh/gl/world.c b/src/refresh/gl/world.c index fb1ca7459..523fd95f3 100644 --- a/src/refresh/gl/world.c +++ b/src/refresh/gl/world.c @@ -251,13 +251,11 @@ void GL_LightPoint(const vec3_t origin, vec3_t color) void R_LightPoint_GL(const vec3_t origin, vec3_t color) { - int i; - GL_LightPoint(origin, color); - for (i = 0; i < 3; i++) { - clamp(color[i], 0, 1); - } + color[0] = Q_clipf(color[0], 0, 1); + color[1] = Q_clipf(color[1], 0, 1); + color[2] = Q_clipf(color[2], 0, 1); } static void GL_MarkLeaves(void) diff --git a/src/refresh/images.c b/src/refresh/images.c index c066102a9..cea0b5a9c 100644 --- a/src/refresh/images.c +++ b/src/refresh/images.c @@ -749,7 +749,7 @@ static void IMG_ScreenShotJPG_f(void) } if (Cmd_Argc() > 2) { - quality = atoi(Cmd_Argv(2)); + quality = Q_atoi(Cmd_Argv(2)); } else { quality = r_screenshot_quality->integer; } @@ -768,7 +768,7 @@ static void IMG_ScreenShotPNG_f(void) } if (Cmd_Argc() > 2) { - compression = atoi(Cmd_Argv(2)); + compression = Q_atoi(Cmd_Argv(2)); } else { compression = r_screenshot_compression->integer; } diff --git a/src/refresh/model_iqm.c b/src/refresh/model_iqm.c index 624d31f9c..88e20b7ae 100644 --- a/src/refresh/model_iqm.c +++ b/src/refresh/model_iqm.c @@ -618,8 +618,7 @@ int MOD_LoadIQM_Base(model_t* model, const void* rawdata, size_t length, const c // Convert blend weights from float to byte for (uint32_t weight_idx = 0; weight_idx < n; weight_idx++) { - float integer_weight = weights[weight_idx] * 255.f; - clamp(integer_weight, 0.f, 255.f); + float integer_weight = Q_clipf(weights[weight_idx] * 255.f, 0.f, 255.f); iqmData->blend_weights[weight_idx] = (byte)integer_weight; } } diff --git a/src/refresh/models.c b/src/refresh/models.c index ab3c9b1f2..f127d4b31 100644 --- a/src/refresh/models.c +++ b/src/refresh/models.c @@ -319,7 +319,7 @@ qhandle_t R_RegisterModel(const char *name) if (*name == '*') { // inline bsp model - index = atoi(name + 1); + index = Q_atoi(name + 1); return ~index; } diff --git a/src/refresh/vkpt/bsp_mesh.c b/src/refresh/vkpt/bsp_mesh.c index 7117a6c43..38033eca1 100644 --- a/src/refresh/vkpt/bsp_mesh.c +++ b/src/refresh/vkpt/bsp_mesh.c @@ -117,8 +117,8 @@ encode_normal(const vec3_t normal) pp[0] = pp[0] * 0.5f + 0.5f; pp[1] = pp[1] * 0.5f + 0.5f; - clamp(pp[0], 0.f, 1.f); - clamp(pp[1], 0.f, 1.f); + pp[0] = Q_clipf(pp[0], 0.f, 1.f); + pp[1] = Q_clipf(pp[1], 0.f, 1.f); uint32_t ux = (uint32_t)(pp[0] * 0xffffu); uint32_t uy = (uint32_t)(pp[1] * 0xffffu); @@ -1529,7 +1529,7 @@ load_sky_and_lava_clusters(bsp_mesh_t* wm, const char* map_name) wm->all_lava_emissive = true; else { - int cluster = atoi(word); + int cluster = Q_atoi(word); wm->sky_clusters[wm->num_sky_clusters++] = cluster; } diff --git a/src/refresh/vkpt/main.c b/src/refresh/vkpt/main.c index 5cc1eaf64..8d57ec5c0 100644 --- a/src/refresh/vkpt/main.c +++ b/src/refresh/vkpt/main.c @@ -2857,7 +2857,7 @@ prepare_ubo(refdef_t *fd, mleaf_t* viewleaf, const reference_mode_t* ref_mode, c // adjust texture LOD bias to the resolution scale, i.e. use negative bias if scale is < 100 float resolution_scale = (drs_effective_scale != 0) ? (float)drs_effective_scale : (float)scr_viewsize->integer; resolution_scale *= 0.01f; - clamp(resolution_scale, 0.1f, 1.f); + resolution_scale = Q_clipf(resolution_scale, 0.1f, 1.f); ubo->pt_texture_lod_bias = cvar_pt_texture_lod_bias->value + log2f(resolution_scale); } @@ -3461,13 +3461,13 @@ static void drs_process(void) if (representative_time < target_time * cvar_drs_adjust_up->value) { f += 0.5; - clamp(f, 1, 10); + f = Q_clipf(f, 1, 10); scale += (int)f; } else if (representative_time > target_time * cvar_drs_adjust_down->value) { f -= 0.5; - clamp(f, -1, -10); + f = Q_clipf(f, -1, -10); scale += f; } diff --git a/src/refresh/vkpt/material.c b/src/refresh/vkpt/material.c index 17b84de68..811280fec 100644 --- a/src/refresh/vkpt/material.c +++ b/src/refresh/vkpt/material.c @@ -388,8 +388,8 @@ static int set_material_attribute(pbr_material_t* mat, const char* attribute, co int ivalue = 0; switch (t->type) { - case ATTR_BOOL: bvalue = atoi(value) == 0 ? false : true; break; - case ATTR_FLOAT: fvalue = (float)atof(value); break; + case ATTR_BOOL: bvalue = Q_atoi(value) == 0 ? false : true; break; + case ATTR_FLOAT: fvalue = (float)Q_atof(value); break; case ATTR_STRING: { char* asterisk = strchr(value, '*'); if (asterisk) { @@ -414,7 +414,7 @@ static int set_material_attribute(pbr_material_t* mat, const char* attribute, co Q_strlcpy(svalue, value, sizeof(svalue)); break; case ATTR_INT: - ivalue = atoi(value); + ivalue = Q_atoi(value); break; } default: diff --git a/src/refresh/vkpt/textures.c b/src/refresh/vkpt/textures.c index 62352730f..4a84cb87a 100644 --- a/src/refresh/vkpt/textures.c +++ b/src/refresh/vkpt/textures.c @@ -730,8 +730,7 @@ static void apply_fake_emissive_threshold(image_t *image, int bright_threshold_i /* Extract "bright" pixels by choosing all those that have one component larger than some threshold. */ - clamp(bright_threshold_int, 0, 255); - byte bright_threshold = (byte)bright_threshold_int; + byte bright_threshold = Q_clip_uint8(bright_threshold_int); float *current_bright_mask = bright_mask; byte *src_pixel = image->pix_data; @@ -884,7 +883,7 @@ image_t *vkpt_fake_emissive_texture(image_t *image, int bright_threshold_int) if(new_image == R_NOTEXTURE) return image; - new_image->flags |= IF_FAKE_EMISSIVE | (clamp(bright_threshold_int, 0, 255) << IF_FAKE_EMISSIVE_THRESH_SHIFT); + new_image->flags |= IF_FAKE_EMISSIVE | (Q_clip_uint8(bright_threshold_int) << IF_FAKE_EMISSIVE_THRESH_SHIFT); apply_fake_emissive_threshold(new_image, bright_threshold_int); return new_image; diff --git a/src/server/ac.c b/src/server/ac.c index 449308c61..1160957f5 100644 --- a/src/server/ac.c +++ b/src/server/ac.c @@ -1576,7 +1576,7 @@ void AC_Info_f(void) filesubstring = Cmd_Argv(2); if (COM_IsUint(substring)) { - clientID = atoi(substring); + clientID = Q_atoi(substring); if (clientID < 0 || clientID >= sv_maxclients->integer) { Com_Printf("Invalid client ID.\n"); return; diff --git a/src/server/commands.c b/src/server/commands.c index 1b4ff74eb..2ab495c1d 100644 --- a/src/server/commands.c +++ b/src/server/commands.c @@ -125,7 +125,7 @@ client_t *SV_GetPlayer(const char *s, bool partial) // numeric values are just slot numbers if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); if (i < 0 || i >= sv_maxclients->integer) { Com_Printf("Bad client slot number: %d\n", i); return NULL; @@ -1058,7 +1058,7 @@ static bool parse_mask(char *s, netadr_t *addr, netadr_t *mask) Com_Printf("Please specify a mask after '/'.\n"); return false; } - bits = atoi(p); + bits = Q_atoi(p); } else { bits = -1; } @@ -1156,7 +1156,7 @@ void SV_DelMatch_f(list_t *list) // numeric values are just slot numbers if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); match = LIST_INDEX(addrmatch_t, i - 1, list, entry); if (match) { goto remove; @@ -1283,7 +1283,7 @@ static void SV_DelStuffCmd(list_t *list, int arg, const char *what) } if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); stuff = LIST_INDEX(stuffcmd_t, i - 1, list, entry); if (!stuff) { Com_Printf("No such %scmd index: %d\n", what, i); @@ -1481,7 +1481,7 @@ static void SV_DelFilterCmd_f(void) } if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); filter = LIST_INDEX(filtercmd_t, i - 1, &sv_filterlist, entry); if (!filter) { Com_Printf("No such filtercmd index: %d\n", i); @@ -1603,7 +1603,7 @@ static void SV_DelCvarBan(list_t *list, const char *what) } if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); ban = LIST_INDEX(cvarban_t, i - 1, list, entry); if (!ban) { Com_Printf("No such %sban index: %d\n", what, i); diff --git a/src/server/init.c b/src/server/init.c index 2c6cc866d..5c62232a5 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -47,8 +47,7 @@ static void set_frame_time(void) framediv = sv_fps->integer / BASE_FRAMERATE; else framediv = 1; - - clamp(framediv, 1, MAX_FRAMEDIV); + framediv = Q_clip(framediv, 1, MAX_FRAMEDIV); sv.framerate = framediv * BASE_FRAMERATE; sv.frametime = BASE_FRAMETIME / framediv; diff --git a/src/server/main.c b/src/server/main.c index 4d9ec17cd..e7ee7529e 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -535,7 +535,7 @@ static void SVC_Info(void) if (sv_maxclients->integer == 1) return; // ignore in single player - version = atoi(Cmd_Argv(1)); + version = Q_atoi(Cmd_Argv(1)); if (version < PROTOCOL_VERSION_DEFAULT || version > PROTOCOL_VERSION_Q2PRO) return; // ignore invalid versions @@ -640,9 +640,9 @@ typedef struct { static bool parse_basic_params(conn_params_t *p) { - p->protocol = atoi(Cmd_Argv(1)); - p->qport = atoi(Cmd_Argv(2)) ; - p->challenge = atoi(Cmd_Argv(3)); + p->protocol = Q_atoi(Cmd_Argv(1)); + p->qport = Q_atoi(Cmd_Argv(2)) ; + p->challenge = Q_atoi(Cmd_Argv(3)); // check for invalid protocol version if (p->protocol < PROTOCOL_VERSION_OLD || @@ -740,7 +740,7 @@ static bool parse_packet_length(conn_params_t *p) if (p->protocol >= PROTOCOL_VERSION_R1Q2) { s = Cmd_Argv(5); if (*s) { - p->maxlength = atoi(s); + p->maxlength = Q_atoi(s); if (p->maxlength < 0 || p->maxlength > MAX_PACKETLEN_WRITABLE) return reject("Invalid maximum message length.\n"); @@ -771,10 +771,9 @@ static bool parse_enhanced_params(conn_params_t *p) // set minor protocol version s = Cmd_Argv(6); if (*s) { - p->version = atoi(s); - clamp(p->version, - PROTOCOL_VERSION_R1Q2_MINIMUM, - PROTOCOL_VERSION_R1Q2_CURRENT); + p->version = Q_clip(Q_atoi(s), + PROTOCOL_VERSION_R1Q2_MINIMUM, + PROTOCOL_VERSION_R1Q2_CURRENT); } else { p->version = PROTOCOL_VERSION_R1Q2_MINIMUM; } @@ -784,7 +783,7 @@ static bool parse_enhanced_params(conn_params_t *p) // set netchan type s = Cmd_Argv(6); if (*s) { - p->nctype = atoi(s); + p->nctype = Q_atoi(s); if (p->nctype < NETCHAN_OLD || p->nctype > NETCHAN_NEW) return reject("Invalid netchan type.\n"); } else { @@ -793,15 +792,14 @@ static bool parse_enhanced_params(conn_params_t *p) // set zlib s = Cmd_Argv(7); - p->has_zlib = !*s || atoi(s); + p->has_zlib = !*s || Q_atoi(s); // set minor protocol version s = Cmd_Argv(8); if (*s) { - p->version = atoi(s); - clamp(p->version, - PROTOCOL_VERSION_Q2PRO_MINIMUM, - PROTOCOL_VERSION_Q2PRO_CURRENT); + p->version = Q_clip(Q_atoi(s), + PROTOCOL_VERSION_Q2PRO_MINIMUM, + PROTOCOL_VERSION_Q2PRO_CURRENT); if (p->version == PROTOCOL_VERSION_Q2PRO_RESERVED) { p->version--; // never use this version } @@ -2035,8 +2033,7 @@ void SV_UserinfoChanged(client_t *cl) // rate command val = Info_ValueForKey(cl->userinfo, "rate"); if (*val) { - cl->rate = atoi(val); - clamp(cl->rate, sv_min_rate->integer, sv_max_rate->integer); + cl->rate = Q_clip(Q_atoi(val), sv_min_rate->integer, sv_max_rate->integer); } else { cl->rate = 5000; } @@ -2055,8 +2052,7 @@ void SV_UserinfoChanged(client_t *cl) // msg command val = Info_ValueForKey(cl->userinfo, "msg"); if (*val) { - cl->messagelevel = atoi(val); - clamp(cl->messagelevel, PRINT_LOW, PRINT_CHAT + 1); + cl->messagelevel = Q_clip(Q_atoi(val), PRINT_LOW, PRINT_CHAT + 1); } } diff --git a/src/server/mvd.c b/src/server/mvd.c index 9a752ac14..da3a693ef 100644 --- a/src/server/mvd.c +++ b/src/server/mvd.c @@ -142,7 +142,7 @@ static char dummy_buffer_text[MAX_STRING_CHARS]; static void dummy_wait_f(void) { - int count = atoi(Cmd_Argv(1)); + int count = Q_atoi(Cmd_Argv(1)); dummy_buffer.waitCount += max(count, 1); } diff --git a/src/server/mvd/client.c b/src/server/mvd/client.c index 325427d7d..3826e5554 100644 --- a/src/server/mvd/client.c +++ b/src/server/mvd/client.c @@ -255,7 +255,7 @@ mvd_t *MVD_SetChannel(int arg) } else #endif if (COM_IsUint(s)) { - id = atoi(s); + id = Q_atoi(s); FOR_EACH_MVD(mvd) { if (mvd->id == id) { return mvd; @@ -355,7 +355,7 @@ static gtv_t *gtv_set_conn(int arg) } if (COM_IsUint(s)) { - id = atoi(s); + id = Q_atoi(s); FOR_EACH_GTV(gtv) { if (gtv->id == id) { return gtv; @@ -2203,7 +2203,7 @@ static void MVD_Skip_f(void) return; } - count = atoi(Cmd_Argv(2)); + count = Q_atoi(Cmd_Argv(2)); if (count < 1) { count = 1; } @@ -2265,7 +2265,7 @@ static void MVD_Seek_f(void) return; } - clamp(percent, 0, 100); + percent = Q_clipf(percent, 0, 100); dest = gtv->demoofs + gtv->demosize * percent / 100; byte_seek = true; @@ -2458,7 +2458,7 @@ static void MVD_Control_f(void) Cmd_PrintHelp(options); return; case 'l': - loop = atoi(cmd_optarg); + loop = Q_atoi(cmd_optarg); if (loop < 0) { Com_Printf("Invalid value for %s option.\n", cmd_optopt); Cmd_PrintHint(); @@ -2539,7 +2539,7 @@ static void MVD_Play_f(void) "Prepend slash to specify raw path.\n"); return; case 'l': - loop = atoi(cmd_optarg); + loop = Q_atoi(cmd_optarg); if (loop < 0) { Com_Printf("Invalid value for %s option.\n", cmd_optopt); Cmd_PrintHint(); diff --git a/src/server/mvd/game.c b/src/server/mvd/game.c index 0b459fad2..ef4877124 100644 --- a/src/server/mvd/game.c +++ b/src/server/mvd/game.c @@ -1027,7 +1027,7 @@ static mvd_player_t *MVD_SetPlayer(mvd_client_t *client, const char *s) // numeric values are just slot numbers if (COM_IsUint(s)) { - i = atoi(s); + i = Q_atoi(s); if (i < 0 || i >= mvd->maxclients) { SV_ClientPrintf(client->cl, PRINT_HIGH, "[MVD] Player slot number %d is invalid.\n", i); @@ -1230,7 +1230,7 @@ static void MVD_AutoFollow_f(mvd_client_t *client) memset(client->chase_bitmap, 0, sizeof(client->chase_bitmap)); for (i = 2; i < argc; i++) { - j = atoi(Cmd_Argv(i)); + j = Q_atoi(Cmd_Argv(i)); if (j >= 0 && j < mvd->maxclients) Q_SetBit(client->chase_bitmap, j); } @@ -1943,9 +1943,9 @@ static void MVD_GameClientUserinfoChanged(edict_t *ent, char *userinfo) mvd_client_t *client = EDICT_MVDCL(ent); int fov; - client->uf = atoi(Info_ValueForKey(userinfo, "uf")); + client->uf = Q_atoi(Info_ValueForKey(userinfo, "uf")); - fov = atoi(Info_ValueForKey(userinfo, "fov")); + fov = Q_atoi(Info_ValueForKey(userinfo, "fov")); if (fov < 1) { fov = 90; } else if (fov > 160) { diff --git a/src/server/mvd/parse.c b/src/server/mvd/parse.c index 04251b90e..e003ceb17 100644 --- a/src/server/mvd/parse.c +++ b/src/server/mvd/parse.c @@ -100,7 +100,7 @@ void MVD_ParseEntityString(mvd_t *mvd, const char *data) } else if (!strcmp(key, "angles")) { sscanf(value, "%f %f", &angles[0], &angles[1]); } else if (!strcmp(key, "angle")) { - angles[1] = atof(value); + angles[1] = Q_atof(value); } } @@ -957,7 +957,7 @@ static void MVD_ParseServerData(mvd_t *mvd, int extrabits) } // parse maxclients - index = atoi(mvd->configstrings[mvd->csr->maxclients]); + index = Q_atoi(mvd->configstrings[mvd->csr->maxclients]); if (index < 1 || index > MAX_CLIENTS) { MVD_Destroyf(mvd, "Invalid maxclients"); } @@ -1006,7 +1006,7 @@ static void MVD_ParseServerData(mvd_t *mvd, int extrabits) if (ret) { Com_EPrintf("[%s] =!= Couldn't load %s: %s\n", mvd->name, string, BSP_ErrorString(ret)); // continue with null visibility - } else if (mvd->cm.cache->checksum != atoi(mvd->configstrings[mvd->csr->mapchecksum])) { + } else if (mvd->cm.cache->checksum != Q_atoi(mvd->configstrings[mvd->csr->mapchecksum])) { Com_EPrintf("[%s] =!= Local map version differs from server!\n", mvd->name); CM_FreeMap(&mvd->cm); } diff --git a/src/server/user.c b/src/server/user.c index 5d0e0798f..d51b85633 100644 --- a/src/server/user.c +++ b/src/server/user.c @@ -598,7 +598,7 @@ static void SV_BeginDownload_f(void) len = FS_NormalizePath(name); if (Cmd_Argc() > 2) - offset = atoi(Cmd_Argv(2)); // downloaded offset + offset = Q_atoi(Cmd_Argv(2)); // downloaded offset // hacked by zoid to allow more conrol over download // first off, no .. or global allow check @@ -766,7 +766,7 @@ static void SV_NextServer_f(void) if (sv.state == ss_pic && !Cvar_VariableInteger("coop")) return; // ss_pic can be nextserver'd in coop mode - if (atoi(Cmd_Argv(1)) != sv.spawncount) + if (Q_atoi(Cmd_Argv(1)) != sv.spawncount) return; // leftover from last server sv.spawncount ^= 1; // make sure another doesn't sneak in @@ -846,7 +846,7 @@ static void SV_PacketdupHack_f(void) int numdups = sv_client->numpackets - 1; if (Cmd_Argc() > 1) { - numdups = atoi(Cmd_Argv(1)); + numdups = Q_atoi(Cmd_Argv(1)); if (numdups < 0 || numdups > sv_packetdup_hack->integer) { SV_ClientPrintf(sv_client, PRINT_HIGH, "Packetdup of %d is not allowed on this server.\n", numdups); @@ -868,11 +868,11 @@ static bool match_cvar_val(const char *s, const char *v) case '*': return *v; case '=': - return atof(v) == atof(s); + return Q_atof(v) == Q_atof(s); case '<': - return atof(v) < atof(s); + return Q_atof(v) < Q_atof(s); case '>': - return atof(v) > atof(s); + return Q_atof(v) > Q_atof(s); case '~': return Q_stristr(v, s); case '#': @@ -1507,10 +1507,7 @@ static void set_client_fps(int value) if (!value) value = sv.framerate; - framediv = value / BASE_FRAMERATE; - - clamp(framediv, 1, MAX_FRAMEDIV); - + framediv = Q_clip(value / BASE_FRAMERATE, 1, MAX_FRAMEDIV); framediv = sv.framediv / Q_gcd(sv.framediv, framediv); framerate = sv.framerate / framediv; diff --git a/src/shared/shared.c b/src/shared/shared.c index afda03abf..321d6650d 100644 --- a/src/shared/shared.c +++ b/src/shared/shared.c @@ -863,6 +863,13 @@ size_t Q_strnlen(const char *s, size_t maxlen) return p ? p - s : maxlen; } +#ifndef _WIN32 +int Q_atoi(const char *s) +{ + return Q_clipl_int32(strtol(s, NULL, 10)); +} +#endif + /* ===================================================================== diff --git a/src/unix/video/sdl.c b/src/unix/video/sdl.c index 4d88284af..a596c9b32 100644 --- a/src/unix/video/sdl.c +++ b/src/unix/video/sdl.c @@ -312,7 +312,7 @@ static int get_dpi_scale(void) int scale_x = (sdl.width + sdl.win_width / 2) / sdl.win_width; int scale_y = (sdl.height + sdl.win_height / 2) / sdl.win_height; if (scale_x == scale_y) - return clamp(scale_x, 1, 10); + return Q_clip(scale_x, 1, 10); } return 1;