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.47 by root, Sun Apr 13 01:34:09 2008 UTC vs.
Revision 1.71 by root, Fri Oct 16 01:56:42 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra 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 * Deliantra 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 <support@deliantra.net> 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>
74 75
75sint64 76sint64
76query_cost (const object *tmp, object *who, int flag) 77query_cost (const object *tmp, object *who, int flag)
77{ 78{
78 double val; 79 double val;
79 int number; /* used to better calculate value */
80 int no_bargain; 80 int no_bargain;
81 int identified; 81 int identified;
82 int not_cursed; 82 int not_cursed;
83 int approximate; 83 int approximate;
84 int shop; 84 int shop;
109 109
110 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 ());
111 return 0; 111 return 0;
112 } 112 }
113 113
114 number = tmp->nrof; 114 int number = tmp->number_of ();
115 if (number == 0) 115
116 number = 1;
117 if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified) 116 if (QUERY_FLAG (tmp, FLAG_IDENTIFIED) || !need_identify (tmp) || identified)
118 { 117 {
119 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)))
120 return 0; 119 return 0;
121 else 120 else
122 val = tmp->value * number; 121 val = tmp->value * number;
123 } 122 }
124 /* 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 */
125 else 124 else
126 { 125 {
127 if (tmp->arch != NULL) 126 if (flag == F_BUY)
128 { 127 {
129 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
130 { 136 {
131 LOG (llevError, "Asking for buy-value of unidentified object.\n"); 137 /* Get 2/3'rd value for applied objects, 1/3'rd for totally
132 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;
133 } 144 }
134 else
135 { /* Trying to sell something, or get true value */
136 if (tmp->type == POTION)
137 val = number * 40; /* Don't want to give anything away */
138 else
139 {
140 /* Get 2/3'rd value for applied objects, 1/3'rd for totally
141 * unknown objects
142 */
143 if (QUERY_FLAG (tmp, FLAG_BEEN_APPLIED))
144 val = number * tmp->arch->value * 2 / 3;
145 else
146 val = number * tmp->arch->value / 3;
147 }
148 }
149 }
150 else
151 { /* No archetype with this object */
152 LOG (llevDebug, "In sell item: Have object with no archetype: %s\n", &tmp->name);
153 if (flag == F_BUY)
154 {
155 LOG (llevError, "Asking for buy-value of unidentified object without arch.\n");
156 val = number * tmp->value * 10;
157 }
158 else
159 val = number * tmp->value / 5;
160 } 145 }
161 } 146 }
162 147
163 /* 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
164 * identified, AND the object is magical and the archetype is non 149 * identified, AND the object is magical and the archetype is non
206 * AND Cha = 30 will get optimal price. 191 * AND Cha = 30 will get optimal price.
207 * Thus charisma will never get useless. 192 * Thus charisma will never get useless.
208 * -b.e. edler@heydernet.de 193 * -b.e. edler@heydernet.de
209 */ 194 */
210 195
211 if (who != NULL && who->type == PLAYER) 196 if (who && who->type == PLAYER)
212 { 197 {
213 int lev_bargain = 0; 198 int lev_bargain = 0;
214 int lev_identify = 0; 199 int lev_identify = 0;
215 int idskill1 = 0;
216 int idskill2 = 0;
217 const typedata *tmptype;
218
219 tmptype = get_typedata (tmp->type);
220 200
221 if (find_skill_by_number (who, SK_BARGAINING)) 201 if (find_skill_by_number (who, SK_BARGAINING))
222 lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; 202 lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level;
223 203
224 if (tmptype) 204 if (const typedata *tmptype = get_typedata (tmp->type))
225 { 205 {
226 idskill1 = tmptype->identifyskill; 206 if (int idskill1 = tmptype->identifyskill)
227
228 if (idskill1)
229 { 207 {
230 idskill2 = tmptype->identifyskill2; 208 int idskill2 = tmptype->identifyskill2;
231 209
232 if (find_skill_by_number (who, idskill1)) 210 if (find_skill_by_number (who, idskill1))
233 lev_identify = find_skill_by_number (who, idskill1)->level; 211 lev_identify = find_skill_by_number (who, idskill1)->level;
234 212
235 if (idskill2 && find_skill_by_number (who, idskill2)) 213 if (idskill2 && find_skill_by_number (who, idskill2))
236 lev_identify += find_skill_by_number (who, idskill2)->level; 214 lev_identify += find_skill_by_number (who, idskill2)->level;
237 } 215 }
238 } 216 }
239 else
240 LOG (llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->debug_desc ());
241 217
242 /* ratio determines how much of the price modification 218 /* ratio determines how much of the price modification
243 * will come from the basic stat charisma 219 * will come from the basic stat charisma
244 * the rest will come from the level in bargaining skill 220 * the rest will come from the level in bargaining skill
245 */ 221 */
418 394
419 if (coin == NULL) 395 if (coin == NULL)
420 return "nothing"; 396 return "nothing";
421 397
422 num = real_value / coin->value; 398 num = real_value / coin->value;
399
423 if (num == 1) 400 if (num == 1)
424 sprintf (buf, "about one %s", &coin->object::name); 401 sprintf (buf, "about one %s", &coin->object::name);
425 else if (num < 5) 402 else if (num < 5)
426 sprintf (buf, "a few %s", &coin->object::name_pl); 403 sprintf (buf, "a few %s", &coin->object::name_pl);
427 else if (num < 10) 404 else if (num < 10)
432 sprintf (buf, "lots of %s", &coin->object::name_pl); 409 sprintf (buf, "lots of %s", &coin->object::name_pl);
433 else if (num < 1000) 410 else if (num < 1000)
434 sprintf (buf, "a great many %s", &coin->object::name_pl); 411 sprintf (buf, "a great many %s", &coin->object::name_pl);
435 else 412 else
436 sprintf (buf, "a vast quantity of %s", &coin->object::name_pl); 413 sprintf (buf, "a vast quantity of %s", &coin->object::name_pl);
414
437 return buf; 415 return buf;
438 } 416 }
439 } 417 }
440 } 418 }
441 419
472 } 450 }
473 451
474 for (tmp = op->inv; tmp; tmp = tmp->below) 452 for (tmp = op->inv; tmp; tmp = tmp->below)
475 if (tmp->type == MONEY) 453 if (tmp->type == MONEY)
476 total += tmp->nrof * (sint64)tmp->value; 454 total += tmp->nrof * (sint64)tmp->value;
477 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")))
478 total += query_money (tmp); 456 total += query_money (tmp);
479 457
480 return total; 458 return total;
481} 459}
482 460
497 return 0; 475 return 0;
498 476
499 pay_from_container (pl, pl, to_pay); 477 pay_from_container (pl, pl, to_pay);
500 478
501 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) 479 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
502 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")))
503 pay_from_container (pl, pouch, to_pay); 481 pay_from_container (pl, pouch, to_pay);
504 482
505 pl->update_stats (); 483 pl->update_stats ();
506 return 1; 484 return 1;
507} 485}
534 change_exp (pl, saved_money, "bargaining", SK_EXP_NONE); 512 change_exp (pl, saved_money, "bargaining", SK_EXP_NONE);
535 513
536 pay_from_container (pl, pl, to_pay); 514 pay_from_container (pl, pl, to_pay);
537 515
538 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) 516 for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below)
539 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")))
540 pay_from_container (pl, pouch, to_pay); 518 pay_from_container (pl, pouch, to_pay);
541 519
542 pl->update_stats (); 520 pl->update_stats ();
543 521
544 return 1; 522 return 1;
583 { 561 {
584 // 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.
585 if (coin_objs [i]) 563 if (coin_objs [i])
586 { 564 {
587 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]);
588 tmp->remove ();
589 coin_objs[i]->nrof += tmp->nrof; 566 coin_objs[i]->nrof += tmp->nrof;
590 esrv_del_item (pl->contr, tmp->count);
591 tmp->destroy (); 567 tmp->destroy ();
592 } 568 }
593 else 569 else
594 { 570 {
595 tmp->remove (); 571 tmp->remove ();
596
597 if (pouch->type == PLAYER)
598 esrv_del_item (pl->contr, tmp->count);
599
600 coin_objs[i] = tmp; 572 coin_objs[i] = tmp;
601 } 573 }
602 574
603 break; 575 break;
604 } 576 }
624 } 596 }
625 597
626 for (i = 0; i < NUM_COINS; i++) 598 for (i = 0; i < NUM_COINS; i++)
627 { 599 {
628 object &coin = *coin_objs[i]; 600 object &coin = *coin_objs[i];
629 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);
630 to_pay -= num_coins * coin.value; 602 to_pay -= num_coins * coin.value;
631 603
632 coin.nrof -= num_coins; 604 coin.nrof -= num_coins;
633 /* Now start making change. Start at the coin value 605 /* Now start making change. Start at the coin value
634 * below the one we just did, and work down to 606 * below the one we just did, and work down to
644 count--; 616 count--;
645 } 617 }
646 } 618 }
647 619
648 for (i = 0; i < NUM_COINS; i++) 620 for (i = 0; i < NUM_COINS; i++)
649 {
650 if (coin_objs[i]->nrof) 621 if (coin_objs[i]->nrof)
651 {
652 object *tmp = insert_ob_in_ob (coin_objs[i], pouch); 622 insert_ob_in_ob (coin_objs [i], pouch);
653
654 esrv_send_item (pl, tmp);
655 esrv_send_item (pl, pouch);
656
657 if (pl != pouch)
658 esrv_update_item (UPD_WEIGHT, pl, pouch);
659
660 if (pl->type != PLAYER)
661 esrv_send_item (pl, pl);
662 }
663 else 623 else
664 coin_objs[i]->destroy (); 624 coin_objs[i]->destroy ();
665 }
666} 625}
667 626
668/* 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
669 * 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.
670 * 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
690 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP); 649 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP);
691 } 650 }
692 651
693 if (unpaid_price > player_wealth) 652 if (unpaid_price > player_wealth)
694 { 653 {
695 char buf[MAX_BUF]; 654 dynbuf_text &buf = msg_dynbuf; buf.clear ();
696 char cost[MAX_BUF];
697 char missing[MAX_BUF];
698 655
699 snprintf (cost, MAX_BUF, "%s", cost_string_from_value (unpaid_price, 0)); 656 buf << "You have " << unpaid_count
700 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.>";
701 661
702 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);
703 unpaid_count, cost, missing);
704 new_draw_info (NDI_UNIQUE, 0, pl, buf);
705 663
706 return 0; 664 return 0;
707 } 665 }
708 else 666 else
709 return 1; 667 return 1;
738 SET_FLAG (op, FLAG_UNPAID); 696 SET_FLAG (op, FLAG_UNPAID);
739 return 0; 697 return 0;
740 } 698 }
741 else 699 else
742 { 700 {
743 object *tmp;
744
745 CLEAR_FLAG (op, FLAG_UNPAID); 701 CLEAR_FLAG (op, FLAG_UNPAID);
746 CLEAR_FLAG (op, FLAG_PLAYER_SOLD); 702 CLEAR_FLAG (op, FLAG_PLAYER_SOLD);
747 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));
748 tmp = merge_ob (op, NULL);
749 704
750 if (pl->type == PLAYER) 705 if (!merge_ob (op, op->env->inv))
751 {
752 if (tmp)
753 { /* it was merged */
754 esrv_del_item (pl->contr, op->count);
755 op = tmp;
756 }
757
758 esrv_send_item (pl, op); 706 esrv_update_item (UPD_FLAGS, pl, op);
759 }
760 707
761 goto next_item; 708 goto next_item;
762 } 709 }
763 } 710 }
764 } 711 }
793 LOG (llevError, "Could not find %s archetype\n", coins[count]); 740 LOG (llevError, "Could not find %s archetype\n", coins[count]);
794 else if ((amount / at->value) > 0) 741 else if ((amount / at->value) > 0)
795 { 742 {
796 for (pouch = pl->inv; pouch; pouch = pouch->below) 743 for (pouch = pl->inv; pouch; pouch = pouch->below)
797 { 744 {
798 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"))
799 { 746 {
800 int w = at->weight * (100 - pouch->stats.Str) / 100; 747 int w = at->weight * (100 - pouch->stats.Str) / 100;
801 int n = amount / at->value; 748 int n = amount / at->value;
802 749
803 if (w == 0) 750 if (w == 0)
806 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))
807 { 754 {
808 if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) 755 if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n)
809 n = (pouch->weight_limit - pouch->carrying) / w; 756 n = (pouch->weight_limit - pouch->carrying) / w;
810 757
811 tmp = arch_to_object (at); 758 object *tmp = arch_to_object (at);
812 tmp->nrof = n; 759 tmp->nrof = n;
813 amount -= tmp->nrof * tmp->value; 760 amount -= tmp->nrof * tmp->value;
814 tmp = insert_ob_in_ob (tmp, pouch); 761 pouch->insert (tmp);
815 esrv_send_item (pl, tmp);
816 esrv_send_item (pl, pouch);
817 esrv_update_item (UPD_WEIGHT, pl, pouch);
818 esrv_send_item (pl, pl);
819 } 762 }
820 } 763 }
821 } 764 }
822 765
823 if (amount / at->value > 0) 766 if (amount / at->value > 0)
824 { 767 {
825 tmp = arch_to_object (at); 768 object *tmp = arch_to_object (at);
826 tmp->nrof = amount / tmp->value; 769 tmp->nrof = amount / tmp->value;
827 amount -= tmp->nrof * tmp->value; 770 amount -= tmp->nrof * tmp->value;
828 tmp = insert_ob_in_ob (tmp, pl); 771 pl->insert (tmp);
829 esrv_send_item (pl, tmp);
830 esrv_send_item (pl, pl);
831 } 772 }
832 } 773 }
833 } 774 }
834 775
835 if (amount != 0) 776 if (amount != 0)
838 779
839/* elmex: this is for the bank plugin :( */ 780/* elmex: this is for the bank plugin :( */
840sint64 781sint64
841pay_player_arch (object *pl, const char *arch, sint64 amount) 782pay_player_arch (object *pl, const char *arch, sint64 amount)
842{ 783{
843 archetype *at = archetype::find (arch);
844
845 if (!at)
846 return 0;
847
848 if (amount > 0) 784 if (amount)
849 { 785 {
850 object *tmp = arch_to_object (at); 786 object *ob = archetype::get (arch);
787
788 if (!ob)
789 return 0;
790
851 tmp->nrof = amount; 791 ob->nrof = amount;
852 tmp = insert_ob_in_ob (tmp, pl); 792 pl->insert (ob);
853 esrv_send_item (pl, tmp);
854 esrv_send_item (pl, pl);
855 } 793 }
856 794
857 return 1; 795 return 1;
858} 796}
859 797
862 * buy item. 800 * buy item.
863 * 801 *
864 * Modified to fill available race: gold containers before dumping 802 * Modified to fill available race: gold containers before dumping
865 * remaining coins in character's inventory. 803 * remaining coins in character's inventory.
866 */ 804 */
867void 805bool
868sell_item (object *op, object *pl) 806sell_item (object *op, object *pl)
869{ 807{
870 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;
871 809
872 if (pl == NULL || pl->type != PLAYER) 810 if (pl == NULL || pl->type != PLAYER)
873 { 811 {
874 LOG (llevDebug, "Object other than player tried to sell something.\n"); 812 LOG (llevDebug, "Object other than player tried to sell something.\n");
875 return; 813 return false;
876 } 814 }
877 815
878 op->custom_name = 0; 816 op->custom_name = 0;
879 817
880 if (!amount) 818 if (!amount)
881 { 819 {
882 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.",
883 821 query_name (op));
884 /* 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
885 * worth something. If so, make it unpaid 823 // interested in it.
886 */
887 if (op->value)
888 {
889 SET_FLAG (op, FLAG_UNPAID);
890 SET_FLAG (op, FLAG_PLAYER_SOLD);
891 }
892
893 identify (op);
894 return; 824 return false;
895 } 825 }
896 826
897 /* We compare the price with the one for a player 827 /* We compare the price with the one for a player
898 * without bargaining skill. 828 * without bargaining skill.
899 * This determins the amount of exp (if any) gained for bargaining. 829 * This determins the amount of exp (if any) gained for bargaining.
904 if (extra_gain > 0) 834 if (extra_gain > 0)
905 change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE); 835 change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE);
906 836
907 pay_player (pl, amount); 837 pay_player (pl, amount);
908 838
909 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));
910 pl->play_sound (sound_find ("shop_sell")); 841 pl->play_sound (sound_find ("shop_sell"));
911 842
912 SET_FLAG (op, FLAG_UNPAID); 843 SET_FLAG (op, FLAG_UNPAID);
913 identify (op); 844 identify (op);
845
846 return true;
914} 847}
915 848
916/* 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
917 * 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,
918 * returned value is between SPECIALISATION_EFFECT and 1. 851 * returned value is between SPECIALISATION_EFFECT and 1.
1031 964
1032/* gives a desciption of the shop on their current map to the player op. */ 965/* gives a desciption of the shop on their current map to the player op. */
1033int 966int
1034describe_shop (const object *op) 967describe_shop (const object *op)
1035{ 968{
969 dynbuf_text buf;
1036 maptile *map = op->map; 970 maptile *map = op->map;
1037 971
1038 /*shopitems *items=map->shopitems; */ 972 /*shopitems *items=map->shopitems; */
1039 int pos = 0, i; 973 int pos = 0, i;
1040 double opinion = 0; 974 double opinion = 0;
1041 char tmp[MAX_BUF] = "\0";
1042 975
1043 if (op->type != PLAYER) 976 if (op->type != PLAYER)
1044 return 0; 977 return 0;
1045 978
1046 /*check if there is a shop specified for this map */ 979 /*check if there is a shop specified for this map */
1047 if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax) 980 if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax)
1048 { 981 {
1049 new_draw_info (NDI_UNIQUE, 0, op, "From looking at the nearby shop you determine that it trades in:"); 982 buf << "From looking at the nearby shop you determine that it trades in ";
983
984 char tmp[MAX_BUF];
1050 985
1051 if (map->shopitems) 986 if (map->shopitems)
1052 for (i = 0; i < map->shopitems[0].index; i++) 987 for (i = 0; i < map->shopitems[0].index; i++)
1053 if (map->shopitems[i].name && map->shopitems[i].strength > 10) 988 if (map->shopitems[i].name && map->shopitems[i].strength > 10)
1054 { 989 {
1055 snprintf (tmp + pos, sizeof (tmp) - pos, "%s, ", map->shopitems[i].name_pl); 990 snprintf (tmp + pos, sizeof (tmp) - pos, "%s, ", map->shopitems[i].name_pl);
1056 pos += strlen (tmp + pos); 991 pos += strlen (tmp + pos);
1057 } 992 }
1058 993
1059 if (!pos) 994 if (pos)
995 make_list_like (tmp);
996 else
1060 strcat (tmp, "a little of everything."); 997 strcat (tmp, "a little of everything.");
1061 998
1062 /* format the string into a list */ 999 /* format the string into a list */
1063 make_list_like (tmp); 1000 buf << tmp << "\n\n";
1064 new_draw_info_format (NDI_UNIQUE, 0, op, "%s", tmp);
1065 1001
1066 if (map->shopmax) 1002 if (map->shopmax)
1067 new_draw_info_format (NDI_UNIQUE, 0, op, "It won't trade for items above %s.", cost_string_from_value (map->shopmax, 0)); 1003 buf << "It won't trade for items above " << cost_string_from_value (map->shopmax, 0) << ".\n\n";
1068 1004
1069 if (map->shopmin) 1005 if (map->shopmin)
1070 new_draw_info_format (NDI_UNIQUE, 0, op, "It won't trade in items worth less than %s.", cost_string_from_value (map->shopmin, 0)); 1006 buf << "It won't trade in items worth less than " << cost_string_from_value (map->shopmin, 0) << ".\n\n";
1071 1007
1072 if (map->shopgreed) 1008 if (map->shopgreed)
1073 { 1009 {
1074 if (map->shopgreed > 2.0) 1010 if (map->shopgreed > 2.0)
1075 new_draw_info (NDI_UNIQUE, 0, op, "It tends to overcharge massively."); 1011 buf << "It tends to overcharge massively.\n\n";
1076 else if (map->shopgreed > 1.5) 1012 else if (map->shopgreed > 1.5)
1077 new_draw_info (NDI_UNIQUE, 0, op, "It tends to overcharge substantially."); 1013 buf << "It tends to overcharge substantially.\n\n";
1078 else if (map->shopgreed > 1.1) 1014 else if (map->shopgreed > 1.1)
1079 new_draw_info (NDI_UNIQUE, 0, op, "It tends to overcharge slightly."); 1015 buf << "It tends to overcharge slightly.\n\n";
1080 else if (map->shopgreed < 0.9) 1016 else if (map->shopgreed < 0.9)
1081 new_draw_info (NDI_UNIQUE, 0, op, "It tends to undercharge."); 1017 buf << "It tends to undercharge.\n\n";
1082 } 1018 }
1083 1019
1084 if (map->shoprace) 1020 if (map->shoprace)
1085 { 1021 {
1086 opinion = shopkeeper_approval (map, op); 1022 opinion = shopkeeper_approval (map, op);
1023
1087 if (opinion > 0.8) 1024 if (opinion > 0.8)
1088 new_draw_info (NDI_UNIQUE, 0, op, "You think the shopkeeper likes you."); 1025 buf << "You think the shopkeeper likes you.\n\n";
1089 else if (opinion > 0.5) 1026 else if (opinion > 0.5)
1090 new_draw_info (NDI_UNIQUE, 0, op, "The shopkeeper seems unconcerned by you."); 1027 buf << "The shopkeeper seems unconcerned by you.\n\n";
1091 else 1028 else
1092 new_draw_info (NDI_UNIQUE, 0, op, "The shopkeeper seems to have taken a dislike to you."); 1029 buf << "The shopkeeper seems to have taken a dislike to you.\n\n";
1093 } 1030 }
1094 } 1031 }
1095 else 1032 else
1096 new_draw_info (NDI_UNIQUE, 0, op, "There is no shop nearby."); 1033 buf << "There is no shop nearby.\n\n";
1034
1035 op->contr->infobox (MSG_CHANNEL ("shopinfo"), buf);
1097 1036
1098 return 1; 1037 return 1;
1099} 1038}
1100 1039
1101struct shopinv 1040struct shopinv
1116{ 1055{
1117 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2; 1056 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2;
1118 1057
1119 if (s1->type < s2->type) 1058 if (s1->type < s2->type)
1120 return -1; 1059 return -1;
1060
1121 if (s1->type > s2->type) 1061 if (s1->type > s2->type)
1122 return 1; 1062 return 1;
1123 1063
1124 /* the type is the same (what atoi gets), so do a strcasecmp to sort 1064 /* the type is the same (what atoi gets), so do a strcasecmp to sort
1125 * via alphabetical order 1065 * via alphabetical order
1168 items[*numitems].item_sort = strdup (query_base_name (tmp, 0)); 1108 items[*numitems].item_sort = strdup (query_base_name (tmp, 0));
1169 items[*numitems].item_real = strdup (query_base_name (tmp, 1)); 1109 items[*numitems].item_real = strdup (query_base_name (tmp, 1));
1170 (*numitems)++; 1110 (*numitems)++;
1171 break; 1111 break;
1172 } 1112 }
1113
1173 SET_FLAG (tmp, FLAG_UNPAID); 1114 SET_FLAG (tmp, FLAG_UNPAID);
1174} 1115}
1175 1116
1176void 1117void
1177shop_listing (object *sign, object *op) 1118shop_listing (object *sign, object *op)
1178{ 1119{
1179 int i, j, numitems = 0, numallocated = 0, x1, x2, y1, y2; 1120 int i, j, x1, x2, y1, y2;
1180 const char *shop_coords = get_ob_key_value (sign, "shop_coords"); 1121 const char *shop_coords = sign->kv (shstr_shop_coords);
1181 object *stack; 1122 object *stack;
1182 shopinv *items; 1123 shopinv *items;
1183 1124
1184 /* Should never happen, but just in case a monster does apply a sign */ 1125 /* Should never happen, but just in case a monster does apply a sign */
1185 if (op->type != PLAYER) 1126 if (!op->is_player ())
1186 return; 1127 return;
1128
1129 dynbuf_text &buf = msg_dynbuf; buf.clear ();
1187 1130
1188 if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) 1131 if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2)))
1189 { 1132 {
1190 x1 = 0; 1133 x1 = 0;
1191 y1 = 0; 1134 y1 = 0;
1192 x2 = op->map->width - 1; 1135 x2 = op->map->width - 1;
1193 y2 = op->map->height - 1; 1136 y2 = op->map->height - 1;
1194 } 1137 }
1195 1138
1196 items = (shopinv *) malloc (40 * sizeof (shopinv));
1197 numallocated = 40; 1139 int numallocated = 40;
1140 int numitems = 0;
1141 items = (shopinv *)malloc (sizeof (shopinv) * numallocated);
1198 1142
1199 /* Find all the appropriate items */ 1143 /* Find all the appropriate items */
1200 for (i = x1; i <= x2; i++) 1144 for (i = x1; i <= x2; i++)
1201 {
1202 for (j = y1; j < y2; j++) 1145 for (j = y1; j < y2; j++)
1146 if (is_in_shop (op->map, i, j))
1203 { 1147 {
1204 if (is_in_shop (op->map, i, j)) 1148 stack = GET_MAP_OB (op->map, i, j);
1149
1150 while (stack)
1205 { 1151 {
1206 stack = GET_MAP_OB (op->map, i, j); 1152 if (QUERY_FLAG (stack, FLAG_UNPAID))
1207
1208 while (stack)
1209 { 1153 {
1210 if (QUERY_FLAG (stack, FLAG_UNPAID))
1211 {
1212 if (numitems == numallocated) 1154 if (numitems == numallocated)
1213 {
1214 items = (shopinv *) realloc (items, sizeof (shopinv) * (numallocated + 10)); 1155 items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2));
1215 numallocated += 10;
1216 }
1217 1156
1218 add_shop_item (stack, items, &numitems, &numallocated); 1157 add_shop_item (stack, items, &numitems, &numallocated);
1219 }
1220
1221 stack = stack->above;
1222 } 1158 }
1159
1160 stack = stack->above;
1223 } 1161 }
1224 } 1162 }
1225 }
1226 1163
1227 if (numitems == 0) 1164 buf << (numitems ? "T<This shop contains:>\n\n"
1228 { 1165 : "T<This shop is currently empty.>");
1229 new_draw_info (NDI_UNIQUE, 0, op, "The shop is currently empty.\n");
1230 free (items);
1231 return;
1232 }
1233 1166
1234 qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); 1167 qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort);
1235
1236 new_draw_info (NDI_UNIQUE, 0, op, "\nThe shop contains:");
1237 1168
1238 for (i = 0; i < numitems; i++) 1169 for (i = 0; i < numitems; i++)
1239 { 1170 {
1240 /* Collapse items of the same name together */ 1171 /* Collapse items of the same name together */
1241 if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) 1172 if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real))
1242 {
1243 items[i + 1].nrof += items[i].nrof; 1173 items[i + 1].nrof += items[i].nrof;
1244 free (items[i].item_sort);
1245 free (items[i].item_real);
1246 }
1247 else 1174 else
1248 {
1249 new_draw_info_format (NDI_UNIQUE, 0, op, "%d %s",
1250 items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); 1175 buf.printf (" %4d %s\n", items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real);
1176
1251 free (items[i].item_sort); 1177 free (items[i].item_sort);
1252 free (items[i].item_real); 1178 free (items[i].item_real);
1253 }
1254 } 1179 }
1180
1181 op->contr->infobox (MSG_CHANNEL ("shopitems"), buf);
1255 1182
1256 free (items); 1183 free (items);
1257} 1184}
1258 1185
1259/* elmex: this function checks whether the object is in a shop */ 1186/* elmex: this function checks whether the object is in a shop */
1260bool 1187bool
1261is_in_shop (object *o) 1188is_in_shop (object *o)
1262{ 1189{
1263 if (!o->map) 1190 if (!o->is_on_map ())
1264 return false; 1191 return false;
1265 1192
1266 return is_in_shop (o->map, o->x, o->y); 1193 return is_in_shop (o->map, o->x, o->y);
1267} 1194}
1268 1195
1276is_in_shop (maptile *map, int x, int y) 1203is_in_shop (maptile *map, int x, int y)
1277{ 1204{
1278 for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above) 1205 for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above)
1279 if (QUERY_FLAG (floor, FLAG_IS_FLOOR)) 1206 if (QUERY_FLAG (floor, FLAG_IS_FLOOR))
1280 return floor->type == SHOP_FLOOR; 1207 return floor->type == SHOP_FLOOR;
1208
1281 return false; 1209 return false;
1282} 1210}
1283 1211

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines