Improved implementation of fuzzy booleans. As a result, wrecks work again now. Cached result of [UNIVERSE nearbyDestinationsWithinRange] in -performHyperSpaceExitReplace: because looking for nearby systems in every frame is very expensive.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@1665 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Nikos Barkas 2008-06-02 08:20:47 +00:00
parent 1c5949869a
commit 451297e529
3 changed files with 46 additions and 10 deletions

View File

@ -1879,11 +1879,20 @@ WormholeEntity* whole;
- (void)performHyperSpaceExitReplace:(BOOL)replace
{
// The [UNIVERSE nearbyDestinationsWithinRange:] method is very expensive, so cache
// its results.
static NSArray *sDests = nil;
whole = nil;
// get a list of destinations within range
NSArray* dests = [UNIVERSE nearbyDestinationsWithinRange: 0.1 * fuel];
int n_dests = [dests count];
if (sDests == nil)
{
sDests = [[UNIVERSE nearbyDestinationsWithinRange: 0.1 * fuel] copy];
}
int n_dests = [sDests count];
// if none available report to the AI and exit
if (!n_dests)
@ -1906,9 +1915,9 @@ WormholeEntity* whole;
if (n_dests > 1)
i = ranrot_rand() % n_dests;
NSString* systemSeedKey = [(NSDictionary*)[dests objectAtIndex:i] objectForKey:@"system_seed"];
NSString* systemSeedKey = [(NSDictionary*)[sDests objectAtIndex:i] objectForKey:@"system_seed"];
Random_Seed targetSystem = RandomSeedFromString(systemSeedKey);
fuel -= 10 * [[(NSDictionary*)[dests objectAtIndex:i] objectForKey:@"distance"] doubleValue];
fuel -= 10 * [[(NSDictionary*)[sDests objectAtIndex:i] objectForKey:@"distance"] doubleValue];
// create wormhole
whole = [[[WormholeEntity alloc] initWormholeTo: targetSystem fromShip: self] autorelease];
@ -1920,6 +1929,11 @@ WormholeEntity* whole;
[shipAI reactToMessage:@"WITCHSPACE OKAY"]; // must be a reaction, the ship is about to disappear
[self enterWormhole:whole replacing:replace]; // TODO
// If we have reached this code, it means that the ship has already entered hyperspace,
// the destinations array is therefore no longer required and can be released.
[sDests release];
sDests = nil;
}

View File

@ -378,7 +378,7 @@ BOOL OOBooleanFromObject(id object, BOOL defaultValue);
accepted by OOBooleanFromObject(), or a number indicating probability of
a yes (between 0 and 1).
*/
BOOL OOFuzzyBooleanFromObject(id object, BOOL defaultValue);
BOOL OOFuzzyBooleanFromObject(id object, float defaultValue);
#endif

View File

@ -1195,6 +1195,26 @@ static BOOL BooleanFromString(NSString *string, BOOL defaultValue)
}
static float FuzzyBooleanProbabilityFromString(NSString *string, float defaultValue)
{
if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] ||
NSOrderedSame == [string caseInsensitiveCompare:@"true"] ||
NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
[string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
{
return 1.0f;
}
else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] ||
NSOrderedSame == [string caseInsensitiveCompare:@"false"] ||
NSOrderedSame == [string caseInsensitiveCompare:@"off"] ||
IsZeroString(string))
{
return 0.0f;
}
return defaultValue;
}
BOOL OOBooleanFromObject(id object, BOOL defaultValue)
{
BOOL result;
@ -1215,22 +1235,24 @@ BOOL OOBooleanFromObject(id object, BOOL defaultValue)
#ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
BOOL OOFuzzyBooleanFromObject(id object, BOOL defaultValue)
BOOL OOFuzzyBooleanFromObject(id object, float defaultValue)
{
float probability;
if ([object isKindOfClass:[NSString class]])
{
probability = [object floatValue];
if (probability == 0.0)
// If our string represents zero, it might be erroneous input or simply yes/no,
// true/false or on/off valid boolean strings. Act on it.
if (probability == 0.0f && !IsZeroString(object))
{
// Treat as normal boolean string
return BooleanFromString(object, defaultValue);
probability = FuzzyBooleanProbabilityFromString(object, defaultValue);
}
}
else
{
probability = OOFloatFromObject(object, defaultValue ? 1.0f : 0.0f);
probability = OOFloatFromObject(object, defaultValue);
}
/* This will always be NO for negative values and YES for values