-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathSuffixed.cs
More file actions
589 lines (520 loc) · 21 KB
/
Copy pathSuffixed.cs
File metadata and controls
589 lines (520 loc) · 21 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
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
588
589
using System;
using System.Collections.Generic;
using System.Linq;
using kOS.Execution;
using kOS.Safe.Encapsulation;
using kOS.Safe.Exceptions;
using kOS.Safe.Function;
using kOS.Suffixed;
using kOS.Utilities;
using FinePrint;
using kOS.Safe;
namespace kOS.Function
{
[Function("node")]
public class FunctionNode : FunctionBase
{
public override void Execute(SharedObjects shared)
{
double prograde = GetDouble(PopValueAssert(shared));
double normal = GetDouble(PopValueAssert(shared));
double radial = GetDouble(PopValueAssert(shared));
double time = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = new Node(time, radial, normal, prograde, shared);
ReturnValue = result;
}
}
[Function("v")]
public class FunctionVector : FunctionBase
{
public override void Execute(SharedObjects shared)
{
double z = GetDouble(PopValueAssert(shared));
double y = GetDouble(PopValueAssert(shared));
double x = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = new Vector(x, y, z);
ReturnValue = result;
}
}
[Function("r")]
public class FunctionRotation : FunctionBase
{
public override void Execute(SharedObjects shared)
{
double roll = GetDouble(PopValueAssert(shared));
double yaw = GetDouble(PopValueAssert(shared));
double pitch = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = new Direction(new Vector3d(pitch, yaw, roll), true);
ReturnValue = result;
}
}
[Function("q")]
public class FunctionQuaternion : FunctionBase
{
public override void Execute(SharedObjects shared)
{
double angle = GetDouble(PopValueAssert(shared));
double roll = GetDouble(PopValueAssert(shared));
double yaw = GetDouble(PopValueAssert(shared));
double pitch = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = new Direction(new UnityEngine.Quaternion((float)pitch, (float)yaw, (float)roll, (float)angle));
ReturnValue = result;
}
}
[Function("rotatefromto")]
public class FunctionRotateFromTo : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Vector toVector = GetVector(PopValueAssert(shared));
Vector fromVector = GetVector(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = Direction.FromVectorToVector(fromVector, toVector);
ReturnValue = result;
}
}
[Function("lookdirup")]
public class FunctionLookDirUp : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Vector topVector = GetVector(PopValueAssert(shared));
Vector lookVector = GetVector(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = Direction.LookRotation(lookVector, topVector);
ReturnValue = result;
}
}
[Function("angleaxis")]
public class FunctionAngleAxis : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Vector axisVector = GetVector(PopValueAssert(shared));
double degrees = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = Direction.AngleAxis(degrees, axisVector);
ReturnValue = result;
}
}
[Function("latlng")]
public class FunctionLatLng : FunctionBase
{
public override void Execute(SharedObjects shared)
{
double longitude = GetDouble(PopValueAssert(shared));
double latitude = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = new GeoCoordinates(shared, latitude, longitude);
ReturnValue = result;
}
}
[Function("vessel")]
public class FunctionVessel : FunctionBase
{
public override void Execute(SharedObjects shared)
{
string vesselName = PopValueAssert(shared).ToString();
AssertArgBottomAndConsume(shared);
var result = new VesselTarget(VesselUtils.GetVesselByName(vesselName, shared.Vessel), shared);
ReturnValue = result;
}
}
[Function("body")]
public class FunctionBody : FunctionBase
{
public override void Execute(SharedObjects shared)
{
string bodyName = PopValueAssert(shared).ToString();
AssertArgBottomAndConsume(shared);
var result = new BodyTarget(bodyName, shared);
ReturnValue = result;
}
}
[Function("bodyatmosphere")]
public class FunctionBodyAtmosphere : FunctionBase
{
public override void Execute(SharedObjects shared)
{
string bodyName = PopValueAssert(shared).ToString();
AssertArgBottomAndConsume(shared);
var result = new BodyAtmosphere(VesselUtils.GetBodyByName(bodyName));
ReturnValue = result;
}
}
[Function("orbit")]
public class FunctionOrbit : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var when = GetTimeSpan(PopValueAssert(shared));
var body = (BodyTarget) PopValueAssert(shared);
if (body == null)
throw new KOSInvalidArgumentException("orbit", "body", "no body found");
Vector vel = GetVector(PopValueAssert(shared));
Vector pos = GetVector(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
var result = new OrbitInfo(pos, vel, body, when.ToUnixStyleTime(), shared);
ReturnValue = result;
}
}
[Function("heading")]
public class FunctionHeading : FunctionBase
{
public override void Execute(SharedObjects shared)
{
double pitchAboveHorizon = GetDouble(PopValueAssert(shared));
double degreesFromNorth = GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
Vessel currentVessel = shared.Vessel;
var q = UnityEngine.Quaternion.LookRotation(VesselUtils.GetNorthVector(currentVessel), currentVessel.upAxis);
q *= UnityEngine.Quaternion.Euler(new UnityEngine.Vector3((float)-pitchAboveHorizon, (float)degreesFromNorth, 0));
var result = new Direction(q);
ReturnValue = result;
}
}
[Function("list")]
public class FunctionList : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Structure[] argArray = new Structure[CountRemainingArgs(shared)];
for (int i = argArray.Length - 1 ; i >= 0 ; --i)
argArray[i] = PopStructureAssertEncapsulated(shared); // fill array in reverse order because .. stack args.
AssertArgBottomAndConsume(shared);
var listValue = new ListValue(argArray.ToList());
ReturnValue = listValue;
}
}
[Function("queue")]
public class FunctionQueue : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Structure[] argArray = new Structure[CountRemainingArgs(shared)];
for (int i = argArray.Length - 1 ; i >= 0 ; --i)
argArray[i] = PopStructureAssertEncapsulated(shared); // fill array in reverse order because .. stack args.
AssertArgBottomAndConsume(shared);
var queueValue = new QueueValue(argArray.ToList());
ReturnValue = queueValue;
}
}
[Function("stack")]
public class FunctionStack : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Structure[] argArray = new Structure[CountRemainingArgs(shared)];
for (int i = argArray.Length - 1 ; i >= 0 ; --i)
argArray[i] = PopStructureAssertEncapsulated(shared); // fill array in reverse order because .. stack args.
AssertArgBottomAndConsume(shared);
var stackValue = new StackValue(argArray.ToList());
ReturnValue = stackValue;
}
}
[Function("range")]
public class FunctionRange : FunctionBase
{
public override void Execute(SharedObjects shared)
{
// Default values for parameters
int from = RangeValue.DEFAULT_START;
int to = RangeValue.DEFAULT_STOP;
int step = RangeValue.DEFAULT_STEP;
int argCount = CountRemainingArgs(shared);
// assign parameter values from the stack, pop them in reverse order
switch (argCount)
{
case 1:
to = GetInt(PopStructureAssertEncapsulated(shared));
break;
case 2:
to = GetInt(PopStructureAssertEncapsulated(shared));
from = GetInt(PopStructureAssertEncapsulated(shared));
break;
case 3:
step = GetInt(PopStructureAssertEncapsulated(shared));
to = GetInt(PopStructureAssertEncapsulated(shared));
from = GetInt(PopStructureAssertEncapsulated(shared));
break;
default:
throw new KOSArgumentMismatchException(new int[] { 1, 2, 3 }, argCount, "Thrown from function RANGE()");
}
AssertArgBottomAndConsume(shared);
ReturnValue = new RangeValue(from, to, step);
}
}
[Function("lex", "lexicon")]
public class FunctionLexicon : FunctionBase
{
public override void Execute(SharedObjects shared)
{
Structure[] argArray = new Structure[CountRemainingArgs(shared)];
for (int i = argArray.Length - 1 ; i >= 0 ; --i)
argArray[i] = PopStructureAssertEncapsulated(shared); // fill array in reverse order because .. stack args.
AssertArgBottomAndConsume(shared);
var lexicon = new Lexicon(argArray.ToList());
ReturnValue = lexicon;
}
}
[Function("hsv")]
public class FunctionHsv : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var v = (float) GetDouble(PopValueAssert(shared));
var s = (float) GetDouble(PopValueAssert(shared));
var h = (float) GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = new HsvColor(h,s,v);
}
}
[Function("hsva")]
public class FunctionHsva : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var a = (float) GetDouble(PopValueAssert(shared));
var v = (float) GetDouble(PopValueAssert(shared));
var s = (float) GetDouble(PopValueAssert(shared));
var h = (float) GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = new HsvColor(h,s,v,a);
}
}
[Function("rgb")]
public class FunctionRgb : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var b = (float) GetDouble(PopValueAssert(shared));
var g = (float) GetDouble(PopValueAssert(shared));
var r = (float) GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = new RgbaColor(r,g,b);
}
}
[Function("rgba")]
public class FunctionRgba : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var a = (float) GetDouble(PopValueAssert(shared));
var b = (float) GetDouble(PopValueAssert(shared));
var g = (float) GetDouble(PopValueAssert(shared));
var r = (float) GetDouble(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = new RgbaColor(r,g,b,a);
}
}
// There are two ways to initialize a vecdraw, with
// vecdraw()
// or with
// vecdrawargs(,vector,vector,rgba,double,bool)
//
// Note: vecdraw now counts the args and changes its behavior accordingly.
// For backward compatibility, vecdrawargs has been aliased to vecdraw.
//
[Function("vecdraw", "vecdrawargs")]
public class FunctionVecDrawNull : FunctionBase
{
public override void Execute(SharedObjects shared)
{
int argc = CountRemainingArgs(shared);
// Handle the var args that might be passed in, or give defaults if fewer args:
double width = (argc >= 7) ? GetDouble(PopValueAssert(shared)) : 0.2;
bool show = (argc >= 6) ? Convert.ToBoolean(PopValueAssert(shared)) : false;
double scale = (argc >= 5) ? GetDouble(PopValueAssert(shared)) : 1.0;
string str = (argc >= 4) ? PopValueAssert(shared).ToString() : "";
RgbaColor rgba = (argc >= 3) ? GetRgba(PopValueAssert(shared)) : new RgbaColor(1.0f, 1.0f, 1.0f);
Vector vec = (argc >= 2) ? GetVector(PopValueAssert(shared)) : new Vector(1.0, 0.0, 0.0);
Vector start = (argc >= 1) ? GetVector(PopValueAssert(shared)) : new Vector(0.0, 0.0, 0.0);
AssertArgBottomAndConsume(shared);
DoExecuteWork(shared, start, vec, rgba, str, scale, show, width);
}
public void DoExecuteWork(SharedObjects shared, Vector start, Vector vec, RgbaColor rgba, string str, double scale, bool show, double width)
{
var vRend = new VectorRenderer( shared.UpdateHandler, shared )
{
Vector = vec,
Start = start,
Color = rgba,
Scale = scale,
Width = width
};
vRend.SetLabel( str );
vRend.SetShow( show );
ReturnValue = vRend;
}
}
[Function("clearvecdraws")]
public class FunctionHideAllVecdraws : FunctionBase
{
public override void Execute(SharedObjects shared)
{
AssertArgBottomAndConsume(shared);
VectorRenderer.ClearAll(shared.UpdateHandler);
}
}
[Function("positionat")]
public class FunctionPositionAt : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var when = GetTimeSpan(PopValueAssert(shared));
var what = GetOrbitable(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = what.GetPositionAtUT(when);
}
}
[Function("velocityat")]
public class FunctionVelocityAt : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var when = GetTimeSpan(PopValueAssert(shared));
var what = GetOrbitable(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = what.GetVelocitiesAtUT(when);
}
}
[Function("highlight")]
public class FunctionHightlight : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var color = GetRgba(PopValueAssert(shared));
var obj = PopValueAssert(shared);
AssertArgBottomAndConsume(shared);
ReturnValue = new HighlightStructure(shared.UpdateHandler, obj, color);
}
}
[Function("orbitat")]
public class FunctionOrbitAt : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var when = GetTimeSpan(PopValueAssert(shared));
var what = GetOrbitable(PopValueAssert(shared));
AssertArgBottomAndConsume(shared);
ReturnValue = new OrbitInfo( what.GetOrbitAtUT(when.ToUnixStyleTime()), shared );
}
}
[Function("career")]
public class FunctionCareer : FunctionBase
{
public override void Execute(SharedObjects shared)
{
AssertArgBottomAndConsume(shared); // no args
ReturnValue = new Career();
}
}
[Function("constant")]
public class FunctionConstant : FunctionBase
{
public override void Execute(SharedObjects shared)
{
AssertArgBottomAndConsume(shared); // no args
ReturnValue = new ConstantValue();
}
}
[Function("allwaypoints")]
public class FunctionAllWaypoints : FunctionBase
{
public override void Execute(SharedObjects shared)
{
AssertArgBottomAndConsume(shared); // no args
// ReSharper disable SuggestUseVarKeywordEvident
ListValue<WaypointValue> returnList = new ListValue<WaypointValue>();
// ReSharper enable SuggestUseVarKeywordEvident
WaypointManager wpm = WaypointManager.Instance();
if (wpm == null)
{
ReturnValue = returnList; // When no waypoints exist, there isn't even a waypoint manager at all.
return;
}
List<Waypoint> points = wpm.Waypoints;
// If the code below gets used in more places it may be worth moving into a factory method
// akin to how PartValueFactory makes a ListValue<PartValue> from a List<Part>.
// But for now, this is the only place it's done:
foreach (Waypoint point in points)
returnList.Add(new WaypointValue(point, shared));
ReturnValue = returnList;
}
}
[Function("waypoint")]
public class FunctionWaypoint : FunctionBase
{
public override void Execute(SharedObjects shared)
{
string pointName = PopValueAssert(shared).ToString();
AssertArgBottomAndConsume(shared);
WaypointManager wpm = WaypointManager.Instance();
// If no contracts have been generated with waypoints in them,
// then sometimes the stock game's waypoint manager doesn't even
// exist yet either. (The base game seems not to instance one until the
// first time a contract with a waypoint is created).
if (wpm == null)
throw new KOSInvalidArgumentException("waypoint", "\""+pointName+"\"", "no waypoints exist");
string baseName;
int index;
bool hasGreek = WaypointValue.GreekToInteger(pointName, out index, out baseName);
if (hasGreek)
pointName = baseName;
Waypoint point = wpm.Waypoints.FirstOrDefault(
p => string.Equals(p.name, pointName,StringComparison.CurrentCultureIgnoreCase) && (!hasGreek || p.index == index));
// We can't communicate the concept of a lookup fail to the script in a way it can catch (can't do
// nulls), so bomb out here:
if (point ==null)
throw new KOSInvalidArgumentException("waypoint", "\""+pointName+"\"", "no such waypoint");
ReturnValue = new WaypointValue(point, shared);
}
}
[Function("transferall")]
public class FunctionTransferAll : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var transferTo = PopValueAssert(shared);
var transferFrom = PopValueAssert(shared);
var resourceName = PopValueAssert(shared).ToString();
AssertArgBottomAndConsume(shared);
var resourceInfo = TransferManager.ParseResource(resourceName);
if (resourceInfo == null)
{
throw new KOSInvalidArgumentException("TransferAll", "Resource",
resourceName + " was not found in the resource list");
}
object toPush = shared.TransferManager.CreateTransfer(resourceInfo, transferTo, transferFrom);
ReturnValue = toPush;
}
}
[Function("transfer")]
public class FunctionTransfer : FunctionBase
{
public override void Execute(SharedObjects shared)
{
var amount = PopValueAssert(shared);
var transferTo = PopValueAssert(shared);
var transferFrom = PopValueAssert(shared);
var resourceName = PopValueAssert(shared).ToString();
AssertArgBottomAndConsume(shared);
var resourceInfo = TransferManager.ParseResource(resourceName);
if (resourceInfo == null)
{
throw new KOSInvalidArgumentException("TransferAll", "Resource",
resourceName + " was not found in the resource list");
}
double parsedAmount;
if (Double.TryParse(amount.ToString(), out parsedAmount))
{
object toPush = shared.TransferManager.CreateTransfer(resourceInfo, transferTo, transferFrom, parsedAmount);
ReturnValue = toPush;
}
}
}
}