Asteroid Belts

Sector renderer is now also rendering asteroid belts. All gravity calculations have been moved out of monobehaviors.
This commit is contained in:
Reinier Vens 2020-11-29 22:50:12 +00:00
commit a981a46170
76 changed files with 25027 additions and 2016 deletions

View file

@ -4,7 +4,6 @@ using System.Collections.Generic;
using JsonKnownTypes;
using MessagePack;
using Newtonsoft.Json;
using UnityEngine;
[MessagePackObject,
Union(0, typeof(StationTowing)),

View file

@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using MessagePack;
using Newtonsoft.Json;
using UnityEngine;
public class HaulingTask : AgentTask
{

View file

@ -63,7 +63,7 @@ public class ResourceScanner : IBehavior, IAlwaysUpdatedBehavior
public bool Update(float delta)
{
var planetData = Context.Cache.Get<PlanetData>(ScanTarget);
var planetData = Context.Cache.Get<BodyData>(ScanTarget);
if (planetData != null)
{
if (planetData is AsteroidBeltData beltData)

View file

@ -64,7 +64,7 @@ public class SurveyController : ControllerBase, IBehavior, IPersistentBehavior,
if (!corporation.PlanetSurveyFloor.ContainsKey(_targetPlanet) ||
corporation.PlanetSurveyFloor[_targetPlanet] + .5f < _scanningTool.MinimumDensity)
{
var planetData = _context.Cache.Get<PlanetData>(_targetPlanet);
var planetData = _context.Cache.Get<BodyData>(_targetPlanet);
MoveTo(() =>
{
var target = GetPosition(planetData, _asteroid);
@ -88,7 +88,7 @@ public class SurveyController : ControllerBase, IBehavior, IPersistentBehavior,
{
if (!Moving)
{
var planetData = _context.Cache.Get<PlanetData>(_targetPlanet);
var planetData = _context.Cache.Get<BodyData>(_targetPlanet);
if ((!corporation.PlanetSurveyFloor.ContainsKey(_targetPlanet) ||
corporation.PlanetSurveyFloor[_targetPlanet] + .5f < _scanningTool.MinimumDensity) &&
length(_entity.Position - GetPosition(planetData, _asteroid)) < _scanningTool.Range)
@ -117,7 +117,7 @@ public class SurveyController : ControllerBase, IBehavior, IPersistentBehavior,
_targetPlanet = nearestPlanet.ID;
}
private float2 GetPosition(PlanetData planet, int asteroid = -1)
private float2 GetPosition(BodyData planet, int asteroid = -1)
{
if (planet is AsteroidBeltData)
{
@ -127,7 +127,7 @@ public class SurveyController : ControllerBase, IBehavior, IPersistentBehavior,
return Zone.GetOrbitPosition(planet.Orbit);
}
private float2 GetVelocity(PlanetData planet, int asteroid = -1)
private float2 GetVelocity(BodyData planet, int asteroid = -1)
{
if (planet is AsteroidBeltData)
{

View file

@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using MessagePack;
using Newtonsoft.Json;
using UnityEngine;
[RethinkTable("Galaxy"), MessagePackObject, JsonObject(MemberSerialization.OptIn)]

View file

@ -6,9 +6,6 @@ using Newtonsoft.Json;
// TODO: USE THIS EVERYWHERE
using Unity.Mathematics;
using static Unity.Mathematics.math;
#if UNITY_EDITOR
using UnityEditor;
#endif
public interface INamedEntry
{
@ -33,7 +30,7 @@ public interface INamedEntry
Union(13, typeof(MegaCorporation)),
Union(14, typeof(OrbitalEntity)),
Union(15, typeof(OrbitData)),
Union(16, typeof(PlanetData)),
Union(16, typeof(BodyData)),
Union(17, typeof(PersonalityAttribute)),
Union(18, typeof(AgentTask)),
Union(19, typeof(LoadoutData)),
@ -45,6 +42,7 @@ public interface INamedEntry
Union(25, typeof(AsteroidBeltData)),
Union(26, typeof(GasGiantData)),
Union(27, typeof(SunData)),
Union(28, typeof(PlanetData)),
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
//[Union(21, typeof(ContractData))]
//[Union(22, typeof(Station))]

View file

@ -3,14 +3,16 @@
"references": [
"MessagePack",
"MagicOnion.Abstractions",
"Unity.Mathematics"
"Unity.Mathematics",
"UniRx"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": true
}

View file

@ -20,3 +20,28 @@ public class ExponentialCurve
public float Evaluate(float value) => Multiplier * pow(value, Exponent) + Constant;
}
[Serializable, MessagePackObject, JsonObject]
public class GravitySettings
{
[JsonProperty("gravityDepth"), Key(0)]
public ExponentialCurve GravityDepth;
[JsonProperty("gravityRadius"), Key(1)]
public ExponentialCurve GravityRadius;
[JsonProperty("waveDepth"), Key(2)]
public ExponentialCurve WaveDepth;
[JsonProperty("waveRadius"), Key(3)]
public ExponentialCurve WaveRadius;
[JsonProperty("waveFrequency"), Key(4)]
public ExponentialCurve WaveFrequency;
[JsonProperty("waveSpeed"), Key(5)]
public ExponentialCurve WaveSpeed;
[JsonProperty("gravityStrength"), Key(6)]
public float GravityStrength;
}

View file

@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IChangeSource

View file

@ -34,7 +34,7 @@ namespace LiteNetLib
if (Logger == null)
{
#if UNITY_4 || UNITY_5 || UNITY_5_3_OR_NEWER
UnityEngine.Debug.Log(string.Format(str, args));
//UnityEngine.Debug.Log(string.Format(str, args));
#else
Console.WriteLine(str, args);
#endif

View file

@ -8,10 +8,14 @@ using float2 = Unity.Mathematics.float2;
public class Zone
{
public float ZoneDepth;
public float ZoneDepthExponent;
public float ZoneDepthRadius;
public GravitySettings GravitySettings;
//public HashSet<Guid> Planets = new HashSet<Guid>();
public Dictionary<Guid, Entity> Entities = new Dictionary<Guid, Entity>();
//public Dictionary<Guid, OrbitData> Orbits = new Dictionary<Guid, OrbitData>();
public Dictionary<Guid, PlanetData> Planets = new Dictionary<Guid, PlanetData>();
public Dictionary<Guid, BodyData> Planets = new Dictionary<Guid, BodyData>();
public Dictionary<Guid, Orbit> Orbits = new Dictionary<Guid, Orbit>();
public Dictionary<Guid, AsteroidBelt> AsteroidBelts = new Dictionary<Guid, AsteroidBelt>();
@ -178,7 +182,7 @@ public class Zone
belt.Transforms[i] = float4(
OrbitData.Evaluate((float) frac(_context.Time / _context.GlobalData.OrbitalPeriod(beltData.Asteroids[i].Distance) * _context.GlobalData.OrbitSpeedMultiplier +
beltData.Asteroids[i].Phase)) * beltData.Asteroids[i].Distance + orbitPosition,
(float) (_context.Time * beltData.Asteroids[i].RotationSpeed % 360.0), size);
(float) (_context.Time * beltData.Asteroids[i].RotationSpeed % (PI * 2)), size);
}
}
@ -244,6 +248,73 @@ public class Zone
miner.AddCargo(newSimpleCommodity);
}
}
public float GetHeight(float2 position)
{
float result = -PowerPulse(length(position)/ZoneDepthRadius, ZoneDepthExponent) * ZoneDepth;
foreach (var body in Planets.Values)
{
if (body is AsteroidBeltData) continue;
var p = (position - GetOrbitPosition(body.Orbit));
var dist = length(p);
var gravityRadius = GravitySettings.GravityRadius.Evaluate(body.Mass.Value) * body.GravityRadiusMultiplier.Value;
if (dist < gravityRadius)
{
var depth = GravitySettings.GravityDepth.Evaluate(body.Mass.Value) * body.GravityDepthMultiplier.Value;
result -= PowerPulse(dist / gravityRadius, body.GravityDepthExponent.Value) * depth;
}
if (body is GasGiantData)
{
var waveRadius = GravitySettings.WaveRadius.Evaluate(body.Mass.Value) * body.GravityRadiusMultiplier.Value;
if(dist < waveRadius)
{
var depth = GravitySettings.WaveDepth.Evaluate(body.Mass.Value) * body.GravityDepthMultiplier.Value;
var frequency = GravitySettings.WaveFrequency.Evaluate(body.Mass.Value);
var speed = GravitySettings.WaveSpeed.Evaluate(body.Mass.Value);
result -= RadialWaves(dist / gravityRadius, 8, 1.5f, frequency, (float) (_context.Time * speed)) * depth;
}
}
}
return result;
}
public float2 GetForce(float2 position)
{
var normal = GetNormal(position);
var f = new float2(normal.x, normal.z);
return f * GravitySettings.GravityStrength * lengthsq(f);// * Mathf.Abs(GetHeight(position));
}
public static float PowerPulse(float x, float exponent)
{
x = clamp(x, -1, 1);
return pow((x + 1) * (1 - x), exponent);
}
public static float RadialWaves(float x, float maskExponent, float sineExponent, float frequency, float phase)
{
return PowerPulse(x, maskExponent) * cos(pow(x, sineExponent) * frequency + phase);
}
public float3 GetNormal(float2 pos, float step, float mul)
{
float hL = GetHeight(new float2(pos.x - step, pos.y)) * mul;
float hR = GetHeight(new float2(pos.x + step, pos.y)) * mul;
float hD = GetHeight(new float2(pos.x, pos.y - step)) * mul;
float hU = GetHeight(new float2(pos.x, pos.y + step)) * mul;
// Deduce terrain normal
float3 normal = new float3((hL - hR), (hD - hU), 2);
normalize(normal);
return new float3(normal.x, normal.z, normal.y);
}
public float3 GetNormal(float2 pos)
{
return GetNormal(pos, .1f, 1);
}
public override int GetHashCode()
{

View file

@ -3,9 +3,10 @@ using System.Collections;
using System.Collections.Generic;
using MessagePack;
using Newtonsoft.Json;
using UniRx;
using Unity.Mathematics;
using UnityEngine;
using static Unity.Mathematics.math;
using float3 = Unity.Mathematics.float3;
[RethinkTable("Galaxy"), MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class ZoneData : DatabaseEntry, INamedEntry
@ -48,7 +49,7 @@ public class ZonePack : INamedEntry
public ZoneData Data;
[JsonProperty("planets"), Key(1)]
public List<PlanetData> Planets = new List<PlanetData>();
public List<BodyData> Planets = new List<BodyData>();
[JsonProperty("orbits"), Key(2)]
public List<OrbitData> Orbits = new List<OrbitData>();
@ -97,73 +98,85 @@ public class ZonePack : INamedEntry
[MessagePackObject,
JsonObject(MemberSerialization.OptIn),
Union(0, typeof(PlanetData)),
Union(1, typeof(AsteroidBeltData)),
Union(2, typeof(GasGiantData)),
Union(3, typeof(SunData))]
public class PlanetData : DatabaseEntry, INamedEntry
public abstract class BodyData : DatabaseEntry, INamedEntry
{
[JsonProperty("name"), Key(1)]
public string Name;
public ReactiveProperty<string> Name = new ReactiveProperty<string>("");
[JsonProperty("orbit"), Key(2)]
public Guid Orbit;
[JsonProperty("mass"), Key(3)]
public float Mass;
public ReactiveProperty<float> Mass = new ReactiveProperty<float>(0);
[JsonProperty("resources"), Key(4)]
public Dictionary<Guid, float> Resources = new Dictionary<Guid, float>();
[JsonProperty("radiusMul")] [Key(5)]
public float GravityRadiusMultiplier = 1;
public ReactiveProperty<float> GravityRadiusMultiplier = new ReactiveProperty<float>(1);
[JsonProperty("depthMul")] [Key(6)]
public float GravityDepthMultiplier = 1;
public ReactiveProperty<float> GravityDepthMultiplier = new ReactiveProperty<float>(1);
[JsonProperty("depthExp")] [Key(7)]
public ReactiveProperty<float> GravityDepthExponent = new ReactiveProperty<float>(8);
[IgnoreMember] public string EntryName
{
get => Name;
set => Name = value;
get => Name.Value;
set => Name.Value = value;
}
}
public class AsteroidBeltData : PlanetData
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class PlanetData : BodyData
{
[JsonProperty("asteroids"), Key(7)]
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class AsteroidBeltData : BodyData
{
[JsonProperty("asteroids"), Key(8)]
public Asteroid[] Asteroids;
}
public class GasGiantData : PlanetData
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class GasGiantData : BodyData
{
[JsonProperty("firstOffsetDomainRotationSpeed"), Key(7)]
public float FirstOffsetDomainRotationSpeed;
[JsonProperty("firstOffsetDomainRotationSpeed"), Key(8)]
public ReactiveProperty<float> FirstOffsetDomainRotationSpeed = new ReactiveProperty<float>(1);
[JsonProperty("firstOffsetRotationSpeed"), Key(8)]
public float FirstOffsetRotationSpeed;
[JsonProperty("firstOffsetRotationSpeed"), Key(9)]
public ReactiveProperty<float> FirstOffsetRotationSpeed = new ReactiveProperty<float>(1);
[JsonProperty("secondOffsetDomainRotationSpeed"), Key(9)]
public float SecondOffsetDomainRotationSpeed;
[JsonProperty("secondOffsetDomainRotationSpeed"), Key(10)]
public ReactiveProperty<float> SecondOffsetDomainRotationSpeed = new ReactiveProperty<float>(1);
[JsonProperty("secondOffsetRotationSpeed"), Key(10)]
public float SecondOffsetRotationSpeed;
[JsonProperty("secondOffsetRotationSpeed"), Key(11)]
public ReactiveProperty<float> SecondOffsetRotationSpeed = new ReactiveProperty<float>(1);
[JsonProperty("albedoRotationSpeed"), Key(11)]
public float AlbedoRotationSpeed;
[JsonProperty("albedoRotationSpeed"), Key(12)]
public ReactiveProperty<float> AlbedoRotationSpeed = new ReactiveProperty<float>(1);
[JsonProperty("materialOverrides"), Key(12)]
[JsonProperty("materialOverrides"), Key(13)]
public List<string> MaterialOverrides = new List<string>();
[JsonProperty("colors"), Key(13)]
public Gradient Colors;
[JsonProperty("colors"), Key(14)]
public ReactiveProperty<float4[]> Colors = new ReactiveProperty<float4[]>();
}
public class SunData : GasGiantData
{
[JsonProperty("lightColor"), Key(17)]
public Color LightColor;
[JsonProperty("lightColor"), Key(15)]
public ReactiveProperty<float3> LightColor = new ReactiveProperty<float3>(float3.zero);
[JsonProperty("fogTintColor"), Key(18)]
public Color FogTintColor;
[JsonProperty("fogTintColor"), Key(16)]
public ReactiveProperty<float3> FogTintColor = new ReactiveProperty<float3>(float3.zero);
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]

View file

@ -5,7 +5,6 @@ using System.Globalization;
using System.Linq;
using MessagePack;
using Unity.Mathematics;
using UnityEngine;
using static Unity.Mathematics.math;
using Random = Unity.Mathematics.Random;
@ -103,7 +102,7 @@ public class ZoneGenerator
}
}
PlanetData planetData;
BodyData planetData;
switch (bodyType)
{
case BodyType.Asteroid:
@ -123,14 +122,14 @@ public class ZoneGenerator
throw new ArgumentOutOfRangeException();
}
planetData.Mass = planet.Mass;
planetData.Mass.Value = planet.Mass;
planetData.Orbit = orbitMap[planet].ID;
planetData.Resources = planetResources;
planetData.Name = planetData.ID.ToString().Substring(0, 8);
planetData.Name.Value = planetData.ID.ToString().Substring(0, 8);
if (planetData is AsteroidBeltData beltData)
{
beltData.Asteroids =
Enumerable.Range(0, (int) (pow(planetData.Mass, context.GlobalData.BeltMassExponent) / context.GlobalData.BeltMassRatio * orbitMap[planet].Distance))
Enumerable.Range(0, (int) (pow(planetData.Mass.Value, context.GlobalData.BeltMassExponent) / context.GlobalData.BeltMassRatio * orbitMap[planet].Distance))
.Select(_ => new Asteroid
{
Distance = context.Random.NextFloat(orbitMap[planet].Distance * .5f, orbitMap[planet].Distance * 1.5f),
@ -143,19 +142,17 @@ public class ZoneGenerator
}
else if (planetData is GasGiantData gas)
{
var grad = new Gradient();
var times = Enumerable.Range(0, 5).Select(i => (float) i / 4).ToArray();
grad.alphaKeys = times.Select(f => new GradientAlphaKey(1, f)).ToArray();
grad.colorKeys = times.Select(f => new GradientColorKey(UnityEngine.Random.ColorHSV(), f)).ToArray();
gas.Colors = grad;
gas.AlbedoRotationSpeed = 1;
gas.FirstOffsetRotationSpeed = 1;
gas.FirstOffsetDomainRotationSpeed = 1;
gas.SecondOffsetRotationSpeed = 1;
gas.SecondOffsetDomainRotationSpeed = 1;
gas.Colors.Value = times.Select(time => float4(context.Random.NextFloat3(),time)).ToArray();
gas.AlbedoRotationSpeed.Value = -3;
gas.FirstOffsetRotationSpeed.Value = 5;
gas.FirstOffsetDomainRotationSpeed.Value = 0;
gas.SecondOffsetRotationSpeed.Value = 10;
gas.SecondOffsetDomainRotationSpeed.Value = -25;
if (planetData is SunData sun)
{
sun.FogTintColor = sun.LightColor = UnityEngine.Random.ColorHSV();
sun.FogTintColor.Value = sun.LightColor.Value = context.Random.NextFloat3();
gas.FirstOffsetDomainRotationSpeed.Value = 5;
}
}
return planetData;