private void DistributeResource( IResourceDefinition resource, IEnumerable <IHexCell> validLocations, int count ) { var nodeLocations = CellRandomSampler.SampleElementsFromSet( validLocations, count, GetResourceWeightFunction(resource) ); if (nodeLocations.Count < count) { Debug.LogWarningFormat( "Could not find enough valid locations to place {0} {1} after weighting. Only found {2}", count, resource.name, nodeLocations.Count ); } foreach (var location in nodeLocations) { int copies = resource.Type == ResourceType.Strategic ? UnityEngine.Random.Range(Config.MinStrategicCopies, Config.MaxStrategicCopies) : 1; NodeFactory.BuildNode(location, resource, copies); } }
public void DistributeStrategicsAcrossHomeland(HomelandData homelandData) { int nodesLeft = Mathf.CeilToInt(homelandData.YieldAndResources.StrategicNodesPerCell * homelandData.Cells.Count()); int copiesLeft = Mathf.CeilToInt(homelandData.YieldAndResources.StrategicCopiesPerCell * homelandData.Cells.Count()); var validStrategics = new List <IResourceDefinition>(StrategicResources); var resourceWeightsByRegion = new Dictionary <MapRegion, Dictionary <IResourceDefinition, int> >(); foreach (var region in homelandData.AllRegions) { resourceWeightsByRegion[region] = homelandData.GetDataOfRegion(region).GetResourceWeights(); } var regions = homelandData.AllRegions.ToList(); int iterations = regions.Count * 10; while (nodesLeft > 0 && copiesLeft > 0 && iterations-- > 0) { if (regions.Count == 0) { regions.AddRange(homelandData.AllRegions); } var region = regions.Random(); regions.Remove(region); var resourceWeights = resourceWeightsByRegion[region]; var strategic = ResourceRandomSampler.SampleElementsFromSet( validStrategics, 1, resource => resourceWeights.ContainsKey(resource) ? resourceWeights[resource] : 0 ).FirstOrDefault(); if (strategic == null) { continue; } var location = CellRandomSampler.SampleElementsFromSet( region.Cells, 1, cell => ResourceRestrictionCanon.GetPlacementWeightOnCell(strategic, cell) ).FirstOrDefault(); if (location != null && NodeFactory.CanBuildNode(location, strategic)) { int copies = StrategicCopiesLogic.GetWeightedRandomCopies(); copies = Math.Min(copies, copiesLeft); NodeFactory.BuildNode(location, strategic, copies); nodesLeft--; copiesLeft -= copies; } } }
public bool TryIncreaseYield( MapRegion region, RegionData regionData, YieldType type, out YieldSummary yieldAdded ) { var availableResources = BonusResourcesWithYield[type].ToList(); while (availableResources.Count > 0) { var chosenResource = ResourceRandomSampler.SampleElementsFromSet( availableResources, 1, GetResourceSelectionWeightFunction(region, regionData) ).FirstOrDefault(); if (chosenResource == null) { break; } var cell = CellRandomSampler.SampleElementsFromSet( region.Cells, 1, GetCellPlacementWeightFunction(chosenResource) ).FirstOrDefault(); if (cell != null) { var oldYield = YieldEstimator.GetYieldEstimateForCell(cell, TechCanon.AvailableTechs); int copies = chosenResource.Type == ResourceType.Strategic ? StrategicCopiesLogic.GetWeightedRandomCopies() : 0; ResourceNodeFactory.BuildNode(cell, chosenResource, copies); yieldAdded = YieldEstimator.GetYieldEstimateForCell(cell, TechCanon.AvailableTechs) - oldYield; return(true); } else { availableResources.Remove(chosenResource); } } yieldAdded = YieldSummary.Empty; return(false); }
private void OnCellClicked(Tuple <IHexCell, PointerEventData> data) { var cell = data.Item1; if (IsDeleting) { var nodeOnCell = NodePositionCanon.GetPossessionsOfOwner(cell).FirstOrDefault(); if (nodeOnCell != null) { nodeOnCell.Destroy(); } } else if (ActiveResource != null && ResourceNodeFactory.CanBuildNode(cell, ActiveResource)) { ResourceNodeFactory.BuildNode(cell, ActiveResource, ActiveCopies); } }