Files
wwdpublic/Content.Shared/Station/SharedStationSpawningSystem.cs
MilenVolf 167eee3eb6 Localize ai dataset names (#33608)
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

(cherry picked from commit b7e34aa267dae1069691b77e72172b331857e595)
2025-01-14 02:03:08 +03:00

143 lines
5.3 KiB
C#

using System.Linq;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Inventory;
using Content.Shared.Preferences.Loadouts;
using Content.Shared.Roles;
using Content.Shared.Storage;
using Content.Shared.Storage.EntitySystems;
using Robust.Shared.Collections;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Shared.Station;
public abstract class SharedStationSpawningSystem : EntitySystem
{
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] protected readonly InventorySystem InventorySystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedStorageSystem _storage = default!;
[Dependency] private readonly SharedTransformSystem _xformSystem = default!;
private EntityQuery<HandsComponent> _handsQuery;
private EntityQuery<InventoryComponent> _inventoryQuery;
private EntityQuery<StorageComponent> _storageQuery;
private EntityQuery<TransformComponent> _xformQuery;
public override void Initialize()
{
base.Initialize();
_handsQuery = GetEntityQuery<HandsComponent>();
_inventoryQuery = GetEntityQuery<InventoryComponent>();
_storageQuery = GetEntityQuery<StorageComponent>();
_xformQuery = GetEntityQuery<TransformComponent>();
}
/// <summary>
/// Applies the role's name as applicable to the entity.
/// </summary>
public void EquipRoleName(EntityUid entity, RoleLoadout loadout, RoleLoadoutPrototype roleProto)
{
string? name = null;
if (string.IsNullOrEmpty(name) && PrototypeManager.TryIndex(roleProto.NameDataset, out var nameData))
{
name = Loc.GetString(_random.Pick(nameData.Values));
}
if (!string.IsNullOrEmpty(name))
{
_metadata.SetEntityName(entity, name);
}
}
/// <summary>
/// <see cref="EquipStartingGear(Robust.Shared.GameObjects.EntityUid,System.Nullable{Robust.Shared.Prototypes.ProtoId{Content.Shared.Roles.StartingGearPrototype}},bool)"/>
/// </summary>
public void EquipStartingGear(EntityUid entity, ProtoId<StartingGearPrototype>? startingGear, bool raiseEvent = true)
{
PrototypeManager.TryIndex(startingGear, out var gearProto);
EquipStartingGear(entity, gearProto);
}
/// <summary>
/// Equips starting gear onto the given entity.
/// </summary>
/// <param name="entity">Entity to load out.</param>
/// <param name="startingGear">Starting gear to use.</param>
/// <param name="raiseEvent">Should we raise the event for equipped. Set to false if you will call this manually</param>
public void EquipStartingGear(EntityUid entity, StartingGearPrototype? startingGear, bool raiseEvent = true)
{
if (startingGear == null)
return;
var xform = _xformQuery.GetComponent(entity);
if (InventorySystem.TryGetSlots(entity, out var slotDefinitions))
{
foreach (var slot in slotDefinitions)
{
var equipmentStr = startingGear.GetGear(slot.Name, null);
if (string.IsNullOrEmpty(equipmentStr))
continue;
var equipmentEntity = EntityManager.SpawnEntity(equipmentStr, xform.Coordinates);
InventorySystem.TryEquip(entity, equipmentEntity, slot.Name, true, force:true);
}
}
if (_handsQuery.TryComp(entity, out var handsComponent))
{
var inhand = startingGear.Inhand;
var coords = xform.Coordinates;
foreach (var prototype in inhand)
{
var inhandEntity = EntityManager.SpawnEntity(prototype, coords);
if (_handsSystem.TryGetEmptyHand(entity, out var emptyHand, handsComponent))
{
_handsSystem.TryPickup(entity, inhandEntity, emptyHand, checkActionBlocker: false,
handsComp: handsComponent);
}
}
}
if (startingGear.Storage.Count > 0)
{
var coords = _xformSystem.GetMapCoordinates(entity);
var ents = new ValueList<EntityUid>();
_inventoryQuery.TryComp(entity, out var inventoryComp);
foreach (var (slot, entProtos) in startingGear.Storage)
{
if (entProtos.Count == 0)
continue;
foreach (var ent in entProtos)
{
ents.Add(Spawn(ent, coords));
}
if (inventoryComp != null &&
InventorySystem.TryGetSlotEntity(entity, slot, out var slotEnt, inventoryComponent: inventoryComp) &&
_storageQuery.TryComp(slotEnt, out var storage))
{
foreach (var ent in ents)
{
_storage.Insert(slotEnt.Value, ent, out _, storageComp: storage, playSound: false);
}
}
}
}
if (raiseEvent)
{
var ev = new StartingGearEquippedEvent(entity);
RaiseLocalEvent(entity, ref ev);
}
}
}