-
Notifications
You must be signed in to change notification settings - Fork 0
/
mining-rig.cs
183 lines (158 loc) · 5.86 KB
/
mining-rig.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
string LocalGridConnector = "Mining Connector";
string LocalGridDrillGroup = "Mining Elite Drills";
string LocalGridPistonGroup = "All Mining Pistons";
string LocalGridRotorGroup = "Mining Advanced Rotors";
string LocalGridConnectorPistonGroup = "Mining Connector Pistons";
string RemoteGridReceiver = "BaseMiningReceiver";
bool isRotorMovingUp = true;
const string WaitingForConnection = "WaitingForConnection";
const string NormalOperation = "Normal";
string currentState = NormalOperation;
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Update100;
// Load existing settings
var settings = LoadStorage();
// Initialize or update rotor direction from storage
string direction;
if (settings.TryGetValue("RotorDirection", out direction))
{
isRotorMovingUp = direction == "up";
}
else
{
// Set default if not found
isRotorMovingUp = true;
settings["RotorDirection"] = "up"; // Default setting
SaveStorage(settings);
}
}
// Serialize key-value pairs into a storage string
void SaveStorage(Dictionary<string, string> data)
{
Storage = string.Join(";", data.Select(kv => kv.Key + "=" + kv.Value));
}
// Deserialize the storage string back into key-value pairs
Dictionary<string, string> LoadStorage()
{
return Storage.Split(';')
.Where(part => part.Contains('='))
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => split[1]);
}
public void Main(string argument, UpdateType updateSource)
{
ChangeRotorDirection();
if (argument == "reset")
{
currentState = NormalOperation;
}
// Get all cargo containers on the same grid as the programmable block
var containers = new List<IMyCargoContainer>();
GridTerminalSystem.GetBlocksOfType(containers, container => container.CubeGrid == Me.CubeGrid);
double totalVolume = 0;
double currentVolume = 0;
foreach (var container in containers)
{
var inventory = container.GetInventory(0);
totalVolume += (double)inventory.MaxVolume;
currentVolume += (double)inventory.CurrentVolume;
}
bool isFull = currentVolume >= totalVolume;
bool isEmpty = currentVolume == 0;
if (currentState == WaitingForConnection)
{
CheckConnectorStatus();
}
else if (isFull)
{
HandleFullInventory();
}
else if (isEmpty)
{
HandleEmptyInventory();
}
}
void HandleFullInventory()
{
ControlBlockGroup<IMyPistonBase>(LocalGridPistonGroup, piston => piston.Enabled = false);
ControlBlockGroup<IMyMotorStator>(LocalGridRotorGroup, rotor => rotor.RotorLock = true);
ControlBlockGroup<IMyMotorStator>(LocalGridRotorGroup, rotor => rotor.Enabled = false);
ControlBlockGroup<IMyShipDrill>(LocalGridDrillGroup, drill => drill.Enabled = false);
ControlBlockGroup<IMyPistonBase>(LocalGridConnectorPistonGroup, piston => piston.Extend());
IGC.SendBroadcastMessage(RemoteGridReceiver, "Extend", TransmissionDistance.TransmissionDistanceMax);
var connector = GridTerminalSystem.GetBlockWithName(LocalGridConnector) as IMyShipConnector;
if (connector != null)
{
connector.Connect();
currentState = WaitingForConnection;
}
}
void HandleEmptyInventory()
{
var connector = GridTerminalSystem.GetBlockWithName(LocalGridConnector) as IMyShipConnector;
if (connector != null)
{
connector.Disconnect();
}
IGC.SendBroadcastMessage(RemoteGridReceiver, "Retract", TransmissionDistance.TransmissionDistanceMax);
ControlBlockGroup<IMyPistonBase>(LocalGridConnectorPistonGroup, piston => piston.Retract());
ControlBlockGroup<IMyShipDrill>(LocalGridDrillGroup, drill => drill.Enabled = true);
ControlBlockGroup<IMyMotorStator>(LocalGridRotorGroup, rotor => rotor.RotorLock = false);
ControlBlockGroup<IMyMotorStator>(LocalGridRotorGroup, rotor => rotor.Enabled = true);
ControlBlockGroup<IMyPistonBase>(LocalGridPistonGroup, piston => piston.Enabled = true);
currentState = NormalOperation;
}
void CheckConnectorStatus()
{
var connector = GridTerminalSystem.GetBlockWithName(LocalGridConnector) as IMyShipConnector;
if (connector != null && connector.Status == MyShipConnectorStatus.Connected)
{
currentState = NormalOperation; // Change state back to normal after connection is confirmed
// Here you might add further actions that should take place once the connection is confirmed
}
}
void ControlBlockGroup<T>(string groupName, Action<T> action) where T : class, IMyTerminalBlock
{
var group = GridTerminalSystem.GetBlockGroupWithName(groupName);
if (group == null)
{
Echo("Group not found: " + groupName);
return;
}
var blocks = new List<T>();
group.GetBlocksOfType(blocks);
foreach (var block in blocks)
{
action(block);
}
}
void ChangeRotorDirection()
{
var rotor = GridTerminalSystem.GetBlockWithName("Mining Advanced Rotor Top") as IMyMotorStator;
if (rotor == null)
{
Echo("Rotor not found!");
return;
}
double currentAngle = MathHelper.ToDegrees(rotor.Angle);
Echo($"Current Rotor Angle: {currentAngle} degrees");
bool shouldMoveUp = currentAngle < 160;
bool shouldMoveDown = currentAngle > 200;
var settings = LoadStorage();
if (shouldMoveUp && !isRotorMovingUp)
{
rotor.TargetVelocityRPM = Math.Abs(rotor.TargetVelocityRPM);
isRotorMovingUp = true;
settings["RotorDirection"] = "up"; // Update setting
Echo("Rotor direction changed to move up.");
}
else if (shouldMoveDown && isRotorMovingUp)
{
rotor.TargetVelocityRPM = -Math.Abs(rotor.TargetVelocityRPM);
isRotorMovingUp = false;
settings["RotorDirection"] = "down"; // Update setting
Echo("Rotor direction changed to move down.");
}
SaveStorage(settings);
}