Optimization: Ingredient search not limited by recipes search radius

Started by Razuhl, April 25, 2018, 12:05:41 AM

Previous topic - Next topic

Razuhl

When a pawn thinks about performing a recipe a search will be started that looks at the regions around the workbench(near to far). In the recipes configuration dialog the user can set a maximum distance at which ingredients are used. The search now moves through regions until it has found ingredients that are within the maximum distance or until the entire map has been explored(and in consequence every haulable item on the map). The region traversal completely ignores the fact that a region outside the maximum distance can not have relevant items.

This behaviour can be fixed by extending the region entry condition in WorkGiver_DoBill.TryFindBestBillIngredients with a test whether the region overlaps with the circle describing the allowed distance.


//set to maximum so no restriction
if ( Math.Abs(Bill.MaxIngredientSearchRadius - bill.ingredientSearchRadius) < 1f ) entryCondition = (Region from, Region r) => r.Allows(traverseParams, false);
else entryCondition = (Region from, Region r) => {
if ( !r.Allows(traverseParams, false) ) return false;
//check if the distance restriction touches the regions rect.
CellRect rect = r.extentsClose;
int DeltaX = Math.Abs(billGiver.Position.x - Math.Max(rect.minX, Math.Min(billGiver.Position.x, rect.maxX)));
if ( DeltaX > bill.ingredientSearchRadius ) return false;
int DeltaY = Math.Abs(billGiver.Position.z - Math.Max(rect.minZ, Math.Min(billGiver.Position.z, rect.maxZ)));
if ( DeltaY > bill.ingredientSearchRadius ) return false;
return (DeltaX * DeltaX + DeltaY * DeltaY) <= (bill.ingredientSearchRadius * bill.ingredientSearchRadius);
};


The behaviour can be observed by loading a game with a working colony(do not unpause). Then turning on the "Draw Region Traversal" option in the debug menu. Now a recipe is edited to have a distance limit in which no ingredients are found. Right clicking the corresponding workbench with a pawn that can do the recipe will light up the entire map. Alternatively if ingredients are found the search ends early but it doesn't end if all explored regions are past the distance limit.

With the fix in place only regions inside the limited area are searched even if no ingredients are found.

Tynan

Tynan Sylvester - @TynanSylvester - Tynan's Blog