/** * @brief Attempts to sell a commodity. * @param wid Window selling commodity from. * @param str Unused. */ static void commodity_sell( unsigned int wid, char* str ) { (void)str; char *comname; Commodity *com; unsigned int q; credits_t price; HookParam hparam[3]; /* Get parameters. */ q = commodity_getMod(); comname = toolkit_getList( wid, "lstGoods" ); com = commodity_get( comname ); price = planet_commodityPrice( land_planet, com ); /* Remove commodity. */ q = pilot_cargoRm( player.p, com, q ); price = price * (credits_t)q; player_modCredits( price ); land_checkAddRefuel(); commodity_update(wid, NULL); /* Run hooks. */ hparam[0].type = HOOK_PARAM_STRING; hparam[0].u.str = comname; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = q; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "comm_sell", hparam ); if (land_takeoff) takeoff(1); }
/** * @brief Updates the commodity window. * @param wid Window to update. * @param str Unused. */ static void commodity_update( unsigned int wid, char* str ) { (void)str; char buf[PATH_MAX]; char *comname; Commodity *com; comname = toolkit_getList( wid, "lstGoods" ); if ((comname==NULL) || (strcmp( comname, "None" )==0)) { snprintf( buf, PATH_MAX, "NA Tons\n" "NA Credits/Ton\n" "\n" "NA Tons\n" ); window_modifyText( wid, "txtDInfo", buf ); window_modifyText( wid, "txtDesc", "No outfits available." ); } com = commodity_get( comname ); /* modify text */ snprintf( buf, PATH_MAX, "%d Tons\n" "%"CREDITS_PRI" Credits/Ton\n" "\n" "%d Tons\n", pilot_cargoOwned( player.p, comname ), planet_commodityPrice( land_planet, com ), pilot_cargoFree(player.p)); window_modifyText( wid, "txtDInfo", buf ); window_modifyText( wid, "txtDesc", com->description ); }
/** * @brief Buys the selected commodity. * @param wid Window buying from. * @param str Unused. */ static void commodity_buy( unsigned int wid, char* str ) { (void)str; char *comname; Commodity *com; unsigned int q; credits_t price; HookParam hparam[3]; /* Get selected. */ q = commodity_getMod(); comname = toolkit_getList( wid, "lstGoods" ); com = commodity_get( comname ); price = planet_commodityPrice( land_planet, com ); price *= q; /* Check stuff. */ if (!player_hasCredits( price )) { dialogue_alert( "Insufficient credits!" ); return; } else if (pilot_cargoFree(player.p) <= 0) { dialogue_alert( "Insufficient free space!" ); return; } /* Make the buy. */ q = pilot_cargoAdd( player.p, com, q ); player_modCredits( -price ); land_checkAddRefuel(); commodity_update(wid, NULL); /* Run hooks. */ hparam[0].type = HOOK_PARAM_STRING; hparam[0].u.str = comname; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = q; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "comm_buy", hparam ); if (land_takeoff) takeoff(1); }
/** * @brief Gets the base price of an commodity at a certain planet. * * @usage if o:priceAt( planet.get("Polaris Prime") ) > 100 then -- Checks price of an outfit at polaris prime * * @luaparam o Commodity to get information of. * @luaparam p Planet to get price at. * @luareturn The price of the commodity at the planet. * @luafunc priceAt( o, p ) */ static int commodityL_priceAt( lua_State *L ) { Commodity *c; Planet *p; StarSystem *sys; char *sysname; c = luaL_validcommodity(L,1); p = luaL_validplanet(L,2); sysname = planet_getSystem( p->name ); if (sysname == NULL) { NLUA_ERROR( L, "Planet '%s' does not belong to a system", p->name ); return 0; } sys = system_get( sysname ); if (sys == NULL) { NLUA_ERROR( L, "Planet '%s' can not find its system '%s'", p->name, sysname ); return 0; } lua_pushnumber( L, planet_commodityPrice( p, c ) ); return 1; }
static int commodity_canBuy( char *name ) { int failure; unsigned int q, price; Commodity *com; char buf[ECON_CRED_STRLEN]; failure = 0; q = commodity_getMod(); com = commodity_get( name ); price = planet_commodityPrice( land_planet, com ) * q; if (!player_hasCredits( price )) { credits2str( buf, price - player.p->credits, 2 ); land_errDialogueBuild("You need %s more credits.", buf ); failure = 1; } if (pilot_cargoFree(player.p) <= 0) { land_errDialogueBuild("No cargo space available!"); failure = 1; } return !failure; }