/** * @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 Adds some mission cargo to the player. He cannot sell it nor get rid of it * unless he abandons the mission in which case it'll get eliminated. * * @luaparam cargo Name of the cargo to add. * @luaparam quantity Quantity of cargo to add. * @luareturn The id of the cargo which can be used in cargoRm. * @luafunc cargoAdd( cargo, quantity ) */ static int misn_cargoAdd( lua_State *L ) { const char *cname; Commodity *cargo; int quantity, ret; Mission *cur_mission; /* Parameters. */ cname = luaL_checkstring(L,1); quantity = luaL_checkint(L,2); cargo = commodity_get( cname ); /* Check if the cargo exists. */ if(cargo == NULL) { NLUA_ERROR(L, "Cargo '%s' not found.", cname); return 0; } cur_mission = misn_getFromLua(L); /* First try to add the cargo. */ ret = pilot_addMissionCargo( player.p, cargo, quantity ); mission_linkCargo( cur_mission, ret ); lua_pushnumber(L, ret); return 1; }
/** * @brief Makes sure the commodity is valid or raises a Lua error. * * @param L State currently running. * @param ind Index of the commodity to validate. * @return The commodity (doesn't return if fails - raises Lua error ). */ Commodity* luaL_validcommodity( lua_State *L, int ind ) { Commodity *o; if (lua_iscommodity(L, ind)) o = luaL_checkcommodity(L, ind); else if (lua_isstring(L, ind)) o = commodity_get( lua_tostring(L, ind) ); else { luaL_typerror(L, ind, COMMODITY_METATABLE); return NULL; } if (o == NULL) NLUA_ERROR(L, "Commodity is invalid."); return o; }
/** * @brief Adds some mission cargo to the player. He cannot sell it nor get rid of it * unless he abandons the mission in which case it'll get eliminated. * * @luaparam cargo Name of the cargo to add. * @luaparam quantity Quantity of cargo to add. * @luareturn The id of the cargo which can be used in rmCargo. * @luafunc addCargo( cargo, quantity ) */ static int misn_addCargo( lua_State *L ) { const char *cname; Commodity *cargo; int quantity, ret; /* Parameters. */ cname = luaL_checkstring(L,1); quantity = luaL_checkint(L,2); cargo = commodity_get( cname ); /* First try to add the cargo. */ ret = pilot_addMissionCargo( player, cargo, quantity ); mission_linkCargo( cur_mission, ret ); lua_pushnumber(L, ret); return 1; }
/** * @brief Gets a commodity. * * @usage s = commodity.get( "Food" ) -- Gets the food commodity * * @luaparam s Name of the commodity to get. * @luareturn The commodity matching name or nil if error. * @luafunc get( s ) */ static int commodityL_get( lua_State *L ) { const char *name; Commodity *commodity; /* Handle parameters. */ name = luaL_checkstring(L,1); /* Get commodity. */ commodity = commodity_get( name ); if (commodity == NULL) { NLUA_ERROR(L,"Commodity '%s' not found!", name); return 0; } /* Push. */ lua_pushcommodity(L, commodity); return 1; }
/** * @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); }
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; }