diff --git a/Doc/CHANGELOG.TXT b/Doc/CHANGELOG.TXT index c8409d9b..78b8bb91 100644 --- a/Doc/CHANGELOG.TXT +++ b/Doc/CHANGELOG.TXT @@ -146,6 +146,7 @@ Methods: * ship.adjustCargo(commodity,amount) * ship.removeCollisionException(ship) * ship.hasEquipmentProviding(eq) +* ship.dumpCargo(amount, preferredtype) second parameter * player.ship.equipmentStatus(eqKey,true) second parameter * SystemInfo.setInterstellarProperty(g,s1,s2,l,k,v,[m]) diff --git a/src/Core/Entities/ShipEntity.h b/src/Core/Entities/ShipEntity.h index 4963b005..9878d925 100644 --- a/src/Core/Entities/ShipEntity.h +++ b/src/Core/Entities/ShipEntity.h @@ -1122,7 +1122,7 @@ Vector positionOffsetForShipInRotationToAlignment(ShipEntity* ship, Quaternion q - (BOOL) launchCascadeMine; - (ShipEntity *) launchEscapeCapsule; - (OOCommodityType) dumpCargo; -- (ShipEntity *) dumpCargoItem; +- (ShipEntity *) dumpCargoItem:(OOCommodityType)preferred; - (OOCargoType) dumpItem: (ShipEntity*) jetto; - (void) manageCollisions; diff --git a/src/Core/Entities/ShipEntity.m b/src/Core/Entities/ShipEntity.m index 1d89119a..b912c7c9 100644 --- a/src/Core/Entities/ShipEntity.m +++ b/src/Core/Entities/ShipEntity.m @@ -12341,7 +12341,7 @@ Vector positionOffsetForShipInRotationToAlignment(ShipEntity* ship, Quaternion q // This is a documented AI method; do not change semantics. (Note: AIs don't have access to the return value.) - (OOCommodityType) dumpCargo { - ShipEntity *jetto = [self dumpCargoItem]; + ShipEntity *jetto = [self dumpCargoItem:nil]; if (jetto != nil) { return [jetto commodityType]; @@ -12353,17 +12353,40 @@ Vector positionOffsetForShipInRotationToAlignment(ShipEntity* ship, Quaternion q } -- (ShipEntity *) dumpCargoItem +- (ShipEntity *) dumpCargoItem:(OOCommodityType)preferred { ShipEntity *jetto = nil; + NSUInteger i = 0; if (([cargo count] > 0)&&([UNIVERSE getTime] - cargo_dump_time > 0.5)) // space them 0.5s or 10m apart { - jetto = [[[cargo objectAtIndex:0] retain] autorelease]; + if (preferred == nil) + { + jetto = [[[cargo objectAtIndex:0] retain] autorelease]; + } + else + { + BOOL found = NO; + for (i=0;i<[cargo count];i++) + { + if ([[[cargo objectAtIndex:i] commodityType] isEqualToString:preferred]) + { + jetto = [[[cargo objectAtIndex:i] retain] autorelease]; + found = YES; + break; + } + } + if (found == NO) + { + // dump anything + jetto = [[[cargo objectAtIndex:0] retain] autorelease]; + i = 0; + } + } if (jetto != nil) { [self dumpItem:jetto]; // CLASS_CARGO, STATUS_IN_FLIGHT, AI state GLOBAL - [cargo removeObjectAtIndex:0]; + [cargo removeObjectAtIndex:i]; [self broadcastAIMessage:@"CARGO_DUMPED"]; // goes only to 16 nearby ships in range, but that should be enough. unsigned i; // only send script event to powered entities diff --git a/src/Core/Scripting/OOJSShip.m b/src/Core/Scripting/OOJSShip.m index 83422a01..6af6ae38 100644 --- a/src/Core/Scripting/OOJSShip.m +++ b/src/Core/Scripting/OOJSShip.m @@ -2200,7 +2200,8 @@ static JSBool ShipDumpCargo(JSContext *context, uintN argc, jsval *vp) OOJS_NATIVE_ENTER(context) ShipEntity *thisEnt = nil; - + OOCommodityType pref = nil; + GET_THIS_SHIP(thisEnt); if (EXPECT_NOT([thisEnt isPlayer] && [(PlayerEntity *)thisEnt isDocked])) @@ -2208,6 +2209,12 @@ static JSBool ShipDumpCargo(JSContext *context, uintN argc, jsval *vp) OOJSReportWarningForCaller(context, @"PlayerShip", @"dumpCargo", @"Can't dump cargo while docked, ignoring."); OOJS_RETURN_NULL; } + + if (argc > 1) + { + pref = OOStringFromJSValue(context, OOJS_ARGV[1]); + } + // NPCs can queue multiple items to dump if (!EXPECT_NOT([thisEnt isPlayer])) { @@ -2216,7 +2223,7 @@ static JSBool ShipDumpCargo(JSContext *context, uintN argc, jsval *vp) if (argc > 0) gotCount = JS_ValueToInt32(context, OOJS_ARGV[0], &count); if (EXPECT_NOT(!gotCount || count < 1 || count > 64)) { - OOJSReportBadArguments(context, @"Ship", @"dumpCargo", MIN(argc, 1U), OOJS_ARGV, nil, @"optional quantity (1 to 64)"); + OOJSReportBadArguments(context, @"Ship", @"dumpCargo", MIN(argc, 1U), OOJS_ARGV, nil, @"optional quantity (1 to 64), optional preferred commodity"); return NO; } @@ -2226,7 +2233,7 @@ static JSBool ShipDumpCargo(JSContext *context, uintN argc, jsval *vp) } } - OOJS_RETURN_OBJECT([thisEnt dumpCargoItem]); + OOJS_RETURN_OBJECT([thisEnt dumpCargoItem:pref]); OOJS_NATIVE_EXIT }