Skip to content

Commit

Permalink
add bool AllowSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
Cn-mjt44 committed Mar 28, 2024
1 parent cc45f8f commit f78c2c9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 72 deletions.
1 change: 1 addition & 0 deletions RW_ModularizationWeapon/AI/JobDriver_ModifyWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected override IEnumerable<Toil> MakeNewToils()
CompModularizationWeapon comp = TargetB.Thing;
if (comp != null)
{
if (!comp.AllowSwap) this.EndJobWith(JobCondition.Incompletable);
Toil toil = new Toil();
toil.initAction = delegate ()
{
Expand Down
144 changes: 74 additions & 70 deletions RW_ModularizationWeapon/CompModularizationWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,28 +371,28 @@ public bool AllowPart(Thing part, string id = null, bool checkOccupy = true)

public void SetPartToDefault()
{
foreach (WeaponAttachmentProperties properties in Props.attachmentProperties)
if (AllowSwap)
{
ThingDef def = properties.defultThing;
if (def != null)
foreach (WeaponAttachmentProperties properties in Props.attachmentProperties)
{
Thing thing = ThingMaker.MakeThing(def, GenStuff.RandomStuffFor(def));
thing.TryGetComp<CompQuality>()?.SetQuality(QualityUtility.GenerateQualityRandomEqualChance(), ArtGenerationContext.Colony);
SetTargetPart(properties.id, thing);
ThingDef def = properties.defultThing;
if (def != null)
{
Thing thing = ThingMaker.MakeThing(def, GenStuff.RandomStuffFor(def));
thing.TryGetComp<CompQuality>()?.SetQuality(QualityUtility.GenerateQualityRandomEqualChance(), ArtGenerationContext.Colony);
SetTargetPart(properties.id, thing);
}
else SetTargetPart(properties.id, null);
ChildNodes[properties.id]?.Destroy();
}
else SetTargetPart(properties.id, null);
ChildNodes[properties.id]?.Destroy();
}
foreach (Thing thing in targetPartsWithId.Values)
{
CompModularizationWeapon comp = thing;
if (comp?.Props.setRandomPartWhenCreate ?? false)
foreach (Thing thing in targetPartsWithId.Values)
{
comp?.SetPartToDefault();
CompModularizationWeapon comp = thing;
if (comp?.Props.setRandomPartWhenCreate ?? false)
{
comp?.SetPartToDefault();
}
}
}
if (!Occupyed)
{
SwapTargetPart();
ClearTargetPart();
}
Expand All @@ -402,69 +402,69 @@ public void SetPartToDefault()
public void SetPartToRandom()
{

//Console.WriteLine($"==================================== {parent}.SetPartToRandom Start ====================================");
foreach (WeaponAttachmentProperties properties in Props.attachmentProperties)
//Console.WriteLine($"==================================== {parent}.SetPartToRandom End ====================================");
if (AllowSwap)
{
if(properties.randomThingDefWeights.NullOrEmpty())
//Console.WriteLine($"==================================== {parent}.SetPartToRandom Start ====================================");
foreach (WeaponAttachmentProperties properties in Props.attachmentProperties)
{
for (int i = 0; i < 3; i++)
if (properties.randomThingDefWeights.NullOrEmpty())
{
int j = Rand.Range(0, properties.allowEmpty ? (properties.filter.AllowedDefCount + 1) : properties.filter.AllowedDefCount);
ThingDef def = j < properties.filter.AllowedDefCount ? properties.filter.AllowedThingDefs.ToList()[j] : null;
if (def != null)
for (int i = 0; i < 3; i++)
{
Thing thing = ThingMaker.MakeThing(def, GenStuff.RandomStuffFor(def));
thing.TryGetComp<CompQuality>()?.SetQuality(QualityUtility.GenerateQualityRandomEqualChance(), ArtGenerationContext.Outsider);
if (SetTargetPart(properties.id, thing))
int j = Rand.Range(0, properties.allowEmpty ? (properties.filter.AllowedDefCount + 1) : properties.filter.AllowedDefCount);
ThingDef def = j < properties.filter.AllowedDefCount ? properties.filter.AllowedThingDefs.ToList()[j] : null;
if (def != null)
{
ChildNodes[properties.id]?.Destroy();
break;
Thing thing = ThingMaker.MakeThing(def, GenStuff.RandomStuffFor(def));
thing.TryGetComp<CompQuality>()?.SetQuality(QualityUtility.GenerateQualityRandomEqualChance(), ArtGenerationContext.Outsider);
if (SetTargetPart(properties.id, thing))
{
ChildNodes[properties.id]?.Destroy();
break;
}
}
else break;
}
else break;
}
}
else
{
float count = 0;
properties.randomThingDefWeights.ForEach(x => count += x.count);
for (int i = 0; i < 3; i++)
else
{
float j = Rand.Range(0, count);
float k = 0;
ThingDef def = null;
foreach(ThingDefCountClass weight in properties.randomThingDefWeights)
float count = 0;
properties.randomThingDefWeights.ForEach(x => count += x.count);
for (int i = 0; i < 3; i++)
{
float next = k + weight.count;
if (k <= j && next >= j) def = weight.thingDef;
k = next;
}
if (def != null)
{
Thing thing = ThingMaker.MakeThing(def, GenStuff.RandomStuffFor(def));
thing.TryGetComp<CompQuality>()?.SetQuality(QualityUtility.GenerateQualityRandomEqualChance(), ArtGenerationContext.Outsider);
if (SetTargetPart(properties.id, thing))
float j = Rand.Range(0, count);
float k = 0;
ThingDef def = null;
foreach (ThingDefCountClass weight in properties.randomThingDefWeights)
{
ChildNodes[properties.id]?.Destroy();
break;
float next = k + weight.count;
if (k <= j && next >= j) def = weight.thingDef;
k = next;
}
if (def != null)
{
Thing thing = ThingMaker.MakeThing(def, GenStuff.RandomStuffFor(def));
thing.TryGetComp<CompQuality>()?.SetQuality(QualityUtility.GenerateQualityRandomEqualChance(), ArtGenerationContext.Outsider);
if (SetTargetPart(properties.id, thing))
{
ChildNodes[properties.id]?.Destroy();
break;
}
}
else break;
}
else break;
}
//Console.WriteLine($"{parent}[{properties.id}]:{ChildNodes[properties.id]},{GetTargetPart(properties.id)}");
}
//Console.WriteLine($"{parent}[{properties.id}]:{ChildNodes[properties.id]},{GetTargetPart(properties.id)}");
}
foreach (Thing thing in targetPartsWithId.Values)
{
CompModularizationWeapon comp = thing;
if (comp?.Props.setRandomPartWhenCreate ?? false)
foreach (Thing thing in targetPartsWithId.Values)
{
comp?.SetPartToRandom();
CompModularizationWeapon comp = thing;
if (comp?.Props.setRandomPartWhenCreate ?? false)
{
comp?.SetPartToRandom();
}
}
}
//Console.WriteLine($"==================================== {parent}.SetPartToRandom End ====================================");
if (!Occupyed)
{
SwapTargetPart();
ClearTargetPart();
}
Expand All @@ -477,7 +477,7 @@ protected override IEnumerable<Thing> PostGenRecipe_MakeRecipeProducts(RecipeDef
{
SetPartToDefault();
}
else if(invokeSource == RecipeInvokeSource.ingredients)
else if(invokeSource == RecipeInvokeSource.ingredients && AllowSwap)
{
IEnumerable<Thing> Ingredients(IEnumerable<Thing> org)
{
Expand Down Expand Up @@ -607,7 +607,7 @@ private bool CheckAndSetTargetCache()
}


private bool CheckTargetVaild()
private bool CheckTargetVaild(bool deSpawn)
{
bool result = true;
foreach (string id in this.PartIDs)
Expand All @@ -616,7 +616,10 @@ private bool CheckTargetVaild()
{
if (target.HasThing && target.Thing.holdingOwner != null)
{
if (target.Thing.Spawned && parent.MapHeld != null && target.Thing.Map == parent.MapHeld) target.Thing.DeSpawn();
if (target.Thing.Spawned && parent.MapHeld != null && target.Thing.Map == parent.MapHeld)
{
if (deSpawn) target.Thing.DeSpawn();
}
else
{
SetTargetPart(id, ChildNodes[id]);
Expand All @@ -626,7 +629,7 @@ private bool CheckTargetVaild()
}
if (NodeProccesser.AllowNode(target.Thing, id))
{
result = (((CompModularizationWeapon)target.Thing)?.CheckTargetVaild() ?? true) && result;
result = (((CompModularizationWeapon)target.Thing)?.CheckTargetVaild(deSpawn) ?? true) && result;
}
else
{
Expand All @@ -636,7 +639,7 @@ private bool CheckTargetVaild()
}
else
{
result = (((CompModularizationWeapon)ChildNodes[id])?.CheckTargetVaild() ?? true) && result;
result = (((CompModularizationWeapon)ChildNodes[id])?.CheckTargetVaild(deSpawn) ?? true) && result;
}
}
return result;
Expand All @@ -649,13 +652,14 @@ protected override bool PreUpdateNode(CompChildNodeProccesser actionNode, Dictio
ChildNodes[keyValue.Key] = keyValue.Value;
}
CompModularizationWeapon root = RootPart;
bool occupyed = root.Occupyed;
//Log.Message($"{parent} update -> {eventName} : {costomEventInfo}");
if (root == this)
{
while (!CheckTargetVaild()) continue;
while (!CheckTargetVaild(!occupyed)) continue;
CheckAndSetTargetCache();
}
if (root.Occupyed) return false;
if (occupyed) return false;
//Console.WriteLine($"==================================== {parent}.PreUpdateNode Start ====================================");
foreach (string id in this.PartIDs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace RW_ModularizationWeapon
{
public partial class CompModularizationWeapon
{
public bool AllowSwap { get => this.RootPart == this && !this.Occupyed; }


public bool SetTargetPart(string id, LocalTargetInfo targetInfo)
{
Expand Down Expand Up @@ -49,7 +52,7 @@ public LocalTargetInfo GetTargetPart(string id)

public void SwapTargetPart()
{
if(RootPart == this && !Occupyed)
if(AllowSwap)
{
NeedUpdate = true;
NodeProccesser.UpdateNode();
Expand Down
2 changes: 1 addition & 1 deletion RW_ModularizationWeapon/UI/CustomWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ from x
in pawn.Map.listerThings.AllThings
where
(x?.Spawned ?? false) &&
((CompModularizationWeapon)x) != null &&
(((CompModularizationWeapon)x)?.AllowSwap ?? false) &&
creaftingTable.Props.filter.Allows(x) &&
pawn.CanReserveAndReach(x, PathEndMode.Touch, Danger.Deadly, 1, -1, null, false)
select (x, x.def)
Expand Down

0 comments on commit f78c2c9

Please sign in to comment.