This commit is contained in:
kay27 2021-02-08 03:15:34 +04:00
parent 26037157f8
commit bd1e4187a0
1 changed files with 290 additions and 294 deletions

View File

@ -1,294 +1,290 @@
local S = minetest.get_translator("mcl_playerplus") local S = minetest.get_translator("mcl_playerplus")
-- Internal player state -- Internal player state
local mcl_playerplus_internal = {} local mcl_playerplus_internal = {}
local def = {} local def = {}
local time = 0 local time = 0
-- converts yaw to degrees -- converts yaw to degrees
local function degrees(rad) local function degrees(rad)
return rad * 180.0 / math.pi return rad * 180.0 / math.pi
end end
local pitch, name, node_stand, node_stand_below, node_head, node_feet, pos local pitch, name, node_stand, node_stand_below, node_head, node_feet, pos
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
time = time + dtime time = time + dtime
-- Update jump status immediately since we need this info in real time. -- Update jump status immediately since we need this info in real time.
-- WARNING: This section is HACKY as hell since it is all just based on heuristics. -- WARNING: This section is HACKY as hell since it is all just based on heuristics.
for _,player in pairs(minetest.get_connected_players()) do for _,player in pairs(minetest.get_connected_players()) do
local controls = player:get_player_control() local controls = player:get_player_control()
name = player:get_player_name() name = player:get_player_name()
-- controls head bone -- controls head bone
pitch = degrees(player:get_look_vertical()) * -1 pitch = degrees(player:get_look_vertical()) * -1
if controls.LMB then if controls.LMB then
player:set_bone_position("Arm_Right_Pitch_Control", vector.new(-3,5.785,0), vector.new(pitch,0,0)) player:set_bone_position("Arm_Right_Pitch_Control", vector.new(-3,5.785,0), vector.new(pitch,0,0))
else else
player:set_bone_position("Arm_Right_Pitch_Control", vector.new(-3,5.785,0), vector.new(0,0,0)) player:set_bone_position("Arm_Right_Pitch_Control", vector.new(-3,5.785,0), vector.new(0,0,0))
end end
if controls.sneak and player:get_attach() == nil then if controls.sneak and player:get_attach() == nil then
-- controls head pitch when sneaking -- controls head pitch when sneaking
player:set_bone_position("Head", vector.new(0,6.3,0), vector.new(pitch+36,0,0)) player:set_bone_position("Head", vector.new(0,6.3,0), vector.new(pitch+36,0,0))
-- sets collisionbox, eye height, and nametag color accordingly -- sets eye height, and nametag color accordingly
if player:get_properties().collisionbox ~= {-0.35,0,-0.35,0.35,1.35,0.35} then player:set_properties({eye_height = 1.35, nametag_color = { r = 225, b = 225, a = 0, g = 225 }})
player:set_properties({collisionbox = {-0.35,0,-0.35,0.35,1.35,0.35}, eye_height = 1.35, nametag_color = { r = 255, b = 225, a = 0, g = 225 }})
end elseif minetest.get_item_group(mcl_playerinfo[name].node_stand, "water") ~= 0 then
elseif minetest.get_item_group(mcl_playerinfo[name].node_stand, "water") ~= 0 then -- controls head pitch when swiming
-- controls head pitch when swiming player:set_bone_position("Head", vector.new(0,6.3,0), vector.new(pitch+90,0,0))
player:set_bone_position("Head", vector.new(0,6.3,0), vector.new(pitch+90,0,0)) -- sets eye height, and nametag color accordingly
-- sets collisionbox, eye height, and nametag color accordingly player:set_properties({eye_height = 1.65, nametag_color = { r = 225, b = 225, a = 225, g = 225 }})
if player:get_properties().collisionbox ~= {-0.35,0.2,-0.35,0.35,1.8,0.35} then
player:set_properties({collisionbox = {-0.35,0.2,-0.35,0.35,1.8,0.35}, eye_height = 1.65, nametag_color = { r = 255, b = 225, a = 225, g = 225 }}) else
end -- controls head pitch when not sneaking
else player:set_bone_position("Head", vector.new(0,6.3,0), vector.new(pitch,0,0))
-- controls head pitch when not sneaking -- sets eye height, and nametag color accordingly
player:set_bone_position("Head", vector.new(0,6.3,0), vector.new(pitch,0,0)) player:set_properties({eye_height = 1.65, nametag_color = { r = 225, b = 225, a = 225, g = 225 }})
-- sets collisionbox, eye height, and nametag color accordingly end
if player:get_properties().collisionbox ~= {-0.35,0,-0.35,0.35,1.8,0.35} then
player:set_properties({collisionbox = {-0.35,0,-0.35,0.35,1.8,0.35}, eye_height = 1.65, nametag_color = { r = 255, b = 225, a = 225, g = 225 }}) if mcl_playerplus_internal[name].jump_cooldown > 0 then
end mcl_playerplus_internal[name].jump_cooldown = mcl_playerplus_internal[name].jump_cooldown - dtime
end end
if player:get_player_control().jump and mcl_playerplus_internal[name].jump_cooldown <= 0 then
if mcl_playerplus_internal[name].jump_cooldown > 0 then
mcl_playerplus_internal[name].jump_cooldown = mcl_playerplus_internal[name].jump_cooldown - dtime pos = player:get_pos()
end
if player:get_player_control().jump and mcl_playerplus_internal[name].jump_cooldown <= 0 then node_stand = mcl_playerinfo[name].node_stand
node_stand_below = mcl_playerinfo[name].node_stand_below
pos = player:get_pos() node_head = mcl_playerinfo[name].node_head
node_feet = mcl_playerinfo[name].node_feet
node_stand = mcl_playerinfo[name].node_stand if not node_stand or not node_stand_below or not node_head or not node_feet then
node_stand_below = mcl_playerinfo[name].node_stand_below return
node_head = mcl_playerinfo[name].node_head end
node_feet = mcl_playerinfo[name].node_feet if not minetest.registered_nodes[node_stand] or not minetest.registered_nodes[node_stand_below] or not minetest.registered_nodes[node_head] or not minetest.registered_nodes[node_feet] then
if not node_stand or not node_stand_below or not node_head or not node_feet then return
return end
end
if not minetest.registered_nodes[node_stand] or not minetest.registered_nodes[node_stand_below] or not minetest.registered_nodes[node_head] or not minetest.registered_nodes[node_feet] then -- Cause buggy exhaustion for jumping
return
end --[[ Checklist we check to know the player *actually* jumped:
* Not on or in liquid
-- Cause buggy exhaustion for jumping * Not on or at climbable
* On walkable
--[[ Checklist we check to know the player *actually* jumped: * Not on disable_jump
* Not on or in liquid FIXME: This code is pretty hacky and it is possible to miss some jumps or detect false
* Not on or at climbable jumps because of delays, rounding errors, etc.
* On walkable What this code *really* needs is some kind of jumping callback which this engine lacks
* Not on disable_jump as of 0.4.15.
FIXME: This code is pretty hacky and it is possible to miss some jumps or detect false ]]
jumps because of delays, rounding errors, etc.
What this code *really* needs is some kind of jumping callback which this engine lacks if minetest.get_item_group(node_feet, "liquid") == 0 and
as of 0.4.15. minetest.get_item_group(node_stand, "liquid") == 0 and
]] not minetest.registered_nodes[node_feet].climbable and
not minetest.registered_nodes[node_stand].climbable and
if minetest.get_item_group(node_feet, "liquid") == 0 and (minetest.registered_nodes[node_stand].walkable or minetest.registered_nodes[node_stand_below].walkable)
minetest.get_item_group(node_stand, "liquid") == 0 and and minetest.get_item_group(node_stand, "disable_jump") == 0
not minetest.registered_nodes[node_feet].climbable and and minetest.get_item_group(node_stand_below, "disable_jump") == 0 then
not minetest.registered_nodes[node_stand].climbable and -- Cause exhaustion for jumping
(minetest.registered_nodes[node_stand].walkable or minetest.registered_nodes[node_stand_below].walkable) if mcl_sprint.is_sprinting(name) then
and minetest.get_item_group(node_stand, "disable_jump") == 0 mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_SPRINT_JUMP)
and minetest.get_item_group(node_stand_below, "disable_jump") == 0 then else
-- Cause exhaustion for jumping mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_JUMP)
if mcl_sprint.is_sprinting(name) then end
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_SPRINT_JUMP)
else -- Reset cooldown timer
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_JUMP) mcl_playerplus_internal[name].jump_cooldown = 0.45
end end
end
-- Reset cooldown timer end
mcl_playerplus_internal[name].jump_cooldown = 0.45
end -- Run the rest of the code every 0.5 seconds
end if time < 0.5 then
end return
end
-- Run the rest of the code every 0.5 seconds
if time < 0.5 then -- reset time for next check
return -- FIXME: Make sure a regular check interval applies
end time = 0
-- reset time for next check -- check players
-- FIXME: Make sure a regular check interval applies for _,player in pairs(minetest.get_connected_players()) do
time = 0 -- who am I?
local name = player:get_player_name()
-- check players
for _,player in pairs(minetest.get_connected_players()) do -- where am I?
-- who am I? local pos = player:get_pos()
local name = player:get_player_name()
-- what is around me?
-- where am I? local node_stand = mcl_playerinfo[name].node_stand
local pos = player:get_pos() local node_stand_below = mcl_playerinfo[name].node_stand_below
local node_head = mcl_playerinfo[name].node_head
-- what is around me? local node_feet = mcl_playerinfo[name].node_feet
local node_stand = mcl_playerinfo[name].node_stand if not node_stand or not node_stand_below or not node_head or not node_feet then
local node_stand_below = mcl_playerinfo[name].node_stand_below return
local node_head = mcl_playerinfo[name].node_head end
local node_feet = mcl_playerinfo[name].node_feet
if not node_stand or not node_stand_below or not node_head or not node_feet then -- set defaults
return def.speed = 1
end
-- Standing on soul sand? If so, walk slower (unless player wears Soul Speed boots)
-- set defaults if node_stand == "mcl_nether:soul_sand" then
def.speed = 1 -- TODO: Tweak walk speed
-- TODO: Also slow down mobs
-- Standing on soul sand? If so, walk slower (unless player wears Soul Speed boots) -- Slow down even more when soul sand is above certain block
if node_stand == "mcl_nether:soul_sand" then local boots = player:get_inventory():get_stack("armor", 5)
-- TODO: Tweak walk speed local soul_speed = mcl_enchanting.get_enchantment(boots, "soul_speed")
-- TODO: Also slow down mobs if soul_speed > 0 then
-- Slow down even more when soul sand is above certain block playerphysics.add_physics_factor(player, "speed", "mcl_playerplus:surface", soul_speed * 0.105 + 1.3)
local boots = player:get_inventory():get_stack("armor", 5) else
local soul_speed = mcl_enchanting.get_enchantment(boots, "soul_speed") if node_stand_below == "mcl_core:ice" or node_stand_below == "mcl_core:packed_ice" or node_stand_below == "mcl_core:slimeblock" or node_stand_below == "mcl_core:water_source" then
if soul_speed > 0 then playerphysics.add_physics_factor(player, "speed", "mcl_playerplus:surface", 0.1)
playerphysics.add_physics_factor(player, "speed", "mcl_playerplus:surface", soul_speed * 0.105 + 1.3) else
else playerphysics.add_physics_factor(player, "speed", "mcl_playerplus:surface", 0.4)
if node_stand_below == "mcl_core:ice" or node_stand_below == "mcl_core:packed_ice" or node_stand_below == "mcl_core:slimeblock" or node_stand_below == "mcl_core:water_source" then end
playerphysics.add_physics_factor(player, "speed", "mcl_playerplus:surface", 0.1) end
else else
playerphysics.add_physics_factor(player, "speed", "mcl_playerplus:surface", 0.4) -- Reset speed decrease
end playerphysics.remove_physics_factor(player, "speed", "mcl_playerplus:surface")
end end
else
-- Reset speed decrease -- Is player suffocating inside node? (Only for solid full opaque cube type nodes
playerphysics.remove_physics_factor(player, "speed", "mcl_playerplus:surface") -- without group disable_suffocation=1)
end local ndef = minetest.registered_nodes[node_head]
-- Is player suffocating inside node? (Only for solid full opaque cube type nodes if (ndef.walkable == nil or ndef.walkable == true)
-- without group disable_suffocation=1) and (ndef.collision_box == nil or ndef.collision_box.type == "regular")
local ndef = minetest.registered_nodes[node_head] and (ndef.node_box == nil or ndef.node_box.type == "regular")
and (ndef.groups.disable_suffocation ~= 1)
if (ndef.walkable == nil or ndef.walkable == true) and (ndef.groups.opaque == 1)
and (ndef.collision_box == nil or ndef.collision_box.type == "regular") and (node_head ~= "ignore")
and (ndef.node_box == nil or ndef.node_box.type == "regular") -- Check privilege, too
and (ndef.groups.disable_suffocation ~= 1) and (not minetest.check_player_privs(name, {noclip = true})) then
and (ndef.groups.opaque == 1) if player:get_hp() > 0 then
and (node_head ~= "ignore") mcl_death_messages.player_damage(player, S("@1 suffocated to death.", name))
-- Check privilege, too player:set_hp(player:get_hp() - 1)
and (not minetest.check_player_privs(name, {noclip = true})) then end
if player:get_hp() > 0 then end
mcl_death_messages.player_damage(player, S("@1 suffocated to death.", name))
player:set_hp(player:get_hp() - 1) -- Am I near a cactus?
end local near = minetest.find_node_near(pos, 1, "mcl_core:cactus")
end if not near then
near = minetest.find_node_near({x=pos.x, y=pos.y-1, z=pos.z}, 1, "mcl_core:cactus")
-- Am I near a cactus? end
local near = minetest.find_node_near(pos, 1, "mcl_core:cactus") if near then
if not near then -- Am I touching the cactus? If so, it hurts
near = minetest.find_node_near({x=pos.x, y=pos.y-1, z=pos.z}, 1, "mcl_core:cactus") local dist = vector.distance(pos, near)
end local dist_feet = vector.distance({x=pos.x, y=pos.y-1, z=pos.z}, near)
if near then if dist < 1.1 or dist_feet < 1.1 then
-- Am I touching the cactus? If so, it hurts if player:get_hp() > 0 then
local dist = vector.distance(pos, near) mcl_death_messages.player_damage(player, S("@1 was prickled to death by a cactus.", name))
local dist_feet = vector.distance({x=pos.x, y=pos.y-1, z=pos.z}, near) player:set_hp(player:get_hp() - 1, { type = "punch", from = "mod" })
if dist < 1.1 or dist_feet < 1.1 then end
if player:get_hp() > 0 then end
mcl_death_messages.player_damage(player, S("@1 was prickled to death by a cactus.", name)) end
player:set_hp(player:get_hp() - 1, { type = "punch", from = "mod" })
end --[[ Swimming: Cause exhaustion.
end NOTE: As of 0.4.15, it only counts as swimming when you are with the feet inside the liquid!
end Head alone does not count. We respect that for now. ]]
if minetest.get_item_group(node_feet, "liquid") ~= 0 or
--[[ Swimming: Cause exhaustion. minetest.get_item_group(node_stand, "liquid") ~= 0 then
NOTE: As of 0.4.15, it only counts as swimming when you are with the feet inside the liquid! local lastPos = mcl_playerplus_internal[name].lastPos
Head alone does not count. We respect that for now. ]] if lastPos then
if minetest.get_item_group(node_feet, "liquid") ~= 0 or local dist = vector.distance(lastPos, pos)
minetest.get_item_group(node_stand, "liquid") ~= 0 then mcl_playerplus_internal[name].swimDistance = mcl_playerplus_internal[name].swimDistance + dist
local lastPos = mcl_playerplus_internal[name].lastPos if mcl_playerplus_internal[name].swimDistance >= 1 then
if lastPos then local superficial = math.floor(mcl_playerplus_internal[name].swimDistance)
local dist = vector.distance(lastPos, pos) mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_SWIM * superficial)
mcl_playerplus_internal[name].swimDistance = mcl_playerplus_internal[name].swimDistance + dist mcl_playerplus_internal[name].swimDistance = mcl_playerplus_internal[name].swimDistance - superficial
if mcl_playerplus_internal[name].swimDistance >= 1 then end
local superficial = math.floor(mcl_playerplus_internal[name].swimDistance) end
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_SWIM * superficial)
mcl_playerplus_internal[name].swimDistance = mcl_playerplus_internal[name].swimDistance - superficial end
end
end -- Underwater: Spawn bubble particles
if minetest.get_item_group(node_head, "water") ~= 0 then
end
minetest.add_particlespawner({
-- Underwater: Spawn bubble particles amount = 10,
if minetest.get_item_group(node_head, "water") ~= 0 then time = 0.15,
minpos = { x = -0.25, y = 0.3, z = -0.25 },
minetest.add_particlespawner({ maxpos = { x = 0.25, y = 0.7, z = 0.75 },
amount = 10, attached = player,
time = 0.15, minvel = {x = -0.2, y = 0, z = -0.2},
minpos = { x = -0.25, y = 0.3, z = -0.25 }, maxvel = {x = 0.5, y = 0, z = 0.5},
maxpos = { x = 0.25, y = 0.7, z = 0.75 }, minacc = {x = -0.4, y = 4, z = -0.4},
attached = player, maxacc = {x = 0.5, y = 1, z = 0.5},
minvel = {x = -0.2, y = 0, z = -0.2}, minexptime = 0.3,
maxvel = {x = 0.5, y = 0, z = 0.5}, maxexptime = 0.8,
minacc = {x = -0.4, y = 4, z = -0.4}, minsize = 0.7,
maxacc = {x = 0.5, y = 1, z = 0.5}, maxsize = 2.4,
minexptime = 0.3, texture = "mcl_particles_bubble.png"
maxexptime = 0.8, })
minsize = 0.7, end
maxsize = 2.4,
texture = "mcl_particles_bubble.png" -- Show positions of barriers when player is wielding a barrier
}) local wi = player:get_wielded_item():get_name()
end if wi == "mcl_core:barrier" or wi == "mcl_core:realm_barrier" then
local pos = vector.round(player:get_pos())
-- Show positions of barriers when player is wielding a barrier local r = 8
local wi = player:get_wielded_item():get_name() local vm = minetest.get_voxel_manip()
if wi == "mcl_core:barrier" or wi == "mcl_core:realm_barrier" then local emin, emax = vm:read_from_map({x=pos.x-r, y=pos.y-r, z=pos.z-r}, {x=pos.x+r, y=pos.y+r, z=pos.z+r})
local pos = vector.round(player:get_pos()) local area = VoxelArea:new{
local r = 8 MinEdge = emin,
local vm = minetest.get_voxel_manip() MaxEdge = emax,
local emin, emax = vm:read_from_map({x=pos.x-r, y=pos.y-r, z=pos.z-r}, {x=pos.x+r, y=pos.y+r, z=pos.z+r}) }
local area = VoxelArea:new{ local data = vm:get_data()
MinEdge = emin, for x=pos.x-r, pos.x+r do
MaxEdge = emax, for y=pos.y-r, pos.y+r do
} for z=pos.z-r, pos.z+r do
local data = vm:get_data() local vi = area:indexp({x=x, y=y, z=z})
for x=pos.x-r, pos.x+r do local nodename = minetest.get_name_from_content_id(data[vi])
for y=pos.y-r, pos.y+r do local tex
for z=pos.z-r, pos.z+r do if nodename == "mcl_core:barrier" then
local vi = area:indexp({x=x, y=y, z=z}) tex = "mcl_core_barrier.png"
local nodename = minetest.get_name_from_content_id(data[vi]) elseif nodename == "mcl_core:realm_barrier" then
local tex tex = "mcl_core_barrier.png^[colorize:#FF00FF:127^[transformFX"
if nodename == "mcl_core:barrier" then end
tex = "mcl_core_barrier.png" if tex then
elseif nodename == "mcl_core:realm_barrier" then minetest.add_particle({
tex = "mcl_core_barrier.png^[colorize:#FF00FF:127^[transformFX" pos = {x=x, y=y, z=z},
end expirationtime = 1,
if tex then size = 8,
minetest.add_particle({ texture = tex,
pos = {x=x, y=y, z=z}, glow = 14,
expirationtime = 1, playername = name
size = 8, })
texture = tex, end
glow = 14, end
playername = name end
}) end
end end
end
end -- Update internal values
end mcl_playerplus_internal[name].lastPos = pos
end
end
-- Update internal values
mcl_playerplus_internal[name].lastPos = pos end)
end -- set to blank on join (for 3rd party mods)
minetest.register_on_joinplayer(function(player)
end) local name = player:get_player_name()
-- set to blank on join (for 3rd party mods) mcl_playerplus_internal[name] = {
minetest.register_on_joinplayer(function(player) lastPos = nil,
local name = player:get_player_name() swimDistance = 0,
jump_cooldown = -1, -- Cooldown timer for jumping, we need this to prevent the jump exhaustion to increase rapidly
mcl_playerplus_internal[name] = { }
lastPos = nil, end)
swimDistance = 0,
jump_cooldown = -1, -- Cooldown timer for jumping, we need this to prevent the jump exhaustion to increase rapidly -- clear when player leaves
} minetest.register_on_leaveplayer(function(player)
end) local name = player:get_player_name()
-- clear when player leaves mcl_playerplus_internal[name] = nil
minetest.register_on_leaveplayer(function(player) end)
local name = player:get_player_name()
mcl_playerplus_internal[name] = nil
end)