Version 1.1.0
[minetest_playerphysics.git] / init.lua
blobb410b025300671efac1f8069db3817832d7ae5cb
1 playerphysics = {}
3 local function calculate_attribute_product(player, attribute)
4 local a = minetest.deserialize(player:get_meta():get_string("playerphysics:physics"))
5 local product = 1
6 if a == nil or a[attribute] == nil then
7 return product
8 end
9 local factors = a[attribute]
10 if type(factors) == "table" then
11 for _, factor in pairs(factors) do
12 product = product * factor
13 end
14 end
15 return product
16 end
18 function playerphysics.add_physics_factor(player, attribute, id, value)
19 local meta = player:get_meta()
20 local a = minetest.deserialize(meta:get_string("playerphysics:physics"))
21 if a == nil then
22 a = { [attribute] = { [id] = value } }
23 elseif a[attribute] == nil then
24 a[attribute] = { [id] = value }
25 else
26 a[attribute][id] = value
27 end
28 meta:set_string("playerphysics:physics", minetest.serialize(a))
29 local raw_value = calculate_attribute_product(player, attribute)
30 player:set_physics_override({[attribute] = raw_value})
31 end
33 function playerphysics.remove_physics_factor(player, attribute, id)
34 local meta = player:get_meta()
35 local a = minetest.deserialize(meta:get_string("playerphysics:physics"))
36 if a == nil or a[attribute] == nil then
37 -- Nothing to remove
38 return
39 else
40 a[attribute][id] = nil
41 end
42 meta:set_string("playerphysics:physics", minetest.serialize(a))
43 local raw_value = calculate_attribute_product(player, attribute)
44 player:set_physics_override({[attribute] = raw_value})
45 end
47 function playerphysics.get_physics_factor(player, attribute, id)
48 local meta = player:get_meta()
49 local a = minetest.deserialize(meta:get_string("playerphysics:physics"))
50 if a == nil then
51 return nil
52 elseif a[attribute] == nil then
53 return nil
54 else
55 return a[attribute][id]
56 end
57 end