-
Notifications
You must be signed in to change notification settings - Fork 2
/
rev8.cs
587 lines (492 loc) · 24.7 KB
/
rev8.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
using System;
using System.Collections.Generic;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using Sandbox.Common.ObjectBuilders;
using VRage;
using VRageMath;
class rev8 : Program {
// =================================[ START OF CONFIGURATION ]=================================
static class Configuration {
// Target average maximum power output to reach (in kW)
// Default (Vanilla): 119.5f for large ship/station, 29.5f for small ship
// Allowed values: 0f - 999999999f
public const float TargetAveragePowerOutput = 119.5f;
// Delay between executions while aligning (in seconds)
// Default: 2.0f
// Recommended values: 1.5f - 2.5f
public const float WorkDelay = 2.0f;
// Delay between executions while idling (in seconds)
// Default: 10.0f
// Recommended values: 2.5f - 10.0f
public const float IdleDelay = 10.0f;
// Delay between executions while aligning in hibernation mode (in seconds)
// Default: 1.5f
// Recommended values: 1.5f - 5f;
public const float HibernationWorkDelay = 1.5f;
// Delay between executions while idling in hibernation mode (in seconds)
// Default: 10.0f
// Recommended values: 2.5f - 10.0f
public const float HibernationIdleDelay = 10.0f;
// Maximum deviation when comparing angles (in degrees)
// Default: 1.0f;
// Recommended values: 0.0001f - 1.0f
public const float MaxAngleDeviation = 1.0f;
// Speed of all rotors that are used to align the solar panels (in RPM)
// Lower values mean higher accuracy but also higher alignment time.
// Default: 0.1f
// Recommended values: 0.05f - 0.5f
public const float RotorSpeed = 0.1f;
// Name of the solar panel that should be used for optimization
// The name has to be EXACTLY THE SAME as in the terminal overview.
// Default: "Solar Panel (SPAS)"
public const string SolarPanelName = "Solar Panel (SPAS)";
// Determines if the name that was provided for SolarPanelName should be used as group name (true) or block name (false).
// Default: false
// Allowed values: true, false
public const bool SolarPanelName_IsGroup = false;
// Name of the timer block that should be used for looping
// The name has to be EXACTLY THE SAME as in the terminal overview.
// Default: "Timer Block (SPAS)"
public const string TimerName = "Timer Block (SPAS)";
// Names of all rotors that are connected to the solar panels that should be optimized
// Each name has to be EXACTLY THE SAME as the corresponding rotor's name in the terminal overview.
// Default: { "Advanced Rotor (SPAS)" }
// Allowed values: comma-separated list of strings
public static readonly string[] RotorNames = { "Advanced Rotor (SPAS)" };
// Determines for each element of RotorNames if the name provided should be used as group name (true) or block name (false).
// If this does not have the same amount of elements as RotorNames, each remaining element of RotorNames is treated as block, not as group.
// Default: { }
// Allowed values: comma-separated list of true and false
public static readonly bool[] RotorNames_IsGroup = {};
// Suffix which can be applied to a rotor in a group to make it rotate in the opposite direction.
// Use this in combination with rotor groups to set up more than one rotor in one axis.
// Default: "[inv]"
public const string InvertedRotorSuffix = "[inv]";
// Configure features like the Energy Saver feature inside this class.
public static class Features {
public static class EnergySaver {
// Enables the energy saver feature. This will cause rotors to move faster if the power output is low or make them do nothing if the power output is too low.
// Default: true
// Allowed values: true, false
public const bool Enabled = true;
// Maximum power output at which the script will stay in idle mode (in kW).
// Default (Vanilla): 20 for large ship/station, 5 for small ship
// Allowed values: 0 - 999999999
public const int HibernatePowerOutput = 20;
// Maximum power output at which the rotors will be accelerated by the panic speed multiplier (in kW).
// Default (Vanilla): 60 for large ship/station, 15 for small ship
// Allowed values: 0 - 999999999
public const int PanicPowerOutput = 60;
// Value by which the default rotor speed will be multiplied while in panic mode.
// Default: 2.0f
// Recommended values: 1.0f - 5.0f
public const float PanicSpeedMultiplier = 2.0f;
}
public static class MaintenanceMode {
// Enables the maintenance mode feature. Use it to shut this script down temporarily, e.g. for repairs.
// Default: true
// Allowed values: true, false
public const bool Enabled = true;
// Determines if the script should reinitialize after resuming from maintenance mode. This is needed if you add blocks to a group.
// Default: true
// Allowed values: true, false
public const bool Reinitialize = true;
// Delay between executions while in maintenance mode (in seconds).
// Default: 30.0f
public const float MaintenanceDelay = 30.0f;
// Suffix which should be used to mark a programmable block as in maintenance mode.
// Default: " [MAINTENANCE]"
public const string MaintenanceSuffix = " [MAINTENANCE]";
}
}
// Configure your language by editing the values inside this class to the texts that are displayed inside your game.
public static class Localization {
// Text that is located before the maximum power output value in the detailed description of the solar panel, including whitespaces.
// English: "Max Output: "
public const string MaxOutput = "Max Output: ";
// Text that is located before the current power output value in the detailed description of the solar panel, including whitespaces.
// English: "Current Output: "
public const string CurrentOutput = "Current Output: ";
}
}
// =================================[ END OF CONFIGURATION ]=================================
static class Status { public const int HIBERNATING = -1, IDLING = 0, UPDATING = 1, TESTING = 2, ALIGNING = 3; }
int CurrentStatus {
get { return currentStatus; }
set {
if (value == Status.HIBERNATING) Echo("Hibernating...");
else if (value == Status.IDLING) Echo("Idling...");
else if (value == Status.UPDATING) Echo("Updating...");
else if (value == Status.TESTING) Echo("Testing...");
else if (value == Status.ALIGNING) Echo("Aligning...");
currentStatus = value;
}
}
float currentPower = 0.0f;
int currentRotorIndex = 0;
int currentDirection = 1;
int currentStatus = Status.UPDATING;
IMyTimerBlock timer = null;
List<IMyMotorStator> currentRotors = new List<IMyMotorStator>();
List<IMySolarPanel> solarPanels = new List<IMySolarPanel>();
List<List<IMyMotorStator>> rotors = new List<List<IMyMotorStator>>();
void Main(string argument) {
// toggle maintenance mode
if (Configuration.Features.MaintenanceMode.Enabled && (argument + " ").Contains("maintenance ")) {
if (Me.CustomName.EndsWith(Configuration.Features.MaintenanceMode.MaintenanceSuffix)) {
Me.SetCustomName(Me.CustomName.Substring(0, Me.CustomName.Length - Configuration.Features.MaintenanceMode.MaintenanceSuffix.Length));
Echo("This programmable block is no longer in maintenance mode.");
InitializeSolarPanels(Configuration.Features.MaintenanceMode.Reinitialize);
InitializeRotors(Configuration.Features.MaintenanceMode.Reinitialize);
InitializeTimer(Configuration.Features.MaintenanceMode.Reinitialize);
} else {
Me.SetCustomName(Me.CustomName + Configuration.Features.MaintenanceMode.MaintenanceSuffix);
Echo("This programmable block is now in maintenance mode.");
return;
}
} else {
// initialize the solar panels
InitializeSolarPanels();
// initialize the rotors
InitializeRotors();
// initialize the timer
InitializeTimer();
}
// maintenance mode
if (Me.CustomName.EndsWith(Configuration.Features.MaintenanceMode.MaintenanceSuffix)) {
Echo("MAINTENANCE MODE");
Utilities.Trigger(timer, Configuration.Features.MaintenanceMode.MaintenanceDelay);
return;
}
// get current power output
if (!Utilities.AverageMaxOutput(solarPanels, out currentPower)) throw new Exception(" Main(): failed to read average maximum power from solar panels\n\nDid you configure this script correctly?");
if (currentPower == float.NaN) throw new Exception(" Main(): failed to read average maximum power from solar panels - power output too high (>= 1 PW)");
Echo("current average output: " + currentPower + " kW");
// check if the energy saver feature has been enabled
if (Configuration.Features.EnergySaver.Enabled) {
// check if the current power output is too low to make rotating effective but allow direction testing
if (currentPower <= Configuration.Features.EnergySaver.HibernatePowerOutput && CurrentStatus != Status.TESTING) {
Hibernate();
return;
}
}
// check if the target output has been reached
if (currentPower >= Configuration.TargetAveragePowerOutput) {
Idle();
return;
}
// update the rotor and check which direction yields the higher power output
if (CurrentStatus == Status.UPDATING || CurrentStatus == Status.IDLING || CurrentStatus == Status.HIBERNATING) {
currentRotors = rotors[currentRotorIndex];
UpdateNames();
Utilities.SetSpeed(currentRotors, GetRotorSpeed());
Utilities.ToggleOn(currentRotors);
CurrentStatus = Status.TESTING;
Utilities.Trigger(timer, Configuration.WorkDelay);
return;
}
// set the rotation direction towards the higher power output
if (CurrentStatus == Status.TESTING) {
Utilities.SetSpeed(currentRotors, GetRotorSpeed());
Utilities.ToggleOff(currentRotors);
float oldPower;
UpdateNames(out oldPower);
if (currentPower < oldPower) {
currentDirection = -currentDirection;
Utilities.SetSpeed(currentRotors, GetRotorSpeed());
}
CurrentStatus = Status.ALIGNING;
}
// rotate towards the local maximum output
if (CurrentStatus == Status.ALIGNING) {
Utilities.SetSpeed(currentRotors, GetRotorSpeed());
Utilities.ToggleOn(currentRotors);
float oldPower;
UpdateNames(out oldPower);
if (currentPower < oldPower) {
if (rotors.Count > 1) {
Utilities.ToggleOff(currentRotors);
CurrentStatus = Status.UPDATING;
currentRotorIndex = (currentRotorIndex + 1) % rotors.Count;
} else {
currentDirection = -currentDirection;
Utilities.SetSpeed(currentRotors, GetRotorSpeed());
}
}
Utilities.Trigger(timer, Configuration.WorkDelay);
return;
}
}
void InitializeSolarPanels(bool forced = false) {
// exit this method if the solar panels have already been initialized
if (solarPanels.Count > 0 && !forced) return;
if (Configuration.SolarPanelName_IsGroup) {
InitializeSolarPanelGroup();
} else {
InitializeSolarPanel();
}
Echo("initialized solar panels");
}
void InitializeSolarPanelGroup() {
// get a list of all block groups
List<IMyBlockGroup> groups = new List<IMyBlockGroup>();
GridTerminalSystem.GetBlockGroups(groups);
// search all block groups for one that has the provided name as its own
for (int i = 0; i < groups.Count; i++) {
IMyBlockGroup group = groups[i];
if (group.Name.Equals(Configuration.SolarPanelName)) {
// add all solar panels in that group
for (int j = 0; j < group.Blocks.Count; j++) {
IMySolarPanel solarPanel = group.Blocks[j] as IMySolarPanel;
if (solarPanel != null) {
solarPanels.Add(solarPanel);
}
}
break;
}
}
if (solarPanels.Count <= 0) throw new Exception(" InitializeSolarPanelGroup(): failed to find any solar panels in the group \"" + Configuration.SolarPanelName + "\"");
}
void InitializeSolarPanel() {
// get a list of all solar panels
List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(panels);
// search all solar panels for one which starts with the provided name
for (int i = 0; i < panels.Count; i++) {
IMySolarPanel solarPanel = panels[i] as IMySolarPanel;
if (solarPanel != null && solarPanel.CustomName.StartsWith(Configuration.SolarPanelName)) {
solarPanels.Add(solarPanel);
break;
}
}
if (solarPanels.Count <= 0) throw new Exception(" InitializeSolarPanels(): failed to find solar panel with name \"" + Configuration.SolarPanelName + "\"");
}
void InitializeRotors(bool forced = false) {
// exit this method if the rotors have already been initialized
if (rotors.Count > 0 && !forced) return;
for (int i = 0; i < Configuration.RotorNames.Length; i++) {
List<IMyMotorStator> axis = new List<IMyMotorStator>();
string name = Configuration.RotorNames[i];
bool isGroup = i >= Configuration.RotorNames_IsGroup.Length? false : Configuration.RotorNames_IsGroup[i];
if (isGroup) {
InitializeRotorGroup(axis, name);
} else {
InitializeRotor(axis, name);
}
rotors.Add(axis);
}
Echo("initialized rotors");
}
void InitializeRotorGroup(List<IMyMotorStator> axis, string groupName) {
// get a list of all block groups
List<IMyBlockGroup> groups = new List<IMyBlockGroup>();
GridTerminalSystem.GetBlockGroups(groups);
bool foundGroup = false;
// search all block groups for one that has the provided name as its own
for (int j = 0; j < groups.Count; j++) {
IMyBlockGroup group = groups[j];
if (group.Name.Equals(groupName)) {
foundGroup = true;
// add all rotors in that group
for (int k = 0; k < group.Blocks.Count; k++) {
IMyMotorStator rotor = group.Blocks[k] as IMyMotorStator;
if (rotor != null) {
axis.Add(rotor);
Utilities.ToggleOff(rotor);
}
}
break;
}
}
if (!foundGroup) throw new Exception(" InitializeRotors(): failed to find group with name \"" + groupName + "\"");
if (axis.Count <= 0) throw new Exception(" InitializeRotors(): failed to find any rotors in the group \"" + groupName + "\"");
}
void InitializeRotor(List<IMyMotorStator> axis, string rotorName) {
IMyMotorStator rotor = GridTerminalSystem.GetBlockWithName(rotorName) as IMyMotorStator;
if (rotor == null) {
string message = " InitializeRotor(): failed to find rotor with name \"" + rotorName + "\"";
if (rotorName.Contains(",")) {
message += "\n\nDid you want to write\nRotorNames = { [...]\"";
string[] split = rotorName.Split(',');
for (int k = 0; k < split.Length; k++) {
message += split[k];
if (k < split.Length - 1) message += "\", \"";
}
message += "\" [...] };\ninstead?";
}
throw new Exception(message);
}
axis.Add(rotor);
Utilities.ToggleOff(rotor);
}
void InitializeTimer(bool forced = false) {
// exit this method if the timer has already been initialized
if (timer != null && !forced) return;
timer = GridTerminalSystem.GetBlockWithName(Configuration.TimerName) as IMyTimerBlock;
if (timer == null) throw new Exception("InitializeTimer(): failed to find timer block with name \"" + Configuration.TimerName + "\"");
Echo("initialized timer");
}
void Idle() {
for (int i = 0; i < rotors.Count; i++) {
Utilities.ToggleOff(rotors[i]);
}
UpdateNames();
CurrentStatus = Status.IDLING;
Utilities.Trigger(timer, Configuration.IdleDelay);
}
void Hibernate() {
bool idle = true;
float speed = GetRotorSpeed();
for (int i = 0; i < rotors.Count; i++) {
List<IMyMotorStator> axis = rotors[i];
for (int j = 0; j < axis.Count; j++) {
IMyMotorStator rotor = axis[j];
if (Utilities.ReachedLowerLimit(rotor) || Utilities.ReachedUpperLimit(rotor)) {
Utilities.ToggleOff(rotor);
} else {
float distanceFromUpperLimit = rotor.UpperLimit - rotor.Angle;
float distanceFromLowerLimit = rotor.Angle - rotor.LowerLimit;
Utilities.ToggleOn(rotor);
if (distanceFromLowerLimit < distanceFromUpperLimit) Utilities.RotateTowards(rotor, Utilities.ToDegrees(rotor.UpperLimit), speed);
else Utilities.RotateTowards(rotor, Utilities.ToDegrees(rotor.LowerLimit), speed);
idle = false;
}
}
}
UpdateNames();
CurrentStatus = Status.HIBERNATING;
Utilities.Trigger(timer, idle ? Configuration.HibernationIdleDelay : Configuration.HibernationWorkDelay);
}
void UpdateNames() {
float __ignored;
UpdateNames(out __ignored);
}
void UpdateNames(out float oldPower) {
oldPower = 0.0f;
for (int i = 0; i < solarPanels.Count; i++) {
IMySolarPanel panel = solarPanels[i];
float panelOutput;
if (!Utilities.MaxOutput(panel, out panelOutput)) continue;
string[] split = panel.CustomName.Split('~');
if (split.Length <= 1) {
panel.SetCustomName(panel.CustomName + "~" + panelOutput);
continue;
}
float panelOldOutput;
float.TryParse(split[split.Length - 1], out panelOldOutput);
string name = "";
for (int k = 0; k < split.Length - 1; k++) {
name += split[k];
}
name += "~" + panelOutput;
panel.SetCustomName(name);
if (oldPower == 0) oldPower = panelOldOutput;
else oldPower = (oldPower + panelOldOutput) / 2;
}
}
float GetRotorSpeed() {
float speed = Configuration.RotorSpeed;
if (Configuration.Features.EnergySaver.Enabled && currentPower <= Configuration.Features.EnergySaver.PanicPowerOutput) {
speed *= Configuration.Features.EnergySaver.PanicSpeedMultiplier;
Echo("Panicking...");
}
return speed * currentDirection;
}
static class Utilities {
public static void ToggleOn(IMyFunctionalBlock block) {
block.GetActionWithName("OnOff_On").Apply(block);
}
public static void ToggleOn(List<IMyMotorStator> blocks) {
for (int i = 0; i < blocks.Count; i++) {
ToggleOn(blocks[i]);
}
}
public static void ToggleOff(IMyFunctionalBlock block) {
block.GetActionWithName("OnOff_Off").Apply(block);
}
public static void ToggleOff(List<IMyMotorStator> blocks) {
for (int i = 0; i < blocks.Count; i++) {
ToggleOff(blocks[i]);
}
}
public static void Trigger(IMyTimerBlock timer, float delay) {
timer.SetValue("TriggerDelay", delay);
timer.GetActionWithName("Start").Apply(timer);
}
public static void SetSpeed(IMyMotorStator rotor, float speed) {
if (rotor.CustomName.EndsWith(Configuration.InvertedRotorSuffix)) speed = -speed;
rotor.SetValue("Velocity", speed);
}
public static void SetSpeed(List<IMyMotorStator> rotors, float speed) {
for (int i = 0; i < rotors.Count; i++) {
SetSpeed(rotors[i], speed);
}
}
public static void RotateTowards(IMyMotorStator rotor, float target, float speed) {
float angle = ToDegrees(rotor.Angle);
if (RotationEquals(angle, target)) SetSpeed(rotor, 0);
float posrot = (target + 360 - angle) % 360;
float negrot = (angle + 360 - target) % 360;
if (posrot < negrot) {
if (HasUpperLimit(rotor) && rotor.UpperLimit <= target) SetSpeed(rotor, -speed);
else SetSpeed(rotor, speed);
} else {
if (HasLowerLimit(rotor) && rotor.LowerLimit >= target) SetSpeed(rotor, speed);
else SetSpeed(rotor, -speed);
}
}
public static bool RotationEquals(float a, float b) {
return Math.Abs(a - b) <= Configuration.MaxAngleDeviation;
}
public static bool HasLowerLimit(IMyMotorStator rotor) {
return !float.IsInfinity(rotor.LowerLimit);
}
public static bool HasUpperLimit(IMyMotorStator rotor) {
return !float.IsInfinity(rotor.UpperLimit);
}
public static bool ReachedLowerLimit(IMyMotorStator rotor) {
return !HasLowerLimit(rotor) || (HasLowerLimit(rotor) && RotationEquals(ToDegrees(rotor.Angle), ToDegrees(rotor.LowerLimit)));
}
public static bool ReachedUpperLimit(IMyMotorStator rotor) {
return !HasUpperLimit(rotor) || (HasUpperLimit(rotor) && RotationEquals(ToDegrees(rotor.Angle), ToDegrees(rotor.UpperLimit)));
}
public static float ToDegrees(float radians) {
return (float)(radians / Math.PI) * 180;
}
public static bool MaxOutput(IMySolarPanel panel, out float power) {
power = 0.0f;
int start = panel.DetailedInfo.IndexOf(Configuration.Localization.MaxOutput);
int end = panel.DetailedInfo.IndexOf(Configuration.Localization.CurrentOutput);
if (start < 0 || end < 0 || end < start) return false;
start += Configuration.Localization.MaxOutput.Length;
string maxOutput = panel.DetailedInfo.Substring(start, end - start);
if (float.TryParse(System.Text.RegularExpressions.Regex.Replace(maxOutput, @"[^0-9.]", ""), out power)) {
float factor = 0.0f;
if (maxOutput.Contains(" W")) factor = 0.001f;
else if (maxOutput.Contains(" kW")) factor = 1.0f;
else if (maxOutput.Contains(" MW")) factor = 1000.0f;
else if (maxOutput.Contains(" GW")) factor = 1000000.0f;
else if (maxOutput.Contains(" TW")) factor = 1000000000.0f;
else {
power = float.NaN;
return true;
}
power *= factor;
return true;
}
return false;
}
public static bool AverageMaxOutput(List<IMySolarPanel> panels, out float power) {
power = 0.0f;
for (int i = 0; i < panels.Count; i++) {
float maxOutput;
IMySolarPanel panel = panels[i];
if (!MaxOutput(panel, out maxOutput)) return false;
if (power == 0) power = maxOutput;
else power = (power + maxOutput) / 2;
}
return true;
}
}
}