]> git.lizzy.rs Git - Crafter.git/blob - mods/redstone/torch.lua
Fix a bug in redstone
[Crafter.git] / mods / redstone / torch.lua
1 --get point where particle spawner is added
2 local function get_offset(wdir)
3       local z = 0
4       local x = 0
5       if wdir == 4 then
6             z = 0.25
7       elseif wdir == 2 then
8             x = 0.25
9       elseif wdir == 5 then
10             z = -0.25
11       elseif wdir == 3 then
12             x = -0.25
13       end
14       return {x = x, y = 0.27, z = z}      
15 end
16
17 --remove smoke and fire
18 local function delete_ps(pos)
19       local meta = minetest.get_meta(pos)
20       minetest.delete_particlespawner(meta:get_int("psf"))
21       minetest.delete_particlespawner(meta:get_int("pss"))
22 end
23
24 --add in smoke and fire
25 local function create_ps(pos)
26       local dir = get_offset(minetest.get_node(pos).param2)
27       local ppos = vector.add(dir,pos)
28       local meta = minetest.get_meta(pos)
29       local psf = minetest.add_particlespawner({
30             amount = 2,
31             time = 0,
32             minpos = ppos,
33             maxpos = ppos,
34             minvel = vector.new(0,0,0),
35             maxvel = vector.new(0,0,0),
36             minacc = {x=0, y=0, z=0},
37             maxacc = {x=0, y=0, z=0},
38             minexptime = 1,
39             maxexptime = 1,
40             minsize = 3,
41             maxsize = 3,
42             collisiondetection = false,
43             vertical = true,
44             texture = "redstone_torch_animated.png",
45             animation = {type = "vertical_frames",
46
47                   aspect_w = 16,
48                   -- Width of a frame in pixels
49
50                   aspect_h = 16,
51                   -- Height of a frame in pixels
52
53                   length =  0.2,
54                   -- Full loop length
55             },
56       })
57       local pss = minetest.add_particlespawner({
58             amount = 2,
59             time = 0,
60             minpos = ppos,
61             maxpos = ppos,
62             minvel = vector.new(-0.1,0.1,-0.1),
63             maxvel = vector.new(0.1,0.3,0.1),
64             minacc = vector.new(0,0,0),
65             maxacc = vector.new(0,0,0),
66             minexptime = 1,
67             maxexptime = 2,
68             minsize = 1,
69             maxsize = 2,
70             collisiondetection = false,
71             vertical = false,
72             texture = "smoke.png",
73       })
74       meta:set_int("psf", psf)
75       meta:set_int("pss", pss)
76 end
77
78 --reload smoke and flame on load
79 minetest.register_lbm({
80       name = "redstone:torch",
81       nodenames = {"redstone:torch_floor","redstone:torch_wall"},
82       run_at_every_load = true,
83       action = function(pos, node)
84             create_ps(pos)
85       end,
86 })
87
88 -- Item definitions
89 minetest.register_craftitem("redstone:torch", {
90       description = "Redstone Torch",
91       inventory_image = "redstone_torch.png",
92       wield_image = "redstone_torch.png",
93       wield_scale = {x = 1, y = 1, z = 1 + 1/16},
94       liquids_pointable = false,
95       power = 9,
96       on_place = function(itemstack, placer, pointed_thing)
97             if pointed_thing.type ~= "node" then
98                   return itemstack
99             end
100
101             local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
102
103             local fakestack = itemstack
104             local retval = false
105             if wdir < 1 then
106                   return itemstack
107             elseif wdir == 1 then
108                   retval = fakestack:set_name("redstone:torch_floor")
109             else
110                   retval = fakestack:set_name("redstone:torch_wall")
111             end
112             if not retval then
113                   return itemstack
114             end
115             itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
116             itemstack:set_name("redstone:torch")
117
118             if retval then
119                   minetest.sound_play("wood", {pos=pointed_thing.above, gain = 1.0})
120             end
121
122             return itemstack
123       end
124 })
125
126 minetest.register_node("redstone:torch_floor", {
127       inventory_image = "redstone_torch.png",
128       wield_image = "redstone_torch.png",
129       wield_scale = {x = 1, y = 1, z = 1 + 2/16},
130       drawtype = "mesh",
131       mesh = "torch_floor.obj",
132       tiles = {"redstone_torch.png"},
133       paramtype = "light",
134       paramtype2 = "none",
135       power = 9,
136       sunlight_propagates = true,
137       drop = "redstone:torch",
138       walkable = false,
139       light_source = 13,
140       groups = {choppy=2, dig_immediate=3, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,connect_to_raillike=1},
141       legacy_wallmounted = true,
142       selection_box = {
143             type = "fixed",
144             fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
145       },
146       on_construct = function(pos)
147             create_ps(pos)
148             redstone.update(pos)
149       end,
150       after_destruct = function(pos, oldnode)
151             redstone.update(pos,oldnode)
152       end,
153       on_destruct = function(pos)
154             delete_ps(pos)
155       end,
156       sounds = main.woodSound(),
157 })
158
159 minetest.register_node("redstone:torch_wall", {
160       inventory_image = "redstone_torch.png",
161       wield_image = "redstone_torch.png",
162       wield_scale = {x = 1, y = 1, z = 1 + 1/16},
163       drawtype = "mesh",
164       mesh = "torch_wall.obj",
165       tiles = {"redstone_torch.png"},
166       paramtype = "light",
167       paramtype2 = "wallmounted",
168       sunlight_propagates = true,
169       walkable = false,
170       light_source = 13,
171       power = 9,
172       groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,connect_to_raillike=1},
173       drop = "redstone:torch",
174       selection_box = {
175             type = "wallmounted",
176             wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
177             wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
178             wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
179       },
180       on_construct = function(pos)
181             create_ps(pos)
182             redstone.update(pos)
183       end,
184       after_destruct = function(pos, oldnode)
185             redstone.update(pos,oldnode)
186       end,
187       on_destruct = function(pos)
188             delete_ps(pos)
189       end,
190       sounds = main.woodSound(),
191 })
192
193 minetest.register_craft({
194       output = "redstone:torch 4",
195       recipe = {
196             {"redstone:dust"},
197             {"main:stick"}
198       }
199 })
200
201
202
203
204
205
206
207
208
209
210
211
212
213 minetest.register_craftitem("redstone:blink_torch", {
214       description = "Redstone Blink Torch",
215       inventory_image = "redstone_torch.png",
216       wield_image = "redstone_torch.png",
217       wield_scale = {x = 1, y = 1, z = 1 + 1/16},
218       liquids_pointable = false,
219       power = 8,
220       on_place = function(itemstack, placer, pointed_thing)
221             if pointed_thing.type ~= "node" then
222                   return itemstack
223             end
224
225             local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
226
227             local fakestack = itemstack
228             local retval = false
229             if wdir < 1 then
230                   return itemstack
231             elseif wdir == 1 then
232                   retval = fakestack:set_name("redstone:blink_torch_floor_1")
233             else
234                   retval = fakestack:set_name("redstone:blink_torch_wall_1")
235             end
236             if not retval then
237                   return itemstack
238             end
239             itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
240             itemstack:set_name("redstone:blink_torch")
241
242             if retval then
243                   minetest.sound_play("wood", {pos=pointed_thing.above, gain = 1.0})
244             end
245
246             return itemstack
247       end
248 })
249 for i = 0,1 do
250       local coloring = 160*(1-i)
251       -- BLINK TORCH
252
253       minetest.register_node("redstone:blink_torch_floor_"..i, {
254             inventory_image = "redstone_torch.png",
255             wield_image = "redstone_torch.png",
256             wield_scale = {x = 1, y = 1, z = 1 + 2/16},
257             drawtype = "mesh",
258             mesh = "torch_floor.obj",
259             tiles = {"redstone_torch.png^[colorize:black:"..coloring},
260             paramtype = "light",
261             paramtype2 = "none",
262             power = 8*i,
263             sunlight_propagates = true,
264             drop = "redstone:torch",
265             walkable = false,
266             light_source = i*13,
267             groups = {choppy=2, dig_immediate=3, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,connect_to_raillike=1,blinker_torch = 1},
268             legacy_wallmounted = true,
269             selection_box = {
270                   type = "fixed",
271                   fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
272             },
273             on_construct = function(pos)
274                   redstone.update(pos)
275             end,
276             after_destruct = function(pos, oldnode)
277                   redstone.update(pos,oldnode)
278             end,
279             sounds = main.woodSound(),
280       })
281
282       minetest.register_node("redstone:blink_torch_wall_"..i, {
283             inventory_image = "redstone_torch.png",
284             wield_image = "redstone_torch.png",
285             wield_scale = {x = 1, y = 1, z = 1 + 1/16},
286             drawtype = "mesh",
287             mesh = "torch_wall.obj",
288             tiles = {"redstone_torch.png^[colorize:black:"..coloring},
289             paramtype = "light",
290             paramtype2 = "wallmounted",
291             sunlight_propagates = true,
292             walkable = false,
293             light_source = 13*i,
294             power = 8*i,
295             groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,connect_to_raillike=1,blinker_torch = 1},
296             drop = "redstone:torch",
297             selection_box = {
298                   type = "wallmounted",
299                   wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
300                   wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
301                   wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
302             },
303             on_construct = function(pos)
304                   redstone.update(pos)
305             end,
306             after_destruct = function(pos, oldnode)
307                   redstone.update(pos,oldnode)
308             end,
309             sounds = main.woodSound(),
310       })
311
312 end
313
314
315 minetest.register_abm{
316       label = "Torch Blink",
317         nodenames = {"group:blinker_torch"},
318         --neighbors = {"group:redstone"},
319         interval = 0.4,
320         chance = 1,
321         action = function(pos, node, active_object_count, active_object_count_wider)
322             --minetest.set_node(pos,{name=node.name:sub(1, -2)..0})
323                 --redstone.update(pos)
324             print("tests")
325             local inversion = math.abs(tonumber(node.name:sub(#node.name, #node.name))-1) --never do this
326             minetest.set_node(pos,{name=node.name:sub(1, #node.name-1)..inversion})
327             redstone.update(pos)
328         end,
329 }
330 minetest.register_craft({
331       output = "redstone:blink_torch 4",
332       recipe = {
333             {"redstone:dust"},
334             {"redstone:dust"},
335             {"main:stick"}
336       }
337 })
338