Add feller skill and clean up redundancy

This commit is contained in:
Austin Shenk 2013-06-29 23:44:38 -04:00
parent 56679ece10
commit 2f821858b7

View File

@ -8,7 +8,7 @@ dofile(modpath.."/xp.lua")
--variable used for time keeping for updating xp --variable used for time keeping for updating xp
time = 0 time = 0
local get_specialInfo = function(player, specialty) local get_specialInfo = function(name, specialty)
local formspec = "size[8,8]" -- size of the formspec page local formspec = "size[8,8]" -- size of the formspec page
.."button_exit[0,0;0.75,0.5;close;X]" -- back to main inventory .."button_exit[0,0;0.75,0.5;close;X]" -- back to main inventory
.."button[2,0;2,0.5;miner;Miner]" .."button[2,0;2,0.5;miner;Miner]"
@ -18,7 +18,7 @@ local get_specialInfo = function(player, specialty)
.."button[2,3;2,0.5;builder;Builder]" .."button[2,3;2,0.5;builder;Builder]"
.."list[current_player;main;0,4;8,4;]" .."list[current_player;main;0,4;8,4;]"
if(specialty ~= "") then if(specialty ~= "") then
formspec = formspec.."label[4,0;XP: "..specialties.players[player:get_player_name()][specialty].."]"..specialties.skills[specialty].menu formspec = formspec.."label[4,0;XP: "..specialties.players[name][specialty].."]"..specialties.skills[specialty].menu
end end
return formspec return formspec
end end
@ -27,19 +27,12 @@ minetest.register_on_leaveplayer(function(player)--Called if on a server, if sin
specialties.updateXP(player:get_player_name()) specialties.updateXP(player:get_player_name())
end) end)
--Initial Files Created
minetest.register_on_newplayer(function(player)
for skill,_ in pairs(specialties.skills) do
specialties.writeXP(player:get_player_name(), skill, 0)
end
end)
--Initial XP Extraction --Initial XP Extraction
--optimizes the amount of calls to files --optimizes the amount of calls to files
minetest.register_chatcommand("spec", { minetest.register_chatcommand("spec", {
description = "Show Specialties menu", description = "Show Specialties menu",
func = function(name, param) func = function(name, param)
minetest.show_formspec(name, "specialties:spec", get_specialInfo(minetest.get_player_by_name(name), "")) minetest.show_formspec(name, "specialties:spec", get_specialInfo(name, ""))
end, end,
}) })
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
@ -51,28 +44,27 @@ minetest.register_on_joinplayer(function(player)
player:get_inventory():set_size("buildtrash", 1) player:get_inventory():set_size("buildtrash", 1)
name = player:get_player_name() name = player:get_player_name()
specialties.players[name] = {} specialties.players[name] = {}
for skill,_ in pairs(specialties.skills) do specialties.players[name] = specialties.readXP(name)
specialties.players[name][skill] = specialties.readXP(name, skill)
end
end) end)
local function show_formspec(player, specialty) local function show_formspec(name, specialty)
minetest.show_formspec(player, "specialties:spec", get_specialInfo(minetest.get_player_by_name(player), specialty)) minetest.show_formspec(name, "specialties:spec", get_specialInfo(name, specialty))
end end
--Skill Events --Skill Events
local function healTool(player, list, specialty, cost) local function healTool(player, list, specialty, cost)
tool = player:get_inventory():get_list(list)[1] local tool = player:get_inventory():get_list(list)[1]
if tool:get_name():find(":"..list) == nil then return end if tool:get_name():find(":"..list) == nil then return end
local name = player:get_player_name()
if tool:get_wear() ~= 0 and specialties.healAmount[tool:get_name()] ~= nil then if tool:get_wear() ~= 0 and specialties.healAmount[tool:get_name()] ~= nil then
if specialties.changeXP(player:get_player_name(), specialty, -cost) then if specialties.changeXP(name, specialty, -cost) then
tool:add_wear(-specialties.healAmount[tool:get_name()]) tool:add_wear(-specialties.healAmount[tool:get_name()])
player:get_inventory():set_stack(list, 1, tool) player:get_inventory():set_stack(list, 1, tool)
end end
end end
show_formspec(player:get_player_name(), specialty) show_formspec(name, specialty)
end end
local function upgradeTool(player, list, specialty, cost) local function upgradeTool(player, list, specialty, cost)
tool = player:get_inventory():get_list(list)[1] local tool = player:get_inventory():get_list(list)[1]
if tool:get_name():find(":"..list) == nil then return end if tool:get_name():find(":"..list) == nil then return end
if specialties.upgradeTree[tool:get_name()] ~= nil then if specialties.upgradeTree[tool:get_name()] ~= nil then
if specialties.changeXP(player:get_player_name(), specialty, -cost) then if specialties.changeXP(player:get_player_name(), specialty, -cost) then
@ -86,7 +78,8 @@ local function addSpecial2Tool(player, skill, list, specialty, cost)
local toolname = tool:get_name() local toolname = tool:get_name()
if toolname:find(":"..list) == nil then return end if toolname:find(":"..list) == nil then return end
if toolname:find("_"..skill) ~= nil then return end if toolname:find("_"..skill) ~= nil then return end
if specialties.changeXP(player:get_player_name(), specialty, -cost) then local name = player:get_player_name()
if specialties.changeXP(name, specialty, -cost) then
local def = tool:get_definition() local def = tool:get_definition()
local colonpos = toolname:find(":") local colonpos = toolname:find(":")
local modname = toolname:sub(0,colonpos-1) local modname = toolname:sub(0,colonpos-1)
@ -94,7 +87,7 @@ local function addSpecial2Tool(player, skill, list, specialty, cost)
local name = toolname.."_"..skill local name = toolname.."_"..skill
player:get_inventory():set_stack(list, 1, name) player:get_inventory():set_stack(list, 1, name)
end end
show_formspec(player:get_player_name(), specialty) show_formspec(name, specialty)
end end
local function doTransfer(player, list, factor) local function doTransfer(player, list, factor)
@ -102,14 +95,15 @@ end
--GUI Events --GUI Events
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if fields.specialties then if fields.specialties then
show_formspec(player:get_player_name(), "") show_formspec(name, "")
return return
end end
--MINER --MINER
if fields.miner then if fields.miner then
show_formspec(player:get_player_name(), "miner") show_formspec(name, "miner")
return return
end end
if fields.healpick then healTool(player, "pick", "miner", 100) end if fields.healpick then healTool(player, "pick", "miner", 100) end
@ -118,7 +112,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
--LUMBERJACK --LUMBERJACK
if fields.lumberjack then if fields.lumberjack then
show_formspec(player:get_player_name(), "lumberjack") show_formspec(name, "lumberjack")
return return
end end
if fields.healaxe then healTool(player, "axe", "lumberjack", 100) end if fields.healaxe then healTool(player, "axe", "lumberjack", 100) end
@ -127,7 +121,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
--DIGGER --DIGGER
if fields.digger then if fields.digger then
show_formspec(player:get_player_name(), "digger") show_formspec(name, "digger")
return return
end end
if fields.healshovel then healTool(player, "shovel", "digger", 100) end if fields.healshovel then healTool(player, "shovel", "digger", 100) end
@ -136,7 +130,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
--FARMER --FARMER
if fields.farmer then if fields.farmer then
show_formspec(player:get_player_name(), "farmer") show_formspec(name, "farmer")
return return
end end
if fields.healhoe then healTool(player, "hoe", "farmer", 100) end if fields.healhoe then healTool(player, "hoe", "farmer", 100) end
@ -145,7 +139,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
--BUILDER --BUILDER
if fields.builder then if fields.builder then
show_formspec(player:get_player_name(), "builder") show_formspec(name, "builder")
return return
end end
if fields.dorefill then doTransfer(player, "refill", 1) end if fields.dorefill then doTransfer(player, "refill", 1) end
@ -156,24 +150,33 @@ end)
--XP Events --XP Events
minetest.register_on_dignode(function(pos, oldnode, digger) minetest.register_on_dignode(function(pos, oldnode, digger)
if(digger == nil) then if digger == nil then
return return
end end
if(digger:get_wielded_item():is_empty())then if digger:get_wielded_item():is_empty() then
return return
end end
local tool = digger:get_wielded_item():get_name() local tool = digger:get_wielded_item():get_name()
local name = digger:get_player_name() local name = digger:get_player_name()
if(tool:find("pick") ~= nil)then if tool:find("pick") ~= nil then
specialties.changeXP(name, "miner", 1) specialties.changeXP(name, "miner", 1)
end end
if(tool:find("axe") ~= nil)then if tool:find("axe") ~= nil then
specialties.changeXP(name, "lumberjack", 1) specialties.changeXP(name, "lumberjack", 1)
if tool:find("feller") ~= nil and minetest.get_item_group(oldnode.name, "tree") ~= 0 then
local y = 1
local abovepos = {x=pos.x,y=pos.y+y,z=pos.z}
while minetest.get_node(abovepos).name == oldnode.name do
minetest.dig_node(abovepos)
y = y+1
abovepos = {x=pos.x,y=pos.y+y,z=pos.z}
end end
if(tool:find("shovel") ~= nil)then end
end
if tool:find("shovel") ~= nil then
specialties.changeXP(name, "digger", 1) specialties.changeXP(name, "digger", 1)
end end
if(oldnode.name:find("farming") ~= nil) then if oldnode.name:find("farming") ~= nil then
specialties.changeXP(name, "farmer", 5) specialties.changeXP(name, "farmer", 5)
end end
end) end)
@ -181,12 +184,12 @@ minetest.register_on_placenode(function(pos, newnode, placer, oldnode)
specialties.changeXP(placer:get_player_name(), "builder", 1) specialties.changeXP(placer:get_player_name(), "builder", 1)
end) end)
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
if(time+dtime < 10) then if time+dtime < 10 then
time = time+dtime time = time+dtime
else else
time = 0 time = 0
for key in pairs(specialties.players)do for player in pairs(specialties.players)do
specialties.updateXP(key) specialties.writeXP(player)
end end
end end
end) end)