ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.C
Revision: 1.81
Committed: Wed Apr 28 19:20:48 2010 UTC (14 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.80: +21 -37 lines
Log Message:
-MAX_BUF, You paid message...

File Contents

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