2019-03-17 18:13:26 +00:00
|
|
|
--[[
|
|
|
|
Shooter Turret Gun [shooter_turret]
|
|
|
|
Copyright (C) 2013-2019 stujones11, Stuart Jones
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
]]--
|
|
|
|
|
2019-03-15 17:55:37 +00:00
|
|
|
local use_player_api = minetest.get_modpath("player_api")
|
|
|
|
|
2014-07-26 20:18:10 +00:00
|
|
|
local function get_turret_entity(pos)
|
|
|
|
local entity = nil
|
|
|
|
local objects = minetest.get_objects_inside_radius(pos, 1)
|
|
|
|
for _, obj in ipairs(objects) do
|
|
|
|
local ent = obj:get_luaentity()
|
|
|
|
if ent then
|
2017-07-13 12:33:00 +00:00
|
|
|
if ent.name == "shooter_turret:turret_entity" then
|
2014-07-26 20:18:10 +00:00
|
|
|
-- Remove duplicates
|
|
|
|
if entity then
|
|
|
|
obj:remove()
|
|
|
|
else
|
|
|
|
entity = ent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return entity
|
|
|
|
end
|
|
|
|
|
2017-07-13 12:33:00 +00:00
|
|
|
minetest.register_entity("shooter_turret:turret_entity", {
|
2014-07-26 20:18:10 +00:00
|
|
|
physical = true,
|
|
|
|
visual = "mesh",
|
|
|
|
mesh = "shooter_turret.b3d",
|
|
|
|
visual_size = {x=1, y=1},
|
|
|
|
collisionbox = {-0.3, 0.5,-0.3, 0.3,1,0.3},
|
|
|
|
textures = {
|
|
|
|
"shooter_turret_uv.png",
|
|
|
|
},
|
|
|
|
timer = 0,
|
2019-03-15 17:55:37 +00:00
|
|
|
user = nil,
|
2014-07-26 20:18:10 +00:00
|
|
|
pitch = 40,
|
|
|
|
yaw = 0,
|
|
|
|
firing = false,
|
2019-03-19 19:23:23 +00:00
|
|
|
on_activate = function(self)
|
2019-03-15 17:55:37 +00:00
|
|
|
self.pos = self.object:get_pos()
|
|
|
|
self.yaw = self.object:get_yaw()
|
2017-07-13 12:33:00 +00:00
|
|
|
if minetest.get_node(self.pos).name ~= "shooter_turret:turret" then
|
2014-07-26 20:18:10 +00:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.object:set_animation({x=self.pitch, y=self.pitch}, 0)
|
2019-03-19 19:23:23 +00:00
|
|
|
self.object:set_armor_groups({immortal=1})
|
2014-07-26 20:18:10 +00:00
|
|
|
-- Remove duplicates
|
|
|
|
get_turret_entity(self.pos)
|
|
|
|
end,
|
|
|
|
on_rightclick = function(self, clicker)
|
2019-03-15 17:55:37 +00:00
|
|
|
if self.user then
|
|
|
|
local player = minetest.get_player_by_name(self.user)
|
|
|
|
if player then
|
|
|
|
player:set_detach()
|
|
|
|
if use_player_api then
|
|
|
|
player_api.player_attached[self.user] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.user = nil
|
2016-03-30 18:04:12 +00:00
|
|
|
else
|
2019-03-15 17:55:37 +00:00
|
|
|
clicker:set_attach(self.object, "", {x=0,y=-5,z=-8}, {x=0,y=0,z=0})
|
|
|
|
self.user = clicker:get_player_name()
|
|
|
|
if use_player_api then
|
|
|
|
player_api.player_attached[self.user] = true
|
|
|
|
end
|
2014-07-26 20:18:10 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_step = function(self, dtime)
|
2019-03-15 17:55:37 +00:00
|
|
|
if not self.user then
|
|
|
|
return
|
|
|
|
end
|
2014-07-26 20:18:10 +00:00
|
|
|
self.timer = self.timer + dtime
|
2019-03-15 17:55:37 +00:00
|
|
|
if self.timer < 0.2 then
|
2014-07-26 20:18:10 +00:00
|
|
|
return
|
2019-03-15 17:55:37 +00:00
|
|
|
end
|
|
|
|
local player = minetest.get_player_by_name(self.user)
|
|
|
|
if player then
|
2014-07-26 20:18:10 +00:00
|
|
|
local pitch = self.pitch
|
2019-03-15 17:55:37 +00:00
|
|
|
local yaw = self.object:get_yaw()
|
|
|
|
local ctrl = player:get_player_control()
|
2014-07-26 20:18:10 +00:00
|
|
|
local step = 2
|
|
|
|
if ctrl then
|
|
|
|
if ctrl.sneak then
|
|
|
|
step = 1
|
|
|
|
if ctrl.jump then
|
|
|
|
if self.firing == false then
|
|
|
|
self:fire()
|
|
|
|
self.firing = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.firing = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if ctrl.down then
|
|
|
|
pitch = pitch + 1 * step
|
|
|
|
elseif ctrl.up then
|
|
|
|
pitch = pitch - 1 * step
|
|
|
|
end
|
|
|
|
if ctrl.left then
|
|
|
|
yaw = yaw + 0.025 * step
|
|
|
|
elseif ctrl.right then
|
|
|
|
yaw = yaw - 0.025 * step
|
|
|
|
end
|
|
|
|
if pitch < 0 then
|
|
|
|
pitch = 0
|
2019-03-19 19:23:23 +00:00
|
|
|
elseif pitch > 90 then
|
2014-07-26 20:18:10 +00:00
|
|
|
pitch = 90
|
|
|
|
end
|
|
|
|
if self.pitch ~= pitch then
|
|
|
|
self.object:set_animation({x=pitch, y=pitch}, 0)
|
|
|
|
self.pitch = pitch
|
|
|
|
end
|
|
|
|
if self.yaw ~= yaw then
|
2019-03-15 17:55:37 +00:00
|
|
|
self.object:set_yaw(yaw)
|
2014-07-26 20:18:10 +00:00
|
|
|
self.yaw = yaw
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.timer = 0
|
|
|
|
end,
|
|
|
|
fire = function(self)
|
2019-03-15 17:55:37 +00:00
|
|
|
if not self.user then
|
|
|
|
return
|
|
|
|
end
|
2014-07-26 20:18:10 +00:00
|
|
|
local meta = minetest.get_meta(self.pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
if not inv then
|
|
|
|
return
|
|
|
|
end
|
2019-03-15 17:55:37 +00:00
|
|
|
if not inv:contains_item("main", "shooter_rocket:rocket") then
|
2014-07-26 20:18:10 +00:00
|
|
|
minetest.sound_play("shooter_click", {object=self.object})
|
|
|
|
return
|
|
|
|
end
|
2019-03-15 17:55:37 +00:00
|
|
|
inv:remove_item("main", "shooter_rocket:rocket")
|
2018-01-03 20:21:00 +00:00
|
|
|
minetest.sound_play("shooter_shotgun", {object=self.object})
|
2014-07-26 20:18:10 +00:00
|
|
|
local pitch = math.rad(self.pitch - 40)
|
|
|
|
local len = math.cos(pitch)
|
|
|
|
local dir = vector.normalize({
|
|
|
|
x = len * math.sin(-self.yaw),
|
|
|
|
y = math.sin(pitch),
|
|
|
|
z = len * math.cos(self.yaw),
|
|
|
|
})
|
|
|
|
local pos = {x=self.pos.x, y=self.pos.y + 0.87, z=self.pos.z}
|
2019-03-15 17:55:37 +00:00
|
|
|
pos = vector.add(pos, vector.multiply(dir, 1.5))
|
|
|
|
local obj = minetest.add_entity(pos, "shooter_rocket:rocket_entity")
|
2014-07-26 20:18:10 +00:00
|
|
|
if obj then
|
2014-10-01 19:43:33 +00:00
|
|
|
local ent = obj:get_luaentity()
|
|
|
|
if ent then
|
|
|
|
minetest.sound_play("shooter_rocket_fire", {object=obj})
|
2019-03-15 17:55:37 +00:00
|
|
|
ent.user = self.user
|
|
|
|
obj:set_yaw(self.yaw)
|
|
|
|
obj:set_velocity(vector.multiply(dir, 30))
|
|
|
|
obj:set_acceleration({x=dir.x * -3, y=-10, z=dir.z * -3})
|
2014-10-01 19:43:33 +00:00
|
|
|
end
|
2014-07-26 20:18:10 +00:00
|
|
|
end
|
2018-01-05 20:02:18 +00:00
|
|
|
if shooter.config.enable_particle_fx == true then
|
2019-03-15 17:55:37 +00:00
|
|
|
minetest.add_particlespawner({
|
|
|
|
amount = 10,
|
|
|
|
time = 0.1,
|
|
|
|
minpos = {x=pos.x - 1, y=pos.y - 1, z=pos.z - 1},
|
|
|
|
maxpos = {x=pos.x + 1, y=pos.y + 1, z=pos.z + 1},
|
|
|
|
minvel = {x=0, y=0, z=0},
|
|
|
|
maxvel = {x=0, y=0, z=0},
|
|
|
|
minacc = {x=-0.5, y=-0.5, z=-0.5},
|
|
|
|
maxacc = {x=0.5, y=0.5, z=0.5},
|
|
|
|
minexptime = 0.1,
|
|
|
|
maxexptime = 1,
|
|
|
|
minsize = 8,
|
|
|
|
maxsize = 15,
|
|
|
|
collisiondetection = false,
|
2019-03-21 21:08:40 +00:00
|
|
|
texture = "shooter_smoke.png",
|
2019-03-15 17:55:37 +00:00
|
|
|
})
|
2014-07-26 20:18:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2017-07-13 12:33:00 +00:00
|
|
|
minetest.register_node("shooter_turret:turret", {
|
2014-07-26 20:18:10 +00:00
|
|
|
description = "Turret Gun",
|
|
|
|
tiles = {"shooter_turret_base.png"},
|
|
|
|
inventory_image = "shooter_turret_gun.png",
|
|
|
|
wield_image = "shooter_turret_gun.png",
|
|
|
|
drawtype = "nodebox",
|
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
2019-03-15 17:55:37 +00:00
|
|
|
groups = {snappy=2, choppy=2, oddly_breakable_by_hand=3},
|
2014-07-26 20:18:10 +00:00
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{-1/8, 1/8, -1/8, 1/8, 1/2, 1/8},
|
|
|
|
{-5/16, 0, -5/16, 5/16, 1/8, 5/16},
|
|
|
|
{-3/8, -1/2, -3/8, -1/4, 0, -1/4},
|
|
|
|
{1/4, -1/2, 1/4, 3/8, 0, 3/8},
|
|
|
|
{1/4, -1/2, -3/8, 3/8, 0, -1/4},
|
|
|
|
{-3/8, -1/2, 1/4, -1/4, 0, 3/8},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_string("formspec", "size[8,9]"..
|
|
|
|
"list[current_name;main;2,0;4,4;]"..
|
2019-03-15 17:55:37 +00:00
|
|
|
"list[current_player;main;0,5;8,4;]"..
|
|
|
|
"listring[]"
|
2014-07-26 20:18:10 +00:00
|
|
|
)
|
|
|
|
meta:set_string("infotext", "Turret Gun")
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("main", 16)
|
|
|
|
end,
|
2019-03-19 19:23:23 +00:00
|
|
|
after_place_node = function(pos)
|
2014-07-26 20:18:10 +00:00
|
|
|
local node = minetest.get_node({x=pos.x, y=pos.y + 1, z=pos.z})
|
|
|
|
if node.name == "air" then
|
|
|
|
if not get_turret_entity(pos) then
|
2017-07-13 12:33:00 +00:00
|
|
|
minetest.add_entity(pos, "shooter_turret:turret_entity")
|
2014-07-26 20:18:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2019-03-19 19:23:23 +00:00
|
|
|
can_dig = function(pos)
|
2014-07-26 20:18:10 +00:00
|
|
|
local meta = minetest.get_meta(pos);
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return inv:is_empty("main")
|
|
|
|
end,
|
2019-03-19 19:23:23 +00:00
|
|
|
after_destruct = function(pos)
|
2014-07-26 20:18:10 +00:00
|
|
|
local ent = get_turret_entity(pos)
|
|
|
|
if ent then
|
|
|
|
ent.object:remove()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
mesecons = {
|
|
|
|
effector = {
|
2019-03-19 19:23:23 +00:00
|
|
|
action_on = function(pos)
|
2014-07-26 20:18:10 +00:00
|
|
|
local ent = get_turret_entity(pos)
|
|
|
|
if ent then
|
|
|
|
if ent.firing == false then
|
|
|
|
ent:fire()
|
|
|
|
ent.firing = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2019-03-19 19:23:23 +00:00
|
|
|
action_off = function(pos)
|
2014-07-26 20:18:10 +00:00
|
|
|
local ent = get_turret_entity(pos)
|
|
|
|
if ent then
|
|
|
|
ent.firing = false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-01-03 20:24:36 +00:00
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = {"shooter_turret:turret"},
|
|
|
|
interval = 15,
|
|
|
|
chance = 1,
|
2019-03-19 19:23:23 +00:00
|
|
|
action = function(pos)
|
2018-01-03 20:24:36 +00:00
|
|
|
if not get_turret_entity(pos) then
|
|
|
|
minetest.add_entity(pos, "shooter_turret:turret_entity")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2018-01-05 20:02:18 +00:00
|
|
|
if shooter.config.enable_crafting == true then
|
2014-07-26 20:18:10 +00:00
|
|
|
minetest.register_craft({
|
2017-07-13 12:33:00 +00:00
|
|
|
output = "shooter_turret:turret",
|
2014-07-26 20:18:10 +00:00
|
|
|
recipe = {
|
|
|
|
{"default:bronze_ingot", "default:bronze_ingot", "default:steel_ingot"},
|
|
|
|
{"", "default:bronze_ingot", "default:steel_ingot"},
|
|
|
|
{"", "default:diamond", ""},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2017-07-13 12:33:00 +00:00
|
|
|
--Backward compatibility
|
|
|
|
minetest.register_alias("shooter:turret", "shooter_turret:turret")
|
|
|
|
|