public void TransferResourcesTo(SimResourceBinCollection resourcesTarget) { for (int i = 0; i < bins.Count; i++) { SimResourceBin sourceBin = bins[i]; SimResourceBin targetBin = resourcesTarget.FindOrAddBin(sourceBin.resouce); sourceBin.TransferTo(targetBin); } }
public SimUnit GetUnitWithTargetAndCapacity(string searchTarget, SimResourceBinCollection resources) { for (int i = 0; i < units.Count; i++) { if (units[i].Accepts(searchTarget, resources)) { return(units[i]); } } return(null); }
public bool CanAddSomeResources(SimResourceBinCollection resourcesToTryAdd) { for (int i = 0; i < resourcesToTryAdd.bins.Count; i++) { SimResourceBin sourceBin = resourcesToTryAdd.bins[i]; if (sourceBin.amount > 0 && GetAmount(sourceBin.resouce) < GetCapacity(sourceBin.resouce)) return true; } return false; }
private SimResourceBinCollection ParseResourcesArray(string[] lineSplit, ref int splitOffset) { string[] strings = ParseStringArray(lineSplit, ref splitOffset); SimResourceBinCollection resources = new SimResourceBinCollection(); for (int i = 0; i < strings.Length; i += 2) { SimResource resource = definition.GetResource(strings[i]); if (resource == null) { ThrowInvalidLine("ParseResourcesArray() - Unknown resource id"); } int amount = ParseInt(strings[i + 1]); resources.AddResource(resource, amount); } return(resources); }
public bool Accepts(string searchTarget, SimResourceBinCollection resourcesToTryToAdd) { return(Array.IndexOf(unitType.targets, searchTarget) >= 0 && resources.CanAddSomeResources(resourcesToTryToAdd)); }
public void SetCapacities(SimResourceBinCollection resourcesCapacities) { for (int i = 0; i < resourcesCapacities.bins.Count; i++) SetCapacity(resourcesCapacities.bins[i].resouce, resourcesCapacities.bins[i].amount); }
public void AddResources(SimResourceBinCollection resourcesToAdd) { for (int i = 0; i < resourcesToAdd.bins.Count; i++) AddResource(resourcesToAdd.bins[i].resouce, resourcesToAdd.bins[i].amount); }
public SimAgent AddAgent(SimAgentType agentType, SimPoint position, SimUnit owner, SimResourceBinCollection resources, string searchTarget) { SimAgent agent = new SimAgent(); agent.Init(agentType, nextAgentId++, position, owner, resources, searchTarget); agents.Add(agent); boxListener.OnAgentAdded(agent); return(agent); }
public SimPoint FindNextPoint(SimPoint fromPoint, string searchTarget, SimResourceBinCollection resources) { //This implementation MUST be replaced with something that is really fast.. right now //we are not even using a priority queue!! //We should store as much information as possible in each SimPoint to make this search as //fast as possible. closedSet.Clear(); openSet.Clear(); cameFrom.Clear(); scoreFromStart.Clear(); scorePlusHeuristicFromStart.Clear(); openSet.Add(fromPoint); scoreFromStart[fromPoint] = 0; scorePlusHeuristicFromStart[fromPoint] = scoreFromStart[fromPoint] + Heuristic(fromPoint, fromPoint); while (openSet.Count > 0) { SimPoint current = GetPointWithLowestScorePlusHeuristicFromStart(); if (current.GetUnitWithTargetAndCapacity(searchTarget, resources) != null) { if (current == fromPoint) { return(current); } while (cameFrom[current] != fromPoint) { current = cameFrom[current]; } return(current); } openSet.Remove(current); closedSet.Add(current); foreach (SimSegment segment in current.segments) { SimPoint neighbor; if (segment.point1 == current) { neighbor = segment.point2; } else { neighbor = segment.point1; } float neighborScoreFromStart = scoreFromStart[current] + segment.length; if (closedSet.Contains(neighbor)) { if (neighborScoreFromStart >= scoreFromStart[neighbor]) { continue; } } if (!openSet.Contains(neighbor) || neighborScoreFromStart < scoreFromStart[neighbor]) { cameFrom[neighbor] = current; scoreFromStart[neighbor] = neighborScoreFromStart; scorePlusHeuristicFromStart[neighbor] = neighborScoreFromStart + Heuristic(neighbor, fromPoint); if (!openSet.Contains(neighbor)) { openSet.Add(neighbor); } } } } //No path found.. return random point! if (fromPoint.segments.Count > 0) { SimSegment randomSegment = fromPoint.segments[rnd.Next(0, fromPoint.segments.Count)]; if (randomSegment.point1 == fromPoint) { return(randomSegment.point2); } else if (randomSegment.point2 == fromPoint) { return(randomSegment.point1); } } return(null); }
public void Init(SimAgentType agentType, int id, SimPoint position, SimUnit owner, SimResourceBinCollection resources, string searchTarget) { this.agentType = agentType; this.id = id; this.owner = owner; this.searchTarget = searchTarget; this.resources.AddResources(resources); this.worldPosition = position.worldPosition; this.lastPoint = position; }