From 3b8d2b2e8c08527f804d60c8f2b82bbc7f13e494 Mon Sep 17 00:00:00 2001 From: Mihail Date: Wed, 31 Jul 2024 16:11:50 +0400 Subject: [PATCH] Add files via upload --- res/modules/internal/math.lua | 11 ++++++ res/modules/internal/string.lua | 65 +++++++++++++++++++++++++++++++++ res/modules/internal/table.lua | 17 +++++++++ 3 files changed, 93 insertions(+) create mode 100644 res/modules/internal/math.lua create mode 100644 res/modules/internal/string.lua create mode 100644 res/modules/internal/table.lua diff --git a/res/modules/internal/math.lua b/res/modules/internal/math.lua new file mode 100644 index 00000000..6637a5cc --- /dev/null +++ b/res/modules/internal/math.lua @@ -0,0 +1,11 @@ +function math.Rand( low, high ) + return low + (high - low) * math.random() +end + +function math.Clamp( _in, low, high ) + return math.min(math.max( _in, low ), high) +end + +function math.Lerp(frac, from, to) + return from + (to - from) * frac +end \ No newline at end of file diff --git a/res/modules/internal/string.lua b/res/modules/internal/string.lua new file mode 100644 index 00000000..944ce0a0 --- /dev/null +++ b/res/modules/internal/string.lua @@ -0,0 +1,65 @@ +function string.FormattedTime(seconds, format ) -- from gmod + if not seconds then seconds = 0 end + + local hours = math.floor(seconds / 3600) + local minutes = math.floor((seconds / 60) % 60) + local millisecs = (seconds - math.floor(seconds)) * 100 + seconds = math.floor(seconds % 60) + + if format then + return string.format(format, minutes, seconds, millisecs) + else + return {h = hours, m = minutes, s = seconds, ms = millisecs} + end +end + +function string.Left(str, num) return string.sub(str, 1, num) end +function string.Right(str, num) return string.sub(str, -num) end + +function string.Replace(str, tofind, toreplace) + local tbl = string.Explode(tofind, str) + if (tbl[1]) then return table.concat(tbl, toreplace) end + return str +end + +local totable = string.ToTable +local string_sub = string.sub +local string_find = string.find +local string_len = string.len +function string.Explode(separator, str, withpattern) + if (separator == "") then return totable(str) end + if (withpattern == nil) then withpattern = false end + + local ret = {} + local current_pos = 1 + + for i = 1, string_len(str) do + local start_pos, end_pos = string_find(str, separator, current_pos, not withpattern) + if (not start_pos) then break end + ret[i] = string_sub(str, current_pos, start_pos - 1) + current_pos = end_pos + 1 + end + + ret[#ret + 1] = string_sub(str, current_pos) + + return ret +end + +function string.Split( str, delimiter ) + return string.Explode( delimiter, str ) +end + +function string.Trim(s, char) + if char then char = string.PatternSafe(char) else char = "%s" end + return string.match(s, "^" .. char .. "*(.-)" .. char .. "*$") or s +end + +function string.TrimRight(s, char) + if char then char = string.PatternSafe(char) else char = "%s" end + return string.match(s, "^(.-)" .. char .. "*$") or s +end + +function string.TrimLeft(s, char) + if char then char = string.PatternSafe(char) else char = "%s" end + return string.match(s, "^" .. char .. "*(.+)$") or s +end \ No newline at end of file diff --git a/res/modules/internal/table.lua b/res/modules/internal/table.lua new file mode 100644 index 00000000..3dc0ee39 --- /dev/null +++ b/res/modules/internal/table.lua @@ -0,0 +1,17 @@ +function table.Copy(t) + local newT = {} + for k, v in pairs(t) do + newT[k] = v + end + return newT +end + +function table.Random(t) + local randomT = {} + for k, v in pairs(t) do randomT[#randomT + 1] = {k, v} end + + local var = randomT[math.random(1, #randomT)] + local key, value = var[1], var[2] + + return value, key +end \ No newline at end of file