/** * @brief Player attempts to buy a ship. * @param wid Window player is buying ship from. * @param str Unused. */ static void shipyard_buy( unsigned int wid, char* str ) { (void)str; char *shipname, buf[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); if (land_errDialogue( shipname, "buyShip" )) return; credits2str( buf, targetprice, 2 ); if (dialogue_YesNo("Are you sure?", /* confirm */ "Do you really want to spend %s on a new ship?", buf )==0) return; /* player just got a new ship */ if (player_newShip( ship, NULL, 0, 0 ) == NULL) { /* Player actually aborted naming process. */ return; } player_modCredits( -targetprice ); /* ouch, paying is hard */ /* Update shipyard. */ shipyard_update(wid, NULL); }
/** * @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 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" ); ship = ship_get( shipname ); credits_t targetprice = ship->price; credits_t playerprice = player_shipPrice(player.p->name); if (land_errDialogue( shipname, "trade" )) 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_checkAddRefuel(); /* Update shipyard. */ shipyard_update(wid, NULL); }
/** * @brief Gives the player a new ship. * * @note Should be given when landed, ideally on a planet with a shipyard. * * @usage player.addShip( "Pirate Kestrel", "Seiryuu" ) -- Gives the player a Pirate Kestrel named Seiryuu if player cancels the naming. * * @luaparam ship Name of the ship to add. * @luaparam name Name to give the ship if player refuses to name it. * @luafunc addShip( ship, name ) */ static int playerL_addShip( lua_State *L ) { const char *str, *name; Ship *s; /* Handle parameters. */ str = luaL_checkstring(L, 1); name = luaL_checkstring(L, 2); /* Get ship. */ s = ship_get(str); if (s==NULL) { NLUA_ERROR(L, "Ship '%s' not found.", str); return 0; } /* Add the ship. */ player_newShip( s, 0., 0, 0., 0., 0., name ); return 0; }
/** * @brief Helper function for playerL_addShip. */ static Pilot* playerL_newShip( lua_State *L ) { const char *str, *name, *pntname; Ship *s; Pilot *new_ship; Planet *pnt, *t; int noname; /* Defaults. */ t = NULL; /* Handle parameters. */ str = luaL_checkstring(L, 1); if (lua_gettop(L) > 1) name = luaL_checkstring(L,2); else name = str; if (lua_gettop(L) > 2) pntname = luaL_checkstring (L,3); else pntname = NULL; noname = lua_toboolean(L,4); /* Get planet. */ if (pntname != NULL) { pnt = planet_get( pntname ); if (pnt == NULL) { NLUA_ERROR(L, "Planet '%s' not found!", pntname); return 0; } /* Horrible hack to swap variables. */ t = land_planet; land_planet = pnt; } else pnt = NULL; /* Must be landed if pnt is NULL. */ if ((pnt == NULL) && (land_planet==NULL)) { NLUA_ERROR(L, "Player must be landed to add a ship without location parameter."); return 0; } /* Get ship. */ s = ship_get(str); if (s==NULL) { NLUA_ERROR(L, "Ship '%s' not found.", str); return 0; } /* Add the ship, look in case it's cancelled. */ do { new_ship = player_newShip( s, name, 0, noname ); } while (new_ship == NULL); /* Undo the horrible hack. */ if (t != NULL) land_planet = t; return new_ship; }