[Add] Pathfinding avoids unknown systems.

This commit is contained in:
Allanis 2013-06-23 20:42:33 +01:00
parent 10a4791d81
commit 6c3d96ba51
3 changed files with 32 additions and 18 deletions

View File

@ -35,8 +35,6 @@ extern int hyperspace_target;
static void map_close(char* str); static void map_close(char* str);
static void map_update(void); static void map_update(void);
static int map_inPath(StarSystem* sys); static int map_inPath(StarSystem* sys);
static int map_sysReachable(StarSystem* sys);
static int map_sysReachable(StarSystem* sys);
static void map_render(double bx, double by, double w, double h); static void map_render(double bx, double by, double w, double h);
static void map_mouse(SDL_Event* event, double mx, double my); static void map_mouse(SDL_Event* event, double mx, double my);
static void map_buttonZoom(char* str); static void map_buttonZoom(char* str);
@ -150,20 +148,6 @@ static int map_inPath(StarSystem* sys) {
return 0; return 0;
} }
/* Return 1 if player can reach the system. */
static int map_sysReachable(StarSystem* sys) {
int i;
if(sys->known != 0) return 1; /* It is known. */
/* Check to see if it is adjacent to known. */
for(i = 0; i < sys->njumps; i++)
if(systems_stack[sys->jumps[i]].known == 1)
return 1;
return 0;
}
/* Render the map as a custom widget. */ /* Render the map as a custom widget. */
static void map_render(double bx, double by, double w, double h) { static void map_render(double bx, double by, double w, double h) {
int i, j, n, m; int i, j, n, m;
@ -188,7 +172,7 @@ static void map_render(double bx, double by, double w, double h) {
sys = &systems_stack[i]; sys = &systems_stack[i];
/* Check ot make sure system is known of adjacent to known. */ /* Check ot make sure system is known of adjacent to known. */
if(!map_sysReachable(sys)) { if(!space_sysReachable(sys)) {
for(j = 0; j < sys->njumps; j++) for(j = 0; j < sys->njumps; j++)
if(systems_stack[sys->jumps[j]].known == 1) if(systems_stack[sys->jumps[j]].known == 1)
break; break;
@ -287,7 +271,7 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
sys = &systems_stack[i]; sys = &systems_stack[i];
/* Must be reachable. */ /* Must be reachable. */
if(!map_sysReachable(sys)) if(!space_sysReachable(sys))
continue; continue;
/* Get position. */ /* Get position. */

View File

@ -305,6 +305,19 @@ StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) {
ssys = system_get(sysstart); /* Start. */ ssys = system_get(sysstart); /* Start. */
esys = system_get(sysend); /* End. */ esys = system_get(sysend); /* End. */
/* System target must be known. */
if(esys->known == 0) {
if(space_sysReachable(esys)) { /* Can we still reach it? */
res = malloc(sizeof(StarSystem*));
(*njumps) = 1;
res[0] = esys;
return res;
}
/* Can't reach - Don't make path. */
(*njumps) = 0;
return NULL;
}
/* Start the linked lists. */ /* Start the linked lists. */
open = closed = NULL; open = closed = NULL;
cur = A_newNode(ssys, NULL); cur = A_newNode(ssys, NULL);
@ -318,6 +331,9 @@ StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) {
for(i = 0; i < cur->sys->njumps; i++) { for(i = 0; i < cur->sys->njumps; i++) {
sys = &systems_stack[cur->sys->jumps[i]]; sys = &systems_stack[cur->sys->jumps[i]];
if(sys->known == 0) continue;
neighbour = A_newNode(sys, NULL); neighbour = A_newNode(sys, NULL);
ocost = A_in(open, sys); ocost = A_in(open, sys);
@ -474,6 +490,19 @@ char* space_getRndPlanet(void) {
return res; return res;
} }
/* Return 1 if player can reach the system. */
int space_sysReachable(StarSystem* sys) {
int i;
if(sys->known != 0) return 1; /* It is known. */
/* Check to see if it is adjacent to known. */
for(i = 0; i < sys->njumps; i++)
if(systems_stack[sys->jumps[i]].known == 1)
return 1;
return 0;
}
/* Get the system from it's name. */ /* Get the system from it's name. */
static StarSystem* system_get(const char* sysname) { static StarSystem* system_get(const char* sysname) {
int i; int i;

View File

@ -118,6 +118,7 @@ void space_update(const double dt);
StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend); StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend);
int space_canHyperspace(Pilot* p); int space_canHyperspace(Pilot* p);
int space_hyperspace(Pilot* p); int space_hyperspace(Pilot* p);
int space_sysReachable(StarSystem* sys);
char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions); char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions);
char* space_getRndPlanet(void); char* space_getRndPlanet(void);
void space_clearKnown(void); void space_clearKnown(void);