function constrain(val, low, high) if val > high then return high elseif val < low then return low else return val end end function map(value, inMin, inMax, outMin, outMax) local inPercent = value / (inMax - inMin) return (outMax - outMin) * (inPercent) + outMin end function lerp(a, b, amount) return (1 - amount) * a + (amount * b) end -- Write to the project tree itself (not LÖVE's save dir) so edits persist in the -- repo during dev. Uses plain io.open against love.filesystem.getSource(), which -- is the absolute project path when running `love .` from a folder. Falls back to -- love.filesystem.write (save dir) if the source is a packaged .love archive. function writeToSource(relPath, contents) local base = love.filesystem.getSource() local file = io.open(base .. "/" .. relPath, "w") if file then file:write(contents) file:close() return true end return love.filesystem.write(relPath, contents) end -- Read from the project tree directly so a stale copy in LÖVE's save directory -- can't shadow the real source file. Falls back to love.filesystem for a -- packaged .love. function readFromSource(relPath) local base = love.filesystem.getSource() local file = io.open(base .. "/" .. relPath, "r") if file then local contents = file:read("*a") file:close() return contents end return love.filesystem.read(relPath) end