1
0
mirror of https://codeberg.org/tenplus1/mobs_redo.git synced 2025-06-28 06:30:18 +02:00

tidy and tweak code, implement sound check for mineclon*

This commit is contained in:
tenplus1
2024-08-10 13:18:57 +01:00
parent bc6b8931da
commit a8297e6a8e
8 changed files with 492 additions and 621 deletions

110
mount.lua
View File

@ -1,8 +1,10 @@
-- lib_mount by Blert2112 (edited by TenPlus1)
local is_mc2 = minetest.get_modpath("mcl_mobs") -- MineClone2 check
-- one of these is needed to ride mobs, otherwise no riding for you
if not minetest.get_modpath("player_api") and not is_mc2 then
function mobs.attach() end
@ -14,12 +16,11 @@ if not minetest.get_modpath("player_api") and not is_mc2 then
end
-- Localise some functions
local abs, cos, floor, sin, sqrt, pi =
math.abs, math.cos, math.floor, math.sin, math.sqrt, math.pi
--
-- Helper functions
--
-- helper functions
local node_ok = function(pos, fallback)
@ -27,9 +28,7 @@ local node_ok = function(pos, fallback)
local node = minetest.get_node_or_nil(pos)
if node and minetest.registered_nodes[node.name] then
return node
end
if node and minetest.registered_nodes[node.name] then return node end
return {name = fallback}
end
@ -39,9 +38,7 @@ local function node_is(entity)
if not entity.standing_on then return "other" end
if entity.standing_on == "air" then
return "air"
end
if entity.standing_on == "air" then return "air" end
if minetest.get_item_group(entity.standing_on, "lava") ~= 0 then
return "lava"
@ -61,13 +58,9 @@ end
local function get_sign(i)
i = i or 0
if not i or i == 0 then return 0 end
if i == 0 then
return 0
else
return i / abs(i)
end
return i / abs(i)
end
@ -89,9 +82,7 @@ local function force_detach(player)
local attached_to = player and player:get_attach()
if not attached_to then
return
end
if not attached_to then return end
local entity = attached_to:get_luaentity()
@ -115,11 +106,13 @@ local function force_detach(player)
player:set_properties({visual_size = {x = 1, y = 1}})
end
-- detach player on leaving
minetest.register_on_leaveplayer(function(player)
force_detach(player)
end)
-- detatch all players on shutdown
minetest.register_on_shutdown(function()
@ -130,25 +123,21 @@ minetest.register_on_shutdown(function()
end
end)
-- detatch player when dead
minetest.register_on_dieplayer(function(player)
force_detach(player)
return true
end)
-- find free position to detach player
-- Just for correct detaching
local function find_free_pos(pos)
local check = {
{x = 1, y = 0, z = 0},
{x = 1, y = 1, z = 0},
{x = -1, y = 0, z = 0},
{x = -1, y = 1, z = 0},
{x = 0, y = 0, z = 1},
{x = 0, y = 1, z = 1},
{x = 0, y = 0, z = -1},
{x = 0, y = 1, z = -1}
{x = 1, y = 0, z = 0}, {x = 1, y = 1, z = 0}, {x = -1, y = 0, z = 0},
{x = -1, y = 1, z = 0}, {x = 0, y = 0, z = 1}, {x = 0, y = 1, z = 1},
{x = 0, y = 0, z = -1}, {x = 0, y = 1, z = -1}
}
for _, c in pairs(check) do
@ -169,8 +158,8 @@ local function find_free_pos(pos)
return pos
end
-- are we a real player ?
local function is_player(player)
if player and type(player) == "userdata" and minetest.is_player(player) then
@ -178,6 +167,7 @@ local function is_player(player)
end
end
-- attach player to mob entity
function mobs.attach(entity, player)
@ -190,9 +180,7 @@ function mobs.attach(entity, player)
local rot_view = 0
if entity.player_rotation.y == 90 then
rot_view = pi / 2
end
if entity.player_rotation.y == 90 then rot_view = pi / 2 end
local attach_at = entity.driver_attach_at
local eye_offset = entity.driver_eye_offset
@ -211,10 +199,7 @@ function mobs.attach(entity, player)
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
}
visual_size = {x = entity.driver_scale.x, y = entity.driver_scale.y}
})
minetest.after(0.2, function()
@ -232,6 +217,7 @@ function mobs.attach(entity, player)
player:set_look_horizontal(entity.object:get_yaw() - rot_view)
end
-- detatch player from mob
function mobs.detach(player)
@ -250,15 +236,14 @@ function mobs.detach(player)
end)
end
-- ride mob like car or horse
function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
local yaw = entity.object:get_yaw() or 0
local rot_view = 0
if entity.player_rotation.y == 90 then
rot_view = pi / 2
end
if entity.player_rotation.y == 90 then rot_view = pi / 2 end
local acce_y = 0
local velo = entity.object:get_velocity() ; if not velo then return end
@ -276,9 +261,7 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
elseif ctrl.down then -- move backwards
if entity.max_speed_reverse == 0 and entity.v == 0 then
return
end
if entity.max_speed_reverse == 0 and entity.v == 0 then return end
entity.v = entity.v - entity.accel * dtime
end
@ -290,12 +273,8 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
horz = yaw
if ctrl.left then
horz = horz + 0.05
elseif ctrl.right then
horz = horz - 0.05
end
if ctrl.left then horz = horz + 0.05
elseif ctrl.right then horz = horz - 0.05 end
else
horz = entity.driver:get_look_horizontal() or 0
end
@ -333,8 +312,7 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
if ctrl.jump then -- jump (only when standing on solid surface)
if velo.y == 0
and entity.standing_on ~= "air"
and entity.standing_on ~= "ignore"
and entity.standing_on ~= "air" and entity.standing_on ~= "ignore"
and minetest.get_item_group(entity.standing_on, "liquid") == 0 then
velo.y = velo.y + entity.jump_height
acce_y = acce_y + (acce_y * 3) + 1
@ -346,17 +324,13 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
-- if not moving then set animation and return
if entity.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
if stand_anim then
entity:set_animation(stand_anim)
end
if stand_anim then entity:set_animation(stand_anim) end
return
end
-- set moving animation
if moving_anim then
entity:set_animation(moving_anim)
end
if moving_anim then entity:set_animation(moving_anim) end
-- Stop!
local s = get_sign(entity.v)
@ -392,9 +366,7 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
if ni == "air" then
if can_fly == true then
new_acce.y = 0
end
if can_fly == true then new_acce.y = 0 end
elseif ni == "liquid" or ni == "lava" then
@ -441,8 +413,8 @@ function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
entity.v2 = v
end
-- fly mob in facing direction (by D00Med, edited by TenPlus1)
-- directional flying routine by D00Med (edited by TenPlus1)
function mobs.fly(entity, _, speed, shoots, arrow, moving_anim, stand_anim)
local ctrl = entity.driver:get_player_control() ; if not ctrl then return end
@ -454,19 +426,13 @@ function mobs.fly(entity, _, speed, shoots, arrow, moving_anim, stand_anim)
if ctrl.up then
entity.object:set_velocity({
x = dir.x * speed,
y = dir.y * speed + 2,
z = dir.z * speed
})
entity.object:set_velocity(
{x = dir.x * speed, y = dir.y * speed + 2, z = dir.z * speed})
elseif ctrl.down then
entity.object:set_velocity({
x = -dir.x * speed,
y = dir.y * speed + 2,
z = -dir.z * speed
})
entity.object:set_velocity(
{x = -dir.x * speed, y = dir.y * speed + 2, z = -dir.z * speed})
elseif not ctrl.down or ctrl.up or ctrl.jump then
entity.object:set_velocity({x = 0, y = -2, z = 0})
@ -501,11 +467,9 @@ function mobs.fly(entity, _, speed, shoots, arrow, moving_anim, stand_anim)
end
end
-- change animation if stopped
if velo.x == 0 and velo.y == 0 and velo.z == 0 then
entity:set_animation(stand_anim)
entity:set_animation(stand_anim) -- stopped animation
else
-- moving animation
entity:set_animation(moving_anim)
entity:set_animation(moving_anim) -- moving animation
end
end