Esempio n. 1
0
    public UT_BuildingMechanics(List <DAT_Building> buildings)
    {
        m_graphBuildings = new UT_DirectedGraph <BuildingStats>();

        // add nodes
        foreach (DAT_Building building in buildings)
        {
            BuildingStats temp = new BuildingStats();
            temp.data         = building;
            temp.availability = 0;

            m_graphBuildings.AddNode(temp);
        }

        // set connections
        m_graphBuildings.Foreach((UT_Node <BuildingStats> node) =>
        {
            BuildingStats stats = node.GetData();

            foreach (DAT_Building.BuildDependency dep in stats.data.dependencies)
            {
                UT_Node <BuildingStats> temp = GetNode(dep.unlocksBuilding);
                m_graphBuildings.AddConnection(node, temp);
            }
        });

        GetPathLengths(5);
        GetPathLengths(1);
        GetPathLengths(33);
        m_graphBuildings.Foreach((UT_Node <BuildingStats> node) => {
            Debug.Log(node.GetData().data.name + " level: " + node.GetData().buildingLevel);
        });
    }
Esempio n. 2
0
    public UT_Node <T> AddNode(T data)
    {
        UT_Node <T> node = new UT_Node <T>(data);

        m_nodes.Add(node);
        return(node);
    }
Esempio n. 3
0
    public List <UT_Node <T> > BFS(UT_Node <T> startNode)
    {
        List <UT_Node <T> > res = new List <UT_Node <T> >();

        bool[] visited = new bool[m_nodes.Count];

        Queue <UT_Node <T> > queue = new Queue <UT_Node <T> >();

        visited[m_nodes.IndexOf(startNode)] = true;
        queue.Enqueue(startNode);

        // startNode is now used as a temporary node
        while (queue.Count > 0)
        {
            startNode = queue.Dequeue();
            res.Add(startNode);

            List <UT_Node <T> > adj = startNode.GetAdjacent();
            foreach (UT_Node <T> node in adj)
            {
                int index = m_nodes.IndexOf(node);
                if (!visited[index])
                {
                    visited[index] = true;
                    queue.Enqueue(node);
                }
            }
        }

        return(res);
    }
Esempio n. 4
0
    /// <summary>
    /// Func done by each node traversed.
    /// </summary>
    public void BFS(UT_Node <T> startNode, Action <UT_Node <T> > Func)
    {
        bool[] visited = new bool[m_nodes.Count];

        Queue <UT_Node <T> > queue = new Queue <UT_Node <T> >();

        visited[m_nodes.IndexOf(startNode)] = true;
        queue.Enqueue(startNode);

        // startNode is now used as a temporary node
        while (queue.Count > 0)
        {
            startNode = queue.Dequeue();
            Func(startNode);

            List <UT_Node <T> > adj = startNode.GetAdjacent();
            foreach (UT_Node <T> node in adj)
            {
                int index = m_nodes.IndexOf(node);
                if (!visited[index])
                {
                    visited[index] = true;
                    queue.Enqueue(node);
                }
            }
        }
    }
Esempio n. 5
0
    public List <UT_Pair <UT_Node <T>, int> > GetPathLengths(UT_Node <T> startNode)
    {
        int level = 0;
        List <UT_Pair <UT_Node <T>, int> > res = new List <UT_Pair <UT_Node <T>, int> >();

        bool[] visited = new bool[m_nodes.Count];

        Queue <UT_Pair <UT_Node <T>, int> > queue = new Queue <UT_Pair <UT_Node <T>, int> >();

        visited[m_nodes.IndexOf(startNode)] = true;
        queue.Enqueue(new UT_Pair <UT_Node <T>, int>(startNode, level));

        while (queue.Count > 0)
        {
            UT_Pair <UT_Node <T>, int> temp = queue.Dequeue();
            res.Add(temp);
            if (temp.second > level)
            {
                level++;
            }

            List <UT_Node <T> > adj = temp.first.GetAdjacent();
            foreach (UT_Node <T> node in adj)
            {
                int index = m_nodes.IndexOf(node);
                if (!visited[index])
                {
                    visited[index] = true;
                    queue.Enqueue(new UT_Pair <UT_Node <T>, int>(node, level + 1));
                }
            }
        }

        return(res);
    }
Esempio n. 6
0
 public bool RemoveNode(UT_Node <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException("Given node instance cannot be a null!");
     }
     return(m_nodes.Remove(node));
 }
Esempio n. 7
0
    public void UpdatePerDay()
    {
        m_graphBuildings.Foreach((UT_Node <BuildingStats> node) =>
        {
            node.GetData().overallMood      = 0;
            node.GetData().availability     = 0;
            node.GetData().createdInstances = 0;
        });

        List <IBuilding> buildings = M_BuildingManager.SGetBuildings();

        foreach (IBuilding building in buildings)
        {
            if (building is IBuildingHouse)
            {
                GetNode(building.GetData().id).GetData().createdInstances++;
                continue;
            }
            if (!(building is IBuildingProduction))
            {
                continue;
            }

            IBuildingProduction     prod = (IBuildingProduction)building;
            UT_Node <BuildingStats> node = GetNode(building.GetData().id);
            node.GetData().overallMood  += prod.GetMood();
            node.GetData().createdInstances++;

            if (prod.IsWorking())
            {
                M_WaresManager.SAddProductiveBuilding(prod.GetId(), prod.GetProducedWareType());
            }
            else
            {
                M_WaresManager.SRemoveProductiveBuilding(prod.GetId(), prod.GetProducedWareType());
            }
        }

        m_graphBuildings.Foreach((UT_Node <BuildingStats> node) =>
        {
            BuildingStats stats = node.GetData();
            foreach (DAT_Building.BuildDependency dep in stats.data.dependencies)
            {
                if (stats.overallMood > dep.requiredMood)
                {
                    UT_Node <BuildingStats> tempNode = GetNode(dep.unlocksBuilding);
                    tempNode.GetData().availability += 1;
                    if (tempNode.GetData().availability == tempNode.GetAdjacent().Count &&
                        m_currentBuildingsLevel < tempNode.GetData().buildingLevel)
                    {
                        m_currentBuildingsLevel = tempNode.GetData().buildingLevel;
                    }
                }
            }
        });
    }
Esempio n. 8
0
    public void PrintBFS(UT_Node <T> startNode)
    {
        List <UT_Node <T> > traversed = BFS(startNode);
        StringBuilder       builder   = new StringBuilder();

        builder.AppendLine("BFS(): ");
        for (int i = 0; i < traversed.Count; i++)
        {
            builder.AppendLine(string.Format("{0} node: {1}", i, traversed[i].GetData().ToString()));
        }
        Debug.Log(builder.ToString());
    }
Esempio n. 9
0
    public void GetPathLengths(int start)
    {
        List <UT_Node <BuildingStats> > m_nodes = m_graphBuildings.GetNodes();

        int level = 0;

        bool[] visited = new bool[m_nodes.Count];

        Queue <UT_Node <BuildingStats> > queue = new Queue <UT_Node <BuildingStats> >();

        UT_Node <BuildingStats> startNode = GetNode(start);

        visited[m_nodes.IndexOf(startNode)] = true;
        queue.Enqueue(startNode);

        startNode.GetData().buildingLevel = level;

        while (queue.Count > 0)
        {
            UT_Node <BuildingStats> temp = queue.Dequeue();

            if (temp.GetData().buildingLevel > level)
            {
                level++;
            }

            List <UT_Node <BuildingStats> > adj = temp.GetAdjacent();
            foreach (UT_Node <BuildingStats> node in adj)
            {
                int index = m_nodes.IndexOf(node);
                if (!visited[index])
                {
                    visited[index] = true;
                    node.GetData().buildingLevel = level + 1;
                    queue.Enqueue(node);
                }
            }
        }
    }
Esempio n. 10
0
    public void AddConnection(UT_Node <T> nodeOut, UT_Node <T> nodeIn)
    {
        if (nodeOut == null || nodeIn == null)
        {
            throw new ArgumentNullException("Given node instance cannot be a null!");
        }
        if (!m_nodes.Contains(nodeOut))
        {
            m_nodes.Add(nodeOut);
        }
        if (!m_nodes.Contains(nodeIn))
        {
            m_nodes.Add(nodeIn);
        }

        if (nodeIn.HasAdjacent(nodeOut))
        {
            return;
        }

        nodeOut.AddAdjacent(nodeIn);
    }
Esempio n. 11
0
 public bool HasAdjacent(UT_Node <T> node)
 {
     return(m_adjacent.Contains(node));
 }
Esempio n. 12
0
 public void RemoveAdjacent(UT_Node <T> node)
 {
     node.m_inAdjacent.Remove(this);
     m_adjacent.Remove(node);
 }
Esempio n. 13
0
 public void AddAdjacent(UT_Node <T> node)
 {
     m_adjacent.Add(node);
     node.m_inAdjacent.Add(this);
 }
Esempio n. 14
0
    public int GetCount(int id)
    {
        UT_Node <BuildingStats> node = GetNode(id);

        return(node.GetData().createdInstances);
    }