ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.C
Revision: 1.48
Committed: Tue Apr 22 02:46:18 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.47: +1 -7 lines
Log Message:
- remove all "obviously safe" uses of esrv_del_item.

File Contents

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