Skip to content

Commit

Permalink
rewrote detour
Browse files Browse the repository at this point in the history
  • Loading branch information
Skullywag committed Aug 4, 2016
1 parent 2227caa commit 5690e13
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 18 deletions.
Binary file modified Assemblies/ReclaimFabric.dll
Binary file not shown.
11 changes: 2 additions & 9 deletions Defs/RecipeDefs/RecycleApparel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,9 @@
<categories>
<li>Apparel</li>
</categories>
<exceptedThingDefs>
<li>Apparel_PowerArmor</li>
<li>Apparel_PersonalShield</li>
<li>Apparel_PsychicFoilHelmet</li>
<li>Apparel_KevlarHelmet</li>
<li>Apparel_PowerArmorHelmet</li>
<li>Apparel_VestPlate</li>
</exceptedThingDefs>
</fixedIngredientFilter>
<workSkill>Crafting</workSkill>
<workSkill>Crafting</workSkill>
<efficiencyStat>TailoringSpeed</efficiencyStat>
</RecipeDef>


Expand Down
39 changes: 39 additions & 0 deletions Defs/SpecialThingFilterDefs/SpecialThingFilters.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<SpecialThingFilters>

<SpecialThingFilterDef>
<defName>ReclaimArmor</defName>
<label>disallow armour</label>
<description>Disallow armour.</description>
<parentCategory>Apparel</parentCategory>
<allowedByDefault>false</allowedByDefault>
<saveKey>ReclaimArmor</saveKey>
<configurable>false</configurable>
<workerClass>ReclaimFabric.SpecialThingFilterWorker_ReclaimArmor</workerClass>
</SpecialThingFilterDef>

<SpecialThingFilterDef>
<defName>ReclaimAdvancedArmor</defName>
<label>disallow advanced armour</label>
<description>Disallow advanced armour.</description>
<parentCategory>Apparel</parentCategory>
<allowedByDefault>false</allowedByDefault>
<saveKey>ReclaimAdvancedArmor</saveKey>
<configurable>false</configurable>
<workerClass>ReclaimFabric.SpecialThingFilterWorker_ReclaimAdvancedArmor</workerClass>
</SpecialThingFilterDef>

<SpecialThingFilterDef>
<defName>ReclaimEnergyShields</defName>
<label>disallow energy shields</label>
<description>Disallow energy shields.</description>
<parentCategory>Apparel</parentCategory>
<allowedByDefault>false</allowedByDefault>
<saveKey>ReclaimEnergyShields</saveKey>
<configurable>false</configurable>
<workerClass>ReclaimFabric.SpecialThingFilterWorker_ReclaimEnergyShields</workerClass>
</SpecialThingFilterDef>

</SpecialThingFilters>


Binary file modified Source/.vs/ReclaimFabric/v14/.suo
Binary file not shown.
258 changes: 258 additions & 0 deletions Source/ReclaimFabric/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;
using RimWorld;
using Verse;

namespace ReclaimFabric
{

public static class Helper
{

public static bool IsClothes( this ThingDef def )
{
return(
( !def.menuHidden )&&
(
( def.thingClass == typeof( Apparel ) )||
( def.thingClass.IsSubclassOf( typeof( Apparel ) ) )
)&&
( !def.apparel.tags.NullOrEmpty() )&&
(
( !def.apparel.tags.Contains( "Military" ) )&&
( !def.apparel.tags.Contains( "PersonalShield" ) )
)
);
}

public static bool IsAdvancedArmor( this ThingDef def )
{
return(
( !def.menuHidden )&&
(
( def.thingClass == typeof( Apparel ) )||
( def.thingClass.IsSubclassOf( typeof( Apparel ) ) )
)&&
( !def.apparel.tags.NullOrEmpty() )&&
(
( def.apparel.tags.Contains( "Military" ) )&&
( def.apparel.tags.Contains( "Spacer" ) )
)
);
}

public static bool IsArmor( this ThingDef def )
{
return(
( !def.menuHidden )&&
(
( def.thingClass == typeof( Apparel ) )||
( def.thingClass.IsSubclassOf( typeof( Apparel ) ) )
)&&
( !def.apparel.tags.NullOrEmpty() )&&
(
( def.apparel.tags.Contains( "Military" ) )&&
( !def.apparel.tags.Contains( "Spacer" ) )
)
);
}

public static bool IsEnergyWeapon( this ThingDef def )
{
return(
( !def.menuHidden )&&
( !def.weaponTags.NullOrEmpty() )&&
(
( def.weaponTags.Contains( "AdvancedGun" ) )||
( def.weaponTags.Contains( "GrenadeEMP" ) )
)
);
}

public static bool IsGunpowderWeapon( this ThingDef def )
{
return(
( !def.menuHidden )&&
( !def.weaponTags.NullOrEmpty() )&&
(
( def.weaponTags.Contains( "Gun" ) )||
( def.weaponTags.Contains( "GunHeavy" ) )||
( def.weaponTags.Contains( "GrenadeDestructive" ) )
)&&
( !def.weaponTags.Contains( "AdvancedGun" ) )
);
}

public static bool IsMeleeWeapon( this ThingDef def )
{
return(
( !def.menuHidden )&&
( !def.weaponTags.NullOrEmpty() )&&
(
( def.weaponTags.Contains( "Melee" ) )||
( def.weaponTags.Contains( "NeolithicMelee" ) )
)
);
}

public static bool IsRangedWeapon( this ThingDef def )
{
return(
( !def.menuHidden )&&
( !def.weaponTags.NullOrEmpty() )&&
(
( def.weaponTags.Contains( "NeolithicRanged" ) )
)
);
}

public static bool IsEnergyShield( this ThingDef def )
{
return(
( !def.menuHidden )&&
(
( def.thingClass == typeof( Apparel ) )||
( def.thingClass.IsSubclassOf( typeof( Apparel ) ) )
)&&
( !def.apparel.tags.NullOrEmpty() )&&
(
( def.apparel.tags.Contains( "PersonalShield" ) )
)
);
}

public static ThingDef MendWithDef( this Thing thing )
{
if( thing.Stuff != null )
{
// Use stuff
return thing.Stuff;
}
if( thing.def.costList != null )
{
// Use most abundant ingredient requirement
var repairWith = (ThingDef) null;
int num = 0;
foreach( var thingCount in thing.def.costList )
{
if( thingCount.count > num )
{
repairWith = thingCount.thingDef;
num = thingCount.count;
}
}
return repairWith;
}
// Can't make this item, take a "best guess"
// TODO: Search recipes for ones with products of the thing to mend
if( thing.def.IsClothes() )
{
if(
( !thing.def.apparel.tags.NullOrEmpty() )&&
( thing.def.apparel.tags.Contains( "Spacer" ) )
)
{
return ThingDef.Named( "Synthread" );
}
return ThingDefOf.Cloth;
}
if(
( thing.def.IsAdvancedArmor() )||
( thing.def.IsEnergyShield() )||
( thing.def.IsEnergyWeapon() )
)
{
return ThingDefOf.Plasteel;
}
return ThingDefOf.Steel;
}

public static int MendCountMax( this Thing thing )
{
float baseCount = 0;
if( thing.Stuff != null )
{
// Use stuff
baseCount = thing.def.costStuffCount;
if( thing.def.costList != null )
{
// Add any cost list based ingredient which is the same as stuff
foreach( var thingCount in thing.def.costList )
{
if( thingCount.thingDef == thing.Stuff )
{
baseCount += thingCount.count;
}
}
}
}
else
{
if( thing.def.costList != null )
{
// Use most abundant ingredient requirement
foreach( var thingCount in thing.def.costList )
{
if( thingCount.count > baseCount )
{
baseCount = thingCount.count;
}
}
}
else
{
// No recipe maker for this item, take a "best guess"
// TODO: Search recipes for ones with products of the thing to mend
var repairWith = MendWithDef ( thing );
if( repairWith == null )
{
// No resource, 0 count
return 0;
}
// Take item base value
var marketValue = thing.def.BaseMarketValue;
// And the resource value
var valuePerResource = repairWith.BaseMarketValue;
// Get the amount of work using this stuff
var statValueAbstract = StatExtension.GetStatValueAbstract( thing.def, StatDefOf.WorkToMake, repairWith );
// Calculate labour value (see StatWorker_MarketValue.GetValueUnfinalized)
var labourValue = (float) Mathf.RoundToInt( statValueAbstract / 0.004f );
// Calculate base material cost
var materialCost = marketValue - labourValue;
// Calculcate resource count from material cost / cost per material
baseCount = Mathf.Max( 1, ( materialCost / valuePerResource ) );
}
}
// Return resource count
return (int) baseCount;
}

public static float MendCountPerHitPoint( this Thing thing )
{
float countMax = MendCountMax( thing ) * 10f;
if( countMax < 1 )
{
return 0;
}
// Round to 0.1 (min) per hit point
var perHP = countMax / (float) thing.MaxHitPoints;
return Mathf.Max( 1, Mathf.RoundToInt( countMax / (float)thing.MaxHitPoints ) ) * 0.1f;
}

public static int MendCountRequired( this Thing thing )
{
// Repair amount
var repairAmount = (float) thing.MaxHitPoints - (float) thing.HitPoints;
// Resources per hit point
var perHP = MendCountPerHitPoint( thing );
// Return resource count per hitpoint * number of hitpoints to repair
return (int) ( perHP * repairAmount );
}

}

}
4 changes: 4 additions & 0 deletions Source/ReclaimFabric/ReclaimFabric.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DetourInjector.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpecialThingFilterWorker_ReclaimAdvancedArmor.cs" />
<Compile Include="SpecialThingFilterWorker_ReclaimArmor.cs" />
<Compile Include="SpecialThingFilterWorker_ReclaimEnergyShields.cs" />
<Compile Include="_Thing.cs" />
</ItemGroup>
<ItemGroup />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Verse;

namespace ReclaimFabric
{

public class SpecialThingFilterWorker_ReclaimAdvancedArmor : SpecialThingFilterWorker
{

public override bool Matches( Thing t )
{
return AlwaysMatches( t.def );
}

public override bool AlwaysMatches( ThingDef def )
{
return def.IsAdvancedArmor();
}

}

}
21 changes: 21 additions & 0 deletions Source/ReclaimFabric/SpecialThingFilterWorker_ReclaimArmor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Verse;

namespace ReclaimFabric
{

public class SpecialThingFilterWorker_ReclaimArmor : SpecialThingFilterWorker
{

public override bool Matches( Thing t )
{
return AlwaysMatches( t.def );
}

public override bool AlwaysMatches( ThingDef def )
{
return def.IsArmor();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Verse;

namespace ReclaimFabric
{

public class SpecialThingFilterWorker_ReclaimEnergyShields : SpecialThingFilterWorker
{

public override bool Matches( Thing t )
{
return AlwaysMatches( t.def );
}

public override bool AlwaysMatches( ThingDef def )
{
return def.IsEnergyShield();
}

}

}
Loading

0 comments on commit 5690e13

Please sign in to comment.