Restore the stable legacy Unity project

This commit is contained in:
Meta Vens 2026-08-10 14:57:50 +01:00
commit 7006a6b0dd
2070 changed files with 180490 additions and 169303 deletions

View file

@ -7,8 +7,9 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using static CultMath.math;
using cfloat2 = CultMath.float2;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using float2 = Unity.Mathematics.float2;
public class Agent
{
@ -54,27 +55,26 @@ public class Agent
}
}
public void Accelerate(cfloat2 targetVelocity, bool noTurn = false)
public void Accelerate(float2 targetVelocity, bool noTurn = false)
{
var deltaV = targetVelocity - Ship.CultVelocity;
var deltaV = targetVelocity - Ship.Velocity;
var deltaVMag = length(deltaV);
var deltaVDirection = normalize(deltaV);
// If Delta V is above the threshold, direct the ship towards the delta and use only main thrusters
if (!noTurn && deltaVMag > FORWARD_DELTA_THRESHOLD)
{
Ship.CultLookDirectionXZ = deltaVDirection;
Ship.MovementDirection = new cfloat2(0, pow(dot(Ship.CultDirection, deltaVDirection), 2));
Ship.LookDirection = float3(deltaVDirection.x, 0, deltaVDirection.y);
Ship.MovementDirection = float2(0, pow(dot(Ship.Direction, deltaVDirection), 2));
}
// If Delta V is low, direct the ship towards the target and use all thrusters
else if(deltaVMag > THRUST_DELTA_THRESHOLD)
{
var shipDirection = Ship.CultDirection;
var right = AetheriaMath.Rotate(shipDirection, ItemRotation.Clockwise);
Ship.MovementDirection = new cfloat2(dot(right, deltaVDirection), dot(shipDirection, deltaVDirection));
var right = Ship.Direction.Rotate(ItemRotation.Clockwise);
Ship.MovementDirection = float2(dot(right, deltaVDirection), dot(Ship.Direction, deltaVDirection));
}
else
{
Ship.MovementDirection = cfloat2.zero;
Ship.MovementDirection = float2.zero;
}
}
}

View file

@ -2,8 +2,10 @@ using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using static CultMath.math;
using cfloat3 = CultMath.float3;
using static Unity.Mathematics.math;
using float2 = Unity.Mathematics.float2;
using float3 = Unity.Mathematics.float3;
using float2x2 = Unity.Mathematics.float2x2;
public class CombatState : BaseState
{
@ -11,7 +13,7 @@ public class CombatState : BaseState
private float _optimumRange;
private readonly List<(int index, float dps)> _availableGroups = new List<(int index, float dps)>();
private readonly List<LockWeapon> _availableLockingWeapons = new List<LockWeapon>();
public CombatState(Agent agent) : base(agent)
{
SampleDps();
@ -29,9 +31,7 @@ public class CombatState : BaseState
_availableGroups.Clear();
_availableLockingWeapons.Clear();
var shipPosition = _agent.Ship.CultPosition;
var targetPosition = target.CultPosition;
var toTarget = targetPosition - shipPosition;
var toTarget = target.Position - _agent.Ship.Position;
var targetDistance = length(toTarget);
for (var i = 0; i < _agent.Ship.WeaponGroups.Length; i++)
{
@ -66,17 +66,16 @@ public class CombatState : BaseState
}
}
var targetDirection = target.CultDirection;
var targetRight = AetheriaMath.Rotate(targetDirection, ItemRotation.Clockwise);
var targetRight = target.Direction.Rotate(ItemRotation.Clockwise);
var optimumRangeDelta = abs(_optimumRange - targetDistance);
var directionToTarget = normalize(toTarget.xz);
var targetPortAlignment = dot(targetRight, directionToTarget);
var targetForeAlignment = dot(-targetDirection, directionToTarget);
var targetForeAlignment = dot(-target.Direction, directionToTarget);
var forwardness = saturate(optimumRangeDelta / _agent.Settings.AgentMaxForwardDistance) * _agent.Settings.AgentForwardLerp;
if (targetForeAlignment > 0) forwardness = lerp(forwardness, 1, pow(targetPortAlignment, 2));
var movementDirection = normalize(lerp(
AetheriaMath.Rotate(directionToTarget, targetPortAlignment > 0 ? ItemRotation.Clockwise : ItemRotation.CounterClockwise),
directionToTarget.Rotate(targetPortAlignment > 0 ? ItemRotation.Clockwise : ItemRotation.CounterClockwise),
targetDistance > _optimumRange ? directionToTarget : -directionToTarget, forwardness));
if (selectedGroup >= 0)
@ -84,17 +83,18 @@ public class CombatState : BaseState
var testWeapon = _agent.Ship.WeaponGroups[selectedGroup].weapons.First();
if(testWeapon.Velocity > 1)
{
var targetHull = _agent.ItemManager.GetRuntimeItem(target.Hull);
var targetVelocity = new cfloat3(target.CultVelocity.x, 0, target.CultVelocity.y);
var targetHullData = _agent.ItemManager.GetData(target.Hull) as HullData;
var targetVelocity = float3(target.Velocity.x, 0, target.Velocity.y);
var shipVelocity = float3(_agent.Ship.Velocity.x, 0, _agent.Ship.Velocity.y);
var predictedPosition = AetheriaMath.FirstOrderIntercept(
shipPosition,
cfloat3.zero,
_agent.Ship.Position,
float3.zero,
testWeapon.Velocity,
targetPosition,
target.Position,
targetVelocity
);
predictedPosition.y = _agent.Ship.Zone.GetHeight(predictedPosition.xz) + (float)(targetHull?.HullGridOffset ?? 0);
toTarget = normalize(predictedPosition - shipPosition);
predictedPosition.y = _agent.Ship.Zone.GetHeight(predictedPosition.xz) + targetHullData.GridOffset;
toTarget = normalize(predictedPosition - _agent.Ship.Position);
}
var shouldFire = dot(
@ -108,7 +108,7 @@ public class CombatState : BaseState
weapon.Deactivate();
}
}
_agent.Ship.CultLookDirection = toTarget;
_agent.Ship.LookDirection = toTarget;
_agent.Accelerate(movementDirection * _agent.TopSpeed, true);//selectedGroup >= 0);
// Fire charged guns!
@ -148,4 +148,4 @@ public class CombatState : BaseState
}
}
}
}
}

View file

@ -1,9 +1,10 @@
using static CultMath.math;
using cfloat2 = CultMath.float2;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using float2 = Unity.Mathematics.float2;
public abstract class MatchVelocityState : BaseState
{
protected abstract cfloat2 TargetVelocity { get; }
protected abstract float2 TargetVelocity { get; }
protected MatchVelocityState(Agent agent) : base(agent) { }
@ -12,12 +13,12 @@ public abstract class MatchVelocityState : BaseState
_agent.Accelerate(TargetVelocity);
}
public cfloat2 MatchDistanceTime
public float2 MatchDistanceTime
{
get
{
var velocity = length(_agent.Ship.CultVelocity);
var deltaV = TargetVelocity - _agent.Ship.CultVelocity;
var velocity = length(_agent.Ship.Velocity);
var deltaV = TargetVelocity - _agent.Ship.Velocity;
var stoppingTime = length(deltaV) / (_agent.Ship.ForwardThrust / _agent.Ship.Mass);
var stoppingDistance = stoppingTime * (velocity / 2);
@ -25,7 +26,7 @@ public abstract class MatchVelocityState : BaseState
var turnaroundTime = _agent.Ship.TurnTime(deltaV);
var turnaroundDistance = turnaroundTime * velocity;
return new cfloat2(stoppingDistance + turnaroundDistance, stoppingTime + turnaroundTime);
return float2(stoppingDistance + turnaroundDistance, stoppingTime + turnaroundTime);
}
}
}
}

View file

@ -1,12 +1,13 @@
using System;
using static CultMath.math;
using cfloat2 = CultMath.float2;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using float2 = Unity.Mathematics.float2;
public abstract class MoveToState : BaseState
{
private VelocityLimit _velocityLimit;
protected abstract cfloat2 TargetPosition { get; }
protected abstract float2 TargetPosition { get; }
public float Distance { get; private set; }
protected MoveToState(Agent agent) : base(agent)
@ -15,13 +16,13 @@ public abstract class MoveToState : BaseState
public override void Update(float delta)
{
var diff = TargetPosition - _agent.Ship.CultPositionXZ;
var diff = TargetPosition - _agent.Ship.Position.xz;
var dir = normalize(diff);
Distance = length(diff);
// We want to go top speed in the direction of our target
var desiredVelocity = dir * _agent.TopSpeed;
_agent.Ship.CultLookDirectionXZ = dir;
_agent.Ship.LookDirection = float3(dir.x, 0, dir.y);
_agent.Accelerate(desiredVelocity);
}
}
@ -29,15 +30,15 @@ public abstract class MoveToState : BaseState
public class MoveToEntityState : MoveToState
{
public Entity TargetEntity { get; set; }
protected override cfloat2 TargetPosition => TargetEntity != null ? TargetEntity.CultPositionXZ : cfloat2.zero;
protected override float2 TargetPosition => TargetEntity?.Position.xz ?? float2.zero;
public MoveToEntityState(Agent agent) : base(agent) { }
}
public class MoveToOrbitState : MoveToState
{
public string OrbitKey { get; set; } = "";
public Guid Orbit { get; set; }
public MoveToOrbitState(Agent agent) : base(agent) { }
protected override cfloat2 TargetPosition => AetheriaMath.ToCult(_agent.Ship.Zone.GetOrbitPosition(OrbitKey));
protected override float2 TargetPosition => _agent.Ship.Zone.GetOrbitPosition(Orbit);
}

View file

@ -1,16 +1,32 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
public abstract class AgentTask
using System;
using System.Collections;
using System.Collections.Generic;
using JsonKnownTypes;
using MessagePack;
using Newtonsoft.Json;
[MessagePackObject,
Union(0, typeof(StationTowing)),
Union(1, typeof(Mining)),
Union(2, typeof(Survey)),
Union(3, typeof(HaulingTask)),
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
public abstract class AgentTask : DatabaseEntry
{
[JsonProperty("priority"), Key(1)]
public int Priority;
public string ZoneKey = "";
[JsonProperty("zone"), Key(2)]
public Guid Zone;
[JsonProperty("reserved"), Key(3)]
public bool Reserved;
public abstract TaskType Type { get; }
[IgnoreMember] public abstract TaskType Type { get; }
}
public enum TaskType

View file

@ -1,16 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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;
using System.Collections;
using System.Collections.Generic;
using MessagePack;
using Newtonsoft.Json;
public class HaulingTask : AgentTask
{
public override TaskType Type => TaskType.Haul;
[JsonProperty("origin"), Key(4)]
public Entity Origin;
[JsonProperty("target"), Key(5)]
public Entity Target;
public string ItemKey;
[JsonProperty("itemType"), Key(6)]
public Guid ItemType;
[JsonProperty("quantity"), Key(7)]
public int Quantity;
}

View file

@ -1,10 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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;
using System.Collections;
using System.Collections.Generic;
using JsonKnownTypes;
using MessagePack;
using Newtonsoft.Json;
[MessagePackObject,
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
public class Mining : AgentTask
{
public override TaskType Type => TaskType.Mine;
public string AsteroidBeltBodyKey = "";
}
[IgnoreMember] public override TaskType Type => TaskType.Mine;
[JsonProperty("asteroids"), Key(4)]
public Guid Asteroids;
}

View file

@ -3,13 +3,13 @@ using System;
public class PatrolOrbitsTask : AgentTask
{
public override TaskType Type => TaskType.Defend;
public string[] Circuit = Array.Empty<string>();
public Guid[] Circuit;
}
public class PatrolOrbitsState : BaseState
{
public PatrolOrbitsTask Task;
public string CurrentTarget
public Guid CurrentTarget
{
get => Task.Circuit[_currentTargetIndex];
}
@ -25,7 +25,7 @@ public class PatrolOrbitsState : BaseState
var patrolMoveState = new MoveToOrbitState(agent);
Transitions.Add(new StateTransition(patrolMoveState,
() => true,
() => patrolMoveState.OrbitKey = CurrentTarget));
() => patrolMoveState.Orbit = CurrentTarget));
patrolMoveState.Transitions.Add(new StateTransition(this, () => patrolMoveState.Distance < 10, NextTarget));
}
}
}

View file

@ -1,16 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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;
using System.Collections;
using System.Collections.Generic;
using JsonKnownTypes;
using MessagePack;
using Newtonsoft.Json;
[MessagePackObject,
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
public class StationTowing : AgentTask
{
public override TaskType Type => TaskType.Tow;
[IgnoreMember] public override TaskType Type => TaskType.Tow;
[JsonProperty("station"), Key(4)]
public OrbitalEntity Station;
public string OrbitParentKey = "";
[JsonProperty("orbitParent"), Key(5)]
public Guid OrbitParent;
[JsonProperty("orbitDistance"), Key(6)]
public float OrbitDistance;
}

View file

@ -1,12 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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;
using System.Collections;
using System.Collections.Generic;
using JsonKnownTypes;
using MessagePack;
using Newtonsoft.Json;
[MessagePackObject,
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
public class Survey : AgentTask
{
public override TaskType Type => TaskType.Explore;
public List<string> PlanetBodyKeys;
}
[IgnoreMember] public override TaskType Type => TaskType.Explore;
[JsonProperty("station"), Key(4)]
public List<Guid> Planets;
}