Replace getpos() with get_pos()
[MineClone/MineClone2.git] / mods / ITEMS / REDSTONE / mesecons_pressureplates / init.lua
blob395c5130b1f400a8a39fbbf73923afb0dea68938
1 local PRESSURE_PLATE_INTERVAL = 0.04
3 local pp_box_off = {
4 type = "fixed",
5 fixed = { -7/16, -8/16, -7/16, 7/16, -7/16, 7/16 },
8 local pp_box_on = {
9 type = "fixed",
10 fixed = { -7/16, -8/16, -7/16, 7/16, -7.5/16, 7/16 },
13 local function pp_on_timer(pos, elapsed)
14 local node = minetest.get_node(pos)
15 local basename = minetest.registered_nodes[node.name].pressureplate_basename
16 local activated_by = minetest.registered_nodes[node.name].pressureplate_activated_by
18 -- This is a workaround for a strange bug that occurs when the server is started
19 -- For some reason the first time on_timer is called, the pos is wrong
20 if not basename then return end
22 if activated_by == nil then
23 activated_by = { any = true }
24 end
26 local obj_does_activate = function(obj, activated_by)
27 if activated_by.any then
28 return true
29 elseif activated_by.mob and obj:get_luaentity() and obj:get_luaentity()._cmi_is_mob == true then
30 return true
31 elseif activated_by.player and obj:is_player() then
32 return true
33 else
34 return false
35 end
36 end
38 local objs = minetest.get_objects_inside_radius(pos, 1)
40 if node.name == basename .. "_on" then
41 local disable
42 if #objs == 0 then
43 disable = true
44 elseif not activated_by.any then
45 disable = true
46 for k, obj in pairs(objs) do
47 if obj_does_activate(obj, activated_by) then
48 disable = false
49 break
50 end
51 end
52 end
53 if disable then
54 minetest.set_node(pos, {name = basename .. "_off"})
55 mesecon.receptor_off(pos, mesecon.rules.pplate)
56 end
57 elseif node.name == basename .. "_off" then
58 for k, obj in pairs(objs) do
59 local objpos = obj:get_pos()
60 if obj_does_activate(obj, activated_by) then
61 if objpos.y > pos.y-1 and objpos.y < pos.y then
62 minetest.set_node(pos, {name = basename .. "_on"})
63 mesecon.receptor_on(pos, mesecon.rules.pplate)
64 break
65 end
66 end
67 end
68 end
69 return true
70 end
72 -- Register a Pressure Plate
73 -- basename: base name of the pressure plate
74 -- description: description displayed in the player's inventory
75 -- textures_off:textures of the pressure plate when inactive
76 -- textures_on: textures of the pressure plate when active
77 -- image_w: wield image of the pressure plate
78 -- image_i: inventory image of the pressure plate
79 -- recipe: crafting recipe of the pressure plate
80 -- sounds: sound table (like in minetest.register_node)
81 -- plusgroups: group memberships (attached_node=1 and not_in_creative_inventory=1 are already used)
82 -- activated_by: optinal table with elements denoting by which entities this pressure plate is triggered
83 -- Possible table fields:
84 -- * player=true: Player
85 -- * mob=true: Mob
86 -- By default, is triggered by all entities
87 -- longdesc: Customized long description for the in-game help (if omitted, a dummy text is used)
89 function mesecon.register_pressure_plate(basename, description, textures_off, textures_on, image_w, image_i, recipe, sounds, plusgroups, activated_by, longdesc)
90 local groups_off = table.copy(plusgroups)
91 groups_off.attached_node = 1
92 groups_off.dig_by_piston = 1
93 groups_off.pressure_plate = 1
94 local groups_on = table.copy(groups_off)
95 groups_on.not_in_creative_inventory = 1
96 groups_on.pressure_plate = 2
97 if not longdesc then
98 longdesc = "A pressure plate is a redstone component which supplies its surrounding blocks with redstone power while someone or something rests on top of it."
99 end
101 mesecon.register_node(basename, {
102 drawtype = "nodebox",
103 inventory_image = image_i,
104 wield_image = image_w,
105 paramtype = "light",
106 walkable = false,
107 description = description,
108 on_timer = pp_on_timer,
109 on_construct = function(pos)
110 minetest.get_node_timer(pos):start(PRESSURE_PLATE_INTERVAL)
111 end,
112 sounds = sounds,
113 is_ground_content = false,
114 pressureplate_basename = basename,
115 pressureplate_activated_by = activated_by,
116 _mcl_blast_resistance = 2.5,
117 _mcl_hardness = 0.5,
119 node_box = pp_box_off,
120 selection_box = pp_box_off,
121 groups = groups_off,
122 tiles = textures_off,
124 mesecons = {receptor = { state = mesecon.state.off, rules = mesecon.rules.pplate }},
125 _doc_items_longdesc = longdesc,
127 node_box = pp_box_on,
128 selection_box = pp_box_on,
129 groups = groups_on,
130 tiles = textures_on,
132 mesecons = {receptor = { state = mesecon.state.on, rules = mesecon.rules.pplate }},
133 _doc_items_create_entry = false,
136 minetest.register_craft({
137 output = basename .. "_off",
138 recipe = recipe,
141 if minetest.get_modpath("doc") then
142 doc.add_entry_alias("nodes", basename .. "_off", "nodes", basename .. "_on")
146 local woods = {
147 { "wood", "mcl_core:wood", "default_wood.png", "Oak Pressure Plate" },
148 { "acaciawood", "mcl_core:acaciawood", "default_acacia_wood.png", "Acacia Pressure Plate" },
149 { "birchwood", "mcl_core:birchwood", "mcl_core_planks_birch.png", "Birch Pressure Plate" },
150 { "darkwood", "mcl_core:darkwood", "mcl_core_planks_big_oak.png", "Dark Oak Pressure Plate" },
151 { "sprucewood", "mcl_core:sprucewood", "mcl_core_planks_spruce.png", "Spruce Pressure Plate" },
152 { "junglewood", "mcl_core:junglewood", "default_junglewood.png", "Jungle Pressure Plate" },
155 for w=1, #woods do
156 mesecon.register_pressure_plate(
157 "mesecons_pressureplates:pressure_plate_"..woods[w][1],
158 woods[w][4],
159 {woods[w][3]},
160 {woods[w][3]},
161 woods[w][3],
162 nil,
163 {{woods[w][2], woods[w][2]}},
164 mcl_sounds.node_sound_wood_defaults(),
165 {axey=1, material_wood=1},
166 nil,
167 "A wooden pressure plate is a redstone component which supplies its surrounding blocks with redstone power while any movable object (including dropped items, players and mobs) rests on top of it.")
169 minetest.register_craft({
170 type = "fuel",
171 recipe = "mesecons_pressureplates:pressure_plate_"..woods[w][1].."_off",
172 burntime = 15
177 mesecon.register_pressure_plate(
178 "mesecons_pressureplates:pressure_plate_stone",
179 "Stone Pressure Plate",
180 {"default_stone.png"},
181 {"default_stone.png"},
182 "default_stone.png",
183 nil,
184 {{"mcl_core:stone", "mcl_core:stone"}},
185 mcl_sounds.node_sound_stone_defaults(),
186 {pickaxey=1, material_stone=1},
187 { player = true, mob = true },
188 "A stone pressure plate is a redstone component which supplies its surrounding blocks with redstone power while a player or mob stands on top of it. It is not triggered by anything else.")