Skip to content

Commit

Permalink
Merge pull request #77 from SwissalpS/compass
Browse files Browse the repository at this point in the history
Compass and more intuitive bookmark reading
  • Loading branch information
SwissalpS committed Oct 13, 2020
2 parents c41280e + 1acb3b9 commit e4a61fd
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 67 deletions.
165 changes: 103 additions & 62 deletions bookmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,76 +32,117 @@ jumpdrive.write_to_book = function(pos, sender)

end

local function has_nil(pos)
if nil == pos
or nil == pos.x
or nil == pos.y
or nil == pos.z
then
return true
end
return false
end

local function sanitize_and_set_coordinates(meta, pos)
meta:set_int("x", jumpdrive.sanitize_coord(pos.x))
meta:set_int("y", jumpdrive.sanitize_coord(pos.y))
meta:set_int("z", jumpdrive.sanitize_coord(pos.z))
end

jumpdrive.read_from_book = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local player_name = meta:get_string("owner")

if inv:contains_item("main", {name="default:book_written", count=1}) then
local stack = inv:remove_item("main", {name="default:book_written", count=1})
local stackMeta = stack:get_meta()

local text = stackMeta:get_string("text")
local data = minetest.deserialize(text)

if data == nil then
-- put book back, it may contain other information
inv:add_item("main", stack)
-- alert player
if nil ~= player_name then
minetest.chat_send_player(player_name, "Invalid data")
local inv_size = inv:get_size("main")
local stack
local stack_name
local stack_meta
local text
local target_pos
for i = inv_size, 1, -1 do
stack = inv:get_stack("main", i)
stack_name = stack:get_name()
if "default:book_written" == stack_name then
-- remove item from inventory
inv:set_stack("main", i, ItemStack())
stack_meta = stack:get_meta()
text = stack_meta:get_string("text")
local data = minetest.deserialize(text)
if has_nil(data) then
-- put book back where it was, it may contain other information
inv:set_stack("main", i, stack)
else

target_pos = {
x = tonumber(data.x),
y = tonumber(data.y),
z = tonumber(data.z)
}

if has_nil(target_pos) then
-- put book back where it was, it may contain other information
inv:set_stack("main", i, stack)
-- alert player
if nil ~= player_name then
minetest.chat_send_player(player_name, "Invalid coordinates")
end
return
end
sanitize_and_set_coordinates(meta, target_pos)
-- put book back to next free slot
inv:add_item("main", stack)
return
end
return
end

local x = tonumber(data.x)
local y = tonumber(data.y)
local z = tonumber(data.z)

if x == nil or y == nil or z == nil then
-- put book back, it may contain other information
inv:add_item("main", stack)
-- alert player
if nil ~= player_name then
minetest.chat_send_player(player_name, "Invalid coordinates")
elseif "missions:wand_position" == stack_name then
-- remove item from inventory
inv:set_stack("main", i, ItemStack())
stack_meta = stack:get_meta()

text = stack_meta:get_string("pos")
target_pos = minetest.string_to_pos(text)

if has_nil(target_pos) then
-- put wand back where it was.
-- In singleplayer/creative you can get an invalid position wand
inv:set_stack("main", i, stack)
else
-- don't know how you could get unsanitary coords in a wand,
-- let's just be safe
sanitize_and_set_coordinates(meta, target_pos)
-- put wand back to next free slot
inv:add_item("main", stack)
return
end
return
end

meta:set_int("x", jumpdrive.sanitize_coord(x))
meta:set_int("y", jumpdrive.sanitize_coord(y))
meta:set_int("z", jumpdrive.sanitize_coord(z))

-- put book back
inv:add_item("main", stack)
elseif inv:contains_item("main", {name="missions:wand_position", count=1}) then
local stack = inv:remove_item("main", {name="missions:wand_position", count=1})
local stackMeta = stack:get_meta()

local text = stackMeta:get_string("pos")
local target_pos = minetest.string_to_pos(text)

if nil == target_pos then
-- put wand back, I don't see a way to corrupt a wand atm
inv:add_item("main", stack)
return
end

local x = target_pos.x
local y = target_pos.y
local z = target_pos.z

if x == nil or y == nil or z == nil then
-- put wand back, I don't see a way to corrupt a wand atm
inv:add_item("main", stack)
return
end

meta:set_int("x", jumpdrive.sanitize_coord(x))
meta:set_int("y", jumpdrive.sanitize_coord(y))
meta:set_int("z", jumpdrive.sanitize_coord(z))

-- put wand back
inv:add_item("main", stack)
elseif "ccompass:" == stack_name:sub(1, 9)
or "compass:" == stack_name:sub(1, 8) then
-- remove item from inventory
inv:set_stack("main", i, ItemStack())
stack_meta = stack:get_meta()

text = stack_meta:get_string("target_pos")
target_pos = minetest.string_to_pos(text)

if has_nil(target_pos) then
-- put compass back, it is probably not calibrated
-- we put it at same position as we did not actually use it
inv:set_stack("main", i, stack)
else
sanitize_and_set_coordinates(meta, target_pos)
-- put compass back to next free slot
inv:add_item("main", stack)
return
end
end -- switch item type
end -- loop inventory
-- if we got here, there was nothing.
-- should we or should we not message user?
--[[
if nil ~= player_name then
minetest.chat_send_player(player_name, "No valid bookmark item found.")
end
--]]
end

3 changes: 2 additions & 1 deletion engines/area_formspec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jumpdrive.update_area_formspec = function(meta, pos)
"button[6,1;2,1;reset;Reset]" ..

"button[0,2;4,1;write_book;Write to book]" ..
"button[4,2;4,1;read_book;Read from book]" ..
"button[4,2;4,1;read_book;Read from bookmark]" ..

-- main inventory for fuel and books
"list[context;main;0,3.25;8,1;]" ..
Expand All @@ -43,3 +43,4 @@ jumpdrive.update_area_formspec = function(meta, pos)

meta:set_string("formspec", formspec)
end

3 changes: 2 additions & 1 deletion engines/default_formspec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jumpdrive.update_formspec = function(meta, pos)
"button[6,1;2,1;reset;Reset]" ..

"button[0,2;4,1;write_book;Write to book]" ..
"button[4,2;4,1;read_book;Read from book]" ..
"button[4,2;4,1;read_book;Read from bookmark]" ..

-- main inventory for fuel and books
"list[context;main;0,3.25;8,1;]" ..
Expand All @@ -46,3 +46,4 @@ jumpdrive.update_formspec = function(meta, pos)

meta:set_string("formspec", formspec)
end

3 changes: 2 additions & 1 deletion fleet/fleet_formspec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jumpdrive.fleet.update_formspec = function(meta, pos)
button_line ..

"button[0,2.5;4,1;write_book;Write to book]" ..
"button[4,2.5;4,1;read_book;Read from book]" ..
"button[4,2.5;4,1;read_book;Read from bookmark]" ..

"list[context;main;0,3.75;8,1;]" ..

Expand All @@ -38,3 +38,4 @@ jumpdrive.fleet.update_formspec = function(meta, pos)
-- listring stuff
"listring[]")
end

11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Optional dependencies:
* Beds (thx to @tuedel)
* Ropes (thx to @tuedel)
* Mission-wand as coordinate bookmark (thx to @SwissalpS)
* Compass as coordinate bookmark (thx to @SwissalpS)
* Areas
* Drawers

Expand Down Expand Up @@ -81,8 +82,13 @@ Example:

## Coordinate bookmarking

You can place empty books into the drive inventory and write the coordinates to it with the "Write to book" button
The "Read from book" reads the coordinates from the next book in the inventory
You can place empty books into the drive inventory and write the coordinates to them with the "Write to book" button.
The "Read from bookmark" button reads the coordinates from the next valid bookmark item in the inventory. From right to left.
A used bookmark item is placed in the first free slot from the left.
Bookmark items are:
* Written books saved by jumpdrive (or correctly by hand)
* Mission position wands
* Compasses

## Diglines

Expand Down Expand Up @@ -169,3 +175,4 @@ end

* Initial version
* Cascade operation (with issues)

0 comments on commit e4a61fd

Please sign in to comment.