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, 5 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

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * 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 *
14 * 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 *
19 * 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 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 #include <global.h>
27 #include <spells.h>
28 #include <skills.h>
29 #include <living.h>
30 #include <sproto.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 #define SPECIALISATION_EFFECT .5
40
41 // price a shopkeeper will give someone that is not of their race
42 #define DISLIKE_RATIO 0.8
43
44 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 static double shop_specialisation_ratio (const object *item, const maptile *map);
47 static double shop_greed (const maptile *map);
48
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 static sint64 approx_range;
75
76 sint64
77 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 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;
176 /* 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->is_player ())
196 {
197 int lev_bargain = 0;
198 int lev_identify = 0;
199
200 if (!no_bargain)
201 if (object *skill = find_skill_by_name (who, shstr_bargaining))
202 lev_bargain = skill->level;
203
204 if (const typedata *tmptype = get_typedata (tmp->type))
205 {
206 if (int idskill1 = tmptype->identifyskill)
207 {
208 int idskill2 = tmptype->identifyskill2;
209
210 if (find_skill_by_number (who, idskill1))
211 lev_identify = find_skill_by_number (who, idskill1)->level;
212
213 if (idskill2 && find_skill_by_number (who, idskill2))
214 lev_identify += find_skill_by_number (who, idskill2)->level;
215 }
216 }
217
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 double bargaining = max (0., 1. - powf (double (lev_bargain) / MAXLEVEL_TREASURE, 0.25));
224 double charisma = (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.);
225
226 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
231 if (flag == F_BUY)
232 val += val * factor;
233 else if (flag == F_SELL)
234 val -= val * factor;
235
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 approx_range = val / sqrt (lev_identify * 3 + 1);
240 }
241
242 /* I don't think this should really happen - if it does, it indicates and
243 * overflow of diff above. That should only happen if
244 * 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 /* Unidentified stuff won't sell for more than 10gp */
251 if (flag == F_SELL && !tmp->flag [FLAG_IDENTIFIED] && tmp->need_identify () && !identified)
252 min_it (val, 1000);
253
254 /* 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 val *= shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who) / shop_greed (who->map);
259 else if (flag == F_BUY)
260 {
261 /*
262 * 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 * 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 * 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 * 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 * antiques in a junk shop in real life).
273 */
274 if (tmp->flag [FLAG_PLAYER_SOLD])
275 val *= shop_specialisation_ratio (tmp, who->map);
276 else
277 val /= shop_specialisation_ratio (tmp, who->map);
278
279 val *= shop_greed (who->map) / shopkeeper_approval (who->map, who);
280 }
281
282 /* 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 * pointful, and could give fun with rounding.
285 */
286 //TODO: why use cosf at all, just % and scale linearly, gives more even distribution
287 if (val > 50)
288 val *= 1 + .05f * cosf ((tmp->uuid.seq & 0xffff) * M_PI * 2. / 0x10000);
289 }
290
291 return val;
292 }
293
294 /* Find the coin type that is worth more the 'c'. Starts at the
295 * cointype placement.
296 */
297
298 static archetype *
299 find_next_coin (sint64 c, int *cointype)
300 {
301 archetype *coin;
302
303 do
304 {
305 if (!coins [*cointype])
306 return 0;
307
308 coin = archetype::find (coins [*cointype]);
309
310 if (!coin)
311 return 0;
312
313 *cointype += 1;
314 }
315 while (coin->value > c);
316
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 const char *
331 cost_string_from_value (sint64 cost, int approx)
332 {
333 archetype *coin, *next_coin;
334 int num, cointype = 0;
335
336 coin = find_next_coin (cost, &cointype);
337 if (!coin)
338 return "nothing";
339
340 num = cost / coin->value;
341 /* 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 if (cost / coin->value > UINT32_MAX)
346 return "an unimaginable sum of money";
347
348 cost -= num * (sint64)coin->value;
349
350 char *buf = format ("%d %s", num, num > 1 ? &coin->object::name_pl : &coin->object::name);
351
352 next_coin = find_next_coin (cost, &cointype);
353 if (!next_coin || approx)
354 return buf;
355
356 coin = next_coin;
357 num = cost / coin->value;
358 cost -= num * (sint64)coin->value;
359
360 return format ("%s and %d %s", buf, num, num > 1 ? &coin->object::name_pl : &coin->object::name);
361 }
362
363 const char *
364 query_cost_string (const object *tmp, object *who, int flag)
365 {
366 sint64 real_value = query_cost (tmp, who, flag);
367 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_name (who, shstr_bargaining))
390 {
391 int num, cointype = 0;
392 archetype *coin = find_next_coin (real_value, &cointype);
393
394 if (!coin)
395 return "nothing";
396
397 num = real_value / coin->value;
398
399 if (num == 1)
400 return format ("about one %s", &coin->object::name);
401 else if (num < 5)
402 return format ("a few %s", &coin->object::name_pl);
403 else if (num < 10)
404 return format ("several %s", &coin->object::name_pl);
405 else if (num < 25)
406 return format ("a moderate amount of %s", &coin->object::name_pl);
407 else if (num < 100)
408 return format ("lots of %s", &coin->object::name_pl);
409 else if (num < 1000)
410 return format ("a great many %s", &coin->object::name_pl);
411 else
412 return format ("a vast quantity of %s", &coin->object::name_pl);
413 }
414 }
415 }
416
417 if (approx_range)
418 {
419 int hash = tmp->random_seed () & 1023;
420 sint64 lo = real_value - (approx_range * hash >> 10);
421
422 return format ("between %s and %s",
423 cost_string_from_value (lo, 1),
424 cost_string_from_value (lo + approx_range, 1));
425 }
426 }
427
428 return cost_string_from_value (real_value, 0);
429 }
430
431 /* This function finds out how much money the player is carrying,
432 * including what is in containers.
433 */
434 sint64
435 query_money (const object *op)
436 {
437 object *tmp;
438 sint64 total = 0;
439
440 if (op->type != PLAYER && op->type != CONTAINER)
441 {
442 LOG (llevError, "Query money called with non player/container\n");
443 return 0;
444 }
445
446 for (tmp = op->inv; tmp; tmp = tmp->below)
447 if (tmp->type == MONEY)
448 total += tmp->nrof * (sint64)tmp->value;
449 else if (tmp->type == CONTAINER && tmp->flag [FLAG_APPLIED] && (!tmp->race || tmp->race.contains (shstr_gold)))
450 total += query_money (tmp);
451
452 return total;
453 }
454
455 /* 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 int
461 pay_for_amount (sint64 to_pay, object *pl)
462 {
463 object *pouch;
464
465 if (to_pay == 0)
466 return 1;
467
468 if (to_pay > query_money (pl))
469 return 0;
470
471 pay_from_container (pl, pl, to_pay);
472
473 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
474 if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains (shstr_gold)))
475 pay_from_container (pl, pouch, to_pay);
476
477 pl->update_stats ();
478 return 1;
479 }
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 int
487 pay_for_item (object *op, object *pl)
488 {
489 sint64 to_pay = query_cost (op, pl, F_BUY | F_SHOP);
490 object *pouch;
491 sint64 saved_money;
492
493 if (to_pay == 0)
494 return 1;
495
496 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 change_exp (pl, saved_money, shstr_bargaining, SK_EXP_NONE);
507
508 pay_from_container (pl, pl, to_pay);
509
510 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
511 if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains (shstr_gold)))
512 pay_from_container (pl, pouch, to_pay);
513
514 pl->update_stats ();
515
516 return 1;
517 }
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 static void
534 pay_from_container (object *pl, object *pouch, sint64 &to_pay)
535 {
536 int count, i;
537 object *tmp, *next;
538 archetype *at;
539
540 if (pouch->type != PLAYER && pouch->type != CONTAINER)
541 return;
542
543 object *coin_objs[NUM_COINS] = { 0 };
544
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 if (tmp->value == tmp->arch->value && !strcmp (coins[NUM_COINS - 1 - i], tmp->arch->archname))
555 {
556 // This should not happen, but if it does, just merge the two.
557 if (coin_objs [i])
558 {
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 tmp->destroy ();
562 }
563 else
564 {
565 tmp->remove ();
566 coin_objs[i] = tmp;
567 }
568
569 break;
570 }
571 }
572
573 if (i == NUM_COINS)
574 LOG (llevError, "in pay_for_item: Did not find string match for %s\n", &tmp->arch->archname);
575 }
576 }
577
578 /* 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 if (!coin_objs[i])
582 {
583 at = archetype::find (coins[NUM_COINS - 1 - i]);
584
585 if (at == NULL)
586 LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]);
587
588 coin_objs[i] = at->instance ();
589 coin_objs[i]->nrof = 0;
590 }
591
592 for (i = 0; i < NUM_COINS; i++)
593 {
594 object &coin = *coin_objs[i];
595 sint64 num_coins = min ((to_pay + coin.value - 1) / coin.value, (sint64) coin.nrof);
596 to_pay -= num_coins * coin.value;
597
598 coin.nrof -= num_coins;
599 /* 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
605 while (to_pay < 0 && count >= 0)
606 {
607 num_coins = (-to_pay) / coin_objs[count]->value;
608 coin_objs[count]->nrof += num_coins;
609 to_pay += num_coins * coin_objs[count]->value;
610 count--;
611 }
612 }
613
614 for (i = 0; i < NUM_COINS; i++)
615 if (coin_objs[i]->nrof)
616 insert_ob_in_ob (coin_objs [i], pouch);
617 else
618 coin_objs[i]->destroy ();
619 }
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 * Returns 1 if they can, and 0 if they can't. also prints an appropriate message
624 * to the player
625 */
626 int
627 can_pay (object *pl)
628 {
629 int unpaid_count = 0;
630 sint64 unpaid_price = 0;
631 sint64 player_wealth = query_money (pl);
632
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 }
638
639 for (object::depth_iterator item = pl->begin (); item != pl->end (); ++item)
640 if (item->flag [FLAG_UNPAID])
641 {
642 unpaid_count++;
643 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP);
644 }
645
646 if (unpaid_price > player_wealth)
647 {
648 dynbuf_text &buf = msg_dynbuf; buf.clear ();
649
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 << " to be able to afford that. "
654 "H<You cannot leave a shop without paying - drop unpaid items first to be able to leave.>";
655
656 pl->failmsg (buf);
657
658 return 0;
659 }
660 else
661 return 1;
662 }
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 int
671 get_payment (object *pl)
672 {
673 for (;;)
674 {
675 next_item:
676
677 for (object::depth_iterator op = pl->begin (); op != pl->end (); ++op)
678 {
679 if (op->flag [FLAG_UNPAID])
680 {
681 const char *buf = query_cost_string (op, pl, F_BUY | F_SHOP);
682
683 if (!pay_for_item (op, pl))
684 {
685 sint64 i = query_cost (op, pl, F_BUY | F_SHOP) - query_money (pl);
686
687 op->clr_flag (FLAG_UNPAID);
688 new_draw_info_format (NDI_UNIQUE, 0, pl, "You lack %s to buy %s.", cost_string_from_value (i, 0), query_name (op));
689 op->set_flag (FLAG_UNPAID);
690 return 0;
691 }
692 else
693 {
694 op->clr_flag (FLAG_UNPAID);
695 op->clr_flag (FLAG_PLAYER_SOLD);
696 new_draw_info_format (NDI_UNIQUE, 0, pl, "You paid %s for %s.", buf, query_name (op));
697
698 if (!merge_ob (op, op->env->inv))
699 esrv_update_item (UPD_FLAGS, pl, op);
700
701 goto next_item;
702 }
703 }
704 }
705
706 return 1;
707 }
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 void
722 pay_player (object *pl, sint64 amount)
723 {
724 int count = 0;
725 archetype *at = 0;
726 object *pouch = 0;
727
728 for (count = 0; coins[count]; count++)
729 {
730 at = archetype::find (coins[count]);
731
732 if (at == NULL)
733 LOG (llevError, "Could not find %s archetype\n", coins[count]);
734 else if ((amount / at->value) > 0)
735 {
736 for (pouch = pl->inv; pouch; pouch = pouch->below)
737 {
738 if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && pouch->race.contains (shstr_gold))
739 {
740 int w = at->weight * (100 - pouch->stats.Str) / 100;
741 int n = amount / at->value;
742
743 if (w == 0)
744 w = 1; /* Prevent divide by zero */
745
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 object *tmp = at->instance ();
752 tmp->nrof = n;
753 amount -= tmp->nrof * tmp->value;
754 pouch->insert (tmp);
755 }
756 }
757 }
758
759 if (amount / at->value > 0)
760 {
761 object *tmp = at->instance ();
762 tmp->nrof = amount / tmp->value;
763 amount -= tmp->nrof * tmp->value;
764 pl->insert (tmp);
765 }
766 }
767 }
768
769 if (amount != 0)
770 LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount);
771 }
772
773 /* elmex: this is for the bank plugin :( */
774 sint64
775 pay_player_arch (object *pl, const char *arch, sint64 amount)
776 {
777 if (amount)
778 {
779 object *ob = archetype::get (arch);
780
781 if (!ob)
782 return 0;
783
784 ob->nrof = amount;
785 pl->insert (ob);
786 }
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 bool
799 sell_item (object *op, object *pl)
800 {
801 sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain;
802
803 if (pl == NULL || pl->type != PLAYER)
804 {
805 LOG (llevDebug, "Object other than player tried to sell something.\n");
806 return false;
807 }
808
809 op->custom_name = 0;
810
811 if (!amount)
812 {
813 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 }
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 change_exp (pl, extra_gain / 10, shstr_bargaining, SK_EXP_NONE);
829
830 pay_player (pl, amount);
831
832 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 pl->play_sound (sound_find ("shop_sell"));
835
836 op->set_flag (FLAG_UNPAID);
837 identify (op);
838
839 return true;
840 }
841
842 /* returns a double that is the ratio of the price that a shop will offer for
843 * item based on the shops specialisation. Does not take account of greed,
844 * returned value is between SPECIALISATION_EFFECT and 1.
845 */
846 static double
847 shop_specialisation_ratio (const object *item, const maptile *map)
848 {
849 shopitems *items = map->shopitems;
850 int likedness = 0;
851 int i;
852
853 if (item == NULL)
854 {
855 LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", &map->path);
856 return 0;
857 }
858
859 if (!item->type)
860 {
861 LOG (llevError, "shop_specialisation_ratio: passed an item with an invalid type: %s\n", item->debug_desc ());
862 /*
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 */
866 return SPECIALISATION_EFFECT;
867 }
868
869 if (map->shopitems)
870 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
874 if (likedness > 100)
875 { /* someone has been rather silly with the map headers. */
876 LOG (llevDebug, "shop_specialisation ratio: item %s on map %s is above 100%%\n", item->debug_desc (), &map->path);
877 likedness = 100;
878 }
879
880 if (likedness < -100)
881 {
882 LOG (llevDebug, "shop_specialisation ratio: item %s on map %s is below -100%%\n", item->debug_desc (), &map->path);
883 likedness = -100;
884 }
885
886 return lerp (double (likedness), -100., 100., SPECIALISATION_EFFECT, 1.);
887 }
888
889 /*returns the greed of the shop on map, or 1 if it isn't specified. */
890 static double
891 shop_greed (const maptile *map)
892 {
893 return map->shopgreed
894 ? map->shopgreed
895 : 1.;
896 }
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 double
902 shopkeeper_approval (const maptile *map, const object *player)
903 {
904 return map->shoprace && player->race != map->shoprace
905 ? DISLIKE_RATIO
906 : 1.;
907 }
908
909 /* limit the value of items based on the wealth of the shop. If the item is close
910 * 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 * want it and offer nothing. If it isn't a shop, check whether we should do generic
913 * value reduction.
914 *
915 */
916 static sint64
917 value_limit (sint64 val, int quantity, const object *who, int isshop)
918 {
919 sint64 newval, unit_price, tmpshopmax;
920 maptile *map;
921
922 unit_price = val / quantity;
923
924 if (!isshop || !who)
925 {
926 if (unit_price > 250000)
927 newval = (sint64) (250000. - pow (250000., .75) * 65. + pow (unit_price, .75) * 65.);
928 else
929 newval = unit_price;
930 }
931 else
932 {
933 if (!who->map)
934 {
935 LOG (llevError, "value_limit: asked shop price for ob %s on NULL map\n", &who->name);
936 return val;
937 }
938
939 map = who->map;
940
941 tmpshopmax = map->shopmax ? map->shopmax : 100000; // 20 royalties default
942
943 if (map->shopmin && unit_price < map->shopmin)
944 return 0;
945 else if (unit_price > tmpshopmax / 2)
946 newval = min ((tmpshopmax / 2) + isqrt (unit_price - tmpshopmax / 2), tmpshopmax);
947 else
948 newval = unit_price;
949 }
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 int
958 describe_shop (const object *op)
959 {
960 dynbuf_text buf;
961 maptile *map = op->map;
962
963 /*shopitems *items=map->shopitems; */
964 int i;
965 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 buf << "From looking at the nearby shop you determine that it trades in ";
974 int lastcomma = 0, prevcomma = 0;
975
976 if (map->shopitems)
977 for (i = 0; i < map->shopitems[0].index; i++)
978 if (map->shopitems[i].name && map->shopitems[i].strength > 10)
979 {
980 buf << map->shopitems[i].name_pl;
981 prevcomma = lastcomma;
982 lastcomma = buf.size (); // remember offset
983 buf << ", ";
984 }
985
986 if (lastcomma)
987 {
988 buf.splice (lastcomma, 2);
989
990 if (prevcomma)
991 buf.splice (prevcomma, 2, " and ");
992 }
993 else
994 buf << "a little of everything.";
995
996 buf << ".\n\n";
997
998 if (map->shopmax)
999 buf << "It won't trade for items above " << cost_string_from_value (map->shopmax, 0) << ".\n\n";
1000
1001 if (map->shopmin)
1002 buf << "It won't trade in items worth less than " << cost_string_from_value (map->shopmin, 0) << ".\n\n";
1003
1004 if (map->shopgreed)
1005 {
1006 if (map->shopgreed > 2.0)
1007 buf << "It tends to overcharge massively.\n\n";
1008 else if (map->shopgreed > 1.5)
1009 buf << "It tends to overcharge substantially.\n\n";
1010 else if (map->shopgreed > 1.1)
1011 buf << "It tends to overcharge slightly.\n\n";
1012 else if (map->shopgreed < 0.9)
1013 buf << "It tends to undercharge.\n\n";
1014 }
1015
1016 if (map->shoprace)
1017 {
1018 opinion = shopkeeper_approval (map, op);
1019
1020 if (opinion > 0.8)
1021 buf << "You think the shopkeeper likes you.\n\n";
1022 else if (opinion > 0.5)
1023 buf << "The shopkeeper seems unconcerned by you.\n\n";
1024 else
1025 buf << "The shopkeeper seems to have taken a dislike to you.\n\n";
1026 }
1027 }
1028 else
1029 buf << "There is no shop nearby.\n\n";
1030
1031 op->contr->infobox (MSG_CHANNEL ("shopinfo"), buf);
1032
1033 return 1;
1034 }
1035
1036 struct shopinv
1037 {
1038 char *item_sort;
1039 char *item_real;
1040 sint64 value;
1041 uint16 type;
1042 uint32 nrof;
1043 };
1044
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 static int
1051 shop_sort (const void *a1, const void *a2)
1052 {
1053 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2;
1054
1055 if (s1->type < s2->type)
1056 return -1;
1057
1058 if (s1->type > s2->type)
1059 return 1;
1060
1061 /* 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 }
1066
1067 static void
1068 add_shop_item (object *tmp, shopinv * items, int *numitems, int *numallocated)
1069 {
1070 /* 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
1075 tmp->clr_flag (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 items[*numitems].value = tmp->value;
1085
1086 switch (tmp->type)
1087 {
1088 #if 0
1089 case BOOTS:
1090 case GLOVES:
1091 case RING:
1092 case AMULET:
1093 case BRACERS:
1094 case GIRDLE:
1095 sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL));
1096 items[*numitems].item_sort = strdup (buf);
1097 sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL));
1098 items[*numitems].item_real = strdup (buf);
1099 (*numitems)++;
1100 break;
1101 #endif
1102
1103 default:
1104 items[*numitems].item_sort = strdup (query_base_name (tmp, 0));
1105 items[*numitems].item_real = strdup (query_base_name (tmp, 1));
1106 items[*numitems].value += tmp->value;
1107 (*numitems)++;
1108 break;
1109 }
1110
1111 tmp->set_flag (FLAG_UNPAID);
1112 }
1113
1114 void
1115 shop_listing (object *sign, object *op)
1116 {
1117 int i, j, x1, x2, y1, y2;
1118 const char *shop_coords = sign->kv [shstr_shop_coords];
1119 object *stack;
1120 shopinv *items;
1121
1122 /* Should never happen, but just in case a monster does apply a sign */
1123 if (!op->is_player ())
1124 return;
1125
1126 dynbuf_text &buf = msg_dynbuf; buf.clear ();
1127
1128 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
1136 int numallocated = 40;
1137 int numitems = 0;
1138 items = (shopinv *)malloc (sizeof (shopinv) * numallocated);
1139
1140 /* Find all the appropriate items */
1141 for (i = x1; i <= x2; i++)
1142 for (j = y1; j < y2; j++)
1143 if (op->map->is_in_shop (i, j))
1144 {
1145 stack = GET_MAP_OB (op->map, i, j);
1146
1147 while (stack)
1148 {
1149 if (stack->flag [FLAG_UNPAID])
1150 {
1151 if (numitems == numallocated)
1152 items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2));
1153
1154 add_shop_item (stack, items, &numitems, &numallocated);
1155 }
1156
1157 stack = stack->above;
1158 }
1159 }
1160
1161 buf << (numitems ? "T<This shop contains:>\n\n"
1162 : "T<This shop is currently empty.>");
1163
1164 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 items[i + 1].nrof += items[i].nrof;
1171 else
1172 {
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
1180 free (items[i].item_sort);
1181 free (items[i].item_real);
1182 }
1183
1184 op->contr->infobox (MSG_CHANNEL ("shopitems"), buf);
1185
1186 free (items);
1187 }
1188
1189 /* 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 bool
1195 maptile::is_in_shop (int x, int y) const
1196 {
1197 for (object *floor = at (x, y).bot; floor; floor = floor->above)
1198 if (floor->flag [FLAG_IS_FLOOR])
1199 return floor->type == SHOP_FLOOR;
1200
1201 return false;
1202 }
1203