Skip to content

Commit

Permalink
Fitting fix (#383)
Browse files Browse the repository at this point in the history
* initial fix, pre-compute fitting limits, isolate optimizations

* bring back in the optims, red herring

* cancel token on exit, try/catch on accessing empty flocks

* capture task cancellation logic into abstract base class

* more context on warn
  • Loading branch information
MikeJeffers committed Aug 22, 2021
1 parent b26a49a commit 572cc1d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/Perpetuum.RequestHandlers/Zone/Containers/EquipModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ public override void DoChange(IZoneRequest request, Player player, Container con
var componentType = request.Data.GetOrDefault<string>(k.robotComponent).ToEnum<RobotComponentType>();
var component = player.GetRobotComponentOrThrow(componentType);
var slot = request.Data.GetOrDefault<int>(k.slot);
component.MakeSlotFree(slot, container);
component.GetModule(slot).ThrowIfNotNull(ErrorCodes.UsedSlot); //OPP: explicitly prohibit this to make this simpler
// component.MakeSlotFree(slot, container); // Big nope

var moduleEid = request.Data.GetOrDefault<long>(k.moduleEID);
var module = (Module)container.GetItemOrThrow(moduleEid).Unstack(1);
module.CheckEnablerExtensionsAndThrowIfFailed(player.Character);

//Perform pre-fit check for fitting legality
player.CheckEnergySystemAndThrowIfFailed(module);

component.EquipModuleOrThrow(module, slot);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public override void DoChange(IZoneRequest request, Player player, Container con
{
var moduleEid = request.Data.GetOrDefault<long>(k.moduleEID);
var module = player.GetModule(moduleEid).ThrowIfNull(ErrorCodes.ModuleNotFound);
player.CheckEnergySystemAndThrowIfFailed(module, true);
module.Owner = player.Character.Eid;
module.Unequip(container);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Transactions;
using Perpetuum.Accounting.Characters;
Expand Down Expand Up @@ -77,6 +78,7 @@ private void EquipChange(IZoneRequest request, Player player, Container containe
DoChange(request, player, container);
player.OnEquipChange();
player.Initialize(character);

player.CheckEnergySystemAndThrowIfFailed();

// Apply ratios to prevent resetting values
Expand Down Expand Up @@ -106,6 +108,7 @@ private MessageBuilder BuildMessage(IZoneRequest request, Player player, Contain

private static void OnCompleted(Player player, MessageBuilder message)
{
player.Initialize(player.Character);
player.SendRefreshUnitPacket();
message.Send();
}
Expand Down
10 changes: 10 additions & 0 deletions src/Perpetuum/Modules/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,15 @@ public void ApplyRobotPropertyModifiers(ref ItemPropertyModifier modifier)
ParentRobot?.GetPropertyModifier(m).Modify(ref modifier);
}
}

public void SimulateRobotPropertyModifiers(Robot parent, ref ItemPropertyModifier modifier)
{
var modifiers = PropertyModifiers.GetOrEmpty(modifier.Field);

foreach (var m in modifiers)
{
parent.GetPropertyModifier(m).Modify(ref modifier);
}
}
}
}
23 changes: 19 additions & 4 deletions src/Perpetuum/Robots/Robot.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,29 @@ public override ItemPropertyModifier GetPropertyModifier(AggregateField field)
return modifier;
}

public bool CheckPowerGridForModule(Module module)
public bool CheckPowerGridForModule(Module module, bool removing=false)
{
return TotalPowerGridUsage + module.PowerGridUsage <= PowerGridMax;
return SimulateFitting(module, removing, PowerGridMax, TotalPowerGridUsage, AggregateField.powergrid_usage, AggregateField.powergrid_max_modifier);
}

public bool CheckCpuForModule(Module module)
public bool CheckCpuForModule(Module module, bool removing = false)
{
return TotalCpuUsage + module.CpuUsage <= CpuMax;
return SimulateFitting(module, removing, CpuMax, TotalCpuUsage, AggregateField.cpu_usage, AggregateField.cpu_max_modifier);
}

private bool SimulateFitting(Module module, bool removing, double max, double current, AggregateField usageField, AggregateField maxModField)
{
double moduleUsageEstimate = 0;
var itemMod = module.BasePropertyModifiers.GetPropertyModifier(usageField);
module.SimulateRobotPropertyModifiers(this, ref itemMod);
itemMod.Modify(ref moduleUsageEstimate);
moduleUsageEstimate = removing ? -moduleUsageEstimate : moduleUsageEstimate;
if (removing && module.BasePropertyModifiers.GetPropertyModifier(maxModField).HasValue)
{
var mod = module.BasePropertyModifiers.GetPropertyModifier(maxModField).Value;
max /= Math.Max(mod, 1);
}
return current + moduleUsageEstimate <= max;
}

public double TotalPowerGridUsage
Expand Down
9 changes: 9 additions & 0 deletions src/Perpetuum/Robots/Robot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ public IEnumerable<RobotComponent> RobotComponents
get { return _robotComponents.Value; }
}

public void CheckEnergySystemAndThrowIfFailed(Module module, bool isRemoving=false)
{
if (!CheckPowerGridForModule(module, isRemoving))
throw PerpetuumException.Create(ErrorCodes.OutOfPowergrid);

if (!CheckCpuForModule(module, isRemoving))
throw PerpetuumException.Create(ErrorCodes.OutOfCpu);
}

public void CheckEnergySystemAndThrowIfFailed()
{
if (PowerGrid < 0)
Expand Down

0 comments on commit 572cc1d

Please sign in to comment.