From bdbd7158b994785ba7664a567cda0ad87cd5ae4f Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 24 Apr 2024 10:05:20 -0400 Subject: [PATCH] Dynamically compute the number of stars in the array. --- sc2/src/uqm/hyper.c | 2 +- sc2/src/uqm/plandata.c | 21 +++++++++++++++++++++ sc2/src/uqm/planets/pstarmap.c | 21 ++++++++++++++------- sc2/src/uqm/starmap.c | 4 ++-- sc2/src/uqm/starmap.h | 5 ++++- sc2/src/uqm/state.h | 10 ++++++++-- sc2/src/uqm/uqmdebug.c | 2 +- 7 files changed, 51 insertions(+), 14 deletions(-) diff --git a/sc2/src/uqm/hyper.c b/sc2/src/uqm/hyper.c index 9a9b9f46c..3b7008573 100644 --- a/sc2/src/uqm/hyper.c +++ b/sc2/src/uqm/hyper.c @@ -483,7 +483,7 @@ InterplanetaryTransition (ELEMENT *ElementPtr) COUNT index; const POINT portal_pt[] = QUASISPACE_PORTALS_HYPERSPACE_ENDPOINTS; - index = CurStarDescPtr - &star_array[NUM_SOLAR_SYSTEMS + 1]; + index = CurStarDescPtr - &star_array[GetStarCount() + 1]; GLOBAL_SIS (log_x) = UNIVERSE_TO_LOGX (portal_pt[index].x); GLOBAL_SIS (log_y) = UNIVERSE_TO_LOGY (portal_pt[index].y); diff --git a/sc2/src/uqm/plandata.c b/sc2/src/uqm/plandata.c index a79c2d512..3a3d7308c 100644 --- a/sc2/src/uqm/plandata.c +++ b/sc2/src/uqm/plandata.c @@ -22,6 +22,9 @@ #include "planets/elemdata.h" +// This array has to be sorted in Y coordinate order then X coordinate order. +// Items out of order can cause crashes (better, actually) or "ghost" +// stars in the map that cannot be entered. STAR_DESC starmap_array[] = { // postfix name index (like 'Normae') @@ -572,6 +575,24 @@ STAR_DESC starmap_array[] = {{MAX_X_UNIVERSE << 1, MAX_Y_UNIVERSE << 1}, 0, 0, 0, 0}, }; + +COUNT +GetStarCount( void ) +{ + static bool ready= false; + static COUNT rv= 0; + + while( !ready ) + { + if( starmap_array[ rv ].star_pt.x == MAX_X_UNIVERSE << 1 + && starmap_array[ rv ].star_pt.y == MAX_Y_UNIVERSE << 1 ) break; + ++rv; + } + ready= true; + + return rv; +} + const BYTE element_array[NUMBER_OF_ELEMENTS] = { COMMON, /* HYDROGEN */ diff --git a/sc2/src/uqm/planets/pstarmap.c b/sc2/src/uqm/planets/pstarmap.c index cd3385870..54cad3e10 100644 --- a/sc2/src/uqm/planets/pstarmap.c +++ b/sc2/src/uqm/planets/pstarmap.c @@ -331,7 +331,7 @@ DrawStarMap (COUNT race_update, RECT *pClipRect) } else { - SDPtr = &star_array[NUM_SOLAR_SYSTEMS + 1]; + SDPtr = &star_array[GetStarCount() + 1]; SetContextForeGroundColor ( BUILD_COLOR (MAKE_RGB15 (0x00, 0x0B, 0x00), 0x6D)); SetContextBackGroundColor ( @@ -817,7 +817,7 @@ typedef struct starsearch_state int PrefixLen; int ClusterLen; int ClusterPos; - int SortedStars[NUM_SOLAR_SYSTEMS]; + int *SortedStars; } STAR_SEARCH_STATE; static int @@ -849,10 +849,10 @@ SortStarsOnName (STAR_SEARCH_STATE *pSS) int i; int *sorted = pSS->SortedStars; - for (i = 0; i < NUM_SOLAR_SYSTEMS; i++) + for (i = 0; i < GetStarCount(); i++) sorted[i] = i; - qsort (sorted, NUM_SOLAR_SYSTEMS, sizeof (int), compStarName); + qsort (sorted, GetStarCount(), sizeof (int), compStarName); } static void @@ -917,7 +917,7 @@ SkipStarCluster (int *sortedStars, int istar) { int Postfix = star_array[sortedStars[istar]].Postfix; - for (++istar; istar < NUM_SOLAR_SYSTEMS && + for (++istar; istar < GetStarCount() && star_array[sortedStars[istar]].Postfix == Postfix; ++istar) ; @@ -932,7 +932,7 @@ FindNextStarIndex (STAR_SEARCH_STATE *pSS, int from, BOOLEAN WithinClust) if (!pSS->Cluster) return -1; // nothing to search for - for (i = from; i < NUM_SOLAR_SYSTEMS; ++i) + for (i = from; i < GetStarCount(); ++i) { STAR_DESC *SDPtr = &star_array[pSS->SortedStars[i]]; UNICODE FullName[STAR_SEARCH_BUFSIZE]; @@ -1009,7 +1009,7 @@ FindNextStarIndex (STAR_SEARCH_STATE *pSS, int from, BOOLEAN WithinClust) break; // found one } - return (i < NUM_SOLAR_SYSTEMS) ? i : -1; + return (i < GetStarCount()) ? i : -1; } static void @@ -1177,6 +1177,12 @@ DoStarSearch (MENU_STATE *pMS) pss = HMalloc (sizeof (*pss)); if (!pss) return FALSE; + pss->SortedStars= HMalloc( GetStarCount() * sizeof( int * ) ); + if (!pss->SortedStars) + { + HFree (pss); + return FALSE; + } DrawSISMessageEx ("", 0, 0, DSME_SETFR); @@ -1202,6 +1208,7 @@ DoStarSearch (MENU_STATE *pMS) DrawSISMessageEx (pss->Text, -1, -1, DSME_CLEARFR); + HFree (pss->SortedStars); HFree (pss); return success; diff --git a/sc2/src/uqm/starmap.c b/sc2/src/uqm/starmap.c index a209917bd..6d4801250 100644 --- a/sc2/src/uqm/starmap.c +++ b/sc2/src/uqm/starmap.c @@ -36,12 +36,12 @@ FindStar (STAR_DESC *LastSDPtr, POINT *puniverse, SIZE xbounds, if (GET_GAME_STATE (ARILOU_SPACE_SIDE) <= 1) { BaseSDPtr = star_array; - hi = NUM_SOLAR_SYSTEMS - 1; + hi = GetStarCount() - 1; } else { #define NUM_HYPER_VORTICES 15 - BaseSDPtr = &star_array[NUM_SOLAR_SYSTEMS + 1]; + BaseSDPtr = &star_array[GetStarCount() + 1]; hi = (NUM_HYPER_VORTICES + 1) - 1; } diff --git a/sc2/src/uqm/starmap.h b/sc2/src/uqm/starmap.h index 016183025..ee683b563 100644 --- a/sc2/src/uqm/starmap.h +++ b/sc2/src/uqm/starmap.h @@ -27,13 +27,16 @@ extern "C" { extern STAR_DESC *CurStarDescPtr; extern STAR_DESC *star_array; -#define NUM_SOLAR_SYSTEMS 502 +//#define NUM_SOLAR_SYSTEMS 503 extern STAR_DESC* FindStar (STAR_DESC *pLastStar, POINT *puniverse, SIZE xbounds, SIZE ybounds); extern void GetClusterName (const STAR_DESC *pSD, UNICODE buf[]); +// Defined in plandata.c +extern COUNT GetStarCount (void); + #if defined(__cplusplus) } #endif diff --git a/sc2/src/uqm/state.h b/sc2/src/uqm/state.h index e469cba04..7685d3e9c 100644 --- a/sc2/src/uqm/state.h +++ b/sc2/src/uqm/state.h @@ -54,8 +54,14 @@ typedef struct GAME_STATE_FILE GAME_STATE_FILE; #define STARINFO_FILE 0 //"starinfo.dat" -#define STAR_BUFSIZE (NUM_SOLAR_SYSTEMS * sizeof (DWORD) \ - + 3800 * (3 * sizeof (DWORD))) +// Hack for up to 4k star systems (for now)... +// Each system having up to 15 planets with +// up to 5 moons each. This is somewhat +// overkill.... but I don't want to unspool +// all of the C arrays just yet. Memory +// is cheap, so let it get big... +#define STAR_BUFSIZE (4096 * sizeof (DWORD) \ + + 16 * 5 * 4096 * (3 * sizeof (DWORD))) #define RANDGRPINFO_FILE 1 //"randgrp.dat" #define RAND_BUFSIZE (4 * 1024) diff --git a/sc2/src/uqm/uqmdebug.c b/sc2/src/uqm/uqmdebug.c index 4113a5d82..6ed3da410 100644 --- a/sc2/src/uqm/uqmdebug.c +++ b/sc2/src/uqm/uqmdebug.c @@ -568,7 +568,7 @@ forAllStars (void (*callback) (STAR_DESC *, void *), void *arg) int i; extern STAR_DESC starmap_array[]; - for (i = 0; i < NUM_SOLAR_SYSTEMS; i++) + for (i = 0; i < GetStarCount(); i++) callback (&starmap_array[i], arg); }