Esempio n. 1
0
    public ActionSimulatorGraph(Dictionary <Unit, UnitDisplacement> displacements, Board board)
    {
        this.nodes = new Dictionary <Vector2, SimulatorNode>();
        this.board = board;

        foreach (KeyValuePair <Unit, UnitDisplacement> pair in displacements)
        {
            SimulatedDisplacement sim = new SimulatedDisplacement(pair.Value);
            Vector2 startCoord        = pair.Value.GetStartCoordinate();
            Vector2 endCoord          = sim.GetNextSimulationStep(board);

            if (!nodes.ContainsKey(startCoord))
            {
                nodes.Add(startCoord, new SimulatorNode(startCoord));
            }
            if (!nodes.ContainsKey(endCoord))
            {
                nodes.Add(endCoord, new SimulatorNode(endCoord));
            }
            nodes[endCoord].AddEdge(new SimulatorEdge(nodes[startCoord], nodes[endCoord], sim));
        }
    }
Esempio n. 2
0
    public void Relax()
    {
        bool changed;

        do
        {
            Dictionary <Vector2, SimulatorNode> nextIter = new Dictionary <Vector2, SimulatorNode>();
            changed = false;

            foreach (KeyValuePair <Vector2, SimulatorNode> pair in nodes)
            {
                SimulatorNode node = pair.Value;
                if (IsRelaxed(node))
                {
                    //No Conflict Resolution Needed, update unit values
                    foreach (SimulatorEdge edge in node.edges)
                    {
                        SimulatedDisplacement sim = edge.sim;
                        Vector2 current           = sim.GetCurrentVector();
                        Vector2 next;

                        if (!sim.conflict && !sim.outOfBounds)
                        {
                            next    = sim.GetNextSimulationStep(board);
                            changed = changed || next != current;
                        }
                        else
                        {
                            next = current;                             //do not move forward
                        }

                        //Create and push to simulation our new location
                        if (!nextIter.ContainsKey(current))
                        {
                            nextIter.Add(current, new SimulatorNode(current));
                        }
                        if (!nextIter.ContainsKey(next))
                        {
                            nextIter.Add(next, new SimulatorNode(next));
                        }
                        nextIter[next].AddEdge(new SimulatorEdge(nextIter[current], nextIter[next], sim));
                    }
                }
                else
                {
                    //Create and push to simulation our new location
                    foreach (SimulatorEdge edge in node.edges)
                    {
                        SimulatedDisplacement sim = edge.sim;
                        Vector2 current           = sim.GetCurrentVector();
                        Vector2 prev = sim.GetPreviousSimulationStep(board);

                        sim.conflict = true;
                        changed      = changed || prev != current;

                        //Create and push to simulation our new location
                        if (!nextIter.ContainsKey(current))
                        {
                            nextIter.Add(current, new SimulatorNode(current));
                        }
                        if (!nextIter.ContainsKey(prev))
                        {
                            nextIter.Add(prev, new SimulatorNode(prev));
                        }
                        nextIter[prev].AddEdge(new SimulatorEdge(nextIter[current], nextIter[prev], sim));
                    }
                }
            }
            nodes = nextIter;
        } while(changed);
    }