From 5a84795861a99b71766c06534aae88b2f851e7eb Mon Sep 17 00:00:00 2001 From: cim Date: Sat, 22 Aug 2015 15:34:22 +0100 Subject: [PATCH] Make ship.weaponPosition* return an array of vectors --- src/Core/Entities/ShipEntity.m | 7 +++++-- src/Core/OOCollectionExtractors.m | 6 +++++- src/Core/OOVector.h | 12 ++++++++++++ src/Core/OOVector.m | 22 ++++++++++++++++++++++ src/Core/Scripting/OOJSShip.m | 3 +++ src/Core/Scripting/OOJSVector.h | 3 +++ src/Core/Scripting/OOJSVector.m | 2 ++ src/Core/Scripting/OOJavaScriptEngine.m | 11 +++++++++++ 8 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Core/Entities/ShipEntity.m b/src/Core/Entities/ShipEntity.m index 2fe87f23..9ba1ce4d 100644 --- a/src/Core/Entities/ShipEntity.m +++ b/src/Core/Entities/ShipEntity.m @@ -1971,7 +1971,7 @@ static ShipEntity *doOctreesCollide(ShipEntity *prime, ShipEntity *other); } -#define MAKE_VECTOR_ARRAY(v) [NSArray arrayWithObjects:[NSNumber numberWithFloat:v.x], [NSNumber numberWithFloat:v.y], [NSNumber numberWithFloat:v.z], nil] +#define MAKE_VECTOR_ARRAY(v) [[[OONativeVector alloc] initWithVector:v] autorelease] - (NSArray *) getWeaponOffsetFrom:(NSDictionary *)dict withKey:(NSString *)key inMode:(NSString *)mode { @@ -11467,7 +11467,10 @@ Vector positionOffsetForShipInRotationToAlignment(ShipEntity* ship, Quaternion q ShipEntity *se = nil; for (subEnum = [self shipSubEntityEnumerator]; (se = [subEnum nextObject]); ) { - if ([se fireSubentityLaserShot:range]) fired = YES; + if ([se fireSubentityLaserShot:range]) + { + fired = YES; + } } } diff --git a/src/Core/OOCollectionExtractors.m b/src/Core/OOCollectionExtractors.m index d10f102a..a323003f 100644 --- a/src/Core/OOCollectionExtractors.m +++ b/src/Core/OOCollectionExtractors.m @@ -1409,7 +1409,11 @@ Vector OOVectorFromObject(id object, Vector defaultValue) Vector result = defaultValue; NSDictionary *dict = nil; - if ([object isKindOfClass:[NSString class]]) + if ([object isKindOfClass:[OONativeVector class]]) + { + result = [object getVector]; + } + else if ([object isKindOfClass:[NSString class]]) { // This will only write result if a valid vector is found, and will write an error message otherwise. ScanVectorFromString(object, &result); diff --git a/src/Core/OOVector.h b/src/Core/OOVector.h index 90387d51..b622affa 100644 --- a/src/Core/OOVector.h +++ b/src/Core/OOVector.h @@ -131,6 +131,18 @@ OOINLINE Vector normal_to_surface(Vector v1, Vector v2, Vector v3) CONST_FUNC; #if __OBJC__ NSString *VectorDescription(Vector vector); // @"(x, y, z)" + +/* For storing vectors in NSArrays */ +@interface OONativeVector: NSObject +{ +@private + Vector v; +} +- (id) initWithVector:(Vector)vect; +- (Vector) getVector; + +@end + #endif #if OOMATHS_OPENGL_INTEGRATION diff --git a/src/Core/OOVector.m b/src/Core/OOVector.m index 0aa5ae16..73ca06ab 100644 --- a/src/Core/OOVector.m +++ b/src/Core/OOVector.m @@ -44,9 +44,31 @@ NSString *VectorDescription(Vector vector) { return [NSString stringWithFormat:@"(%g, %g, %g)", vector.x, vector.y, vector.z]; } + +@implementation OONativeVector + +- (id) initWithVector:(Vector)vect +{ + self = [super init]; + if (EXPECT_NOT(self == nil)) return nil; + + v = vect; + + return self; +} + +- (Vector) getVector +{ + return v; +} + + +@end + #endif + #if !OOMATHS_STANDALONE /* This generates random vectors distrubuted evenly over the surface of the unit sphere. It does this the simple way, by generating vectors in the diff --git a/src/Core/Scripting/OOJSShip.m b/src/Core/Scripting/OOJSShip.m index f4e58576..5360833a 100644 --- a/src/Core/Scripting/OOJSShip.m +++ b/src/Core/Scripting/OOJSShip.m @@ -4122,6 +4122,9 @@ static JSBool ShipThreatAssessment(JSContext *context, uintN argc, jsval *vp) // check lasers OOWeaponType wt = [thisEnt weaponTypeIDForFacing:WEAPON_FACING_FORWARD strict:NO]; + /* Not affected by multiple mounts here: they're either just a + * split of the power, or only more dangerous until they + * overheat */ assessment += ShipThreatAssessmentWeapon(wt); if (isWeaponNone(wt)) { diff --git a/src/Core/Scripting/OOJSVector.h b/src/Core/Scripting/OOJSVector.h index fb5c6b21..499002f3 100644 --- a/src/Core/Scripting/OOJSVector.h +++ b/src/Core/Scripting/OOJSVector.h @@ -73,3 +73,6 @@ BOOL VectorFromArgumentList(JSContext *context, NSString *scriptClass, NSString Like VectorFromArgumentList(), but does not report an error on failure. */ BOOL VectorFromArgumentListNoError(JSContext *context, uintN argc, jsval *argv, HPVector *outVector, uintN *outConsumed) GCC_ATTR((nonnull (1, 3, 4))); + + + diff --git a/src/Core/Scripting/OOJSVector.m b/src/Core/Scripting/OOJSVector.m index 4e5799be..39385cfd 100644 --- a/src/Core/Scripting/OOJSVector.m +++ b/src/Core/Scripting/OOJSVector.m @@ -1136,3 +1136,5 @@ static JSBool VectorStaticRandomDirectionAndLength(JSContext *context, uintN arg OOJS_PROFILE_EXIT } + + diff --git a/src/Core/Scripting/OOJavaScriptEngine.m b/src/Core/Scripting/OOJavaScriptEngine.m index 078a263e..68490b45 100644 --- a/src/Core/Scripting/OOJavaScriptEngine.m +++ b/src/Core/Scripting/OOJavaScriptEngine.m @@ -1904,6 +1904,17 @@ NSString *OOJSDescribeValue(JSContext *context, jsval value, BOOL abbreviateObje @end +@implementation OONativeVector (OOJavaScriptConversion) + +- (jsval)oo_jsValueInContext:(JSContext *)context +{ + jsval value = JSVAL_VOID; + VectorToJSValue(context, v, &value); + return value; +} + +@end + @implementation NSDictionary (OOJavaScriptConversion)