Skip to content
Merged

p34.7 #511

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
11 changes: 7 additions & 4 deletions src/Perpetuum.ExportedTypes/CategoryFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public enum CategoryFlags : long
cf_command_robots = 0x0000000000001001,
cf_combat_command_robots = 0x0000000000011001,

cf_drones = 0x0000000000001101,
cf_assault_drones = 0x0000000000011101,
cf_industrial_drones = 0x0000000000021101,
cf_support_drones = 0x0000000000031101,
cf_attack_drones = 0x0000000000041101,

cf_ammo = 0x000000000000000A,
cf_railgun_ammo = 0x000000000000010A,
cf_small_railgun_ammo = 0x000000000001010A,
Expand Down Expand Up @@ -193,6 +199,7 @@ public enum CategoryFlags : long
cf_energy_warfare_upgrades = 0x00000000000D030F,
cf_reactor_sealings = 0x00000000000E030F,
cf_landmine_detectors = 0x00000000000F030F,
cf_jumpers = 0x000000000010030F,
cf_electronics_equipment = 0x000000000000040F,
cf_sensor_boosters = 0x000000000001040F,
cf_remote_sensor_boosters = 0x000000000002040F,
Expand Down Expand Up @@ -611,10 +618,6 @@ public enum CategoryFlags : long
cf_mining_turrets = 0x0000000000000E92,
cf_harvesting_turrets = 0x0000000000000F92,
cf_combat_drones = 0x0000000000001092,
cf_assault_drones = 0x0000000000001192,
cf_industrial_drones = 0x0000000000001292,
cf_support_drones = 0x0000000000001392,
cf_attack_drones = 0x0000000000001492,

cf_production_items = 0x0000000000000094,
cf_research_kits = 0x0000000000000194,
Expand Down
36 changes: 18 additions & 18 deletions src/Perpetuum.RequestHandlers/EquipModule.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;
using System.Transactions;
using Perpetuum.Containers;
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.Host.Requests;
using Perpetuum.Items;
using Perpetuum.Modules;
using Perpetuum.Robots;
using System.Collections.Generic;
using System.Transactions;

namespace Perpetuum.RequestHandlers
{
Expand All @@ -15,50 +15,50 @@ public class EquipModule : IRequestHandler
private readonly IEntityRepository _entityRepository;
private readonly RobotHelper _robotHelper;

public EquipModule(IEntityRepository entityRepository,RobotHelper robotHelper)
public EquipModule(IEntityRepository entityRepository, RobotHelper robotHelper)
{
_entityRepository = entityRepository;
_robotHelper = robotHelper;
}

public void HandleRequest(IRequest request)
{
using (var scope = Db.CreateTransaction())
using (TransactionScope scope = Db.CreateTransaction())
{
var character = request.Session.Character;
Accounting.Characters.Character character = request.Session.Character;
character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

var containerEid = request.Data.GetOrDefault<long>(k.containerEID);
var container = Container.GetWithItems(containerEid, character).ThrowIfNull(ErrorCodes.ContainerNotFound);
long containerEid = request.Data.GetOrDefault<long>(k.containerEID);
Container container = Container.GetWithItems(containerEid, character).ThrowIfNull(ErrorCodes.ContainerNotFound);
container.ThrowIfType<VolumeWrapperContainer>(ErrorCodes.AccessDenied);

var robotEid = request.Data.GetOrDefault<long>(k.robotEID);
var robot = _robotHelper.LoadRobotOrThrow(robotEid);
long robotEid = request.Data.GetOrDefault<long>(k.robotEID);
Robot robot = _robotHelper.LoadRobotOrThrow(robotEid);
robot.IsSingleAndUnpacked.ThrowIfFalse(ErrorCodes.RobotMustbeSingleAndNonRepacked);
robot.Initialize(character);

var slot = request.Data.GetOrDefault<int>(k.slot);
var componentType = request.Data.GetOrDefault<string>(k.robotComponent).ToEnum<RobotComponentType>();
var component = robot.GetRobotComponentOrThrow(componentType);
int slot = request.Data.GetOrDefault<int>(k.slot);
RobotComponentType componentType = request.Data.GetOrDefault<string>(k.robotComponent).ToEnum<RobotComponentType>();
RobotComponent component = robot.GetRobotComponentOrThrow(componentType);
component.MakeSlotFree(slot, container);
var moduleEid = request.Data.GetOrDefault<long>(k.moduleEID);
var module = (Module)container.GetItemOrThrow(moduleEid).Unstack(1);
component.EquipModuleOrThrow(module, slot);
long moduleEid = request.Data.GetOrDefault<long>(k.moduleEID);
Module module = (Module)container.GetItemOrThrow(moduleEid).Unstack(1);
component.EquipModuleOrThrow(module, slot, robot.Definition);

robot.Initialize(character);
robot.Save();
container.Save();

Transaction.Current.OnCompleted(completed =>
{
var result = new Dictionary<string, object>
Dictionary<string, object> result = new Dictionary<string, object>
{
{k.robot, robot.ToDictionary()},
{k.robot, robot.ToDictionary()},
{k.container, container.ToDictionary()}
};
Message.Builder.FromRequest(request).WithData(result).WrapToResult().Send();
});

scope.Complete();
}
}
Expand Down
46 changes: 25 additions & 21 deletions src/Perpetuum.RequestHandlers/FittingPreset/FittingPresetApply.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;
using System.Linq;
using Perpetuum.Containers;
using Perpetuum.Data;
using Perpetuum.EntityFramework;
Expand All @@ -8,6 +6,8 @@
using Perpetuum.Items.Ammos;
using Perpetuum.Modules;
using Perpetuum.Robots;
using System.Collections.Generic;
using System.Linq;

namespace Perpetuum.RequestHandlers.FittingPreset
{
Expand All @@ -16,51 +16,55 @@ public class FittingPresetApply : FittingPresetRequestHandler
private readonly IEntityRepository _entityRepository;
private readonly RobotHelper _robotHelper;

public FittingPresetApply(IEntityRepository entityRepository,RobotHelper robotHelper)
public FittingPresetApply(IEntityRepository entityRepository, RobotHelper robotHelper)
{
_entityRepository = entityRepository;
_robotHelper = robotHelper;
}

public override void HandleRequest(IRequest request)
{
using (var scope = Db.CreateTransaction())
using (System.Transactions.TransactionScope scope = Db.CreateTransaction())
{
var id = request.Data.GetOrDefault<int>(k.ID);
var robotEid = request.Data.GetOrDefault<long>(k.robotEID);
var containerEid = request.Data.GetOrDefault<long>(k.containerEID);
var forCorporation = request.Data.GetOrDefault<int>(k.forCorporation).ToBool();
int id = request.Data.GetOrDefault<int>(k.ID);
long robotEid = request.Data.GetOrDefault<long>(k.robotEID);
long containerEid = request.Data.GetOrDefault<long>(k.containerEID);
bool forCorporation = request.Data.GetOrDefault<int>(k.forCorporation).ToBool();

var character = request.Session.Character;
var repo = GetFittingPresetRepository(character, forCorporation);
var preset = repo.Get(id);
var robot = _robotHelper.LoadRobotForCharacter(robotEid, character);
Accounting.Characters.Character character = request.Session.Character;
Robots.Fitting.IFittingPresetRepository repo = GetFittingPresetRepository(character, forCorporation);
Robots.Fitting.FittingPreset preset = repo.Get(id);
Robot robot = _robotHelper.LoadRobotForCharacter(robotEid, character);
robot.ED.ThrowIfNotEqual(preset.Robot, ErrorCodes.WTFErrorMedicalAttentionSuggested);

var container = Container.GetWithItems(containerEid, character);
Container container = Container.GetWithItems(containerEid, character);
robot.EmptyRobot(character, container, false);
robot.Initialize(character);

foreach (var moduleInfos in preset.Modules.GroupBy(i => i.Component))
foreach (IGrouping<RobotComponentType, Robots.Fitting.FittingPreset.ModuleInfo> moduleInfos in preset.Modules.GroupBy(i => i.Component))
{
var component = robot.GetRobotComponent((RobotComponentType) moduleInfos.Key).ThrowIfNull(ErrorCodes.ItemNotFound);
RobotComponent component = robot.GetRobotComponent(moduleInfos.Key).ThrowIfNull(ErrorCodes.ItemNotFound);

foreach (var moduleInfo in moduleInfos)
foreach (Robots.Fitting.FittingPreset.ModuleInfo moduleInfo in moduleInfos)
{
var module = container.GetItems().OfType<Module>().FirstOrDefault(m => m.ED == moduleInfo.Module);
Module module = container.GetItems().OfType<Module>().FirstOrDefault(m => m.ED == moduleInfo.Module);
if (module == null)
{
continue;
}

module = (Module)module.Unstack(1);

if (module is ActiveModule activeModule && moduleInfo.Ammo != EntityDefault.None)
{
var ammo = (Ammo)container.GetAndRemoveItemByDefinition(moduleInfo.Ammo.Definition, activeModule.AmmoCapacity);
Ammo ammo = (Ammo)container.GetAndRemoveItemByDefinition(moduleInfo.Ammo.Definition, activeModule.AmmoCapacity);
if (ammo != null)
{
activeModule.SetAmmo(ammo);
}
}

component.EquipModuleOrThrow(module, moduleInfo.Slot);
component.EquipModuleOrThrow(module, moduleInfo.Slot, robot.Definition);
}
}

Expand All @@ -69,13 +73,13 @@ public override void HandleRequest(IRequest request)
robot.Save();
container.Save();

var result = new Dictionary<string, object>
Dictionary<string, object> result = new Dictionary<string, object>
{
{k.robot, robot.ToDictionary()},
{k.container, container.ToDictionary()}
};
Message.Builder.FromRequest(request).WithData(result).Send();

scope.Complete();
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/Perpetuum.RequestHandlers/Zone/Containers/EquipModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ public class EquipModule : ZoneChangeModule
{
public override void DoChange(IZoneRequest request, Player player, Container container)
{
var componentType = request.Data.GetOrDefault<string>(k.robotComponent).ToEnum<RobotComponentType>();
var component = player.GetRobotComponentOrThrow(componentType);
var slot = request.Data.GetOrDefault<int>(k.slot);
RobotComponentType componentType = request.Data.GetOrDefault<string>(k.robotComponent).ToEnum<RobotComponentType>();
RobotComponent component = player.GetRobotComponentOrThrow(componentType);
int slot = request.Data.GetOrDefault<int>(k.slot);
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);
long moduleEid = request.Data.GetOrDefault<long>(k.moduleEID);
Module 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);
Robot robot = player;

component.EquipModuleOrThrow(module, slot, robot.Definition);
}
}
}
11 changes: 11 additions & 0 deletions src/Perpetuum/EntityFramework/EntityDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ public Faction Faction
}
}

public int[] AllowedBots
{
get
{
int[] ids = _dictionary.GetOrDefault(k.AllowedBots, Array.Empty<int>());
Debug.Assert(ids.Length > 0);

return ids;
}
}

public int PlasmaDefinition => _dictionary.GetOrDefault<int>(k.PlasmaDefinition);

public int PlasmaConsumption => _dictionary.GetOrDefault<int>(k.PlasmaConsumption);
Expand Down
1 change: 1 addition & 0 deletions src/Perpetuum/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,5 +717,6 @@ public enum ErrorCodes
NoxTeleportForbidden,
PlasmaNotFound,
Overheat,
NotAllowedOnThisBot,
}
}
3 changes: 3 additions & 0 deletions src/Perpetuum/Keywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,5 +1228,8 @@ public class k
// Plasma-based modules
public const string PlasmaDefinition = "plasmaDefinition";
public const string PlasmaConsumption = "plasmaConsumption";

// Allowed bots for this equipment
public const string AllowedBots = "allowedBots";
}
}
15 changes: 14 additions & 1 deletion src/Perpetuum/Robots/Robot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,25 @@ protected override void OnBeforeRemovedFromZone(IZone zone)
base.OnBeforeRemovedFromZone(zone);
}

//TODO: review if it's still has to be Lazy
private void InitComponents()
{
//Peanuts Plague
components = new Lazy<IEnumerable<Item>>(() => Children.OfType<Item>().ToArray());
_ = components.Value; // force evaluation
robotComponents = new Lazy<IEnumerable<RobotComponent>>(() => Components.OfType<RobotComponent>().ToArray());
modules = new Lazy<IEnumerable<Module>>(() => RobotComponents.SelectMany(c => c.Modules).ToArray());
_ = robotComponents.Value; // force evaluation
modules = new Lazy<IEnumerable<Module>>(() => RobotComponents
.SelectMany(c =>
{
c.Initialize();

return c.Modules;
})
.ToArray());
_ = modules.Value; // force evaluation
activeModules = new Lazy<IEnumerable<ActiveModule>>(() => Modules.OfType<ActiveModule>().ToArray());
_ = activeModules.Value; // force evaluation
}

protected override void OnEnterZone(IZone zone, ZoneEnterType enterType)
Expand Down
41 changes: 25 additions & 16 deletions src/Perpetuum/Robots/RobotComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public override void Initialize()

private void InitModules()
{
modules = new Lazy<IEnumerable<Module>>(() => Children.OfType<Module>().ToArray());
activeModules = new Lazy<IEnumerable<ActiveModule>>(() => Modules.OfType<ActiveModule>().ToArray());
modules = new Lazy<IEnumerable<Module>>(() => Children.OfType<Module>().ToArray(), isThreadSafe: true);
_ = modules.Value;
activeModules = new Lazy<IEnumerable<ActiveModule>>(() => Modules.OfType<ActiveModule>().ToArray(), isThreadSafe: true);
_ = activeModules.Value;
}

public override void AcceptVisitor(IEntityVisitor visitor)
Expand Down Expand Up @@ -129,24 +131,31 @@ public bool IsValidSlotTo(Module module, int slot)
(!specializedSlot || specializedModule);
}

public ErrorCodes CanEquipModule(Module module, int slot)
public ErrorCodes CanEquipModule(Module module, int slot, int robotDefinition = 0)
{
return IsUsedSlot(slot)
? ErrorCodes.UsedSlot
: !IsValidSlotTo(module, slot)
? ErrorCodes.InvalidSlot
: module.Quantity <= 0
? ErrorCodes.WTFErrorMedicalAttentionSuggested
: module.IsDamaged
? ErrorCodes.ItemHasToBeRepaired
: !CheckUniqueModule(module)
? ErrorCodes.OnlyOnePerCategoryPerRobotAllowed
: ErrorCodes.NoError;
return IsRobotAllowed(module, robotDefinition)
? ErrorCodes.NotAllowedOnThisBot
: IsUsedSlot(slot)
? ErrorCodes.UsedSlot
: !IsValidSlotTo(module, slot)
? ErrorCodes.InvalidSlot
: module.Quantity <= 0
? ErrorCodes.WTFErrorMedicalAttentionSuggested
: module.IsDamaged
? ErrorCodes.ItemHasToBeRepaired
: !CheckUniqueModule(module)
? ErrorCodes.OnlyOnePerCategoryPerRobotAllowed
: ErrorCodes.NoError;
}

public void EquipModuleOrThrow(Module module, int slot)
private bool IsRobotAllowed(Module module, int robotDefinition = 0)
{
_ = CanEquipModule(module, slot).ThrowIfError();
return module.ED.Options.AllowedBots.Length > 0 && !module.ED.Options.AllowedBots.Contains(robotDefinition);
}

public void EquipModuleOrThrow(Module module, int slot, int robotDefinition = 0)
{
_ = CanEquipModule(module, slot, robotDefinition).ThrowIfError();
EquipModule(module, slot);
}

Expand Down
4 changes: 0 additions & 4 deletions src/Perpetuum/Zones/LandMines/LandMine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ public override void AcceptVisitor(IEntityVisitor visitor)
}
}

public override void OnUnitsFound(List<Player> unitsFound)
{
}

protected override ProximityDeviceBase GetThis()
{
return this;
Expand Down
2 changes: 0 additions & 2 deletions src/Perpetuum/Zones/ProximityProbes/ProximityDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ public virtual void OnDeviceCreated()


public abstract void OnUnitsFound(List<Robot> unitsFound);

public abstract void OnUnitsFound(List<Player> unitsFound);
#endregion

protected override void OnDead(Unit killer)
Expand Down
Loading