From: oilboi <47129783+oilboi@users.noreply.github.com> Date: Tue, 30 Jun 2020 12:03:30 +0000 (-0400) Subject: Add in advanced player detectors X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=05de43075e68e7e70dc5a3192937b64c88c8473d;p=Crafter.git Add in advanced player detectors --- diff --git a/mods/redstone/init.lua b/mods/redstone/init.lua index 78afbb4..6bf60f6 100644 --- a/mods/redstone/init.lua +++ b/mods/redstone/init.lua @@ -32,10 +32,20 @@ local vec_equals = vector.equals local activator_table = {} -- this holds the translation data of activator tables (activator functions) local capacitor_table = {} +local player_detection_table = {} -- redstone class redstone = {} +redstone.player_detector_add = function(pos) + player_detection_table[minetest.serialize(pos)] = pos +end + +redstone.player_detector_remove = function(pos) + player_detection_table[minetest.serialize(pos)] = nil +end + + -- enables mods to create data functions function redstone.register_activator(data) activator_table[data.name] = { @@ -64,7 +74,7 @@ dofile(path.."/piston.lua") dofile(path.."/craft.lua") --dofile(path.."/ore.lua") dofile(path.."/inverter.lua") ---dofile(path.."/player_detector.lua") +dofile(path.."/player_detector.lua") dofile(path.."/space_maker.lua") --dofile(path.."/pressure_plate.lua") dofile(path.."/capacitors.lua") @@ -520,7 +530,42 @@ function redstone.update(pos,is_capacitor) calculate(pos,is_capacitor) end + +local level +local pos2 +local power +local max +local function player_detector_calculation() + for _,pos in pairs(player_detection_table) do + level = pool[pos.x][pos.y][pos.z].torch + max = 0 + for _,player in ipairs(minetest.get_connected_players()) do + pos2 = player:get_pos() + power = floor(10-vector_distance(pos2,pos)) + if power > 9 then + power = 9 + elseif power < 0 then + power = 0 + end + + if power > max then + max = power + end + end + + if max ~= level then + swap_node(pos,{name="redstone:player_detector_"..max}) + redstone.inject(pos,{ + name = "redstone:player_detector_"..max, + torch = max, + }) + redstone.update(pos) + end + end +end + minetest.register_globalstep(function() + player_detector_calculation() recursion_check = {} end) diff --git a/mods/redstone/player_detector.lua b/mods/redstone/player_detector.lua index 1310c4a..d425e3f 100644 --- a/mods/redstone/player_detector.lua +++ b/mods/redstone/player_detector.lua @@ -1,33 +1,54 @@ -local minetest,ipairs,math = minetest,ipairs,math +local +minetest,ipairs,math += +minetest,ipairs,math + --detects players and outputs accordingly for i = 0,9 do + minetest.register_node("redstone:player_detector_"..i, { description = "Redstone Player Detector", - --inventory_image = "redstone_torch.png", - --wield_image = "redstone_torch.png", - --wield_scale = {x = 1, y = 1, z = 1 + 2/16}, drawtype = "normal", tiles = {"player_detector.png"}, paramtype = "light", paramtype2 = "none", - power = 9, - --sunlight_propagates = true, drop = "redstone:player_detector_0", - --walkable = false, - light_source = i, groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4, torch=1,redstone=1,redstone_torch=1,redstone_power=i, redstone_player_detection = 1}, legacy_wallmounted = true, on_construct = function(pos) - redstone.collect_info(pos) + redstone.inject(pos,{ + name = "redstone:player_detector_"..i, + torch = i, + }) + redstone.player_detector_add(pos) + redstone.update(pos) end, - after_destruct = function(pos, oldnode) - redstone.collect_info(pos) + on_destruct = function(pos, oldnode) + redstone.player_detector_remove(pos) + redstone.inject(pos,nil) + redstone.update(pos) end, sounds = main.stoneSound(), }) + +minetest.register_lbm({ + name = "redstone:player_detector_"..i, + nodenames = {"redstone:player_detector_"..i}, + run_at_every_load = true, + action = function(pos) + redstone.inject(pos,{ + name = "redstone:player_detector_"..i, + torch = i, + }) + redstone.player_detector_add(pos) + end, +}) + end +--[[ + minetest.register_abm{ label = "Redstone Player Detection", nodenames = {"group:redstone_player_detection"}, @@ -53,8 +74,9 @@ minetest.register_abm{ end end if found_player == false then - minetest.set_node(pos,{name="redstone:player_detector_0"}) + minetest.swap_node(pos,{name="redstone:player_detector_0"}) redstone.collect_info(pos) end end, } +]]-- \ No newline at end of file