Permalink
Browse files

Added 'deadly' and 'management' gamemodes.

  • Loading branch information...
rdococ committed Nov 8, 2017
1 parent b33f885 commit 6e9ce5d1543b0431195e951a630abef5cb7fc9f1
Showing with 72 additions and 26 deletions.
  1. +72 −26 init.lua
View
@@ -1,17 +1,34 @@
perplayer_gamemode = { users = {} }
perplayer_gamemode = { creative = {}, damage = {} }
dofile(minetest.get_modpath("perplayer_gamemode") .. "/ChatCmdBuilder.lua")
function perplayer_gamemode.is_enabled_for(name)
if perplayer_gamemode.users[name] == nil then
function perplayer_gamemode.is_in_creative_mode(name)
if perplayer_gamemode.creative[name] == nil then
return minetest.setting_getbool("creative_mode")
else
return perplayer_gamemode.users[name]
return perplayer_gamemode.creative[name]
end
end
function perplayer_gamemode.can_take_damage(name)
if perplayer_gamemode.creative[name] == nil then
return false
else
return perplayer_gamemode.damage[name]
end
end
function perplayer_gamemode.set_creative(name, v)
perplayer_gamemode.users[name] = v
perplayer_gamemode.creative[name] = v
local player = minetest.get_player_by_name(name)
if player then
local context = sfinv.contexts[name]
context.page = sfinv.get_homepage_name(player)
sfinv.set_player_inventory_formspec(player)
end
end
function perplayer_gamemode.set_damage(name, v)
perplayer_gamemode.damage[name] = v
local player = minetest.get_player_by_name(name)
if player then
@@ -22,61 +39,90 @@ function perplayer_gamemode.set_creative(name, v)
end
if creative.is_enabled_for then
creative.is_enabled_for = perplayer_gamemode.is_enabled_for
creative.is_enabled_for = perplayer_gamemode.is_in_creative_mode
else
for name, def in pairs(sfinv.pages) do
if #name > 9 and name:sub(1, 9) == "creative:" then
def.is_in_nav = function(self, player, context)
return perplayer_gamemode.is_enabled_for(player:get_player_name())
return perplayer_gamemode.is_in_creative_mode(player:get_player_name())
end
end
end
local old_homepage_name = sfinv.get_homepage_name
function sfinv.get_homepage_name(player)
if perplayer_gamemode.is_enabled_for(player:get_player_name()) then
if perplayer_gamemode.is_in_creative_mode(player:get_player_name()) then
return "creative:all"
else
return "sfinv:crafting"
end
end
end
minetest.register_privilege("gamemode", "Can set own creative mode")
minetest.register_privilege("gamemode_super", "Can set anyone's creative mode")
minetest.register_privilege("gamemode", "Can set own gamemode")
minetest.register_privilege("gamemode_super", "Can set anyone's gamemode")
local GamemodeNames = {}
-- Gamemodes with creative mode (infinite blocks)
GamemodeNames[true] = {}
GamemodeNames[true][false] = "creative" -- Free resources. Damage is disabled.
GamemodeNames[true][true] = "deadly" -- Free resources. Damage is enabled.
-- Gamemodes without creative mode
GamemodeNames[false] = {}
GamemodeNames[false][false] = "management" -- No free resources. Damage is disabled.
GamemodeNames[false][true] = "survival" -- No free resources. Damage is enabled.
local function is_creative(str)
return minetest.is_yes(str) or str == "creative"
return str:sub(1, 1) == "c" or str:sub(1, 1) == "d" or str:sub(1, 1) == "0"
end
local function is_damage(str)
return str:sub(1, 1) == "d" or str:sub(1, 1) == "s" or str:sub(1, 1) == "0"
end
perplayer_gamemode.ChatCmdBuilder.new("gamemode", function(cmd)
cmd:sub(":value", function(name, value)
if minetest.check_player_privs(name, { gamemode = true }) then
local v = is_creative(value)
perplayer_gamemode.set_creative(name, v)
if v then
return true, "Turned creative mode on"
else
return true, "Turned creative mode off"
end
local isCreative, isDamage = is_creative(value), is_damage(value)
perplayer_gamemode.set_creative(name, isCreative)
perplayer_gamemode.set_damage(name, isDamage)
return true, "Set gamemode to " .. GamemodeNames[isCreative][isDamage]
.. ". Creative = " .. tostring(isCreative) .. ", damage = " .. tostring(isDamage)
else
return false, "Missing privs: gamemode"
end
end)
cmd:sub(":username :value", function(name, username, value)
if minetest.check_player_privs(name, { gamemode_super = true }) then
local v = is_creative(value)
perplayer_gamemode.set_creative(username, v)
if v then
return true, "Turned creative mode on for " .. username
else
return true, "Turned creative mode off for " .. username
end
local isCreative, isDamage = is_creative(value), is_damage(value)
perplayer_gamemode.set_creative(username, isCreative)
perplayer_gamemode.set_damage(username, isDamage)
return true, "Set gamemode to " .. GamemodeNames[isCreative][isDamage]
.. ". Creative = " .. tostring(isCreative) .. ", damage = " .. tostring(isDamage)
else
return false, "Missing privs: gamemode_super"
end
end)
end, {
description = "Set game mode (creative or survival)"
description = "Set gamemode (creative or survival)"
})
minetest.register_on_player_hpchange(function (player, hp_change)
if not perplayer_gamemode.can_take_damage(player:get_player_name()) then
return 0, true
end
return hp_change
end, true)
if not minetest.setting_getbool("enable_damage") then
minetest.log("warning", "The enable_damage setting is disabled, meaning that "
.. GamemodeNames[true][true]
.. " and "
.. GamemodeNames[false][true]
.. " modes will not work as intended.")
end

0 comments on commit 6e9ce5d

Please sign in to comment.