Skip to content

Commit

Permalink
Changed asteroid handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwin99 committed Sep 19, 2019
1 parent c60dd85 commit d89c10b
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Sandbox.Game.World.Generator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Game;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using VRage.Game;
using VRage.Noise;
using VRage.Utils;
Expand All @@ -21,7 +20,7 @@
*/
namespace SEWorldGenPlugin.Generator.Asteroids
{
[MyStorageDataProvider(10004)]
[MyStorageDataProvider(10003)]
internal sealed class MyCompositeShapeProvider : IMyStorageDataProvider
{
public class MyCombinedCompositeInfoProvider : MyProceduralCompositeInfoProvider, IMyCompositionInfoProvider
Expand Down
98 changes: 50 additions & 48 deletions SEWorldGenPlugin/Generator/Asteroids/MyCompositeShapes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Sandbox.Definitions;
using Sandbox.Engine.Voxels;
using Sandbox.Game.World;
using Sandbox.Game.World.Generator;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -48,29 +49,27 @@ static MyCompositeShapes()
3,
4
};
AsteroidGenerators = (from x in source
select MyTuple.Create(x, arg2: false)).Concat(from x in source2
select MyTuple.Create(x, arg2: true)).Select((Func<MyTuple<int, bool>, MyCompositeShapeGeneratorDelegate>)delegate (MyTuple<int, bool> info)
{
int version = info.Item1;
bool combined = info.Item2;
return delegate (int generatorSeed, int seed, float size)
{
if (size == 0f)
{
size = MyUtils.GetRandomFloat(128f, 512f);
}
MyCompositeShapes myCompositeShapes = new MyCompositeShapes(generatorSeed, seed, version);
using (MyRandom.Instance.PushSeed(seed))
{
if (combined)
{
return myCompositeShapes.CombinedGenerator(version, seed, size);
}
return myCompositeShapes.ProceduralGenerator(version, seed, size);
}
};
}).ToArray();
AsteroidGenerators = (from x in source select MyTuple.Create(x, arg2: false)).Concat(from x in source2 select MyTuple.Create(x, arg2: true)).Select((Func<MyTuple<int, bool>, MyCompositeShapeGeneratorDelegate>)delegate (MyTuple<int, bool> info)
{
int version = info.Item1;
bool combined = info.Item2;
return delegate (int generatorSeed, int seed, float size)
{
if (size == 0f)
{
size = MyUtils.GetRandomFloat(128f, 512f);
}
MyCompositeShapes myCompositeShapes = new MyCompositeShapes(generatorSeed, seed, version);
using (MyRandom.Instance.PushSeed(seed))
{
if (combined)
{
return myCompositeShapes.CombinedGenerator(version, seed, size);
}
return myCompositeShapes.ProceduralGenerator(version, seed, size);
}
};
}).ToArray();
}

private MyCompositeShapes(int generatorSeed, int asteroidSeed, int version)
Expand Down Expand Up @@ -120,7 +119,7 @@ private IMyCompositionInfoProvider CombinedGenerator(int version, int seed, floa
data.Deposits = Array.Empty<MyCompositeShapeOreDeposit>();
data.FilledShapes = new MyCsgShapeBase[num];
IMyCompositeShape[] array = new IMyCompositeShape[6];
FillSpan(instance, size, array.Span(0, 1), MyDefinitionManager.Static.GetVoxelMapStorageDefinitionsForProceduralPrimaryAdditions(), prefferOnlyBestFittingSize: true);
FillSpan(instance, size, new Span<IMyCompositeShape>(array, 0, 1), MyDefinitionManager.Static.GetVoxelMapStorageDefinitionsForProceduralPrimaryAdditions(), prefferOnlyBestFittingSize: true);
size = ((MyOctreeStorage)array[0]).Size.AbsMax();
float idealSize = size / 2f;
float idealSize2 = size / 2f;
Expand All @@ -136,7 +135,7 @@ private IMyCompositionInfoProvider CombinedGenerator(int version, int seed, floa
FillSpan(instance, idealSize2, array, MyDefinitionManager.Static.GetVoxelMapStorageDefinitionsForProceduralAdditions());
FillSpan(instance, idealSize, array2, MyDefinitionManager.Static.GetVoxelMapStorageDefinitionsForProceduralRemovals());
TranslateShapes(array2, size, instance);
TranslateShapes(array.Span(1), size, instance);
TranslateShapes(new Span<IMyCompositeShape>(array, 1, array.Length - 1), size, instance);
if (size > 512f)
{
size /= 2f;
Expand Down Expand Up @@ -415,14 +414,14 @@ private void GenerateMaterials(int version, float size, MyRandom random, MyCsgSh
}
Action<List<MyVoxelMaterialDefinition>> action = delegate (List<MyVoxelMaterialDefinition> list)
{
int num9 = list.Count;
while (num9 > 1)
int num10 = list.Count;
while (num10 > 1)
{
int index = random.Next() % num9;
num9--;
int index = random.Next() % num10;
num10--;
MyVoxelMaterialDefinition value = list[index];
list[index] = list[num9];
list[num9] = value;
list[index] = list[num10];
list[num10] = value;
}
};
action(m_depositMaterials);
Expand All @@ -444,7 +443,9 @@ private void GenerateMaterials(int version, float size, MyRandom random, MyCsgSh
int val;
if (flag)
{
val = ((size <= 64f) ? 2 : ((size <= 128f) ? 4 : ((size <= 256f) ? 6 : ((!(size <= 512f)) ? 10 : 8))));
int num = 0;
num = ((size <= 64f) ? 1 : ((size <= 128f) ? 2 : ((size <= 256f) ? 3 : ((!(size <= 512f)) ? 5 : 4))));
val = (int)(MySession.Static.Settings.DepositsCountCoefficient * (float)num);
if (m_depositMaterials.Count == 0)
{
val = 0;
Expand All @@ -456,9 +457,10 @@ private void GenerateMaterials(int version, float size, MyRandom random, MyCsgSh
}
val = Math.Max(val, filledShapes.Length);
deposits = new MyCompositeShapeOreDeposit[val];
float num = (!flag) ? (size / 10f) : (size / 30f + 8f);
float depositSizeDenominator = MySession.Static.Settings.DepositSizeDenominator;
float num2 = (!flag || !(depositSizeDenominator > 0f)) ? (size / 10f) : (size / depositSizeDenominator + 8f);
MyVoxelMaterialDefinition material = defaultMaterial;
int num2 = 0;
int num3 = 0;
for (int i = 0; i < filledShapes.Length; i++)
{
if (i == 0)
Expand All @@ -474,7 +476,7 @@ private void GenerateMaterials(int version, float size, MyRandom random, MyCsgSh
}
else
{
material = m_depositMaterials[num2++];
material = m_depositMaterials[num3++];
}
}
else
Expand All @@ -491,30 +493,30 @@ private void GenerateMaterials(int version, float size, MyRandom random, MyCsgSh
}
else
{
material = m_depositMaterials[num2++];
material = m_depositMaterials[num3++];
}
deposits[i] = new MyCompositeShapeOreDeposit(filledShapes[i].DeepCopy(), material);
deposits[i].Shape.ShrinkTo(random.NextFloat() * (flag ? 0.6f : 0.15f) + (flag ? 0.1f : 0.6f));
if (num2 == m_depositMaterials.Count)
if (num3 == m_depositMaterials.Count)
{
num2 = 0;
num3 = 0;
action(m_depositMaterials);
}
}
for (int j = filledShapes.Length; j < val; j++)
{
float num5 = 0f;
float num6 = 0f;
Vector3 vector = Vector3.Zero;
for (int k = 0; k < 10; k++)
{
vector = CreateRandomPointInBox(random, size * (flag ? 0.6f : 0.7f)) + storageOffset + size * 0.15f;
num5 = random.NextFloat() * num + (flag ? 5f : 8f);
num6 = random.NextFloat() * num2 + (flag ? 5f : 8f);
if (shapeInfo == null)
{
break;
}
double num6 = Math.Sqrt(num5 * num5 / 2f) * 0.5;
Vector3I b = new Vector3I((int)num6);
double num7 = Math.Sqrt(num6 * num6 / 2f) * 0.5;
Vector3I b = new Vector3I((int)num7);
BoundingBoxI box = new BoundingBoxI((Vector3I)vector - b, (Vector3I)vector + b);
if (MyCompositeShapeProvider.Intersect(shapeInfo, box, 0) != 0)
{
Expand All @@ -523,20 +525,20 @@ private void GenerateMaterials(int version, float size, MyRandom random, MyCsgSh
}
random.NextFloat();
random.NextFloat();
MyCsgShapeBase shape = new MyCsgSphere(vector, num5);
material = ((m_depositMaterials.Count != 0) ? m_depositMaterials[num2++] : m_surfaceMaterials[num2++]);
MyCsgShapeBase shape = new MyCsgSphere(vector, num6);
material = ((m_depositMaterials.Count != 0) ? m_depositMaterials[num3++] : m_surfaceMaterials[num3++]);
deposits[j] = new MyCompositeShapeOreDeposit(shape, material);
if (m_depositMaterials.Count == 0)
{
if (num2 == m_surfaceMaterials.Count)
if (num3 == m_surfaceMaterials.Count)
{
num2 = 0;
num3 = 0;
action(m_surfaceMaterials);
}
}
else if (num2 == m_depositMaterials.Count)
else if (num3 == m_depositMaterials.Count)
{
num2 = 0;
num3 = 0;
action(m_depositMaterials);
}
}
Expand Down
5 changes: 3 additions & 2 deletions SEWorldGenPlugin/Generator/ProceduralGen/EntityExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using VRage.Game.Entity;
using Sandbox.Game.Entities;
using VRage.Game.Entity;
using VRage.Utils;

namespace SEWorldGenPlugin.Generator.ProceduralGen
Expand All @@ -8,10 +9,10 @@ public static class EntityExtension

public static void ProceduralGeneratorTracking(this MyEntity thisEntity)
{
if (thisEntity is MyVoxelBase) return;
if(ProceduralGenerator.Static != null)
{
ProceduralGenerator.Static.TrackEntity(thisEntity);
MyLog.Default.WriteLine("Tracking Entity" + thisEntity.DisplayName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Sandbox.Engine.Voxels;
using Sandbox;
using Sandbox.Definitions;
using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Voxels;
using Sandbox.Game;
using Sandbox.Game.Entities;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.World;
Expand Down Expand Up @@ -27,8 +31,22 @@ public class ProceduralAsteroidsRingModule : ProceduralModule
private const int SUBCELL_SIZE = OBJECT_SIZE_MAX * 3;
private const int SUBCELLS = 6;

private HashSet<MyVoxelBase> m_NotSavedMaps;
private bool m_saving;
private MyAsteroidGeneratorDefinition m_data;

public ProceduralAsteroidsRingModule(int seed) : base(seed, SUBCELLS * SUBCELL_SIZE)
{
m_NotSavedMaps = new HashSet<MyVoxelBase>();
MySession.Static.OnSavingCheckpoint += delegate
{
foreach(var map in m_NotSavedMaps)
{
map.Save = false;
}
m_saving = true;
};
m_data = GetData();
}

public override ProceduralCell GenerateCell(ref Vector3I id)
Expand Down Expand Up @@ -61,7 +79,7 @@ public override ProceduralCell GenerateCell(ref Vector3I id)
minSize = ((MySystemBeltItem)obj).RoidSize;

if (obj.Type == SystemObjectType.RING)
minSize = ((MyPlanetRingItem)obj).RoidSize;
minSize = ((MyPlanetRingItem)obj).RoidSize;

var cellObject = new CellObject(cell, position, MyRandom.Instance.Next(Math.Min(OBJECT_SIZE_MAX, minSize), OBJECT_SIZE_MAX));
cellObject.Params.Type = MyObjectSeedType.Asteroid;
Expand Down Expand Up @@ -108,23 +126,28 @@ public override void GenerateObjects(List<CellObject> objects, HashSet<MyObjectS

if (!exists)
{
var provider = MyCompositeShapeProvider.CreateAsteroidShape(obj.Params.Seed, obj.Size, MySession.Static.Settings.VoxelGeneratorVersion);
var provider = MyCompositeShapeProvider.CreateAsteroidShape(obj.Params.Seed, obj.Size, m_data.UseGeneratorSeed ? obj.Params.GeneratorSeed : 0);
var storage = new MyOctreeStorage(provider, GetAsteroidVoxelSize(obj.Size));

Vector3D pos = obj.BoundingVolume.Center - VRageMath.MathHelper.GetNearestBiggerPowerOfTwo(obj.Size) / 2;
Vector3D pos = obj.BoundingVolume.Center - MathHelper.GetNearestBiggerPowerOfTwo(obj.Size) / 2;

MyVoxelMap voxelMap = new MyVoxelMap();
MyVoxelMap voxelMap;

MyWorldGenerator.AddVoxelMap(storageName, storage, pos, GetAsteroidEntityId(storageName));
voxelMap = MyWorldGenerator.AddVoxelMap(storageName, storage, pos, GetAsteroidEntityId(storageName));
voxelMap.Save = true;

m_NotSavedMaps.Add(voxelMap);

voxelMap.Save = false;
MyVoxelBase.StorageChanged OnStorageRangeChanged = null;
OnStorageRangeChanged = delegate (MyVoxelBase voxel, Vector3I minVoxelChanged, Vector3I maxVoxelChanged, MyStorageDataTypeFlags changedData)
{
voxelMap.Save = true;
m_NotSavedMaps.Remove(voxelMap);
voxelMap.RangeChanged -= OnStorageRangeChanged;
};

voxelMap.RangeChanged += OnStorageRangeChanged;

}
tmp_voxelMaps.Clear();
}
Expand Down Expand Up @@ -179,6 +202,11 @@ public override void UnloadCells()
}
cellObjects.Clear();
}
foreach(ProceduralCell cell in m_toUnloadCells)
{
m_cells.Remove(cell.CellId);
m_cellsTree.RemoveProxy(cell.proxyId);
}
m_toUnloadCells.Clear();
}

Expand All @@ -195,12 +223,14 @@ private void UnloadAsteroid(CellObject obj)
{
if(map.StorageName == storageName)
{
if (!map.Save)
if (m_NotSavedMaps.Contains(map))
{
map.Close();
map.OnClose += delegate
{
obj.Params.Generated = false;
m_NotSavedMaps.Remove(map);
map.Save = false;
};
}
break;
Expand All @@ -215,5 +245,45 @@ private long GetAsteroidEntityId(string storageName)
long hash = storageName.GetHashCode64();
return hash & 0x00FFFFFFFFFFFFFF | ((long)MyEntityIdentifier.ID_OBJECT_TYPE.ASTEROID << 56);
}

public override void UpdateObjects()
{
if (!m_saving) return;
foreach(var map in m_NotSavedMaps)
{
map.Save = true;
}
m_saving = false;
}

private static MyAsteroidGeneratorDefinition GetData()
{
MyAsteroidGeneratorDefinition myAsteroidGeneratorDefinition = null;
int voxelGeneratorVersion = MySession.Static.Settings.VoxelGeneratorVersion;
foreach (MyAsteroidGeneratorDefinition value in MyDefinitionManager.Static.GetAsteroidGeneratorDefinitions().Values)
{
if (value.Version == voxelGeneratorVersion)
{
myAsteroidGeneratorDefinition = value;
break;
}
}
if (myAsteroidGeneratorDefinition == null)
{
MyLog.Default.WriteLine("Generator of version " + voxelGeneratorVersion + "not found!");
{
foreach (MyAsteroidGeneratorDefinition value2 in MyDefinitionManager.Static.GetAsteroidGeneratorDefinitions().Values)
{
if (myAsteroidGeneratorDefinition == null || (value2.Version > voxelGeneratorVersion && (myAsteroidGeneratorDefinition.Version < voxelGeneratorVersion || value2.Version < myAsteroidGeneratorDefinition.Version)))
{
myAsteroidGeneratorDefinition = value2;
}
}
return myAsteroidGeneratorDefinition;
}
}
return myAsteroidGeneratorDefinition;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public override void UpdateBeforeSimulation()
if (asteroidModule == null) return;

asteroidModule.UnloadCells();

asteroidModule.UpdateObjects();
}

protected override void UnloadData()
Expand Down
Loading

0 comments on commit d89c10b

Please sign in to comment.