diff --git a/src/map.c b/src/map.c index 18d640a..2666214 100644 --- a/src/map.c +++ b/src/map.c @@ -15,12 +15,14 @@ #define BUTTON_HEIGHT 40 static int map_wid = 0; -static double map_zoom = 1.; // Zoom of the map. -static double map_xpos = 0.; // Map position. +static double map_zoom = 1.; // Zoom of the map. +static double map_xpos = 0.; // Map position. static double map_ypos = 0.; -static int map_selected = 0; +static int map_selected = -1; +static StarSystem** map_path = NULL; // The path to current selected system. +static int map_npath = 0; -static int map_drag = 0; // Is the user dragging the map? +static int map_drag = 0; // Is the user dragging the map? // Extern. // space.c @@ -32,6 +34,7 @@ extern int hyperspace_target; static void map_close(char* str); static void map_update(void); +static int map_inPath(StarSystem* sys); 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_buttonZoom(char* str); @@ -130,6 +133,15 @@ static void map_update(void) { window_modifyText(map_wid, "txtPlanets", buf); } +// Return 1 if sys is part of the map_path. +static int map_inPath(StarSystem* sys) { + int i; + for(i = 0; i < map_npath; i++) + if(map_path[i] == sys) + return 1; + return 0; +} + // Render the map as a custom widget. static void map_render(double bx, double by, double w, double h) { int i, j; @@ -180,6 +192,9 @@ static void map_render(double bx, double by, double w, double h) { ((cur_system == &systems_stack[sys->jumps[j]]) && (sys = &systems_stack[cur_system->jumps[hyperspace_target]]))) col = &cRed; + // Is the route part of the path? + else if(map_inPath(&systems_stack[sys->jumps[j]]) && map_inPath(sys)) + col = &cGreen; else col = &cDarkBlue; glBegin(GL_LINE_STRIP); ACOLOUR(*col, 0.); @@ -209,6 +224,7 @@ static void map_render(double bx, double by, double w, double h) { static void map_mouse(SDL_Event* event, double mx, double my) { int i, j; double x, y, t; + StarSystem* sys; t = 13.*15.; // Threshold. @@ -225,18 +241,31 @@ static void map_mouse(SDL_Event* event, double mx, double my) { // Selecting star system. else { for(i = 0; i < systems_nstack; i++) { + sys = &systems_stack[i]; + + // Get position. x = systems_stack[i].pos.x * map_zoom; y = systems_stack[i].pos.y * map_zoom; if((pow2(mx-x)+pow2(my-y)) < t) { + // Select the current system and make a path to it. map_selected = i; - for(j = 0; j < cur_system->njumps; j++) { - if(i == cur_system->jumps[j]) { - planet_target = -1; // Override planet_target. - hyperspace_target = j; - break; + if(map_path) + free(map_path); + map_path = system_getJumpPath(&map_npath, + cur_system->name, sys->name); + + if(map_npath == 0) + hyperspace_target = -1; + else + // See if it a valid hyperspace target. + for(j = 0; j < cur_system->njumps; j++) { + if(map_path[0] == &systems_stack[cur_system->jumps[j]]) { + planet_target = -1; // Override planet_target. + hyperspace_target = j; + break; + } } - } map_update(); break; } @@ -271,3 +300,63 @@ static void map_buttonZoom(char* str) { } } +// Set the map to sane defaults. +void map_clear(void) { + int i; + + map_zoom = 1.; + if(cur_system != NULL) { + map_xpos = cur_system->pos.x; + map_ypos = cur_system->pos.y; + } else { + map_xpos = 0.; + map_ypos = 0.; + } + if(map_path != NULL) { + free(map_path); + map_path = NULL; + map_npath = 0; + } + + // Default system is current system. + if(cur_system != NULL) + for(i = 0; i < systems_nstack; i++) + if(&systems_stack[i] == cur_system) { + map_selected = i; + break; + } + else + map_selected = 0; +} + +// Update the map after a jump. +void map_jump(void) { + int j; + + map_xpos = cur_system->pos.x; + map_ypos = cur_system->pos.y; + + // Update path if set. + if(map_path != NULL) { + map_npath--; + if(map_npath == 0) { + // Path is empty. + free(map_path); + map_path = NULL; + } else { + // Get rid of bottom of the path. + memcpy(&map_path[0], &map_path[1], sizeof(StarSystem*) * map_npath); + map_path = realloc(map_path, sizeof(StarSystem*) * map_npath); + + // Set the next jump to be the next in path. + for(j = 0; j < cur_system->njumps; j++) { + if(map_path[0] == &systems_stack[cur_system->jumps[j]]) { + planet_target = -1; // Override planet_target. + hyperspace_target = j; + break; + } + } + } + } +} + diff --git a/src/map.h b/src/map.h index 96c3b22..a150efe 100644 --- a/src/map.h +++ b/src/map.h @@ -1,4 +1,9 @@ #pragma once +// Open the map window. void map_open(void); +// Misc. +void map_clear(void); +void map_jump(void); + diff --git a/src/player.c b/src/player.c index d539ba0..5676d68 100644 --- a/src/player.c +++ b/src/player.c @@ -19,6 +19,7 @@ #include "misn_lua.h" #include "ltime.h" #include "hook.h" +#include "map.h" #include "player.h" #define XML_GUI_ID "GUIs" // XML section identifier. @@ -241,6 +242,9 @@ static void player_newMake(void) { // Create the player and start the game. player_newShip(ship, x, y, 0., 0., RNG(0, 359)/180.*M_PI); space_init(system); + + // Clear the map. + map_clear(); } // Create a dialogue to name the new ship. @@ -1268,6 +1272,10 @@ void player_brokeHyperspace(void) { // Stop hyperspace. pilot_rmFlag(player, PILOT_HYPERSPACE | PILOT_HYP_BEGIN | PILOT_HYP_PREP); + // Update the map. + map_jump(); + + // Run the jump hooks. hooks_run("jump"); player_message("BANG!"); diff --git a/src/space.c b/src/space.c index 6e9f435..45d1af8 100644 --- a/src/space.c +++ b/src/space.c @@ -294,7 +294,7 @@ StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) { // Initial and target systems. ssys = system_get(sysstart); // Start. - ssys = system_get(sysend); // End. + esys = system_get(sysend); // End. // Start the linked lists. open = closed = NULL;