-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildableDirtTilePatches.cs
99 lines (84 loc) · 4.59 KB
/
BuildableDirtTilePatches.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
using CaiLib.Utils;
using static CaiLib.Utils.BuildingUtils;
using static CaiLib.Utils.StringUtils;
using static CoolLib.Log;
namespace BuildableDirtTile {
public class BuildableDirtTilePatches {
public static Config Settings = Config.Load();
public static void OnLoad() => LogInit("BDT");
[HarmonyPatch(typeof(GeneratedBuildings), "LoadGeneratedBuildings")]
public static class GeneratedBuildings_LoadGeneratedBuildings_Patch {
public static void Prefix() {
AddBuildingStrings(DirtTileConfig.ID, DirtTileConfig.DisplayName, DirtTileConfig.Description, DirtTileConfig.Effect);
int index = TUNING.BUILDINGS.PLANORDER.FindIndex(x => x.category == "Base");
if (index == -1)
return;
// add tile to `Base` building tab
IList<string> planOrderList = TUNING.BUILDINGS.PLANORDER[index].data;
planOrderList.Add(DirtTileConfig.ID);
}
}
[HarmonyPatch(typeof(Database.Techs), "Init")]
public static class Database_Techs_Init_Patch {
public static void Postfix(ref Database.Techs __instance) {
// Add to `Basic Farming` technology
Tech techBasicFarming = __instance.TryGetTechForTechItem("RationBox");
techBasicFarming.unlockedItemIDs.Add(DirtTileConfig.ID);
}
}
[HarmonyPatch(typeof(BuildingComplete), "OnSpawn")]
public class BuildingComplete_OnSpawn_Patch {
private static CellOffset[] _displacementOffsets = new CellOffset[]
{
new CellOffset(0, 1),
new CellOffset(0, -1),
new CellOffset(1, 0),
new CellOffset(-1, 0),
new CellOffset(1, 1),
new CellOffset(1, -1),
new CellOffset(-1, 1),
new CellOffset(-1, -1)
};
public static void Postfix(BuildingComplete __instance) {
GameObject go = __instance.gameObject;
if (__instance.name == "DirtTileComplete") {
Debug.Log("[DIRT] <BuildingComplete_OnSpawn> __instance.name: " + __instance.name);
Vector3 pos = go.transform.position;
float temperature = go.GetComponent<PrimaryElement>().Temperature;
float mass = Settings.BlockMass; // 50kg
byte disease_idx = byte.MaxValue;
int cell = Grid.PosToCell(pos);
SimMessages.ReplaceAndDisplaceElement(cell, SimHashes.Dirt, null, mass, temperature, disease_idx, 0, -1); // spawn Dirt Block
Debug.Log($"[DIRT] <BuildingComplete_OnSpawn> pos: {pos}, temperature: {temperature}, cell: {cell}");
// NOTE: Displace pickupables needs to be watched for possibly slowing down the game during late game
int origCell = cell;
foreach (Pickupable pickupable in Components.Pickupables) {
if (Grid.PosToCell(pickupable) == origCell) {
Debug.Log($"[DIRT] <BuildingComplete_OnSpawn> pickupable: {pickupable}");
for (int i = 0; i < _displacementOffsets.Length; i++) {
int offsetCell = Grid.OffsetCell(cell, _displacementOffsets[i]);
if (Grid.IsValidCell(offsetCell) && !Grid.Solid[offsetCell]) {
Vector3 position = Grid.CellToPosCBC(offsetCell, Grid.SceneLayer.Move);
KCollider2D component = pickupable.GetComponent<KCollider2D>();
if (component != null) {
position.y += pickupable.transform.GetPosition().y - component.bounds.min.y;
}
pickupable.transform.SetPosition(position);
cell = offsetCell;
Traverse.Create(pickupable).Method("RemoveFaller", new Type[] { }).GetValue();
Traverse.Create(pickupable).Method("AddFaller", new Type[] { typeof(Vector2) }).GetValue(Vector2.zero);
break;
}
}
}
}
go.DeleteObject(); // remove Dirt Tile
}
}
}
}
}