2025-07-18 00:21:33 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2020-12-22 03:13:43 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-11-27 22:47:33 +00:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-01-17 23:26:53 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-12-01 00:16:11 +00:00
|
|
|
using UniRx;
|
2020-11-27 22:47:33 +00:00
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
using float2 = Unity.Mathematics.float2;
|
2020-12-01 00:16:11 +00:00
|
|
|
using Random = Unity.Mathematics.Random;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
public class Zone
|
|
|
|
|
{
|
2021-02-11 03:14:20 +00:00
|
|
|
public Action<string> Log;
|
2020-11-27 22:47:33 +00:00
|
|
|
//public HashSet<Guid> Planets = new HashSet<Guid>();
|
2020-12-22 03:13:43 +00:00
|
|
|
public ReactiveCollection<Entity> Entities = new ReactiveCollection<Entity>();
|
2020-11-27 22:47:33 +00:00
|
|
|
//public Dictionary<Guid, OrbitData> Orbits = new Dictionary<Guid, OrbitData>();
|
2020-11-29 22:50:12 +00:00
|
|
|
public Dictionary<Guid, BodyData> Planets = new Dictionary<Guid, BodyData>();
|
2020-12-01 00:16:11 +00:00
|
|
|
public Dictionary<Guid, Planet> PlanetInstances = new Dictionary<Guid, Planet>();
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
public Dictionary<Guid, Orbit> Orbits = new Dictionary<Guid, Orbit>();
|
|
|
|
|
public Dictionary<Guid, AsteroidBelt> AsteroidBelts = new Dictionary<Guid, AsteroidBelt>();
|
2021-02-11 03:14:20 +00:00
|
|
|
public PlanetSettings Settings;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2020-12-03 17:32:34 +00:00
|
|
|
private HashSet<Guid> _updatedOrbits = new HashSet<Guid>();
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2021-03-13 03:23:58 +00:00
|
|
|
private ItemManager _itemManager;
|
2021-03-27 10:03:24 +00:00
|
|
|
private double _time;
|
2020-12-01 00:16:11 +00:00
|
|
|
private Random _random;
|
2021-03-09 02:26:58 +00:00
|
|
|
public List<Agent> Agents = new List<Agent>();
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2021-01-17 23:26:53 +00:00
|
|
|
private List<Task> BeltUpdates = new List<Task>();
|
2021-03-15 11:40:46 +00:00
|
|
|
|
2021-03-27 10:03:24 +00:00
|
|
|
public float Time
|
|
|
|
|
{
|
|
|
|
|
get => (float) _time;
|
|
|
|
|
}
|
2021-03-15 11:40:46 +00:00
|
|
|
public ZonePack Pack { get; }
|
2021-05-14 03:03:28 +01:00
|
|
|
public GalaxyZone GalaxyZone { get; }
|
|
|
|
|
public Galaxy Galaxy { get; }
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2021-05-14 03:03:28 +01:00
|
|
|
public Zone(ItemManager itemManager, PlanetSettings settings, ZonePack pack, GalaxyZone galaxyZone, Galaxy galaxy)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2021-03-27 10:03:24 +00:00
|
|
|
_time = pack.Time;
|
2021-05-14 03:03:28 +01:00
|
|
|
GalaxyZone = galaxyZone;
|
|
|
|
|
Galaxy = galaxy;
|
2021-03-15 11:40:46 +00:00
|
|
|
Pack = pack;
|
2021-03-13 03:23:58 +00:00
|
|
|
_itemManager = itemManager;
|
2021-01-06 14:56:39 +00:00
|
|
|
Settings = settings;
|
2021-05-14 03:03:28 +01:00
|
|
|
_random = new Random(Convert.ToUInt32(abs(galaxyZone?.Name.GetHashCode() ?? 1337)));
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
foreach (var orbit in pack.Orbits)
|
|
|
|
|
{
|
2021-01-06 14:56:39 +00:00
|
|
|
Orbits.Add(orbit.ID, new Orbit(Settings, orbit));
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
2020-12-01 00:16:11 +00:00
|
|
|
|
|
|
|
|
foreach (var planet in pack.Planets)
|
|
|
|
|
{
|
|
|
|
|
Planets.Add(planet.ID, planet);
|
|
|
|
|
switch (planet)
|
|
|
|
|
{
|
|
|
|
|
case AsteroidBeltData belt:
|
2020-12-03 17:32:34 +00:00
|
|
|
AsteroidBelts[belt.ID] = new AsteroidBelt(belt);
|
2020-12-01 00:16:11 +00:00
|
|
|
break;
|
2021-04-12 04:20:52 +01:00
|
|
|
case SunData sun:
|
|
|
|
|
PlanetInstances.Add(sun.ID, new Sun(settings, sun, Orbits[planet.Orbit]));
|
|
|
|
|
break;
|
2020-12-01 00:16:11 +00:00
|
|
|
case GasGiantData gas:
|
2020-12-03 17:32:34 +00:00
|
|
|
PlanetInstances.Add(gas.ID, new GasGiant(settings, gas, Orbits[planet.Orbit]));
|
2020-12-01 00:16:11 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2020-12-03 17:32:34 +00:00
|
|
|
PlanetInstances.Add(planet.ID, new Planet(settings, planet, Orbits[planet.Orbit]));
|
2020-12-01 00:16:11 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 03:23:58 +00:00
|
|
|
|
2021-03-15 11:40:46 +00:00
|
|
|
foreach (var entityPack in pack.Entities)
|
2021-03-13 03:23:58 +00:00
|
|
|
{
|
2021-03-15 11:40:46 +00:00
|
|
|
var entity = EntitySerializer.Unpack(_itemManager, this, entityPack);
|
|
|
|
|
Entities.Add(entity);
|
|
|
|
|
entity.Activate();
|
2021-03-18 00:36:34 +00:00
|
|
|
if (entity is Ship {IsPlayerShip: false} ship)
|
|
|
|
|
{
|
|
|
|
|
Agents.Add(CreateAgent(ship));
|
|
|
|
|
if (lengthsq(ship.Position) < 1)
|
|
|
|
|
ship.Position = _itemManager.Random.NextFloat3(float3(-pack.Radius * .5f), float3(pack.Radius * .5f));
|
|
|
|
|
}
|
2021-03-13 03:23:58 +00:00
|
|
|
}
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
// TODO: Associate planets with stored entities for planetary colonies
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 00:36:34 +00:00
|
|
|
private Agent CreateAgent(Ship ship)
|
|
|
|
|
{
|
|
|
|
|
var agent = new Minion(ship);
|
|
|
|
|
var task = new PatrolOrbitsTask();
|
|
|
|
|
task.Circuit = Orbits.OrderBy(_ => _itemManager.Random.NextFloat()).Take(4).Select(x => x.Key).ToArray();
|
|
|
|
|
agent.Task = task;
|
|
|
|
|
return agent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 11:40:46 +00:00
|
|
|
public ZonePack PackZone()
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
|
|
|
|
return new ZonePack
|
|
|
|
|
{
|
2021-03-15 11:40:46 +00:00
|
|
|
Radius = Pack.Radius,
|
|
|
|
|
Mass = Pack.Mass,
|
2021-03-13 03:23:58 +00:00
|
|
|
Entities = Entities.Select(EntitySerializer.Pack).ToList(),
|
2020-11-27 22:47:33 +00:00
|
|
|
Orbits = Orbits.Values.Select(o=>o.Data).ToList(),
|
2021-03-27 10:03:24 +00:00
|
|
|
Planets = Planets.Values.ToList(),
|
|
|
|
|
Time = _time
|
2020-11-27 22:47:33 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddOrbit(OrbitData orbit)
|
|
|
|
|
{
|
2021-01-06 14:56:39 +00:00
|
|
|
Orbits.Add(orbit.ID, new Orbit(Settings, orbit));
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-27 10:03:24 +00:00
|
|
|
public void Update(float deltaTime)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2021-03-27 10:03:24 +00:00
|
|
|
_time += deltaTime;
|
2020-12-03 17:32:34 +00:00
|
|
|
_updatedOrbits.Clear();
|
2021-01-17 23:26:53 +00:00
|
|
|
foreach (var t in BeltUpdates)
|
|
|
|
|
t.Wait();
|
|
|
|
|
BeltUpdates.Clear();
|
2020-11-27 22:47:33 +00:00
|
|
|
foreach (var orbit in Orbits)
|
|
|
|
|
{
|
|
|
|
|
orbit.Value.PreviousPosition = orbit.Value.Position;
|
|
|
|
|
orbit.Value.Position = GetOrbitPosition(orbit.Key);
|
|
|
|
|
orbit.Value.Velocity = (orbit.Value.Position - orbit.Value.PreviousPosition) / deltaTime;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 23:26:53 +00:00
|
|
|
foreach (var belt in AsteroidBelts)
|
|
|
|
|
{
|
2022-07-19 18:55:17 +01:00
|
|
|
Array.Copy(belt.Value.NewTransforms, belt.Value.Transforms, belt.Value.Transforms.Length);
|
|
|
|
|
belt.Value.OrbitPosition = belt.Value.NewOrbitPosition;
|
|
|
|
|
BeltUpdates.Add(Task.Run(() => UpdateAsteroidTransforms(belt.Key)));
|
2021-01-17 23:26:53 +00:00
|
|
|
}
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2021-03-09 02:26:58 +00:00
|
|
|
foreach(var agent in Agents)
|
|
|
|
|
agent.Update(deltaTime);
|
|
|
|
|
|
2021-03-11 01:36:08 +00:00
|
|
|
foreach (var entity in Entities.ToArray()) entity.Update(deltaTime);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine orbital position recursively, caching parent positions to avoid repeated calculations
|
2020-12-01 00:16:11 +00:00
|
|
|
public float2 GetOrbitPosition(Guid orbitID)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
// Root orbit is fixed at origin
|
|
|
|
|
if(orbitID==Guid.Empty)
|
2020-11-27 22:47:33 +00:00
|
|
|
return float2.zero;
|
2021-02-11 03:14:20 +00:00
|
|
|
if (!Orbits.ContainsKey(orbitID))
|
|
|
|
|
{
|
|
|
|
|
Log?.Invoke("Requested orbit is not part of this zone!");
|
|
|
|
|
return float2.zero;
|
|
|
|
|
}
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
if (!_updatedOrbits.Contains(orbitID))
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
var orbit = Orbits[orbitID];
|
2020-11-27 22:47:33 +00:00
|
|
|
float2 pos = float2.zero;
|
2025-07-18 00:21:33 +01:00
|
|
|
if (orbit.Period > .01f)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
var phase = (float) frac(_time / orbit.Period);
|
|
|
|
|
pos = OrbitData.Evaluate(frac(phase + orbit.Data.Phase)) * orbit.Data.Distance;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
if (float.IsNaN(pos.x))
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
//_context.Log("Orbit position is NaN, something went very wrong!");
|
2020-11-27 22:47:33 +00:00
|
|
|
pos = float2.zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 22:58:37 +00:00
|
|
|
var parentPosition = Orbits[orbitID].Data.Parent == Guid.Empty
|
|
|
|
|
? Orbits[orbitID].Data.FixedPosition :
|
|
|
|
|
GetOrbitPosition(orbit.Data.Parent);
|
|
|
|
|
Orbits[orbitID].Position = parentPosition + pos;
|
2020-12-01 00:16:11 +00:00
|
|
|
_updatedOrbits.Add(orbitID);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
return Orbits[orbitID].Position;
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float2 GetOrbitVelocity(Guid orbit)
|
|
|
|
|
{
|
|
|
|
|
if (Orbits.ContainsKey(orbit))
|
|
|
|
|
return Orbits[orbit].Velocity;
|
|
|
|
|
return float2.zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int NearestAsteroid(Guid planetDataID, float2 position)
|
|
|
|
|
{
|
|
|
|
|
var beltData = Planets[planetDataID] as AsteroidBeltData;
|
|
|
|
|
|
2022-07-19 18:55:17 +01:00
|
|
|
var asteroidPositions = AsteroidBelts[planetDataID].Transforms;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
int nearest = 0;
|
|
|
|
|
float nearestDistance = Single.MaxValue;
|
|
|
|
|
for (int i = 0; i < beltData.Asteroids.Length; i++)
|
|
|
|
|
{
|
2021-01-17 23:26:53 +00:00
|
|
|
var dist = lengthsq(asteroidPositions[i].xz - position);
|
2020-11-27 22:47:33 +00:00
|
|
|
if (AsteroidExists(planetDataID, i) && dist < nearestDistance)
|
|
|
|
|
{
|
|
|
|
|
nearest = i;
|
|
|
|
|
nearestDistance = dist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nearest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AsteroidExists(Guid planetDataID, int asteroid) => ((AsteroidBeltData) Planets[planetDataID]).Asteroids.Length > asteroid && asteroid >= 0;
|
|
|
|
|
|
2022-07-19 18:55:17 +01:00
|
|
|
private void UpdateAsteroidTransforms(Guid planetDataID)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
|
|
|
|
var beltData = Planets[planetDataID] as AsteroidBeltData;
|
|
|
|
|
|
|
|
|
|
var belt = AsteroidBelts[planetDataID];
|
|
|
|
|
|
|
|
|
|
var orbitData = Orbits[beltData.Orbit].Data;
|
2022-07-19 18:55:17 +01:00
|
|
|
belt.NewOrbitPosition = GetOrbitPosition(orbitData.Parent);
|
2020-11-27 22:47:33 +00:00
|
|
|
for (var i = 0; i < beltData.Asteroids.Length; i++)
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
float size;
|
2020-11-27 22:47:33 +00:00
|
|
|
if(belt.RespawnTimers.ContainsKey(i)) size = 0;
|
|
|
|
|
else if (belt.Damage.ContainsKey(i))
|
|
|
|
|
{
|
2021-01-06 14:56:39 +00:00
|
|
|
var asteroidHitpoints = Settings.AsteroidHitpoints.Evaluate(beltData.Asteroids[i].Size);
|
2020-12-01 00:16:11 +00:00
|
|
|
var damage = (asteroidHitpoints - belt.Damage[i]) / asteroidHitpoints;
|
2021-01-06 14:56:39 +00:00
|
|
|
size = Settings.AsteroidSize.Evaluate(damage * beltData.Asteroids[i].Size);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
2021-01-06 14:56:39 +00:00
|
|
|
else size = Settings.AsteroidSize.Evaluate(beltData.Asteroids[i].Size);
|
2021-01-17 23:26:53 +00:00
|
|
|
|
2022-07-19 18:55:17 +01:00
|
|
|
var rot = (float) (_time * beltData.Asteroids[i].RotationSpeed % (PI * 2));
|
2021-03-27 10:03:24 +00:00
|
|
|
var pos = OrbitData.Evaluate((float) frac(_time / Settings.OrbitPeriod.Evaluate(beltData.Asteroids[i].Distance) +
|
2022-07-19 18:55:17 +01:00
|
|
|
beltData.Asteroids[i].Phase)) * beltData.Asteroids[i].Distance + belt.NewOrbitPosition;
|
|
|
|
|
//belt.NewPositions[i] = float3(pos.x, GetHeight(pos) + Settings.AsteroidVerticalOffset, pos.y);
|
|
|
|
|
belt.NewTransforms[i] = float4(pos.x, pos.y, rot, size);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrbitData CreateOrbit(Guid parent, float2 position)
|
|
|
|
|
{
|
|
|
|
|
var parentPosition = GetOrbitPosition(parent);
|
|
|
|
|
var delta = position - parentPosition;
|
|
|
|
|
var distance = length(delta);
|
2021-01-06 14:56:39 +00:00
|
|
|
var period = Settings.OrbitPeriod.Evaluate(distance);
|
2020-11-27 22:47:33 +00:00
|
|
|
var phase = atan2(delta.y, delta.x) / (PI * 2);
|
2020-12-01 00:16:11 +00:00
|
|
|
var currentPhase = frac(_time / period);
|
2020-11-27 22:47:33 +00:00
|
|
|
var storedPhase = (float) frac(phase - currentPhase);
|
|
|
|
|
|
|
|
|
|
var orbit = new OrbitData
|
|
|
|
|
{
|
2020-12-23 22:38:30 +00:00
|
|
|
ID = Guid.NewGuid(),
|
2025-07-18 00:21:33 +01:00
|
|
|
Distance = distance,
|
2020-11-27 22:47:33 +00:00
|
|
|
Parent = parent,
|
|
|
|
|
Phase = storedPhase
|
|
|
|
|
};
|
2021-01-06 14:56:39 +00:00
|
|
|
Orbits.Add(orbit.ID, new Orbit(Settings, orbit));
|
2020-11-27 22:47:33 +00:00
|
|
|
return orbit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MineAsteroid(Entity miner, Guid asteroidBelt, int asteroid, float damage, float efficiency, float penetration)
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
var beltData = Planets[asteroidBelt] as AsteroidBeltData;
|
2020-11-27 22:47:33 +00:00
|
|
|
var belt = AsteroidBelts[asteroidBelt];
|
2021-01-17 23:26:53 +00:00
|
|
|
//var asteroidTransform = belt.Transforms[asteroid];
|
2020-12-01 00:16:11 +00:00
|
|
|
|
|
|
|
|
var size = beltData.Asteroids[asteroid].Size;
|
2021-01-06 14:56:39 +00:00
|
|
|
var asteroidHitpoints = Settings.AsteroidHitpoints.Evaluate(size);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
if (!belt.Damage.ContainsKey(asteroid))
|
|
|
|
|
belt.Damage[asteroid] = 0;
|
|
|
|
|
belt.Damage[asteroid] = belt.Damage[asteroid] + damage;
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
if (!belt.MiningAccumulator.ContainsKey((miner, asteroid)))
|
|
|
|
|
belt.MiningAccumulator[(miner, asteroid)] = 0;
|
|
|
|
|
belt.MiningAccumulator[(miner, asteroid)] = belt.MiningAccumulator[(miner, asteroid)] + damage;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
if (belt.Damage[asteroid] > asteroidHitpoints)
|
|
|
|
|
{
|
2021-01-06 14:56:39 +00:00
|
|
|
belt.RespawnTimers[asteroid] = Settings.AsteroidRespawnTime.Evaluate(size);
|
2020-11-27 22:47:33 +00:00
|
|
|
belt.Damage.Remove(asteroid);
|
2020-12-10 23:20:38 +00:00
|
|
|
belt.MiningAccumulator.Remove((miner, asteroid));
|
2020-11-27 22:47:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
var resourceCount = beltData.Resources.Sum(x => x.Value);
|
|
|
|
|
var resource = beltData.Resources.MaxBy(x => pow(x.Value, 1f / penetration) * _random.NextFloat());
|
2021-01-06 14:56:39 +00:00
|
|
|
if (efficiency * _random.NextFloat() * belt.MiningAccumulator[(miner, asteroid)] * resourceCount / Settings.MiningDifficulty > 1)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
belt.MiningAccumulator.Remove((miner, asteroid));
|
|
|
|
|
// var newSimpleCommodity = new SimpleCommodity
|
|
|
|
|
// {
|
|
|
|
|
// Data = resource.Key,
|
|
|
|
|
// Quantity = 1
|
|
|
|
|
// };
|
|
|
|
|
// TODO: Drop item onto the Grid
|
|
|
|
|
//miner.AddCargo(newSimpleCommodity);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-11 04:45:42 +01:00
|
|
|
|
|
|
|
|
public SecurityLevel GetSecurityLevel(float2 pos)
|
|
|
|
|
{
|
2021-05-14 03:03:28 +01:00
|
|
|
if (GalaxyZone.Owner==null) return SecurityLevel.Open;
|
2021-05-11 04:45:42 +01:00
|
|
|
|
|
|
|
|
var security = SecurityLevel.Open;
|
|
|
|
|
foreach (var entity in Entities)
|
|
|
|
|
{
|
2021-05-14 03:03:28 +01:00
|
|
|
if (entity is OrbitalEntity orbitalEntity && orbitalEntity.SecurityRadius > 1 && entity.Faction.ID == GalaxyZone.Owner.ID)
|
2021-05-11 04:45:42 +01:00
|
|
|
{
|
|
|
|
|
if (orbitalEntity.SecurityLevel > security && length(orbitalEntity.Position.xz - pos) < orbitalEntity.SecurityRadius * Settings.SecureAreaRadiusMultiplier)
|
|
|
|
|
security = orbitalEntity.SecurityLevel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return security;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
public float GetHeight(float2 position)
|
|
|
|
|
{
|
2021-04-12 04:20:52 +01:00
|
|
|
var result = -PowerPulse(length(position)/(Pack.Radius*2), Settings.ZoneDepthExponent) * Settings.ZoneDepth;
|
2020-12-01 00:16:11 +00:00
|
|
|
foreach (var body in PlanetInstances.Values)
|
2020-11-29 22:50:12 +00:00
|
|
|
{
|
2021-01-17 23:26:53 +00:00
|
|
|
var p = position - body.Orbit.Position; //GetOrbitPosition(body.BodyData.Orbit)
|
2020-12-22 21:32:37 +00:00
|
|
|
var distSqr = lengthsq(p);
|
2025-07-18 00:21:33 +01:00
|
|
|
var gravityRadius = body.GravityWellRadius;
|
2020-12-22 21:32:37 +00:00
|
|
|
if (distSqr < gravityRadius*gravityRadius)
|
2020-11-29 22:50:12 +00:00
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
var depth = body.GravityWellDepth;
|
|
|
|
|
result -= PowerPulse(sqrt(distSqr) / gravityRadius, body.BodyData.GravityDepthExponent) * depth;
|
2020-11-29 22:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
if (body is GasGiant gas)
|
2020-11-29 22:50:12 +00:00
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
var waveRadius = gas.GravityWavesRadius;
|
2020-12-22 21:32:37 +00:00
|
|
|
if(distSqr < waveRadius*waveRadius)
|
2020-11-29 22:50:12 +00:00
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
var depth = gas.GravityWavesDepth;
|
|
|
|
|
var frequency = Settings.WaveFrequency.Evaluate(body.BodyData.Mass);
|
|
|
|
|
var speed = gas.GravityWavesSpeed;
|
2021-03-27 10:03:24 +00:00
|
|
|
result -= RadialWaves(sqrt(distSqr) / waveRadius, 8, 1.25f, frequency, (float) (_time * speed)) * depth;
|
2020-11-29 22:50:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 04:20:52 +01:00
|
|
|
public float GetLight(float2 position)
|
|
|
|
|
{
|
|
|
|
|
var light = 0f;
|
|
|
|
|
foreach (var body in PlanetInstances.Values)
|
|
|
|
|
{
|
|
|
|
|
if (body is Sun sun)
|
|
|
|
|
{
|
|
|
|
|
var p = position - body.Orbit.Position;
|
|
|
|
|
var distSqr = lengthsq(p);
|
2025-07-18 00:21:33 +01:00
|
|
|
var lightRadius = sun.LightRadius;
|
2021-04-12 04:20:52 +01:00
|
|
|
if (distSqr < lightRadius * lightRadius)
|
|
|
|
|
{
|
|
|
|
|
light += PowerPulse(sqrt(distSqr) / lightRadius, 8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return light;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
public float2 GetForce(float2 position)
|
|
|
|
|
{
|
|
|
|
|
var normal = GetNormal(position);
|
|
|
|
|
var f = new float2(normal.x, normal.z);
|
2025-07-18 00:21:33 +01:00
|
|
|
return f * Settings.GravityStrength * lengthsq(f);
|
2020-11-29 22:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float PowerPulse(float x, float exponent)
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
x *= 2;
|
2020-11-29 22:50:12 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2020-12-22 21:32:37 +00:00
|
|
|
//x *= 2;
|
|
|
|
|
return PowerPulse(x, maskExponent) * cos(pow(x*2, sineExponent) * frequency + phase);
|
2020-11-29 22:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-22 03:13:43 +00:00
|
|
|
public float3 GetNormal(float2 pos, float step = .1f, float mul = 1)
|
2020-11-29 22:50:12 +00:00
|
|
|
{
|
|
|
|
|
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
|
2020-12-22 03:13:43 +00:00
|
|
|
float3 normal = new float3((hL - hR), (hD - hU), step*2);
|
|
|
|
|
return normalize(normal).xzy;
|
2020-11-29 22:50:12 +00:00
|
|
|
}
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
public class Planet
|
|
|
|
|
{
|
2020-12-03 17:32:34 +00:00
|
|
|
public Orbit Orbit;
|
2025-07-18 00:21:33 +01:00
|
|
|
protected readonly PlanetSettings Settings;
|
2020-12-01 00:16:11 +00:00
|
|
|
public BodyData BodyData;
|
2025-07-18 00:21:33 +01:00
|
|
|
public float GravityWellDepth;
|
|
|
|
|
public float GravityWellRadius;
|
|
|
|
|
public float BodyRadius;
|
2020-12-01 00:16:11 +00:00
|
|
|
|
2020-12-03 17:32:34 +00:00
|
|
|
public Planet(PlanetSettings settings, BodyData data, Orbit orbit)
|
2020-12-01 00:16:11 +00:00
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
Settings = settings;
|
2020-12-03 17:32:34 +00:00
|
|
|
Orbit = orbit;
|
2020-12-01 00:16:11 +00:00
|
|
|
BodyData = data;
|
2025-07-18 00:21:33 +01:00
|
|
|
CalculateProperties();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CalculateProperties()
|
|
|
|
|
{
|
|
|
|
|
BodyRadius = Settings.BodyRadius.Evaluate(BodyData.Mass) * BodyData.BodyRadiusMultiplier;
|
|
|
|
|
GravityWellRadius = Settings.GravityRadius.Evaluate(BodyData.Mass) * BodyData.GravityRadiusMultiplier;
|
|
|
|
|
GravityWellDepth = Settings.GravityDepth.Evaluate(BodyData.Mass) * BodyData.GravityDepthMultiplier;
|
2020-12-01 00:16:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GasGiant : Planet
|
|
|
|
|
{
|
|
|
|
|
public GasGiantData GasGiantData;
|
2025-07-18 00:21:33 +01:00
|
|
|
public float GravityWavesDepth;
|
|
|
|
|
public float GravityWavesRadius;
|
|
|
|
|
public float GravityWavesSpeed;
|
2020-12-01 00:16:11 +00:00
|
|
|
|
2020-12-03 17:32:34 +00:00
|
|
|
public GasGiant(PlanetSettings settings, GasGiantData data, Orbit orbit) : base(settings, data, orbit)
|
2020-12-01 00:16:11 +00:00
|
|
|
{
|
|
|
|
|
GasGiantData = data;
|
2025-07-18 00:21:33 +01:00
|
|
|
CalculateProperties();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new void CalculateProperties()
|
|
|
|
|
{
|
|
|
|
|
base.CalculateProperties();
|
|
|
|
|
GravityWavesDepth = Settings.WaveDepth.Evaluate(BodyData.Mass) * GasGiantData.WaveDepthMultiplier;
|
|
|
|
|
GravityWavesRadius = Settings.WaveRadius.Evaluate(BodyData.Mass) * GasGiantData.WaveRadiusMultiplier;
|
|
|
|
|
GravityWavesSpeed = Settings.WaveSpeed.Evaluate(BodyData.Mass) * GasGiantData.WaveSpeedMultiplier;
|
2020-12-01 00:16:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 04:20:52 +01:00
|
|
|
public class Sun : GasGiant
|
|
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
public SunData SunData;
|
|
|
|
|
public float LightRadius;
|
|
|
|
|
|
2021-04-12 04:20:52 +01:00
|
|
|
public Sun(PlanetSettings settings, SunData data, Orbit orbit) : base(settings, data, orbit)
|
|
|
|
|
{
|
2025-07-18 00:21:33 +01:00
|
|
|
SunData = data;
|
|
|
|
|
CalculateProperties();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new void CalculateProperties()
|
|
|
|
|
{
|
|
|
|
|
base.CalculateProperties();
|
|
|
|
|
LightRadius = Settings.LightRadius.Evaluate(BodyData.Mass) * SunData.LightRadiusMultiplier;
|
2021-04-12 04:20:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 00:16:11 +00:00
|
|
|
|
2020-11-27 22:47:33 +00:00
|
|
|
public class AsteroidBelt
|
|
|
|
|
{
|
2020-12-03 17:32:34 +00:00
|
|
|
public AsteroidBeltData Data;
|
2022-07-19 18:55:17 +01:00
|
|
|
public float4[] Transforms; // x, y, rotation, scale
|
|
|
|
|
public float4[] NewTransforms; // x, y, rotation, scale
|
|
|
|
|
public float Radius { get; }
|
|
|
|
|
public float2 OrbitPosition;
|
|
|
|
|
public float2 NewOrbitPosition;
|
2020-11-27 22:47:33 +00:00
|
|
|
public Dictionary<int, float> RespawnTimers = new Dictionary<int, float>();
|
|
|
|
|
public Dictionary<int, float> Damage = new Dictionary<int, float>();
|
2020-12-10 23:20:38 +00:00
|
|
|
public Dictionary<(Entity, int), float> MiningAccumulator = new Dictionary<(Entity, int), float>();
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2020-12-03 17:32:34 +00:00
|
|
|
public AsteroidBelt(AsteroidBeltData data)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
2020-12-03 17:32:34 +00:00
|
|
|
Data = data;
|
2022-07-19 18:55:17 +01:00
|
|
|
Transforms = new float4[data.Asteroids.Length];
|
|
|
|
|
NewTransforms = new float4[data.Asteroids.Length];
|
|
|
|
|
Radius = data.Asteroids.Max(a => a.Distance);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Orbit
|
|
|
|
|
{
|
|
|
|
|
public OrbitData Data { get; }
|
|
|
|
|
public float2 Velocity = float2.zero;
|
|
|
|
|
public float2 Position = float2.zero;
|
|
|
|
|
public float2 PreviousPosition = float2.zero;
|
2025-07-18 00:21:33 +01:00
|
|
|
public float Period;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
public Orbit(PlanetSettings settings, OrbitData data)
|
2020-11-27 22:47:33 +00:00
|
|
|
{
|
|
|
|
|
Data = data;
|
2025-07-18 00:21:33 +01:00
|
|
|
Period = settings.OrbitPeriod.Evaluate(data.Distance);
|
2020-11-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
}
|