void IBotTick.BotTick(IBot bot) { if (firstTick) { DeployMcvs(bot, false); firstTick = false; } if (--scanInterval <= 0) { scanInterval = Info.ScanForNewMcvInterval; DeployMcvs(bot, true); // No construction yards - Build a new MCV if (ShouldBuildMCV()) { var unitBuilder = requestUnitProduction.FirstOrDefault(Exts.IsTraitEnabled); if (unitBuilder != null) { var mcvInfo = AIUtils.GetInfoByCommonName(Info.McvTypes, player); if (unitBuilder.RequestedProductionCount(bot, mcvInfo.Name) == 0) { unitBuilder.RequestUnitProduction(bot, mcvInfo.Name); } } } } }
protected void ProduceHarvesters(IBot bot) { // Less harvesters than refineries - build a new harvester var unitBuilder = requestUnitProduction.FirstOrDefault(Exts.IsTraitEnabled); if (unitBuilder != null && Info.HarvesterTypes.Any()) { var harvInfo = AIUtils.GetInfoByCommonName(Info.HarvesterTypes, player); var numHarvesters = AIUtils.CountActorByCommonName(Info.HarvesterTypes, player); if (numHarvesters >= Info.MaxHarvesters) { return; } var harvCountTooLow = numHarvesters < AIUtils.CountBuildingByCommonName(Info.RefineryTypes, player) * Info.HarvestersPerRefinery; if (harvCountTooLow && unitBuilder.RequestedProductionCount(bot, harvInfo.Name) == 0) { unitBuilder.RequestUnitProduction(bot, harvInfo.Name); } } }
Target FindNextResource(Actor actor, MinerTraitWrapper miner) { var towerInfo = AIUtils.GetInfoByCommonName(Info.DeployedActorTypes, player); var buildingInfo = towerInfo.TraitInfo <BuildingInfo>(); Func <CPos, bool> isValidResource = cell => domainIndex.IsPassable(actor.Location, cell, miner.Locomotor.Info) && Info.DeployableTerrainTypes.Contains(world.Map.GetTerrainInfo(cell).Type) && miner.Locomotor.CanStayInCell(cell) && world.CanPlaceBuilding(cell + miner.Transforms.Info.Offset, towerInfo, buildingInfo, actor); var path = pathfinder.FindPath( PathSearch.Search(world, miner.Locomotor, actor, BlockedByActor.Stationary, isValidResource) .WithCustomCost(loc => world.FindActorsInCircle(world.Map.CenterOfCell(loc), Info.EnemyAvoidanceRadius) .Where(u => !u.IsDead && actor.Owner.Stances[u.Owner] == Stance.Enemy) .Sum(u => Math.Max(WDist.Zero.Length, Info.EnemyAvoidanceRadius.Length - (world.Map.CenterOfCell(loc) - u.CenterPosition).Length))) .FromPoint(actor.Location)); if (path.Count == 0) { return(Target.Invalid); } return(Target.FromCell(world, path[0])); }
void IBotTick.BotTick(IBot bot) { if (resourceLayer == null || resourceLayer.IsEmpty) { return; } if (--scanForIdleHarvestersTicks > 0) { return; } var toRemove = harvesters.Keys.Where(unitCannotBeOrdered).ToList(); foreach (var a in toRemove) { harvesters.Remove(a); } scanForIdleHarvestersTicks = Info.ScanForIdleHarvestersInterval; // Find new harvesters // TODO: Look for a more performance-friendly way to update this list var newHarvesters = world.ActorsHavingTrait <Harvester>().Where(a => a.Owner == player && !harvesters.ContainsKey(a)); foreach (var a in newHarvesters) { harvesters[a] = new HarvesterTraitWrapper(a); } // Find idle harvesters and give them orders: foreach (var h in harvesters) { if (!h.Key.IsIdle) { // Ignore this actor if FindAndDeliverResources is working fine or it is performing a different activity if (!(h.Key.CurrentActivity is FindAndDeliverResources act) || !act.LastSearchFailed) { continue; } } if (h.Value.Parachutable != null && h.Value.Parachutable.IsInAir) { continue; } // Tell the idle harvester to quit slacking: var newSafeResourcePatch = FindNextResource(h.Key, h.Value); AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(h.Key, newSafeResourcePatch)); bot.QueueOrder(new Order("Harvest", h.Key, newSafeResourcePatch, false)); } // Less harvesters than refineries - build a new harvester var unitBuilder = requestUnitProduction.FirstOrDefault(Exts.IsTraitEnabled); if (unitBuilder != null && Info.HarvesterTypes.Any()) { var harvInfo = AIUtils.GetInfoByCommonName(Info.HarvesterTypes, player); var harvCountTooLow = AIUtils.CountActorByCommonName(Info.HarvesterTypes, player) < AIUtils.CountBuildingByCommonName(Info.RefineryTypes, player); if (harvCountTooLow && unitBuilder.RequestedProductionCount(bot, harvInfo.Name) == 0) { unitBuilder.RequestUnitProduction(bot, harvInfo.Name); } } }
void IBotTick.BotTick(IBot bot) { if (resourceLayer == null || resourceLayer.IsResourceLayerEmpty) { return; } if (--scanForIdleMinersTicks > 0) { return; } scanForIdleMinersTicks = Info.MinimumScanDelay; var toRemove = miners.Keys.Where(unitCannotBeOrdered).ToList(); foreach (var a in toRemove) { miners.Remove(a); } // TODO: Look for a more performance friendly way to update this list var newMiners = world.Actors.Where(a => Info.DeployableActorTypes.Contains(a.Info.Name) && a.Owner == player && !miners.ContainsKey(a)); foreach (var a in newMiners) { miners[a] = new MinerTraitWrapper(a); } foreach (var miner in miners) { if (!miner.Key.IsIdle) { continue; } if (Info.DeployableTerrainTypes.Contains(world.Map.GetTerrainInfo(miner.Key.Location).Type)) { bot.QueueOrder(new Order("DeployTransform", miner.Key, true)); continue; } // Tell the idle miner to quit slacking: var newSafeResourcePatch = FindNextResource(miner.Key, miner.Value); if (newSafeResourcePatch.Type == TargetType.Invalid) { scanForIdleMinersTicks = Info.LastSearchFailedDelay; return; } AIUtils.BotDebug("AI: Miner {0} is idle. Ordering to {1} in search for new resources.".F(miner.Key, newSafeResourcePatch)); bot.QueueOrder(new Order("Move", miner.Key, newSafeResourcePatch, true)); } // Keep the economy running before starving out. var unitBuilder = requestUnitProduction.FirstOrDefault(Exts.IsTraitEnabled); if (unitBuilder != null) { var minerInfo = AIUtils.GetInfoByCommonName(Info.DeployableActorTypes, player); var miningTowers = AIUtils.CountBuildingByCommonName(Info.DeployedActorTypes, player); if (miningTowers < Info.MinimumDeployedActors && unitBuilder.RequestedProductionCount(bot, minerInfo.Name) == 0) { unitBuilder.RequestUnitProduction(bot, minerInfo.Name); } } }