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 
173 static float shop_cha_modifier(int charisma) {
174  return tanh((charisma+15.0)/20);
175 }
176 
184 float shop_efficiency(const object *player) {
185  return shop_greed(player->map)
186  * shop_approval(player->map, player)
187  * shop_cha_modifier(player->stats.Cha);
188 }
189 
190 uint64_t shop_price_buy(const object *tmp, object *who) {
191  assert(who != NULL && who->type == PLAYER);
192  const uint64_t val = price_base(tmp);
193  const char *key = object_get_value(tmp, "price_adjustment_buy");
194  float adj = 1;
195  if (key != NULL) {
196  adj = atof(key);
197  }
198  float E = shop_efficiency(who);
199  const float adj_val = val * adj / E;
200  if (getenv("CF_DEBUG_SHOP")) {
201  LOG(llevDebug, "price_buy %s %u*adj(%.2f)/E(%.2f) = %u\n",
202  tmp->arch->name, val, adj, E, adj_val);
203  }
204  if (isfinite(adj_val)) {
205  return adj_val;
206  } else {
207  return UINT64_MAX;
208  }
209 }
210 
211 uint64_t shop_price_sell(const object *tmp, object *who) {
212  assert(who != NULL && who->type == PLAYER);
213  const uint64_t val = price_base(tmp);
214  const char *key = object_get_value(tmp, "price_adjustment_sell");
215  float adj = 1;
216  if (key != NULL) {
217  adj = atof(key);
218  }
219  float spec = shop_specialisation_ratio(tmp, who->map);
220  float E = shop_efficiency(who);
221  const uint64_t adj_val = val * adj * spec * E;
222 
223  /* Limit amount of money you can get for really great items. */
224  int number = NROF(tmp);
225  uint64_t limval = value_limit(adj_val, number, who, 1);
226 
227  if (getenv("CF_DEBUG_SHOP")) {
228  LOG(llevDebug, "price_sell %s %u*adj(%.2f)*s(%.2f)*E(%.2f) = %u limited to %u\n",
229  tmp->arch->name, val, adj, spec, E, adj_val, limval);
230  }
231  return limval;
232 }
233 
245 static archetype *find_next_coin(uint64_t c, int *cointype) {
246  archetype *coin;
247 
248  do {
249  if (coins[*cointype] == NULL)
250  return NULL;
251  coin = find_archetype(coins[*cointype]);
252  if (coin == NULL)
253  return NULL;
254  *cointype += 1;
255  } while (coin->clone.value > (int64_t) c);
256 
257  return coin;
258 }
259 
277 char* cost_string_from_value(uint64_t cost, int largest_coin) {
278  archetype *coin, *next_coin;
279  uint32_t num;
280  int cointype = largest_coin;
281 
282  if (cointype < 0)
283  cointype = 0;
284  else if (cointype >= NUM_COINS)
285  cointype = NUM_COINS - 1;
286 
288  if (cost == UINT64_MAX) {
289  stringbuffer_append_string(buf, "an unimaginable sum of money");
290  goto done;
291  }
292 
293  coin = find_next_coin(cost, &cointype);
294  if (coin == NULL) {
295  stringbuffer_append_string(buf, "nothing");
296  goto done;
297  }
298 
299  num = cost/coin->clone.value;
300  /* so long as nrof is 32 bit, this is true.
301  * If it takes more coins than a person can possibly carry, this
302  * is basically true.
303  */
304  if ((cost/coin->clone.value) > UINT32_MAX) {
305  stringbuffer_append_string(buf, "an unimaginable sum of money");
306  goto done;
307  }
308 
309  cost -= (uint64_t)num*(uint64_t)coin->clone.value;
310  if (num == 1)
311  stringbuffer_append_printf(buf, "1 %s", coin->clone.name);
312  else
313  stringbuffer_append_printf(buf, "%u %ss", num, coin->clone.name);
314 
315  next_coin = find_next_coin(cost, &cointype);
316  if (next_coin == NULL)
317  goto done;
318 
319  do {
320  coin = next_coin;
321  num = cost/coin->clone.value;
322  cost -= (uint64_t)num*(uint64_t)coin->clone.value;
323 
324  if (cost == 0)
325  next_coin = NULL;
326  else
327  next_coin = find_next_coin(cost, &cointype);
328 
329  if (next_coin) {
330  /* There will be at least one more string to add to the list,
331  * use a comma.
332  */
334  } else {
336  }
337  if (num == 1)
338  stringbuffer_append_printf(buf, "1 %s", coin->clone.name);
339  else
340  stringbuffer_append_printf(buf, "%u %ss", num, coin->clone.name);
341  } while (next_coin);
342 
343 done:
344  return stringbuffer_finish(buf);
345 }
346 
357 static StringBuffer *real_money_value(const object *coin, StringBuffer *buf) {
358  assert(coin->type == MONEY);
359  assert(buf);
360 
361  stringbuffer_append_printf(buf, "%ld %s", (long)coin->nrof, coin->nrof == 1 ? coin->name : coin->name_pl);
362  return buf;
363 }
364 
365 char *cost_str(uint64_t cost) {
367 }
368 
369 char *cost_approx_str(const object *tmp, object *who) {
370  uint64_t approx_val = price_approx(tmp, who);
371  int idskill1 = 0;
372  int idskill2 = 0;
373  const typedata *tmptype;
374 
376 
377  /* money it's pretty hard to not give the exact price, so skip all logic and just return the real value. */
378  if (tmp->type == MONEY) {
380  }
381 
382  tmptype = get_typedata(tmp->type);
383  if (tmptype) {
384  idskill1 = tmptype->identifyskill;
385  idskill2 = tmptype->identifyskill2;
386  }
387 
388  /* we show an approximate price if
389  * 1) we are approximating
390  * 2) there either is no id skill(s) for the item, or we don't have them
391  * 3) we don't have bargaining skill either
392  */
393  if (!idskill1 || !find_skill_by_number(who, idskill1)) {
394  if (!idskill2 || !find_skill_by_number(who, idskill2)) {
396  int num;
398  archetype *coin = find_next_coin(approx_val, &cointype);
399 
400  if (coin == NULL) {
401  stringbuffer_append_string(buf, "nothing");
402  return stringbuffer_finish(buf);
403  }
404 
405  num = approx_val/coin->clone.value;
406  if (num == 1)
407  stringbuffer_append_printf(buf, "about one %s", coin->clone.name);
408  else if (num < 5)
409  stringbuffer_append_printf(buf, "a few %s", coin->clone.name_pl);
410  else if (num < 10)
411  stringbuffer_append_printf(buf, "several %s", coin->clone.name_pl);
412  else if (num < 25)
413  stringbuffer_append_printf(buf, "a moderate amount of %s", coin->clone.name_pl);
414  else if (num < 100)
415  stringbuffer_append_printf(buf, "lots of %s", coin->clone.name_pl);
416  else if (num < 1000)
417  stringbuffer_append_printf(buf, "a great many %s", coin->clone.name_pl);
418  else
419  stringbuffer_append_printf(buf, "a vast quantity of %s", coin->clone.name_pl);
420  return stringbuffer_finish(buf);
421  }
422  }
423  }
424 
425  // If we get here, return the price we guessed.
427  return cost_str(approx_val);
428 }
429 
430 uint64_t query_money(const object *op) {
431  uint64_t total = 0;
432 
433  if (op->type != PLAYER && op->type != CONTAINER) {
434  LOG(llevError, "Query money called with non player/container\n");
435  return 0;
436  }
438  if (tmp->type == MONEY) {
439  total += (uint64_t)tmp->nrof*(uint64_t)tmp->value;
440  } else if (tmp->type == CONTAINER
442  && (tmp->race == NULL || strstr(tmp->race, "gold"))) {
443  total += query_money(tmp);
444  }
445  } FOR_INV_FINISH();
446  return total;
447 }
448 
461 int pay_for_amount(uint64_t to_pay, object *pl) {
462  if (to_pay == 0)
463  return 1;
464  if (to_pay > query_money(pl))
465  return 0;
466 
467  to_pay = pay_from_container(pl, pl, to_pay);
468 
469  FOR_INV_PREPARE(pl, pouch) {
470  if (to_pay <= 0)
471  break;
472  if (pouch->type == CONTAINER
473  && QUERY_FLAG(pouch, FLAG_APPLIED)
474  && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
475  to_pay = pay_from_container(pl, pouch, to_pay);
476  }
477  } FOR_INV_FINISH();
478  if (to_pay > 0) {
479  LOG(llevError, "pay_for_amount: Cannot remove enough money -- %"FMT64U" remains\n", to_pay);
480  }
481 
482  fix_object(pl);
483  return 1;
484 }
485 
501 int pay_for_item(object *op, object *pl, uint64_t reduction) {
502  uint64_t to_pay = shop_price_buy(op, pl);
503  assert(to_pay >= reduction);
504  to_pay -= reduction;
505 
506  if (to_pay == 0)
507  return 1;
508  if (to_pay > query_money(pl))
509  return 0;
510 
511  to_pay = pay_from_container(pl, pl, to_pay);
512 
513  FOR_INV_PREPARE(pl, pouch) {
514  if (to_pay <= 0)
515  break;
516  if (pouch->type == CONTAINER
517  && QUERY_FLAG(pouch, FLAG_APPLIED)
518  && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
519  to_pay = pay_from_container(pl, pouch, to_pay);
520  }
521  } FOR_INV_FINISH();
522  if (to_pay > 0) {
523  LOG(llevError, "pay_for_item: Cannot remove enough money -- %"FMT64U" remains\n", to_pay);
524  }
527  fix_object(pl);
528  return 1;
529 }
530 
542 static int64_t remove_value(object *coin_objs[], int64_t remain) {
543  int i;
544 
545  for (i = 0; i < NUM_COINS; i++) {
546  int count;
547  int64_t num_coins;
548 
549  if ((int64_t)coin_objs[i]->nrof * coin_objs[i]->value > remain) {
550  num_coins = remain/coin_objs[i]->value;
551  if ((uint64_t)num_coins*(uint64_t)coin_objs[i]->value < (uint64_t) remain) {
552  num_coins++;
553  }
554  } else {
555  num_coins = coin_objs[i]->nrof;
556  }
557  remain -= (int64_t)num_coins*(int64_t)coin_objs[i]->value;
558  coin_objs[i]->nrof -= num_coins;
559  /* Now start making change. Start at the coin value
560  * below the one we just did, and work down to
561  * the lowest value.
562  */
563  count = i-1;
564  while (remain < 0 && count >= 0) {
565  num_coins = -remain/coin_objs[count]->value;
566  coin_objs[count]->nrof += num_coins;
567  remain += num_coins*coin_objs[count]->value;
568  count--;
569  }
570  }
571 
572  return remain;
573 }
574 
583 static void add_value(object *coin_objs[], int64_t value) {
584  int i;
585 
586  for (i = NUM_COINS-LARGEST_COIN_GIVEN-1; i >= 0; i--) {
587  uint32_t nrof;
588 
589  nrof = (uint32_t)(value/coin_objs[i]->value);
590  value -= nrof*coin_objs[i]->value;
591  coin_objs[i]->nrof += nrof;
592  }
593 }
594 
607 static void insert_objects(object *pl, object *container, object *objects[], int objects_len) {
608  int i, one = 0;
609 
610  for (i = 0; i < objects_len; i++) {
611  if (objects[i]->nrof > 0) {
612  object_insert_in_ob(objects[i], container);
613  one = 1;
614  } else {
616  }
617  }
618  if (one)
619  esrv_update_item(UPD_WEIGHT, pl, container);
620 }
621 
635 static uint64_t pay_from_container(object *pl, object *pouch, uint64_t to_pay) {
636  size_t i;
637  int64_t remain;
638  object *coin_objs[NUM_COINS];
639  object *other_money[16]; /* collects MONEY objects not matching coins[] */
640  size_t other_money_len; /* number of allocated entries in other_money[] */
641  archetype *at;
642 
643  if (pouch->type != PLAYER && pouch->type != CONTAINER)
644  return to_pay;
645 
646  remain = to_pay;
647  for (i = 0; i < NUM_COINS; i++)
648  coin_objs[i] = NULL;
649 
650  /* This hunk should remove all the money objects from the player/container */
651  other_money_len = 0;
652  FOR_INV_PREPARE(pouch, tmp) {
653  if (tmp->type == MONEY) {
654  for (i = 0; i < NUM_COINS; i++) {
655  if (!strcmp(coins[NUM_COINS-1-i], tmp->arch->name)
656  && (tmp->value == tmp->arch->clone.value)) {
657  /* This should not happen, but if it does, just
658  * merge the two.
659  */
660  if (coin_objs[i] != NULL) {
661  LOG(llevError, "%s has two money entries of (%s)\n", pouch->name, coins[NUM_COINS-1-i]);
663  coin_objs[i]->nrof += tmp->nrof;
665  } else {
667  coin_objs[i] = tmp;
668  }
669  break;
670  }
671  }
672  if (i == NUM_COINS) {
673  if (other_money_len >= sizeof(other_money)/sizeof(*other_money)) {
674  LOG(llevError, "pay_for_item: Cannot store non-standard money object %s\n", tmp->arch->name);
675  } else {
677  other_money[other_money_len++] = tmp;
678  }
679  }
680  }
681  } FOR_INV_FINISH();
682 
683  /* Fill in any gaps in the coin_objs array - needed to make change. */
684  /* Note that the coin_objs array goes from least value to greatest value */
685  for (i = 0; i < NUM_COINS; i++)
686  if (coin_objs[i] == NULL) {
687  at = find_archetype(coins[NUM_COINS-1-i]);
688  if (at == NULL) {
689  continue;
690  }
691  coin_objs[i] = object_new();
692  object_copy(&at->clone, coin_objs[i]);
693  coin_objs[i]->nrof = 0;
694  }
695 
696  /* Try to pay from standard coins first. */
697  remain = remove_value(coin_objs, remain);
698 
699  /* Now pay from non-standard coins until all is paid. */
700  for (i = 0; i < other_money_len && remain > 0; i++) {
701  uint32_t nrof;
702  object *coin;
703 
704  coin = other_money[i];
705 
706  /* Find the minimal number of coins to use. This prevents converting
707  * excess non-standard coins to standard money.
708  */
709  nrof = (remain+coin->value-1)/coin->value;
710  if (nrof > coin->nrof) {
711  nrof = coin->nrof;
712  }
713  coin->nrof -= nrof;
714  add_value(coin_objs, nrof*coin->value);
715 
716  remain = remove_value(coin_objs, remain);
717  }
718 
719  /* re-insert remaining coins into player */
720  insert_objects(pl, pouch, coin_objs, NUM_COINS);
721  insert_objects(pl, pouch, other_money, other_money_len);
722 
723  return(remain);
724 }
725 
738 static void count_unpaid(object *pl, object *item, int *unpaid_count, uint64_t *unpaid_price) {
740  if (QUERY_FLAG(item, FLAG_UNPAID)) {
741  (*unpaid_count)++;
742  uint64_t price = shop_price_buy(item, pl);
743  if (*unpaid_price != UINT64_MAX) {
744  if (UINT64_MAX - (*unpaid_price) < price) {
745  // Overflow
746  *unpaid_price = UINT64_MAX;
747  } else {
748  *unpaid_price += price;
749  }
750  }
751  }
752  if (item->inv) {
753  count_unpaid(pl, item->inv, unpaid_count, unpaid_price);
754  }
756 }
757 
765 static void count_coins(object *item, uint32_t *coincount) {
767  /* Merely converting the player's monetary wealth won't do.
768  * If we did that, we could print the wrong numbers for the
769  * coins, so we count the money instead.
770  */
771  for (int i = 0; i < NUM_COINS; i++) {
772  if (!strcmp(coins[i], item->arch->name)) {
773  coincount[i] += item->nrof;
774  break;
775  }
776  }
777  if (item->inv) {
778  count_coins(item->inv, coincount);
779  }
781 }
782 
791 static uint64_t compute_price_variation_with_bargaining(object *pl, uint64_t price, float max_variation) {
792  object *skill = find_skill_by_number(pl, SK_BARGAINING);
793  if (skill && skill->level > 0) {
794  return rndm(0, price * (max_variation * skill->level / settings.max_level));
795  }
796  return 0;
797 }
798 
811 int can_pay(object *pl) {
812  int unpaid_count = 0, i;
813  uint64_t unpaid_price = 0;
814  uint32_t coincount[NUM_COINS];
815 
816  if (!pl || pl->type != PLAYER) {
817  LOG(llevError, "can_pay(): called against something that isn't a player\n");
818  return 0;
819  }
820  uint64_t player_wealth = query_money(pl);
821 
822  for (i = 0; i < NUM_COINS; i++)
823  coincount[i] = 0;
824 
825  count_unpaid(pl, pl->inv, &unpaid_count, &unpaid_price);
826  count_coins(pl->inv, coincount);
827 
828  if (unpaid_price > player_wealth) {
829  char buf[MAX_BUF], coinbuf[MAX_BUF];
830  int denominations = 0;
831  char *value = cost_str(unpaid_price);
832 
833  snprintf(buf, sizeof(buf), "You have %d unpaid items that would cost you %s, ", unpaid_count, value);
834  free(value);
835  for (i = 0; i < NUM_COINS; i++) {
836  if (coincount[i] > 0 && coins[i]) {
837  if (denominations == 0)
838  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "but you only have");
839  denominations++;
841  if (arch != NULL)
842  {
843  snprintf(coinbuf, sizeof(coinbuf), " %u %s,", coincount[i], arch->clone.name_pl);
844  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", coinbuf);
845  }
846  }
847  }
848  if (denominations == 0)
849  snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "but you don't have any money.");
850  else if (denominations > 1)
854  return 0;
855  } else
856  return 1;
857 }
858 
865 int shop_pay_unpaid(object *pl, object *op) {
866  char name_op[MAX_BUF];
867 
868  if (!op) {
869  return 1;
870  }
871 
872  if (op->inv)
873  if (!shop_pay_unpaid(pl, op->inv))
874  return 0;
875 
876  while (op) {
877  object *below = op->below;
878 
879  if (QUERY_FLAG(op, FLAG_UNPAID)) {
880  uint64_t price = shop_price_buy(op, pl);
882  if (!pay_for_item(op, pl, reduction)) {
883  uint64_t i = price - query_money(pl);
884  char *missing = cost_str(i);
885 
887  query_name(op, name_op, MAX_BUF);
890  "You lack %s to buy %s.",
891  missing, name_op);
892  free(missing);
894  return 0;
895  } else {
896  // TODO: Figure out how to pass in the shop owner for player shops.
898  return 0;
899  object *tmp;
900  char *value = cost_str(price - reduction);
901 
904  query_name(op, name_op, MAX_BUF);
905 
906  if (reduction > 0) {
907  char *reduction_str = cost_str(reduction);
910  "You paid %s for %s after bargaining a reduction of %s.",
911  value, name_op, reduction_str);
912  change_exp(pl, reduction, "bargaining", SK_EXP_NONE);
913  free(reduction_str);
914  } else {
917  "You paid %s for %s.",
918  value, name_op);
919  }
920  free(value);
921  tmp = object_merge(op, NULL);
922  if (pl->type == PLAYER && !tmp) {
923  /* If item wasn't merged we update it. If merged, object_merge() handled everything for us. */
925  }
926  }
927  }
928 
929  op = below;
930  }
931  return 1;
932 }
933 
945 void sell_item(object *op, object *pl) {
946  object *tmp;
947  archetype *at;
948  char obj_name[MAX_BUF];
949 
950  query_name(op, obj_name, MAX_BUF);
951 
952  if (pl == NULL || pl->type != PLAYER) {
953  LOG(llevDebug, "Object other than player tried to sell something.\n");
954  return;
955  }
956 
958  return;
959 
960  if (op->custom_name)
961  FREE_AND_CLEAR_STR(op->custom_name);
962 
963  uint64_t price = shop_price_sell(op, pl);
964  if (price == 0) {
967  "We're not interested in %s.",
968  obj_name);
969  return;
970  }
971 
973  char *value_str = cost_str(price + extra_gain);
974 
975  if (extra_gain > 0) {
976  change_exp(pl, extra_gain, "bargaining", SK_EXP_NONE);
977  char *extra_str = cost_str(extra_gain);
979  "You receive %s for %s, after bargaining for %s more than proposed.", value_str, obj_name, extra_str);
980  free(extra_str);
981  price += extra_gain;
982  } else {
984  "You receive %s for %s.", value_str, obj_name);
985  }
986  free(value_str);
987 
988  for (int count = LARGEST_COIN_GIVEN; coins[count] != NULL; count++) {
989  at = find_archetype(coins[count]);
990  if (at == NULL)
991  LOG(llevError, "Could not find %s archetype\n", coins[count]);
992  else if ((price/at->clone.value) > 0) {
993  FOR_INV_PREPARE(pl, pouch) {
994  if (pouch->type == CONTAINER
995  && QUERY_FLAG(pouch, FLAG_APPLIED)
996  && pouch->race
997  && strstr(pouch->race, "gold")) {
998  int w = at->clone.weight*(100-pouch->stats.Str)/100;
999  int n = price/at->clone.value;
1000 
1001  if (w == 0)
1002  w = 1; /* Prevent divide by zero */
1003  if (n > 0
1004  && (!pouch->weight_limit || pouch->carrying+w <= pouch->weight_limit)) {
1005  if (pouch->weight_limit
1006  && (pouch->weight_limit-pouch->carrying)/w < n)
1007  n = (pouch->weight_limit-pouch->carrying)/w;
1008 
1009  tmp = object_new();
1010  object_copy(&at->clone, tmp);
1011  tmp->nrof = n;
1012  price -= (uint64_t)tmp->nrof*(uint64_t)tmp->value;
1013  tmp = object_insert_in_ob(tmp, pouch);
1015  }
1016  }
1017  } FOR_INV_FINISH();
1018  if (price/at->clone.value > 0) {
1019  tmp = object_new();
1020  object_copy(&at->clone, tmp);
1021  tmp->nrof = price/tmp->value;
1022  price -= (uint64_t)tmp->nrof*(uint64_t)tmp->value;
1025  }
1026  }
1027  }
1028 
1029  if (price != 0) {
1030  LOG(llevError, "Warning - payment not zero: %" PRIo64 "\n", price);
1031  }
1032 
1034  identify(op);
1035 }
1036 
1052 static double shop_specialisation_ratio(const object *item, const mapstruct *map) {
1053  shopitems *items = map->shopitems;
1054  double ratio = SPECIALISATION_EFFECT, likedness = 0.001;
1055  int i;
1056 
1057  if (item == NULL) {
1058  LOG(llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path);
1059  return 0;
1060  }
1061  if (item->type == (uint8_t)-1) {
1062  LOG(llevError, "shop_specialisation_ratio: passed an item with an invalid type\n");
1063  /*
1064  * I'm not really sure what the /right/ thing to do here is,
1065  * these types of item shouldn't exist anyway, but returning
1066  * the ratio is probably the best bet.."
1067  */
1068  return ratio;
1069  }
1070  if (map->shopitems) {
1071  for (i = 0; i < items[0].index; i++)
1072  if (items[i].typenum == item->type || (items[i].typenum == -1 && likedness == 0.001))
1073  likedness = items[i].strength/100.0;
1074  }
1075  if (likedness > 1.0) { /* someone has been rather silly with the map headers. */
1076  LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, map->path);
1077  likedness = 1.0;
1078  }
1079  if (likedness < -1.0) {
1080  LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, map->path);
1081  likedness = -1.0;
1082  }
1083  ratio = ratio+(1.0-ratio)*likedness;
1084  if (ratio <= 0.1)
1085  ratio = 0.1; /* if the ratio were much lower than this, we would get silly prices */
1086  return ratio;
1087 }
1088 
1103 static double shop_greed(const mapstruct *map) {
1104  float greed = map->shopgreed;
1105  if (greed == 0) {
1106  greed = 1;
1107  }
1108  return tanh(-greed+2.0)/2 + 0.5;
1109 }
1110 
1111 double shop_approval(const mapstruct *map, const object *player) {
1112  double approval = 1.0;
1113  if (map->shoprace) {
1114  approval = NEUTRAL_RATIO;
1115  if (player->race && !strcmp(player->race, map->shoprace))
1116  approval = 1.0;
1117  }
1118  return approval;
1119 }
1120 
1140 static uint64_t value_limit(uint64_t val, int quantity, const object *who, int isshop) {
1141  uint64_t newval, unit_price;
1142  mapstruct *map;
1143 
1144  unit_price = val/quantity;
1145  if (!isshop || !who) {
1146  if (unit_price > 10000)
1147  newval = 8000+isqrt(unit_price)*20;
1148  else
1149  newval = unit_price;
1150  } else {
1151  if (!who->map) {
1152  LOG(llevError, "value_limit: asked shop price for ob %s on NULL map\n", who->name);
1153  return val;
1154  }
1155  map = who->map;
1156  if (map->shopmin && unit_price < map->shopmin)
1157  return 0;
1158  else if (map->shopmax && unit_price > map->shopmax/2)
1159  newval = MIN((map->shopmax/2)+isqrt(unit_price-map->shopmax/2), map->shopmax);
1160  else if (unit_price > 10000)
1161  newval = 8000+isqrt(unit_price)*20;
1162  else
1163  newval = unit_price;
1164  }
1165  newval *= quantity;
1166  return newval;
1167 }
1168 
1174 int shop_describe(const object *op) {
1175  mapstruct *map = op->map;
1176  /*shopitems *items=map->shopitems;*/
1177  int pos = 0, i;
1178  double opinion = 0;
1179  char tmp[MAX_BUF] = "\0", *value;
1180 
1181  if (op->type != PLAYER)
1182  return 0;
1183 
1184  /*check if there is a shop specified for this map */
1185  if (map->shopitems
1186  || map->shopgreed
1187  || map->shoprace
1188  || map->shopmin
1189  || map->shopmax) {
1191  "From looking at the nearby shop you determine that it trades in:");
1192 
1193  if (map->shopitems) {
1194  for (i = 0; i < map->shopitems[0].index; i++) {
1195  if (map->shopitems[i].name && map->shopitems[i].strength > 10) {
1196  snprintf(tmp+pos, sizeof(tmp)-pos, "%s, ", map->shopitems[i].name_pl);
1197  pos += strlen(tmp+pos);
1198  }
1199  }
1200  }
1201  if (!pos)
1202  strcpy(tmp, "a little of everything.");
1203 
1204  /* format the string into a list */
1208 
1209  if (map->shopmax) {
1210  value = cost_str(map->shopmax);
1213  "It won't trade for items above %s.",
1214  value);
1215  free(value);
1216  }
1217 
1218  if (map->shopmin) {
1219  value = cost_str(map->shopmin);
1222  "It won't trade in items worth less than %s.",
1223  value);
1224  free(value);
1225  }
1226 
1227  if (map->shopgreed) {
1228  if (map->shopgreed > 2.0)
1231  "It tends to overcharge massively.");
1232  else if (map->shopgreed > 1.5)
1235  "It tends to overcharge substantially.");
1236  else if (map->shopgreed > 1.1)
1239  "It tends to overcharge slightly.");
1240  else if (map->shopgreed < 0.9)
1243  "It tends to undercharge.");
1244  }
1245  if (map->shoprace) {
1246  opinion = shop_approval(map, op);
1247  if (opinion > 0.8)
1250  "You think the shopkeeper likes you.");
1251  else if (opinion > 0.5)
1254  "The shopkeeper seems unconcerned by you.");
1255  else
1258  "The shopkeeper seems to have taken a dislike to you.");
1259  }
1261  "There is no shop nearby.");
1262 
1263  return 1;
1264 }
1265 
1269 static bool coords_in_shop(mapstruct *map, int x, int y) {
1270  FOR_MAP_PREPARE(map, x, y, floor)
1271  if (floor->type == SHOP_FLOOR) return true;
1272  FOR_MAP_FINISH();
1273  return false;
1274 }
1275 
1276 bool shop_contains(object *ob) {
1277  if (!ob->map) return 0;
1278  return coords_in_shop(ob->map, ob->x, ob->y);
1279 }
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:1140
global.h
diamondslots.cointype
string cointype
Definition: diamondslots.py:18
shop_contains
bool shop_contains(object *ob)
Definition: shop.c:1276
object_remove
void object_remove(object *op)
Definition: object.c:1819
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Definition: define.h:730
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
Settings::max_level
int16_t max_level
Definition: global.h:297
llevError
@ llevError
Definition: logger.h:11
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:1103
obj::value
int32_t value
Definition: object.h:355
FALSE
#define FALSE
Definition: compat.h:14
object_new
object * object_new(void)
Definition: object.c:1255
c
static event_registration c
Definition: citylife.cpp:427
UPD_WEIGHT
#define UPD_WEIGHT
Definition: newclient.h:316
objects
object * objects
Definition: object.c:294
SHOP_FLOOR
@ SHOP_FLOOR
Definition: object.h:183
GEM
@ GEM
Definition: object.h:167
pl
Definition: player.h:105
can_pay
int can_pay(object *pl)
Definition: shop.c:811
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:1174
mail_login.total
total
Definition: mail_login.py:30
add_value
static void add_value(object *coin_objs[], int64_t value)
Definition: shop.c:583
MIN
#define MIN(x, y)
Definition: compat.h:21
count_unpaid
static void count_unpaid(object *pl, object *item, int *unpaid_count, uint64_t *unpaid_price)
Definition: shop.c:738
cost_approx_str
char * cost_approx_str(const object *tmp, object *who)
Definition: shop.c:369
NEUTRAL_RATIO
#define NEUTRAL_RATIO
Definition: shop.c:45
shopitem::typenum
int typenum
Definition: map.h:300
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:1611
shop_pay_unpaid
int shop_pay_unpaid(object *pl, object *op)
Definition: shop.c:865
make_list_like
void make_list_like(char *input)
Definition: utils.c:378
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:274
isqrt
int isqrt(int n)
Definition: utils.c:569
obj::nrof
uint32_t nrof
Definition: object.h:337
archt
Definition: object.h:469
settings
struct Settings settings
Definition: init.c:39
obj
Definition: object.h:277
query_money
uint64_t query_money(const object *op)
Definition: shop.c:430
object_merge
object * object_merge(object *op, object *top)
Definition: object.c:2031
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4317
identify
object * identify(object *op)
Definition: item.c:1409
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:245
NROF
static uint32_t NROF(const object *const ob)
Definition: object.h:611
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:314
object_copy
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:1064
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:585
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:1052
say.price
dictionary price
Definition: say.py:147
obj::name_pl
sstring name_pl
Definition: object.h:318
FOR_OB_AND_BELOW_FINISH
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:754
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:791
SCRIPT_FIX_ALL
#define SCRIPT_FIX_ALL
Definition: global.h:377
shop_price_sell
uint64_t shop_price_sell(const object *tmp, object *who)
Definition: shop.c:211
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:677
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:607
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:635
commongive.quantity
quantity
Definition: commongive.py:7
FOR_OB_AND_BELOW_PREPARE
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:750
mapdef
Definition: map.h:317
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:277
shop_price_buy
uint64_t shop_price_buy(const object *tmp, object *who)
Definition: shop.c:190
pay_for_item
int pay_for_item(object *op, object *pl, uint64_t reduction)
Definition: shop.c:501
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:195
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:723
sell_item
void sell_item(object *op, object *pl)
Definition: shop.c:945
obj::arch
struct archt * arch
Definition: object.h:417
shop_cha_modifier
static float shop_cha_modifier(int charisma)
Definition: shop.c:173
obj::type
uint8_t type
Definition: object.h:343
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:262
obj::stats
living stats
Definition: object.h:373
archt::clone
object clone
Definition: object.h:473
real_money_value
static StringBuffer * real_money_value(const object *coin, StringBuffer *buf)
Definition: shop.c:357
MSG_TYPE_SHOP_SELL
#define MSG_TYPE_SHOP_SELL
Definition: newclient.h:512
obj::weight
int32_t weight
Definition: object.h:370
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
shop_efficiency
float shop_efficiency(const object *player)
Definition: shop.c:184
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:284
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:303
diamondslots.y
y
Definition: diamondslots.py:16
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:1610
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.c:138
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:2833
get_typedata
const typedata * get_typedata(int itemtype)
Definition: item.c:320
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:1111
remove_value
static int64_t remove_value(object *coin_objs[], int64_t remain)
Definition: shop.c:542
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:266
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:1546
coords_in_shop
static bool coords_in_shop(mapstruct *map, int x, int y)
Definition: shop.c:1269
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:365
count_coins
static void count_coins(object *item, uint32_t *coincount)
Definition: shop.c:765
FLAG_CURSED
#define FLAG_CURSED
Definition: define.h:316
shopitem::strength
int8_t strength
Definition: map.h:301
obj::magic
int8_t magic
Definition: object.h:353
if
if(!(yy_init))
Definition: loader.c:2626
shopitem
Definition: map.h:297
FOR_INV_PREPARE
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:670
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:356
pay_for_amount
int pay_for_amount(uint64_t to_pay, object *pl)
Definition: shop.c:461
llevDebug
@ llevDebug
Definition: logger.h:13
MONEY
@ MONEY
Definition: object.h:137
is_identified
int is_identified(const object *op)
Definition: item.c:1336