-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElevator.cs
320 lines (291 loc) · 12.3 KB
/
Elevator.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
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.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
{
internal class Elevator
{
private List<IMyPistonBase> _pistons = new List<IMyPistonBase>();
private List<Floor> _floors = new List<Floor>();
private IMyTextPanel _lcdMonitor;
private Direction _direction = Direction.none;
private Floor _destinationFloor;
private double _totalPistonPosition = 0f;
private bool _isInitializedErrorFree = true;
private string _errorMessage = string.Empty;
private float _startPosition = 0.0f;
private float _tolerance = 0.1f;
private float _maxVelocity = 5.0f;
private float _minVelocity = 0.5f;
private float _accelleration = 0.5f;
// =========================[ CONSTRUCTOR ]========================= \\
internal Elevator(List<IMyPistonBase> pistons, List<Floor> floors, IMyTextPanel textPanel = null)
{
this._lcdMonitor = textPanel;
// Allocate Pistons
foreach(IMyPistonBase piston in pistons)
{
if(piston == null)
{
this._errorMessage = "Cannot allocate all pistons.";
this._isInitializedErrorFree = false;
if(this._lcdMonitor != null)
{
this._lcdMonitor.WriteText("Elevator initialization failed!\n" + this._errorMessage);
}
}
else
{
this._pistons.Add(piston);
}
}
// Checking floors
if(this._isInitializedErrorFree)
{
if(floors.Count >= 2)
{
// Check maximum heigth wich can be reach with N pistons (1 piston = 10 units) and allocate the value if it is ok.
foreach(Floor floor in floors)
{
if (floor.Height > (this._pistons.Count * 10))
{
this._errorMessage = "The floor " + floor.Name + " cannot be reached, because there are not enough pistons.";
this._isInitializedErrorFree = false;
if (this._lcdMonitor != null)
{
this._lcdMonitor.WriteText("Elevator initialization failed!\n" + this._errorMessage);
}
}
else
{
this._floors.Add(floor);
}
}
}
else
{
this._errorMessage = "You need at least two floors!";
this._isInitializedErrorFree = false;
if (this._lcdMonitor != null)
{
this._lcdMonitor.WriteText("Elevator initialization failed!\n" + this._errorMessage);
}
}
}
} // End of constructor
private double GetPistonPosition(IMyPistonBase piston)
{
string info = piston.DetailedInfo;
string[] infoSplit = info.Split(':');
string extractedValue = infoSplit[1].Substring(0, infoSplit[1].Length - 1);
double dValue;
if (Double.TryParse(extractedValue, out dValue))
{
return dValue;
}
else
{
this._errorMessage = "(Cannot parse " + extractedValue + ")";
return -1.0f;
}
}
internal string ErrorMessage
{
get
{
return this._errorMessage;
}
}
internal bool IsInitializedErrorFree
{
get
{
return this._isInitializedErrorFree;
}
}
internal float ElevatorPosition
{
get
{
this._totalPistonPosition = 0f;
foreach (IMyPistonBase p in this._pistons)
{
this._totalPistonPosition += this.GetPistonPosition(p);
}
return (float)this._totalPistonPosition;
}
}
internal void StopElevator()
{
for (int i = 0; i < this._pistons.Count; i++)
{
this._pistons[i].Velocity = 0.0000f;
}
this._direction = Direction.none;
this._destinationFloor = null;
}
internal void UpdateLcdScreen()
{
string tmpName = this._destinationFloor == null ? "null" : this._destinationFloor.Name;
string tmpDestHeight = this._destinationFloor == null ? "null" : this._destinationFloor.Height.ToString();
string tmpErrorMsg = this._errorMessage == "" ? "no errors found :-)" : this._errorMessage;
if (this._lcdMonitor != null)
{
this._lcdMonitor.WriteText(
"========[ Elevator Info ]========" +
"\nDate & Time: " + DateTime.Now.ToString() +
"\nDestination name: " + tmpName +
"\nStart position: " + this._startPosition.ToString() +
"\nCurrent height: " + this.ElevatorPosition.ToString() +
"\nDestination height: " + tmpDestHeight.ToString() +
"\nDirection: " + this._direction.ToString() +
"\nDestination reached: " + this.IsDestinationReached.ToString() +
"\n\nError: " + tmpErrorMsg
, false);
}
}
internal bool IsDestinationReached
{
get
{
if(this._destinationFloor != null)
{
if (this.ElevatorPosition >= this._destinationFloor.Height - this._tolerance && this.ElevatorPosition <= this._destinationFloor.Height + this._tolerance)
{
return true;
}
else
{
// Emergency stop ;-)
if (this._direction == Direction.up && this.ElevatorPosition > this._destinationFloor.Height)
{
return true;
}
else if (this._direction == Direction.down && this.ElevatorPosition < this._destinationFloor.Height)
{
return true;
}
else
{
return false;
}
}
}
else
{
// Destination floor wasn't found.
// Stops elevator indirectly by pretending that elevator has reached the target assuming that
// the main program stops the lift if the destination will be reached.
return true;
}
}
}
internal bool SetDestination(string floor)
{
// Search a certain element:
this._destinationFloor = this._floors.Find(f => f.Name == floor);
// Remember the start position for later calculations to get the current velocity.
this._startPosition = this.ElevatorPosition;
if (this._destinationFloor != null)
{
if (this._destinationFloor.Height > this.ElevatorPosition)
{
this._direction = Direction.up;
}
else
{
this._direction = Direction.down;
}
return true;
}
else
{
this._errorMessage = "Cannot reach floor " + floor + " or this floor does not exist.";
this._direction = Direction.none;
return false;
}
}
internal void Move()
{
// Distribute the total velocity to all pistons to reach smooth ride.
// Important notice: If we distribute the velocity, we will get an new problem,
// because the value vor the heigth of a piston has only one decimal place :-(.
// That means that the whole elevators height value is no more precisly like
// with only one piston. (0,1 + 0,1 + 0,1 with three pistons = 0,3 as smalest value)
// Solution: We could allocate the velocity only to one piston at the last meter.
float v = this.GetCurrentVelocity() / this._pistons.Count;
if (this._direction == Direction.up || this._direction == Direction.down)
{
for (int i = 0; i < this._pistons.Count; i++)
{
this._pistons[i].Velocity = v;
}
}
else // direction = none
{
this.StopElevator();
}
}
// Calculate active velocity dynamically and relative to the distance that must be covered.
// We will achieve a smooth start and stop speed.
internal float GetCurrentVelocity()
{
// X must not be zero, because the velocity would be zero forever and the elevator would not start.
// To avoid this, we add a very tiny value to the result.
float x = this.ElevatorPosition - this._startPosition;
float y1, y2, v;
if (this._direction == Direction.up)
{
y1 = this._accelleration * x;
y2 = this._accelleration * -(x - this._destinationFloor.Height + this._startPosition);
}
else
{
// X must not be negative, else we always would get a negative value
// So, when the elevator moves down, we have to change the negative value into an positive.
x = x * -1f;
y1 = this._accelleration * x;
y2 = (this._accelleration * -(x - (this._startPosition - this._destinationFloor.Height)));
}
v = Math.Min(Math.Min(y1, y2), this._maxVelocity);
if (v < this._minVelocity) { v = this._minVelocity; }
if (this._direction == Direction.up)
{
return v;
}
else if (this._direction == Direction.down)
{
return v * -1f;
}
else // none
{
return 0f;
}
}
}
}
internal enum Direction
{
up,
down,
none
}
}