Advertisement
Guest User

Untitled

a guest
Jan 13th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. --- Computes a "temporary" value into a property
  2. -- Example: You cant to give an actor a boost to life_regen, but you do not want it to be permanent<br/>
  3. -- You cannot simply increase life_regen, so you use this method which will increase it AND
  4. -- store the increase. it will return an "increase id" that can be passed to removeTemporaryValue()
  5. -- to remove the effect.
  6. -- @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.
  7. -- @param v the value to add.  This should either be a number or a table of properties and numbers.
  8. -- @param noupdate if true the actual property is not changed and needs to be changed by the caller
  9. -- @return an id that can be passed to removeTemporaryValue() to delete this value
  10. function _M:addTemporaryValue(prop, v, noupdate)
  11.     if not self.compute_vals then self.compute_vals = {n=0} end
  12.  
  13.     local t = self.compute_vals
  14.     local id = t.n + 1
  15.     while t[id] ~= nil do id = id + 1 end
  16.     t[id] = v
  17.     t.n = id
  18.  
  19.     -- Find the base, one removed from the last prop
  20.     local initial_base, initial_prop
  21.     if type(prop) == "table" then
  22.         initial_base = self
  23.         local idx = 1
  24.         while idx < #prop do
  25.             initial_base = initial_base[prop[idx]]
  26.             idx = idx + 1
  27.         end
  28.         initial_prop = prop[idx]
  29.     else
  30.         initial_base = self
  31.         initial_prop = prop
  32.     end
  33.  
  34.     -- The recursive enclosure
  35.     local recursive
  36.     recursive = function(base, prop, v)
  37.         if type(v) == "number" then
  38.             -- Simple addition
  39.             base[prop] = (base[prop] or 0) + v
  40.             self:onTemporaryValueChange(prop, nil, v)
  41.             print("addTmpVal", base.name or base, prop, v, " :=: ", #t, id)
  42.         elseif type(v) == "table" then
  43.             print("addTmpValTable", base.name or base, base[prop])
  44.             for k, e in pairs(v) do
  45.                 base[prop] = base[prop] or {}
  46.                 recursive(base[prop], k, e)
  47.             end
  48.         else
  49.             error("unsupported temporary value type: "..type(v).." :=: "..v)
  50.         end
  51.     end
  52.    
  53.     -- Update the base prop
  54.     if not noupdate then
  55.         recursive(initial_base, initial_prop, v)
  56.     end
  57.  
  58.     return id
  59. end
  60.  
  61. --- Removes a temporary value, see addTemporaryValue()
  62. -- @param prop the property to affect
  63. -- @param id the id of the increase to delete
  64. -- @param noupdate if true the actual property is not changed and needs to be changed by the caller
  65. function _M:removeTemporaryValue(prop, id, noupdate)
  66.     local oldval = self.compute_vals[id]
  67.     print("removeTempVal", prop, oldval, " :=: ", id)
  68.     self.compute_vals[id] = nil
  69.  
  70.     -- Find the base, one removed from the last prop
  71.     local initial_base, initial_prop
  72.     if type(prop) == "table" then
  73.         initial_base = self
  74.         local idx = 1
  75.         while idx < #prop do
  76.             initial_base = initial_base[prop[idx]]
  77.             idx = idx + 1
  78.         end
  79.         initial_prop = prop[idx]
  80.     else
  81.         initial_base = self
  82.         initial_prop = prop
  83.     end
  84.  
  85.     -- The recursive enclosure
  86.     local recursive
  87.     recursive = function(base, prop, v)
  88.         if type(v) == "number" then
  89.             -- Simple addition
  90.             base[prop] = base[prop] - v
  91.             self:onTemporaryValueChange(prop, nil, v)
  92.             print("removeTmpVal", base.name or base, prop, v)
  93.         elseif type(v) == "table" then
  94.             print("removeTmpValTable", base.name or base, base[prop])
  95.             for k, e in pairs(v) do
  96.                 recursive(base[prop], k, e)
  97.             end
  98.         else
  99.             error("unsupported temporary value type: "..type(v).." :=: "..v)
  100.         end
  101.     end
  102.    
  103.     -- Update the base prop
  104.     if not noupdate then
  105.         recursive(initial_base, initial_prop, oldval)
  106.     end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement