41#define OO_LIKELY_PROPERTIES_PER_SYSTEM 50
43@interface OOSystemDescriptionManager (OOPrivate)
45- (NSDictionary *) calculatePropertiesForSystemKey:(NSString *)key;
46- (void) updateCacheEntry:(NSUInteger)i;
47- (void) updateCacheEntry:(NSUInteger)i forProperty:(NSString *)property;
48- (id) getProperty:(NSString *)property forSystemKey:(NSString *)key withUniversal:(BOOL)universal;
52- (id) getProperty:(NSString *)property1 orProperty:(NSString *)property2 forSystemKey:(NSString *)key withUniversal:(BOOL)universal;
54- (void) saveScriptedChangeToProperty:(NSString *)property forSystemKey:(NSString *)key andLayer:(
OOSystemLayer)layer toValue:(
id)value fromManifest:(NSString *)manifest;
67 universalProperties = [[NSMutableDictionary alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
70 systemDescriptions = [[NSMutableDictionary alloc] initWithCapacity:OO_SYSTEM_CACHE_LENGTH+20];
73 propertyCache[i] = [[NSMutableDictionary alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
76 neighbourCache[i] = [[NSMutableArray alloc] initWithCapacity:24];
78 propertiesInUse = [[NSMutableSet alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
79 scriptedChanges = [[NSMutableDictionary alloc] initWithCapacity:64];
100- (void) buildRouteCache
102 NSUInteger i,j,k,jIndex,kIndex;
106 coordinatesCache[i] =
PointFromString([propertyCache[i] oo_stringForKey:
@"coordinates"]);
121 [neighbourCache[jIndex] addObject:[NSNumber numberWithUnsignedInteger:k]];
122 [neighbourCache[kIndex] addObject:[NSNumber numberWithUnsignedInteger:j]];
131- (void) setUniversalProperties:(NSDictionary *)properties
133 [universalProperties addEntriesFromDictionary:properties];
134 [propertiesInUse addObjectsFromArray:[properties allKeys]];
137 [
self updateCacheEntry:i];
142- (void) setInterstellarProperties:(NSDictionary *)properties
144 [
self setProperties:properties inDescription:interstellarSpace];
148- (void) setProperties:(NSDictionary *)properties forSystemKey:(NSString *)key
155 [systemDescriptions setObject:desc forKey:key];
157 [
self setProperties:properties inDescription:desc];
158 [propertiesInUse addObjectsFromArray:[properties allKeys]];
163 OOGalaxyID g = [tokens oo_unsignedIntegerAtIndex:0];
164 OOSystemID s = [tokens oo_unsignedIntegerAtIndex:1];
168 OOLog(
@"system.description.error",
@"'%@' is an invalid system key. This is an internal error. Please report it.",key);
172 [
self updateCacheEntry:index];
178- (void) setProperty:(NSString *)property forSystemKey:(NSString *)key andLayer:(
OOSystemLayer)layer toValue:(
id)value fromManifest:(NSString *)manifest
185 [systemDescriptions setObject:desc forKey:key];
188 [propertiesInUse addObject:property];
193 [
self saveScriptedChangeToProperty:property forSystemKey:key andLayer:layer toValue:value fromManifest:manifest];
195 OOGalaxyID g = [tokens oo_unsignedIntegerAtIndex:0];
196 OOSystemID s = [tokens oo_unsignedIntegerAtIndex:1];
200 OOLog(
@"system.description.error",
@"'%@' is an invalid system key. This is an internal error. Please report it.",key);
204 [
self updateCacheEntry:index forProperty:property];
210 [
self saveScriptedChangeToProperty:property forSystemKey:key andLayer:layer toValue:value fromManifest:manifest];
215- (void) saveScriptedChangeToProperty:(NSString *)property forSystemKey:(NSString *)key andLayer:(
OOSystemLayer)layer toValue:(
id)value fromManifest:(NSString *)manifest
223 NSArray *overrideKey = [NSArray arrayWithObjects:manifest,key,property,[[NSNumber numberWithInt:layer] stringValue],nil];
226 NSString *overrideKeyStr = [overrideKey componentsJoinedByString:kOOScriptedChangeJoiner];
229 [scriptedChanges setObject:value forKey:overrideKeyStr];
233 [scriptedChanges removeObjectForKey:overrideKeyStr];
238- (void) importScriptedChanges:(NSDictionary *)scripted
241 NSString *keyStr =
nil;
242 NSString *manifest =
nil;
245 key = [keyStr componentsSeparatedByString:kOOScriptedChangeJoiner];
246 if ([key
count] == 4)
248 manifest = [key oo_stringAtIndex:0];
252 [
self setProperty:[key oo_stringAtIndex:2]
253 forSystemKey:[key oo_stringAtIndex:1]
254 andLayer:[key oo_intAtIndex:3]
255 toValue:[scripted objectForKey:keyStr]
256 fromManifest:manifest];
265 OOLog(
@"systemManager.import",
@"Key '%@' has unexpected format - skipping",keyStr);
273- (void) importLegacyScriptedChanges:(NSDictionary *)scripted
275 NSString *systemKey =
nil;
276 NSString *propertyKey =
nil;
277 NSString *defaultManifest =
@"org.oolite.oolite";
281 NSDictionary *legacyChanges = [scripted oo_dictionaryForKey:systemKey];
282 if ([legacyChanges objectForKey:
@"sun_gone_nova"] !=
nil)
288 [
self setProperty:propertyKey
289 forSystemKey:systemKey
290 andLayer:OO_LAYER_OXP_DYNAMIC
291 toValue:[legacyChanges objectForKey:propertyKey]
292 fromManifest:defaultManifest];
296 id sr = [
self getProperty:@"sun_radius" forSystemKey:systemKey];
297 float sr_num = [sr floatValue];
298 if (sr_num < 600000) {
300 [
self setProperty:@"sun_radius"
301 forSystemKey:systemKey
302 andLayer:OO_LAYER_OXP_DYNAMIC
303 toValue:[NSNumber numberWithFloat:sr_num+600000.0f]
304 fromManifest:defaultManifest];
312- (NSDictionary *) exportScriptedChanges
314 return [[scriptedChanges copy] autorelease];
318- (NSDictionary *) getPropertiesForCurrentSystem
326 OOLog(
@"system.description.error",
@"'%lu' is an invalid system index for the current system. This is an internal error. Please report it.",index);
327 return [NSDictionary dictionary];
329 return propertyCache[index];
333 OOLog(
@"system.description.error",
@"%@",
@"getPropertiesForCurrentSystem called while player in interstellar space. This is an internal error. Please report it.");
335 return [NSDictionary dictionary];
340- (NSDictionary *) getPropertiesForSystemKey:(NSString *)key
345 OOGalaxyID g = [tokens oo_unsignedIntegerAtIndex:0];
346 OOSystemID s = [tokens oo_unsignedIntegerAtIndex:1];
350 OOLog(
@"system.description.error",
@"'%@' is an invalid system key. This is an internal error. Please report it.",key);
351 return [NSDictionary dictionary];
353 return propertyCache[index];
356 return [
self calculatePropertiesForSystemKey:key];
365 OOLog(
@"system.description.error",
@"'%u, %u' is an invalid system. This is an internal error. Please report it.",g,s);
366 return [NSDictionary dictionary];
368 return propertyCache[index];
372- (id) getProperty:(NSString *)property forSystemKey:(NSString *)key
374 return [
self getProperty:property forSystemKey:key withUniversal:YES];
381 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
387 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
390 return [propertyCache[index] objectForKey:property];
394- (id) getProperty:(NSString *)property forSystemKey:(NSString *)key withUniversal:(BOOL)universal
397 if (
EXPECT_NOT([key isEqualToString:
@"interstellar"]))
399 desc = interstellarSpace;
403 desc = [systemDescriptions objectForKey:key];
419 if (result ==
nil && universal)
421 result = [universalProperties objectForKey:property];
431- (id) getProperty:(NSString *)property1 orProperty:(NSString *)property2 forSystemKey:(NSString *)key withUniversal:(BOOL)universal
464 result = [universalProperties objectForKey:property1];
468 result = [universalProperties objectForKey:property2];
485 OOSystemLayer layer = [properties oo_unsignedIntForKey:kOOSystemLayerProperty defaultValue:OO_LAYER_OXP_STATIC];
488 OOLog(
@"system.description.error",
@"Layer %u is not a valid layer number in system information.",layer);
496 [propertiesInUse addObject:key];
503- (NSDictionary *) calculatePropertiesForSystemKey:(NSString *)key
505 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
506 NSString *
property =
nil;
508 BOOL interstellar = [key hasPrefix:@"interstellar:"];
509 foreach (property, propertiesInUse)
512 val = [
self getProperty:property forSystemKey:key withUniversal:!interstellar];
516 [dict setObject:val forKey:property];
518 else if (interstellar)
522 val = [
self getProperty:property forSystemKey:@"interstellar"];
525 [dict setObject:val forKey:property];
533- (void) updateCacheEntry:(NSUInteger)i
536 NSString *key = [NSString stringWithFormat:@"%lu %lu",i/OO_SYSTEMS_PER_GALAXY,i%OO_SYSTEMS_PER_GALAXY];
537 NSDictionary *current = [
self calculatePropertiesForSystemKey:key];
539 [propertyCache[i] removeAllObjects];
540 [propertyCache[i] addEntriesFromDictionary:current];
544- (void) updateCacheEntry:(NSUInteger)i forProperty:(NSString *)property
547 NSString *key = [NSString stringWithFormat:@"%lu %lu",i/OO_SYSTEMS_PER_GALAXY,i%OO_SYSTEMS_PER_GALAXY];
548 id current = [
self getProperty:property forSystemKey:key];
551 [propertyCache[i] removeObjectForKey:property];
555 [propertyCache[i] setObject:current forKey:property];
564 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
565 return (NSPoint){0,0};
570 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
571 return (NSPoint){0,0};
573 return coordinatesCache[index];
581 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
587 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
591 return neighbourCache[index];
607 OOLog(
@"system.description.error",
@"'%lu' is an invalid system index for the current system. This is an internal error. Please report it.",(
unsigned long)index);
619 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
625 OOLog(
@"system.description.error",
@"'%d %d' is an invalid system key. This is an internal error. Please report it.",g,s);
635@interface OOSystemDescriptionEntry (OOPrivate)
636- (id) validateProperty:(NSString *)property withValue:(
id)value;
648 layers[i] = [[NSMutableDictionary alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
665- (void) setProperty:(NSString *)property forLayer:(
OOSystemLayer)layer toValue:(
id)value
669 [layers[layer] removeObjectForKey:property];
674 value = [
self validateProperty:property withValue:value];
679 [layers[layer] setObject:value forKey:property];
685- (id) getProperty:(NSString *)property forLayer:(
OOSystemLayer)layer
687 return [layers[layer] objectForKey:property];
696- (id) validateProperty:(NSString *)property withValue:(
id)value
698 if ([property isEqualToString:
@"coordinates"])
702 if (![value isKindOfClass:[NSString
class]])
704 OOLog(
@"system.description.error",
@"'%@' is not a valid format for coordinates",value);
708 if ([tokens
count] != 2)
710 OOLog(
@"system.description.error",
@"'%@' is not a valid format for coordinates (must have exactly two numbers)",value);
714 else if ([property isEqualToString:
@"radius"] || [property isEqualToString:
@"government"])
717 if (![value isKindOfClass:[NSString
class]])
719 if ([value isKindOfClass:[NSNumber
class]])
721 return [value stringValue];
725 OOLog(
@"system.description.error",
@"'%@' is not a valid value for '%@' (string required)",value,property);
730 else if ([property isEqualToString:
@"inhabitant"] || [property isEqualToString:
@"inhabitants"] || [property isEqualToString:
@"name"] )
733 if (![value isKindOfClass:[NSString
class]])
735 OOLog(
@"system.description.error",
@"'%@' is not a valid value for '%@' (string required)",value,property);
#define foreachkey(VAR, DICT)
#define OOLog(class, format,...)
NSPoint PointFromString(NSString *xyString)
NSMutableArray * ScanTokensFromString(NSString *values)
Random_Seed RandomSeedFromString(NSString *abcdefString)
#define OO_GALAXIES_AVAILABLE
#define OO_SYSTEM_CACHE_LENGTH
#define OO_SYSTEMS_PER_GALAXY
static NSString * kOOSystemLayerProperty
static NSString *const kOOScriptedChangeJoiner
id getProperty:forLayer:(NSString *property,[forLayer] OOSystemLayer layer)
void setProperty:forLayer:toValue:(NSString *property,[forLayer] OOSystemLayer layer,[toValue] id value)
const Random_Seed kNilRandomSeed
OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC