/** public override void UpdateBeforeSimulation10() //Methods for turret targeting * { * IMyEntity target; * Vector3 turretVector; * Vector3 destinationVector; * float angle; * foreach (IMyLargeTurretBase turretBase in _turretTargetDict.Keys) { * if (_turretTargetDict.TryGetValue(turretBase, out target)) { * turretVector = Vector3.Normalize(Vector3.Transform(turretBase.WorldMatrix.Forward, Matrix.CreateFromYawPitchRoll(turretBase.Azimuth, turretBase.Elevation, 0f))); * destinationVector = Vector3.Normalize(target.GetPosition() - turretBase.GetPosition()); * angle = MyUtils.GetAngleBetweenVectors(turretVector, destinationVector); * if (angle < 0.02f) { * List<Sandbox.ModAPI.Interfaces.ITerminalAction> actionList = new List<Sandbox.ModAPI.Interfaces.ITerminalAction>(); * turretBase.GetActions(actionList, null); * Log.Info("actionlist " + actionList); * foreach (var action in actionList) { * Log.Info(action.ToString()); * } * } * } * } * base.UpdateBeforeSimulation10(); * } **/ public override void UpdateAfterSimulation() { var player = MyAPIGateway.Session.Player; if ((player != null) && (player.Controller != null) && (player.Controller.ControlledEntity != null) && (player.Controller.ControlledEntity.Entity != null)) { if (player.Controller.ControlledEntity.Entity.GetTopMostParent().EntityId == Entity.EntityId) { if (MyAPIGateway.Input.IsNewRightMousePressed()) { ModDebugger.Launch(); var view = MyAPIGateway.Session.Camera.WorldMatrix; var view2 = player.Controller.ControlledEntity.GetHeadMatrix(true, true, false); Ray directionRay = new Ray(view2.Translation, Vector3.Normalize(view.Forward)); //var color = new Vector4(0.95f, 0.45f, 0.45f, 0.75f); //MyTransparentGeometry.AddLineBillboard("Firefly", color, directionRay.Position, view.Forward, 5000, 2); IMyEntity bestTarget = GuidedMissileCore.GetClosestTargetAlongRay(directionRay, 5000, 7.5, Entity.GetTopMostParent()); if (bestTarget != null) { SetMissileTarget(bestTarget); var targetName = bestTarget.DisplayName; MyAPIGateway.Utilities.ShowNotification(targetName + " was set as missile target!", 1000, MyFontEnum.Red); } } } } base.UpdateAfterSimulation(); }
protected override void GuideMissiles(HashSet <IMyEntity> missileSet) { // Log.Info("called guidemissiles in flarelauncher"); try { if (missileSet == null) { return; } if (missileSet.Count == 0) { return; } Log.Info("Set wasnt 0 or empty"); ISet <IMyEntity> incomingMissiles = GuidedMissileSingleton.GetMissilesByTargetGrid(Entity.GetTopMostParent()); if (incomingMissiles.Count == 0) { return; } foreach (IMyEntity guidedMissile in incomingMissiles) { foreach (IMyEntity flare in missileSet) { if (!_flareSet.Contains(flare)) { Log.Info("got a flare: "); _flareSet.Add(flare); int randomNumber = GuidedMissileCore.GetSyncedRandom().Next(1, 10); if (randomNumber > (int)Math.Round(DeflectChance * 10)) { GuidedMissileSingleton.SetTargetForMissile(guidedMissile, flare); Log.Info("won dice roll! setting flare target for missile! " + randomNumber); } else { Log.Info("failed dice roll! not deflecting missile! " + randomNumber); } } } } foreach (IMyEntity flare in _flareSet) { if ((flare == null) || (flare.MarkedForClose)) { _deleteSet.Add(flare); } _flareSet.ExceptWith(_deleteSet); _deleteSet.Clear(); } } catch (Exception e) { Log.Info("flares failed to work! caught exception: " + e); } }
public override void UpdateBeforeSimulation() { HashSet <IMyEntity> componentSet = new HashSet <IMyEntity>(); Entity.GetTopMostParent().Hierarchy.GetChildrenRecursive(componentSet); bool underControl = false; IMyPlayer currentPlayer = null; foreach (IMyEntity component in componentSet) { if (component is IMyCockpit) { var cockpit = component as IMyCockpit; if (cockpit.IsUnderControl) { underControl = true; currentPlayer = MyAPIGateway.Players.GetPlayerControllingEntity(cockpit); break; } } } var gun = Entity as IMyUserControllableGun; if (gun != null && ((underControl) && (gun.IsShooting))) { foreach (IMyEntity missile in GetMissilesInBoundingBox()) { Ray ray = new Ray(missile.GetPosition(), Vector3.Normalize(missile.WorldMatrix.Forward)); IMyEntity target = GuidedMissileCore.GetClosestTargetAlongRay(ray, MaxDistance, 7, Entity.GetTopMostParent()); // IMyEntity target = GuidedMissileTargetGridHook.GetRandomBlockInGrid(targetGrid); AssignTarget(target); missile.Close(); if ((currentPlayer == MyAPIGateway.Session.Player) && (target != null)) { MyAPIGateway.Utilities.ShowNotification(target.GetTopMostParent().DisplayName + " was set as missile target!", 2000, MyFontEnum.Red); } break; } } base.UpdateBeforeSimulation(); }
public static IMyEntity GetRandomBlockInGrid(IMyEntity grid) { if (grid == null) { return(null); } // if(target.OwnerId == Entity.OwnerId) return false; //faction check? if (grid.Hierarchy == null) { return(null); } if (grid.Hierarchy.Children == null) { return(null); } HashSet <IMyEntity> componentSet = new HashSet <IMyEntity>(); grid.Hierarchy.GetChildrenRecursive(componentSet); List <IMyEntity> componentList = new List <IMyEntity>(); componentList.AddRange(componentSet); IMyEntity randomTarget = null; // Random r = new Random(); if (componentList.Count == 0) { return(null); } // Log.Info("ComponentList.Count = " + componentList.Count); while (randomTarget == null) { int randInt = GuidedMissileCore.GetSyncedRandom().Next(0, componentList.Count - 1); randomTarget = componentList[randInt]; //Log.Info("Got random block: " + randomTarget.GetType() + " by virtue of random number: " + randInt); } return(randomTarget); }
public void UpdateManually() { // Log.Info("updating manually: turret"); try { bool localPlayerEntered = false; IMyPlayerCollection allPlayers = MyAPIGateway.Players; IMyPlayer currentPlayer = null; List <IMyPlayer> playerList = new List <IMyPlayer>(); allPlayers.GetPlayers(playerList); // Log.Info("update manually:"); // Log.Info("" + allPlayers + currentPlayer + playerList); foreach (IMyPlayer player in playerList) { // Log.Info("iterating over player : " + player); // Log.Info("iterating over player " + player); if ((player != null) && (player.Controller != null) && (player.Controller.ControlledEntity != null) && (player.Controller.ControlledEntity.Entity != null)) { // Log.Info("first if"); // Log.Info("a controlled entity was not zero"); if (player.Controller.ControlledEntity.Entity.EntityId == Entity.EntityId) { // Log.Info("second if"); currentPlayer = player; // Log.Info("currentplayer found"); // Log.Info("set current player"); if (MyAPIGateway.Session.Player == player) { // Log.Info("third if"); //currentPlayer = player; if (localPlayerEntered == false) { localPlayerEntered = true; } } } } } // string pId = ""; bool success = false; string targetName = "NoTargetFound"; if (currentPlayer != null) { // Log.Info("fouth if"); // Log.Info("currentplayer wasnt null"); // if (currentPlayer.Controller.ControlledEntity.Entity != null) pId = currentPlayer.Controller.ControlledEntity.Entity.ToString(); BoundingSphereD sphere = new BoundingSphereD(Entity.GetPosition() + Vector3.Normalize(Entity.WorldMatrix.Up) * 2, 25); // MyAPIGateway.Entities.EnableEntityBoundingBoxDraw(Entity, true, new Vector4(255, 100, 100, 100), 0.01f, null); List <IMyEntity> entitiesFound = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere); foreach (IMyEntity ent in entitiesFound) { // Log.Info("found an entity around me " + ent.GetType().ToString()); // if (ent is Sandbox.Game.Weapons.MyMissile) if (ent.GetType().ToString() == GuidedMissileSingleton.SandboxGameWeaponsMyMissile) //CHECK FOR OWNER OR SOMETHING ;_; { bool isActualMissile = false; if ((Entity.Physics != null) && ((ent.Physics.LinearVelocity - Entity.Physics.LinearVelocity).Length() > 450f)) { Log.Info("physics wasnt null and speed was sufficient"); isActualMissile = true; } else if ((Entity.Physics == null)) { Log.Info("physics was null!"); Log.Info("ent speed was: " + ent.Physics.LinearVelocity.Length()); if (ent.Physics.LinearVelocity.Length() > 450f) { isActualMissile = true; } } if (isActualMissile) { Log.Info("found a missile"); Ray directionRay = new Ray(ent.GetPosition(), Vector3.Normalize(ent.Physics.LinearVelocity)); IMyEntity bestTarget = GuidedMissileCore.GetClosestTargetAlongRay(directionRay, 5000, 7.5, Entity.GetTopMostParent()); // Log.Info("assign target, trying to call with : " + bestTarget); if (bestTarget != null) { success = AssignTarget(bestTarget); targetName = bestTarget.DisplayName; } } ent.Close(); } } } /** if (localPlayerEntered != this.playerEntered) * { * SetPlayerStatus(localPlayerEntered); * } **/ if (localPlayerEntered) { if (success) { MyAPIGateway.Utilities.ShowNotification(targetName + " was set as missile target!", 1000, MyFontEnum.Red); } } } catch (Exception e) { Log.Error(e); MyAPIGateway.Utilities.ShowNotification("" + e, 1000, MyFontEnum.Red); } }
protected override IMyEntity GetTarget(IMyEntity missile) { //IMyEntity targetGrid = GuidedMissileTargetGridHook.GetMissileTargetForGrid(Entity.GetTopMostParent()); Ray ray = new Ray(missile.GetPosition(), Vector3.Normalize(missile.WorldMatrix.Forward)); IMyEntity targetGrid = GuidedMissileCore.GetClosestTargetAlongRay(ray, 3000, 7, Entity.GetTopMostParent()); IMyEntity target = GuidedMissileTargetGridHook.GetRandomBlockInGrid(targetGrid); if (target == null) { // Log.Info("target was null..."); // if (targetGrid == null) Log.Info("targetgrid was null as well!"); target = targetGrid; Log.Info("target was null, now set to grid"); // IMyEntity grid = target; } // Log.Info("got a target: " + target.ToString()); if (target != null) { IMyPlayerCollection allPlayers = MyAPIGateway.Players; List <IMyPlayer> playerList = new List <IMyPlayer>(); HashSet <IMyEntity> componentSet = new HashSet <IMyEntity>(); Entity.GetTopMostParent().Hierarchy.GetChildrenRecursive(componentSet); List <IMyEntity> componentList = new List <IMyEntity>(); componentList.AddRange(componentSet); allPlayers.GetPlayers(playerList); componentSet.Clear(); foreach (IMyEntity component in componentList) { if (component is IMyCockpit) { componentSet.Add(component); } } foreach (IMyPlayer player in playerList) { if (MyAPIGateway.Session.Player == player) { if ((player != null) && (player.Controller != null) && (player.Controller.ControlledEntity != null) && (player.Controller.ControlledEntity.Entity != null)) { foreach (IMyEntity entity in componentSet) { if (player.Controller.ControlledEntity.Entity.EntityId == entity.EntityId) { MyAPIGateway.Utilities.ShowNotification("" + target.GetTopMostParent().DisplayName + " was set as missile target!", 2000, MyFontEnum.Red); } } } } } } return(target); }