Crossfire Server, Trunk  R20608
shop.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
21 #include "global.h"
22 
23 #include <assert.h>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "shop.h"
29 #include "sproto.h"
30 
39 #define SPECIALISATION_EFFECT 0.5
40 
42 #define DISAPPROVAL_RATIO 0.2
43 
45 #define NEUTRAL_RATIO 0.8
46 
47 static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay);
48 static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop);
49 static double shop_specialisation_ratio(const object *item, const mapstruct *map);
50 static double shop_greed(const mapstruct *map);
51 
52 #define NUM_COINS 5
54 #define LARGEST_COIN_GIVEN 2
57 static const char *const coins[] = {
58  "ambercoin",
59  "jadecoin",
60  "platinacoin",
61  "goldcoin",
62  "silvercoin",
63  NULL
64 };
65 
66 uint64_t price_base(const object *tmp) {
67  // When there are zero objects, there is really one.
68  int number = (tmp->nrof == 0) ? 1 : tmp->nrof;
69  uint64_t val = (uint64_t)tmp->value * number;
70  bool identified = QUERY_FLAG(tmp, FLAG_IDENTIFIED) || !need_identify(tmp);
71 
72  // Objects with price adjustments skip the rest of the calculations.
73  const char *key = object_get_value(tmp, "price_adjustment");
74  if (key != NULL) {
75  float ratio = atof(key);
76  return val * ratio;
77  }
78 
79  // Money and gems have fixed prices at shops.
80  if (tmp->type == MONEY || tmp->type == GEM) {
81  return val;
82  }
83 
84  // If not identified, assume it is a collection of base items.
85  if (!identified && tmp->arch)
86  {
87  val = tmp->arch->clone.value * number;
88  }
89 
95  val *= 0.8;
96  }
97 
102  val *= 1.15;
103  }
104 
105  // If an item has an archetype and is identified, compare its base enchantment.
106  if (tmp->arch != NULL && identified) {
107  int diff = tmp->magic - tmp->arch->clone.magic;
108  val *= pow(1.15, diff);
109  }
110 
111  // FIXME: Is the 'baseline' 50 charges per wand?
112  if (tmp->type == WAND) {
113  val *= tmp->stats.food / 50.0;
114  }
115 
116  /* we need to multiply these by 4.0 to keep buy costs roughly the same
117  * (otherwise, you could buy a potion of charisma for around 400 pp.
118  * Arguable, the costs in the archetypes should be updated to better
119  * reflect values (potion charisma list for 1250 gold)
120  */
121  val *= 4;
122 
123  return val;
124 }
125 
126 uint64_t price_approx(const object *tmp, object *who) {
127  uint64_t val = price_base(tmp);
128 
129  /* If we are approximating, then the value returned should
130  * be allowed to be wrong however merely using a random number
131  * each time will not be sufficient, as then multiple examinations
132  * would give different answers, so we'll use the count instead.
133  * By taking the sine of the count, a value between -1 and 1 is
134  * generated, we then divide by the square root of the bargaining
135  * skill and the appropriate identification skills, so that higher
136  * level players get better estimates. (We need a +1 there to
137  * prevent dividing by zero.)
138  */
139  const typedata *tmptype = get_typedata(tmp->type);
140  int lev_identify = 0;
141 
142  if (tmptype) {
143  int idskill1 = tmptype->identifyskill;
144  if (idskill1) {
145  int idskill2 = tmptype->identifyskill2;
146  if (find_skill_by_number(who, idskill1)) {
147  lev_identify = find_skill_by_number(who, idskill1)->level;
148  }
149  if (idskill2 && find_skill_by_number(who, idskill2)) {
150  lev_identify += find_skill_by_number(who, idskill2)->level;
151  }
152  }
153  } else {
154  LOG(llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->name);
155  }
156  val += val * (sin(tmp->count) / sqrt(lev_identify * 3 + 1.0));
157 
158  return val;
159 }
160 
167 static float shop_buy_multiplier(int charisma) {
168  float multiplier = 1 / (0.38 * pow(1.06, charisma));
169 
170  if (multiplier > 2) {
171  return 2;
172  } else if (multiplier < 0.5) {
173  return 0.5;
174  } else {
175  return multiplier;
176  }
177 }
178 
186 static float shop_bargain_multiplier(int lev_bargain) {
187  return 1 - 0.5 * lev_bargain / settings.max_level;
188 }
189 
190 uint64_t shop_price_buy(const object *tmp, object *who) {
191  assert(who != NULL && who->type == PLAYER);
192  uint64_t val = price_base(tmp);
193 
194  const char *key = object_get_value(tmp, "price_adjustment_buy");
195  if (key != NULL) {
196  float ratio = atof(key);
197  return val * ratio;
198  }
199 
200  if (tmp->type == GEM) {
201  return 1.03 * val;
202  }
203 
204  int bargain_level = 0;
206  bargain_level = find_skill_by_number(who, SK_BARGAINING)->level;
207  }
208 
209  float multiplier = shop_bargain_multiplier(bargain_level);
210  multiplier *= shop_buy_multiplier(who->stats.Cha);
211 
212  // Limit buy price multiplier to 0.5, no matter what.
213  val *= multiplier > 0.5 ? multiplier : 0.5;
214 
215  /*
216  * When buying, if the item was sold by another player, it is
217  * ok to let the item be sold cheaper, according to the
218  * specialisation of the shop. If a player sold an item here,
219  * then his sale price was multiplied by the specialisation
220  * ratio, to do the same to the buy price will not generate
221  * extra money. However, the same is not true of generated
222  * items, these have to /divide/ by the specialisation, so
223  * that the price is never less than what they could
224  * be sold for (otherwise players could camp map resets to
225  * make money).
226  * In game terms, a non-specialist shop might not recognise
227  * the true value of the items it sells (much like how people
228  * sometimes find antiques in a junk shop in real life).
229  */
230  if (QUERY_FLAG(tmp, FLAG_PLAYER_SOLD)) {
231  val = (int64_t)val*shop_greed(who->map)
232  *shop_specialisation_ratio(tmp, who->map)
233  /shop_approval(who->map, who);
234  } else {
235  val = (int64_t)val*shop_greed(who->map)
236  /(shop_specialisation_ratio(tmp, who->map)
237  *shop_approval(who->map, who));
238  }
239 
240  return val;
241 }
242 
243 uint64_t shop_price_sell(const object *tmp, object *who) {
244  assert(who != NULL && who->type == PLAYER);
245  uint64_t val = price_base(tmp);
246  bool identified = QUERY_FLAG(tmp, FLAG_IDENTIFIED) || !need_identify(tmp);
247 
248  const char *key = object_get_value(tmp, "price_adjustment_sell");
249  if (key != NULL) {
250  float ratio = atof(key);
251  return val * ratio;
252  }
253 
254  if (tmp->type == GEM) {
255  return 0.97 * val;
256  }
257 
258  // Shops value unidentified items less.
259  if (!identified) {
260  if (tmp->arch != NULL) {
261  // Unidentified standard objects are only worth a little less.
262  if (QUERY_FLAG(tmp, FLAG_BEEN_APPLIED)) {
263  val *= 0.85;
264  } else {
265  val *= 0.70;
266  }
267  } else {
268  // No archetype, so probably an artifact.
269  val /= 2;
270  }
271  }
272 
273  // Selling to shops yields roughly 50% of the base price.
274  val /= 2;
275 
276  /* Limit amount of money you can get for really great items. */
277  int number = (tmp->nrof == 0) ? 1 : tmp->nrof;
278  val = value_limit(val, number, who, 1);
279 
280  val = (int64_t)val*shop_specialisation_ratio(tmp, who->map)*
281  shop_approval(who->map, who)/shop_greed(who->map);
282  return val;
283 }
284 
296 static archetype *find_next_coin(uint64_t c, int *cointype) {
297  archetype *coin;
298 
299  do {
300  if (coins[*cointype] == NULL)
301  return NULL;
302  coin = find_archetype(coins[*cointype]);
303  if (coin == NULL)
304  return NULL;
305  *cointype += 1;
306  } while (coin->clone.value > (int64_t) c);
307 
308  return coin;
309 }
310 
330 char* cost_string_from_value(uint64_t cost, int largest_coin) {
331  archetype *coin, *next_coin;
332  uint32_t num;
333  int cointype = largest_coin;
334 
335  if (cointype < 0)
336  cointype = 0;
337  else if (cointype >= NUM_COINS)
338  cointype = NUM_COINS - 1;
339 
341  coin = find_next_coin(cost, &cointype);
342  if (coin == NULL) {
343  stringbuffer_append_string(buf, "nothing");
344  goto done;
345  }
346 
347  num = cost/coin->clone.value;
348  /* so long as nrof is 32 bit, this is true.
349  * If it takes more coins than a person can possibly carry, this
350  * is basically true.
351  */
352  if ((cost/coin->clone.value) > UINT32_MAX) {
353  stringbuffer_append_string(buf, "an unimaginable sum of money.");
354  goto done;
355  }
356 
357  cost -= (uint64_t)num*(uint64_t)coin->clone.value;
358  if (num == 1)
359  stringbuffer_append_printf(buf, "1 %s", coin->clone.name);
360  else
361  stringbuffer_append_printf(buf, "%u %ss", num, coin->clone.name);
362 
363  next_coin = find_next_coin(cost, &cointype);
364  if (next_coin == NULL)
365  goto done;
366 
367  do {
368  coin = next_coin;
369  num = cost/coin->clone.value;
370  cost -= (uint64_t)num*(uint64_t)coin->clone.value;
371 
372  if (cost == 0)
373  next_coin = NULL;
374  else
375  next_coin = find_next_coin(cost, &cointype);
376 
377  if (next_coin) {
378  /* There will be at least one more string to add to the list,
379  * use a comma.
380  */
381  stringbuffer_append_string(buf, ", ");
382  } else {
383  stringbuffer_append_string(buf, " and ");
384  }
385  if (num == 1)
386  stringbuffer_append_printf(buf, "1 %s", coin->clone.name);
387  else
388  stringbuffer_append_printf(buf, "%u %ss", num, coin->clone.name);
389  } while (next_coin);
390 
391 done:
392  return stringbuffer_finish(buf);
393 }
394 
405 static StringBuffer *real_money_value(const object *coin, StringBuffer *buf) {
406  assert(coin->type == MONEY);
407  assert(buf);
408 
409  stringbuffer_append_printf(buf, "%ld %s", (long)coin->nrof, coin->nrof == 1 ? coin->name : coin->name_pl);
410  return buf;
411 }
412 
413 char *cost_str(uint64_t cost) {
415 }
416 
417 char *cost_approx_str(const object *tmp, object *who) {
418  uint64_t approx_val = price_approx(tmp, who);
419  int idskill1 = 0;
420  int idskill2 = 0;
421  const typedata *tmptype;
422 
424 
425  /* money it's pretty hard to not give the exact price, so skip all logic and just return the real value. */
426  if (tmp->type == MONEY) {
427  return stringbuffer_finish(real_money_value(tmp, buf));
428  }
429 
430  tmptype = get_typedata(tmp->type);
431  if (tmptype) {
432  idskill1 = tmptype->identifyskill;
433  idskill2 = tmptype->identifyskill2;
434  }
435 
436  /* we show an approximate price if
437  * 1) we are approximating
438  * 2) there either is no id skill(s) for the item, or we don't have them
439  * 3) we don't have bargaining skill either
440  */
441  if (!idskill1 || !find_skill_by_number(who, idskill1)) {
442  if (!idskill2 || !find_skill_by_number(who, idskill2)) {
443  if (!find_skill_by_number(who, SK_BARGAINING)) {
444  int num;
445  int cointype = LARGEST_COIN_GIVEN;
446  archetype *coin = find_next_coin(approx_val, &cointype);
447 
448  if (coin == NULL) {
449  stringbuffer_append_string(buf, "nothing");
450  return stringbuffer_finish(buf);
451  }
452 
453  num = approx_val/coin->clone.value;
454  if (num == 1)
455  stringbuffer_append_printf(buf, "about one %s", coin->clone.name);
456  else if (num < 5)
457  stringbuffer_append_printf(buf, "a few %s", coin->clone.name_pl);
458  else if (num < 10)
459  stringbuffer_append_printf(buf, "several %s", coin->clone.name_pl);
460  else if (num < 25)
461  stringbuffer_append_printf(buf, "a moderate amount of %s", coin->clone.name_pl);
462  else if (num < 100)
463  stringbuffer_append_printf(buf, "lots of %s", coin->clone.name_pl);
464  else if (num < 1000)
465  stringbuffer_append_printf(buf, "a great many %s", coin->clone.name_pl);
466  else
467  stringbuffer_append_printf(buf, "a vast quantity of %s", coin->clone.name_pl);
468  return stringbuffer_finish(buf);
469  }
470  }
471  }
472 
473  // If we get here, return the price we guessed.
474  stringbuffer_delete(buf);
475  return cost_str(approx_val);
476 }
477 
478 uint64_t query_money(const object *op) {
479  uint64_t total = 0;
480 
481  if (op->type != PLAYER && op->type != CONTAINER) {
482  LOG(llevError, "Query money called with non player/container\n");
483  return 0;
484  }
485  FOR_INV_PREPARE(op, tmp) {
486  if (tmp->type == MONEY) {
487  total += (uint64_t)tmp->nrof*(uint64_t)tmp->value;
488  } else if (tmp->type == CONTAINER
489  && QUERY_FLAG(tmp, FLAG_APPLIED)
490  && (tmp->race == NULL || strstr(tmp->race, "gold"))) {
491  total += query_money(tmp);
492  }
493  } FOR_INV_FINISH();
494  return total;
495 }
496 
509 int pay_for_amount(uint64_t to_pay, object *pl) {
510  if (to_pay == 0)
511  return 1;
512  if (to_pay > query_money(pl))
513  return 0;
514 
515  to_pay = pay_from_container(pl, pl, to_pay);
516 
517  FOR_INV_PREPARE(pl, pouch) {
518  if (to_pay <= 0)
519  break;
520  if (pouch->type == CONTAINER
521  && QUERY_FLAG(pouch, FLAG_APPLIED)
522  && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
523  to_pay = pay_from_container(pl, pouch, to_pay);
524  }
525  } FOR_INV_FINISH();
526  if (to_pay > 0) {
527  LOG(llevError, "pay_for_amount: Cannot remove enough money -- %"FMT64U" remains\n", to_pay);
528  }
529 
530  fix_object(pl);
531  return 1;
532 }
533 
548 int pay_for_item(object *op, object *pl) {
549  uint64_t to_pay = shop_price_buy(op, pl);
550 
551  if (to_pay == 0)
552  return 1;
553  if (to_pay > query_money(pl))
554  return 0;
555 
556  /* We compare the paid price with the one for a player
557  * without bargaining skill.
558  * This determins the amount of exp (if any) gained for bargaining.
559  */
560  int64_t saved_money = price_base(op) - to_pay;
561  if (saved_money > 0)
562  change_exp(pl, saved_money, "bargaining", SK_EXP_NONE);
563 
564  to_pay = pay_from_container(pl, pl, to_pay);
565 
566  FOR_INV_PREPARE(pl, pouch) {
567  if (to_pay <= 0)
568  break;
569  if (pouch->type == CONTAINER
570  && QUERY_FLAG(pouch, FLAG_APPLIED)
571  && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
572  to_pay = pay_from_container(pl, pouch, to_pay);
573  }
574  } FOR_INV_FINISH();
575  if (to_pay > 0) {
576  LOG(llevError, "pay_for_item: Cannot remove enough money -- %"FMT64U" remains\n", to_pay);
577  }
579  SET_FLAG(op, FLAG_WAS_WIZ);
580  fix_object(pl);
581  return 1;
582 }
583 
595 static int64_t remove_value(object *coin_objs[], int64_t remain) {
596  int i;
597 
598  for (i = 0; i < NUM_COINS; i++) {
599  int count;
600  int64_t num_coins;
601 
602  if ((int64_t)coin_objs[i]->nrof * coin_objs[i]->value > remain) {
603  num_coins = remain/coin_objs[i]->value;
604  if ((uint64_t)num_coins*(uint64_t)coin_objs[i]->value < (uint64_t) remain) {
605  num_coins++;
606  }
607  } else {
608  num_coins = coin_objs[i]->nrof;
609  }
610  remain -= (int64_t)num_coins*(int64_t)coin_objs[i]->value;
611  coin_objs[i]->nrof -= num_coins;
612  /* Now start making change. Start at the coin value
613  * below the one we just did, and work down to
614  * the lowest value.
615  */
616  count = i-1;
617  while (remain < 0 && count >= 0) {
618  num_coins = -remain/coin_objs[count]->value;
619  coin_objs[count]->nrof += num_coins;
620  remain += num_coins*coin_objs[count]->value;
621  count--;
622  }
623  }
624 
625  return remain;
626 }
627 
636 static void add_value(object *coin_objs[], int64_t value) {
637  int i;
638 
639  for (i = NUM_COINS-LARGEST_COIN_GIVEN-1; i >= 0; i--) {
640  uint32_t nrof;
641 
642  nrof = (uint32_t)(value/coin_objs[i]->value);
643  value -= nrof*coin_objs[i]->value;
644  coin_objs[i]->nrof += nrof;
645  }
646 }
647 
660 static void insert_objects(object *pl, object *container, object *objects[], int objects_len) {
661  int i, one = 0;
662 
663  for (i = 0; i < objects_len; i++) {
664  if (objects[i]->nrof > 0) {
665  object_insert_in_ob(objects[i], container);
666  one = 1;
667  } else {
668  object_free_drop_inventory(objects[i]);
669  }
670  }
671  if (one)
672  esrv_update_item(UPD_WEIGHT, pl, container);
673 }
674 
691 static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay) {
692  size_t i;
693  int64_t remain;
694  object *coin_objs[NUM_COINS];
695  object *other_money[16]; /* collects MONEY objects not matching coins[] */
696  size_t other_money_len; /* number of allocated entries in other_money[] */
697  archetype *at;
698 
699  if (pouch->type != PLAYER && pouch->type != CONTAINER)
700  return to_pay;
701 
702  remain = to_pay;
703  for (i = 0; i < NUM_COINS; i++)
704  coin_objs[i] = NULL;
705 
706  /* This hunk should remove all the money objects from the player/container */
707  other_money_len = 0;
708  FOR_INV_PREPARE(pouch, tmp) {
709  if (tmp->type == MONEY) {
710  for (i = 0; i < NUM_COINS; i++) {
711  if (!strcmp(coins[NUM_COINS-1-i], tmp->arch->name)
712  && (tmp->value == tmp->arch->clone.value)) {
713  /* This should not happen, but if it does, just
714  * merge the two.
715  */
716  if (coin_objs[i] != NULL) {
717  LOG(llevError, "%s has two money entries of (%s)\n", pouch->name, coins[NUM_COINS-1-i]);
718  object_remove(tmp);
719  coin_objs[i]->nrof += tmp->nrof;
721  } else {
722  object_remove(tmp);
723  coin_objs[i] = tmp;
724  }
725  break;
726  }
727  }
728  if (i == NUM_COINS) {
729  if (other_money_len >= sizeof(other_money)/sizeof(*other_money)) {
730  LOG(llevError, "pay_for_item: Cannot store non-standard money object %s\n", tmp->arch->name);
731  } else {
732  object_remove(tmp);
733  other_money[other_money_len++] = tmp;
734  }
735  }
736  }
737  } FOR_INV_FINISH();
738 
739  /* Fill in any gaps in the coin_objs array - needed to make change. */
740  /* Note that the coin_objs array goes from least value to greatest value */
741  for (i = 0; i < NUM_COINS; i++)
742  if (coin_objs[i] == NULL) {
743  at = find_archetype(coins[NUM_COINS-1-i]);
744  if (at == NULL)
745  LOG(llevError, "Could not find %s archetype\n", coins[NUM_COINS-1-i]);
746  coin_objs[i] = object_new();
747  object_copy(&at->clone, coin_objs[i]);
748  coin_objs[i]->nrof = 0;
749  }
750 
751  /* Try to pay from standard coins first. */
752  remain = remove_value(coin_objs, remain);
753 
754  /* Now pay from non-standard coins until all is paid. */
755  for (i = 0; i < other_money_len && remain > 0; i++) {
756  uint32_t nrof;
757  object *coin;
758 
759  coin = other_money[i];
760 
761  /* Find the minimal number of coins to use. This prevents converting
762  * excess non-standard coins to standard money.
763  */
764  nrof = (remain+coin->value-1)/coin->value;
765  if (nrof > coin->nrof) {
766  nrof = coin->nrof;
767  }
768  coin->nrof -= nrof;
769  add_value(coin_objs, nrof*coin->value);
770 
771  remain = remove_value(coin_objs, remain);
772  }
773 
774  /* re-insert remaining coins into player */
775  insert_objects(pl, pouch, coin_objs, NUM_COINS);
776  insert_objects(pl, pouch, other_money, other_money_len);
777 
778  return(remain);
779 }
780 
797 static void count_unpaid(object *pl, object *item, int *unpaid_count, uint64_t *unpaid_price, uint32_t *coincount) {
798  int i;
799 
801  if (QUERY_FLAG(item, FLAG_UNPAID)) {
802  (*unpaid_count)++;
803  (*unpaid_price) += shop_price_buy(item, pl);
804  }
805  /* Merely converting the player's monetary wealth won't do.
806  * If we did that, we could print the wrong numbers for the
807  * coins, so we count the money instead.
808  */
809  for (i = 0; i < NUM_COINS; i++)
810  if (!strcmp(coins[i], item->arch->name)) {
811  coincount[i] += item->nrof;
812  break;
813  }
814  if (item->inv)
815  count_unpaid(pl, item->inv, unpaid_count, unpaid_price, coincount);
817 }
818 
831 int can_pay(object *pl) {
832  int unpaid_count = 0, i;
833  uint64_t unpaid_price = 0;
834  uint64_t player_wealth = query_money(pl);
835  uint32_t coincount[NUM_COINS];
836 
837  if (!pl || pl->type != PLAYER) {
838  LOG(llevError, "can_pay(): called against something that isn't a player\n");
839  return 0;
840  }
841 
842  for (i = 0; i < NUM_COINS; i++)
843  coincount[i] = 0;
844 
845  count_unpaid(pl, pl->inv, &unpaid_count, &unpaid_price, coincount);
846 
847  if (unpaid_price > player_wealth) {
848  char buf[MAX_BUF], coinbuf[MAX_BUF];
849  int denominations = 0;
850  char *value = cost_str(unpaid_price);
851 
852  snprintf(buf, sizeof(buf), "You have %d unpaid items that would cost you %s, ", unpaid_count, value);
853  free(value);
854  for (i = 0; i < NUM_COINS; i++) {
855  if (coincount[i] > 0 && coins[i]) {
856  if (denominations == 0)
857  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "but you only have");
858  denominations++;
859  snprintf(coinbuf, sizeof(coinbuf), " %u %s,", coincount[i], find_archetype(coins[i])->clone.name_pl);
860  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", coinbuf);
861  }
862  }
863  if (denominations == 0)
864  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "but you don't have any money.");
865  else if (denominations > 1)
866  make_list_like(buf);
868  MSG_TYPE_SHOP_PAYMENT, buf);
869  return 0;
870  } else
871  return 1;
872 }
873 
874 int shop_pay_unpaid(object *pl, object *op) {
875  char name_op[MAX_BUF];
876  int ret = 1;
877 
878  if (op != NULL && op->inv)
879  ret = shop_pay_unpaid(pl, op->inv);
880 
881  if (!ret)
882  return 0;
883 
884  if (op != NULL && op->below)
885  ret = shop_pay_unpaid(pl, op->below);
886 
887  if (!ret)
888  return 0;
889 
890  if (op != NULL && QUERY_FLAG(op, FLAG_UNPAID)) {
891  uint64_t price = shop_price_buy(op, pl);
892  if (!pay_for_item(op, pl)) {
893  uint64_t i = price - query_money(pl);
894  char *missing = cost_str(i);
895 
896  CLEAR_FLAG(op, FLAG_UNPAID);
897  query_name(op, name_op, MAX_BUF);
900  "You lack %s to buy %s.",
901  missing, name_op);
902  free(missing);
903  SET_FLAG(op, FLAG_UNPAID);
904  return 0;
905  } else {
906  object *tmp;
907  char *value = cost_str(price);
908 
909  CLEAR_FLAG(op, FLAG_UNPAID);
911  query_name(op, name_op, MAX_BUF);
914  "You paid %s for %s.",
915  value, name_op);
916  free(value);
917  tmp = object_merge(op, NULL);
918  if (pl->type == PLAYER && !tmp) {
919  /* If item wasn't merged we update it. If merged, object_merge() handled everything for us. */
921  }
922  }
923  }
924  return 1;
925 }
926 
940 void sell_item(object *op, object *pl) {
941  object *tmp;
942  archetype *at;
943  char obj_name[MAX_BUF];
944 
945  query_name(op, obj_name, MAX_BUF);
946 
947  if (pl == NULL || pl->type != PLAYER) {
948  LOG(llevDebug, "Object other than player tried to sell something.\n");
949  return;
950  }
951 
952  if (execute_event(op, EVENT_SELLING, pl, NULL, NULL, SCRIPT_FIX_ALL) != 0)
953  return;
954 
955  if (op->custom_name)
957 
958  uint64_t price = shop_price_sell(op, pl);
959  if (price == 0) {
962  "We're not interested in %s.",
963  obj_name);
964  return;
965  }
966 
967  /* We compare the price with the one for a player
968  * without bargaining skill.
969  * This determins the amount of exp (if any) gained for bargaining.
970  * exp/10 -> 1 for each gold coin
971  */
972  int64_t extra_gain = price - price_base(op);
973  if (extra_gain > 0) {
974  change_exp(pl, extra_gain/10, "bargaining", SK_EXP_NONE);
975  }
976 
977  char *value_str = cost_str(price);
979  "You receive %s for %s.", value_str, obj_name);
980  free(value_str);
981 
982  for (int count = LARGEST_COIN_GIVEN; coins[count] != NULL; count++) {
983  at = find_archetype(coins[count]);
984  if (at == NULL)
985  LOG(llevError, "Could not find %s archetype\n", coins[count]);
986  else if ((price/at->clone.value) > 0) {
987  FOR_INV_PREPARE(pl, pouch) {
988  if (pouch->type == CONTAINER
989  && QUERY_FLAG(pouch, FLAG_APPLIED)
990  && pouch->race
991  && strstr(pouch->race, "gold")) {
992  int w = at->clone.weight*(100-pouch->stats.Str)/100;
993  int n = price/at->clone.value;
994 
995  if (w == 0)
996  w = 1; /* Prevent divide by zero */
997  if (n > 0
998  && (!pouch->weight_limit || pouch->carrying+w <= pouch->weight_limit)) {
999  if (pouch->weight_limit
1000  && (pouch->weight_limit-pouch->carrying)/w < n)
1001  n = (pouch->weight_limit-pouch->carrying)/w;
1002 
1003  tmp = object_new();
1004  object_copy(&at->clone, tmp);
1005  tmp->nrof = n;
1006  price -= (uint64_t)tmp->nrof*(uint64_t)tmp->value;
1007  tmp = object_insert_in_ob(tmp, pouch);
1008  esrv_update_item(UPD_WEIGHT, pl, pl);
1009  }
1010  }
1011  } FOR_INV_FINISH();
1012  if (price/at->clone.value > 0) {
1013  tmp = object_new();
1014  object_copy(&at->clone, tmp);
1015  tmp->nrof = price/tmp->value;
1016  price -= (uint64_t)tmp->nrof*(uint64_t)tmp->value;
1017  tmp = object_insert_in_ob(tmp, pl);
1018  esrv_update_item(UPD_WEIGHT, pl, pl);
1019  }
1020  }
1021  }
1022 
1023  if (price != 0) {
1024  LOG(llevError, "Warning - payment not zero: %" PRIo64 "\n", price);
1025  }
1026 
1027  SET_FLAG(op, FLAG_UNPAID);
1028  identify(op);
1029 }
1030 
1044 static double shop_specialisation_ratio(const object *item, const mapstruct *map) {
1045  shopitems *items = map->shopitems;
1046  double ratio = SPECIALISATION_EFFECT, likedness = 0.001;
1047  int i;
1048 
1049  if (item == NULL) {
1050  LOG(llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path);
1051  return 0;
1052  }
1053  if (item->type == (uint8_t)-1) {
1054  LOG(llevError, "shop_specialisation_ratio: passed an item with an invalid type\n");
1055  /*
1056  * I'm not really sure what the /right/ thing to do here is,
1057  * these types of item shouldn't exist anyway, but returning
1058  * the ratio is probably the best bet.."
1059  */
1060  return ratio;
1061  }
1062  if (map->shopitems) {
1063  for (i = 0; i < items[0].index; i++)
1064  if (items[i].typenum == item->type || (items[i].typenum == -1 && likedness == 0.001))
1065  likedness = items[i].strength/100.0;
1066  }
1067  if (likedness > 1.0) { /* someone has been rather silly with the map headers. */
1068  LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, map->path);
1069  likedness = 1.0;
1070  }
1071  if (likedness < -1.0) {
1072  LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, map->path);
1073  likedness = -1.0;
1074  }
1075  ratio = ratio+(1.0-ratio)*likedness;
1076  if (ratio <= 0.1)
1077  ratio = 0.1; /* if the ratio were much lower than this, we would get silly prices */
1078  return ratio;
1079 }
1080 
1089 static double shop_greed(const mapstruct *map) {
1090  double greed = 1.0;
1091 
1092  if (map->shopgreed)
1093  return map->shopgreed;
1094  return greed;
1095 }
1096 
1097 double shop_approval(const mapstruct *map, const object *player) {
1098  double approval = 1.0;
1099 
1100  if (map->shoprace) {
1101  approval = NEUTRAL_RATIO;
1102  if (player->race && !strcmp(player->race, map->shoprace))
1103  approval = 1.0;
1104  }
1105  return approval;
1106 }
1107 
1127 static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop) {
1128  uint64_t newval, unit_price;
1129  mapstruct *map;
1130 
1131  unit_price = val/quantity;
1132  if (!isshop || !who) {
1133  if (unit_price > 10000)
1134  newval = 8000+isqrt(unit_price)*20;
1135  else
1136  newval = unit_price;
1137  } else {
1138  if (!who->map) {
1139  LOG(llevError, "value_limit: asked shop price for ob %s on NULL map\n", who->name);
1140  return val;
1141  }
1142  map = who->map;
1143  if (map->shopmin && unit_price < map->shopmin)
1144  return 0;
1145  else if (map->shopmax && unit_price > map->shopmax/2)
1146  newval = MIN((map->shopmax/2)+isqrt(unit_price-map->shopmax/2), map->shopmax);
1147  else if (unit_price > 10000)
1148  newval = 8000+isqrt(unit_price)*20;
1149  else
1150  newval = unit_price;
1151  }
1152  newval *= quantity;
1153  return newval;
1154 }
1155 
1156 int shop_describe(const object *op) {
1157  mapstruct *map = op->map;
1158  /*shopitems *items=map->shopitems;*/
1159  int pos = 0, i;
1160  double opinion = 0;
1161  char tmp[MAX_BUF] = "\0", *value;
1162 
1163  if (op->type != PLAYER)
1164  return 0;
1165 
1166  /*check if there is a shop specified for this map */
1167  if (map->shopitems
1168  || map->shopgreed
1169  || map->shoprace
1170  || map->shopmin
1171  || map->shopmax) {
1173  "From looking at the nearby shop you determine that it trades in:");
1174 
1175  if (map->shopitems) {
1176  for (i = 0; i < map->shopitems[0].index; i++) {
1177  if (map->shopitems[i].name && map->shopitems[i].strength > 10) {
1178  snprintf(tmp+pos, sizeof(tmp)-pos, "%s, ", map->shopitems[i].name_pl);
1179  pos += strlen(tmp+pos);
1180  }
1181  }
1182  }
1183  if (!pos)
1184  strcpy(tmp, "a little of everything.");
1185 
1186  /* format the string into a list */
1187  make_list_like(tmp);
1188  draw_ext_info(NDI_UNIQUE, 0, op,
1190 
1191  if (map->shopmax) {
1192  value = cost_str(map->shopmax);
1195  "It won't trade for items above %s.",
1196  value);
1197  free(value);
1198  }
1199 
1200  if (map->shopmin) {
1201  value = cost_str(map->shopmin);
1204  "It won't trade in items worth less than %s.",
1205  value);
1206  free(value);
1207  }
1208 
1209  if (map->shopgreed) {
1210  if (map->shopgreed > 2.0)
1211  draw_ext_info(NDI_UNIQUE, 0, op,
1213  "It tends to overcharge massively.");
1214  else if (map->shopgreed > 1.5)
1215  draw_ext_info(NDI_UNIQUE, 0, op,
1217  "It tends to overcharge substantially.");
1218  else if (map->shopgreed > 1.1)
1219  draw_ext_info(NDI_UNIQUE, 0, op,
1221  "It tends to overcharge slightly.");
1222  else if (map->shopgreed < 0.9)
1223  draw_ext_info(NDI_UNIQUE, 0, op,
1225  "It tends to undercharge.");
1226  }
1227  if (map->shoprace) {
1228  opinion = shop_approval(map, op);
1229  if (opinion > 0.8)
1230  draw_ext_info(NDI_UNIQUE, 0, op,
1232  "You think the shopkeeper likes you.");
1233  else if (opinion > 0.5)
1234  draw_ext_info(NDI_UNIQUE, 0, op,
1236  "The shopkeeper seems unconcerned by you.");
1237  else
1238  draw_ext_info(NDI_UNIQUE, 0, op,
1240  "The shopkeeper seems to have taken a dislike to you.");
1241  }
1243  "There is no shop nearby.");
1244 
1245  return 1;
1246 }
1247 
1251 static bool coords_in_shop(mapstruct *map, int x, int y) {
1252  FOR_MAP_PREPARE(map, x, y, floor)
1253  if (floor->type == SHOP_FLOOR) return true;
1254  FOR_MAP_FINISH();
1255  return false;
1256 }
1257 
1258 bool shop_contains(object *ob) {
1259  if (!ob->map) return 0;
1260  return coords_in_shop(ob->map, ob->x, ob->y);
1261 }
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:315
char path[HUGE_BUF]
Definition: map.h:365
#define FLAG_KNOWN_BLESSED
Definition: define.h:379
uint64_t price_base(const object *tmp)
Definition: shop.c:66
Definition: player.h:92
#define FLAG_PLAYER_SOLD
Definition: define.h:252
double shopgreed
Definition: map.h:358
archetype * find_archetype(const char *name)
Definition: arch.c:692
#define FLAG_DAMNED
Definition: define.h:318
#define FLAG_UNPAID
Definition: define.h:236
#define MSG_TYPE_SHOP_LISTING
Definition: newclient.h:486
static archetype * find_next_coin(uint64_t c, int *cointype)
Definition: shop.c:296
static const char *const coins[]
Definition: shop.c:57
uint64_t query_money(const object *op)
Definition: shop.c:478
static StringBuffer * real_money_value(const object *coin, StringBuffer *buf)
Definition: shop.c:405
const char * race
Definition: object.h:318
uint64_t shop_price_sell(const object *tmp, object *who)
Definition: shop.c:243
#define MSG_TYPE_SHOP_SELL
Definition: newclient.h:492
#define SET_FLAG(xyz, p)
Definition: define.h:223
unsigned char uint8_t
Definition: win32.h:161
int identifyskill
Definition: define.h:93
#define NUM_COINS
Definition: shop.c:52
static bool coords_in_shop(mapstruct *map, int x, int y)
Definition: shop.c:1251
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
int16_t max_level
Definition: global.h:298
static double shop_specialisation_ratio(const object *item, const mapstruct *map)
Definition: shop.c:1044
#define FMT64U
Definition: compat.h:13
#define MSG_TYPE_SHOP_PAYMENT
Definition: newclient.h:489
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:342
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4246
Definition: object.h:137
static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop)
Definition: shop.c:1127
double shop_approval(const mapstruct *map, const object *player)
Definition: shop.c:1097
uint64_t shop_price_buy(const object *tmp, object *who)
Definition: shop.c:190
object clone
Definition: object.h:470
bool shop_contains(object *ob)
Definition: shop.c:1258
int isqrt(int n)
Definition: utils.c:585
static void count_unpaid(object *pl, object *item, int *unpaid_count, uint64_t *unpaid_price, uint32_t *coincount)
Definition: shop.c:797
#define FLAG_BLESSED
Definition: define.h:378
const char * name
Definition: map.h:306
static void insert_objects(object *pl, object *container, object *objects[], int objects_len)
Definition: shop.c:660
#define FALSE
Definition: compat.h:11
#define SCRIPT_FIX_ALL
Definition: global.h:359
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:310
Definition: object.h:465
Definition: object.h:220
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:789
#define MIN(x, y)
Definition: compat.h:17
int typenum
Definition: map.h:308
int can_pay(object *pl)
Definition: shop.c:831
uint64_t price_approx(const object *tmp, object *who)
Definition: shop.c:126
const typedata * get_typedata(int itemtype)
Definition: item.c:335
static void add_value(object *coin_objs[], int64_t value)
Definition: shop.c:636
void make_list_like(char *input)
Definition: utils.c:394
void object_free_drop_inventory(object *ob)
Definition: object.c:1368
int16_t y
Definition: object.h:326
int pay_for_item(object *op, object *pl)
Definition: shop.c:548
static int64_t remove_value(object *coin_objs[], int64_t remain)
Definition: shop.c:595
object * object_new(void)
Definition: object.c:1037
const char * name_pl
Definition: object.h:315
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Definition: stringbuffer.c:95
int index
Definition: map.h:311
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2690
char * cost_string_from_value(uint64_t cost, int largest_coin)
Definition: shop.c:330
int32_t weight
Definition: object.h:365
int8_t strength
Definition: map.h:309
struct mapdef * map
Definition: object.h:297
#define snprintf
Definition: win32.h:46
uint64_t shopmin
Definition: map.h:359
#define FLAG_IDENTIFIED
Definition: define.h:261
#define FOR_INV_FINISH()
Definition: define.h:712
#define MSG_TYPE_SHOP_MISC
Definition: newclient.h:493
object * objects
Definition: object.c:60
const char * name
Definition: object.h:311
char * cost_approx_str(const object *tmp, object *who)
Definition: shop.c:417
static double shop_greed(const mapstruct *map)
Definition: shop.c:1089
struct obj * below
Definition: object.h:287
uint32_t nrof
Definition: object.h:333
int8_t Cha
Definition: living.h:35
#define UPD_FLAGS
Definition: newclient.h:290
uint8_t real_wiz
Definition: global.h:267
#define EVENT_SELLING
Definition: plugin.h:79
int pay_for_amount(uint64_t to_pay, object *pl)
Definition: shop.c:509
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:206
#define MSG_TYPE_SHOP
Definition: newclient.h:378
#define UPD_WEIGHT
Definition: newclient.h:291
unsigned __int64 uint64_t
Definition: win32.h:167
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
void change_exp(object *op, int64_t exp, const char *skill_name, int flag)
Definition: living.c:2100
#define FLAG_BEEN_APPLIED
Definition: define.h:324
#define MAX_BUF
Definition: define.h:35
int16_t x
Definition: object.h:326
int shop_pay_unpaid(object *pl, object *op)
Definition: shop.c:874
signed __int64 int64_t
Definition: win32.h:168
int identifyskill2
Definition: define.h:94
#define FOR_MAP_FINISH()
Definition: define.h:765
#define FLAG_KNOWN_CURSED
Definition: define.h:321
int need_identify(const object *op)
Definition: item.c:1332
#define FLAG_CURSED
Definition: define.h:317
unsigned int uint32_t
Definition: win32.h:162
Definition: object.h:107
static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay)
Definition: shop.c:691
static float shop_buy_multiplier(int charisma)
Definition: shop.c:167
const char * custom_name
Definition: object.h:432
tag_t count
Definition: object.h:299
const char * name_pl
Definition: map.h:307
living stats
Definition: object.h:368
struct archt * arch
Definition: object.h:412
#define LARGEST_COIN_GIVEN
Definition: shop.c:54
void stringbuffer_delete(StringBuffer *sb)
Definition: stringbuffer.c:71
uint64_t shopmax
Definition: map.h:360
char * cost_str(uint64_t cost)
Definition: shop.c:413
uint8_t type
Definition: object.h:338
struct Settings settings
Definition: init.c:40
#define SK_EXP_NONE
Definition: skills.h:81
Definition: map.h:305
#define FLAG_APPLIED
Definition: define.h:235
void sell_item(object *op, object *pl)
Definition: shop.c:940
object * object_merge(object *op, object *top)
Definition: object.c:1869
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:364
#define UPD_NAME
Definition: newclient.h:293
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:785
#define SPECIALISATION_EFFECT
Definition: shop.c:39
object * identify(object *op)
Definition: item.c:1438
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.c:104
static float shop_bargain_multiplier(int lev_bargain)
Definition: shop.c:186
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:838
struct obj * inv
Definition: object.h:290
#define NDI_UNIQUE
Definition: newclient.h:245
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
#define FLAG_WAS_WIZ
Definition: define.h:234
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:758
struct shopitem * shopitems
Definition: map.h:356
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:626
char * shoprace
Definition: map.h:357
object * find_skill_by_number(object *who, int skillno)
Definition: main.c:351
Definition: map.h:325
Definition: object.h:167
#define NEUTRAL_RATIO
Definition: shop.c:45
int shop_describe(const object *op)
Definition: shop.c:1156
int16_t level
Definition: object.h:351
void fix_object(object *op)
Definition: living.c:1118
int32_t value
Definition: object.h:350
int8_t magic
Definition: object.h:348
const char * name
Definition: object.h:466
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:705
void object_remove(object *op)
Definition: object.c:1654
int32_t food
Definition: living.h:47