private void FinalizeTargetDb() { MyPlanetTmp = MyGamePruningStructure.GetClosestPlanet(ScanVolume.Center); ObstructionsTmp.Clear(); StaticsInRangeTmp.Clear(); for (int i = 0; i < NearByEntitiesTmp; i++) { var ent = _possibleTargets[i]; if (Session.ShieldApiLoaded && ent.DefinitionId?.SubtypeId == Session.ShieldHash && ent.Render.Visible) { var shieldblock = Session.SApi.MatchEntToShieldFast(ent, false); if (shieldblock != null) { NearByShieldsTmp.Add(new Shields { Id = ent.Hierarchy.ChildId, ShieldEnt = ent, ShieldBlock = (MyCubeBlock)shieldblock }); } } var voxel = ent as MyVoxelBase; var grid = ent as MyCubeGrid; var safeZone = ent as MySafeZone; var blockingThings = safeZone != null || ent.Physics != null && grid != null || voxel != null && voxel == voxel.RootVoxel; if (!blockingThings || voxel != null && (voxel.RootVoxel is MyPlanet || voxel.PositionComp.LocalVolume.Radius < 15)) { continue; } if (voxel != null || safeZone != null || ent.Physics.IsStatic) { StaticsInRangeTmp.Add(ent); } GridMap map; if (grid != null && (PrevSubGrids.Contains(grid) || ValidGrids.Contains(ent) || grid.PositionComp.LocalVolume.Radius < 10 || Session.GridToInfoMap.TryGetValue(grid, out map) && map.Trash || grid.BigOwners.Count == 0)) { continue; } ObstructionsTmp.Add(ent); } foreach (var pair in NoTargetLos) { if (Session.Tick - pair.Value > 120) { uint lastLosTick; NoTargetLos.TryRemove(pair.Key, out lastLosTick); } } ValidGrids.Clear(); _possibleTargets.Clear(); }
public void MakeMove(BoardState board) { Debug.Log("AI IS MAKING A MOVE"); // first, let's get a list of all of our active units List <Unit> activeUnits = new List <Unit>(); foreach (Grid grid in board.GetGrids()) { if (grid.GetActiveUnit(team) != null) { activeUnits.Add(grid.GetActiveUnit()); } } // what are our potential moves with them? List <PotentialMove> potentialMoves = new List <PotentialMove>(); foreach (Unit unit in activeUnits) { foreach (Vector2 direction in directions) { int distance = unit.GetGrid().GetOccupants().Length; for (int i = 1; i <= distance; i++) { Vector2 coords = unit.GetGrid().GetCoords() + (i * direction); bool outOfX = coords.x < 0 || coords.x >= board.GetGrids().GetLength(0); bool outOfY = coords.y < 0 || coords.y >= board.GetGrids().GetLength(1); if (outOfX || outOfY) { break; } Grid targetGrid = board.GetGrids()[(int)coords.x, (int)coords.y]; PotentialMove move = new PotentialMove(unit, targetGrid); potentialMoves.Add(move); ValidGrids grids = controller.GetValidGrids(unit, targetGrid); if (grids.valid) { potentialMoves.Add(new PotentialMove(unit, targetGrid)); } } } } // if any of those are an enemy, take it List <PotentialMove> enemies = new List <PotentialMove>(); foreach (PotentialMove move in potentialMoves) { if (move.targetGrid.GetActiveUnit() != null && !move.targetGrid.GetActiveUnit().GetTeam().Equals(team)) { enemies.Add(move); } } if (enemies.Count > 0) { controller.RandomDelay(this, minWait, maxWait, enemies); } // okay, no enemies in range, move toward one then else { controller.RandomDelay(this, minWait, maxWait, potentialMoves); } }
private void FinalizeTargetDb() { MyPlanetTmp = MyGamePruningStructure.GetClosestPlanet(GridVolume.Center); ShieldNearTmp = false; ObstructionsTmp.Clear(); StaticsInRangeTmp.Clear(); for (int i = 0; i < _possibleTargets.Count; i++) { var ent = _possibleTargets[i]; var hasPhysics = ent.Physics != null; if (Session.ShieldApiLoaded && hasPhysics && !ent.Physics.Enabled && ent.Physics.IsPhantom && !ent.Render.CastShadows) { long testId; long.TryParse(ent.Name, out testId); if (testId != 0) { MyEntity shieldEnt; if (testId == MyGrid.EntityId) { MyShieldTmp = ent; } else if (MyEntities.TryGetEntityById(testId, out shieldEnt)) { var shieldGrid = shieldEnt as MyCubeGrid; if (shieldGrid != null && MyGrid.IsSameConstructAs(shieldGrid)) { MyShieldTmp = ent; } } else if (!ShieldNearTmp) { ShieldNearTmp = true; } } } var voxel = ent as MyVoxelBase; var grid = ent as MyCubeGrid; var blockingThings = ent.Physics != null && (voxel != null && voxel.RootVoxel == voxel || grid != null); if (!blockingThings) { continue; } if (ent.Physics.IsStatic) { if (voxel is MyPlanet) { continue; } StaticsInRangeTmp.Add(ent); } if (grid != null && (PrevSubGrids.Contains(grid) || ValidGrids.Contains(ent) || grid.PositionComp.LocalVolume.Radius < 6)) { continue; } ObstructionsTmp.Add(ent); } ValidGrids.Clear(); _possibleTargets.Clear(); }
internal void Scan() { using (_scanLock.AcquireExclusiveUsing()) { if (!Scanning && Session.Tick - _lastScan > 100) { Scanning = true; _lastScan = Session.Tick; GridVolume.Radius = MaxTargetingRange; MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref GridVolume, _possibleTargets); foreach (var grid in PrevSubGrids) { RemSubGrids.Add(grid); } PrevSubGrids.Clear(); for (int i = 0; i < _possibleTargets.Count; i++) { var ent = _possibleTargets[i]; using (ent.Pin()) { if (ent is MyVoxelBase || ent.Physics == null || ent is MyFloatingObject || ent.MarkedForClose || !ent.InScene || ent.IsPreview || ent.Physics.IsPhantom) { continue; } var grid = ent as MyCubeGrid; if (grid != null && MyGrid.IsSameConstructAs(grid)) { PrevSubGrids.Add(grid); continue; } Sandbox.ModAPI.Ingame.MyDetectedEntityInfo entInfo; if (!CreateEntInfo(ent, MyOwner, out entInfo)) { continue; } switch (entInfo.Relationship) { case MyRelationsBetweenPlayerAndBlock.Owner: case MyRelationsBetweenPlayerAndBlock.FactionShare: case MyRelationsBetweenPlayerAndBlock.Friends: continue; } if (grid != null) { FatMap fatMap; if (!Session.GridToFatMap.TryGetValue(grid, out fatMap) || fatMap.Trash) { continue; } var allFat = fatMap.MyCubeBocks; var fatCount = allFat.Count; if (fatCount <= 0 || !grid.IsPowered) { continue; } if (fatCount <= 20) // possible debris { var valid = false; for (int j = 0; j < fatCount; j++) { var fat = allFat[j]; if (fat is IMyTerminalBlock && fat.IsWorking) { valid = true; break; } } if (!valid) { continue; } } int partCount; GridAi targetAi; if (Session.GridTargetingAIs.TryGetValue(grid, out targetAi)) { targetAi.TargetAisTmp.Add(this); TargetAisTmp.Add(targetAi); partCount = targetAi.Construct.BlockCount; } else { partCount = fatMap.MostBlocks; } NewEntities.Add(new DetectInfo(Session, ent, entInfo, partCount, fatCount)); ValidGrids.Add(ent); } else { NewEntities.Add(new DetectInfo(Session, ent, entInfo, 1, 0)); } } } FinalizeTargetDb(); SubGridDetect(); } Scanning = false; } }
internal void Scan() { MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref ScanVolume, _possibleTargets); NearByEntitiesTmp = _possibleTargets.Count; foreach (var grid in PrevSubGrids) { RemSubGrids.Add((MyCubeGrid)grid); } PrevSubGrids.Clear(); for (int i = 0; i < NearByEntitiesTmp; i++) { var ent = _possibleTargets[i]; using (ent.Pin()) { if (ent is MyVoxelBase || ent.Physics == null || ent is MyFloatingObject || ent.MarkedForClose || !ent.InScene || ent.IsPreview || ent.Physics.IsPhantom) { continue; } var grid = ent as MyCubeGrid; if (grid != null && MyGrid.IsSameConstructAs(grid)) { PrevSubGrids.Add(grid); continue; } Sandbox.ModAPI.Ingame.MyDetectedEntityInfo entInfo; if (!CreateEntInfo(ent, AiOwner, out entInfo)) { continue; } switch (entInfo.Relationship) { case MyRelationsBetweenPlayerAndBlock.Owner: case MyRelationsBetweenPlayerAndBlock.FactionShare: case MyRelationsBetweenPlayerAndBlock.Friends: continue; } if (grid != null) { GridMap gridMap; if (!Session.GridToInfoMap.TryGetValue(grid, out gridMap) || gridMap.Trash) { continue; } var allFat = gridMap.MyCubeBocks; var fatCount = allFat.Count; if (fatCount <= 0) { continue; } var loneWarhead = false; if (fatCount <= 20) // possible debris { var valid = false; for (int j = 0; j < fatCount; j++) { var fat = allFat[j]; var warhead = fat is IMyWarhead; if (warhead || fat is IMyTerminalBlock && fat.IsWorking) { loneWarhead = warhead && fatCount == 1; valid = true; break; } } if (!valid) { continue; } } int partCount; GridAi targetAi; if (Session.GridTargetingAIs.TryGetValue(grid, out targetAi)) { targetAi.TargetAisTmp.Add(this); TargetAisTmp.Add(targetAi); partCount = targetAi.Construct.BlockCount; } else { partCount = gridMap.MostBlocks; } NewEntities.Add(new DetectInfo(Session, ent, entInfo, partCount, !loneWarhead? fatCount : 2));// bump warhead to 2 fatblocks so its not ignored by targeting ValidGrids.Add(ent); } else { NewEntities.Add(new DetectInfo(Session, ent, entInfo, 1, 0)); } } } FinalizeTargetDb(); SubGridDetect(); }