Skip to content

Commit

Permalink
-Finished main implementation of warp engine
Browse files Browse the repository at this point in the history
  • Loading branch information
chessmaster42 committed Jul 6, 2014
1 parent dc6d7f3 commit e029657
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 4 deletions.
Binary file modified Libraries/SEModAPI.dll
Binary file not shown.
Binary file modified Libraries/SEModAPIExtensions.dll
Binary file not shown.
Binary file modified Libraries/SEModAPIInternal.dll
Binary file not shown.
Binary file added Libraries/VRage.Common.dll
Binary file not shown.
90 changes: 87 additions & 3 deletions WarpDrivePlugin/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@
using System.Linq;
using System.Text;

using Sandbox.Common.ObjectBuilders;

using SEModAPIExtensions.API.Plugin;
using SEModAPIExtensions.API.Plugin.Events;

using SEModAPIInternal.API.Common;
using SEModAPIInternal.API.Entity;
using SEModAPIInternal.API.Entity.Sector.SectorObject;
using SEModAPIInternal.API.Entity.Sector.SectorObject.CubeGrid;
using SEModAPIInternal.API.Entity.Sector.SectorObject.CubeGrid.CubeBlock;

using VRageMath;

namespace SEModAPIExtensions.API.Plugin
namespace WarpDrivePlugin
{
public class Core : PluginBase
public class Core : PluginBase, ICubeGridHandler, IBaseEntityHandler
{
#region "Attributes"

private DateTime m_lastMovementTime;
private TimeSpan m_timeSinceLastMovement;

#endregion

#region "Constructors and Initializers"

public Core()
{
m_timeSinceLastMovement = new TimeSpan();

Console.WriteLine("WarpDrivePlugin '" + Id.ToString() + "' constructed!");
}

Expand All @@ -29,7 +47,73 @@ public override void Init()

public override void Update()
{
//Console.WriteLine("WarpDrivePlugin '" + Id.ToString() + "' updated!");
}

public void OnCubeGridMoved(CubeGridEntity cubeGrid)
{
}

public void OnCubeGridCreated(CubeGridEntity cubeGrid)
{
}

public void OnCubeGridDeleted(CubeGridEntity cubeGrid)
{
}

public void OnBaseEntityMoved(BaseEntity entity)
{
if (entity.GetType() == typeof(CubeGridEntity))
{
CubeGridEntity cubeGrid = (CubeGridEntity)entity;

Vector3 velocity = cubeGrid.LinearVelocity;
float speed = velocity.Length();

if (speed > 1)
{
if (speed > 100)
{
//if(SandboxGameAssemblyWrapper.IsDebugging)
//Console.WriteLine("WarpDrivePlugin - Ship '" + cubeGrid.Name + "' is moving very fast!");

m_timeSinceLastMovement = DateTime.Now - m_lastMovementTime;

List<CubeBlockEntity> cubeBlocks = cubeGrid.CubeBlocks;
if (cubeGrid.GridSizeEnum == MyCubeSize.Large)
{
//Search for a warp reactor on the ship
ReactorEntity warpReactor = null;
foreach (CubeBlockEntity cubeBlock in cubeBlocks)
{
if (cubeBlock.GetType() == typeof(ReactorEntity))
{
ReactorEntity reactor = (ReactorEntity)cubeBlock;
if (reactor.Name == "WarpEngine")
{
warpReactor = reactor;
break;
}
}
}

//If we found a warp reactor then run the warp procedure
if (warpReactor != null)
{
WarpEngine engine = new WarpEngine(warpReactor);
bool result = engine.Warp();
}
}
}
else
{
//if (SandboxGameAssemblyWrapper.IsDebugging)
//Console.WriteLine("WarpDrivePlugin - Ship '" + cubeGrid.Name + "' is moving!");
}
}
}

m_lastMovementTime = DateTime.Now;
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion WarpDrivePlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b0933c38-1234-4d18-b1dc-ef5a054a6ac2")]
[assembly: Guid("b0933c38-1234-5678-b1dc-ef5a054a6ac2")]

// Version information for an assembly consists of the following four values:
//
Expand Down
9 changes: 9 additions & 0 deletions WarpDrivePlugin/WarpDrivePlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Sandbox.Common">
<HintPath>..\Libraries\Sandbox.Common.dll</HintPath>
</Reference>
<Reference Include="Sandbox.Common.XmlSerializers">
<HintPath>..\Libraries\Sandbox.Common.XmlSerializers.dll</HintPath>
</Reference>
Expand All @@ -58,6 +61,12 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.XML" />
<Reference Include="VRage.Common">
<HintPath>..\Libraries\VRage.Common.dll</HintPath>
</Reference>
<Reference Include="VRage.Math">
<HintPath>..\Libraries\VRage.Math.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
98 changes: 98 additions & 0 deletions WarpDrivePlugin/WarpEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,108 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

using SEModAPIInternal.API.Entity;
using SEModAPIInternal.API.Entity.Sector.SectorObject.CubeGrid.CubeBlock;
using SEModAPIInternal.Support;

using VRageMath;

namespace WarpDrivePlugin
{
public class WarpEngine
{
#region "Attributes"

private float m_warpFuelRequired;
private ReactorEntity m_linkedReactor;
private Timer m_stopWarpTimer;

#endregion

#region "Constructors and Initializers"

public WarpEngine(ReactorEntity reactor)
{
m_linkedReactor = reactor;
m_warpFuelRequired = 100;
}

#endregion

#region "Properties"
#endregion

#region "Methods"

public bool Warp()
{
try
{
if (m_linkedReactor.Fuel < m_warpFuelRequired) //Not enough fuel
return false;
if (m_linkedReactor.Enabled == false) //Reactor is off
return false;
if (m_linkedReactor.Parent.MaxLinearVelocity > 150) //Already warping
return false;

Console.WriteLine("WarpDrivePlugin - Ship '" + m_linkedReactor.Parent.Name + "' is warping!");

//Consume the fuel
float fuelRequired = m_warpFuelRequired;
float totalFuelRemoved = 0;
List<InventoryItemEntity> fuelItems = m_linkedReactor.Inventory.Items;
foreach (InventoryItemEntity fuelItem in fuelItems)
{
if (fuelItem.TotalMass > fuelRequired)
{
fuelItem.Amount -= fuelRequired;
totalFuelRemoved = fuelRequired;
}
else
{
totalFuelRemoved += fuelItem.TotalMass;
fuelItem.Amount = 0;
}

if (totalFuelRemoved >= fuelRequired)
break;
}

//Turn off the reactor
m_linkedReactor.Enabled = false;

//Set the ship's velocity to the 100x speed
m_linkedReactor.Parent.MaxLinearVelocity = 10000;
m_linkedReactor.Parent.LinearVelocity = Vector3.Multiply(m_linkedReactor.Parent.LinearVelocity, new Vector3(100, 100, 100));

//Start the timer to stop warp
m_stopWarpTimer = new Timer();
m_stopWarpTimer.Interval = 2000;
m_stopWarpTimer.Elapsed += StopWarp;
m_stopWarpTimer.Start();

return true;
}
catch (Exception ex)
{
LogManager.APILog.WriteLineAndConsole("Error while going to warp");
LogManager.GameLog.WriteLine(ex);
return false;
}
}

public void StopWarp(Object source, ElapsedEventArgs e)
{
m_linkedReactor.Parent.LinearVelocity = Vector3.Divide(m_linkedReactor.Parent.LinearVelocity, new Vector3(100, 100, 100));
m_linkedReactor.Parent.MaxLinearVelocity = (float)104.7;

m_stopWarpTimer.Stop();

Console.WriteLine("WarpDrivePlugin - Ship '" + m_linkedReactor.Parent.Name + "' just left warp and is normal again");
}

#endregion
}
}

0 comments on commit e029657

Please sign in to comment.