private void ProduceOrder(Order order) { // If the unit is told to build a structure far away, and he is not in range yet if (CanMove() && order.targetPosition != null && order.targetPosition.Distance(position) > 1) { MoveTowards(order.targetPosition); return; } status = Status.Producing; // If the player does not has enough moneyz if (owner.gold < order.unitTypeBuild.goldCost || owner.iron < order.unitTypeBuild.ironCost || owner.manaCystals < order.unitTypeBuild.manaCrystalsCost) { // Try again later engine.ScheduleUpdate(buildRetryCooldown, this); return; } // Find place to put unit Position targetLocation = position; Position producePosition = null; if (order.targetPosition == null) { foreach (var testPosition in Pathfinder.BreadthFirst(engine, targetLocation, distance: 1)) { if (engine.GetUnitAt(testPosition) == null) { producePosition = testPosition; break; } } } else { producePosition = order.targetPosition; } if (producePosition == null) { // Try again in the future engine.ScheduleUpdate(buildRetryCooldown, this); return; } // Subtract resources owner.gold -= order.unitTypeBuild.goldCost; owner.iron -= order.unitTypeBuild.ironCost; owner.manaCystals -= order.unitTypeBuild.manaCrystalsCost; // Create the unit Unit u = engine.AddUnit(order.unitTypeBuild, producePosition, owner); if (order.targetPosition != null && u.CanMove()) { // Tell the produced unit to go to the rally point engine.OrderMove(u, order.targetPosition); } else if (u.type.name.Equals("GoldMine")) { engine.OrderGather(u, u.position); } NextOrder(); engine.ScheduleUpdate(buildCooldown, this); }