Exemplo n.º 1
0
    private void FetchTraverseMap(ParcelBot bot, ref int[,] traverseMap)
    {
        //loop over every cell
        for (int i = 0; i < BoardData.height; i++)
        {
            for (int j = 0; j < BoardData.width; j++)
            {
                BoardData.CellType staticCell = BoardData.instance.GetCellType(i, j);

                //if the cell is parcel hole, the value will be -2 else -1
                //-2 means blocked
                //-1 means walkable
                traverseMap[i, j] = staticCell == BoardData.CellType.ParcelHole ? -2 : -1;
            }
        }

        //loop over everybot except current bot that we are going to assign direction
        foreach (ParcelBot b in BoardData.instance.GetBots())
        {
            if (b == bot)
            {
                continue;
            }

            //set value -2 to the cell that each bot is standing or stepping to
            Vector2Int bCurrent  = b.GetCurrent();
            Vector2Int bNextStep = b.GetNextStep();
            traverseMap[bCurrent.y, bCurrent.x]   = -2;
            traverseMap[bNextStep.y, bNextStep.x] = -2;
        }
    }
Exemplo n.º 2
0
    public IEnumerator RequestNextStep(ParcelBot bot)
    {
        Vector2Int target  = bot.GetTarget();
        Vector2Int current = bot.GetCurrent();

        //start the algorithm
        yield return(StartCoroutine(CalculateNextStep(bot, current, target)));
    }
    public void Start()
    {
        BoardData.instance.ClearAllBots();

        //create bots object. the amount is according to BoardData.botNum
        for (int i = 0; i < BoardData.botNum; i++)
        {
            ParcelBot bot = Instantiate(prefab) as ParcelBot;
            bot.transform.parent = transform;
            bot.Initialize(i);
            BoardData.instance.AddBot(bot);
        }
    }
Exemplo n.º 4
0
 public void AddBot(ParcelBot b)
 {
     //add new bot b to bot list
     bots.Add(b);
 }
Exemplo n.º 5
0
    //this method will be called when clicked
    private void OnMouseDown()
    {
        //find the bot with the minimum load
        //find the block with minimum load

        ParcelBot         responsibleBot  = null;
        List <ParcelBot>  allBots         = BoardData.instance.GetBots();
        List <Vector2Int> availableBlocks = new List <Vector2Int>(nearbyBlocks);

        int       minLoad = 999999;
        ParcelBot minBot  = null;

        foreach (ParcelBot bot in allBots)
        {
            //int load = bot.GetLoadCount();
            int load = bot.GetLoadCount() + Mathf.Abs(bot.GetId() - x) + Mathf.Abs(y);
            if (bot.IsFree() == false)
            {
                load += 100;
            }
            //float load = (float)(bot.GetLoadCount() + Mathf.Abs(bot.GetCurrent().x - x) + Mathf.Abs(bot.GetCurrent().y - y));
            //print(bot.GetCurrent());
            //print(x +" " + y);
            //print(load);
            //print("");

            if (load < minLoad)
            {
                responsibleBot = bot;
                minLoad        = load;
                minBot         = bot;
            }

            if (responsibleBot == null && bot.IsFree())
            {
                responsibleBot = bot;
            }

            if (availableBlocks.Contains(bot.GetTarget()))
            {
                availableBlocks.Remove(bot.GetTarget());
            }
        }

        //forget what this condition does but don't dare to remove it ;-)
        if (responsibleBot == null)
        {
            responsibleBot = minBot;
        }

        //no bot with enough battery available
        if (responsibleBot == null)
        {
            return;
        }

        //if there is no available nearby block that is currently free
        //just pick one randomly
        if (availableBlocks.Count == 0)
        {
            availableBlocks.Add(nearbyBlocks[Random.Range(0, 3)]);
        }

        //assign the bot to move to the nearby block with minimum load
        responsibleBot.AssignNextParcelPoint(availableBlocks[0], id);
    }
Exemplo n.º 6
0
    private IEnumerator CalculateNextStep(ParcelBot bot, Vector2Int current, Vector2Int target)
    {
        //create temp map arrays and traverse queue for calculation
        int[,] traverseMap = new int[BoardData.height, BoardData.width];
        Queue <Vector2Int> traverseQueue = new Queue <Vector2Int>();

        //fill values into map arrays
        FetchTraverseMap(bot, ref traverseMap);

        //if the target cell is not available (not empty)
        if (traverseMap[target.y, target.x] == -2)
        {
            //stop bot movement
            bot.SetNextStep(current);
            yield break;
        }

        //initialize first value in the queue
        traverseMap[current.y, current.x] = 0;
        traverseQueue.Clear();
        traverseQueue.Enqueue(current);

        //while the queue is not empty
        while (traverseQueue.Count > 0)
        {
            Vector2Int now = traverseQueue.Dequeue();

            //add nearby cell's position that bot can walk to to the queue for next round of calculation
            AddPossibleNextStepToQueue(now, target, ref traverseMap, ref traverseQueue);
            //get the shortest cost to travel from 'current' to 'now'
            traverseMap[now.y, now.x] = GetBestValueFromAround(now, ref traverseMap) + 1;
            //stop loop when reached target cell
            if (traverseMap[target.y, target.x] != -1)
            {
                break;
            }
        }

        //DebugTraverseMap();

        //if the target is not reached
        if (traverseMap[target.y, target.x] < 0)
        {
            //stop bot movement
            bot.SetNextStep(current);
            yield break;
        }

        //initialize values to finc next step from calculated map
        Vector2Int tracker = target;    //stat from target cell
        Vector2Int prev    = tracker;   //this var is to temporarily keep tracking position

        //do the block until tracker reached start cell
        while (tracker != current)
        {
            prev = tracker;
            //move tracker one step closer to start point
            tracker = GetNextBackStep(tracker, ref traverseMap);
        }

        //tell bot to move to the cell before the tracker reached bot
        bot.SetNextStep(prev);
    }