diff --git a/src/land.c b/src/land.c index 3d387a3..f03830c 100644 --- a/src/land.c +++ b/src/land.c @@ -41,7 +41,7 @@ int landed = 0; static int land_wid = 0; // Primary land window. // For the second opened land window (We can only have 2 max). static int secondary_wid = 0; -static Planet* planet = NULL; +Planet* land_planet = NULL; // Commodity excahnge. static void commodity_exchange(void); @@ -102,13 +102,14 @@ static void commodity_exchange(void) { window_addText(secondary_wid, -40, -100, BUTTON_WIDTH-20, BUTTON_WIDTH, 0, "txtDesc", &gl_smallFont, &cBlack, NULL); - goods = malloc(sizeof(char*)*planet->ncommodities); - for(i = 0; i < planet->ncommodities; i++) - goods[i] = strdup(planet->commodities[i]->name); + goods = malloc(sizeof(char*)*land_planet->ncommodities); + for(i = 0; i < land_planet->ncommodities; i++) + goods[i] = strdup(land_planet->commodities[i]->name); window_addList(secondary_wid, 20, -40, - COMMODITY_WIDTH-BUTTON_WIDTH-60, COMMODITY_HEIGHT-80-BUTTON_HEIGHT, - "lstGoods", goods, planet->ncommodities, 0, commodity_update); + COMMODITY_WIDTH-BUTTON_WIDTH-60, + COMMODITY_HEIGHT-80-BUTTON_HEIGHT, "lstGoods", + goods, land_planet->ncommodities, 0, commodity_update); commodity_update(NULL); } @@ -185,7 +186,7 @@ static void outfits(void) { int noutfits; char buf[128]; - snprintf(buf, 128, "%s - Outfits", planet->name); + snprintf(buf, 128, "%s - Outfits", land_planet->name); secondary_wid = window_create(buf, -1, -1, OUTFITS_WIDTH, OUTFITS_HEIGHT); @@ -229,7 +230,7 @@ static void outfits(void) { &gl_smallFont, NULL, NULL); // Set up the outfits to buy/sell. - outfits = outfit_getTech(&noutfits, planet->tech, PLANET_TECH_MAX); + outfits = outfit_getTech(&noutfits, land_planet->tech, PLANET_TECH_MAX); window_addList(secondary_wid, 20, 40, 200, OUTFITS_HEIGHT-80, "lstOutfits", outfits, noutfits, 0, outfits_update); @@ -371,7 +372,7 @@ static void shipyard(void) { int nships; char buf[128]; - snprintf(buf, 128, "%s - Shipyard", planet->name); + snprintf(buf, 128, "%s - Shipyard", land_planet->name); secondary_wid = window_create(buf, -1, -1, SHIPYARD_WIDTH, SHIPYARD_HEIGHT); @@ -411,7 +412,7 @@ static void shipyard(void) { &gl_smallFont, NULL, NULL); // Setup the ships to buy/sell. - ships = ship_getTech(&nships, planet->tech, PLANET_TECH_MAX); + ships = ship_getTech(&nships, land_planet->tech, PLANET_TECH_MAX); window_addList(secondary_wid, 20, 40, 200, SHIPYARD_HEIGHT-80, "lstShipyard", ships, nships, 0, shipyard_update); @@ -482,7 +483,8 @@ static void spaceport_bar(void) { window_addText(secondary_wid, 20, -30, BAR_WIDTH-40, BAR_HEIGHT - 40 - BUTTON_HEIGHT, 0, - "txtDescription", &gl_smallFont, &cBlack, planet->bar_description); + "txtDescription", &gl_smallFont, &cBlack, + land_planet->bar_description); window_addButton(secondary_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseBar", "Close", spaceport_bar_close); @@ -526,7 +528,7 @@ void land(Planet* p) { music_load(MUSIC_LAND); music_play(); - planet = p; + land_planet = p; land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT); // Pretty display. @@ -537,25 +539,26 @@ void land(Planet* p) { window_addButton(land_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnTakeoff", "Takeoff", (void(*)(char*))takeoff); - if(planet_hasService(planet, PLANET_SERVICE_COMMODITY)) + if(planet_hasService(land_planet, PLANET_SERVICE_COMMODITY)) window_addButton(land_wid, -20, 20 + BUTTON_HEIGHT + 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnCommodity", "Commodity Exchange", (void(*)(char*))commodity_exchange); - if(planet_hasService(planet, PLANET_SERVICE_SHIPYARD)) + if(planet_hasService(land_planet, PLANET_SERVICE_SHIPYARD)) window_addButton(land_wid, -20 - BUTTON_WIDTH - 20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnShipyard", "Shipyard", (void(*)(char*))shipyard); - if(planet_hasService(planet, PLANET_SERVICE_OUTFITS)) + if(planet_hasService(land_planet, PLANET_SERVICE_OUTFITS)) window_addButton(land_wid, -20 - BUTTON_WIDTH - 20, 20 + BUTTON_HEIGHT + 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnOutfits", "Outfits", (void(*)(char*))outfits); - if(planet_hasService(planet, PLANET_SERVICE_BASIC)) { + if(planet_hasService(land_planet, PLANET_SERVICE_BASIC)) { window_addButton(land_wid, 20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnNews", "Mission Terminal", NULL); + window_addButton(land_wid, 20, 20 + BUTTON_HEIGHT + 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnBar", "Spaceport Bar", (void(*)(char*))spaceport_bar); @@ -573,14 +576,15 @@ void takeoff(void) { music_play(); int sw, sh; - sw = planet->gfx_space->w; - sh = planet->gfx_space->h; + sw = land_planet->gfx_space->w; + sh = land_planet->gfx_space->h; // No longer authorized to land. player_rmFlag(PLAYER_LANDACK); // Set player to another position with random facing direction and no velocity. - player_warp(planet->pos.x + RNG(-sw/2, sw/2), planet->pos.y + RNG(-sh/2, sh/2)); + player_warp(land_planet->pos.x + RNG(-sw/2, sw/2), + land_planet->pos.y + RNG(-sh/2, sh/2)); vect_pset(&player->solid->vel, 0., 0.); player->solid->dir = RNG(0, 359) * M_PI/180.; @@ -591,7 +595,7 @@ void takeoff(void) { space_init(NULL); - planet = NULL; + land_planet = NULL; window_destroy(land_wid); landed = 0; } diff --git a/src/lephisto.c b/src/lephisto.c index 6c99c19..222485a 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -205,14 +205,15 @@ int main(int argc, char** argv) { } // Unload data. - weapon_exit(); // Destroy all active weapons. - space_exit(); // Clean up the universe!!! - pilots_free(); // Free the pilots, they where locked up D: - gui_free(); // Free up the gui. + player_cleanup(); // Cleans up the player stuff. + weapon_exit(); // Destroy all active weapons. + space_exit(); // Clean up the universe!!! + pilots_free(); // Free the pilots, they where locked up D: + gui_free(); // Free up the gui. fleet_free(); ships_free(); outfit_free(); - spfx_free(); // Remove the special effects. + spfx_free(); // Remove the special effects. factions_free(); commodity_free(); gl_freeFont(NULL); @@ -224,8 +225,8 @@ int main(int argc, char** argv) { joystick_exit(); // Release joystick. input_exit(); // Clean up keybindings. gl_exit(); // Kills video output. - sound_exit(); // Kills the sound. - SDL_Quit(); // Quits SDL. + sound_exit(); // Kills the sound. + SDL_Quit(); // Quits SDL. // All is good. exit(EXIT_SUCCESS); diff --git a/src/pilot.c b/src/pilot.c index d0f8024..afd37f8 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -49,7 +49,7 @@ static void pilot_update(Pilot* pilot, const double dt); static void pilot_hyperspace(Pilot* pilot); void pilot_render(Pilot* pilot); static void pilot_calcStats(Pilot* pilot); -static void pilot_free(Pilot* p); +void pilot_free(Pilot* p); static Fleet* fleet_parse(const xmlNodePtr parent); static void pilot_dead(Pilot* p); static int pilot_oquantity(Pilot* p, PilotOutfit* w); @@ -749,7 +749,8 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, } // Copy src pilot to dest. -void pilot_copy(Pilot* dest, Pilot* src) { +Pilot* pilot_copy(Pilot* src) { + Pilot* dest = malloc(sizeof(Pilot)); memcpy(dest, src, sizeof(Pilot)); if(src->name) dest->name = strdup(src->name); @@ -775,10 +776,12 @@ void pilot_copy(Pilot* dest, Pilot* src) { // Will set afterburner and correct stats. pilot_calcStats(dest); + + return dest; } // Frees and cleans up a pilot. -static void pilot_free(Pilot* p) { +void pilot_free(Pilot* p) { if(player == p) player = NULL; solid_free(p->solid); if(p->outfits) free(p->outfits); diff --git a/src/pilot.h b/src/pilot.h index 1187af7..ad43473 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -149,12 +149,13 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile* ai, const double dir, const Vec2* pos, const Vec2* vel, const int flags); -void pilot_copy(Pilot* dest, Pilot* src); +Pilot* pilot_copy(Pilot* src); // Init/Cleanup. void pilot_destroy(Pilot* p); void pilots_free(void); void pilots_clean(void); +void pilot_free(Pilot* p); int fleet_load(void); // TODO void fleet_free(void); diff --git a/src/player.c b/src/player.c index cdeef3a..9117ec4 100644 --- a/src/player.c +++ b/src/player.c @@ -32,6 +32,10 @@ Pilot* player = NULL; // extern in pilot.h static Ship* player_ship = NULL; // Temp ship to hold when naming it. // More hacks. static double player_px, player_py, player_vx, player_vy, player_dir; +// Player pilot stack - Ships she owns. +static Pilot** player_stack = NULL; +static char** player_lstack = NULL; // Names of the planet the ships are at. +static int player_nstack = 0; // Player global properties. char* player_name = NULL; // Player name. int player_credits = 0; // Ze monies. @@ -55,6 +59,9 @@ extern int pilots; // Space stuff for GUI. extern StarSystem* systems_stack; +// Land stuff for the player_stack. +extern Planet* land_planet; + // GUI crap. typedef struct Radar_ { double x,y; // Position. @@ -135,10 +142,14 @@ void player_destroyed(void); void player_new(void) { unsigned int wid; + // Let's not seg fault due to a lack of environment. player_setFlag(PLAYER_DESTROYED); vectnull(&player_cam); gl_bindCamera(&player_cam); + // Cleanup player stuff if we'll be re-creating. + player_cleanup(); + wid = window_create("Player Name", -1, -1, 240, 140); window_addText(wid, 30, -30, 180, 20, 0, "txtInfo", @@ -152,7 +163,6 @@ static void player_nameClose(char* str) { unsigned int wid; wid = window_get("Player Name"); - if(player_name) free(player_name); player_name = strdup(window_getInput(wid, "inpName")); window_destroy(wid); @@ -267,8 +277,14 @@ static void player_nameShipClose(char* str) { // Change the players ship. static void player_newShipMake(char* name) { Vec2 vp, vv; - if(player) + if(player) { + player_stack = realloc(player_stack, sizeof(Pilot*)*(player_nstack+1)); + player_stack[player_nstack] = pilot_copy(player); + player_lstack = realloc(player_lstack, sizeof(char*)*(player_nstack+1)); + player_lstack[player_nstack] = strdup(land_planet->name); + player_nstack++; pilot_destroy(player); + } // In case we're respawning. player_rmFlag(PLAYER_DESTROYED); @@ -282,6 +298,28 @@ static void player_newShipMake(char* name) { gl_bindCamera(&player->solid->pos); // Set opengl camera. } +// Clean up player stuff like player_stack. +void player_cleanup(void) { + int i; + + // Cleanup name. + if(player_name) free(player_name); + + // Clean up the stack. + if(player_stack) { + for(i = 0; i < player_nstack; i++) { + pilot_free(player_stack[i]); + free(player_lstack[i]); + } + free(player_stack); + player_stack = NULL; + free(player_lstack); + player_lstack = NULL; + // Nothing left. + player_nstack = 0; + } +} + void player_message(const char* fmt, ...) { va_list ap; int i; diff --git a/src/player.h b/src/player.h index 49b8f4d..ce529eb 100644 --- a/src/player.h +++ b/src/player.h @@ -29,10 +29,11 @@ extern int combat_crating; // For render functions. typedef enum RadarShape_ { RADAR_RECT, RADAR_CIRCLE } RadarShape; -// Creation. +// Creation/Cleanup. void player_new(void); void player_newShip(Ship* ship, double px, double py, double vx, double vy, double dir); +void player_cleanup(void); // Render. int gui_init(void);