Commit 49efd9398b3cae576a8d941808a435230efe5db6

Authored by DarkGod
2 parents c1f9f575 691381e8

Merge branch 'inconstant-resource-drain' into 'master'

Support inconstant resource drain

Make things like `drain_vim` recalculate in every turn.

This will fix the problem with `Fearscape`, and the regen value of `Dream Prison` and `Meditation` will be automatically updated.

It will also make addon maker easier to make sustained talent with changing resource drain.

See merge request !727
... ... @@ -579,6 +579,18 @@ function _M:actBase()
579 579 end
580 580 end
581 581
  582 + for _, defs in pairs(self._applied_resources_inconstant_drain or {}) do
  583 + local new_cost = util.getval(defs.cost, self, defs.ab) or 0
  584 + new_cost = self:alterTalentCost(defs.ab, defs.res_def.drain_prop, new_cost)
  585 + if not defs.res_def.invert_values then
  586 + new_cost = - new_cost
  587 + end
  588 + if defs.old_cost ~= new_cost then
  589 + self:removeTemporaryValue(defs.res_def.regen_prop, defs.temporary_value)
  590 + defs.temporary_value = self:addTemporaryValue(defs.res_def.regen_prop, new_cost)
  591 + defs.old_cost = new_cost
  592 + end
  593 + end
582 594 self:regenResources()
583 595
584 596 -- update psionic feedback
... ... @@ -6350,7 +6362,7 @@ function _M:postUseTalent(ab, ret, silent)
6350 6362 trigger = true; self:incMaxFeedback(-util.getval(ab.sustain_feedback, self, ab))
6351 6363 end
6352 6364 local cost
6353   - ret._applied_costs, ret._applied_drains = {}, {} -- to store the resource effects
  6365 + ret._applied_costs, ret._applied_drains, ret._applied_inconstant_drains = {}, {}, {} -- to store the resource effects
6354 6366 for res, res_def in ipairs(_M.resources_def) do
6355 6367 -- apply sustain costs
6356 6368 cost = ab[res_def.sustain_prop]
... ... @@ -6369,7 +6381,19 @@ function _M:postUseTalent(ab, ret, silent)
6369 6381 end
6370 6382 -- apply drain costs
6371 6383 cost = ab[res_def.drain_prop]
6372   - if cost then
  6384 + if type(cost) == "function" then -- non-constant resource drain
  6385 + local init_cost = util.getval(cost, self, ab) or 0
  6386 + init_cost = self:alterTalentCost(ab, res_def.drain_prop, init_cost)
  6387 + if not res_def.invert_values then
  6388 + init_cost = - init_cost
  6389 + end
  6390 + local temporary_value = self:addTemporaryValue(res_def.regen_prop, init_cost)
  6391 + local tid_res = ab.id .. "_" .. res_def.drain_prop
  6392 + self._applied_resources_inconstant_drain = self._applied_resources_inconstant_drain or {}
  6393 + self._applied_resources_inconstant_drain[tid_res] = {ab = ab, res_def = res_def, cost = cost, old_cost = init_cost, temporary_value = temporary_value}
  6394 + ret._applied_inconstant_drains[res_def.short_name] = tid_res
  6395 + trigger = true
  6396 + elseif cost then
6373 6397 cost = util.getval(cost, self, ab) or 0
6374 6398 cost = self:alterTalentCost(ab, res_def.drain_prop, cost)
6375 6399 if cost ~= 0 then
... ... @@ -6412,6 +6436,14 @@ function _M:postUseTalent(ab, ret, silent)
6412 6436 end
6413 6437 end
6414 6438 end
  6439 + -- reverse non-constant resource drain
  6440 + if ret._applied_inconstant_drains then
  6441 + for _, tid_res in pairs(ret._applied_inconstant_drains) do
  6442 + local defs = self._applied_resources_inconstant_drain[tid_res]
  6443 + self:removeTemporaryValue(defs.res_def.regen_prop, defs.temporary_value)
  6444 + self._applied_resources_inconstant_drain[tid_res] = nil
  6445 + end
  6446 + end
6415 6447 -- reverse resource drains
6416 6448 if ret._applied_drains then
6417 6449 local res_def
... ...
... ... @@ -125,9 +125,7 @@ newTalent{
125 125 callbackOnActBase = function(self, t)
126 126 local p = self:isTalentActive(t.id)
127 127 if not p then return end
128   - if p.stamina then self:removeTemporaryValue("stamina_regen", p.stamina) end
129 128 if p.turns then p.turns = p.turns + 1 end
130   - p.stamina = self:addTemporaryValue("stamina_regen", -(t.drain_stamina(self, t, p.turns) - t.drain_stamina(self, t, 0)))
131 129
132 130 -- This should be redundant but there are cases the value won't properly update, ie direct life reductions
133 131 if p.resid then self:removeTemporaryValue("resists", p.resid) end
... ...