Skip to content

Commit

Permalink
replace physics hell with hopefully better entity hell (#81)
Browse files Browse the repository at this point in the history
* replace physics hell with hopefully better entity hell

* toss worthless optional depend

* no need to save entity if we dont remove it for some reason - crashes, etc

* fix sofas

* handle the rest of chairs

* catch malformed seating, since lua tables are 1 indexed

* niklp suggestion
  • Loading branch information
wsor4035 authored Oct 31, 2024
1 parent 79416b9 commit 4089810
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 29 deletions.
96 changes: 68 additions & 28 deletions homedecor_seating/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

local S = minetest.get_translator("homedecor_seating")
local modpath = minetest.get_modpath("homedecor_seating")
local has_player_monoids = minetest.get_modpath("player_monoids")

lrfurn = {}

Expand Down Expand Up @@ -79,15 +78,62 @@ function lrfurn.fix_sofa_rotation_nsew(pos, placer, itemstack, pointed_thing)
minetest.swap_node(pos, { name = node.name, param2 = fdir+colorbits })
end

local physics_cache = {}
local seated_cache = {}

minetest.register_entity("homedecor_seating:seat", {
initial_properties = {
visual = "cube",
--comment out the following when testing so you can see it
textures = {"blank.png", "blank.png", "blank.png", "blank.png", "blank.png", "blank.png"},
collisionbox = { -0.01, -0.01, -0.01, 0.01, 0.01, 0.01 },
selectionbox = { -0.01, -0.01, -0.01, 0.01, 0.01, 0.01, rotate = false },
static_save = false,
},
on_punch = function(self)
self.object:remove()
end,
})

--we only care about 4 rotations, but just in case someone worldedits, etc - do something other than crash
--radians are stupid, using degrees and then converting
local p2r = {
0*math.pi/180,
0*math.pi/180, --correct
180*math.pi/180, --correct
90*math.pi/180, --correct
270*math.pi/180, --correct
0*math.pi/180,
0*math.pi/180,
0*math.pi/180,
}
p2r[0] = p2r[1]

local p2r_sofa = {
0*math.pi/180,
90*math.pi/180, --correct
270*math.pi/180, --correct
180*math.pi/180, --correct
0*math.pi/180, --correct
0*math.pi/180,
0*math.pi/180,
0*math.pi/180,
}
p2r_sofa[0] = p2r_sofa[1]

local p2r_facedir = {
[0] = 180*math.pi/180,
[1] = 90*math.pi/180,
[2] = 0*math.pi/180,
[3] = 270*math.pi/180,
}

function lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, seats)
if not clicker:is_player() then
return itemstack
end

local name = clicker:get_player_name()
if physics_cache[name] then --already sitting
if seated_cache[name] then --already sitting
lrfurn.stand(clicker)
return itemstack
end
Expand Down Expand Up @@ -132,41 +178,35 @@ function lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, seats)
--seat the player
clicker:set_pos(sit_pos)

xcompat.player.player_attached[name] = true
xcompat.player.set_animation(clicker, "sit", 0)
if has_player_monoids then
physics_cache[name] = true
player_monoids.speed:add_change(clicker, 0, "homedecor_seating:sit")
player_monoids.jump:add_change(clicker, 0, "homedecor_seating:sit")
player_monoids.gravity:add_change(clicker, 0, "homedecor_seating:sit")
local entity = minetest.add_entity(sit_pos, "homedecor_seating:seat")
if not entity then return itemstack end --catch for when the entity fails to spawn just in case

clicker:set_attach(entity, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0}, true)
local nodedef = minetest.registered_nodes[node.name]
if nodedef.paramtype2 == "facedir" then
entity:set_rotation({x = 0, y = p2r_facedir[node.param2 % 4], z = 0})
elseif string.find(node.name, "sofa") then
entity:set_rotation({x = 0, y = p2r_sofa[node.param2 % 8], z = 0})
else
physics_cache[name] = table.copy(clicker:get_physics_override())
clicker:set_physics_override({speed = 0, jump = 0, gravity = 0})
entity:set_rotation({x = 0, y = p2r[node.param2 % 8], z = 0})
end

xcompat.player.player_attached[name] = true
xcompat.player.set_animation(clicker, "sit", 0)
seated_cache[name] = true

return itemstack
end

function lrfurn.stand(clicker)
local name = clicker:get_player_name()
xcompat.player.player_attached[name] = false
if physics_cache[name] then
if has_player_monoids then
player_monoids.speed:del_change(clicker, "homedecor_seating:sit")
player_monoids.jump:del_change(clicker, "homedecor_seating:sit")
player_monoids.gravity:del_change(clicker, "homedecor_seating:sit")
else
clicker:set_physics_override(physics_cache[name])
end
physics_cache[name] = nil
else --in case this is called and the cache is empty
if has_player_monoids then
player_monoids.speed:del_change(clicker, "homedecor_seating:sit")
player_monoids.jump:del_change(clicker, "homedecor_seating:sit")
player_monoids.gravity:del_change(clicker, "homedecor_seating:sit")
else
clicker:set_physics_override({speed = 1, jump = 1, gravity = 1})
if seated_cache[name] then
local attached_to = clicker:get_attach()
if attached_to then --check, a stupid clearobjects might have been called, etc
attached_to:remove() --removing also detaches
end
seated_cache[name] = nil
end
end

Expand Down
2 changes: 1 addition & 1 deletion homedecor_seating/mod.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = homedecor_seating
description = Homedecor mod: seating
depends = homedecor_common
optional_depends = screwdriver, wool, default, unifieddyes, basic_materials, player_monoids
optional_depends = screwdriver, wool, default, unifieddyes, basic_materials

1 comment on commit 4089810

@MCLV-pandorabox
Copy link

@MCLV-pandorabox MCLV-pandorabox commented on 4089810 Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entity hell seems to work better. Thanks
Still getting hurt by dangerous node on chair.y+2
Still able to move around while seated, but I have to die on a chair for that one now.

chair.y+2
I mean, if you put a simple homedecor:armchair on position 0, then put default:stone on position 1 above that. That is obviously a big outchy. But if you place another default:stone on top of that (position 2 eg chair.y+2) and remove the stone on position 1. you should have enough clearance and not be hurt. This was the case before you introduced the new seating arrangement. and a lot of in-game builds kinda depend on that behavior I think. But I don't know many other servers than Pandorabox

Please sign in to comment.