2020-08-22 22:29:20 +02:00
|
|
|
using System;
|
2020-08-18 14:39:08 +02:00
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Hands.Components;
|
|
|
|
|
using Content.Server.Items;
|
|
|
|
|
using Content.Server.Weapon.Ranged.Barrels.Components;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Shared.Popups;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Power;
|
2021-03-01 15:24:46 -08:00
|
|
|
using Robust.Shared.Containers;
|
2020-06-28 09:23:26 -06:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.Localization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-06-28 09:23:26 -06:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Power.Components
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
[ComponentReference(typeof(IActivate))]
|
|
|
|
|
[ComponentReference(typeof(IInteractUsing))]
|
|
|
|
|
public abstract class BaseCharger : Component, IActivate, IInteractUsing
|
|
|
|
|
{
|
|
|
|
|
[ViewVariables]
|
2020-08-22 22:29:20 +02:00
|
|
|
private BatteryComponent? _heldBattery;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-10-05 14:29:03 +11:00
|
|
|
public ContainerSlot Container = default!;
|
|
|
|
|
|
|
|
|
|
public bool HasCell => Container.ContainedEntity != null;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
private CellChargerStatus _status;
|
|
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("chargeRate")]
|
|
|
|
|
private int _chargeRate = 100;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("transferEfficiency")]
|
|
|
|
|
private float _transferEfficiency = 0.85f;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
Owner.EnsureComponent<ApcPowerReceiverComponent>();
|
2021-10-05 14:29:03 +11:00
|
|
|
Container = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-powerCellContainer");
|
2020-06-28 09:23:26 -06:00
|
|
|
// Default state in the visualizer is OFF, so when this gets powered on during initialization it will generally show empty
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 18:10:40 +02:00
|
|
|
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
|
2021-01-03 09:13:01 -06:00
|
|
|
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning disable 618
|
2021-01-03 09:13:01 -06:00
|
|
|
base.HandleMessage(message, component);
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning restore 618
|
2021-01-03 09:13:01 -06:00
|
|
|
switch (message)
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
2021-01-03 09:13:01 -06:00
|
|
|
case PowerChangedMessage powerChanged:
|
|
|
|
|
PowerUpdate(powerChanged);
|
|
|
|
|
break;
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
2021-01-03 09:13:01 -06:00
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnRemove()
|
2021-01-03 09:13:01 -06:00
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
_heldBattery = null;
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
base.OnRemove();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 14:39:08 +02:00
|
|
|
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
var result = TryInsertItem(eventArgs.Using);
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
eventArgs.User.PopupMessage(Owner, Loc.GetString("base-charger-on-interact-using-fail"));
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IActivate.Activate(ActivateEventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
RemoveItem(eventArgs.User);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This will remove the item directly into the user's hand / floor
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="user"></param>
|
2021-10-05 14:29:03 +11:00
|
|
|
public void RemoveItem(IEntity user)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
var heldItem = Container.ContainedEntity;
|
2020-06-28 09:23:26 -06:00
|
|
|
if (heldItem == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
Container.Remove(heldItem);
|
2020-06-28 09:23:26 -06:00
|
|
|
_heldBattery = null;
|
2020-08-22 22:29:20 +02:00
|
|
|
if (user.TryGetComponent(out HandsComponent? handsComponent))
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
handsComponent.PutInHandOrDrop(heldItem.GetComponent<ItemComponent>());
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
if (heldItem.TryGetComponent(out ServerBatteryBarrelComponent? batteryBarrelComponent))
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
batteryBarrelComponent.UpdateAppearance();
|
|
|
|
|
}
|
2020-07-06 14:27:03 -07:00
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
UpdateStatus();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 09:13:01 -06:00
|
|
|
private void PowerUpdate(PowerChangedMessage eventArgs)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
UpdateStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CellChargerStatus GetStatus()
|
|
|
|
|
{
|
2021-07-04 18:11:52 +02:00
|
|
|
if (Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) &&
|
2020-08-22 22:29:20 +02:00
|
|
|
!receiver.Powered)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return CellChargerStatus.Off;
|
|
|
|
|
}
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!HasCell)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return CellChargerStatus.Empty;
|
|
|
|
|
}
|
|
|
|
|
if (_heldBattery != null && Math.Abs(_heldBattery.MaxCharge - _heldBattery.CurrentCharge) < 0.01)
|
|
|
|
|
{
|
|
|
|
|
return CellChargerStatus.Charged;
|
|
|
|
|
}
|
|
|
|
|
return CellChargerStatus.Charging;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
public bool TryInsertItem(IEntity entity)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!IsEntityCompatible(entity) || HasCell)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!Container.Insert(entity))
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_heldBattery = GetBatteryFrom(entity);
|
|
|
|
|
UpdateStatus();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If the supplied entity should fit into the charger.
|
|
|
|
|
/// </summary>
|
2021-10-05 14:29:03 +11:00
|
|
|
public abstract bool IsEntityCompatible(IEntity entity);
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2021-02-16 01:42:48 -07:00
|
|
|
protected abstract BatteryComponent? GetBatteryFrom(IEntity entity);
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
private void UpdateStatus()
|
|
|
|
|
{
|
|
|
|
|
// Not called UpdateAppearance just because it messes with the load
|
|
|
|
|
var status = GetStatus();
|
2020-08-22 22:29:20 +02:00
|
|
|
if (_status == status ||
|
2021-07-04 18:11:52 +02:00
|
|
|
!Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver))
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
_status = status;
|
2020-08-22 22:29:20 +02:00
|
|
|
Owner.TryGetComponent(out AppearanceComponent? appearance);
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
switch (_status)
|
|
|
|
|
{
|
|
|
|
|
// Update load just in case
|
|
|
|
|
case CellChargerStatus.Off:
|
2020-08-22 22:29:20 +02:00
|
|
|
receiver.Load = 0;
|
|
|
|
|
appearance?.SetData(CellVisual.Light, CellChargerStatus.Off);
|
2020-06-28 09:23:26 -06:00
|
|
|
break;
|
|
|
|
|
case CellChargerStatus.Empty:
|
2020-08-22 22:29:20 +02:00
|
|
|
receiver.Load = 0;
|
|
|
|
|
appearance?.SetData(CellVisual.Light, CellChargerStatus.Empty);
|
2020-06-28 09:23:26 -06:00
|
|
|
break;
|
|
|
|
|
case CellChargerStatus.Charging:
|
2020-08-22 22:29:20 +02:00
|
|
|
receiver.Load = (int) (_chargeRate / _transferEfficiency);
|
|
|
|
|
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charging);
|
2020-06-28 09:23:26 -06:00
|
|
|
break;
|
|
|
|
|
case CellChargerStatus.Charged:
|
2020-08-22 22:29:20 +02:00
|
|
|
receiver.Load = 0;
|
|
|
|
|
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charged);
|
2020-06-28 09:23:26 -06:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
appearance?.SetData(CellVisual.Occupied, HasCell);
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnUpdate(float frameTime) //todo: make single system for this
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (_status == CellChargerStatus.Empty || _status == CellChargerStatus.Charged || !HasCell)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
TransferPower(frameTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TransferPower(float frameTime)
|
|
|
|
|
{
|
2021-07-04 18:11:52 +02:00
|
|
|
if (Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) &&
|
2020-08-22 22:29:20 +02:00
|
|
|
!receiver.Powered)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
|
|
|
|
if (_heldBattery == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
_heldBattery.CurrentCharge += _chargeRate * frameTime;
|
|
|
|
|
// Just so the sprite won't be set to 99.99999% visibility
|
|
|
|
|
if (_heldBattery.MaxCharge - _heldBattery.CurrentCharge < 0.01)
|
|
|
|
|
{
|
|
|
|
|
_heldBattery.CurrentCharge = _heldBattery.MaxCharge;
|
|
|
|
|
}
|
|
|
|
|
UpdateStatus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|