Skip to content

Commit

Permalink
Fixed crash bugs - prepwork for new feature (currently disabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
DraygoKorvan committed Aug 31, 2015
1 parent 63fcc49 commit ec6bb5e
Show file tree
Hide file tree
Showing 9 changed files with 665 additions and 112 deletions.
359 changes: 265 additions & 94 deletions Core.cs

Large diffs are not rendered by default.

161 changes: 161 additions & 0 deletions PersonalAsteroid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VRageMath;

using SEModAPIInternal.API.Common;

using Sandbox.Game.World;
using VRage.Utils;

namespace SEDropship
{
[Serializable()]
public class PersonalAsteroid
{
public Vector3I field;
public ulong user;
private Random m_rand;
private static readonly Dictionary<string, int> LargePrefabs
= new Dictionary<string, int>
{

{ "EacPrisonAsteroid", 222 },
{ "hopebase512", 222 },
{ "PirateBaseStaticAsteroid_A_5000m_1", 222 },
{ "PirateBaseStaticAsteroid_A_5000m_2", 222 }

};

private static readonly Dictionary<string, int> SmallPrefabs
= new Dictionary<string, int>
{
{ "AsteroidBase2", 111 },
{ "AsteroidSpaceStation", 111 },
{ "Barths_moon_base", 111 },
{ "barths_moon_camp", 111 },
{ "Bioresearch", 111 },
{ "Chinese_Corridor_Tunnel_256x256x256", 111 },
{ "EngineersOutpost", 111 },
{ "Junkyard_RaceAsteroid_256x256x256", 111 },
{ "Russian_Transmitter_2", 111 },
{ "TorusWithManyTunnels_256x128x256", 111 },
{ "TorusWithSmallTunnel_256x128x256", 111 },
{ "VangelisBase", 111 },
{ "VerticalIslandStorySector_128x256x128", 111 },
{ "JunkYardToxic_128x128x128", 55 },
{ "ScratchedBoulder_128x128x128", 55 },
{ "small_overhang_flat", 55 },
{ "Small_Pirate_Base_Asteroid", 55 }

};

public Vector3D center { get; set; }
public int halfextent { get; set; }
internal PersonalAsteroid()
{

}
public PersonalAsteroid(Vector3I newField, ulong uid)
{
m_rand = new Random((int)DateTime.UtcNow.ToBinary());
user = uid;
this.field = newField;
generateField();
}

private void generateField()
{
int i = 0;
Console.WriteLine("Generating field");
//spawn home asteroid
int pick = m_rand.Next(LargePrefabs.Count);
Vector3D pos = field;
//pos = pos * SEDropshipSettings.FieldSize;//home position
Console.WriteLine("Setting Home Position");
pos.X = (pos.X * SEDropshipSettings.FieldSize) + (m_rand.NextDouble() * 40000 - 20000);
pos.Y = (pos.Y * SEDropshipSettings.FieldSize) + (m_rand.NextDouble() * 40000 - 20000);
pos.Z = (pos.Z * SEDropshipSettings.FieldSize) + (m_rand.NextDouble() * 40000 - 20000);
Console.WriteLine(pos.ToString());
center = pos;
halfextent = LargePrefabs.ElementAt(pick).Value;
pos.X -= halfextent;
pos.Y -= halfextent;
pos.Z -= halfextent;
Console.WriteLine("Spawning home roid");
SandboxGameAssemblyWrapper.Instance.GameAction(() =>
{
try
{
MyWorldGenerator.AddAsteroidPrefab(LargePrefabs.First().Key, pos, string.Format("p-Roid-{0}_{1}", i, user));
}
catch (Exception ex)
{
MyLog.Default.WriteLineAndConsole(ex.ToString());
MyLog.Default.WriteLineAndConsole(ex.StackTrace.ToString());
throw ex;
}
});


//spawn second large asteroid
i++;
var randvector = Vector3.Multiply(Vector3.Normalize(new Vector3D(m_rand.NextDouble() - 0.5, m_rand.NextDouble() - 0.5, m_rand.NextDouble() - 0.5)), 1500);
var newpos = pos + randvector;
pick = m_rand.Next(SmallPrefabs.Count);
string _roid = SmallPrefabs.ElementAt(pick).Key;
int _halfextent = SmallPrefabs[_roid];
newpos.X -= _halfextent;
newpos.Y -= _halfextent;
newpos.Z -= _halfextent;
Console.WriteLine("spawning second roid");
SandboxGameAssemblyWrapper.Instance.GameAction(() =>
{
try
{
MyWorldGenerator.AddAsteroidPrefab(_roid, newpos, string.Format("p-Roid-{0}_{1}", i, user));
}
catch (Exception ex)
{
MyLog.Default.WriteLineAndConsole(ex.ToString());
MyLog.Default.WriteLineAndConsole(ex.StackTrace.ToString());
throw ex;
}
});

//spawn small asteroids
while (i < 5)
{
i++;
randvector = Vector3.Multiply(Vector3.Normalize(new Vector3D(m_rand.NextDouble()-0.5, m_rand.NextDouble() - 0.5, m_rand.NextDouble() - 0.5)), 1500 + i*500);
newpos = pos + randvector;
pick = m_rand.Next(LargePrefabs.Count);
_roid = SmallPrefabs.ElementAt(pick).Key;
_halfextent = SmallPrefabs[_roid];
newpos.X -= _halfextent;
newpos.Y -= _halfextent;
newpos.Z -= _halfextent;
Console.WriteLine("Spawning " + i.ToString() + " + 2 roid.");
SandboxGameAssemblyWrapper.Instance.GameAction(() =>
{
try
{
MyWorldGenerator.AddAsteroidPrefab(_roid, newpos, string.Format("p-Roid-{0}_{1}", i, user));
}
catch (Exception ex)
{
MyLog.Default.WriteLineAndConsole(ex.ToString());
MyLog.Default.WriteLineAndConsole(ex.StackTrace.ToString());
throw ex;
}
});
}



}


}
}
197 changes: 197 additions & 0 deletions PersonalAsteroidClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;

using VRageMath;
using Sandbox;
using Sandbox.ModAPI;

namespace SEDropship
{
[Serializable()]
public class PersonalAsteroidManager
{
[NonSerialized]
private Dictionary<ulong, PersonalAsteroid> m_cache = new Dictionary<ulong, PersonalAsteroid>();
//private List<ulong> m_clients = new List<ulong>();
[NonSerialized]
private long m_pos = 2;

internal PersonalAsteroidManager() { }
public List<PersonalWrapper> m_savedata
{
get; set;
}
public long pos
{
get
{
return m_pos;

}
set
{
m_pos = value;
}
}
internal void load()
{
try
{
m_savedata = new List<PersonalWrapper>();
if (File.Exists(Location + "SE-Personal-Asteroid.xml"))
{

XmlSerializer x = new XmlSerializer(typeof(PersonalAsteroidManager));
TextReader reader = new StreamReader(Location + "SE-Personal-Asteroid.xml");
PersonalAsteroidManager obj = (PersonalAsteroidManager)x.Deserialize(reader);
pos = obj.pos;
m_savedata = obj.m_savedata;
reader.Close();
sync();

return;
}

}
catch (Exception)
{
//do nothin
Console.WriteLine("Exception thrown while loading.");
}
}

private void sync()
{
foreach( var wrapper in m_savedata)
{
m_cache.Add(wrapper.id, wrapper.roid);
}
}

internal void loadClient(ulong obj)
{

//save();
}

private void save()
{
//save settings and trigger world save
XmlSerializer x = new XmlSerializer(typeof(PersonalAsteroidManager));
TextWriter writer = new StreamWriter(Location + "SE-Personal-Asteroid.xml");
x.Serialize(writer, this);
writer.Close();
Console.WriteLine("Saving");
MyAPIGateway.Session.Save();
}



public Vector3I getNewFieldLocation()
{
m_pos++;

Vector3I val = Vector3I.Zero;
val.Z = (int)(m_pos % 3)-1;
long div = m_pos / 3;
long edge = (long)Math.Floor(Math.Sqrt(div));
if ((edge % 2) == 0)
{
edge--;
}
edge = edge / 2;
edge++;

//now we have our edge
val.X = (int)-edge;
val.Y = (int)-edge;

int pos = (int)(div - ((edge*2-1) * (edge * 2 - 1)));


switch (pos % 4)
{
case 0:
val.X += pos / 4;
val.Y *= -1;
break;
case 1:
val.X *= -1;
val.Y *= -1;
val.Y -= pos / 4;
break;
case 2:
val.X *= -1;
val.X -= pos / 4;
val.Y += 0;
break;
case 3:
val.X += 0;
val.Y += pos / 4 ;
break;
}

return val;
}

internal Vector3D targetpos(ulong steamid)
{
if (!m_cache.ContainsKey(steamid))
{
createEntry(steamid);
}
return m_cache[steamid].center;
}

private void createEntry(ulong steamid)
{
Console.WriteLine("Creating roid");
var roid = new PersonalAsteroid(getNewFieldLocation(), steamid);
Console.WriteLine("Adding to m_cache");
m_cache.Add(steamid, roid);
Console.WriteLine("Adding to savedata");
m_savedata.Add(new PersonalWrapper(steamid, roid));
Console.WriteLine("Initiating save");
save();
}

internal int halfextent(ulong steamid)
{
if (!m_cache.ContainsKey(steamid))
{
createEntry(steamid);
save();
}
return m_cache[steamid].halfextent;
}

public string DefaultLocation
{
get { return System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\"; }

}

public string Location
{
get { return MySandboxGame.ConfigDedicated.LoadWorld + "\\"; }
}


}
[Serializable()]
public class PersonalWrapper
{
public PersonalWrapper(ulong steamid, PersonalAsteroid roid)
{
id = steamid;
this.roid = roid;
}
public PersonalWrapper() { }

public ulong id { get; set; }
public PersonalAsteroid roid { get; set; }
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("0.5.4.4")]
[assembly: AssemblyFileVersion("0.5.4.4")]
[assembly: AssemblyVersion("0.5.5.0")]
[assembly: AssemblyFileVersion("0.5.5.0")]
6 changes: 6 additions & 0 deletions SE-Dropship.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Compile Include="PersonalAsteroid.cs" />
<Compile Include="PersonalAsteroidClass.cs" />
<Compile Include="Core.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SeDropshipAsteroids.cs" />
Expand Down Expand Up @@ -84,6 +86,10 @@
<Reference Include="SpaceEngineersDedicated">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\SpaceEngineersDedicated.exe</HintPath>
</Reference>
<Reference Include="SteamSDK, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\SteamSDK.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.XML" />
<Reference Include="VRage, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
5 changes: 2 additions & 3 deletions SE-Dropship.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SE-Dropship", "SE-Dropship.csproj", "{DBB92823-BD91-42CA-ADC5-5ABED872EF13}"
EndProject
Expand Down
Binary file modified SE-Dropship.v12.suo
Binary file not shown.
Loading

0 comments on commit ec6bb5e

Please sign in to comment.