init.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. kotatsu_table = {}
  2. kotatsu_table.toggle_sitting = function(npos, _, player)
  3. local meta = minetest.get_meta(npos)
  4. local tablepos = minetest.string_to_pos(meta:get_string("p"))
  5. if not tablepos then minetest.remove_node(npos); return end
  6. local name = player:get_player_name()
  7. if default.player_attached[name] then
  8. player:set_pos(vector.add(player:get_pos(), {x=0,y=1,z=0}))
  9. default.player_attached[name] = false
  10. player:set_physics_override(1, 1, 1)
  11. player:set_eye_offset({x=0, y=0, z=0}, {x=0, y=0, z=0})
  12. default.player_set_animation(player, "stand", 30)
  13. else
  14. local v = player:get_player_velocity()
  15. if v.x ~= 0 or v.y ~= 0 or v.z ~= 0 then return end
  16. local ppos = player:get_pos()
  17. ppos.x = ppos.x - tablepos.x
  18. ppos.y = tablepos.y - 0.5
  19. ppos.z = ppos.z - tablepos.z
  20. local inside = ppos.x > -1.25 and ppos.x < 1.25 and
  21. ppos.z > -1.25 and ppos.z < 1.25
  22. if inside then
  23. if math.abs(ppos.x) > math.abs(ppos.z) then
  24. ppos.x = (ppos.x > 0) and 1.25 or -1.25
  25. else
  26. ppos.z = (ppos.z > 0) and 1.25 or -1.25
  27. end
  28. else
  29. ppos.x = math.max(-1.25, math.min(1.25, ppos.x))
  30. ppos.z = math.max(-1.25, math.min(1.25, ppos.z))
  31. end
  32. ppos.x = ppos.x + tablepos.x
  33. ppos.z = ppos.z + tablepos.z
  34. player:set_pos(ppos)
  35. player:set_physics_override(0, 0, 0)
  36. default.player_attached[name] = true
  37. player:set_eye_offset({x=0, y=-5, z=0}, {x=0, y=0, z=0})
  38. default.player_set_animation(player, "sit", 30)
  39. end
  40. end
  41. kotatsu_table.tabletop_nodebox = {
  42. type="fixed",
  43. fixed = {
  44. {-0.8,0.25,-0.8,0.8,0.3,0.8},
  45. }
  46. }
  47. kotatsu_table.blanket_nodebox = {
  48. type="fixed",
  49. fixed = {
  50. {0.125,-0.125,0.125,1.875,0.25,1.875},
  51. {-0.125,-0.375,-0.125,2.125,-0.125,2.125},
  52. {-0.5,-0.5,-0.5,2.5,-0.375,2.5},
  53. }
  54. }
  55. kotatsu_table.blanket_side_nodebox = {
  56. type="fixed",
  57. fixed = {
  58. {-0.5,-0.125,0.125,0.5,0.25,0.5},
  59. {-0.5,-0.375,-0.125,0.5,-0.125,0.5},
  60. {-0.5,-0.5,-0.5,0.5,-0.375,0.5},
  61. }
  62. }
  63. kotatsu_table.blanket_corner_nodebox = {
  64. type="fixed",
  65. fixed = {
  66. {0.125,-0.125,0.125,0.5,0.25,0.5},
  67. {-0.125,-0.375,-0.125,0.5,-0.125,0.5},
  68. {-0.5,-0.5,-0.5,0.5,-0.375,0.5},
  69. }
  70. }
  71. kotatsu_table.pos_check = {
  72. {offset={x=-1,y=0,z=0},ext="_side",param2=1},
  73. {offset={x=1,y=0,z=0},ext="_side",param2=3},
  74. {offset={x=0,y=0,z=-1},ext="_side",param2=0},
  75. {offset={x=0,y=0,z=1},ext="_side",param2=2},
  76. {offset={x=1,y=0,z=1},ext="_corner",param2=2},
  77. {offset={x=-1,y=0,z=1},ext="_corner",param2=1},
  78. {offset={x=1,y=0,z=-1},ext="_corner",param2=3},
  79. {offset={x=-1,y=0,z=-1},ext="_corner",param2=0}
  80. }
  81. kotatsu_table.register_table = function(name, desc, base, tiles, top, top_tiles, inv_image)
  82. base = base or "wool:white" --Seriously, though, don't omit base...
  83. tiles = tiles or (minetest.registered_nodes[base] or {tiles={"wool_white.png"}}).tiles
  84. top = top or "group:wood"
  85. top_tiles = top_tiles or (minetest.registered_nodes[top] or {tiles={"default_wood.png"}}).tiles
  86. inv_image = inv_image or "(kotatsu_base.png^[mask:"..tiles[1]..")^(kotatsu_top.png^[mask:"..top_tiles[1]..")"
  87. local kotatsu = name or ("kotatsu_table:table_"..string.gsub(base, ":", "_"))
  88. desc = (desc or (minetest.registered_nodes[base] or {description="Terribly Programmed"}).description or "Very Terribly Programmed").." Kotatsu Table"
  89. minetest.register_node(kotatsu.."_side", {
  90. description = desc.." Blanket Side (you hacker, you)",
  91. paramtype = "light",
  92. walkable = false,
  93. drawtype = "nodebox",
  94. paramtype = "light",
  95. paramtype2 = "facedir",
  96. tiles = tiles,
  97. drop = "",
  98. node_box = kotatsu_table.blanket_side_nodebox,
  99. diggable=false,
  100. on_rightclick = kotatsu_table.toggle_sitting,
  101. groups={not_in_creative_inventory=1,immovable=2},
  102. on_blast = function() end,
  103. })
  104. minetest.register_node(kotatsu.."_corner", {
  105. description = desc.." Blanket Corner (you hacker, you)",
  106. paramtype = "light",
  107. walkable = false,
  108. drawtype = "nodebox",
  109. paramtype = "light",
  110. paramtype2 = "facedir",
  111. tiles = tiles,
  112. drop = "",
  113. node_box = kotatsu_table.blanket_corner_nodebox,
  114. diggable=false,
  115. on_rightclick = kotatsu_table.toggle_sitting,
  116. groups={not_in_creative_inventory=1,immovable=2},
  117. on_blast = function() end,
  118. })
  119. minetest.register_node(kotatsu, {
  120. description = desc,
  121. inventory_image = inv_image,
  122. wield_image = inv_image,
  123. drawtype = "nodebox",
  124. tiles = top_tiles,
  125. node_box = kotatsu_table.tabletop_nodebox,
  126. groups={choppy=3,oddly_breakable_by_hand=2,immovable=2},
  127. on_construct = function(pos)
  128. local npos
  129. for _,p in ipairs(kotatsu_table.pos_check) do
  130. npos = vector.add(p.offset, pos)
  131. local n = minetest.get_node_or_nil(npos)
  132. if not n or not (minetest.registered_nodes[n.name] or {}).buildable_to then
  133. minetest.remove_node(pos)
  134. minetest.add_item(pos, kotatsu)
  135. return
  136. end
  137. end
  138. for _,p in ipairs(kotatsu_table.pos_check) do -- Check passed, now build
  139. npos = vector.add(p.offset, pos)
  140. minetest.set_node(npos, {name=kotatsu..p.ext,param2=p.param2})
  141. minetest.after(0, function(np)minetest.get_meta(np):set_string("p", minetest.pos_to_string(pos))end, npos)
  142. end
  143. end,
  144. on_destruct = function(pos)
  145. local npos
  146. for _,p in ipairs(kotatsu_table.pos_check) do
  147. npos = vector.add(p.offset, pos)
  148. local n = minetest.get_node(npos)
  149. if n.name == kotatsu..p.ext and n.param2 == p.param2 then
  150. minetest.remove_node(npos)
  151. end
  152. end
  153. end,
  154. })
  155. minetest.register_craft({
  156. output=kotatsu,
  157. recipe={
  158. {top,top,top},
  159. {base,"default:mese_crystal",base},
  160. {base,"",base},
  161. },
  162. })
  163. end
  164. kotatsu_table.wool_dyes = {
  165. {"white", "White"},
  166. {"grey", "Grey"},
  167. {"black", "Black"},
  168. {"red", "Red"},
  169. {"yellow", "Yellow"},
  170. {"green", "Green"},
  171. {"cyan", "Cyan"},
  172. {"blue", "Blue"},
  173. {"magenta", "Magenta"},
  174. {"orange", "Orange"},
  175. {"violet", "Violet"},
  176. {"brown", "Brown"},
  177. {"pink", "Pink"},
  178. {"dark_grey", "Dark Grey"},
  179. {"dark_green", "Dark Green"},
  180. }
  181. for _,d in pairs(kotatsu_table.wool_dyes) do
  182. kotatsu_table.register_table(nil, d[2], "wool:"..d[1])
  183. end