nodeboxes.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. -- please keep any non-generic nodeboxe with its node definition
  2. -- this file should not accumulate any left over nodeboxes
  3. -- but is meant to host any abstractions or calculations based on nodeboxes
  4. -- a box is defined as {x1, y1, z1, x2, y2, z2}
  5. homedecor.box = {
  6. -- slab starting from -x (after rotation: left)
  7. slab_x = function(depth) return { -0.5, -0.5, -0.5, -0.5+depth, 0.5, 0.5 } end,
  8. -- bottom slab (starting from -y) with height optionally shifted upwards
  9. slab_y = function(height, shift) return { -0.5, -0.5+(shift or 0), -0.5, 0.5, -0.5+height+(shift or 0), 0.5 } end,
  10. -- slab starting from -z (+z with negative depth)
  11. slab_z = function(depth)
  12. -- for consistency with the other functions here, we have to assume that a "z" slab starts from -z and extends by depth,
  13. -- but since conventionally a lot of nodes place slabs against +z for player convenience, we define
  14. -- a "negative" depth as a depth extending from the other side, i.e. +z
  15. if depth > 0 then
  16. -- slab starting from -z
  17. return { -0.5, -0.5, -0.5, 0.5, 0.5, -0.5+depth }
  18. else
  19. -- slab starting from +z (z1=0.5-(-depth))
  20. return { -0.5, -0.5, 0.5+depth, 0.5, 0.5, 0.5 }
  21. end
  22. end,
  23. bar_y = function(radius) return {-radius, -0.5, -radius, radius, 0.5, radius} end,
  24. cuboid = function(radius_x, radius_y, radius_z)
  25. return {-radius_x, -radius_y, -radius_z, radius_x, radius_y, radius_z} end,
  26. }
  27. homedecor.nodebox = {
  28. -- { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
  29. -- can be used in-place as:
  30. -- { type="regular" },
  31. regular = { type="regular" },
  32. null = { type = "fixed", fixed = { 0, 0, 0, 0, 0, 0 } },
  33. corner_xz = function(depth_x, depth_z) return {
  34. type="fixed",
  35. fixed={
  36. homedecor.box.slab_x(depth_x),
  37. homedecor.box.slab_z(depth_z),
  38. -- { -0.5, -0.5, -0.5, 0.5-depth, 0.5, -0.5+depth } -- slab_x without the overlap, but actually looks a bit worse
  39. }
  40. } end,
  41. }
  42. local mt = {}
  43. mt.__index = function(table, key)
  44. local ref = homedecor.box[key]
  45. local ref_type = type(ref)
  46. if ref_type == "function" then
  47. return function(...)
  48. return { type = "fixed", fixed = ref(...) }
  49. end
  50. elseif ref_type == "table" then
  51. return { type = "fixed", fixed = ref }
  52. elseif ref_type == "nil" then
  53. error(key .. "could not be found among nodebox presets and functions")
  54. end
  55. error("unexpected datatype " .. tostring(type(ref)) .. " while looking for " .. key)
  56. end
  57. setmetatable(homedecor.nodebox, mt)