Improve api, respect entity on_blast callbacks

This commit is contained in:
stujones11 2019-03-23 19:17:18 +00:00
parent b5984a231f
commit 0eb5049218
2 changed files with 48 additions and 29 deletions

View file

@ -10,11 +10,11 @@ Crafting
### Gunpowder ### Gunpowder
1 x Coal Lump + 1 x Clay Lump = 5 x Gunpowder (shapeless) 1 x Coal Lump + 1 x Clay Lump = 5 x Gunpowder
``` ```
output = "shooter:gunpowder 5", output = "shooter:gunpowder 5",
type = "shapeless", type = "shapeless",
recipe = {"default:coal_lump", "default:clay_lump"}, recipe = {"default:coal_lump", "default:clay_lump"},
``` ```
Configuration Configuration
------------- -------------
@ -119,8 +119,14 @@ API Documentation
* `shooter.spawn_particles(pos, particles)`: Adds particles at the specified position * `shooter.spawn_particles(pos, particles)`: Adds particles at the specified position
* `particles` is an optional table of overrides for `shooter.default_particles` * `particles` is an optional table of overrides for `shooter.default_particles`
* `shooter.play_node_sound(node, pos)`: Plays the registered 'dug' sound for the node at `pos` * `shooter.play_node_sound(node, pos)`: Plays the registered 'dug' sound for the node at `pos`
* `shooter.punch_node(pos, spec)`: Punches the node at `pos` with the `spec` group capabilities
* `shooter.is_valid_object(object)`: Returns `true` if the object can be damaged * `shooter.is_valid_object(object)`: Returns `true` if the object can be damaged
* `shooter.punch_node(pos, spec)`: Punches the node at `pos` with the `spec` group capabilities
* `shooter.punch_object(object, tool_caps, dir, on_blast)`: Punches the object
* Returns `true` if the object is damaged
* `object`: Object to punched, not checked for validity!
* `tool_caps`: Tool capabilities of the weapon uesd -- See "Weapon Definition"
* `dir`: Unit vector pointing from the source of the punch to the object
* `on_blast`: Respect entity `on_blast` damage callback
* `shooter.fire_weapon(player, itemstack, spec)`: Adds a 'round' with `spec` to the processing que * `shooter.fire_weapon(player, itemstack, spec)`: Adds a 'round' with `spec` to the processing que
* `shooter.blast(pos, radius, fleshy, distance, user)`: Create explosion at `pos` * `shooter.blast(pos, radius, fleshy, distance, user)`: Create explosion at `pos`
* `radius`: Blast radius in nodes * `radius`: Blast radius in nodes
@ -156,7 +162,7 @@ Used by `shooter.register_weapon`
-- `name`: Name of the weapon item, eg. `shooter_guns:rifle` -- `name`: Name of the weapon item, eg. `shooter_guns:rifle`
-- `user`: Name of the player that fired the weapon -- `user`: Name of the player that fired the weapon
-- `origin`: Initial starting position of the shot -- `origin`: Initial starting position of the shot
-- `dir`: Direction of the virtual shot before impact -- `dir`: Unit vector direction of the virtual shot before impact
end, end,
spec = { spec = {
-- Weapon specifications -- Weapon specifications

View file

@ -160,6 +160,20 @@ shooter.play_node_sound = function(node, pos)
end end
end end
shooter.is_valid_object = function(object)
if object then
if object:is_player() == true then
return config.allow_players
end
if config.allow_entities == true then
local luaentity = object:get_luaentity()
if luaentity then
return luaentity.name ~= "__builtin:item"
end
end
end
end
shooter.punch_node = function(pos, spec) shooter.punch_node = function(pos, spec)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if not node then if not node then
@ -185,24 +199,25 @@ shooter.punch_node = function(pos, spec)
shooter.spawn_particles(pos, {texture=item.tiles[1]}) shooter.spawn_particles(pos, {texture=item.tiles[1]})
end end
end end
break return true
end end
end end
end end
end end
shooter.is_valid_object = function(object) shooter.punch_object = function(object, tool_caps, dir, on_blast)
if object then local do_damage = true
if object:is_player() == true then if on_blast then
return config.allow_players local ent = object:get_luaentity()
end local def = minetest.registered_entities[ent.name]
if config.allow_entities == true then if def.on_blast then
local luaentity = object:get_luaentity() do_damage = def.on_blast(ent, tool_caps.fleshy)
if luaentity then
return luaentity.name ~= "__builtin:item"
end
end end
end end
if do_damage then
object:punch(object, nil, tool_caps, dir)
return true
end
end end
local function process_hit(pointed_thing, spec, dir) local function process_hit(pointed_thing, spec, dir)
@ -217,15 +232,12 @@ local function process_hit(pointed_thing, spec, dir)
shooter.punch_node(pos, spec) shooter.punch_node(pos, spec)
elseif pointed_thing.type == "object" then elseif pointed_thing.type == "object" then
local object = pointed_thing.ref local object = pointed_thing.ref
if shooter.is_valid_object(object) == true then if shooter.is_valid_object(object) and
local player = minetest.get_player_by_name(spec.user) shooter.punch_object(object, spec.tool_caps, dir) then
if player then local pos = pointed_thing.intersection_point or object:get_pos()
object:punch(player, nil, spec.tool_caps, dir) local groups = object:get_armor_groups() or {}
local pos = pointed_thing.intersection_point or object:get_pos() if groups.fleshy then
local groups = object:get_armor_groups() or {} shooter.spawn_particles(pos, spec.particles)
if groups.fleshy then
shooter.spawn_particles(pos, spec.particles)
end
end end
end end
end end
@ -364,11 +376,12 @@ shooter.blast = function(pos, radius, fleshy, distance, user)
if dist ~= 0 then if dist ~= 0 then
obj_pos.y = obj_pos.y + 1 obj_pos.y = obj_pos.y + 1
local blast_pos = {x=pos.x, y=pos.y + 4, z=pos.z} local blast_pos = {x=pos.x, y=pos.y + 4, z=pos.z}
if minetest.line_of_sight(obj_pos, blast_pos, 1) then if shooter.is_valid_object(obj) and
obj:punch(user, 1.0, { minetest.line_of_sight(obj_pos, blast_pos, 1) then
shooter.punch_object(obj, {
full_punch_interval = 1.0, full_punch_interval = 1.0,
damage_groups = {fleshy=damage}, damage_groups = {fleshy=damage},
}) }, nil, true)
end end
end end
end end