Reset spawn position on bed destruction
[oweals/minetest_game.git] / mods / beds / api.lua
1
2 local reverse = true
3
4 local function destruct_bed(pos, n)
5         local node = minetest.get_node(pos)
6         local other
7
8         if n == 2 then
9                 local dir = minetest.facedir_to_dir(node.param2)
10                 other = vector.subtract(pos, dir)
11         elseif n == 1 then
12                 local dir = minetest.facedir_to_dir(node.param2)
13                 other = vector.add(pos, dir)
14         end
15
16         if reverse then
17                 reverse = not reverse
18                 minetest.remove_node(other)
19                 minetest.check_for_falling(other)
20                 beds.remove_spawns_at(pos)
21                 beds.remove_spawns_at(other)
22         else
23                 reverse = not reverse
24         end
25 end
26
27 function beds.register_bed(name, def)
28         minetest.register_node(name .. "_bottom", {
29                 description = def.description,
30                 inventory_image = def.inventory_image,
31                 wield_image = def.wield_image,
32                 drawtype = "nodebox",
33                 tiles = def.tiles.bottom,
34                 paramtype = "light",
35                 paramtype2 = "facedir",
36                 is_ground_content = false,
37                 stack_max = 1,
38                 groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 1},
39                 sounds = def.sounds or default.node_sound_wood_defaults(),
40                 node_box = {
41                         type = "fixed",
42                         fixed = def.nodebox.bottom,
43                 },
44                 selection_box = {
45                         type = "fixed",
46                         fixed = def.selectionbox,
47                 },
48
49                 on_place = function(itemstack, placer, pointed_thing)
50                         local under = pointed_thing.under
51                         local node = minetest.get_node(under)
52                         local udef = minetest.registered_nodes[node.name]
53                         if udef and udef.on_rightclick and
54                                         not (placer and placer:is_player() and
55                                         placer:get_player_control().sneak) then
56                                 return udef.on_rightclick(under, node, placer, itemstack,
57                                         pointed_thing) or itemstack
58                         end
59
60                         local pos
61                         if udef and udef.buildable_to then
62                                 pos = under
63                         else
64                                 pos = pointed_thing.above
65                         end
66
67                         local player_name = placer and placer:get_player_name() or ""
68
69                         if minetest.is_protected(pos, player_name) and
70                                         not minetest.check_player_privs(player_name, "protection_bypass") then
71                                 minetest.record_protection_violation(pos, player_name)
72                                 return itemstack
73                         end
74
75                         local node_def = minetest.registered_nodes[minetest.get_node(pos).name]
76                         if not node_def or not node_def.buildable_to then
77                                 return itemstack
78                         end
79
80                         local dir = placer and placer:get_look_dir() and
81                                 minetest.dir_to_facedir(placer:get_look_dir()) or 0
82                         local botpos = vector.add(pos, minetest.facedir_to_dir(dir))
83
84                         if minetest.is_protected(botpos, player_name) and
85                                         not minetest.check_player_privs(player_name, "protection_bypass") then
86                                 minetest.record_protection_violation(botpos, player_name)
87                                 return itemstack
88                         end
89
90                         local botdef = minetest.registered_nodes[minetest.get_node(botpos).name]
91                         if not botdef or not botdef.buildable_to then
92                                 return itemstack
93                         end
94
95                         minetest.set_node(pos, {name = name .. "_bottom", param2 = dir})
96                         minetest.set_node(botpos, {name = name .. "_top", param2 = dir})
97
98                         if not (creative and creative.is_enabled_for
99                                         and creative.is_enabled_for(player_name)) then
100                                 itemstack:take_item()
101                         end
102                         return itemstack
103                 end,
104
105                 on_destruct = function(pos)
106                         destruct_bed(pos, 1)
107                 end,
108
109                 on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
110                         beds.on_rightclick(pos, clicker)
111                         return itemstack
112                 end,
113
114                 on_rotate = function(pos, node, user, _, new_param2)
115                         local dir = minetest.facedir_to_dir(node.param2)
116                         local p = vector.add(pos, dir)
117                         local node2 = minetest.get_node_or_nil(p)
118                         if not node2 or not minetest.get_item_group(node2.name, "bed") == 2 or
119                                         not node.param2 == node2.param2 then
120                                 return false
121                         end
122                         if minetest.is_protected(p, user:get_player_name()) then
123                                 minetest.record_protection_violation(p, user:get_player_name())
124                                 return false
125                         end
126                         if new_param2 % 32 > 3 then
127                                 return false
128                         end
129                         local newp = vector.add(pos, minetest.facedir_to_dir(new_param2))
130                         local node3 = minetest.get_node_or_nil(newp)
131                         local node_def = node3 and minetest.registered_nodes[node3.name]
132                         if not node_def or not node_def.buildable_to then
133                                 return false
134                         end
135                         if minetest.is_protected(newp, user:get_player_name()) then
136                                 minetest.record_protection_violation(newp, user:get_player_name())
137                                 return false
138                         end
139                         node.param2 = new_param2
140                         -- do not remove_node here - it will trigger destroy_bed()
141                         minetest.set_node(p, {name = "air"})
142                         minetest.set_node(pos, node)
143                         minetest.set_node(newp, {name = name .. "_top", param2 = new_param2})
144                         return true
145                 end,
146                 can_dig = function(pos, player)
147                         return beds.can_dig(pos)
148                 end,
149         })
150
151         minetest.register_node(name .. "_top", {
152                 drawtype = "nodebox",
153                 tiles = def.tiles.top,
154                 paramtype = "light",
155                 paramtype2 = "facedir",
156                 is_ground_content = false,
157                 pointable = false,
158                 groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 2},
159                 sounds = def.sounds or default.node_sound_wood_defaults(),
160                 drop = name .. "_bottom",
161                 node_box = {
162                         type = "fixed",
163                         fixed = def.nodebox.top,
164                 },
165                 on_destruct = function(pos)
166                         destruct_bed(pos, 2)
167                 end,
168                 can_dig = function(pos, player)
169                         local node = minetest.get_node(pos)
170                         local dir = minetest.facedir_to_dir(node.param2)
171                         local p = vector.add(pos, dir)
172                         return beds.can_dig(p)
173                 end,
174         })
175
176         minetest.register_alias(name, name .. "_bottom")
177
178         minetest.register_craft({
179                 output = name,
180                 recipe = def.recipe
181         })
182 end