ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.C
Revision: 1.70
Committed: Mon Oct 12 14:00:59 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_81
Changes since 1.69: +7 -6 lines
Log Message:
clarify license

File Contents

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