Skip to content

Commit

Permalink
Add ability for photocopier to copy multiple papers from a folder
Browse files Browse the repository at this point in the history
  • Loading branch information
stalengd committed Sep 20, 2024
1 parent 2b143d6 commit 17a6c17
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
12 changes: 6 additions & 6 deletions Content.Server/SS220/Photocopier/PhotocopierSystem.Copying.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,20 @@ public void FormToDataToCopy(
}

/// <summary>
/// Spawns a copy of paper using data cached in PhotocopierComponent.DataToCopy and PhotocopierComponent.MetaDataToCopy.
/// Spawns a copy of paper using data cached in <see cref="PhotocopierComponent.DocumentsToCopy"/>
/// </summary>
private void SpawnCopyFromPhotocopier(EntityUid uid, PhotocopierComponent? component = null)
private void SpawnCopyFromPhotocopier(EntityUid uid, int documentIndex, PhotocopierComponent? component = null)
{
if (!Resolve(uid, ref component))
return;

var printout = component.DataToCopy;
if (printout is null)
if (documentIndex < 0 || documentIndex >= component.DocumentsToCopy.Count)
{
_sawmill.Error("Entity " + uid + " tried to spawn a copy of paper, but DataToCopy was null.");
_sawmill.Error($"Entity {uid} tried to spawn a copy of paper, but document index {documentIndex} is out of range, total documents: {component.DocumentsToCopy.Count}.");
return;
}
var document = component.DocumentsToCopy[documentIndex];

SpawnCopy(Transform(uid).Coordinates, component.MetaDataToCopy, printout);
SpawnCopy(Transform(uid).Coordinates, document.MetaData, document.Data);
}
}
72 changes: 55 additions & 17 deletions Content.Server/SS220/Photocopier/PhotocopierSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public sealed partial class PhotocopierSystem : EntitySystem
[Dependency] private readonly ChatSystem _chat = default!;
[Dependency] private readonly EntityManager _entityManager = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;

private FormManager? _specificFormManager;
private readonly ISawmill _sawmill = Logger.GetSawmill("photocopier");
Expand Down Expand Up @@ -233,7 +234,8 @@ private void OnPrintButtonPressed(EntityUid uid, PhotocopierComponent component,
return;

FormToDataToCopy(formToCopy, out var dataToCopy, out var metaDataToCopy);
StartPrinting(uid, component, metaDataToCopy, dataToCopy, PhotocopierState.Copying, args.Amount);
QueueDocument(component, new(dataToCopy, metaDataToCopy));
StartPrinting(uid, component, PhotocopierState.Copying, args.Amount);
}

private void OnStopButtonPressed(EntityUid uid, PhotocopierComponent component, PhotocopierStopMessage args)
Expand Down Expand Up @@ -347,7 +349,8 @@ private void TryQueueCopyPhysicalButt(
var buttScanData = new ButtScanPhotocopiedData() { ButtTexturePath = speciesPrototype.ButtScanTexture };
dataToCopy.Add(typeof(ButtScanComponent), buttScanData);

if (StartPrinting(uid, component, metaDataToCopy, dataToCopy, PhotocopierState.Copying, amount))
QueueDocument(component, new(dataToCopy, metaDataToCopy));
if (StartPrinting(uid, component, PhotocopierState.Copying, amount))
{
component.IsCopyingPhysicalButt = true;
component.ButtSpecies = humanoidAppearance.Species;
Expand All @@ -362,15 +365,38 @@ private bool TryQueueCopySlot(EntityUid uid, PhotocopierComponent component, int
if (component.PaperSlot.Item is not { } copyEntity)
return false;

if (!TryGetPhotocopyableMetaData(copyEntity, out var metaData))
if ((!TryComp<ContainerManagerComponent>(copyEntity, out var containerManager) ||
!TryQueueCopyContainer(component, containerManager, copyEntity)) &&
!TryQueueSingleDocumentEntity(component, copyEntity))
{
return false;
}

StartPrinting(uid, component, PhotocopierState.Copying, amount);
return true;
}

private bool TryQueueCopyContainer(PhotocopierComponent photocopier, ContainerManagerComponent containerManager, EntityUid copyEntity)
{
var isAny = false;
foreach (var container in _containerSystem.GetAllContainers(copyEntity, containerManager))
{
foreach (var entity in container.ContainedEntities)
{
isAny |= TryQueueSingleDocumentEntity(photocopier, entity);
}
}
return isAny;
}

private bool TryQueueSingleDocumentEntity(PhotocopierComponent photocopier, EntityUid copyEntity)
{
if (!TryGetPhotocopyableMetaData(copyEntity, out var metaData))
return false;
var dataToCopy = GetDataToCopyFromEntity(copyEntity);
if (dataToCopy.Count == 0)
return false;

StartPrinting(uid, component, metaData, dataToCopy, PhotocopierState.Copying, amount);

QueueDocument(photocopier, new(dataToCopy, metaData));
return true;
}

Expand All @@ -390,19 +416,22 @@ private void StopPrinting(EntityUid uid, PhotocopierComponent component, bool up
}
}

private void QueueDocument(PhotocopierComponent component, PrintableDocumentData document)
{
component.DocumentsToCopy.Add(document);
}

private bool StartPrinting(
EntityUid uid,
PhotocopierComponent component,
PhotocopyableMetaData? metaData,
Dictionary<Type, IPhotocopiedComponentData>? dataToCopy,
PhotocopierState state,
int amount)
{
if (amount <= 0)
return false;
if (component.DocumentsToCopy.Count == 0)
return false;

component.DataToCopy = dataToCopy;
component.MetaDataToCopy = metaData;
component.State = state;
component.CopiesQueued = Math.Clamp(amount, 0, component.MaxQueueLength);

Expand All @@ -419,8 +448,9 @@ private void ResetState(EntityUid uid, PhotocopierComponent component)
{
component.CopiesQueued = 0;
component.PrintingTimeRemaining = 0;
component.DataToCopy = null;
component.MetaDataToCopy = null;
component.CurrentDocumentIndex = 0;
component.CurrentDocumentCopyIndex = 0;
component.DocumentsToCopy.Clear();
component.ButtSpecies = null;
component.State = PhotocopierState.Idle;
component.IsCopyingPhysicalButt = false;
Expand Down Expand Up @@ -466,13 +496,18 @@ private void ProcessPrinting(EntityUid uid, float frameTime, PhotocopierComponen
if (!isPrinted)
return;

SpawnCopyFromPhotocopier(uid, component);
SpawnCopyFromPhotocopier(uid, component.CurrentDocumentIndex, component);

tonerCartridge.Charges--;
component.CopiesQueued--;
component.CurrentDocumentCopyIndex++;

if (component.CopiesQueued <= 0)
ResetState(uid, component); //Reset the rest of the fields
if (component.CurrentDocumentCopyIndex >= component.CopiesQueued)
{
component.CurrentDocumentIndex++;
component.CurrentDocumentCopyIndex = 0;
if (component.CurrentDocumentIndex >= component.DocumentsToCopy.Count)
ResetState(uid, component); //Reset the rest of the fields
}

UpdateUserInterface(uid, component);
TryUpdateVisualState(uid, component);
Expand Down Expand Up @@ -558,10 +593,13 @@ private void UpdateUserInterface(EntityUid uid, PhotocopierComponent? component
var isPaperInserted = component.PaperSlot.Item is not null;
var assIsOnScanner = IsHumanoidOnTop(component);

var totalCount = component.CopiesQueued * component.DocumentsToCopy.Count;
var printedCount = component.CurrentDocumentIndex * component.CopiesQueued + component.CurrentDocumentCopyIndex;
var remainingCount = totalCount - printedCount;
var state = new PhotocopierUiState(
component.PaperSlot.Locked,
isPaperInserted,
component.CopiesQueued,
remainingCount,
component.FormCollections,
tonerAvailable,
tonerCapacity,
Expand Down
37 changes: 26 additions & 11 deletions Content.Shared/SS220/Photocopier/PhotocopierComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public sealed partial class PhotocopierComponent : Component
};

/// <summary>
/// Contains an item to be copied, assumes it's paper
/// Contains an item to be copied, assumes it's paper or container of papers
/// </summary>
[DataField("paperSlot", required: true)]
public ItemSlot PaperSlot = new();
Expand Down Expand Up @@ -143,17 +143,10 @@ public int MaxQueueLength
public bool SusFormsUnlocked = false;

/// <summary>
/// Contains fields of components that will be copied.
/// Is applied to a new entity that is created as a result of photocopying.
/// Currently queued documents to be copied.
/// </summary>
[ViewVariables]
public Dictionary<Type, IPhotocopiedComponentData>? DataToCopy;

/// <summary>
/// Contains metadata that will be copied.
/// Is applied to a new entity that is created as a result of photocopying.
/// </summary>
public PhotocopyableMetaData? MetaDataToCopy;
public List<PrintableDocumentData> DocumentsToCopy = new();

/// <summary>
/// An audio stream of printing sound.
Expand All @@ -175,15 +168,37 @@ public int MaxQueueLength
public float PrintingTimeRemaining;

/// <summary>
/// Remaining amount of copies to print
/// Total amount of copies to print
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public int CopiesQueued;

[ViewVariables(VVAccess.ReadOnly)]
public int CurrentDocumentIndex;

[ViewVariables(VVAccess.ReadOnly)]
public int CurrentDocumentCopyIndex;

[ViewVariables(VVAccess.ReadOnly)]
public bool IsCopyingPhysicalButt;

[ViewVariables(VVAccess.ReadOnly)]
public float? ManualButtBurnAnimationRemainingTime;
}

public struct PrintableDocumentData(Dictionary<Type, IPhotocopiedComponentData> data, PhotocopyableMetaData metaData)
{
/// <summary>
/// Contains fields of components that will be copied.
/// Is applied to a new entity that is created as a result of photocopying.
/// </summary>
[ViewVariables]
public Dictionary<Type, IPhotocopiedComponentData> Data = data;

/// <summary>
/// Contains metadata that will be copied.
/// Is applied to a new entity that is created as a result of photocopying.
/// </summary>
public PhotocopyableMetaData MetaData = metaData;
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
- FaxableObject
- Paper
- ButtScan
tags:
- Folder
tonerSlot:
insertSound: /Audio/Machines/screwdriveropen.ogg
ejectSound: /Audio/Machines/screwdriverclose.ogg
Expand Down

0 comments on commit 17a6c17

Please sign in to comment.