minetest_xpfw/api.lua

310 lines
8.7 KiB
Lua
Raw Normal View History

2018-12-03 21:31:16 -08:00
2018-12-12 20:10:42 -08:00
local M=xpfw
2018-12-04 20:36:23 -08:00
if M.player == nil then
M.player={}
end
if M.experiences == nil then
M.experiences={}
end
2018-12-03 21:31:16 -08:00
local check_value=function(tab,val,def)
if tab[val] == nil then
tab[val] = def
end
2018-12-04 19:04:57 -08:00
end
2018-12-03 21:31:16 -08:00
2018-12-05 20:11:44 -08:00
xpfw.register_attribute=function(name,data)
check_value(data,"min",0)
check_value(data,"max",math.huge)
data.name=name
xpfw.attributes[name]=data
end
2018-12-12 09:41:56 -08:00
local player_addsub_attribute=function(player,attrib,val,maf)
2018-12-05 20:11:44 -08:00
local oldvalue=xpfw.player_get_attribute(player,attrib)
local att_def=xpfw.attributes[attrib]
local new_val = oldvalue + val
2018-12-12 09:41:56 -08:00
if maf ~= nil then
new_val=(oldvalue*maf + val)/(maf + 1)
end
xpfw.player_set_attribute(player,attrib,new_val)
2018-12-05 20:11:44 -08:00
end
2018-12-12 09:41:56 -08:00
xpfw.player_add_attribute=function(player,attrib,val)
local nval=val
local att_def=xpfw.attributes[attrib]
if val==nil then
nval=att_def.max or 20
end
if att_def.moving_average_factor ~= nil then
player_addsub_attribute(player,attrib,nval,att_def.moving_average_factor)
else
player_addsub_attribute(player,attrib,nval)
end
2018-12-12 20:49:30 -08:00
local playerdata=M.player[player:get_player_name()]
playerdata.flags[attrib]=1
2018-12-12 09:41:56 -08:00
end
xpfw.player_sub_attribute=function(player,attrib,val)
local nval=val
local att_def=xpfw.attributes[attrib]
if val==nil then
nval=att_def.max or 20
end
if att_def.recreation_factor ~= nil then
player_addsub_attribute(player,attrib,(-1)*nval,att_def.recreation_factor)
else
player_addsub_attribute(player,attrib,(-1)*nval)
end
end
2018-12-05 20:11:44 -08:00
xpfw.player_get_attribute=function(player,attrib)
local pm=player:get_meta()
return pm:get_float(xpfw.prefix.."_"..attrib) or 0
end
xpfw.player_set_attribute=function(player,attrib,val)
2018-12-05 09:14:47 -08:00
local pm=player:get_meta()
2018-12-05 20:11:44 -08:00
local att_def=xpfw.attributes[attrib]
local setvalue=math.min(att_def.max or math.huge,math.max(att_def.min or 0,val))
pm:set_float(xpfw.prefix.."_"..attrib,setvalue)
2018-12-05 09:14:47 -08:00
end
xpfw.player_ping_attribute=function(player,attrib)
local att_def=xpfw.attributes[attrib]
local ping_max=att_def.max
if ping_max == nil then
ping_max=xpfw.experience_max
xpfw.attributes[attrib]["max"]=xpfw.experience_max
else
if ping_max==math.huge then
ping_max=xpfw.experience_max
xpfw.attributes[attrib]["max"]=xpfw.experience_max
end
end
if att_def.moving_average_factor == nil then
att_def.moving_average_factor=xpfw.mean_weight
end
xpfw.player_add_attribute(player,attrib,ping_max)
end
2018-12-03 21:31:16 -08:00
M.register_experience=function(name,indata)
local tid=table.copy(indata)
tid.name=name
check_value(tid,"default",0)
check_value(tid,"decay",0)
M.experiences[name]=tid
end
2018-12-10 08:20:02 -08:00
xpfw.player_reset_attributes=function(player)
2018-12-12 20:10:42 -08:00
for i,att_def in pairs(xpfw.attributes) do
print(dump2(att_def))
2018-12-06 08:52:28 -08:00
local setval=att_def.min or 0
if att_def.default ~= nil then
setval=att_def.default
end
player:set_attribute(xpfw.prefix.."_"..att_def.name,setval)
end
end
2018-12-03 21:31:16 -08:00
minetest.register_on_joinplayer(function(player)
2018-12-04 20:36:23 -08:00
local playername = player:get_player_name()
2018-12-11 05:04:25 -08:00
for i,att_def in pairs(xpfw.attributes) do
if player:get_attribute(xpfw.prefix.."_"..att_def.name) == nil then
2018-12-06 08:52:28 -08:00
local defval=att_def.min or 0
if att_def.default ~= nil then
defval=att_def.default
end
player:set_attribute(xpfw.prefix.."_"..att_def.name,defval)
2018-12-05 09:14:47 -08:00
end
end
2018-12-03 21:31:16 -08:00
if M.player[playername]==nil then
2018-12-10 21:22:13 -08:00
M.player[playername]={last_pos=player:get_pos(), --actual position
2018-12-12 20:49:30 -08:00
flags={},
2018-12-21 01:12:51 -08:00
}
local playerhud=xpfw.mod_storage:get_int(playername.."_hud")
if playerhud==nil then playerhud=1 end
if playerhud == 1 then
M.player[playername].hud=1
end
2018-12-04 20:36:23 -08:00
end
2018-12-10 21:22:13 -08:00
local playerdata=M.player[playername]
2018-12-05 09:14:47 -08:00
local pm=player:get_meta()
2018-12-05 11:33:32 -08:00
pm:set_int(xpfw.prefix.."_lastlogin",os.time()) -- last login time
2018-12-10 21:22:13 -08:00
xpfw.player_add_attribute(player,"login",1)
2018-12-21 01:12:51 -08:00
if playerdata.hud~=nil then
xpfw.player_add_hud(player)
end
2018-12-12 20:10:42 -08:00
playerdata.dtime=0
-- print(pm:get_int(xpfw.prefix.."_lastlogin"))
end
)
2018-12-12 20:49:30 -08:00
xpfw.player_hud_toggle=function(name)
local player=minetest.get_player_by_name(name)
local playerdata=M.player[name]
if playerdata==nil then
return
end
if playerdata.hidx==nil then
xpfw.player_add_hud(player)
else
xpfw.player_remove_hud(player)
end
end
2018-12-12 20:10:42 -08:00
xpfw.player_add_hud=function(player)
local playerdata=M.player[player:get_player_name()]
if playerdata==nil then
return
end
if playerdata.hud == nil then
playerdata.hud=1
end
2018-12-10 21:22:13 -08:00
playerdata.hidx=player:hud_add({
hud_elem_type = "text",
position = {x=1,y=1},
size = "",
text = "",
alignment = {x=-1,y=-1},
})
2018-12-04 20:36:23 -08:00
end
2018-12-12 20:10:42 -08:00
xpfw.player_remove_hud=function(player)
local playerdata=M.player[player:get_player_name()]
if playerdata==nil then
return
end
if playerdata.hidx ~= nil then
2018-12-12 20:49:30 -08:00
player:hud_remove(playerdata.hidx)
2018-12-12 20:10:42 -08:00
playerdata.hidx = nil
playerdata.hud=nil
end
end
2018-12-04 20:36:23 -08:00
minetest.register_on_placenode(function(pos, newnode, player, oldnode, itemstack, pointed_thing)
if player ~= nil then
local playername = player:get_player_name()
2018-12-05 20:11:44 -08:00
xpfw.player_add_attribute(player,"build",1)
2018-12-12 09:41:56 -08:00
xpfw.player_add_attribute(player,"mean_build_speed")
2018-12-04 20:36:23 -08:00
end
end)
minetest.register_on_dieplayer(function(player, reason)
2018-12-12 20:10:42 -08:00
-- print(dump2(reason))
2018-12-04 20:36:23 -08:00
if player ~= nil then
2018-12-05 20:11:44 -08:00
xpfw.player_add_attribute(player,"deaths",1)
2018-12-04 20:36:23 -08:00
end
end)
2018-12-05 09:14:47 -08:00
2018-12-04 20:36:23 -08:00
minetest.register_on_chat_message(function(player, reason)
2018-12-12 20:10:42 -08:00
-- print(dump2(player))
2018-12-04 20:36:23 -08:00
if player ~= nil then
2018-12-10 08:34:43 -08:00
xpfw.player_add_attribute(minetest.get_player_by_name(player),"spoke",1)
2018-12-04 20:36:23 -08:00
end
end)
minetest.register_on_dignode(function(pos,oldnode,player)
2018-12-05 09:14:47 -08:00
if player ~= nil then
2018-12-05 20:11:44 -08:00
xpfw.player_add_attribute(player,"dug",1)
2018-12-12 09:41:56 -08:00
xpfw.player_add_attribute(player,"mean_dig_speed")
2018-12-03 21:31:16 -08:00
end
2018-12-04 20:36:23 -08:00
end)
2018-12-05 09:14:47 -08:00
minetest.register_on_leaveplayer(function(player)
if player ~= nil then
2018-12-21 01:12:51 -08:00
local playerdata=M.player[player:get_player_name()]
2018-12-04 20:36:23 -08:00
local leave=os.time()
xpfw.player_add_attribute(player,"logon",xpfw.player_get_attribute(player,"lastlogin")-leave)
2018-12-21 01:12:51 -08:00
local playerhud=playerdata.hud
if playerhud==nil then playerhud=0 end
xpfw.mod_storage:set_int(player:get_player_name().."_hud",playerhud)
2018-12-04 20:36:23 -08:00
end
end)
minetest.register_on_shutdown(function()
local leave=os.time()
2018-12-21 01:12:51 -08:00
print(dump2(minetest.get_connected_players()))
local players = minetest.get_connected_players()
for i=1, #players do
local player=players[i]
2018-12-21 01:12:51 -08:00
local playerdata=M.player[player]
xpfw.player_add_attribute(player,"logon",xpfw.player_get_attribute(player,"lastlogin")-leave)
2018-12-21 01:12:51 -08:00
local playerdata=M.player[player:get_player_name()]
local playerhud=playerdata.hud
if playerhud==nil then playerhud=0 end
xpfw.mod_storage:set_int(player:get_player_name().."_hud",playerhud)
end
2018-12-04 19:04:57 -08:00
end
)
2018-12-04 21:31:55 -08:00
minetest.register_globalstep(function(dtime)
local players = minetest.get_connected_players()
for i=1, #players do
local player=players[i]
local name = player:get_player_name()
if M.player[name] ~= nil then
local playerdata=M.player[name]
2018-12-10 21:22:13 -08:00
playerdata.dtime=playerdata.dtime+dtime
local act_pos=player:get_pos()
-- calculating distance to last known position
2018-12-04 21:31:55 -08:00
if playerdata.last_pos ~= nil then
local tdist=vector.distance(act_pos,playerdata.last_pos)
if tdist > 0 then
2018-12-05 20:11:44 -08:00
xpfw.player_add_attribute(player,"distance",tdist)
2018-12-04 21:31:55 -08:00
playerdata.last_pos = act_pos
end
else
playerdata.last_pos = act_pos
2018-12-04 21:31:55 -08:00
end
-- calculation walk by actual velocity
2018-12-04 21:31:55 -08:00
local tvel=player:get_player_velocity()
if tvel ~= nil then
2018-12-10 08:20:02 -08:00
local act_node=minetest.get_node(act_pos)
2018-12-10 21:22:13 -08:00
-- check if swimming
2018-12-10 08:20:02 -08:00
local vel_action="walked"
if minetest.get_item_group(act_node.name,"water")>0 then
vel_action="swam"
end
2018-12-04 21:31:55 -08:00
local tvelo=vector.distance(tvel,{x=0,y=0,z=0})
if tvelo>0 then
2018-12-10 08:20:02 -08:00
xpfw.player_add_attribute(player,vel_action,tvelo*dtime)
2018-12-11 05:04:25 -08:00
-- add experience
local mean_speed="mean_"..vel_action.."_speed"
if xpfw.attributes[mean_speed].max ~= nil then
xpfw.player_add_attribute(player,mean_speed,xpfw.attributes[mean_speed].max)
end
2018-12-04 21:31:55 -08:00
end
end
--calculating mean sun level
local light_level=minetest.get_node_light(act_pos)
if light_level ~= nil then
xpfw.player_add_attribute(player,"meanlight",light_level)
end
2018-12-10 21:22:13 -08:00
if playerdata.hidx ~= nil then
local act_logon=os.clock()-xpfw.player_get_attribute(player,"lastlogin")
local act_print=""
2018-12-11 05:04:25 -08:00
for i,att_def in pairs(xpfw.attributes) do
2018-12-10 21:22:13 -08:00
if att_def.hud ~= nil and att_def.name ~= "logon" then
2018-12-11 05:04:25 -08:00
act_print=act_print..i..": "..math.ceil(xpfw.player_get_attribute(player,att_def.name)).."\n"
2018-12-10 21:22:13 -08:00
end
end
act_print=act_print.."logon: "..math.ceil(xpfw.player_get_attribute(player,"lastlogin")+act_logon)
player:hud_change(playerdata.hidx,"text",act_print)
end
if playerdata.dtime>5 then
playerdata.dtime=0
2018-12-12 20:10:42 -08:00
-- print(dump2(player:hud_get_flags()))
2018-12-11 05:04:25 -08:00
for i,att_def in pairs(xpfw.attributes) do
local att=xpfw.attributes[i]
2018-12-12 20:10:42 -08:00
-- print(i)
2018-12-12 20:49:30 -08:00
if att_def.recreation_factor ~= nil and xpfw.player_get_attribute(player,i) > att.min and playerdata.flags[i] == nil then
2018-12-12 20:10:42 -08:00
-- print(att.min)
2018-12-12 09:41:56 -08:00
xpfw.player_sub_attribute(player,i)
2018-12-10 21:22:13 -08:00
end
2018-12-12 20:49:30 -08:00
playerdata.flags[i]=nil
2018-12-10 21:22:13 -08:00
end
end
2018-12-04 21:31:55 -08:00
end
2018-12-10 21:22:13 -08:00
2018-12-04 21:31:55 -08:00
end
end)