nodeboxes.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, shift)
  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. local s = shift or 0
  16. if depth > 0 then
  17. -- slab starting from -z
  18. return { -0.5, -0.5, -0.5+s, 0.5, 0.5, -0.5+depth+s }
  19. else
  20. -- slab starting from +z (z1=0.5-(-depth))
  21. return { -0.5, -0.5, 0.5+depth+s, 0.5, 0.5, 0.5+s }
  22. end
  23. end,
  24. bar_y = function(radius) return {-radius, -0.5, -radius, radius, 0.5, radius} end,
  25. cuboid = function(radius_x, radius_y, radius_z)
  26. return {-radius_x, -radius_y, -radius_z, radius_x, radius_y, radius_z} end,
  27. }
  28. homedecor.nodebox = {
  29. -- { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
  30. -- can be used in-place as:
  31. -- { type="regular" },
  32. regular = { type="regular" },
  33. null = { type = "fixed", fixed = { 0, 0, 0, 0, 0, 0 } },
  34. corner_xz = function(depth_x, depth_z) return {
  35. type="fixed",
  36. fixed={
  37. homedecor.box.slab_x(depth_x),
  38. homedecor.box.slab_z(depth_z),
  39. -- { -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
  40. }
  41. } end,
  42. }
  43. local mt = {}
  44. mt.__index = function(table, key)
  45. local ref = homedecor.box[key]
  46. local ref_type = type(ref)
  47. if ref_type == "function" then
  48. return function(...)
  49. return { type = "fixed", fixed = ref(...) }
  50. end
  51. elseif ref_type == "table" then
  52. return { type = "fixed", fixed = ref }
  53. elseif ref_type == "nil" then
  54. error(key .. "could not be found among nodebox presets and functions")
  55. end
  56. error("unexpected datatype " .. tostring(type(ref)) .. " while looking for " .. key)
  57. end
  58. setmetatable(homedecor.nodebox, mt)