| 1 | /* |
| 2 | * Elite - The New Kind. |
| 3 | * |
| 4 | * Reverse engineered from the BBC disk version of Elite. |
| 5 | * Additional material by C.J.Pinder. |
| 6 | * |
| 7 | * The original Elite code is (C) I.Bell & D.Braben 1984. |
| 8 | * This version re-engineered in C by C.J.Pinder 1999-2001. |
| 9 | * |
| 10 | * email: <christian@newkind.co.uk> |
| 11 | * |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * trade.c |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "gfx.h" |
| 23 | #include "elite.h" |
| 24 | #include "trade.h" |
| 25 | #include "docked.h" |
| 26 | #include "planet.h" |
| 27 | #include "space.h" |
| 28 | #include "sound.h" |
| 29 | #include "random.h" |
| 30 | #include "main.h" |
| 31 | #include "swat.h" |
| 32 | |
| 33 | #define SLAVES 3 |
| 34 | #define NARCOTICS 6 |
| 35 | #define FIREARMS 10 |
| 36 | |
| 37 | /* |
| 38 | * The following holds the Elite Planet Stock Market. |
| 39 | */ |
| 40 | |
| 41 | #define TONNES 0 |
| 42 | #define KILOGRAMS 1 |
| 43 | #define GRAMS 2 |
| 44 | |
| 45 | struct stock_item stock_market[NO_OF_STOCK_ITEMS]= |
| 46 | { |
| 47 | {"Food", 0, 0, 19, -2, 6, 0x01, TONNES}, |
| 48 | {"Textiles", 0, 0, 20, -1, 10, 0x03, TONNES}, |
| 49 | {"Radioactives", 0, 0, 65, -3, 2, 0x07, TONNES}, |
| 50 | {"Slaves", 0, 0, 40, -5, 226, 0x1F, TONNES}, |
| 51 | {"Liquor/Wines", 0, 0, 83, -5, 251, 0x0F, TONNES}, |
| 52 | {"Luxuries", 0, 0, 196, 8, 54, 0x03, TONNES}, |
| 53 | {"Narcotics", 0, 0, 235, 29, 8, 0x78, TONNES}, |
| 54 | {"Computers", 0, 0, 154, 14, 56, 0x03, TONNES}, |
| 55 | {"Machinery", 0, 0, 117, 6, 40, 0x07, TONNES}, |
| 56 | {"Alloys", 0, 0, 78, 1, 17, 0x1F, TONNES}, |
| 57 | {"Firearms", 0, 0, 124, 13, 29, 0x07, TONNES}, |
| 58 | {"Furs", 0, 0, 176, -9, 220, 0x3F, TONNES}, |
| 59 | {"Minerals", 0, 0, 32, -1, 53, 0x03, TONNES}, |
| 60 | {"Gold", 0, 0, 97, -1, 66, 0x07, KILOGRAMS}, |
| 61 | {"Platinum", 0, 0, 171, -2, 55, 0x1F, KILOGRAMS}, |
| 62 | {"Gem-Stones", 0, 0, 45, -1, 250, 0x0F, GRAMS}, |
| 63 | {"Alien Items", 0, 0, 53, 15, 192, 0x07, TONNES}, |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * Generate the Elite stock market. |
| 71 | * The prices and quantities are affected by the planet's economy. |
| 72 | * There is also a slight amount of randomness added in. |
| 73 | * The random value is changed each time we hyperspace. |
| 74 | */ |
| 75 | |
| 76 | |
| 77 | void generate_stock_market (void) |
| 78 | { |
| 79 | int quant; |
| 80 | int price; |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < NO_OF_STOCK_ITEMS; i++) |
| 84 | { |
| 85 | price = stock_market[i].base_price; /* Start with the base price */ |
| 86 | price += cmdr.market_rnd & stock_market[i].mask; /* Add in a random amount */ |
| 87 | price += current_planet_data.economy * stock_market[i].eco_adjust; /* Adjust for planet economy */ |
| 88 | price &= 255; /* Only need bottom 8 bits */ |
| 89 | |
| 90 | quant = stock_market[i].base_quantity; /* Start with the base quantity */ |
| 91 | quant += cmdr.market_rnd & stock_market[i].mask; /* Add in a random amount */ |
| 92 | quant -= current_planet_data.economy * stock_market[i].eco_adjust; /* Adjust for planet economy */ |
| 93 | quant &= 255; /* Only need bottom 8 bits */ |
| 94 | |
| 95 | if (quant > 127) /* In an 8-bit environment '>127' would be negative */ |
| 96 | quant = 0; /* So we set it to a minimum of zero. */ |
| 97 | |
| 98 | quant &= 63; /* Quantities range from 0..63 */ |
| 99 | |
| 100 | stock_market[i].current_price = price * 4; |
| 101 | stock_market[i].current_quantity = quant; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* Alien Items are never available for purchase... */ |
| 106 | |
| 107 | stock_market[ALIEN_ITEMS_IDX].current_quantity = 0; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | void set_stock_quantities(int *quant) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; i < NO_OF_STOCK_ITEMS; i++) |
| 117 | stock_market[i].current_quantity = quant[i]; |
| 118 | |
| 119 | stock_market[ALIEN_ITEMS_IDX].current_quantity = 0; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | int carrying_contraband (void) |
| 124 | { |
| 125 | return (cmdr.current_cargo[SLAVES] + cmdr.current_cargo[NARCOTICS]) * 2 + |
| 126 | cmdr.current_cargo[FIREARMS]; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | int total_cargo (void) |
| 131 | { |
| 132 | int i; |
| 133 | int cargo_held; |
| 134 | |
| 135 | cargo_held = 0; |
| 136 | for (i = 0; i < 17; i++) |
| 137 | { |
| 138 | if ((cmdr.current_cargo[i] > 0) && |
| 139 | (stock_market[i].units == TONNES)) |
| 140 | { |
| 141 | cargo_held += cmdr.current_cargo[i]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return cargo_held; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void scoop_item (int un) |
| 150 | { |
| 151 | int type; |
| 152 | int trade; |
| 153 | |
| 154 | if (universe[un].flags & FLG_DEAD) |
| 155 | return; |
| 156 | |
| 157 | type = universe[un].type; |
| 158 | |
| 159 | if (type == SHIP_MISSILE) |
| 160 | return; |
| 161 | |
| 162 | if ((cmdr.fuel_scoop == 0) || (universe[un].location.y >= 0) || |
| 163 | (total_cargo() == cmdr.cargo_capacity)) |
| 164 | { |
| 165 | universe[un].flags |= FLG_TARGET; |
| 166 | explode_object (un); |
| 167 | damage_ship (128 + (universe[un].energy / 2), universe[un].location.z > 0); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (type == SHIP_CARGO) |
| 172 | { |
| 173 | trade = rand255() & 7; |
| 174 | cmdr.current_cargo[trade]++; |
| 175 | info_message (stock_market[trade].name); |
| 176 | remove_ship (un); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (ship_list[type]->scoop_type != 0) |
| 181 | { |
| 182 | trade = ship_list[type]->scoop_type + 1; |
| 183 | cmdr.current_cargo[trade]++; |
| 184 | info_message (stock_market[trade].name); |
| 185 | remove_ship (un); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | explode_object (un); |
| 190 | damage_ship (universe[un].energy / 2, universe[un].location.z > 0); |
| 191 | } |
| 192 | |