Skip to content

Commit

Permalink
Final tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Sep 25, 2023
1 parent de395f3 commit 2a908fb
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 101 deletions.
Binary file modified Assemblies/ZombieLand.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Source/CETools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static bool Prefix(ref DamageInfo originalDinfo, Pawn pawn, BodyPartRecord hitPa
{
if (originalDinfo.Def == DamageDefOf.Bullet)
{
var diff = Tools.Difficulty();
var diff = ZombieSettings.Values.spitterThreat;
armorDeflected = Rand.Range(0, 5.1f) < diff;
dinfo.SetAmount(dmgAmount / (1 + 10 * diff));
}
Expand Down
141 changes: 54 additions & 87 deletions Source/JobDriver_Spitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,29 @@ public enum SpitterState

public class JobDriver_Spitter : JobDriver
{
public SpitterState state = SpitterState.Idle;
public int idleCounter = 0;
public bool firstShot = true;
public bool aggressive = false;
public int moveState = -1;
public int tickCounter = 0;
public int spitInterval = 0;
public int waves = 0;
public int remainingZombies = 0;
public ZombieSpitter spitter;

void InitAction()
{
var f = ZombieSettings.Values.spitterThreat;
aggressive = ShipCountdown.CountingDown || Rand.Chance(f / 2f);
(pawn as ZombieSpitter).aggressive = aggressive;
waves = Mathf.FloorToInt(f * (aggressive ? Rand.Range((1f, 2f).F(), (2f, 10f).F()) : Rand.Range((2f, 15f).F(), (4f, 30f).F())));
if (waves < 1)
waves = 1;
idleCounter = 0;
firstShot = true;
}

public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref state, "state", SpitterState.Idle);
Scribe_Values.Look(ref idleCounter, "idleCounter", 0);
Scribe_Values.Look(ref firstShot, "firstShot", true);
Scribe_Values.Look(ref aggressive, "aggressive", false);
Scribe_Values.Look(ref moveState, "moveState", -1);
Scribe_Values.Look(ref tickCounter, "tickCounter", 0);
Scribe_Values.Look(ref spitInterval, "spitInterval", 0);
Scribe_Values.Look(ref waves, "waves", 0);
Scribe_Values.Look(ref remainingZombies, "remainingZombies", 0);
if (Scribe.mode == LoadSaveMode.PostLoadInit)
(pawn as ZombieSpitter).aggressive = aggressive;
spitter = pawn as ZombieSpitter;
}

void DoIdle(int minTicks, int maxTicks)
{
idleCounter++;
if (idleCounter > 3)
spitter.idleCounter++;
if (spitter.idleCounter > 3)
{
DoShooting();
return;
}
tickCounter = Rand.Range(minTicks, maxTicks);
state = SpitterState.Idle;
spitter.tickCounter = Rand.Range(minTicks, maxTicks);
spitter.state = SpitterState.Idle;
}

void DoMoving(IntVec3 destination)
{
pawn.pather.StartPath(destination, PathEndMode.OnCell);
state = SpitterState.Moving;
spitter.pather.StartPath(destination, PathEndMode.OnCell);
spitter.state = SpitterState.Moving;
}

public IntVec3 TryFindNewTarget()
Expand Down Expand Up @@ -117,61 +86,59 @@ public IntVec3 TryFindNewTarget()

void DoShooting()
{
var f = ZombieSettings.Values.spitterThreat;
var fReverse = 5f - f;
spitInterval = Mathf.FloorToInt(fReverse * (aggressive ? Rand.Range((20f, 5f).F(), (40f, 10f).F()) : Rand.Range((60f, 30f).F(), (120f, 60f).F())));
if (spitInterval < 4)
spitInterval = 4;
remainingZombies = Mathf.FloorToInt(f * (aggressive ? Rand.Range((1f, 5f).F(), (10f, 20f).F()) : Rand.Range(1, 2)));
if (remainingZombies < 1)
remainingZombies = 1;
tickCounter = spitInterval;
state = SpitterState.Spitting;
spitter.spitInterval = Mathf.FloorToInt(spitter.aggressive ? Tools.SpitterRandRange(20, 5, 40, 10) : Tools.SpitterRandRange(60, 30, 120, 60));
if (spitter.spitInterval < 4)
spitter.spitInterval = 4;
spitter.remainingZombies = Mathf.FloorToInt(spitter.aggressive ? Tools.SpitterRandRange(1, 5, 10, 20) : Rand.Range(1, 2));
if (spitter.remainingZombies < 1)
spitter.remainingZombies = 1;
spitter.tickCounter = spitter.spitInterval;
spitter.state = SpitterState.Spitting;
}

bool Shoot()
{
var target = TryFindNewTarget();
if (target.IsValid && (target.x != 0 || target.z != 0))
{
CustomDefs.BallSpit.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map, false));
var projectile = (Projectile)GenSpawn.Spawn(CustomDefs.ZombieBall, pawn.Position, pawn.Map, WipeMode.Vanish);
projectile.Launch(pawn, pawn.DrawPos + new Vector3(0, 0, 0.5f), target, target, ProjectileHitFlags.IntendedTarget);
CustomDefs.BallSpit.PlayOneShot(new TargetInfo(spitter.Position, spitter.Map, false));
var projectile = (Projectile)GenSpawn.Spawn(CustomDefs.ZombieBall, spitter.Position, spitter.Map, WipeMode.Vanish);
projectile.Launch(spitter, spitter.DrawPos + new Vector3(0, 0, 0.5f), target, target, ProjectileHitFlags.IntendedTarget);
return true;
}
return false;
}

void DoPreparing()
{
tickCounter = Rand.Range(120, 180);
state = SpitterState.Preparing;
spitter.tickCounter = Rand.Range(120, 180);
spitter.state = SpitterState.Preparing;
}

void TickAction()
{
switch (state)
switch (spitter.state)
{
case SpitterState.Idle:
if (tickCounter > 0)
tickCounter--;
if (spitter.tickCounter > 0)
spitter.tickCounter--;
else
state = SpitterState.Searching;
spitter.state = SpitterState.Searching;
break;

case SpitterState.Searching:
var destination = RCellFinder.FindSiegePositionFrom(pawn.Position, Map, false, false);
var destination = RCellFinder.FindSiegePositionFrom(spitter.Position, Map, false, false);
if (destination.IsValid)
DoMoving(destination);
else
{
var distance = GenMath.LerpDouble(0, 5, 60, 12, Tools.Difficulty());
destination = RCellFinder.RandomWanderDestFor(pawn, pawn.Position, distance, (pawn, c1, c2) => true, Danger.Deadly);
var distance = GenMath.LerpDouble(0, 5, 60, 12, ZombieSettings.Values.spitterThreat);
destination = RCellFinder.RandomWanderDestFor(spitter, spitter.Position, distance, (spitter, c1, c2) => true, Danger.Deadly);
if (destination.IsValid)
DoMoving(destination);
else
{
if (RCellFinder.TryFindSiegePosition(pawn.Position, 10, pawn.Map, false, out destination))
if (RCellFinder.TryFindSiegePosition(spitter.Position, 10, spitter.Map, false, out destination))
DoMoving(destination);
else
DoIdle(120, 120);
Expand All @@ -180,53 +147,53 @@ void TickAction()
break;

case SpitterState.Moving:
var currentMoveState = Mathf.FloorToInt(pawn.Drawer.tweener.MovedPercent() * 3.999f);
if (moveState != currentMoveState)
var currentMoveState = Mathf.FloorToInt(spitter.Drawer.tweener.MovedPercent() * 3.999f);
if (spitter.moveState != currentMoveState)
{
moveState = currentMoveState;
CustomDefs.SpitterMove.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map, false));
spitter.moveState = currentMoveState;
CustomDefs.SpitterMove.PlayOneShot(new TargetInfo(spitter.Position, spitter.Map, false));
}
break;

case SpitterState.Preparing:
if (tickCounter > 0)
tickCounter--;
if (spitter.tickCounter > 0)
spitter.tickCounter--;
else
{
firstShot = true;
spitter.firstShot = true;
DoShooting();
}
break;

case SpitterState.Spitting:
if (remainingZombies <= 0)
if (spitter.remainingZombies <= 0)
{
waves--;
if (waves > 0)
spitter.waves--;
if (spitter.waves > 0)
{
state = SpitterState.Searching;
spitter.state = SpitterState.Searching;
return;
}

if (RCellFinder.TryFindBestExitSpot(pawn, out var exitCell, TraverseMode.ByPawn, false))
if (RCellFinder.TryFindBestExitSpot(spitter, out var exitCell, TraverseMode.ByPawn, false))
{
pawn.pather.StartPath(exitCell, PathEndMode.OnCell);
state = SpitterState.Leaving;
spitter.pather.StartPath(exitCell, PathEndMode.OnCell);
spitter.state = SpitterState.Leaving;
return;
}

DoIdle(300, 900);
break;
}
if (tickCounter < spitInterval)
if (spitter.tickCounter < spitter.spitInterval)
{
tickCounter++;
spitter.tickCounter++;
return;
}
if (Shoot())
{
remainingZombies--;
tickCounter = 0;
spitter.remainingZombies--;
spitter.tickCounter = 0;
}
break;

Expand All @@ -239,9 +206,9 @@ public override void Notify_PatherArrived()
{
base.Notify_PatherArrived();

if (state == SpitterState.Leaving)
if (spitter.state == SpitterState.Leaving)
{
pawn.Destroy();
spitter.Destroy();
return;
}

Expand All @@ -251,15 +218,15 @@ public override void Notify_PatherArrived()
public override void Notify_PatherFailed()
{
base.Notify_PatherFailed();
DoIdle(300, 900);
DoIdle(30, 90);
}

public override string GetReport()
{
var modeStr = aggressive ? "Aggressive".Translate() : "Calm".Translate();
var waveStr = waves < 1 ? "" : $"{waves} {"Waves".Translate()}";
var stateStr = ("SpitterState" + Enum.GetName(typeof(SpitterState), state)).Translate();
var zombieStr = state != SpitterState.Spitting ? "" : $", {remainingZombies} zombies";
var modeStr = spitter.aggressive ? "Aggressive".Translate() : "Calm".Translate();
var waveStr = spitter.waves < 1 ? "" : $"{spitter.waves} {"Waves".Translate()}";
var stateStr = ("SpitterState" + Enum.GetName(typeof(SpitterState), spitter.state)).Translate();
var zombieStr = spitter.state != SpitterState.Spitting ? "" : $", {spitter.remainingZombies} zombies";
return $"{modeStr}, {waveStr}, {stateStr}{zombieStr}";
}

Expand Down
7 changes: 3 additions & 4 deletions Source/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,8 +2089,7 @@ static void Prefix(Thing __instance, IntVec3 value)
{
var now = Tools.Ticks();
var grid = pawn.Map.GetGrid();
var f = ZombieSettings.Values.spitterThreat;
var radius = f * GenMath.LerpDouble(0, 5, 4, 32, Tools.Difficulty());
var radius = GenMath.LerpDouble(0, 5, 4, 32, ZombieSettings.Values.spitterThreat);
Tools.GetCircle(radius).DoIf(vec => exclude.Contains(vec) == false, vec =>
grid.BumpTimestamp(value + vec, now - (long)(2f * vec.LengthHorizontal)));
return;
Expand Down Expand Up @@ -3626,7 +3625,7 @@ static bool Prefix(Thing thing, StatDef stat, ref float __result)
{
if (stat == StatDefOf.IncomingDamageFactor)
{
__result = 6f - Tools.Difficulty();
__result = 6f - ZombieSettings.Values.spitterThreat;
return false;
}
}
Expand Down Expand Up @@ -4061,7 +4060,7 @@ static bool Prefix(ref DamageInfo dinfo, Pawn pawn)
if (pawn is ZombieSpitter)
{
var def1 = dinfo.Def;
var f = 6f - Tools.Difficulty();
var f = 6f - ZombieSettings.Values.spitterThreat;
if (def1.isRanged == false)
dinfo.SetAmount(dinfo.Amount * f);
else
Expand Down
12 changes: 10 additions & 2 deletions Source/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text;
using System.Xml;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using Verse;
using Verse.AI;
using Verse.Sound;
Expand Down Expand Up @@ -205,8 +206,15 @@ public static (int, int, int) ZombieSpitterParameter()

public static float Difficulty() => ZombieSettings.Values.threatScale; // Find.Storyteller.difficulty.threatScale;

public static int F(this (int, int) range) => (int)(GenMath.LerpDouble(0, 5, range.Item1, range.Item2, Difficulty()) * GenMath.LerpDoubleClamped(GenDate.TicksPerYear, GenDate.TicksPerYear * 5, 1, 5, GenTicks.TicksGame));
public static float F(this (float, float) range) => GenMath.LerpDouble(0, 5, range.Item1, range.Item2, Difficulty()) * GenMath.LerpDoubleClamped(GenDate.TicksPerYear, GenDate.TicksPerYear * 5, 1, 5, GenTicks.TicksGame);
public static float SpitterRandRange(float minMin, float maxMin, float minMax, float maxMax)
{
var f = ZombieSettings.Values.spitterThreat;
var min = GenMath.LerpDouble(0, 5, minMin, maxMin, f);
var max = GenMath.LerpDouble(0, 5, minMax, maxMax, f);
if (max < min)
return Rand.Range(max, min);
return Rand.Range(min, max);
}

public static float MoveableWeight(float x, float weight, float factor = 10)
{
Expand Down
44 changes: 38 additions & 6 deletions Source/ZombieSpitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,33 @@ namespace ZombieLand
{
public class ZombieSpitter : Pawn, IDisposable
{
public bool aggressive = false;
static Mesh mesh = null;
bool disposed = false;

public SpitterState state = SpitterState.Idle;
public int idleCounter = 0;
public bool firstShot = true;
public bool aggressive = false;
public int moveState = -1;
public int tickCounter = 0;
public int spitInterval = 0;
public int waves = 0;
public int remainingZombies = 0;

public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref state, "state", SpitterState.Idle);
Scribe_Values.Look(ref idleCounter, "idleCounter", 0);
Scribe_Values.Look(ref firstShot, "firstShot", true);
Scribe_Values.Look(ref aggressive, "aggressive", false);
Scribe_Values.Look(ref moveState, "moveState", -1);
Scribe_Values.Look(ref tickCounter, "tickCounter", 0);
Scribe_Values.Look(ref spitInterval, "spitInterval", 0);
Scribe_Values.Look(ref waves, "waves", 0);
Scribe_Values.Look(ref remainingZombies, "remainingZombies", 0);
}

public static void Spawn(Map map, IntVec3? location = null)
{
if (location.HasValue == false)
Expand All @@ -39,9 +62,18 @@ public static void Spawn(Map map, IntVec3? location = null)

var cell = location.Value;

var spitter = PawnGenerator.GeneratePawn(ZombieDefOf.ZombieSpitter, null);
var spitter = PawnGenerator.GeneratePawn(ZombieDefOf.ZombieSpitter, null) as ZombieSpitter;
spitter.SetFactionDirect(Find.FactionManager.FirstFactionOfDef(ZombieDefOf.Zombies));
GenSpawn.Spawn(spitter, cell, map, Rot4.Random, WipeMode.Vanish, false);

var f = ZombieSettings.Values.spitterThreat;
spitter.aggressive = ShipCountdown.CountingDown || Rand.Chance(f / 2f);
spitter.waves = Mathf.FloorToInt(spitter.aggressive ? ZombieLand.Tools.SpitterRandRange(1, 2, 4, 10) : ZombieLand.Tools.SpitterRandRange(2, 15, 4, 30));
if (spitter.waves < 1)
spitter.waves = 1;
spitter.idleCounter = 0;
spitter.firstShot = true;

spitter.jobs.StartJob(JobMaker.MakeJob(CustomDefs.Spitter));

var headline = "LetterLabelZombiesSpitter".Translate();
Expand All @@ -67,10 +99,10 @@ public override string GetInspectString()
{
var result = new StringBuilder();
var spitter = jobs.curDriver as JobDriver_Spitter;
result.Append("Mode".Translate()).Append(": ").AppendLine(spitter.aggressive ? "Aggressive".Translate() : "Calm".Translate());
if (spitter.waves > 0)
result.Append("Waves".Translate()).Append(": ").Append(spitter.waves).Append(", ");
result.AppendLine(("SpitterState" + Enum.GetName(typeof(SpitterState), spitter.state)).Translate());
result.Append("Mode".Translate()).Append(": ").AppendLine(aggressive ? "Aggressive".Translate() : "Calm".Translate());
if (waves > 0)
result.Append("Waves".Translate()).Append(": ").Append(waves).Append(", ");
result.AppendLine(("SpitterState" + Enum.GetName(typeof(SpitterState), state)).Translate());
return result.ToString().TrimEndNewlines();
}

Expand Down
2 changes: 1 addition & 1 deletion Source/ZombieStateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public static bool Mine(this JobDriver_Stumble driver, Zombie zombie, bool allDi

zombie.rotationTracker.FaceCell(mineable.Position);
effecter.Trigger(zombie, mineable);
var baseDamage = (int)GenMath.LerpDoubleClamped(0, 5, 1, 10, Tools.Difficulty());
var baseDamage = (int)GenMath.LerpDoubleClamped(0, 5, 2, 40, Tools.Difficulty());
var damage = (!mineable.def.building.isNaturalRock) ? baseDamage : baseDamage * 2;
if (mineable.HitPoints > damage)
_ = mineable.TakeDamage(new DamageInfo(DamageDefOf.Mining, damage));
Expand Down

0 comments on commit 2a908fb

Please sign in to comment.