/**    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();
        }
예제 #2
0
        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();
        }
예제 #3
0
        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);
        }