Skip to content

Commit

Permalink
Fix for Hardpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Feb 3, 2019
1 parent db4c33c commit 45f2264
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 35 deletions.
2 changes: 1 addition & 1 deletion source/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void Init(string directory, string settingsJSON)
Registry.RegisterSimpleCustomComponents(Assembly.GetExecutingAssembly());
Validator.RegisterMechValidator(CategoryController.ValidateMech, CategoryController.ValidateMechCanBeFielded);

Logger.Log("Loaded CustomComponents v0.9.1.1 for bt 1.4");
Logger.Log("Loaded CustomComponents v0.9.1.2 for bt 1.4");
#if CCDEBUG
Logger.LogDebug("Loading Categories");
#endif
Expand Down
2 changes: 1 addition & 1 deletion source/CustomComponentSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class CustomComponentSettings
public List<DefaultsInfo> TaggedDefaults = new List<DefaultsInfo>();
public List<DefaultsInfo> Defaults = new List<DefaultsInfo>();

public bool FixWrongDefaults = true;
public bool FixSaveGameMech = true;
public bool TagRestrictionDropValidateRequiredTags = false;
public bool TagRestrictionDropValidateIncompatibleTags = true;

Expand Down
28 changes: 28 additions & 0 deletions source/Helpers/CategoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,36 @@ public static bool IsCategory(this MechComponentRef cref, string categoryid, out
return category != null;
}

private static void remove_per_location(MechDef mechDef)
{
var items_by_category = mechDef.Inventory
.Select(item => new { item, def = item.Def.GetComponents<Category>().Where(i => i.CategoryDescriptor.MaxEquipedPerLocation > 0) })
.Where(i => i.def != null)
.SelectMany(@t => t.def.Select(item => new
{
category = item.CategoryDescriptor,
itemref = @t.item,
}))
.GroupBy(i => i.category)
.ToDictionary(i => i.Key, i => i.Select(item => item.itemref).ToList());

foreach (var pair in items_by_category)
{

}
}

public static void RemoveExcessDefaults(MechDef mechDef)
{
//remove location based defaults
remove_per_location(mechDef);




//remove other defaults


var items_by_category = mechDef.Inventory
.Select(item => new { item, def = item.Def.GetComponents<Category>().Where(i => i.CategoryDescriptor.MaxEquiped > 0)})
.Where(i => i.def != null)
Expand Down
18 changes: 14 additions & 4 deletions source/Helpers/DefaultFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,21 @@ public static void FixMech(MechDef mechDef, SimGameState state)
for (int i =0;i<num_changed;i++)
Control.Logger.LogDebug($"---- {changed_deafult[i]}");
#endif
}

public static void FixSavedMech(MechDef mechDef, SimGameState state)
{
ReAddFixed(mechDef, state);

FixMech(mechDef, state);

//if (Control.Settings.FixWrongDefaults)
//{
// CategoryController.RemoveExcessDefaults(mechDef);
//}
//CategoryController.RemoveExcessDefaults(mechDef);
}

private static void ReAddFixed(MechDef mechDef, SimGameState state)
{
mechDef.SetInventory(mechDef.Inventory.Where(i => i.IsModuleFixed(mechDef)).ToArray());
mechDef.Refresh();
}

public static MechComponentRef GetReplaceFor(MechDef mech, string categoryId, ChassisLocations location, SimGameState state)
Expand Down
19 changes: 15 additions & 4 deletions source/Patches/SimGameState_Rehydrate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ public static void FixMechInMechbay(SimGameState __instance, StatCollection ___c
{
try
{
foreach (var pair in ___ActiveMechs)
DefaultFixer.FixMech(pair.Value, __instance);
foreach (var pair in ___ReadyingMechs)
DefaultFixer.FixMech(pair.Value, __instance);

if (Control.Settings.FixSaveGameMech)
{
foreach (var pair in ___ActiveMechs)
DefaultFixer.FixSavedMech(pair.Value, __instance);
foreach (var pair in ___ReadyingMechs)
DefaultFixer.FixSavedMech(pair.Value, __instance);
}
else
{
foreach (var pair in ___ActiveMechs)
DefaultFixer.FixMech(pair.Value, __instance);
foreach (var pair in ___ReadyingMechs)
DefaultFixer.FixMech(pair.Value, __instance);
}

}
catch (Exception e)
Expand Down
44 changes: 19 additions & 25 deletions source/Validators/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static string ValidateSize(MechLabItemSlotElement drop_item, MechDef mec
.Select(s => new { location = s.Key, val = s.Sum(i => i.val) });

Control.Logger.LogDebug($"drop_item={drop_item.ComponentRef.Def.Description.Id}");
foreach(var location in change_by_location)
foreach (var location in change_by_location)
{
Control.Logger.LogDebug($"location={location.location}");
foreach (var item in mech.Inventory.Where(i => i.MountedLocation == location.location))
Expand Down Expand Up @@ -143,22 +143,17 @@ private static string ValidateJumpJets(MechLabItemSlotElement drop_item, MechDef

private static string ValidateHardpoint(MechLabItemSlotElement drop_item, LocationHelper location, List<IChange> changes)
{

#if CCDEBUG
Control.Logger.LogDebug($"-- Hardpoints");
#endif
// if dropped item not weapon - skip check
if (drop_item.ComponentRef.Def.ComponentType != ComponentType.Weapon)
{
#if CCDEBUG
Control.Logger.LogDebug($"--- Not a weapon");
return string.Empty;

if (changes == null || changes.Count == 0 || !(changes[0] is RemoveChange remove))
return string.Empty;

var current_replace = remove.item;

// if dropped item and replacement both same type weapon - allow replace
if (current_replace != null
&& current_replace.ComponentRef.Def.ComponentType == ComponentType.Weapon
&& current_replace.weaponDef.Category == drop_item.weaponDef.Category)
return string.Empty;

#endif
}

//calculate hardpoint
int num = 0;
Expand All @@ -184,13 +179,10 @@ private static string ValidateHardpoint(MechLabItemSlotElement drop_item, Locati
break;
}

if (current_replace != null)
{
if (num >= num2)
return $"Cannot add {weaponDef.Description.Name} to {location.LocationName}: There are no available {weaponDef.Category.ToString()} hardpoints.";
}
if(num2 == 0 )
return $"Cannot add {weaponDef.Description.Name} to {location.LocationName}: There are no available {weaponDef.Category.ToString()} hardpoints.";

else if (num == num2)
if (num == num2)
{
var mech = location.mechLab.activeMechDef;

Expand All @@ -199,15 +191,17 @@ private static string ValidateHardpoint(MechLabItemSlotElement drop_item, Locati
&& def.Category == weaponDef.Category
&& def.Description.Id != drop_item.ComponentRef.ComponentDefID
&& !i.ComponentRef.IsModuleFixed(mech));

if (replace == null)
return $"Cannot add {weaponDef.Description.Name} to {location.LocationName}: There are no available {weaponDef.Category.ToString()} hardpoints.";
return
$"Cannot add {weaponDef.Description.Name} to {location.LocationName}: There are no available {weaponDef.Category.ToString()} hardpoints.";
else
current_replace = replace;
changes.Add(new RemoveChange(location.widget.loadout.Location, replace));
}
else if (num > num2)
return $"Cannot add {weaponDef.Description.Name} to {location.LocationName}: There are no available {weaponDef.Category.ToString()} hardpoints.";

return string.Empty;
return string.Empty;
}

internal static void ValidateMech(Dictionary<MechValidationType, List<Localize.Text>> errors,
Expand Down Expand Up @@ -241,9 +235,9 @@ internal static bool ValidateMechCanBeFielded(MechDef mechDef)
}

var sizes = mechDef.Inventory.Select(cref =>
new {location = cref.MountedLocation, size = cref.Def.InventorySize})
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();
.Select(i => new { location = i.Key, size = i.Sum(a => a.size) }).ToList();

foreach (var size in sizes)
{
Expand Down

0 comments on commit 45f2264

Please sign in to comment.