Action, Action Container, and Action Upgrade changes (#24005)
* Added TransferActionWithNewAttached and TransferAllActionsWithNewAttached and OnActionAdded event method to ActionContainerSystem. These are needed where the action needs to have a different attached entity for usage. Fixed a bug with not being able to upgrade actions between levels. Added a way to grant and remove a singular action. * adds an action container component to mind on action added to fix tests * Swaps trycomp for hascomp * Maybe this will fix the thests * Grants action container to performer as well * Wait that makes no sense, removing that * fixes mind action grant logic * Changes ent check back to netent check * Reverts unintended container changes
This commit is contained in:
@@ -21,7 +21,7 @@ public sealed class ActionUpgradeSystem : EntitySystem
|
||||
|
||||
private void OnActionUpgradeEvent(EntityUid uid, ActionUpgradeComponent component, ActionUpgradeEvent args)
|
||||
{
|
||||
if (!CanLevelUp(args.NewLevel, component.EffectedLevels, out var newActionProto)
|
||||
if (!CanUpgrade(args.NewLevel, component.EffectedLevels, out var newActionProto)
|
||||
|| !_actions.TryGetActionData(uid, out var actionComp))
|
||||
return;
|
||||
|
||||
@@ -56,8 +56,9 @@ public sealed class ActionUpgradeSystem : EntitySystem
|
||||
_entityManager.DeleteEntity(uid);
|
||||
}
|
||||
|
||||
public bool TryUpgradeAction(EntityUid? actionId, ActionUpgradeComponent? actionUpgradeComponent = null, int newLevel = 0)
|
||||
public bool TryUpgradeAction(EntityUid? actionId, out EntityUid? upgradeActionId, ActionUpgradeComponent? actionUpgradeComponent = null, int newLevel = 0)
|
||||
{
|
||||
upgradeActionId = null;
|
||||
if (!TryGetActionUpgrade(actionId, out var actionUpgradeComp))
|
||||
return false;
|
||||
|
||||
@@ -68,21 +69,26 @@ public sealed class ActionUpgradeSystem : EntitySystem
|
||||
if (newLevel < 1)
|
||||
newLevel = actionUpgradeComponent.Level + 1;
|
||||
|
||||
if (!CanLevelUp(newLevel, actionUpgradeComponent.EffectedLevels, out _))
|
||||
if (!CanLevelUp(newLevel, actionUpgradeComponent.EffectedLevels))
|
||||
return false;
|
||||
|
||||
UpgradeAction(actionId, actionUpgradeComp);
|
||||
actionUpgradeComponent.Level = newLevel;
|
||||
|
||||
// If it can level up but can't upgrade, still return true and return current actionId as the upgradeId.
|
||||
if (!CanUpgrade(newLevel, actionUpgradeComponent.EffectedLevels, out var newActionProto))
|
||||
{
|
||||
upgradeActionId = actionId;
|
||||
DebugTools.AssertNotNull(upgradeActionId);
|
||||
return true;
|
||||
}
|
||||
|
||||
upgradeActionId = UpgradeAction(actionId, actionUpgradeComp, newActionProto, newLevel);
|
||||
DebugTools.AssertNotNull(upgradeActionId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: Add checks for branching upgrades
|
||||
private bool CanLevelUp(
|
||||
int newLevel,
|
||||
Dictionary<int, EntProtoId> levelDict,
|
||||
[NotNullWhen(true)]out EntProtoId? newLevelProto)
|
||||
private bool CanLevelUp(int newLevel, Dictionary<int, EntProtoId> levelDict)
|
||||
{
|
||||
newLevelProto = null;
|
||||
|
||||
if (levelDict.Count < 1)
|
||||
return false;
|
||||
|
||||
@@ -91,25 +97,47 @@ public sealed class ActionUpgradeSystem : EntitySystem
|
||||
|
||||
foreach (var (level, proto) in levelDict)
|
||||
{
|
||||
if (newLevel != level || newLevel > finalLevel)
|
||||
if (newLevel > finalLevel)
|
||||
continue;
|
||||
|
||||
canLevel = true;
|
||||
newLevelProto = proto;
|
||||
DebugTools.AssertNotNull(newLevelProto);
|
||||
break;
|
||||
if ((newLevel <= finalLevel && newLevel != level) || newLevel == level)
|
||||
{
|
||||
canLevel = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return canLevel;
|
||||
}
|
||||
|
||||
private bool CanUpgrade(int newLevel, Dictionary<int, EntProtoId> levelDict, [NotNullWhen(true)]out EntProtoId? newLevelProto)
|
||||
{
|
||||
var canUpgrade = false;
|
||||
newLevelProto = null;
|
||||
|
||||
var finalLevel = levelDict.Keys.ToList()[levelDict.Keys.Count - 1];
|
||||
|
||||
foreach (var (level, proto) in levelDict)
|
||||
{
|
||||
if (newLevel != level || newLevel > finalLevel)
|
||||
continue;
|
||||
|
||||
canUpgrade = true;
|
||||
newLevelProto = proto;
|
||||
DebugTools.AssertNotNull(newLevelProto);
|
||||
break;
|
||||
}
|
||||
|
||||
return canUpgrade;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises a level by one
|
||||
/// </summary>
|
||||
public void UpgradeAction(EntityUid? actionId, ActionUpgradeComponent? actionUpgradeComponent = null, int newLevel = 0)
|
||||
public EntityUid? UpgradeAction(EntityUid? actionId, ActionUpgradeComponent? actionUpgradeComponent = null, EntProtoId? newActionProto = null, int newLevel = 0)
|
||||
{
|
||||
if (!TryGetActionUpgrade(actionId, out var actionUpgradeComp))
|
||||
return;
|
||||
return null;
|
||||
|
||||
actionUpgradeComponent ??= actionUpgradeComp;
|
||||
DebugTools.AssertNotNull(actionUpgradeComponent);
|
||||
@@ -118,7 +146,47 @@ public sealed class ActionUpgradeSystem : EntitySystem
|
||||
if (newLevel < 1)
|
||||
newLevel = actionUpgradeComponent.Level + 1;
|
||||
|
||||
RaiseActionUpgradeEvent(newLevel, actionId.Value);
|
||||
actionUpgradeComponent.Level = newLevel;
|
||||
// RaiseActionUpgradeEvent(newLevel, actionId.Value);
|
||||
|
||||
if (!CanUpgrade(newLevel, actionUpgradeComponent.EffectedLevels, out var newActionPrototype)
|
||||
|| !_actions.TryGetActionData(actionId, out var actionComp))
|
||||
return null;
|
||||
|
||||
newActionProto ??= newActionPrototype;
|
||||
DebugTools.AssertNotNull(newActionProto);
|
||||
|
||||
var originalContainer = actionComp.Container;
|
||||
var originalAttachedEntity = actionComp.AttachedEntity;
|
||||
|
||||
_actionContainer.RemoveAction(actionId.Value, actionComp);
|
||||
|
||||
EntityUid? upgradedActionId = null;
|
||||
if (originalContainer != null
|
||||
&& TryComp<ActionsContainerComponent>(originalContainer.Value, out var actionContainerComp))
|
||||
{
|
||||
upgradedActionId = _actionContainer.AddAction(originalContainer.Value, newActionProto, actionContainerComp);
|
||||
|
||||
if (originalAttachedEntity != null)
|
||||
_actions.GrantContainedActions(originalAttachedEntity.Value, originalContainer.Value);
|
||||
else
|
||||
_actions.GrantContainedActions(originalContainer.Value, originalContainer.Value);
|
||||
}
|
||||
else if (originalAttachedEntity != null)
|
||||
{
|
||||
upgradedActionId = _actionContainer.AddAction(originalAttachedEntity.Value, newActionProto);
|
||||
}
|
||||
|
||||
if (!TryComp<ActionUpgradeComponent>(upgradedActionId, out var upgradeComp))
|
||||
return null;
|
||||
|
||||
upgradeComp.Level = newLevel;
|
||||
|
||||
// TODO: Preserve ordering of actions
|
||||
|
||||
_entityManager.DeleteEntity(actionId);
|
||||
|
||||
return upgradedActionId.Value;
|
||||
}
|
||||
|
||||
private void RaiseActionUpgradeEvent(int level, EntityUid actionId)
|
||||
|
||||
Reference in New Issue
Block a user