-- @version 1.0.1 -- @location /libs/ local utils = {} local lastTick = os.clock() local deltaTime = 0 local tickCount = 0 local lastTickTime = os.clock() local lastServerTickTime = os.clock() local lastTickDayTime = nil local tps = 20 local tickRates = {} local nextIndex = 0 local timeLastTimeUpdate = -1 local timeGameJoined = os.clock() * 1000 registerClientTick(function() tickCount = tickCount + 1 lastTickTime = os.clock() end) registerServerSetTimeEvent(function(dayTime, gameTime, tickDayTime) local now = os.clock() * 1000 if timeLastTimeUpdate ~= -1 then local timeElapsed = (now - timeLastTimeUpdate) / 1000 local currentTps = 20.0 / timeElapsed if currentTps > 20 then currentTps = 20 end if currentTps < 0 then currentTps = 0 end tickRates[nextIndex] = currentTps nextIndex = (nextIndex + 1) % 20 end timeLastTimeUpdate = now lastTickDayTime = tickDayTime end) registerWorldRenderer(function() local currentTick = os.clock() deltaTime = currentTick - lastTick lastTick = currentTick end) ---Reurns sqrt dist between points ---@param pos1 table ---@param pos2 table ---@return number function utils.getDistance(pos1, pos2) return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2) end ---comment ---@param o table ---@param depth number|nil ---@return unknown function utils.dump(o, depth) depth = depth or 0 local indent = string.rep(' ', depth) if type(o) == 'table' then local s = '{\n' for k, v in pairs(o) do if type(k) ~= 'number' then k = '"' .. k .. '"' end s = s .. indent .. ' [' .. k .. '] = ' .. utils.dump(v, depth + 1) .. ',\n' end return s .. indent .. '}' else return tostring(o) end end ---Returns seconds between frames ---@return integer function utils.deltaTime() return deltaTime end ---Rounds a value to n after comma ---@param n number ---@param decimals number ---@return unknown function utils.round(n, decimals) local factor = 10 ^ (decimals or 0) return math.floor(n * factor + 0.5) / factor end ---Normalizes minecraft yaw ---@param yaw number ---@return number function utils.normalizeYaw(yaw) yaw = yaw % 360 if yaw > 180 then yaw = yaw - 360 end return yaw end ---Returns ticks since library load ---@return integer function utils.tickCount() return tickCount end ---Returns date from unix time ---@param stamp number ---@return string|osdate function utils.timestampToDate(stamp) stamp = math.floor(stamp / 1000) return os.date("%Y-%m-%d %H:%M:%S", stamp) end ---Returns fps of the game ---@return integer function utils.getFPS() if deltaTime > 0 then return math.ceil(1 / deltaTime) end return 0 end ---Returns current TPS (Ticks Per Second) ---@return number function utils.getTPS() if os.clock() * 1000 - timeGameJoined < 4000 then return 20 end local numTicks = 0 local sumTickRates = 0 for _, tickRate in ipairs(tickRates) do if tickRate and tickRate > 0 then sumTickRates = sumTickRates + tickRate numTicks = numTicks + 1 end end if numTicks > 0 then return sumTickRates / numTicks end return 0 end ---Returns time since last tick was received (in seconds) ---@return number function utils.getTimeSinceLastTick() if os.clock() * 1000 - timeGameJoined < 4000 then return 0 end return (os.clock() * 1000 - timeLastTimeUpdate) / 1000 end return utils