Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Computes a "temporary" value into a property
- -- Example: You cant to give an actor a boost to life_regen, but you do not want it to be permanent<br/>
- -- You cannot simply increase life_regen, so you use this method which will increase it AND
- -- store the increase. it will return an "increase id" that can be passed to removeTemporaryValue()
- -- to remove the effect.
- -- @param prop the property to affect. This can be either a string or a table of strings, the latter allowing nested properties to be modified.
- -- @param v the value to add. This should either be a number or a table of properties and numbers.
- -- @param noupdate if true the actual property is not changed and needs to be changed by the caller
- -- @return an id that can be passed to removeTemporaryValue() to delete this value
- function _M:addTemporaryValue(prop, v, noupdate)
- if not self.compute_vals then self.compute_vals = {n=0} end
- local t = self.compute_vals
- local id = t.n + 1
- while t[id] ~= nil do id = id + 1 end
- t[id] = v
- t.n = id
- -- Find the base, one removed from the last prop
- local initial_base, initial_prop
- if type(prop) == "table" then
- initial_base = self
- local idx = 1
- while idx < #prop do
- initial_base = initial_base[prop[idx]]
- idx = idx + 1
- end
- initial_prop = prop[idx]
- else
- initial_base = self
- initial_prop = prop
- end
- -- The recursive enclosure
- local recursive
- recursive = function(base, prop, v)
- if type(v) == "number" then
- -- Simple addition
- base[prop] = (base[prop] or 0) + v
- self:onTemporaryValueChange(prop, nil, v)
- print("addTmpVal", base.name or base, prop, v, " :=: ", #t, id)
- elseif type(v) == "table" then
- print("addTmpValTable", base.name or base, base[prop])
- for k, e in pairs(v) do
- base[prop] = base[prop] or {}
- recursive(base[prop], k, e)
- end
- else
- error("unsupported temporary value type: "..type(v).." :=: "..v)
- end
- end
- -- Update the base prop
- if not noupdate then
- recursive(initial_base, initial_prop, v)
- end
- return id
- end
- --- Removes a temporary value, see addTemporaryValue()
- -- @param prop the property to affect
- -- @param id the id of the increase to delete
- -- @param noupdate if true the actual property is not changed and needs to be changed by the caller
- function _M:removeTemporaryValue(prop, id, noupdate)
- local oldval = self.compute_vals[id]
- print("removeTempVal", prop, oldval, " :=: ", id)
- self.compute_vals[id] = nil
- -- Find the base, one removed from the last prop
- local initial_base, initial_prop
- if type(prop) == "table" then
- initial_base = self
- local idx = 1
- while idx < #prop do
- initial_base = initial_base[prop[idx]]
- idx = idx + 1
- end
- initial_prop = prop[idx]
- else
- initial_base = self
- initial_prop = prop
- end
- -- The recursive enclosure
- local recursive
- recursive = function(base, prop, v)
- if type(v) == "number" then
- -- Simple addition
- base[prop] = base[prop] - v
- self:onTemporaryValueChange(prop, nil, v)
- print("removeTmpVal", base.name or base, prop, v)
- elseif type(v) == "table" then
- print("removeTmpValTable", base.name or base, base[prop])
- for k, e in pairs(v) do
- recursive(base[prop], k, e)
- end
- else
- error("unsupported temporary value type: "..type(v).." :=: "..v)
- end
- end
- -- Update the base prop
- if not noupdate then
- recursive(initial_base, initial_prop, oldval)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement