Tidied code, added fly arrow check

This commit is contained in:
TenPlus1 2017-02-22 12:57:02 +00:00
parent bf5d976d8e
commit 59e8ba64f2
2 changed files with 105 additions and 96 deletions

183
api.lua
View File

@ -1,5 +1,5 @@
-- Mobs Api (10th February 2017) -- Mobs Api (22nd February 2017)
mobs = {} mobs = {}
mobs.mod = "redo" mobs.mod = "redo"
@ -3110,109 +3110,111 @@ end
-- capture critter (thanks to blert2112 for idea) -- capture critter (thanks to blert2112 for idea)
function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith) function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
if not self.child if self.child
and clicker:is_player() or not clicker:is_player()
and clicker:get_inventory() then or not clicker:get_inventory() then
return false
end
-- get name of clicked mob -- get name of clicked mob
local mobname = self.name local mobname = self.name
-- if not nil change what will be added to inventory -- if not nil change what will be added to inventory
if replacewith then if replacewith then
mobname = replacewith mobname = replacewith
end
local name = clicker:get_player_name()
-- is mob tamed?
if self.tamed == false
and force_take == false then
minetest.chat_send_player(name, S("Not tamed!"))
return false
end
-- cannot pick up if not owner
if self.owner ~= name
and force_take == false then
minetest.chat_send_player(name, S("@1 is owner!", self.owner))
return false
end
if clicker:get_inventory():room_for_item("main", mobname) then
-- was mob clicked with hand, net, or lasso?
local tool = clicker:get_wielded_item()
local chance = 0
if tool:is_empty() then
chance = chance_hand
elseif tool:get_name() == "mobs:net" then
chance = chance_net
tool:add_wear(4000) -- 17 uses
clicker:set_wielded_item(tool)
elseif tool:get_name() == "mobs:magic_lasso" then
chance = chance_lasso
tool:add_wear(650) -- 100 uses
clicker:set_wielded_item(tool)
end end
local name = clicker:get_player_name() -- calculate chance.. add to inventory if successful?
if chance > 0 and random(1, 100) <= chance then
-- is mob tamed? -- default mob egg
if self.tamed == false local new_stack = ItemStack(mobname)
and force_take == false then
minetest.chat_send_player(name, S("Not tamed!")) -- add special mob egg with all mob information
-- unless 'replacewith' contains new item to use
if not replacewith then
return new_stack = ItemStack(mobname .. "_set")
end
-- cannot pick up if not owner local tmp = {}
if self.owner ~= name
and force_take == false then
minetest.chat_send_player(name, S("@1 is owner!", self.owner)) for _,stat in pairs(self) do
local t = type(stat)
return if t ~= 'function'
end and t ~= 'nil'
and t ~= 'userdata' then
if clicker:get_inventory():room_for_item("main", mobname) then tmp[_] = self[_]
-- was mob clicked with hand, net, or lasso?
local tool = clicker:get_wielded_item()
local chance = 0
if tool:is_empty() then
chance = chance_hand
elseif tool:get_name() == "mobs:net" then
chance = chance_net
tool:add_wear(4000) -- 17 uses
clicker:set_wielded_item(tool)
elseif tool:get_name() == "mobs:magic_lasso" then
chance = chance_lasso
tool:add_wear(650) -- 100 uses
clicker:set_wielded_item(tool)
end
-- return if no chance
if chance == 0 then return end
-- calculate chance.. add to inventory if successful?
if random(1, 100) <= chance then
-- default mob egg
local new_stack = ItemStack(mobname)
-- add special mob egg with all mob information
-- unless 'replacewith' contains new item to use
if not replacewith then
new_stack = ItemStack(mobname .. "_set")
local tmp = {}
for _,stat in pairs(self) do
local t = type(stat)
if t ~= 'function'
and t ~= 'nil'
and t ~= 'userdata' then
tmp[_] = self[_]
end
end end
local data_str = minetest.serialize(tmp)
new_stack:set_metadata(data_str)
end end
local inv = clicker:get_inventory() local data_str = minetest.serialize(tmp)
if inv:room_for_item("main", new_stack) then new_stack:set_metadata(data_str)
inv:add_item("main", new_stack)
else
minetest.add_item(clicker:getpos(), new_stack)
end
self.object:remove()
else
minetest.chat_send_player(name, S("Missed!"))
end end
local inv = clicker:get_inventory()
if inv:room_for_item("main", new_stack) then
inv:add_item("main", new_stack)
else
minetest.add_item(clicker:getpos(), new_stack)
end
self.object:remove()
return true
else
minetest.chat_send_player(name, S("Missed!"))
end end
end end
return false
end end
@ -3226,6 +3228,11 @@ function mobs:protect(self, clicker)
return false return false
end end
if self.protected == true then
minetest.chat_send_player(name, S("Already protected!"))
return false
end
local tool = clicker:get_wielded_item() local tool = clicker:get_wielded_item()
if tool:get_name() == "mobs:protector" then if tool:get_name() == "mobs:protector" then

View File

@ -340,9 +340,6 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
else else
v = v * 0.25 v = v * 0.25
end end
-- elseif ni == "walkable" then
-- v = 0
-- new_acce.y = 1
end end
new_velo = get_velocity(v, entity.object:getyaw() - rot_view, velo.y) new_velo = get_velocity(v, entity.object:getyaw() - rot_view, velo.y)
@ -415,11 +412,16 @@ function mobs.fly(entity, dtime, speed, shoots, arrow, moving_anim, stand_anim)
y = pos.y + 1.5 + dir.y, y = pos.y + 1.5 + dir.y,
z = pos.z + 0 + dir.z * 2.5}, arrow) z = pos.z + 0 + dir.z * 2.5}, arrow)
local vec = {x = dir.x * 6, y = dir.y * 6, z = dir.z * 6} local ent = obj:get_luaentity()
local yaw = entity.driver:get_look_yaw() if ent then
ent.switch = 1 -- for mob specific arrows
obj:setyaw(yaw + math.pi / 2) local vec = {x = dir.x * 6, y = dir.y * 6, z = dir.z * 6}
obj:setvelocity(vec) local yaw = entity.driver:get_look_yaw()
obj:setyaw(yaw + math.pi / 2)
obj:setvelocity(vec)
else
obj:remove()
end
end end
-- change animation if stopped -- change animation if stopped