1
0
mirror of https://github.com/mt-mods/homedecor_modpack.git synced 2025-02-02 11:00:17 +01:00

Jump drive support for seats (#87)

* psuedo code for jump drive support

* avoid re-calculating node hash in loop

* cache new position for next move

* clean comment and minetest -> core

* cache seat offset for sofas

* use first found sitting position

* cleanup some comments

* bugfix: reported seat occupied when it wasn't

---------

Co-authored-by: Luke aka SwissalpS <Luke@SwissalpS.ws>
This commit is contained in:
wsor4035 2024-12-22 13:59:17 -05:00 committed by GitHub
parent 94e25c2192
commit b51d27135b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 5 deletions

View File

@ -35,6 +35,7 @@ minetest.register_node(":lrfurn:armchair", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
homedecor.register("armchair", { homedecor.register("armchair", {
@ -62,6 +63,7 @@ homedecor.register("armchair", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
-- crafts -- crafts

View File

@ -79,6 +79,7 @@ function lrfurn.fix_sofa_rotation_nsew(pos, placer, itemstack, pointed_thing)
end end
local seated_cache = {} local seated_cache = {}
local offset_cache = {}
minetest.register_entity("homedecor_seating:seat", { minetest.register_entity("homedecor_seating:seat", {
initial_properties = { initial_properties = {
@ -161,14 +162,19 @@ function lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, seats)
--see if we can find a non occupied seat --see if we can find a non occupied seat
local sit_pos local sit_pos
local sit_hash
for hash, spos in pairs(valid_seats) do for hash, spos in pairs(valid_seats) do
local pstatus = false local pstatus = false
for _, ref in pairs(minetest.get_objects_inside_radius(spos, 0.5)) do for _, ref in pairs(minetest.get_objects_inside_radius(spos, 0.5)) do
if ref:is_player() then if ref:is_player() and seated_cache[ref:get_player_name()] then
pstatus = true pstatus = true
end end
end end
if not pstatus then sit_pos = spos end if not pstatus then
sit_pos = spos
sit_hash = hash
break;
end
end end
if not sit_pos then if not sit_pos then
minetest.chat_send_player(name, "sorry, this seat is currently occupied") minetest.chat_send_player(name, "sorry, this seat is currently occupied")
@ -194,6 +200,9 @@ function lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, seats)
xcompat.player.player_attached[name] = true xcompat.player.player_attached[name] = true
xcompat.player.set_animation(clicker, "sit", 0) xcompat.player.set_animation(clicker, "sit", 0)
seated_cache[name] = minetest.hash_node_position(pos) seated_cache[name] = minetest.hash_node_position(pos)
if seated_cache[name] ~= sit_hash then
offset_cache[name] = core.hash_node_position(vector.subtract(pos, sit_pos))
end
return itemstack return itemstack
end end
@ -203,14 +212,18 @@ function lrfurn.stand(clicker)
xcompat.player.player_attached[name] = false xcompat.player.player_attached[name] = false
if seated_cache[name] then if seated_cache[name] then
local attached_to = clicker:get_attach() local attached_to = clicker:get_attach()
if attached_to then --check, a stupid clearobjects might have been called, etc -- Check, clearobjects might have been called, etc
attached_to:remove() --removing also detaches if attached_to then
-- Removing also detaches
attached_to:remove()
end end
seated_cache[name] = nil seated_cache[name] = nil
offset_cache[name] = nil
end end
end end
function lrfurn.on_seat_destruct(pos) --called when a seat is destroyed -- Called when a seat is destroyed
function lrfurn.on_seat_destruct(pos)
for name, seatpos in pairs(seated_cache) do for name, seatpos in pairs(seated_cache) do
if seatpos == minetest.hash_node_position(pos) then if seatpos == minetest.hash_node_position(pos) then
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
@ -221,6 +234,30 @@ function lrfurn.on_seat_destruct(pos) --called when a seat is destroyed
end end
end end
function lrfurn.on_seat_movenode(from_pos, to_pos)
local hashed_from_pos = core.hash_node_position(from_pos)
local hashed_to_pos = core.hash_node_position(to_pos)
for name, seatpos in pairs(seated_cache) do
if seatpos == hashed_from_pos then
local player = core.get_player_by_name(name)
if player then
local attached_to = player:get_attach()
-- Check, clearobjects might have been called, etc
if attached_to then
if offset_cache[name] then
-- multi-seat node aka sofas
attached_to:set_pos(vector.subtract(to_pos,
core.get_position_from_hash(offset_cache[name])))
else
attached_to:set_pos(to_pos)
end
seated_cache[name] = hashed_to_pos
end
end
end
end
end
--if the player gets killed in the seat, handle it --if the player gets killed in the seat, handle it
minetest.register_on_dieplayer(function(player) minetest.register_on_dieplayer(function(player)
if seated_cache[player:get_player_name()] then if seated_cache[player:get_player_name()] then

View File

@ -49,6 +49,7 @@ minetest.register_node(":lrfurn:longsofa", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 3) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 3)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
minetest.register_craft({ minetest.register_craft({

View File

@ -24,6 +24,7 @@ homedecor.register("deckchair", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
minetest.register_alias("homedecor:deckchair_foot", "homedecor:deckchair") minetest.register_alias("homedecor:deckchair_foot", "homedecor:deckchair")
@ -45,6 +46,7 @@ homedecor.register("deckchair_striped_blue", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
homedecor.register("simple_bench", { homedecor.register("simple_bench", {
@ -66,6 +68,7 @@ homedecor.register("simple_bench", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
local bl1_sbox = { local bl1_sbox = {
@ -159,6 +162,7 @@ homedecor.register("kitchen_chair_wood", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
homedecor.register("kitchen_chair_padded", { homedecor.register("kitchen_chair_padded", {
@ -186,6 +190,7 @@ homedecor.register("kitchen_chair_padded", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
local ofchairs_sbox = { local ofchairs_sbox = {
@ -226,6 +231,7 @@ for _, c in pairs(chairs) do
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 1)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
end end

View File

@ -49,6 +49,7 @@ minetest.register_node(":lrfurn:sofa", {
return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 2) return lrfurn.sit(pos, node, clicker, itemstack, pointed_thing, 2)
end, end,
on_destruct = lrfurn.on_seat_destruct, on_destruct = lrfurn.on_seat_destruct,
on_movenode = lrfurn.on_seat_movenode,
}) })
minetest.register_craft({ minetest.register_craft({