Skip to content

Commit

Permalink
Removal of ThingFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
Syrchalis committed Nov 24, 2021
1 parent fb429ac commit bb9a2db
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 186 deletions.
Binary file modified 1.3/Assemblies/ProcessorFramework.dll
Binary file not shown.
7 changes: 4 additions & 3 deletions 1.3/Defs/ThingDefs_Buildings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<colorCoded>false</colorCoded>
<processes>
<li>Beer</li>
<!--<li>Test</li>-->
</processes>
</li>
<li Class="CompProperties_Forbiddable"/>
Expand All @@ -74,7 +75,7 @@
</thingDefs>
</ingredientFilter>
<processDays>6</processDays> <!-- in days, a day is 60,000 ticks -->
<capacityFactor>1.0</capacityFactor>
<capacityFactor>1.0</capacityFactor> <!-- do not use too many decimals e.g 0.333 is fine for 1/3 -->
<efficiency>1.0</efficiency>
<usesTemperature>true</usesTemperature>
<temperatureSafe>-1.0~32.0</temperatureSafe>
Expand All @@ -101,7 +102,7 @@
</bonusOutputs>
</ProcessorFramework.ProcessDef>

<!--<ProcessorFramework.ProcessDef>
<ProcessorFramework.ProcessDef>
<defName>Test</defName>
<thingDef>RawPotatoes</thingDef>
<ingredientFilter>
Expand All @@ -118,7 +119,7 @@
<processDays>0.05</processDays>
<efficiency>1.0</efficiency>
<capacityFactor>3.7</capacityFactor>
</ProcessorFramework.ProcessDef>-->
</ProcessorFramework.ProcessDef>

<SoundDef>
<defName>PF_Honk</defName>
Expand Down
4 changes: 2 additions & 2 deletions 1.3/Mods/VFE_Vikings/Defs/ProcessDefs_VFE_Vikings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</thingDefs>
</ingredientFilter>
<processDays>6</processDays>
<efficiency>0.33333333</efficiency>
<capacityFactor>0.33333333</capacityFactor>
<efficiency>0.333</efficiency>
<capacityFactor>0.333</capacityFactor>
</ProcessorFramework.ProcessDef>

</Defs>
File renamed without changes.
116 changes: 68 additions & 48 deletions Source/ProcessorFramework/CompProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace ProcessorFramework
public class CompProcessor : ThingComp, IThingHolder
{
public List<ActiveProcess> activeProcesses = new List<ActiveProcess>();
public Dictionary<ProcessDef, ProcessFilter> enabledProcesses = new Dictionary<ProcessDef, ProcessFilter>();

public Dictionary<ProcessDef, QualityCategory> cachedTargetQualities = new Dictionary<ProcessDef, QualityCategory>();
public bool emptyNow = false;

Expand All @@ -24,11 +26,6 @@ public class CompProcessor : ThingComp, IThingHolder
public CompFlickable flickComp;

public ThingOwner innerContainer = null;
public ThingFilter productFilter = new ThingFilter();
public ThingFilter ingredientFilter = new ThingFilter();

public int unreservedSpaceLeft;
public ThingDef queuedIngredient;

//----------------------------------------------------------------------------------------------------
// Properties
Expand All @@ -38,23 +35,13 @@ public class CompProcessor : ThingComp, IThingHolder
public bool Empty => TotalIngredientCount <= 0;
public bool AnyComplete => activeProcesses.Any(x => x.Complete);
public int SpaceLeft => Props.capacity - TotalIngredientCount;
public int TotalIngredientCount => activeProcesses.Sum(x => Mathf.CeilToInt(x.ingredientCount * x.processDef.capacityFactor));
public IEnumerable<ProcessDef> EnabledProcesses
{
get
{
foreach (ThingDef product in productFilter.AllowedThingDefs)
{
yield return Props.processes.Find(x => x.thingDef == product);
}
}
}
public int TotalIngredientCount => Mathf.CeilToInt(activeProcesses.Sum(x => x.ingredientCount * x.processDef.capacityFactor));
public bool TemperatureOk
{
get
{
float temp = parent.AmbientTemperature;
foreach (ProcessDef process in EnabledProcesses)
foreach (ProcessDef process in enabledProcesses.Keys)
{
if (temp >= process.temperatureSafe.min - 2 || temp <= process.temperatureSafe.max + 2)
{
Expand Down Expand Up @@ -131,19 +118,7 @@ public void GetChildHolders(List<IThingHolder> outChildren)
public override void Initialize(CompProperties props)
{
base.Initialize(props);
unreservedSpaceLeft = Props.capacity;
innerContainer = new ThingOwner<Thing>(this);
productFilter = new ThingFilter();
ingredientFilter = new ThingFilter();

foreach (ProcessDef processDef in Props.processes)
{
productFilter.SetAllow(processDef.thingDef, true);
}
foreach (ThingDef thingDef in Props.processes.SelectMany(x => x.ingredientFilter.AllowedThingDefs))
{
ingredientFilter.SetAllow(thingDef, true);
}

parent.def.inspectorTabsResolved ??= new List<InspectTabBase>();
if (!parent.def.inspectorTabsResolved.Any(t => t is ITab_ProcessSelection))
Expand All @@ -164,13 +139,9 @@ public override void PostSpawnSetup(bool respawningAfterLoad)
{
graphicChangeQueued = true;
}
if (!ingredientFilter.AllowedThingDefs.Except(activeProcesses.SelectMany(x => x.processDef.ingredientFilter.AllowedThingDefs)).EnumerableNullOrEmpty())
if (enabledProcesses == null) //backCompatibility otherwise dict is null because the saved value doesn't exist (=null)
{
ingredientFilter = new ThingFilter();
foreach (ThingDef thingDef in Props.processes.SelectMany(x => x.ingredientFilter.AllowedThingDefs))
{
ingredientFilter.SetAllow(thingDef, true);
}
enabledProcesses = new Dictionary<ProcessDef, ProcessFilter>();
}
}

Expand All @@ -196,8 +167,7 @@ public override void PostExposeData()
{
Scribe_Deep.Look(ref innerContainer, "PF_innerContainer", this);
Scribe_Collections.Look(ref activeProcesses, "PF_activeProcesses", LookMode.Deep, this);
Scribe_Deep.Look(ref productFilter, "PF_productFilter");
Scribe_Deep.Look(ref ingredientFilter, "PF_ingredientFilter");
Scribe_Collections.Look(ref enabledProcesses, "PF_enabledProcesses", LookMode.Def, LookMode.Deep);
}

public override IEnumerable<Gizmo> CompGetGizmosExtra()
Expand Down Expand Up @@ -340,6 +310,59 @@ public override void CompTickLong()
//----------------------------------------------------------------------------------------------------
// Functional Methods

public void EnableAllProcesses()
{
enabledProcesses.Clear();
foreach (ProcessDef processDef in Props.processes)
{
ProcessFilter processFilter = new ProcessFilter(processDef.ingredientFilter.AllowedThingDefs.ToList());
enabledProcesses.Add(processDef, processFilter);
}
}
public void ToggleProcess(ProcessDef processDef, bool on)
{
if (on && !enabledProcesses.ContainsKey(processDef))
{
ProcessFilter processFilter = new ProcessFilter(processDef.ingredientFilter.AllowedThingDefs.ToList());
enabledProcesses.Add(processDef, processFilter);
}
else if (!on && enabledProcesses.ContainsKey(processDef))
{
enabledProcesses.Remove(processDef);
}
}
public void ToggleIngredient(ProcessDef processDef, ThingDef ingredient, bool on)
{
if (on)
{
if (enabledProcesses.ContainsKey(processDef))
{
enabledProcesses[processDef].allowedIngredients.Add(ingredient);
}
else
{
enabledProcesses[processDef] = new ProcessFilter(new List<ThingDef> { ingredient });
}
}
else if (!on && enabledProcesses.ContainsKey(processDef) && enabledProcesses[processDef].allowedIngredients.Contains(ingredient))
{
if (enabledProcesses[processDef].allowedIngredients.Count == 1)
{
enabledProcesses.Remove(processDef);
}
else
{
enabledProcesses[processDef].allowedIngredients.Remove(ingredient);
}
}
}

public int SpaceLeftFor(ProcessDef processDef)
{
int value = Mathf.FloorToInt((Props.capacity - activeProcesses.Sum(x => x.ingredientCount * x.processDef.capacityFactor)) / processDef.capacityFactor);
return value;
}

public void DoTicks(int ticks)
{
if (!Empty && FlickedOn)
Expand Down Expand Up @@ -377,30 +400,29 @@ public void DoActiveProcessesRareTicks()
}
}

public ActiveProcess FindActiveProcess(ThingDef ingredient)
public ActiveProcess FindActiveProcess(ProcessDef processDef)
{
foreach (ActiveProcess activeProcess in activeProcesses)
{
if (activeProcess.processDef.ingredientFilter.Allows(ingredient))
if (activeProcess.processDef == processDef)
{
return activeProcess;
}
}
return null;
}

public void AddIngredient(Thing ingredient)
public void AddIngredient(Thing ingredient, ProcessDef processDef)
{
int num = Mathf.Min(ingredient.stackCount, Props.capacity - TotalIngredientCount);
ProcessDef processDef = EnabledProcesses.First(x => x.ingredientFilter.Allows(ingredient));
int num = Mathf.Min(ingredient.stackCount, SpaceLeftFor(processDef));
if (num < ingredient.stackCount)
{
ingredient.SplitOff(ingredient.stackCount - num);
GenDrop.TryDropSpawn(ingredient.SplitOff(ingredient.stackCount - num), parent.Position, parent.Map, ThingPlaceMode.Near, out _);
}
bool emptyBefore = Empty;
if (num > 0 && processDef != null)
{
if (FindActiveProcess(ingredient.def) is ActiveProcess existingProcess && !Props.independentProcesses)
if (!Props.independentProcesses && FindActiveProcess(processDef) is ActiveProcess existingProcess)
{
TryMergeProcess(ingredient, existingProcess);
}
Expand All @@ -412,7 +434,6 @@ public void AddIngredient(Thing ingredient)
{
GraphicChange(false);
}
queuedIngredient = null;
}
}
private void TryAddNewProcess(Thing ingredient, ProcessDef processDef)
Expand All @@ -430,7 +451,7 @@ private void TryAddNewProcess(Thing ingredient, ProcessDef processDef)
private void TryMergeProcess(Thing ingredient, ActiveProcess activeProcess)
{
activeProcess.MergeProcess(ingredient);
innerContainer.TryAddOrTransfer(ingredient, false);
innerContainer.TryAddOrTransfer(ingredient, true);
}


Expand Down Expand Up @@ -517,7 +538,6 @@ public Thing TakeOutProduct(ActiveProcess activeProcess)
{
GraphicChange(true);
}
unreservedSpaceLeft += Mathf.CeilToInt(activeProcess.ingredientCount * activeProcess.processDef.capacityFactor);
if (!activeProcesses.Any(x => x.processDef.usesQuality))
{
emptyNow = false;
Expand Down Expand Up @@ -556,7 +576,7 @@ public override string CompInspectStringExtra()
else
{
// Usually this will only be one def label shown
string ingredientLabels = activeProcesses.First().ingredientThings.Select(x => x.Label).Join();
string ingredientLabels = activeProcesses.First().ingredientThings.Distinct().Select(x => x.Label).Join();
str.AppendTagged("PF_ContainsIngredient".Translate(TotalIngredientCount, Props.capacity, ingredientLabels.Named("INGREDIENTS")));
}
}
Expand Down
2 changes: 2 additions & 0 deletions Source/ProcessorFramework/DefOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ static DefOf()
public static ThingDef BarrelProcessor;

public static ReservationLayerDef PF_Empty;

public static ProcessDef Beer;
}
}
Loading

0 comments on commit bb9a2db

Please sign in to comment.