Ejemplo n.º 1
0
    private bool UnloadResources()
    {
        SimUnit targetUnit = lastPoint.GetUnitWithTargetAndCapacity(searchTarget, resources);

        if (targetUnit != null)
        {
            resources.TransferResourcesTo(targetUnit.resources);
        }

        return(resources.IsEmpty());
    }
Ejemplo n.º 2
0
    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);
    }