diff --git a/README.md b/README.md index b64b41f..180694d 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ When the guest accepts: * The guest's spawn will be set to the host's spawn position * The guest will be transported to their new spawn immediately -This effect is permanent ; the guest cannot "undo" to retrieve their old spawn. The guest should cleanup their old spawn before leaving it. Server admins may wish to allow extended transportable inventory, like bags or ender chests, to facilitate such transitions. +The guest can return to their original spawn by running `/spawn original` ; they will themselves be charged the same levvy. ## Settings diff --git a/init.lua b/init.lua index 5d5abe2..5b2ac31 100644 --- a/init.lua +++ b/init.lua @@ -67,7 +67,8 @@ end function rspawn:newspawn(pos, radius) -- Given a seed position and a radius, find an exact spawn location - -- that is walkable and with 2 air nodes above it + -- that is an air node, walkable under it, non-walkable over it + -- bright during the day, and not leaves rspawn:debug("Trying somewhere around "..minetest.pos_to_string(pos)) @@ -135,8 +136,6 @@ function rspawn:set_player_spawn(name, newpos) end local function register_original_spawn(playername, pos) - -- We say players cannot find their original spawn after invite - -- That is the intended behaviour ; however we should still record this. Just in case. if not rspawn.playerspawns["original spawns"] then rspawn.playerspawns["original spawns"] = {} end diff --git a/src/commands.lua b/src/commands.lua index 597b15e..2a22cb7 100644 --- a/src/commands.lua +++ b/src/commands.lua @@ -8,7 +8,7 @@ minetest.register_privilege("setspawn", "Can manually set a spawn point") minetest.register_privilege("newspawn", "Can get a new randomized spawn position.") minetest.register_privilege("spawnadmin", "Can clean up timers and set new spawns for players.") --- Splitter +-- Support functions local function splitstring(sdata, sep) local idx @@ -30,11 +30,38 @@ local function splitstring(sdata, sep) return tdata end +local function set_original_spawn(tname) + local tpos = rspawn.playerspawns["original spawns"][tname] + + if not tpos then + minetest.chat_send_player(tname, "Could not find your original spawn!") + + elseif rspawn:consume_levvy(minetest.get_player_by_name(tname)) then + rspawn:set_player_spawn(tname, tpos) + else + minetest.chat_send_player(tname, "You do not have enough to pay the levvy. Aborting.") + end +end + +local function request_new_spawn(username, targetname) + local timername = username + if targetname ~= username then + timername = username.." "..targetname + end + + if not newspawn_cooldown[timername] then + rspawn:renew_player_spawn(targetname) + newspawn_cooldown[timername] = 300 + else + minetest.chat_send_player(username, tostring(math.ceil(newspawn_cooldown[timername])).."sec until you can randomize a new spawn for "..targetname) + end +end + -- Commands minetest.register_chatcommand("spawn", { - description = "Teleport to spawn position, or manage invitations. See you current invitation with '/spawn invite'", - params = "[ invite [] | accept | decline ]", + description = "Teleport to spawn position, or manage invitations. See you current invitation with '/spawn invite'. If you are a guest at a spawn, return to your orgiinal spawn with '/spawn original'", + params = "[ invite [] | accept | decline | original ]", privs = "spawn", func = function(name, args) local target = rspawn.playerspawns[name] @@ -52,20 +79,22 @@ minetest.register_chatcommand("spawn", { elseif args[1] == "accept" then rspawn.invites:accept(name) - -- TODO, only one at a time, must be accepted or declined, and DO move player - not to be used lightly return elseif args[1] == "decline" then - rspawn.invites:decline(name) -- TODO, free up invitation slot + rspawn.invites:decline(name) return + elseif args[1] == "original" then + set_original_spawn(name) + elseif args[1] == "invite" then if #args == 2 then - rspawn.invites:invite_player_fromto(name, args[2]) -- TODO + rspawn.invites:invite_player_fromto(name, args[2]) return elseif #args == 1 then - rspawn.invites:show_invite_for(name) -- TODO + rspawn.invites:show_invite_for(name) return end @@ -86,20 +115,6 @@ minetest.register_chatcommand("setspawn", { end }) -local function request_new_spawn(username, targetname) - local timername = username - if targetname ~= username then - timername = username.." "..targetname - end - - if not newspawn_cooldown[timername] then - rspawn:renew_player_spawn(targetname) - newspawn_cooldown[timername] = 300 - else - minetest.chat_send_player(username, tostring(math.ceil(newspawn_cooldown[timername])).."sec until you can randomize a new spawn for "..targetname) - end -end - minetest.register_chatcommand("newspawn", { description = "Randomly select a new spawn position.", params = "", diff --git a/src/data.lua b/src/data.lua index 48e8262..91f6557 100644 --- a/src/data.lua +++ b/src/data.lua @@ -1,5 +1,22 @@ local spawnsfile = minetest.get_worldpath().."/dynamicspawns.lua.ser" +-- Comatibility with old behaviour - players whose original spawns had not been registered receive the one they are now using +local function reconcile_original_spawns() + if not rspawn.playerspawns["original spawns"] then + rspawn.playerspawns["original spawns"] = {} + end + + for playername,spawnpos in pairs(rspawn.playerspawns) do + if playername ~= "pre gen" and playername ~= "original spawns" then + if not rspawn.playerspawns["original spawns"][playername] then + rspawn.playerspawns["original spawns"][playername] = rspawn.playerspawns[playername] + end + end + end + + rspawn:spawnsave() +end + function rspawn:spawnsave() local serdata = minetest.serialize(rspawn.playerspawns) if not serdata then @@ -30,6 +47,8 @@ function rspawn:spawnload() pregens = rspawn.playerspawns["pre gen"] or {} rspawn.playerspawns["pre gen"] = pregens + reconcile_original_spawns() + minetest.debug("Loaded rspawn data with "..tostring(#pregens).." pregen nodes") end diff --git a/src/invites.lua b/src/invites.lua index a966313..1acc9af 100644 --- a/src/invites.lua +++ b/src/invites.lua @@ -60,7 +60,7 @@ function rspawn.invites:invite_player_fromto(hostname, guestname) local hostspawn_s = minetest.pos_to_string(rspawn.playerspawns[hostname]) - minetest.chat_send_player(guestname, hostname.." invited you to join their spawn point.\nIf you accept, your spawn point will be set to "..hostspawn_s.." and you will be taken there immediately.\n WARNING: This CANNOT BE UNDONE.\n\nRun '/spawn accept' to accept, '/spawn decline' to decline and clear the invite.") + minetest.chat_send_player(guestname, hostname.." invited you to join their spawn point.\nIf you accept, your spawn point will be set to "..hostspawn_s.." and you will be taken there IMMEDIATELY.\nRun '/spawn accept' to accept, '/spawn decline' to decline and clear the invite.") minetest.chat_send_player(hostname, "You have invited "..guestname.." to join your spawn.\nIf they accept, you will be charged \n\n "..levvy_qtty.." "..levvy_nicename.." \n\nwhich will be taken from your inventory." @@ -106,7 +106,7 @@ local function find_levvy(player) return false end -local function consume_levvy(player) +function rspawn:consume_levvy(player) if not player then minetest.log("action", "Tried to access undefined player") return false @@ -160,9 +160,14 @@ function rspawn.invites:accept(guestname) return end - if consume_levvy(minetest.get_player_by_name(hostname) ) then -- Systematically notifies host if they don't have enough + if rspawn:consume_levvy(minetest.get_player_by_name(hostname) ) then -- Systematically notifies host if they don't have enough local hostspawn = rspawn.playerspawns[hostname] rspawn:set_player_spawn(guestname, hostspawn) -- sets new spawn position, saves, teleports player + local success_message = " has accepted the spawn invitation from " + minetest.chat_send_player(guestname, guestname..success_message..hostname) + minetest.chat_send_player(hostname, guestname..success_message..hostname) + + minetest.chat_send_player(guestname, "You can return to your original spawn using '/spawn original' for \n\n "..levvy_qtty.." "..levvy_nicename.." \n\nwhich will be taken from your inventory.") else -- Host was notified, now notify guest minetest.chat_send_player(guestname, hostname.." was unable to pay the levvy. Invitation could not be accepted.")