init.lua 6.2 KB

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