Skip to content

Commit

Permalink
Merge pull request #68 from emipa606/main
Browse files Browse the repository at this point in the history
Mechs should now spawn in raids
  • Loading branch information
rheirman authored Jan 20, 2023
2 parents 83a7409 + 8ac9b0b commit 3f418b3
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 72 deletions.
Binary file modified 1.4/Assemblies/WhatTheHack.dll
Binary file not shown.
1 change: 0 additions & 1 deletion Source/WhatTheHack/Comps/CompOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public bool ShowEye

public override void Initialize(CompProperties props)
{
Log.Message("CompOverlay Initialize");
base.Initialize(props);
//SetLookAround();
}
Expand Down
1 change: 0 additions & 1 deletion Source/WhatTheHack/Harmony/Dialog_FormCaravan_TrySend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
var instruction = instructionsList[i];
if (instruction.opcode == OpCodes.Stloc_0)
{
Log.Message("found Stloc_1");
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call,
typeof(Dialog_FormCaravan_TrySend).GetMethod("AddWarnings"));
Expand Down
3 changes: 1 addition & 2 deletions Source/WhatTheHack/Harmony/GenSpawn_Spawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ private static void Postfix(ref Thing newThing, bool respawningAfterLoad)
{
NameUnnamedMechs(newThing);
AddOwnershipIfNeeded(newThing);
//Log.Message("aa");
if (newThing is Pawn p && p.IsHacked() && p.Faction == Faction.OfPlayer)
{
var storage = Base.Instance.GetExtendedDataStorage();
Expand Down Expand Up @@ -135,7 +134,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
public static void Modified_WipeExistingThings(IntVec3 thingPos, Rot4 thingRot, BuildableDef thingDef, Map map,
DestroyMode mode, Thing thing)
{
if (!(thing.TryGetComp<CompMountable>() is { Active: true }))
if (thing.TryGetComp<CompMountable>() is not { Active: true })
{
GenSpawn.WipeExistingThings(thingPos, thingRot, thingDef, map, mode);
}
Expand Down
100 changes: 39 additions & 61 deletions Source/WhatTheHack/Harmony/IncidentWorker_Raid_TryExecuteWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using RimWorld;
using Verse;
Expand All @@ -12,35 +11,22 @@
namespace WhatTheHack.Harmony;

//Spawn hacked mechnoids in enemy raids
[HarmonyPatch(typeof(IncidentWorker_Raid), "TryExecuteWorker")]
//[HarmonyPatch(typeof(IncidentWorker_Raid), "TryExecuteWorker")]
[HarmonyPatch]
public static class IncidentWorker_Raid_TryExecuteWorker
{
[HarmonyPriority(Priority.First)]
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
private static IEnumerable<MethodBase> TargetMethods()
{
var instructionList = instructions.ToList();

for (var i = 0; i < instructions.Count(); i++)
foreach (var arrivalType in AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes())
.Where(type => type.IsSubclassOf(typeof(PawnsArrivalModeWorker))))
{
if (instructionList[i].operand as MethodInfo ==
AccessTools.Method(typeof(PawnsArrivalModeWorker), "Arrive"))
{
yield return instructionList[i];
yield return instructionList[i - 2];
yield return instructionList[i - 1];
yield return new CodeInstruction(OpCodes.Call,
typeof(IncidentWorker_Raid_TryExecuteWorker).GetMethod("SpawnHackedMechanoids"));
}
else
{
yield return instructionList[i];
}
yield return arrivalType.GetMethod("Arrive");
}
}

//returns pawns for compatibility reasons.
public static void SpawnHackedMechanoids(List<Pawn> pawns, IncidentParms parms)
public static void Prefix(ref List<Pawn> pawns, IncidentParms parms, ref List<Pawn> __state)
{
__state = new List<Pawn>();
if (pawns.Count == 0)
{
return;
Expand All @@ -61,62 +47,54 @@ public static void SpawnHackedMechanoids(List<Pawn> pawns, IncidentParms parms)
var maxMechPoints =
parms.points * rand.Next(minHackedMechPoints, Base.maxHackedMechPoints) / 100f; //TODO: no magic numbers
float cumulativePoints = 0;
var map = parms.target as Map;
var addedPawns = new List<Pawn>();
var possibleMechs = from a in DefDatabase<PawnKindDef>.AllDefs
where a.IsMechanoid() && Utilities.IsAllowedInModOptions(a.race.defName, parms.faction) && a.isFighter &&
(parms.raidArrivalMode == PawnsArrivalModeDefOf.EdgeWalkIn || a.RaceProps.baseBodySize <= 1)
select a; //Only allow small mechs to use drop pods

while (cumulativePoints < maxMechPoints)
{
var points = cumulativePoints;
var selectedPawns = from a in DefDatabase<PawnKindDef>.AllDefs
where a.IsMechanoid() &&
points + a.combatPower < maxMechPoints &&
Utilities.IsAllowedInModOptions(a.race.defName, parms.faction) &&
(parms.raidArrivalMode == PawnsArrivalModeDefOf.EdgeWalkIn ||
a.RaceProps.baseBodySize <= 1) //Only allow small mechs to use drop pods
select a;
var selectedPawns = from a in possibleMechs where points + a.combatPower < maxMechPoints select a;

selectedPawns.TryRandomElement(out var pawnKindDef);

if (pawnKindDef != null)
{
var mechanoid = PawnGenerator.GeneratePawn(pawnKindDef, parms.faction);
if (parms.raidArrivalMode == PawnsArrivalModeDefOf.EdgeWalkIn)
{
var loc = CellFinder.RandomClosewalkCellNear(parms.spawnCenter, map, 8);
GenSpawn.Spawn(mechanoid, loc, map, parms.spawnRotation);
}

mechanoid.health.AddHediff(WTH_DefOf.WTH_TargetingHacked);
mechanoid.health.AddHediff(WTH_DefOf.WTH_BackupBattery);
var powerNeed = (Need_Power)mechanoid.needs.TryGetNeed(WTH_DefOf.WTH_Mechanoid_Power);
powerNeed.CurLevel = powerNeed.MaxLevel;
if (ModsConfig.BiotechActive)
{
mechanoid.needs.energy.curLevelInt = mechanoid.needs.energy.MaxLevel;
}

addedPawns.Add(mechanoid);
cumulativePoints += pawnKindDef.combatPower;
AddModules(mechanoid);
}
else
if (pawnKindDef == null)
{
break;
}

var mechanoid = PawnGenerator.GeneratePawn(pawnKindDef, parms.faction);
pawns.Add(mechanoid);
__state.Add(mechanoid);
cumulativePoints += pawnKindDef.combatPower;
}
}

if (addedPawns.Count > 0 && !addedPawns[0].Spawned)
public static void Postfix(List<Pawn> pawns, IncidentParms parms, List<Pawn> __state)
{
if (!__state.Any())
{
parms.raidArrivalMode.Worker.Arrive(addedPawns, parms);
return;
}

pawns.AddRange(addedPawns);

foreach (var pawn in pawns)
foreach (var mechanoid in __state)
{
if (pawn.equipment == null)
mechanoid.health.AddHediff(WTH_DefOf.WTH_TargetingHacked);
mechanoid.health.AddHediff(WTH_DefOf.WTH_BackupBattery);

var powerNeed = (Need_Power)mechanoid.needs.TryGetNeed(WTH_DefOf.WTH_Mechanoid_Power);
powerNeed.CurLevel = powerNeed.MaxLevel;
if (ModsConfig.BiotechActive && mechanoid.needs.energy != null)
{
mechanoid.needs.energy.curLevelInt = mechanoid.needs.energy.MaxLevel;
}

pawns.Add(mechanoid);
AddModules(mechanoid);
if (mechanoid.equipment == null)
{
pawn.equipment = new Pawn_EquipmentTracker(pawn);
mechanoid.equipment = new Pawn_EquipmentTracker(mechanoid);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Source/WhatTheHack/Harmony/Lord_AddPawnInternal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HarmonyLib;
using Verse;
using Verse.AI.Group;

namespace WhatTheHack.Harmony;

[HarmonyPatch(typeof(Lord), "AddPawnInternal")]
internal static class Lord_AddPawnInternal
{
private static bool Prefix(Pawn p, Lord __instance)
{
return !__instance.ownedPawns.Contains(p);
}
}
2 changes: 1 addition & 1 deletion Source/WhatTheHack/Jobs/JobDriver_MechanoidAbility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected virtual void PerformAbility(DefModExtension_Ability modExt)
{
powerNeed.CurLevel -= modExt.powerDrain;

if (ModsConfig.BiotechActive)
if (ModsConfig.BiotechActive && pawn.needs.energy != null)
{
pawn.needs.energy.CurLevelPercentage = powerNeed.CurLevelPercentage;
}
Expand Down
2 changes: 0 additions & 2 deletions Source/WhatTheHack/Jobs/JobDriver_Mechanoid_Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ public override bool TryMakePreToilReservations(bool errorOnFailed)

public override IEnumerable<Toil> MakeNewToils()
{
//this.AddFinishAction(new Action(delegate { Log.Message("finish action called for job!"); }));
this.FailOnDespawnedOrNull(TargetIndex.A);
if (RestingPlace is Building_BaseMechanoidPlatform)
{
yield return Toils_Bed.ClaimBedIfNonMedical(TargetIndex.A);
yield return GoToPlatform(TargetIndex.A);
}

//goToPlatform.AddPreInitAction(new Action(delegate { Log.Message("first toil pre-initaction"); }));
var toil = new Toil
{
defaultCompleteMode = ToilCompleteMode.Never,
Expand Down
2 changes: 1 addition & 1 deletion Source/WhatTheHack/Needs/Need_Power.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override void NeedInterval()
{
lastLevel = CurLevel;
CurLevel -= PowerFallPerTick * 150f;
if (ModsConfig.BiotechActive)
if (ModsConfig.BiotechActive && pawn.needs.energy != null)
{
pawn.needs.energy.CurLevelPercentage = CurLevelPercentage;
}
Expand Down
2 changes: 0 additions & 2 deletions Source/WhatTheHack/Storage/ExtendedDataStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ public void Cleanup()
{
_store.Remove(key);
}

Log.Message($"Cleaned up {shouldRemove.Count} deprecated records from What the Hack!?");
}


Expand Down
1 change: 0 additions & 1 deletion Source/WhatTheHack/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ public static void InitWorkTypesAndSkills(Pawn pawn, ExtendedPawnData pawnData)
var huntingWorkType = WorkTypeDefOf.Hunting;
if (pawnData.workTypes == null)
{
// Log.Message("WTHACK: " + pawn.Name);
pawnData.workTypes = new List<WorkTypeDef>();
}

Expand Down

0 comments on commit 3f418b3

Please sign in to comment.