Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #147 from FireNameFN/UpdateBadCode
Browse files Browse the repository at this point in the history
Update my first PR
  • Loading branch information
Zekins3366 authored May 12, 2024
2 parents 71e0d7f + 8a92512 commit 9ebd16a
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Content.Server/NPC/HTN/HTNSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Content.Shared.Mobs;
using Content.Shared.NPC;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.CPUJob.JobQueues;
using Robust.Shared.CPUJob.JobQueues.Queues;
using Robust.Shared.Player;
Expand All @@ -26,10 +27,10 @@ public sealed class HTNSystem : EntitySystem
[Dependency] private readonly NPCSystem _npc = default!;
[Dependency] private readonly NPCUtilitySystem _utility = default!;
[Dependency] private readonly WorldControllerSystem _world = default!;
[Dependency] private readonly TransformSystem _transform = default!;

private EntityQuery<TransformComponent> _xformQuery;
private EntityQuery<LoadedChunkComponent> _loadedQuery;
private EntityQuery<WorldControllerComponent> _mapQuery;
private EntityQuery<LoadedChunkComponent> _loadedQuery;

private readonly JobQueue _planQueue = new(0.004);

Expand All @@ -39,9 +40,8 @@ public sealed class HTNSystem : EntitySystem
public override void Initialize()
{
base.Initialize();
_xformQuery = GetEntityQuery<TransformComponent>();
_loadedQuery = GetEntityQuery<LoadedChunkComponent>();
_mapQuery = GetEntityQuery<WorldControllerComponent>();
_loadedQuery = GetEntityQuery<LoadedChunkComponent>();
SubscribeLocalEvent<HTNComponent, MobStateChangedEvent>(_npc.OnMobStateChange);
SubscribeLocalEvent<HTNComponent, MapInitEvent>(_npc.OnNPCMapInit);
SubscribeLocalEvent<HTNComponent, PlayerAttachedEvent>(_npc.OnPlayerNPCAttach);
Expand Down Expand Up @@ -158,13 +158,13 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime)
_planQueue.Process();
var query = EntityQueryEnumerator<ActiveNPCComponent, HTNComponent>();

while(query.MoveNext(out var uid, out _, out var comp))
while (query.MoveNext(out var uid, out _, out var comp))
{
// If we're over our max count or it's not MapInit then ignore the NPC.
if (count >= maxUpdates)
break;

if (!IsNPCActive(comp))
if (!IsNPCActive(uid))
continue;

if (comp.PlanningJob != null)
Expand Down Expand Up @@ -255,12 +255,16 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime)
}
}

private bool IsNPCActive(HTNComponent component)
private bool IsNPCActive(EntityUid entity)
{
if (!_xformQuery.TryGetComponent(component.Owner, out TransformComponent? xform) || !_mapQuery.TryGetComponent(xform.MapUid, out var worldComponent))
var transform = Transform(entity);

if (!_mapQuery.TryGetComponent(transform.MapUid, out var worldComponent))
return true;

return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(xform.WorldPosition).Floored(), xform.MapUid.Value, worldComponent));
var chunk = _world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(_transform.GetWorldPosition(transform)).Floored(), transform.MapUid.Value, worldComponent);

return _loadedQuery.TryGetComponent(chunk, out var loaded) && loaded.Loaders is not null;
}

private void AppendDebugText(HTNTask task, StringBuilder text, List<int> planBtr, List<int> btr, ref int level)
Expand Down

0 comments on commit 9ebd16a

Please sign in to comment.