public void OnVoxelDestroyed(VoxelHandle V) { if (!V.IsValid) { return; } RoomBuilder.OnVoxelDestroyed(V); var toRemove = new List <Stockpile>(); foreach (var s in new List <Stockpile>(Stockpiles).Where(stockpile => stockpile.IsBuilt)) { if (s.ContainsVoxel(V)) { s.RemoveVoxel(V); } if (s.Voxels.Count == 0) { toRemove.Add(s); } } foreach (Stockpile s in toRemove) { Stockpiles.Remove(s); s.Destroy(); } }
public void OnVoxelDestroyed(Voxel v) { if (v.IsEmpty) { return; } Voxel Voxel = v; RoomBuilder.OnVoxelDestroyed(v); List <Stockpile> toRemove = new List <Stockpile>(); List <Stockpile> currentStockpiles = new List <Stockpile>(); currentStockpiles.AddRange(Stockpiles); foreach (Stockpile s in currentStockpiles) { if (s.ContainsVoxel(Voxel)) { s.RemoveVoxel(Voxel); } if (s.Voxels.Count == 0) { toRemove.Add(s); } } foreach (Stockpile s in toRemove) { Stockpiles.Remove(s); s.Destroy(); } }
public bool HasResources(IEnumerable <Quantitiy <Resource.ResourceTags> > resources, bool allowHeterogenous = false) { foreach (Quantitiy <Resource.ResourceTags> resource in resources) { int count = Stockpiles.Sum(stock => stock.Resources.GetResourceCount(resource.ResourceType, allowHeterogenous)); if (count < resource.NumResources) { return(false); } } return(true); }
public bool HasResources(IEnumerable <ResourceAmount> resources) { foreach (ResourceAmount resource in resources) { int count = Stockpiles.Sum(stock => stock.Resources.GetResourceCount(resource.ResourceType)); if (count < resources.Where(r => r.ResourceType == resource.ResourceType).Sum(r => r.NumResources)) { return(false); } } return(true); }
public void ToleratesBlueprintsWithUnknownIngotTypes() { var blueprint = new Blueprint("A", new ItemAndQuantity("Ore/A", 10f), new ItemAndQuantity("Ingot/A", 10f)); var stockpiles = new Stockpiles( new IngotStockpile[0], new[] { blueprint }); var refinery = new Refinery(new Mock <IMyRefinery>().Object, new RefineryType("Refinery") { SupportedBlueprints = { "A" } }); stockpiles.UpdateStockpileEstimates(refinery, blueprint, 5f); }
public void HighYieldBlueprintScoresHigherThanLowYieldBlueprint() { var blueprintALow = new Blueprint("ALow", new ItemAndQuantity("Ore/A", 10f), new ItemAndQuantity("Ingot/A", 10f)); var blueprintAHigh = new Blueprint("AHigh", new ItemAndQuantity("Ore/A", 10f), new ItemAndQuantity("Ingot/A", 12f)); var stockpiles = new Stockpiles( new[] { new IngotStockpile { Ingot = new IngotType("Ingot/A", 1, 10), CurrentQuantity = 50, TargetQuantity = 100 } }, new[] { blueprintALow, blueprintAHigh }); var aLowScore = stockpiles.ScoreBlueprint(blueprintALow); var aHighScore = stockpiles.ScoreBlueprint(blueprintAHigh); Assert.That(aHighScore, Is.GreaterThan(aLowScore)); }
public void BlueprintWithAllOutputsInDemandScoresHigherThanBlueprintWithOnlyOneOutputInDemand() { var blueprintAB = new Blueprint("AB", new ItemAndQuantity("Ore/AB", 10f), new ItemAndQuantity("Ingot/A", 10f), new ItemAndQuantity("Ingot/B", 10f)); var blueprintBC = new Blueprint("BC", new ItemAndQuantity("Ore/BC", 10f), new ItemAndQuantity("Ingot/B", 10f), new ItemAndQuantity("Ingot/C", 10f)); var stockpiles = new Stockpiles( new[] { new IngotStockpile { Ingot = new IngotType("Ingot/A", 1, 10), CurrentQuantity = 500, TargetQuantity = 100 }, // Not in demand. new IngotStockpile { Ingot = new IngotType("Ingot/B", 1, 10), CurrentQuantity = 50, TargetQuantity = 100 }, new IngotStockpile { Ingot = new IngotType("Ingot/C", 1, 10), CurrentQuantity = 50, TargetQuantity = 100 } }, new[] { blueprintAB, blueprintBC }); var abScore = stockpiles.ScoreBlueprint(blueprintAB); var bcScore = stockpiles.ScoreBlueprint(blueprintBC); Assert.That(bcScore, Is.GreaterThan(abScore)); }
public void AssignedWorkAffectsQuotaFactor() { var blueprint = new Blueprint("A", new ItemAndQuantity("Ore/A", 10f), new ItemAndQuantity("Ingot/A", 10f)); var stockpiles = new Stockpiles( new[] { new IngotStockpile { Ingot = new IngotType("Ingot/A", 1, 10), CurrentQuantity = 50, TargetQuantity = 100 } }, new[] { blueprint }); var refinery = new Refinery(new Mock<IMyRefinery>().Object, new RefineryType("Refinery") { SupportedBlueprints = { "A" } }); var initialStockpile = stockpiles.GetStockpiles().Single(); stockpiles.UpdateStockpileEstimates(refinery, blueprint, 5f); var updatedStockpile = stockpiles.GetStockpiles().Single(); Assert.That(updatedStockpile.QuotaFraction, Is.GreaterThan(initialStockpile.QuotaFraction)); Assert.That(updatedStockpile.EstimatedProduction, Is.GreaterThan(0)); }
public void OnVoxelDestroyed(VoxelHandle V) { if (!V.IsValid) { return; } RoomBuilder.OnVoxelDestroyed(V); var toRemove = new List <Stockpile>(); foreach (var s in new List <Stockpile>(Stockpiles).Where(stockpile => stockpile.IsBuilt)) { if (s.ContainsVoxel(V)) { s.RemoveVoxel(V); } if (s.Voxels.Count == 0) { toRemove.Add(s); } } foreach (Stockpile s in toRemove) { foreach (var resource in s.Resources) { var resourceType = ResourceLibrary.GetResourceByName(resource.ResourceType); foreach (var tag in resourceType.Tags) { if (CachedResourceTagCounts.ContainsKey(tag)) { CachedResourceTagCounts[tag] -= resource.NumResources; Trace.Assert(CachedResourceTagCounts[tag] >= 0); } } } RecomputeCachedVoxelstate(); Stockpiles.Remove(s); s.Destroy(); } }
public void SilverAndCobalt2() { var cobalt = new Blueprint("CobaltOreToIngot", new ItemAndQuantity("Ore/Cobalt", 0.25f), new ItemAndQuantity("Ingot/Cobalt", 0.075f)); var silver = new Blueprint("SilverOreToIngot", new ItemAndQuantity("Ore/Silver", 1f), new ItemAndQuantity("Ingot/Silver", 0.1f)); var stockpiles = new Stockpiles( new[] { new IngotStockpile { Ingot = new IngotType("Ingot/Cobalt", 220f, 0.075f), CurrentQuantity = 220, TargetQuantity = 2200 }, new IngotStockpile { Ingot = new IngotType("Ingot/Silver", 10f, 0.1f), CurrentQuantity = 10, TargetQuantity = 100 } }, new[] { cobalt, silver }); var cobaltScore = stockpiles.ScoreBlueprint(cobalt); var silverScore = stockpiles.ScoreBlueprint(silver); Assert.That(silverScore, Is.EqualTo(cobaltScore).Within(0.01).Percent); }
public void IronAndGold() { var gold = new Blueprint("GoldOreToIngot", new ItemAndQuantity("Ore/Gold", 2.5f), new ItemAndQuantity("Ingot/Gold", 0.025f)); var iron = new Blueprint("IronOreToIngot", new ItemAndQuantity("Ore/Iron", 20f), new ItemAndQuantity("Ingot/Iron", 14f)); var stockpiles = new Stockpiles( new[] { new IngotStockpile { Ingot = new IngotType("Ingot/Gold", 5f, 0.025f), CurrentQuantity = 27, TargetQuantity = 50 }, new IngotStockpile { Ingot = new IngotType("Ingot/Iron", 80f, 14f), CurrentQuantity = 15000, TargetQuantity = 800 } }, new[] { gold, iron }); var goldScore = stockpiles.ScoreBlueprint(gold); var ironScore = stockpiles.ScoreBlueprint(iron); Assert.That(ironScore, Is.LessThan(goldScore)); }
public void AssignedWorkAffectsQuotaFactor() { var blueprint = new Blueprint("A", new ItemAndQuantity("Ore/A", 10f), new ItemAndQuantity("Ingot/A", 10f)); var stockpiles = new Stockpiles( new[] { new IngotStockpile { Ingot = new IngotType("Ingot/A", 1, 10), CurrentQuantity = 50, TargetQuantity = 100 } }, new[] { blueprint }); var refinery = new Refinery(new Mock <IMyRefinery>().Object, new RefineryType("Refinery") { SupportedBlueprints = { "A" } }); var initialStockpile = stockpiles.GetStockpiles().Single(); stockpiles.UpdateStockpileEstimates(refinery, blueprint, 5f); var updatedStockpile = stockpiles.GetStockpiles().Single(); Assert.That(updatedStockpile.QuotaFraction, Is.GreaterThan(initialStockpile.QuotaFraction)); Assert.That(updatedStockpile.EstimatedProduction, Is.GreaterThan(0)); }
public Stockpile GetNearestStockpile(Vector3 position, Func <Stockpile, bool> predicate) { Stockpile nearest = null; float closestDist = float.MaxValue; foreach (Stockpile stockpile in Stockpiles.Where(predicate)) { if (!stockpile.IsBuilt) { continue; } float dist = (stockpile.GetBoundingBox().Center() - position).LengthSquared(); if (dist < closestDist) { closestDist = dist; nearest = stockpile; } } return(nearest); }
public bool HasFreeStockpile(ResourceAmount toPut) { return(Stockpiles.Any(s => s.IsBuilt && !s.IsFull() && s.IsAllowed(toPut.ResourceType))); }
public bool HasFreeStockpile() { return(Stockpiles.Any(s => s.IsBuilt && !s.IsFull())); }
public bool IsInStockpile(VoxelHandle v) { return(Stockpiles.Any(s => s.ContainsVoxel(v))); }
public List <Stockpile> GetIntersectingStockpiles(BoundingBox v) { return(Stockpiles.Where(pile => pile.Intersects(v)).ToList()); }
public Stockpile GetIntersectingStockpile(BoundingBox v) { return(Stockpiles.FirstOrDefault(pile => pile.Intersects(v))); }
public decimal ComputeTotalTreasurySpace() { return(Stockpiles.Sum(pile => pile.Voxels.Count * Treasury.MoneyPerPile)); }
public int ComputeTotalStockpileSpace() { return(Stockpiles.Sum(pile => pile.Resources.MaxResources)); }
public int ComputeRemainingStockpileSpace() { return(Stockpiles.Sum(pile => pile.Resources.MaxResources - pile.Resources.CurrentResourceCount)); }
public bool IsInStockpile(Voxel v) { Voxel vRef = v; return(Stockpiles.Any(s => s.ContainsVoxel(vRef))); }
public Stockpile GetIntersectingStockpile(Voxel v) { return(Stockpiles.FirstOrDefault(pile => pile.Intersects(v))); }
public bool RemoveResources(List <ResourceAmount> resources, Vector3 position, bool createItems = true) { Dictionary <ResourceType, ResourceAmount> amounts = new Dictionary <ResourceType, ResourceAmount>(); foreach (ResourceAmount resource in resources) { if (!amounts.ContainsKey(resource.ResourceType)) { amounts.Add(resource.ResourceType, new ResourceAmount(resource)); } else { amounts[resource.ResourceType].NumResources += resource.NumResources; } } if (!HasResources(amounts.Values)) { return(false); } List <Stockpile> stockpilesCopy = new List <Stockpile>(Stockpiles.Where(s => resources.All(r => s.IsAllowed(r.ResourceType)))); stockpilesCopy.Sort((a, b) => CompareZones(a, b, position)); foreach (ResourceAmount resource in resources) { int count = 0; List <Vector3> positions = new List <Vector3>(); foreach (Stockpile stock in stockpilesCopy) { int num = stock.Resources.RemoveMaxResources(resource, resource.NumResources - count); stock.HandleBoxes(); if (stock.Boxes.Count > 0) { for (int i = 0; i < num; i++) { positions.Add(stock.Boxes[stock.Boxes.Count - 1].LocalTransform.Translation); } } count += num; if (count >= resource.NumResources) { break; } } if (createItems) { foreach (Vector3 vec in positions) { Body newEntity = EntityFactory.CreateEntity <Body>(resource.ResourceType + " Resource", vec + MathFunctions.RandVector3Cube() * 0.5f); TossMotion toss = new TossMotion(1.0f + MathFunctions.Rand(0.1f, 0.2f), 2.5f + MathFunctions.Rand(-0.5f, 0.5f), newEntity.LocalTransform, position); newEntity.GetRoot().GetComponent <Physics>().CollideMode = Physics.CollisionMode.None; newEntity.AnimationQueue.Add(toss); newEntity.UpdateRate = 1; toss.OnComplete += () => toss_OnComplete(newEntity); } } } return(true); }
public void ToleratesBlueprintsWithUnknownIngotTypes() { var blueprint = new Blueprint("A", new ItemAndQuantity("Ore/A", 10f), new ItemAndQuantity("Ingot/A", 10f)); var stockpiles = new Stockpiles( new IngotStockpile[0], new[] { blueprint }); var refinery = new Refinery(new Mock<IMyRefinery>().Object, new RefineryType("Refinery") { SupportedBlueprints = { "A" } }); stockpiles.UpdateStockpileEstimates(refinery, blueprint, 5f); }