16#include "land_trade.h"
25#include "player_fleet.h"
37static void commodity_exchange_genList(
unsigned int wid );
39static void commodity_exchange_modifiers(
unsigned int wid )
43 char buf[STRMAX_SHORT];
45 snprintf( buf,
sizeof( buf ), _(
"Buy (%d %s)" ), q, UNIT_MASS );
46 window_buttonCaption( wid,
"btnCommodityBuy", buf );
47 snprintf( buf,
sizeof( buf ), _(
"Sell (%d %s)" ), q, UNIT_MASS );
48 window_buttonCaption( wid,
"btnCommoditySell", buf );
53static int commodity_exchange_events(
unsigned int wid, SDL_Event *evt )
55 if ( ( evt->type == SDL_KEYDOWN ) || ( evt->type == SDL_KEYUP ) )
56 commodity_exchange_modifiers( wid );
65 int w, h, iw, dw, bw, titleHeight, infoHeight;
66 char buf[STRMAX_SHORT];
71 land_tabGenerate( LAND_WINDOW_COMMODITY );
78 iw = 565 + ( w - LAND_WIDTH );
82 bw =
MIN( LAND_BUTTON_WIDTH, ( dw - 40 ) / 3 );
83 snprintf( buf,
sizeof( buf ), _(
"Buy (%d %s)" ), q, UNIT_MASS );
84 window_addButtonKey( wid, 40 + iw, 20, bw, LAND_BUTTON_HEIGHT,
86 snprintf( buf,
sizeof( buf ), _(
"Sell (%d %s)" ), q, UNIT_MASS );
87 window_addButtonKey( wid, 60 + iw + bw, 20, bw, LAND_BUTTON_HEIGHT,
89 window_addButtonKey( wid, 80 + iw + 2 * bw, 20, bw, LAND_BUTTON_HEIGHT,
90 "btnCommodityClose", _(
"Take Off" ),
98 window_addRect( wid, -20, -40, 192, 192,
"rctStore", &cBlack, 0 );
99 window_addImage( wid, -20, -40, 192, 192,
"imgStore", NULL, 1 );
104 window_addText( wid, 40 + iw, -40, dw, titleHeight, 0,
"txtName",
107 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"%s",
108 (
player.fleet_capacity > 0 ) ? _(
"Your fleet has:" )
109 : _(
"You have:" ) );
110 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s",
111 (
player.fleet_capacity > 0 ) ? _(
"Free Space (fleet):" )
112 : _(
"Free Space:" ) );
113 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", _(
"Money:" ) );
114 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", _(
"Market Price:" ) );
115 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s",
116 _(
"Average price here:" ) );
117 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s",
118 _(
"Average galactic price:" ) );
119 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", _(
"Purchased for:" ) );
121 window_addText( wid, 40 + iw, -60 - titleHeight, 200, infoHeight, 0,
123 window_addText( wid, 40 + iw + 224, -60 - titleHeight,
124 dw - ( 200 + 20 + 192 ), infoHeight, 0,
"txtDInfo",
127 window_addText( wid, 40 + iw, -80 - titleHeight - infoHeight, dw, 100, 0,
130 wid, 40 + iw,
MIN( -80 - titleHeight - infoHeight, -192 - 60 ), dw,
131 h - ( 80 + titleHeight + infoHeight ) - ( 40 + LAND_BUTTON_HEIGHT ), 0,
135 commodity_exchange_genList( wid );
141static void commodity_exchange_genList(
unsigned int wid )
145 ImageArrayCell *cgoods;
146 int w, h, iw, ih, j, iconsize;
153 iw = 565 + ( w - LAND_WIDTH );
160 toolkit_saveImageArrayData( wid,
"iarTrade", &idat );
166 for (
int i = 0; i <
array_size( pclist ); i++ ) {
170 if ( !commodity_isFlag( pc->
commodity, COMMODITY_FLAG_ALWAYS_CAN_SELL ) )
175 cgoods = calloc( ngoods,
sizeof( ImageArrayCell ) );
176 if ( commodity_list != NULL )
177 free( commodity_list );
178 commodity_list = malloc( ngoods *
sizeof(
Commodity * ) );
182 for (
int i = 0; i <
array_size( pclist ); i++ ) {
187 COMMODITY_FLAG_ALWAYS_CAN_SELL ) )
200 cgoods[j].caption = strdup( _( com->
name ) );
202 commodity_list[j] =
land_spob->commodities[i];
207 cgoods = calloc( ngoods,
sizeof( ImageArrayCell ) );
208 cgoods[0].image = NULL;
209 cgoods[0].caption = strdup( _(
"None" ) );
215 if ( !conf.big_icons ) {
216 if ( toolkit_simImageArrayVisibleElements( iw, ih, iconsize, iconsize ) <
219 if ( toolkit_simImageArrayVisibleElements( iw, ih, iconsize, iconsize ) <
223 window_addImageArray( wid, 20, 20, iw, ih,
"iarTrade", iconsize, iconsize,
228 toolkit_loadImageArrayData( wid,
"iarTrade", &idat );
231void commodity_exchange_cleanup(
void )
233 free( commodity_list );
234 commodity_list = NULL;
246 char buf_purchase_price[ECON_CRED_STRLEN], buf_credits[ECON_CRED_STRLEN];
249 credits_t mean, globalmean;
250 double std, globalstd;
251 char buf_mean[ECON_CRED_STRLEN], buf_globalmean[ECON_CRED_STRLEN];
252 char buf_std[ECON_CRED_STRLEN], buf_globalstd[ECON_CRED_STRLEN];
253 char buf_local_price[ECON_CRED_STRLEN];
254 char buf_tonnes_owned[ECON_MASS_STRLEN], buf_tonnes_free[ECON_MASS_STRLEN];
255 int owned, cargo_free;
256 int i = toolkit_getImageArrayPos( wid,
"iarTrade" );
262 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"%s", _(
"N/A" ) );
263 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", buf_tonnes_free );
264 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", buf_credits );
265 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s",
"" );
266 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", _(
"N/A" ) );
267 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", _(
"N/A" ) );
268 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", _(
"N/A" ) );
269 window_modifyText( wid,
"txtDInfo", buf );
270 window_modifyText( wid,
"txtDesc", _(
"No commodities available." ) );
271 window_disableButton( wid,
"btnCommodityBuy" );
272 window_disableButton( wid,
"btnCommoditySell" );
275 com = commodity_list[i];
278 window_modifyImage( wid,
"imgStore", com->
gfx_store, 192, 192 );
282 snprintf( buf_std,
sizeof( buf_std ), _(
"%.1f ¤" ),
286 snprintf( buf_globalstd,
sizeof( buf_globalstd ), _(
"%.1f ¤" ),
289 buf_purchase_price[0] =
'\0';
295 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"%s", buf_tonnes_owned );
296 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", buf_tonnes_free );
297 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", buf_credits );
298 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n" );
299 l +=
scnprintf( &buf[l],
sizeof( buf ) - l, _(
"%s/t" ), buf_local_price );
300 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n" );
301 l +=
scnprintf( &buf[l],
sizeof( buf ) - l, _(
"%s/t ± %s/t" ), buf_mean,
303 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n" );
304 l +=
scnprintf( &buf[l],
sizeof( buf ) - l, _(
"%s/t ± %s/t" ),
305 buf_globalmean, buf_globalstd );
306 l +=
scnprintf( &buf[l],
sizeof( buf ) - l,
"\n%s", buf_purchase_price );
308 window_modifyText( wid,
"txtDInfo", buf );
309 window_modifyText( wid,
"txtName", _( com->
name ) );
310 window_modifyText( wid,
"txtDesc", _( com->
description ) );
314 if ( commodity_isFlag( com, COMMODITY_FLAG_PRICE_CONSTANT ) ) {
315 l +=
scnprintf( &buf[l],
sizeof( buf ) - l, _(
"Price is constant." ) );
316 window_modifyText( wid,
"txtDRef", buf );
324 &buf[l],
sizeof( buf ) - l,
325 _(
"Price is based on #%c%.0f%%#0 of the price of #o%s#0." ),
c,
327 window_modifyText( wid,
"txtDRef", buf );
329 window_modifyText( wid,
"txtDRef", NULL );
333 window_enableButton( wid,
"btnCommodityBuy" );
335 window_disableButtonSoft( wid,
"btnCommodityBuy" );
338 window_enableButton( wid,
"btnCommoditySell" );
340 window_disableButtonSoft( wid,
"btnCommoditySell" );
348 int failure, incommodities;
349 unsigned int q, price;
350 char buf[ECON_CRED_STRLEN];
369 if (
land_spob->commodities[i] == com ) {
374 if ( !incommodities ) {
412 i = toolkit_getImageArrayPos( wid,
"iarTrade" );
413 com = commodity_list[i];
429 commodity_exchange_genList( wid );
432 hparam[0].
type = HOOK_PARAM_COMMODITY;
434 hparam[1].
type = HOOK_PARAM_NUMBER;
436 hparam[2].
type = HOOK_PARAM_SENTINEL;
438 land_needsTakeoff( 1 );
457 i = toolkit_getImageArrayPos( wid,
"iarTrade" );
458 com = commodity_list[i];
469 price = price * (credits_t)q;
475 commodity_exchange_genList( wid );
478 hparam[0].
type = HOOK_PARAM_COMMODITY;
480 hparam[1].
type = HOOK_PARAM_NUMBER;
482 hparam[2].
type = HOOK_PARAM_SENTINEL;
484 land_needsTakeoff( 1 );
493 SDL_Keymod mods = SDL_GetModState();
495 if ( mods & ( KMOD_LCTRL | KMOD_RCTRL ) )
497 if ( mods & ( KMOD_LSHIFT | KMOD_RSHIFT ) )
499 if ( mods & ( KMOD_LALT | KMOD_RALT ) )
525 snprintf( buf,
sizeof( buf ),
"%dx", q );
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
void credits2str(char *str, credits_t credits, int decimals)
Converts credits to a usable string for displaying.
void tonnes2str(char *str, int tonnes)
Converts tonnes to a usable string for displaying.
int economy_getAveragePrice(const Commodity *com, credits_t *mean, double *std)
Gets the average price of a good as seen by the player (anywhere).
int gl_printHeightRaw(const glFont *ft_font, const int width, const char *text)
Gets the height of a non-formatted string.
int gl_printMidRaw(const glFont *ft_font, int width, double x, double y, const glColour *c, double outlineR, const char *text)
Displays text centered in position and width.
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
void land_errDialogueBuild(const char *fmt,...)
Generates error dialogues used by several landing tabs.
void land_errClear(void)
Clear error dialogues.
void land_buttonTakeoff(unsigned int wid, const char *unused)
Wrapper for takeoff mission button.
int land_errDisplay(void)
Displays an error if applicable.
int commodity_canBuy(const Commodity *com)
Checks to see if the player can buy a commodity.
int commodity_getMod(void)
Gets the current modifier status.
void commodity_exchange_open(unsigned int wid)
Opens the local market window.
void commodity_sell(unsigned int wid, const char *str)
Attempts to sell a commodity.
void commodity_renderMod(double bx, double by, double w, double h, void *data)
Renders the commodity buying modifier.
void commodity_buy(unsigned int wid, const char *str)
Buys the selected commodity.
int commodity_canSell(const Commodity *com)
Checks to see if a player can sell a commodity.
void commodity_update(unsigned int wid, const char *str)
Updates the commodity window.
Header file with generic functions and naev-specifics.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
credits_t player_modCredits(credits_t amount)
Modifies the amount of credits the player has.
int player_hasCredits(credits_t amount)
Checks to see if the player has enough credits.
PilotCommodity * pfleet_cargoList(void)
Gets a list of all the cargo in the fleet.
int pfleet_cargoFree(void)
Gets the total amount of free cargo space in the player's fleet.
int pfleet_cargoOwned(const Commodity *com)
Gets the total amount of a commodity type owned by the player's fleet.
int pfleet_cargoAdd(const Commodity *com, int q)
Adds some cargo to the player's fleet.
int pfleet_cargoRm(const Commodity *com, int q, int jet)
Removes some cargo from the player's fleet.
int spob_averageSpobPrice(const Spob *p, const Commodity *c, credits_t *mean, double *std)
Gets the average price of a commodity at a spob that has been seen so far.
credits_t spob_commodityPrice(const Spob *p, const Commodity *c)
Gets the price of a commodity at a spob.
credits_t lastPurchasePrice
The actual hook parameter.
union HookParam::@065274143236224234262250043114351136253171035204 u
Stores a pilot commodity.
const Commodity * commodity