Skip to content

Commit

Permalink
DEV
Browse files Browse the repository at this point in the history
  • Loading branch information
Fragoler committed Oct 15, 2024
1 parent f4e444a commit bbabfd9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Content.Client/Entry/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public override void Init()
_prototypeManager.RegisterIgnore("stationGoal"); // Corvax-StationGoal
_prototypeManager.RegisterIgnore("ghostRoleRaffleDecider");
_prototypeManager.RegisterIgnore("spawnGroupProto"); // Exodus-Lavaland

_prototypeManager.RegisterIgnore("actionList"); // Exodus - Advanced AI for Lavaland
_componentFactory.GenerateNetIds();
_adminManager.Initialize();
_screenshotHook.Initialize();
Expand Down
7 changes: 6 additions & 1 deletion Content.Server/NPC/Components/NPCAbilityCombatComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Exodus-Lavaland
using Content.Server.Exodus.Actions;

namespace Content.Server.NPC.Components;

/// <summary>
Expand All @@ -23,7 +25,10 @@ public sealed partial class NPCAbilityCombatComponent : Component
public int UsedActionsLastUpd = 0;

[ViewVariables]
public float ActionsTimeReload = 1.0f;
public float MinActionsInterval = 1.0f;

[ViewVariables]
public ActionList UsingActions = new();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading;
using System.Threading.Tasks;

namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Math;

/// <summary>
/// Set <see cref="SetBoolOperator.Value"/> to bool value for the
/// specified <see cref="SetFloatOperator.TargetKey"/> in the <see cref="NPCBlackboard"/>.
/// </summary>
public sealed partial class SetStringOperator : HTNOperator
{
[DataField(required: true), ViewVariables]
public string TargetKey = string.Empty;

[DataField(required: true), ViewVariables(VVAccess.ReadWrite)]
public string Value = string.Empty;

public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
CancellationToken cancelToken)
{
return (
true,
new Dictionary<string, object>
{
{ TargetKey, Value }
}
);
}
}
2 changes: 2 additions & 0 deletions Content.Server/NPC/NPCBlackboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public sealed partial class NPCBlackboard : IEnumerable<KeyValuePair<string, obj
{"RotateSpeed", float.MaxValue},
{"VisionRadius", 10f},
{"AggroVisionRadius", 10f},
{"MinActionsInterval", 1.0f}, // Exodus - Advanced AI for Lavaland
{"ActionsList", "AllActions"}, // Exodus - Advanced AI for Lavaland
};

/// <summary>
Expand Down
51 changes: 32 additions & 19 deletions Content.Server/NPC/Systems/NPCCombatSystrem.Ability.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Exodus-AdvancedAI
using System.Numerics;
using Content.Server.Actions;
using Content.Server.Exodus.Actions;
using Content.Server.NPC.Components;
using Content.Shared.NPC;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Random;
using Content.Shared.ActionBlocker;
using Content.Shared.Actions;
using Content.Shared.Actions.Events;
using Content.Shared.ActionBlocker;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;

using Content.Shared.Interaction;
using Content.Shared.Actions;
using Content.Server.Actions;
using Content.Shared.Directions;
using Content.Shared.NPC;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using System.Numerics;

namespace Content.Server.NPC.Systems;

Expand All @@ -23,6 +23,7 @@ public sealed partial class NPCCombatSystem
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly RotateToFaceSystem _rotateToFaceSystem = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;

private const float TargetAbilityLostRange = 28f;

Expand Down Expand Up @@ -68,7 +69,7 @@ private void CastActions(EntityUid uid, NPCAbilityCombatComponent combatComp, Ti

if (distance > TargetMeleeLostRange)
{
combatComp.Status = AbilityCombatStatus.TargetUnreachable;
combatComp.Status = AbilityCombatStatus.TargetOutOfRange;
return;
}

Expand Down Expand Up @@ -98,10 +99,17 @@ private void CastActions(EntityUid uid, NPCAbilityCombatComponent combatComp, Ti

var act = _random.PickAndTake(actions);

Log.Debug($"Picked action {actions}");

if (!IsInAvaibleList(act, combatComp.UsingActions))
continue;

var attemptEv = new ActionAttemptEvent(uid);
RaiseLocalEvent(act, ref attemptEv);
if (attemptEv.Cancelled)
return;
continue;

Log.Debug($"Action {actions} is avaible to use");

if (TryUseAction(uid, act, distance, combatComp, curTime))
combatComp.UsedActionsLastUpd++;
Expand All @@ -110,7 +118,7 @@ private void CastActions(EntityUid uid, NPCAbilityCombatComponent combatComp, Ti
if (combatComp.UsedActionsLastUpd >= combatComp.ActionsPerUpd)
{
combatComp.UsedActionsLastUpd = 0;
combatComp.NextAction = curTime + TimeSpan.FromSeconds(combatComp.ActionsTimeReload);
combatComp.NextAction = curTime + TimeSpan.FromSeconds(combatComp.MinActionsInterval);
}
}

Expand Down Expand Up @@ -157,10 +165,7 @@ private bool TryUseAction(EntityUid uid,

if (action.MinAIUseRange >= distance ||
action.MaxAIUseRange <= distance)
{
Log.Error("Out");
return false;
}

// Validate request by checking action blockers and the like:
switch (action)
Expand Down Expand Up @@ -191,16 +196,16 @@ private bool TryUseAction(EntityUid uid,

if (worldAction.Range <= distance)
{
var mapTargetPos = entityCoordinatesTarget.ToMapPos(EntityManager, _transform);
var mapUserPos = Transform(uid).Coordinates.ToMapPos(EntityManager, _transform);
var mapTargetPos = _transform.ToMapCoordinates(entityCoordinatesTarget).Position;
var mapUserPos = _transform.ToMapCoordinates(Transform(uid).Coordinates).Position;

var direction = mapTargetPos - mapUserPos;
var coefficient = worldAction.Range / distance;
var delta = new Vector2(direction.X * coefficient, direction.Y * coefficient);
entityCoordinatesTarget = new EntityCoordinates(entityCoordinatesTarget.EntityId, mapUserPos + delta);
}

_rotateToFaceSystem.TryFaceCoordinates(uid, entityCoordinatesTarget.ToMapPos(EntityManager, _transform));
_rotateToFaceSystem.TryFaceCoordinates(uid, _transform.ToMapCoordinates(entityCoordinatesTarget).Position);

if (!_actions.ValidateWorldTarget(uid, entityCoordinatesTarget, (actionUid, worldAction)))
return false;
Expand Down Expand Up @@ -236,4 +241,12 @@ private bool TryUseAction(EntityUid uid,
return true;
}

private bool IsInAvaibleList(EntityUid action, ActionList list)
{
if (!TryComp(action, out MetaDataComponent? meta) ||
meta.EntityPrototype == null)
return false;

return list.AllAvaibleAbilities || list.Actions.Contains(meta.EntityPrototype!.ID);
}
}
18 changes: 18 additions & 0 deletions Content.Server/_Exodus/Actions/ActionListPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Robust.Shared.Prototypes;

namespace Content.Server.Exodus.Actions;

[Prototype("actionList")]
public sealed partial class ActionListPrototype : ActionList, IPrototype
{
[IdDataField]
public string ID { get; private set; } = default!;
}

[Virtual]
[DataDefinition]
public partial class ActionList
{
public bool AllAvaibleAbilities = false;
public List<string> Actions = [];
}

0 comments on commit bbabfd9

Please sign in to comment.