2013-01-06 23:25:30 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Mob Framework Mod by Sapier
|
2013-12-01 11:50:07 +01:00
|
|
|
--
|
2013-01-06 23:25:30 +00:00
|
|
|
-- You may copy, use, modify or do nearly anything except removing this
|
2013-12-01 11:50:07 +01:00
|
|
|
-- copyright notice.
|
2013-01-06 23:25:30 +00:00
|
|
|
-- And of course you are NOT allow to pretend you have written it.
|
|
|
|
--
|
|
|
|
--! @file ride.lua
|
|
|
|
--! @brief class containing mobf functions for riding
|
|
|
|
--! @copyright Sapier
|
|
|
|
--! @author Sapier
|
|
|
|
--! @date 2013-01-06
|
|
|
|
--
|
|
|
|
--
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @defgroup mobf_ride Rideable mobs subcomponent
|
2013-01-06 23:25:30 +00:00
|
|
|
--! @brief a component containing all functions required to ride a mob
|
|
|
|
--! @ingroup framework_int
|
2013-12-01 11:50:07 +01:00
|
|
|
--! @{
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
-- Contact sapier a t gmx net
|
|
|
|
-------------------------------------------------------------------------------
|
2013-08-21 19:40:14 +02:00
|
|
|
mobf_assert_backtrace(mobf_ride == nil)
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @brief contains all riding specific functions
|
|
|
|
--! @}
|
2013-01-06 23:25:30 +00:00
|
|
|
|
|
|
|
mobf_ride = {}
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2014-05-31 00:46:15 +02:00
|
|
|
-- @function [parent=#mobf_ride] attach_player(entity,player)
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @brief make a player ride this mob
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @private
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @param entity entity to be ridden
|
|
|
|
--! @param player player riding
|
|
|
|
-------------------------------------------------------------------------------
|
2014-05-31 00:46:15 +02:00
|
|
|
function mobf_ride.attach_player(entity,player)
|
2013-01-06 23:25:30 +00:00
|
|
|
|
|
|
|
entity.dynamic_data.ride.is_attached = true
|
|
|
|
entity.dynamic_data.ride.player = player
|
|
|
|
entity.object:setacceleration({x=0,y=-9.81,z=0})
|
|
|
|
entity.object:setvelocity({x=0,y=-9.81,z=0})
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
local attacheoffset = {x=0,y=0.5,z=0}
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if entity.data.ride ~= nil and
|
|
|
|
entity.data.ride.attacheoffset ~= nil then
|
|
|
|
attacheoffset = entity.data.ride.attacheoffset
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2014-05-31 00:46:15 +02:00
|
|
|
player:set_attach(entity.object,"",attacheoffset, {x=0,y=90,z=0})
|
|
|
|
|
|
|
|
-- default always overrides animations even for attached players
|
|
|
|
-- if type(default.player_set_animation) == "function" then
|
|
|
|
-- default.player_set_animation(player, "sit")
|
|
|
|
-- end
|
2013-01-09 20:22:56 +00:00
|
|
|
if entity.data.ride.texturemod ~= nil then
|
|
|
|
entity.object:settexturemod(entity.data.ride.texturemod);
|
|
|
|
end
|
2013-01-06 23:25:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2014-05-31 00:46:15 +02:00
|
|
|
-- @function [parent=#mobf_ride] dettach_player(entity,player)
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @brief make a player ride this mob
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @private
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @param entity entity to be ridden
|
|
|
|
-------------------------------------------------------------------------------
|
2014-05-31 00:46:15 +02:00
|
|
|
function mobf_ride.dettach_player(entity)
|
2013-01-06 23:25:30 +00:00
|
|
|
|
|
|
|
entity.dynamic_data.ride.is_attached = false
|
|
|
|
entity.dynamic_data.ride.player:set_detach()
|
|
|
|
entity.dynamic_data.ride.player = nil
|
2013-01-09 20:22:56 +00:00
|
|
|
entity.object:settexturemod("");
|
2013-01-06 23:25:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2013-12-01 11:50:07 +01:00
|
|
|
-- @function [parent=#mobf_ride] on_step_callback(entity)
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @brief make a player ride this mob
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @public
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @param entity entity to be ridden
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function mobf_ride.on_step_callback(entity)
|
|
|
|
|
|
|
|
if entity.dynamic_data.ride.is_attached then
|
|
|
|
dbg_mobf.ride_lvl3("MOBF: have attached player")
|
|
|
|
local walkspeed = 3
|
|
|
|
local sneakspeed = 0.5
|
|
|
|
local jumpspeed = 30
|
2014-02-06 21:24:04 +01:00
|
|
|
local runspeed = walkspeed
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if entity.data.ride ~= nil then
|
|
|
|
if entity.data.ride.walkspeed ~= nil then
|
|
|
|
walkspeed = entity.data.ride.walkspeed
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if entity.data.ride.runspeed ~= nil then
|
|
|
|
runspeed = entity.data.ride.runspeed
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if entity.data.ride.sneakspeed ~= nil then
|
|
|
|
sneakspeed = entity.data.ride.sneakspeed
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if entity.data.ride.jumpspeed ~= nil then
|
|
|
|
jumpspeed = entity.data.ride.jumpspeed
|
|
|
|
end
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
local dir = entity.dynamic_data.ride.player:get_look_yaw()
|
|
|
|
local current_speed = entity.object:getacceleration()
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
local speed_to_set = {x=0,y=current_speed.y,z=0}
|
|
|
|
if dir ~= nil then
|
|
|
|
local playerctrl = entity.dynamic_data.ride.player:get_player_control()
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if playerctrl ~= nil then
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
local setspeed = false
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if playerctrl.jump and
|
|
|
|
entity.is_on_ground(entity) then
|
|
|
|
speed_to_set.y = jumpspeed
|
|
|
|
setspeed = true
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
--just set speed to playerview direction
|
|
|
|
if playerctrl.up then
|
|
|
|
setspeed = true
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
--invert playerview direction
|
|
|
|
if playerctrl.down then
|
|
|
|
dir = dir + math.pi
|
|
|
|
setspeed = true
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if playerctrl.left then
|
|
|
|
if playerctrl.up then
|
|
|
|
dir = dir + math.pi/4
|
|
|
|
elseif playerctrl.down then
|
|
|
|
dir = dir - math.pi/4
|
|
|
|
else
|
|
|
|
dir = dir + math.pi/2
|
|
|
|
end
|
|
|
|
setspeed = true
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if playerctrl.right then
|
|
|
|
if playerctrl.up then
|
|
|
|
dir = dir - math.pi/4
|
|
|
|
elseif playerctrl.down then
|
|
|
|
dir = dir + math.pi/4
|
|
|
|
else
|
|
|
|
dir = dir - math.pi/2
|
|
|
|
end
|
|
|
|
setspeed = true
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
local selected_speed = walkspeed
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
if playerctrl.sneak then
|
|
|
|
selected_speed = sneakspeed
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
|
|
|
|
if setspeed then
|
2014-02-06 21:24:04 +01:00
|
|
|
local speed_to_set_xz = mobf_calc_vector_components(dir,selected_speed)
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
speed_to_set.x = speed_to_set_xz.x
|
|
|
|
speed_to_set.z = speed_to_set_xz.z
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-09 20:22:56 +00:00
|
|
|
if entity.data.ride.walk_anim ~= nil then
|
|
|
|
graphics.set_animation(entity,entity.data.ride.walk_anim)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if entity.data.ride.walk_anim ~= nil then
|
|
|
|
if entity.data.ride.stand_anim ~= nil then
|
|
|
|
graphics.set_animation(entity,entity.data.ride.stand_anim)
|
|
|
|
mob_state.change_state(entity,mob_state.get_state_by_name(entity,entity.data.ride.state_stand))
|
|
|
|
else
|
|
|
|
graphics.set_animation(entity,"stand")
|
|
|
|
end
|
|
|
|
end
|
2013-01-06 23:25:30 +00:00
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
entity.object:setvelocity(speed_to_set)
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
--fix switched model orientation
|
|
|
|
entity.object:setyaw(dir)
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-09 20:22:56 +00:00
|
|
|
------------------------------------------------------------------------------
|
2013-12-01 11:50:07 +01:00
|
|
|
-- @function [parent=#mobf_ride] on_punch_callback(entity,player)
|
2013-01-09 20:22:56 +00:00
|
|
|
--
|
|
|
|
--! @brief make a player ride this mob
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @public
|
2013-01-09 20:22:56 +00:00
|
|
|
--
|
|
|
|
--! @param entity entity to be ridden
|
|
|
|
--! @param player player riding
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function mobf_ride.on_punch_callback(entity,player)
|
|
|
|
dbg_mobf.ride_lvl2("MOBF: ride on punch callback")
|
|
|
|
print("MOBF: ride on punch callback")
|
2013-12-15 12:32:05 +01:00
|
|
|
|
|
|
|
local saddle = "animalmaterials:saddle"
|
|
|
|
|
|
|
|
if entity.data.ride.saddle ~= nil then
|
|
|
|
saddle = entity.data.ride.saddle
|
|
|
|
end
|
2013-01-09 20:22:56 +00:00
|
|
|
--detache
|
|
|
|
if entity.dynamic_data.ride.is_attached ~= false then
|
|
|
|
dbg_mobf.ride_lvl2("MOBF: punched ridden mob")
|
|
|
|
if entity.dynamic_data.ride.player == player then
|
|
|
|
dbg_mobf.ride_lvl2("MOBF: detaching player")
|
2014-05-31 00:46:15 +02:00
|
|
|
mobf_ride.dettach_player(entity)
|
2013-12-15 12:32:05 +01:00
|
|
|
player:get_inventory():add_item("main",saddle .. " 1")
|
2013-01-09 20:22:56 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
--check if player has saddle
|
|
|
|
dbg_mobf.ride_lvl2("MOBF: punched free mob")
|
2013-12-15 12:32:05 +01:00
|
|
|
if player:get_wielded_item():get_name() == saddle then
|
2013-01-09 20:22:56 +00:00
|
|
|
dbg_mobf.ride_lvl2("MOBF: punching with saddle")
|
2013-12-15 12:32:05 +01:00
|
|
|
|
|
|
|
if player:get_inventory():contains_item("main",saddle .. " 1") then
|
2013-01-09 20:22:56 +00:00
|
|
|
dbg_mobf.ride_lvl2("MOBF: have saddle")
|
2014-05-31 00:46:15 +02:00
|
|
|
mobf_ride.attach_player(entity,player)
|
2013-12-15 12:32:05 +01:00
|
|
|
player:get_inventory():remove_item("main",saddle .. " 1")
|
2013-01-09 20:22:56 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
dbg_mobf.ride_lvl2("MOBF: not punching with saddle but: " .. player:get_wielded_item():get_name())
|
|
|
|
end
|
|
|
|
end
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-09 20:22:56 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2013-12-01 11:50:07 +01:00
|
|
|
-- @function [parent=#mobf_ride] is_enabled(entity)
|
2013-01-09 20:22:56 +00:00
|
|
|
--
|
|
|
|
--! @brief check if riding is enabled for a mob
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @public
|
2013-01-09 20:22:56 +00:00
|
|
|
--
|
|
|
|
--! @param entity entity to be ridden
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function mobf_ride.is_enabled(entity)
|
|
|
|
if entity.data.ride ~= nil then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
dbg_mobf.ride_lvl2("riding of " .. entity.data.name .. " is disabled")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
------------------------------------------------------------------------------
|
2014-05-31 00:46:15 +02:00
|
|
|
-- @function [parent=#mobf_ride] init(entity)
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
2014-05-31 00:46:15 +02:00
|
|
|
--! @brief initialize ride dynamic data
|
2013-04-28 14:09:30 +02:00
|
|
|
--! @class mobf_ride
|
|
|
|
--! @public
|
2013-01-06 23:25:30 +00:00
|
|
|
--
|
|
|
|
--! @param entity entity to be ridden
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function mobf_ride.init(entity)
|
|
|
|
local data = {
|
|
|
|
is_attached = false,
|
|
|
|
player = nil,
|
|
|
|
}
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-06 23:25:30 +00:00
|
|
|
entity.dynamic_data.ride = data
|
2013-01-19 13:50:54 +00:00
|
|
|
end
|
|
|
|
|
2013-04-28 14:09:30 +02:00
|
|
|
-- special handler on leave
|
2013-01-19 13:50:54 +00:00
|
|
|
minetest.register_on_leaveplayer( function(player)
|
|
|
|
if player ~= nil and
|
|
|
|
player.object ~= nil then
|
|
|
|
local pos = player.object:getpos()
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-19 13:50:54 +00:00
|
|
|
--print("MOBF: got player position: " ..printpos(pos) )
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-19 13:50:54 +00:00
|
|
|
if pos ~= nil then
|
|
|
|
local objects = get_objects_inside_radius(pos, 5)
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-19 13:50:54 +00:00
|
|
|
print("MOBF: found " .. dump(#objects) .. " objects around player")
|
2013-12-01 11:50:07 +01:00
|
|
|
|
2013-01-19 13:50:54 +00:00
|
|
|
if objects ~= nil then
|
|
|
|
for i=1,#objects,1 do
|
|
|
|
local entity = objects[i]:get_luaentity()
|
|
|
|
if entity ~= nil and
|
|
|
|
entity.dynamic_data ~= nil and
|
|
|
|
entity.dynamic_data.ride ~= nil and
|
|
|
|
entity.dynamic_data.ride.player == player then
|
|
|
|
print("MOBF: found player to be attached")
|
2014-05-31 00:46:15 +02:00
|
|
|
ride.dettach_player(entity)
|
2013-01-19 13:50:54 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|