diff --git a/laser_mk2.lua b/laser_mk2.lua index 35dda24..6e5a57a 100644 --- a/laser_mk2.lua +++ b/laser_mk2.lua @@ -1,9 +1,14 @@ --license LGPLv2+, for the changes: WTFPL local range = 10 +local dirs = 10 +local side_ran = 3 +local say_laser_info = false -local function round_pos(pos) - return {x=math.floor(pos.x+0.5), y=math.floor(pos.y+0.5), z=math.floor(pos.z+0.5)} +local function info(info) + if say_laser_info then + print("[extrablocks] "..info) + end end local function chps(ran, m, n) @@ -13,8 +18,10 @@ local function chps(ran, m, n) return false end -local function round(z, a) - return math.floor(z*a+0.5)/a +local function round_pos(pos, a) + local output = {x=math.floor(pos.x*a+0.5)/a, y=math.floor(pos.y*a+0.5)/a, z=math.floor(pos.z*a+0.5)/a} + info(dump(output).." round "..dump(a)) + return output end local function get_straight(dir, range) @@ -58,7 +65,34 @@ local function get_qdrt(dir) else z1,z2 = range,0 end - return {x1,x2, y1,y2, z1,z2} + local output = {x1,x2, y1,y2, z1,z2} + info(dump(output).." got_qdrt") + return output +end + +local function make_d_table(dir) + return { + xdz = dir.x/dir.z, + zdx = dir.z/dir.x, + xdy = dir.x/dir.y, + ydx = dir.y/dir.x, + zdy = dir.z/dir.y, + ydz = dir.y/dir.z, + } +end + +local function allowed_pos(i, j, k, d) + local ran = math.hypot(math.hypot(i,j),k)/side_ran + if (chps(ran, i/k, d.xdz) or chps(ran, k/i, d.zdx)) + and (chps(ran, i/j, d.xdy) or chps(ran, j/i, d.ydx)) + and (chps(ran, k/j, d.zdy) or chps(ran, j/k, d.ydz)) then + return true + end + return false +end + +local function def_pos(startpos, i, j, k) + return {x=startpos.x+i, y=startpos.y+j, z=startpos.z+k} end local laser_shoot = function(itemstack, player, pointed_thing) @@ -69,6 +103,7 @@ local laser_shoot = function(itemstack, player, pointed_thing) local node = minetest.get_node(pos) if node.name~="ignore" then minetest.node_dig(pos,node,player) + info(dump(pos).." dug (pointed)") end end @@ -77,40 +112,32 @@ local laser_shoot = function(itemstack, player, pointed_thing) minetest.add_particle(startpos, dir, velocity, 1, 1, false, "extrablocks_laser_beam_mk2.png") minetest.sound_play("extrablocks_laser_mk2", {pos = playerpos, gain = 1.0, max_hear_distance = range}) - dir = {x=round(dir.x, 10), y=round(dir.y, 10), z=round(dir.z, 10)} - playerpos = round_pos(playerpos) + dir = round_pos(dir, dirs) + playerpos = vector.round(playerpos) local straight = get_straight(dir, range) if straight then + info("straight") for i = -straight[1],straight[2],1 do for j = -straight[3],straight[4],1 do for k = -straight[5],straight[6],1 do - local pos = {x=startpos.x+i, y=startpos.y+j, z=startpos.z+k} - lazer_it(pos, player) + lazer_it(def_pos(startpos, i, j, k), player) end end end return true end + info("not straight") local sizes = get_qdrt(dir, range) - local xdz = dir.x/dir.z - local zdx = dir.z/dir.x - local xdy = dir.x/dir.y - local ydx = dir.y/dir.x - local zdy = dir.z/dir.y - local ydz = dir.y/dir.z + local d = make_d_table(dir) for i = -sizes[1],sizes[2],1 do for j = -sizes[3],sizes[4],1 do for k = -sizes[5],sizes[6],1 do - local pos = {x=startpos.x+i, y=startpos.y+j, z=startpos.z+k} - local ran = math.hypot(math.hypot(i,j),k)/3 - if (chps(ran, i/k, xdz) or chps(ran, k/i, zdx)) - and (chps(ran, i/j, xdy) or chps(ran, j/i, ydx)) - and (chps(ran, k/j, zdy) or chps(ran, j/k, ydz)) then - lazer_it(pos, player) + if allowed_pos(i, j, k, d) then + lazer_it(def_pos(startpos, i, j, k), player) end end end @@ -144,6 +171,7 @@ function lazer_it (pos, player) if node.name == "default:water_source" or node.name == "default:water_flowing" then minetest.remove_node(pos) + info(dump(pos).." removed") return end if player then