diff --git a/src/Core/OOStringParsing.m b/src/Core/OOStringParsing.m index 5b94c718..e0a96a55 100644 --- a/src/Core/OOStringParsing.m +++ b/src/Core/OOStringParsing.m @@ -49,7 +49,6 @@ NSMutableArray *ScanTokensFromString(NSString *values) // Note: Shark suggests we're getting a lot of early exits, but testing showed a pretty steady 2% early exit rate. if (EXPECT_NOT(values == nil)) return [NSArray array]; - if (EXPECT_NOT(space_set == nil)) space_set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] retain]; result = [NSMutableArray array]; @@ -232,7 +231,7 @@ Random_Seed RandomSeedFromString(NSString *abcdefString) else { OOLog(kOOLogStringRandomSeedConversion, @"***** ERROR cannot make Random_Seed from '%@': %@", abcdefString, error); - result = nil_seed(); + result = kNilRandomSeed; } return result; diff --git a/src/Core/ParticleEntity.m b/src/Core/ParticleEntity.m index e0716089..3b2ebee9 100644 --- a/src/Core/ParticleEntity.m +++ b/src/Core/ParticleEntity.m @@ -1282,7 +1282,7 @@ static Vector circleVertex[65]; // holds vector coordinates for a unit circle - (void) updateExhaust2:(double) delta_t { #if ADDITIVE_BLENDING - #define OVERALL_ALPHA 0.3f + #define OVERALL_ALPHA 0.4f #else #define OVERALL_ALPHA 1.0f #endif diff --git a/src/Core/Universe.m b/src/Core/Universe.m index 47c11e15..71e87b8a 100644 --- a/src/Core/Universe.m +++ b/src/Core/Universe.m @@ -5617,7 +5617,7 @@ BOOL maintainLinkedLists(Universe* uni) return systems[i]; } OOLog(kOOLogScriptNoSystemForName, @"SCRIPT ERROR could not find a system with the name '%@' in this galaxy", sysname); - return nil_seed(); + return kNilRandomSeed; } - (NSDictionary *) shipyard @@ -5683,6 +5683,16 @@ BOOL maintainLinkedLists(Universe* uni) - (NSDictionary *) generateSystemData:(Random_Seed) s_seed { + static NSDictionary *cachedResult = nil; + static Random_Seed cachedSeed = {0}; + + // Cache hit ratio is over 95% during respawn, about 80% during initial set-up. + if (EXPECT(cachedResult != nil && equal_seeds(cachedSeed, s_seed))) return cachedResult; + + [cachedResult autorelease]; // Stuff may have references to old value, so don't release right off the bat + cachedResult = nil; + cachedSeed = s_seed; + NSMutableDictionary* systemdata = [[NSMutableDictionary alloc] initWithCapacity:8]; int government = (s_seed.c / 8) & 7; @@ -5723,8 +5733,9 @@ BOOL maintainLinkedLists(Universe* uni) [systemdata addEntriesFromDictionary:(NSDictionary *)[planetinfo objectForKey:override_key]]; if ([local_planetinfo_overrides objectForKey:override_key]) [systemdata addEntriesFromDictionary:(NSDictionary *)[local_planetinfo_overrides objectForKey:override_key]]; - - return [NSDictionary dictionaryWithDictionary:[systemdata autorelease]]; + + cachedResult = [systemdata copy]; + return cachedResult; } - (NSDictionary *) currentSystemData diff --git a/src/Core/legacy_random.c b/src/Core/legacy_random.c index 00154e58..bbfb5b84 100644 --- a/src/Core/legacy_random.c +++ b/src/Core/legacy_random.c @@ -29,8 +29,14 @@ MA 02110-1301, USA. #include #include "legacy_random.h" + +const Random_Seed kNilRandomSeed = {0}; + + static struct random_seed rnd_seed; + +// TODO: Why is this based on a static? Should change to MungeCheckSum(&checkSum, value); static int checksum; void clear_checksum() { @@ -92,25 +98,6 @@ inline int ranrot_rand() m_low += m_high; return m_high & 0x7FFFFFFF; } - -// a method used to determine interplanetary distances, -// if accurate, it has to scale distance down by a factor of 7.15:7.0 -// to allow routes navigable in the original! -double distanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2) -{ - int dx = x1 - x2; - int dy = (y1 - y2)/2; - int dist = sqrt(dx*dx + dy*dy); // here's where the rounding errors come in! - return 0.4*dist; -} - -double accurateDistanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2) -{ - double dx = x1 - x2; - double dy = (y1 - y2)/2; - double dist = sqrt(dx*dx + dy*dy); // here's where the rounding errors come in! - return 0.4*dist; -} void seed_for_planet_description (Random_Seed s_seed) { @@ -194,17 +181,9 @@ void make_pseudo_random_seed (struct rand_seed_6uc *seed_ptr) Random_Seed nil_seed() { - Random_Seed result = { (unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)0}; - return result; + return kNilRandomSeed; } -int is_nil_seed(Random_Seed a_seed) -{ - if (a_seed.a | a_seed.b | a_seed.c | a_seed.d | a_seed.e | a_seed.f) - return 0; - else - return -1; -} void rotate_seed (struct rand_seed_6uc *seed_ptr) { @@ -239,15 +218,3 @@ void rotate_seed (struct rand_seed_6uc *seed_ptr) seed_ptr->e = x; seed_ptr->f = y; } - -int rotate_byte_left (int x) -{ - return ((x << 1) | (x >> 7)) & 255; -} - - -int equal_seeds (Random_Seed seed1, Random_Seed seed2) -{ - return ((seed1.a == seed2.a)&&(seed1.b == seed2.b)&&(seed1.c == seed2.c)&&(seed1.d == seed2.d)&&(seed1.e == seed2.e)&&(seed1.f == seed2.f)); -} - diff --git a/src/Core/legacy_random.h b/src/Core/legacy_random.h index 89fd608a..bda2c6a7 100644 --- a/src/Core/legacy_random.h +++ b/src/Core/legacy_random.h @@ -28,6 +28,8 @@ MA 02110-1301, USA. #ifndef LEGACY_RANDOM_H #define LEGACY_RANDOM_H +#import "OOFunctionAttributes.h" + struct rand_seed_6uc { @@ -51,13 +53,17 @@ struct random_seed typedef struct random_seed RNG_Seed; + +extern const Random_Seed kNilRandomSeed; + + // checksum stuff void clear_checksum(); int munge_checksum(int value); // cunning price rounding routine: // -float cunningFee(float value); +float cunningFee(float value) CONST_FUNC; // an implementation of RANROT // pseudo random number generator @@ -65,13 +71,13 @@ float cunningFee(float value); inline void ranrot_srand(unsigned int seed); inline int ranrot_rand(); -double distanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2); -double accurateDistanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2); +OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC; +OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC; -void seed_for_planet_description (Random_Seed s_seed); -void seed_RNG_only_for_planet_description (Random_Seed s_seed); -RNG_Seed currentRandomSeed (void); -void setRandomSeed (RNG_Seed a_seed); +void seed_for_planet_description(Random_Seed s_seed); +void seed_RNG_only_for_planet_description(Random_Seed s_seed); +RNG_Seed currentRandomSeed(void); +void setRandomSeed(RNG_Seed a_seed); inline float randf (void); inline float bellf (int n); @@ -80,12 +86,54 @@ int gen_rnd_number (void); void make_pseudo_random_seed (struct rand_seed_6uc *seed_ptr); -Random_Seed nil_seed(); -int is_nil_seed(Random_Seed a_seed); +Random_Seed nil_seed() DEPRECATED_FUNC; +OOINLINE int is_nil_seed(Random_Seed a_seed) INLINE_CONST_FUNC; void rotate_seed (struct rand_seed_6uc *seed_ptr); -int rotate_byte_left (int x); +OOINLINE int rotate_byte_left (int x) INLINE_CONST_FUNC; -int equal_seeds (Random_Seed seed1, Random_Seed seed2); +OOINLINE int equal_seeds(Random_Seed seed1, Random_Seed seed2) INLINE_CONST_FUNC; + + + +/*** Only inline definitions beyond this point ***/ + +OOINLINE int equal_seeds(Random_Seed seed1, Random_Seed seed2) +{ + return ((seed1.a == seed2.a)&&(seed1.b == seed2.b)&&(seed1.c == seed2.c)&&(seed1.d == seed2.d)&&(seed1.e == seed2.e)&&(seed1.f == seed2.f)); +} + + +OOINLINE int is_nil_seed(Random_Seed a_seed) +{ + return equal_seeds(a_seed, kNilRandomSeed); +} + + +OOINLINE int rotate_byte_left(int x) +{ + return ((x << 1) | (x >> 7)) & 255; +} + + +// a method used to determine interplanetary distances, +// if accurate, it has to scale distance down by a factor of 7.15:7.0 +// to allow routes navigable in the original! +OOINLINE double distanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2) +{ + int dx = x1 - x2; + int dy = (y1 - y2)/2; + int dist = sqrtf(dx*dx + dy*dy); // here's where the rounding errors come in! + return 0.4 * dist; +} + + +OOINLINE double accurateDistanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2) +{ + double dx = x1 - x2; + double dy = (y1 - y2) / 2.0; + double dist = sqrt(dx*dx + dy*dy); // here's where the rounding errors come in! + return 0.4 * dist; +} #endif