api_utility.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. local mod_player_monoids = minetest.get_modpath("player_monoids") ~= nil
  2. local mod_playerphysics = minetest.get_modpath("playerphysics") ~= nil
  3. local mod_pova = minetest.get_modpath("pova") ~= nil
  4. local utility = {}
  5. function utility.rangelim(value, min, max)
  6. return math.min(math.max(value, min), max)
  7. end
  8. -- from https://stackoverflow.com/a/29133654
  9. -- merges two tables together
  10. -- if in conflict, b will override values of a
  11. function utility.merge_tables(a, b)
  12. if type(a) == "table" and type(b) == "table" then
  13. for k,v in pairs(b) do
  14. if type(v)=="table" and type(a[k] or false)=="table" then
  15. utility.merge_tables(a[k],v)
  16. else a[k]=v end
  17. end
  18. end
  19. return a
  20. end
  21. -- see https://en.wikipedia.org/wiki/Logistic_function
  22. function utility.sigmoid(value, max, growth, midpoint)
  23. return max / (1 + math.exp(-growth * (value - midpoint)))
  24. end
  25. -- generates a wave of cycle length 1
  26. -- maps parameters to values between 0 and 1
  27. -- 0 is mapped to 0 and 0.5 to 1
  28. function utility.normalized_cycle(value)
  29. return math.cos((2 * value + 1) * math.pi) / 2 + 0.5
  30. end
  31. return utility