Add better TNT engine with blast resistance

master
minermoder27 2013-12-20 05:49:51 +11:00
parent 6e9c22c3b6
commit 31850502fc
1 changed files with 169 additions and 80 deletions

View File

@ -9,55 +9,89 @@ local tnt_tables = {["bettertnt:tnt1"] = {r=1},
["bettertnt:tnt9"] = {r=16}, ["bettertnt:tnt9"] = {r=16},
["bettertnt:tnt10"] = {r=18},} ["bettertnt:tnt10"] = {r=18},}
tnt = {}
tnt.force = {
["default:brick"] = 4,
["default:cobble"] = 4,
["default:stonebrick"] = 4,
["default:desert_stonebrick"] = 4,
["default:gravel"] = 1,
["default:sand"] = 1,
["default:desert_sand"] = 1,
["default:clay"] = 1,
["default:dirt"] = 1,
["default:dirt_with_grass"] = 1,
["default:dirt_with_grass_footsteps"] = 1,
["default:dirt_with_snow"] = 1,
["default:wood"] = 2,
["default:tree"] = 3,
["default:stone"] = 3,
["default:sandstone"] = 3,
["default:sandstonebrick"] = 3,
["default:desert_stone"] = 3,
["default:stone_with_coal"] = 4,
["default:stone_with_iron"] = 4,
["default:stone_with_copper"] = 4,
["default:stone_with_gold"] = 4,
["default:mese_in_stone"] = 6,
["default:mese"] = 10,
["default:diamonds_in_stone"] = 6,
["default:torch"] = 6,
["default:steelblock"] = 30,
["default:obsidian"] = 30,
}
tnt.accl = {
["default:steelblock"] = true,
["default:obsidian"] = true,
}
local function drop_item(pos, nodename, player)
local function drop_item(pos, nodename, player, count)
local drop = minetest.get_node_drops(nodename) local drop = minetest.get_node_drops(nodename)
for _,item in ipairs(drop) do for _,item in ipairs(drop) do
if type(item) == "string" then if type(item) == "string" then
local obj = minetest.env:add_item(pos, item) item = ItemStack(item)
if obj == nil then end
return for i=1,item:get_count() do
end item:set_count(item:get_count() * count)
obj:get_luaentity().collect = true local obj = minetest.add_item(pos, item)
obj:setacceleration({x=0, y=-10, z=0}) if obj == nil then
obj:setvelocity({x=math.random(0,6)-3, y=10, z=math.random(0,6)-3}) return
else
for i=1,item:get_count() do
local obj = minetest.env:add_item(pos, item:get_name())
if obj == nil then
return
end
obj:get_luaentity().collect = true
obj:setacceleration({x=0, y=-10, z=0})
obj:setvelocity({x=math.random(0,6)-3, y=10, z=math.random(0,6)-3})
end
end end
end obj:get_luaentity().collect = true
obj:setacceleration({x=0, y=-10, z=0})
obj:setvelocity({x=math.random(0,6)-3, y=10, z=math.random(0,6)-3})
end
end
end end
local function is_tnt(id) local function is_tnt(name)
for i=1, #tnt_c_tnt do if tnt_tables[name]~=nil then return true end
if tnt_c_tnt[i]==id then
return true
end
end
return false return false
end end
local function destroy(pos, player) local function destroy(pos, player, ents)
local nodename = minetest.get_node(pos).name local nodename = minetest.get_node(pos).name
local p_pos = area:index(pos.x, pos.y, pos.z) --local p_pos = area:index(pos.x, pos.y, pos.z)
if nodes[p_pos] ~= tnt_c_air then --if nodes[p_pos] ~= tnt_c_air then
if minetest.registered_nodes[nodename].groups.flammable ~= nil then if nodename~="air" then
nodes[p_pos] = tnt_c_fire if tnt_tables[nodename]==nil then
return ents[nodename] = (ents[nodename] or 0) + 1
end end
nodes[p_pos] = tnt_c_air if minetest.registered_nodes[nodename].groups.flammable ~= nil then
end minetest.set_node(pos, {name = "fire:basic_flame"})
if tnt_tables[nodename]==nil then return
drop_item(pos, nodename, player) end
end minetest.remove_node(pos)
end
end end
local function combine_texture(texture_size, frame_count, texture, ani_texture) local function combine_texture(texture_size, frame_count, texture, ani_texture)
@ -90,6 +124,11 @@ for name,data in pairs(tnt_tables) do
on_punch = function(pos, node, puncher) on_punch = function(pos, node, puncher)
if puncher:get_wielded_item():get_name() == "default:torch" then if puncher:get_wielded_item():get_name() == "default:torch" then
if minetest.is_protected(pos, puncher:get_player_name()) then
print(puncher:get_player_name() .. " tried to light TNT at " .. minetest.pos_to_string(pos))
minetest.record_protection_violation(pos, puncher:get_player_name())
return
end
minetest.sound_play("bettertnt_ignite", {pos=pos}) minetest.sound_play("bettertnt_ignite", {pos=pos})
boom(pos, 4, puncher) boom(pos, 4, puncher)
minetest.set_node(pos, {name=name.."_burning"}) minetest.set_node(pos, {name=name.."_burning"})
@ -148,33 +187,33 @@ end
function boom_id(pos, time, player, id) function boom_id(pos, time, player, id)
minetest.after(time, function(pos) minetest.after(time, function(pos)
local tnt_range = tnt_tables[id].r local tnt_range = tnt_tables[id].r * 6
local t1 = os.clock() local t1 = os.clock()
pr = get_tnt_random(pos) pr = get_tnt_random(pos)
minetest.sound_play("bettertnt_explode", {pos=pos, gain=1.5, max_hear_distance=tnt_range*64}) minetest.sound_play("bettertnt_explode", {pos=pos, gain=1.5, max_hear_distance=tnt_range*64})
--minetest.remove_node(pos) minetest.remove_node(pos)
local manip = minetest.get_voxel_manip() -- local manip = minetest.get_voxel_manip()
local width = tnt_range -- local width = tnt_range
local emerged_pos1, emerged_pos2 = manip:read_from_map({x=pos.x-width, y=pos.y-width, z=pos.z-width}, -- local emerged_pos1, emerged_pos2 = manip:read_from_map({x=pos.x-width, y=pos.y-width, z=pos.z-width},
{x=pos.x+width, y=pos.y+width, z=pos.z+width}) -- {x=pos.x+width, y=pos.y+width, z=pos.z+width})
area = VoxelArea:new{MinEdge=emerged_pos1, MaxEdge=emerged_pos2} -- area = VoxelArea:new{MinEdge=emerged_pos1, MaxEdge=emerged_pos2}
nodes = manip:get_data() -- nodes = manip:get_data()
--
local p_pos = area:index(pos.x, pos.y, pos.z) -- local p_pos = area:index(pos.x, pos.y, pos.z)
nodes[p_pos] = tnt_c_air -- nodes[p_pos] = tnt_c_air
minetest.add_particle(pos, {x=0,y=0,z=0}, {x=0,y=0,z=0}, 0.5, 16, false, "bettertnt_boom.png") minetest.add_particle(pos, {x=0,y=0,z=0}, {x=0,y=0,z=0}, 0.5, 16, false, "bettertnt_boom.png")
--minetest.set_node(pos, {name="tnt:boom"}) --minetest.set_node(pos, {name="tnt:boom"})
local objects = minetest.get_objects_inside_radius(pos, 7) local objects = minetest.get_objects_inside_radius(pos, tnt_range/2)
for _,obj in ipairs(objects) do for _,obj in ipairs(objects) do
if obj:is_player() or (obj:get_luaentity() and obj:get_luaentity().name ~= "__builtin:item") then if obj:is_player() or (obj:get_luaentity() and obj:get_luaentity().name ~= "__builtin:item") then
local obj_p = obj:getpos() local obj_p = obj:getpos()
local vec = {x=obj_p.x-pos.x, y=obj_p.y-pos.y, z=obj_p.z-pos.z} local vec = {x=obj_p.x-pos.x, y=obj_p.y-pos.y, z=obj_p.z-pos.z}
local dist = (vec.x^2+vec.y^2+vec.z^2)^0.5 local dist = (vec.x^2+vec.y^2+vec.z^2)^0.5
local damage = (80*0.5^dist)*2 local damage = (80*0.5^(tnt_range - dist))/2
obj:punch(obj, 1.0, { obj:punch(obj, 1.0, {
full_punch_interval=1.0, full_punch_interval=1.0,
damage_groups={fleshy=damage}, damage_groups={fleshy=damage},
@ -182,39 +221,94 @@ function boom_id(pos, time, player, id)
end end
end end
local ents = {}
local storedPoses = {}
for dx=-tnt_range,tnt_range do for dx=-tnt_range,tnt_range do
for dz=-tnt_range,tnt_range do for dz=-tnt_range,tnt_range do
for dy=tnt_range,-tnt_range,-1 do for dy=-tnt_range,tnt_range do
local p = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz} --local p = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz}
----------------------------------------
local p_node = area:index(p.x, p.y, p.z) local dist = (dx^2) + (dy^2) + (dz^2)
local d_p_node = nodes[p_node] dist = dist^(1/2.0)
local node = minetest.get_node(p) if dist < tnt_range and dist + 1 >= tnt_range and dist~=0 then
-- if d_p_node == tnt_c_tnt local dir = {x=dx, y=dy, z=dz}
-- or d_p_node == tnt_c_tnt_burning then --local totalnum = math.abs(dir.x)+math.abs(dir.y)+math.abs(dir.z)
if is_tnt(d_p_node)==true then --dir = vector.normalize(dir)--vector.divide(dir, vector.new(totalnum, totalnum, totalnum))
--nodes[p_node] = tnt_c_tnt dir.x = dir.x / dist
boom({x=p.x, y=p.y, z=p.z}, 0, player) dir.y = dir.y / dist
elseif not ( d_p_node == tnt_c_fire dir.z = dir.z / dist
or string.find(node.name, "default:water_") --local p = {x=pos.x, y=pos.y, z=pos.z} -- {x=0,y=0,z=0}--
or string.find(node.name, "default:lava_")) then local blast = tnt_range / 3
if math.abs(dx)<tnt_range and math.abs(dy)<tnt_range and math.abs(dz)<tnt_range then for i=1, dist do
destroy(p, player) -- i = i - 0.5
elseif pr:next(1,5) <= 4 then local pp = {x=dir.x*i, y=dir.y*i, z=dir.z*i}
destroy(p, player) local p = vector.add(pp, pos)
p.x = math.floor(p.x)
p.y = math.floor(p.y)
p.z = math.floor(p.z)
for i=1, #storedPoses do
if p.x==storedPoses[i].x and p.y==storedPoses[i].y and p.z==storedPoses[i].z then
--print("p: "..dump(p) .. " storedPoses: "..dump(storedPoses[i]))
p = nil
break
end
end
if p==nil then break end
--local p = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz}
--vector.add(p, dir)
----------------------------------------
-- local p_node = area:index(p.x, p.y, p.z)
-- local d_p_node = nodes[p_node]
local node = minetest.get_node(p)
-------------------------------------------------------------
blast = blast - (tnt.force[node.name] or 3)
if tnt.accl[node.name]==true then
storedPoses[#storedPoses + 1] = {x=p.x, y=p.y, z=p.z}
local stored = minetest.get_meta(p):get_int("blast") or 0
blast = blast + stored
end
if blast <= 0 then
if tnt.accl[node.name]==true then
minetest.get_meta(p):set_int("blast", tnt.force[node.name] + blast)
end
break
end
-------------------------------------------------------------
-- if d_p_node == tnt_c_tnt
-- or d_p_node == tnt_c_tnt_burning then
if is_tnt(node.name)==true then
--nodes[p_node] = tnt_c_tnt
minetest.remove_node(p)
boom_id(p, 0.5, player, node.name) -- was {x=p.x, y=p.y, z=p.z}
elseif not ( d_p_node == tnt_c_fire
or string.find(node.name, "default:water_")
or string.find(node.name, "default:lava_")) then
--if math.abs(dx)<tnt_range and math.abs(dy)<tnt_range and math.abs(dz)<tnt_range then
destroy(p, player, ents)
--elseif pr:next(1,5) <= 4 then
-- destroy(p, player, ents)
--end
end
end end
--------------------------------------------
end end
--------------------------------------------
end end
end end
end end
for name, val in pairs(ents) do
drop_item(pos, name, player, val)
end
minetest.add_particlespawner( minetest.add_particlespawner(
3000, --amount tnt_range * 100, --amount
4, --time 1, --time
{x=pos.x-3, y=pos.y-3, z=pos.z-3}, --minpos {x=pos.x-(tnt_range / 2), y=pos.y-(tnt_range / 2), z=pos.z-(tnt_range / 2)}, --minpos
{x=pos.x+3, y=pos.y+3, z=pos.z+3}, --maxpos {x=pos.x+(tnt_range / 2), y=pos.y+(tnt_range / 2), z=pos.z+(tnt_range / 2)}, --maxpos
{x=-0, y=-0, z=-0}, --minvel {x=-0, y=-0, z=-0}, --minvel
{x=0, y=0, z=0}, --maxvel {x=0, y=0, z=0}, --maxvel
{x=-0.5,y=5,z=-0.5}, --minacc {x=-0.5,y=5,z=-0.5}, --minacc
@ -226,12 +320,7 @@ function boom_id(pos, time, player, id)
true, --collisiondetection true, --collisiondetection
"bettertnt_smoke.png" --texture "bettertnt_smoke.png" --texture
) )
manip:set_data(nodes)
manip:write_to_map()
print(string.format("[tnt] exploded in: %.2fs", os.clock() - t1)) print(string.format("[tnt] exploded in: %.2fs", os.clock() - t1))
local t1 = os.clock()
manip:update_map()
print(string.format("[tnt] map updated after: %.2fs", os.clock() - t1))
end, pos) end, pos)
end end