179 lines
8.0 KiB
C#
179 lines
8.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using RimWorld;
|
|
using Verse;
|
|
using Verse.AI;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>
|
|
/// What a piece of food is, for the Ate groups that read the thing rather than its def (a meal with meat
|
|
/// in it, human meat as an ingredient). Hook C reads it in its prefix, before the stack can be destroyed,
|
|
/// and puts it into ActEvent.facts. Built on the game's own checks:
|
|
/// - meat: FoodUtility.GetFoodKind(Thing), which reads a meal's ingredients (RimWorld/FoodUtility.cs:1454-1486);
|
|
/// - human meat: FoodUtility.IsHumanlikeCorpseOrHumanlikeMeatOrIngredient (:1114-1129), as Thing.Ingested
|
|
/// uses it (Verse/Thing.cs:2033);
|
|
/// - insect meat: the meat's source race has insectoid flesh (FoodUtility.GetMeatSourceCategory, :1178-1197),
|
|
/// read for corpses and ingredients by FoodUtility.IsInsectCorpseOrInsectMeatIngredient (:1140-1159).
|
|
/// Meals carry the Meal food type (Core Defs/ThingDefs_Items/Items_Food.xml:36); the Processed food type
|
|
/// marks drugs and chocolate (Core Defs/Drugs), so "any meal" reads FoodTypeFlags.Meal, not IsProcessedFood.
|
|
/// </summary>
|
|
public static class FoodFacts
|
|
{
|
|
/// <summary>The facts of a real thing. Falls back to the def when the thing can't be read.</summary>
|
|
public static ActFacts Of(Thing food)
|
|
{
|
|
if (food?.def?.ingestible == null)
|
|
return ActFacts.None;
|
|
ThingDef def = food.def;
|
|
ActFacts f = OfDefOnly(def) & ~(ActFacts.Meat | ActFacts.HumanMeat | ActFacts.InsectMeat);
|
|
if (FoodUtility.GetFoodKind(food) == FoodKind.Meat)
|
|
f |= ActFacts.Meat;
|
|
if (FoodUtility.IsHumanlikeCorpseOrHumanlikeMeatOrIngredient(food))
|
|
f |= ActFacts.HumanMeat;
|
|
if (FoodUtility.IsInsectCorpseOrInsectMeatIngredient(food))
|
|
f |= ActFacts.InsectMeat;
|
|
return f;
|
|
}
|
|
|
|
/// <summary>The facts a def alone gives: no ingredients, a corpse's race from its def.</summary>
|
|
public static ActFacts OfDefOnly(ThingDef def)
|
|
{
|
|
if (def?.ingestible == null)
|
|
return ActFacts.None;
|
|
ActFacts f = ActFacts.None;
|
|
if (FoodUtility.GetFoodKind(def) == FoodKind.Meat)
|
|
f |= ActFacts.Meat;
|
|
if (IsRawMeatOrCorpse(def))
|
|
f |= ActFacts.RawMeatOrCorpse;
|
|
if (SourceIsHumanlike(def))
|
|
f |= ActFacts.HumanMeat;
|
|
if (SourceIsInsect(def))
|
|
f |= ActFacts.InsectMeat;
|
|
if (IsRawFood(def))
|
|
f |= ActFacts.RawFood;
|
|
if (IsMeal(def))
|
|
f |= ActFacts.Meal;
|
|
return f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The facts of an act: the hook's, or read now from the thing (an act reported by other code), or
|
|
/// from the def. A thing that throws while read (a corpse already emptied) gives the def's facts.
|
|
/// </summary>
|
|
public static ActFacts Of(ActEvent act)
|
|
{
|
|
if (act == null)
|
|
return ActFacts.None;
|
|
if (act.facts != ActFacts.None)
|
|
return act.facts;
|
|
if (act.thing != null)
|
|
{
|
|
try
|
|
{
|
|
return Of(act.thing);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// A destroyed corpse has no inner pawn any more; the def still answers.
|
|
}
|
|
}
|
|
return OfDefOnly(act.def as ThingDef);
|
|
}
|
|
|
|
public static bool IsRawMeatOrCorpse(ThingDef def) => def?.ingestible != null && !def.IsProcessedFood && (def.IsMeat || def.IsCorpse);
|
|
|
|
public static bool IsRawFood(ThingDef def) =>
|
|
def?.ingestible != null && (def.ingestible.preferability == FoodPreferability.RawBad || def.ingestible.preferability == FoodPreferability.RawTasty);
|
|
|
|
public static bool IsMeal(ThingDef def) => def?.ingestible != null && (def.ingestible.foodType & FoodTypeFlags.Meal) != 0;
|
|
|
|
/// <summary>The food can hold ingredients (meals, pemmican), so a thing of it may hold meat.</summary>
|
|
public static bool HasIngredients(ThingDef def) => def?.GetCompProperties<CompProperties_Ingredients>() != null;
|
|
|
|
/// <summary>Human meat or a humanlike corpse, by the def.</summary>
|
|
public static bool SourceIsHumanlike(ThingDef def)
|
|
{
|
|
if (def?.ingestible == null)
|
|
return false;
|
|
if (def.IsCorpse)
|
|
return def.ingestible.sourceDef?.race?.Humanlike ?? false;
|
|
return FoodUtility.GetMeatSourceCategory(def) == MeatSourceCategory.Humanlike;
|
|
}
|
|
|
|
/// <summary>Insect meat or an insect corpse, by the def.</summary>
|
|
public static bool SourceIsInsect(ThingDef def)
|
|
{
|
|
if (def?.ingestible == null)
|
|
return false;
|
|
if (def.IsCorpse)
|
|
return def.ingestible.sourceDef?.race?.FleshType == FleshTypeDefOf.Insectoid;
|
|
return FoodUtility.GetMeatSourceCategory(def) == MeatSourceCategory.Insect;
|
|
}
|
|
}
|
|
|
|
/// <summary>Drugs as the drug binge and its option see them.</summary>
|
|
public static class DrugInfo
|
|
{
|
|
private static Dictionary<ChemicalDef, DrugCategory> chemicalCategory;
|
|
|
|
/// <summary>The chemical a drug def's CompProperties_Drug names, or null.</summary>
|
|
public static ChemicalDef ChemicalOf(ThingDef def) => def?.GetCompProperties<CompProperties_Drug>()?.chemical;
|
|
|
|
/// <summary>The drug category of a def; None for anything that isn't a drug.</summary>
|
|
public static DrugCategory CategoryOf(ThingDef def) => def?.ingestible?.drugCategory ?? DrugCategory.None;
|
|
|
|
/// <summary>
|
|
/// The strongest category among the drugs carrying the chemical (wake-up: Hard; alcohol: Social), or
|
|
/// None when no loaded drug carries it.
|
|
/// </summary>
|
|
public static DrugCategory CategoryOfChemical(ChemicalDef chemical)
|
|
{
|
|
if (chemical == null)
|
|
return DrugCategory.None;
|
|
if (chemicalCategory == null)
|
|
{
|
|
var map = new Dictionary<ChemicalDef, DrugCategory>();
|
|
foreach (ThingDef t in DefDatabase<ThingDef>.AllDefsListForReading)
|
|
{
|
|
ChemicalDef c = ChemicalOf(t);
|
|
if (c == null)
|
|
continue;
|
|
DrugCategory cat = CategoryOf(t);
|
|
if (cat == DrugCategory.Any)
|
|
cat = DrugCategory.None;
|
|
if (!map.TryGetValue(c, out DrugCategory had) || Strength(cat) > Strength(had))
|
|
map[c] = cat;
|
|
}
|
|
chemicalCategory = map;
|
|
}
|
|
return chemicalCategory.TryGetValue(chemical, out DrugCategory found) ? found : DrugCategory.None;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The category a binge's planning case counts under for the "hard" and "social" options. The state's
|
|
/// own category, unless vanilla set it to Any: an addict's binge, or one that found no drug of its
|
|
/// break's category (Verse.AI/MentalState_BingingDrug.cs:60-80). Then the chosen chemical decides.
|
|
/// </summary>
|
|
public static DrugCategory BingeCategory(MentalState_BingingDrug binge)
|
|
{
|
|
if (binge == null)
|
|
return DrugCategory.None;
|
|
if (binge.drugCategory == DrugCategory.Hard || binge.drugCategory == DrugCategory.Social || binge.drugCategory == DrugCategory.Medical)
|
|
return binge.drugCategory;
|
|
return CategoryOfChemical(binge.chemical);
|
|
}
|
|
|
|
private static int Strength(DrugCategory c)
|
|
{
|
|
switch (c)
|
|
{
|
|
case DrugCategory.Hard: return 3;
|
|
case DrugCategory.Social: return 2;
|
|
case DrugCategory.Medical: return 1;
|
|
default: return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|