ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.c
Revision: 1.6
Committed: Wed May 24 03:30:23 2006 UTC (18 years ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +19 -51 lines
Log Message:
Changed shop behaviour to tell the player that he/she will require this
amount of money to buy n items costing that amount of money. (Hope that was
clear :)

File Contents

# Content
1 /*
2 * static char *rcsid_shop_c =
3 * "$Id$";
4 */
5
6 /*
7 CrossFire, A Multiplayer game for X-windows
8
9 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10 Copyright (C) 1992 Frank Tore Johansen
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 The authors can be reached via e-mail at crossfire-devel@real-time.com
27 */
28
29 #include <global.h>
30 #include <spells.h>
31 #include <skills.h>
32 #include <living.h>
33 #include <newclient.h>
34 #ifndef __CEXTRACT__
35 #include <sproto.h>
36 #endif
37 #include <math.h>
38
39 /* this is a measure of how effective store specialisation is. A general store
40 * will offer this proportion of the 'maximum' price, a specialised store will
41 * offer a range of prices around it such that the maximum price is always one
42 * therefore making this number higher, makes specialisation less effective.
43 * setting this value above 1 or to a negative value would have interesting,
44 * (though not useful) effects.
45 */
46 #define SPECIALISATION_EFFECT 0.5
47
48 /* price a shopkeeper will give to someone they disapprove of.*/
49 #define DISAPPROVAL_RATIO 0.2
50
51 /* price a shopkeeper will give someone they neither like nor dislike */
52 #define NEUTRAL_RATIO 0.8
53
54 static uint64 pay_from_container(object *pl, object *pouch, uint64 to_pay);
55 static uint64 value_limit(uint64 val, int quantity, const object *who, int isshop);
56 static double shop_specialisation_ratio(const object *item, const mapstruct *map);
57 static double shop_greed(const mapstruct *map);
58
59 #define NUM_COINS 4 /* number of coin types */
60 static const char* const coins[] = {"royalty", "platinacoin", "goldcoin", "silvercoin", NULL};
61
62 /* Added F_TRUE flag to define.h to mean that the price should not
63 * be adjusted by players charisma. With F_TRUE, it returns the amount
64 * that the item is worth, if it was sold, but unadjusted by charisma.
65 * This is needed for alchemy, to to determine what value of gold nuggets
66 * should be given (the gold nuggets, when sold, will have the adjustment
67 * by charisma done at that time). NULL could have been passed as the
68 * who parameter, but then the adjustment for expensive items (>10000)
69 * would not be done.
70 *
71 * Added F_APPROX flag, which means that the price returned should be wrong by
72 * an amount related to the player's bargaining skill.
73 *
74 * Added F_SHOP flag to mean that the specialisation of the shop on the player's
75 * current map should be taken into account when determining the price. Shops that
76 * specialise in what is being traded will give better prices than those that do not.
77 *
78 * CF 0.91.4 - This function got changed around a bit. Now the
79 * number of object is multiplied by the value early on. This fixes problems
80 * with items worth very little. What happened before is that various
81 * divisions took place, the value got rounded to 0 (Being an int), and
82 * thus remained 0.
83 *
84 * Mark Wedel (mwedel@pyramid.com)
85 */
86 uint64 query_cost(const object *tmp, object *who, int flag) {
87 uint64 val;
88 int number; /* used to better calculate value */
89 int no_bargain;
90 int identified;
91 int not_cursed;
92 int approximate;
93 int shop;
94 float diff;
95 float ratio;
96
97 no_bargain = flag & F_NO_BARGAIN;
98 identified = flag & F_IDENTIFIED;
99 not_cursed = flag & F_NOT_CURSED;
100 approximate = flag & F_APPROX;
101 shop = flag & F_SHOP;
102 flag &= ~(F_NO_BARGAIN|F_IDENTIFIED|F_NOT_CURSED|F_APPROX|F_SHOP);
103
104 if (tmp->type==MONEY) return (tmp->nrof * tmp->value);
105 if (tmp->type==GEM) {
106 if (flag==F_TRUE) return (tmp->nrof * tmp->value);
107 if (flag==F_BUY) return (1.03 * tmp->nrof * tmp->value);
108 if (flag==F_SELL) return (0.97 * tmp->nrof * tmp->value);
109 LOG(llevError,"Query_cost: Gem type with unknown flag : %d\n", flag);
110 return 0;
111 }
112 number = tmp->nrof;
113 if (number==0) number=1;
114 if (QUERY_FLAG(tmp, FLAG_IDENTIFIED) || !need_identify(tmp) || identified) {
115 if (!not_cursed && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)))
116 return 0;
117 else
118 val=tmp->value * number;
119 }
120 /* This area deals with objects that are not identified, but can be */
121 else {
122 if (tmp->arch != NULL) {
123 if (flag == F_BUY) {
124 LOG(llevError, "Asking for buy-value of unidentified object.\n");
125 val = tmp->arch->clone.value * 50 * number;
126 }
127 else { /* Trying to sell something, or get true value */
128 if (tmp->type == POTION)
129 val = number * 1000; /* Don't want to give anything away */
130 else {
131 /* Get 2/3'rd value for applied objects, 1/3'rd for totally
132 * unknown objects
133 */
134 if (QUERY_FLAG(tmp, FLAG_BEEN_APPLIED))
135 val = number * tmp->arch->clone.value *2 / 3;
136 else
137 val = number * tmp->arch->clone.value / 3;
138 }
139 }
140 } else { /* No archetype with this object */
141 LOG(llevDebug,"In sell item: Have object with no archetype: %s\n", tmp->name);
142 if (flag == F_BUY) {
143 LOG(llevError, "Asking for buy-value of unidentified object without arch.\n");
144 val = number * tmp->value * 10;
145 }
146 else
147 val = number * tmp->value / 5;
148 }
149 }
150
151 /* If the item has been applied or identifed or does not need to be
152 * identified, AND the object is magical and the archetype is non
153 * magical, then change values accordingly. The tmp->arch==NULL is
154 * really just a check to prevent core dumps for when it checks
155 * tmp->arch->clone.magic for any magic. The check for archetype
156 * magic is to not give extra money for archetypes that are by
157 * default magical. This is because the archetype value should have
158 * already figured in that value.
159 */
160 if((QUERY_FLAG(tmp, FLAG_IDENTIFIED)||!need_identify(tmp)||identified||
161 QUERY_FLAG(tmp, FLAG_BEEN_APPLIED)) &&
162 tmp->magic&&(tmp->arch==NULL||!tmp->arch->clone.magic)) {
163 if(tmp->magic>0)
164 val*=(3*tmp->magic*tmp->magic*tmp->magic);
165 else
166 /* Note that tmp->magic is negative, so that this
167 * will actually be something like val /=2, /=3, etc.
168 */
169 val/=(1-tmp->magic);
170 }
171
172 if (tmp->type==WAND) {
173 /* Value of the wand is multiplied by the number of
174 * charges. the treasure code already sets up the value
175 * 50 charges is used as the baseline.
176 */
177 if (QUERY_FLAG(tmp, FLAG_IDENTIFIED) || !need_identify(tmp) || identified)
178 val=(val*tmp->stats.food) / 50;
179 else /* if not identified, presume one charge */
180 val/=50;
181 }
182
183 /* Limit amount of money you can get for really great items. */
184 if (flag==F_TRUE || flag==F_SELL)
185 val=value_limit(val, number, who, shop);
186
187 /* we need to multiply these by 4.0 to keep buy costs roughly the same
188 * (otherwise, you could buy a potion of charisma for around 400 pp.
189 * Arguable, the costs in the archetypes should be updated to better
190 * reflect values (potion charisma list for 1250 gold)
191 */
192 val *= 4;
193
194 /* This modification is for bargaining skill.
195 * Now only players with max level in bargaining
196 * AND Cha = 30 will get optimal price.
197 * Thus charisma will never get useless.
198 * -b.e. edler@heydernet.de
199 */
200
201 if (who!=NULL && who->type==PLAYER) {
202 int lev_bargain = 0;
203 int lev_identify = 0;
204 int idskill1=0;
205 int idskill2=0;
206 const typedata *tmptype;
207
208 /* ratio determines how much of the price modification
209 * will come from the basic stat charisma
210 * the rest will come from the level in bargaining skill
211 */
212 ratio = 0.5;
213 tmptype=get_typedata(tmp->type);
214
215 if (find_skill_by_number(who,SK_BARGAINING)) {
216 lev_bargain = find_skill_by_number(who,SK_BARGAINING)->level;
217 }
218 if (tmptype) {
219 idskill1=tmptype->identifyskill;
220 if (idskill1) {
221 idskill2=tmptype->identifyskill2;
222 if (find_skill_by_number(who,idskill1)) {
223 lev_identify = find_skill_by_number(who,idskill1)->level;
224 }
225 if (idskill2 && find_skill_by_number(who,idskill2)) {
226 lev_identify += find_skill_by_number(who,idskill2)->level;
227 }
228 }
229 }
230 else LOG(llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->name);
231 if ( !no_bargain && (lev_bargain>0) )
232 diff = (0.8 - 0.6*((lev_bargain+settings.max_level*0.05)
233 /(settings.max_level*1.05)));
234 else
235 diff = 0.8;
236
237 diff *= 1-ratio;
238
239 /* Diff is now a float between 0.2 and 0.8 */
240 diff+=(cha_bonus[who->stats.Cha]-1)/(1+cha_bonus[who->stats.Cha])*ratio;
241
242 if(flag==F_BUY)
243 val=(val*(long)(1000*(1+diff)))/1000;
244 else if (flag==F_SELL)
245 val=(val*(long)(1000*(1-diff)))/1000;
246
247 /* If we are approximating, then the value returned should be allowed to be wrong
248 * however merely using a random number each time will not be sufficiant, as then
249 * multiple examinations would give different answers, so we'll use the count
250 * instead. By taking the sine of the count, a value between -1 and 1 is
251 * generated, we then divide by the square root of the bargaining skill and the
252 * appropriate identification skills, so that higher level players get better estimates.
253 * (we need a +1 there in case we would otherwise be dividing by zero.
254 */
255 if (approximate)
256 val = (sint64)val + (sint64)((sint64)val*(sin(tmp->count)/sqrt(lev_bargain+lev_identify*2+1.0)));
257 }
258
259 /* I don't think this should really happen - if it does, it indicates and
260 * overflow of diff above. That shoudl only happen if
261 * we are selling objects - in that case, the person just
262 * gets no money.
263 */
264 if((sint64)val<0)
265 val=0;
266
267 /* Unidentified stuff won't sell for more than 60gp */
268 if(flag==F_SELL && !QUERY_FLAG(tmp, FLAG_IDENTIFIED) && need_identify(tmp) && !identified) {
269 val = (val > 600)? 600:val;
270 }
271
272 /* if we are in a shop, check how the type of shop should affect the price */
273 if (shop && who) {
274 if (flag==F_SELL)
275 val=(sint64)val*shop_specialisation_ratio(tmp, who->map)*shopkeeper_approval(who->map, who)
276 /shop_greed(who->map);
277 else if (flag==F_BUY) {
278 /*
279 * when buying, if the item was sold by another player, it is ok to
280 * let the item be sold cheaper, according to the specialisation of
281 * the shop. If a player sold an item here, then his sale price was
282 * multiplied by the specialisation ratio, to do the same to the buy
283 * price will not generate extra money. However, the
284 * same is not true of generated items, these have to /divide/ by the
285 * specialisation, so that the price is never less than what they could
286 * be sold for (otherwise players could camp map resets to make money).
287 * In game terms, a non-specialist shop, might not recognise the true
288 * value of the items they sell (much like how people sometimes find
289 * antiques in a junk shop in real life).
290 */
291 if (QUERY_FLAG(tmp, FLAG_PLAYER_SOLD))
292 val=(sint64)val*shop_greed(who->map)*shop_specialisation_ratio(tmp, who->map)
293 /shopkeeper_approval(who->map, who);
294 else
295 val=(sint64)val*shop_greed(who->map)
296 /(shop_specialisation_ratio(tmp, who->map)*shopkeeper_approval(who->map, who));
297 }
298 /* we will also have an extra 0-5% variation between shops of the same type
299 * for valuable items (below a value of 50 this effect wouldn't be very
300 * pointful, and could give fun with rounding.
301 */
302 if(who->map->path!=NULL && val > 50)
303 val=(sint64)val+0.05*(sint64)val*cos(tmp->count+strlen(who->map->path));
304 }
305 return val;
306 }
307
308 /* Find the coin type that is worth more the 'c'. Starts at the
309 * cointype placement.
310 */
311
312 static archetype *find_next_coin(uint64 c, int *cointype) {
313 archetype *coin;
314
315 do {
316 if (coins[*cointype]==NULL) return NULL;
317 coin = find_archetype(coins[*cointype]);
318 if (coin == NULL)
319 return NULL;
320 *cointype += 1;
321 } while (coin->clone.value > c);
322
323 return coin;
324 }
325
326 /* This returns a string of how much something is worth based on
327 * an integer being passed.
328 * cost is the cost we need to represent.
329 * While cost is 64 bit, the number of any coin is still really
330 * limited to 32 bit (size of nrof field). If it turns out players
331 * have so much money that they have more than 2 billion platinum
332 * coins, there are certainly issues - the easiest fix at that
333 * time is to add a higher denomination (mithril piece with
334 * 10,000 silver or something)
335 */
336 static const char *cost_string_from_value(uint64 cost)
337 {
338 static char buf[MAX_BUF];
339 archetype *coin, *next_coin;
340 char *endbuf;
341 int num, cointype = 0;
342
343 coin = find_next_coin(cost, &cointype);
344 if (coin == NULL)
345 return "nothing";
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 strcpy(buf,"an unimaginable sum of money.");
354 return buf;
355 }
356
357 cost -= (uint64)num * (uint64)coin->clone.value;
358 if (num == 1)
359 sprintf(buf, "1 %s", coin->clone.name);
360 else
361 sprintf(buf, "%d %s", num, coin->clone.name_pl);
362
363 next_coin = find_next_coin(cost, &cointype);
364 if (next_coin == NULL)
365 return buf;
366
367 endbuf = buf + strlen(buf);
368
369 coin = next_coin;
370 num = cost / coin->clone.value;
371 cost -= (uint64)num * (uint64)coin->clone.value;
372
373 strcat(endbuf, " and "); endbuf += 5;
374 if (num == 1)
375 sprintf(endbuf, "1 %s", coin->clone.name);
376 else
377 sprintf(endbuf, "%d %s", num, coin->clone.name_pl);
378
379 return buf;
380 }
381
382 const char *query_cost_string(const object *tmp,object *who,int flag) {
383 uint64 real_value = query_cost(tmp,who,flag);
384 int idskill1=0;
385 int idskill2=0;
386 const typedata *tmptype;
387
388 tmptype=get_typedata(tmp->type);
389 if (tmptype) {
390 idskill1=tmptype->identifyskill;
391 idskill2=tmptype->identifyskill2;
392 }
393
394 /* we show an approximate price if
395 * 1) we are approximating
396 * 2) there either is no id skill(s) for the item, or we don't have them
397 * 3) we don't have bargaining skill either
398 */
399 if (flag & F_APPROX) {
400 if (!idskill1 || !find_skill_by_number(who, idskill1)) {
401 if (!idskill2 || !find_skill_by_number(who, idskill2)) {
402 if (!find_skill_by_number(who,SK_BARGAINING)) {
403 static char buf[MAX_BUF];
404 int num, cointype = 0;
405 archetype *coin = find_next_coin(real_value, &cointype);
406
407 if (coin == NULL) return "nothing";
408
409 num = real_value / coin->clone.value;
410 if (num == 1)
411 sprintf(buf, "about one %s", coin->clone.name);
412 else if (num < 5)
413 sprintf(buf, "a few %s", coin->clone.name_pl);
414 else if (num < 10)
415 sprintf(buf, "several %s", coin->clone.name_pl);
416 else if (num < 25)
417 sprintf(buf, "a moderate amount of %s", coin->clone.name_pl);
418 else if (num < 100)
419 sprintf(buf, "lots of %s", coin->clone.name_pl);
420 else if (num < 1000)
421 sprintf(buf, "a great many %s", coin->clone.name_pl);
422 else
423 sprintf(buf, "a vast quantity of %s", coin->clone.name_pl);
424 return buf;
425 }
426 }
427 }
428 }
429 return cost_string_from_value(real_value);
430 }
431
432 /* This function finds out how much money the player is carrying,
433 * including what is in containers.
434 */
435 uint64 query_money(const object *op) {
436 object *tmp;
437 uint64 total=0;
438
439 if (op->type!=PLAYER && op->type!=CONTAINER) {
440 LOG(llevError, "Query money called with non player/container\n");
441 return 0;
442 }
443 for (tmp = op->inv; tmp; tmp= tmp->below) {
444 if (tmp->type==MONEY) {
445 total += (uint64)tmp->nrof * (uint64)tmp->value;
446 } else if (tmp->type==CONTAINER &&
447 QUERY_FLAG(tmp,FLAG_APPLIED) &&
448 (tmp->race==NULL || strstr(tmp->race,"gold"))) {
449 total += query_money(tmp);
450 }
451 }
452 return total;
453 }
454 /* TCHIZE: This function takes the amount of money from the
455 * the player inventory and from it's various pouches using the
456 * pay_from_container function.
457 * returns 0 if not possible. 1 if success
458 */
459 int pay_for_amount(uint64 to_pay,object *pl) {
460 object *pouch;
461
462 if (to_pay==0) return 1;
463 if (to_pay > query_money(pl)) return 0;
464
465 to_pay = pay_from_container(pl, pl, to_pay);
466
467 for (pouch=pl->inv; (pouch!=NULL) && (to_pay>0); pouch=pouch->below) {
468 if (pouch->type == CONTAINER
469 && QUERY_FLAG(pouch, FLAG_APPLIED)
470 && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
471 to_pay = pay_from_container(pl, pouch, to_pay);
472 }
473 }
474 fix_player(pl);
475 return 1;
476 }
477
478 /* DAMN: This is now a wrapper for pay_from_container, which is
479 * called for the player, then for each active container that can hold
480 * money until op is paid for. Change will be left wherever the last
481 * of the price was paid from.
482 */
483 int pay_for_item(object *op,object *pl) {
484 uint64 to_pay = query_cost(op,pl,F_BUY | F_SHOP);
485 object *pouch;
486 uint64 saved_money;
487
488 if (to_pay==0) return 1;
489 if (to_pay>query_money(pl)) return 0;
490
491 /* We compare the paid price with the one for a player
492 * without bargaining skill.
493 * This determins the amount of exp (if any) gained for bargaining.
494 */
495 saved_money = query_cost(op,pl,F_BUY | F_NO_BARGAIN | F_SHOP) - to_pay;
496
497 if (saved_money > 0)
498 change_exp(pl,saved_money,"bargaining",SK_EXP_NONE);
499
500 to_pay = pay_from_container(pl, pl, to_pay);
501
502 for (pouch=pl->inv; (pouch!=NULL) && (to_pay>0); pouch=pouch->below) {
503 if (pouch->type == CONTAINER
504 && QUERY_FLAG(pouch, FLAG_APPLIED)
505 && (pouch->race == NULL || strstr(pouch->race, "gold"))) {
506 to_pay = pay_from_container(pl, pouch, to_pay);
507 }
508 }
509 if (settings.real_wiz == FALSE && QUERY_FLAG(pl, FLAG_WAS_WIZ))
510 SET_FLAG(op, FLAG_WAS_WIZ);
511 fix_player(pl);
512 return 1;
513 }
514
515 /* This pays for the item, and takes the proper amount of money off
516 * the player.
517 * CF 0.91.4 - this function is mostly redone in order to fix a bug
518 * with weight not be subtracted properly. We now remove and
519 * insert the coin objects - this should update the weight
520 * appropriately
521 *
522 * DAMN: This function is used for the player, then for any active
523 * containers that can hold money.
524 *
525 * pouch is the container (pouch or player) to remove the coins from.
526 * to_pay is the required amount.
527 * returns the amount still missing after using "pouch".
528 */
529 static uint64 pay_from_container(object *pl, object *pouch, uint64 to_pay) {
530 int count, i;
531 sint64 remain;
532 object *tmp, *coin_objs[NUM_COINS], *next;
533 archetype *at;
534
535 if (pouch->type != PLAYER && pouch->type != CONTAINER) return to_pay;
536
537 remain = to_pay;
538 for (i=0; i<NUM_COINS; i++) coin_objs[i] = NULL;
539
540 /* This hunk should remove all the money objects from the player/container */
541 for (tmp=pouch->inv; tmp; tmp=next) {
542 next = tmp->below;
543
544 if (tmp->type == MONEY) {
545 for (i=0; i<NUM_COINS; i++) {
546 if (!strcmp(coins[NUM_COINS-1-i], tmp->arch->name) &&
547 (tmp->value == tmp->arch->clone.value) ) {
548
549 /* This should not happen, but if it does, just *
550 * merge the two. */
551 if (coin_objs[i]!=NULL) {
552 LOG(llevError,"%s has two money entries of (%s)\n",
553 pouch->name, coins[NUM_COINS-1-i]);
554 remove_ob(tmp);
555 coin_objs[i]->nrof += tmp->nrof;
556 esrv_del_item(pl->contr, tmp->count);
557 free_object(tmp);
558 }
559 else {
560 remove_ob(tmp);
561 if(pouch->type==PLAYER) esrv_del_item(pl->contr, tmp->count);
562 coin_objs[i] = tmp;
563 }
564 break;
565 }
566 }
567 if (i==NUM_COINS)
568 LOG(llevError,"in pay_for_item: Did not find string match for %s\n", tmp->arch->name);
569 }
570 }
571
572 /* Fill in any gaps in the coin_objs array - needed to make change. */
573 /* Note that the coin_objs array goes from least value to greatest value */
574 for (i=0; i<NUM_COINS; i++)
575 if (coin_objs[i]==NULL) {
576 at = find_archetype(coins[NUM_COINS-1-i]);
577 if (at==NULL) LOG(llevError, "Could not find %s archetype\n", coins[NUM_COINS-1-i]);
578 coin_objs[i] = get_object();
579 copy_object(&at->clone, coin_objs[i]);
580 coin_objs[i]->nrof = 0;
581 }
582
583 for (i=0; i<NUM_COINS; i++) {
584 int num_coins;
585
586 if (coin_objs[i]->nrof*coin_objs[i]->value> remain) {
587 num_coins = remain / coin_objs[i]->value;
588 if ((uint64)num_coins*(uint64)coin_objs[i]->value < remain) num_coins++;
589 } else {
590 num_coins = coin_objs[i]->nrof;
591 }
592 remain -= (sint64) num_coins * (sint64)coin_objs[i]->value;
593 coin_objs[i]->nrof -= num_coins;
594 /* Now start making change. Start at the coin value
595 * below the one we just did, and work down to
596 * the lowest value.
597 */
598 count=i-1;
599 while (remain<0 && count>=0) {
600 num_coins = -remain/ coin_objs[count]->value;
601 coin_objs[count]->nrof += num_coins;
602 remain += num_coins * coin_objs[count]->value;
603 count--;
604 }
605 }
606 for (i=0; i<NUM_COINS; i++) {
607 if (coin_objs[i]->nrof) {
608 object *tmp = insert_ob_in_ob(coin_objs[i], pouch);
609
610 esrv_send_item(pl, tmp);
611 esrv_send_item (pl, pouch);
612 if (pl != pouch) esrv_update_item (UPD_WEIGHT, pl, pouch);
613 if (pl->type != PLAYER) {
614 esrv_send_item (pl, pl);
615 }
616 } else {
617 free_object(coin_objs[i]);
618 }
619 }
620 return(remain);
621 }
622
623 /* Checks all unpaid items in op's inventory, adds up all the money they
624 * have, and checks that they can actually afford what they want to buy.
625 * Returns 1 if they can, and 0 if they can't. also prints an appropriate message
626 * to the player
627 */
628
629 int can_pay(object *pl) {
630 int unpaid_count = 0, i;
631 uint64 unpaid_price = 0;
632 uint64 player_wealth = query_money(pl);
633 object *item;
634 uint32 coincount[NUM_COINS];
635 if (!pl || pl->type != PLAYER) {
636 LOG(llevError, "can_pay(): called against something that isn't a player\n");
637 return 0;
638 }
639 for (i=0; i< NUM_COINS; i++) coincount[i] = 0;
640 for (item = pl->inv;item;) {
641 if QUERY_FLAG(item, FLAG_UNPAID) {
642 unpaid_count++;
643 unpaid_price += query_cost(item, pl, F_BUY | F_SHOP);
644 }
645 /* merely converting the player's monetary wealth won't do, if we did that,
646 * we could print the wrong numbers for the coins, so we count the money instead
647 */
648 for (i=0; i< NUM_COINS; i++)
649 if (!strcmp(coins[i], item->arch->name))
650 coincount[i] += item->nrof;
651 if (item->inv) item = item->inv;
652 else if (item->below) item = item->below;
653 else if (item->env && item->env != pl && item->env->below) item = item->env->below;
654 else item = NULL;
655 }
656 if (unpaid_price > player_wealth) {
657 char buf[MAX_BUF], coinbuf[MAX_BUF];
658 int denominations = 0;
659 int has_coins = NUM_COINS;
660 char cost[MAX_BUF];
661 char missing[MAX_BUF];
662
663 sprintf(cost, "%s", cost_string_from_value(unpaid_price));
664 sprintf(missing, "%s", cost_string_from_value(unpaid_price - player_wealth));
665
666 sprintf(buf, "You have %d unpaid items that would cost you %s. You need another %s to be able to afford that.",
667 unpaid_count, cost, missing);
668 new_draw_info(NDI_UNIQUE, 0, pl, buf);
669 return 0;
670 }
671 else return 1;
672 }
673
674
675 /* Better get_payment, descends containers looking for
676 * unpaid items, and pays for them.
677 * returns 0 if the player still has unpaid items.
678 * returns 1 if the player has paid for everything.
679 * pl is the player buying the stuff.
680 * op is the object we are examining. If op has
681 * and inventory, we examine that. IF there are objects
682 * below op, we descend down.
683 */
684 int get_payment(object *pl, object *op) {
685 char buf[MAX_BUF];
686 int ret=1;
687
688 if (op!=NULL&&op->inv)
689 ret = get_payment(pl, op->inv);
690
691 if (!ret)
692 return 0;
693
694 if (op!=NULL&&op->below)
695 ret = get_payment (pl, op->below);
696
697 if (!ret)
698 return 0;
699
700 if(op!=NULL&&QUERY_FLAG(op,FLAG_UNPAID)) {
701 strncpy(buf,query_cost_string(op,pl,F_BUY | F_SHOP),MAX_BUF);
702 buf[MAX_BUF-1] = '\0';
703 if(!pay_for_item(op,pl)) {
704 uint64 i=query_cost(op,pl,F_BUY | F_SHOP) - query_money(pl);
705 CLEAR_FLAG(op, FLAG_UNPAID);
706 new_draw_info_format(NDI_UNIQUE, 0, pl,
707 "You lack %s to buy %s.", cost_string_from_value(i),
708 query_name(op));
709 SET_FLAG(op, FLAG_UNPAID);
710 return 0;
711 } else {
712 object *tmp;
713 tag_t c = op->count;
714
715 CLEAR_FLAG(op, FLAG_UNPAID);
716 CLEAR_FLAG(op, FLAG_PLAYER_SOLD);
717 new_draw_info_format(NDI_UNIQUE, 0, op,
718 "You paid %s for %s.",buf,query_name(op));
719 tmp=merge_ob(op,NULL);
720 if (pl->type == PLAYER) {
721 if (tmp) { /* it was merged */
722 esrv_del_item (pl->contr, c);
723 op = tmp;
724 }
725 esrv_send_item(pl, op);
726 }
727 }
728 }
729 return 1;
730 }
731
732
733 /* Modified function to give out platinum coins. This function uses
734 * the coins[] array to know what coins are available, just like
735 * buy item.
736 *
737 * Modified to fill available race: gold containers before dumping
738 * remaining coins in character's inventory.
739 */
740 void sell_item(object *op, object *pl) {
741 uint64 i=query_cost(op,pl,F_SELL | F_SHOP), extra_gain;
742 int count;
743 object *tmp, *pouch;
744 archetype *at;
745
746 if(pl==NULL||pl->type!=PLAYER) {
747 LOG(llevDebug,"Object other than player tried to sell something.\n");
748 return;
749 }
750
751 if(op->custom_name) FREE_AND_CLEAR_STR(op->custom_name);
752
753 if(!i) {
754 new_draw_info_format(NDI_UNIQUE, 0, pl,
755 "We're not interested in %s.",query_name(op));
756
757 /* Even if the character doesn't get anything for it, it may still be
758 * worth something. If so, make it unpaid
759 */
760 if (op->value) {
761 SET_FLAG(op, FLAG_UNPAID);
762 SET_FLAG(op, FLAG_PLAYER_SOLD);
763 }
764 identify(op);
765 return;
766 }
767
768 /* We compare the price with the one for a player
769 * without bargaining skill.
770 * This determins the amount of exp (if any) gained for bargaining.
771 * exp/10 -> 1 for each gold coin
772 */
773 extra_gain = i - query_cost(op,pl,F_SELL | F_NO_BARGAIN | F_SHOP);
774
775 if (extra_gain > 0)
776 change_exp(pl,extra_gain/10,"bargaining",SK_EXP_NONE);
777
778 for (count=0; coins[count]!=NULL; count++) {
779 at = find_archetype(coins[count]);
780 if (at==NULL) LOG(llevError, "Could not find %s archetype\n", coins[count]);
781 else if ((i/at->clone.value) > 0) {
782 for ( pouch=pl->inv ; pouch ; pouch=pouch->below ) {
783 if ( pouch->type==CONTAINER && QUERY_FLAG(pouch, FLAG_APPLIED) && pouch->race && strstr(pouch->race, "gold") ) {
784 int w = at->clone.weight * (100-pouch->stats.Str)/100;
785 int n = i/at->clone.value;
786
787 if (w==0) w=1; /* Prevent divide by zero */
788 if ( n>0 && (!pouch->weight_limit || pouch->carrying+w<=pouch->weight_limit)) {
789 if (pouch->weight_limit && (pouch->weight_limit-pouch->carrying)/w<n)
790 n = (pouch->weight_limit-pouch->carrying)/w;
791
792 tmp = get_object();
793 copy_object(&at->clone, tmp);
794 tmp->nrof = n;
795 i -= (uint64)tmp->nrof * (uint64)tmp->value;
796 tmp = insert_ob_in_ob(tmp, pouch);
797 esrv_send_item (pl, tmp);
798 esrv_send_item (pl, pouch);
799 esrv_update_item (UPD_WEIGHT, pl, pouch);
800 esrv_send_item (pl, pl);
801 }
802 }
803 }
804 if (i/at->clone.value > 0) {
805 tmp = get_object();
806 copy_object(&at->clone, tmp);
807 tmp->nrof = i/tmp->value;
808 i -= (uint64)tmp->nrof * (uint64)tmp->value;
809 tmp = insert_ob_in_ob(tmp, pl);
810 esrv_send_item (pl, tmp);
811 esrv_send_item (pl, pl);
812 }
813 }
814 }
815
816 if (i!=0)
817 #ifndef WIN32
818 LOG(llevError,"Warning - payment not zero: %llu\n", i);
819 #else
820 LOG(llevError,"Warning - payment not zero: %I64u\n", i);
821 #endif
822
823 new_draw_info_format(NDI_UNIQUE, 0, pl,
824 "You receive %s for %s.",query_cost_string(op,pl,F_SELL | F_SHOP),
825 query_name(op));
826 SET_FLAG(op, FLAG_UNPAID);
827 identify(op);
828 }
829
830 /* returns a double that is the ratio of the price that a shop will offer for
831 * item based on the shops specialisation. Does not take account of greed,
832 * returned value is between (2*SPECIALISATION_EFFECT-1) and 1 and in any
833 * event is never less than 0.1 (calling functions divide by it)
834 */
835 static double shop_specialisation_ratio(const object *item, const mapstruct *map) {
836 shopitems *items=map->shopitems;
837 double ratio = SPECIALISATION_EFFECT, likedness=0.001;
838 int i;
839
840 if (item==NULL) {
841 LOG(llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path);
842 return 0;
843 }
844 if (!item->type) {
845 LOG(llevError, "shop_specialisation_ratio: passed an item with an invalid type\n");
846 /*
847 * I'm not really sure what the /right/ thing to do here is, these types of
848 * item shouldn't exist anyway, but returning the ratio is probably the best bet.."
849 */
850 return ratio;
851 }
852 if (map->shopitems) {
853 for (i=0; i<items[0].index; i++)
854 if (items[i].typenum==item->type || (!items[i].typenum && likedness == 0.001))
855 likedness = items[i].strength/100.0;
856 }
857 if (likedness > 1.0) { /* someone has been rather silly with the map headers. */
858 LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n",
859 item->type, map->path);
860 likedness = 1.0;
861 }
862 if (likedness < -1.0) {
863 LOG(llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n",
864 item->type, map->path);
865 likedness = -1.0;
866 }
867 ratio = ratio + (1.0-ratio) * likedness;
868 if (ratio <= 0.1) ratio=0.1; /* if the ratio were much lower than this, we would get silly prices */
869 return ratio;
870 }
871
872 /*returns the greed of the shop on map, or 1 if it isn't specified. */
873 static double shop_greed(const mapstruct *map) {
874 double greed=1.0;
875 if (map->shopgreed)
876 return map->shopgreed;
877 return greed;
878 }
879
880 /* Returns a double based on how much the shopkeeper approves of the player.
881 * this is based on the race of the shopkeeper and that of the player.
882 */
883 double shopkeeper_approval(const mapstruct *map, const object *player) {
884 double approval=1.0;
885
886 if (map->shoprace) {
887 approval=NEUTRAL_RATIO;
888 if (player->race && !strcmp(player->race, map->shoprace)) approval = 1.0;
889 }
890 return approval;
891 }
892
893 /* limit the value of items based on the wealth of the shop. If the item is close
894 * to the maximum value a shop will offer, we start to reduce it, if the item is
895 * below the minimum value the shop is prepared to trade in, then we don't
896 * want it and offer nothing. If it isn't a shop, check whether we should do generic
897 * value reduction.
898 *
899 */
900 static uint64 value_limit(uint64 val, int quantity, const object *who, int isshop) {
901 uint64 newval, unit_price;
902 mapstruct *map;
903 unit_price=val/quantity;
904 if (!isshop || !who) {
905 if (unit_price > 10000)
906 newval=8000+isqrt(unit_price)*20;
907 else
908 newval=unit_price;
909 } else {
910 if (!who->map) {
911 LOG(llevError, "value_limit: asked shop price for ob %s on NULL map\n", who->name);
912 return val;
913 }
914 map=who->map;
915 if (map->shopmin && unit_price < map->shopmin) return 0;
916 else if (map->shopmax && unit_price > map->shopmax/2)
917 newval=MIN((map->shopmax/2)+isqrt(unit_price-map->shopmax/2), map->shopmax);
918 else if (unit_price>10000)
919 newval=8000+isqrt(unit_price)*20;
920 else
921 newval=unit_price;
922 }
923 newval *= quantity;
924 return newval;
925 }
926
927 /* gives a desciption of the shop on their current map to the player op. */
928 int describe_shop(const object *op) {
929 mapstruct *map = op->map;
930 /*shopitems *items=map->shopitems;*/
931 int pos=0, i;
932 double opinion=0;
933 char tmp[MAX_BUF]="\0";
934 if (op->type != PLAYER) return 0;
935
936 /*check if there is a shop specified for this map */
937 if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax) {
938 new_draw_info(NDI_UNIQUE,0,op,"From looking at the nearby shop you determine that it trades in:");
939 if (map->shopitems) {
940 for (i=0; i < map->shopitems[0].index; i++) {
941 if (map->shopitems[i].name && map->shopitems[i].strength > 10) {
942 snprintf(tmp+pos, sizeof(tmp)-pos, "%s, ", map->shopitems[i].name_pl);
943 pos += strlen(tmp+pos);
944 }
945 }
946 }
947 if (!pos) strcat(tmp, "a little of everything.");
948
949 /* format the string into a list */
950 make_list_like(tmp);
951 new_draw_info_format(NDI_UNIQUE, 0, op, "%s", tmp);
952
953 if (map->shopmax)
954 new_draw_info_format(NDI_UNIQUE,0,op,"It won't trade for items above %s.",
955 cost_string_from_value(map->shopmax));
956 if (map->shopmin)
957 new_draw_info_format(NDI_UNIQUE,0,op,"It won't trade in items worth less than %s.",
958 cost_string_from_value(map->shopmin));
959 if (map->shopgreed) {
960 if (map->shopgreed >2.0)
961 new_draw_info(NDI_UNIQUE,0,op,"It tends to overcharge massively.");
962 else if (map->shopgreed >1.5)
963 new_draw_info(NDI_UNIQUE,0,op,"It tends to overcharge substantially.");
964 else if (map->shopgreed >1.1)
965 new_draw_info(NDI_UNIQUE,0,op,"It tends to overcharge slightly.");
966 else if (map->shopgreed <0.9)
967 new_draw_info(NDI_UNIQUE,0,op,"It tends to undercharge.");
968 }
969 if (map->shoprace) {
970 opinion=shopkeeper_approval(map, op);
971 if (opinion > 0.8)
972 new_draw_info(NDI_UNIQUE,0,op,"You think the shopkeeper likes you.");
973 else if (opinion > 0.5)
974 new_draw_info(NDI_UNIQUE,0,op,"The shopkeeper seems unconcerned by you.");
975 else
976 new_draw_info(NDI_UNIQUE,0,op,"The shopkeeper seems to have taken a dislike to you.");
977 }
978 }
979 else new_draw_info(NDI_UNIQUE,0,op,"There is no shop nearby.");
980
981 return 1;
982 }
983 typedef struct shopinv {
984 char *item_sort;
985 char *item_real;
986 uint16 type;
987 uint32 nrof;
988 } shopinv;
989
990 /* There are a lot fo extra casts in here just to suppress warnings - it
991 * makes it look uglier than it really it.
992 * The format of the strings we get is type:name. So we first want to
993 * sort by type (numerical) - if the same type, then sort by name.
994 */
995 static int shop_sort(const void *a1, const void *a2)
996 {
997 shopinv *s1 = (shopinv*)a1, *s2= (shopinv*)a2;
998
999 if (s1->type<s2->type) return -1;
1000 if (s1->type>s2->type) return 1;
1001 /* the type is the same (what atoi gets), so do a strcasecmp to sort
1002 * via alphabetical order
1003 */
1004 return strcasecmp(s1->item_sort, s2->item_sort);
1005 }
1006
1007 static void add_shop_item(object *tmp, shopinv *items, int *numitems, int *numallocated)
1008 {
1009 #if 0
1010 char buf[MAX_BUF];
1011 #endif
1012 /* clear unpaid flag so that doesn't come up in query
1013 * string. We clear nrof so that we can better sort
1014 * the object names.
1015 */
1016
1017 CLEAR_FLAG(tmp, FLAG_UNPAID);
1018 items[*numitems].nrof=tmp->nrof;
1019 /* Non mergable items have nrof of 0, but count them as one
1020 * so the display is properly.
1021 */
1022 if (tmp->nrof == 0) items[*numitems].nrof++;
1023 items[*numitems].type=tmp->type;
1024
1025 switch (tmp->type) {
1026 #if 0
1027 case BOOTS:
1028 case GLOVES:
1029 case RING:
1030 case AMULET:
1031 case BRACERS:
1032 case GIRDLE:
1033 sprintf(buf,"%s %s",query_base_name(tmp,0),describe_item(tmp, NULL));
1034 items[*numitems].item_sort = strdup_local(buf);
1035 sprintf(buf,"%s %s",query_name(tmp),describe_item(tmp, NULL));
1036 items[*numitems].item_real = strdup_local(buf);
1037 (*numitems)++;
1038 break;
1039 #endif
1040
1041 default:
1042 items[*numitems].item_sort = strdup_local(query_base_name(tmp, 0));
1043 items[*numitems].item_real = strdup_local(query_base_name(tmp, 1));
1044 (*numitems)++;
1045 break;
1046 }
1047 SET_FLAG(tmp, FLAG_UNPAID);
1048 }
1049
1050 void shop_listing(object *op)
1051 {
1052 int i,j,numitems=0,numallocated=0, nx, ny;
1053 char *map_mark = (char *) calloc(MAGIC_MAP_SIZE * MAGIC_MAP_SIZE,1);
1054 object *stack;
1055 shopinv *items;
1056
1057 /* Should never happen, but just in case a monster does apply a sign */
1058 if (op->type!=PLAYER) return;
1059
1060 new_draw_info(NDI_UNIQUE, 0, op, "\nThe shop contains:");
1061
1062 magic_mapping_mark(op, map_mark, 3);
1063 items=malloc(40*sizeof(shopinv));
1064 numallocated=40;
1065
1066 /* Find all the appropriate items */
1067 for (i=0; i<MAP_WIDTH(op->map); i++) {
1068 for (j=0; j<MAP_HEIGHT(op->map); j++) {
1069 /* magic map code now centers the map on the object at MAGIC_MAP_HALF.
1070 *
1071 */
1072 nx = i - op->x + MAGIC_MAP_HALF;
1073 ny = j - op->y + MAGIC_MAP_HALF;
1074 /* unlikely, but really big shops could run into this issue */
1075 if (nx < 0 || ny < 0 || nx > MAGIC_MAP_SIZE || ny > MAGIC_MAP_SIZE) continue;
1076
1077 if (map_mark[nx + MAGIC_MAP_SIZE * ny] & FACE_FLOOR) {
1078 stack =get_map_ob(op->map,i,j);
1079
1080 while (stack) {
1081 if (QUERY_FLAG(stack, FLAG_UNPAID)) {
1082 if (numitems==numallocated) {
1083 items=realloc(items, sizeof(shopinv)*(numallocated+10));
1084 numallocated+=10;
1085 }
1086 add_shop_item(stack, items, &numitems, &numallocated);
1087 }
1088 stack = stack->above;
1089 }
1090 }
1091 }
1092 }
1093 free(map_mark);
1094 if (numitems == 0) {
1095 new_draw_info(NDI_UNIQUE, 0, op, "The shop is currently empty.\n");
1096 free(items);
1097 return;
1098 }
1099 qsort(items, numitems, sizeof(shopinv), (int (*)(const void*, const void*))shop_sort);
1100
1101 for (i=0; i<numitems; i++) {
1102 /* Collapse items of the same name together */
1103 if ((i+1)<numitems && !strcmp(items[i].item_real, items[i+1].item_real)) {
1104 items[i+1].nrof += items[i].nrof;
1105 free(items[i].item_sort);
1106 free(items[i].item_real);
1107 } else {
1108 new_draw_info_format(NDI_UNIQUE, 0, op, "%d %s",
1109 items[i].nrof? items[i].nrof:1,
1110 items[i].nrof==1?items[i].item_sort: items[i].item_real);
1111 free(items[i].item_sort);
1112 free(items[i].item_real);
1113 }
1114 }
1115 free(items);
1116 }