diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/common.lua | 24 | ||||
-rw-r--r-- | src/common/random.lua | 40 | ||||
-rw-r--r-- | src/common/request.lua | 61 |
3 files changed, 125 insertions, 0 deletions
diff --git a/src/common/common.lua b/src/common/common.lua new file mode 100644 index 0000000..2288a4d --- /dev/null +++ b/src/common/common.lua @@ -0,0 +1,24 @@ +function skycraft.get_far_node(pos) + local node = minetest.get_node(pos) + if node.name ~= "ignore" then + return node + end + minetest.get_voxel_manip():read_from_map(pos, pos) + return minetest.get_node(pos) +end + +function skycraft.find_free_position_near(pos) + local tries = { + {x = 1, y = 0, z = 0}, + {x = -1, y = 0, z = 0}, + {x = 0, y = 0, z = 1}, + {x = 0, y = 0, z = -1}, + } + for _, d in pairs(tries) do + local p = vector.add(pos, d) + if not minetest.registered_nodes[minetest.get_node(p).name].walkable then + return p, true + end + end + return pos, false +end diff --git a/src/common/random.lua b/src/common/random.lua new file mode 100644 index 0000000..4fbbed3 --- /dev/null +++ b/src/common/random.lua @@ -0,0 +1,40 @@ +skycraft.random = { + choices = {}, + probabilities = {}, + csum = {}, + sum = 0 +} + +skycraft.random.__index = skycraft.random + +function skycraft.random:new(o) + o = o or {} + setmetatable(o, self) + o.choices = {} + o.probabilities = {} + o.csum = {} + o.sum = 0 + return o +end + +function skycraft.random:calc_csum() + self.sum = 0 + for i, choice in ipairs(self.choices) do + self.sum = self.sum + self.probabilities[choice] + self.csum[choice] = self.sum + end +end + +function skycraft.random:choose() + local r = math.random() + math.random(0, self.sum - 1) + for i, choice in pairs(self.choices) do + if r < self.csum[choice] then + return choice + end + end +end + +function skycraft.random:add_choice(choice, probability) + table.insert(self.choices, choice) + self.probabilities[choice] = probability +end diff --git a/src/common/request.lua b/src/common/request.lua new file mode 100644 index 0000000..0b11163 --- /dev/null +++ b/src/common/request.lua @@ -0,0 +1,61 @@ +function skycraft.register_request_system(sysname, action, progressive, preposition, func) + local action_capital = (action:sub(1, 1)):upper() .. action:sub(2) + + local request_list = {} + + minetest.register_on_leaveplayer(function(name) + request_list[name] = nil + end) + + minetest.register_chatcommand(sysname, { + description = "Request to " .. action .. " " .. preposition .. " another player", + params = "<player>", + privs = {skycraft = true}, + func = function(name, param) + if param == "" then + return false, "Usage: /" .. sysname .. " <player>" + end + if not minetest.get_player_by_name(param) then + return false, "There is no player by that name. Keep in mind this is case-sensitive, and the player must be online" + end + request_list[param] = name + minetest.after(60, function() + if request_list[param] then + minetest.chat_send_player(name, "Request timed-out.") + minetest.chat_send_player(param, "Request timed-out.") + request_list[param] = nil + end + end) + minetest.chat_send_player(param, name .. " is requesting to " .. action .. " " .. preposition .. " you. /" .. sysname .. "accept to accept") + return true, action_capital .. " request sent! It will timeout in 60 seconds." + end + }) + + minetest.register_chatcommand(sysname .. "accept", { + description = "Accept " .. action .. " request from another player", + privs = {skycraft = true}, + func = function(name) + if not minetest.get_player_by_name(name) then return false, "You have to be online to use this command" end + local other = request_list[name] + if not other then return false, "Usage: /" .. sysname .. "accept allows you to accept " .. action .. " requests sent to you by other players" end + if not minetest.get_player_by_name(other) then return false, other .. " doesn't exist, or just disconnected/left (by timeout)." end + minetest.chat_send_player(other, action_capital .. " request accepted!") + func(name, other) + request_list[name] = nil + return true, other .. " is " .. progressive .. " " .. preposition .. " you." + end + }) + + minetest.register_chatcommand(sysname .. "deny", { + description = "Deny " .. action .." request from another player", + privs = {skycraft = true}, + func = function(name) + local other = request_list[name] + if not other then return false, "Usage: /" .. sysname .. "deny allows you to deny " .. action .. " requests sent to you by other players." end + minetest.chat_send_player(other, action_capital .. " request denied.") + request_list[name] = nil + return false, "You denied the " .. action .. " request " .. other .. " sent you." + end + }) + +end |