[Add] Buy/Sell commodities. They are all the same price at the minute.

This commit is contained in:
Allanis 2013-03-17 13:40:52 +00:00
parent 91e68ee828
commit ca84fe6a3a
6 changed files with 61 additions and 7 deletions

View File

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Commodities>
<commodity name="Food">
<description>Airtight compressed nutrient packets. Very nutritious.</description>
<high>160</high>
<medium>140</medium>
<low>120</low>
</commodity>
<commodity name="Ore">
<description>An assortment of ores. Needed for all kinds of processes.</description>
<high>240</high>
<medium>210</medium>
<low>180</low>

View File

@ -47,6 +47,7 @@ Commodity* commodity_get(const char* name) {
// Free a commodity.
static void commodity_freeOne(Commodity* com) {
if(com->name) free(com->name);
if(com->description) free(com->description);
}
static Commodity* commodity_parse(xmlNodePtr parent) {
@ -60,7 +61,9 @@ static Commodity* commodity_parse(xmlNodePtr parent) {
node = parent->xmlChildrenNode;
do {
if(xml_isNode(node, "high"))
if(xml_isNode(node, "description"))
tmp->description = strdup(xml_get(node));
else if(xml_isNode(node, "high"))
tmp->high = xml_getInt(node);
else if(xml_isNode(node, "medium"))
tmp->medium = xml_getInt(node);
@ -70,6 +73,7 @@ static Commodity* commodity_parse(xmlNodePtr parent) {
#define MELEMENT(o,s)if(o)WARN("Commodity '%s' missing '"s"' element",tmp->name)
MELEMENT(tmp->high==0, "high");
MELEMENT(tmp->description==NULL, "description");
MELEMENT(tmp->medium==0, "medium");
MELEMENT(tmp->low==0, "low");
#undef MELEMENT

View File

@ -2,6 +2,7 @@
typedef struct Commodity_ {
char* name;
char* description;
int low, medium, high; // Prices.
} Commodity;

View File

@ -46,6 +46,7 @@ static Planet* planet = NULL;
// Commodity excahnge.
static void commodity_exchange(void);
static void commodity_exchange_close(char* str);
static void commodity_update(char* str);
static void commodity_buy(char* str);
static void commodity_sell(char* str);
// Outfits.
@ -86,7 +87,18 @@ static void commodity_exchange(void) {
window_addButton(secondary_wid, -20, 20*2+BUTTON_HEIGHT,
(BUTTON_WIDTH-20)/2, BUTTON_HEIGHT, "btnCommoditySell",
"Sell", commodity_buy);
"Sell", commodity_sell);
window_addText(secondary_wid, -20, -40, BUTTON_WIDTH, 20, 0,
"txtSInfo", &gl_smallFont, &cDConsole,
"You have:\n"
"Market price:\n");
window_addText(secondary_wid, -20, -40, BUTTON_WIDTH/2, 20, 0,
"txtDInfo", &gl_smallFont, &cBlack, NULL);
window_addText(secondary_wid, -40, -80, BUTTON_WIDTH-20, 60, 0,
"txtDesc", &gl_smallFont, &cBlack, NULL);
goods = malloc(sizeof(char*)*planet->ncommodities);
for(i = 0; i < planet->ncommodities; i++)
@ -94,7 +106,9 @@ static void commodity_exchange(void) {
window_addList(secondary_wid, 20, -40,
COMMODITY_WIDTH-BUTTON_WIDTH-60, COMMODITY_HEIGHT-80-BUTTON_HEIGHT,
"lstGoods", goods, planet->ncommodities, 0, NULL);
"lstGoods", goods, planet->ncommodities, 0, commodity_update);
commodity_update(NULL);
}
static void commodity_exchange_close(char* str) {
@ -102,6 +116,25 @@ static void commodity_exchange_close(char* str) {
window_destroy(secondary_wid);
}
static void commodity_update(char* str) {
(void)str;
char buf[128];
char* comname;
Commodity* com;
comname = toolkit_getList(secondary_wid, "lstGoods");
com = commodity_get(comname);
snprintf(buf, 128,
"%d\n"
"%d Screds/ton\n",
player_cargoOwned(comname),
com->medium);
window_modifyText(secondary_wid, "txtDInfo", buf);
window_modifyText(secondary_wid, "txtDesc", com->description);
}
static void commodity_buy(char* str) {
(void)str;
char* comname;
@ -124,6 +157,7 @@ static void commodity_buy(char* str) {
q = pilot_addCargo(player, com, q);
player_credits -= q * com->medium;
commodity_update(NULL);
}
static void commodity_sell(char* str) {
@ -138,6 +172,7 @@ static void commodity_sell(char* str) {
q = pilot_rmCargo(player, com, q);
player_credits += q * com->medium;
commodity_update(NULL);
}
static void outfits(void) {

View File

@ -247,7 +247,7 @@ const char* player_rating(void) {
else return player_ratings[7];
}
// Return how much space the player has remaining.
// Return how much weapon. space the player has remaining.
int player_freeSpace(void) {
int i, s;
s = player->ship->cap_weapon;
@ -268,6 +268,17 @@ int player_outfitOwned(const char* outfitname) {
return 0;
}
// Return how many of the commodity the player has.
int player_cargoOwned(const char* commodityname) {
int i;
for(i = 0; i < player->ncommodities; i++)
if(strcmp(commodityname, player->commodities[i].commodity->name)==0)
return player->commodities[i].quantity;
return 0;
}
// Render the background player stuff, namely planet target
void player_renderBG(void) {
double x, y;

View File

@ -42,6 +42,7 @@ void player_warp(const double x, const double y);
const char* player_rating(void);
int player_freeSpace(void);
int player_outfitOwned(const char* outfitname);
int player_cargoOwned(const char* commodityname);
// Keybind actions.
void player_setRadarRel(int mod);