api.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. --[[
  2. API for using statistic via meta data
  3. ]]
  4. local M=xpfw
  5. if M.player == nil then
  6. M.player={}
  7. end
  8. if M.experiences == nil then
  9. M.experiences={}
  10. end
  11. local check_value=function(tab,val,def)
  12. -- check, if <val> exist in table <tab>. If not, set tab.val = def
  13. if tab[val] == nil then
  14. tab[val] = def
  15. end
  16. end
  17. xpfw.register_attribute=function(name,data)
  18. --[[ Register new attribute for all user with spec in data
  19. data = {name = name of attribute,
  20. min = Minimum value this attribute should be,
  21. max = Maximum value where the attribute is cut, can be inf,
  22. default = default value set at initialisation or reset,
  23. hud = if set, attribute is shown in toggable hud display,
  24. moving_average_factor = Attribute is moving average with this weighting factor,
  25. recreation_factor = During recreation the value is reduced with this weighting factor
  26. }
  27. ]]
  28. check_value(data,"min",0)
  29. check_value(data,"max",math.huge)
  30. data.name=name
  31. xpfw.attributes[name]=data
  32. if data.hud ~= nil and name ~= "logon" then
  33. table.insert(xpfw.hud_intern,name)
  34. end
  35. if data.recreation_factor ~= nil then
  36. table.insert(xpfw.attrib_recreates,name)
  37. end
  38. end
  39. local player_addsub_attribute=function(player,attrib,val,maf)
  40. --internal use for adding and substracting values
  41. local oldvalue=xpfw.player_get_attribute(player,attrib)
  42. local att_def=xpfw.attributes[attrib]
  43. local new_val = oldvalue + val
  44. if maf ~= nil then
  45. new_val=(oldvalue*maf + val)/(maf + 1)
  46. if val < 0 then
  47. new_val=math.floor(new_val)
  48. end
  49. end
  50. xpfw.player_set_attribute(player,attrib,new_val)
  51. end
  52. xpfw.player_add_attribute=function(player,attrib,val)
  53. -- add <val> to <attrib> for <player>
  54. -- if <val> = nil then set to max value
  55. local nval=val
  56. local att_def=xpfw.attributes[attrib]
  57. if att_def == nil then return end
  58. if val==nil then
  59. nval=att_def.max or 20
  60. end
  61. if att_def.moving_average_factor ~= nil then
  62. player_addsub_attribute(player,attrib,nval,att_def.moving_average_factor)
  63. else
  64. player_addsub_attribute(player,attrib,nval)
  65. end
  66. local playerdata=M.player[player:get_player_name()]
  67. playerdata.flags[attrib]=1
  68. end
  69. xpfw.player_sub_attribute=function(player,attrib,val)
  70. -- add <val> to <attrib> for <player>
  71. -- if <val> = nil then set to max value
  72. local nval=val
  73. local playername=player:get_player_name()
  74. local att_def=xpfw.attributes[attrib]
  75. if att_def == nil then return end
  76. if val==nil then
  77. nval=att_def.max or 20
  78. end
  79. if att_def.recreation_factor ~= nil then
  80. player_addsub_attribute(player,attrib,(-1)*nval,att_def.recreation_factor)
  81. else
  82. player_addsub_attribute(player,attrib,(-1)*nval)
  83. end
  84. end
  85. xpfw.player_get_attribute=function(player,attrib)
  86. --[[
  87. Get stored attribute (or 0) for specified player:
  88. - player : ObjectRef to player
  89. - attrib : name of attribute
  90. ]]
  91. local pm=player:get_meta()
  92. return pm:get_float(xpfw.prefix.."_"..attrib) or 0
  93. end
  94. xpfw.player_set_attribute=function(player,attrib,val)
  95. --[[
  96. Set stored attribute (or 0) for specified player:
  97. - player : ObjectRef to player
  98. - attrib : name of attribute
  99. - val : Value to store
  100. ]]
  101. if val == nil then return end
  102. if attrib == nil then return end
  103. if player == nil then return end
  104. local pm=player:get_meta()
  105. local playername=player:get_player_name()
  106. if M.player[playername] == nil then
  107. end
  108. local att_def=M.player[playername].attributes[attrib]
  109. pm:set_float(xpfw.prefix.."_"..attrib,math.min(att_def.max,math.max(att_def.min,val)))
  110. M.player[playername].flags[attrib]=1
  111. end
  112. xpfw.player_init_attributes=function(player)
  113. local pm=player:get_meta()
  114. local playername=player:get_player_name()
  115. M.player[playername]={last_pos=player:get_pos(), --actual position
  116. flags={},
  117. attributes=table.copy(xpfw.attributes),
  118. }
  119. local playerhud=xpfw.mod_storage:get_int(playername.."_hud")
  120. if playerhud==nil then playerhud=1 end
  121. if playerhud == 1 then
  122. M.player[playername].hud=1
  123. end
  124. for i,tdef in pairs(M.player[playername].attributes) do
  125. local rf=xpfw.mod_storage:get_int(playername.."_"..i.."_rf")
  126. local maf=xpfw.mod_storage:get_int(playername.."_"..i.."_maf")
  127. if rf>0 then
  128. M.player[playername].attributes[i].recreation_factor=rf
  129. end
  130. if maf>0 then
  131. M.player[playername].attributes[i].moving_average_factor=maf
  132. end
  133. end
  134. xpfw.player_set_attribute_to_nil(player,"meanlight")
  135. end
  136. xpfw.player_set_attribute_to_nil=function(player,attrib)
  137. --[[
  138. Delete attribute for player (set to nil):
  139. - player : ObjectRef to player
  140. - attrib : name of attribute
  141. ]]
  142. local pm=player:get_meta()
  143. local playername=player:get_player_name()
  144. local att_def=M.player[playername].attributes[attrib]
  145. pm:set_float(xpfw.prefix.."_"..attrib,-1)
  146. end
  147. xpfw.player_remove_flag=function(player,attrib)
  148. -- internal usage to check if attribute was changed since last abm call
  149. M.player[playername].flags[attrib]=nil
  150. end
  151. xpfw.player_ping_attribute=function(player,attrib)
  152. --[[
  153. "Ping" an attribute for player:
  154. - player : ObjectRef to player
  155. - attrib : name of attribute
  156. used for moving averaged attributes. Get max value from definition and add to attribute, which calcs
  157. a moving average.
  158. ]]
  159. local playername=player:get_player_name()
  160. local att_def=M.player[playername].attributes[attrib]
  161. local ping_max=att_def.max
  162. if ping_max == nil then
  163. ping_max=xpfw.experience_max
  164. xpfw.attributes[attrib]["max"]=xpfw.experience_max
  165. else
  166. if ping_max==math.huge then
  167. ping_max=xpfw.experience_max
  168. xpfw.attributes[attrib]["max"]=xpfw.experience_max
  169. end
  170. end
  171. if att_def.moving_average_factor == nil then
  172. att_def.moving_average_factor=xpfw.mean_weight
  173. end
  174. xpfw.player_add_attribute(player,attrib,ping_max)
  175. end
  176. M.register_experience=function(name,indata)
  177. local tid=table.copy(indata)
  178. tid.name=name
  179. check_value(tid,"default",0)
  180. check_value(tid,"decay",0)
  181. M.experiences[name]=tid
  182. end
  183. xpfw.player_reset_single_attribute=function(player,attribute)
  184. if attribute == nil then
  185. return
  186. end
  187. local att_def=xpfw.attributes[attribute]
  188. if att_def ~= nil then
  189. local setval=att_def.min or 0
  190. if att_def.default ~= nil then
  191. setval= att_def.default
  192. end
  193. xpfw.player_set_attribute(player,att_def.name,setval)
  194. end
  195. end
  196. xpfw.player_reset_attributes=function(player,attribute)
  197. if attribute==nil then
  198. for i,att_def in pairs(xpfw.attributes) do
  199. xpfw.player_reset_single_attribute(player,i)
  200. end
  201. else
  202. xpfw.player_reset_single_attribute(player,attribute)
  203. end
  204. end
  205. xpfw.player_hud_toggle=function(name)
  206. -- toggle hud display for player with string <name>
  207. local player=minetest.get_player_by_name(name)
  208. local playerdata=M.player[name]
  209. if playerdata==nil then
  210. return
  211. end
  212. if playerdata.hidx==nil then
  213. xpfw.player_add_hud(player)
  214. else
  215. xpfw.player_remove_hud(player)
  216. end
  217. end
  218. xpfw.player_add_hud=function(player)
  219. local playerdata=M.player[player:get_player_name()]
  220. if playerdata==nil then
  221. return
  222. end
  223. if playerdata.hud == nil then
  224. playerdata.hud=1
  225. end
  226. playerdata.hidx=player:hud_add({
  227. hud_elem_type = "text",
  228. position = {x=1,y=1},
  229. size = "",
  230. text = "",
  231. alignment = {x=-1,y=-1},
  232. })
  233. end
  234. xpfw.player_remove_hud=function(player)
  235. local playerdata=M.player[player:get_player_name()]
  236. if playerdata==nil then
  237. return
  238. end
  239. if playerdata.hidx ~= nil then
  240. player:hud_remove(playerdata.hidx)
  241. playerdata.hidx = nil
  242. playerdata.hud=nil
  243. end
  244. end
  245. xpfw.save_player_data=function(player)
  246. local playerdata=M.player[player:get_player_name()]
  247. for i,tdef in pairs(playerdata.attributes) do
  248. if tdef.moving_average_factor ~= nil then
  249. xpfw.mod_storage:set_int(player:get_player_name().."_"..i.."_maf",tdef.moving_average_factor)
  250. end
  251. if tdef.recreation_factor ~= nil then
  252. xpfw.mod_storage:set_int(player:get_player_name().."_"..i.."_rf",tdef.recreation_factor)
  253. end
  254. end
  255. end