Skip to content

Commit

Permalink
forbid replacement for FixedEquipmend
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Nov 28, 2018
1 parent c635926 commit b09d168
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
13 changes: 7 additions & 6 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 || CategoryDescriptor.ReplaceAnyLocation) && i.IsCategory(CategoryID) && i.IsDefault());
var replace = mech.Inventory.FirstOrDefault(i => !i.IsModuleFixed(mech) && (i.MountedLocation == order.DesiredLocation || CategoryDescriptor.ReplaceAnyLocation) && i.IsCategory(CategoryID) && i.IsDefault());

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

Expand Down Expand Up @@ -146,6 +146,7 @@ public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelp

}
#endif
var mech = location.mechLab.activeMechDef;

if (CategoryDescriptor.MaxEquiped > 0)
{
Expand All @@ -159,9 +160,9 @@ public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelp

if (n >= CategoryDescriptor.MaxEquiped)
{
var
replace = location.LocalInventory
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID));
var replace = location.LocalInventory
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID) && !i.ComponentRef.IsModuleFixed(mech));

if (CategoryDescriptor.ReplaceAnyLocation && replace == null)
{
var mechlab = new MechLabHelper(location.mechLab);
Expand All @@ -172,7 +173,7 @@ public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelp

var loc_helper = new LocationHelper(widget);
replace = loc_helper.LocalInventory
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID));
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID) && !i.ComponentRef.IsModuleFixed(mech));
if(replace != null)
break;
}
Expand Down Expand Up @@ -216,7 +217,7 @@ public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelp


var replace = location.LocalInventory
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID));
.FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID) && !i.ComponentRef.IsModuleFixed(mech));
#if CCDEBUG
Control.Logger.LogDebug($"--- replace: {(replace == null ? "none" : replace.ComponentRef.ComponentDefID)}");
#endif
Expand Down
2 changes: 1 addition & 1 deletion source/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void Init(string directory, string settingsJSON)
Registry.RegisterSimpleCustomComponents(Assembly.GetExecutingAssembly());
Validator.RegisterMechValidator(CategoryController.ValidateMech, CategoryController.ValidateMechCanBeFielded);

Logger.Log("Loaded CustomComponents v0.8.0.0.0 for bt 1.3");
Logger.Log("Loaded CustomComponents v0.8.0.1.0 for bt 1.3");
#if CCDEBUG
Logger.LogDebug("Loading Categories");
#endif
Expand Down
6 changes: 3 additions & 3 deletions source/CustomComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<OutputPath>..\</OutputPath>
<DefineConstants>TRACE;DEBUG;CCDEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
Expand All @@ -27,7 +27,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\</OutputPath>
<DefineConstants>TRACE;CCDEBUG</DefineConstants>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
Expand Down
33 changes: 33 additions & 0 deletions source/Helpers/DefaultHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@ public static bool IsDefault(this MechComponentRef cref)
return cref.Is<Flags>(out var f) && f.Default;
}

public static bool IsModuleFixed(this MechComponentRef item, MechDef mech)
{
#if CCDEBUG
Control.Logger.LogError($"IsModuleFixed: {item.ComponentDefID}");
#endif
if (!item.IsFixed)
{
#if CCDEBUG
Control.Logger.LogError($"-- false: not fixed");
#endif

return false;

}

foreach (var mref in mech.Chassis.FixedEquipment)
{

if (mref.MountedLocation == item.MountedLocation && item.ComponentDefID == mref.ComponentDefID)
{
#if CCDEBUG
Control.Logger.LogError($"-- true!");
#endif
return true;
}
}
#if CCDEBUG
Control.Logger.LogError($"-- false: not really fixed");
#endif

return false;
}

#endregion


Expand Down
8 changes: 7 additions & 1 deletion source/Validators/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,16 @@ private static string ValidateHardpoint(MechLabItemSlotElement drop_item, Locati
if (num >= num2)
return $"Cannot add {weaponDef.Description.Name} to {location.LocationName}: There are no available {weaponDef.Category.ToString()} hardpoints.";
}

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

var replace = location.LocalInventory.FirstOrDefault(i =>
(i?.ComponentRef?.Def is WeaponDef def) && def.Category == weaponDef.Category && def.Description.Id != drop_item.ComponentRef.ComponentDefID);
(i?.ComponentRef?.Def is WeaponDef def)
&& 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.";
else
Expand Down

0 comments on commit b09d168

Please sign in to comment.