51static BOOL
GetRelativeToAndRange(JSContext *context, NSString *methodName, uintN *ioArgc, jsval **ioArgv,
Entity **outRelativeTo,
double *outRange);
60static JSBool
SystemGetProperty(JSContext *context, JSObject *
this, jsid propID, jsval *value);
61static JSBool
SystemSetProperty(JSContext *context, JSObject *
this, jsid propID, JSBool strict, jsval *value);
63static JSBool
SystemToString(JSContext *context, uintN argc, jsval *vp);
64static JSBool
SystemAddPlanet(JSContext *context, uintN argc, jsval *vp);
65static JSBool
SystemAddMoon(JSContext *context, uintN argc, jsval *vp);
76static JSBool
SystemAddShips(JSContext *context, uintN argc, jsval *vp);
77static JSBool
SystemAddGroup(JSContext *context, uintN argc, jsval *vp);
238 if (!JSID_IS_INT(propID))
return YES;
244 NSDictionary *systemData =
nil;
250 switch (JSID_TO_INT(propID))
253 *value = INT_TO_JSVAL([player currentSystemID]);
261 result = [UNIVERSE station];
266 result = [UNIVERSE planet];
271 result = [UNIVERSE sun];
276 result = [[[UNIVERSE planets] objectEnumeratorFilteredWithSelector:@selector(isVisibleToScripts)] allObjects];
281 result = [UNIVERSE stations];
286 result = [UNIVERSE currentWaypoints];
291 result = [UNIVERSE wormholes];
297 result = [UNIVERSE findShipsMatchingPredicate:JSEntityIsJavaScriptSearchablePredicate parameter:NULL inRange:-1 ofEntity:nil];
304 result = [UNIVERSE findShipsMatchingPredicate:JSEntityIsDemoShipPredicate parameter:NULL inRange:-1 ofEntity:nil];
312 result = [UNIVERSE findVisualEffectsMatchingPredicate:JSEntityIsJavaScriptSearchablePredicate parameter:NULL inRange:-1 ofEntity:nil];
318 return JS_NewNumberValue(context, [
UNIVERSE ambientLightLevel], value);
325 return JS_NewNumberValue(context, [player systemPseudoRandomFloat], value);
328 *value = INT_TO_JSVAL([player systemPseudoRandom100]);
332 *value = INT_TO_JSVAL([player systemPseudoRandom256]);
349 systemData = [UNIVERSE currentSystemData];
351 switch (JSID_TO_INT(propID))
354 result = [systemData objectForKey:KEY_NAME];
358 result = [systemData objectForKey:KEY_DESCRIPTION];
362 result = [systemData objectForKey:KEY_INHABITANTS];
371 if (result ==
nil) result =
DESC(
@"not-applicable");
375 *value = INT_TO_JSVAL([systemData oo_intForKey:
KEY_ECONOMY]);
380 if (result ==
nil) result =
DESC(
@"not-applicable");
384 *value = INT_TO_JSVAL([systemData oo_intForKey:
KEY_TECHLEVEL]);
403 switch (JSID_TO_INT(propID))
406 result =
DESC(
@"interstellar-space");
414 result =
DESC(
@"not-applicable");
418 *value = INT_TO_JSVAL(-1);
422 result =
DESC(
@"not-applicable");
426 *value = INT_TO_JSVAL(-1);
430 result =
DESC(
@"not-applicable");
434 *value = INT_TO_JSVAL(-1);
438 *value = INT_TO_JSVAL(0);
442 *value = INT_TO_JSVAL(0);
459static JSBool
SystemSetProperty(JSContext *context, JSObject *
this, jsid propID, JSBool strict, jsval *value)
461 if (!JSID_IS_INT(propID))
return YES;
468 NSString *stringValue =
nil;
469 NSString *manifest =
nil;
479 switch (JSID_TO_INT(propID))
482 if (JS_ValueToNumber(context, *value, &fValue))
484 [UNIVERSE setAmbientLightLevel:fValue];
485 [UNIVERSE setLighting];
491 if (JS_ValueToBoolean(context, *value, &bValue))
493 [UNIVERSE setWitchspaceBreakPattern:bValue];
503 if (system == -1)
return YES;
507 switch (JSID_TO_INT(propID))
511 if (stringValue !=
nil)
513 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_NAME value:stringValue fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
520 if (stringValue !=
nil)
522 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_DESCRIPTION value:stringValue fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
529 if (stringValue !=
nil)
531 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_INHABITANTS value:stringValue fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
537 if (JS_ValueToInt32(context, *value, &iValue))
539 if (iValue < 0) iValue = 0;
540 if (7 < iValue) iValue = 7;
541 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_GOVERNMENT value:[NSNumber numberWithInt:iValue] fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
547 if (JS_ValueToInt32(context, *value, &iValue))
549 if (iValue < 0) iValue = 0;
550 if (7 < iValue) iValue = 7;
551 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_ECONOMY value:[NSNumber numberWithInt:iValue] fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
557 if (JS_ValueToInt32(context, *value, &iValue))
559 if (iValue < 0) iValue = 0;
560 if (15 < iValue) iValue = 15;
561 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_TECHLEVEL value:[NSNumber numberWithInt:iValue] fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
567 if (JS_ValueToInt32(context, *value, &iValue))
569 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_POPULATION value:[NSNumber numberWithInt:iValue] fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
575 if (JS_ValueToInt32(context, *value, &iValue))
577 [UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_PRODUCTIVITY value:[NSNumber numberWithInt:iValue] fromManifest:manifest forLayer:OO_LAYER_OXP_DYNAMIC];
602 NSString *systemDesc =
nil;
604 systemDesc = [NSString stringWithFormat:@"[System %u:%u \"%@\"]", [player
currentGalaxyID], [player
currentSystemID], [[UNIVERSE currentSystemData] objectForKey:KEY_NAME]];
618 OOPlanetEntity *planet =
nil;
644 OOPlanetEntity *planet =
nil;
682 NSString *role =
nil;
700 result = [UNIVERSE countShipsWithPrimaryRole:role inRange:range ofEntity:relativeTo];
714 NSString *role =
nil;
732 result = [UNIVERSE countShipsWithRole:role inRange:range ofEntity:relativeTo];
746 NSString *role =
nil;
749 NSArray *result =
nil;
779 NSString *role =
nil;
782 NSArray *result =
nil;
818 if (scanClass == CLASS_NOT_SET)
830 result = [UNIVERSE countShipsWithScanClass:scanClass inRange:range ofEntity:relativeTo];
847 NSArray *result =
nil;
850 if (scanClass == CLASS_NOT_SET)
877 JSObject *jsThis = NULL;
881 NSArray *result =
nil;
915 NSString *code =
nil;
926 OOPlanetEntity *planet = [UNIVERSE planet];
928 if (sun ==
nil || planet ==
nil)
930 position = [UNIVERSE locationByCode:@"WITCHPOINT" withSun:nil andPlanet:nil];
934 position = [UNIVERSE locationByCode:code withSun:sun andPlanet:planet];
977 NSString *role =
nil;
991 while (
count--) [UNIVERSE witchspaceShipWithPrimaryRole:role];
1007 NSString *role =
nil;
1015 !JS_ValueToNumber(context,
OOJS_ARGV[2], &position)))
1022 while (
count--) [UNIVERSE addShipWithRole:role nearRouteOneAt:position];
1039 NSString *role =
nil;
1041 NSString *coordScheme =
nil;
1042 NSString *arg =
nil;
1049 coordScheme ==
nil ||
1053 OOJSReportBadArguments(context,
@"System",
@"legacy_addShipsAt", argc,
OOJS_ARGV,
nil,
@"role, positive count no greater than 64, coordinate scheme and coordinates");
1058 arg = [NSString stringWithFormat:@"%@ %d %@ %f %f %f", role, count, coordScheme, where.x, where.y, where.z];
1076 NSString *role =
nil;
1078 NSString *coordScheme =
nil;
1079 NSString *arg =
nil;
1086 coordScheme ==
nil ||
1090 OOJSReportBadArguments(context,
@"System",
@"legacy_addShipsAtPrecisely", argc,
OOJS_ARGV,
nil,
@"role, positive count no greater than 64, coordinate scheme and coordinates");
1095 arg = [NSString stringWithFormat:@"%@ %d %@ %f %f %f", role, count, coordScheme, where.x, where.y, where.z];
1114 NSString *role =
nil;
1116 NSString *coordScheme =
nil;
1117 NSString *arg =
nil;
1125 coordScheme ==
nil ||
1128 !JS_ValueToNumber(context,
OOJS_ARGV[3 + consumed], &radius)))
1130 OOJSReportBadArguments(context,
@"System",
@"legacy_addShipWithinRadius", argc,
OOJS_ARGV,
nil,
@"role, positive count no greater than 64, coordinate scheme, coordinates and radius");
1135 arg = [NSString stringWithFormat:@"%@ %d %@ %f %f %f %f", role, count, coordScheme, where.x, where.y, where.z, radius];
1151 NSString *key =
nil;
1162 [UNIVERSE spawnShip:key];
1200 NSString *name =
nil;
1212 result = [UNIVERSE findSystemFromName:name];
1230 if (argc < 2 || !JS_ValueToInt32(context,
OOJS_ARGV[0], &galaxyID) || !JS_ValueToInt32(context,
OOJS_ARGV[1], &systemID))
1258 NSString *key =
nil;
1280 result = [UNIVERSE addVisualEffectAt:where withKey:key];
1294 NSMutableDictionary *settings;
1295 JSObject *params = NULL;
1308 if (argc < 2 || JSVAL_IS_NULL(
OOJS_ARGV[1]))
1311 [UNIVERSE setPopulatorSetting:key to:nil];
1316 if (!JS_ValueToObject(context,
OOJS_ARGV[1], ¶ms))
1321 jsval callback = JSVAL_NULL;
1322 if (JS_GetProperty(context, params,
"callback", &callback) == JS_FALSE || JSVAL_IS_VOID(callback))
1332 [settings setObject:populator forKey:@"callbackObj"];
1334 jsval coords = JSVAL_NULL;
1335 if (JS_GetProperty(context, params,
"coordinates", &coords) != JS_FALSE && !JSVAL_IS_VOID(coords))
1341 [settings setObject:[NSArray arrayWithObjects:[NSNumber numberWithFloat:coordinates.x],[NSNumber numberWithFloat:coordinates.y],[NSNumber numberWithFloat:coordinates.z],nil] forKey:@"coordinates"];
1345 [populator release];
1347 [UNIVERSE setPopulatorSetting:key to:settings];
1361 NSMutableDictionary *settings;
1363 Quaternion orientation;
1367 OOJSReportBadArguments(context,
@"System",
@"setWaypoint",
MIN(argc, 0U), &
OOJS_ARGV[0],
nil,
@"key, position, orientation, definition");
1373 OOJSReportBadArguments(context,
@"System",
@"setWaypoint",
MIN(argc, 0U), &
OOJS_ARGV[0],
nil,
@"key, position, orientation, definition");
1376 if (argc < 4 || JSVAL_IS_NULL(
OOJS_ARGV[3]))
1379 [UNIVERSE defineWaypoint:nil forKey:key];
1386 OOJSReportBadArguments(context,
@"System",
@"setWaypoint",
MIN(argc, 2U),
OOJS_ARGV, NULL,
@"key, position, orientation, definition");
1391 OOJSReportBadArguments(context,
@"System",
@"setWaypoint",
MIN(argc, 3U),
OOJS_ARGV, NULL,
@"key, position, orientation, definition");
1396 OOJSReportBadArguments(context,
@"System",
@"setWaypoint",
MIN(argc, 4U),
OOJS_ARGV, NULL,
@"key, position, orientation, definition");
1400 settings = [[OOJSNativeObjectFromJSObject(context, JSVAL_TO_OBJECT(OOJS_ARGV[3])) mutableCopy] autorelease];
1401 [settings setObject:[NSArray arrayWithObjects:[NSNumber numberWithDouble:position.x],[NSNumber numberWithDouble:position.y],[NSNumber numberWithDouble:position.z],nil] forKey:@"position"];
1402 [settings setObject:[NSArray arrayWithObjects:[NSNumber numberWithDouble:orientation.w],[NSNumber numberWithDouble:orientation.x],[NSNumber numberWithDouble:orientation.y],[NSNumber numberWithDouble:orientation.z],nil] forKey:@"orientation"];
1404 [UNIVERSE defineWaypoint:settings forKey:key];
1419 NSString *role =
nil;
1423 double radius = NSNotFound;
1426 NSString *func = isGroup ?
@"addGroup" :
@"addShips";
1442 where = [UNIVERSE getWitchspaceExitPosition];
1453 if (argc > 2 + consumed)
1455 if (!JS_ValueToNumber(context,
OOJS_ARGV[2 + consumed], &radius))
1465 result = [UNIVERSE addShipsAt:where withRole:role quantity:count withinRadius:radius asGroup:isGroup];
1469 NSArray *array = result;
1470 if ([array
count] > 0) result = [(
ShipEntity *)[array objectAtIndex:0] group];
1485 NSString *role =
nil;
1486 NSString *route =
@"st";
1487 static NSSet *validRoutes =
nil;
1489 double where = NSNotFound;
1492 NSString *func = isGroup ?
@"addGroup" :
@"addShips";
1508 if (!JS_ValueToNumber(context,
OOJS_ARGV[2], &where) || !isfinite(where) || where < 0.0f || where > 1.0f)
1516 route = [OOStringFromJSValue(context, OOJS_ARGV[3]) lowercaseString];
1518 if (validRoutes ==
nil)
1520 validRoutes = [[NSSet alloc] initWithObjects:@"wp", @"pw", @"ws", @"sw", @"sp", @"ps", nil];
1523 if (route ==
nil || ![validRoutes containsObject:route])
1533 result = [UNIVERSE addShipsToRoute:route withRole:role quantity:count routeFraction:where asGroup:isGroup];
1537 NSArray *array = result;
1538 if ([array
count] > 0) result = [(
ShipEntity *)[array objectAtIndex:0] group];
1554 assert(ioArgc && ioArgv && outRelativeTo && outRange);
1564 (*ioArgv)++; (*ioArgc)--;
1570 if (!
EXPECT_NOT(JS_ValueToNumber(context, **ioArgv, outRange)))
1575 (*ioArgv)++; (*ioArgc)--;
1588 NSMutableArray *result =
nil;
1592 predicate, parameter
1595 result = [UNIVERSE findEntitiesMatchingPredicate:ANDPredicate
1598 ofEntity:relativeTo];
1600 if (result !=
nil && relativeTo !=
nil && ![relativeTo isPlayer])
1602 [result sortUsingFunction:CompareEntitiesByDistance context:relativeTo];
1604 if (result ==
nil)
return [NSArray array];
1618 predicate, parameter
1632 *r = (id)relativeTo;
1635 d1 = HPdistance2(ea->position, r->position);
1636 d2 = HPdistance2(eb->position, r->position);
1638 if (d1 < d2)
return NSOrderedAscending;
1639 else if (d1 > d2)
return NSOrderedDescending;
1640 else return NSOrderedSame;
#define SCANNER_MAX_RANGE
OOINLINE OOScanClass OOScanClassFromJSValue(JSContext *context, jsval value)
NSString * OODisplayStringFromEconomyID(OOEconomyID economy)
NSString * OODisplayStringFromGovernmentID(OOGovernmentID government)
void OOStandardsDeprecated(NSString *message)
BOOL ANDPredicate(Entity *entity, void *parameter)
BOOL HasScanClassPredicate(Entity *entity, void *parameter)
BOOL HasPrimaryRolePredicate(Entity *ship, void *parameter)
BOOL IsShipPredicate(Entity *entity, void *parameter)
BOOL HasRolePredicate(Entity *ship, void *parameter)
const HPVector kZeroHPVector
#define OOJS_PROFILE_EXIT
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_PROFILE_ENTER
BOOL JSValueToEntity(JSContext *context, jsval value, Entity **outEntity)
PlayerEntity * OOPlayerForScripting(void)
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
jsval GetJSSystemInfoForSystem(JSContext *context, OOGalaxyID galaxy, OOSystemID system)
static JSPropertySpec sSystemProperties[]
static NSArray * FindShips(EntityFilterPredicate predicate, void *parameter, Entity *relativeTo, double range)
static JSBool SystemFilteredEntities(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddShipsOrGroup(JSContext *context, uintN argc, jsval *vp, BOOL isGroup)
static JSBool SystemLocationFromCode(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemLegacySpawnShip(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemStaticInfoForSystem(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddShipsOrGroupToRoute(JSContext *context, uintN argc, jsval *vp, BOOL isGroup)
static JSBool SystemStaticSystemIDForName(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemCountShipsWithRole(JSContext *context, uintN argc, jsval *vp)
static BOOL GetRelativeToAndRange(JSContext *context, NSString *methodName, uintN *ioArgc, jsval **ioArgv, Entity **outRelativeTo, double *outRange)
static JSFunctionSpec sSystemStaticMethods[]
static NSComparisonResult CompareEntitiesByDistance(id a, id b, void *relativeTo)
static JSBool SystemLegacyAddShips(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemCountShipsWithPrimaryRole(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddGroup(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddVisualEffect(JSContext *context, uintN argc, jsval *vp)
void InitOOJSSystem(JSContext *context, JSObject *global)
@ kSystem_inhabitantsDescription
@ kSystem_populatorSettings
@ kSystem_pseudoRandom100
@ kSystem_allVisualEffects
@ kSystem_governmentDescription
@ kSystem_pseudoRandom256
@ kSystem_pseudoRandomNumber
@ kSystem_economyDescription
@ kSystem_isInterstellarSpace
static JSBool SystemAddShips(JSContext *context, uintN argc, jsval *vp)
static NSArray * FindJSVisibleEntities(EntityFilterPredicate predicate, void *parameter, Entity *relativeTo, double range)
static JSBool SystemSetWaypoint(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemLegacyAddShipsAt(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemShipsWithPrimaryRole(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemLegacyAddSystemShips(JSContext *context, uintN argc, jsval *vp)
static JSClass sSystemClass
static JSBool SystemSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSFunctionSpec sSystemMethods[]
static JSBool SystemLegacyAddShipsAtPrecisely(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemSendAllShipsAway(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddShipsToRoute(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemToString(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemEntitiesWithScanClass(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool SystemAddGroupToRoute(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemSetPopulator(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemCountEntitiesWithScanClass(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddPlanet(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemShipsWithRole(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemLegacyAddShipsWithinRadius(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemStaticSystemNameForID(JSContext *context, uintN argc, jsval *vp)
static JSBool SystemAddMoon(JSContext *context, uintN argc, jsval *vp)
static JSObject * sSystemPrototype
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
BOOL JSValueToHPVector(JSContext *context, jsval value, HPVector *outVector) NONNULL_FUNC
BOOL BOOL VectorFromArgumentListNoError(JSContext *context, uintN argc, jsval *argv, HPVector *outVector, uintN *outConsumed) GCC_ATTR((nonnull(1
void OOJSPauseTimeLimiter(void)
BOOL JSEntityIsJavaScriptSearchablePredicate(Entity *entity, void *parameter)
#define OOJS_PROP_READWRITE_CB
BOOL JSFunctionPredicate(Entity *entity, void *parameter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_RETURN_HPVECTOR(value)
#define OOJS_PROP_READONLY_CB
void OOJSResumeTimeLimiter(void)
#define OOJS_RETURN_INT(v)
BOOL(* EntityFilterPredicate)(Entity *entity, void *parameter)
void setCallback:(jsval callback)
id propertyNamed:(NSString *name)
OOJSScript * currentlyRunningScript()
void addShipsWithinRadius:(NSString *roles_number_system_x_y_z_r)
OOSystemID currentSystemID()
void addShipsAt:(NSString *roles_number_system_x_y_z)
OOGalaxyID currentGalaxyID()
void addShipsAtPrecisely:(NSString *roles_number_system_x_y_z)
OOPlanetEntity * addPlanet:(NSString *planetKey)
OOPlanetEntity * addMoon:(NSString *moonKey)