Hauling Task
Added a control module which enables drones to haul items from colony to colony Added confirmation dialog for actions which require confirmation, can have fields added to it just like a PropertiesPanel Moving items between inventories now displays confirmation dialog
This commit is contained in:
parent
a901a8e829
commit
991d411c91
24 changed files with 1965 additions and 2193 deletions
|
|
@ -10,6 +10,7 @@ using UnityEngine;
|
|||
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
|
||||
{
|
||||
|
|
|
|||
23
Assets/Scripts/ServerShared/Agents/HaulingTask.cs
Normal file
23
Assets/Scripts/ServerShared/Agents/HaulingTask.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
public class HaulingTask : AgentTask
|
||||
{
|
||||
public override TaskType Type => TaskType.Haul;
|
||||
|
||||
[JsonProperty("origin"), Key(4)]
|
||||
public Guid Origin;
|
||||
|
||||
[JsonProperty("target"), Key(5)]
|
||||
public Guid Target;
|
||||
|
||||
[JsonProperty("itemType"), Key(6)]
|
||||
public Guid ItemType;
|
||||
|
||||
[JsonProperty("quantity"), Key(7)]
|
||||
public int Quantity;
|
||||
}
|
||||
11
Assets/Scripts/ServerShared/Agents/HaulingTask.cs.meta
Normal file
11
Assets/Scripts/ServerShared/Agents/HaulingTask.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 167d373f6e5692b4ab939e83c26b7b57
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -99,6 +99,7 @@ public abstract class PersistentBehaviorData
|
|||
Union(27, typeof(SurveyControllerData)),
|
||||
Union(28, typeof(ResourceScannerData)),
|
||||
Union(29, typeof(WanderControllerData)),
|
||||
Union(30, typeof(HaulingControllerData)),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<BehaviorData>)), JsonObject(MemberSerialization.OptIn)]
|
||||
public abstract class BehaviorData
|
||||
{
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
protected List<SimplifiedZoneData> Path;
|
||||
protected Guid Task;
|
||||
protected bool Moving;
|
||||
protected bool Waiting;
|
||||
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
|
|
@ -50,6 +51,8 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
private MovementPhase _movementPhase;
|
||||
private Action _onFinishMoving;
|
||||
private bool _spaceworthy;
|
||||
private float _waitTime;
|
||||
private Action _onFinishWaiting;
|
||||
|
||||
public ControllerBase(GameContext context, ControllerData data, Entity entity)
|
||||
{
|
||||
|
|
@ -88,6 +91,18 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
GoHome();
|
||||
}
|
||||
}
|
||||
|
||||
if (Waiting)
|
||||
{
|
||||
_waitTime -= delta;
|
||||
if (_waitTime < 0)
|
||||
{
|
||||
Waiting = false;
|
||||
_onFinishWaiting?.Invoke();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Moving)
|
||||
{
|
||||
|
|
@ -165,6 +180,13 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
}));
|
||||
}
|
||||
|
||||
public void Wait(float time, Action onFinish = null)
|
||||
{
|
||||
_waitTime = time;
|
||||
Waiting = true;
|
||||
_onFinishWaiting = onFinish;
|
||||
}
|
||||
|
||||
public void MoveTo(Entity entity, bool matchVelocity = true, Action onFinish = null)
|
||||
{
|
||||
TargetPosition = () => entity.Position;
|
||||
|
|
|
|||
163
Assets/Scripts/ServerShared/Behaviors/HaulingController.cs
Normal file
163
Assets/Scripts/ServerShared/Behaviors/HaulingController.cs
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship), Order(-100)]
|
||||
public class HaulingControllerData : ControllerData
|
||||
{
|
||||
[InspectableField, JsonProperty("targetDistance"), Key(6)]
|
||||
public float DockTime = 2;
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new HaulingController(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class HaulingController : ControllerBase, IBehavior, IPersistentBehavior
|
||||
{
|
||||
public override TaskType TaskType => TaskType.Haul;
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private HaulingControllerData _data;
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private bool _taskStarted;
|
||||
private int _itemsDelivered;
|
||||
private SimpleCommodity _simpleCommodityDelivery;
|
||||
private List<CraftedItemInstance> _craftedItemDelivery = new List<CraftedItemInstance>();
|
||||
|
||||
public HaulingController(GameContext context, HaulingControllerData data, Entity entity, Gear item) : base(context, data, entity)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_entity = entity;
|
||||
_item = item;
|
||||
}
|
||||
|
||||
public new bool Update(float delta)
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
if (haulingTask != null)
|
||||
{
|
||||
if (!_taskStarted)
|
||||
{
|
||||
MoveTo(haulingTask.Zone, OnOriginZoneArrival);
|
||||
_taskStarted = true;
|
||||
}
|
||||
}
|
||||
return base.Update(delta);
|
||||
}
|
||||
|
||||
void OnOriginZoneArrival()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
|
||||
_entity.SetMessage("Entering pickup phase.");
|
||||
|
||||
MoveTo(_context.Cache.Get<Entity>(haulingTask.Origin), true, OnPickup);
|
||||
}
|
||||
|
||||
void OnPickup()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
var target = _context.ZoneEntities[_entity.Zone][haulingTask.Origin] as OrbitalEntity;
|
||||
|
||||
_context.SetParent(_entity, target);
|
||||
var itemData = _context.Cache.Get<ItemData>(haulingTask.ItemType);
|
||||
int quantity = min((int) ((_entity.Capacity - _entity.OccupiedCapacity) / itemData.Size), haulingTask.Quantity - _itemsDelivered);
|
||||
if (itemData is SimpleCommodityData)
|
||||
{
|
||||
var itemMatch = target.Cargo
|
||||
.Select(id => _context.Cache.Get<SimpleCommodity>(id))
|
||||
.FirstOrDefault(ii => ii != null && ii.Data == haulingTask.ItemType);
|
||||
if(itemMatch==null)
|
||||
FinishTask();
|
||||
_simpleCommodityDelivery = _entity.AddCargo(target.RemoveCargo(itemMatch, quantity));
|
||||
}
|
||||
else if (itemData is CraftedItemData)
|
||||
{
|
||||
var itemMatches = target.Cargo
|
||||
.Select(id => _context.Cache.Get<CraftedItemInstance>(id))
|
||||
.Where(ii => ii != null && ii.Data == haulingTask.ItemType);
|
||||
if(!itemMatches.Any())
|
||||
FinishTask();
|
||||
foreach (var match in itemMatches)
|
||||
_craftedItemDelivery.Add(_entity.AddCargo(target.RemoveCargo(match)));
|
||||
}
|
||||
Wait(_data.DockTime, OnItemObtained);
|
||||
}
|
||||
|
||||
void OnItemObtained()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
_entity.SetMessage("Entering delivery phase.");
|
||||
|
||||
var target = _context.Cache.Get<Entity>(haulingTask.Target);
|
||||
|
||||
MoveTo(target.Zone, OnTargetZoneArrival);
|
||||
}
|
||||
|
||||
void OnTargetZoneArrival()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
|
||||
MoveTo(_context.Cache.Get<Entity>(haulingTask.Target), true, OnDelivery);
|
||||
}
|
||||
|
||||
void OnDelivery()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
var target = _context.ZoneEntities[_entity.Zone][haulingTask.Target] as OrbitalEntity;
|
||||
|
||||
_context.SetParent(_entity, target);
|
||||
|
||||
var itemData = _context.Cache.Get<ItemData>(haulingTask.ItemType);
|
||||
if (itemData is SimpleCommodityData)
|
||||
{
|
||||
target.AddCargo(_entity.RemoveCargo(_simpleCommodityDelivery, _simpleCommodityDelivery.Quantity));
|
||||
_itemsDelivered += _simpleCommodityDelivery.Quantity;
|
||||
_simpleCommodityDelivery = null;
|
||||
}
|
||||
else if (itemData is CraftedItemData)
|
||||
{
|
||||
foreach (var item in _craftedItemDelivery)
|
||||
target.AddCargo(_entity.RemoveCargo(item));
|
||||
_itemsDelivered += _craftedItemDelivery.Count;
|
||||
_craftedItemDelivery.Clear();
|
||||
}
|
||||
|
||||
Wait(_data.DockTime, () =>
|
||||
{
|
||||
if(_itemsDelivered == haulingTask.Quantity)
|
||||
FinishTask();
|
||||
else
|
||||
_taskStarted = false;
|
||||
});
|
||||
}
|
||||
|
||||
public PersistentBehaviorData Store()
|
||||
{
|
||||
return new HaulingControllerPersistence
|
||||
{
|
||||
Task = Task
|
||||
};
|
||||
}
|
||||
|
||||
public void Restore(PersistentBehaviorData data)
|
||||
{
|
||||
var towingControllerPersistence = data as HaulingControllerPersistence;
|
||||
Task = towingControllerPersistence.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public class HaulingControllerPersistence : PersistentBehaviorData
|
||||
{
|
||||
[JsonProperty("task"), Key(0)]
|
||||
public Guid Task;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e8c5153aa2579174c901c552bfb495a4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -41,6 +41,7 @@ public interface INamedEntry
|
|||
Union(21, typeof(Mining)),
|
||||
Union(22, typeof(StationTowing)),
|
||||
Union(23, typeof(Survey)),
|
||||
Union(24, typeof(HaulingTask)),
|
||||
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
|
||||
//[Union(21, typeof(ContractData))]
|
||||
//[Union(22, typeof(Station))]
|
||||
|
|
|
|||
|
|
@ -170,6 +170,13 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
|
|||
|
||||
foreach (var hardpoint in Hardpoints)
|
||||
if(hardpoint.Gear!=null)
|
||||
foreach (var behavior in hardpoint.Behaviors)
|
||||
{
|
||||
if(behavior is IPopulationAssignment populationAssignment)
|
||||
PopulationAssignments.Add(populationAssignment);
|
||||
if(behavior is IInitializableBehavior initializableBehavior)
|
||||
initializableBehavior.Initialize();
|
||||
}
|
||||
|
||||
RecalculateMass();
|
||||
}
|
||||
|
|
@ -199,13 +206,6 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
|
|||
})
|
||||
.ToArray();
|
||||
|
||||
foreach (var behavior in hardpoint.Behaviors)
|
||||
{
|
||||
if(behavior is IPopulationAssignment populationAssignment)
|
||||
PopulationAssignments.Add(populationAssignment);
|
||||
if(behavior is IInitializableBehavior initializableBehavior)
|
||||
initializableBehavior.Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
public void Unequip(Hardpoint hardpoint)
|
||||
|
|
@ -550,20 +550,34 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
|
|||
|
||||
public virtual void Update(float delta)
|
||||
{
|
||||
if (!Active)
|
||||
return;
|
||||
foreach (var hardpoint in Hardpoints.Where(hp=>hp.Gear!=null))
|
||||
if (Active)
|
||||
{
|
||||
foreach (var group in hardpoint.BehaviorGroups)
|
||||
foreach (var hardpoint in Hardpoints.Where(hp=>hp.Gear!=null))
|
||||
{
|
||||
foreach (var group in hardpoint.BehaviorGroups)
|
||||
foreach(var behavior in group.Behaviors)
|
||||
if(!behavior.Update(delta))
|
||||
break;
|
||||
|
||||
foreach (var behavior in hardpoint.Behaviors.Where(b => b is IAlwaysUpdatedBehavior).Cast<IAlwaysUpdatedBehavior>())
|
||||
behavior.AlwaysUpdate(delta);
|
||||
foreach (var behavior in hardpoint.Behaviors.Where(b => b is IAlwaysUpdatedBehavior).Cast<IAlwaysUpdatedBehavior>())
|
||||
behavior.AlwaysUpdate(delta);
|
||||
|
||||
if(hardpoint.Gear.ItemData.Performance(Temperature) < .01f)
|
||||
hardpoint.Gear.Durability -= delta;
|
||||
if(hardpoint.Gear.ItemData.Performance(Temperature) < .01f)
|
||||
hardpoint.Gear.Durability -= delta;
|
||||
}
|
||||
|
||||
foreach (var message in Messages.Keys.ToArray())
|
||||
{
|
||||
Messages[message] = Messages[message] - delta;
|
||||
if (Messages[message] < 0)
|
||||
Messages.Remove(message);
|
||||
}
|
||||
|
||||
foreach (var incompleteGear in IncompleteGear.Keys.ToArray())
|
||||
{
|
||||
IncompleteGear[incompleteGear] = IncompleteGear[incompleteGear] - delta;
|
||||
if (IncompleteGear[incompleteGear] < 0) Equip(incompleteGear);
|
||||
}
|
||||
}
|
||||
|
||||
if (Parent != Guid.Empty)
|
||||
|
|
@ -574,79 +588,6 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
|
|||
Temperature = parent.Temperature;
|
||||
}
|
||||
|
||||
// Processing stale item data properly was a pain in my ass, so the shortcut is just persisting and restoring everything
|
||||
// if (Hardpoints.Any(hp => hp.Gear.ItemData != hp.ItemData))
|
||||
// {
|
||||
// OnBeforeSerialize();
|
||||
// OnAfterDeserialize();
|
||||
// }
|
||||
|
||||
foreach (var message in Messages.Keys.ToArray())
|
||||
{
|
||||
Messages[message] = Messages[message] - delta;
|
||||
if (Messages[message] < 0)
|
||||
Messages.Remove(message);
|
||||
}
|
||||
|
||||
foreach (var incompleteGear in IncompleteGear.Keys.ToArray())
|
||||
{
|
||||
IncompleteGear[incompleteGear] = IncompleteGear[incompleteGear] - delta;
|
||||
if (IncompleteGear[incompleteGear] < 0) Equip(incompleteGear);
|
||||
}
|
||||
|
||||
// Here's the PITA way
|
||||
// foreach (var staleItem in GearData
|
||||
// .Where(kvp=>kvp.Key.ItemData!=kvp.Value)
|
||||
// .Select(kvp=>kvp.Key))
|
||||
// {
|
||||
// // Item is stale, there's an updated one in the database cache
|
||||
// //var actualItem = Context.Cache.Get<Gear>(staleItem.ID);
|
||||
// var staleBehaviors = ItemBehaviors[staleItem];
|
||||
// ItemBehaviors.Remove(staleItem);
|
||||
//
|
||||
// // We'll be reloading the item along with its behaviors, some data must be persisted
|
||||
// var persistentData = staleBehaviors
|
||||
// .Where(b => b is IPersistentBehavior)
|
||||
// .Cast<IPersistentBehavior>()
|
||||
// .Select(b => b.Store());
|
||||
//
|
||||
// // Regenerate the behavior list for the updated item
|
||||
// ItemBehaviors[staleItem] = staleItem.ItemData.Behaviors
|
||||
// .Select(bd => bd.CreateInstance(Context, this, staleItem))
|
||||
// .OrderBy(b => b.GetType().GetCustomAttribute<UpdateOrderAttribute>()?.Order ?? 0).ToList();
|
||||
//
|
||||
// foreach(var behavior in ItemBehaviors[staleItem])
|
||||
// behavior.Initialize();
|
||||
//
|
||||
// // Restore state for persisted behaviors
|
||||
// foreach(var persistentBehaviorData in ItemBehaviors[staleItem]
|
||||
// .Where(b => b is IPersistentBehavior)
|
||||
// .Cast<IPersistentBehavior>()
|
||||
// .Zip(persistentData, (behavior, data) => new {behavior, data}))
|
||||
// persistentBehaviorData.behavior.Restore(persistentBehaviorData.data);
|
||||
//
|
||||
// // Regenerate activated behavior bindings
|
||||
// if (Bindings.ContainsKey(staleItem))
|
||||
// {
|
||||
// Bindings.Remove(staleItem);
|
||||
// Bindings[staleItem] = ItemBehaviors[staleItem]
|
||||
// .Where(b => b is IActivatedBehavior)
|
||||
// .Cast<IActivatedBehavior>().ToList();
|
||||
// }
|
||||
//
|
||||
// // Remove axis bindings and overrides for analog behaviors
|
||||
// foreach (var axis in staleBehaviors.Where(b => b is IAnalogBehavior).Cast<IAnalogBehavior>())
|
||||
// {
|
||||
// Axes.Remove(axis);
|
||||
// if (AxisOverrides.ContainsKey(axis))
|
||||
// AxisOverrides.Remove(axis);
|
||||
// }
|
||||
//
|
||||
// // Regenerate axis bindings for analog behaviors
|
||||
// foreach (var axis in ItemBehaviors[staleItem].Where(b => b is IAnalogBehavior).Cast<IAnalogBehavior>())
|
||||
// Axes[axis] = 0;
|
||||
// }
|
||||
|
||||
if (_cargoChanged)
|
||||
{
|
||||
CargoEvent.Change();
|
||||
|
|
|
|||
|
|
@ -709,15 +709,9 @@ public class GameContext
|
|||
|
||||
if (hullData.HullType == HullType.Ship)
|
||||
{
|
||||
var entity = new Ship(this, hull.ID, gear.Select(g => g.ID),
|
||||
cargo.Select(c => c.ID).Concat(simpleCargo.Select(c => c.ID)), zone, corporation)
|
||||
{
|
||||
Name = $"{loadoutData.Name} {Random.NextInt(1,255):X}"
|
||||
};
|
||||
var entity = CreateShip(hull.ID, gear.Select(g => g.ID),
|
||||
cargo.Select(c => c.ID).Concat(simpleCargo.Select(c => c.ID)), zone, corporation, loadoutData.Name);
|
||||
entity.Active = true;
|
||||
Cache.Add(entity);
|
||||
|
||||
ZoneEntities[zone][entity.ID] = entity;
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
|
@ -757,6 +751,19 @@ public class GameContext
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Ship CreateShip(Guid hull, IEnumerable<Guid> gear, IEnumerable<Guid> cargo, Guid zone, Guid corporation, string typeName)
|
||||
{
|
||||
|
||||
var ship = new Ship(this, hull, gear, cargo, zone, corporation)
|
||||
{
|
||||
Name = $"{typeName} {Random.NextInt(1,255):X}"
|
||||
};
|
||||
Cache.Add(ship);
|
||||
|
||||
ZoneEntities[zone][ship.ID] = ship;
|
||||
return ship;
|
||||
}
|
||||
|
||||
public SimpleCommodity CreateInstance(Guid data, int count)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue