Skip to content

Commit

Permalink
Fixes #42 - Added vampire guest handling code
Browse files Browse the repository at this point in the history
This should prevent vampire guests from overstaying their welcome.
  • Loading branch information
jecrell committed Jan 29, 2018
1 parent 5edef0f commit bebc1c4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
11 changes: 10 additions & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>

HangedMan - CrossCircle
The Star - ButtCrack
The Devil - Circle dot
The Hermit - Phallus
Death - Triangle Down
Empress - Triangle Up

Ring Pinky Middle Index Pinky

<ModMetaData>
<name>Rim of Madness - Vampires</name>
<author>Jecrell</author>
<targetVersion>0.18.1722</targetVersion>
<description>V1.18.1.8
<description>V1.18.1.10
Adds vampires to RimWorld.

Heavily inspired by Vampire the Masquerade, this mod introduces a disease known as vampirism.
Expand Down
Binary file modified Assemblies/Vampire.dll
Binary file not shown.
29 changes: 29 additions & 0 deletions Source/Vampires/HarmonyPatches/HarmonyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ where typeof(WorkGiver).IsAssignableFrom(assemblyType)
harmony.Patch(AccessTools.Method(typeof(StatPart_Glow), "FactorFromGlow"), null,
new HarmonyMethod(typeof(HarmonyPatches), nameof(VampiresAlwaysWorkHard)));

//Vampire guests and visitors should leave after their time is passed.
harmony.Patch(AccessTools.Method(typeof(LordMaker), "MakeNewLord"), null,
new HarmonyMethod(typeof(HarmonyPatches), nameof(VampiresGuestTracker)));

//Remove temporary character (PawnTemporary) corpses from the list, since they can't tie.
//harmony.Patch(AccessTools.Method(typeof(ThingDefGenerator_Corpses), "ImpliedCorpseDefs"), null,
// new HarmonyMethod(typeof(HarmonyPatches), nameof(RemovePawnTemporaryCorpses)));
Expand Down Expand Up @@ -486,6 +490,31 @@ where typeof(WorkGiver).IsAssignableFrom(assemblyType)

#endregion
}

public static Dictionary<Pawn, int> VampGuestCache = new Dictionary<Pawn, int>();

public static void VampiresGuestTracker(Faction faction, LordJob lordJob, Map map,
IEnumerable<Pawn> startingPawns, ref Lord __result)
{
//Only a few lords will have vampires with these issues.
if (!(lordJob is LordJob_VisitColony) && !(lordJob is LordJob_AssistColony) &&
!(lordJob is LordJob_TravelAndExit)) return;
if (startingPawns == null || !startingPawns.Any()) return;

foreach (var startingPawn in startingPawns)
{
if (startingPawn.IsVampire())
{
if (HarmonyPatches.VampGuestCache.ContainsKey(startingPawn))
{
HarmonyPatches.VampGuestCache.Remove(startingPawn);
}
int curTicks = Find.TickManager.TicksGame;
HarmonyPatches.VampGuestCache.Add(startingPawn, curTicks);
//Log.Message("Vampire tracking: " + startingPawn.Label + " " + curTicks);
}
}
}

// RimWorld.StatPart_Glow
public static void VampiresAlwaysWorkHard(Thing t, ref float __result) //FactorFromGlow
Expand Down
55 changes: 48 additions & 7 deletions Source/Vampires/WorldComponent_VampireTracker.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using Verse;
using RimWorld.Planet;
using Verse.AI;

namespace Vampire
{
Expand Down Expand Up @@ -58,7 +60,7 @@ private int GetNewlySpawnedVampireGeneration(Pawn childe)
return result;
}
result = Rand.Range(10, 13);
Log.Message("Vampires :: Spawned " + result + " generaton vampire.");
//Log.Message("Vampires :: Spawned " + result + " generaton vampire.");
return result;
}

Expand Down Expand Up @@ -198,11 +200,50 @@ public WorldComponent_VampireTracker(World world) : base(world)
public override void WorldComponentTick()
{
base.WorldComponentTick();
//if (debugPrinted == false)
//{
// debugPrinted = true;
// PrintVampires();
//}
if (Find.TickManager.TicksGame % 100 == 0)
{
CleanVampGuestCache();
if (HarmonyPatches.VampGuestCache == null || !HarmonyPatches.VampGuestCache.Any()) return;
foreach (var keyValuePair in HarmonyPatches.VampGuestCache)
{
Pawn p = keyValuePair.Key;
if (p == null) continue;
if (p.Downed) continue;
if (keyValuePair.Value + 16000 > Find.TickManager.TicksGame) continue;
if (p.CurJob?.def == JobDefOf.Goto) continue;
if (p.InMentalState || p.IsFighting()) continue;
if (p.IsSunRisingOrDaylight()) continue;
if (p.ParentHolder is Building_HideyHole g)
{
g.EjectContents();
}
TryGiveJobGiverToVampGuest(p);
}
}
}

private static void TryGiveJobGiverToVampGuest(Pawn p)
{
var thinkNode_JobGiver = (ThinkNode_JobGiver) Activator.CreateInstance(typeof(JobGiver_ExitMapBest));
thinkNode_JobGiver.ResolveReferences();
var thinkResult = thinkNode_JobGiver.TryIssueJobPackage(p, default(JobIssueParams));
if (thinkResult.Job != null)
{
//Log.Message("Vampire Guest Handler :: " + p.LabelShort + " :: Started ExitMapBest job.");
p.jobs.StartJob(thinkResult.Job, JobCondition.None, null, false, true, null, null, false);
}
else
{
//Log.Message("Vampire Guest Handler :: " + p.LabelShort + " :: Failed to give ExitMapBest job.");
}
}

private static void CleanVampGuestCache()
{
HarmonyPatches.VampGuestCache.RemoveAll(
x => x.Key is Pawn p &&
(p.Dead || p.Faction == Faction.OfPlayerSilentFail ||
(!p.Spawned && !(p.ParentHolder is Building_HideyHole))));
}

public void PrintVampires()
Expand Down

0 comments on commit bebc1c4

Please sign in to comment.