Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 6fd4a25

Browse files
committed
Allow cargo ships to spawn in gravity. Command /admin spawn cargo atmosphere triggers this. /admin spawn cargo triggers a cargo ship event in the base game.
1 parent 1ad0e27 commit 6fd4a25

File tree

9 files changed

+97
-109
lines changed

9 files changed

+97
-109
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//75
1+
//83
22
//
33
// This code was generated by a tool. Any changes made manually will be lost
44
// the next time this code is regenerated.
55
//
66

77
using System.Reflection;
88

9-
[assembly: AssemblyFileVersion("1.13.6.75")]
10-
[assembly: AssemblyVersion("1.13.6.75")]
9+
[assembly: AssemblyFileVersion("1.13.6.83")]
10+
[assembly: AssemblyVersion("1.13.6.83")]

EssentialsPlugin/ChatHandlers/Admin/HandleAdminSpawnCargo.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
namespace EssentialsPlugin.ChatHandlers.Admin
22
{
3+
using System;
4+
using Sandbox.Game.World;
35
using Utility;
6+
using VRage.Game;
7+
48
public class HandleAdminSpawnCargo : ChatHandlerBase
59
{
610

@@ -36,7 +40,24 @@ public override bool AllowedInConsole()
3640

3741
public override bool HandleCommand( ulong userId, string[ ] words )
3842
{
39-
CargoShips.SpawnCargoShip( );
43+
if ( words.Length == 1 && words[0].ToLower( ) == "atmosphere" )
44+
{
45+
CargoShips.SpawnCargoShip( false );
46+
return true;
47+
}
48+
49+
var cargoShipEvent = MyGlobalEvents.GetEventById(new MyDefinitionId(typeof(MyObjectBuilder_GlobalEventBase), "SpawnCargoShip"));
50+
if (cargoShipEvent == null )
51+
{
52+
//we can't force the game to spawn a ship if the option is off, so use our own method
53+
CargoShips.SpawnCargoShip( true );
54+
}
55+
else
56+
{
57+
MyGlobalEvents.RemoveGlobalEvent(cargoShipEvent);
58+
cargoShipEvent.SetActivationTime(TimeSpan.Zero);
59+
MyGlobalEvents.AddGlobalEvent(cargoShipEvent);
60+
}
4061
return true;
4162
}
4263

EssentialsPlugin/Essentials.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,28 +1105,28 @@ public MTObservableCollection<TimedCommandItem> TimedCommandsItems
11051105
}
11061106

11071107
[Category( "Cargo Ships" )]
1108-
[Description( "This enables or disables random cargo ships." )]
1108+
[Description( "This enables or disables cargo ships spawning in gravity." )]
11091109
[Browsable( true )]
11101110
[ReadOnly( false )]
1111-
public bool CargoShipsEnabled {
1111+
public bool AtmosphericCargoShipsEnabled {
11121112
get {
1113-
return PluginSettings.Instance.CargoShipsEnabled;
1113+
return PluginSettings.Instance.AtmosphericCargoShipsEnabled;
11141114
}
11151115
set {
1116-
PluginSettings.Instance.CargoShipsEnabled = value;
1116+
PluginSettings.Instance.AtmosphericCargoShipsEnabled = value;
11171117
}
11181118
}
11191119

11201120
[Category( "Cargo Ships" )]
11211121
[Description( "The amount of time, in minutes, between ship spawn." )]
11221122
[Browsable( true )]
11231123
[ReadOnly( false )]
1124-
public float CargoShipSpawnTime {
1124+
public float AtmosphericCargoShipSpawnTime {
11251125
get {
1126-
return PluginSettings.Instance.CargoShipSpawnTime;
1126+
return PluginSettings.Instance.AtmosphericCargoShipSpawnTime;
11271127
}
11281128
set {
1129-
PluginSettings.Instance.CargoShipSpawnTime = value;
1129+
PluginSettings.Instance.AtmosphericCargoShipSpawnTime = value;
11301130
}
11311131
}
11321132
/*
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
namespace EssentialsPlugin.ProcessHandlers
22
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Diagnostics;
6-
using System.IO;
7-
using System.Net;
8-
using System.Threading;
9-
using System.Windows.Forms;
10-
using EssentialsPlugin.Settings;
11-
using EssentialsPlugin.Utility;
12-
using Sandbox;
13-
using Sandbox.ModAPI;
14-
using SEModAPIExtensions.API;
15-
using SEModAPIInternal.API.Common;
16-
using SteamSDK;
17-
using VRage.Game.ModAPI;
18-
using VRage.ModAPI;
19-
using VRageMath;
3+
using EssentialsPlugin.Utility;
204

21-
class ProcessCargoShips : ProcessHandlerBase
5+
class ProcessCargoShips : ProcessHandlerBase
226
{
237
public override int GetUpdateResolution()
248
{
25-
return (int)(PluginSettings.Instance.CargoShipSpawnTime * 60000);
9+
return (int)(PluginSettings.Instance.AtmosphericCargoShipSpawnTime * 60000);
2610
}
2711

2812
public override void Handle()
2913
{
30-
if(!PluginSettings.Instance.CargoShipsEnabled)
14+
if(!PluginSettings.Instance.AtmosphericCargoShipsEnabled)
3115
return;
3216

33-
CargoShips.SpawnCargoShip( );
17+
CargoShips.SpawnCargoShip( false );
3418
}
3519
}
3620
}

EssentialsPlugin/Settings/PluginSettings.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public class PluginSettings
121121
private bool _timedCommandsEnabled;
122122
private MTObservableCollection<TimedCommandItem> _TimedCommandsItem;
123123

124-
private bool _cargoShipsEnabled;
125-
private float _cargoShipSpawnTime;
124+
private bool _atmosphericCargoShipsEnabled;
125+
private float _atmosphericCargoShipSpawnTime;
126126

127127
private List<TicketPlayerItem> _ticketPlayers;
128128

@@ -970,22 +970,22 @@ public MTObservableCollection<TimedCommandItem> TimedCommandsItems
970970
}
971971
}
972972

973-
public bool CargoShipsEnabled
973+
public bool AtmosphericCargoShipsEnabled
974974
{
975-
get { return _cargoShipsEnabled; }
975+
get { return _atmosphericCargoShipsEnabled; }
976976
set
977977
{
978-
_cargoShipsEnabled = value;
978+
_atmosphericCargoShipsEnabled = value;
979979
Save( );
980980
}
981981
}
982982

983-
public float CargoShipSpawnTime
983+
public float AtmosphericCargoShipSpawnTime
984984
{
985-
get { return _cargoShipSpawnTime; }
985+
get { return _atmosphericCargoShipSpawnTime; }
986986
set
987987
{
988-
_cargoShipSpawnTime = value;
988+
_atmosphericCargoShipSpawnTime = value;
989989
Save( );
990990
}
991991
}
@@ -1075,8 +1075,8 @@ public PluginSettings()
10751075
_TimedCommandsItem = new MTObservableCollection<TimedCommandItem>( );
10761076
_TimedCommandsItem.CollectionChanged += ItemsCollectionChanged;
10771077

1078-
_cargoShipsEnabled = false;
1079-
_cargoShipSpawnTime = 10.0f;
1078+
_atmosphericCargoShipsEnabled = false;
1079+
_atmosphericCargoShipSpawnTime = 10.0f;
10801080

10811081
_ticketPlayers = new List<TicketPlayerItem>();
10821082
}

EssentialsPlugin/Utility/CargoShips.cs

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -70,58 +70,8 @@ private static void Init( )
7070
m_spawnGroupCumulativeFrequencies.Add(m_spawnGroupTotalFrequencies);
7171
}
7272
}
73-
74-
public static bool DoesTrajectoryIntersect( Vector3D start, Vector3D end, float spawnRadius )
75-
{
76-
Ray trajectory = new Ray( start, (end - start) );
77-
return DoesTrajectoryIntersect( trajectory, spawnRadius );
78-
}
79-
80-
public static bool DoesTrajectoryIntersect( Ray trajectory, float spawnRadius )
81-
{
82-
if ( ExtenderOptions.IsDebugging )
83-
Essentials.Log.Info( "Checking gravity intersect" );
84-
85-
foreach ( IMyGravityProvider provider in MyGravityProviderSystem.NaturalGravityProviders )
86-
{
87-
MyPlanet planet = provider as MyPlanet;
88-
if ( planet == null )
89-
continue;
90-
BoundingSphereD gravitySphere = new BoundingSphereD( planet.PositionComp.GetPosition( ), planet.GravityLimit + spawnRadius);
91-
92-
float? intersect = trajectory.Intersects( gravitySphere );
93-
if ( intersect.HasValue && intersect.Value > 0 )
94-
return true;
95-
}
96-
/*
97-
foreach ( BoundingSphereD sphere in gravitySphereList )
98-
{
99-
if ( trajectory.Intersects( sphere ).HasValue )
100-
return true;
101-
}
102-
*/
103-
return false;
104-
}
105-
106-
public static bool IsPointInGravity( Vector3D testPoint )
107-
{
108-
if ( ExtenderOptions.IsDebugging )
109-
Essentials.Log.Info( "Checking point gravity intersect" );
110-
111-
return !Vector3D.IsZero( MyGravityProviderSystem.CalculateNaturalGravityInPoint( testPoint, true ) );
112-
113-
/*
114-
foreach ( BoundingSphereD sphere in gravitySphereList )
115-
{
116-
if ( sphere.Contains( testPoint ) == ContainmentType.Contains)
117-
return true;
118-
}
119-
120-
return false;
121-
*/
122-
}
12373

124-
public static void SpawnCargoShip()
74+
public static void SpawnCargoShip( bool checkGravity )
12575
{
12676
Init( );
12777
Wrapper.GameAction( ( ) =>
@@ -245,22 +195,26 @@ public static void SpawnCargoShip()
245195
Vector3D shipDestination = shipPosition + directionMult;
246196
float radius = prefabDef == null ? 10.0f : prefabDef.BoundingSphere.Radius;
247197

248-
if ( IsPointInGravity( shipPosition ) )
249-
{
250-
if(ExtenderOptions.IsDebugging)
251-
Essentials.Log.Info( "Failed to spawn cargo ship: Spawn location is in gravity well" );
252-
return;
253-
}
254-
if ( IsPointInGravity( shipDestination ) )
198+
if ( checkGravity )
255199
{
256-
if(ExtenderOptions.IsDebugging)
257-
Essentials.Log.Info( "Failed to spawn cargo ship: Destination is in gravity well" );
258-
}
259-
if ( DoesTrajectoryIntersect( shipPosition, shipDestination, spawnGroup.SpawnRadius ) )
260-
{
261-
if ( ExtenderOptions.IsDebugging )
262-
Essentials.Log.Info( "Failed to spawn cargo ship: Ship path intersects gravity well" );
263-
return;
200+
//these point checks could be done in the trajectory intersect, but checking points is faster than ray intersect
201+
if ( MyGravityProviderSystem.IsPositionInNaturalGravity( shipPosition, spawnGroup.SpawnRadius ) )
202+
{
203+
if ( ExtenderOptions.IsDebugging )
204+
Essentials.Log.Info( "Failed to spawn cargo ship: Spawn location is in gravity well" );
205+
return;
206+
}
207+
if ( MyGravityProviderSystem.IsPositionInNaturalGravity( shipDestination, spawnGroup.SpawnRadius ) )
208+
{
209+
if ( ExtenderOptions.IsDebugging )
210+
Essentials.Log.Info( "Failed to spawn cargo ship: Destination is in gravity well" );
211+
}
212+
if ( MyGravityProviderSystem.DoesTrajectoryIntersectNaturalGravity( shipPosition, shipDestination, spawnGroup.SpawnRadius + 500 ))
213+
{
214+
if ( ExtenderOptions.IsDebugging )
215+
Essentials.Log.Info( "Failed to spawn cargo ship: Ship path intersects gravity well" );
216+
return;
217+
}
264218
}
265219

266220
MyPhysics.CastRay( shipPosition, shipDestination, m_raycastHits, MyPhysics.CollisionLayers.ObjectDetectionCollisionLayer );

EssentialsPlugin/Utility/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class Extensions
1111
{
1212
public static void Stop(this IMyEntity entity)
1313
{
14-
if (entity?.Physics == null)
14+
if (entity?.Physics == null || entity.Closed)
1515
return;
1616

1717
Wrapper.GameAction(() =>

EssentialsPlugin/Utility/GridGroup.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Sandbox.Game.Entities;
1010
using Sandbox.Game.Entities.Cube;
1111
using VRage.Game.Entity;
12+
using VRageMath;
1213

1314
public class GridGroup
1415
{
@@ -148,6 +149,14 @@ public void Close( )
148149
} );
149150
}
150151

152+
public void Stop( )
153+
{
154+
foreach ( MyCubeGrid grid in _grids )
155+
{
156+
Wrapper.GameAction( ( ) => grid.Stop( ) );
157+
}
158+
}
159+
151160
private void GetParent( )
152161
{
153162
if ( _grids.Count < 1 )

EssentialsPlugin/Utility/MathUtility.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,27 @@ public static float GenerateRandomCoord( int maxDist, int minDist )
8888
{
8989
float result = (m_random.Next( minDist, maxDist )) * (m_random.Next( 2 ) == 0 ? -1 : 1);
9090
return result;
91+
}
92+
93+
public static Vector3D? TraceVector(Vector3D position, Vector3D velocity, int distance, int radius = 100)
94+
{
95+
Vector3D normVelocity = Vector3D.Normalize(velocity);
96+
Vector3D result = normVelocity * distance + position;
9197

98+
//make sure the point is clear
99+
int trycount = 1;
100+
BoundingSphereD checkSphere = new BoundingSphereD(result, radius);
101+
while (MyAPIGateway.Entities.GetIntersectionWithSphere(ref checkSphere) != null)
102+
{
103+
//try to find a location 20 times, increasing distance from start each try
104+
trycount++;
105+
result = normVelocity * (distance * trycount) + position;
106+
checkSphere = new BoundingSphereD(result, radius);
107+
108+
if (trycount > 20)
109+
return null;
110+
}
111+
return result;
92112
}
93113
}
94114
}

0 commit comments

Comments
 (0)