18#include "land_trade.h"
25#include "land_shipyard.h"
31#include "player_fleet.h"
33#include "tk/toolkit_priv.h"
42static void commodity_exchange_modifiers(
unsigned int wid )
46 char buf[STRMAX_SHORT];
48 snprintf( buf,
sizeof(buf), _(
"Buy (%d %s)"), q, UNIT_MASS );
49 window_buttonCaption( wid,
"btnCommodityBuy", buf );
50 snprintf( buf,
sizeof(buf), _(
"Sell (%d %s)"), q, UNIT_MASS );
51 window_buttonCaption( wid,
"btnCommoditySell", buf );
56static int commodity_exchange_events(
unsigned int wid, SDL_Event *evt )
58 if ((evt->type==SDL_KEYDOWN) || (evt->type==SDL_KEYUP))
59 commodity_exchange_modifiers( wid );
69 ImageArrayCell *cgoods;
70 int w, h, iw, ih, dw, bw, titleHeight, infoHeight;
71 char buf[STRMAX_SHORT];
77 land_tabGenerate(LAND_WINDOW_COMMODITY);
84 iw = 565 + (w - LAND_WIDTH);
90 snprintf( buf,
sizeof(buf), _(
"Buy (%d %s)"), q, UNIT_MASS );
91 window_addButtonKey( wid, 40 + iw, 20, bw, LAND_BUTTON_HEIGHT,
93 snprintf( buf,
sizeof(buf), _(
"Sell (%d %s)"), q, UNIT_MASS );
94 window_addButtonKey( wid, 60 + iw + bw, 20, bw, LAND_BUTTON_HEIGHT,
96 window_addButtonKey( wid, 80 + iw + 2*bw, 20, bw, LAND_BUTTON_HEIGHT,
104 window_addRect( wid, -20, -40, 192, 192,
"rctStore", &cBlack, 0 );
105 window_addImage( wid, -20, -40, 192, 192,
"imgStore", NULL, 1 );
109 window_addText( wid, 40 + iw, -40, dw, titleHeight, 0,
114 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"Money:") );
115 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"Market Price:") );
116 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"Average price here:") );
117 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"Average galactic price:") );
118 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"Purchased for:") );
120 window_addText( wid, 40 + iw, -60 - titleHeight, 200, infoHeight, 0,
122 window_addText( wid, 40 + iw + 224, -60 - titleHeight,
123 dw - (200 + 20+192), infoHeight, 0,
126 window_addText( wid, 40 + iw, -80-titleHeight-infoHeight,
128 window_addText( wid, 40 + iw,
MIN(-80-titleHeight-infoHeight, -192-60),
129 dw, h - (80+titleHeight+infoHeight) - (40+LAND_BUTTON_HEIGHT), 0,
141 if (!commodity_isFlag(pc->
commodity, COMMODITY_FLAG_ALWAYS_CAN_SELL))
146 cgoods = calloc( ngoods,
sizeof(ImageArrayCell) );
147 if (commodity_list != NULL)
148 free(commodity_list);
149 commodity_list = malloc( ngoods*
sizeof(
Commodity*) );
157 if (!commodity_isFlag(pc->
commodity, COMMODITY_FLAG_ALWAYS_CAN_SELL))
175 cgoods = calloc( ngoods,
sizeof(ImageArrayCell) );
176 cgoods[0].image = NULL;
177 cgoods[0].caption = strdup(_(
"None"));
184 if (toolkit_simImageArrayVisibleElements(iw,ih,iconsize,iconsize) < ngoods)
186 if (toolkit_simImageArrayVisibleElements(iw,ih,iconsize,iconsize) < ngoods)
189 window_addImageArray( wid, 20, 20,
190 iw, ih,
"iarTrade", iconsize, iconsize,
197void commodity_exchange_cleanup (
void)
199 free(commodity_list);
200 commodity_list = NULL;
212 char buf_purchase_price[ECON_CRED_STRLEN], buf_credits[ECON_CRED_STRLEN];
215 credits_t mean,globalmean;
216 double std, globalstd;
217 char buf_mean[ECON_CRED_STRLEN], buf_globalmean[ECON_CRED_STRLEN];
218 char buf_std[ECON_CRED_STRLEN], buf_globalstd[ECON_CRED_STRLEN];
219 char buf_local_price[ECON_CRED_STRLEN];
220 char buf_tonnes_owned[ECON_MASS_STRLEN], buf_tonnes_free[ECON_MASS_STRLEN];
221 int owned, cargo_free;
222 int i = toolkit_getImageArrayPos( wid,
"iarTrade" );
228 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"%s", _(
"N/A") );
229 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", buf_tonnes_free );
230 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", buf_credits );
231 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s",
"" );
232 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"N/A") );
233 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"N/A") );
234 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", _(
"N/A") );
235 window_modifyText( wid,
"txtDInfo", buf );
236 window_modifyText( wid,
"txtDesc", _(
"No commodities available.") );
237 window_disableButton( wid,
"btnCommodityBuy" );
238 window_disableButton( wid,
"btnCommoditySell" );
241 com = commodity_list[i];
244 window_modifyImage( wid,
"imgStore", com->
gfx_store, 192, 192 );
248 snprintf( buf_std,
sizeof(buf_std), _(
"%.1f ¤"), std );
251 snprintf( buf_globalstd,
sizeof(buf_globalstd), _(
"%.1f ¤"), globalstd );
253 buf_purchase_price[0]=
'\0';
259 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"%s", buf_tonnes_owned );
260 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", buf_tonnes_free );
261 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", buf_credits );
262 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n" );
263 l +=
scnprintf( &buf[l],
sizeof(buf)-l, _(
"%s/t"), buf_local_price );
264 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n" );
265 l +=
scnprintf( &buf[l],
sizeof(buf)-l, _(
"%s/t ± %s/t"), buf_mean, buf_std );
266 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n" );
267 l +=
scnprintf( &buf[l],
sizeof(buf)-l, _(
"%s/t ± %s/t"), buf_globalmean, buf_globalstd );
268 l +=
scnprintf( &buf[l],
sizeof(buf)-l,
"\n%s", buf_purchase_price );
270 window_modifyText( wid,
"txtDInfo", buf );
271 window_modifyText( wid,
"txtName", _(com->
name) );
272 window_modifyText( wid,
"txtDesc", _(com->
description) );
276 if (commodity_isFlag(com, COMMODITY_FLAG_PRICE_CONSTANT)) {
277 l +=
scnprintf( &buf[l],
sizeof(buf)-l, _(
"Price is constant.") );
278 window_modifyText( wid,
"txtDRef", buf );
286 l +=
scnprintf( &buf[l],
sizeof(buf)-l, _(
"Price is based on #%c%.0f%%#0 of the price of #o%s#0."),
c, com->
price_mod*100., _(com->
price_ref) );
287 window_modifyText( wid,
"txtDRef", buf );
290 window_modifyText( wid,
"txtDRef", NULL );
294 window_enableButton( wid,
"btnCommodityBuy" );
296 window_disableButtonSoft( wid,
"btnCommodityBuy" );
299 window_enableButton( wid,
"btnCommoditySell" );
301 window_disableButtonSoft( wid,
"btnCommoditySell" );
309 int failure, incommodities;
310 unsigned int q, price;
311 char buf[ECON_CRED_STRLEN];
335 if (!incommodities) {
373 i = toolkit_getImageArrayPos( wid,
"iarTrade" );
374 com = commodity_list[i];
391 hparam[0].
type = HOOK_PARAM_COMMODITY;
393 hparam[1].
type = HOOK_PARAM_NUMBER;
395 hparam[2].
type = HOOK_PARAM_SENTINEL;
397 land_needsTakeoff( 1 );
416 i = toolkit_getImageArrayPos( wid,
"iarTrade" );
417 com = commodity_list[i];
428 price = price * (credits_t)q;
435 hparam[0].
type = HOOK_PARAM_COMMODITY;
437 hparam[1].
type = HOOK_PARAM_NUMBER;
439 hparam[2].
type = HOOK_PARAM_SENTINEL;
441 land_needsTakeoff( 1 );
450 SDL_Keymod mods = SDL_GetModState();
452 if (mods & (KMOD_LCTRL | KMOD_RCTRL))
454 if (mods & (KMOD_LSHIFT | KMOD_RSHIFT))
456 if (mods & (KMOD_LALT | KMOD_RALT))
482 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.
Stores a pilot commodity.
const Commodity * commodity