1 | |
|
|
2 | /* |
1 | /* |
3 | * static char *rcsid_shop_c = |
|
|
4 | * "$Id: shop.C,v 1.13 2006/09/12 01:09:54 root Exp $"; |
|
|
5 | */ |
|
|
6 | |
|
|
7 | /* |
|
|
8 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game |
9 | |
3 | * |
|
|
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
10 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
11 | Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
12 | |
7 | * |
13 | This program is free software; you can redistribute it and/or modify |
8 | * This program is free software; you can redistribute it and/or modify |
14 | it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
15 | the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
16 | (at your option) any later version. |
11 | * (at your option) any later version. |
17 | |
12 | * |
18 | This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
22 | |
17 | * |
23 | You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License |
24 | along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
26 | |
21 | * |
27 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
28 | */ |
23 | */ |
29 | |
24 | |
30 | #include <global.h> |
25 | #include <global.h> |
31 | #include <spells.h> |
26 | #include <spells.h> |
32 | #include <skills.h> |
27 | #include <skills.h> |
33 | #include <living.h> |
28 | #include <living.h> |
34 | #include <newclient.h> |
|
|
35 | #ifndef __CEXTRACT__ |
|
|
36 | # include <sproto.h> |
29 | #include <sproto.h> |
37 | #endif |
|
|
38 | #include <math.h> |
30 | #include <math.h> |
39 | |
31 | |
40 | /* this is a measure of how effective store specialisation is. A general store |
32 | /* this is a measure of how effective store specialisation is. A general store |
41 | * will offer this proportion of the 'maximum' price, a specialised store will |
33 | * will offer this proportion of the 'maximum' price, a specialised store will |
42 | * offer a range of prices around it such that the maximum price is always one |
34 | * offer a range of prices around it such that the maximum price is always one |
… | |
… | |
49 | /* price a shopkeeper will give someone they neither like nor dislike */ |
41 | /* price a shopkeeper will give someone they neither like nor dislike */ |
50 | #define NEUTRAL_RATIO 0.8 |
42 | #define NEUTRAL_RATIO 0.8 |
51 | |
43 | |
52 | static void pay_from_container (object *pl, object *pouch, sint64 &to_pay); |
44 | static void pay_from_container (object *pl, object *pouch, sint64 &to_pay); |
53 | static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop); |
45 | static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop); |
54 | static double shop_specialisation_ratio (const object *item, const mapstruct *map); |
46 | static double shop_specialisation_ratio (const object *item, const maptile *map); |
55 | static double shop_greed (const mapstruct *map); |
47 | static double shop_greed (const maptile *map); |
56 | |
48 | |
57 | #define NUM_COINS 4 /* number of coin types */ |
49 | #define NUM_COINS 4 /* number of coin types */ |
58 | static const char *const coins[] = { "royalty", "platinacoin", "goldcoin", "silvercoin", NULL }; |
50 | static const char *const coins[] = { "royalty", "platinacoin", "goldcoin", "silvercoin", NULL }; |
59 | |
51 | |
60 | /* Added F_TRUE flag to define.h to mean that the price should not |
52 | /* Added F_TRUE flag to define.h to mean that the price should not |
… | |
… | |
104 | approximate = flag & F_APPROX; |
96 | approximate = flag & F_APPROX; |
105 | shop = flag & F_SHOP; |
97 | shop = flag & F_SHOP; |
106 | flag &= ~(F_NO_BARGAIN | F_IDENTIFIED | F_NOT_CURSED | F_APPROX | F_SHOP); |
98 | flag &= ~(F_NO_BARGAIN | F_IDENTIFIED | F_NOT_CURSED | F_APPROX | F_SHOP); |
107 | |
99 | |
108 | if (tmp->type == MONEY) |
100 | if (tmp->type == MONEY) |
109 | return (tmp->nrof * tmp->value); |
101 | return tmp->nrof * tmp->value; |
|
|
102 | |
110 | if (tmp->type == GEM) |
103 | if (tmp->type == GEM) |
111 | { |
104 | { |
112 | if (flag == F_TRUE) |
105 | if (flag == F_TRUE) |
113 | return (tmp->nrof * tmp->value); |
106 | return (tmp->nrof * tmp->value); |
|
|
107 | |
114 | if (flag == F_BUY) |
108 | if (flag == F_BUY) |
115 | return (sint64) (1.03 * tmp->nrof * tmp->value); |
109 | return (sint64) (1.03 * tmp->nrof * tmp->value); |
|
|
110 | |
116 | if (flag == F_SELL) |
111 | if (flag == F_SELL) |
117 | return (sint64) (0.97 * tmp->nrof * tmp->value); |
112 | return (sint64) (0.97 * tmp->nrof * tmp->value); |
|
|
113 | |
118 | LOG (llevError, "Query_cost: Gem type with unknown flag : %d\n", flag); |
114 | LOG (llevError, "Query_cost: Gem type with unknown flag %d: %s\n", flag, tmp->debug_desc ()); |
119 | return 0; |
115 | return 0; |
120 | } |
116 | } |
|
|
117 | |
121 | number = tmp->nrof; |
118 | number = tmp->nrof; |
122 | if (number == 0) |
119 | if (number == 0) |
123 | number = 1; |
120 | number = 1; |
124 | if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified) |
121 | if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified) |
125 | { |
122 | { |
… | |
… | |
224 | const typedata *tmptype; |
221 | const typedata *tmptype; |
225 | |
222 | |
226 | tmptype = get_typedata (tmp->type); |
223 | tmptype = get_typedata (tmp->type); |
227 | |
224 | |
228 | if (find_skill_by_number (who, SK_BARGAINING)) |
225 | if (find_skill_by_number (who, SK_BARGAINING)) |
229 | { |
|
|
230 | lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; |
226 | lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; |
231 | } |
227 | |
232 | if (tmptype) |
228 | if (tmptype) |
233 | { |
229 | { |
234 | idskill1 = tmptype->identifyskill; |
230 | idskill1 = tmptype->identifyskill; |
|
|
231 | |
235 | if (idskill1) |
232 | if (idskill1) |
236 | { |
233 | { |
237 | idskill2 = tmptype->identifyskill2; |
234 | idskill2 = tmptype->identifyskill2; |
|
|
235 | |
238 | if (find_skill_by_number (who, idskill1)) |
236 | if (find_skill_by_number (who, idskill1)) |
239 | { |
|
|
240 | lev_identify = find_skill_by_number (who, idskill1)->level; |
237 | lev_identify = find_skill_by_number (who, idskill1)->level; |
241 | } |
238 | |
242 | if (idskill2 && find_skill_by_number (who, idskill2)) |
239 | if (idskill2 && find_skill_by_number (who, idskill2)) |
243 | { |
|
|
244 | lev_identify += find_skill_by_number (who, idskill2)->level; |
240 | lev_identify += find_skill_by_number (who, idskill2)->level; |
245 | } |
|
|
246 | } |
241 | } |
247 | } |
242 | } |
248 | else |
243 | else |
249 | LOG (llevError, "Query_cost: item %s hasn't got a valid type\n", &tmp->name); |
244 | LOG (llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->debug_desc ()); |
250 | |
245 | |
251 | /* ratio determines how much of the price modification |
246 | /* ratio determines how much of the price modification |
252 | * will come from the basic stat charisma |
247 | * will come from the basic stat charisma |
253 | * the rest will come from the level in bargaining skill |
248 | * the rest will come from the level in bargaining skill |
254 | */ |
249 | */ |
255 | const double cha_ratio = 0.40; |
250 | const double cha_ratio = 0.40; |
256 | |
251 | |
257 | diff = no_bargain ? 1.0 : 1. - pow (lev_bargain / (double) settings.max_level, 0.25); |
252 | diff = no_bargain ? 1.0 : 1. - pow (lev_bargain / (double) settings.max_level, 0.25); |
258 | |
|
|
259 | diff = (1. - cha_ratio) * diff + cha_ratio * (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.); |
253 | diff = (1. - cha_ratio) * diff + cha_ratio * (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.); |
260 | |
|
|
261 | diff = .02 + (.80 - .02) * diff; |
254 | diff = .02 + (.80 - .02) * diff; |
262 | |
255 | |
263 | if (flag == F_BUY) |
256 | if (flag == F_BUY) |
264 | val += (val * diff); |
257 | val += val * diff; |
265 | else if (flag == F_SELL) |
258 | else if (flag == F_SELL) |
266 | val -= (val * diff); |
259 | val -= val * diff; |
267 | |
260 | |
268 | // now find a price range. the less good we can judge, the larger the range is |
261 | // now find a price range. the less good we can judge, the larger the range is |
269 | // then the range is adjusted randomly around the correct value |
262 | // then the range is adjusted randomly around the correct value |
270 | if (approximate) |
263 | if (approximate) |
271 | approx_range = sint64 (val / sqrt (lev_identify * 3 + 1)); |
264 | approx_range = sint64 (val / sqrt (lev_identify * 3 + 1)); |
272 | } |
265 | } |
273 | |
266 | |
274 | /* I don't think this should really happen - if it does, it indicates and |
267 | /* I don't think this should really happen - if it does, it indicates and |
275 | * overflow of diff above. That shoudl only happen if |
268 | * overflow of diff above. That should only happen if |
276 | * we are selling objects - in that case, the person just |
269 | * we are selling objects - in that case, the person just |
277 | * gets no money. |
270 | * gets no money. |
278 | */ |
271 | */ |
279 | if ((sint64) val < 0) |
272 | if ((sint64) val < 0) |
280 | val = 0; |
273 | val = 0; |
… | |
… | |
308 | if (QUERY_FLAG (tmp, FLAG_PLAYER_SOLD)) |
301 | if (QUERY_FLAG (tmp, FLAG_PLAYER_SOLD)) |
309 | val = (val * shop_greed (who->map) * shop_specialisation_ratio (tmp, who->map) / shopkeeper_approval (who->map, who)); |
302 | val = (val * shop_greed (who->map) * shop_specialisation_ratio (tmp, who->map) / shopkeeper_approval (who->map, who)); |
310 | else |
303 | else |
311 | val = (val * shop_greed (who->map) / (shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who))); |
304 | val = (val * shop_greed (who->map) / (shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who))); |
312 | } |
305 | } |
|
|
306 | |
313 | /* we will also have an extra 0-5% variation between shops of the same type |
307 | /* we will also have an extra 0-5% variation between shops of the same type |
314 | * for valuable items (below a value of 50 this effect wouldn't be very |
308 | * for valuable items (below a value of 50 this effect wouldn't be very |
315 | * pointful, and could give fun with rounding. |
309 | * pointful, and could give fun with rounding. |
316 | */ |
310 | */ |
317 | if (who->map->path != NULL && val > 50) |
311 | //TODO: why use cosf at all, just % and scale linearly, gives more even distribution |
318 | val = (val + 0.05 * (sint64) val * cos (tmp->count + strlen (who->map->path))); |
312 | if (val > 50) |
|
|
313 | val += float (val) * .05f * cosf ((tmp->uuid.seq & 0xffff) * float (M_PI * 2. / 0x10000)); |
319 | } |
314 | } |
|
|
315 | |
320 | return (sint64) val; |
316 | return (sint64) val; |
321 | } |
317 | } |
322 | |
318 | |
323 | /* Find the coin type that is worth more the 'c'. Starts at the |
319 | /* Find the coin type that is worth more the 'c'. Starts at the |
324 | * cointype placement. |
320 | * cointype placement. |
… | |
… | |
331 | |
327 | |
332 | do |
328 | do |
333 | { |
329 | { |
334 | if (coins[*cointype] == NULL) |
330 | if (coins[*cointype] == NULL) |
335 | return NULL; |
331 | return NULL; |
336 | coin = find_archetype (coins[*cointype]); |
332 | coin = archetype::find (coins[*cointype]); |
337 | if (coin == NULL) |
333 | if (coin == NULL) |
338 | return NULL; |
334 | return NULL; |
339 | *cointype += 1; |
335 | *cointype += 1; |
340 | } |
336 | } |
341 | while (coin->clone.value > c); |
337 | while (coin->clone.value > c); |
… | |
… | |
508 | |
504 | |
509 | for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) |
505 | for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) |
510 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) |
506 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) |
511 | pay_from_container (pl, pouch, to_pay); |
507 | pay_from_container (pl, pouch, to_pay); |
512 | |
508 | |
513 | fix_player (pl); |
509 | pl->update_stats (); |
514 | return 1; |
510 | return 1; |
515 | } |
511 | } |
516 | |
512 | |
517 | /* DAMN: This is now a wrapper for pay_from_container, which is |
513 | /* DAMN: This is now a wrapper for pay_from_container, which is |
518 | * called for the player, then for each active container that can hold |
514 | * called for the player, then for each active container that can hold |
… | |
… | |
545 | |
541 | |
546 | for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) |
542 | for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) |
547 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) |
543 | if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) |
548 | pay_from_container (pl, pouch, to_pay); |
544 | pay_from_container (pl, pouch, to_pay); |
549 | |
545 | |
550 | if (settings.real_wiz == FALSE && QUERY_FLAG (pl, FLAG_WAS_WIZ)) |
546 | pl->update_stats (); |
551 | SET_FLAG (op, FLAG_WAS_WIZ); |
|
|
552 | |
547 | |
553 | fix_player (pl); |
|
|
554 | return 1; |
548 | return 1; |
555 | } |
549 | } |
556 | |
550 | |
557 | /* This pays for the item, and takes the proper amount of money off |
551 | /* This pays for the item, and takes the proper amount of money off |
558 | * the player. |
552 | * the player. |
… | |
… | |
593 | { |
587 | { |
594 | // This should not happen, but if it does, just merge the two. |
588 | // This should not happen, but if it does, just merge the two. |
595 | if (coin_objs [i]) |
589 | if (coin_objs [i]) |
596 | { |
590 | { |
597 | LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); |
591 | LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); |
598 | remove_ob (tmp); |
592 | tmp->remove (); |
599 | coin_objs[i]->nrof += tmp->nrof; |
593 | coin_objs[i]->nrof += tmp->nrof; |
600 | esrv_del_item (pl->contr, tmp->count); |
594 | esrv_del_item (pl->contr, tmp->count); |
601 | free_object (tmp); |
595 | tmp->destroy (); |
602 | } |
596 | } |
603 | else |
597 | else |
604 | { |
598 | { |
605 | remove_ob (tmp); |
599 | tmp->remove (); |
606 | |
600 | |
607 | if (pouch->type == PLAYER) |
601 | if (pouch->type == PLAYER) |
608 | esrv_del_item (pl->contr, tmp->count); |
602 | esrv_del_item (pl->contr, tmp->count); |
609 | |
603 | |
610 | coin_objs[i] = tmp; |
604 | coin_objs[i] = tmp; |
… | |
… | |
622 | /* Fill in any gaps in the coin_objs array - needed to make change. */ |
616 | /* Fill in any gaps in the coin_objs array - needed to make change. */ |
623 | /* Note that the coin_objs array goes from least value to greatest value */ |
617 | /* Note that the coin_objs array goes from least value to greatest value */ |
624 | for (i = 0; i < NUM_COINS; i++) |
618 | for (i = 0; i < NUM_COINS; i++) |
625 | if (!coin_objs[i]) |
619 | if (!coin_objs[i]) |
626 | { |
620 | { |
627 | at = find_archetype (coins[NUM_COINS - 1 - i]); |
621 | at = archetype::find (coins[NUM_COINS - 1 - i]); |
628 | |
622 | |
629 | if (at == NULL) |
623 | if (at == NULL) |
630 | LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]); |
624 | LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]); |
631 | |
625 | |
632 | coin_objs[i] = arch_to_object (at); |
626 | coin_objs[i] = arch_to_object (at); |
… | |
… | |
669 | |
663 | |
670 | if (pl->type != PLAYER) |
664 | if (pl->type != PLAYER) |
671 | esrv_send_item (pl, pl); |
665 | esrv_send_item (pl, pl); |
672 | } |
666 | } |
673 | else |
667 | else |
674 | free_object (coin_objs[i]); |
668 | coin_objs[i]->destroy (); |
675 | } |
669 | } |
676 | } |
670 | } |
677 | |
671 | |
678 | /* Checks all unpaid items in op's inventory, adds up all the money they |
672 | /* Checks all unpaid items in op's inventory, adds up all the money they |
679 | * have, and checks that they can actually afford what they want to buy. |
673 | * have, and checks that they can actually afford what they want to buy. |
… | |
… | |
684 | can_pay (object *pl) |
678 | can_pay (object *pl) |
685 | { |
679 | { |
686 | int unpaid_count = 0; |
680 | int unpaid_count = 0; |
687 | sint64 unpaid_price = 0; |
681 | sint64 unpaid_price = 0; |
688 | sint64 player_wealth = query_money (pl); |
682 | sint64 player_wealth = query_money (pl); |
689 | object *item; |
|
|
690 | |
683 | |
691 | if (!pl || pl->type != PLAYER) |
684 | if (!pl || pl->type != PLAYER) |
692 | { |
685 | { |
693 | LOG (llevError, "can_pay(): called against something that isn't a player\n"); |
686 | LOG (llevError, "can_pay(): called against something that isn't a player\n"); |
694 | return 0; |
687 | return 0; |
… | |
… | |
750 | return 0; |
743 | return 0; |
751 | } |
744 | } |
752 | else |
745 | else |
753 | { |
746 | { |
754 | object *tmp; |
747 | object *tmp; |
755 | tag_t c = op->count; |
|
|
756 | |
748 | |
757 | CLEAR_FLAG (op, FLAG_UNPAID); |
749 | CLEAR_FLAG (op, FLAG_UNPAID); |
758 | CLEAR_FLAG (op, FLAG_PLAYER_SOLD); |
750 | CLEAR_FLAG (op, FLAG_PLAYER_SOLD); |
759 | new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op)); |
751 | new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op)); |
760 | tmp = merge_ob (op, NULL); |
752 | tmp = merge_ob (op, NULL); |
761 | |
753 | |
762 | if (pl->type == PLAYER) |
754 | if (pl->type == PLAYER) |
763 | { |
755 | { |
764 | if (tmp) |
756 | if (tmp) |
765 | { /* it was merged */ |
757 | { /* it was merged */ |
766 | esrv_del_item (pl->contr, c); |
758 | esrv_del_item (pl->contr, op->count); |
767 | op = tmp; |
759 | op = tmp; |
768 | } |
760 | } |
769 | |
761 | |
770 | esrv_send_item (pl, op); |
762 | esrv_send_item (pl, op); |
771 | } |
763 | } |
… | |
… | |
797 | archetype *at = 0; |
789 | archetype *at = 0; |
798 | object *pouch = 0, *tmp = 0; |
790 | object *pouch = 0, *tmp = 0; |
799 | |
791 | |
800 | for (count = 0; coins[count] != NULL; count++) |
792 | for (count = 0; coins[count] != NULL; count++) |
801 | { |
793 | { |
802 | at = find_archetype (coins[count]); |
794 | at = archetype::find (coins[count]); |
803 | |
795 | |
804 | if (at == NULL) |
796 | if (at == NULL) |
805 | LOG (llevError, "Could not find %s archetype\n", coins[count]); |
797 | LOG (llevError, "Could not find %s archetype\n", coins[count]); |
806 | else if ((amount / at->clone.value) > 0) |
798 | else if ((amount / at->clone.value) > 0) |
807 | { |
799 | { |
… | |
… | |
843 | } |
835 | } |
844 | } |
836 | } |
845 | } |
837 | } |
846 | |
838 | |
847 | if (amount != 0) |
839 | if (amount != 0) |
848 | #ifndef WIN32 |
|
|
849 | LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount); |
840 | LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount); |
850 | #else |
|
|
851 | LOG (llevError, "Warning - payment in pay_player () not zero: %I64u\n", amount); |
|
|
852 | #endif |
|
|
853 | } |
841 | } |
854 | |
842 | |
855 | /* elmex: this is for the bank plugin :( */ |
843 | /* elmex: this is for the bank plugin :( */ |
856 | sint64 |
844 | sint64 |
857 | pay_player_arch (object *pl, const char *arch, sint64 amount) |
845 | pay_player_arch (object *pl, const char *arch, sint64 amount) |
858 | { |
846 | { |
859 | archetype *at = find_archetype (arch); |
847 | archetype *at = archetype::find (arch); |
860 | object *tmp = NULL; |
848 | object *tmp = NULL; |
861 | |
849 | |
862 | if (at == NULL) |
850 | if (at == NULL) |
863 | return 0; |
851 | return 0; |
864 | |
852 | |
… | |
… | |
934 | * item based on the shops specialisation. Does not take account of greed, |
922 | * item based on the shops specialisation. Does not take account of greed, |
935 | * returned value is between (2*SPECIALISATION_EFFECT-1) and 1 and in any |
923 | * returned value is between (2*SPECIALISATION_EFFECT-1) and 1 and in any |
936 | * event is never less than 0.1 (calling functions divide by it) |
924 | * event is never less than 0.1 (calling functions divide by it) |
937 | */ |
925 | */ |
938 | static double |
926 | static double |
939 | shop_specialisation_ratio (const object *item, const mapstruct *map) |
927 | shop_specialisation_ratio (const object *item, const maptile *map) |
940 | { |
928 | { |
941 | shopitems *items = map->shopitems; |
929 | shopitems *items = map->shopitems; |
942 | double ratio = SPECIALISATION_EFFECT, likedness = 0.001; |
930 | double ratio = SPECIALISATION_EFFECT, likedness = 0.001; |
943 | int i; |
931 | int i; |
944 | |
932 | |
945 | if (item == NULL) |
933 | if (item == NULL) |
946 | { |
934 | { |
947 | LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", map->path); |
935 | LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", &map->path); |
948 | return 0; |
936 | return 0; |
949 | } |
937 | } |
950 | |
938 | |
951 | if (!item->type) |
939 | if (!item->type) |
952 | { |
940 | { |
… | |
… | |
965 | likedness = items[i].strength / 100.0; |
953 | likedness = items[i].strength / 100.0; |
966 | } |
954 | } |
967 | |
955 | |
968 | if (likedness > 1.0) |
956 | if (likedness > 1.0) |
969 | { /* someone has been rather silly with the map headers. */ |
957 | { /* someone has been rather silly with the map headers. */ |
970 | LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, map->path); |
958 | LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, &map->path); |
971 | likedness = 1.0; |
959 | likedness = 1.0; |
972 | } |
960 | } |
973 | |
961 | |
974 | if (likedness < -1.0) |
962 | if (likedness < -1.0) |
975 | { |
963 | { |
976 | LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, map->path); |
964 | LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, &map->path); |
977 | likedness = -1.0; |
965 | likedness = -1.0; |
978 | } |
966 | } |
979 | |
967 | |
980 | ratio = ratio + (1.0 - ratio) * likedness; |
968 | ratio = ratio + (1.0 - ratio) * likedness; |
981 | |
969 | |
… | |
… | |
985 | return ratio; |
973 | return ratio; |
986 | } |
974 | } |
987 | |
975 | |
988 | /*returns the greed of the shop on map, or 1 if it isn't specified. */ |
976 | /*returns the greed of the shop on map, or 1 if it isn't specified. */ |
989 | static double |
977 | static double |
990 | shop_greed (const mapstruct *map) |
978 | shop_greed (const maptile *map) |
991 | { |
979 | { |
992 | double greed = 1.0; |
980 | double greed = 1.0; |
993 | |
981 | |
994 | if (map->shopgreed) |
982 | if (map->shopgreed) |
995 | return map->shopgreed; |
983 | return map->shopgreed; |
… | |
… | |
998 | |
986 | |
999 | /* Returns a double based on how much the shopkeeper approves of the player. |
987 | /* Returns a double based on how much the shopkeeper approves of the player. |
1000 | * this is based on the race of the shopkeeper and that of the player. |
988 | * this is based on the race of the shopkeeper and that of the player. |
1001 | */ |
989 | */ |
1002 | double |
990 | double |
1003 | shopkeeper_approval (const mapstruct *map, const object *player) |
991 | shopkeeper_approval (const maptile *map, const object *player) |
1004 | { |
992 | { |
1005 | double approval = 1.0; |
993 | double approval = 1.0; |
1006 | |
994 | |
1007 | if (map->shoprace) |
995 | if (map->shoprace) |
1008 | { |
996 | { |
… | |
… | |
1023 | */ |
1011 | */ |
1024 | static sint64 |
1012 | static sint64 |
1025 | value_limit (sint64 val, int quantity, const object *who, int isshop) |
1013 | value_limit (sint64 val, int quantity, const object *who, int isshop) |
1026 | { |
1014 | { |
1027 | sint64 newval, unit_price, tmpshopmax; |
1015 | sint64 newval, unit_price, tmpshopmax; |
1028 | mapstruct *map; |
1016 | maptile *map; |
1029 | |
1017 | |
1030 | unit_price = val / quantity; |
1018 | unit_price = val / quantity; |
1031 | |
1019 | |
1032 | if (!isshop || !who) |
1020 | if (!isshop || !who) |
1033 | { |
1021 | { |
… | |
… | |
1063 | |
1051 | |
1064 | /* gives a desciption of the shop on their current map to the player op. */ |
1052 | /* gives a desciption of the shop on their current map to the player op. */ |
1065 | int |
1053 | int |
1066 | describe_shop (const object *op) |
1054 | describe_shop (const object *op) |
1067 | { |
1055 | { |
1068 | mapstruct *map = op->map; |
1056 | maptile *map = op->map; |
1069 | |
1057 | |
1070 | /*shopitems *items=map->shopitems; */ |
1058 | /*shopitems *items=map->shopitems; */ |
1071 | int pos = 0, i; |
1059 | int pos = 0, i; |
1072 | double opinion = 0; |
1060 | double opinion = 0; |
1073 | char tmp[MAX_BUF] = "\0"; |
1061 | char tmp[MAX_BUF] = "\0"; |
… | |
… | |
1187 | case RING: |
1175 | case RING: |
1188 | case AMULET: |
1176 | case AMULET: |
1189 | case BRACERS: |
1177 | case BRACERS: |
1190 | case GIRDLE: |
1178 | case GIRDLE: |
1191 | sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL)); |
1179 | sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL)); |
1192 | items[*numitems].item_sort = strdup_local (buf); |
1180 | items[*numitems].item_sort = strdup (buf); |
1193 | sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL)); |
1181 | sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL)); |
1194 | items[*numitems].item_real = strdup_local (buf); |
1182 | items[*numitems].item_real = strdup (buf); |
1195 | (*numitems)++; |
1183 | (*numitems)++; |
1196 | break; |
1184 | break; |
1197 | #endif |
1185 | #endif |
1198 | |
1186 | |
1199 | default: |
1187 | default: |
1200 | items[*numitems].item_sort = strdup_local (query_base_name (tmp, 0)); |
1188 | items[*numitems].item_sort = strdup (query_base_name (tmp, 0)); |
1201 | items[*numitems].item_real = strdup_local (query_base_name (tmp, 1)); |
1189 | items[*numitems].item_real = strdup (query_base_name (tmp, 1)); |
1202 | (*numitems)++; |
1190 | (*numitems)++; |
1203 | break; |
1191 | break; |
1204 | } |
1192 | } |
1205 | SET_FLAG (tmp, FLAG_UNPAID); |
1193 | SET_FLAG (tmp, FLAG_UNPAID); |
1206 | } |
1194 | } |
1207 | |
1195 | |
1208 | void |
1196 | void |
1209 | shop_listing (object *op) |
1197 | shop_listing (object *sign, object *op) |
1210 | { |
1198 | { |
1211 | int i, j, numitems = 0, numallocated = 0, nx, ny; |
1199 | int i, j, numitems = 0, numallocated = 0, x1, x2, y1, y2; |
1212 | char *map_mark = (char *) calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1); |
1200 | const char *shop_coords = get_ob_key_value (sign, "shop_coords"); |
1213 | object *stack; |
1201 | object *stack; |
1214 | shopinv *items; |
1202 | shopinv *items; |
1215 | |
1203 | |
1216 | /* Should never happen, but just in case a monster does apply a sign */ |
1204 | /* Should never happen, but just in case a monster does apply a sign */ |
1217 | if (op->type != PLAYER) |
1205 | if (op->type != PLAYER) |
1218 | return; |
1206 | return; |
1219 | |
1207 | |
1220 | new_draw_info (NDI_UNIQUE, 0, op, "\nThe shop contains:"); |
1208 | if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) |
|
|
1209 | { |
|
|
1210 | x1 = 0; |
|
|
1211 | y1 = 0; |
|
|
1212 | x2 = op->map->width - 1; |
|
|
1213 | y2 = op->map->height - 1; |
|
|
1214 | } |
1221 | |
1215 | |
1222 | magic_mapping_mark (op, map_mark, 3); |
|
|
1223 | items = (shopinv *) malloc (40 * sizeof (shopinv)); |
1216 | items = (shopinv *) malloc (40 * sizeof (shopinv)); |
1224 | numallocated = 40; |
1217 | numallocated = 40; |
1225 | |
1218 | |
1226 | /* Find all the appropriate items */ |
1219 | /* Find all the appropriate items */ |
1227 | for (i = 0; i < MAP_WIDTH (op->map); i++) |
1220 | for (i = x1; i <= x2; i++) |
1228 | { |
1221 | { |
1229 | for (j = 0; j < MAP_HEIGHT (op->map); j++) |
1222 | for (j = y1; j < y2; j++) |
1230 | { |
|
|
1231 | /* magic map code now centers the map on the object at MAGIC_MAP_HALF. |
|
|
1232 | * |
1223 | { |
1233 | */ |
1224 | if (is_in_shop (op->map, i, j)) |
1234 | nx = i - op->x + MAGIC_MAP_HALF; |
|
|
1235 | ny = j - op->y + MAGIC_MAP_HALF; |
|
|
1236 | /* unlikely, but really big shops could run into this issue */ |
|
|
1237 | if (nx < 0 || ny < 0 || nx > MAGIC_MAP_SIZE || ny > MAGIC_MAP_SIZE) |
|
|
1238 | continue; |
|
|
1239 | |
|
|
1240 | if (map_mark[nx + MAGIC_MAP_SIZE * ny] & FACE_FLOOR) |
|
|
1241 | { |
1225 | { |
1242 | stack = get_map_ob (op->map, i, j); |
1226 | stack = GET_MAP_OB (op->map, i, j); |
1243 | |
1227 | |
1244 | while (stack) |
1228 | while (stack) |
1245 | { |
1229 | { |
1246 | if (QUERY_FLAG (stack, FLAG_UNPAID)) |
1230 | if (QUERY_FLAG (stack, FLAG_UNPAID)) |
1247 | { |
1231 | { |
1248 | if (numitems == numallocated) |
1232 | if (numitems == numallocated) |
1249 | { |
1233 | { |
1250 | items = (shopinv *) realloc (items, sizeof (shopinv) * (numallocated + 10)); |
1234 | items = (shopinv *) realloc (items, sizeof (shopinv) * (numallocated + 10)); |
1251 | numallocated += 10; |
1235 | numallocated += 10; |
1252 | } |
1236 | } |
|
|
1237 | |
1253 | add_shop_item (stack, items, &numitems, &numallocated); |
1238 | add_shop_item (stack, items, &numitems, &numallocated); |
1254 | } |
1239 | } |
|
|
1240 | |
1255 | stack = stack->above; |
1241 | stack = stack->above; |
1256 | } |
1242 | } |
1257 | } |
1243 | } |
1258 | } |
1244 | } |
1259 | } |
1245 | } |
1260 | |
1246 | |
1261 | free (map_mark); |
|
|
1262 | |
|
|
1263 | if (numitems == 0) |
1247 | if (numitems == 0) |
1264 | { |
1248 | { |
1265 | new_draw_info (NDI_UNIQUE, 0, op, "The shop is currently empty.\n"); |
1249 | new_draw_info (NDI_UNIQUE, 0, op, "The shop is currently empty.\n"); |
1266 | free (items); |
1250 | free (items); |
1267 | return; |
1251 | return; |
1268 | } |
1252 | } |
1269 | |
1253 | |
1270 | qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); |
1254 | qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); |
|
|
1255 | |
|
|
1256 | new_draw_info (NDI_UNIQUE, 0, op, "\nThe shop contains:"); |
1271 | |
1257 | |
1272 | for (i = 0; i < numitems; i++) |
1258 | for (i = 0; i < numitems; i++) |
1273 | { |
1259 | { |
1274 | /* Collapse items of the same name together */ |
1260 | /* Collapse items of the same name together */ |
1275 | if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) |
1261 | if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) |
… | |
… | |
1284 | items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); |
1270 | items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); |
1285 | free (items[i].item_sort); |
1271 | free (items[i].item_sort); |
1286 | free (items[i].item_real); |
1272 | free (items[i].item_real); |
1287 | } |
1273 | } |
1288 | } |
1274 | } |
|
|
1275 | |
1289 | free (items); |
1276 | free (items); |
1290 | } |
1277 | } |
1291 | |
1278 | |
1292 | /* elmex: this function checks whether the object is in a shop */ |
1279 | /* elmex: this function checks whether the object is in a shop */ |
1293 | bool |
1280 | bool |
… | |
… | |
1299 | return is_in_shop (o->map, o->x, o->y); |
1286 | return is_in_shop (o->map, o->x, o->y); |
1300 | } |
1287 | } |
1301 | |
1288 | |
1302 | /* elmex: this function checks whether we are in a shop or not */ |
1289 | /* elmex: this function checks whether we are in a shop or not */ |
1303 | bool |
1290 | bool |
1304 | is_in_shop (mapstruct *map, int x, int y) |
1291 | is_in_shop (maptile *map, int x, int y) |
1305 | { |
1292 | { |
1306 | for (object *floor = get_map_ob (map, x, y); floor; floor = floor->above) |
1293 | for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above) |
1307 | if (floor->type == SHOP_FLOOR) |
1294 | if (floor->type == SHOP_FLOOR) |
1308 | return true; |
1295 | return true; |
1309 | |
1296 | |
1310 | return false; |
1297 | return false; |
1311 | } |
1298 | } |