ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.C
Revision: 1.95
Committed: Sat Nov 17 23:40:04 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.94: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.44 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.92 *
4 root 1.95 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.93 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.75 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.92 *
9 root 1.70 * Deliantra is free software: you can redistribute it and/or modify it under
10     * the terms of the Affero GNU General Public License as published by the
11     * Free Software Foundation, either version 3 of the License, or (at your
12     * option) any later version.
13 root 1.92 *
14 root 1.40 * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18 root 1.92 *
19 root 1.70 * You should have received a copy of the Affero GNU General Public License
20     * and the GNU General Public License along with this program. If not, see
21     * <http://www.gnu.org/licenses/>.
22 root 1.92 *
23 root 1.44 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.31 */
25 elmex 1.1
26     #include <global.h>
27     #include <spells.h>
28     #include <skills.h>
29     #include <living.h>
30 root 1.24 #include <sproto.h>
31 elmex 1.1
32     /* this is a measure of how effective store specialisation is. A general store
33     * will offer this proportion of the 'maximum' price, a specialised store will
34     * offer a range of prices around it such that the maximum price is always one
35 root 1.94 * therefore making this number higher, makes specialisation less effective.
36 elmex 1.1 * setting this value above 1 or to a negative value would have interesting,
37     * (though not useful) effects.
38     */
39 root 1.41 #define SPECIALISATION_EFFECT .5
40 elmex 1.1
41 root 1.42 // price a shopkeeper will give someone that is not of their race
42     #define DISLIKE_RATIO 0.8
43 elmex 1.1
44 root 1.12 static void pay_from_container (object *pl, object *pouch, sint64 &to_pay);
45     static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop);
46 root 1.18 static double shop_specialisation_ratio (const object *item, const maptile *map);
47     static double shop_greed (const maptile *map);
48 elmex 1.1
49     /* Added F_TRUE flag to define.h to mean that the price should not
50     * be adjusted by players charisma. With F_TRUE, it returns the amount
51     * that the item is worth, if it was sold, but unadjusted by charisma.
52     * This is needed for alchemy, to to determine what value of gold nuggets
53     * should be given (the gold nuggets, when sold, will have the adjustment
54     * by charisma done at that time). NULL could have been passed as the
55     * who parameter, but then the adjustment for expensive items (>10000)
56     * would not be done.
57     *
58     * Added F_APPROX flag, which means that the price returned should be wrong by
59     * an amount related to the player's bargaining skill.
60     *
61 root 1.94 * Added F_SHOP flag to mean that the specialisation of the shop on the player's
62 elmex 1.1 * current map should be taken into account when determining the price. Shops that
63 root 1.94 * specialise in what is being traded will give better prices than those that do not.
64 elmex 1.1 *
65     * CF 0.91.4 - This function got changed around a bit. Now the
66     * number of object is multiplied by the value early on. This fixes problems
67     * with items worth very little. What happened before is that various
68     * divisions took place, the value got rounded to 0 (Being an int), and
69     * thus remained 0.
70     *
71     * Mark Wedel (mwedel@pyramid.com)
72     */
73    
74 root 1.12 static sint64 approx_range;
75 elmex 1.1
76 root 1.12 sint64
77 root 1.10 query_cost (const object *tmp, object *who, int flag)
78     {
79     double val;
80     int no_bargain;
81     int identified;
82     int not_cursed;
83     int approximate;
84     int shop;
85    
86     approx_range = 0;
87    
88     no_bargain = flag & F_NO_BARGAIN;
89     identified = flag & F_IDENTIFIED;
90     not_cursed = flag & F_NOT_CURSED;
91     approximate = flag & F_APPROX;
92     shop = flag & F_SHOP;
93     flag &= ~(F_NO_BARGAIN | F_IDENTIFIED | F_NOT_CURSED | F_APPROX | F_SHOP);
94    
95 root 1.80 int number = tmp->number_of ();
96    
97 root 1.10 if (tmp->type == MONEY)
98 root 1.80 return number * tmp->value;
99 root 1.14
100 root 1.10 if (tmp->type == GEM)
101     {
102     if (flag == F_TRUE)
103 root 1.80 return number * tmp->value;
104 root 1.14
105 root 1.10 if (flag == F_BUY)
106 root 1.80 return 1.03 * number * tmp->value;
107 root 1.14
108 root 1.10 if (flag == F_SELL)
109 root 1.80 return 0.97 * number * tmp->value;
110 root 1.14
111     LOG (llevError, "Query_cost: Gem type with unknown flag %d: %s\n", flag, tmp->debug_desc ());
112 root 1.10 return 0;
113     }
114 root 1.14
115 root 1.79 if (tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified)
116 root 1.10 {
117 root 1.78 if (!not_cursed && (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED]))
118 root 1.6 return 0;
119 root 1.10 else
120 root 1.80 val = number * tmp->value;
121 elmex 1.1 }
122 root 1.10 /* This area deals with objects that are not identified, but can be */
123     else
124     {
125 root 1.63 if (flag == F_BUY)
126 root 1.10 {
127 root 1.63 LOG (llevError | logBacktrace, "Asking for buy-value of unidentified object: %s\n", tmp->debug_desc ());
128     val = tmp->arch->value * 50 * number;
129 root 1.10 }
130     else
131 root 1.63 { /* Trying to sell something, or get true value */
132     if (tmp->type == POTION)
133     val = number * 40; /* Don't want to give anything away */
134     else
135 root 1.10 {
136 root 1.63 /* Get 2/3'rd value for applied objects, 1/3'rd for totally
137     * unknown objects
138     */
139 root 1.78 if (tmp->flag [FLAG_BEEN_APPLIED])
140 root 1.63 val = number * tmp->arch->value * 2 / 3;
141     else
142 root 1.80 val = number * tmp->arch->value / 3;
143 root 1.6 }
144     }
145 elmex 1.1 }
146    
147 root 1.10 /* If the item has been applied or identifed or does not need to be
148     * identified, AND the object is magical and the archetype is non
149     * magical, then change values accordingly. The tmp->arch==NULL is
150     * really just a check to prevent core dumps for when it checks
151 root 1.39 * tmp->arch->magic for any magic. The check for archetype
152 root 1.10 * magic is to not give extra money for archetypes that are by
153     * default magical. This is because the archetype value should have
154     * already figured in that value.
155     */
156 root 1.79 if ((tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified || tmp->flag [FLAG_BEEN_APPLIED])
157     && tmp->magic && (!tmp->arch || !tmp->arch->magic))
158 root 1.10 {
159     if (tmp->magic > 0)
160 root 1.80 val *= 3 * tmp->magic * tmp->magic * tmp->magic;
161 root 1.10 else
162     /* Note that tmp->magic is negative, so that this
163     * will actually be something like val /=2, /=3, etc.
164 root 1.6 */
165 root 1.80 val /= 1 - tmp->magic;
166 root 1.10 }
167    
168     if (tmp->type == WAND)
169     {
170     /* Value of the wand is multiplied by the number of
171     * charges. the treasure code already sets up the value
172     * 50 charges is used as the baseline.
173     */
174 root 1.79 if (tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified)
175 elmex 1.85 val *= tmp->stats.food;
176     /* if not identified, presume one charge */
177     val /= 50;
178 root 1.10 }
179    
180     /* Limit amount of money you can get for really great items. */
181     if (flag == F_SELL)
182 root 1.80 val = value_limit (val, number, who, shop);
183 root 1.10
184     // use a nonlinear price adjustment. as my predecessor said, don't change
185     // the archetypes, its work required for balancing, and we don't care.
186     //val = pow (val, 1.05);
187    
188     /* This modification is for bargaining skill.
189     * Now only players with max level in bargaining
190     * AND Cha = 30 will get optimal price.
191     * Thus charisma will never get useless.
192     * -b.e. edler@heydernet.de
193     */
194    
195 root 1.84 if (who && who->is_player ())
196 root 1.10 {
197 root 1.82 int lev_bargain = 0;
198 root 1.10 int lev_identify = 0;
199    
200 root 1.82 if (!no_bargain)
201     if (object *skill = find_skill_by_name (who, shstr_bargaining))
202     lev_bargain = skill->level;
203 root 1.14
204 root 1.51 if (const typedata *tmptype = get_typedata (tmp->type))
205 root 1.10 {
206 root 1.51 if (int idskill1 = tmptype->identifyskill)
207 root 1.10 {
208 root 1.51 int idskill2 = tmptype->identifyskill2;
209 root 1.14
210 root 1.10 if (find_skill_by_number (who, idskill1))
211 root 1.14 lev_identify = find_skill_by_number (who, idskill1)->level;
212    
213 root 1.10 if (idskill2 && find_skill_by_number (who, idskill2))
214 root 1.14 lev_identify += find_skill_by_number (who, idskill2)->level;
215 root 1.6 }
216     }
217 root 1.10
218     /* ratio determines how much of the price modification
219     * will come from the basic stat charisma
220     * the rest will come from the level in bargaining skill
221     */
222     const double cha_ratio = 0.40;
223 root 1.84 double bargaining = max (0., 1. - powf (double (lev_bargain) / MAXLEVEL_TREASURE, 0.25));
224 root 1.82 double charisma = (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.);
225 elmex 1.1
226 root 1.82 double factor = (1. - cha_ratio) * bargaining + cha_ratio * charisma;
227    
228     // scale 0..1 to 2 .. 80%
229     factor = lerp (factor, 0., 1., 0.02, 0.80);
230 root 1.10
231     if (flag == F_BUY)
232 root 1.82 val += val * factor;
233 root 1.10 else if (flag == F_SELL)
234 root 1.82 val -= val * factor;
235 root 1.10
236     // now find a price range. the less good we can judge, the larger the range is
237     // then the range is adjusted randomly around the correct value
238     if (approximate)
239 root 1.80 approx_range = val / sqrt (lev_identify * 3 + 1);
240 root 1.10 }
241    
242     /* I don't think this should really happen - if it does, it indicates and
243 root 1.34 * overflow of diff above. That should only happen if
244 root 1.10 * we are selling objects - in that case, the person just
245     * gets no money.
246     */
247     if ((sint64) val < 0)
248     val = 0;
249    
250 root 1.80 /* Unidentified stuff won't sell for more than 10gp */
251 root 1.79 if (flag == F_SELL && !tmp->flag [FLAG_IDENTIFIED] && tmp->need_identify () && !identified)
252 root 1.80 min_it (val, 1000);
253 elmex 1.1
254 root 1.10 /* if we are in a shop, check how the type of shop should affect the price */
255     if (shop && who)
256     {
257     if (flag == F_SELL)
258 root 1.80 val *= shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who) / shop_greed (who->map);
259 root 1.10 else if (flag == F_BUY)
260     {
261 root 1.94 /*
262 root 1.10 * when buying, if the item was sold by another player, it is ok to
263     * let the item be sold cheaper, according to the specialisation of
264     * the shop. If a player sold an item here, then his sale price was
265 root 1.94 * multiplied by the specialisation ratio, to do the same to the buy
266     * price will not generate extra money. However, the
267     * same is not true of generated items, these have to /divide/ by the
268 root 1.10 * specialisation, so that the price is never less than what they could
269     * be sold for (otherwise players could camp map resets to make money).
270 root 1.94 * In game terms, a non-specialist shop, might not recognise the true
271     * value of the items they sell (much like how people sometimes find
272 root 1.10 * antiques in a junk shop in real life).
273     */
274 root 1.78 if (tmp->flag [FLAG_PLAYER_SOLD])
275 root 1.80 val *= shop_specialisation_ratio (tmp, who->map);
276 root 1.10 else
277 root 1.80 val /= shop_specialisation_ratio (tmp, who->map);
278    
279     val *= shop_greed (who->map) / shopkeeper_approval (who->map, who);
280 root 1.10 }
281 root 1.34
282 root 1.94 /* we will also have an extra 0-5% variation between shops of the same type
283     * for valuable items (below a value of 50 this effect wouldn't be very
284 root 1.10 * pointful, and could give fun with rounding.
285     */
286 root 1.33 //TODO: why use cosf at all, just % and scale linearly, gives more even distribution
287 root 1.32 if (val > 50)
288 root 1.80 val *= 1 + .05f * cosf ((tmp->uuid.seq & 0xffff) * M_PI * 2. / 0x10000);
289 elmex 1.1 }
290 root 1.32
291 root 1.80 return val;
292 elmex 1.1 }
293    
294     /* Find the coin type that is worth more the 'c'. Starts at the
295     * cointype placement.
296     */
297    
298 root 1.10 static archetype *
299 root 1.12 find_next_coin (sint64 c, int *cointype)
300 root 1.10 {
301 elmex 1.1 archetype *coin;
302    
303 pippijn 1.9 do
304 root 1.10 {
305 root 1.80 if (!coins [*cointype])
306     return 0;
307    
308     coin = archetype::find (coins [*cointype]);
309    
310     if (!coin)
311     return 0;
312    
313 root 1.10 *cointype += 1;
314     }
315 root 1.39 while (coin->value > c);
316 elmex 1.1
317     return coin;
318     }
319    
320     /* This returns a string of how much something is worth based on
321     * an integer being passed.
322     * cost is the cost we need to represent.
323     * While cost is 64 bit, the number of any coin is still really
324     * limited to 32 bit (size of nrof field). If it turns out players
325     * have so much money that they have more than 2 billion platinum
326     * coins, there are certainly issues - the easiest fix at that
327     * time is to add a higher denomination (mithril piece with
328     * 10,000 silver or something)
329     */
330 root 1.10 const char *
331 root 1.12 cost_string_from_value (sint64 cost, int approx)
332 elmex 1.1 {
333 root 1.10 archetype *coin, *next_coin;
334     int num, cointype = 0;
335    
336     coin = find_next_coin (cost, &cointype);
337 root 1.81 if (!coin)
338 root 1.10 return "nothing";
339    
340 root 1.39 num = cost / coin->value;
341 root 1.10 /* so long as nrof is 32 bit, this is true.
342     * If it takes more coins than a person can possibly carry, this
343     * is basically true.
344     */
345 root 1.81 if (cost / coin->value > UINT32_MAX)
346     return "an unimaginable sum of money";
347 root 1.10
348 root 1.39 cost -= num * (sint64)coin->value;
349 elmex 1.1
350 root 1.81 char *buf = format ("%d %s", num, num > 1 ? &coin->object::name_pl : &coin->object::name);
351 root 1.10
352     next_coin = find_next_coin (cost, &cointype);
353 root 1.81 if (!next_coin || approx)
354 elmex 1.1 return buf;
355 root 1.10
356     coin = next_coin;
357 root 1.39 num = cost / coin->value;
358     cost -= num * (sint64)coin->value;
359 root 1.10
360 root 1.81 return format ("%s and %d %s", buf, num, num > 1 ? &coin->object::name_pl : &coin->object::name);
361 elmex 1.1 }
362    
363 root 1.10 const char *
364     query_cost_string (const object *tmp, object *who, int flag)
365     {
366 root 1.12 sint64 real_value = query_cost (tmp, who, flag);
367 root 1.10 int idskill1 = 0;
368     int idskill2 = 0;
369     const typedata *tmptype;
370    
371     tmptype = get_typedata (tmp->type);
372     if (tmptype)
373     {
374     idskill1 = tmptype->identifyskill;
375     idskill2 = tmptype->identifyskill2;
376     }
377    
378     /* we show an approximate price if
379     * 1) we are approximating
380     * 2) there either is no id skill(s) for the item, or we don't have them
381     * 3) we don't have bargaining skill either
382     */
383     if (flag & F_APPROX)
384     {
385     if (!idskill1 || !find_skill_by_number (who, idskill1))
386     {
387     if (!idskill2 || !find_skill_by_number (who, idskill2))
388     {
389 root 1.83 if (!find_skill_by_name (who, shstr_bargaining))
390 root 1.10 {
391     int num, cointype = 0;
392     archetype *coin = find_next_coin (real_value, &cointype);
393    
394 root 1.81 if (!coin)
395 root 1.10 return "nothing";
396    
397 root 1.39 num = real_value / coin->value;
398 root 1.51
399 root 1.10 if (num == 1)
400 root 1.81 return format ("about one %s", &coin->object::name);
401 root 1.10 else if (num < 5)
402 root 1.81 return format ("a few %s", &coin->object::name_pl);
403 root 1.10 else if (num < 10)
404 root 1.81 return format ("several %s", &coin->object::name_pl);
405 root 1.10 else if (num < 25)
406 root 1.81 return format ("a moderate amount of %s", &coin->object::name_pl);
407 root 1.10 else if (num < 100)
408 root 1.81 return format ("lots of %s", &coin->object::name_pl);
409 root 1.10 else if (num < 1000)
410 root 1.81 return format ("a great many %s", &coin->object::name_pl);
411 root 1.10 else
412 root 1.81 return format ("a vast quantity of %s", &coin->object::name_pl);
413 root 1.6 }
414     }
415     }
416 elmex 1.1
417 root 1.10 if (approx_range)
418     {
419 root 1.81 int hash = tmp->random_seed () & 1023;
420     sint64 lo = real_value - (approx_range * hash >> 10);
421 elmex 1.1
422 root 1.81 return format ("between %s and %s",
423     cost_string_from_value (lo, 1),
424     cost_string_from_value (lo + approx_range, 1));
425 root 1.10 }
426 elmex 1.1 }
427    
428 root 1.10 return cost_string_from_value (real_value, 0);
429 elmex 1.1 }
430    
431     /* This function finds out how much money the player is carrying,
432     * including what is in containers.
433     */
434 root 1.12 sint64
435 root 1.10 query_money (const object *op)
436     {
437     object *tmp;
438 root 1.12 sint64 total = 0;
439 elmex 1.1
440 root 1.10 if (op->type != PLAYER && op->type != CONTAINER)
441     {
442     LOG (llevError, "Query money called with non player/container\n");
443     return 0;
444 elmex 1.1 }
445 root 1.12
446 root 1.10 for (tmp = op->inv; tmp; tmp = tmp->below)
447 root 1.12 if (tmp->type == MONEY)
448     total += tmp->nrof * (sint64)tmp->value;
449 root 1.86 else if (tmp->type == CONTAINER && tmp->flag [FLAG_APPLIED] && (!tmp->race || tmp->race.contains (shstr_gold)))
450 root 1.12 total += query_money (tmp);
451    
452 root 1.10 return total;
453 elmex 1.1 }
454 root 1.10
455 elmex 1.1 /* TCHIZE: This function takes the amount of money from the
456     * the player inventory and from it's various pouches using the
457     * pay_from_container function.
458     * returns 0 if not possible. 1 if success
459     */
460 root 1.10 int
461 root 1.12 pay_for_amount (sint64 to_pay, object *pl)
462 root 1.10 {
463     object *pouch;
464 elmex 1.1
465 root 1.10 if (to_pay == 0)
466     return 1;
467 root 1.12
468 root 1.10 if (to_pay > query_money (pl))
469     return 0;
470 elmex 1.1
471 root 1.12 pay_from_container (pl, pl, to_pay);
472    
473     for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
474 root 1.86 if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains (shstr_gold)))
475 root 1.12 pay_from_container (pl, pouch, to_pay);
476 elmex 1.1
477 root 1.26 pl->update_stats ();
478 root 1.10 return 1;
479 elmex 1.1 }
480    
481     /* 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
483     * money until op is paid for. Change will be left wherever the last
484     * of the price was paid from.
485     */
486 root 1.10 int
487     pay_for_item (object *op, object *pl)
488     {
489 root 1.12 sint64 to_pay = query_cost (op, pl, F_BUY | F_SHOP);
490 root 1.10 object *pouch;
491 root 1.12 sint64 saved_money;
492 root 1.10
493     if (to_pay == 0)
494 elmex 1.1 return 1;
495 root 1.12
496 root 1.10 if (to_pay > query_money (pl))
497     return 0;
498    
499     /* We compare the paid price with the one for a player
500     * without bargaining skill.
501     * This determins the amount of exp (if any) gained for bargaining.
502     */
503     saved_money = query_cost (op, pl, F_BUY | F_NO_BARGAIN | F_SHOP) - to_pay;
504    
505     if (saved_money > 0)
506 root 1.83 change_exp (pl, saved_money, shstr_bargaining, SK_EXP_NONE);
507 root 1.10
508 root 1.12 pay_from_container (pl, pl, to_pay);
509    
510     for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
511 root 1.86 if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains (shstr_gold)))
512 root 1.12 pay_from_container (pl, pouch, to_pay);
513 root 1.10
514 root 1.36 pl->update_stats ();
515 root 1.12
516 root 1.10 return 1;
517 elmex 1.1 }
518    
519     /* This pays for the item, and takes the proper amount of money off
520     * the player.
521     * CF 0.91.4 - this function is mostly redone in order to fix a bug
522     * with weight not be subtracted properly. We now remove and
523     * insert the coin objects - this should update the weight
524     * appropriately
525     *
526     * DAMN: This function is used for the player, then for any active
527     * containers that can hold money.
528     *
529     * pouch is the container (pouch or player) to remove the coins from.
530     * to_pay is the required amount.
531     * returns the amount still missing after using "pouch".
532     */
533 root 1.12 static void
534     pay_from_container (object *pl, object *pouch, sint64 &to_pay)
535 root 1.10 {
536     int count, i;
537 root 1.12 object *tmp, *next;
538 root 1.10 archetype *at;
539    
540     if (pouch->type != PLAYER && pouch->type != CONTAINER)
541 root 1.12 return;
542 root 1.10
543 root 1.12 object *coin_objs[NUM_COINS] = { 0 };
544 root 1.10
545     /* This hunk should remove all the money objects from the player/container */
546     for (tmp = pouch->inv; tmp; tmp = next)
547     {
548     next = tmp->below;
549    
550     if (tmp->type == MONEY)
551     {
552     for (i = 0; i < NUM_COINS; i++)
553     {
554 root 1.39 if (tmp->value == tmp->arch->value && !strcmp (coins[NUM_COINS - 1 - i], tmp->arch->archname))
555 root 1.10 {
556 root 1.12 // This should not happen, but if it does, just merge the two.
557     if (coin_objs [i])
558 root 1.10 {
559     LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]);
560     coin_objs[i]->nrof += tmp->nrof;
561 root 1.57 tmp->destroy ();
562 root 1.6 }
563 root 1.10 else
564     {
565 root 1.21 tmp->remove ();
566 root 1.10 coin_objs[i] = tmp;
567 root 1.6 }
568 root 1.12
569 root 1.10 break;
570 root 1.6 }
571     }
572 root 1.12
573 root 1.10 if (i == NUM_COINS)
574 root 1.38 LOG (llevError, "in pay_for_item: Did not find string match for %s\n", &tmp->arch->archname);
575 root 1.6 }
576 elmex 1.1 }
577    
578 root 1.10 /* Fill in any gaps in the coin_objs array - needed to make change. */
579     /* Note that the coin_objs array goes from least value to greatest value */
580     for (i = 0; i < NUM_COINS; i++)
581 root 1.12 if (!coin_objs[i])
582 root 1.10 {
583 root 1.15 at = archetype::find (coins[NUM_COINS - 1 - i]);
584 root 1.12
585 root 1.10 if (at == NULL)
586     LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]);
587 root 1.12
588 root 1.74 coin_objs[i] = at->instance ();
589 root 1.10 coin_objs[i]->nrof = 0;
590     }
591    
592     for (i = 0; i < NUM_COINS; i++)
593     {
594 root 1.12 object &coin = *coin_objs[i];
595 elmex 1.68 sint64 num_coins = min ((to_pay + coin.value - 1) / coin.value, (sint64) coin.nrof);
596 root 1.12 to_pay -= num_coins * coin.value;
597 root 1.10
598 root 1.12 coin.nrof -= num_coins;
599 root 1.10 /* Now start making change. Start at the coin value
600     * below the one we just did, and work down to
601     * the lowest value.
602     */
603     count = i - 1;
604 root 1.12
605     while (to_pay < 0 && count >= 0)
606 root 1.10 {
607 root 1.12 num_coins = (-to_pay) / coin_objs[count]->value;
608 root 1.10 coin_objs[count]->nrof += num_coins;
609 root 1.12 to_pay += num_coins * coin_objs[count]->value;
610 root 1.10 count--;
611     }
612     }
613 root 1.12
614 root 1.10 for (i = 0; i < NUM_COINS; i++)
615 root 1.56 if (coin_objs[i]->nrof)
616     insert_ob_in_ob (coin_objs [i], pouch);
617     else
618 root 1.57 coin_objs[i]->destroy ();
619 elmex 1.1 }
620    
621     /* Checks all unpaid items in op's inventory, adds up all the money they
622     * have, and checks that they can actually afford what they want to buy.
623 root 1.94 * Returns 1 if they can, and 0 if they can't. also prints an appropriate message
624 elmex 1.1 * to the player
625     */
626 root 1.10 int
627     can_pay (object *pl)
628     {
629 root 1.12 int unpaid_count = 0;
630     sint64 unpaid_price = 0;
631     sint64 player_wealth = query_money (pl);
632 root 1.10
633     if (!pl || pl->type != PLAYER)
634     {
635     LOG (llevError, "can_pay(): called against something that isn't a player\n");
636     return 0;
637 elmex 1.1 }
638 root 1.11
639 root 1.13 for (object::depth_iterator item = pl->begin (); item != pl->end (); ++item)
640 root 1.78 if (item->flag [FLAG_UNPAID])
641 root 1.12 {
642     unpaid_count++;
643     unpaid_price += query_cost (item, pl, F_BUY | F_SHOP);
644     }
645 root 1.11
646 root 1.10 if (unpaid_price > player_wealth)
647     {
648 root 1.67 dynbuf_text &buf = msg_dynbuf; buf.clear ();
649 root 1.54
650     buf << "You have " << unpaid_count
651     << " unpaid item(s) that would cost you " << cost_string_from_value (unpaid_price, 0)
652     << ". You need another " << cost_string_from_value (unpaid_price - player_wealth, 0)
653 root 1.58 << " to be able to afford that. "
654 root 1.59 "H<You cannot leave a shop without paying - drop unpaid items first to be able to leave.>";
655 root 1.54
656     pl->failmsg (buf);
657 root 1.12
658 root 1.10 return 0;
659 elmex 1.1 }
660 root 1.10 else
661     return 1;
662 elmex 1.1 }
663    
664     /* Better get_payment, descends containers looking for
665     * unpaid items, and pays for them.
666     * returns 0 if the player still has unpaid items.
667     * returns 1 if the player has paid for everything.
668     * pl is the player buying the stuff.
669     */
670 root 1.10 int
671 root 1.12 get_payment (object *pl)
672 root 1.10 {
673 root 1.12 for (;;)
674     {
675     next_item:
676 root 1.10
677 root 1.13 for (object::depth_iterator op = pl->begin (); op != pl->end (); ++op)
678 root 1.12 {
679 root 1.78 if (op->flag [FLAG_UNPAID])
680 root 1.12 {
681 root 1.81 const char *buf = query_cost_string (op, pl, F_BUY | F_SHOP);
682 root 1.10
683 root 1.12 if (!pay_for_item (op, pl))
684     {
685     sint64 i = query_cost (op, pl, F_BUY | F_SHOP) - query_money (pl);
686 elmex 1.1
687 root 1.78 op->clr_flag (FLAG_UNPAID);
688 root 1.12 new_draw_info_format (NDI_UNIQUE, 0, pl, "You lack %s to buy %s.", cost_string_from_value (i, 0), query_name (op));
689 root 1.78 op->set_flag (FLAG_UNPAID);
690 root 1.12 return 0;
691     }
692     else
693     {
694 root 1.78 op->clr_flag (FLAG_UNPAID);
695     op->clr_flag (FLAG_PLAYER_SOLD);
696 root 1.81 new_draw_info_format (NDI_UNIQUE, 0, pl, "You paid %s for %s.", buf, query_name (op));
697 root 1.10
698 root 1.49 if (!merge_ob (op, op->env->inv))
699     esrv_update_item (UPD_FLAGS, pl, op);
700 elmex 1.1
701 root 1.12 goto next_item;
702 root 1.6 }
703     }
704     }
705 root 1.12
706     return 1;
707 elmex 1.1 }
708     }
709    
710     /* written by elmex:
711     * moved this code from sell_item () here to have a function
712     * that pays the player an amount. Mainly put the code here to
713     * be able to call it from a plugin.
714     *
715     * If the player can't carry all the money that is paid, it gets inserted
716     * in his inventory anyway. This is the best alternative to not pay any money
717     * or put it on the ground under the player. This way the player can still
718     * go somewhere and unload the money at a safe place.
719     *
720     */
721 root 1.10 void
722 root 1.12 pay_player (object *pl, sint64 amount)
723 root 1.10 {
724 elmex 1.1 int count = 0;
725     archetype *at = 0;
726 root 1.91 object *pouch = 0;
727 elmex 1.1
728 root 1.43 for (count = 0; coins[count]; count++)
729 elmex 1.1 {
730 root 1.15 at = archetype::find (coins[count]);
731 elmex 1.1
732     if (at == NULL)
733 root 1.10 LOG (llevError, "Could not find %s archetype\n", coins[count]);
734 root 1.39 else if ((amount / at->value) > 0)
735 elmex 1.1 {
736 root 1.10 for (pouch = pl->inv; pouch; pouch = pouch->below)
737 elmex 1.1 {
738 root 1.86 if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && pouch->race.contains (shstr_gold))
739 elmex 1.1 {
740 root 1.39 int w = at->weight * (100 - pouch->stats.Str) / 100;
741     int n = amount / at->value;
742 elmex 1.1
743     if (w == 0)
744 root 1.10 w = 1; /* Prevent divide by zero */
745 elmex 1.1
746     if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit))
747     {
748     if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n)
749     n = (pouch->weight_limit - pouch->carrying) / w;
750    
751 root 1.74 object *tmp = at->instance ();
752 elmex 1.1 tmp->nrof = n;
753 root 1.12 amount -= tmp->nrof * tmp->value;
754 root 1.49 pouch->insert (tmp);
755 elmex 1.1 }
756     }
757     }
758    
759 root 1.39 if (amount / at->value > 0)
760 elmex 1.1 {
761 root 1.74 object *tmp = at->instance ();
762 elmex 1.1 tmp->nrof = amount / tmp->value;
763 root 1.12 amount -= tmp->nrof * tmp->value;
764 root 1.49 pl->insert (tmp);
765 elmex 1.1 }
766     }
767     }
768    
769 root 1.10 if (amount != 0)
770     LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount);
771 elmex 1.1 }
772    
773     /* elmex: this is for the bank plugin :( */
774 root 1.12 sint64
775     pay_player_arch (object *pl, const char *arch, sint64 amount)
776 root 1.10 {
777 root 1.54 if (amount)
778     {
779     object *ob = archetype::get (arch);
780 elmex 1.1
781 root 1.54 if (!ob)
782     return 0;
783 elmex 1.1
784 root 1.54 ob->nrof = amount;
785     pl->insert (ob);
786 elmex 1.1 }
787    
788     return 1;
789     }
790    
791     /* Modified function to give out platinum coins. This function uses
792     * the coins[] array to know what coins are available, just like
793     * buy item.
794     *
795     * Modified to fill available race: gold containers before dumping
796     * remaining coins in character's inventory.
797     */
798 elmex 1.50 bool
799 root 1.10 sell_item (object *op, object *pl)
800     {
801 root 1.12 sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain;
802 elmex 1.1
803 root 1.10 if (pl == NULL || pl->type != PLAYER)
804 elmex 1.1 {
805 root 1.10 LOG (llevDebug, "Object other than player tried to sell something.\n");
806 elmex 1.50 return false;
807 elmex 1.1 }
808    
809 root 1.8 op->custom_name = 0;
810 elmex 1.1
811 root 1.10 if (!amount)
812 elmex 1.1 {
813 elmex 1.50 new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.",
814     query_name (op));
815     // elmex: change: the player now gets the item back if the shop is not
816     // interested in it.
817     return false;
818 elmex 1.1 }
819    
820     /* We compare the price with the one for a player
821     * without bargaining skill.
822     * This determins the amount of exp (if any) gained for bargaining.
823     * exp/10 -> 1 for each gold coin
824     */
825     extra_gain = amount - query_cost (op, pl, F_SELL | F_NO_BARGAIN | F_SHOP);
826    
827     if (extra_gain > 0)
828 root 1.83 change_exp (pl, extra_gain / 10, shstr_bargaining, SK_EXP_NONE);
829 elmex 1.1
830     pay_player (pl, amount);
831    
832 elmex 1.50 new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.",
833     query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op));
834 root 1.43 pl->play_sound (sound_find ("shop_sell"));
835 elmex 1.1
836 root 1.78 op->set_flag (FLAG_UNPAID);
837 elmex 1.1 identify (op);
838 elmex 1.50
839     return true;
840 elmex 1.1 }
841    
842     /* returns a double that is the ratio of the price that a shop will offer for
843 root 1.94 * item based on the shops specialisation. Does not take account of greed,
844 root 1.41 * returned value is between SPECIALISATION_EFFECT and 1.
845 elmex 1.1 */
846 root 1.10 static double
847 root 1.18 shop_specialisation_ratio (const object *item, const maptile *map)
848 root 1.10 {
849     shopitems *items = map->shopitems;
850 root 1.73 int likedness = 0;
851 root 1.10 int i;
852 elmex 1.1
853 root 1.10 if (item == NULL)
854     {
855 root 1.28 LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", &map->path);
856 root 1.10 return 0;
857     }
858 root 1.12
859 root 1.10 if (!item->type)
860     {
861 root 1.89 LOG (llevError, "shop_specialisation_ratio: passed an item with an invalid type: %s\n", item->debug_desc ());
862 root 1.94 /*
863     * I'm not really sure what the /right/ thing to do here is, these types of
864     * item shouldn't exist anyway, but returning the ratio is probably the best bet.."
865 root 1.10 */
866 root 1.41 return SPECIALISATION_EFFECT;
867 root 1.10 }
868 root 1.12
869 root 1.10 if (map->shopitems)
870 root 1.73 for (i = 0; i < items[0].index; i++)
871     if (items[i].typenum == item->type || (!items[i].typenum && !likedness))
872     likedness = items[i].strength;
873 root 1.12
874 root 1.73 if (likedness > 100)
875 root 1.10 { /* someone has been rather silly with the map headers. */
876 root 1.89 LOG (llevDebug, "shop_specialisation ratio: item %s on map %s is above 100%%\n", item->debug_desc (), &map->path);
877 root 1.73 likedness = 100;
878 elmex 1.1 }
879 root 1.12
880 root 1.73 if (likedness < -100)
881 root 1.10 {
882 root 1.89 LOG (llevDebug, "shop_specialisation ratio: item %s on map %s is below -100%%\n", item->debug_desc (), &map->path);
883 root 1.73 likedness = -100;
884 elmex 1.1 }
885 root 1.12
886 root 1.73 return lerp (double (likedness), -100., 100., SPECIALISATION_EFFECT, 1.);
887 elmex 1.1 }
888    
889     /*returns the greed of the shop on map, or 1 if it isn't specified. */
890 root 1.10 static double
891 root 1.18 shop_greed (const maptile *map)
892 root 1.10 {
893 root 1.42 return map->shopgreed
894     ? map->shopgreed
895     : 1.;
896 elmex 1.1 }
897    
898     /* Returns a double based on how much the shopkeeper approves of the player.
899     * this is based on the race of the shopkeeper and that of the player.
900     */
901 root 1.10 double
902 root 1.18 shopkeeper_approval (const maptile *map, const object *player)
903 root 1.10 {
904 root 1.42 return map->shoprace && player->race != map->shoprace
905     ? DISLIKE_RATIO
906     : 1.;
907 elmex 1.1 }
908    
909     /* limit the value of items based on the wealth of the shop. If the item is close
910 root 1.94 * to the maximum value a shop will offer, we start to reduce it, if the item is
911     * below the minimum value the shop is prepared to trade in, then we don't
912 elmex 1.1 * want it and offer nothing. If it isn't a shop, check whether we should do generic
913     * value reduction.
914 root 1.92 *
915 elmex 1.1 */
916 root 1.12 static sint64
917     value_limit (sint64 val, int quantity, const object *who, int isshop)
918 root 1.10 {
919 root 1.12 sint64 newval, unit_price, tmpshopmax;
920 root 1.18 maptile *map;
921 elmex 1.1
922     unit_price = val / quantity;
923    
924     if (!isshop || !who)
925     {
926     if (unit_price > 250000)
927 root 1.12 newval = (sint64) (250000. - pow (250000., .75) * 65. + pow (unit_price, .75) * 65.);
928 elmex 1.1 else
929     newval = unit_price;
930     }
931     else
932     {
933     if (!who->map)
934     {
935 root 1.10 LOG (llevError, "value_limit: asked shop price for ob %s on NULL map\n", &who->name);
936 elmex 1.1 return val;
937     }
938    
939     map = who->map;
940    
941 root 1.10 tmpshopmax = map->shopmax ? map->shopmax : 100000; // 20 royalties default
942 elmex 1.1
943     if (map->shopmin && unit_price < map->shopmin)
944     return 0;
945     else if (unit_price > tmpshopmax / 2)
946 root 1.73 newval = min ((tmpshopmax / 2) + isqrt (unit_price - tmpshopmax / 2), tmpshopmax);
947 elmex 1.1 else
948 root 1.10 newval = unit_price;
949 elmex 1.1 }
950    
951     newval *= quantity;
952    
953     return newval;
954     }
955    
956     /* gives a desciption of the shop on their current map to the player op. */
957 root 1.10 int
958     describe_shop (const object *op)
959     {
960 root 1.71 dynbuf_text buf;
961 root 1.18 maptile *map = op->map;
962 root 1.10
963     /*shopitems *items=map->shopitems; */
964 root 1.91 int i;
965 root 1.10 double opinion = 0;
966    
967     if (op->type != PLAYER)
968     return 0;
969    
970     /*check if there is a shop specified for this map */
971     if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax)
972     {
973 root 1.71 buf << "From looking at the nearby shop you determine that it trades in ";
974 root 1.72 int lastcomma = 0, prevcomma = 0;
975 root 1.12
976 root 1.10 if (map->shopitems)
977 root 1.12 for (i = 0; i < map->shopitems[0].index; i++)
978     if (map->shopitems[i].name && map->shopitems[i].strength > 10)
979 root 1.10 {
980 root 1.72 buf << map->shopitems[i].name_pl;
981     prevcomma = lastcomma;
982     lastcomma = buf.size (); // remember offset
983     buf << ", ";
984 root 1.6 }
985 root 1.12
986 root 1.72 if (lastcomma)
987     {
988     buf.splice (lastcomma, 2);
989    
990     if (prevcomma)
991     buf.splice (prevcomma, 2, " and ");
992     }
993 root 1.71 else
994 root 1.72 buf << "a little of everything.";
995 root 1.6
996 root 1.72 buf << ".\n\n";
997 root 1.10
998     if (map->shopmax)
999 root 1.71 buf << "It won't trade for items above " << cost_string_from_value (map->shopmax, 0) << ".\n\n";
1000 root 1.12
1001 root 1.10 if (map->shopmin)
1002 root 1.71 buf << "It won't trade in items worth less than " << cost_string_from_value (map->shopmin, 0) << ".\n\n";
1003 root 1.12
1004 root 1.10 if (map->shopgreed)
1005     {
1006     if (map->shopgreed > 2.0)
1007 root 1.71 buf << "It tends to overcharge massively.\n\n";
1008 root 1.10 else if (map->shopgreed > 1.5)
1009 root 1.71 buf << "It tends to overcharge substantially.\n\n";
1010 root 1.10 else if (map->shopgreed > 1.1)
1011 root 1.71 buf << "It tends to overcharge slightly.\n\n";
1012 root 1.10 else if (map->shopgreed < 0.9)
1013 root 1.71 buf << "It tends to undercharge.\n\n";
1014 root 1.10 }
1015 root 1.12
1016 root 1.10 if (map->shoprace)
1017     {
1018     opinion = shopkeeper_approval (map, op);
1019 root 1.71
1020 root 1.10 if (opinion > 0.8)
1021 root 1.71 buf << "You think the shopkeeper likes you.\n\n";
1022 root 1.10 else if (opinion > 0.5)
1023 root 1.71 buf << "The shopkeeper seems unconcerned by you.\n\n";
1024 root 1.10 else
1025 root 1.71 buf << "The shopkeeper seems to have taken a dislike to you.\n\n";
1026 root 1.6 }
1027 elmex 1.1 }
1028 root 1.10 else
1029 root 1.71 buf << "There is no shop nearby.\n\n";
1030    
1031     op->contr->infobox (MSG_CHANNEL ("shopinfo"), buf);
1032 elmex 1.1
1033 root 1.10 return 1;
1034 elmex 1.1 }
1035 root 1.12
1036     struct shopinv
1037 root 1.10 {
1038     char *item_sort;
1039     char *item_real;
1040 elmex 1.85 sint64 value;
1041 root 1.10 uint16 type;
1042     uint32 nrof;
1043 root 1.12 };
1044 elmex 1.1
1045     /* There are a lot fo extra casts in here just to suppress warnings - it
1046     * makes it look uglier than it really it.
1047     * The format of the strings we get is type:name. So we first want to
1048     * sort by type (numerical) - if the same type, then sort by name.
1049     */
1050 root 1.10 static int
1051     shop_sort (const void *a1, const void *a2)
1052 elmex 1.1 {
1053 root 1.10 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2;
1054 elmex 1.1
1055 root 1.10 if (s1->type < s2->type)
1056     return -1;
1057 root 1.52
1058 root 1.10 if (s1->type > s2->type)
1059     return 1;
1060 root 1.12
1061 root 1.10 /* the type is the same (what atoi gets), so do a strcasecmp to sort
1062     * via alphabetical order
1063     */
1064     return strcasecmp (s1->item_sort, s2->item_sort);
1065 elmex 1.1 }
1066    
1067 root 1.10 static void
1068     add_shop_item (object *tmp, shopinv * items, int *numitems, int *numallocated)
1069 elmex 1.1 {
1070 root 1.10 /* clear unpaid flag so that doesn't come up in query
1071     * string. We clear nrof so that we can better sort
1072     * the object names.
1073     */
1074 elmex 1.1
1075 root 1.78 tmp->clr_flag (FLAG_UNPAID);
1076 root 1.10 items[*numitems].nrof = tmp->nrof;
1077     /* Non mergable items have nrof of 0, but count them as one
1078     * so the display is properly.
1079     */
1080     if (tmp->nrof == 0)
1081     items[*numitems].nrof++;
1082     items[*numitems].type = tmp->type;
1083    
1084 elmex 1.85 items[*numitems].value = tmp->value;
1085    
1086 root 1.10 switch (tmp->type)
1087     {
1088 elmex 1.1 #if 0
1089 root 1.6 case BOOTS:
1090     case GLOVES:
1091     case RING:
1092     case AMULET:
1093     case BRACERS:
1094     case GIRDLE:
1095 root 1.10 sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL));
1096 root 1.23 items[*numitems].item_sort = strdup (buf);
1097 root 1.10 sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL));
1098 root 1.23 items[*numitems].item_real = strdup (buf);
1099 root 1.10 (*numitems)++;
1100     break;
1101 elmex 1.1 #endif
1102    
1103 root 1.6 default:
1104 root 1.23 items[*numitems].item_sort = strdup (query_base_name (tmp, 0));
1105     items[*numitems].item_real = strdup (query_base_name (tmp, 1));
1106 elmex 1.85 items[*numitems].value += tmp->value;
1107 root 1.10 (*numitems)++;
1108     break;
1109     }
1110 root 1.69
1111 root 1.78 tmp->set_flag (FLAG_UNPAID);
1112 root 1.10 }
1113    
1114     void
1115 elmex 1.35 shop_listing (object *sign, object *op)
1116 root 1.10 {
1117 root 1.65 int i, j, x1, x2, y1, y2;
1118 root 1.88 const char *shop_coords = sign->kv [shstr_shop_coords];
1119 root 1.10 object *stack;
1120     shopinv *items;
1121    
1122     /* Should never happen, but just in case a monster does apply a sign */
1123 root 1.65 if (!op->is_player ())
1124 root 1.10 return;
1125    
1126 root 1.67 dynbuf_text &buf = msg_dynbuf; buf.clear ();
1127 root 1.65
1128 elmex 1.35 if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2)))
1129     {
1130     x1 = 0;
1131     y1 = 0;
1132     x2 = op->map->width - 1;
1133     y2 = op->map->height - 1;
1134     }
1135 root 1.10
1136 root 1.65 int numallocated = 40;
1137     int numitems = 0;
1138     items = (shopinv *)malloc (sizeof (shopinv) * numallocated);
1139 root 1.10
1140     /* Find all the appropriate items */
1141 elmex 1.35 for (i = x1; i <= x2; i++)
1142 root 1.65 for (j = y1; j < y2; j++)
1143 root 1.77 if (op->map->is_in_shop (i, j))
1144 root 1.10 {
1145 root 1.65 stack = GET_MAP_OB (op->map, i, j);
1146    
1147     while (stack)
1148 root 1.10 {
1149 root 1.78 if (stack->flag [FLAG_UNPAID])
1150 root 1.10 {
1151 root 1.65 if (numitems == numallocated)
1152     items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2));
1153 root 1.30
1154 root 1.65 add_shop_item (stack, items, &numitems, &numallocated);
1155     }
1156 root 1.30
1157 root 1.65 stack = stack->above;
1158 root 1.6 }
1159     }
1160 root 1.12
1161 root 1.65 buf << (numitems ? "T<This shop contains:>\n\n"
1162     : "T<This shop is currently empty.>");
1163 root 1.12
1164 root 1.10 qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort);
1165    
1166     for (i = 0; i < numitems; i++)
1167     {
1168     /* Collapse items of the same name together */
1169     if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real))
1170 root 1.65 items[i + 1].nrof += items[i].nrof;
1171 root 1.10 else
1172 elmex 1.85 {
1173     buf.printf (
1174     " %4d %s\n for %s\n",
1175     items[i].nrof ? items[i].nrof : 1,
1176     items[i].nrof == 1 ? items[i].item_sort : items[i].item_real,
1177     cost_string_from_value (items[i].value, op->flag [FLAG_WIZ] ? 0 : 1));
1178     }
1179 root 1.65
1180     free (items[i].item_sort);
1181     free (items[i].item_real);
1182 elmex 1.1 }
1183 root 1.30
1184 root 1.65 op->contr->infobox (MSG_CHANNEL ("shopitems"), buf);
1185    
1186 root 1.10 free (items);
1187 elmex 1.1 }
1188 elmex 1.7
1189 elmex 1.45 /* elmex: this function checks whether we are in a shop or not
1190     - change 2007-11-26: enhanced the O(n) case by stopping at the first
1191     floor tile. this possibly will make map bugs where shopfloors are above
1192     floors more obvious.
1193     */
1194 root 1.10 bool
1195 root 1.77 maptile::is_in_shop (int x, int y) const
1196 elmex 1.7 {
1197 root 1.77 for (object *floor = at (x, y).bot; floor; floor = floor->above)
1198     if (floor->flag [FLAG_IS_FLOOR])
1199 elmex 1.45 return floor->type == SHOP_FLOOR;
1200 root 1.52
1201 elmex 1.7 return false;
1202     }
1203 root 1.43