-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
257 lines (222 loc) · 9.11 KB
/
Program.cs
File metadata and controls
257 lines (222 loc) · 9.11 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Cube;
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using VRage;
using VRage.Collections;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ObjectBuilders.Definitions;
using VRageMath;
namespace IngameScript
{
partial class Program : MyGridProgram
{
/*
* R e a d m e
* -----------
* Production equipment inventory management for LCD.
* 太空工程师,生产设备库存管理脚本, 自动将装配机精炼厂的产物,放进储物箱子。
*
* @see <https://github.com/se-scripts/produce-inventory-manager>
* @author [Hi.James](https://space.bilibili.com/368005035)
* @author [li-guohao](https://github.com/li-guohao)
*/
const string version = "1.0.1";
MyIni _ini = new MyIni();
List<IMyCargoContainer> cargoContainers = new List<IMyCargoContainer>();
List<IMyAssembler> assemblers = new List<IMyAssembler>();
List<IMyRefinery> refineries = new List<IMyRefinery>();
List<IMyShipConnector> connectors = new List<IMyShipConnector>();
List<IMyCryoChamber> cryoChambers = new List<IMyCryoChamber>();
List<IMyCockpit> cockpits = new List<IMyCockpit>();
int counter_InventoryManagement = 0;
double counter_Logo = 0;
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Update100;
GridTerminalSystem.GetBlocksOfType(cargoContainers, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(assemblers, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(refineries, b => b.IsSameConstructAs(Me) && !b.BlockDefinition.ToString().Contains("Shield"));
GridTerminalSystem.GetBlocksOfType(connectors, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(cryoChambers, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(cockpits, b => b.IsSameConstructAs(Me));
ProgrammableBlockScreen();
}
public void DebugLCD(string text)
{
List<IMyTextPanel> debugPanel = new List<IMyTextPanel>();
GridTerminalSystem.GetBlocksOfType(debugPanel, b => b.IsSameConstructAs(Me) && b.CustomName == "DEBUGLCD");
if (debugPanel.Count == 0) return;
string temp = "";
foreach (var panel in debugPanel)
{
temp = "";
temp = panel.GetText();
}
foreach (var panel in debugPanel)
{
if (panel.ContentType != ContentType.TEXT_AND_IMAGE) panel.ContentType = ContentType.TEXT_AND_IMAGE;
panel.FontSize = 0.55f;
panel.Font = "LoadingScreen";
panel.WriteText(DateTime.Now.ToString(), false);
panel.WriteText("\n", true);
panel.WriteText(text, true);
panel.WriteText("\n", true);
panel.WriteText(temp, true);
}
}
public void AssemblerToCargoContainers()
{
Echo("AssemblerToCargoContainers");
foreach (var assembler in assemblers) {
if (!assembler.IsProducing)
{
TransferToCargoContainers(assembler.InputInventory);
}
TransferToCargoContainers(assembler.OutputInventory);
}
}// Assembler_to_CargoContainers END!
public void TransferItems(IMyInventory from, IMyInventory to)
{
if (from == null || to == null) { return; }
if (from.ItemCount == 0) { return; }
if (to.IsFull || !to.CanPutItems) { return; }
List<MyInventoryItem> items = new List<MyInventoryItem>(from.ItemCount);
from.GetItems(items);
foreach (var item in items) {
if (!from.CanTransferItemTo(to, item.Type)) { continue; }
from.TransferItemTo(to, item);
}
}
public void TransferToCargoContainers(IMyInventory from) {
if (from == null ) { return; }
if (from.ItemCount == 0) { return; }
if (cargoContainers == null || cargoContainers.Count == 0) { return;}
foreach (var cargo in cargoContainers) {
TransferItems(from, cargo.GetInventory());
}
}
public void RefineryToCargoContainers()
{
Echo("RefineryToCargoContainers");
foreach (var refinery in refineries) {
if (!refinery.IsProducing)
{
TransferToCargoContainers(refinery.InputInventory);
}
TransferToCargoContainers(refinery.OutputInventory);
}
}// Refinery_to_CargoContainers END!
public void ConnectorToCargoContainers()
{
Echo("ConnectorToCargoContainers");
foreach (var connector in connectors) {
TransferToCargoContainers(connector.GetInventory());
}
}
public void CockpitToCargoContainers() {
Echo("CockpitToCargoContainers");
foreach (var cockpit in cockpits) {
TransferToCargoContainers(cockpit.GetInventory());
}
}
public void ProgrammableBlockScreen()
{
// 512 X 320
IMyTextSurface panel = Me.GetSurface(0);
if (panel == null) return;
panel.ContentType = ContentType.SCRIPT;
MySpriteDrawFrame frame = panel.DrawFrame();
float x = 512 / 2, y1 = 205;
DrawLogo(frame, x, y1, 200);
PanelWriteText(frame, "Inventory management\nby Hi.James and li-guohao\nWith version " + version, x, y1 + 110, 1f, TextAlignment.CENTER);
frame.Dispose();
}
public void PanelWriteText(MySpriteDrawFrame frame, string text, float x, float y, float fontSize, TextAlignment alignment)
{
MySprite sprite = new MySprite()
{
Type = SpriteType.TEXT,
Data = text,
Position = new Vector2(x, y),
RotationOrScale = fontSize,
Color = Color.Coral,
Alignment = alignment,
FontId = "LoadingScreen"
};
frame.Add(sprite);
}
public void DrawLogo(MySpriteDrawFrame frame, float x, float y, float width)
{
MySprite sprite = new MySprite()
{
Type = SpriteType.TEXTURE,
Data = "Screen_LoadingBar",
Position = new Vector2(x, y),
Size = new Vector2(width - 6, width - 6),
RotationOrScale = Convert.ToSingle(counter_Logo / 360 * 2 * Math.PI),
Alignment = TextAlignment.CENTER,
};
frame.Add(sprite);
sprite = new MySprite()
{
Type = SpriteType.TEXTURE,
Data = "Screen_LoadingBar",
Position = new Vector2(x, y),
Size = new Vector2(width / 2, width / 2),
RotationOrScale = Convert.ToSingle(2 * Math.PI - counter_Logo / 360 * 2 * Math.PI),
Alignment = TextAlignment.CENTER,
};
frame.Add(sprite);
sprite = new MySprite()
{
Type = SpriteType.TEXTURE,
Data = "Screen_LoadingBar",
Position = new Vector2(x, y),
Size = new Vector2(width / 4, width / 4),
RotationOrScale = Convert.ToSingle(Math.PI + counter_Logo / 360 * 2 * Math.PI),
Alignment = TextAlignment.CENTER,
};
frame.Add(sprite);
}
public void Main(string argument, UpdateType updateSource)
{
Echo($"{DateTime.Now}");
Echo("Program is running.");
DateTime beforDT = System.DateTime.Now;
if (counter_InventoryManagement++ >= 5) counter_InventoryManagement = 1;
switch (counter_InventoryManagement)
{
case 1:
AssemblerToCargoContainers();
break;
case 2:
RefineryToCargoContainers();
break;
case 3:
ConnectorToCargoContainers();
break;
case 4:
CockpitToCargoContainers();
break;
}
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforDT);
Echo("Total cost ms:" + ts.TotalMilliseconds);
DebugLCD("Total cost ms: " + ts.TotalMilliseconds);
}
}
}