-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRaptorFastDelete.cs
188 lines (161 loc) · 7.23 KB
/
RaptorFastDelete.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections.Generic;
using static Bulldozer.Log;
namespace Bulldozer
{
/// <summary>
/// This code was adapted from code contributed by Velociraptor115 on Github / Raptor#4825 on Discord
/// </summary>
public static class RaptorFastDelete
{
public static void Execute()
{
if (GameMain.mainPlayer == null)
return;
FastDeleteEntities();
}
private static void FastDeleteEntities()
{
var player = GameMain.mainPlayer;
if (player.factory == null)
return;
var factory = player.factory;
var factorySystem = factory.factorySystem;
var cargoTraffic = factory.cargoTraffic;
var powerSystem = factory.powerSystem;
// Close all the build tools, so we don't have to worry about BuildTool.buildPreview
foreach (var buildTool in player.controller.actionBuild.tools)
buildTool._Close();
// Close inspect
player.controller.actionInspect.InspectNothing();
//const int maxItemId = 12000;
//var takeBackCount = new int[maxItemId];
//var takeBackInc = new int[maxItemId];
var takeBackCount = new Dictionary<int, int>();
var takeBackInc = new Dictionary<int, int>();
var powerConRemoval = new Dictionary<int, HashSet<int>>();
for (int i = 0; i < powerSystem.netCursor; i++)
{
powerConRemoval[i] = new HashSet<int>();
}
foreach (var item in LDB.items.dataArray)
{
takeBackCount[item.ID] = 0;
takeBackInc[item.ID] = 0;
}
void DeleteInserters()
{
var inserterPool = factorySystem.inserterPool;
var entityPool = factory.entityPool;
var consumerPool = powerSystem.consumerPool;
void TakeBackItemsOptimized(ref InserterComponent inserter)
{
if (inserter.itemId > 0 && inserter.stackCount > 0)
{
takeBackCount[inserter.itemId] += inserter.itemCount;
takeBackInc[inserter.itemId] += inserter.itemInc;
}
}
void RemoveConsumerComponent(int id)
{
ref var powerCon = ref consumerPool[id];
if (powerCon.id != 0)
{
powerConRemoval[powerCon.networkId].Add(id);
powerCon.SetEmpty();
powerSystem.consumerRecycle[powerSystem.consumerRecycleCursor] = id;
powerSystem.consumerRecycleCursor++;
}
}
for (int i = 1; i < factorySystem.inserterCursor; i++)
{
ref var inserter = ref inserterPool[i];
var entityId = inserter.entityId;
if (entityId == 0)
continue;
if (inserter.id == i)
{
// record the inserter in the takeback data
var entityData = factory.entityPool[entityId];
takeBackCount[entityData.protoId]++;
TakeBackItemsOptimized(ref inserter);
}
RemoveConsumerComponent(entityPool[entityId].powerConId);
// Help remove the power consumers before removing the entity
factory.RemoveEntityWithComponents(entityId);
}
for (int i = 0; i < powerSystem.netCursor; i++)
{
var network = powerSystem.netPool[i];
if (network != null && network.id == i)
{
var consumersToRemove = powerConRemoval[network.id];
foreach (var node in network.nodes)
node.consumers.RemoveAll(x => consumersToRemove.Contains(x));
network.consumers.RemoveAll(x => consumersToRemove.Contains(x));
}
}
if (factory.planet.factoryModel != null)
factory.planet.factoryModel.RefreshPowerConsumers();
}
void DeleteBelts()
{
var beltPool = cargoTraffic.beltPool;
void TakeBackItemsOptimized()
{
var cargoContainer = cargoTraffic.container;
var cargoPool = cargoContainer.cargoPool;
var cursor = cargoContainer.cursor;
for (int i = 0; i < cursor; i++)
{
ref var cargo = ref cargoPool[i];
if (cargo.item == 0)
continue;
if (!takeBackCount.ContainsKey(cargo.item))
{
Warn($"found cargo item not in LDB.items.dataArray: {cargo.item}");
takeBackCount[cargo.item] = 0;
takeBackInc[cargo.item] = 0;
}
takeBackCount[cargo.item] += cargo.stack;
takeBackInc[cargo.item] += cargo.inc;
cargo.stack = 0;
cargo.inc = 0;
cargo.item = 0;
cargoContainer.recycleIds[cargoContainer.recycleEnd & (cargoContainer.poolCapacity - 1)] = i;
cargoContainer.recycleEnd++;
}
}
TakeBackItemsOptimized();
for (int i = 1; i < cargoTraffic.beltCursor; i++)
{
ref var belt = ref beltPool[i];
var entityId = belt.entityId;
if (entityId == 0)
continue;
var entityData = factory.entityPool[belt.entityId];
// record the belt in the take back data
takeBackCount[entityData.protoId]++;
// factory.RemoveEntityWithComponents(entityId);
// The above call is potentially too expensive,
// so we try to remove the component before letting it run
if (belt.id != 0)
{
cargoTraffic.RemoveBeltRenderer(i);
cargoTraffic.RemoveCargoPath(belt.segPathId);
belt.SetEmpty();
cargoTraffic.beltRecycle[cargoTraffic.beltRecycleCursor] = i;
cargoTraffic.beltRecycleCursor++;
}
factory.RemoveEntityWithComponents(entityId);
}
}
var stopwatch = new HighStopwatch();
stopwatch.Begin();
DeleteBelts();
DeleteInserters();
foreach (var kvp in takeBackCount)
player.TryAddItemToPackage(kvp.Key, kvp.Value, takeBackInc[kvp.Key], true);
Debug($"Took {stopwatch.duration} to fast delete belts");
}
}
}