From be5d0e26a896a390da76f55a741bc63ee4c38d84 Mon Sep 17 00:00:00 2001 From: Chris Morris Date: Wed, 25 Apr 2012 19:03:08 +0000 Subject: [PATCH] - Laser setting API now takes any valid equipmentInfoExpression - Laser setting API now works fore and aft for NPCs git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@4883 127b21dd-08f5-0310-b4b7-95ae10353056 --- src/Core/Entities/ShipEntity.h | 1 + src/Core/Entities/ShipEntity.m | 26 ++++++++++++++++++++++++++ src/Core/Scripting/OOJSShip.m | 32 ++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/Core/Entities/ShipEntity.h b/src/Core/Entities/ShipEntity.h index 50f7f825..195a3e60 100644 --- a/src/Core/Entities/ShipEntity.h +++ b/src/Core/Entities/ShipEntity.h @@ -516,6 +516,7 @@ typedef enum - (BOOL) hasEquipmentItem:(id)equipmentKeys; // Short for hasEquipmentItem:foo includeWeapons:NO whileLoading:NO - (BOOL) hasAllEquipment:(id)equipmentKeys includeWeapons:(BOOL)includeWeapons whileLoading:(BOOL)loading; // Like hasEquipmentItem:includeWeapons:, but requires _all_ elements in collection. - (BOOL) hasAllEquipment:(id)equipmentKeys; // Short for hasAllEquipment:foo includeWeapons:NO +- (BOOL) setWeaponMount:(int)facing toWeapon:(NSString *)eqKey; - (BOOL) canAddEquipment:(NSString *)equipmentKey; // Test ability to add equipment, taking equipment-specific constriants into account. - (BOOL) equipmentValidToAdd:(NSString *)equipmentKey; // Actual test if equipment satisfies validation criteria. - (BOOL) equipmentValidToAdd:(NSString *)equipmentKey whileLoading:(BOOL)loading; diff --git a/src/Core/Entities/ShipEntity.m b/src/Core/Entities/ShipEntity.m index 2632365a..d3c75d69 100644 --- a/src/Core/Entities/ShipEntity.m +++ b/src/Core/Entities/ShipEntity.m @@ -2525,6 +2525,32 @@ ShipEntity* doOctreesCollide(ShipEntity* prime, ShipEntity* other) } +- (BOOL) setWeaponMount:(int)facing toWeapon:(NSString *)eqKey +{ +// sets WEAPON_NONE if not recognised + int chosen_weapon = OOWeaponTypeFromEquipmentIdentifierStrict(eqKey); + + switch (facing) + { + case WEAPON_FACING_FORWARD : + forward_weapon_type = chosen_weapon; + break; + case WEAPON_FACING_AFT : + aft_weapon_type = chosen_weapon; + break; +/* // not for now + case WEAPON_FACING_PORT : + port_weapon_type = chosen_weapon; + break; + case WEAPON_FACING_STARBOARD : + starboard_weapon_type = chosen_weapon; + break; */ + } + + return YES; +} + + - (BOOL) addEquipmentItem:(NSString *)equipmentKey { return [self addEquipmentItem:equipmentKey withValidation:YES]; diff --git a/src/Core/Scripting/OOJSShip.m b/src/Core/Scripting/OOJSShip.m index 05d9640f..96844f3c 100644 --- a/src/Core/Scripting/OOJSShip.m +++ b/src/Core/Scripting/OOJSShip.m @@ -691,6 +691,7 @@ static JSBool ShipSetProperty(JSContext *context, JSObject *this, jsid propID, J Vector vValue; OOShipGroup *group = nil; OOColor *colorForScript = nil; + BOOL exists; if (EXPECT_NOT(!JSShipGetShipEntity(context, this, &entity))) return NO; if (OOIsStaleEntity(entity)) return YES; @@ -950,20 +951,39 @@ static JSBool ShipSetProperty(JSContext *context, JSObject *this, jsid propID, J } break; -// TO DO: make these work for NPC ships too - case kShip_aftWeapon: - case kShip_forwardWeapon: + case kShip_portWeapon: case kShip_starboardWeapon: if (EXPECT_NOT(![entity isPlayer])) goto npcReadOnly; - sValue = OOStringFromJSValue(context,*value); + case kShip_aftWeapon: + case kShip_forwardWeapon: + + sValue = JSValueToEquipmentKeyRelaxed(context, *value, &exists); + if (sValue == nil) + { + sValue = @"EQ_WEAPON_NONE"; + } switch (JSID_TO_INT(propID)) { case kShip_aftWeapon: - [PLAYER setWeaponMount:WEAPON_FACING_AFT toWeapon:sValue]; + if ([entity isPlayer]) + { + [PLAYER setWeaponMount:WEAPON_FACING_AFT toWeapon:sValue]; + } + else + { + [entity setWeaponMount:WEAPON_FACING_AFT toWeapon:sValue]; + } break; case kShip_forwardWeapon: - [PLAYER setWeaponMount:WEAPON_FACING_FORWARD toWeapon:sValue]; + if ([entity isPlayer]) + { + [PLAYER setWeaponMount:WEAPON_FACING_FORWARD toWeapon:sValue]; + } + else + { + [entity setWeaponMount:WEAPON_FACING_FORWARD toWeapon:sValue]; + } break; case kShip_portWeapon: [PLAYER setWeaponMount:WEAPON_FACING_PORT toWeapon:sValue];