ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/shop.C
(Generate patch)

Comparing deliantra/server/server/shop.C (file contents):
Revision 1.42 by root, Sat Jul 21 19:18:02 2007 UTC vs.
Revision 1.70 by root, Mon Oct 12 14:00:59 2009 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Crossfire TRT is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24#include <global.h> 25#include <global.h>
25#include <spells.h> 26#include <spells.h>
26#include <skills.h> 27#include <skills.h>
42 43
43static void pay_from_container (object *pl, object *pouch, sint64 &to_pay); 44static void pay_from_container (object *pl, object *pouch, sint64 &to_pay);
44static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop); 45static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop);
45static double shop_specialisation_ratio (const object *item, const maptile *map); 46static double shop_specialisation_ratio (const object *item, const maptile *map);
46static double shop_greed (const maptile *map); 47static double shop_greed (const maptile *map);
47
48#define NUM_COINS 4 /* number of coin types */
49static const char *const coins[] = { "royalty", "platinacoin", "goldcoin", "silvercoin", NULL };
50 48
51/* Added F_TRUE flag to define.h to mean that the price should not 49/* Added F_TRUE flag to define.h to mean that the price should not
52 * be adjusted by players charisma. With F_TRUE, it returns the amount 50 * be adjusted by players charisma. With F_TRUE, it returns the amount
53 * that the item is worth, if it was sold, but unadjusted by charisma. 51 * that the item is worth, if it was sold, but unadjusted by charisma.
54 * This is needed for alchemy, to to determine what value of gold nuggets 52 * This is needed for alchemy, to to determine what value of gold nuggets
77 75
78sint64 76sint64
79query_cost (const object *tmp, object *who, int flag) 77query_cost (const object *tmp, object *who, int flag)
80{ 78{
81 double val; 79 double val;
82 int number; /* used to better calculate value */
83 int no_bargain; 80 int no_bargain;
84 int identified; 81 int identified;
85 int not_cursed; 82 int not_cursed;
86 int approximate; 83 int approximate;
87 int shop; 84 int shop;
112 109
113 LOG (llevError, "Query_cost: Gem type with unknown flag %d: %s\n", flag, tmp->debug_desc ()); 110 LOG (llevError, "Query_cost: Gem type with unknown flag %d: %s\n", flag, tmp->debug_desc ());
114 return 0; 111 return 0;
115 } 112 }
116 113
117 number = tmp->nrof; 114 int number = tmp->number_of ();
118 if (number == 0) 115
119 number = 1;
120 if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified) 116 if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified)
121 { 117 {
122 if (!not_cursed && (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))) 118 if (!not_cursed && (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED)))
123 return 0; 119 return 0;
124 else 120 else
125 val = tmp->value * number; 121 val = tmp->value * number;
126 } 122 }
127 /* This area deals with objects that are not identified, but can be */ 123 /* This area deals with objects that are not identified, but can be */
128 else 124 else
129 { 125 {
130 if (tmp->arch != NULL) 126 if (flag == F_BUY)
131 { 127 {
132 if (flag == F_BUY) 128 LOG (llevError | logBacktrace, "Asking for buy-value of unidentified object: %s\n", tmp->debug_desc ());
129 val = tmp->arch->value * 50 * number;
130 }
131 else
132 { /* Trying to sell something, or get true value */
133 if (tmp->type == POTION)
134 val = number * 40; /* Don't want to give anything away */
135 else
133 { 136 {
134 LOG (llevError, "Asking for buy-value of unidentified object.\n"); 137 /* Get 2/3'rd value for applied objects, 1/3'rd for totally
135 val = tmp->arch->value * 50 * number; 138 * unknown objects
139 */
140 if (QUERY_FLAG (tmp, FLAG_BEEN_APPLIED))
141 val = number * tmp->arch->value * 2 / 3;
142 else
143 val = number * tmp->arch->value / 3;
136 } 144 }
137 else
138 { /* Trying to sell something, or get true value */
139 if (tmp->type == POTION)
140 val = number * 40; /* Don't want to give anything away */
141 else
142 {
143 /* Get 2/3'rd value for applied objects, 1/3'rd for totally
144 * unknown objects
145 */
146 if (QUERY_FLAG (tmp, FLAG_BEEN_APPLIED))
147 val = number * tmp->arch->value * 2 / 3;
148 else
149 val = number * tmp->arch->value / 3;
150 }
151 }
152 }
153 else
154 { /* No archetype with this object */
155 LOG (llevDebug, "In sell item: Have object with no archetype: %s\n", &tmp->name);
156 if (flag == F_BUY)
157 {
158 LOG (llevError, "Asking for buy-value of unidentified object without arch.\n");
159 val = number * tmp->value * 10;
160 }
161 else
162 val = number * tmp->value / 5;
163 } 145 }
164 } 146 }
165 147
166 /* If the item has been applied or identifed or does not need to be 148 /* If the item has been applied or identifed or does not need to be
167 * identified, AND the object is magical and the archetype is non 149 * identified, AND the object is magical and the archetype is non
209 * AND Cha = 30 will get optimal price. 191 * AND Cha = 30 will get optimal price.
210 * Thus charisma will never get useless. 192 * Thus charisma will never get useless.
211 * -b.e. edler@heydernet.de 193 * -b.e. edler@heydernet.de
212 */ 194 */
213 195
214 if (who != NULL && who->type == PLAYER) 196 if (who && who->type == PLAYER)
215 { 197 {
216 int lev_bargain = 0; 198 int lev_bargain = 0;
217 int lev_identify = 0; 199 int lev_identify = 0;
218 int idskill1 = 0;
219 int idskill2 = 0;
220 const typedata *tmptype;
221
222 tmptype = get_typedata (tmp->type);
223 200
224 if (find_skill_by_number (who, SK_BARGAINING)) 201 if (find_skill_by_number (who, SK_BARGAINING))
225 lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; 202 lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level;
226 203
227 if (tmptype) 204 if (const typedata *tmptype = get_typedata (tmp->type))
228 { 205 {
229 idskill1 = tmptype->identifyskill; 206 if (int idskill1 = tmptype->identifyskill)
230
231 if (idskill1)
232 { 207 {
233 idskill2 = tmptype->identifyskill2; 208 int idskill2 = tmptype->identifyskill2;
234 209
235 if (find_skill_by_number (who, idskill1)) 210 if (find_skill_by_number (who, idskill1))
236 lev_identify = find_skill_by_number (who, idskill1)->level; 211 lev_identify = find_skill_by_number (who, idskill1)->level;
237 212
238 if (idskill2 && find_skill_by_number (who, idskill2)) 213 if (idskill2 && find_skill_by_number (who, idskill2))
239 lev_identify += find_skill_by_number (who, idskill2)->level; 214 lev_identify += find_skill_by_number (who, idskill2)->level;
240 } 215 }
241 } 216 }
242 else
243 LOG (llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->debug_desc ());
244 217
245 /* ratio determines how much of the price modification 218 /* ratio determines how much of the price modification
246 * will come from the basic stat charisma 219 * will come from the basic stat charisma
247 * the rest will come from the level in bargaining skill 220 * the rest will come from the level in bargaining skill
248 */ 221 */
421 394
422 if (coin == NULL) 395 if (coin == NULL)
423 return "nothing"; 396 return "nothing";
424 397
425 num = real_value / coin->value; 398 num = real_value / coin->value;
399
426 if (num == 1) 400 if (num == 1)
427 sprintf (buf, "about one %s", &coin->object::name); 401 sprintf (buf, "about one %s", &coin->object::name);
428 else if (num < 5) 402 else if (num < 5)
429 sprintf (buf, "a few %s", &coin->object::name_pl); 403 sprintf (buf, "a few %s", &coin->object::name_pl);
430 else if (num < 10) 404 else if (num < 10)
435 sprintf (buf, "lots of %s", &coin->object::name_pl); 409 sprintf (buf, "lots of %s", &coin->object::name_pl);
436 else if (num < 1000) 410 else if (num < 1000)
437 sprintf (buf, "a great many %s", &coin->object::name_pl); 411 sprintf (buf, "a great many %s", &coin->object::name_pl);
438 else 412 else
439 sprintf (buf, "a vast quantity of %s", &coin->object::name_pl); 413 sprintf (buf, "a vast quantity of %s", &coin->object::name_pl);
414
440 return buf; 415 return buf;
441 } 416 }
442 } 417 }
443 } 418 }
444 419
475 } 450 }
476 451
477 for (tmp = op->inv; tmp; tmp = tmp->below) 452 for (tmp = op->inv; tmp; tmp = tmp->below)
478 if (tmp->type == MONEY) 453 if (tmp->type == MONEY)
479 total += tmp->nrof * (sint64)tmp->value; 454 total += tmp->nrof * (sint64)tmp->value;
480 else if (tmp->type == CONTAINER && QUERY_FLAG (tmp, FLAG_APPLIED) && (tmp->race == NULL || strstr (tmp->race, "gold"))) 455 else if (tmp->type == CONTAINER && QUERY_FLAG (tmp, FLAG_APPLIED) && (!tmp->race || tmp->race.contains ("gold")))
481 total += query_money (tmp); 456 total += query_money (tmp);
482 457
483 return total; 458 return total;
484} 459}
485 460
500 return 0; 475 return 0;
501 476
502 pay_from_container (pl, pl, to_pay); 477 pay_from_container (pl, pl, to_pay);
503 478
504 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) 479 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
505 if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) 480 if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (!pouch->race || pouch->race.contains ("gold")))
506 pay_from_container (pl, pouch, to_pay); 481 pay_from_container (pl, pouch, to_pay);
507 482
508 pl->update_stats (); 483 pl->update_stats ();
509 return 1; 484 return 1;
510} 485}
537 change_exp (pl, saved_money, "bargaining", SK_EXP_NONE); 512 change_exp (pl, saved_money, "bargaining", SK_EXP_NONE);
538 513
539 pay_from_container (pl, pl, to_pay); 514 pay_from_container (pl, pl, to_pay);
540 515
541 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) 516 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
542 if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (pouch->race == NULL || strstr (pouch->race, "gold"))) 517 if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && (!pouch->race || pouch->race.contains ("gold")))
543 pay_from_container (pl, pouch, to_pay); 518 pay_from_container (pl, pouch, to_pay);
544 519
545 pl->update_stats (); 520 pl->update_stats ();
546 521
547 return 1; 522 return 1;
586 { 561 {
587 // This should not happen, but if it does, just merge the two. 562 // This should not happen, but if it does, just merge the two.
588 if (coin_objs [i]) 563 if (coin_objs [i])
589 { 564 {
590 LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); 565 LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]);
591 tmp->remove ();
592 coin_objs[i]->nrof += tmp->nrof; 566 coin_objs[i]->nrof += tmp->nrof;
593 esrv_del_item (pl->contr, tmp->count);
594 tmp->destroy (); 567 tmp->destroy ();
595 } 568 }
596 else 569 else
597 { 570 {
598 tmp->remove (); 571 tmp->remove ();
599
600 if (pouch->type == PLAYER)
601 esrv_del_item (pl->contr, tmp->count);
602
603 coin_objs[i] = tmp; 572 coin_objs[i] = tmp;
604 } 573 }
605 574
606 break; 575 break;
607 } 576 }
627 } 596 }
628 597
629 for (i = 0; i < NUM_COINS; i++) 598 for (i = 0; i < NUM_COINS; i++)
630 { 599 {
631 object &coin = *coin_objs[i]; 600 object &coin = *coin_objs[i];
632 sint64 num_coins = min ((to_pay + coin.value - 1) / coin.value, coin.nrof); 601 sint64 num_coins = min ((to_pay + coin.value - 1) / coin.value, (sint64) coin.nrof);
633 to_pay -= num_coins * coin.value; 602 to_pay -= num_coins * coin.value;
634 603
635 coin.nrof -= num_coins; 604 coin.nrof -= num_coins;
636 /* Now start making change. Start at the coin value 605 /* Now start making change. Start at the coin value
637 * below the one we just did, and work down to 606 * below the one we just did, and work down to
647 count--; 616 count--;
648 } 617 }
649 } 618 }
650 619
651 for (i = 0; i < NUM_COINS; i++) 620 for (i = 0; i < NUM_COINS; i++)
652 {
653 if (coin_objs[i]->nrof) 621 if (coin_objs[i]->nrof)
654 {
655 object *tmp = insert_ob_in_ob (coin_objs[i], pouch); 622 insert_ob_in_ob (coin_objs [i], pouch);
656
657 esrv_send_item (pl, tmp);
658 esrv_send_item (pl, pouch);
659
660 if (pl != pouch)
661 esrv_update_item (UPD_WEIGHT, pl, pouch);
662
663 if (pl->type != PLAYER)
664 esrv_send_item (pl, pl);
665 }
666 else 623 else
667 coin_objs[i]->destroy (); 624 coin_objs[i]->destroy ();
668 }
669} 625}
670 626
671/* Checks all unpaid items in op's inventory, adds up all the money they 627/* Checks all unpaid items in op's inventory, adds up all the money they
672 * have, and checks that they can actually afford what they want to buy. 628 * have, and checks that they can actually afford what they want to buy.
673 * Returns 1 if they can, and 0 if they can't. also prints an appropriate message 629 * Returns 1 if they can, and 0 if they can't. also prints an appropriate message
693 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP); 649 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP);
694 } 650 }
695 651
696 if (unpaid_price > player_wealth) 652 if (unpaid_price > player_wealth)
697 { 653 {
698 char buf[MAX_BUF]; 654 dynbuf_text &buf = msg_dynbuf; buf.clear ();
699 char cost[MAX_BUF];
700 char missing[MAX_BUF];
701 655
702 snprintf (cost, MAX_BUF, "%s", cost_string_from_value (unpaid_price, 0)); 656 buf << "You have " << unpaid_count
703 snprintf (missing, MAX_BUF, "%s", cost_string_from_value (unpaid_price - player_wealth, 0)); 657 << " unpaid item(s) that would cost you " << cost_string_from_value (unpaid_price, 0)
658 << ". You need another " << cost_string_from_value (unpaid_price - player_wealth, 0)
659 << " to be able to afford that. "
660 "H<You cannot leave a shop without paying - drop unpaid items first to be able to leave.>";
704 661
705 snprintf (buf, MAX_BUF, "You have %d unpaid items that would cost you %s. You need another %s to be able to afford that.", 662 pl->failmsg (buf);
706 unpaid_count, cost, missing);
707 new_draw_info (NDI_UNIQUE, 0, pl, buf);
708 663
709 return 0; 664 return 0;
710 } 665 }
711 else 666 else
712 return 1; 667 return 1;
741 SET_FLAG (op, FLAG_UNPAID); 696 SET_FLAG (op, FLAG_UNPAID);
742 return 0; 697 return 0;
743 } 698 }
744 else 699 else
745 { 700 {
746 object *tmp;
747
748 CLEAR_FLAG (op, FLAG_UNPAID); 701 CLEAR_FLAG (op, FLAG_UNPAID);
749 CLEAR_FLAG (op, FLAG_PLAYER_SOLD); 702 CLEAR_FLAG (op, FLAG_PLAYER_SOLD);
750 new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op)); 703 new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op));
751 tmp = merge_ob (op, NULL);
752 704
753 if (pl->type == PLAYER) 705 if (!merge_ob (op, op->env->inv))
754 {
755 if (tmp)
756 { /* it was merged */
757 esrv_del_item (pl->contr, op->count);
758 op = tmp;
759 }
760
761 esrv_send_item (pl, op); 706 esrv_update_item (UPD_FLAGS, pl, op);
762 }
763 707
764 goto next_item; 708 goto next_item;
765 } 709 }
766 } 710 }
767 } 711 }
786{ 730{
787 int count = 0; 731 int count = 0;
788 archetype *at = 0; 732 archetype *at = 0;
789 object *pouch = 0, *tmp = 0; 733 object *pouch = 0, *tmp = 0;
790 734
791 for (count = 0; coins[count] != NULL; count++) 735 for (count = 0; coins[count]; count++)
792 { 736 {
793 at = archetype::find (coins[count]); 737 at = archetype::find (coins[count]);
794 738
795 if (at == NULL) 739 if (at == NULL)
796 LOG (llevError, "Could not find %s archetype\n", coins[count]); 740 LOG (llevError, "Could not find %s archetype\n", coins[count]);
797 else if ((amount / at->value) > 0) 741 else if ((amount / at->value) > 0)
798 { 742 {
799 for (pouch = pl->inv; pouch; pouch = pouch->below) 743 for (pouch = pl->inv; pouch; pouch = pouch->below)
800 { 744 {
801 if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && pouch->race && strstr (pouch->race, "gold")) 745 if (pouch->type == CONTAINER && QUERY_FLAG (pouch, FLAG_APPLIED) && pouch->race.contains ("gold"))
802 { 746 {
803 int w = at->weight * (100 - pouch->stats.Str) / 100; 747 int w = at->weight * (100 - pouch->stats.Str) / 100;
804 int n = amount / at->value; 748 int n = amount / at->value;
805 749
806 if (w == 0) 750 if (w == 0)
809 if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit)) 753 if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit))
810 { 754 {
811 if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) 755 if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n)
812 n = (pouch->weight_limit - pouch->carrying) / w; 756 n = (pouch->weight_limit - pouch->carrying) / w;
813 757
814 tmp = arch_to_object (at); 758 object *tmp = arch_to_object (at);
815 tmp->nrof = n; 759 tmp->nrof = n;
816 amount -= tmp->nrof * tmp->value; 760 amount -= tmp->nrof * tmp->value;
817 tmp = insert_ob_in_ob (tmp, pouch); 761 pouch->insert (tmp);
818 esrv_send_item (pl, tmp);
819 esrv_send_item (pl, pouch);
820 esrv_update_item (UPD_WEIGHT, pl, pouch);
821 esrv_send_item (pl, pl);
822 } 762 }
823 } 763 }
824 } 764 }
825 765
826 if (amount / at->value > 0) 766 if (amount / at->value > 0)
827 { 767 {
828 tmp = arch_to_object (at); 768 object *tmp = arch_to_object (at);
829 tmp->nrof = amount / tmp->value; 769 tmp->nrof = amount / tmp->value;
830 amount -= tmp->nrof * tmp->value; 770 amount -= tmp->nrof * tmp->value;
831 tmp = insert_ob_in_ob (tmp, pl); 771 pl->insert (tmp);
832 esrv_send_item (pl, tmp);
833 esrv_send_item (pl, pl);
834 } 772 }
835 } 773 }
836 } 774 }
837 775
838 if (amount != 0) 776 if (amount != 0)
841 779
842/* elmex: this is for the bank plugin :( */ 780/* elmex: this is for the bank plugin :( */
843sint64 781sint64
844pay_player_arch (object *pl, const char *arch, sint64 amount) 782pay_player_arch (object *pl, const char *arch, sint64 amount)
845{ 783{
846 archetype *at = archetype::find (arch);
847 object *tmp = NULL;
848
849 if (at == NULL)
850 return 0;
851
852 if (amount > 0) 784 if (amount)
853 { 785 {
854 tmp = arch_to_object (at); 786 object *ob = archetype::get (arch);
787
788 if (!ob)
789 return 0;
790
855 tmp->nrof = amount; 791 ob->nrof = amount;
856 tmp = insert_ob_in_ob (tmp, pl); 792 pl->insert (ob);
857 esrv_send_item (pl, tmp);
858 esrv_send_item (pl, pl);
859 } 793 }
860 794
861 return 1; 795 return 1;
862} 796}
863 797
866 * buy item. 800 * buy item.
867 * 801 *
868 * Modified to fill available race: gold containers before dumping 802 * Modified to fill available race: gold containers before dumping
869 * remaining coins in character's inventory. 803 * remaining coins in character's inventory.
870 */ 804 */
871void 805bool
872sell_item (object *op, object *pl) 806sell_item (object *op, object *pl)
873{ 807{
874 sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain; 808 sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain;
875 809
876 if (pl == NULL || pl->type != PLAYER) 810 if (pl == NULL || pl->type != PLAYER)
877 { 811 {
878 LOG (llevDebug, "Object other than player tried to sell something.\n"); 812 LOG (llevDebug, "Object other than player tried to sell something.\n");
879 return; 813 return false;
880 } 814 }
881 815
882 op->custom_name = 0; 816 op->custom_name = 0;
883 817
884 if (!amount) 818 if (!amount)
885 { 819 {
886 new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.", query_name (op)); 820 new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.",
887 821 query_name (op));
888 /* Even if the character doesn't get anything for it, it may still be 822 // elmex: change: the player now gets the item back if the shop is not
889 * worth something. If so, make it unpaid 823 // interested in it.
890 */
891 if (op->value)
892 {
893 SET_FLAG (op, FLAG_UNPAID);
894 SET_FLAG (op, FLAG_PLAYER_SOLD);
895 }
896
897 identify (op);
898 return; 824 return false;
899 } 825 }
900 826
901 /* We compare the price with the one for a player 827 /* We compare the price with the one for a player
902 * without bargaining skill. 828 * without bargaining skill.
903 * This determins the amount of exp (if any) gained for bargaining. 829 * This determins the amount of exp (if any) gained for bargaining.
908 if (extra_gain > 0) 834 if (extra_gain > 0)
909 change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE); 835 change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE);
910 836
911 pay_player (pl, amount); 837 pay_player (pl, amount);
912 838
913 new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.", query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op)); 839 new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.",
840 query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op));
841 pl->play_sound (sound_find ("shop_sell"));
914 842
915 SET_FLAG (op, FLAG_UNPAID); 843 SET_FLAG (op, FLAG_UNPAID);
916 identify (op); 844 identify (op);
917}
918 845
846 return true;
847}
919 848
920/* returns a double that is the ratio of the price that a shop will offer for 849/* returns a double that is the ratio of the price that a shop will offer for
921 * item based on the shops specialisation. Does not take account of greed, 850 * item based on the shops specialisation. Does not take account of greed,
922 * returned value is between SPECIALISATION_EFFECT and 1. 851 * returned value is between SPECIALISATION_EFFECT and 1.
923 */ 852 */
1120{ 1049{
1121 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2; 1050 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2;
1122 1051
1123 if (s1->type < s2->type) 1052 if (s1->type < s2->type)
1124 return -1; 1053 return -1;
1054
1125 if (s1->type > s2->type) 1055 if (s1->type > s2->type)
1126 return 1; 1056 return 1;
1127 1057
1128 /* the type is the same (what atoi gets), so do a strcasecmp to sort 1058 /* the type is the same (what atoi gets), so do a strcasecmp to sort
1129 * via alphabetical order 1059 * via alphabetical order
1172 items[*numitems].item_sort = strdup (query_base_name (tmp, 0)); 1102 items[*numitems].item_sort = strdup (query_base_name (tmp, 0));
1173 items[*numitems].item_real = strdup (query_base_name (tmp, 1)); 1103 items[*numitems].item_real = strdup (query_base_name (tmp, 1));
1174 (*numitems)++; 1104 (*numitems)++;
1175 break; 1105 break;
1176 } 1106 }
1107
1177 SET_FLAG (tmp, FLAG_UNPAID); 1108 SET_FLAG (tmp, FLAG_UNPAID);
1178} 1109}
1179 1110
1180void 1111void
1181shop_listing (object *sign, object *op) 1112shop_listing (object *sign, object *op)
1182{ 1113{
1183 int i, j, numitems = 0, numallocated = 0, x1, x2, y1, y2; 1114 int i, j, x1, x2, y1, y2;
1184 const char *shop_coords = get_ob_key_value (sign, "shop_coords"); 1115 const char *shop_coords = sign->kv (shstr_shop_coords);
1185 object *stack; 1116 object *stack;
1186 shopinv *items; 1117 shopinv *items;
1187 1118
1188 /* Should never happen, but just in case a monster does apply a sign */ 1119 /* Should never happen, but just in case a monster does apply a sign */
1189 if (op->type != PLAYER) 1120 if (!op->is_player ())
1190 return; 1121 return;
1122
1123 dynbuf_text &buf = msg_dynbuf; buf.clear ();
1191 1124
1192 if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) 1125 if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2)))
1193 { 1126 {
1194 x1 = 0; 1127 x1 = 0;
1195 y1 = 0; 1128 y1 = 0;
1196 x2 = op->map->width - 1; 1129 x2 = op->map->width - 1;
1197 y2 = op->map->height - 1; 1130 y2 = op->map->height - 1;
1198 } 1131 }
1199 1132
1200 items = (shopinv *) malloc (40 * sizeof (shopinv));
1201 numallocated = 40; 1133 int numallocated = 40;
1134 int numitems = 0;
1135 items = (shopinv *)malloc (sizeof (shopinv) * numallocated);
1202 1136
1203 /* Find all the appropriate items */ 1137 /* Find all the appropriate items */
1204 for (i = x1; i <= x2; i++) 1138 for (i = x1; i <= x2; i++)
1205 {
1206 for (j = y1; j < y2; j++) 1139 for (j = y1; j < y2; j++)
1140 if (is_in_shop (op->map, i, j))
1207 { 1141 {
1208 if (is_in_shop (op->map, i, j)) 1142 stack = GET_MAP_OB (op->map, i, j);
1143
1144 while (stack)
1209 { 1145 {
1210 stack = GET_MAP_OB (op->map, i, j); 1146 if (QUERY_FLAG (stack, FLAG_UNPAID))
1211
1212 while (stack)
1213 { 1147 {
1214 if (QUERY_FLAG (stack, FLAG_UNPAID))
1215 {
1216 if (numitems == numallocated) 1148 if (numitems == numallocated)
1217 {
1218 items = (shopinv *) realloc (items, sizeof (shopinv) * (numallocated + 10)); 1149 items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2));
1219 numallocated += 10;
1220 }
1221 1150
1222 add_shop_item (stack, items, &numitems, &numallocated); 1151 add_shop_item (stack, items, &numitems, &numallocated);
1223 }
1224
1225 stack = stack->above;
1226 } 1152 }
1153
1154 stack = stack->above;
1227 } 1155 }
1228 } 1156 }
1229 }
1230 1157
1231 if (numitems == 0) 1158 buf << (numitems ? "T<This shop contains:>\n\n"
1232 { 1159 : "T<This shop is currently empty.>");
1233 new_draw_info (NDI_UNIQUE, 0, op, "The shop is currently empty.\n");
1234 free (items);
1235 return;
1236 }
1237 1160
1238 qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); 1161 qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort);
1239
1240 new_draw_info (NDI_UNIQUE, 0, op, "\nThe shop contains:");
1241 1162
1242 for (i = 0; i < numitems; i++) 1163 for (i = 0; i < numitems; i++)
1243 { 1164 {
1244 /* Collapse items of the same name together */ 1165 /* Collapse items of the same name together */
1245 if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) 1166 if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real))
1246 {
1247 items[i + 1].nrof += items[i].nrof; 1167 items[i + 1].nrof += items[i].nrof;
1248 free (items[i].item_sort);
1249 free (items[i].item_real);
1250 }
1251 else 1168 else
1252 {
1253 new_draw_info_format (NDI_UNIQUE, 0, op, "%d %s",
1254 items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); 1169 buf.printf (" %4d %s\n", items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real);
1170
1255 free (items[i].item_sort); 1171 free (items[i].item_sort);
1256 free (items[i].item_real); 1172 free (items[i].item_real);
1257 }
1258 } 1173 }
1174
1175 op->contr->infobox (MSG_CHANNEL ("shopitems"), buf);
1259 1176
1260 free (items); 1177 free (items);
1261} 1178}
1262 1179
1263/* elmex: this function checks whether the object is in a shop */ 1180/* elmex: this function checks whether the object is in a shop */
1264bool 1181bool
1265is_in_shop (object *o) 1182is_in_shop (object *o)
1266{ 1183{
1267 if (!o->map) 1184 if (!o->is_on_map ())
1268 return false; 1185 return false;
1269 1186
1270 return is_in_shop (o->map, o->x, o->y); 1187 return is_in_shop (o->map, o->x, o->y);
1271} 1188}
1272 1189
1273/* elmex: this function checks whether we are in a shop or not */ 1190/* elmex: this function checks whether we are in a shop or not
1191 - change 2007-11-26: enhanced the O(n) case by stopping at the first
1192 floor tile. this possibly will make map bugs where shopfloors are above
1193 floors more obvious.
1194*/
1195
1274bool 1196bool
1275is_in_shop (maptile *map, int x, int y) 1197is_in_shop (maptile *map, int x, int y)
1276{ 1198{
1277 for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above) 1199 for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above)
1200 if (QUERY_FLAG (floor, FLAG_IS_FLOOR))
1278 if (floor->type == SHOP_FLOOR) 1201 return floor->type == SHOP_FLOOR;
1279 return true;
1280 1202
1281 return false; 1203 return false;
1282} 1204}
1205

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines