Skip to content

Commit af9f90d

Browse files
committed
Merge branch 'Capacitor-Rework'
2 parents 0376b8d + 005441f commit af9f90d

File tree

11 files changed

+126
-37
lines changed

11 files changed

+126
-37
lines changed

Diff for: Assemblies/RimForge.dll

1.5 KB
Binary file not shown.

Diff for: Languages/English/Keyed/Keys.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ WARNING: Firing cannot be cancelled once started!</RF.Coilgun.AttackDesc>
8080
<RF.Coilgun.DisabledNoCaps>Cannot fire: not enough capacitors or not enough energy stored in capacitors.</RF.Coilgun.DisabledNoCaps>
8181
<RF.Coilgun.DisabledNoPower>Cannot fire: not powered.</RF.Coilgun.DisabledNoPower>
8282
<RF.Coilgun.DisabledNoShell>Cannot fire: shell not loaded.</RF.Coilgun.DisabledNoShell>
83-
<RF.Coilgun.CapState>Shot power: {0}% ({1} connected capacitors, {2} stored Wd)</RF.Coilgun.CapState> <!-- TODO remove -->
83+
<RF.Coilgun.CapState>{0} Wd stored in {1} connected capacitors. (min. {2} Wd to fire)</RF.Coilgun.CapState> <!-- TODO remove -->
8484
<RF.Coilgun.ChangeShellLabel>Change shell type</RF.Coilgun.ChangeShellLabel>
8585
<RF.Coilgun.ChangeShellDesc>Change the type of shell that is loaded into this coilgun.
8686
This action ejects the current shell onto the ground, and a colonist will have to load a new one.</RF.Coilgun.ChangeShellDesc>

Diff for: Source/RimForge/Buildings/Building_Capacitor.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using RimForge.Comps;
2-
using RimWorld;
32
using Verse;
43

54
namespace RimForge.Buildings

Diff for: Source/RimForge/Buildings/Building_CoilGun.cs

+29-28
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,21 @@ public IEnumerable<Building_Capacitor> GetConnectedCapacitors()
327327
}
328328
}
329329

330-
private void ClearCapacitorPower()
330+
private void RemoveCapacitorPower(float wd)
331331
{
332+
float toRemove = wd;
332333
foreach(var cap in GetConnectedCapacitors())
333334
{
334-
cap.CompCap.StoredWd = 0;
335+
if (toRemove <= 0f)
336+
return;
337+
338+
float take = Mathf.Min(toRemove, cap.CompCap.StoredWd);
339+
cap.CompCap.StoredWd -= take;
340+
toRemove -= take;
335341
}
342+
343+
if (toRemove > 0)
344+
Core.Error($"Tried to remove {wd} Wd of power from capacitors, could only remove {wd - toRemove} Wd.");
336345
}
337346

338347
private void GetCapacitorState(out int count, out float stored)
@@ -346,19 +355,6 @@ private void GetCapacitorState(out int count, out float stored)
346355
}
347356
}
348357

349-
private float GetRelativePower(int capCount, float storedPower)
350-
{
351-
if (capCount == 0)
352-
return 0;
353-
354-
float baseLine = Settings.CoilgunBasePowerReq;
355-
356-
if (storedPower < baseLine)
357-
return 0;
358-
359-
return Mathf.Clamp(storedPower / baseLine, 1, Settings.CoilgunMaxPowerMulti);
360-
}
361-
362358
private void RefreshCapacitors()
363359
{
364360
rawCapacitors.Clear();
@@ -435,8 +431,7 @@ private void Fire(IntVec3 endPos)
435431

436432
var shellDef = GetCurrentShellType();
437433

438-
GetCapacitorState(out int capCount, out float capStored);
439-
float damage = shellDef.baseDamage * Settings.CoilgunBaseDamageMultiplier * GetRelativePower(capCount, capStored);
434+
float damage = shellDef.baseDamage * Settings.CoilgunBaseDamageMultiplier;
440435
//Core.Log($"Base damage: {damage}, multi: {Settings.CoilgunPenDamageMultiplier}, building multi: {Settings.CoilgunBuildingDamageMulti}");
441436

442437
int affected = 0;
@@ -455,6 +450,8 @@ private void Fire(IntVec3 endPos)
455450
float totalDamage = 0;
456451
int bloodCount = 0;
457452
ThingDef bloodToSplatter = null;
453+
Color bloodSplatterColor = default;
454+
Vector2 bloodStartPos = default;
458455
string bloodPawnName = null;
459456

460457
void MakeBloodAt(IntVec3 cell, int remaining)
@@ -472,11 +469,18 @@ void MakeBloodAt(IntVec3 cell, int remaining)
472469
continue;
473470
}
474471

475-
int tick = tickCounter + (BloodTrailLength - remaining) * Rand.Range(1, 3);
472+
int delay = (BloodTrailLength - remaining) * Rand.Range(1, 3);
473+
int tick = tickCounter + delay;
476474
AddTickEvent(tick, () =>
477475
{
478476
FilthMaker.TryMakeFilth(pos, map, bloodToSplatter, bloodPawnName, Mathf.Min(remaining, 2));
479477
});
478+
var splash = new FollowBezierSpark();
479+
splash.Start = bloodStartPos;
480+
splash.End = pos.ToVector3Shifted().WorldToFlat();
481+
splash.Ticks = delay;
482+
splash.Color = bloodSplatterColor;
483+
splash.Spawn(map);
480484
}
481485
}
482486

@@ -525,9 +529,12 @@ void MakeBloodAt(IntVec3 cell, int remaining)
525529

526530
if(Settings.CoilgunSplatterBlood && pawn?.RaceProps?.BloodDef != null)
527531
{
532+
Color defaultBlood = pawn.RaceProps.meatColor == Color.white ? Color.red : pawn.RaceProps.meatColor;
528533
bloodToSplatter = pawn.RaceProps.BloodDef;
529534
bloodCount = BloodTrailLength;
530535
bloodPawnName = pawn.LabelIndefinite();
536+
bloodStartPos = pawn.DrawPos.WorldToFlat();
537+
bloodSplatterColor = pawn.RaceProps.IsMechanoid ? Color.black : pawn.RaceProps.Humanlike ? Color.red : defaultBlood;
531538
}
532539

533540
if (pawn?.RaceProps?.IsMechanoid ?? false)
@@ -563,7 +570,7 @@ void MakeBloodAt(IntVec3 cell, int remaining)
563570

564571
Current.CameraDriver.shaker.DoShake(5);
565572

566-
ClearCapacitorPower();
573+
RemoveCapacitorPower(Settings.CoilgunBasePowerReq);
567574
ClearLoadedShell();
568575
Core.Log($"Hit {affected} things for total {totalDamage} damage, scanned {cells} of {list.Count} cells.");
569576
}
@@ -730,7 +737,6 @@ public override IEnumerable<Gizmo> GetGizmos()
730737
yield return item;
731738

732739
GetCapacitorState(out int capCount, out float capPower);
733-
float power = GetRelativePower(capCount, capPower);
734740
bool hasPower = PowerComp.PowerOn;
735741
bool hasShell = GetLoadedShell() != null;
736742

@@ -754,7 +760,7 @@ public override IEnumerable<Gizmo> GetGizmos()
754760
LastKnowPos = Position;
755761
},
756762
gun = this,
757-
disabled = power <= 0 || !hasPower || !hasShell,
763+
disabled = capCount <= 0 || capPower < Settings.CoilgunBasePowerReq || !hasPower || !hasShell,
758764
disabledReason = !hasPower ? "RF.Coilgun.DisabledNoPower".Translate() : !hasShell ? "RF.Coilgun.DisabledNoShell".Translate() : "RF.Coilgun.DisabledNoCaps".Translate()
759765
};
760766
yield return new Command_Action()
@@ -781,16 +787,11 @@ public override string GetInspectString()
781787
{
782788
string basic = base.GetInspectString();
783789
GetCapacitorState(out int connected, out float stored);
784-
float power = GetRelativePower(connected, stored);
785-
786-
string shellName = GetLoadedShell()?.LabelCap ?? "RF.None".Translate().CapitalizeFirst();
787-
string loaded = "RF.Coilgun.LoadedShell".Translate(shellName);
788790

789-
string pct = (power * 100f).ToString("F0");
790-
string capState = "RF.Coilgun.CapState".Translate(pct, connected, stored.ToString("F0"));
791+
string capState = "RF.Coilgun.CapState".Translate(stored.ToString("F0"), connected, Settings.CoilgunBasePowerReq);
791792

792793
if(!string.IsNullOrWhiteSpace(basic))
793-
return $"{basic.TrimEnd()}\n{loaded}\n{capState}";
794+
return $"{basic.TrimEnd()}\n{capState}";
794795
return capState;
795796
}
796797
}

Diff for: Source/RimForge/Buildings/Building_LongDistanceCabled.cs

-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ public void GeneratePointsAsync(Building_LongDistanceCabled dom, Building_LongDi
9494

9595
public virtual int GetCablePointCount(Vector2 a, Vector2 b)
9696
{
97-
if (a == null || b == null || a == b)
98-
return 0;
99-
10097
return Mathf.RoundToInt((a - b).magnitude * Settings.CableSegmentsPerCell);
10198
}
10299

Diff for: Source/RimForge/Effects/FollowBezierSpark.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using UnityEngine;
2+
using Verse;
3+
4+
namespace RimForge.Effects
5+
{
6+
public class FollowBezierSpark : MapEffect
7+
{
8+
public Vector2 Position;
9+
public Vector2 Start, End;
10+
public int Ticks;
11+
public Color Color = Color.red;
12+
13+
private Vector2[] points;
14+
private int tickCounter;
15+
private Vector2 oldPos;
16+
private Material mat;
17+
18+
public override void Draw(bool tick, Map map)
19+
{
20+
if (mat == null)
21+
mat = RitualSparks.GetMat(Color, false);
22+
23+
float y = AltitudeLayer.Projectile.AltitudeFor();
24+
Vector3 end = Position.FlatToWorld(y);
25+
Vector3 start = (Position + (oldPos - Position).normalized * 0.6f).FlatToWorld(y);
26+
GenDraw.DrawLineBetween(end, start, mat);
27+
//Core.Log($"Draw from {start} to {end} ({oldPos == Position})");
28+
}
29+
30+
public override void TickAccurate()
31+
{
32+
base.TickAccurate();
33+
Tick();
34+
tickCounter++;
35+
}
36+
37+
private void Tick()
38+
{
39+
if(tickCounter >= Ticks)
40+
{
41+
Destroy();
42+
return;
43+
}
44+
45+
if (points == null)
46+
{
47+
float dst = (Start - End).magnitude;
48+
float height = Mathf.Lerp(0.2f, 2f, dst / 25f);
49+
Vector2 midA = Vector2.Lerp(Start, End, 0.3f) + new Vector2(0, height);
50+
Vector2 midB = Vector2.Lerp(Start, End, 0.7f) + new Vector2(0, height);
51+
int pointCount = Ticks;
52+
points = new Vector2[pointCount];
53+
54+
for (int i = 0; i < pointCount; i++)
55+
{
56+
float p = i / (pointCount - 1f);
57+
points[i] = Bezier.Evaluate(p, Start, midA, midB, End);
58+
}
59+
}
60+
61+
oldPos = tickCounter == 0 ? points[0] : points[tickCounter - 1];
62+
Position = points[tickCounter];
63+
}
64+
}
65+
}

Diff for: Source/RimForge/Effects/MapEffect.cs

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public virtual bool ShouldDeSpawn()
3333
return Destroyed || MapId == -1;
3434
}
3535

36+
public virtual void TickAccurate()
37+
{
38+
39+
}
40+
3641
public abstract void Draw(bool tick, Map map);
3742
}
3843
}

Diff for: Source/RimForge/Effects/MapEffectHandler.cs

+23
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,29 @@ public void RegisterEffect(MapEffect effect, Map map)
113113
}
114114
}
115115

116+
public override void WorldComponentTick()
117+
{
118+
base.WorldComponentTick();
119+
120+
foreach (var list in effects.Values)
121+
{
122+
for (int i = 0; i < list.Count; i++)
123+
{
124+
var item = list[i];
125+
if (item == null || item.ShouldDeSpawn())
126+
{
127+
list.RemoveAt(i);
128+
if (item != null)
129+
item.MapId = -1;
130+
i--;
131+
continue;
132+
}
133+
134+
item.TickAccurate();
135+
}
136+
}
137+
}
138+
116139
private void OnDrawLate(Map map)
117140
{
118141
if (map != null && effects.TryGetValue(map.uniqueID, out var found))

Diff for: Source/RimForge/Effects/RitualSparks.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ private static void ClearMatCache()
2525
Core.Log("Cleared the materials cache.");
2626
}
2727

28-
private static Material GetMat(Color color)
28+
public static Material GetMat(Color color, bool unlit = true)
2929
{
3030
if (matCache.TryGetValue(color, out var mat))
3131
return mat;
3232

33-
mat = MaterialPool.MatFrom("RF/Effects/Spark", ShaderDatabase.MoteGlow, color);
33+
mat = MaterialPool.MatFrom("RF/Effects/Spark", unlit ? ShaderDatabase.MoteGlow : ShaderDatabase.Mote, color);
3434
matCache.Add(color, mat);
3535
return mat;
3636
}

Diff for: Source/RimForge/RimForge.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="Effects\Bezier.cs" />
116116
<Compile Include="Effects\ElectricArc.cs" />
117117
<Compile Include="Effects\BezierElectricArc.cs" />
118+
<Compile Include="Effects\FollowBezierSpark.cs" />
118119
<Compile Include="Effects\LinearElectricArc.cs" />
119120
<Compile Include="Effects\MapEffect.cs" />
120121
<Compile Include="Effects\MapEffectHandler.cs" />

Diff for: Source/RimForge/Settings.cs

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public class Settings : ModSettings
3131
public static float CoilgunBuildingDamageMulti = 1f;
3232
[TweakValue("RimForge", 100, 10000)]
3333
public static float CoilgunBasePowerReq = 2000;
34-
[TweakValue("RimForge", 1, 50)]
35-
public static int CoilgunMaxPowerMulti = 3;
3634
[TweakValue("RimForge", 0, 1)]
3735
public static bool CoilgunSplatterBlood = true;
3836

0 commit comments

Comments
 (0)