Skip to content

Commit

Permalink
AutoReplace with set Location
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Jul 28, 2018
1 parent ea74267 commit 95d3290
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 13 deletions.
12 changes: 8 additions & 4 deletions source/Components/AutoReplace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace CustomComponents
public class AutoReplace : SimpleCustomComponent, IOnItemGrabbed, IOnInstalled
{
public string ComponentDefId { get; set; }
public ChassisLocations Location { get; set; }

public void OnItemGrabbed(IMechLabDraggableItem item, MechLabPanel mechLab, MechLabLocationWidget widget)
{
Expand All @@ -18,8 +19,10 @@ public void OnItemGrabbed(IMechLabDraggableItem item, MechLabPanel mechLab, Mech
return;
}
Control.Logger.LogDebug($"-- {widget}");
DefaultHelper.AddMechLab(ComponentDefId, Def.ComponentType, new MechLabHelper(mechLab), widget.loadout.Location);
Control.Logger.LogDebug($"-- added {ComponentDefId}");
var location = Location == ChassisLocations.None ? widget.loadout.Location : Location;

DefaultHelper.AddMechLab(ComponentDefId, Def.ComponentType, new MechLabHelper(mechLab),location);
Control.Logger.LogDebug($"-- added {ComponentDefId} to {location}");
mechLab.ValidateLoadout(false);
}

Expand All @@ -29,8 +32,9 @@ public void OnInstalled(WorkOrderEntry_InstallComponent order, SimGameState stat
Control.Logger.LogDebug($"-- search replace for {order.MechComponentRef.ComponentDefID}");
if (order.PreviousLocation != ChassisLocations.None)
{
Control.Logger.LogDebug($"-- found, adding {ComponentDefId} to {order.PreviousLocation}");
DefaultHelper.AddInventory(ComponentDefId, mech, order.PreviousLocation, order.MechComponentRef.ComponentDefType, state);
var location = Location == ChassisLocations.None ? order.PreviousLocation : Location;
Control.Logger.LogDebug($"-- found, adding {ComponentDefId} to {location}");
DefaultHelper.AddInventory(ComponentDefId, mech, location, order.MechComponentRef.ComponentDefType, state);
}
else
{
Expand Down
25 changes: 21 additions & 4 deletions source/Components/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void OnInstalled(WorkOrderEntry_InstallComponent order, SimGameState stat

Control.Logger.LogDebug($"-- total {n1}/{CategoryDescriptor.MaxEquiped} location: {n2}/{CategoryDescriptor.MaxEquipedPerLocation}");

var replace = mech.Inventory.FirstOrDefault(i => i.MountedLocation == order.DesiredLocation && i.IsCategory(CategoryID) && i.IsDefault());
var replace = mech.Inventory.FirstOrDefault(i => (i.MountedLocation == order.DesiredLocation || CategoryDescriptor.ReplaceAnyLocation) && i.IsCategory(CategoryID) && i.IsDefault());

Control.Logger.LogDebug($"-- possible replace: {(replace == null ? "not found" : replace.ComponentDefID)}");

Expand All @@ -103,7 +103,7 @@ public void OnInstalled(WorkOrderEntry_InstallComponent order, SimGameState stat
Control.Logger.LogDebug($"-- need_repalce: {need_replace}");

if (need_replace)
DefaultHelper.RemoveInventory(replace.ComponentDefID, mech, order.DesiredLocation, replace.ComponentDefType);
DefaultHelper.RemoveInventory(replace.ComponentDefID, mech, replace.MountedLocation, replace.ComponentDefType);

}

Expand All @@ -117,7 +117,7 @@ public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelp
CategoryDescriptor.MaxEquiped <= 0 && CategoryDescriptor.MaxEquipedPerLocation <= 0)
{
#if CCDEBUG
Control.Logger.LogDebug($"--- no replace needed");
Control.Logger.LogDebug($"--- no replace needed");
#endif
return String.Empty;
}
Expand Down Expand Up @@ -159,8 +159,25 @@ public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelp

if (n >= CategoryDescriptor.MaxEquiped)
{
var replace = location.LocalInventory
var
replace = location.LocalInventory
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID));
if (CategoryDescriptor.ReplaceAnyLocation && replace == null)
{
var mechlab = new MechLabHelper(location.mechLab);
foreach (var widget in mechlab.GetWidgets())
{
if(widget.loadout.Location == location.widget.loadout.Location)
continue;

var loc_helper = new LocationHelper(widget);
replace = loc_helper.LocalInventory
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID));
if(replace != null)
break;
}
}

#if CCDEBUG
Control.Logger.LogDebug($"--- replace: {(replace == null ? "none" : replace.ComponentRef.ComponentDefID)}");
#endif
Expand Down
2 changes: 1 addition & 1 deletion source/Components/RequieredCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void ValidateMech(Dictionary<MechValidationType, List<string>> errors, Me
if (mechDef.Inventory.Any(i => i.IsCategory(CategoryID)))
return;
errors[MechValidationType.InvalidInventorySlots].Add(string.IsNullOrEmpty(ErrorMessage) ?
$"{Def.Description.Name} require {category.displayName} installed" :
$"{Def.Description.Name} requires {category.displayName} installed" :
ErrorMessage);
}

Expand Down
2 changes: 1 addition & 1 deletion source/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void Init(string directory, string settingsJSON)
Validator.RegisterMechValidator(CategoryController.ValidateMech, CategoryController.ValidateMechCanBeFielded);


Logger.Log("Loaded CustomComponents v0.6.2.1.0");
Logger.Log("Loaded CustomComponents v0.6.3.0.3");
#if CCDEBUG
Logger.LogDebug("Loading Categories");
#endif
Expand Down
4 changes: 4 additions & 0 deletions source/Helpers/CategoryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public string DisplayName
[DefaultValue(false), JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool AutoReplace = false;


[DefaultValue(false), JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool ReplaceAnyLocation = false;

/// <summary>
/// max of items per mech
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion source/Helpers/DefaultHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static void AddMechLab(string id, ComponentType type, MechLabHelper mechL
}

var slot = CreateSlot(id, type, mechLab.MechLab);
slot.MountedLocation = location;
target.OnAddItem(slot, false);
}

Expand Down Expand Up @@ -246,7 +247,11 @@ internal static MechComponentRef[] ClearInventory(MechDef source, SimGameState s
if (list[i].Is<AutoReplace>(out var replace))
{
var ref_item = CreateRef(replace.ComponentDefId, list[i].ComponentDefType, list[i].DataManager, state);
ref_item.SetData(list[i].MountedLocation, list[i].HardpointSlot, list[i].DamageLevel);
var location = replace.Location == ChassisLocations.None
? list[i].MountedLocation
: replace.Location;

ref_item.SetData(location, list[i].HardpointSlot, list[i].DamageLevel);
ref_item.SetSimGameUID(state.GenerateSimGameUID());
result_list.Add(ref_item);
Control.Logger.LogDebug($"-- Replace with {ref_item.ComponentDefID} - {ref_item.SimGameUID}");
Expand Down
16 changes: 15 additions & 1 deletion source/Helpers/MechLabHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BattleTech;
using System.Collections.Generic;
using BattleTech;
using BattleTech.UI;
using Harmony;

Expand All @@ -12,6 +13,19 @@ public class MechLabHelper
private Traverse main;
private Traverse drag_item;

public IEnumerable<MechLabLocationWidget> GetWidgets()
{
yield return MechLab.headWidget;
yield return MechLab.leftArmWidget;
yield return MechLab.leftTorsoWidget;
yield return MechLab.centerTorsoWidget;
yield return MechLab.rightTorsoWidget;
yield return MechLab.rightArmWidget;
yield return MechLab.leftLegWidget;
yield return MechLab.rightLegWidget;
}


public MechLabLocationWidget GetLocationWidget(ChassisLocations location)
{
switch (location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool do_cancel(string error)
List<IChange> changes = new List<IChange>();
changes.Add(new AddChange(__instance.loadout.Location, dragItem));
if (replaceItem != null)
changes.Add(new RemoveChange(__instance.loadout.Location, replaceItem));
changes.Add(new RemoveChange(replaceItem.MountedLocation, replaceItem));



Expand Down
24 changes: 24 additions & 0 deletions source/Validators/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ internal static void ValidateMech(Dictionary<MechValidationType, List<string>> e
{
validator(errors, validationLevel, mechDef);
}

var sizes = mechDef.Inventory.Select(cref =>
new { location = cref.MountedLocation, size = cref.Def.InventorySize })
.GroupBy(i => i.location)
.Select(i => new { location = i.Key, size = i.Sum(a => a.size) }).ToList();

foreach (var size in sizes)
{
if (mechDef.GetChassisLocationDef(size.location).InventorySlots < size.size)
{
errors[MechValidationType.InvalidInventorySlots].Add($"{size.location} no space left, remove excess equipment");
}
}
}

internal static bool ValidateMechCanBeFielded(MechDef mechDef)
Expand All @@ -216,6 +229,17 @@ internal static bool ValidateMechCanBeFielded(MechDef mechDef)
return false;
}

var sizes = mechDef.Inventory.Select(cref =>
new {location = cref.MountedLocation, size = cref.Def.InventorySize})
.GroupBy(i => i.location)
.Select(i => new {location = i.Key, size = i.Sum(a => a.size)}).ToList();

foreach (var size in sizes)
{
if (mechDef.GetChassisLocationDef(size.location).InventorySlots < size.size)
return false;
}

return true;
}
}
Expand Down

0 comments on commit 95d3290

Please sign in to comment.