/** * @brief Player attempts to buy a ship, trading the current ship in. * @param wid Window player is buying ship from. * @param str Unused. */ static void shipyard_trade( unsigned int wid, char* str ) { (void)str; char *shipname, buf[ECON_CRED_STRLEN], buf2[ECON_CRED_STRLEN], buf3[ECON_CRED_STRLEN], buf4[ECON_CRED_STRLEN]; Ship* ship; shipname = toolkit_getImageArray( wid, "iarShipyard" ); if (strcmp(shipname, "None") == 0) return; ship = ship_get( shipname ); credits_t targetprice = ship_buyPrice(ship); credits_t playerprice = player_shipPrice(player.p->name); if (land_errDialogue( shipname, "tradeShip" )) return; credits2str( buf, targetprice, 2 ); credits2str( buf2, playerprice, 2 ); credits2str( buf3, targetprice - playerprice, 2 ); credits2str( buf4, playerprice - targetprice, 2 ); /* Display the correct dialogue depending on the new ship's price versus the player's. */ if ( targetprice == playerprice ) { if (dialogue_YesNo("Are you sure?", /* confirm */ "Your %s is worth %s, exactly as much as the new ship, so no credits need be exchanged. Are you sure you want to trade your ship in?", player.p->ship->name, buf2)==0) return; } else if ( targetprice < playerprice ) { if (dialogue_YesNo("Are you sure?", /* confirm */ "Your %s is worth %s credits, more than the new ship. For your ship, you will get the new %s and %s credits. Are you sure you want to trade your ship in?", player.p->ship->name, buf2, ship->name, buf4)==0) return; } else if ( targetprice > playerprice ) { if (dialogue_YesNo("Are you sure?", /* confirm */ "Your %s is worth %s, so the new ship will cost %s credits. Are you sure you want to trade your ship in?", player.p->ship->name, buf2, buf3)==0) return; } /* player just got a new ship */ if (player_newShip( ship, NULL, 1, 0 ) == NULL) return; /* Player aborted the naming process. */ player_modCredits( playerprice - targetprice ); /* Modify credits by the difference between ship values. */ land_refuel(); /* The newShip call will trigger a loadGUI that will recreate the land windows. Therefore the land ID will * be void. We must reload in in order to properly update it again.*/ wid = land_getWid(LAND_WINDOW_SHIPYARD); /* Update shipyard. */ shipyard_update(wid, NULL); }
/** * @brief Recreates the land windows. * * @param load Is loading game? * @param changetab Should it change to the last open tab? */ void land_genWindows( int load, int changetab ) { int i, j; const char *names[LAND_NUMWINDOWS]; int w, h; Planet *p; int regen; /* Destroy old window if exists. */ if (land_wid > 0) { land_regen = 2; /* Mark we're regenning. */ window_destroy(land_wid); /* Mark tabs as not generated. */ land_generated = 0; } land_loaded = 0; /* Get planet. */ p = land_planet; regen = landed; /* Create window. */ if ((gl_screen.rw < 1024) || (gl_screen.rh < 768)) { w = -1; /* Fullscreen. */ h = -1; } else { w = 800 + 0.5 * (SCREEN_W - 800); h = 600 + 0.5 * (SCREEN_H - 600); } land_wid = window_create( p->name, -1, -1, w, h ); window_onClose( land_wid, land_cleanupWindow ); /* Set window map to invalid. */ for (i=0; i<LAND_NUMWINDOWS; i++) land_windowsMap[i] = -1; /* See what is available. */ j = 0; /* Main. */ land_windowsMap[LAND_WINDOW_MAIN] = j; names[j++] = land_windowNames[LAND_WINDOW_MAIN]; /* Bar. */ if (planet_hasService(land_planet, PLANET_SERVICE_BAR)) { land_windowsMap[LAND_WINDOW_BAR] = j; names[j++] = land_windowNames[LAND_WINDOW_BAR]; } /* Missions. */ if (planet_hasService(land_planet, PLANET_SERVICE_MISSIONS)) { land_windowsMap[LAND_WINDOW_MISSION] = j; names[j++] = land_windowNames[LAND_WINDOW_MISSION]; } /* Outfits. */ if (planet_hasService(land_planet, PLANET_SERVICE_OUTFITS)) { land_windowsMap[LAND_WINDOW_OUTFITS] = j; names[j++] = land_windowNames[LAND_WINDOW_OUTFITS]; } /* Shipyard. */ if (planet_hasService(land_planet, PLANET_SERVICE_SHIPYARD)) { land_windowsMap[LAND_WINDOW_SHIPYARD] = j; names[j++] = land_windowNames[LAND_WINDOW_SHIPYARD]; } /* Equipment. */ if (planet_hasService(land_planet, PLANET_SERVICE_OUTFITS) || planet_hasService(land_planet, PLANET_SERVICE_SHIPYARD)) { land_windowsMap[LAND_WINDOW_EQUIPMENT] = j; names[j++] = land_windowNames[LAND_WINDOW_EQUIPMENT]; } /* Commodity. */ if (planet_hasService(land_planet, PLANET_SERVICE_COMMODITY)) { land_windowsMap[LAND_WINDOW_COMMODITY] = j; names[j++] = land_windowNames[LAND_WINDOW_COMMODITY]; } /* Create tabbed window. */ land_windows = window_addTabbedWindow( land_wid, -1, -1, -1, -1, "tabLand", j, names, 0 ); /* * Order here is very important: * * 1) Create main tab - must have decent background. * 2) Set landed, play music and run land hooks - so hooks run well. * 3) Generate missions - so that campaigns are fluid. * 4) Create other tabs - lists depend on NPC and missions. */ /* 1) Create main tab. */ land_createMainTab( land_getWid(LAND_WINDOW_MAIN) ); /* Add local system map button. */ land_checkAddMap(); /* 2) Set as landed and run hooks. */ if (!regen) { landed = 1; music_choose("land"); /* Must be before hooks in case hooks change music. */ if (!load) { hooks_run("land"); } events_trigger( EVENT_TRIGGER_LAND ); /* 3) Generate computer and bar missions. */ if (planet_hasService(land_planet, PLANET_SERVICE_MISSIONS)) mission_computer = missions_genList( &mission_ncomputer, land_planet->faction, land_planet->name, cur_system->name, MIS_AVAIL_COMPUTER ); if (planet_hasService(land_planet, PLANET_SERVICE_BAR)) npc_generate(); /* Generate bar npc. */ } /* 4) Create other tabs. */ #define should_open(s, w) \ (planet_hasService(land_planet, s) && (!land_tabGenerated(w))) /* Things get a bit hairy here. Hooks may have triggered a GUI reload via * e.g. player.swapShip, so the land tabs may have been generated already * and we need to check that before regenerating them. */ /* Basic - bar + missions */ if (should_open( PLANET_SERVICE_BAR, LAND_WINDOW_BAR )) bar_open( land_getWid(LAND_WINDOW_BAR) ); if (should_open( PLANET_SERVICE_MISSIONS, LAND_WINDOW_MISSION )) misn_open( land_getWid(LAND_WINDOW_MISSION) ); /* Outfits. */ if (should_open( PLANET_SERVICE_OUTFITS, LAND_WINDOW_OUTFITS )) outfits_open( land_getWid(LAND_WINDOW_OUTFITS) ); /* Shipyard. */ if (should_open( PLANET_SERVICE_SHIPYARD, LAND_WINDOW_SHIPYARD )) shipyard_open( land_getWid(LAND_WINDOW_SHIPYARD) ); /* Equipment. */ if ((planet_hasService(land_planet, PLANET_SERVICE_OUTFITS) || planet_hasService(land_planet, PLANET_SERVICE_SHIPYARD)) && !land_tabGenerated( LAND_WINDOW_EQUIPMENT )) equipment_open( land_getWid(LAND_WINDOW_EQUIPMENT) ); /* Commodity. */ if (should_open( PLANET_SERVICE_COMMODITY, LAND_WINDOW_COMMODITY )) commodity_exchange_open( land_getWid(LAND_WINDOW_COMMODITY) ); #undef should_open if (!regen) { /* Reset markers if needed. */ mission_sysMark(); /* Check land missions. */ if (!has_visited(VISITED_LAND)) { missions_run(MIS_AVAIL_LAND, land_planet->faction, land_planet->name, cur_system->name); visited(VISITED_LAND); } } /* Go to last open tab. */ window_tabWinOnChange( land_wid, "tabLand", land_changeTab ); if (changetab && land_windowsMap[ last_window ] != -1) window_tabWinSetActive( land_wid, "tabLand", land_windowsMap[ last_window ] ); /* Refresh the map button in case the player couldn't afford it prior to * mission payment. */ land_checkAddMap(); /* Refuel if necessary. */ land_refuel(); /* Finished loading. */ land_loaded = 1; }
/** * @brief Makes the player take off if landed. * * @param delay Whether or not to have time pass as if the player landed normally. */ void takeoff( int delay ) { int h; char *nt; double a, r; if (!landed) return; /* Player's ship is not able to fly. */ if (!player_canTakeoff()) { char message[512]; pilot_reportSpaceworthy( player.p, message, sizeof(message) ); dialogue_msg( "Ship not fit for flight", message ); /* Check whether the player needs rescuing. */ land_stranded(); return; } /* Clear queued takeoff. */ land_takeoff = 0; /* Refuel if needed. */ land_refuel(); /* In case we had paused messy sounds. */ sound_stopAll(); /* ze music */ music_choose("takeoff"); /* to randomize the takeoff a bit */ a = RNGF() * 2. * M_PI; r = RNGF() * land_planet->radius; /* no longer authorized to land */ player_rmFlag(PLAYER_LANDACK); pilot_rmFlag(player.p,PILOT_LANDING); /* No longer landing. */ /* set player to another position with random facing direction and no vel */ player_warp( land_planet->pos.x + r * cos(a), land_planet->pos.y + r * sin(a) ); vect_pset( &player.p->solid->vel, 0., 0. ); player.p->solid->dir = RNGF() * 2. * M_PI; cam_setTargetPilot( player.p->id, 0 ); /* heal the player */ pilot_healLanded( player.p ); /* Clear planet target. Allows for easier autonav out of the system. */ player_targetPlanetSet( -1 ); /* initialize the new space */ h = player.p->nav_hyperspace; space_init(NULL); player.p->nav_hyperspace = h; /* cleanup */ if (save_all() < 0) /* must be before cleaning up planet */ dialogue_alert( "Failed to save game! You should exit and check the log to see what happened and then file a bug report!" ); /* time goes by, triggers hook before takeoff */ if (delay) ntime_inc( ntime_create( 0, 1, 0 ) ); /* 1 STP */ nt = ntime_pretty( 0, 2 ); player_message("\epTaking off from %s on %s.", land_planet->name, nt); free(nt); /* Hooks and stuff. */ land_cleanup(); /* Cleanup stuff */ hooks_run("takeoff"); /* Must be run after cleanup since we don't want the missions to think we are landed. */ if (menu_isOpen(MENU_MAIN)) return; player_addEscorts(); hooks_run("enter"); if (menu_isOpen(MENU_MAIN)) return; events_trigger( EVENT_TRIGGER_ENTER ); missions_run( MIS_AVAIL_SPACE, -1, NULL, NULL ); if (menu_isOpen(MENU_MAIN)) return; player.p->ptimer = PILOT_TAKEOFF_DELAY; pilot_setFlag( player.p, PILOT_TAKEOFF ); pilot_setThrust( player.p, 0. ); pilot_setTurn( player.p, 0. ); }