Files
tbd-station-14/Content.Server/GameObjects/Components/Weapon/Ranged/Ammunition/AmmoBoxComponent.cs
DrSmugleaf 4b4e83d2bf Add changing the amount of hands on the GUI depending on your body parts (#1406)
* Multiple hands in gui first pass

* Remove IHandsComponent interface

* Create hand class and more hand textures

* Refactor ServerHandsComponent to use a single list of hands

* Seal SharedHand

* Fix picked up items not showing on top of the hand buttons

* Remove HandsGui buttons and panels dictionaries

* Fix items in hands rendering

* Fix wrong hand container comparison

* Fix not updating the location of duplicate hands

* Change ClientHandsComponent to use a SortedList instead of a dictionary

* More merge conflict fixes

* Change SortedList to List

* Fix hand button order

* Add item tooltip for more than 2 hands and updating when removing hands

* Add add hand and remove hand command

* Merge conflict fixes

* Remove nullable reference type from ContainerSlot

* Fix texture errors

* Fix error when reaching 0 hands

* Fix error when swapping hands with no hands

* Merged remove hand methods

* Fix item panel texture errors

* Merge conflict fixes

* Fix addhand and removehand command descriptions

* Add properly displaying tooltips for 2 hands

* Make hand indexes and locations consistent across the client and server

* Add dropping held entity if a hand is removed

* Change hand location to be calculated by index

* Made different hand gui updates more consistent

* Remove human body yml testing changes

* Sanitize addhand and removehand commands

* Merge conflict fixes

* Remove testing changes

* Revert body system changes

* Add missing imports

* Remove obsolete hands parameter in yml files

* Fix broken import

* Fix startup error and adding and removing hands on the same tick

* Make hand container id use an uint

In case someone gets more than 2 billion hands

* Rename hand component files

* Make hands state use an array
2020-07-25 15:11:16 +02:00

221 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Weapons.Ranged.Barrels;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
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.Localization;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition
{
[RegisterComponent]
public sealed class AmmoBoxComponent : Component, IInteractUsing, IUse, IInteractHand, IMapInit
{
public override string Name => "AmmoBox";
private BallisticCaliber _caliber;
public int Capacity => _capacity;
private int _capacity;
public int AmmoLeft => _spawnedAmmo.Count + _unspawnedCount;
private Stack<IEntity> _spawnedAmmo;
private Container _ammoContainer;
private int _unspawnedCount;
private string _fillPrototype;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _caliber, "caliber", BallisticCaliber.Unspecified);
serializer.DataField(ref _capacity, "capacity", 30);
serializer.DataField(ref _fillPrototype, "fillPrototype", null);
_spawnedAmmo = new Stack<IEntity>(_capacity);
}
public override void Initialize()
{
base.Initialize();
_ammoContainer = ContainerManagerComponent.Ensure<Container>($"{Name}-container", Owner, out var existing);
if (existing)
{
foreach (var entity in _ammoContainer.ContainedEntities)
{
_unspawnedCount--;
_spawnedAmmo.Push(entity);
_ammoContainer.Insert(entity);
}
}
}
void IMapInit.MapInit()
{
_unspawnedCount += _capacity;
UpdateAppearance();
}
private void UpdateAppearance()
{
if (Owner.TryGetComponent(out AppearanceComponent appearanceComponent))
{
appearanceComponent.SetData(MagazineBarrelVisuals.MagLoaded, true);
appearanceComponent.SetData(AmmoVisuals.AmmoCount, AmmoLeft);
appearanceComponent.SetData(AmmoVisuals.AmmoMax, _capacity);
}
}
public IEntity TakeAmmo()
{
if (_spawnedAmmo.TryPop(out IEntity ammo))
{
_ammoContainer.Remove(ammo);
return ammo;
}
if (_unspawnedCount > 0)
{
ammo = Owner.EntityManager.SpawnEntity(_fillPrototype, Owner.Transform.GridPosition);
_unspawnedCount--;
}
return ammo;
}
public bool TryInsertAmmo(IEntity user, IEntity entity)
{
if (!entity.TryGetComponent(out AmmoComponent ammoComponent))
{
return false;
}
if (ammoComponent.Caliber != _caliber)
{
Owner.PopupMessage(user, Loc.GetString("Wrong caliber"));
return false;
}
if (AmmoLeft >= Capacity)
{
Owner.PopupMessage(user, Loc.GetString("No room"));
return false;
}
_spawnedAmmo.Push(entity);
_ammoContainer.Insert(entity);
UpdateAppearance();
return true;
}
bool IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (eventArgs.Using.HasComponent<AmmoComponent>())
{
return TryInsertAmmo(eventArgs.User, eventArgs.Using);
}
if (eventArgs.Using.TryGetComponent(out RangedMagazineComponent rangedMagazine))
{
for (var i = 0; i < Math.Max(10, rangedMagazine.ShotsLeft); i++)
{
var ammo = rangedMagazine.TakeAmmo();
if (!TryInsertAmmo(eventArgs.User, ammo))
{
rangedMagazine.TryInsertAmmo(eventArgs.User, ammo);
return true;
}
}
return true;
}
return false;
}
private bool TryUse(IEntity user)
{
if (!user.TryGetComponent(out HandsComponent handsComponent))
{
return false;
}
var ammo = TakeAmmo();
var itemComponent = ammo.GetComponent<ItemComponent>();
if (!handsComponent.CanPutInHand(itemComponent))
{
TryInsertAmmo(user, ammo);
return false;
}
handsComponent.PutInHand(itemComponent);
UpdateAppearance();
return true;
}
private void EjectContents(int count)
{
var ejectCount = Math.Min(count, Capacity);
var ejectAmmo = new List<IEntity>(ejectCount);
for (var i = 0; i < Math.Min(count, Capacity); i++)
{
var ammo = TakeAmmo();
if (ammo == null)
{
break;
}
ejectAmmo.Add(ammo);
}
ServerRangedBarrelComponent.EjectCasings(ejectAmmo);
UpdateAppearance();
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
{
return TryUse(eventArgs.User);
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
return TryUse(eventArgs.User);
}
// So if you have 200 rounds in a box and that suddenly creates 200 entities you're not having a fun time
[Verb]
private sealed class DumpVerb : Verb<AmmoBoxComponent>
{
protected override void GetData(IEntity user, AmmoBoxComponent component, VerbData data)
{
if (!ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Text = Loc.GetString("Dump 10");
data.Visibility = component.AmmoLeft > 0 ? VerbVisibility.Visible : VerbVisibility.Disabled;
}
protected override void Activate(IEntity user, AmmoBoxComponent component)
{
component.EjectContents(10);
}
}
}
}