private void tryAnOrder() { if (orders.Count > 0) { int rand = (int)Random.Range(0, orders.Count); Order o = orders[rand]; if (o.deliveryModule == null) { bool foundAnOrder = false; //IT IS A PUSH, we must find an order for that resource, or a container. for (int i = 0; i < orders.Count && !foundAnOrder; ++i) { if (orders[i].r == o.r && orders[i].deliveryModule != null && orders[i].crate == null) // if order is about the same resource AND not a push of crate { //Debug.Log("Merging orders " + o.id + " and " + orders[i].id); o.deliveryModule = orders[i].deliveryModule; //Merging the orders and deleting one. orders.Remove(orders[i]); foundAnOrder = true; } } if (!foundAnOrder) { //We must find a container ContainerController cc = getRandomAvailableContainer(); if (cc != null) { //Debug.Log("order" + o.id + " incontainer"); o.deliveryModule = cc; cc.notifyIncomingDelivry(); } else { return; //Can't find any order or containers, can't do anything. } } } if (amounts[o.r] > 0) { List <RobotController> readyBots = getAvailableBots(); if (readyBots.Count > 0) { if (o.pickUpModule != null || o.crate != null) { //ITS A PUSH getRandomAvailableBot().setOrder(o); orders.Remove(o); } else { //GET A CRATE CrateBehavior c = getCrateOf(o.r); if (c != null) { //Debug.Log("Assigning crate " + c.GetInstanceID() + " to order" + o.id + " "); o.crate = c; crates.Remove(c); --amounts[c.r]; //ASSIGN ORDER getRandomAvailableBot().setOrder(o); orders.Remove(o); } else { ContainerController cc = getContainerWith(o.r); if (cc != null) { o.pickUpModule = cc; cc.bookCrate(o.r); --amounts[o.r]; //ASSIGN ORDER getRandomAvailableBot().setOrder(o); orders.Remove(o); } } } } else if (debug) { Debug.Log("Not enough bots for " + o.r + " order."); } } else if (debug) { Debug.Log("Not enough resrouces for " + o.r + " order."); } } else if (debug) { Debug.Log("No order."); } }