1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-11-08 19:40:21 +01:00

Reorganized pclasses and tweaked sprint bar

- Tweaked sprint bar in order to change its maximum value according to the one defined for each player
 - Reorganized pclasses into multiple files (api, init, one for each class). Next thing to do : document/simplify api
This commit is contained in:
LeMagnesium 2015-07-27 19:13:30 +02:00
parent 403895e8e4
commit 0754d7a5aa
7 changed files with 258 additions and 218 deletions

View File

@ -328,6 +328,7 @@ local function update_hud(player)
--health --health
hb.change_hudbar(player, "health", player:get_hp()) hb.change_hudbar(player, "health", player:get_hp())
end end
hb.change_hudbar(player, "sprint", sprint.players[player:get_player_name()].stamina, sprint.players[player:get_player_name()].maxStamina)
end end
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)

View File

@ -0,0 +1,11 @@
-----------------------------
-- Default class assignment
--
pclasses.api.register_class("adventurer", {
determination = function() return true end,
on_assigned = function(pname)
minetest.chat_send_player(pname, "You are now an adventurer")
end
})

161
mods/pclasses/api.lua Normal file
View File

@ -0,0 +1,161 @@
------------------
-- PClasses' API
--
-- Various utility functions
-- Register the class (basic registration)
function pclasses.api.register_class(cname, def)
if not cname then
minetest.log("error", "[PClasses] Error registering unamed class")
return
elseif not def then
minetest.log("error", "[PClasses] Error registering class " ..
cname .. ". Reason : no definition table.")
return
elseif not def.determination then
minetest.log("error", "[PClasses] Error registreing class " ..
cname .. ". Reason : no determination function.")
return
end
pclasses.classes[cname] = def
return true
end
------------------------
-- Getters and Setters
--
-- Get class specs by name
function pclasses.api.get_class_by_name(cname)
return pclasses.classes[cname]
end
-- Get single player
function pclasses.api.get_player_class(pname)
return pclasses.datas.players[pname]
end
-- Get all players for a class
function pclasses.api.get_class_players(cname)
local pnames = {}
if pclasses.api.get_class_by_name(cname) then
for p,c in ipairs(pclasses.datas.players) do
if c == cname then
table.insert(pnames, table.getn(pnames)+1)
end
end
end
end
-- Set single player
function pclasses.api.set_player_class(pname, cname)
if pclasses.api.get_class_by_name(cname) then
pclasses.datas.players[pname] = cname
return true
end
return false
end
-- Util function(s)
pclasses.api.util.does_wear_full_armor = function(pname, material, noshield)
local inv = minetest.get_inventory({type = "detached", name = pname .. "_armor"})
if not inv or inv:is_empty("armor") then
return false
end
local full_armor = true
for _, piece in pairs({"chestplate", "leggings", "boots", "helmet"}) do
full_armor = full_armor and inv:contains_item("armor", "3d_armor:" .. piece .. "_" .. material)
end
return full_armor and (inv:contains_item("armor", "shields:shield_" .. material) or noshield)
end
----------------------------
-- Determination callback --
----------------------------
function pclasses.api.assign_class(player)
-- Look for every sign needed to deduct a player's class
-- Starting from the most important class to the less one
local pname = player:get_player_name()
if pclasses.classes["hunter"].determination(player) then
if pclasses.api.get_player_class(pname) ~= "hunter" then
if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then
pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname)
end
pclasses.api.set_player_class(pname, "hunter")
pclasses.api.get_class_by_name("hunter").on_assigned(pname)
end
elseif pclasses.api.get_class_by_name("warrior").determination(player) then
if pclasses.api.get_player_class(pname) ~= "warrior" then
if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then
pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname)
end
pclasses.api.set_player_class(pname, "warrior")
pclasses.api.get_class_by_name("warrior").on_assigned(pname)
end
elseif pclasses.conf.default_class then
if pclasses.api.get_player_class(pname) ~= pclasses.conf.default_class then
if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then
pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname)
end
pclasses.api.set_player_class(pname, pclasses.conf.default_class)
pclasses.api.get_class_by_name(pclasses.conf.default_class).on_assigned(pname)
end
end
end
minetest.register_on_respawnplayer(pclasses.api.assign_class)
minetest.register_on_joinplayer(function(player) minetest.after(1, pclasses.api.assign_class, player) end)
minetest.register_on_leaveplayer(pclasses.api.assign_class)
-------------------
-- Reserved items
--
function pclasses.api.reserve_item(cname, itemstring)
pclasses.datas.reserved_items[itemstring] = pclasses.datas.reserved_items or {}
table.insert(pclasses.datas.reserved_items[itemstring], 1, cname)
end
pclasses.api.reserve_item("warrior", "moreores:sword_mithril")
pclasses.api.reserve_item("warrior", "default:dungeon_master_s_blood_sword")
pclasses.api.reserve_item("hunter", "throwing:bow_minotaur_horn")
pclasses.api.reserve_item("hunter", "throwing:bow_minotaur_born_improved")
minetest.register_globalstep(function(dtime)
for id, ref in ipairs(minetest.get_connected_players()) do
local name = ref:get_player_name()
local inv = minetest.get_inventory({type="player", name = name})
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
if pclasses.datas.reserved_items[stack:get_name()] then
local drop_stack = true
for class in pairs(pclasses.datas.reserved_items) do
if pclasses.api.get_player_class(name) == class then
drop_stack = false
end
end
if drop_stack then
inv:set_stack("main", i, "")
local pos = ref:getpos()
pos.y = pos.y+2
pos.x = pos.x + math.random(-10,10)
pos.z = pos.z + math.random(-10,10)
minetest.after(1, function()
local item = minetest.add_item(pos, stack)
if item then
item:setvelocity({x = math.random(-10,10), y = math.random(1,7), z = math.random(-10,10)})
end
end)
end
end
end
end
end)

28
mods/pclasses/hunter.lua Normal file
View File

@ -0,0 +1,28 @@
------------------
-- Hunter class --
------------------
--
-- See https://github.com/Ombridride/minetest-minetestforfun-server/issues/114
--
pclasses.api.register_class("hunter", {
determination = function(player)
return pclasses.api.util.does_wear_full_armor(player:get_player_name(), "reinforcedleather", true)
or pclasses.api.util.does_wear_full_armor(player:get_player_name(), "hardenedleather", true)
end,
on_assigned = function(pname)
minetest.chat_send_player(pname, "You are now a hunter")
minetest.sound_play("pclasses_full_hunter")
local reinforced = pclasses.api.util.does_wear_full_armor(pname, "reinforcedleather", true)
if reinforced then
sprint.increase_maxstamina(pname, 20)
else
sprint.increase_maxstamina(pname, 10)
end
minetest.log("action", "[PClasses] Player " .. pname .. " become a hunter")
end,
on_unassigned = function(pname)
sprint.set_default_maxstamina(pname)
end,
})

View File

@ -27,63 +27,9 @@ pclasses.datas.players = {}
pclasses.datas.reserved_items = {} pclasses.datas.reserved_items = {}
pclasses.datas.hud_ids = {} -- HUD maybe? pclasses.datas.hud_ids = {} -- HUD maybe?
dofile(minetest.get_modpath("pclasses") .. "/api.lua")
-- Various utility functions
-- Register the class (basic registration)
function pclasses.api.register_class(cname, def)
if not cname then
minetest.log("error", "[PClasses] Error registering unamed class")
return
elseif not def then
minetest.log("error", "[PClasses] Error registering class " ..
cname .. ". Reason : no definition table.")
return
elseif not def.determination then
minetest.log("error", "[PClasses] Error registreing class " ..
cname .. ". Reason : no determination function.")
return
end
pclasses.classes[cname] = def
return true
end
------------------------
-- Getters and Setters
--
-- Get class specs by name
function pclasses.api.get_class_by_name(cname)
return pclasses.classes[cname]
end
-- Get single player
function pclasses.api.get_player_class(pname)
return pclasses.datas.players[pname]
end
-- Get all players for a class
function pclasses.api.get_class_players(cname)
local pnames = {}
if pclasses.api.get_class_by_name(cname) then
for p,c in ipairs(pclasses.datas.players) do
if c == cname then
table.insert(pnames, table.getn(pnames)+1)
end
end
end
end
-- Set single player
function pclasses.api.set_player_class(pname, cname)
if pclasses.api.get_class_by_name(cname) then
pclasses.datas.players[pname] = cname
return true
end
return false
end
--------------------------- ---------------------------
-- Backup and load system -- Backup and load system
@ -117,17 +63,15 @@ minetest.register_globalstep(function(dtime)
end) end)
minetest.register_on_shutdown(save_datas) minetest.register_on_shutdown(save_datas)
-----------------------------
-- Default class assignment
------------------
-- Default class
-- --
if pclasses.conf.default_class then if pclasses.conf.default_class then
local res = pclasses.api.register_class(pclasses.conf.default_class, { dofile(minetest.get_modpath("pclasses") .. "/" .. pclasses.conf.default_class .. ".lua")
determination = function() return true end, if pclasses.api.get_class_by_name(pclasses.conf.default_class) then
on_assigned = function(pname)
minetest.chat_send_player(pname, "You are now an adventurer")
end
})
if res then
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
if not pclasses.api.get_player_class(player:get_player_name()) then if not pclasses.api.get_player_class(player:get_player_name()) then
pclasses.api.set_player_class(player:get_player_name(), pclasses.api.set_player_class(player:get_player_name(),
@ -137,146 +81,12 @@ if pclasses.conf.default_class then
end end
end end
pclasses.api.util.does_wear_full_armor = function(pname, material, noshield)
local inv = minetest.get_inventory({type = "detached", name = pname .. "_armor"})
if not inv or inv:is_empty("armor") then
return false
end
local full_armor = true
for _, piece in pairs({"chestplate", "leggings", "boots", "helmet"}) do
full_armor = full_armor and inv:contains_item("armor", "3d_armor:" .. piece .. "_" .. material)
end
return full_armor and (inv:contains_item("armor", "shields:shield_" .. material) or noshield)
end
------------ ------------
-- Classes -- Classes
-- --
pclasses.api.register_class("warrior", { dofile(minetest.get_modpath("pclasses") .. "/warrior.lua")
determination = function(player) dofile(minetest.get_modpath("pclasses") .. "/hunter.lua")
return pclasses.api.util.does_wear_full_armor(player:get_player_name(), "harnededleather", true)
or pclasses.api.util.does_wear_full_armor(player:get_player_name(), "reinforcedleather", true)
end,
on_assigned = function(pname)
minetest.sound_play("pclasses_full_warrior")
minetest.chat_send_player(pname, "You are now a warrior")
sprint.set_max_stamina(pname, 20)
end,
on_unassigned = function(pname)
sprint.set_default_maxstamina(pname)
end,
})
pclasses.api.register_class("hunter", {
determination = function(player)
local inv = minetest.get_inventory({type = "detached", name = player:get_player_name() .. "_armor"})
local shift_class = false
if not inv or inv:is_empty("armor") then
return shift_class
end
shift_class = true
for _,piece in pairs({"chestplate", "leggings", "boots", "helmet"}) do
shift_class = shift_class and (inv:contains_item("armor", "3d_armor:" .. piece .. "_reinforcedleather")
or inv:contains_item("armor", "3d_armor:" .. piece .. "_hardenedleather"))
end
return shift_class
end,
on_assigned = function(pname)
minetest.chat_send_player(pname, "You are now a hunter")
minetest.sound_play("pclasses_full_hunter")
local reinforced = pclasses.api.util.does_wear_full_armor(pname, "reinforcedleather", true)
if reinforced then
sprint.increase_maxstamina(pname, 20)
else
sprint.increase_maxstamina(pname, 10)
end
end,
on_unassigned = function(pname)
sprint.set_default_maxstamina(pname)
end,
})
function pclasses.api.assign_class(player)
-- Look for every sign needed to deduct a player's class
-- Starting from the most important class to the less one
local pname = player:get_player_name()
if pclasses.api.get_class_by_name("hunter").determination(player) then
if pclasses.api.get_player_class(pname) ~= "hunter" then
if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then
pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname)
end
pclasses.api.set_player_class(pname, "hunter")
pclasses.api.get_class_by_name("hunter").on_assigned(pname)
end
elseif pclasses.api.get_class_by_name("warrior").determination(player) then
if pclasses.api.get_player_class(pname) ~= "warrior" then
if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then
pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname)
end
pclasses.api.set_player_class(pname, "warrior")
pclasses.api.get_class_by_name("warrior").on_assigned(pname)
end
elseif pclasses.conf.default_class then
if pclasses.api.get_player_class(pname) ~= pclasses.conf.default_class then
if pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned then
pclasses.api.get_class_by_name(pclasses.api.get_player_class(pname)).on_unassigned(pname)
end
pclasses.api.set_player_class(pname, pclasses.conf.default_class)
pclasses.api.get_class_by_name(pclasses.conf.default_class).on_assigned(pname)
end
end
end
minetest.register_on_respawnplayer(pclasses.api.assign_class)
minetest.register_on_joinplayer(function(player) minetest.after(1, pclasses.api.assign_class, player) end)
minetest.register_on_leaveplayer(pclasses.api.assign_class)
-------------------
-- Reserved items
--
function pclasses.api.reserve_item(cname, itemstring)
pclasses.datas.reserved_items[itemstring] = pclasses.datas.reserved_items or {}
table.insert(pclasses.datas.reserved_items[itemstring], 1, cname)
end
pclasses.api.reserve_item("warrior", "moreores:sword_mithril")
pclasses.api.reserve_item("warrior", "default:dungeon_master_s_blood_sword")
pclasses.api.reserve_item("hunter", "throwing:bow_minotaur_horn")
pclasses.api.reserve_item("hunter", "throwing:bow_minotaur_born_improved")
minetest.register_globalstep(function(dtime)
for id, ref in ipairs(minetest.get_connected_players()) do
local name = ref:get_player_name()
local inv = minetest.get_inventory({type="player", name = name})
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
if pclasses.datas.reserved_items[stack:get_name()] then
local drop_stack = true
for class in pairs(pclasses.datas.reserved_items) do
if pclasses.api.get_player_class(name) == class then
drop_stack = false
end
end
if drop_stack then
inv:set_stack("main", i, "")
local pos = ref:getpos()
pos.y = pos.y+2
pos.x = pos.x + math.random(-10,10)
pos.z = pos.z + math.random(-10,10)
minetest.after(1, function()
local item = minetest.add_item(pos, stack)
if item then
item:setvelocity({x = math.random(-10,10), y = math.random(1,7), z = math.random(-10,10)})
end
end)
end
end
end
end
end)

22
mods/pclasses/warrior.lua Normal file
View File

@ -0,0 +1,22 @@
-------------------
-- Warrior class --
-------------------
--
-- See https://github.com/Ombridride/minetest-minetestforfun-server/issues/113
--
pclasses.api.register_class("warrior", {
determination = function(player)
return pclasses.api.util.does_wear_full_armor(player:get_player_name(), "blackmithril", true)
end,
on_assigned = function(pname)
minetest.sound_play("pclasses_full_warrior")
minetest.chat_send_player(pname, "You are now a warrior")
sprint.set_maxstamina(pname, 20)
minetest.log("action", "[PClasses] Player " .. pname .. " become a warrior")
end,
on_unassigned = function(pname)
sprint.set_default_maxstamina(pname)
end,
})

View File

@ -7,19 +7,19 @@ to this software to the public domain worldwide. This software is
distributed without any warranty. distributed without any warranty.
]] ]]
local players = {} sprint.players = {}
local staminaHud = {} local staminaHud = {}
-- Lil' helping functions -- Lil' helping functions
sprint.set_maxstamina = function(pname, mstamina) sprint.set_maxstamina = function(pname, mstamina)
if players[pname] and mstamina > 0 then if sprint.players[pname] and mstamina > 0 then
players[pname].maxStamina = mstamina sprint.players[pname].maxStamina = mstamina
end end
end end
sprint.get_maxstamina = function(pname) sprint.get_maxstamina = function(pname)
if players[pname] then if sprint.players[pname] then
return players[pname].maxStamina return sprint.players[pname].maxStamina
end end
end end
@ -37,11 +37,18 @@ sprint.dicrease_maxstamina = function(pname, sdicrease)
end end
end end
sprint.set_default_maxstamina = function(pname)
if sprint.players[pname] then
sprint.players[pname].maxStamina = SPRINT_STAMINA
end
end
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local playerName = player:get_player_name() local playerName = player:get_player_name()
players[playerName] = { sprint.players[playerName] = {
sprinting = false, sprinting = false,
timeOut = 0, timeOut = 0,
stamina = SPRINT_STAMINA, stamina = SPRINT_STAMINA,
@ -51,7 +58,7 @@ minetest.register_on_joinplayer(function(player)
if SPRINT_HUDBARS_USED then if SPRINT_HUDBARS_USED then
hb.init_hudbar(player, "sprint") hb.init_hudbar(player, "sprint")
else else
players[playerName].hud = player:hud_add({ sprint.players[playerName].hud = player:hud_add({
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = {x=0.5,y=1}, position = {x=0.5,y=1},
size = {x=24, y=24}, size = {x=24, y=24},
@ -65,22 +72,22 @@ minetest.register_on_joinplayer(function(player)
end) end)
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local playerName = player:get_player_name() local playerName = player:get_player_name()
players[playerName] = nil sprint.players[playerName] = nil
end) end)
local gameTime = 0 local gameTime = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
--Get the gametime --Get the gametime
gameTime = gameTime + dtime gameTime = gameTime + dtime
--Loop through all connected players --Loop through all connected sprint.players
for playerName,playerInfo in pairs(players) do for playerName,playerInfo in pairs(sprint.players) do
local player = minetest.get_player_by_name(playerName) local player = minetest.get_player_by_name(playerName)
if player ~= nil then if player ~= nil then
--Check if the player should be sprinting --Check if the player should be sprinting
if player:get_player_control()["aux1"] and player:get_player_control()["up"] then if player:get_player_control()["aux1"] and player:get_player_control()["up"] then
players[playerName]["shouldSprint"] = true sprint.players[playerName]["shouldSprint"] = true
else else
players[playerName]["shouldSprint"] = false sprint.players[playerName]["shouldSprint"] = false
end end
--Stop sprinting if the player is pressing the LMB or RMB --Stop sprinting if the player is pressing the LMB or RMB
if player:get_player_control()["LMB"] or player:get_player_control()["RMB"] then if player:get_player_control()["LMB"] or player:get_player_control()["RMB"] then
@ -119,9 +126,9 @@ minetest.register_globalstep(function(dtime)
end end
end end
--Adjust player states --Adjust player states
if players[playerName]["shouldSprint"] == true and playerInfo["timeOut"] == 0 then --Stopped if sprint.players[playerName]["shouldSprint"] == true and playerInfo["timeOut"] == 0 then --Stopped
setSprinting(playerName, true) setSprinting(playerName, true)
elseif players[playerName]["shouldSprint"] == false then elseif sprint.players[playerName]["shouldSprint"] == false then
setSprinting(playerName, false) setSprinting(playerName, false)
end end
@ -152,7 +159,7 @@ minetest.register_globalstep(function(dtime)
playerInfo["stamina"] = playerInfo["maxStamina"] playerInfo["stamina"] = playerInfo["maxStamina"]
end end
--Update the players's hud sprint stamina bar --Update the sprint.players's hud sprint stamina bar
if SPRINT_HUDBARS_USED then if SPRINT_HUDBARS_USED then
hb.change_hudbar(player, "sprint", playerInfo["stamina"]) hb.change_hudbar(player, "sprint", playerInfo["stamina"])
@ -169,8 +176,8 @@ end)
function setSprinting(playerName, sprinting) --Sets the state of a player (0=stopped/moving, 1=sprinting) function setSprinting(playerName, sprinting) --Sets the state of a player (0=stopped/moving, 1=sprinting)
local player = minetest.get_player_by_name(playerName) local player = minetest.get_player_by_name(playerName)
if players[playerName] then if sprint.players[playerName] then
players[playerName]["sprinting"] = sprinting sprint.players[playerName]["sprinting"] = sprinting
if sprinting == true then if sprinting == true then
player:set_physics_override({speed=SPRINT_SPEED,jump=SPRINT_JUMP}) player:set_physics_override({speed=SPRINT_SPEED,jump=SPRINT_JUMP})
elseif sprinting == false then elseif sprinting == false then