Esempio n. 1
0
    private void MovementDecisionWithMessages()
    {
        List <Agent> agentsOrderingToMove = mailBox.ReadMoveOrders(id);

        isOrderedToMove = agentsOrderingToMove.Count >= 1;

        List <Vector2Int> poolOfMoves = board.PossibleMovesOfAgent(this);

        if (actualPos == desiredPos)
        {
            // case when no movement is required
            if (!isOrderedToMove)
            {
                return;
            }

            poolOfMoves.Remove(actualPos);
            if (poolOfMoves.Count == 0) // no adjacent cell to move to
            {
                Agent adjacentAgent = board.GetAnAdjacentAgent(this, agentsOrderingToMove);
                if (adjacentAgent != null)
                {
                    EmitMoveOrder(adjacentAgent);
                }
            }
            else
            {
                int rdmIndex = Random.Range(0, poolOfMoves.Count);
                Move(poolOfMoves[rdmIndex]);
                SignalToAgentsWhoOrderedMeToMove(agentsOrderingToMove);
            }
            return;
        }

        if (Random.Range(0.0f, 1.0f) > 0.8 && poolOfMoves.Count > 0)
        {
            int rdmIndex = Random.Range(0, poolOfMoves.Count);
            Move(poolOfMoves[rdmIndex]);
            SignalToAgentsWhoOrderedMeToMove(agentsOrderingToMove);
            return;
        }

        poolOfMoves = board.AdjacentCells(actualPos);
        foreach (Agent otherAgent in agentsOrderingToMove)
        {
            poolOfMoves.Remove(otherAgent.actualPos);
        }
        if (isOrderedToMove)
        {
            poolOfMoves.Remove(actualPos);
        }
        // si entouré d'agent ordonnant un déplacement: ne rien faire
        if (poolOfMoves.Count == 0)
        {
            Debug.Log("entouré d'agents ordonnat un déplacement");
            return;
        }

        //Vector2Int selectedMove = Geometry.ClosestCellToGoalCell(poolOfMoves.ToArray(), desiredPos);
        List <Vector2Int> goodEnoughMoves = Geometry.CloserCellsToGoalCell(poolOfMoves.ToArray(), desiredPos, this.actualPos);
        List <Vector2Int> freeCells       = new List <Vector2Int>();

        foreach (Vector2Int move in goodEnoughMoves)
        {
            if (move != actualPos && board.IsCellOccupied(move) == null)
            {
                freeCells.Add(move);
            }
        }


        if (freeCells.Count > 0)
        {
            int        rdmIndex = Random.Range(0, freeCells.Count);
            Vector2Int freeCell = Geometry.ClosestCellToGoalCell(freeCells.ToArray(), desiredPos);
            if (freeCell == actualPos)
            {
                return;
            }
            Move(freeCells[rdmIndex]);
            SignalToAgentsWhoOrderedMeToMove(agentsOrderingToMove);
        }
        else
        {
            if (goodEnoughMoves.Count == 0)
            {
                Debug.Log("What");
                //List<Vector2Int> poolOfMoves2 = board.AdjacentCells(actualPos);
                //Vector2Int rdmCell = poolOfMoves2[Random.Range(0, poolOfMoves.Count)];
                //if (board.IsCellOccupied(rdmCell)) EmitMoveOrder(rdmCell);
                //else {
                //    Move(rdmCell);
                //    SignalToAgentsWhoOrderedMeToMove(agentsOrderingToMove);
                //}
                return;
            }
            Vector2Int aGoodEnoughMove          = goodEnoughMoves[Random.Range(0, goodEnoughMoves.Count)];
            Agent      possibleObstructingAgent = board.IsCellOccupied(aGoodEnoughMove);
            if (possibleObstructingAgent != null && possibleObstructingAgent != this)
            {
                EmitMoveOrder(possibleObstructingAgent);
            }
        }
    }