1
0
Fork 0
MineClone2/mods/ENTITIES/mcl_mobs/api/mount.lua

452 lines
9.1 KiB
Lua
Raw Normal View History

2017-05-25 08:33:19 +00:00
-- lib_mount by Blert2112 (edited by TenPlus1)
2017-07-04 23:52:39 +00:00
local enable_crash = false
2017-05-25 08:33:19 +00:00
local crash_threshold = 6.5 -- ignored if enable_crash=false
------------------------------------------------------------------------------
--
-- Helper functions
--
2017-07-04 23:52:39 +00:00
local node_ok = function(pos, fallback)
2017-07-25 02:30:23 +00:00
fallback = fallback or mobs.fallback_node
2017-07-04 23:52:39 +00:00
local node = minetest.get_node_or_nil(pos)
if node and minetest.registered_nodes[node.name] then
return node
end
return {name = fallback}
end
2017-05-25 08:33:19 +00:00
local function node_is(pos)
2017-07-04 23:52:39 +00:00
local node = node_ok(pos)
2017-05-25 08:33:19 +00:00
if node.name == "air" then
return "air"
end
if minetest.get_item_group(node.name, "lava") ~= 0 then
return "lava"
end
if minetest.get_item_group(node.name, "liquid") ~= 0 then
return "liquid"
end
2017-07-04 23:52:39 +00:00
if minetest.registered_nodes[node.name].walkable == true then
2017-05-25 08:33:19 +00:00
return "walkable"
end
return "other"
end
local function get_sign(i)
i = i or 0
if i == 0 then
return 0
else
return i / math.abs(i)
end
end
local function get_velocity(v, yaw, y)
local x = -math.sin(yaw) * v
local z = math.cos(yaw) * v
return {x = x, y = y, z = z}
end
local function get_v(v)
return math.sqrt(v.x * v.x + v.z * v.z)
end
local function force_detach(player)
local attached_to = player:get_attach()
if not attached_to then
return
end
local entity = attached_to:get_luaentity()
if entity.driver
and entity.driver == player then
entity.driver = nil
end
player:set_detach()
mcl_player.player_attached[player:get_player_name()] = false
player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
mcl_player.player_set_animation(player, "stand" , 30)
player:set_properties({visual_size = {x = 1, y = 1} })
end
-------------------------------------------------------------------------------
minetest.register_on_leaveplayer(function(player)
force_detach(player)
end)
minetest.register_on_shutdown(function()
local players = minetest.get_connected_players()
for i = 1, #players do
force_detach(players[i])
end
end)
minetest.register_on_dieplayer(function(player)
force_detach(player)
return true
end)
-------------------------------------------------------------------------------
function mobs.attach(entity, player)
local attach_at, eye_offset
2017-05-25 08:33:19 +00:00
entity.player_rotation = entity.player_rotation or {x = 0, y = 0, z = 0}
entity.driver_attach_at = entity.driver_attach_at or {x = 0, y = 0, z = 0}
entity.driver_eye_offset = entity.driver_eye_offset or {x = 0, y = 0, z = 0}
entity.driver_scale = entity.driver_scale or {x = 1, y = 1}
local rot_view = 0
if entity.player_rotation.y == 90 then
rot_view = math.pi/2
end
attach_at = entity.driver_attach_at
eye_offset = entity.driver_eye_offset
entity.driver = player
force_detach(player)
player:set_attach(entity.object, "", attach_at, entity.player_rotation)
mcl_player.player_attached[player:get_player_name()] = true
player:set_eye_offset(eye_offset, {x = 0, y = 0, z = 0})
player:set_properties({
visual_size = {
x = entity.driver_scale.x,
y = entity.driver_scale.y
}
})
2018-06-03 14:17:55 +00:00
minetest.after(0.2, function(name)
local player = minetest.get_player_by_name(name)
if player then
2021-03-10 20:27:42 +00:00
mcl_player.player_set_animation(player, "sit_mount" , 30)
2018-06-03 14:13:46 +00:00
end
2018-06-03 14:17:55 +00:00
end, player:get_player_name())
2017-05-25 08:33:19 +00:00
2017-11-03 23:22:43 +00:00
player:set_look_horizontal(entity.object:get_yaw() - rot_view)
2017-05-25 08:33:19 +00:00
end
function mobs.detach(player, offset)
force_detach(player)
mcl_player.player_set_animation(player, "stand" , 30)
2021-04-07 01:59:51 +00:00
--local pos = player:get_pos()
2017-05-25 08:33:19 +00:00
2021-04-07 01:59:51 +00:00
--pos = {x = pos.x + offset.x, y = pos.y + 0.2 + offset.y, z = pos.z + offset.z}
2017-05-25 08:33:19 +00:00
2021-04-07 02:02:52 +00:00
player:add_velocity(vector.new(math.random(-6,6),math.random(5,8),math.random(-6,6))) --throw the rider off
2021-04-07 01:59:51 +00:00
--[[
2018-06-03 14:17:55 +00:00
minetest.after(0.1, function(name, pos)
local player = minetest.get_player_by_name(name)
if player then
2018-06-03 14:13:46 +00:00
player:set_pos(pos)
end
2018-06-03 14:17:55 +00:00
end, player:get_player_name(), pos)
2021-04-07 01:59:51 +00:00
]]--
2017-05-25 08:33:19 +00:00
end
function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
local rot_view = 0
2017-05-25 08:33:19 +00:00
if entity.player_rotation.y == 90 then
rot_view = math.pi/2
2017-05-25 08:33:19 +00:00
end
local acce_y = 0
2019-03-06 03:38:57 +00:00
local velo = entity.object:get_velocity()
2017-05-25 08:33:19 +00:00
entity.v = get_v(velo) * get_sign(entity.v)
-- process controls
if entity.driver then
local ctrl = entity.driver:get_player_control()
-- move forwards
if ctrl.up then
2021-04-23 19:53:01 +00:00
mobs.set_velocity(entity, entity.run_velocity)
mobs.set_mob_animation(entity, moving_anim)
2017-05-25 08:33:19 +00:00
-- move backwards
elseif ctrl.down then
2021-04-23 19:53:01 +00:00
mobs.set_velocity(entity, -entity.run_velocity)
mobs.set_mob_animation(entity, moving_anim)
--halt
else
2017-05-25 08:33:19 +00:00
2021-04-23 19:53:01 +00:00
mobs.set_velocity(entity, 0)
mobs.set_mob_animation(entity, stand_anim)
2017-05-25 08:33:19 +00:00
end
2021-04-23 19:53:01 +00:00
-- mob rotation
2019-03-06 03:38:57 +00:00
entity.object:set_yaw(entity.driver:get_look_horizontal() - entity.rotate)
2021-04-23 19:53:01 +00:00
entity.yaw = entity.driver:get_look_horizontal() - entity.rotate
2017-05-25 08:33:19 +00:00
2021-04-23 19:53:01 +00:00
--[[
2017-05-25 08:33:19 +00:00
if can_fly then
-- fly up
if ctrl.jump then
velo.y = velo.y + 1
if velo.y > entity.accel then velo.y = entity.accel end
elseif velo.y > 0 then
velo.y = velo.y - 0.1
if velo.y < 0 then velo.y = 0 end
end
-- fly down
if ctrl.sneak then
velo.y = velo.y - 1
if velo.y < -entity.accel then velo.y = -entity.accel end
elseif velo.y < 0 then
velo.y = velo.y + 0.1
if velo.y > 0 then velo.y = 0 end
end
else
2021-04-23 19:53:01 +00:00
]]--
2017-05-25 08:33:19 +00:00
2021-04-23 19:53:01 +00:00
-- jump
if ctrl.jump then
2017-05-25 08:33:19 +00:00
2021-04-23 19:53:01 +00:00
mobs.jump(entity)
2017-05-25 08:33:19 +00:00
end
2021-04-23 19:53:01 +00:00
--end
2017-05-25 08:33:19 +00:00
end
2021-04-23 19:53:01 +00:00
--[[
2017-05-25 08:33:19 +00:00
-- set moving animation
if moving_anim then
2021-04-23 19:53:01 +00:00
mobs:set_mob_animation(entity, moving_anim)
2017-05-25 08:33:19 +00:00
end
-- Stop!
local s = get_sign(entity.v)
entity.v = entity.v - 0.02 * s
if s ~= get_sign(entity.v) then
2019-03-06 03:38:57 +00:00
entity.object:set_velocity({x = 0, y = 0, z = 0})
2017-05-25 08:33:19 +00:00
entity.v = 0
return
end
-- enforce speed limit forward and reverse
local max_spd = entity.max_speed_reverse
if get_sign(entity.v) >= 0 then
max_spd = entity.max_speed_forward
end
if math.abs(entity.v) > max_spd then
entity.v = entity.v - get_sign(entity.v)
end
-- Set position, velocity and acceleration
2017-11-03 23:22:43 +00:00
local p = entity.object:get_pos()
local new_velo
2017-05-25 08:33:19 +00:00
local new_acce = {x = 0, y = -9.8, z = 0}
p.y = p.y - 0.5
local ni = node_is(p)
local v = entity.v
if ni == "air" then
if can_fly == true then
new_acce.y = 0
end
elseif ni == "liquid" or ni == "lava" then
if ni == "lava" and entity.lava_damage ~= 0 then
entity.lava_counter = (entity.lava_counter or 0) + dtime
if entity.lava_counter > 1 then
minetest.sound_play("default_punch", {
object = entity.object,
max_hear_distance = 5
2020-04-06 22:55:45 +00:00
}, true)
2017-05-25 08:33:19 +00:00
entity.object:punch(entity.object, 1.0, {
full_punch_interval = 1.0,
damage_groups = {fleshy = entity.lava_damage}
}, nil)
entity.lava_counter = 0
end
end
if entity.terrain_type == 2
or entity.terrain_type == 3 then
new_acce.y = 0
p.y = p.y + 1
if node_is(p) == "liquid" then
if velo.y >= 5 then
velo.y = 5
elseif velo.y < 0 then
new_acce.y = 20
else
new_acce.y = 5
end
else
if math.abs(velo.y) < 1 then
2017-11-03 23:22:43 +00:00
local pos = entity.object:get_pos()
2017-05-25 08:33:19 +00:00
pos.y = math.floor(pos.y) + 0.5
2019-03-06 03:38:57 +00:00
entity.object:set_pos(pos)
2017-05-25 08:33:19 +00:00
velo.y = 0
end
end
else
v = v * 0.25
end
end
2017-11-03 23:22:43 +00:00
new_velo = get_velocity(v, entity.object:get_yaw() - rot_view, velo.y)
2017-05-25 08:33:19 +00:00
new_acce.y = new_acce.y + acce_y
2019-03-06 03:38:57 +00:00
entity.object:set_velocity(new_velo)
entity.object:set_acceleration(new_acce)
2017-05-25 08:33:19 +00:00
-- CRASH!
if enable_crash then
local intensity = entity.v2 - v
if intensity >= crash_threshold then
entity.object:punch(entity.object, 1.0, {
full_punch_interval = 1.0,
damage_groups = {fleshy = intensity}
}, nil)
end
end
entity.v2 = v
2021-04-23 19:53:01 +00:00
]]--
2017-05-25 08:33:19 +00:00
end
-- directional flying routine by D00Med (edited by TenPlus1)
function mobs.fly(entity, dtime, speed, shoots, arrow, moving_anim, stand_anim)
2021-04-23 19:53:01 +00:00
if true then
print("succ")
return
end
2017-05-25 08:33:19 +00:00
local ctrl = entity.driver:get_player_control()
2019-03-06 03:38:57 +00:00
local velo = entity.object:get_velocity()
2017-05-25 08:33:19 +00:00
local dir = entity.driver:get_look_dir()
local yaw = entity.driver:get_look_horizontal() + 1.57 -- offset fix between old and new commands
if ctrl.up then
2019-03-06 03:38:57 +00:00
entity.object:set_velocity({
2017-05-25 08:33:19 +00:00
x = dir.x * speed,
y = dir.y * speed + 2,
z = dir.z * speed
})
elseif ctrl.down then
2019-03-06 03:38:57 +00:00
entity.object:set_velocity({
2017-05-25 08:33:19 +00:00
x = -dir.x * speed,
y = dir.y * speed + 2,
z = -dir.z * speed
})
elseif not ctrl.down or ctrl.up or ctrl.jump then
2019-03-06 03:38:57 +00:00
entity.object:set_velocity({x = 0, y = -2, z = 0})
2017-05-25 08:33:19 +00:00
end
2019-03-06 03:38:57 +00:00
entity.object:set_yaw(yaw + math.pi + math.pi / 2 - entity.rotate)
2017-05-25 08:33:19 +00:00
-- firing arrows
if ctrl.LMB and ctrl.sneak and shoots then
2017-11-03 23:22:43 +00:00
local pos = entity.object:get_pos()
2017-05-25 08:33:19 +00:00
local obj = minetest.add_entity({
x = pos.x + 0 + dir.x * 2.5,
y = pos.y + 1.5 + dir.y,
z = pos.z + 0 + dir.z * 2.5}, arrow)
local ent = obj:get_luaentity()
if ent then
ent.switch = 1 -- for mob specific arrows
ent.owner_id = tostring(entity.object) -- so arrows dont hurt entity you are riding
local vec = {x = dir.x * 6, y = dir.y * 6, z = dir.z * 6}
local yaw = entity.driver:get_look_horizontal()
2019-03-06 03:38:57 +00:00
obj:set_yaw(yaw + math.pi / 2)
obj:set_velocity(vec)
2017-05-25 08:33:19 +00:00
else
obj:remove()
end
end
-- change animation if stopped
if velo.x == 0 and velo.y == 0 and velo.z == 0 then
2021-04-23 19:53:01 +00:00
mobs:set_mob_animation(entity, stand_anim)
2017-05-25 08:33:19 +00:00
else
-- moving animation
2021-04-23 19:53:01 +00:00
mobs:set_mob_animation(entity, moving_anim)
2017-05-25 08:33:19 +00:00
end
end