|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_shop_c = |
3 | * static char *rcsid_shop_c = |
3 | * "$Id: shop.C,v 1.9 2006/09/07 10:01:58 pippijn Exp $"; |
4 | * "$Id: shop.C,v 1.10 2006/09/10 15:59:57 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
30 | #include <spells.h> |
31 | #include <spells.h> |
31 | #include <skills.h> |
32 | #include <skills.h> |
32 | #include <living.h> |
33 | #include <living.h> |
33 | #include <newclient.h> |
34 | #include <newclient.h> |
34 | #ifndef __CEXTRACT__ |
35 | #ifndef __CEXTRACT__ |
35 | #include <sproto.h> |
36 | # include <sproto.h> |
36 | #endif |
37 | #endif |
37 | #include <math.h> |
38 | #include <math.h> |
38 | |
39 | |
39 | /* this is a measure of how effective store specialisation is. A general store |
40 | /* this is a measure of how effective store specialisation is. A general store |
40 | * will offer this proportion of the 'maximum' price, a specialised store will |
41 | * will offer this proportion of the 'maximum' price, a specialised store will |
… | |
… | |
46 | #define SPECIALISATION_EFFECT 0.5 |
47 | #define SPECIALISATION_EFFECT 0.5 |
47 | |
48 | |
48 | /* price a shopkeeper will give someone they neither like nor dislike */ |
49 | /* price a shopkeeper will give someone they neither like nor dislike */ |
49 | #define NEUTRAL_RATIO 0.8 |
50 | #define NEUTRAL_RATIO 0.8 |
50 | |
51 | |
51 | static uint64 pay_from_container(object *pl, object *pouch, uint64 to_pay); |
52 | static uint64 pay_from_container (object *pl, object *pouch, uint64 to_pay); |
52 | static uint64 value_limit(uint64 val, int quantity, const object *who, int isshop); |
53 | static uint64 value_limit (uint64 val, int quantity, const object *who, int isshop); |
53 | static double shop_specialisation_ratio(const object *item, const mapstruct *map); |
54 | static double shop_specialisation_ratio (const object *item, const mapstruct *map); |
54 | static double shop_greed(const mapstruct *map); |
55 | static double shop_greed (const mapstruct *map); |
55 | |
56 | |
56 | #define NUM_COINS 4 /* number of coin types */ |
57 | #define NUM_COINS 4 /* number of coin types */ |
57 | static const char* const coins[] = {"royalty", "platinacoin", "goldcoin", "silvercoin", NULL}; |
58 | static const char *const coins[] = { "royalty", "platinacoin", "goldcoin", "silvercoin", NULL }; |
58 | |
59 | |
59 | /* Added F_TRUE flag to define.h to mean that the price should not |
60 | /* Added F_TRUE flag to define.h to mean that the price should not |
60 | * be adjusted by players charisma. With F_TRUE, it returns the amount |
61 | * be adjusted by players charisma. With F_TRUE, it returns the amount |
61 | * that the item is worth, if it was sold, but unadjusted by charisma. |
62 | * that the item is worth, if it was sold, but unadjusted by charisma. |
62 | * This is needed for alchemy, to to determine what value of gold nuggets |
63 | * This is needed for alchemy, to to determine what value of gold nuggets |
… | |
… | |
81 | * Mark Wedel (mwedel@pyramid.com) |
82 | * Mark Wedel (mwedel@pyramid.com) |
82 | */ |
83 | */ |
83 | |
84 | |
84 | static uint64 approx_range; |
85 | static uint64 approx_range; |
85 | |
86 | |
|
|
87 | uint64 |
86 | uint64 query_cost(const object *tmp, object *who, int flag) { |
88 | query_cost (const object *tmp, object *who, int flag) |
|
|
89 | { |
87 | double val; |
90 | double val; |
88 | int number; /* used to better calculate value */ |
91 | int number; /* used to better calculate value */ |
89 | int no_bargain; |
92 | int no_bargain; |
90 | int identified; |
93 | int identified; |
91 | int not_cursed; |
94 | int not_cursed; |
92 | int approximate; |
95 | int approximate; |
93 | int shop; |
96 | int shop; |
94 | double diff; |
97 | double diff; |
95 | |
98 | |
96 | approx_range = 0; |
99 | approx_range = 0; |
97 | |
100 | |
98 | no_bargain = flag & F_NO_BARGAIN; |
101 | no_bargain = flag & F_NO_BARGAIN; |
99 | identified = flag & F_IDENTIFIED; |
102 | identified = flag & F_IDENTIFIED; |
100 | not_cursed = flag & F_NOT_CURSED; |
103 | not_cursed = flag & F_NOT_CURSED; |
101 | approximate = flag & F_APPROX; |
104 | approximate = flag & F_APPROX; |
102 | shop = flag & F_SHOP; |
105 | shop = flag & F_SHOP; |
103 | flag &= ~(F_NO_BARGAIN|F_IDENTIFIED|F_NOT_CURSED|F_APPROX|F_SHOP); |
106 | flag &= ~(F_NO_BARGAIN | F_IDENTIFIED | F_NOT_CURSED | F_APPROX | F_SHOP); |
104 | |
107 | |
|
|
108 | if (tmp->type == MONEY) |
105 | if (tmp->type==MONEY) return (tmp->nrof * tmp->value); |
109 | return (tmp->nrof * tmp->value); |
106 | if (tmp->type==GEM) { |
110 | if (tmp->type == GEM) |
|
|
111 | { |
|
|
112 | if (flag == F_TRUE) |
107 | if (flag==F_TRUE) return (tmp->nrof * tmp->value); |
113 | return (tmp->nrof * tmp->value); |
|
|
114 | if (flag == F_BUY) |
108 | if (flag==F_BUY) return (uint64) (1.03 * tmp->nrof * tmp->value); |
115 | return (uint64) (1.03 * tmp->nrof * tmp->value); |
|
|
116 | if (flag == F_SELL) |
109 | if (flag==F_SELL) return (uint64) (0.97 * tmp->nrof * tmp->value); |
117 | return (uint64) (0.97 * tmp->nrof * tmp->value); |
110 | LOG(llevError,"Query_cost: Gem type with unknown flag : %d\n", flag); |
118 | LOG (llevError, "Query_cost: Gem type with unknown flag : %d\n", flag); |
|
|
119 | return 0; |
|
|
120 | } |
|
|
121 | number = tmp->nrof; |
|
|
122 | if (number == 0) |
|
|
123 | number = 1; |
|
|
124 | if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified) |
|
|
125 | { |
|
|
126 | if (!not_cursed && (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))) |
111 | return 0; |
127 | return 0; |
112 | } |
|
|
113 | number = tmp->nrof; |
|
|
114 | if (number==0) number=1; |
|
|
115 | if (QUERY_FLAG(tmp, FLAG_IDENTIFIED) || !need_identify(tmp) || identified) { |
|
|
116 | if (!not_cursed && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))) |
|
|
117 | return 0; |
|
|
118 | else |
128 | else |
119 | val=tmp->value * number; |
129 | val = tmp->value * number; |
120 | } |
130 | } |
121 | /* This area deals with objects that are not identified, but can be */ |
131 | /* This area deals with objects that are not identified, but can be */ |
122 | else { |
132 | else |
|
|
133 | { |
123 | if (tmp->arch != NULL) { |
134 | if (tmp->arch != NULL) |
|
|
135 | { |
124 | if (flag == F_BUY) { |
136 | if (flag == F_BUY) |
|
|
137 | { |
125 | LOG(llevError, "Asking for buy-value of unidentified object.\n"); |
138 | LOG (llevError, "Asking for buy-value of unidentified object.\n"); |
126 | val = tmp->arch->clone.value * 50 * number; |
139 | val = tmp->arch->clone.value * 50 * number; |
127 | } |
140 | } |
|
|
141 | else |
128 | else { /* Trying to sell something, or get true value */ |
142 | { /* Trying to sell something, or get true value */ |
129 | if (tmp->type == POTION) |
143 | if (tmp->type == POTION) |
130 | val = number * 40; /* Don't want to give anything away */ |
144 | val = number * 40; /* Don't want to give anything away */ |
131 | else { |
145 | else |
|
|
146 | { |
132 | /* Get 2/3'rd value for applied objects, 1/3'rd for totally |
147 | /* Get 2/3'rd value for applied objects, 1/3'rd for totally |
133 | * unknown objects |
148 | * unknown objects |
134 | */ |
149 | */ |
135 | if (QUERY_FLAG(tmp, FLAG_BEEN_APPLIED)) |
150 | if (QUERY_FLAG (tmp, FLAG_BEEN_APPLIED)) |
136 | val = number * tmp->arch->clone.value *2 / 3; |
151 | val = number * tmp->arch->clone.value * 2 / 3; |
137 | else |
152 | else |
138 | val = number * tmp->arch->clone.value / 3; |
153 | val = number * tmp->arch->clone.value / 3; |
139 | } |
154 | } |
140 | } |
155 | } |
|
|
156 | } |
|
|
157 | else |
141 | } else { /* No archetype with this object */ |
158 | { /* No archetype with this object */ |
142 | LOG(llevDebug,"In sell item: Have object with no archetype: %s\n", &tmp->name); |
159 | LOG (llevDebug, "In sell item: Have object with no archetype: %s\n", &tmp->name); |
143 | if (flag == F_BUY) { |
160 | if (flag == F_BUY) |
|
|
161 | { |
144 | LOG(llevError, "Asking for buy-value of unidentified object without arch.\n"); |
162 | LOG (llevError, "Asking for buy-value of unidentified object without arch.\n"); |
145 | val = number * tmp->value * 10; |
163 | val = number * tmp->value * 10; |
146 | } |
164 | } |
147 | else |
165 | else |
148 | val = number * tmp->value / 5; |
166 | val = number * tmp->value / 5; |
149 | } |
167 | } |
150 | } |
168 | } |
151 | |
169 | |
152 | /* If the item has been applied or identifed or does not need to be |
170 | /* If the item has been applied or identifed or does not need to be |
153 | * identified, AND the object is magical and the archetype is non |
171 | * identified, AND the object is magical and the archetype is non |
154 | * magical, then change values accordingly. The tmp->arch==NULL is |
172 | * magical, then change values accordingly. The tmp->arch==NULL is |
155 | * really just a check to prevent core dumps for when it checks |
173 | * really just a check to prevent core dumps for when it checks |
156 | * tmp->arch->clone.magic for any magic. The check for archetype |
174 | * tmp->arch->clone.magic for any magic. The check for archetype |
157 | * magic is to not give extra money for archetypes that are by |
175 | * magic is to not give extra money for archetypes that are by |
158 | * default magical. This is because the archetype value should have |
176 | * default magical. This is because the archetype value should have |
159 | * already figured in that value. |
177 | * already figured in that value. |
160 | */ |
178 | */ |
161 | if((QUERY_FLAG(tmp, FLAG_IDENTIFIED)||!need_identify(tmp)||identified|| |
179 | if ((QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified || |
162 | QUERY_FLAG(tmp, FLAG_BEEN_APPLIED)) && |
180 | QUERY_FLAG (tmp, FLAG_BEEN_APPLIED)) && tmp->magic && (tmp->arch == NULL || !tmp->arch->clone.magic)) |
163 | tmp->magic&&(tmp->arch==NULL||!tmp->arch->clone.magic)) { |
181 | { |
164 | if(tmp->magic>0) |
182 | if (tmp->magic > 0) |
165 | val*=(3*tmp->magic*tmp->magic*tmp->magic); |
183 | val *= (3 * tmp->magic * tmp->magic * tmp->magic); |
166 | else |
184 | else |
167 | /* Note that tmp->magic is negative, so that this |
185 | /* Note that tmp->magic is negative, so that this |
168 | * will actually be something like val /=2, /=3, etc. |
186 | * will actually be something like val /=2, /=3, etc. |
169 | */ |
|
|
170 | val/=(1-tmp->magic); |
|
|
171 | } |
|
|
172 | |
|
|
173 | if (tmp->type==WAND) { |
|
|
174 | /* Value of the wand is multiplied by the number of |
|
|
175 | * charges. the treasure code already sets up the value |
|
|
176 | * 50 charges is used as the baseline. |
|
|
177 | */ |
187 | */ |
|
|
188 | val /= (1 - tmp->magic); |
|
|
189 | } |
|
|
190 | |
|
|
191 | if (tmp->type == WAND) |
|
|
192 | { |
|
|
193 | /* Value of the wand is multiplied by the number of |
|
|
194 | * charges. the treasure code already sets up the value |
|
|
195 | * 50 charges is used as the baseline. |
|
|
196 | */ |
178 | if (QUERY_FLAG(tmp, FLAG_IDENTIFIED) || !need_identify(tmp) || identified) |
197 | if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified) |
179 | val=(val*tmp->stats.food) / 50; |
198 | val = (val * tmp->stats.food) / 50; |
180 | else /* if not identified, presume one charge */ |
199 | else /* if not identified, presume one charge */ |
181 | val/=50; |
200 | val /= 50; |
182 | } |
201 | } |
183 | |
202 | |
184 | /* Limit amount of money you can get for really great items. */ |
203 | /* Limit amount of money you can get for really great items. */ |
185 | if (flag==F_SELL) |
204 | if (flag == F_SELL) |
186 | val=value_limit((uint64)val, number, who, shop); |
205 | val = value_limit ((uint64) val, number, who, shop); |
187 | |
206 | |
188 | // use a nonlinear price adjustment. as my predecessor said, don't change |
207 | // use a nonlinear price adjustment. as my predecessor said, don't change |
189 | // the archetypes, its work required for balancing, and we don't care. |
208 | // the archetypes, its work required for balancing, and we don't care. |
190 | //val = pow (val, 1.05); |
209 | //val = pow (val, 1.05); |
191 | |
210 | |
192 | /* This modification is for bargaining skill. |
211 | /* This modification is for bargaining skill. |
193 | * Now only players with max level in bargaining |
212 | * Now only players with max level in bargaining |
194 | * AND Cha = 30 will get optimal price. |
213 | * AND Cha = 30 will get optimal price. |
195 | * Thus charisma will never get useless. |
214 | * Thus charisma will never get useless. |
196 | * -b.e. edler@heydernet.de |
215 | * -b.e. edler@heydernet.de |
197 | */ |
216 | */ |
198 | |
217 | |
199 | if (who!=NULL && who->type==PLAYER) { |
218 | if (who != NULL && who->type == PLAYER) |
|
|
219 | { |
200 | int lev_bargain = 0; |
220 | int lev_bargain = 0; |
201 | int lev_identify = 0; |
221 | int lev_identify = 0; |
202 | int idskill1=0; |
222 | int idskill1 = 0; |
203 | int idskill2=0; |
223 | int idskill2 = 0; |
204 | const typedata *tmptype; |
224 | const typedata *tmptype; |
205 | |
225 | |
206 | tmptype=get_typedata(tmp->type); |
226 | tmptype = get_typedata (tmp->type); |
207 | |
227 | |
208 | if (find_skill_by_number(who,SK_BARGAINING)) { |
228 | if (find_skill_by_number (who, SK_BARGAINING)) |
|
|
229 | { |
209 | lev_bargain = find_skill_by_number(who,SK_BARGAINING)->level; |
230 | lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; |
210 | } |
231 | } |
211 | if (tmptype) { |
232 | if (tmptype) |
|
|
233 | { |
212 | idskill1=tmptype->identifyskill; |
234 | idskill1 = tmptype->identifyskill; |
213 | if (idskill1) { |
235 | if (idskill1) |
|
|
236 | { |
214 | idskill2=tmptype->identifyskill2; |
237 | idskill2 = tmptype->identifyskill2; |
215 | if (find_skill_by_number(who,idskill1)) { |
238 | if (find_skill_by_number (who, idskill1)) |
|
|
239 | { |
216 | lev_identify = find_skill_by_number(who,idskill1)->level; |
240 | lev_identify = find_skill_by_number (who, idskill1)->level; |
|
|
241 | } |
|
|
242 | if (idskill2 && find_skill_by_number (who, idskill2)) |
|
|
243 | { |
|
|
244 | lev_identify += find_skill_by_number (who, idskill2)->level; |
|
|
245 | } |
217 | } |
246 | } |
218 | if (idskill2 && find_skill_by_number(who,idskill2)) { |
|
|
219 | lev_identify += find_skill_by_number(who,idskill2)->level; |
|
|
220 | } |
247 | } |
221 | } |
248 | else |
222 | } |
|
|
223 | else LOG(llevError, "Query_cost: item %s hasn't got a valid type\n", &tmp->name); |
249 | LOG (llevError, "Query_cost: item %s hasn't got a valid type\n", &tmp->name); |
224 | |
250 | |
225 | /* ratio determines how much of the price modification |
251 | /* ratio determines how much of the price modification |
226 | * will come from the basic stat charisma |
252 | * will come from the basic stat charisma |
227 | * the rest will come from the level in bargaining skill |
253 | * the rest will come from the level in bargaining skill |
228 | */ |
254 | */ |
229 | const double cha_ratio = 0.40; |
255 | const double cha_ratio = 0.40; |
230 | |
256 | |
231 | diff = no_bargain ? 1.0 : 1. - pow (lev_bargain / (double)settings.max_level, 0.25); |
257 | diff = no_bargain ? 1.0 : 1. - pow (lev_bargain / (double) settings.max_level, 0.25); |
232 | |
258 | |
233 | diff = (1. - cha_ratio) * diff |
|
|
234 | + cha_ratio * (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.); |
259 | diff = (1. - cha_ratio) * diff + cha_ratio * (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.); |
235 | |
260 | |
236 | diff = .02 + (.80 - .02) * diff; |
261 | diff = .02 + (.80 - .02) * diff; |
237 | |
|
|
238 | if (flag == F_BUY) val += (val * diff); |
|
|
239 | else if (flag == F_SELL) val -= (val * diff); |
|
|
240 | |
262 | |
|
|
263 | if (flag == F_BUY) |
|
|
264 | val += (val * diff); |
|
|
265 | else if (flag == F_SELL) |
|
|
266 | val -= (val * diff); |
|
|
267 | |
241 | // now find a price range. the less good we can judge, the larger the range is |
268 | // now find a price range. the less good we can judge, the larger the range is |
242 | // then the range is adjusted randomly around the correct value |
269 | // then the range is adjusted randomly around the correct value |
243 | if (approximate) |
270 | if (approximate) |
244 | approx_range = uint64 (val / sqrt (lev_identify * 3 + 1)); |
271 | approx_range = uint64 (val / sqrt (lev_identify * 3 + 1)); |
245 | } |
|
|
246 | |
272 | } |
|
|
273 | |
247 | /* I don't think this should really happen - if it does, it indicates and |
274 | /* I don't think this should really happen - if it does, it indicates and |
248 | * overflow of diff above. That shoudl only happen if |
275 | * overflow of diff above. That shoudl only happen if |
249 | * we are selling objects - in that case, the person just |
276 | * we are selling objects - in that case, the person just |
250 | * gets no money. |
277 | * gets no money. |
251 | */ |
278 | */ |
252 | if((sint64)val<0) |
279 | if ((sint64) val < 0) |
253 | val=0; |
280 | val = 0; |
254 | |
281 | |
255 | /* Unidentified stuff won't sell for more than 60gp */ |
282 | /* Unidentified stuff won't sell for more than 60gp */ |
256 | if(flag==F_SELL && !QUERY_FLAG(tmp, FLAG_IDENTIFIED) && need_identify(tmp) && !identified) { |
283 | if (flag == F_SELL && !QUERY_FLAG (tmp, FLAG_IDENTIFIED) && need_identify (tmp) && !identified) |
|
|
284 | { |
257 | val = (val > 600)? 600:val; |
285 | val = (val > 600) ? 600 : val; |
258 | } |
|
|
259 | |
286 | } |
|
|
287 | |
260 | /* if we are in a shop, check how the type of shop should affect the price */ |
288 | /* if we are in a shop, check how the type of shop should affect the price */ |
261 | if (shop && who) { |
289 | if (shop && who) |
|
|
290 | { |
262 | if (flag==F_SELL) |
291 | if (flag == F_SELL) |
263 | val= ( |
292 | val = (val * shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who) / shop_greed (who->map)); |
264 | val |
|
|
265 | * shop_specialisation_ratio(tmp, who->map) |
|
|
266 | * shopkeeper_approval(who->map, who) |
|
|
267 | / shop_greed(who->map) |
|
|
268 | ); |
|
|
269 | else if (flag==F_BUY) { |
293 | else if (flag == F_BUY) |
|
|
294 | { |
270 | /* |
295 | /* |
271 | * when buying, if the item was sold by another player, it is ok to |
296 | * when buying, if the item was sold by another player, it is ok to |
272 | * let the item be sold cheaper, according to the specialisation of |
297 | * let the item be sold cheaper, according to the specialisation of |
273 | * the shop. If a player sold an item here, then his sale price was |
298 | * the shop. If a player sold an item here, then his sale price was |
274 | * multiplied by the specialisation ratio, to do the same to the buy |
299 | * multiplied by the specialisation ratio, to do the same to the buy |
275 | * price will not generate extra money. However, the |
300 | * price will not generate extra money. However, the |
276 | * same is not true of generated items, these have to /divide/ by the |
301 | * same is not true of generated items, these have to /divide/ by the |
277 | * specialisation, so that the price is never less than what they could |
302 | * specialisation, so that the price is never less than what they could |
278 | * be sold for (otherwise players could camp map resets to make money). |
303 | * be sold for (otherwise players could camp map resets to make money). |
279 | * In game terms, a non-specialist shop, might not recognise the true |
304 | * In game terms, a non-specialist shop, might not recognise the true |
280 | * value of the items they sell (much like how people sometimes find |
305 | * value of the items they sell (much like how people sometimes find |
281 | * antiques in a junk shop in real life). |
306 | * antiques in a junk shop in real life). |
282 | */ |
307 | */ |
283 | if (QUERY_FLAG(tmp, FLAG_PLAYER_SOLD)) |
308 | if (QUERY_FLAG (tmp, FLAG_PLAYER_SOLD)) |
284 | val= ( |
309 | val = (val * shop_greed (who->map) * shop_specialisation_ratio (tmp, who->map) / shopkeeper_approval (who->map, who)); |
285 | val |
|
|
286 | * shop_greed(who->map) |
|
|
287 | * shop_specialisation_ratio(tmp, who->map) |
|
|
288 | / shopkeeper_approval(who->map, who) |
|
|
289 | ); |
|
|
290 | else |
310 | else |
291 | val= ( |
|
|
292 | val |
|
|
293 | * shop_greed(who->map) |
|
|
294 | / (shop_specialisation_ratio(tmp, who->map)*shopkeeper_approval(who->map, who)) |
311 | val = (val * shop_greed (who->map) / (shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who))); |
295 | ); |
|
|
296 | } |
312 | } |
297 | /* we will also have an extra 0-5% variation between shops of the same type |
313 | /* we will also have an extra 0-5% variation between shops of the same type |
298 | * for valuable items (below a value of 50 this effect wouldn't be very |
314 | * for valuable items (below a value of 50 this effect wouldn't be very |
299 | * pointful, and could give fun with rounding. |
315 | * pointful, and could give fun with rounding. |
300 | */ |
316 | */ |
301 | if(who->map->path!=NULL && val > 50) |
317 | if (who->map->path != NULL && val > 50) |
302 | val= (val+0.05*(sint64)val*cos(tmp->count+strlen(who->map->path))); |
318 | val = (val + 0.05 * (sint64) val * cos (tmp->count + strlen (who->map->path))); |
303 | } |
319 | } |
304 | return (uint64)val; |
320 | return (uint64) val; |
305 | } |
321 | } |
306 | |
322 | |
307 | /* Find the coin type that is worth more the 'c'. Starts at the |
323 | /* Find the coin type that is worth more the 'c'. Starts at the |
308 | * cointype placement. |
324 | * cointype placement. |
309 | */ |
325 | */ |
310 | |
326 | |
|
|
327 | static archetype * |
311 | static archetype *find_next_coin(uint64 c, int *cointype) { |
328 | find_next_coin (uint64 c, int *cointype) |
|
|
329 | { |
312 | archetype *coin; |
330 | archetype *coin; |
313 | |
331 | |
314 | do |
332 | do |
315 | { |
333 | { |
316 | if (coins[*cointype] == NULL) return NULL; |
334 | if (coins[*cointype] == NULL) |
|
|
335 | return NULL; |
317 | coin = find_archetype(coins[*cointype]); |
336 | coin = find_archetype (coins[*cointype]); |
318 | if (coin == NULL) return NULL; |
337 | if (coin == NULL) |
|
|
338 | return NULL; |
319 | *cointype += 1; |
339 | *cointype += 1; |
|
|
340 | } |
320 | } while ((uint64) coin->clone.value > c); |
341 | while ((uint64) coin->clone.value > c); |
321 | |
342 | |
322 | return coin; |
343 | return coin; |
323 | } |
344 | } |
324 | |
345 | |
325 | /* This returns a string of how much something is worth based on |
346 | /* This returns a string of how much something is worth based on |
… | |
… | |
330 | * have so much money that they have more than 2 billion platinum |
351 | * have so much money that they have more than 2 billion platinum |
331 | * coins, there are certainly issues - the easiest fix at that |
352 | * coins, there are certainly issues - the easiest fix at that |
332 | * time is to add a higher denomination (mithril piece with |
353 | * time is to add a higher denomination (mithril piece with |
333 | * 10,000 silver or something) |
354 | * 10,000 silver or something) |
334 | */ |
355 | */ |
|
|
356 | const char * |
335 | const char *cost_string_from_value(uint64 cost, int approx) |
357 | cost_string_from_value (uint64 cost, int approx) |
336 | { |
358 | { |
337 | static char buf[MAX_BUF]; |
359 | static char buf[MAX_BUF]; |
338 | archetype *coin, *next_coin; |
360 | archetype *coin, *next_coin; |
339 | int num, cointype = 0; |
361 | int num, cointype = 0; |
340 | |
362 | |
341 | coin = find_next_coin(cost, &cointype); |
363 | coin = find_next_coin (cost, &cointype); |
342 | if (coin == NULL) |
364 | if (coin == NULL) |
343 | return "nothing"; |
365 | return "nothing"; |
344 | |
366 | |
345 | num = cost / coin->clone.value; |
367 | num = cost / coin->clone.value; |
346 | /* so long as nrof is 32 bit, this is true. |
368 | /* so long as nrof is 32 bit, this is true. |
347 | * If it takes more coins than a person can possibly carry, this |
369 | * If it takes more coins than a person can possibly carry, this |
348 | * is basically true. |
370 | * is basically true. |
349 | */ |
371 | */ |
350 | if ( (cost / coin->clone.value) > UINT32_MAX) { |
372 | if ((cost / coin->clone.value) > UINT32_MAX) |
|
|
373 | { |
351 | strcpy(buf,"an unimaginable sum of money"); |
374 | strcpy (buf, "an unimaginable sum of money"); |
352 | return buf; |
375 | return buf; |
353 | } |
376 | } |
354 | |
377 | |
355 | cost -= (uint64)num * (uint64)coin->clone.value; |
378 | cost -= (uint64) num *(uint64) coin->clone.value; |
|
|
379 | |
356 | sprintf(buf, "%d %s", num, num > 1 ? &coin->clone.name_pl : &coin->clone.name); |
380 | sprintf (buf, "%d %s", num, num > 1 ? &coin->clone.name_pl : &coin->clone.name); |
357 | |
381 | |
358 | next_coin = find_next_coin(cost, &cointype); |
382 | next_coin = find_next_coin (cost, &cointype); |
359 | if (next_coin == NULL || approx) |
383 | if (next_coin == NULL || approx) |
360 | return buf; |
|
|
361 | |
|
|
362 | coin = next_coin; |
|
|
363 | num = cost / coin->clone.value; |
|
|
364 | cost -= (uint64)num * (uint64)coin->clone.value; |
|
|
365 | |
|
|
366 | sprintf (buf + strlen (buf), " and %d %s", num, num > 1 ? &coin->clone.name_pl : &coin->clone.name); |
|
|
367 | |
|
|
368 | return buf; |
384 | return buf; |
369 | } |
|
|
370 | |
385 | |
|
|
386 | coin = next_coin; |
|
|
387 | num = cost / coin->clone.value; |
|
|
388 | cost -= (uint64) num *(uint64) coin->clone.value; |
|
|
389 | |
|
|
390 | sprintf (buf + strlen (buf), " and %d %s", num, num > 1 ? &coin->clone.name_pl : &coin->clone.name); |
|
|
391 | |
|
|
392 | return buf; |
|
|
393 | } |
|
|
394 | |
|
|
395 | const char * |
371 | const char *query_cost_string(const object *tmp,object *who,int flag) { |
396 | query_cost_string (const object *tmp, object *who, int flag) |
|
|
397 | { |
372 | uint64 real_value = query_cost(tmp,who,flag); |
398 | uint64 real_value = query_cost (tmp, who, flag); |
373 | int idskill1=0; |
399 | int idskill1 = 0; |
374 | int idskill2=0; |
400 | int idskill2 = 0; |
375 | const typedata *tmptype; |
401 | const typedata *tmptype; |
376 | |
402 | |
377 | tmptype=get_typedata(tmp->type); |
403 | tmptype = get_typedata (tmp->type); |
378 | if (tmptype) { |
404 | if (tmptype) |
|
|
405 | { |
379 | idskill1=tmptype->identifyskill; |
406 | idskill1 = tmptype->identifyskill; |
380 | idskill2=tmptype->identifyskill2; |
407 | idskill2 = tmptype->identifyskill2; |
381 | } |
408 | } |
382 | |
409 | |
383 | /* we show an approximate price if |
410 | /* we show an approximate price if |
384 | * 1) we are approximating |
411 | * 1) we are approximating |
385 | * 2) there either is no id skill(s) for the item, or we don't have them |
412 | * 2) there either is no id skill(s) for the item, or we don't have them |
386 | * 3) we don't have bargaining skill either |
413 | * 3) we don't have bargaining skill either |
387 | */ |
414 | */ |
388 | if (flag & F_APPROX) { |
415 | if (flag & F_APPROX) |
|
|
416 | { |
389 | if (!idskill1 || !find_skill_by_number(who, idskill1)) { |
417 | if (!idskill1 || !find_skill_by_number (who, idskill1)) |
|
|
418 | { |
390 | if (!idskill2 || !find_skill_by_number(who, idskill2)) { |
419 | if (!idskill2 || !find_skill_by_number (who, idskill2)) |
|
|
420 | { |
391 | if (!find_skill_by_number(who,SK_BARGAINING)) { |
421 | if (!find_skill_by_number (who, SK_BARGAINING)) |
|
|
422 | { |
392 | static char buf[MAX_BUF]; |
423 | static char buf[MAX_BUF]; |
393 | int num, cointype = 0; |
424 | int num, cointype = 0; |
394 | archetype *coin = find_next_coin(real_value, &cointype); |
425 | archetype *coin = find_next_coin (real_value, &cointype); |
395 | |
426 | |
|
|
427 | if (coin == NULL) |
396 | if (coin == NULL) return "nothing"; |
428 | return "nothing"; |
397 | |
429 | |
398 | num = real_value / coin->clone.value; |
430 | num = real_value / coin->clone.value; |
399 | if (num == 1) |
431 | if (num == 1) |
400 | sprintf(buf, "about one %s", &coin->clone.name); |
432 | sprintf (buf, "about one %s", &coin->clone.name); |
401 | else if (num < 5) |
433 | else if (num < 5) |
402 | sprintf(buf, "a few %s", &coin->clone.name_pl); |
434 | sprintf (buf, "a few %s", &coin->clone.name_pl); |
403 | else if (num < 10) |
435 | else if (num < 10) |
404 | sprintf(buf, "several %s", &coin->clone.name_pl); |
436 | sprintf (buf, "several %s", &coin->clone.name_pl); |
405 | else if (num < 25) |
437 | else if (num < 25) |
406 | sprintf(buf, "a moderate amount of %s", &coin->clone.name_pl); |
438 | sprintf (buf, "a moderate amount of %s", &coin->clone.name_pl); |
407 | else if (num < 100) |
439 | else if (num < 100) |
408 | sprintf(buf, "lots of %s", &coin->clone.name_pl); |
440 | sprintf (buf, "lots of %s", &coin->clone.name_pl); |
409 | else if (num < 1000) |
441 | else if (num < 1000) |
410 | sprintf(buf, "a great many %s", &coin->clone.name_pl); |
442 | sprintf (buf, "a great many %s", &coin->clone.name_pl); |
411 | else |
443 | else |
412 | sprintf(buf, "a vast quantity of %s", &coin->clone.name_pl); |
444 | sprintf (buf, "a vast quantity of %s", &coin->clone.name_pl); |
413 | return buf; |
445 | return buf; |
414 | } |
446 | } |
415 | } |
447 | } |
416 | } |
448 | } |
417 | |
449 | |
418 | int hash = ((unsigned int)tmp->count * 174364621) & 1023; |
450 | int hash = ((unsigned int) tmp->count * 174364621) & 1023; |
419 | |
451 | |
420 | if (approx_range) |
452 | if (approx_range) |
421 | { |
453 | { |
422 | uint64 lo = (sint64)real_value - (approx_range * hash >> 10); |
454 | uint64 lo = (sint64) real_value - (approx_range * hash >> 10); |
423 | static char buf[MAX_BUF]; |
455 | static char buf[MAX_BUF]; |
424 | |
456 | |
425 | sprintf (buf, "between %s", cost_string_from_value (lo, 1)); |
457 | sprintf (buf, "between %s", cost_string_from_value (lo, 1)); |
426 | sprintf (buf + strlen (buf), " and %s", cost_string_from_value (lo + approx_range, 1)); |
458 | sprintf (buf + strlen (buf), " and %s", cost_string_from_value (lo + approx_range, 1)); |
427 | |
459 | |
428 | return buf; |
460 | return buf; |
429 | } |
461 | } |
430 | } |
462 | } |
431 | |
463 | |
432 | return cost_string_from_value (real_value, 0); |
464 | return cost_string_from_value (real_value, 0); |
433 | } |
465 | } |
434 | |
466 | |
435 | /* This function finds out how much money the player is carrying, |
467 | /* This function finds out how much money the player is carrying, |
436 | * including what is in containers. |
468 | * including what is in containers. |
437 | */ |
469 | */ |
|
|
470 | uint64 |
438 | uint64 query_money(const object *op) { |
471 | query_money (const object *op) |
|
|
472 | { |
439 | object *tmp; |
473 | object *tmp; |
440 | uint64 total=0; |
474 | uint64 total = 0; |
441 | |
475 | |
442 | if (op->type!=PLAYER && op->type!=CONTAINER) { |
476 | if (op->type != PLAYER && op->type != CONTAINER) |
|
|
477 | { |
443 | LOG(llevError, "Query money called with non player/container\n"); |
478 | LOG (llevError, "Query money called with non player/container\n"); |
444 | return 0; |
479 | return 0; |
445 | } |
480 | } |
446 | for (tmp = op->inv; tmp; tmp= tmp->below) { |
481 | for (tmp = op->inv; tmp; tmp = tmp->below) |
|
|
482 | { |
447 | if (tmp->type==MONEY) { |
483 | if (tmp->type == MONEY) |
|
|
484 | { |
448 | total += (uint64)tmp->nrof * (uint64)tmp->value; |
485 | total += (uint64) tmp->nrof * (uint64) tmp->value; |
449 | } else if (tmp->type==CONTAINER && |
486 | } |
450 | QUERY_FLAG(tmp,FLAG_APPLIED) && |
487 | else if (tmp->type == CONTAINER && QUERY_FLAG (tmp, FLAG_APPLIED) && (tmp->race == NULL || strstr (tmp->race, "gold"))) |
451 | (tmp->race==NULL || strstr(tmp->race,"gold"))) { |
488 | { |
452 | total += query_money(tmp); |
489 | total += query_money (tmp); |
453 | } |
490 | } |
454 | } |
491 | } |
455 | return total; |
492 | return total; |
456 | } |
493 | } |
|
|
494 | |
457 | /* TCHIZE: This function takes the amount of money from the |
495 | /* TCHIZE: This function takes the amount of money from the |
458 | * the player inventory and from it's various pouches using the |
496 | * the player inventory and from it's various pouches using the |
459 | * pay_from_container function. |
497 | * pay_from_container function. |
460 | * returns 0 if not possible. 1 if success |
498 | * returns 0 if not possible. 1 if success |
461 | */ |
499 | */ |
|
|
500 | int |
462 | int pay_for_amount(uint64 to_pay,object *pl) { |
501 | pay_for_amount (uint64 to_pay, object *pl) |
|
|
502 | { |
463 | object *pouch; |
503 | object *pouch; |
464 | |
504 | |
465 | if (to_pay==0) return 1; |
505 | if (to_pay == 0) |
466 | if (to_pay > query_money(pl)) return 0; |
|
|
467 | |
|
|
468 | to_pay = pay_from_container(pl, pl, to_pay); |
|
|
469 | |
|
|
470 | for (pouch=pl->inv; (pouch!=NULL) && (to_pay>0); pouch=pouch->below) { |
|
|
471 | if (pouch->type == CONTAINER |
|
|
472 | && QUERY_FLAG(pouch, FLAG_APPLIED) |
|
|
473 | && (pouch->race == NULL || strstr(pouch->race, "gold"))) { |
|
|
474 | to_pay = pay_from_container(pl, pouch, to_pay); |
|
|
475 | } |
|
|
476 | } |
|
|
477 | fix_player(pl); |
|
|
478 | return 1; |
506 | return 1; |
|
|
507 | if (to_pay > query_money (pl)) |
|
|
508 | return 0; |
|
|
509 | |
|
|
510 | to_pay = pay_from_container (pl, pl, to_pay); |
|
|
511 | |
|
|
512 | for (pouch = pl->inv; (pouch != NULL) && (to_pay > 0); pouch = pouch->below) |
|
|
513 | { |
|
|
514 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) |
|
|
515 | { |
|
|
516 | to_pay = pay_from_container (pl, pouch, to_pay); |
|
|
517 | } |
|
|
518 | } |
|
|
519 | fix_player (pl); |
|
|
520 | return 1; |
479 | } |
521 | } |
480 | |
522 | |
481 | /* DAMN: This is now a wrapper for pay_from_container, which is |
523 | /* DAMN: This is now a wrapper for pay_from_container, which is |
482 | * called for the player, then for each active container that can hold |
524 | * called for the player, then for each active container that can hold |
483 | * money until op is paid for. Change will be left wherever the last |
525 | * money until op is paid for. Change will be left wherever the last |
484 | * of the price was paid from. |
526 | * of the price was paid from. |
485 | */ |
527 | */ |
|
|
528 | int |
486 | int pay_for_item(object *op,object *pl) { |
529 | pay_for_item (object *op, object *pl) |
|
|
530 | { |
487 | uint64 to_pay = query_cost(op,pl,F_BUY | F_SHOP); |
531 | uint64 to_pay = query_cost (op, pl, F_BUY | F_SHOP); |
488 | object *pouch; |
532 | object *pouch; |
489 | uint64 saved_money; |
533 | uint64 saved_money; |
490 | |
534 | |
491 | if (to_pay==0) return 1; |
535 | if (to_pay == 0) |
492 | if (to_pay>query_money(pl)) return 0; |
|
|
493 | |
|
|
494 | /* We compare the paid price with the one for a player |
|
|
495 | * without bargaining skill. |
|
|
496 | * This determins the amount of exp (if any) gained for bargaining. |
|
|
497 | */ |
|
|
498 | saved_money = query_cost(op,pl,F_BUY | F_NO_BARGAIN | F_SHOP) - to_pay; |
|
|
499 | |
|
|
500 | if (saved_money > 0) |
|
|
501 | change_exp(pl,saved_money,"bargaining",SK_EXP_NONE); |
|
|
502 | |
|
|
503 | to_pay = pay_from_container(pl, pl, to_pay); |
|
|
504 | |
|
|
505 | for (pouch=pl->inv; (pouch!=NULL) && (to_pay>0); pouch=pouch->below) { |
|
|
506 | if (pouch->type == CONTAINER |
|
|
507 | && QUERY_FLAG(pouch, FLAG_APPLIED) |
|
|
508 | && (pouch->race == NULL || strstr(pouch->race, "gold"))) { |
|
|
509 | to_pay = pay_from_container(pl, pouch, to_pay); |
|
|
510 | } |
|
|
511 | } |
|
|
512 | if (settings.real_wiz == FALSE && QUERY_FLAG(pl, FLAG_WAS_WIZ)) |
|
|
513 | SET_FLAG(op, FLAG_WAS_WIZ); |
|
|
514 | fix_player(pl); |
|
|
515 | return 1; |
536 | return 1; |
|
|
537 | if (to_pay > query_money (pl)) |
|
|
538 | return 0; |
|
|
539 | |
|
|
540 | /* We compare the paid price with the one for a player |
|
|
541 | * without bargaining skill. |
|
|
542 | * This determins the amount of exp (if any) gained for bargaining. |
|
|
543 | */ |
|
|
544 | saved_money = query_cost (op, pl, F_BUY | F_NO_BARGAIN | F_SHOP) - to_pay; |
|
|
545 | |
|
|
546 | if (saved_money > 0) |
|
|
547 | change_exp (pl, saved_money, "bargaining", SK_EXP_NONE); |
|
|
548 | |
|
|
549 | to_pay = pay_from_container (pl, pl, to_pay); |
|
|
550 | |
|
|
551 | for (pouch = pl->inv; (pouch != NULL) && (to_pay > 0); pouch = pouch->below) |
|
|
552 | { |
|
|
553 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) |
|
|
554 | { |
|
|
555 | to_pay = pay_from_container (pl, pouch, to_pay); |
|
|
556 | } |
|
|
557 | } |
|
|
558 | if (settings.real_wiz == FALSE && QUERY_FLAG (pl, FLAG_WAS_WIZ)) |
|
|
559 | SET_FLAG (op, FLAG_WAS_WIZ); |
|
|
560 | fix_player (pl); |
|
|
561 | return 1; |
516 | } |
562 | } |
517 | |
563 | |
518 | /* This pays for the item, and takes the proper amount of money off |
564 | /* This pays for the item, and takes the proper amount of money off |
519 | * the player. |
565 | * the player. |
520 | * CF 0.91.4 - this function is mostly redone in order to fix a bug |
566 | * CF 0.91.4 - this function is mostly redone in order to fix a bug |
… | |
… | |
527 | * |
573 | * |
528 | * pouch is the container (pouch or player) to remove the coins from. |
574 | * pouch is the container (pouch or player) to remove the coins from. |
529 | * to_pay is the required amount. |
575 | * to_pay is the required amount. |
530 | * returns the amount still missing after using "pouch". |
576 | * returns the amount still missing after using "pouch". |
531 | */ |
577 | */ |
|
|
578 | static uint64 |
532 | static uint64 pay_from_container(object *pl, object *pouch, uint64 to_pay) { |
579 | pay_from_container (object *pl, object *pouch, uint64 to_pay) |
|
|
580 | { |
533 | int count, i; |
581 | int count, i; |
534 | uint64 remain; |
582 | uint64 remain; |
535 | object *tmp, *coin_objs[NUM_COINS], *next; |
583 | object *tmp, *coin_objs[NUM_COINS], *next; |
536 | archetype *at; |
584 | archetype *at; |
537 | |
585 | |
538 | if (pouch->type != PLAYER && pouch->type != CONTAINER) return to_pay; |
586 | if (pouch->type != PLAYER && pouch->type != CONTAINER) |
|
|
587 | return to_pay; |
539 | |
588 | |
540 | remain = to_pay; |
589 | remain = to_pay; |
541 | for (i=0; i<NUM_COINS; i++) coin_objs[i] = NULL; |
590 | for (i = 0; i < NUM_COINS; i++) |
|
|
591 | coin_objs[i] = NULL; |
542 | |
592 | |
543 | /* This hunk should remove all the money objects from the player/container */ |
593 | /* This hunk should remove all the money objects from the player/container */ |
544 | for (tmp=pouch->inv; tmp; tmp=next) { |
594 | for (tmp = pouch->inv; tmp; tmp = next) |
|
|
595 | { |
545 | next = tmp->below; |
596 | next = tmp->below; |
546 | |
597 | |
547 | if (tmp->type == MONEY) { |
598 | if (tmp->type == MONEY) |
|
|
599 | { |
548 | for (i=0; i<NUM_COINS; i++) { |
600 | for (i = 0; i < NUM_COINS; i++) |
549 | if (!strcmp(coins[NUM_COINS-1-i], tmp->arch->name) && |
601 | { |
550 | (tmp->value == tmp->arch->clone.value) ) { |
602 | if (!strcmp (coins[NUM_COINS - 1 - i], tmp->arch->name) && (tmp->value == tmp->arch->clone.value)) |
|
|
603 | { |
551 | |
604 | |
552 | /* This should not happen, but if it does, just * |
605 | /* This should not happen, but if it does, just * |
553 | * merge the two. */ |
606 | * merge the two. */ |
554 | if (coin_objs[i]!=NULL) { |
607 | if (coin_objs[i] != NULL) |
|
|
608 | { |
555 | LOG(llevError,"%s has two money entries of (%s)\n", |
609 | LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); |
556 | &pouch->name, coins[NUM_COINS-1-i]); |
|
|
557 | remove_ob(tmp); |
610 | remove_ob (tmp); |
558 | coin_objs[i]->nrof += tmp->nrof; |
611 | coin_objs[i]->nrof += tmp->nrof; |
559 | esrv_del_item(pl->contr, tmp->count); |
612 | esrv_del_item (pl->contr, tmp->count); |
560 | free_object(tmp); |
613 | free_object (tmp); |
561 | } |
614 | } |
562 | else { |
615 | else |
|
|
616 | { |
563 | remove_ob(tmp); |
617 | remove_ob (tmp); |
|
|
618 | if (pouch->type == PLAYER) |
564 | if(pouch->type==PLAYER) esrv_del_item(pl->contr, tmp->count); |
619 | esrv_del_item (pl->contr, tmp->count); |
565 | coin_objs[i] = tmp; |
620 | coin_objs[i] = tmp; |
566 | } |
621 | } |
567 | break; |
622 | break; |
568 | } |
623 | } |
569 | } |
624 | } |
570 | if (i==NUM_COINS) |
625 | if (i == NUM_COINS) |
571 | LOG(llevError,"in pay_for_item: Did not find string match for %s\n", &tmp->arch->name); |
626 | LOG (llevError, "in pay_for_item: Did not find string match for %s\n", &tmp->arch->name); |
572 | } |
627 | } |
573 | } |
628 | } |
574 | |
629 | |
575 | /* Fill in any gaps in the coin_objs array - needed to make change. */ |
630 | /* Fill in any gaps in the coin_objs array - needed to make change. */ |
576 | /* Note that the coin_objs array goes from least value to greatest value */ |
631 | /* Note that the coin_objs array goes from least value to greatest value */ |
577 | for (i=0; i<NUM_COINS; i++) |
632 | for (i = 0; i < NUM_COINS; i++) |
578 | if (coin_objs[i]==NULL) { |
633 | if (coin_objs[i] == NULL) |
|
|
634 | { |
579 | at = find_archetype(coins[NUM_COINS-1-i]); |
635 | at = find_archetype (coins[NUM_COINS - 1 - i]); |
|
|
636 | if (at == NULL) |
580 | if (at==NULL) LOG(llevError, "Could not find %s archetype\n", coins[NUM_COINS-1-i]); |
637 | LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]); |
581 | coin_objs[i] = arch_to_object (at); |
638 | coin_objs[i] = arch_to_object (at); |
582 | coin_objs[i]->nrof = 0; |
639 | coin_objs[i]->nrof = 0; |
583 | } |
640 | } |
584 | |
641 | |
585 | for (i=0; i<NUM_COINS; i++) { |
642 | for (i = 0; i < NUM_COINS; i++) |
|
|
643 | { |
586 | int num_coins; |
644 | int num_coins; |
587 | |
645 | |
588 | if (coin_objs[i]->nrof*coin_objs[i]->value> remain) { |
646 | if (coin_objs[i]->nrof * coin_objs[i]->value > remain) |
|
|
647 | { |
589 | num_coins = remain / coin_objs[i]->value; |
648 | num_coins = remain / coin_objs[i]->value; |
590 | if ( (uint64) num_coins * (uint64) coin_objs[i]->value < remain ) num_coins++; |
649 | if ((uint64) num_coins * (uint64) coin_objs[i]->value < remain) |
|
|
650 | num_coins++; |
|
|
651 | } |
591 | } else { |
652 | else |
|
|
653 | { |
592 | num_coins = coin_objs[i]->nrof; |
654 | num_coins = coin_objs[i]->nrof; |
593 | } |
655 | } |
594 | remain -= (sint64) num_coins * (sint64)coin_objs[i]->value; |
656 | remain -= (sint64) num_coins *(sint64) coin_objs[i]->value; |
|
|
657 | |
595 | coin_objs[i]->nrof -= num_coins; |
658 | coin_objs[i]->nrof -= num_coins; |
596 | /* Now start making change. Start at the coin value |
659 | /* Now start making change. Start at the coin value |
597 | * below the one we just did, and work down to |
660 | * below the one we just did, and work down to |
598 | * the lowest value. |
661 | * the lowest value. |
599 | */ |
662 | */ |
600 | count=i-1; |
663 | count = i - 1; |
601 | while (remain<0 && count>=0) { |
664 | while (remain < 0 && count >= 0) |
|
|
665 | { |
602 | num_coins = -remain/ coin_objs[count]->value; |
666 | num_coins = -remain / coin_objs[count]->value; |
603 | coin_objs[count]->nrof += num_coins; |
667 | coin_objs[count]->nrof += num_coins; |
604 | remain += num_coins * coin_objs[count]->value; |
668 | remain += num_coins * coin_objs[count]->value; |
605 | count--; |
669 | count--; |
606 | } |
670 | } |
607 | } |
671 | } |
608 | for (i=0; i<NUM_COINS; i++) { |
672 | for (i = 0; i < NUM_COINS; i++) |
|
|
673 | { |
609 | if (coin_objs[i]->nrof) { |
674 | if (coin_objs[i]->nrof) |
|
|
675 | { |
610 | object *tmp = insert_ob_in_ob(coin_objs[i], pouch); |
676 | object *tmp = insert_ob_in_ob (coin_objs[i], pouch); |
611 | |
677 | |
612 | esrv_send_item(pl, tmp); |
678 | esrv_send_item (pl, tmp); |
613 | esrv_send_item (pl, pouch); |
679 | esrv_send_item (pl, pouch); |
|
|
680 | if (pl != pouch) |
614 | if (pl != pouch) esrv_update_item (UPD_WEIGHT, pl, pouch); |
681 | esrv_update_item (UPD_WEIGHT, pl, pouch); |
615 | if (pl->type != PLAYER) { |
682 | if (pl->type != PLAYER) |
|
|
683 | { |
616 | esrv_send_item (pl, pl); |
684 | esrv_send_item (pl, pl); |
617 | } |
685 | } |
|
|
686 | } |
618 | } else { |
687 | else |
|
|
688 | { |
619 | free_object(coin_objs[i]); |
689 | free_object (coin_objs[i]); |
620 | } |
690 | } |
621 | } |
691 | } |
622 | return(remain); |
692 | return (remain); |
623 | } |
693 | } |
624 | |
694 | |
625 | /* Checks all unpaid items in op's inventory, adds up all the money they |
695 | /* Checks all unpaid items in op's inventory, adds up all the money they |
626 | * have, and checks that they can actually afford what they want to buy. |
696 | * have, and checks that they can actually afford what they want to buy. |
627 | * Returns 1 if they can, and 0 if they can't. also prints an appropriate message |
697 | * Returns 1 if they can, and 0 if they can't. also prints an appropriate message |
628 | * to the player |
698 | * to the player |
629 | */ |
699 | */ |
630 | |
700 | |
|
|
701 | int |
631 | int can_pay(object *pl) { |
702 | can_pay (object *pl) |
|
|
703 | { |
632 | int unpaid_count = 0, i; |
704 | int unpaid_count = 0, i; |
633 | uint64 unpaid_price = 0; |
705 | uint64 unpaid_price = 0; |
634 | uint64 player_wealth = query_money(pl); |
706 | uint64 player_wealth = query_money (pl); |
635 | object *item; |
707 | object *item; |
636 | uint32 coincount[NUM_COINS]; |
708 | uint32 coincount[NUM_COINS]; |
|
|
709 | |
637 | if (!pl || pl->type != PLAYER) { |
710 | if (!pl || pl->type != PLAYER) |
|
|
711 | { |
638 | LOG(llevError, "can_pay(): called against something that isn't a player\n"); |
712 | LOG (llevError, "can_pay(): called against something that isn't a player\n"); |
639 | return 0; |
713 | return 0; |
640 | } |
714 | } |
641 | for (i=0; i< NUM_COINS; i++) coincount[i] = 0; |
715 | for (i = 0; i < NUM_COINS; i++) |
|
|
716 | coincount[i] = 0; |
642 | for (item = pl->inv;item;) { |
717 | for (item = pl->inv; item;) |
|
|
718 | { |
|
|
719 | if QUERY_FLAG |
643 | if QUERY_FLAG(item, FLAG_UNPAID) { |
720 | (item, FLAG_UNPAID) |
|
|
721 | { |
644 | unpaid_count++; |
722 | unpaid_count++; |
645 | unpaid_price += query_cost(item, pl, F_BUY | F_SHOP); |
723 | unpaid_price += query_cost (item, pl, F_BUY | F_SHOP); |
646 | } |
724 | } |
647 | /* merely converting the player's monetary wealth won't do, if we did that, |
725 | /* merely converting the player's monetary wealth won't do, if we did that, |
648 | * we could print the wrong numbers for the coins, so we count the money instead |
726 | * we could print the wrong numbers for the coins, so we count the money instead |
649 | */ |
727 | */ |
650 | for (i=0; i< NUM_COINS; i++) |
728 | for (i = 0; i < NUM_COINS; i++) |
651 | if (!strcmp(coins[i], item->arch->name)) |
729 | if (!strcmp (coins[i], item->arch->name)) |
652 | coincount[i] += item->nrof; |
730 | coincount[i] += item->nrof; |
|
|
731 | if (item->inv) |
653 | if (item->inv) item = item->inv; |
732 | item = item->inv; |
654 | else if (item->below) item = item->below; |
733 | else if (item->below) |
|
|
734 | item = item->below; |
655 | else if (item->env && item->env != pl && item->env->below) item = item->env->below; |
735 | else if (item->env && item->env != pl && item->env->below) |
|
|
736 | item = item->env->below; |
|
|
737 | else |
656 | else item = NULL; |
738 | item = NULL; |
657 | } |
739 | } |
658 | if (unpaid_price > player_wealth) { |
740 | if (unpaid_price > player_wealth) |
|
|
741 | { |
659 | char buf[MAX_BUF]; |
742 | char buf[MAX_BUF]; |
660 | char cost[MAX_BUF]; |
743 | char cost[MAX_BUF]; |
661 | char missing[MAX_BUF]; |
744 | char missing[MAX_BUF]; |
662 | |
745 | |
663 | sprintf(cost, "%s", cost_string_from_value (unpaid_price, 0)); |
746 | sprintf (cost, "%s", cost_string_from_value (unpaid_price, 0)); |
664 | sprintf(missing, "%s", cost_string_from_value (unpaid_price - player_wealth, 0)); |
747 | sprintf (missing, "%s", cost_string_from_value (unpaid_price - player_wealth, 0)); |
665 | |
748 | |
666 | sprintf(buf, "You have %d unpaid items that would cost you %s. You need another %s to be able to afford that.", |
749 | sprintf (buf, "You have %d unpaid items that would cost you %s. You need another %s to be able to afford that.", |
667 | unpaid_count, cost, missing); |
750 | unpaid_count, cost, missing); |
668 | new_draw_info(NDI_UNIQUE, 0, pl, buf); |
751 | new_draw_info (NDI_UNIQUE, 0, pl, buf); |
669 | return 0; |
752 | return 0; |
670 | } |
753 | } |
|
|
754 | else |
671 | else return 1; |
755 | return 1; |
672 | } |
756 | } |
673 | |
757 | |
674 | |
758 | |
675 | /* Better get_payment, descends containers looking for |
759 | /* Better get_payment, descends containers looking for |
676 | * unpaid items, and pays for them. |
760 | * unpaid items, and pays for them. |
… | |
… | |
679 | * pl is the player buying the stuff. |
763 | * pl is the player buying the stuff. |
680 | * op is the object we are examining. If op has |
764 | * op is the object we are examining. If op has |
681 | * and inventory, we examine that. IF there are objects |
765 | * and inventory, we examine that. IF there are objects |
682 | * below op, we descend down. |
766 | * below op, we descend down. |
683 | */ |
767 | */ |
|
|
768 | int |
684 | int get_payment(object *pl, object *op) { |
769 | get_payment (object *pl, object *op) |
|
|
770 | { |
685 | char buf[MAX_BUF]; |
771 | char buf[MAX_BUF]; |
686 | int ret=1; |
772 | int ret = 1; |
687 | |
773 | |
688 | if (op!=NULL&&op->inv) |
774 | if (op != NULL && op->inv) |
689 | ret = get_payment(pl, op->inv); |
775 | ret = get_payment (pl, op->inv); |
690 | |
776 | |
691 | if (!ret) |
777 | if (!ret) |
692 | return 0; |
778 | return 0; |
693 | |
779 | |
694 | if (op!=NULL&&op->below) |
780 | if (op != NULL && op->below) |
695 | ret = get_payment (pl, op->below); |
781 | ret = get_payment (pl, op->below); |
696 | |
782 | |
697 | if (!ret) |
783 | if (!ret) |
698 | return 0; |
784 | return 0; |
699 | |
785 | |
700 | if(op!=NULL&&QUERY_FLAG(op,FLAG_UNPAID)) { |
786 | if (op != NULL && QUERY_FLAG (op, FLAG_UNPAID)) |
|
|
787 | { |
701 | strncpy(buf,query_cost_string(op,pl,F_BUY | F_SHOP),MAX_BUF); |
788 | strncpy (buf, query_cost_string (op, pl, F_BUY | F_SHOP), MAX_BUF); |
702 | buf[MAX_BUF-1] = '\0'; |
789 | buf[MAX_BUF - 1] = '\0'; |
703 | if(!pay_for_item(op,pl)) { |
790 | if (!pay_for_item (op, pl)) |
|
|
791 | { |
704 | uint64 i=query_cost(op,pl,F_BUY | F_SHOP) - query_money(pl); |
792 | uint64 i = query_cost (op, pl, F_BUY | F_SHOP) - query_money (pl); |
|
|
793 | |
705 | CLEAR_FLAG(op, FLAG_UNPAID); |
794 | CLEAR_FLAG (op, FLAG_UNPAID); |
706 | new_draw_info_format(NDI_UNIQUE, 0, pl, |
795 | new_draw_info_format (NDI_UNIQUE, 0, pl, "You lack %s to buy %s.", cost_string_from_value (i, 0), query_name (op)); |
707 | "You lack %s to buy %s.", cost_string_from_value (i, 0), |
|
|
708 | query_name(op)); |
|
|
709 | SET_FLAG(op, FLAG_UNPAID); |
796 | SET_FLAG (op, FLAG_UNPAID); |
710 | return 0; |
797 | return 0; |
|
|
798 | } |
711 | } else { |
799 | else |
|
|
800 | { |
712 | object *tmp; |
801 | object *tmp; |
713 | tag_t c = op->count; |
802 | tag_t c = op->count; |
714 | |
803 | |
715 | CLEAR_FLAG(op, FLAG_UNPAID); |
804 | CLEAR_FLAG (op, FLAG_UNPAID); |
716 | CLEAR_FLAG(op, FLAG_PLAYER_SOLD); |
805 | CLEAR_FLAG (op, FLAG_PLAYER_SOLD); |
717 | new_draw_info_format(NDI_UNIQUE, 0, op, |
806 | new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op)); |
718 | "You paid %s for %s.",buf,query_name(op)); |
|
|
719 | tmp=merge_ob(op,NULL); |
807 | tmp = merge_ob (op, NULL); |
720 | if (pl->type == PLAYER) { |
808 | if (pl->type == PLAYER) |
|
|
809 | { |
|
|
810 | if (tmp) |
721 | if (tmp) { /* it was merged */ |
811 | { /* it was merged */ |
722 | esrv_del_item (pl->contr, c); |
812 | esrv_del_item (pl->contr, c); |
723 | op = tmp; |
813 | op = tmp; |
724 | } |
814 | } |
725 | esrv_send_item(pl, op); |
815 | esrv_send_item (pl, op); |
726 | } |
816 | } |
727 | } |
817 | } |
728 | } |
818 | } |
729 | return 1; |
819 | return 1; |
730 | } |
820 | } |
731 | |
821 | |
732 | /* written by elmex: |
822 | /* written by elmex: |
733 | * moved this code from sell_item () here to have a function |
823 | * moved this code from sell_item () here to have a function |
734 | * that pays the player an amount. Mainly put the code here to |
824 | * that pays the player an amount. Mainly put the code here to |
… | |
… | |
738 | * in his inventory anyway. This is the best alternative to not pay any money |
828 | * in his inventory anyway. This is the best alternative to not pay any money |
739 | * or put it on the ground under the player. This way the player can still |
829 | * or put it on the ground under the player. This way the player can still |
740 | * go somewhere and unload the money at a safe place. |
830 | * go somewhere and unload the money at a safe place. |
741 | * |
831 | * |
742 | */ |
832 | */ |
|
|
833 | void |
743 | void pay_player (object *pl, uint64 amount) { |
834 | pay_player (object *pl, uint64 amount) |
|
|
835 | { |
744 | int count = 0; |
836 | int count = 0; |
745 | archetype *at = 0; |
837 | archetype *at = 0; |
746 | object *pouch = 0, *tmp = 0; |
838 | object *pouch = 0, *tmp = 0; |
747 | |
839 | |
748 | for (count = 0; coins[count] != NULL; count++) |
840 | for (count = 0; coins[count] != NULL; count++) |
749 | { |
841 | { |
750 | at = find_archetype (coins[count]); |
842 | at = find_archetype (coins[count]); |
751 | |
843 | |
752 | if (at == NULL) |
844 | if (at == NULL) |
753 | LOG(llevError, "Could not find %s archetype\n", coins[count]); |
845 | LOG (llevError, "Could not find %s archetype\n", coins[count]); |
754 | else if ((amount / at->clone.value) > 0) |
846 | else if ((amount / at->clone.value) > 0) |
755 | { |
847 | { |
756 | for (pouch=pl->inv; pouch; pouch=pouch->below) |
848 | for (pouch = pl->inv; pouch; pouch = pouch->below) |
757 | { |
849 | { |
758 | if (pouch->type == CONTAINER |
850 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && pouch->race && strstr (pouch->race, "gold")) |
759 | && QUERY_FLAG (pouch, FLAG_APPLIED) |
|
|
760 | && pouch->race |
|
|
761 | && strstr (pouch->race, "gold")) |
|
|
762 | { |
851 | { |
763 | int w = at->clone.weight * (100 - pouch->stats.Str) / 100; |
852 | int w = at->clone.weight * (100 - pouch->stats.Str) / 100; |
764 | int n = amount / at->clone.value; |
853 | int n = amount / at->clone.value; |
765 | |
854 | |
766 | if (w == 0) |
855 | if (w == 0) |
767 | w = 1; /* Prevent divide by zero */ |
856 | w = 1; /* Prevent divide by zero */ |
768 | |
857 | |
769 | if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit)) |
858 | if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit)) |
770 | { |
859 | { |
771 | if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) |
860 | if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) |
772 | n = (pouch->weight_limit - pouch->carrying) / w; |
861 | n = (pouch->weight_limit - pouch->carrying) / w; |
773 | |
862 | |
774 | tmp = arch_to_object (at); |
863 | tmp = arch_to_object (at); |
775 | tmp->nrof = n; |
864 | tmp->nrof = n; |
776 | amount -= (uint64)tmp->nrof * (uint64)tmp->value; |
865 | amount -= (uint64) tmp->nrof * (uint64) tmp->value; |
777 | tmp = insert_ob_in_ob (tmp, pouch); |
866 | tmp = insert_ob_in_ob (tmp, pouch); |
778 | esrv_send_item (pl, tmp); |
867 | esrv_send_item (pl, tmp); |
779 | esrv_send_item (pl, pouch); |
868 | esrv_send_item (pl, pouch); |
780 | esrv_update_item (UPD_WEIGHT, pl, pouch); |
869 | esrv_update_item (UPD_WEIGHT, pl, pouch); |
781 | esrv_send_item (pl, pl); |
870 | esrv_send_item (pl, pl); |
… | |
… | |
785 | |
874 | |
786 | if (amount / at->clone.value > 0) |
875 | if (amount / at->clone.value > 0) |
787 | { |
876 | { |
788 | tmp = arch_to_object (at); |
877 | tmp = arch_to_object (at); |
789 | tmp->nrof = amount / tmp->value; |
878 | tmp->nrof = amount / tmp->value; |
790 | amount -= (uint64)tmp->nrof * (uint64)tmp->value; |
879 | amount -= (uint64) tmp->nrof * (uint64) tmp->value; |
791 | tmp = insert_ob_in_ob (tmp, pl); |
880 | tmp = insert_ob_in_ob (tmp, pl); |
792 | esrv_send_item (pl, tmp); |
881 | esrv_send_item (pl, tmp); |
793 | esrv_send_item (pl, pl); |
882 | esrv_send_item (pl, pl); |
794 | } |
883 | } |
795 | } |
884 | } |
796 | } |
885 | } |
797 | |
886 | |
798 | if (amount != 0) |
887 | if (amount != 0) |
799 | #ifndef WIN32 |
888 | #ifndef WIN32 |
800 | LOG (llevError,"Warning - payment in pay_player () not zero: %llu\n", amount); |
889 | LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount); |
801 | #else |
890 | #else |
802 | LOG (llevError,"Warning - payment in pay_player () not zero: %I64u\n", amount); |
891 | LOG (llevError, "Warning - payment in pay_player () not zero: %I64u\n", amount); |
803 | #endif |
892 | #endif |
804 | } |
893 | } |
805 | |
894 | |
806 | /* elmex: this is for the bank plugin :( */ |
895 | /* elmex: this is for the bank plugin :( */ |
|
|
896 | uint64 |
807 | uint64 pay_player_arch (object *pl, const char *arch, uint64 amount) { |
897 | pay_player_arch (object *pl, const char *arch, uint64 amount) |
|
|
898 | { |
808 | archetype *at = find_archetype (arch); |
899 | archetype *at = find_archetype (arch); |
809 | object *tmp = NULL; |
900 | object *tmp = NULL; |
810 | |
901 | |
811 | if (at == NULL) |
902 | if (at == NULL) |
812 | return 0; |
903 | return 0; |
… | |
… | |
828 | * buy item. |
919 | * buy item. |
829 | * |
920 | * |
830 | * Modified to fill available race: gold containers before dumping |
921 | * Modified to fill available race: gold containers before dumping |
831 | * remaining coins in character's inventory. |
922 | * remaining coins in character's inventory. |
832 | */ |
923 | */ |
|
|
924 | void |
833 | void sell_item(object *op, object *pl) { |
925 | sell_item (object *op, object *pl) |
|
|
926 | { |
834 | uint64 amount = query_cost (op,pl,F_SELL | F_SHOP), extra_gain; |
927 | uint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain; |
835 | |
928 | |
836 | if(pl==NULL || pl->type != PLAYER) |
929 | if (pl == NULL || pl->type != PLAYER) |
837 | { |
930 | { |
838 | LOG(llevDebug,"Object other than player tried to sell something.\n"); |
931 | LOG (llevDebug, "Object other than player tried to sell something.\n"); |
839 | return; |
932 | return; |
840 | } |
933 | } |
841 | |
934 | |
842 | op->custom_name = 0; |
935 | op->custom_name = 0; |
843 | |
936 | |
844 | if(!amount) |
937 | if (!amount) |
845 | { |
938 | { |
846 | new_draw_info_format (NDI_UNIQUE, 0, pl, |
939 | new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.", query_name (op)); |
847 | "We're not interested in %s.", query_name (op)); |
|
|
848 | |
940 | |
849 | /* Even if the character doesn't get anything for it, it may still be |
941 | /* Even if the character doesn't get anything for it, it may still be |
850 | * worth something. If so, make it unpaid |
942 | * worth something. If so, make it unpaid |
851 | */ |
943 | */ |
852 | if (op->value) |
944 | if (op->value) |
853 | { |
945 | { |
854 | SET_FLAG(op, FLAG_UNPAID); |
946 | SET_FLAG (op, FLAG_UNPAID); |
855 | SET_FLAG(op, FLAG_PLAYER_SOLD); |
947 | SET_FLAG (op, FLAG_PLAYER_SOLD); |
856 | } |
948 | } |
857 | |
949 | |
858 | identify (op); |
950 | identify (op); |
859 | return; |
951 | return; |
860 | } |
952 | } |
… | |
… | |
865 | * exp/10 -> 1 for each gold coin |
957 | * exp/10 -> 1 for each gold coin |
866 | */ |
958 | */ |
867 | extra_gain = amount - query_cost (op, pl, F_SELL | F_NO_BARGAIN | F_SHOP); |
959 | extra_gain = amount - query_cost (op, pl, F_SELL | F_NO_BARGAIN | F_SHOP); |
868 | |
960 | |
869 | if (extra_gain > 0) |
961 | if (extra_gain > 0) |
870 | change_exp(pl, extra_gain / 10, "bargaining", SK_EXP_NONE); |
962 | change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE); |
871 | |
963 | |
872 | pay_player (pl, amount); |
964 | pay_player (pl, amount); |
873 | |
965 | |
874 | new_draw_info_format (NDI_UNIQUE, 0, pl, |
966 | new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.", query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op)); |
875 | "You receive %s for %s.", query_cost_string (op, pl, F_SELL | F_SHOP), |
|
|
876 | query_name (op)); |
|
|
877 | |
967 | |
878 | SET_FLAG (op, FLAG_UNPAID); |
968 | SET_FLAG (op, FLAG_UNPAID); |
879 | identify (op); |
969 | identify (op); |
880 | } |
970 | } |
881 | |
971 | |
… | |
… | |
883 | /* returns a double that is the ratio of the price that a shop will offer for |
973 | /* returns a double that is the ratio of the price that a shop will offer for |
884 | * item based on the shops specialisation. Does not take account of greed, |
974 | * item based on the shops specialisation. Does not take account of greed, |
885 | * returned value is between (2*SPECIALISATION_EFFECT-1) and 1 and in any |
975 | * returned value is between (2*SPECIALISATION_EFFECT-1) and 1 and in any |
886 | * event is never less than 0.1 (calling functions divide by it) |
976 | * event is never less than 0.1 (calling functions divide by it) |
887 | */ |
977 | */ |
|
|
978 | static double |
888 | static double shop_specialisation_ratio(const object *item, const mapstruct *map) { |
979 | shop_specialisation_ratio (const object *item, const mapstruct *map) |
|
|
980 | { |
889 | shopitems *items=map->shopitems; |
981 | shopitems *items = map->shopitems; |
890 | double ratio = SPECIALISATION_EFFECT, likedness=0.001; |
982 | double ratio = SPECIALISATION_EFFECT, likedness = 0.001; |
891 | int i; |
983 | int i; |
892 | |
984 | |
893 | if (item==NULL) { |
985 | if (item == NULL) |
|
|
986 | { |
894 | LOG(llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path); |
987 | LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path); |
895 | return 0; |
988 | return 0; |
896 | } |
989 | } |
897 | if (!item->type) { |
990 | if (!item->type) |
|
|
991 | { |
898 | LOG(llevError, "shop_specialisation_ratio: passed an item with an invalid type\n"); |
992 | LOG (llevError, "shop_specialisation_ratio: passed an item with an invalid type\n"); |
899 | /* |
993 | /* |
900 | * I'm not really sure what the /right/ thing to do here is, these types of |
994 | * I'm not really sure what the /right/ thing to do here is, these types of |
901 | * item shouldn't exist anyway, but returning the ratio is probably the best bet.." |
995 | * item shouldn't exist anyway, but returning the ratio is probably the best bet.." |
902 | */ |
996 | */ |
903 | return ratio; |
997 | return ratio; |
904 | } |
998 | } |
905 | if (map->shopitems) { |
999 | if (map->shopitems) |
|
|
1000 | { |
906 | for (i=0; i<items[0].index; i++) |
1001 | for (i = 0; i < items[0].index; i++) |
907 | if (items[i].typenum==item->type || (!items[i].typenum && likedness == 0.001)) |
1002 | if (items[i].typenum == item->type || (!items[i].typenum && likedness == 0.001)) |
908 | likedness = items[i].strength/100.0; |
1003 | likedness = items[i].strength / 100.0; |
909 | } |
1004 | } |
910 | if (likedness > 1.0) { /* someone has been rather silly with the map headers. */ |
1005 | if (likedness > 1.0) |
|
|
1006 | { /* someone has been rather silly with the map headers. */ |
911 | LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", |
1007 | LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, map->path); |
912 | item->type, map->path); |
|
|
913 | likedness = 1.0; |
1008 | likedness = 1.0; |
914 | } |
1009 | } |
915 | if (likedness < -1.0) { |
1010 | if (likedness < -1.0) |
|
|
1011 | { |
916 | LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", |
1012 | LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, map->path); |
917 | item->type, map->path); |
|
|
918 | likedness = -1.0; |
1013 | likedness = -1.0; |
919 | } |
1014 | } |
920 | ratio = ratio + (1.0-ratio) * likedness; |
1015 | ratio = ratio + (1.0 - ratio) * likedness; |
921 | if (ratio <= 0.1) ratio=0.1; /* if the ratio were much lower than this, we would get silly prices */ |
1016 | if (ratio <= 0.1) |
|
|
1017 | ratio = 0.1; /* if the ratio were much lower than this, we would get silly prices */ |
922 | return ratio; |
1018 | return ratio; |
923 | } |
1019 | } |
924 | |
1020 | |
925 | /*returns the greed of the shop on map, or 1 if it isn't specified. */ |
1021 | /*returns the greed of the shop on map, or 1 if it isn't specified. */ |
|
|
1022 | static double |
926 | static double shop_greed(const mapstruct *map) { |
1023 | shop_greed (const mapstruct *map) |
|
|
1024 | { |
927 | double greed=1.0; |
1025 | double greed = 1.0; |
|
|
1026 | |
928 | if (map->shopgreed) |
1027 | if (map->shopgreed) |
929 | return map->shopgreed; |
1028 | return map->shopgreed; |
930 | return greed; |
1029 | return greed; |
931 | } |
1030 | } |
932 | |
1031 | |
933 | /* Returns a double based on how much the shopkeeper approves of the player. |
1032 | /* Returns a double based on how much the shopkeeper approves of the player. |
934 | * this is based on the race of the shopkeeper and that of the player. |
1033 | * this is based on the race of the shopkeeper and that of the player. |
935 | */ |
1034 | */ |
|
|
1035 | double |
936 | double shopkeeper_approval(const mapstruct *map, const object *player) { |
1036 | shopkeeper_approval (const mapstruct *map, const object *player) |
|
|
1037 | { |
937 | double approval=1.0; |
1038 | double approval = 1.0; |
938 | |
1039 | |
939 | if (map->shoprace) { |
1040 | if (map->shoprace) |
|
|
1041 | { |
940 | approval=NEUTRAL_RATIO; |
1042 | approval = NEUTRAL_RATIO; |
941 | if (player->race && !strcmp(player->race, map->shoprace)) approval = 1.0; |
1043 | if (player->race && !strcmp (player->race, map->shoprace)) |
|
|
1044 | approval = 1.0; |
942 | } |
1045 | } |
943 | return approval; |
1046 | return approval; |
944 | } |
1047 | } |
945 | |
1048 | |
946 | /* limit the value of items based on the wealth of the shop. If the item is close |
1049 | /* limit the value of items based on the wealth of the shop. If the item is close |
947 | * to the maximum value a shop will offer, we start to reduce it, if the item is |
1050 | * to the maximum value a shop will offer, we start to reduce it, if the item is |
948 | * below the minimum value the shop is prepared to trade in, then we don't |
1051 | * below the minimum value the shop is prepared to trade in, then we don't |
949 | * want it and offer nothing. If it isn't a shop, check whether we should do generic |
1052 | * want it and offer nothing. If it isn't a shop, check whether we should do generic |
950 | * value reduction. |
1053 | * value reduction. |
951 | * |
1054 | * |
952 | */ |
1055 | */ |
|
|
1056 | static uint64 |
953 | static uint64 value_limit (uint64 val, int quantity, const object *who, int isshop) { |
1057 | value_limit (uint64 val, int quantity, const object *who, int isshop) |
|
|
1058 | { |
954 | uint64 newval, unit_price, tmpshopmax; |
1059 | uint64 newval, unit_price, tmpshopmax; |
955 | mapstruct *map; |
1060 | mapstruct *map; |
956 | |
1061 | |
957 | unit_price = val / quantity; |
1062 | unit_price = val / quantity; |
958 | |
1063 | |
… | |
… | |
965 | } |
1070 | } |
966 | else |
1071 | else |
967 | { |
1072 | { |
968 | if (!who->map) |
1073 | if (!who->map) |
969 | { |
1074 | { |
970 | LOG(llevError, "value_limit: asked shop price for ob %s on NULL map\n", &who->name); |
1075 | LOG (llevError, "value_limit: asked shop price for ob %s on NULL map\n", &who->name); |
971 | return val; |
1076 | return val; |
972 | } |
1077 | } |
973 | |
1078 | |
974 | map = who->map; |
1079 | map = who->map; |
975 | |
1080 | |
976 | tmpshopmax = map->shopmax ? map->shopmax : 100000; // 20 royalties default |
1081 | tmpshopmax = map->shopmax ? map->shopmax : 100000; // 20 royalties default |
977 | |
1082 | |
978 | if (map->shopmin && unit_price < map->shopmin) |
1083 | if (map->shopmin && unit_price < map->shopmin) |
979 | return 0; |
1084 | return 0; |
980 | else if (unit_price > tmpshopmax / 2) |
1085 | else if (unit_price > tmpshopmax / 2) |
981 | newval = MIN ((tmpshopmax / 2) + isqrt (unit_price - tmpshopmax / 2), tmpshopmax); |
1086 | newval = MIN ((tmpshopmax / 2) + isqrt (unit_price - tmpshopmax / 2), tmpshopmax); |
982 | else |
1087 | else |
983 | newval=unit_price; |
1088 | newval = unit_price; |
984 | } |
1089 | } |
985 | |
1090 | |
986 | newval *= quantity; |
1091 | newval *= quantity; |
987 | |
1092 | |
988 | return newval; |
1093 | return newval; |
989 | } |
1094 | } |
990 | |
1095 | |
991 | /* gives a desciption of the shop on their current map to the player op. */ |
1096 | /* gives a desciption of the shop on their current map to the player op. */ |
|
|
1097 | int |
992 | int describe_shop(const object *op) { |
1098 | describe_shop (const object *op) |
|
|
1099 | { |
993 | mapstruct *map = op->map; |
1100 | mapstruct *map = op->map; |
|
|
1101 | |
994 | /*shopitems *items=map->shopitems;*/ |
1102 | /*shopitems *items=map->shopitems; */ |
995 | int pos=0, i; |
1103 | int pos = 0, i; |
996 | double opinion=0; |
1104 | double opinion = 0; |
997 | char tmp[MAX_BUF]="\0"; |
1105 | char tmp[MAX_BUF] = "\0"; |
|
|
1106 | |
998 | if (op->type != PLAYER) return 0; |
1107 | if (op->type != PLAYER) |
|
|
1108 | return 0; |
999 | |
1109 | |
1000 | /*check if there is a shop specified for this map */ |
1110 | /*check if there is a shop specified for this map */ |
1001 | if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax) { |
1111 | if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax) |
|
|
1112 | { |
1002 | new_draw_info(NDI_UNIQUE,0,op,"From looking at the nearby shop you determine that it trades in:"); |
1113 | new_draw_info (NDI_UNIQUE, 0, op, "From looking at the nearby shop you determine that it trades in:"); |
1003 | if (map->shopitems) { |
1114 | if (map->shopitems) |
|
|
1115 | { |
1004 | for (i=0; i < map->shopitems[0].index; i++) { |
1116 | for (i = 0; i < map->shopitems[0].index; i++) |
|
|
1117 | { |
1005 | if (map->shopitems[i].name && map->shopitems[i].strength > 10) { |
1118 | if (map->shopitems[i].name && map->shopitems[i].strength > 10) |
|
|
1119 | { |
1006 | snprintf(tmp+pos, sizeof(tmp)-pos, "%s, ", map->shopitems[i].name_pl); |
1120 | snprintf (tmp + pos, sizeof (tmp) - pos, "%s, ", map->shopitems[i].name_pl); |
1007 | pos += strlen(tmp+pos); |
1121 | pos += strlen (tmp + pos); |
1008 | } |
1122 | } |
1009 | } |
1123 | } |
1010 | } |
1124 | } |
|
|
1125 | if (!pos) |
1011 | if (!pos) strcat(tmp, "a little of everything."); |
1126 | strcat (tmp, "a little of everything."); |
1012 | |
1127 | |
1013 | /* format the string into a list */ |
1128 | /* format the string into a list */ |
1014 | make_list_like(tmp); |
1129 | make_list_like (tmp); |
1015 | new_draw_info_format(NDI_UNIQUE, 0, op, "%s", tmp); |
1130 | new_draw_info_format (NDI_UNIQUE, 0, op, "%s", tmp); |
1016 | |
1131 | |
1017 | if (map->shopmax) |
1132 | if (map->shopmax) |
1018 | new_draw_info_format(NDI_UNIQUE,0,op,"It won't trade for items above %s.", |
1133 | new_draw_info_format (NDI_UNIQUE, 0, op, "It won't trade for items above %s.", cost_string_from_value (map->shopmax, 0)); |
1019 | cost_string_from_value(map->shopmax, 0)); |
|
|
1020 | if (map->shopmin) |
1134 | if (map->shopmin) |
1021 | new_draw_info_format(NDI_UNIQUE,0,op,"It won't trade in items worth less than %s.", |
1135 | new_draw_info_format (NDI_UNIQUE, 0, op, "It won't trade in items worth less than %s.", cost_string_from_value (map->shopmin, 0)); |
1022 | cost_string_from_value(map->shopmin, 0)); |
|
|
1023 | if (map->shopgreed) { |
1136 | if (map->shopgreed) |
|
|
1137 | { |
1024 | if (map->shopgreed >2.0) |
1138 | if (map->shopgreed > 2.0) |
1025 | new_draw_info(NDI_UNIQUE,0,op,"It tends to overcharge massively."); |
1139 | new_draw_info (NDI_UNIQUE, 0, op, "It tends to overcharge massively."); |
1026 | else if (map->shopgreed >1.5) |
1140 | else if (map->shopgreed > 1.5) |
1027 | new_draw_info(NDI_UNIQUE,0,op,"It tends to overcharge substantially."); |
1141 | new_draw_info (NDI_UNIQUE, 0, op, "It tends to overcharge substantially."); |
1028 | else if (map->shopgreed >1.1) |
1142 | else if (map->shopgreed > 1.1) |
1029 | new_draw_info(NDI_UNIQUE,0,op,"It tends to overcharge slightly."); |
1143 | new_draw_info (NDI_UNIQUE, 0, op, "It tends to overcharge slightly."); |
1030 | else if (map->shopgreed <0.9) |
1144 | else if (map->shopgreed < 0.9) |
1031 | new_draw_info(NDI_UNIQUE,0,op,"It tends to undercharge."); |
1145 | new_draw_info (NDI_UNIQUE, 0, op, "It tends to undercharge."); |
1032 | } |
1146 | } |
1033 | if (map->shoprace) { |
1147 | if (map->shoprace) |
|
|
1148 | { |
1034 | opinion=shopkeeper_approval(map, op); |
1149 | opinion = shopkeeper_approval (map, op); |
1035 | if (opinion > 0.8) |
1150 | if (opinion > 0.8) |
1036 | new_draw_info(NDI_UNIQUE,0,op,"You think the shopkeeper likes you."); |
1151 | new_draw_info (NDI_UNIQUE, 0, op, "You think the shopkeeper likes you."); |
1037 | else if (opinion > 0.5) |
1152 | else if (opinion > 0.5) |
1038 | new_draw_info(NDI_UNIQUE,0,op,"The shopkeeper seems unconcerned by you."); |
1153 | new_draw_info (NDI_UNIQUE, 0, op, "The shopkeeper seems unconcerned by you."); |
1039 | else |
1154 | else |
1040 | new_draw_info(NDI_UNIQUE,0,op,"The shopkeeper seems to have taken a dislike to you."); |
1155 | new_draw_info (NDI_UNIQUE, 0, op, "The shopkeeper seems to have taken a dislike to you."); |
1041 | } |
1156 | } |
1042 | } |
1157 | } |
|
|
1158 | else |
1043 | else new_draw_info(NDI_UNIQUE,0,op,"There is no shop nearby."); |
1159 | new_draw_info (NDI_UNIQUE, 0, op, "There is no shop nearby."); |
1044 | |
1160 | |
1045 | return 1; |
1161 | return 1; |
1046 | } |
1162 | } |
1047 | typedef struct shopinv { |
1163 | typedef struct shopinv |
|
|
1164 | { |
1048 | char *item_sort; |
1165 | char *item_sort; |
1049 | char *item_real; |
1166 | char *item_real; |
1050 | uint16 type; |
1167 | uint16 type; |
1051 | uint32 nrof; |
1168 | uint32 nrof; |
1052 | } shopinv; |
1169 | } shopinv; |
1053 | |
1170 | |
1054 | /* There are a lot fo extra casts in here just to suppress warnings - it |
1171 | /* There are a lot fo extra casts in here just to suppress warnings - it |
1055 | * makes it look uglier than it really it. |
1172 | * makes it look uglier than it really it. |
1056 | * The format of the strings we get is type:name. So we first want to |
1173 | * The format of the strings we get is type:name. So we first want to |
1057 | * sort by type (numerical) - if the same type, then sort by name. |
1174 | * sort by type (numerical) - if the same type, then sort by name. |
1058 | */ |
1175 | */ |
|
|
1176 | static int |
1059 | static int shop_sort(const void *a1, const void *a2) |
1177 | shop_sort (const void *a1, const void *a2) |
1060 | { |
1178 | { |
1061 | shopinv *s1 = (shopinv*)a1, *s2= (shopinv*)a2; |
1179 | shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2; |
1062 | |
1180 | |
1063 | if (s1->type<s2->type) return -1; |
1181 | if (s1->type < s2->type) |
|
|
1182 | return -1; |
1064 | if (s1->type>s2->type) return 1; |
1183 | if (s1->type > s2->type) |
|
|
1184 | return 1; |
1065 | /* the type is the same (what atoi gets), so do a strcasecmp to sort |
1185 | /* the type is the same (what atoi gets), so do a strcasecmp to sort |
1066 | * via alphabetical order |
1186 | * via alphabetical order |
1067 | */ |
1187 | */ |
1068 | return strcasecmp(s1->item_sort, s2->item_sort); |
1188 | return strcasecmp (s1->item_sort, s2->item_sort); |
1069 | } |
1189 | } |
1070 | |
1190 | |
|
|
1191 | static void |
1071 | static void add_shop_item(object *tmp, shopinv *items, int *numitems, int *numallocated) |
1192 | add_shop_item (object *tmp, shopinv * items, int *numitems, int *numallocated) |
1072 | { |
1193 | { |
1073 | #if 0 |
1194 | #if 0 |
1074 | char buf[MAX_BUF]; |
1195 | char buf[MAX_BUF]; |
1075 | #endif |
1196 | #endif |
1076 | /* clear unpaid flag so that doesn't come up in query |
1197 | /* clear unpaid flag so that doesn't come up in query |
1077 | * string. We clear nrof so that we can better sort |
1198 | * string. We clear nrof so that we can better sort |
1078 | * the object names. |
1199 | * the object names. |
1079 | */ |
1200 | */ |
1080 | |
1201 | |
1081 | CLEAR_FLAG(tmp, FLAG_UNPAID); |
1202 | CLEAR_FLAG (tmp, FLAG_UNPAID); |
1082 | items[*numitems].nrof=tmp->nrof; |
1203 | items[*numitems].nrof = tmp->nrof; |
1083 | /* Non mergable items have nrof of 0, but count them as one |
1204 | /* Non mergable items have nrof of 0, but count them as one |
1084 | * so the display is properly. |
1205 | * so the display is properly. |
1085 | */ |
1206 | */ |
1086 | if (tmp->nrof == 0) items[*numitems].nrof++; |
1207 | if (tmp->nrof == 0) |
|
|
1208 | items[*numitems].nrof++; |
1087 | items[*numitems].type=tmp->type; |
1209 | items[*numitems].type = tmp->type; |
1088 | |
1210 | |
1089 | switch (tmp->type) { |
1211 | switch (tmp->type) |
|
|
1212 | { |
1090 | #if 0 |
1213 | #if 0 |
1091 | case BOOTS: |
1214 | case BOOTS: |
1092 | case GLOVES: |
1215 | case GLOVES: |
1093 | case RING: |
1216 | case RING: |
1094 | case AMULET: |
1217 | case AMULET: |
1095 | case BRACERS: |
1218 | case BRACERS: |
1096 | case GIRDLE: |
1219 | case GIRDLE: |
1097 | sprintf(buf,"%s %s",query_base_name(tmp,0),describe_item(tmp, NULL)); |
1220 | sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL)); |
1098 | items[*numitems].item_sort = strdup_local(buf); |
1221 | items[*numitems].item_sort = strdup_local (buf); |
1099 | sprintf(buf,"%s %s",query_name(tmp),describe_item(tmp, NULL)); |
1222 | sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL)); |
1100 | items[*numitems].item_real = strdup_local(buf); |
1223 | items[*numitems].item_real = strdup_local (buf); |
1101 | (*numitems)++; |
1224 | (*numitems)++; |
1102 | break; |
1225 | break; |
1103 | #endif |
1226 | #endif |
1104 | |
1227 | |
1105 | default: |
1228 | default: |
1106 | items[*numitems].item_sort = strdup_local(query_base_name(tmp, 0)); |
1229 | items[*numitems].item_sort = strdup_local (query_base_name (tmp, 0)); |
1107 | items[*numitems].item_real = strdup_local(query_base_name(tmp, 1)); |
1230 | items[*numitems].item_real = strdup_local (query_base_name (tmp, 1)); |
1108 | (*numitems)++; |
1231 | (*numitems)++; |
1109 | break; |
1232 | break; |
1110 | } |
1233 | } |
1111 | SET_FLAG(tmp, FLAG_UNPAID); |
1234 | SET_FLAG (tmp, FLAG_UNPAID); |
1112 | } |
1235 | } |
1113 | |
1236 | |
|
|
1237 | void |
1114 | void shop_listing(object *op) |
1238 | shop_listing (object *op) |
1115 | { |
1239 | { |
1116 | int i,j,numitems=0,numallocated=0, nx, ny; |
1240 | int i, j, numitems = 0, numallocated = 0, nx, ny; |
1117 | char *map_mark = (char *) calloc(MAGIC_MAP_SIZE * MAGIC_MAP_SIZE,1); |
1241 | char *map_mark = (char *) calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1); |
1118 | object *stack; |
1242 | object *stack; |
1119 | shopinv *items; |
1243 | shopinv *items; |
1120 | |
1244 | |
1121 | /* Should never happen, but just in case a monster does apply a sign */ |
1245 | /* Should never happen, but just in case a monster does apply a sign */ |
1122 | if (op->type!=PLAYER) return; |
1246 | if (op->type != PLAYER) |
|
|
1247 | return; |
1123 | |
1248 | |
1124 | new_draw_info(NDI_UNIQUE, 0, op, "\nThe shop contains:"); |
1249 | new_draw_info (NDI_UNIQUE, 0, op, "\nThe shop contains:"); |
1125 | |
1250 | |
1126 | magic_mapping_mark(op, map_mark, 3); |
1251 | magic_mapping_mark (op, map_mark, 3); |
1127 | items=(shopinv*)malloc(40*sizeof(shopinv)); |
1252 | items = (shopinv *) malloc (40 * sizeof (shopinv)); |
1128 | numallocated=40; |
1253 | numallocated = 40; |
1129 | |
1254 | |
1130 | /* Find all the appropriate items */ |
1255 | /* Find all the appropriate items */ |
1131 | for (i=0; i<MAP_WIDTH(op->map); i++) { |
1256 | for (i = 0; i < MAP_WIDTH (op->map); i++) |
|
|
1257 | { |
1132 | for (j=0; j<MAP_HEIGHT(op->map); j++) { |
1258 | for (j = 0; j < MAP_HEIGHT (op->map); j++) |
|
|
1259 | { |
1133 | /* magic map code now centers the map on the object at MAGIC_MAP_HALF. |
1260 | /* magic map code now centers the map on the object at MAGIC_MAP_HALF. |
1134 | * |
1261 | * |
1135 | */ |
1262 | */ |
1136 | nx = i - op->x + MAGIC_MAP_HALF; |
1263 | nx = i - op->x + MAGIC_MAP_HALF; |
1137 | ny = j - op->y + MAGIC_MAP_HALF; |
1264 | ny = j - op->y + MAGIC_MAP_HALF; |
1138 | /* unlikely, but really big shops could run into this issue */ |
1265 | /* unlikely, but really big shops could run into this issue */ |
1139 | if (nx < 0 || ny < 0 || nx > MAGIC_MAP_SIZE || ny > MAGIC_MAP_SIZE) continue; |
1266 | if (nx < 0 || ny < 0 || nx > MAGIC_MAP_SIZE || ny > MAGIC_MAP_SIZE) |
|
|
1267 | continue; |
1140 | |
1268 | |
1141 | if (map_mark[nx + MAGIC_MAP_SIZE * ny] & FACE_FLOOR) { |
1269 | if (map_mark[nx + MAGIC_MAP_SIZE * ny] & FACE_FLOOR) |
|
|
1270 | { |
1142 | stack =get_map_ob(op->map,i,j); |
1271 | stack = get_map_ob (op->map, i, j); |
1143 | |
1272 | |
1144 | while (stack) { |
1273 | while (stack) |
|
|
1274 | { |
1145 | if (QUERY_FLAG(stack, FLAG_UNPAID)) { |
1275 | if (QUERY_FLAG (stack, FLAG_UNPAID)) |
|
|
1276 | { |
1146 | if (numitems==numallocated) { |
1277 | if (numitems == numallocated) |
|
|
1278 | { |
1147 | items=(shopinv*)realloc(items, sizeof(shopinv)*(numallocated+10)); |
1279 | items = (shopinv *) realloc (items, sizeof (shopinv) * (numallocated + 10)); |
1148 | numallocated+=10; |
1280 | numallocated += 10; |
1149 | } |
1281 | } |
1150 | add_shop_item(stack, items, &numitems, &numallocated); |
1282 | add_shop_item (stack, items, &numitems, &numallocated); |
1151 | } |
1283 | } |
1152 | stack = stack->above; |
1284 | stack = stack->above; |
1153 | } |
1285 | } |
1154 | } |
1286 | } |
1155 | } |
1287 | } |
1156 | } |
1288 | } |
1157 | free(map_mark); |
1289 | free (map_mark); |
1158 | if (numitems == 0) { |
1290 | if (numitems == 0) |
|
|
1291 | { |
1159 | new_draw_info(NDI_UNIQUE, 0, op, "The shop is currently empty.\n"); |
1292 | new_draw_info (NDI_UNIQUE, 0, op, "The shop is currently empty.\n"); |
1160 | free(items); |
1293 | free (items); |
1161 | return; |
1294 | return; |
1162 | } |
1295 | } |
1163 | qsort(items, numitems, sizeof(shopinv), (int (*)(const void*, const void*))shop_sort); |
1296 | qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); |
1164 | |
1297 | |
1165 | for (i=0; i<numitems; i++) { |
1298 | for (i = 0; i < numitems; i++) |
|
|
1299 | { |
1166 | /* Collapse items of the same name together */ |
1300 | /* Collapse items of the same name together */ |
1167 | if ((i+1)<numitems && !strcmp(items[i].item_real, items[i+1].item_real)) { |
1301 | if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) |
|
|
1302 | { |
1168 | items[i+1].nrof += items[i].nrof; |
1303 | items[i + 1].nrof += items[i].nrof; |
1169 | free(items[i].item_sort); |
1304 | free (items[i].item_sort); |
1170 | free(items[i].item_real); |
1305 | free (items[i].item_real); |
|
|
1306 | } |
1171 | } else { |
1307 | else |
|
|
1308 | { |
1172 | new_draw_info_format(NDI_UNIQUE, 0, op, "%d %s", |
1309 | new_draw_info_format (NDI_UNIQUE, 0, op, "%d %s", |
1173 | items[i].nrof? items[i].nrof:1, |
|
|
1174 | items[i].nrof==1?items[i].item_sort: items[i].item_real); |
1310 | items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); |
1175 | free(items[i].item_sort); |
1311 | free (items[i].item_sort); |
1176 | free(items[i].item_real); |
1312 | free (items[i].item_real); |
1177 | } |
1313 | } |
1178 | } |
1314 | } |
1179 | free(items); |
1315 | free (items); |
1180 | } |
1316 | } |
1181 | |
1317 | |
1182 | /* elmex: this function checks whether the object is in a shop */ |
1318 | /* elmex: this function checks whether the object is in a shop */ |
|
|
1319 | bool |
1183 | bool is_in_shop (object *o) |
1320 | is_in_shop (object *o) |
1184 | { |
1321 | { |
1185 | if (!o->map) |
1322 | if (!o->map) |
1186 | return false; |
1323 | return false; |
1187 | |
1324 | |
1188 | return is_in_shop (o->map, o->x, o->y); |
1325 | return is_in_shop (o->map, o->x, o->y); |
1189 | } |
1326 | } |
1190 | |
1327 | |
1191 | /* elmex: this function checks whether we are in a shop or not */ |
1328 | /* elmex: this function checks whether we are in a shop or not */ |
|
|
1329 | bool |
1192 | bool is_in_shop (mapstruct *map, int x, int y) |
1330 | is_in_shop (mapstruct *map, int x, int y) |
1193 | { |
1331 | { |
1194 | for (object *floor = get_map_ob (map, x, y); floor; floor = floor->above) |
1332 | for (object *floor = get_map_ob (map, x, y); floor; floor = floor->above) |
1195 | if (floor->type == SHOP_FLOOR) |
1333 | if (floor->type == SHOP_FLOOR) |
1196 | return true; |
1334 | return true; |
1197 | |
1335 | |