Place doors to the close side of block instead of the far side
[oweals/minetest_game.git] / mods / doors / init.lua
1 -- Minetest 0.4 mod: doors
2 -- See README.txt for licensing and other information.
3 --------------------------------------------------------------------------------
4
5 local WALLMX = 3
6 local WALLMZ = 5
7 local WALLPX = 2
8 local WALLPZ = 4
9
10 --------------------------------------------------------------------------------
11
12 minetest.register_alias('door', 'doors:door_wood')
13 minetest.register_alias('door_wood', 'doors:door_wood')
14
15 minetest.register_node( 'doors:door_wood', {
16         description         = 'Wooden Door',
17         drawtype            = 'signlike',
18         tile_images         = { 'door_wood.png' },
19         inventory_image     = 'door_wood.png',
20         wield_image         = 'door_wood.png',
21         paramtype2          = 'wallmounted',
22         selection_box       = { type = 'wallmounted' },
23         groups              = { choppy=2, dig_immediate=2 },
24 })
25
26 minetest.register_craft( {
27         output              = 'doors:door_wood',
28         recipe = {
29                 { 'default:wood', 'default:wood' },
30                 { 'default:wood', 'default:wood' },
31                 { 'default:wood', 'default:wood' },
32         },
33 })
34
35 minetest.register_craft({
36         type = 'fuel',
37         recipe = 'doors:door_wood',
38         burntime = 30,
39 })
40
41 minetest.register_node( 'doors:door_wood_a_c', {
42         Description         = 'Top Closed Door',
43         drawtype            = 'signlike',
44         tile_images         = { 'door_wood_a.png' },
45         inventory_image     = 'door_wood_a.png',
46         paramtype           = 'light',
47         paramtype2          = 'wallmounted',
48         walkable            = true,
49         selection_box       = { type = "wallmounted", },
50         groups              = { choppy=2, dig_immediate=2 },
51         legacy_wallmounted  = true,
52         drop                = 'doors:door_wood',
53 })
54
55 minetest.register_node( 'doors:door_wood_b_c', {
56         Description         = 'Bottom Closed Door',
57         drawtype            = 'signlike',
58         tile_images         = { 'door_wood_b.png' },
59         inventory_image     = 'door_wood_b.png',
60         paramtype           = 'light',
61         paramtype2          = 'wallmounted',
62         walkable            = true,
63         selection_box       = { type = "wallmounted", },
64         groups              = { choppy=2, dig_immediate=2 },
65         legacy_wallmounted  = true,
66         drop                = 'doors:door_wood',
67 })
68
69 minetest.register_node( 'doors:door_wood_a_o', {
70         Description         = 'Top Open Door',
71         drawtype            = 'signlike',
72         tile_images         = { 'door_wood_a_r.png' },
73         inventory_image     = 'door_wood_a_r.png',
74         paramtype           = 'light',
75         paramtype2          = 'wallmounted',
76         walkable            = false,
77         selection_box       = { type = "wallmounted", },
78         groups              = { choppy=2, dig_immediate=2 },
79         legacy_wallmounted  = true,
80         drop                = 'doors:door_wood',
81 })
82
83 minetest.register_node( 'doors:door_wood_b_o', {
84         Description         = 'Bottom Open Door',
85         drawtype            = 'signlike',
86         tile_images         = { 'door_wood_b_r.png' },
87         inventory_image     = 'door_wood_b_r.png',
88         paramtype           = 'light',
89         paramtype2          = 'wallmounted',
90         walkable            = false,
91         selection_box       = { type = "wallmounted", },
92         groups              = { choppy=2, dig_immediate=2 },
93         legacy_wallmounted  = true,
94         drop                = 'doors:door_wood',
95 })
96
97 --------------------------------------------------------------------------------
98
99 local round = function( n )
100         if n >= 0 then
101                 return math.floor( n + 0.5 )
102         else
103                 return math.ceil( n - 0.5 )
104         end
105 end
106
107 local on_door_placed = function( pos, node, placer )
108         if node.name ~= 'doors:door_wood' then return end
109
110         upos = { x = pos.x, y = pos.y - 1, z = pos.z }
111         apos = { x = pos.x, y = pos.y + 1, z = pos.z }
112         und = minetest.env:get_node( upos )
113         abv = minetest.env:get_node( apos )
114
115         dir = placer:get_look_dir()
116
117         if     round( dir.x ) == 1  then
118                 newparam = WALLMX
119         elseif round( dir.x ) == -1 then
120                 newparam = WALLPX
121         elseif round( dir.z ) == 1  then
122                 newparam = WALLMZ
123         elseif round( dir.z ) == -1 then
124                 newparam = WALLPZ
125         end
126
127         if und.name == 'air' then
128                 minetest.env:add_node( pos,  { name = 'doors:door_wood_a_c', param2 = newparam } )
129                 minetest.env:add_node( upos, { name = 'doors:door_wood_b_c', param2 = newparam } )
130         elseif abv.name == 'air' then
131                 minetest.env:add_node( pos,  { name = 'doors:door_wood_b_c', param2 = newparam } )
132                 minetest.env:add_node( apos, { name = 'doors:door_wood_a_c', param2 = newparam } )
133         else
134                 minetest.env:remove_node( pos )
135                 placer:get_inventory():add_item( "main", 'doors:door_wood' )
136                 minetest.chat_send_player( placer:get_player_name(), 'not enough space' )
137         end
138 end
139
140 local on_door_punched = function( pos, node, puncher )
141         if string.find( node.name, 'doors:door_wood' ) == nil then return end
142
143         upos = { x = pos.x, y = pos.y - 1, z = pos.z }
144         apos = { x = pos.x, y = pos.y + 1, z = pos.z }
145
146         if string.find( node.name, '_c', -2 ) ~= nil then
147                 if     node.param2 == WALLPX then
148                         newparam = WALLMZ
149                 elseif node.param2 == WALLMZ then
150                         newparam = WALLMX
151                 elseif node.param2 == WALLMX then
152                         newparam = WALLPZ
153                 elseif node.param2 == WALLPZ then
154                         newparam = WALLPX
155                 end
156         elseif string.find( node.name, '_o', -2 ) ~= nil then
157                 if     node.param2 == WALLMZ then
158                         newparam = WALLPX
159                 elseif node.param2 == WALLMX then
160                         newparam = WALLMZ
161                 elseif node.param2 == WALLPZ then
162                         newparam = WALLMX
163                 elseif node.param2 == WALLPX then
164                         newparam = WALLPZ
165                 end
166         end
167
168         if ( node.name == 'doors:door_wood_a_c' ) then
169                 minetest.env:add_node( pos,  { name = 'doors:door_wood_a_o', param2 = newparam } )
170                 minetest.env:add_node( upos, { name = 'doors:door_wood_b_o', param2 = newparam } )
171
172         elseif ( node.name == 'doors:door_wood_b_c' ) then
173                 minetest.env:add_node( pos,  { name = 'doors:door_wood_b_o', param2 = newparam } )
174                 minetest.env:add_node( apos, { name = 'doors:door_wood_a_o', param2 = newparam } )
175
176         elseif ( node.name == 'doors:door_wood_a_o' ) then
177                 minetest.env:add_node( pos,  { name = 'doors:door_wood_a_c', param2 = newparam } )
178                 minetest.env:add_node( upos, { name = 'doors:door_wood_b_c', param2 = newparam } )
179
180         elseif ( node.name == 'doors:door_wood_b_o' ) then
181                 minetest.env:add_node( pos,  { name = 'doors:door_wood_b_c', param2 = newparam } )
182                 minetest.env:add_node( apos, { name = 'doors:door_wood_a_c', param2 = newparam } )
183
184         end
185 end
186
187 local on_door_digged = function( pos, node, digger )
188         upos = { x = pos.x, y = pos.y - 1, z = pos.z }
189         apos = { x = pos.x, y = pos.y + 1, z = pos.z }
190
191         if ( node.name == 'doors:door_wood_a_c' ) or ( node.name == 'doors:door_wood_a_o' ) then
192                 minetest.env:remove_node( upos )
193         elseif ( node.name == 'doors:door_wood_b_c' ) or ( node.name == 'doors:door_wood_b_o' ) then
194                 minetest.env:remove_node( apos )
195         end
196 end
197
198 --------------------------------------------------------------------------------
199
200 minetest.register_on_placenode( on_door_placed )
201 minetest.register_on_punchnode( on_door_punched )
202 minetest.register_on_dignode( on_door_digged )
203
204 --------------------------------------------------------------------------------
205