Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI #309

Merged
merged 5 commits into from
Nov 11, 2023
Merged

AI #309

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Content.Client/Backmen/StationAI/Systems/StationAISystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Content.Shared.Backmen.EntityHealthBar;
using Content.Shared.Backmen.StationAI.Events;

namespace Content.Client.Backmen.StationAI;

public sealed class StationAISystem : EntitySystem
{
[Dependency] private readonly IEntityManager _entityManager = default!;

public override void Initialize()
{
base.Initialize();

SubscribeNetworkEvent<NetworkedAIHealthOverlayEvent>(OnHealthOverlayEvent);
}

private void OnHealthOverlayEvent(NetworkedAIHealthOverlayEvent args)
{
var uid = GetEntity(args.Performer);

if (!_entityManager.TryGetComponent<ShowHealthBarsComponent>(uid, out var health))
{
health = _entityManager.AddComponent<ShowHealthBarsComponent>(uid);
}
else
{
_entityManager.RemoveComponent<ShowHealthBarsComponent>(uid);
}
}
}
14 changes: 14 additions & 0 deletions Content.Client/Backmen/StationAI/UI/AICameraList.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
MinWidth="450" MinHeight="350" Title="{Loc ai-warp-menu-title}">
<BoxContainer Name="Box" Orientation="Vertical" Margin="5">
<BoxContainer Orientation="Horizontal" Margin="0 5 0 5">
<LineEdit Name="SearchBar" PlaceHolder="{Loc 'ai-warp-menu-search-placeholder'}" HorizontalExpand="True" Margin="0 0 2 0"/>
<Button Name="Refresh" Text="{Loc 'ai-warp-menu-refresh'}" Margin="2 0 0 0"/>
</BoxContainer>

<Label Name="Text" Text="{Loc 'ai-warp-menu-no-cameras'}" />
<TabContainer Name="SubnetList" VerticalExpand="True" Margin="0 5" />
</BoxContainer>
</controls:FancyWindow>
123 changes: 123 additions & 0 deletions Content.Client/Backmen/StationAI/UI/AICameraList.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Linq;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;
using Content.Shared.Backmen.StationAI;
using Robust.Client.UserInterface.Controls;
using Content.Client.UserInterface.Controls;

namespace Content.Client.Backmen.StationAI.UI;

[GenerateTypedNameReferences]
public sealed partial class AICameraList : FancyWindow
{
private List<EntityUid> _cameras = new();
public event Action? TryUpdateCameraList;
public event Action<EntityUid>? WarpToCamera;

public AICameraList()
{
RobustXamlLoader.Load(this);

SearchBar.OnTextChanged += (_) => FillCameraList(SearchBar.Text);
Refresh.OnPressed += (_) => UpdateCameraList();
}

private void ItemSelected(ItemList.ItemListSelectedEventArgs obj)
{
var meta = obj.ItemList[obj.ItemIndex].Metadata;
if (meta == null ||
meta is not AICameraComponent camera ||
camera.Enabled == false)
return;

WarpToCamera?.Invoke(camera.Owner);
}

private void FillCameraList(string? filter = null)
{
foreach (var child in SubnetList.Children.ToArray())
{
child.Dispose();
}

if (_cameras.Count == 0)
{
Text.Text = Loc.GetString("ai-warp-menu-no-cameras");
return;
}

Text.Text = Loc.GetString("ai-warp-menu-select-camera");

var namedCameraList = new List<(string, List<string>, EntityUid)>();
var categoryList = new List<string>();

foreach (var uid in _cameras.ToArray())
{
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent<AICameraComponent>(uid, out var camera)) continue;

if (camera.Enabled == false) continue;

namedCameraList.Add((camera.CameraName, camera.CameraCategories, uid));
}

namedCameraList.Sort((a, b) => a.Item1.CompareTo(b.Item1));

foreach (var (name, categories, uid) in namedCameraList.ToArray())
{
if (categories.Count == 0) continue;

foreach (var category in categories.ToArray().Where(category => !categoryList.Contains(category.Replace("SurveillanceCamera", ""))))
{
categoryList.Add(category.Replace("SurveillanceCamera", ""));
}
}

categoryList.Sort();

foreach (var tab in categoryList.ToArray().Select(category => new ItemList()
{
Name = category
}))
{
tab.OnItemSelected += ItemSelected;
SubnetList.AddChild(tab);
}

foreach (var (name, categories, uid) in namedCameraList.ToArray())
{
if (!string.IsNullOrEmpty(filter) && !name.ToLowerInvariant().Contains(filter.Trim().ToLowerInvariant()))
continue;

if (!IoCManager.Resolve<IEntityManager>().TryGetComponent<AICameraComponent>(uid, out var camera)) continue;

if (camera.Enabled == false) continue;

foreach (var category in categories.ToArray())
foreach (var child in SubnetList.Children.ToArray())
{
if (child.Name != category.Replace("SurveillanceCamera", "")) continue;
if (child is not ItemList list) continue;

ItemList.Item cameraItem = new(list)
{
Metadata = camera,
Text = camera.CameraName
};

list.Add(cameraItem);
}
}
}

public void UpdateCameraList(List<EntityUid>? cameras = null)
{
if (cameras == null)
{
TryUpdateCameraList?.Invoke();
return;
}

_cameras = cameras;
FillCameraList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Content.Shared.Backmen.StationAI.Events;

namespace Content.Client.Backmen.StationAI.UI;

/// <summary>
/// Initializes a <see cref="AICameraList"/> and updates it when new server messages are received.
/// </summary>
public sealed class AICameraListBoundUserInterface : BoundUserInterface
{
[Dependency] private readonly IEntityManager _entityManager = default!;
private AICameraList _window = new AICameraList();

public AICameraListBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
IoCManager.InjectDependencies(this);
var netId = _entityManager.GetNetEntity(owner);
_window.TryUpdateCameraList += () => SendMessage(new AICameraListMessage(netId));
_window.WarpToCamera += (uid) => SendMessage(new AICameraWarpMessage(netId, _entityManager.GetNetEntity(uid)));
}

protected override void Open()
{
base.Open();

if (State != null) UpdateState(State);

_window.OpenCentered();
}

/// <summary>
/// Update the UI state based on server-sent info
/// </summary>
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);

if (_window == null || state is not AIBoundUserInterfaceState cast)
return;

_window.UpdateCameraList(_entityManager.GetEntityList(cast.Cameras));
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window.Dispose();
}
}
1 change: 1 addition & 0 deletions Content.IntegrationTests/Tests/PostMapInitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ await server.WaitPost(() =>
var jobList = entManager.GetComponent<StationJobsComponent>(station).RoundStartJobList
.Where(x => x.Value != 0)
.Where(x=>x.Key != "Prisoner") // backmen: Fugitive
.Where(x=>x.Key != "SAI") // backmen: SAI
.Where(x=>x.Key != "Freelancer") // backmen: shipwrecked
.Select(x => x.Key);
var spawnPoints = entManager.EntityQuery<SpawnPointComponent>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public override void Initialize()

private void OnInit(EntityUid uid, MindSwapPowerComponent component, ComponentInit args)
{

_actions.AddAction(uid, ref component.MindSwapPowerAction, ActionMindSwap);

#if !DEBUG
Expand Down
45 changes: 40 additions & 5 deletions Content.Server/Backmen/Fugitive/FugitiveSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Content.Shared.Objectives;
using Content.Shared.Objectives.Components;
using Content.Shared.Paper;
using Content.Shared.Radio.Components;
using Content.Shared.Random;
using Content.Shared.Roles.Jobs;
using Content.Shared.Wall;
Expand All @@ -53,6 +54,7 @@ public sealed class FugitiveSystem : EntitySystem
{
[ValidatePrototypeId<AntagPrototype>] private const string FugitiveAntagRole = "Fugitive";
[ValidatePrototypeId<JobPrototype>] private const string FugitiveRole = "Fugitive";

[ValidatePrototypeId<EntityPrototype>]
private const string EscapeObjective = "EscapeShuttleObjectiveFugitive";

Expand Down Expand Up @@ -80,17 +82,24 @@ public override void Initialize()
SubscribeLocalEvent<FugitiveComponent, GhostRoleSpawnerUsedEvent>(OnSpawned);
SubscribeLocalEvent<FugitiveComponent, MindAddedMessage>(OnMindAdded);
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEnd);
SubscribeLocalEvent<PlayerSpawningEvent>(OnPlayerSpawn, before: new []{typeof(ArrivalsSystem),typeof(SpawnPointSystem)});
SubscribeLocalEvent<PlayerSpawningEvent>(OnPlayerSpawn,
before: new[] { typeof(ArrivalsSystem), typeof(SpawnPointSystem) });
}

[ValidatePrototypeId<JobPrototype>]
private const string JobPrisoner = "Prisoner";

[ValidatePrototypeId<JobPrototype>]
private const string JobSAI = "SAI";

private void OnPlayerSpawn(PlayerSpawningEvent args)
{
if (args.SpawnResult != null)
return;

if (!(args.Job?.Prototype != null && _prototypeManager.TryIndex<JobPrototype>(args.Job!.Prototype!, out var jobInfo) && jobInfo.AlwaysUseSpawner))
if (!(args.Job?.Prototype != null &&
_prototypeManager.TryIndex<JobPrototype>(args.Job!.Prototype!, out var jobInfo) &&
jobInfo.AlwaysUseSpawner))
{
return;
}
Expand All @@ -113,6 +122,8 @@ private void OnPlayerSpawn(PlayerSpawningEvent args)
}
}

#region Prisoner

if (possiblePositions.Count == 0 && args.Job?.Prototype == JobPrisoner)
{
var points = EntityQueryEnumerator<EntityStorageComponent, TransformComponent, MetaDataComponent>();
Expand All @@ -126,14 +137,37 @@ private void OnPlayerSpawn(PlayerSpawningEvent args)
{
if (HasComp<WallMountComponent>(uid))
{
possiblePositions.Add(xform.Coordinates.WithPosition(xform.LocalPosition + xform.LocalRotation.ToWorldVec() * 1f));
possiblePositions.Add(
xform.Coordinates.WithPosition(xform.LocalPosition +
xform.LocalRotation.ToWorldVec() * 1f));
continue;
}

possiblePositions.Add(xform.Coordinates);
}
}
}

#endregion

#region SAI

if (possiblePositions.Count == 0 && args.Job?.Prototype == JobSAI)
{
var points = EntityQueryEnumerator<TelecomServerComponent, TransformComponent, MetaDataComponent>();

while (points.MoveNext(out var uid, out _, out var xform, out var spawnPoint))
{
if (args.Station != null && _stationSystem.GetOwningStation(uid, xform) != args.Station)
continue;

possiblePositions.Add(
xform.Coordinates.WithPosition(xform.LocalPosition + xform.LocalRotation.ToWorldVec() * 1f));
}
}

#endregion

if (possiblePositions.Count == 0)
{
Log.Warning("No spawn points were available! MakeFugitive");
Expand Down Expand Up @@ -185,6 +219,7 @@ public bool MakeFugitive([NotNullWhen(true)] out EntityUid? Fugitive, bool force
{
EnsureComp<FugitiveComponent>(Fugitive.Value).ForcedHuman = true;
}

return true;
}

Expand Down Expand Up @@ -223,7 +258,7 @@ public override void Update(float frameTime)
{
StampedColor = Color.Red,
StampedName = Loc.GetString("fugitive-announcement-GALPOL")
},"paper_stamp-generic");
}, "paper_stamp-generic");
}

RemCompDeferred<FugitiveCountdownComponent>(owner);
Expand Down Expand Up @@ -280,6 +315,7 @@ private void OnMindAdded(EntityUid uid, FugitiveComponent component, MindAddedMe
{
_roleSystem.MindRemoveRole<JobComponent>(mindId);
}

_roleSystem.MindAddRole(mindId, new JobComponent
{
Prototype = FugitiveRole
Expand Down Expand Up @@ -442,5 +478,4 @@ private FormattedMessage GenerateFugiReport(EntityUid uid)

[ValidatePrototypeId<EntityPrototype>] private const string SpawnPointPrototype = "SpawnPointGhostFugitive";
[ValidatePrototypeId<EntityPrototype>] private const string SpawnMobPrototype = "MobHumanFugitive";

}
Loading
Loading