Difference between revisions of "Module:Test/data/virtual"

From RimWorld Wiki
Jump to navigation Jump to search
(support for extra precalculated fields)
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- New wiki page: Remove this with text of the new pagelocal DefInfo = DefInfo or require "DefInfo"
+
----------
 +
-- diet --
 +
----------
 +
local VF = {}
 +
VF.diet = {}
 +
VF.diet.foodType = {
 +
  None = true,
 +
  VegetableOrFruit = true,
 +
  Meat = true,
 +
  Fluid = true,
 +
  Corpse = true,
 +
  Seed = true,
 +
  AnimalProduct = true,
 +
  Plant = true,
 +
  Tree = true,
 +
  Meal = true,
 +
  Processed = true,
 +
  Liquor = true,
 +
  Kibble = true,
 +
  VegetarianAnimal = {
 +
    VegetableOrFruit = true,
 +
    Seed = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Liquor = true,
 +
    Kibble = true
 +
  },
 +
  VegetarianRoughAnimal = {
 +
    VegetableOrFruit = true,
 +
    Seed = true,
 +
    Plant = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Liquor = true,
 +
    Kibble = true
 +
  },
 +
  CarnivoreAnimal = {
 +
    Meat = true,
 +
    Corpse = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Kibble = true
 +
  },
 +
  CarnivoreAnimalStrict = {
 +
    Meat = true,
 +
    Corpse = true
 +
  },
 +
  OmnivoreAnimal = {
 +
    VegetableOrFruit = true,
 +
    Meat = true,
 +
    Corpse = true,
 +
    Seed = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Liquor = true,
 +
    Kibble = true
 +
  },
 +
  OmnivoreRoughAnimal = {
 +
    VegetableOrFruit = true,
 +
    Meat = true,
 +
    Corpse = true,
 +
    Seed = true,
 +
    Plant = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Liquor = true,
 +
    Kibble = true
 +
  },
 +
  DendrovoreAnimal = {
 +
    VegetableOrFruit = true,
 +
    Seed = true,
 +
    Tree = true,
 +
    Processed = true,
 +
    Kibble = true
 +
  },
 +
  OvivoreAnimal = {
 +
    AnimalProduct = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Kibble = true
 +
  },
 +
  OmnivoreHuman = {
 +
    VegetableOrFruit = true,
 +
    Meat = true,
 +
    Fluid = true,
 +
    Corpse = true,
 +
    Seed = true,
 +
    AnimalProduct = true,
 +
    Meal = true,
 +
    Processed = true,
 +
    Liquor = true,
 +
    Kibble = true
 +
  }
 +
}
 +
 
 +
--~ function VF.diet.resolveDietCategory(foodType)
 +
--~  if util.diet[foodType].None then
 +
--~      return "Never eats"
 +
--~  end
 +
--~  if util.diet[foodType].Tree then
 +
--~      return "Dendrovorous"
 +
--~  end
 +
--~  if util.diet[foodType].Meat then
 +
--~      if util.diet[foodType].VegetableOrFruit or util.diet[foodType].Plant then
 +
--~          return "Omnivorous"
 +
--~      end
 +
--~      return "Carnivorous"
 +
--~  end
 +
--~  if util.diet[foodType].AnimalProduct then
 +
--~      return "Ovivorous"
 +
--~  end
 +
--~  return "Herbivorous"
 +
--~ end
 +
 
 +
--~ ------------------------
 +
--~ -- module entry point --
 +
--~ ------------------------
 +
--~ function VF.expand(mergedDef)
 +
--~  for k,func in pairs(VF.vfields) do
 +
--~    func(mergedDef)
 +
--~  end
 +
--~ end
 +
 
 +
return {
 +
 
 +
  --------------
 +
  -- tool DPS --
 +
  --------------
 +
  toolDPS = function (def)
 +
    if not def.tools then return nil end
 +
 
 +
    for _,tool in ipairs(def.tools) do
 +
      if tool.power and tool.cooldownTime then
 +
        local dps = tool.power / tool.cooldownTime
 +
        tool.DPS = Util.round(dps, 3)
 +
      end
 +
    end
 +
    return true
 +
  end,
 +
 
 +
  ---------------
 +
  -- verb DPS --
 +
  ---------------
 +
  verbDPS = function (def)
 +
    local filters = {
 +
      {'verbs', 1, 'defaultProjectile'},
 +
      {'verbs', 1, 'warmupTime'},
 +
      {'statBases', 'RangedWeapon_Cooldown'}
 +
    }
 +
    if not Util.table.checkMultiple(def, filters) then return nil end
 +
 
 +
    local projectileDef
 +
    local damageDef
  
local VF = {}
+
    local damage
VF.vfields = {}
+
    local warmup
 +
    local cooldown
 +
    local burst
 +
    local burstPause
 +
    local dps
 +
 
 +
    projectileDef = DefInfo.getDef(def.verbs[1].defaultProjectile, false)
 +
    if not projectileDef then return nil end
 +
 
 +
    -- def.verbs[1].defaultProjectile = projectile
  
---------------
+
    if Util.table.check(projectileDef, 'projectile', 'damageAmountBase') then
-- tools.DPS --
+
      damage = projectileDef.projectile.damageAmountBase
---------------
+
    elseif Util.table.check(projectileDef, 'projectile', 'damageDef') and projectileDef.projectile.damageDef == 'Bomb' then
function VF.vfields.unarmedDPS(def)
+
      damageDef = DefInfo.getDef(projectileDef.projectile.damageDef, false)
  if not def.tools then return nil end
+
      if damageDef then
 +
        damage = damageDef.defaultDamage
 +
        -- def.verbs[1].defaultProjectile.projectile.damageDef = damageDef
 +
      end
 +
    end
  
  for _,tool in ipairs(def.tools) do
+
    warmup    = def.verbs[1].warmupTime
     local dps = tool.power / tool.cooldownTime
+
     burst      = def.verbs[1].burstShotCount
     if dps then tool.DPS = Util.round(dps, 3) end
+
     burstPause = def.verbs[1].ticksBetweenBurstShots
   end
+
    cooldown   = def.statBases.RangedWeapon_Cooldown
end
 
  
---------------
+
    if burst and pause then
-- verbs.DPS --
+
      dps = damage * burst / (warmup + cooldown + (burst-1)*(burstPause/60))
---------------
+
     else
function VF.vfields.verbsDPS(def)
+
      dps = damage / (warmup + cooldown)
  local filters = {
+
     end
     {'verbs', 1, 'defaultProjectile'},
 
    {'verbs', 1, 'warmupTime'},
 
     {'statBases', 'RangedWeapon_Cooldown'}
 
  }
 
  if not Util.table.checkMultiple(def, filters) then return nil end
 
  
  local projectile = DefInfo.getDef(def.verbs[1].defaultProjectile)
+
    if dps then
  local warmup     = def.verbs[1].warmupTime
+
      def.verbs[1].DPS = Util.round(dps, 3)
   local cooldown  = def.statBases.RangedWeapon_Cooldown
+
      return true
 +
     end
 +
   end,
  
   if not projectile then return nil end
+
   -------------------------------------
 +
  -- naturally lives in these biomes --
 +
  -------------------------------------
 +
  pawnLivesIn = function (def)
 +
    if def.thingClass ~= 'Pawn' then return nil end
  
  local burst      = def.verbs[1].burstShotCount
+
    local biomes = {}
  local pause      = def.verbs[1].ticksBetweenBurstShots
 
  local damage    = Util.table.check(projectile, 'projectile', 'damageAmountBase') and projectile.projectile.damageAmountBase
 
  local dps
 
  
  if burst and pause then
+
    for biomeK,biome in pairs(Data) do
    dps = damage * burst / (warmup + cooldown + burst*(pause/60))
+
      if Util.table.check(biome, 'wildAnimals') then
  else
+
        for animalK,animal in pairs(biome.wildAnimals) do
    dps = damage / (warmup + cooldown)
+
          if animalK == def.defName then
  end
+
            table.insert(biomes, biome.defName)
 +
          end
 +
        end
 +
      end
 +
    end
  
  if dps then
+
    if #biomes > 0 then
    def.verbs[1].DPS = Util.round(dps, 3)
+
      def._.livesIn = biomes
  end
+
      return true
end
+
    end
 +
  end,
  
-------------------------------------
+
  -----------------------
-- naturally lives in these biomes --
+
  -- foodType expanded --
-------------------------------------
+
  -----------------------
function VF.vfields.raceLivesIn(def)
+
  foodTypeExpanded = function (def)
  if def.thingClass ~= 'Pawn' then return nil end
+
    if def.thingClass ~= 'Pawn' then return nil end
 +
    -- if not Util.table.check(def, 'race', 'foodType') then return nil end
  
  local biomes = {}
+
    local diet = def.race.foodType
 +
    local flags = {}
 +
    local expandedFoodTypes = {}
  
  for biomeK,biome in pairs(Data) do
+
    for _,foodType in ipairs(diet) do
    if Util.table.check(biome, 'wildAnimals') then
+
      if type(VF.diet.foodType[foodType]) == "boolean" then
       for animalK,animal in pairs(biome.wildAnimals) do
+
        flags[foodType] = true
        if animalK == def.defName then
+
       else
          table.insert(biomes, biome.defName)
+
        for foodItem,_ in pairs(VF.diet.foodType[foodType]) do
 +
          flags[foodItem] = true
 
         end
 
         end
 
       end
 
       end
 
     end
 
     end
  end
 
  
  if #biomes > 0 then
+
    for flag,_ in pairs(flags) do
     def._.livesIn = biomes
+
      table.insert(expandedFoodTypes, flag)
 +
    end
 +
 
 +
    if #expandedFoodTypes > 0 then
 +
      def.race.foodTypeExpanded = expandedFoodTypes
 +
      return true
 +
    end
 +
  end,
 +
 
 +
  -----------------
 +
  -- MarketValue -- works only for items (not stuffables)
 +
  -----------------
 +
  MarketValue = function (def)
 +
    if Util.table.check(def, 'statBases', 'MarketValue') then return nil end
 +
     if not Util.table.check(def, 'statBases', 'WorkToMake') then return nil end
 +
    if not Util.table.check(def, 'costList') then return nil end
 +
 
 +
    local workRate = 0.0036
 +
    local workCost
 +
    local ingredientCost = 0
 +
 
 +
    if def.costList then
 +
      for k,v in pairs(def.costList) do
 +
        ingredientCost = ingredientCost + Data['ThingDef:'..k].statBases.MarketValue * v
 +
      end
 +
    end
 +
 
 +
    workCost = def.statBases.WorkToMake * workRate
 +
 
 +
    def.statBases.MarketValue = ingredientCost + workCost
 +
 
 +
    return true
 
   end
 
   end
end
 
  
------------------------
 
-- module entry point --
 
------------------------
 
function VF.expand(mergedDef)
 
  for k,func in pairs(VF.vfields) do
 
    func(mergedDef)
 
  end
 
end
 
  
return VF
+
}

Latest revision as of 21:07, 17 May 2021


----------
-- diet --
----------
local VF = {}
VF.diet = {}
VF.diet.foodType = {
  None = true,
  VegetableOrFruit = true,
  Meat = true,
  Fluid = true,
  Corpse = true,
  Seed = true,
  AnimalProduct = true,
  Plant = true,
  Tree = true,
  Meal = true,
  Processed = true,
  Liquor = true,
  Kibble = true,
  VegetarianAnimal = {
    VegetableOrFruit = true,
    Seed = true,
    Meal = true,
    Processed = true,
    Liquor = true,
    Kibble = true
  },
  VegetarianRoughAnimal = {
    VegetableOrFruit = true,
    Seed = true,
    Plant = true,
    Meal = true,
    Processed = true,
    Liquor = true,
    Kibble = true
  },
  CarnivoreAnimal = {
    Meat = true,
    Corpse = true,
    Meal = true,
    Processed = true,
    Kibble = true
  },
  CarnivoreAnimalStrict = {
    Meat = true,
    Corpse = true
  },
  OmnivoreAnimal = {
    VegetableOrFruit = true,
    Meat = true,
    Corpse = true,
    Seed = true,
    Meal = true,
    Processed = true,
    Liquor = true,
    Kibble = true
  },
  OmnivoreRoughAnimal = {
    VegetableOrFruit = true,
    Meat = true,
    Corpse = true,
    Seed = true,
    Plant = true,
    Meal = true,
    Processed = true,
    Liquor = true,
    Kibble = true
  },
  DendrovoreAnimal = {
    VegetableOrFruit = true,
    Seed = true,
    Tree = true,
    Processed = true,
    Kibble = true
  },
  OvivoreAnimal = {
    AnimalProduct = true,
    Meal = true,
    Processed = true,
    Kibble = true
  },
  OmnivoreHuman = {
    VegetableOrFruit = true,
    Meat = true,
    Fluid = true,
    Corpse = true,
    Seed = true,
    AnimalProduct = true,
    Meal = true,
    Processed = true,
    Liquor = true,
    Kibble = true
  }
}

--~ function VF.diet.resolveDietCategory(foodType)
--~   if util.diet[foodType].None then
--~       return "Never eats"
--~   end
--~   if util.diet[foodType].Tree then
--~       return "Dendrovorous"
--~   end
--~   if util.diet[foodType].Meat then
--~       if util.diet[foodType].VegetableOrFruit or util.diet[foodType].Plant then
--~           return "Omnivorous"
--~       end
--~       return "Carnivorous"
--~   end
--~   if util.diet[foodType].AnimalProduct then
--~       return "Ovivorous"
--~   end
--~   return "Herbivorous"
--~ end

--~ ------------------------
--~ -- module entry point --
--~ ------------------------
--~ function VF.expand(mergedDef)
--~   for k,func in pairs(VF.vfields) do
--~     func(mergedDef)
--~   end
--~ end

return {

  --------------
  -- tool DPS --
  --------------
  toolDPS = function (def)
    if not def.tools then return nil end

    for _,tool in ipairs(def.tools) do
      if tool.power and tool.cooldownTime then
        local dps = tool.power / tool.cooldownTime
        tool.DPS = Util.round(dps, 3)
      end
    end
    return true
  end,

  ---------------
  -- verb DPS --
  ---------------
  verbDPS = function (def)
    local filters = {
      {'verbs', 1, 'defaultProjectile'},
      {'verbs', 1, 'warmupTime'},
      {'statBases', 'RangedWeapon_Cooldown'}
    }
    if not Util.table.checkMultiple(def, filters) then return nil end

    local projectileDef
    local damageDef

    local damage
    local warmup
    local cooldown
    local burst
    local burstPause
    local dps

    projectileDef = DefInfo.getDef(def.verbs[1].defaultProjectile, false)
    if not projectileDef then return nil end

    -- def.verbs[1].defaultProjectile = projectile

    if Util.table.check(projectileDef, 'projectile', 'damageAmountBase') then
      damage = projectileDef.projectile.damageAmountBase
    elseif Util.table.check(projectileDef, 'projectile', 'damageDef') and projectileDef.projectile.damageDef == 'Bomb' then
      damageDef = DefInfo.getDef(projectileDef.projectile.damageDef, false)
      if damageDef then
        damage = damageDef.defaultDamage
        -- def.verbs[1].defaultProjectile.projectile.damageDef = damageDef
      end
    end

    warmup     = def.verbs[1].warmupTime
    burst      = def.verbs[1].burstShotCount
    burstPause = def.verbs[1].ticksBetweenBurstShots
    cooldown   = def.statBases.RangedWeapon_Cooldown

    if burst and pause then
      dps = damage * burst / (warmup + cooldown + (burst-1)*(burstPause/60))
    else
      dps = damage / (warmup + cooldown)
    end

    if dps then
      def.verbs[1].DPS = Util.round(dps, 3)
      return true
    end
  end,

  -------------------------------------
  -- naturally lives in these biomes --
  -------------------------------------
  pawnLivesIn = function (def)
    if def.thingClass ~= 'Pawn' then return nil end

    local biomes = {}

    for biomeK,biome in pairs(Data) do
      if Util.table.check(biome, 'wildAnimals') then
        for animalK,animal in pairs(biome.wildAnimals) do
          if animalK == def.defName then
            table.insert(biomes, biome.defName)
          end
        end
      end
    end

    if #biomes > 0 then
      def._.livesIn = biomes
      return true
    end
  end,

  -----------------------
  -- foodType expanded --
  -----------------------
  foodTypeExpanded = function (def)
    if def.thingClass ~= 'Pawn' then return nil end
    -- if not Util.table.check(def, 'race', 'foodType') then return nil end

    local diet = def.race.foodType
    local flags = {}
    local expandedFoodTypes = {}

    for _,foodType in ipairs(diet) do
      if type(VF.diet.foodType[foodType]) == "boolean" then
        flags[foodType] = true
      else
        for foodItem,_ in pairs(VF.diet.foodType[foodType]) do
          flags[foodItem] = true
        end
      end
    end

    for flag,_ in pairs(flags) do
      table.insert(expandedFoodTypes, flag)
    end

    if #expandedFoodTypes > 0 then
      def.race.foodTypeExpanded = expandedFoodTypes
      return true
    end
  end,

  -----------------
  -- MarketValue -- works only for items (not stuffables)
  -----------------
  MarketValue = function (def)
    if Util.table.check(def, 'statBases', 'MarketValue') then return nil end
    if not Util.table.check(def, 'statBases', 'WorkToMake') then return nil end
    if not Util.table.check(def, 'costList') then return nil end

    local workRate = 0.0036
    local workCost
    local ingredientCost = 0

    if def.costList then
      for k,v in pairs(def.costList) do
        ingredientCost = ingredientCost + Data['ThingDef:'..k].statBases.MarketValue * v
      end
    end

    workCost = def.statBases.WorkToMake * workRate

    def.statBases.MarketValue = ingredientCost + workCost

    return true
  end


}