2014-07-03 13:18:00 -06:00
|
|
|
--lua/syndrome-util.lua
|
2014-06-26 06:36:57 -06:00
|
|
|
--author expwnent
|
|
|
|
--some utilities for adding syndromes to units
|
|
|
|
|
2014-07-03 13:18:00 -06:00
|
|
|
local _ENV = mkmodule("syndrome-util")
|
2014-06-27 03:47:52 -06:00
|
|
|
local utils = require("utils")
|
2014-06-26 06:36:57 -06:00
|
|
|
|
|
|
|
function findUnitSyndrome(unit,syn_id)
|
|
|
|
for index,syndrome in ipairs(unit.syndromes.active) do
|
2014-07-03 13:18:00 -06:00
|
|
|
if syndrome['type'] == syn_id then
|
2014-06-26 06:36:57 -06:00
|
|
|
return syndrome
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2014-07-09 04:21:52 -06:00
|
|
|
--usage: syndromeUtil.ResetPolicy.DoNothing, syndromeUtil.ResetPolicy.ResetDuration, etc
|
2014-06-27 03:47:52 -06:00
|
|
|
ResetPolicy = ResetPolicy or utils.invert({
|
2014-06-26 06:36:57 -06:00
|
|
|
"DoNothing",
|
|
|
|
"ResetDuration",
|
|
|
|
"AddDuration",
|
|
|
|
"NewInstance"
|
|
|
|
})
|
|
|
|
|
2018-04-20 09:14:03 -06:00
|
|
|
function eraseSyndromeData(unit,syndromeId,oldestFirst)
|
|
|
|
for i = (oldestFirst and 0) or #unit.body.wounds-1,(oldestFirst and #unit.body.wounds-1) or 0,(oldestFirst and 1) or -1 do
|
|
|
|
if unit.body.wounds[i].syndrome_id == syndromeId then
|
|
|
|
unit.body.wounds:erase(i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-26 06:36:57 -06:00
|
|
|
function eraseSyndrome(unit,syndromeId,oldestFirst)
|
|
|
|
local i1
|
|
|
|
local iN
|
|
|
|
local d
|
|
|
|
if oldestFirst then
|
|
|
|
i1 = 0
|
|
|
|
iN = #unit.syndromes.active-1
|
|
|
|
d = 1
|
|
|
|
else
|
|
|
|
i1 = #unit.syndromes.active-1
|
|
|
|
iN = 0
|
|
|
|
d = -1
|
|
|
|
end
|
|
|
|
local syndromes = unit.syndromes.active
|
|
|
|
for i=i1,iN,d do
|
2014-07-09 04:21:52 -06:00
|
|
|
if syndromes[i]['type'] == syndromeId then
|
2018-04-20 09:14:03 -06:00
|
|
|
eraseSyndromeData(unit,syndromeId,oldestFirst)
|
2014-06-26 06:36:57 -06:00
|
|
|
syndromes:erase(i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-11-14 21:33:49 -07:00
|
|
|
local function eraseSyndromeClassHelper(unit,synclass)
|
|
|
|
for i,unitSyndrome in ipairs(unit.syndromes.active) do
|
|
|
|
local syndrome = df.syndrome.find(unitSyndrome.type)
|
|
|
|
for _,class in ipairs(syndrome.syn_class) do
|
|
|
|
if class.value == synclass then
|
2018-04-20 09:14:03 -06:00
|
|
|
eraseSyndromeData(unit,syndrome.id)
|
2014-11-14 21:33:49 -07:00
|
|
|
unit.syndromes.active:erase(i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function eraseSyndromeClass(unit,synclass)
|
|
|
|
local count=0
|
|
|
|
while eraseSyndromeClassHelper(unit,synclass) do
|
|
|
|
count = count+1
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2014-06-26 06:36:57 -06:00
|
|
|
function eraseSyndromes(unit,syndromeId)
|
|
|
|
local count=0
|
|
|
|
while eraseSyndrome(unit,syndromeId,true) do
|
|
|
|
count = count+1
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
2014-07-09 04:21:52 -06:00
|
|
|
|
|
|
|
--target is a df.unit, syndrome is a df.syndrome, resetPolicy is one of syndromeUtil.ResetPolicy
|
2014-06-26 06:36:57 -06:00
|
|
|
--if the target has an instance of the syndrome already, the reset policy takes effect
|
|
|
|
--returns true if the unit did not have the syndrome before calling and false otherwise
|
|
|
|
function infectWithSyndrome(target,syndrome,resetPolicy)
|
|
|
|
local oldSyndrome = findUnitSyndrome(target,syndrome.id)
|
|
|
|
if oldSyndrome == nil or resetPolicy == nil or resetPolicy == ResetPolicy.NewInstance then
|
|
|
|
local unitSyndrome = df.unit_syndrome:new()
|
|
|
|
unitSyndrome.type = syndrome.id
|
|
|
|
unitSyndrome.year = df.global.cur_year
|
|
|
|
unitSyndrome.year_time = df.global.cur_year_tick
|
|
|
|
unitSyndrome.ticks = 0
|
|
|
|
unitSyndrome.wound_id = -1
|
|
|
|
for k,v in ipairs(syndrome.ce) do
|
|
|
|
local symptom = df.unit_syndrome.T_symptoms:new()
|
|
|
|
symptom.quantity = 0
|
|
|
|
symptom.delay = 0
|
|
|
|
symptom.ticks = 0
|
2014-06-27 03:47:52 -06:00
|
|
|
symptom.flags.active = true
|
2014-06-26 06:36:57 -06:00
|
|
|
unitSyndrome.symptoms:insert("#",symptom)
|
|
|
|
end
|
|
|
|
target.syndromes.active:insert("#",unitSyndrome)
|
|
|
|
elseif resetPolicy == ResetPolicy.DoNothing then
|
|
|
|
elseif resetPolicy == ResetPolicy.ResetDuration then
|
|
|
|
for k,symptom in ipairs(oldSyndrome.symptoms) do
|
|
|
|
symptom.ticks = 0
|
|
|
|
end
|
|
|
|
oldSyndrome.ticks = 0
|
|
|
|
elseif resetPolicy == ResetPolicy.AddDuration then
|
|
|
|
for k,symptom in ipairs(oldSyndrome.symptoms) do
|
|
|
|
--really it's syndrome.ce[k].end, but lua doesn't like that because keywords
|
|
|
|
if syndrome.ce[k]["end"] ~= -1 then
|
|
|
|
symptom.ticks = symptom.ticks - syndrome.ce[k]["end"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else qerror("Bad reset policy: " .. resetPolicy)
|
|
|
|
end
|
|
|
|
return (oldSyndrome == nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
function isValidTarget(unit,syndrome)
|
|
|
|
--mostly copied from itemsyndrome, which is based on autoSyndrome
|
|
|
|
if
|
|
|
|
#syndrome.syn_affected_class==0
|
|
|
|
and #syndrome.syn_affected_creature==0
|
|
|
|
and #syndrome.syn_affected_caste==0
|
|
|
|
and #syndrome.syn_immune_class==0
|
|
|
|
and #syndrome.syn_immune_creature==0
|
|
|
|
and #syndrome.syn_immune_caste==0
|
|
|
|
then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
local affected = false
|
|
|
|
local unitRaws = df.creature_raw.find(unit.race)
|
|
|
|
local casteRaws = unitRaws.caste[unit.caste]
|
|
|
|
local unitRaceName = unitRaws.creature_id
|
2014-07-03 13:18:00 -06:00
|
|
|
local unitCasteName = casteRaws.caste_id
|
2014-06-26 06:36:57 -06:00
|
|
|
local unitClasses = casteRaws.creature_class
|
|
|
|
for _,unitClass in ipairs(unitClasses) do
|
|
|
|
for _,syndromeClass in ipairs(syndrome.syn_affected_class) do
|
|
|
|
if unitClass.value==syndromeClass.value then
|
|
|
|
affected = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for caste,creature in ipairs(syndrome.syn_affected_creature) do
|
|
|
|
local affectedCreature = creature.value
|
2014-10-04 19:54:07 -06:00
|
|
|
local affectedCaste = syndrome.syn_affected_caste[caste].value
|
2014-07-03 13:18:00 -06:00
|
|
|
if affectedCreature == unitRaceName and (affectedCaste == unitCasteName or affectedCaste == "ALL") then
|
2014-06-26 06:36:57 -06:00
|
|
|
affected = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _,unitClass in ipairs(unitClasses) do
|
|
|
|
for _,syndromeClass in ipairs(syndrome.syn_immune_class) do
|
|
|
|
if unitClass.value == syndromeClass.value then
|
|
|
|
affected = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-07-03 13:18:00 -06:00
|
|
|
for caste,creature in ipairs(syndrome.syn_immune_creature) do
|
2014-06-26 06:36:57 -06:00
|
|
|
local immuneCreature = creature.value
|
|
|
|
local immuneCaste = syndrome.syn_immune_caste[caste].value
|
2014-07-03 13:18:00 -06:00
|
|
|
if immuneCreature == unitRaceName and (immuneCaste == unitCasteName or immuneCaste == "ALL") then
|
2014-06-26 06:36:57 -06:00
|
|
|
affected = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return affected
|
|
|
|
end
|
|
|
|
|
|
|
|
function infectWithSyndromeIfValidTarget(target,syndrome,resetPolicy)
|
2014-06-27 03:47:52 -06:00
|
|
|
if isValidTarget(target,syndrome) then
|
2014-06-26 06:36:57 -06:00
|
|
|
infectWithSyndrome(target,syndrome,resetPolicy)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return _ENV
|