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

From RimWorld Wiki
Jump to navigation Jump to search
Line 1: Line 1:
if mw then
+
----------
   DefInfo = DefInfo or require("Module:Test")
+
-- diet --
else
+
----------
   DefInfo = DefInfo or require "DefInfo"
+
local VF = {}
end
+
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,
  
local VF = {}
+
  ---------------
VF.vfields = {}
+
  -- 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
-- tools.DPS --
+
    local damageDef
---------------
 
function VF.vfields.unarmedDPS(def)
 
  if not def.tools then return nil end
 
  
  for _,tool in ipairs(def.tools) do
+
    local damage
     local dps = tool.power / tool.cooldownTime
+
     local warmup
     if dps then tool.DPS = Util.round(dps, 3) end
+
    local cooldown
  end
+
     local burst
end
+
    local burstPause
 +
    local dps
  
---------------
+
    projectileDef = DefInfo.getDef(def.verbs[1].defaultProjectile, false)
-- verbs.DPS --
+
     if not projectileDef then return nil end
---------------
 
function VF.vfields.verbsDPS(def)
 
  local filters = {
 
    {'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)
+
    -- def.verbs[1].defaultProjectile = projectile
  if not projectile 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
  
   local damage
+
    warmup    = def.verbs[1].warmupTime
 +
    burst      = def.verbs[1].burstShotCount
 +
    burstPause = def.verbs[1].ticksBetweenBurstShots
 +
    cooldown   = def.statBases.RangedWeapon_Cooldown
  
  if Util.table.check(projectile, 'projectile', 'damageAmountBase') then
+
    if burst and pause then
    damage = projectile.projectile.damageAmountBase
+
      dps = damage * burst / (warmup + cooldown + (burst-1)*(burstPause/60))
  elseif Util.table.check(projectile, 'projectile', 'damageDef') and projectile.projectile.damageDef == 'Bomb' then
+
     else
    local bomb = DefInfo.getDef(projectile.projectile.damageDef)
+
       dps = damage / (warmup + cooldown)
     if bomb then
 
       damage = bomb.defaultDamage
 
      def.verbs[1].defaultProjectile.projectile.damageDef = bomb
 
 
     end
 
     end
  end
 
  
  local warmup     = def.verbs[1].warmupTime
+
     if dps then
   local cooldown   = def.statBases.RangedWeapon_Cooldown
+
      def.verbs[1].DPS = Util.round(dps, 3)
   local burst      = def.verbs[1].burstShotCount
+
      return true
  local pause      = def.verbs[1].ticksBetweenBurstShots
+
    end
 +
   end,
 +
 
 +
  -------------------------------------
 +
  -- naturally lives in these biomes --
 +
   -------------------------------------
 +
   pawnLivesIn = function (def)
 +
    if def.thingClass ~= 'Pawn' then return nil end
  
  local dps
+
    local biomes = {}
  
  if burst and pause then
+
     for biomeK,biome in pairs(Data) do
     dps = damage * burst / (warmup + cooldown + (burst-1)*(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 def.statBases.MarketValue then return nil end
 +
    if not def.statBases.WorkToMake then return nil end
 +
    if not 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
+
}

Revision as of 21:00, 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 def.statBases.MarketValue then return nil end
    if not def.statBases.WorkToMake then return nil end
    if not 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


}