Exemple #1
0
        public override void RollBackInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn,
                                          LevelBoard.Directions dirOut, bool connected)
        {
            // Pop last Log entry
            this.CrossingLog.Pop();

            // If the Slime had come from a Crossing and a connection was made
            if (this.CrossingLog.Count > 0 && connected)
            {
                var last = this.CrossingLog.Peek(); // Get last visited Crossing

                // If the last visited Crossing is not the present Crossing
                if (last.Key != crossingCoords)
                {
                    // Disconnect the Crossings
                    this.UnregisterCrossingConnection(last.Key, last.Value, crossingCoords, dirIn);
                }
                else if (this.DeadEnds)
                {
                    this.UnregisterDeadEnd(crossingCoords, LevelBoard.InvertDirection(dirIn));
                }
            }

            base.RollBackInfo(crossingCoords, dirIn, dirOut, connected);
        }
Exemple #2
0
        public override bool CommunicateInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut)
        {
            base.CommunicateInfo(crossingCoords, dirIn, dirOut);

            var connected = false;

            // If the Slime has come from a Crossing
            if (this.CrossingLog.Count > 0)
            {
                var last = this.CrossingLog.Peek(); // Get last visited Crossing

                // If the last visited Crossing is not the present Crossing
                if (last.Key != crossingCoords)
                {
                    // Add this Crossing as the last one's child
                    connected = this.RegisterCrossingConnection(last.Key, last.Value, crossingCoords, dirIn);
                }
                else if (this.DeadEnds && !this.PathBlocked)
                {
                    this.RegisterDeadEnd(crossingCoords, LevelBoard.InvertDirection(dirIn));

                    connected = true;
                }

                this.PathBlocked = false;
            }

            // Save this Crossing as its most recently visited, and Direction from which it left
            this.CrossingLog.Push(new KeyValuePair <Vector2Int, LevelBoard.Directions>(crossingCoords, dirOut));

            return(connected);
        }
Exemple #3
0
        public virtual void RollBackInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut, bool connected)
        {
            // Unregister path by which the present Crossing was reached
            this.UnregisterExploredPath(crossingCoords, LevelBoard.InvertDirection(dirIn));

            // Unregister path chosen to exit present Crossing
            this.UnregisterExploredPath(crossingCoords, dirOut);
        }
Exemple #4
0
        public virtual bool CommunicateInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut)
        {
            // Register path chosen to exit present Crossing
            this.RegisterExploredPath(crossingCoords, dirOut);

            // Register path by which the present Crossing was reached
            this.RegisterExploredPath(crossingCoords, LevelBoard.InvertDirection(dirIn));

            return(true);
        }
Exemple #5
0
        public void RemoveCrossingConnection(Vector2Int startCoords, LevelBoard.Directions dirOut,
                                             Vector2Int endCoords, LevelBoard.Directions dirIn)
        {
            if (this.Crossings.TryGetValue(startCoords, out var start) &&
                this.Crossings.TryGetValue(endCoords, out var end))
            {
                start.RemoveConnection(dirOut);

                var invDir = LevelBoard.InvertDirection(dirIn);

                end.RemoveConnection(invDir);

                Debug.Log("Removed connection between " + startCoords + " through " + dirOut + " to " + endCoords);
                Debug.Log("Removed connection between " + endCoords + " through " + invDir + " to " + startCoords);
            }
        }
Exemple #6
0
        public bool AddCrossingConnection(Vector2Int startCoords, LevelBoard.Directions dirOut,
                                          Vector2Int endCoords, LevelBoard.Directions dirIn)
        {
            if (this.Crossings.TryGetValue(startCoords, out var start) &&
                this.Crossings.TryGetValue(endCoords, out var end))
            {
                var first = start.AddConnection(dirOut, end);

                var invDir = LevelBoard.InvertDirection(dirIn);

                var second = end.AddConnection(invDir, start);

                //if(first)  Debug.Log("Added connection between " + startCoords + " through " + dirOut + " to " + endCoords);
                //if(second) Debug.Log("Added connection between " + endCoords   + " through " + invDir + " to " + startCoords);

                return(first || second);
            }

            return(false);
        }
Exemple #7
0
        public override Action Available(Agent agent)
        {
            var adjacents = this.CheckAdjacentSolderTiles(agent);

            #region Crossing Attributes
            var crossing = false;

            Dictionary <LevelBoard.Directions, int> utilities = null;

            var slime = (SmarterElectricSlime)agent;

            // If this is a Crossing
            if (adjacents.Count > 2)
            {
                crossing = true;

                slime.UpdateCrossing(slime.Coords, new List <LevelBoard.Directions>(adjacents.Keys));

                utilities = slime.GetUtilities(slime.Coords);
            }
            #endregion

            var start = ((int)(agent.Orientation)) / 2;

            var maxUtility = float.MinValue;
            var bestChoice = LevelBoard.Directions.None;
            var isMove     = true;

            // For each cardinal Direction
            for (var i = 0; i < 4; i++)
            {
                // Make the Direction
                var dir = (LevelBoard.Directions)(((start + i + 3) * 2) % 8);

                // If there is an adjacent Solder Tile
                if (adjacents.TryGetValue(dir, out var piece))
                {
                    var utility = (4 - i) * 0.25f;

                    // If this is a Crossing get the Directions utility
                    if (crossing && utilities.ContainsKey(dir))
                    {
                        utility += utilities[dir];
                    }

                    if (dir == LevelBoard.InvertDirection(agent.Orientation))
                    {
                        if (utility > 0)
                        {
                            utility *= 0.5f;
                        }
                        else
                        {
                            utility *= 2f;
                        }
                    }

                    // If there's a Piece that is not a Component
                    // Or a Component that is at capacity, check the next Direction
                    if ((piece != null && !(piece is CircuitComponent)) ||
                        (piece is CircuitComponent component && component.Stats.Food >= component.Stats.MaxFood))
                    {
                        if (utility > maxUtility)
                        {
                            slime.PathBlocked = true;
                        }
                        continue;
                    }

                    // If the utility is better going this way
                    if (utility > maxUtility)
                    {
                        maxUtility = utility;

                        bestChoice = dir;

                        isMove = piece == null;
                    }
                }
            }

            // If the best choice is not to stay in place
            if (bestChoice != LevelBoard.Directions.None)
            {
                if (isMove)
                {
                    return(new SmarterElectricMovement(bestChoice, LevelBoard.GetAdjacentCoords(agent.Coords, bestChoice), crossing));
                }
                else
                {
                    return(new Charge((CircuitComponent)adjacents[bestChoice], crossing));
                }
            }

            return(null);
        }