Crossfire Server, Trunk
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 
48 #define MAX_BUY_REDUCTION 0.1f
49 
50 #define MAX_SELL_EXTRA 0.1f
51 
52 static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay);
53 static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop);
54 static double shop_specialisation_ratio(const object *item, const mapstruct *map);
55 static double shop_greed(const mapstruct *map);
56 
57 #define NUM_COINS 5
59 #define LARGEST_COIN_GIVEN 2
62 static const char *const coins[] = {
63  "ambercoin",
64  "jadecoin",
65  "platinacoin",
66  "goldcoin",
67  "silvercoin",
68  NULL
69 };
70 
75 uint64_t price_base(const object *obj) {
76  // When there are zero objects, there is really one.
77  const int number = NROF(obj);
78  const bool identified = is_identified(obj);
79  uint64_t val = (uint64_t)obj->value * number;
80 
81  // Objects with price adjustments skip the rest of the calculations.
82  const char *key = object_get_value(obj, "price_adjustment");
83  if (key != NULL) {
84  float ratio = atof(key);
85  return val * ratio;
86  }
87 
88  // Money and gems have fixed prices at shops.
89  if (obj->type == MONEY || obj->type == GEM) {
90  return val;
91  }
92 
93  // If unidentified, price item based on its archetype.
94  if (!identified && obj->arch) {
95  val = obj->arch->clone.value * number;
96  }
97 
103  if (QUERY_FLAG(obj, FLAG_BLESSED)){
104  val *= 1.15;
105  } else if (QUERY_FLAG(obj, FLAG_CURSED)) {
106  val *= 0.8;
107  } else if (QUERY_FLAG(obj, FLAG_DAMNED)) {
108  val *= 0.6;
109  }
110 
111  // If an item is identified to have an enchantment above its archetype
112  // enchantment, increase price exponentially.
113  if (obj->arch != NULL && identified) {
114  int diff = obj->magic - obj->arch->clone.magic;
115  val *= pow(1.15, diff);
116  }
117 
118  // FIXME: Is the 'baseline' 50 charges per wand?
119  if (obj->type == WAND) {
120  val *= obj->stats.food / 50.0;
121  }
122 
123  /* we need to multiply these by 4.0 to keep buy costs roughly the same
124  * (otherwise, you could buy a potion of charisma for around 400 pp.
125  * Arguable, the costs in the archetypes should be updated to better
126  * reflect values (potion charisma list for 1250 gold)
127  */
128  val *= 4; // FIXME
129 
130  return val;
131 }
132 
133 uint64_t price_approx(const object *tmp, object *who) {
134  uint64_t val = price_base(tmp);
135 
136  /* If we are approximating, then the value returned should
137  * be allowed to be wrong however merely using a random number
138  * each time will not be sufficient, as then multiple examinations
139  * would give different answers, so we'll use the count instead.
140  * By taking the sine of the count, a value between -1 and 1 is
141  * generated, we then divide by the square root of the bargaining
142  * skill and the appropriate identification skills, so that higher
143  * level players get better estimates. (We need a +1 there to
144  * prevent dividing by zero.)
145  */
146  const typedata *tmptype = get_typedata(tmp->type);
147  int lev_identify = 0;
148 
149  if (tmptype) {
150  int idskill1 = tmptype->identifyskill;
151  if (idskill1) {
152  int idskill2 = tmptype->identifyskill2;
153  if (find_skill_by_number(who, idskill1)) {
154  lev_identify = find_skill_by_number(who, idskill1)->level;
155  }
156  if (idskill2 && find_skill_by_number(who, idskill2)) {
157  lev_identify += find_skill_by_number(who, idskill2)->level;
158  }
159  }
160  } else {
161  LOG(llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->name);
162  }
163  val += val * (sin(tmp->count) / sqrt(lev_identify * 3 + 1.0));
164 
165  return val;
166 }
167 
174 static float shop_buy_multiplier(int charisma) {
175  float multiplier = 1 / (0.38 * pow(1.06, charisma));
176 
177  if (multiplier > 2) {
178  return 2;
179  } else if (multiplier < 0.5) {
180  return 0.5;
181  } else {
182  return multiplier;
183  }
184 }
185 
186 uint64_t shop_price_buy(const object *tmp, object *who) {
187  assert(who != NULL && who->type == PLAYER);
188  uint64_t val = price_base(tmp);
189 
190  const char *key = object_get_value(tmp, "price_adjustment_buy");
191  if (key != NULL) {
192  float ratio = atof(key);
193  return val * ratio;
194  }
195 
196  if (tmp->type == GEM) {
197  return 1.03 * val;
198  }
199 
200  // Further reduce the sell price of cursed and damned items.
202  val *= 0.8;
203  }
204 
205  float multiplier = shop_buy_multiplier(who->stats.Cha);
206 
207  // Limit buy price multiplier to 0.5, no matter what.
208  val *= multiplier > 0.5 ? multiplier : 0.5;
209 
210  /*
211  * When buying, if the item was sold by another player, it is
212  * ok to let the item be sold cheaper, according to the
213  * specialisation of the shop. If a player sold an item here,
214  * then his sale price was multiplied by the specialisation
215  * ratio, to do the same to the buy price will not generate
216  * extra money. However, the same is not true of generated
217  * items, these have to /divide/ by the specialisation, so
218  * that the price is never less than what they could
219  * be sold for (otherwise players could camp map resets to
220  * make money).
221  * In game terms, a non-specialist shop might not recognise
222  * the true value of the items it sells (much like how people
223  * sometimes find antiques in a junk shop in real life).
224  */
226  val = (int64_t)val*shop_greed(who->map)
228  /shop_approval(who->map, who);
229  } else {
230  val = (int64_t)val*shop_greed(who->map)
232  *shop_approval(who->map, who));
233  }
234 
235  return val;
236 }
237 
238 uint64_t shop_price_sell(const object *tmp, object *who) {
239  assert(who != NULL && who->type == PLAYER);
240  uint64_t val = price_base(tmp);
241 
242  const char *key = object_get_value(tmp, "price_adjustment_sell");
243  if (key != NULL) {
244  float ratio = atof(key);
245  return val * ratio;
246  }
247 
248  if (tmp->type == GEM) {
249  return 0.97 * val;
250  }
251 
252  // Shops value unidentified items less.
253  if (!is_identified(tmp)) {
254  if (tmp->arch != NULL) {
255  // Unidentified standard objects are only worth a little less.
257  val *= 0.85;
258  } else {
259  val *= 0.70;
260  }
261  } else {
262  // No archetype, so probably an artifact.
263  val /= 2;
264  }
265  }
266 
267  // Selling to shops yields roughly 50% of the base price.
268  val /= 2;
269 
270  /* Limit amount of money you can get for really great items. */
271  int number = NROF(tmp);
272  val = value_limit(val, number, who, 1);
273 
274  val = (int64_t)val*shop_specialisation_ratio(tmp, who->map)*
275  shop_approval(who->map, who)/shop_greed(who->map);
276  return val;
277 }
278 
290 static archetype *find_next_coin(uint64_t c, int *cointype) {
291  archetype *coin;
292 
293  do {
294  if (coins[*cointype] == NULL)
295  return NULL;
296  coin = find_archetype(coins[*cointype]);
297  if (coin == NULL)
298  return NULL;
299  *cointype += 1;
300  } while (coin->clone.value > (int64_t) c);
301 
302  return coin;
303 }
304 
322 char* cost_string_from_value(uint64_t cost, int largest_coin) {
323  archetype *coin, *next_coin;
324  uint32_t num;
325  int cointype = largest_coin;
326 
327  if (cointype < 0)
328  cointype = 0;
329  else if (cointype >= NUM_COINS)
330  cointype = NUM_COINS - 1;
331 
333  coin = find_next_coin(cost, &cointype);
334  if (coin == NULL) {
335  stringbuffer_append_string(buf, "nothing");
336  goto done;
337  }
338 
339  num = cost/coin->clone.value;
340  /* so long as nrof is 32 bit, this is true.
341  * If it takes more coins than a person can possibly carry, this
342  * is basically true.
343  */
344  if ((cost/coin->clone.value) > UINT32_MAX) {
345  stringbuffer_append_string(buf, "an unimaginable sum of money.");
346  goto done;
347  }
348 
349  cost -= (uint64_t)num*(uint64_t)coin->clone.value;
350  if (num == 1)
351  stringbuffer_append_printf(buf, "1 %s", coin->clone.name);
352  else
353  stringbuffer_append_printf(buf, "%u %ss", num, coin->clone.name);
354 
355  next_coin = find_next_coin(cost, &cointype);
356  if (next_coin == NULL)
357  goto done;
358 
359  do {
360  coin = next_coin;
361  num = cost/coin->clone.value;
362  cost -= (uint64_t)num*(uint64_t)coin->clone.value;
363 
364  if (cost == 0)
365  next_coin = NULL;
366  else
367  next_coin = find_next_coin(cost, &cointype);
368 
369  if (next_coin) {
370  /* There will be at least one more string to add to the list,
371  * use a comma.
372  */
374  } else {
376  }
377  if (num == 1)
378  stringbuffer_append_printf(buf, "1 %s", coin->clone.name);
379  else
380  stringbuffer_append_printf(buf, "%u %ss", num, coin->clone.name);
381  } while (next_coin);
382 
383 done:
384  return stringbuffer_finish(buf);
385 }
386 
397 static StringBuffer *real_money_value(const object *coin, StringBuffer *buf) {
398  assert(coin->type == MONEY);
399  assert(buf);
400 
401  stringbuffer_append_printf(buf, "%ld %s", (long)coin->nrof, coin->nrof == 1 ? coin->name : coin->name_pl);
402  return buf;
403 }
404 
405 char *cost_str(uint64_t cost) {
407 }
408 
409 char *cost_approx_str(const object *tmp, object *who) {
410  uint64_t approx_val = price_approx(tmp, who);
411  int idskill1 = 0;
412  int idskill2 = 0;
413  const typedata *tmptype;
414 
416 
417  /* money it's pretty hard to not give the exact price, so skip all logic and just return the real value. */
418  if (tmp->type == MONEY) {
420  }
421 
422  tmptype = get_typedata(tmp->type);
423  if (tmptype) {
424  idskill1 = tmptype->identifyskill;
425  idskill2 = tmptype->identifyskill2;
426  }
427 
428  /* we show an approximate price if
429  * 1) we are approximating
430  * 2) there either is no id skill(s) for the item, or we don't have them
431  * 3) we don't have bargaining skill either
432  */
433  if (!idskill1 || !find_skill_by_number(who, idskill1)) {
434  if (!idskill2 || !find_skill_by_number(who, idskill2)) {
436  int num;
438  archetype *coin = find_next_coin(approx_val, &cointype);
439 
440  if (coin == NULL) {
441  stringbuffer_append_string(buf, "nothing");
442  return stringbuffer_finish(buf);
443  }
444 
445  num = approx_val/coin->clone.value;
446  if (num == 1)
447  stringbuffer_append_printf(buf, "about one %s", coin->clone.name);
448  else if (num < 5)
449  stringbuffer_append_printf(buf, "a few %s", coin->clone.name_pl);
450  else if (num < 10)
451  stringbuffer_append_printf(buf, "several %s", coin->clone.name_pl);
452  else if (num < 25)
453  stringbuffer_append_printf(buf, "a moderate amount of %s", coin->clone.name_pl);
454  else if (num < 100)
455  stringbuffer_append_printf(buf, "lots of %s", coin->clone.name_pl);
456  else if (num < 1000)
457  stringbuffer_append_printf(buf, "a great many %s", coin->clone.name_pl);
458  else
459  stringbuffer_append_printf(buf, "a vast quantity of %s", coin->clone.name_pl);
460  return stringbuffer_finish(buf);
461  }
462  }
463  }
464 
465  // If we get here, return the price we guessed.
467  return cost_str(approx_val);
468 }
469 
470 uint64_t query_money(const object *op) {
471  uint64_t total = 0;
472 
473  if (op->type != PLAYER && op->type != CONTAINER) {
474  LOG(llevError, "Query money called with non player/container\n");
475  return 0;
476  }
478  if (tmp->type == MONEY) {
479  total += (uint64_t)tmp->nrof*(uint64_t)tmp->value;
480  } else if (tmp->type == CONTAINER
482  && (tmp->race == NULL || strstr(tmp->race, "gold"))) {
483  total += query_money(tmp);
484  }
485  } FOR_INV_FINISH();
486  return total;
487 }
488 
501 int pay_for_amount(uint64_t to_pay, object *pl) {
502  if (to_pay == 0)
503  return 1;
504  if (to_pay > query_money(pl))
505  return 0;
506 
507  to_pay = pay_from_container(pl, pl, to_pay);
508 
509  FOR_INV_PREPARE(pl, pouch) {
510  if (to_pay <= 0)
511  break;
512  if (pouch->type == CONTAINER
513  && QUERY_FLAG(pouch, FLAG_APPLIED)
514  && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
515  to_pay = pay_from_container(pl, pouch, to_pay);
516  }
517  } FOR_INV_FINISH();
518  if (to_pay > 0) {
519  LOG(llevError, "pay_for_amount: Cannot remove enough money -- %"FMT64U" remains\n", to_pay);
520  }
521 
522  fix_object(pl);
523  return 1;
524 }
525 
541 int pay_for_item(object *op, object *pl, uint64_t reduction) {
542  uint64_t to_pay = shop_price_buy(op, pl);
543  assert(to_pay >= reduction);
544  to_pay -= reduction;
545 
546  if (to_pay == 0)
547  return 1;
548  if (to_pay > query_money(pl))
549  return 0;
550 
551  to_pay = pay_from_container(pl, pl, to_pay);
552 
553  FOR_INV_PREPARE(pl, pouch) {
554  if (to_pay <= 0)
555  break;
556  if (pouch->type == CONTAINER
557  && QUERY_FLAG(pouch, FLAG_APPLIED)
558  && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
559  to_pay = pay_from_container(pl, pouch, to_pay);
560  }
561  } FOR_INV_FINISH();
562  if (to_pay > 0) {
563  LOG(llevError, "pay_for_item: Cannot remove enough money -- %"FMT64U" remains\n", to_pay);
564  }
567  fix_object(pl);
568  return 1;
569 }
570 
582 static int64_t remove_value(object *coin_objs[], int64_t remain) {
583  int i;
584 
585  for (i = 0; i < NUM_COINS; i++) {
586  int count;
587  int64_t num_coins;
588 
589  if ((int64_t)coin_objs[i]->nrof * coin_objs[i]->value > remain) {
590  num_coins = remain/coin_objs[i]->value;
591  if ((uint64_t)num_coins*(uint64_t)coin_objs[i]->value < (uint64_t) remain) {
592  num_coins++;
593  }
594  } else {
595  num_coins = coin_objs[i]->nrof;
596  }
597  remain -= (int64_t)num_coins*(int64_t)coin_objs[i]->value;
598  coin_objs[i]->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  while (remain < 0 && count >= 0) {
605  num_coins = -remain/coin_objs[count]->value;
606  coin_objs[count]->nrof += num_coins;
607  remain += num_coins*coin_objs[count]->value;
608  count--;
609  }
610  }
611 
612  return remain;
613 }
614 
623 static void add_value(object *coin_objs[], int64_t value) {
624  int i;
625 
626  for (i = NUM_COINS-LARGEST_COIN_GIVEN-1; i >= 0; i--) {
627  uint32_t nrof;
628 
629  nrof = (uint32_t)(value/coin_objs[i]->value);
630  value -= nrof*coin_objs[i]->value;
631  coin_objs[i]->nrof += nrof;
632  }
633 }
634 
647 static void insert_objects(object *pl, object *container, object *objects[], int objects_len) {
648  int i, one = 0;
649 
650  for (i = 0; i < objects_len; i++) {
651  if (objects[i]->nrof > 0) {
652  object_insert_in_ob(objects[i], container);
653  one = 1;
654  } else {
656  }
657  }
658  if (one)
659  esrv_update_item(UPD_WEIGHT, pl, container);
660 }
661 
675 static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay) {
676  size_t i;
677  int64_t remain;
678  object *coin_objs[NUM_COINS];
679  object *other_money[16]; /* collects MONEY objects not matching coins[] */
680  size_t other_money_len; /* number of allocated entries in other_money[] */
681  archetype *at;
682 
683  if (pouch->type != PLAYER && pouch->type != CONTAINER)
684  return to_pay;
685 
686  remain = to_pay;
687  for (i = 0; i < NUM_COINS; i++)
688  coin_objs[i] = NULL;
689 
690  /* This hunk should remove all the money objects from the player/container */
691  other_money_len = 0;
692  FOR_INV_PREPARE(pouch, tmp) {
693  if (tmp->type == MONEY) {
694  for (i = 0; i < NUM_COINS; i++) {
695  if (!strcmp(coins[NUM_COINS-1-i], tmp->arch->name)
696  && (tmp->value == tmp->arch->clone.value)) {
697  /* This should not happen, but if it does, just
698  * merge the two.
699  */
700  if (coin_objs[i] != NULL) {
701  LOG(llevError, "%s has two money entries of (%s)\n", pouch->name, coins[NUM_COINS-1-i]);
703  coin_objs[i]->nrof += tmp->nrof;
705  } else {
707  coin_objs[i] = tmp;
708  }
709  break;
710  }
711  }
712  if (i == NUM_COINS) {
713  if (other_money_len >= sizeof(other_money)/sizeof(*other_money)) {
714  LOG(llevError, "pay_for_item: Cannot store non-standard money object %s\n", tmp->arch->name);
715  } else {
717  other_money[other_money_len++] = tmp;
718  }
719  }
720  }
721  } FOR_INV_FINISH();
722 
723  /* Fill in any gaps in the coin_objs array - needed to make change. */
724  /* Note that the coin_objs array goes from least value to greatest value */
725  for (i = 0; i < NUM_COINS; i++)
726  if (coin_objs[i] == NULL) {
727  at = find_archetype(coins[NUM_COINS-1-i]);
728  if (at == NULL) {
729  continue;
730  }
731  coin_objs[i] = object_new();
732  object_copy(&at->clone, coin_objs[i]);
733  coin_objs[i]->nrof = 0;
734  }
735 
736  /* Try to pay from standard coins first. */
737  remain = remove_value(coin_objs, remain);
738 
739  /* Now pay from non-standard coins until all is paid. */
740  for (i = 0; i < other_money_len && remain > 0; i++) {
741  uint32_t nrof;
742  object *coin;
743 
744  coin = other_money[i];
745 
746  /* Find the minimal number of coins to use. This prevents converting
747  * excess non-standard coins to standard money.
748  */
749  nrof = (remain+coin->value-1)/coin->value;
750  if (nrof > coin->nrof) {
751  nrof = coin->nrof;
752  }
753  coin->nrof -= nrof;
754  add_value(coin_objs, nrof*coin->value);
755 
756  remain = remove_value(coin_objs, remain);
757  }
758 
759  /* re-insert remaining coins into player */
760  insert_objects(pl, pouch, coin_objs, NUM_COINS);
761  insert_objects(pl, pouch, other_money, other_money_len);
762 
763  return(remain);
764 }
765 
780 static void count_unpaid(object *pl, object *item, int *unpaid_count, uint64_t *unpaid_price, uint32_t *coincount) {
781  int i;
782 
784  if (QUERY_FLAG(item, FLAG_UNPAID)) {
785  (*unpaid_count)++;
786  (*unpaid_price) += shop_price_buy(item, pl);
787  }
788  /* Merely converting the player's monetary wealth won't do.
789  * If we did that, we could print the wrong numbers for the
790  * coins, so we count the money instead.
791  */
792  for (i = 0; i < NUM_COINS; i++)
793  if (!strcmp(coins[i], item->arch->name)) {
794  coincount[i] += item->nrof;
795  break;
796  }
797  if (item->inv)
798  count_unpaid(pl, item->inv, unpaid_count, unpaid_price, coincount);
800 }
801 
810 static uint64_t compute_price_variation_with_bargaining(object *pl, uint64_t price, float max_variation) {
811  object *skill = find_skill_by_number(pl, SK_BARGAINING);
812  if (skill && skill->level > 0) {
813  return rndm(0, price * (max_variation * skill->level / settings.max_level));
814  }
815  return 0;
816 }
817 
830 int can_pay(object *pl) {
831  int unpaid_count = 0, i;
832  uint64_t unpaid_price = 0;
833  uint32_t coincount[NUM_COINS];
834 
835  if (!pl || pl->type != PLAYER) {
836  LOG(llevError, "can_pay(): called against something that isn't a player\n");
837  return 0;
838  }
839  uint64_t player_wealth = query_money(pl);
840 
841  for (i = 0; i < NUM_COINS; i++)
842  coincount[i] = 0;
843 
844  count_unpaid(pl, pl->inv, &unpaid_count, &unpaid_price, coincount);
845 
846  if (unpaid_price > player_wealth) {
847  char buf[MAX_BUF], coinbuf[MAX_BUF];
848  int denominations = 0;
849  char *value = cost_str(unpaid_price);
850 
851  snprintf(buf, sizeof(buf), "You have %d unpaid items that would cost you %s, ", unpaid_count, value);
852  free(value);
853  for (i = 0; i < NUM_COINS; i++) {
854  if (coincount[i] > 0 && coins[i]) {
855  if (denominations == 0)
856  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "but you only have");
857  denominations++;
859  if (arch != NULL)
860  {
861  snprintf(coinbuf, sizeof(coinbuf), " %u %s,", coincount[i], arch->clone.name_pl);
862  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", coinbuf);
863  }
864  }
865  }
866  if (denominations == 0)
867  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "but you don't have any money.");
868  else if (denominations > 1)
872  return 0;
873  } else
874  return 1;
875 }
876 
883 int shop_pay_unpaid(object *pl, object *op) {
884  char name_op[MAX_BUF];
885 
886  if (!op) {
887  return 1;
888  }
889 
890  if (op->inv)
891  if (!shop_pay_unpaid(pl, op->inv))
892  return 0;
893 
894  while (op) {
895  object *below = op->below;
896 
897  if (QUERY_FLAG(op, FLAG_UNPAID)) {
898  uint64_t price = shop_price_buy(op, pl);
900  if (!pay_for_item(op, pl, reduction)) {
901  uint64_t i = price - query_money(pl);
902  char *missing = cost_str(i);
903 
905  query_name(op, name_op, MAX_BUF);
908  "You lack %s to buy %s.",
909  missing, name_op);
910  free(missing);
912  return 0;
913  } else {
914  // TODO: Figure out how to pass in the shop owner for player shops.
916  return 0;
917  object *tmp;
918  char *value = cost_str(price - reduction);
919 
922  query_name(op, name_op, MAX_BUF);
923 
924  if (reduction > 0) {
925  char *reduction_str = cost_str(reduction);
928  "You paid %s for %s after bargaining a reduction of %s.",
929  value, name_op, reduction_str);
930  change_exp(pl, reduction, "bargaining", SK_EXP_NONE);
931  free(reduction_str);
932  } else {
935  "You paid %s for %s.",
936  value, name_op);
937  }
938  free(value);
939  tmp = object_merge(op, NULL);
940  if (pl->type == PLAYER && !tmp) {
941  /* If item wasn't merged we update it. If merged, object_merge() handled everything for us. */
943  }
944  }
945  }
946 
947  op = below;
948  }
949  return 1;
950 }
951 
963 void sell_item(object *op, object *pl) {
964  object *tmp;
965  archetype *at;
966  char obj_name[MAX_BUF];
967 
968  query_name(op, obj_name, MAX_BUF);
969 
970  if (pl == NULL || pl->type != PLAYER) {
971  LOG(llevDebug, "Object other than player tried to sell something.\n");
972  return;
973  }
974 
976  return;
977 
978  if (op->custom_name)
979  FREE_AND_CLEAR_STR(op->custom_name);
980 
981  uint64_t price = shop_price_sell(op, pl);
982  if (price == 0) {
985  "We're not interested in %s.",
986  obj_name);
987  return;
988  }
989 
991  char *value_str = cost_str(price + extra_gain);
992 
993  if (extra_gain > 0) {
994  change_exp(pl, extra_gain, "bargaining", SK_EXP_NONE);
995  char *extra_str = cost_str(extra_gain);
997  "You receive %s for %s, after bargaining for %s more than proposed.", value_str, obj_name, extra_str);
998  free(extra_str);
999  price += extra_gain;
1000  } else {
1002  "You receive %s for %s.", value_str, obj_name);
1003  }
1004  free(value_str);
1005 
1006  for (int count = LARGEST_COIN_GIVEN; coins[count] != NULL; count++) {
1007  at = find_archetype(coins[count]);
1008  if (at == NULL)
1009  LOG(llevError, "Could not find %s archetype\n", coins[count]);
1010  else if ((price/at->clone.value) > 0) {
1011  FOR_INV_PREPARE(pl, pouch) {
1012  if (pouch->type == CONTAINER
1013  && QUERY_FLAG(pouch, FLAG_APPLIED)
1014  && pouch->race
1015  && strstr(pouch->race, "gold")) {
1016  int w = at->clone.weight*(100-pouch->stats.Str)/100;
1017  int n = price/at->clone.value;
1018 
1019  if (w == 0)
1020  w = 1; /* Prevent divide by zero */
1021  if (n > 0
1022  && (!pouch->weight_limit || pouch->carrying+w <= pouch->weight_limit)) {
1023  if (pouch->weight_limit
1024  && (pouch->weight_limit-pouch->carrying)/w < n)
1025  n = (pouch->weight_limit-pouch->carrying)/w;
1026 
1027  tmp = object_new();
1028  object_copy(&at->clone, tmp);
1029  tmp->nrof = n;
1030  price -= (uint64_t)tmp->nrof*(uint64_t)tmp->value;
1031  tmp = object_insert_in_ob(tmp, pouch);
1033  }
1034  }
1035  } FOR_INV_FINISH();
1036  if (price/at->clone.value > 0) {
1037  tmp = object_new();
1038  object_copy(&at->clone, tmp);
1039  tmp->nrof = price/tmp->value;
1040  price -= (uint64_t)tmp->nrof*(uint64_t)tmp->value;
1043  }
1044  }
1045  }
1046 
1047  if (price != 0) {
1048  LOG(llevError, "Warning - payment not zero: %" PRIo64 "\n", price);
1049  }
1050 
1052  identify(op);
1053 }
1054 
1068 static double shop_specialisation_ratio(const object *item, const mapstruct *map) {
1069  shopitems *items = map->shopitems;
1070  double ratio = SPECIALISATION_EFFECT, likedness = 0.001;
1071  int i;
1072 
1073  if (item == NULL) {
1074  LOG(llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path);
1075  return 0;
1076  }
1077  if (item->type == (uint8_t)-1) {
1078  LOG(llevError, "shop_specialisation_ratio: passed an item with an invalid type\n");
1079  /*
1080  * I'm not really sure what the /right/ thing to do here is,
1081  * these types of item shouldn't exist anyway, but returning
1082  * the ratio is probably the best bet.."
1083  */
1084  return ratio;
1085  }
1086  if (map->shopitems) {
1087  for (i = 0; i < items[0].index; i++)
1088  if (items[i].typenum == item->type || (items[i].typenum == -1 && likedness == 0.001))
1089  likedness = items[i].strength/100.0;
1090  }
1091  if (likedness > 1.0) { /* someone has been rather silly with the map headers. */
1092  LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, map->path);
1093  likedness = 1.0;
1094  }
1095  if (likedness < -1.0) {
1096  LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, map->path);
1097  likedness = -1.0;
1098  }
1099  ratio = ratio+(1.0-ratio)*likedness;
1100  if (ratio <= 0.1)
1101  ratio = 0.1; /* if the ratio were much lower than this, we would get silly prices */
1102  return ratio;
1103 }
1104 
1113 static double shop_greed(const mapstruct *map) {
1114  double greed = 1.0;
1115 
1116  if (map->shopgreed)
1117  return map->shopgreed;
1118  return greed;
1119 }
1120 
1121 double shop_approval(const mapstruct *map, const object *player) {
1122  double approval = 1.0;
1123 
1124  if (map->shoprace) {
1125  approval = NEUTRAL_RATIO;
1126  if (player->race && !strcmp(player->race, map->shoprace))
1127  approval = 1.0;
1128  }
1129  return approval;
1130 }
1131 
1151 static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop) {
1152  uint64_t newval, unit_price;
1153  mapstruct *map;
1154 
1155  unit_price = val/quantity;
1156  if (!isshop || !who) {
1157  if (unit_price > 10000)
1158  newval = 8000+isqrt(unit_price)*20;
1159  else
1160  newval = unit_price;
1161  } else {
1162  if (!who->map) {
1163  LOG(llevError, "value_limit: asked shop price for ob %s on NULL map\n", who->name);
1164  return val;
1165  }
1166  map = who->map;
1167  if (map->shopmin && unit_price < map->shopmin)
1168  return 0;
1169  else if (map->shopmax && unit_price > map->shopmax/2)
1170  newval = MIN((map->shopmax/2)+isqrt(unit_price-map->shopmax/2), map->shopmax);
1171  else if (unit_price > 10000)
1172  newval = 8000+isqrt(unit_price)*20;
1173  else
1174  newval = unit_price;
1175  }
1176  newval *= quantity;
1177  return newval;
1178 }
1179 
1185 int shop_describe(const object *op) {
1186  mapstruct *map = op->map;
1187  /*shopitems *items=map->shopitems;*/
1188  int pos = 0, i;
1189  double opinion = 0;
1190  char tmp[MAX_BUF] = "\0", *value;
1191 
1192  if (op->type != PLAYER)
1193  return 0;
1194 
1195  /*check if there is a shop specified for this map */
1196  if (map->shopitems
1197  || map->shopgreed
1198  || map->shoprace
1199  || map->shopmin
1200  || map->shopmax) {
1202  "From looking at the nearby shop you determine that it trades in:");
1203 
1204  if (map->shopitems) {
1205  for (i = 0; i < map->shopitems[0].index; i++) {
1206  if (map->shopitems[i].name && map->shopitems[i].strength > 10) {
1207  snprintf(tmp+pos, sizeof(tmp)-pos, "%s, ", map->shopitems[i].name_pl);
1208  pos += strlen(tmp+pos);
1209  }
1210  }
1211  }
1212  if (!pos)
1213  strcpy(tmp, "a little of everything.");
1214 
1215  /* format the string into a list */
1219 
1220  if (map->shopmax) {
1221  value = cost_str(map->shopmax);
1224  "It won't trade for items above %s.",
1225  value);
1226  free(value);
1227  }
1228 
1229  if (map->shopmin) {
1230  value = cost_str(map->shopmin);
1233  "It won't trade in items worth less than %s.",
1234  value);
1235  free(value);
1236  }
1237 
1238  if (map->shopgreed) {
1239  if (map->shopgreed > 2.0)
1242  "It tends to overcharge massively.");
1243  else if (map->shopgreed > 1.5)
1246  "It tends to overcharge substantially.");
1247  else if (map->shopgreed > 1.1)
1250  "It tends to overcharge slightly.");
1251  else if (map->shopgreed < 0.9)
1254  "It tends to undercharge.");
1255  }
1256  if (map->shoprace) {
1257  opinion = shop_approval(map, op);
1258  if (opinion > 0.8)
1261  "You think the shopkeeper likes you.");
1262  else if (opinion > 0.5)
1265  "The shopkeeper seems unconcerned by you.");
1266  else
1269  "The shopkeeper seems to have taken a dislike to you.");
1270  }
1272  "There is no shop nearby.");
1273 
1274  return 1;
1275 }
1276 
1280 static bool coords_in_shop(mapstruct *map, int x, int y) {
1281  FOR_MAP_PREPARE(map, x, y, floor)
1282  if (floor->type == SHOP_FLOOR) return true;
1283  FOR_MAP_FINISH();
1284  return false;
1285 }
1286 
1287 bool shop_contains(object *ob) {
1288  if (!ob->map) return 0;
1289  return coords_in_shop(ob->map, ob->x, ob->y);
1290 }
PLAYER
@ PLAYER
Definition: object.h:107
value_limit
static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop)
Definition: shop.c:1151
global.h
diamondslots.cointype
string cointype
Definition: diamondslots.py:18
shop_contains
bool shop_contains(object *ob)
Definition: shop.c:1287
object_remove
void object_remove(object *op)
Definition: object.c:1806
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Definition: define.h:728
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
Settings::max_level
int16_t max_level
Definition: global.h:298
llevError
@ llevError
Definition: logger.h:11
count_unpaid
static void count_unpaid(object *pl, object *item, int *unpaid_count, uint64_t *unpaid_price, uint32_t *coincount)
Definition: shop.c:780
find_skill_by_number
object * find_skill_by_number(object *who, int skillno)
Definition: main.c:376
WAND
@ WAND
Definition: object.h:220
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
diamondslots.x
x
Definition: diamondslots.py:15
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
SPECIALISATION_EFFECT
#define SPECIALISATION_EFFECT
Definition: shop.c:39
shop_greed
static double shop_greed(const mapstruct *map)
Definition: shop.c:1113
obj::value
int32_t value
Definition: object.h:353
FALSE
#define FALSE
Definition: compat.h:14
object_new
object * object_new(void)
Definition: object.c:1242
c
static event_registration c
Definition: citylife.cpp:425
UPD_WEIGHT
#define UPD_WEIGHT
Definition: newclient.h:316
objects
object * objects
Definition: object.c:283
SHOP_FLOOR
@ SHOP_FLOOR
Definition: object.h:183
GEM
@ GEM
Definition: object.h:167
pl
Definition: player.h:92
can_pay
int can_pay(object *pl)
Definition: shop.c:830
price_approx
uint64_t price_approx(const object *tmp, object *who)
Definition: shop.c:133
typedata::identifyskill
int identifyskill
Definition: define.h:93
guildjoin.ob
ob
Definition: guildjoin.py:42
shop_describe
int shop_describe(const object *op)
Definition: shop.c:1185
mail_login.total
total
Definition: mail_login.py:30
add_value
static void add_value(object *coin_objs[], int64_t value)
Definition: shop.c:623
MIN
#define MIN(x, y)
Definition: compat.h:21
cost_approx_str
char * cost_approx_str(const object *tmp, object *who)
Definition: shop.c:409
NEUTRAL_RATIO
#define NEUTRAL_RATIO
Definition: shop.c:45
shopitem::typenum
int typenum
Definition: map.h:307
Ice.tmp
int tmp
Definition: Ice.py:207
FLAG_BLESSED
#define FLAG_BLESSED
Definition: define.h:369
MSG_TYPE_SHOP_PAYMENT
#define MSG_TYPE_SHOP_PAYMENT
Definition: newclient.h:510
done
int done
Definition: readable.c:1607
shop_pay_unpaid
int shop_pay_unpaid(object *pl, object *op)
Definition: shop.c:883
make_list_like
void make_list_like(char *input)
Definition: utils.c:395
MAX_SELL_EXTRA
#define MAX_SELL_EXTRA
Definition: shop.c:50
SK_EXP_NONE
#define SK_EXP_NONE
Definition: skills.h:80
MSG_TYPE_SHOP_MISC
#define MSG_TYPE_SHOP_MISC
Definition: newclient.h:513
FLAG_APPLIED
#define FLAG_APPLIED
Definition: define.h:235
events_execute_object_event
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: events.cpp:259
isqrt
int isqrt(int n)
Definition: utils.c:586
obj::nrof
uint32_t nrof
Definition: object.h:335
archt
Definition: object.h:468
settings
struct Settings settings
Definition: init.c:39
obj
Definition: object.h:275
query_money
uint64_t query_money(const object *op)
Definition: shop.c:470
object_merge
object * object_merge(object *op, object *top)
Definition: object.c:2018
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4354
identify
object * identify(object *op)
Definition: item.c:1408
typedata
Definition: define.h:89
autojail.who
who
Definition: autojail.py:3
find_next_coin
static archetype * find_next_coin(uint64_t c, int *cointype)
Definition: shop.c:290
NROF
static uint32_t NROF(const object *const ob)
Definition: object.h:612
disinfect.map
map
Definition: disinfect.py:4
FLAG_WAS_WIZ
#define FLAG_WAS_WIZ
Definition: define.h:234
obj::name
sstring name
Definition: object.h:312
object_copy
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:1053
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:584
EVENT_SELLING
#define EVENT_SELLING
Definition: events.h:29
shop_specialisation_ratio
static double shop_specialisation_ratio(const object *item, const mapstruct *map)
Definition: shop.c:1068
say.price
dictionary price
Definition: say.py:147
obj::name_pl
sstring name_pl
Definition: object.h:316
FOR_OB_AND_BELOW_FINISH
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:752
stringbuffer_append_string
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Definition: stringbuffer.c:95
CONTAINER
@ CONTAINER
Definition: object.h:231
compute_price_variation_with_bargaining
static uint64_t compute_price_variation_with_bargaining(object *pl, uint64_t price, float max_variation)
Definition: shop.c:810
SCRIPT_FIX_ALL
#define SCRIPT_FIX_ALL
Definition: global.h:367
shop_price_sell
uint64_t shop_price_sell(const object *tmp, object *who)
Definition: shop.c:238
FLAG_DAMNED
#define FLAG_DAMNED
Definition: define.h:317
fix_object
void fix_object(object *op)
Definition: living.c:1126
UPD_FLAGS
#define UPD_FLAGS
Definition: newclient.h:315
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
LARGEST_COIN_GIVEN
#define LARGEST_COIN_GIVEN
Definition: shop.c:59
FOR_INV_FINISH
#define FOR_INV_FINISH()
Definition: define.h:675
rndm
int rndm(int min, int max)
Definition: utils.c:162
disinfect.count
int count
Definition: disinfect.py:7
SK_BARGAINING
@ SK_BARGAINING
Definition: skills.h:28
insert_objects
static void insert_objects(object *pl, object *container, object *objects[], int objects_len)
Definition: shop.c:647
sproto.h
liv::food
int32_t food
Definition: living.h:48
price_base
uint64_t price_base(const object *obj)
Definition: shop.c:75
pay_from_container
static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay)
Definition: shop.c:675
commongive.quantity
quantity
Definition: commongive.py:7
FOR_OB_AND_BELOW_PREPARE
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:748
mapdef
Definition: map.h:324
MSG_TYPE_SHOP
#define MSG_TYPE_SHOP
Definition: newclient.h:403
cost_string_from_value
char * cost_string_from_value(uint64_t cost, int largest_coin)
Definition: shop.c:322
shop_price_buy
uint64_t shop_price_buy(const object *tmp, object *who)
Definition: shop.c:186
pay_for_item
int pay_for_item(object *op, object *pl, uint64_t reduction)
Definition: shop.c:541
MAX_BUF
#define MAX_BUF
Definition: define.h:35
EVENT_BOUGHT
#define EVENT_BOUGHT
Definition: events.h:22
StringBuffer
Definition: stringbuffer.c:25
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:196
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:721
sell_item
void sell_item(object *op, object *pl)
Definition: shop.c:963
obj::arch
struct archt * arch
Definition: object.h:416
obj::type
uint8_t type
Definition: object.h:341
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:262
obj::stats
living stats
Definition: object.h:371
archt::clone
object clone
Definition: object.h:472
real_money_value
static StringBuffer * real_money_value(const object *coin, StringBuffer *buf)
Definition: shop.c:397
MSG_TYPE_SHOP_SELL
#define MSG_TYPE_SHOP_SELL
Definition: newclient.h:512
obj::weight
int32_t weight
Definition: object.h:368
item
Definition: item.py:1
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
give.op
op
Definition: give.py:33
autojail.value
value
Definition: autojail.py:6
NUM_COINS
#define NUM_COINS
Definition: shop.c:57
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:278
shop.h
esrv_update_item
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:360
shopitem::index
int index
Definition: map.h:310
diamondslots.y
y
Definition: diamondslots.py:16
FLAG_BEEN_APPLIED
#define FLAG_BEEN_APPLIED
Definition: define.h:323
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
MSG_TYPE_SHOP_LISTING
#define MSG_TYPE_SHOP_LISTING
Definition: newclient.h:508
buf
StringBuffer * buf
Definition: readable.c:1606
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.c:104
change_exp
void change_exp(object *op, int64_t exp, const char *skill_name, int flag)
Definition: living.c:2168
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2820
get_typedata
const typedata * get_typedata(int itemtype)
Definition: item.c:319
MAX_BUY_REDUCTION
#define MAX_BUY_REDUCTION
Definition: shop.c:48
typedata::identifyskill2
int identifyskill2
Definition: define.h:94
shop_approval
double shop_approval(const mapstruct *map, const object *player)
Definition: shop.c:1121
remove_value
static int64_t remove_value(object *coin_objs[], int64_t remain)
Definition: shop.c:582
castle_read.key
key
Definition: castle_read.py:64
FMT64U
#define FMT64U
Definition: compat.h:17
UPD_NAME
#define UPD_NAME
Definition: newclient.h:318
Settings::real_wiz
uint8_t real_wiz
Definition: global.h:267
draw_ext_info
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:309
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.c:1533
coords_in_shop
static bool coords_in_shop(mapstruct *map, int x, int y)
Definition: shop.c:1280
FLAG_PLAYER_SOLD
#define FLAG_PLAYER_SOLD
Definition: define.h:252
FLAG_UNPAID
#define FLAG_UNPAID
Definition: define.h:236
UINT32_MAX
#define UINT32_MAX
Definition: loader.c:83
diamondslots.cost
int cost
Definition: diamondslots.py:21
stringbuffer_delete
void stringbuffer_delete(StringBuffer *sb)
Definition: stringbuffer.c:71
coins
static const char *const coins[]
Definition: shop.c:62
cost_str
char * cost_str(uint64_t cost)
Definition: shop.c:405
FLAG_CURSED
#define FLAG_CURSED
Definition: define.h:316
shopitem::strength
int8_t strength
Definition: map.h:308
obj::magic
int8_t magic
Definition: object.h:351
if
if(!(yy_init))
Definition: loader.c:2589
shopitem
Definition: map.h:304
FOR_INV_PREPARE
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:668
draw_ext_info_format
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:319
obj::level
int16_t level
Definition: object.h:354
pay_for_amount
int pay_for_amount(uint64_t to_pay, object *pl)
Definition: shop.c:501
llevDebug
@ llevDebug
Definition: logger.h:13
MONEY
@ MONEY
Definition: object.h:137
shop_buy_multiplier
static float shop_buy_multiplier(int charisma)
Definition: shop.c:174
is_identified
int is_identified(const object *op)
Definition: item.c:1335