From 743507564f0544bd097864e4bea0076d3923084d Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Thu, 20 Aug 2015 17:11:46 +0200 Subject: [PATCH] Updated pclasses - Moved the holographic item's name as a field in switch_params (pclasses.api.register_class) - Added wizard class' skeleton for later - Fixed a few bugs in nodes, inventory and api - Splitted tick function to bury items in two - Items from 3d_armor's armor inventory are also affected by inventory vacuuming (from tick function). The player's static armor inventory is first cleared, then copied to the detached one, then armor updates everything else (rendering, model, etc) - Fixed itemname of admin shield to reserve it properly - Return graveyard inventory if it already exists. Do not create it every two seconds --- mods/mff/mff_pclasses/admin.lua | 5 ++- mods/mff/mff_pclasses/hunter.lua | 3 +- mods/mff/mff_pclasses/init.lua | 1 + mods/mff/mff_pclasses/warrior.lua | 5 ++- mods/mff/mff_pclasses/wizard.lua | 22 ++++++++++ mods/pclasses/README.md | 68 +++++++++++++++++++++---------- mods/pclasses/adventurer.lua | 1 + mods/pclasses/api.lua | 48 ++++++++++++++-------- mods/pclasses/init.lua | 1 + mods/pclasses/inventory.lua | 8 +++- mods/pclasses/nodes.lua | 13 ++---- 11 files changed, 120 insertions(+), 55 deletions(-) create mode 100644 mods/mff/mff_pclasses/wizard.lua diff --git a/mods/mff/mff_pclasses/admin.lua b/mods/mff/mff_pclasses/admin.lua index c72f9312..36a16552 100755 --- a/mods/mff/mff_pclasses/admin.lua +++ b/mods/mff/mff_pclasses/admin.lua @@ -11,7 +11,8 @@ pclasses.api.register_class("admin", { minetest.chat_send_player(pname, "Hello admin.") end, switch_params = { - color = {r = 255, g = 00, b = 224} + color = {r = 255, g = 00, b = 224}, + holo_item = "maptools:pick_admin" } }) @@ -19,6 +20,6 @@ pclasses.api.reserve_item("admin", "3d_armor:helmet_admin") pclasses.api.reserve_item("admin", "3d_armor:chestplate_admin") pclasses.api.reserve_item("admin", "3d_armor:leggings_admin") pclasses.api.reserve_item("admin", "3d_armor:boots_admin") -pclasses.api.reserve_item("admin", "shields:shields_admin") +pclasses.api.reserve_item("admin", "shields:shield_admin") pclasses.api.reserve_item("admin", "maptools:pick_admin") pclasses.api.reserve_item("admin", "maptools:pick_admin_with_drops") diff --git a/mods/mff/mff_pclasses/hunter.lua b/mods/mff/mff_pclasses/hunter.lua index 75c30b61..8fcf7aae 100755 --- a/mods/mff/mff_pclasses/hunter.lua +++ b/mods/mff/mff_pclasses/hunter.lua @@ -23,7 +23,8 @@ pclasses.api.register_class("hunter", { end, switch_params = { color = {r = 30, g = 170, b = 00}, - tile = "default_wood.png" + tile = "default_wood.png", + holo_item = "throwing:bow_minotaur_horn_improved" } }) diff --git a/mods/mff/mff_pclasses/init.lua b/mods/mff/mff_pclasses/init.lua index 422c908a..d885d86d 100755 --- a/mods/mff/mff_pclasses/init.lua +++ b/mods/mff/mff_pclasses/init.lua @@ -3,3 +3,4 @@ local path = minetest.get_modpath("mff_pclasses") dofile(path .. "/warrior.lua") dofile(path .. "/hunter.lua") dofile(path .. "/admin.lua") +dofile(path .. "/wizard.lua") diff --git a/mods/mff/mff_pclasses/warrior.lua b/mods/mff/mff_pclasses/warrior.lua index d612ef6a..1b6965dc 100755 --- a/mods/mff/mff_pclasses/warrior.lua +++ b/mods/mff/mff_pclasses/warrior.lua @@ -11,14 +11,15 @@ pclasses.api.register_class("warrior", { 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") + minetest.log("action", "[PClasses] Player " .. pname .. " becomes a warrior") end, on_unassigned = function(pname) sprint.set_default_maxstamina(pname) end, switch_params = { color = {r = 06, g = 06, b = 30}, - tile = "default_steel_block.png" + tile = "default_steel_block.png", + holo_item = "default:dungeon_master_s_blood_sword" } }) diff --git a/mods/mff/mff_pclasses/wizard.lua b/mods/mff/mff_pclasses/wizard.lua new file mode 100644 index 00000000..77f78596 --- /dev/null +++ b/mods/mff/mff_pclasses/wizard.lua @@ -0,0 +1,22 @@ +------------------ +-- Wizard class -- +------------------ + +-- +-- No Issue Yet +-- + +pclasses.api.register_class("wizard", { + on_assigned = function(pname) +-- minetest.sound_play("pclasses_full_wizard") + minetest.chat_send_player(pname, "You are now a wizard.") + -- Add specs here + minetest.log("action", "[PClasses] Player " .. pname .. " becomes a wizard") + end, + switch_params = { + color = {r = 230, g = 230, b = 0}, + holo_item = "default:book" + } +}) + +-- Reserved items here diff --git a/mods/pclasses/README.md b/mods/pclasses/README.md index 39212d22..d972c4ac 100755 --- a/mods/pclasses/README.md +++ b/mods/pclasses/README.md @@ -15,39 +15,42 @@ Yet another class mod for Minetest. ### pclasses.api - All functions used to declare, get, set classes +### pclasses.api.util + - Some utility functions + +### pclasses.conf + - Some configuration values + ### pclasses.classes - All classes and their specs -### pclasses.datas - - Miscellaneous datas +### pclasses.data + - Miscellaneous data -#### pclasses.datas.players +#### pclasses.data.players - List of all players' class. Index is player's name and value is the class's name -#### pclasses.datas.hud_ids +#### pclasses.data.hud_ids - Surely useful in the future with a hypothetical hud to show current class # Functions -### pclasses.api.create_class_id - - Arguments : None - - Indicates the next free id/index in the classes' table +### pclasses.api.register_class + - Arguments : cname, def + - Registers a class and its specifications + - Def is a definition table that can contain many functions/values : + - `on_assigned` which is a function, receiving as argument the player name + - `on_unassigned` which is a function, receiving as argument the player name + - `switch_params`, which is a table, containing parameters for the switch pedestal : + - `holo_item` is mandatory. It's the itemstring of the item to be put over the pedestal + - `color` is optional. Default is white. It's a RGB table. + - `tile` is optional. Default is none. It's a string of the texture to be applied over the pedestal -### pclasses.api.id_for_class - - Arguments : cname (class' name) - - Returns the id/index corresponding the class in the classes' table - - Returns 0 if not found, nil if no name given - -### pclasses.api.register_class(cname) - - Argument : cname - - Registers a class in the classes' table - - Pretty useless at the moment - - Returns class' id or nil if any error - -### pclasses.api.get_class_by_id - - Argument : id - - Return the class' specs (table) corresponding an id or nil when not found +### pclasses.register_class_switch + - Arguments : cname, params + - Used internally to create switch pedestals + - `params` is the `def` table given to `pclasses.api.register_class`, documented above ### pclasses.api.get_class_by_name - Argument : cname @@ -64,4 +67,27 @@ Yet another class mod for Minetest. ### pclasses.api.set_player_class - Arguments : pname, cname - Assign a player the cname class + - Returns true if achieved, false if not +### pclasses.api.util.does_wear_full_armor + - Arguments : pname, material, noshield + - Returns true if player `pname` is wearing the full armor made out of `material` + - `noshield` must be true when the full armor has no shield + +### pclasses.api.util.can_have_item + - Arguments : pname, itemname + - Returns true if player `pname` can have items `itemstring` in his main inventory, according to his class + +### pclasses.api.reserve_item + - Arguments : cname, itemstring + - Adds an entry in the reserved items' table. Players will need to belong to class `cname` in order to have items `itemstring` in their main inventory + - Note : You can reserve the same item for two classes, any player of either of both can then have the item + +### pclasses.api.create_graveyard_inventory + - Argument : player + - Creates a detached inventory dedicated to 'dead' items (confiscated reserved items) + - Used internally, should not be used outside of pclasses + +### pclasses.api.vacuum_graveyard + - Argument : player + - Check all of `player`'s graveyard inventory to get them back items they obtained to right to have diff --git a/mods/pclasses/adventurer.lua b/mods/pclasses/adventurer.lua index 5a9120c5..57c0b2ab 100755 --- a/mods/pclasses/adventurer.lua +++ b/mods/pclasses/adventurer.lua @@ -6,6 +6,7 @@ pclasses.api.register_class("adventurer", { switch_params = { color = { r = 142, g = 64, b = 00}, tile = "wool_white.png", + holo_item = "unified_inventory:bag_large" }, on_assigned = function(pname) minetest.chat_send_player(pname, "You are now an adventurer") diff --git a/mods/pclasses/api.lua b/mods/pclasses/api.lua index f85cfb90..8b87ba05 100755 --- a/mods/pclasses/api.lua +++ b/mods/pclasses/api.lua @@ -116,27 +116,41 @@ end -- Determination and reserved items tick -- ------------------------------------------- -local function tick() - 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.data.reserved_items[stack:get_name()] then - if not pclasses.api.util.can_have_item(name, stack:get_name()) then - inv:set_stack("main", i, "") - local grave_inv = pclasses.api.create_graveyard_inventory(ref) - if grave_inv and grave_inv:room_for_item("graveyard", stack) then - grave_inv:add_item("graveyard", stack) - inv:add_item("graveyard", stack) - -- ^ Because add_item doesn't trigger on_put, nonsense - else - minetest.add_item(ref:getpos(), stack) - end +local function vacuum_inventory(name, inv, invname) + local ref = minetest.get_player_by_name(name) + for i = 1, inv:get_size(invname) do + local stack = inv:get_stack(invname, i) + if pclasses.data.reserved_items[stack:get_name()] then + if not pclasses.api.util.can_have_item(name, stack:get_name()) then + inv:set_stack(invname, i, "") + local grave_inv = pclasses.api.create_graveyard_inventory(ref) + if grave_inv and grave_inv:room_for_item("graveyard", stack) then + grave_inv:add_item("graveyard", stack) + inv:add_item("graveyard", stack) + -- ^ Because add_item doesn't trigger on_put, nonsense + else + minetest.add_item(ref:getpos(), stack) end end end end +end + + +local function tick() + for id, ref in ipairs(minetest.get_connected_players()) do + local name = ref:get_player_name() + local armor_inv = minetest.get_inventory({type = "detached", name = name .. "_armor"}) + local inv = ref:get_inventory() + vacuum_inventory(name, inv, "main") + vacuum_inventory(name, inv, "armor") + -- Hack the hack + for i = 1, armor_inv:get_size("armor") do + armor_inv:set_stack("armor", i, inv:get_stack("armor", i)) + end + armor:set_player_armor(ref) + armor:update_inventory(ref) + end minetest.after(2, tick) end diff --git a/mods/pclasses/init.lua b/mods/pclasses/init.lua index a9f68d00..e3ed3236 100755 --- a/mods/pclasses/init.lua +++ b/mods/pclasses/init.lua @@ -18,6 +18,7 @@ pclasses.conf.default_class = "adventurer" pclasses.conf.save_interval = 3 * 60 pclasses.conf.datafile = minetest.get_worldpath() .. "/pclasses" pclasses.conf.gravefile = minetest.get_worldpath() .. "/graveyards" + -- Classes pclasses.classes = {} diff --git a/mods/pclasses/inventory.lua b/mods/pclasses/inventory.lua index 8d38727b..de9c4df1 100755 --- a/mods/pclasses/inventory.lua +++ b/mods/pclasses/inventory.lua @@ -5,8 +5,12 @@ -- Inventory for 'dead' items pclasses.api.create_graveyard_inventory = function(player) local pname = player:get_player_name() + local grave_inv = minetest.get_inventory({type = "detached", name = pname .. "_graveyard"}) + if grave_inv then + return grave_inv + end local player_inv = minetest.get_inventory({type = "player", name = pname}) - local grave_inv = minetest.create_detached_inventory(pname .. "_graveyard", { + grave_inv = minetest.create_detached_inventory(pname .. "_graveyard", { on_take = function(inv, listname, index, stack, player) player_inv:set_stack(listname, index, nil) end, @@ -64,7 +68,7 @@ function pclasses.api.vacuum_graveyard(player) if player_inv:room_for_item("main", stack) then player_inv:add_item("main", stack) else - minetest.add_item(pos, stack) + minetest.add_item(player:getpos(), stack) end end end diff --git a/mods/pclasses/nodes.lua b/mods/pclasses/nodes.lua index fb7d5bc4..5b39e55e 100755 --- a/mods/pclasses/nodes.lua +++ b/mods/pclasses/nodes.lua @@ -61,18 +61,11 @@ minetest.register_entity("pclasses:item", { end, }) -local classes_items = { - ["hunter"] = "throwing:bow_minotaur_horn_improved", - ["warrior"] = "default:dungeon_master_s_blood_sword", - ["admin"] = "maptools:pick_admin", - ["adventurer"] = "unified_inventory:bag_large", - ["wizard"] = "default:book" -} - function pclasses.register_class_switch(cname, params) local color = params.color or { r = 255, g = 255, b = 255 } local txtcolor = string.format("#%02x%02x%02x", color.r, color.g, color.b) local overlay = "pclasses_class_switch_orb_overlay.png" + local holo_item = params.holo_item or "default:diamond" minetest.register_node(":pclasses:class_switch_" .. cname, { description = "Class switch orb (" .. cname .. ")", tiles = {(params.tile or overlay) .. "^[colorize:" .. txtcolor .. ":200"}, @@ -101,7 +94,7 @@ function pclasses.register_class_switch(cname, params) local obj = minetest.add_entity(pos, "pclasses:item") if obj then - obj:get_luaentity():set_item(classes_items[cname]) + obj:get_luaentity():set_item(holo_item) obj:get_luaentity():set_class(cname) end pos.y = pos.y - 1 @@ -119,7 +112,7 @@ function pclasses.register_class_switch(cname, params) local obj = minetest.add_entity(pos, "pclasses:item") if obj then - obj:get_luaentity():set_item(classes_items[cname]) + obj:get_luaentity():set_item(holo_item) obj:get_luaentity():set_class(cname) end return true