]> git.lizzy.rs Git - Crafter.git/blob - mods/redstone/torch.lua
Entirely recode redstone (extreme efficiency)
[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 --[[
80 minetest.register_lbm({
81         name = "redstone:torch",
82         nodenames = {"redstone:torch_floor","redstone:torch_wall"},
83         run_at_every_load = true,
84         action = function(pos, node)
85                 create_ps(pos)
86         end,
87 })
88 ]]--
89 -- Item definitions
90 minetest.register_craftitem("redstone:torch", {
91         description = "Redstone Torch",
92         inventory_image = "redstone_torch.png",
93         wield_image = "redstone_torch.png",
94         wield_scale = {x = 1, y = 1, z = 1 + 1/16},
95         liquids_pointable = false,
96         power = 9,
97         on_place = function(itemstack, placer, pointed_thing)
98                 if pointed_thing.type ~= "node" then
99                         return itemstack
100                 end
101
102                 local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
103
104                 local fakestack = itemstack
105                 local retval = false
106                 if wdir < 1 then
107                         return itemstack
108                 elseif wdir == 1 then
109                         retval = fakestack:set_name("redstone:torch_floor")
110                 else
111                         retval = fakestack:set_name("redstone:torch_wall")
112                 end
113                 if not retval then
114                         return itemstack
115                 end
116                 itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
117                 itemstack:set_name("redstone:torch")
118
119                 if retval then
120                         minetest.sound_play("wood", {pos=pointed_thing.above, gain = 1.0})
121                 end
122
123                 return itemstack
124         end
125 })
126
127 minetest.register_node("redstone:torch_floor", {
128         inventory_image = "redstone_torch.png",
129         wield_image = "redstone_torch.png",
130         wield_scale = {x = 1, y = 1, z = 1 + 2/16},
131         drawtype = "mesh",
132         mesh = "torch_floor.obj",
133         tiles = {"redstone_torch.png"},
134         paramtype = "light",
135         paramtype2 = "none",
136         power = 9,
137         sunlight_propagates = true,
138         drop = "redstone:torch",
139         walkable = false,
140         light_source = 13,
141         groups = {choppy=2, dig_immediate=3, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,connect_to_raillike=1},
142         legacy_wallmounted = true,
143         selection_box = {
144                 type = "fixed",
145                 fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
146         },
147         
148         on_construct = function(pos)
149                 redstone.collect_info(pos)
150         end,
151         after_destruct = function(pos, oldnode)
152                 redstone.collect_info(pos)
153         end,
154         sounds = main.woodSound(),
155 })
156
157 minetest.register_node("redstone:torch_wall", {
158         inventory_image = "redstone_torch.png",
159         wield_image = "redstone_torch.png",
160         wield_scale = {x = 1, y = 1, z = 1 + 1/16},
161         drawtype = "mesh",
162         mesh = "torch_wall.obj",
163         tiles = {"redstone_torch.png"},
164         paramtype = "light",
165         paramtype2 = "wallmounted",
166         sunlight_propagates = true,
167         walkable = false,
168         light_source = 13,
169         power = 9,
170         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},
171         drop = "redstone:torch",
172         selection_box = {
173                 type = "wallmounted",
174                 wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
175                 wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
176                 wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
177         },
178         on_construct = function(pos)
179                 redstone.collect_info(pos)
180         end,
181         after_destruct = function(pos, oldnode)
182                 redstone.collect_info(pos)
183         end,
184         sounds = main.woodSound(),
185 })
186
187
188 minetest.register_craftitem("redstone:blink_torch", {
189         description = "Redstone Blink Torch",
190         inventory_image = "redstone_torch.png",
191         wield_image = "redstone_torch.png",
192         wield_scale = {x = 1, y = 1, z = 1 + 1/16},
193         liquids_pointable = false,
194         power = 8,
195         on_place = function(itemstack, placer, pointed_thing)
196                 if pointed_thing.type ~= "node" then
197                         return itemstack
198                 end
199
200                 local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
201
202                 local fakestack = itemstack
203                 local retval = false
204                 if wdir < 1 then
205                         return itemstack
206                 elseif wdir == 1 then
207                         retval = fakestack:set_name("redstone:blink_torch_floor_1")
208                 else
209                         retval = fakestack:set_name("redstone:blink_torch_wall_1")
210                 end
211                 if not retval then
212                         return itemstack
213                 end
214                 itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
215                 itemstack:set_name("redstone:blink_torch")
216
217                 if retval then
218                         minetest.sound_play("wood", {pos=pointed_thing.above, gain = 1.0})
219                 end
220
221                 return itemstack
222         end
223 })
224 for i = 0,1 do
225         local coloring = 160*(1-i)
226         -- BLINK TORCH
227
228         minetest.register_node("redstone:blink_torch_floor_"..i, {
229                 inventory_image = "redstone_torch.png",
230                 wield_image = "redstone_torch.png",
231                 wield_scale = {x = 1, y = 1, z = 1 + 2/16},
232                 drawtype = "mesh",
233                 mesh = "torch_floor.obj",
234                 tiles = {"redstone_torch.png^[colorize:black:"..coloring},
235                 paramtype = "light",
236                 paramtype2 = "none",
237                 power = 9*i,
238                 sunlight_propagates = true,
239                 drop = "redstone:torch",
240                 walkable = false,
241                 light_source = i*13,
242                 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},
243                 legacy_wallmounted = true,
244                 selection_box = {
245                         type = "fixed",
246                         fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
247                 },
248                 --there is no way for a player to get blink torch off
249                 --so shortcut is to add this to both states
250                 on_construct = function(pos)
251                         redstone.add(pos,true)
252                 end,
253                 after_destruct = function(pos, oldnode)
254                         redstone.remove(pos,9,true)
255                 end,
256                 sounds = main.woodSound(),
257         })
258
259         minetest.register_node("redstone:blink_torch_wall_"..i, {
260                 inventory_image = "redstone_torch.png",
261                 wield_image = "redstone_torch.png",
262                 wield_scale = {x = 1, y = 1, z = 1 + 1/16},
263                 drawtype = "mesh",
264                 mesh = "torch_wall.obj",
265                 tiles = {"redstone_torch.png^[colorize:black:"..coloring},
266                 paramtype = "light",
267                 paramtype2 = "wallmounted",
268                 sunlight_propagates = true,
269                 walkable = false,
270                 light_source = 13*i,
271                 power = 9*i,
272                 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},
273                 drop = "redstone:torch",
274                 selection_box = {
275                         type = "wallmounted",
276                         wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
277                         wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
278                         wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
279                 },
280                 on_construct = function(pos)
281                         redstone.add(pos,true)
282                 end,
283                 after_destruct = function(pos, oldnode)
284                         redstone.remove(pos,9,true)
285                 end,
286                 sounds = main.woodSound(),
287         })
288
289 end
290
291
292 minetest.register_abm{
293         label = "Torch Blink",
294         nodenames = {"group:blinker_torch"},
295         --neighbors = {"group:redstone"},
296         interval = 0.7,
297         chance = 1,
298         action = function(pos, node, active_object_count, active_object_count_wider)
299                 --minetest.set_node(pos,{name=node.name:sub(1, -2)..0})
300                 --redstone.update(pos)
301                 local inversion = math.abs(tonumber(node.name:sub(#node.name, #node.name))-1) --never do this
302                 minetest.set_node(pos,{name=node.name:sub(1, #node.name-1)..inversion})
303                 if inversion == 1 then
304                         redstone.add(pos,true)
305                 elseif inversion == 0 then
306                         redstone.remove(pos,9,true)
307                 end
308                 
309         end,
310 }
311 minetest.register_craft({
312         output = "redstone:blink_torch 4",
313         recipe = {
314                 {"redstone:dust"},
315                 {"redstone:dust"},
316                 {"main:stick"}
317         },
318 })
319
320 minetest.register_craft({
321         output = "redstone:torch 4",
322         recipe = {
323                 {"redstone:dust"},
324                 {"main:stick"}
325         }
326 })
327
328
329
330
331
332
333
334
335
336
337
338 --[[
339
340 minetest.register_craftitem("redstone:blink_torch", {
341         description = "Redstone Blink Torch",
342         inventory_image = "redstone_torch.png",
343         wield_image = "redstone_torch.png",
344         wield_scale = {x = 1, y = 1, z = 1 + 1/16},
345         liquids_pointable = false,
346         power = 8,
347         on_place = function(itemstack, placer, pointed_thing)
348                 if pointed_thing.type ~= "node" then
349                         return itemstack
350                 end
351
352                 local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
353
354                 local fakestack = itemstack
355                 local retval = false
356                 if wdir < 1 then
357                         return itemstack
358                 elseif wdir == 1 then
359                         retval = fakestack:set_name("redstone:blink_torch_floor_1")
360                 else
361                         retval = fakestack:set_name("redstone:blink_torch_wall_1")
362                 end
363                 if not retval then
364                         return itemstack
365                 end
366                 itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
367                 itemstack:set_name("redstone:blink_torch")
368
369                 if retval then
370                         minetest.sound_play("wood", {pos=pointed_thing.above, gain = 1.0})
371                 end
372
373                 return itemstack
374         end
375 })
376 for i = 0,1 do
377         local coloring = 160*(1-i)
378         -- BLINK TORCH
379
380         minetest.register_node("redstone:blink_torch_floor_"..i, {
381                 inventory_image = "redstone_torch.png",
382                 wield_image = "redstone_torch.png",
383                 wield_scale = {x = 1, y = 1, z = 1 + 2/16},
384                 drawtype = "mesh",
385                 mesh = "torch_floor.obj",
386                 tiles = {"redstone_torch.png^[colorize:black:"..coloring},
387                 paramtype = "light",
388                 paramtype2 = "none",
389                 power = 8*i,
390                 sunlight_propagates = true,
391                 drop = "redstone:torch",
392                 walkable = false,
393                 light_source = i*13,
394                 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},
395                 legacy_wallmounted = true,
396                 selection_box = {
397                         type = "fixed",
398                         fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
399                 },
400                 on_construct = function(pos)
401                         redstone.update(pos)
402                 end,
403                 after_destruct = function(pos, oldnode)
404                         redstone.update(pos,oldnode)
405                 end,
406                 sounds = main.woodSound(),
407         })
408
409         minetest.register_node("redstone:blink_torch_wall_"..i, {
410                 inventory_image = "redstone_torch.png",
411                 wield_image = "redstone_torch.png",
412                 wield_scale = {x = 1, y = 1, z = 1 + 1/16},
413                 drawtype = "mesh",
414                 mesh = "torch_wall.obj",
415                 tiles = {"redstone_torch.png^[colorize:black:"..coloring},
416                 paramtype = "light",
417                 paramtype2 = "wallmounted",
418                 sunlight_propagates = true,
419                 walkable = false,
420                 light_source = 13*i,
421                 power = 8*i,
422                 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},
423                 drop = "redstone:torch",
424                 selection_box = {
425                         type = "wallmounted",
426                         wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
427                         wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
428                         wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
429                 },
430                 on_construct = function(pos)
431                         redstone.update(pos)
432                 end,
433                 after_destruct = function(pos, oldnode)
434                         redstone.update(pos,oldnode)
435                 end,
436                 sounds = main.woodSound(),
437         })
438
439 end
440
441
442 minetest.register_abm{
443         label = "Torch Blink",
444         nodenames = {"group:blinker_torch"},
445         --neighbors = {"group:redstone"},
446         interval = 0.4,
447         chance = 1,
448         action = function(pos, node, active_object_count, active_object_count_wider)
449                 --minetest.set_node(pos,{name=node.name:sub(1, -2)..0})
450                 --redstone.update(pos)
451                 print("tests")
452                 local inversion = math.abs(tonumber(node.name:sub(#node.name, #node.name))-1) --never do this
453                 minetest.set_node(pos,{name=node.name:sub(1, #node.name-1)..inversion})
454                 redstone.update(pos)
455         end,
456 }
457 minetest.register_craft({
458         output = "redstone:blink_torch 4",
459         recipe = {
460                 {"redstone:dust"},
461                 {"redstone:dust"},
462                 {"main:stick"}
463         }
464 })
465 ]]--
466