ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.c
Revision: 1.1.1.2 (vendor branch)
Committed: Wed Feb 22 18:03:24 2006 UTC (18 years, 3 months ago) by elmex
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_02_22
Changes since 1.1.1.1: +18 -16 lines
Log Message:
cvs -z7 -d:ext:elmex@cvs.schmorp.de:/schmorpforge import cf.schmorp.de UPSTREAM UPSTREAM_2006_02_22

File Contents

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