Storage system refactor & map init.

* Demonstrated map init working with guns, toolboxes, tool lockers.
* Refactored EntityStorage and ServerStorage to have a common interface.
* EntityStorage no longer uses ServerStorage PURELY for visuals.
  Use an appearance visualizer instead.
This commit is contained in:
Pieter-Jan Briers
2019-05-05 18:52:06 +02:00
parent 030f1f2a57
commit e35d5390db
17 changed files with 384 additions and 950 deletions

View File

@@ -4,6 +4,7 @@ using Content.Server.Interfaces.GameObjects;
using Content.Shared.GameObjects;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
@@ -15,7 +16,7 @@ namespace Content.Server.GameObjects.Components.Interactable
/// <summary>
/// Component that represents a handheld lightsource which can be toggled on and off.
/// </summary>
internal class HandheldLightComponent : Component, IUse, IExamine, IAttackBy
internal class HandheldLightComponent : Component, IUse, IExamine, IAttackBy, IMapInit
{
public const float Wattage = 10;
[ViewVariables] private ContainerSlot _cellContainer;
@@ -76,12 +77,6 @@ namespace Content.Server.GameObjects.Components.Interactable
Owner.TryGetComponent(out _clothingComponent);
_cellContainer =
ContainerManagerComponent.Ensure<ContainerSlot>("flashlight_cell_container", Owner, out var existed);
if (!existed)
{
var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallHyper");
_cellContainer.Insert(cell);
}
}
/// <summary>
@@ -179,5 +174,15 @@ namespace Content.Server.GameObjects.Components.Interactable
component.EjectCell(user);
}
}
void IMapInit.MapInit()
{
if (_cellContainer.ContainedEntity != null)
{
return;
}
var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallHyper");
_cellContainer.Insert(cell);
}
}
}

View File

@@ -8,14 +8,16 @@ using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System.Linq;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Shared.GameObjects.Components.Storage;
using Robust.Server.GameObjects;
namespace Content.Server.GameObjects.Components
{
public class EntityStorageComponent : Component, IAttackHand
public class EntityStorageComponent : Component, IAttackHand, IStorageComponent
{
public override string Name => "EntityStorage";
private ServerStorageComponent StorageComponent;
private int StorageCapacityMax;
private bool IsCollidableWhenOpen;
private Container Contents;
@@ -24,14 +26,7 @@ namespace Content.Server.GameObjects.Components
public override void Initialize()
{
base.Initialize();
Contents = ContainerManagerComponent.Ensure<Container>($"{typeof(EntityStorageComponent).FullName}{Owner.Uid.ToString()}", Owner);
if (!Owner.TryGetComponent(out StorageComponent))
{
StorageComponent = Owner.AddComponent<ServerStorageComponent>();
// TODO: This is a terrible hack.
// Components should not need to be manually initialized in Initialize().
StorageComponent.Initialize();
}
Contents = ContainerManagerComponent.Ensure<Container>(nameof(EntityStorageComponent), Owner);
entityQuery = new IntersectingEntityQuery(Owner);
}
@@ -43,12 +38,8 @@ namespace Content.Server.GameObjects.Components
serializer.DataField(ref IsCollidableWhenOpen, "IsCollidableWhenOpen", false);
}
[ViewVariables(VVAccess.ReadWrite)]
public bool Open
{
get => StorageComponent.Open;
set => StorageComponent.Open = value;
}
[ViewVariables]
public bool Open { get; private set; }
public bool AttackHand(AttackHandEventArgs eventArgs)
{
@@ -67,7 +58,7 @@ namespace Content.Server.GameObjects.Components
{
Open = false;
var entities = Owner.EntityManager.GetEntities(entityQuery);
int count = 0;
var count = 0;
foreach (var entity in entities)
{
if (!AddToContents(entity))
@@ -96,13 +87,19 @@ namespace Content.Server.GameObjects.Components
{
collidableComponent.CollisionEnabled = IsCollidableWhenOpen || !Open;
}
if (Owner.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
{
placeableSurfaceComponent.IsPlaceable = Open;
}
if (Owner.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(StorageVisuals.Open, Open);
}
}
private bool AddToContents(IEntity entity)
private bool AddToContents(IEntity entity)
{
var collidableComponent = Owner.GetComponent<ICollidableComponent>();
if(entity.TryGetComponent<ICollidableComponent>(out var entityCollidableComponent))
@@ -140,10 +137,10 @@ namespace Content.Server.GameObjects.Components
private void EmptyContents()
{
while (Contents.ContainedEntities.Count > 0 )
foreach (var contained in Contents.ContainedEntities.ToArray())
{
var containedEntity = Contents.ContainedEntities.First();
Contents.Remove(containedEntity);
Contents.Remove(contained);
contained.Transform.WorldPosition = Owner.Transform.WorldPosition;
}
}
@@ -154,12 +151,44 @@ namespace Content.Server.GameObjects.Components
switch (message)
{
case RelayMovementEntityMessage msg:
if(msg.Entity.TryGetComponent<HandsComponent>(out var handsComponent))
if (msg.Entity.HasComponent<HandsComponent>())
{
OpenStorage();
}
break;
}
}
public bool Remove(IEntity entity)
{
return Contents.CanRemove(entity);
}
public bool Insert(IEntity entity)
{
// Trying to add while open just dumps it on the ground below us.
if (Open)
{
entity.Transform.WorldPosition = Owner.Transform.WorldPosition;
return true;
}
return Contents.Insert(entity);
}
public bool CanInsert(IEntity entity)
{
if (Open)
{
return true;
}
if (Contents.ContainedEntities.Count >= StorageCapacityMax)
{
return false;
}
return Contents.CanInsert(entity);
}
}
}

View File

@@ -0,0 +1,92 @@
using System;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Server.GameObjects.Components.Items.Storage.Fill
{
internal sealed class ToolLockerFillComponent : Component, IMapInit
{
public override string Name => "ToolLockerFill";
#pragma warning disable 649
[Dependency] private readonly IEntityManager _entityManager;
#pragma warning restore 649
void IMapInit.MapInit()
{
var storage = Owner.GetComponent<IStorageComponent>();
var random = new Random(DateTime.Now.GetHashCode() ^ Owner.Uid.GetHashCode());
void Spawn(string prototype)
{
storage.Insert(_entityManager.SpawnEntity(prototype));
}
if (random.Prob(0.4f))
{
Spawn("HazardVestClothing");
}
if (random.Prob(0.7f))
{
Spawn("FlashlightLantern");
}
if (random.Prob(0.7f))
{
Spawn("Screwdriver");
}
if (random.Prob(0.7f))
{
Spawn("Wrench");
}
if (random.Prob(0.7f))
{
Spawn("Welder");
}
if (random.Prob(0.7f))
{
Spawn("Crowbar");
}
if (random.Prob(0.7f))
{
Spawn("Wirecutter");
}
if (random.Prob(0.2f))
{
Spawn("Multitool");
}
if (random.Prob(0.2f))
{
Spawn("UtilityBeltClothing");
}
if (random.Prob(0.05f))
{
Spawn("YellowGloves");
}
if (random.Prob(0.4f))
{
Spawn("HelmetEngineering");
}
for (var i = 0; i < 3; i++)
{
if (random.Prob(0.3f))
{
Spawn("CableStack");
}
}
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Server.GameObjects.Components.Items.Storage.Fill
{
internal sealed class ToolboxElectricalFillComponent : Component, IMapInit
{
public override string Name => "ToolboxElectricalFill";
#pragma warning disable 649
[Dependency] private readonly IEntityManager _entityManager;
#pragma warning restore 649
void IMapInit.MapInit()
{
var storage = Owner.GetComponent<IStorageComponent>();
var random = new Random(DateTime.Now.GetHashCode() ^ Owner.Uid.GetHashCode());
void Spawn(string prototype)
{
storage.Insert(_entityManager.SpawnEntity(prototype));
}
Spawn("Screwdriver");
Spawn("Crowbar");
Spawn("Wirecutter");
Spawn("CableStack");
Spawn("CableStack");
// 5% chance for a pair of fancy insulated gloves, else just a third cable coil.
Spawn(random.Prob(0.05f) ? "YellowGloves" : "CableStack");
}
}
}

View File

@@ -0,0 +1,11 @@
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Items.Storage
{
public interface IStorageComponent
{
bool Remove(IEntity entity);
bool Insert(IEntity entity);
bool CanInsert(IEntity entity);
}
}

View File

@@ -18,6 +18,7 @@ using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.ViewVariables;
using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Items.Storage;
using Robust.Server.GameObjects.EntitySystemMessages;
namespace Content.Server.GameObjects
@@ -25,7 +26,7 @@ namespace Content.Server.GameObjects
/// <summary>
/// Storage component for containing entities within this one, matches a UI on the client which shows stored entities
/// </summary>
public class ServerStorageComponent : SharedStorageComponent, IAttackBy, IUse, IActivate
public class ServerStorageComponent : SharedStorageComponent, IAttackBy, IUse, IActivate, IStorageComponent
{
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager;
@@ -40,20 +41,6 @@ namespace Content.Server.GameObjects
private int StorageCapacityMax = 10000;
public HashSet<IPlayerSession> SubscribedSessions = new HashSet<IPlayerSession>();
[ViewVariables(VVAccess.ReadWrite)]
public bool Open
{
get => _open;
set
{
if (_open == value)
return;
_open = value;
Dirty();
}
}
public override void Initialize()
{
base.Initialize();
@@ -61,12 +48,6 @@ namespace Content.Server.GameObjects
storage = ContainerManagerComponent.Ensure<Container>("storagebase", Owner);
}
/// <inheritdoc />
public override ComponentState GetComponentState()
{
return new StorageComponentState(_open);
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
@@ -243,7 +224,10 @@ namespace Content.Server.GameObjects
private void UpdateDoorState()
{
Open = SubscribedSessions.Count != 0;
if (Owner.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(StorageVisuals.Open, SubscribedSessions.Count != 0);
}
}
public void HandlePlayerSessionChangeEvent(object obj, SessionStatusEventArgs SSEA)

View File

@@ -3,31 +3,41 @@ using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Weapons.Ranged;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
public class BallisticMagazineComponent : Component
public class BallisticMagazineComponent : Component, IMapInit
{
public override string Name => "BallisticMagazine";
// Stack of loaded bullets.
[ViewVariables]
private readonly Stack<IEntity> _loadedBullets = new Stack<IEntity>();
[ViewVariables]
private string _fillType;
[ViewVariables]
private Container _bulletContainer;
[ViewVariables]
private AppearanceComponent _appearance;
private BallisticMagazineType _magazineType;
private BallisticCaliber _caliber;
private int _capacity;
[ViewVariables]
public BallisticMagazineType MagazineType => _magazineType;
[ViewVariables]
public BallisticCaliber Caliber => _caliber;
[ViewVariables]
public int Capacity => _capacity;
[ViewVariables]
public int CountLoaded => _loadedBullets.Count;
public event Action OnAmmoCountChanged;
@@ -62,18 +72,10 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
_loadedBullets.Push(entity);
}
_updateAppearance();
}
else if (_fillType != null)
{
// Load up bullets from fill.
for (var i = 0; i < Capacity; i++)
{
var bullet = Owner.EntityManager.SpawnEntity(_fillType);
AddBullet(bullet);
}
}
_updateAppearance();
OnAmmoCountChanged?.Invoke();
_appearance.SetData(BallisticMagazineVisuals.AmmoCapacity, Capacity);
}
@@ -113,6 +115,21 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
_appearance.SetData(BallisticMagazineVisuals.AmmoLeft, CountLoaded);
}
void IMapInit.MapInit()
{
if (_fillType == null)
{
return;
}
// Load up bullets from fill.
for (var i = 0; i < Capacity; i++)
{
var bullet = Owner.EntityManager.SpawnEntity(_fillType);
AddBullet(bullet);
}
}
}
public enum BallisticMagazineType

View File

@@ -6,31 +6,43 @@ using Content.Shared.GameObjects.Components.Weapons.Ranged;
using Content.Shared.Interfaces;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
public class BallisticMagazineWeaponComponent : BallisticWeaponComponent, IUse, IAttackBy
public class BallisticMagazineWeaponComponent : BallisticWeaponComponent, IUse, IAttackBy, IMapInit
{
public override string Name => "BallisticMagazineWeapon";
[ViewVariables]
private string _defaultMagazine;
[ViewVariables]
private ContainerSlot _magazineSlot;
private BallisticMagazineType _magazineType;
[ViewVariables]
public BallisticMagazineType MagazineType => _magazineType;
[ViewVariables]
private IEntity Magazine => _magazineSlot.ContainedEntity;
[ViewVariables]
private Random _bulletDropRandom;
[ViewVariables]
private string _magInSound;
[ViewVariables]
private string _magOutSound;
[ViewVariables]
private string _autoEjectSound;
[ViewVariables]
private bool _autoEjectMagazine;
[ViewVariables]
private AppearanceComponent _appearance;
private static readonly Direction[] _randomBulletDirs =
@@ -67,16 +79,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
base.Startup();
_magazineSlot =
ContainerManagerComponent.Ensure<ContainerSlot>("ballistic_gun_magazine", Owner,
out var alreadyExisted);
_magazineSlot = ContainerManagerComponent.Ensure<ContainerSlot>("ballistic_gun_magazine", Owner);
if (!alreadyExisted && _defaultMagazine != null)
{
var magazine = Owner.EntityManager.SpawnEntity(_defaultMagazine);
InsertMagazine(magazine, false);
}
else if (Magazine != null)
if (Magazine != null)
{
// Already got magazine from loading a container.
Magazine.GetComponent<BallisticMagazineComponent>().OnAmmoCountChanged += _magazineAmmoCountChanged;
@@ -262,5 +267,14 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
component.EjectMagazine();
}
}
void IMapInit.MapInit()
{
if (_defaultMagazine != null)
{
var magazine = Owner.EntityManager.SpawnEntity(_defaultMagazine);
InsertMagazine(magazine, false);
}
}
}
}