Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Collections.Specialized;
- using Sandbox.Game;
- using Sandbox.Game.Entities;
- using Sandbox.ModAPI;
- using VRage.Game;
- using VRage.Game.Components;
- using VRage.Game.ModAPI;
- using VRageMath;
- using VRage.Utils;
- using VRage.ModAPI;
- using VRage.ObjectBuilders;
- using VRage.Game.Entity;
- /* #----------------------------------------------------------------------------#
- * | Zebedee Copyright (C) 2021 Colin ELcock |
- * #----------------------------------------------------------------------------#
- * | This mod is designed to fix the rover falling through the planet issue. |
- * | We take the planet check if there is a rover inside it |
- * | and then place the rover back on the planet surface. |
- * | |
- * | Initialy designed for use on the Sigma Draconis Impossible Server |
- * #----------------------------------------------------------------------------#
- * | Author: Idiotonastic |
- * | Contact: idiotonastic@hotmail.com |
- * | License: GNU GENERAL PUBLIC LICENSE |
- * #----------------------------------------------------------------------------#
- */
- namespace Zebedee
- {
- [MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)]
- public class Zebedee : MySessionComponentBase
- {
- private int timer = 0;
- private List<MyEntity> all_dynamic_entitys = new List<MyEntity>();
- private readonly List<BoundingSphereD> spheres = new List<BoundingSphereD>();
- private readonly Boolean debug = false;
- private List<MyPlanet> mplan = new List<MyPlanet>();
- private List<IMySlimBlock> blocks = new List<IMySlimBlock>();
- /*
- * Function | Init
- * runs on initiliazation, overidden to include OnEntityAdd
- */
- public override void Init(MyObjectBuilder_SessionComponent sessionComponent)
- {
- MyAPIGateway.Entities.OnEntityAdd += OnEntityAdd;
- }
- /*
- * Function | LoadData
- * runs on Data Load, overidden to include OnEntityAdd
- */
- public override void LoadData()
- {
- MyAPIGateway.Entities.OnEntityAdd += OnEntityAdd;
- base.LoadData();
- }
- /*
- * Function | OnEntityAdd
- * gets entity checks if it's a planet and adds it to list of planets: mplan
- * calls addSphere
- */
- private void OnEntityAdd(IMyEntity obj)
- {
- MyPlanet check_planet = obj as MyPlanet;
- if (check_planet != null)
- {
- if (!mplan.Contains(check_planet))
- {
- mplan.Add(check_planet);
- addSphere(check_planet);
- }
- }
- }
- /*
- * Function | addSphere
- * checks is sphere exists, if not adds it to list: spheres
- */
- private void addSphere(MyPlanet P)
- {
- BoundingSphereD Nsphere = new BoundingSphereD
- {
- Radius = P.MinimumRadius - 500,
- Center = P.WorldMatrix.Translation
- };
- if (!spheres.Contains(Nsphere))
- {
- spheres.Add(Nsphere);
- }
- if (debug)
- {
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "Planets Loaded: " + mplan.Count);
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "Spheres Loaded: " + spheres.Count);
- }
- }
- private static Vector3D GetOrientationToPlanet(ref Vector3D lPos, ref Vector3 gravVect)
- {
- if (Vector3.IsZero(gravVect))
- {
- gravVect = Vector3.Up;
- }
- Vector3D value = Vector3D.Normalize(gravVect);
- Vector3D value2 = -value;
- Vector3D result = lPos + value2 * 1;
- return result;
- }
- public override void UpdateAfterSimulation()
- {
- try
- {
- if (MyAPIGateway.Session == null || !MyAPIGateway.Session.IsServer)
- {
- MyLog.Default.WriteLine("Invalid Session"); //if invalid session stop working
- return;
- }
- if (timer % 120 == 0)
- {
- foreach (BoundingSphereD BdnS in spheres) //itterate through spheres
- {
- BoundingSphereD Current = BdnS;
- all_dynamic_entitys.Clear(); //puge previous entity data
- MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref Current, all_dynamic_entitys, MyEntityQueryType.Dynamic); //get all dynamic entities within current sphere
- foreach (MyEntity ent in all_dynamic_entitys)
- {
- if (ent != null)
- {
- if (ent is IMyFloatingObject) //if the entity is floating object delete
- {
- ent.Close();
- if (debug)
- {
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "floatObj ");
- }
- }
- else
- if(ent is IMyCharacter) //if the entity is a player put on surface
- {
- if (debug)
- {
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "player");
- }
- var check_character = ent as IMyCharacter;
- MyPlanet NearbyPlanet = null;
- NearbyPlanet = MyGamePruningStructure.GetClosestPlanet(check_character.WorldAABB.Center); //get rearby planet
- if (NearbyPlanet != null)
- {
- check_character.SetPosition(NearbyPlanet.GetClosestSurfacePointGlobal(check_character.WorldAABB.Center) + (-1 * Vector3D.Normalize(check_character.Physics.Gravity)) * 3); //place about 3m above planet using gr\avity to detrimine dist above
- }
- }
- else
- if (ent is IMyCubeGrid) //if the entity is a grid put on surface
- {
- if (debug)
- {
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "rover");
- }
- IMyCubeGrid roverGrid = ent as IMyCubeGrid;
- MyPlanet NearbyPlanet = null;
- NearbyPlanet = MyGamePruningStructure.GetClosestPlanet(roverGrid.WorldMatrix.Translation);
- roverGrid.GetBlocks(blocks);
- BoundingSphere roversphere = BoundingSphere.CreateFromBoundingBox((BoundingBox)roverGrid.WorldAABB);
- if (blocks != null && blocks.Count >= 10) {
- if (NearbyPlanet != null)
- {
- roverGrid.Physics.ClearSpeed();
- roverGrid.GetBlocks(blocks);
- Vector3D pos = (NearbyPlanet.GetClosestSurfacePointGlobal(roverGrid.WorldAABB.Center) + (-1 * Vector3D.Normalize(roverGrid.Physics.Gravity)) * (roversphere.Radius + 10));
- Vector3 grav = roverGrid.Physics.Gravity;
- Vector3 up;
- up = GetOrientationToPlanet(ref pos, ref grav);
- MatrixD m = new MatrixD
- {
- Translation = pos,
- Up = up
- };
- roverGrid.Teleport(m); //place about 40m above planet using gravity to detrimine dist above
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "Boing!");
- }
- else
- {
- if (debug)
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "No Planet");
- }
- }
- }
- else
- {
- if (debug)
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "No Grid");
- }
- }
- else
- {
- if (debug)
- MyAPIGateway.Utilities.ShowMessage("Zebedee", "No Rover");
- }
- }
- }
- }
- timer += 1;
- }
- catch (Exception e)
- {
- MyLog.Default.WriteLine("BOING: " + e);
- }
- }
- protected override void UnloadData()
- {
- }
- }
- }
- /*
- This mod has bee designed by Colin Elcock to fix the rover falling through the planet issue.
- Copyright (C) 2021 Colin Elcock
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
Add Comment
Please, Sign In to add comment