JavaScript tweaks: deprecated entity IDs; made Entity.position and Entity.orienation read/write since their read-onliness wasn't really accomplishing anything; added toSource() method to Vector and Quaternion; Vector() and Quaternion() constructors can now be called without the new keyword, allowing the use of call() and apply().

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@2038 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2009-02-07 12:53:09 +00:00
parent 2bb7420bbb
commit ca68c038a4
7 changed files with 127 additions and 63 deletions

View File

@ -1059,7 +1059,7 @@
083325DC09DDBCDE00F5B8E4 /* OOColor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOColor.m; sourceTree = "<group>"; };
083DB4D30A70E51E00B419B2 /* OOBrain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOBrain.h; sourceTree = "<group>"; };
083DB4D40A70E51E00B419B2 /* OOBrain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOBrain.m; sourceTree = "<group>"; };
0865432206B8447D000CA0AB /* OoliteDev.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OoliteDev.app; sourceTree = BUILT_PRODUCTS_DIR; };
0865432206B8447D000CA0AB /* Oolite.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Oolite.app; sourceTree = BUILT_PRODUCTS_DIR; };
0878FD2F086EF845004CB752 /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = /System/Library/Frameworks/Carbon.framework; sourceTree = "<absolute>"; };
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
1A020E0A0D020AFB00C3F51E /* changedScriptHandlers.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = changedScriptHandlers.plist; sourceTree = "<group>"; };
@ -1763,7 +1763,7 @@
19C28FACFE9D520D11CA2CBB /* Products */ = {
isa = PBXGroup;
children = (
0865432206B8447D000CA0AB /* OoliteDev.app */,
0865432206B8447D000CA0AB /* Oolite.app */,
1A71E6F30BCE340C00CD5C13 /* libpng.a */,
);
name = Products;
@ -2393,6 +2393,8 @@
1A60AFB60D56093B0070510D /* OOMusicController.m */,
1A71D8A90E5F17410088C456 /* OOSoundSource.h */,
1A71D8A80E5F17410088C456 /* OOSoundSource.m */,
1ACBF06F0D82DF9B00CC005F /* OOSoundSourcePool.h */,
1ACBF0700D82DF9B00CC005F /* OOSoundSourcePool.m */,
1A7B967E0E620C9E00322821 /* OOSoundInternal.h */,
1A8A3D8F0B9B058500AB7625 /* Mac-specific */,
);
@ -2413,24 +2415,14 @@
1A8A3BE90B963F02007D20B8 /* Source */ = {
isa = PBXGroup;
children = (
1A8A3C0F0B964002007D20B8 /* AI */,
1A8A3C0C0B963FF8007D20B8 /* Entities */,
1A8A3C0F0B964002007D20B8 /* AI */,
1A8A3C1E0B9640B4007D20B8 /* Mathematics */,
1A8A3C180B964030007D20B8 /* Resource Management */,
1A8A3C1B0B96407A007D20B8 /* User Interface */,
1A8A3BFB0B963F91007D20B8 /* Utilities */,
1A8A3BF20B963F3C007D20B8 /* Mac-specific */,
1ACEA3BC0C915A1600C7CE97 /* Item management */,
1A3A04610BC547DC00B5E2D9 /* OOTypes.h */,
25161144099544390037C2E1 /* Universe.h */,
25161143099544390037C2E1 /* Universe.m */,
25161116099544390037C2E1 /* OOSound.h */,
25161110099544390037C2E1 /* OOTrumble.h */,
25161108099544390037C2E1 /* OOTrumble.m */,
2516110B099544390037C2E1 /* GameController.h */,
2516110C099544390037C2E1 /* GameController.m */,
1A5218D80D72EC21000865E9 /* OOSpatialReference.h */,
1A5218DD0D72ECE5000865E9 /* OOSpatialReference.m */,
1ACEA3BC0C915A1600C7CE97 /* Misc */,
1A7B96800E620CA000322821 /* Audio */,
1A2A1B020BD2768300152975 /* Graphics */,
1A5DBA980BC000DC00D57389 /* Scripting */,
@ -2516,8 +2508,6 @@
1AAB9A960D779F3C00A9F424 /* OOCocoa.m */,
1A1616600D7DCFDC0094AE5B /* OOFilteringEnumerator.h */,
1A1616610D7DCFDC0094AE5B /* OOFilteringEnumerator.m */,
1ACBF06F0D82DF9B00CC005F /* OOSoundSourcePool.h */,
1ACBF0700D82DF9B00CC005F /* OOSoundSourcePool.m */,
1A047B7C0DCB3D7500EE1CD0 /* OOProbabilitySet.h */,
1A047B7D0DCB3D7500EE1CD0 /* OOProbabilitySet.m */,
);
@ -2722,9 +2712,19 @@
path = Doc;
sourceTree = "<group>";
};
1ACEA3BC0C915A1600C7CE97 /* Item management */ = {
1ACEA3BC0C915A1600C7CE97 /* Misc */ = {
isa = PBXGroup;
children = (
1A3A04610BC547DC00B5E2D9 /* OOTypes.h */,
25161144099544390037C2E1 /* Universe.h */,
25161143099544390037C2E1 /* Universe.m */,
25161116099544390037C2E1 /* OOSound.h */,
25161110099544390037C2E1 /* OOTrumble.h */,
25161108099544390037C2E1 /* OOTrumble.m */,
2516110B099544390037C2E1 /* GameController.h */,
2516110C099544390037C2E1 /* GameController.m */,
1A5218D80D72EC21000865E9 /* OOSpatialReference.h */,
1A5218DD0D72ECE5000865E9 /* OOSpatialReference.m */,
1ACEA3470C91507000C7CE97 /* OORoleSet.h */,
1ACEA3480C91507000C7CE97 /* OORoleSet.m */,
1A0479E70DC9F81000EE1CD0 /* OOShipRegistry.h */,
@ -2732,7 +2732,7 @@
1A62F0FC0E26A2A000897506 /* OOEquipmentType.h */,
1A62F0FB0E26A2A000897506 /* OOEquipmentType.m */,
);
name = "Item management";
name = Misc;
sourceTree = "<group>";
};
1AD0C6B90C47B70F0070BD23 /* Smart Crash Reports */ = {
@ -3048,7 +3048,7 @@
name = Oolite;
productInstallPath = "$(HOME)/Applications";
productName = Oolite;
productReference = 0865432206B8447D000CA0AB /* OoliteDev.app */;
productReference = 0865432206B8447D000CA0AB /* Oolite.app */;
productType = "com.apple.product-type.application";
};
1A71E6F20BCE340C00CD5C13 /* libpng-custom */ = {

View File

@ -278,5 +278,34 @@ this.defineCompatibilitySubGetter("player", "ship", "isPlanet");
this.defineCompatibilitySubGetter("player", "ship", "isSun");
this.defineCompatibilitySubGetter("player", "ship", "distanceTravelled");
this.defineCompatibilitySubGetter("player", "ship", "spawnTime");
this.defineCompatibilitySubMethod("player", "ship", "setPosition");
this.defineCompatibilitySubMethod("player", "ship", "setOrientation");
player.setPosition = function ()
{
special.jsWarning("player.setPosition() is deprecated, use player.ship.position = foo instead.");
this.ship.position = Vector.apply(Vector, arguments);
}
player.setOrientation = function ()
{
special.jsWarning("player.setOrientation() is deprecated, use player.ship.orientation = foo instead.");
this.ship.orientation = Quaternion.apply(Quaternion, arguments);
}
/**** To be removed after 1.74 at the latest ****/
Entity.__proto__.setPosition = function ()
{
special.jsWarning("Entity.setPosition() is deprecated, use entity.position = foo instead.");
this.position = Vector.apply(Vector, arguments);
}
Entity.__proto__.setOrientation = function ()
{
special.jsWarning("Entity.setOrientation() is deprecated, use entity.orientation = foo instead.");
this.orientation = Quaternion.apply(Quaternion, arguments);
}
// Entity.ID, Entity.entityWithID(), ability to pass an ID instead of an entity

View File

@ -241,7 +241,7 @@ static void PerformActionStatment(NSArray *statement, Entity *target)
selectorString = [statement objectAtIndex:1];
if ([statement count] > 2) argumentString = [statement objectAtIndex:2];
TraceLog(kOOLogTraceScriptAction, @"script action: \"%@%@\"", selectorString, argumentString ? [@" " stringByAppendingString:argumentString] : @"");
TraceLog(kOOLogTraceScriptAction, @"script action: \"%@%@\"", selectorString, argumentString ? (NSString *)[@" " stringByAppendingString:argumentString] : (NSString *)@"");
selector = NSSelectorFromString(selectorString);
@ -515,7 +515,7 @@ static BOOL sRunningScript = NO;
{
lhsString = [self performSelector:selector];
TraceLog(kOOLogTraceTestConditionValues, @"..... comparing %@ (from %@) to \"%@\" with operator %@",
lhsString ? [NSString stringWithFormat:@"\"%@\"", lhsString] : (NSString *)@"nil",
lhsString ? (NSString *)[NSString stringWithFormat:@"\"%@\"", lhsString] : (NSString *)@"nil",
selectorString,
expandedRHS ? expandedRHS: (NSString *)(comparator == COMPARISON_UNDEFINED ? @"undefined" : @"nil"),
OOComparisonTypeToString(comparator));

View File

@ -641,6 +641,8 @@ static BOOL sRectangleTextureAvailable;
}
_mipLevels = level - 1;
// FIXME: GL_TEXTURE_MAX_LEVEL requires OpenGL 1.2. This should be fixed by generating all mip-maps for non-square textures so we don't need to use it.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, _mipLevels);
}

View File

@ -40,10 +40,6 @@ static JSObject *sEntityPrototype;
static JSBool EntityGetProperty(JSContext *context, JSObject *this, jsval name, jsval *outValue);
static JSBool EntitySetProperty(JSContext *context, JSObject *this, jsval name, jsval *value);
// Methods
static JSBool EntitySetPosition(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool EntitySetOrientation(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
// Static methods
static JSBool EntityStaticEntityWithID(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
@ -75,8 +71,8 @@ enum
{
// Property IDs
kEntity_ID, // universalID, int, read-only
kEntity_position, // position in system space, Vector, read-only
kEntity_orientation, // orientation, quaternion, read-write
kEntity_position, // position in system space, Vector, read/write
kEntity_orientation, // orientation, quaternion, read/write
kEntity_heading, // heading, vector, read-only (like orientation but ignoring twist angle)
kEntity_status, // entity status, string, read-only
kEntity_scanClass, // scan class, string, read-only
@ -100,8 +96,8 @@ static JSPropertySpec sEntityProperties[] =
{
// JS name ID flags
{ "ID", kEntity_ID, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "position", kEntity_position, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "orientation", kEntity_orientation, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "position", kEntity_position, JSPROP_PERMANENT | JSPROP_ENUMERATE },
{ "orientation", kEntity_orientation, JSPROP_PERMANENT | JSPROP_ENUMERATE },
{ "heading", kEntity_heading, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "status", kEntity_status, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "scanClass", kEntity_scanClass, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
@ -126,8 +122,6 @@ static JSFunctionSpec sEntityMethods[] =
{
// JS name Function min args
{ "toString", JSObjectWrapperToString, 0 },
{ "setPosition", EntitySetPosition, 1 },
{ "setOrientation", EntitySetOrientation, 1 },
{ 0 }
};
@ -159,6 +153,7 @@ BOOL JSValueToEntity(JSContext *context, jsval value, Entity **outEntity)
}
else if (JSVAL_IS_INT(value)) // Should we accept general numbers? (Currently, UniversalIDs are clamped to [100, 1000].)
{
OOReportJSWarning(context, @"The ability to pass an entity ID instead of an entity is deprecated and will be removed in a future version of Oolite.");
entity = [UNIVERSE entityForUniversalID:JSVAL_TO_INT(value)];
if (entity && [entity isVisibleToScripts])
{
@ -229,6 +224,7 @@ static JSBool EntityGetProperty(JSContext *context, JSObject *this, jsval name,
switch (JSVAL_TO_INT(name))
{
case kEntity_ID:
OOReportJSWarning(context, @"The property Entity.ID is deprecated and will be removed in a future version of Oolite.");
*outValue = INT_TO_JSVAL([entity universalID]);
OK = YES;
break;
@ -332,12 +328,30 @@ static JSBool EntitySetProperty(JSContext *context, JSObject *this, jsval name,
BOOL OK = NO;
Entity *entity = nil;
double fValue;
Vector vValue;
Quaternion qValue;
if (!JSVAL_IS_INT(name)) return YES;
if (EXPECT_NOT(!JSEntityGetEntity(context, this, &entity))) return NO;
switch (JSVAL_TO_INT(name))
{
case kEntity_position:
if (JSValueToVector(context, *value, &vValue))
{
[entity setPosition:vValue];
OK = YES;
}
break;
case kEntity_orientation:
if (JSValueToQuaternion(context, *value, &qValue))
{
[entity setNormalOrientation:qValue];
OK = YES;
}
break;
case kEntity_energy:
if (JS_ValueToNumber(context, *value, &fValue))
{
@ -355,36 +369,6 @@ static JSBool EntitySetProperty(JSContext *context, JSObject *this, jsval name,
}
// *** Methods ***
// setPosition(position : vectorExpression)
static JSBool EntitySetPosition(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{
Entity *thisEnt = nil;
Vector vector;
if (!JSEntityGetEntity(context, this, &thisEnt)) return YES; // stale reference, no-op.
if (EXPECT_NOT(!VectorFromArgumentList(context, @"Entity", @"setPosition", argc, argv, &vector, NULL))) return NO;
[thisEnt setPosition:vector];
return YES;
}
// setOrientation(orientation : quaternionExpression)
static JSBool EntitySetOrientation(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{
Entity *thisEnt = nil;
Quaternion quaternion;
if (!JSEntityGetEntity(context, this, &thisEnt)) return YES; // stale reference, no-op.
if (EXPECT_NOT(!QuaternionFromArgumentList(context, @"Entity", @"setOrientation", argc, argv, &quaternion, NULL))) return NO;
[thisEnt setNormalOrientation:quaternion];
return YES;
}
// *** Static methods ***
// entityWithID(ID : Number) : Entity
@ -393,6 +377,8 @@ static JSBool EntityStaticEntityWithID(JSContext *context, JSObject *this, uintN
Entity *result = nil;
int32 ID;
OOReportJSWarning(context, @"The function Entity.entityWithID() is deprecated and will be removed in a future version of Oolite.");
if (EXPECT_NOT(!JS_ValueToInt32(context, *argv, &ID)))
{
OOReportJSBadArguments(context, @"Entity", @"entityWithID", argc, argv, @"Invalid entity ID", @"integer");

View File

@ -47,6 +47,7 @@ static JSBool QuaternionEquality(JSContext *context, JSObject *this, jsval value
// Methods
static JSBool QuaternionToString(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool QuaternionToSource(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool QuaternionMultiply(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool QuaternionDot(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool QuaternionRotate(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
@ -111,6 +112,7 @@ static JSFunctionSpec sQuaternionMethods[] =
{
// JS name Function min args
{ "toString", QuaternionToString, 0, },
{ "toSource", QuaternionToSource, 0, },
{ "multiply", QuaternionMultiply, 1, },
{ "dot", QuaternionDot, 1, },
{ "rotate", QuaternionRotate, 2, },
@ -412,6 +414,14 @@ static JSBool QuaternionConstruct(JSContext *context, JSObject *this, uintN argc
private = malloc(sizeof *private);
if (EXPECT_NOT(private == NULL)) return NO;
// If called without new, replace this with a new Vector object.
if (!JS_IsConstructing(context))
{
this = JS_NewObject(context, &sQuaternionClass.base, NULL, NULL);
if (this == NULL) return NO;
*outResult = OBJECT_TO_JSVAL(this);
}
if (argc != 0)
{
if (EXPECT_NOT(!QuaternionFromArgumentListNoErrorInternal(context, argc, argv, &quaternion, NULL, NO)))
@ -465,6 +475,19 @@ static JSBool QuaternionToString(JSContext *context, JSObject *this, uintN argc,
}
// toSource() : String
static JSBool QuaternionToSource(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{
Quaternion thisq;
if (EXPECT_NOT(!JSObjectGetQuaternion(context, this, &thisq))) return NO;
*outResult = [[NSString stringWithFormat:@"Quaternion(%g, %g, %g, %g)", thisq.w, thisq.x, thisq.y, thisq.z]
javaScriptValueInContext:context];
return YES;
}
// multiply(q : quaternionExpression) : Quaternion
static JSBool QuaternionMultiply(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{

View File

@ -47,6 +47,7 @@ static JSBool VectorEquality(JSContext *context, JSObject *this, jsval value, JS
// Methods
static JSBool VectorToString(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool VectorToSource(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool VectorAdd(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool VectorSubtract(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool VectorDistanceTo(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
@ -68,6 +69,7 @@ static JSBool VectorStaticInterpolate(JSContext *context, JSObject *this, uintN
static JSBool VectorStaticRandom(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool VectorStaticRandomDirection(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSBool VectorStaticRandomDirectionAndLength(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
//static JSBool VectorStaticConstruct(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSExtendedClass sVectorClass =
@ -116,6 +118,7 @@ static JSFunctionSpec sVectorMethods[] =
{
// JS name Function min args
{ "toString", VectorToString, 0, },
{ "toSource", VectorToSource, 0, },
{ "add", VectorAdd, 1, },
{ "subtract", VectorSubtract, 1, },
{ "distanceTo", VectorDistanceTo, 1, },
@ -416,6 +419,14 @@ static JSBool VectorConstruct(JSContext *context, JSObject *this, uintN argc, js
private = malloc(sizeof *private);
if (EXPECT_NOT(private == NULL)) return NO;
// If called without new, replace this with a new Vector object.
if (!JS_IsConstructing(context))
{
this = JS_NewObject(context, &sVectorClass.base, NULL, NULL);
if (this == NULL) return NO;
*outResult = OBJECT_TO_JSVAL(this);
}
if (argc != 0)
{
if (EXPECT_NOT(!VectorFromArgumentListNoErrorInternal(context, argc, argv, &vector, NULL, NO)))
@ -468,6 +479,19 @@ static JSBool VectorToString(JSContext *context, JSObject *this, uintN argc, jsv
}
// toSource() : String
static JSBool VectorToSource(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{
Vector thisv;
if (EXPECT_NOT(!JSObjectGetVector(context, this, &thisv))) return NO;
*outResult = [[NSString stringWithFormat:@"Vector(%g, %g, %g)", thisv.x, thisv.y, thisv.z]
javaScriptValueInContext:context];
return YES;
}
// add(v : vectorExpression) : Vector3D
static JSBool VectorAdd(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{