Refactor for consistency.
This commit is contained in:
parent
302609a004
commit
e897b5dfd3
|
@ -5,6 +5,21 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
public class Door
|
public class Door
|
||||||
{
|
{
|
||||||
|
private MyGridProgram _p;
|
||||||
|
private List<DoorHinge> _hinges;
|
||||||
|
|
||||||
|
public bool Locked
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
foreach (DoorHinge hinge in _hinges)
|
||||||
|
{
|
||||||
|
if (!hinge.Locked) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Door(MyGridProgram p)
|
public Door(MyGridProgram p)
|
||||||
{
|
{
|
||||||
_p = p;
|
_p = p;
|
||||||
|
@ -43,17 +58,5 @@ namespace IngameScript
|
||||||
}
|
}
|
||||||
return done;
|
return done;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Locked()
|
|
||||||
{
|
|
||||||
foreach (DoorHinge hinge in _hinges)
|
|
||||||
{
|
|
||||||
if (!hinge.Locked) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private MyGridProgram _p;
|
|
||||||
private List<DoorHinge> _hinges;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,6 +6,16 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
public class DoorHinge
|
public class DoorHinge
|
||||||
{
|
{
|
||||||
|
public bool Locked { get { return _hinge.RotorLock; } }
|
||||||
|
|
||||||
|
private MyGridProgram _p;
|
||||||
|
private IMyMotorStator _hinge;
|
||||||
|
private float _targetAngle;
|
||||||
|
private float _lastAngle;
|
||||||
|
private float _openAngle = 90F;
|
||||||
|
private float _closedAngle = 0F;
|
||||||
|
private float _velocity = 5F;
|
||||||
|
|
||||||
public DoorHinge(MyGridProgram p, IMyMotorStator hinge)
|
public DoorHinge(MyGridProgram p, IMyMotorStator hinge)
|
||||||
{
|
{
|
||||||
_p = p;
|
_p = p;
|
||||||
|
@ -42,8 +52,6 @@ namespace IngameScript
|
||||||
return Locked;
|
return Locked;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Locked { get { return _hinge.RotorLock; } }
|
|
||||||
|
|
||||||
private void parseConfig()
|
private void parseConfig()
|
||||||
{
|
{
|
||||||
string[] lines = _hinge.CustomData.Split('\n');
|
string[] lines = _hinge.CustomData.Split('\n');
|
||||||
|
@ -70,13 +78,5 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
return degrees * ((float)Math.PI / 180F);
|
return degrees * ((float)Math.PI / 180F);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MyGridProgram _p;
|
|
||||||
private IMyMotorStator _hinge;
|
|
||||||
private float _targetAngle;
|
|
||||||
private float _lastAngle;
|
|
||||||
private float _openAngle = 90F;
|
|
||||||
private float _closedAngle = 0F;
|
|
||||||
private float _velocity = 5F;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
25
MechanicalDoor/MechanicalDoor.sln
Normal file
25
MechanicalDoor/MechanicalDoor.sln
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.5.002.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MechanicalDoor", "MechanicalDoor.csproj", "{2C578770-65D8-4DF9-A207-B884D73781DD}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{2C578770-65D8-4DF9-A207-B884D73781DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{2C578770-65D8-4DF9-A207-B884D73781DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{2C578770-65D8-4DF9-A207-B884D73781DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{2C578770-65D8-4DF9-A207-B884D73781DD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {3F9DC75B-CA4A-4C90-8DEA-EA981315767A}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
|
@ -1,40 +1,23 @@
|
||||||
using Sandbox.Game.EntityComponents;
|
using Sandbox.ModAPI.Ingame;
|
||||||
using Sandbox.ModAPI.Ingame;
|
|
||||||
using Sandbox.ModAPI.Interfaces;
|
|
||||||
using SpaceEngineers.Game.ModAPI.Ingame;
|
using SpaceEngineers.Game.ModAPI.Ingame;
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
|
||||||
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
|
namespace IngameScript
|
||||||
{
|
{
|
||||||
public partial class Program : MyGridProgram
|
public partial class Program : MyGridProgram
|
||||||
{
|
{
|
||||||
MyCommandLine cli;
|
private MyCommandLine _cli;
|
||||||
List<IEnumerator<bool>> jobs;
|
private List<IEnumerator<bool>> _jobs;
|
||||||
Dictionary<string, Door> doors;
|
private Dictionary<string, Door> _doors;
|
||||||
int tickCount;
|
private int _tickCount;
|
||||||
|
|
||||||
|
|
||||||
public Program()
|
public Program()
|
||||||
{
|
{
|
||||||
tickCount = 0;
|
_tickCount = 0;
|
||||||
cli = new MyCommandLine();
|
_cli = new MyCommandLine();
|
||||||
jobs = new List<IEnumerator<bool>>();
|
_jobs = new List<IEnumerator<bool>>();
|
||||||
|
|
||||||
doors = new Dictionary<string, Door>();
|
_doors = new Dictionary<string, Door>();
|
||||||
|
|
||||||
List<IMyMotorStator> allHinges = new List<IMyMotorStator>();
|
List<IMyMotorStator> allHinges = new List<IMyMotorStator>();
|
||||||
GridTerminalSystem.GetBlocksOfType(allHinges);
|
GridTerminalSystem.GetBlocksOfType(allHinges);
|
||||||
|
@ -43,33 +26,33 @@ namespace IngameScript
|
||||||
if (hinge.CustomName.StartsWith("Door"))
|
if (hinge.CustomName.StartsWith("Door"))
|
||||||
{
|
{
|
||||||
string doorName = hinge.CustomName.Split(' ')[0];
|
string doorName = hinge.CustomName.Split(' ')[0];
|
||||||
if (!doors.ContainsKey(doorName))
|
if (!_doors.ContainsKey(doorName))
|
||||||
{
|
{
|
||||||
doors[doorName] = new Door(this);
|
_doors[doorName] = new Door(this);
|
||||||
}
|
}
|
||||||
doors[doorName].AddHinge(hinge);
|
_doors[doorName].AddHinge(hinge);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Echo($"Found {doors.Keys.Count} doors.");
|
Echo($"Found {_doors.Keys.Count} doors.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Main(string argument, UpdateType updateSource)
|
public void Main(string argument, UpdateType updateSource)
|
||||||
{
|
{
|
||||||
Echo($"index: {tickCount++}");
|
Echo($"index: {_tickCount++}");
|
||||||
|
|
||||||
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal)
|
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal)
|
||||||
{
|
{
|
||||||
// Create a new job
|
// Create a new job
|
||||||
cli.TryParse(argument);
|
_cli.TryParse(argument);
|
||||||
List<Door> doorsToControl = new List<Door>();
|
List<Door> doorsToControl = new List<Door>();
|
||||||
|
|
||||||
if (cli.ArgumentCount == 0)
|
if (_cli.ArgumentCount == 0)
|
||||||
{
|
{
|
||||||
Echo("No arguments passed. Controlling all doors.");
|
Echo("No arguments passed. Controlling all doors.");
|
||||||
foreach (Door door in doors.Values)
|
foreach (Door door in _doors.Values)
|
||||||
{
|
{
|
||||||
if (!door.Locked())
|
if (!door.Locked)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -77,20 +60,20 @@ namespace IngameScript
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < cli.ArgumentCount; i++)
|
for (int i = 0; i < _cli.ArgumentCount; i++)
|
||||||
{
|
{
|
||||||
string key = "Door" + cli.Argument(i);
|
string key = "Door" + _cli.Argument(i);
|
||||||
if (!doors.ContainsKey(key))
|
if (!_doors.ContainsKey(key))
|
||||||
{
|
{
|
||||||
Echo($"Door with identifier {key} not found. Skipping.");
|
Echo($"Door with identifier {key} not found. Skipping.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!doors[key].Locked())
|
if (!_doors[key].Locked)
|
||||||
{
|
{
|
||||||
Echo($"Door {key} already moving. Skipping.");
|
Echo($"Door {key} already moving. Skipping.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
doorsToControl.Add(doors[key]);
|
doorsToControl.Add(_doors[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doorsToControl.Count == 0)
|
if (doorsToControl.Count == 0)
|
||||||
|
@ -100,51 +83,51 @@ namespace IngameScript
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Echo("Creating new job.");
|
Echo("Creating new job.");
|
||||||
if (cli.Switch("close")) jobs.Add(CloseDoors(doorsToControl));
|
if (_cli.Switch("close")) _jobs.Add(closeDoors(doorsToControl));
|
||||||
else jobs.Add(OpenDoors(doorsToControl));
|
else _jobs.Add(openDoors(doorsToControl));
|
||||||
Runtime.UpdateFrequency = UpdateFrequency.Update1;
|
Runtime.UpdateFrequency = UpdateFrequency.Update1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process running jobs
|
// Process running jobs
|
||||||
for (int i = 0; i < jobs.Count; i++)
|
for (int i = 0; i < _jobs.Count; i++)
|
||||||
{
|
{
|
||||||
if (!jobs[i].MoveNext())
|
if (!_jobs[i].MoveNext())
|
||||||
{
|
{
|
||||||
jobs[i].Dispose();
|
_jobs[i].Dispose();
|
||||||
jobs.Remove(jobs[i]);
|
_jobs.Remove(_jobs[i]);
|
||||||
i--;
|
i--;
|
||||||
Echo("Operation Complete.");
|
Echo("Operation Complete.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jobs.Count == 0)
|
if (_jobs.Count == 0)
|
||||||
{
|
{
|
||||||
Runtime.UpdateFrequency = UpdateFrequency.None;
|
Runtime.UpdateFrequency = UpdateFrequency.None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator<bool> OpenDoors(List<Door> doorsToControl)
|
private IEnumerator<bool> openDoors(List<Door> doorsToControl)
|
||||||
{
|
{
|
||||||
Echo("Opening doors.");
|
Echo("Opening doors.");
|
||||||
foreach (Door door in doorsToControl)
|
foreach (Door door in doorsToControl)
|
||||||
{
|
{
|
||||||
door.OpenDoor();
|
door.OpenDoor();
|
||||||
}
|
}
|
||||||
return ActuateDoors(doorsToControl);
|
return actuateDoors(doorsToControl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator<bool> CloseDoors(List<Door> doorsToControl)
|
private IEnumerator<bool> closeDoors(List<Door> doorsToControl)
|
||||||
{
|
{
|
||||||
Echo("Closing doors.");
|
Echo("Closing doors.");
|
||||||
foreach (Door door in doorsToControl)
|
foreach (Door door in doorsToControl)
|
||||||
{
|
{
|
||||||
door.CloseDoor();
|
door.CloseDoor();
|
||||||
}
|
}
|
||||||
return ActuateDoors(doorsToControl);
|
return actuateDoors(doorsToControl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator<bool> ActuateDoors(List<Door> doorsToControl)
|
private IEnumerator<bool> actuateDoors(List<Door> doorsToControl)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user