Add player stats, remove lifetimer from some NPCs

pull/18/head
Brandon 2015-08-18 23:32:28 -05:00
parent f5a4b3e935
commit 997843977c
10 changed files with 138 additions and 6 deletions

View File

@ -29,6 +29,7 @@ local function adventuretest_die_player(player)
mg_villages.spawnplayer(player)
end
energy.respawnplayer(player)
stats.increment(player:get_player_name(),STAT_DIED,1)
return true
end
@ -56,8 +57,21 @@ local function adventuretest_dignode(pos, node, digger)
if digger ~= nil and digger ~= "" then
local name= digger:get_player_name()
if player_energy[name] ~= nil then
player_energy[name] = player_energy[name] - 0.08
player_energy[name] = player_energy[name] - 0.05
end
stats.increment(name,STAT_DUG,1)
local dug = stats.get(name,STAT_DUG)
if dug % 100 == 0 then
local ppos = digger:getpos()
-- every 100 give them some experience
local multiplier = dug / 100
local exp = 5 * multiplier
local e = experience.exp_to_items(exp)
for _,item in pairs(e) do
default.drop_item(ppos,item)
end
end
end
hunger.handle_node_actions(pos, node, digger)
@ -66,6 +80,22 @@ minetest.register_on_dignode(adventuretest_dignode)
local function adventuretest_placenode(pos, node, placer)
hunger.handle_node_actions(pos,node,placer)
if placer:is_player() then
local name = placer:get_player_name()
stats.increment(name,STAT_PLACED,1)
local placed = stats.get(name,STAT_PLACED)
if placed % 100 == 0 then
local ppos = placer:getpos()
-- every 100 give them some experience
local multiplier = placed / 100
local exp = 5 * multiplier
local e = experience.exp_to_items(exp)
for _,item in pairs(e) do
default.drop_item(ppos,item)
end
end
end
end
minetest.register_on_placenode(adventuretest_placenode)
@ -75,3 +105,20 @@ local function on_generated(minp,maxp,seed)
end
minetest.register_on_generated(on_generated)
local function on_join(player)
stats.load(player:get_player_name())
end
minetest.register_on_joinplayer(on_join)
local function on_leave(player)
local name = player:get_player_name()
stats.save(name)
stats.unload(name)
end
minetest.register_on_leaveplayer(on_leave)
local function on_shutdown()
stats.save_all()
end
minetest.register_on_shutdown(on_shutdown)

View File

@ -40,6 +40,8 @@ function energy.update_energy(p,name)
local hdiff = math.sqrt(math.pow(pos.x-player_lastpos[name].x, 2) + math.pow(pos.z-player_lastpos[name].z, 2))
stats.increment(name,STAT_TRAVEL,math.floor(hdiff))
adj = adj - ( hdiff * 0.03 )
--print("Energy Adjustments")
--print(tostring(adj))

View File

@ -479,6 +479,7 @@ function mobs:register_mob(name, def)
if self.state == "stand" then
-- randomly turn
math.randomseed(os.clock())
if math.random(1, 100) < self.activity_level then
-- if there is a player nearby look at them
local lp = nil

View File

@ -57,7 +57,9 @@ mobs:register_mob("mobs:blacksmith",{
group_attack = false,
blood_amount = 35,
blood_offset = 0.25,
activity_level = 15,
activity_level = 1,
lifetimer = false,
})
-- list of active blacksmiths... I'm not sure how this is going to work when an entity is unloaded

View File

@ -58,4 +58,6 @@ mobs:register_mob("mobs:explorer",{
{chance=60, item="experience:6_exp"},
{chance=50, item="potions:magic_replenish1"},
},
lifetimer = false,
activity_level = 2,
})

View File

@ -55,7 +55,8 @@ mobs:register_mob("mobs:male1_npc",{
{chance=60, item="experience:6_exp"},
{chance=50, item="potions:magic_replenish1"},
},
walk_chance = 10,
walk_chance = 5,
lifetimer = false,
})
mobs:register_mob("mobs:male2_npc",{
@ -115,7 +116,8 @@ mobs:register_mob("mobs:male2_npc",{
{chance=60, item="experience:6_exp"},
{chance=50, item="potions:magic_replenish1"},
},
walk_chance = 10,
walk_chance = 5,
lifetimer = false,
})
mobs:register_mob("mobs:male3_npc",{
@ -175,5 +177,6 @@ mobs:register_mob("mobs:male3_npc",{
{chance=60, item="experience:6_exp"},
{chance=50, item="potions:magic_replenish1"},
},
walk_chance = 10,
walk_chance = 2,
lifetimer = false,
})

View File

@ -52,6 +52,7 @@ type = "npc",
{chance=40, item="experience:6_exp"},
{chance=60, item="potions:magic_replenish1"},
},
lifetimer = false,
})
mobs:register_mob("mobs:female2_npc", {
@ -108,7 +109,8 @@ type = "npc",
{chance=40, item="experience:6_exp"},
{chance=60, item="potions:magic_replenish1"},
},
walk_chance = 10,
walk_chance = 3,
lifetimer = false,
})
mobs:register_mob("mobs:female3_npc", {
@ -167,4 +169,5 @@ type = "npc",
{chance=40, item="potions:magic_replenish1"},
},
walk_chance = 4,
lifetimer = false,
})

View File

@ -115,6 +115,12 @@ function getformspec(pos)
end
function mobs.spawn_npc_and_spawner(pos,barbarian_village)
if randomChance(30) then
return
end
math.randomseed(os.clock())
local numNPCs = math.random(0,1)
--print("Spawning "..tostring(numNPCs).." NPCs")
if numNPCs > 0 then

8
mods/player_stats/const.lua Executable file
View File

@ -0,0 +1,8 @@
STAT_DUG = 1
STAT_PLACED = 2
STAT_DIED = 3
STAT_TRAVEL = 4
STAT_PK = 5
STAT_KILLS = 6
strStat = {"Nodes Dug","Nodes Placed","Died","Distance Traveled","Players Killed","Mobs Killed"}

58
mods/player_stats/init.lua Executable file
View File

@ -0,0 +1,58 @@
stats = {}
local player_stats = {}
dofile(minetest.get_modpath("player_stats").."/const.lua")
stats.load = function ( name )
player_stats[name] = default.deserialize_from_file(minetest.get_worldpath() .. "/players/" .. name ..".stats")
end
stats.save = function (name)
default.serialize_to_file(minetest.get_worldpath() .. "/players/" .. name .. ".stats",player_stats[name])
end
stats.save_all = function()
for name,s in pairs(player_stats) do
if s ~= nil then
stats.save(name)
end
end
minetest.after(600,stats.save_all)
end
stats.unload = function(name)
player_stats[name] = nil
end
stats.set = function (name, stat, value)
if player_stats[name] ~= nil then
player_stats[name][stat] = value
end
end
stats.get = function (name, stat)
return player_stats[name][stat]
end
stats.increment = function (name, stat, amount)
print("increment stat "..tostring(stat).." by "..tostring(amount))
if player_stats[name] ~= nil then
if tonumber(player_stats[name][stat]) ~= nil then
player_stats[name][stat] = player_stats[name][stat] + amount
else
player_stats[name][stat] = amount -- stat wasn't set or was invalid so reset it
end
end
end
minetest.after(600,stats.save_all)
minetest.register_chatcommand("stats",{
params = "",
description = "Shows players stats",
func = function(name, param)
for i,d in pairs(strStat) do
minetest.chat_send_player(name, d..": "..tostring(player_stats[name][i]))
end
end,
})