2
0

item_s.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. -- Minetest: builtin/item_s.lua
  2. -- The distinction of what goes here is a bit tricky, basically it's everything
  3. -- that does not (directly or indirectly) need access to ServerEnvironment,
  4. -- Server or writable access to IGameDef on the engine side.
  5. -- (The '_s' stands for standalone.)
  6. --
  7. -- Item definition helpers
  8. --
  9. function core.inventorycube(img1, img2, img3)
  10. img2 = img2 or img1
  11. img3 = img3 or img1
  12. return "[inventorycube"
  13. .. "{" .. img1:gsub("%^", "&")
  14. .. "{" .. img2:gsub("%^", "&")
  15. .. "{" .. img3:gsub("%^", "&")
  16. end
  17. function core.dir_to_facedir(dir, is6d)
  18. --account for y if requested
  19. if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
  20. --from above
  21. if dir.y < 0 then
  22. if math.abs(dir.x) > math.abs(dir.z) then
  23. if dir.x < 0 then
  24. return 19
  25. else
  26. return 13
  27. end
  28. else
  29. if dir.z < 0 then
  30. return 10
  31. else
  32. return 4
  33. end
  34. end
  35. --from below
  36. else
  37. if math.abs(dir.x) > math.abs(dir.z) then
  38. if dir.x < 0 then
  39. return 15
  40. else
  41. return 17
  42. end
  43. else
  44. if dir.z < 0 then
  45. return 6
  46. else
  47. return 8
  48. end
  49. end
  50. end
  51. --otherwise, place horizontally
  52. elseif math.abs(dir.x) > math.abs(dir.z) then
  53. if dir.x < 0 then
  54. return 3
  55. else
  56. return 1
  57. end
  58. else
  59. if dir.z < 0 then
  60. return 2
  61. else
  62. return 0
  63. end
  64. end
  65. end
  66. -- Table of possible dirs
  67. local facedir_to_dir = {
  68. vector.new( 0, 0, 1),
  69. vector.new( 1, 0, 0),
  70. vector.new( 0, 0, -1),
  71. vector.new(-1, 0, 0),
  72. vector.new( 0, -1, 0),
  73. vector.new( 0, 1, 0),
  74. }
  75. -- Mapping from facedir value to index in facedir_to_dir.
  76. local facedir_to_dir_map = {
  77. [0]=1, 2, 3, 4,
  78. 5, 2, 6, 4,
  79. 6, 2, 5, 4,
  80. 1, 5, 3, 6,
  81. 1, 6, 3, 5,
  82. 1, 4, 3, 2,
  83. }
  84. function core.facedir_to_dir(facedir)
  85. return facedir_to_dir[facedir_to_dir_map[facedir % 32]]
  86. end
  87. function core.dir_to_wallmounted(dir)
  88. if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
  89. if dir.y < 0 then
  90. return 1
  91. else
  92. return 0
  93. end
  94. elseif math.abs(dir.x) > math.abs(dir.z) then
  95. if dir.x < 0 then
  96. return 3
  97. else
  98. return 2
  99. end
  100. else
  101. if dir.z < 0 then
  102. return 5
  103. else
  104. return 4
  105. end
  106. end
  107. end
  108. -- table of dirs in wallmounted order
  109. local wallmounted_to_dir = {
  110. [0] = vector.new( 0, 1, 0),
  111. vector.new( 0, -1, 0),
  112. vector.new( 1, 0, 0),
  113. vector.new(-1, 0, 0),
  114. vector.new( 0, 0, 1),
  115. vector.new( 0, 0, -1),
  116. }
  117. function core.wallmounted_to_dir(wallmounted)
  118. return wallmounted_to_dir[wallmounted % 8]
  119. end
  120. function core.dir_to_yaw(dir)
  121. return -math.atan2(dir.x, dir.z)
  122. end
  123. function core.yaw_to_dir(yaw)
  124. return vector.new(-math.sin(yaw), 0, math.cos(yaw))
  125. end
  126. function core.is_colored_paramtype(ptype)
  127. return (ptype == "color") or (ptype == "colorfacedir") or
  128. (ptype == "colorwallmounted") or (ptype == "colordegrotate")
  129. end
  130. function core.strip_param2_color(param2, paramtype2)
  131. if not core.is_colored_paramtype(paramtype2) then
  132. return nil
  133. end
  134. if paramtype2 == "colorfacedir" then
  135. param2 = math.floor(param2 / 32) * 32
  136. elseif paramtype2 == "colorwallmounted" then
  137. param2 = math.floor(param2 / 8) * 8
  138. elseif paramtype2 == "colordegrotate" then
  139. param2 = math.floor(param2 / 32) * 32
  140. end
  141. -- paramtype2 == "color" requires no modification.
  142. return param2
  143. end