Oolite
Loading...
Searching...
No Matches
OOJSSystemInfo.m
Go to the documentation of this file.
1/*
2
3OOJSSystemInfo.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOJSSystemInfo.h"
26#import "OOConstToString.h"
27#import "OOIsNumberLiteral.h"
28#import "OOJSScript.h"
29#import "OOJSVector.h"
33#import "Universe.h"
34
35static JSObject* sSystemInfoPrototype;
36static JSObject* sCachedSystemInfo;
39
40static JSBool SystemInfoDeleteProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
41static JSBool SystemInfoGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
42static JSBool SystemInfoSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
43static void SystemInfoFinalize(JSContext* context, JSObject* this);
44static JSBool SystemInfoEnumerate(JSContext* context, JSObject* this, JSIterateOp enumOp, jsval* state, jsid* idp);
45
46static JSBool SystemInfoDistanceToSystem(JSContext* context, uintN argc, jsval* vp);
47static JSBool SystemInfoRouteToSystem(JSContext* context, uintN argc, jsval* vp);
48static JSBool SystemInfoSamplePrice(JSContext* context, uintN argc, jsval* vp);
49static JSBool SystemInfoSetPropertyMethod(JSContext* context, uintN argc, jsval* vp);
50
51static JSBool SystemInfoStaticSetInterstellarProperty(JSContext* context, uintN argc, jsval* vp);
52static JSBool SystemInfoStaticFilteredSystems(JSContext* context, uintN argc, jsval* vp);
53
54static JSClass sSystemInfoClass = {
55 "SystemInfo",
56 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_ENUMERATE,
57
58 JS_PropertyStub,
62 (JSEnumerateOp)SystemInfoEnumerate,
63 JS_ResolveStub,
64 JS_ConvertStub,
66 JSCLASS_NO_OPTIONAL_MEMBERS
67};
68
69enum {
70 // Property IDs
71 kSystemInfo_coordinates, // system coordinates (in LY), Vector3D (with z = 0), read-only
72 kSystemInfo_internalCoordinates, // system coordinates (unscaled), Vector3D (with z = 0), read-only
73 kSystemInfo_galaxyID, // galaxy number, integer, read-only
74 kSystemInfo_systemID // system number, integer, read-only
75};
76
77static JSPropertySpec sSystemInfoProperties[] = {
78 // JS name ID flags
83 { 0 }
84};
85
86static JSFunctionSpec sSystemInfoMethods[] = {
87 // JS name Function min args
88 { "toString", OOJSObjectWrapperToString, 0 },
89 { "distanceToSystem", SystemInfoDistanceToSystem, 1 },
90 { "routeToSystem", SystemInfoRouteToSystem, 1 },
91 { "samplePrice", SystemInfoSamplePrice, 1 },
92 { "setProperty", SystemInfoSetPropertyMethod, 3 },
93 { 0 }
94};
95
96static JSFunctionSpec sSystemInfoStaticMethods[] = {
97 // JS name Function min args
98 { "filteredSystems", SystemInfoStaticFilteredSystems, 2 },
99 { "setInterstellarProperty", SystemInfoStaticSetInterstellarProperty, 4 },
100 { 0 }
101};
102
103// Helper class wrapped by JS SystemInfo objects
104@interface OOSystemInfo : NSObject {
105@private
108 NSString* _planetKey;
109}
110
111- (id)initWithGalaxy:(OOGalaxyID)galaxy system:(OOSystemID)system;
112
113- (id)valueForKey:(NSString*)key;
114- (void)setValue:(id)value forKey:(NSString*)key;
115
116- (NSArray*)allKeys;
117
118- (OOGalaxyID)galaxy;
119- (OOSystemID)system;
120//- (Random_Seed) systemSeed;
121
122@end
123
125
126@implementation OOSystemInfo
127
128- (id)init
129{
130 [self release];
131 return nil;
132}
133
134- (id)initWithGalaxy:(OOGalaxyID)galaxy system:(OOSystemID)system
135{
137 [self release];
138 return nil;
139 }
140
141 if ((self = [super init])) {
142 _galaxy = galaxy;
143 _system = system;
144 _planetKey = [[NSString stringWithFormat:@"%u %i", galaxy, system] retain];
145 }
146 return self;
147}
148
149- (void)dealloc
150{
151 [_planetKey release];
152
153 [super dealloc];
154}
155
157{
158 return [NSString stringWithFormat:@"galaxy %u, system %i", _galaxy, _system];
159}
160
162{
163 return _planetKey;
164}
165
166- (NSString*)oo_jsClassName
167{
168 return @"SystemInfo";
169}
170
171- (BOOL)isEqual:(id)other
172{
173 return other == self || ([other isKindOfClass:[OOSystemInfo class]] && [other galaxy] == _galaxy && [other system] == _system);
174}
175
176- (NSUInteger)hash
177{
178 NSUInteger hash = _galaxy;
179 hash <<= 16;
180 hash |= (uint16_t)_system;
181 return hash;
182}
183
184- (id)valueForKey:(NSString*)key
185{
186 if ([UNIVERSE inInterstellarSpace] && _system == -1) {
187 return [[UNIVERSE currentSystemData] objectForKey:key];
188 }
189 return [UNIVERSE systemDataForGalaxy:_galaxy planet:_system key:key];
190}
191
192- (void)setValue:(id)value forKey:(NSString*)key
193{
194 NSString* manifest = [[OOJSScript currentlyRunningScript] propertyNamed:kLocalManifestProperty];
195
196 [UNIVERSE setSystemDataForGalaxy:_galaxy planet:_system key:key value:value fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
197}
198
199- (NSArray*)allKeys
200{
201 if ([UNIVERSE inInterstellarSpace] && _system == -1) {
202 return [[UNIVERSE currentSystemData] allKeys];
203 }
204 return [UNIVERSE systemDataKeysForGalaxy:_galaxy planet:_system];
205}
206
208{
209 return _galaxy;
210}
211
213{
214 return _system;
215}
216
217/*- (Random_Seed) systemSeed
218{
219 NSAssert([PLAYER currentGalaxyID] == _galaxy, @"Attempt to use -[OOSystemInfo systemSeed] from a different galaxy.");
220 return [UNIVERSE systemSeedForSystemNumber:_system];
221 }*/
222
223- (NSPoint)coordinates
224{
225 if ([UNIVERSE inInterstellarSpace] && _system == -1) {
226 return [PLAYER galaxy_coordinates];
227 }
228 return [UNIVERSE coordinatesForSystem:_system];
229}
230
231- (jsval)oo_jsValueInContext:(JSContext*)context
232{
233 JSObject* jsSelf = NULL;
234 jsval result = JSVAL_NULL;
235
236 jsSelf = JS_NewObject(context, &sSystemInfoClass, sSystemInfoPrototype, NULL);
237 if (jsSelf != NULL) {
238 if (!JS_SetPrivate(context, jsSelf, [self retain]))
239 jsSelf = NULL;
240 }
241 if (jsSelf != NULL)
242 result = OBJECT_TO_JSVAL(jsSelf);
243
244 return result;
245}
246
247@end
248
254
256{
258
259 // Use cached object if possible.
261 return OBJECT_TO_JSVAL(sCachedSystemInfo);
262 }
263
264 // If not, create a new one.
265 OOSystemInfo* info = nil;
266 jsval result;
268 info = [[[OOSystemInfo alloc] initWithGalaxy:galaxy system:system] autorelease];
270
271 if (EXPECT_NOT(info == nil)) {
272 OOJSReportWarning(context, @"Could not create system info object for galaxy %u, system %i.", galaxy, system);
273 }
274
275 result = OOJSValueFromNativeObject(context, info);
276
277 // Cache is not a root; we clear it in finalize if necessary.
278 sCachedSystemInfo = JSVAL_TO_OBJECT(result);
281
282 return result;
283
285}
286
287static void SystemInfoFinalize(JSContext* context, JSObject* this)
288{
290
291 [(id)JS_GetPrivate(context, this) release];
292 JS_SetPrivate(context, this, nil);
293
294 // Clear now-stale cache entry if appropriate.
295 if (sCachedSystemInfo == this)
296 sCachedSystemInfo = NULL;
297
299}
300
301static JSBool SystemInfoEnumerate(JSContext* context, JSObject* this, JSIterateOp enumOp, jsval* state, jsid* idp)
302{
303 OOJS_NATIVE_ENTER(context)
304
305 NSEnumerator* enumerator = nil;
306
307 switch (enumOp) {
308 case JSENUMERATE_INIT:
309 case JSENUMERATE_INIT_ALL: // For ES5 Object.getOwnPropertyNames(). Since we have no non-enumerable properties, this is the same as _INIT.
310 {
311 OOSystemInfo* info = JS_GetPrivate(context, this);
312 NSArray* keys = [info allKeys];
313 enumerator = [[keys objectEnumerator] retain];
314 *state = PRIVATE_TO_JSVAL(enumerator);
315
316 NSUInteger count = [keys count];
317 assert(count <= INT32_MAX);
318 if (idp != NULL)
319 *idp = INT_TO_JSID((int32_t)count);
320 return YES;
321 }
322
323 case JSENUMERATE_NEXT: {
324 enumerator = JSVAL_TO_PRIVATE(*state);
325 NSString* next = [enumerator nextObject];
326 if (next != nil) {
327 jsval val = [next oo_jsValueInContext:context];
328 return JS_ValueToId(context, val, idp);
329 }
330 // else:
331 *state = JSVAL_NULL;
332 // Fall through.
333 }
334
335 case JSENUMERATE_DESTROY: {
336 if (enumerator == nil && JSVAL_IS_DOUBLE(*state)) {
337 enumerator = JSVAL_TO_PRIVATE(*state);
338 }
339 [enumerator release];
340
341 if (idp != NULL)
342 *idp = JSID_VOID;
343 return YES;
344 }
345 }
346
348}
349
350static JSBool SystemInfoDeleteProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
351{
352 OOJS_PROFILE_ENTER // Any exception will be converted in SystemInfoSetProperty()
353
354 jsval v = JSVAL_VOID;
355 return SystemInfoSetProperty(context, this, propID, NO, &v);
356
358}
359
360static JSBool SystemInfoGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
361{
362 OOJS_NATIVE_ENTER(context)
363
364 if (this == sSystemInfoPrototype) {
365 // Let SpiderMonkey handle access to the prototype object (where info will be nil).
366 return YES;
367 }
368
369 OOSystemInfo* info = OOJSNativeObjectOfClassFromJSObject(context, this, [OOSystemInfo class]);
370 // What if we're trying to access a saved witchspace systemInfo object?
371 BOOL savedInterstellarInfo = ![UNIVERSE inInterstellarSpace] && [info system] == -1;
372 BOOL sameGalaxy = [PLAYER currentGalaxyID] == [info galaxy];
373
374 if (JSID_IS_INT(propID)) {
375 switch (JSID_TO_INT(propID)) {
377 if (sameGalaxy && !savedInterstellarInfo) {
378 return VectorToJSValue(context, OOGalacticCoordinatesFromInternal([info coordinates]), value);
379 } else {
380 OOJSReportError(context, @"Cannot read systemInfo values for %@.", savedInterstellarInfo ? @"invalid interstellar space reference" : @"other galaxies");
381 return NO;
382 }
383 break;
384
386 if (sameGalaxy && !savedInterstellarInfo) {
387 return NSPointToVectorJSValue(context, [info coordinates], value);
388 } else {
389 OOJSReportError(context, @"Cannot read systemInfo values for %@.", savedInterstellarInfo ? @"invalid interstellar space reference" : @"other galaxies");
390 return NO;
391 }
392 break;
393
395 *value = INT_TO_JSVAL([info galaxy]);
396 return YES;
397
399 *value = INT_TO_JSVAL([info system]);
400 return YES;
401
402 default:
404 return NO;
405 }
406 } else if (JSID_IS_STRING(propID)) {
407 NSString* key = OOStringFromJSString(context, JSID_TO_STRING(propID));
408
409 OOSystemDescriptionManager* systemManager = [UNIVERSE systemManager];
410 id propValue = nil;
411 // interstellar space needs more work at this stage
412 if ([info system] != -1) {
413 propValue = [systemManager getProperty:key forSystem:[info system] inGalaxy:[info galaxy]];
414 } else {
415 propValue = [info valueForKey:key];
416 }
417
418 if (propValue != nil) {
419 if ([propValue isKindOfClass:[NSNumber class]] || OOIsNumberLiteral([propValue description], YES)) {
420 BOOL OK = JS_NewNumberValue(context, [propValue doubleValue], value);
421 if (!OK) {
422 *value = JSVAL_VOID;
423 return NO;
424 }
425 } else {
426 *value = [propValue oo_jsValueInContext:context];
427 }
428 }
429 }
430 return YES;
431
433}
434
435static JSBool SystemInfoSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
436{
437 if (EXPECT_NOT(this == sSystemInfoPrototype)) {
438 // Let SpiderMonkey handle access to the prototype object (where info will be nil).
439 return YES;
440 }
441
442 OOJS_NATIVE_ENTER(context);
443
444 if (JSID_IS_STRING(propID)) {
445 NSString* key = OOStringFromJSString(context, JSID_TO_STRING(propID));
446 OOSystemInfo* info = OOJSNativeObjectOfClassFromJSObject(context, this, [OOSystemInfo class]);
447
448 [info setValue:OOStringFromJSValue(context, *value) forKey:key];
449 }
450 return YES;
451
453}
454
455// distanceToSystem(sys : SystemInfo) : Number
456static JSBool SystemInfoDistanceToSystem(JSContext* context, uintN argc, jsval* vp)
457{
458 OOJS_NATIVE_ENTER(context)
459
460 OOSystemInfo* thisInfo = nil;
461 JSObject* otherObj = NULL;
462 OOSystemInfo* otherInfo = nil;
463
464 if (!JSSystemInfoGetSystemInfo(context, OOJS_THIS, &thisInfo))
465 return NO;
466 if (argc < 1 || !JS_ValueToObject(context, OOJS_ARGV[0], &otherObj) || !JSSystemInfoGetSystemInfo(context, otherObj, &otherInfo)) {
467 OOJSReportBadArguments(context, @"SystemInfo", @"distanceToSystem", MIN(argc, 1U), OOJS_ARGV, nil, @"system info");
468 return NO;
469 }
470
471 BOOL sameGalaxy = ([thisInfo galaxy] == [otherInfo galaxy]);
472 if (!sameGalaxy) {
473 OOJSReportErrorForCaller(context, @"SystemInfo", @"distanceToSystem", @"Cannot calculate distance for systems in other galaxies.");
474 return NO;
475 }
476
477 NSPoint thisCoord = [thisInfo coordinates];
478 NSPoint otherCoord = [otherInfo coordinates];
479
480 OOJS_RETURN_DOUBLE(distanceBetweenPlanetPositions(thisCoord.x, thisCoord.y, otherCoord.x, otherCoord.y));
481
483}
484
485// routeToSystem(sys : SystemInfo [, optimizedBy : String]) : Object
486static JSBool SystemInfoRouteToSystem(JSContext* context, uintN argc, jsval* vp)
487{
488 OOJS_NATIVE_ENTER(context)
489
490 OOSystemInfo* thisInfo = nil;
491 JSObject* otherObj = NULL;
492 OOSystemInfo* otherInfo = nil;
493 NSDictionary* result = nil;
495
496 if (!JSSystemInfoGetSystemInfo(context, OOJS_THIS, &thisInfo))
497 return NO;
498 if (argc < 1 || !JS_ValueToObject(context, OOJS_ARGV[0], &otherObj) || !JSSystemInfoGetSystemInfo(context, otherObj, &otherInfo)) {
499 OOJSReportBadArguments(context, @"SystemInfo", @"routeToSystem", MIN(argc, 1U), OOJS_ARGV, nil, @"system info");
500 return NO;
501 }
502
503 BOOL sameGalaxy = ([thisInfo galaxy] == [otherInfo galaxy]);
504 if (!sameGalaxy) {
505 OOJSReportErrorForCaller(context, @"SystemInfo", @"routeToSystem", @"Cannot calculate route for destinations in other galaxies.");
506 return NO;
507 }
508
509 if (argc >= 2) {
510 routeType = StringToRouteType(OOStringFromJSValue(context, OOJS_ARGV[1]));
511 }
512
514 result = [UNIVERSE routeFromSystem:[thisInfo system] toSystem:[otherInfo system] optimizedBy:routeType];
516
517 OOJS_RETURN_OBJECT(result);
518
520}
521
522// samplePrice(commodity)
523static JSBool SystemInfoSamplePrice(JSContext* context, uintN argc, jsval* vp)
524{
525 OOJS_NATIVE_ENTER(context)
526
527 OOSystemInfo* thisInfo = nil;
528
529 if (!JSSystemInfoGetSystemInfo(context, OOJS_THIS, &thisInfo))
530 return NO;
531 OOCommodityType commodity = OOStringFromJSValue(context, OOJS_ARGV[0]);
532 if (EXPECT_NOT(![[UNIVERSE commodities] goodDefined:commodity])) {
533 OOJSReportBadArguments(context, @"SystemInfo", @"samplePrice", MIN(argc, 1U), OOJS_ARGV, NULL, @"Unrecognised commodity type");
534 return NO;
535 }
536
537 BOOL sameGalaxy = ([thisInfo galaxy] == [PLAYER galaxyNumber]);
538 if (!sameGalaxy) {
539 OOJSReportErrorForCaller(context, @"SystemInfo", @"samplePrice", @"Cannot calculate sample price for destinations in other galaxies.");
540 return NO;
541 }
542
543 OOCreditsQuantity price = [[UNIVERSE commodities] samplePriceForCommodity:commodity inEconomy:[[thisInfo valueForKey:@"economy"] intValue] withScript:[thisInfo valueForKey:@"commodity_script"] inSystem:[thisInfo system]];
544
545 return JS_NewNumberValue(context, price, &OOJS_RVAL);
546
548}
549
550static JSBool SystemInfoSetPropertyMethod(JSContext* context, uintN argc, jsval* vp)
551{
552 OOJS_NATIVE_ENTER(context)
553
554 OOSystemInfo* thisInfo = nil;
555
556 if (!JSSystemInfoGetSystemInfo(context, OOJS_THIS, &thisInfo))
557 return NO;
558
559 NSString* property = nil;
560 id value = nil;
561 NSString* manifest = nil;
562
563 int32 iValue;
564
565 if (argc < 3) {
566 OOJSReportBadArguments(context, @"SystemInfo", @"setProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(layer, property, value [,manifest])");
567 return NO;
568 }
569 if (!JS_ValueToInt32(context, OOJS_ARGV[0], &iValue)) {
570 OOJSReportBadArguments(context, @"SystemInfo", @"setProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(layer, property, value [,manifest])");
571 return NO;
572 }
573 if (iValue < 0 || iValue >= OO_SYSTEM_LAYERS) {
574 OOJSReportBadArguments(context, @"SystemInfo", @"setProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"layer must be 0, 1, 2 or 3");
575 return NO;
576 }
577 OOSystemLayer layer = (OOSystemLayer)iValue;
578
579 property = OOStringFromJSValue(context, OOJS_ARGV[1]);
580 if (!JSVAL_IS_NULL(OOJS_ARGV[2])) {
581 value = OOJSNativeObjectFromJSValue(context, OOJS_ARGV[2]);
582 }
583 if (argc >= 4) {
584 manifest = OOStringFromJSValue(context, OOJS_ARGV[3]);
585 } else {
586 manifest = [[OOJSScript currentlyRunningScript] propertyNamed:kLocalManifestProperty];
587 }
588
589 [UNIVERSE setSystemDataForGalaxy:[thisInfo galaxy] planet:[thisInfo system] key:property value:value fromManifest:manifest forLayer:layer];
590
592
594}
595
596// filteredSystems(this : Object, predicate : Function) : Array
597static JSBool SystemInfoStaticFilteredSystems(JSContext* context, uintN argc, jsval* vp)
598{
599 OOJS_NATIVE_ENTER(context)
600
601 JSObject* jsThis = NULL;
602
603 // Get this and predicate arguments
604 if (argc < 2 || !OOJSValueIsFunction(context, OOJS_ARGV[1]) || !JS_ValueToObject(context, OOJS_ARGV[0], &jsThis)) {
605 OOJSReportBadArguments(context, @"SystemInfo", @"filteredSystems", argc, OOJS_ARGV, nil, @"this and predicate function");
606 return NO;
607 }
608 jsval predicate = OOJS_ARGV[1];
609
610 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
611 NSMutableArray* result = [NSMutableArray arrayWithCapacity:256];
612
613 // Not OOJS_BEGIN_FULL_NATIVE() - we use JSAPI while paused.
615
616 // Iterate over systems.
617 BOOL OK = result != nil;
618 OOGalaxyID galaxy = [PLAYER currentGalaxyID];
620 for (system = 0; system <= kOOMaximumSystemID; system++) {
621 // NOTE: this deliberately bypasses the cache, since iteration is inherently unfriendly to a single-item cache.
622 OOSystemInfo* info = [[[OOSystemInfo alloc] initWithGalaxy:galaxy system:system] autorelease];
623 jsval args[1] = { OOJSValueFromNativeObject(context, info) };
624
625 jsval rval = JSVAL_VOID;
627 OK = JS_CallFunctionValue(context, jsThis, predicate, 1, args, &rval);
629
630 if (OK) {
631 if (JS_IsExceptionPending(context)) {
632 JS_ReportPendingException(context);
633 OK = NO;
634 }
635 }
636
637 if (OK) {
638 JSBool boolVal;
639 if (JS_ValueToBoolean(context, rval, &boolVal) && boolVal) {
640 [result addObject:info];
641 }
642 }
643
644 if (!OK)
645 break;
646 }
647
648 if (OK) {
649 OOJS_SET_RVAL([result oo_jsValueInContext:context]);
650 } else {
651 OOJS_SET_RVAL(JSVAL_VOID);
652 }
653
654 [pool release];
655
657 return OK;
658
660}
661
662static JSBool SystemInfoStaticSetInterstellarProperty(JSContext* context, uintN argc, jsval* vp)
663{
664 OOJS_NATIVE_ENTER(context)
665
666 NSString* property = nil;
667 id value = nil;
668 NSString* manifest = nil;
669
670 int32 iValue;
671 OOGalaxyID g;
672 OOSystemID s1, s2;
673
674 if (argc < 6) {
675 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(galaxy, fromsystem, tosystem, layer, property, value [,manifest])");
676 return NO;
677 }
678 if (!JS_ValueToInt32(context, OOJS_ARGV[0], &iValue)) {
679 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(galaxy, fromsystem, tosystem, layer, property, value [,manifest])");
680 return NO;
681 }
682 if (iValue < 0 || iValue > kOOMaximumGalaxyID) {
683 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"galaxy out of range");
684 return NO;
685 } else {
686 g = (OOGalaxyID)iValue;
687 }
688
689 if (!JS_ValueToInt32(context, OOJS_ARGV[1], &iValue)) {
690 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(galaxy, fromsystem, tosystem, layer, property, value [,manifest])");
691 return NO;
692 }
693 if (iValue < 0 || iValue > kOOMaximumSystemID) {
694 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"fromsystem out of range");
695 return NO;
696 } else {
697 s1 = (OOSystemID)iValue;
698 }
699
700 if (!JS_ValueToInt32(context, OOJS_ARGV[2], &iValue)) {
701 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(galaxy, fromsystem, tosystem, layer, property, value [,manifest])");
702 return NO;
703 }
704 if (iValue < 0 || iValue > kOOMaximumSystemID) {
705 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"tosystem out of range");
706 return NO;
707 } else {
708 s2 = (OOSystemID)iValue;
709 }
710
711 if (!JS_ValueToInt32(context, OOJS_ARGV[3], &iValue)) {
712 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"setProperty(galaxy, fromsystem, tosystem, layer, property, value [,manifest])");
713 return NO;
714 }
715 if (iValue < 0 || iValue >= OO_SYSTEM_LAYERS) {
716 OOJSReportBadArguments(context, @"SystemInfo", @"setInterstellarProperty", MIN(argc, 3U), OOJS_ARGV, NULL, @"layer must be 0, 1, 2 or 3");
717 return NO;
718 }
719 OOSystemLayer layer = (OOSystemLayer)iValue;
720
721 property = OOStringFromJSValue(context, OOJS_ARGV[4]);
722 if (!JSVAL_IS_NULL(OOJS_ARGV[5])) {
723 value = OOJSNativeObjectFromJSValue(context, OOJS_ARGV[5]);
724 }
725 if (argc >= 7) {
726 manifest = OOStringFromJSValue(context, OOJS_ARGV[6]);
727 } else {
728 manifest = [[OOJSScript currentlyRunningScript] propertyNamed:kLocalManifestProperty];
729 }
730
731 NSString* key = [NSString stringWithFormat:@"interstellar: %u %u %u", g, s1, s2];
732
733 [[UNIVERSE systemManager] setProperty:property forSystemKey:key andLayer:layer toValue:value fromManifest:manifest];
734
736
738}
OORouteType StringToRouteType(NSString *string)
#define EXPECT_NOT(x)
BOOL OOIsNumberLiteral(NSString *string, BOOL allowSpaces)
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_EXIT_VOID
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
#define OOJS_PROFILE_EXIT_JSVAL
void InitOOJSSystemInfo(JSContext *context, JSObject *global)
jsval GetJSSystemInfoForSystem(JSContext *context, OOGalaxyID galaxy, OOSystemID system)
static JSBool SystemInfoDistanceToSystem(JSContext *context, uintN argc, jsval *vp)
static OOSystemID sCachedSystem
static JSBool SystemInfoStaticFilteredSystems(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemInfoRouteToSystem(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemInfoDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSPropertySpec sSystemInfoProperties[]
static JSBool SystemInfoSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSObject * sCachedSystemInfo
static JSFunctionSpec sSystemInfoStaticMethods[]
static JSObject * sSystemInfoPrototype
static JSBool SystemInfoStaticSetInterstellarProperty(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemInfoSetPropertyMethod(JSContext *context, uintN argc, jsval *vp)
@ kSystemInfo_systemID
@ kSystemInfo_coordinates
@ kSystemInfo_internalCoordinates
@ kSystemInfo_galaxyID
static JSBool SystemInfoGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSClass sSystemInfoClass
static void SystemInfoFinalize(JSContext *context, JSObject *this)
static OOGalaxyID sCachedGalaxy
static JSFunctionSpec sSystemInfoMethods[]
static JSBool SystemInfoEnumerate(JSContext *context, JSObject *this, JSIterateOp enumOp, jsval *state, jsid *idp)
static JSBool SystemInfoSamplePrice(JSContext *context, uintN argc, jsval *vp)
BOOL NSPointToVectorJSValue(JSContext *context, NSPoint point, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:328
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:264
void OOJSPauseTimeLimiter(void)
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
void OOJSReportWarning(JSContext *context, NSString *format,...)
#define OOJS_THIS
#define OOJS_RVAL
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
#define OOJS_SET_RVAL(v)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
#define OOJS_RETURN_DOUBLE(value)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
void OOJSReportErrorForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
void OOJSResumeTimeLimiter(void)
#define OOJS_RETURN_VOID
NSString * OOStringFromJSString(JSContext *context, JSString *string)
id OOJSNativeObjectOfClassFromJSObject(JSContext *context, JSObject *object, Class requiredClass)
#define MIN(A, B)
Definition OOMaths.h:91
unsigned count
return nil
#define OO_SYSTEM_LAYERS
@ kOOMaximumSystemID
Definition OOTypes.h:187
@ kOOMaximumGalaxyID
Definition OOTypes.h:186
@ kOOMinimumSystemID
Definition OOTypes.h:188
NSString * OOCommodityType
Definition OOTypes.h:97
OORouteType
Definition OOTypes.h:31
@ OPTIMIZED_BY_JUMPS
Definition OOTypes.h:33
uint64_t OOCreditsQuantity
Definition OOTypes.h:160
int16_t OOSystemID
Definition OOTypes.h:183
uint8_t OOGalaxyID
Definition OOTypes.h:182
Vector OOGalacticCoordinatesFromInternal(NSPoint internalCoordinates)
static JSClassDef sSystemInfoClass
#define UNIVERSE
Definition Universe.h:827
OOCreditsQuantity samplePriceForCommodity:inEconomy:withScript:inSystem:(OOCommodityType commodity,[inEconomy] OOEconomyID economy,[withScript] NSString *scriptName,[inSystem] OOSystemID system)
id propertyNamed:(NSString *name)
Definition OOJSScript.m:431
OOJSScript * currentlyRunningScript()
Definition OOJSScript.m:101
id getProperty:forSystem:inGalaxy:(NSString *property,[forSystem] OOSystemID s,[inGalaxy] OOGalaxyID g)
NSString * _planetKey
id valueForKey:(NSString *key)
OOSystemID system()
NSPoint coordinates()
OOSystemID _system
OOGalaxyID _galaxy
OOGalaxyID galaxy()
NSArray * allKeys()
NSString * oo_jsClassName()
NSString * descriptionComponents()
NSUInteger hash()
void setValue:forKey:(id value,[forKey] NSString *key)
NSString * shortDescriptionComponents()
OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC