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.55 by root, Sun Sep 21 23:21:49 2008 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 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
9 * 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
10 * the Free Software Foundation, either version 3 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your 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,
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 GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24#include <global.h> 24#include <global.h>
25#include <spells.h> 25#include <spells.h>
26#include <skills.h> 26#include <skills.h>
42 42
43static void pay_from_container (object *pl, object *pouch, sint64 &to_pay); 43static void pay_from_container (object *pl, object *pouch, sint64 &to_pay);
44static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop); 44static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop);
45static double shop_specialisation_ratio (const object *item, const maptile *map); 45static double shop_specialisation_ratio (const object *item, const maptile *map);
46static double shop_greed (const maptile *map); 46static 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 47
51/* Added F_TRUE flag to define.h to mean that the price should not 48/* 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 49 * 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. 50 * 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 51 * This is needed for alchemy, to to determine what value of gold nuggets
129 { 126 {
130 if (tmp->arch != NULL) 127 if (tmp->arch != NULL)
131 { 128 {
132 if (flag == F_BUY) 129 if (flag == F_BUY)
133 { 130 {
134 LOG (llevError, "Asking for buy-value of unidentified object.\n"); 131 LOG (llevError | logBacktrace, "Asking for buy-value of unidentified object.\n");
135 val = tmp->arch->value * 50 * number; 132 val = tmp->arch->value * 50 * number;
136 } 133 }
137 else 134 else
138 { /* Trying to sell something, or get true value */ 135 { /* Trying to sell something, or get true value */
139 if (tmp->type == POTION) 136 if (tmp->type == POTION)
153 else 150 else
154 { /* No archetype with this object */ 151 { /* No archetype with this object */
155 LOG (llevDebug, "In sell item: Have object with no archetype: %s\n", &tmp->name); 152 LOG (llevDebug, "In sell item: Have object with no archetype: %s\n", &tmp->name);
156 if (flag == F_BUY) 153 if (flag == F_BUY)
157 { 154 {
158 LOG (llevError, "Asking for buy-value of unidentified object without arch.\n"); 155 LOG (llevError | logBacktrace, "Asking for buy-value of unidentified object without arch.\n");
159 val = number * tmp->value * 10; 156 val = number * tmp->value * 10;
160 } 157 }
161 else 158 else
162 val = number * tmp->value / 5; 159 val = number * tmp->value / 5;
163 } 160 }
209 * AND Cha = 30 will get optimal price. 206 * AND Cha = 30 will get optimal price.
210 * Thus charisma will never get useless. 207 * Thus charisma will never get useless.
211 * -b.e. edler@heydernet.de 208 * -b.e. edler@heydernet.de
212 */ 209 */
213 210
214 if (who != NULL && who->type == PLAYER) 211 if (who && who->type == PLAYER)
215 { 212 {
216 int lev_bargain = 0; 213 int lev_bargain = 0;
217 int lev_identify = 0; 214 int lev_identify = 0;
218 int idskill1 = 0;
219 int idskill2 = 0;
220 const typedata *tmptype;
221
222 tmptype = get_typedata (tmp->type);
223 215
224 if (find_skill_by_number (who, SK_BARGAINING)) 216 if (find_skill_by_number (who, SK_BARGAINING))
225 lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; 217 lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level;
226 218
227 if (tmptype) 219 if (const typedata *tmptype = get_typedata (tmp->type))
228 { 220 {
229 idskill1 = tmptype->identifyskill; 221 if (int idskill1 = tmptype->identifyskill)
230
231 if (idskill1)
232 { 222 {
233 idskill2 = tmptype->identifyskill2; 223 int idskill2 = tmptype->identifyskill2;
234 224
235 if (find_skill_by_number (who, idskill1)) 225 if (find_skill_by_number (who, idskill1))
236 lev_identify = find_skill_by_number (who, idskill1)->level; 226 lev_identify = find_skill_by_number (who, idskill1)->level;
237 227
238 if (idskill2 && find_skill_by_number (who, idskill2)) 228 if (idskill2 && find_skill_by_number (who, idskill2))
239 lev_identify += find_skill_by_number (who, idskill2)->level; 229 lev_identify += find_skill_by_number (who, idskill2)->level;
240 } 230 }
241 } 231 }
242 else
243 LOG (llevError, "Query_cost: item %s hasn't got a valid type\n", tmp->debug_desc ());
244 232
245 /* ratio determines how much of the price modification 233 /* ratio determines how much of the price modification
246 * will come from the basic stat charisma 234 * will come from the basic stat charisma
247 * the rest will come from the level in bargaining skill 235 * the rest will come from the level in bargaining skill
248 */ 236 */
421 409
422 if (coin == NULL) 410 if (coin == NULL)
423 return "nothing"; 411 return "nothing";
424 412
425 num = real_value / coin->value; 413 num = real_value / coin->value;
414
426 if (num == 1) 415 if (num == 1)
427 sprintf (buf, "about one %s", &coin->object::name); 416 sprintf (buf, "about one %s", &coin->object::name);
428 else if (num < 5) 417 else if (num < 5)
429 sprintf (buf, "a few %s", &coin->object::name_pl); 418 sprintf (buf, "a few %s", &coin->object::name_pl);
430 else if (num < 10) 419 else if (num < 10)
435 sprintf (buf, "lots of %s", &coin->object::name_pl); 424 sprintf (buf, "lots of %s", &coin->object::name_pl);
436 else if (num < 1000) 425 else if (num < 1000)
437 sprintf (buf, "a great many %s", &coin->object::name_pl); 426 sprintf (buf, "a great many %s", &coin->object::name_pl);
438 else 427 else
439 sprintf (buf, "a vast quantity of %s", &coin->object::name_pl); 428 sprintf (buf, "a vast quantity of %s", &coin->object::name_pl);
429
440 return buf; 430 return buf;
441 } 431 }
442 } 432 }
443 } 433 }
444 434
586 { 576 {
587 // This should not happen, but if it does, just merge the two. 577 // This should not happen, but if it does, just merge the two.
588 if (coin_objs [i]) 578 if (coin_objs [i])
589 { 579 {
590 LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); 580 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; 581 coin_objs[i]->nrof += tmp->nrof;
593 esrv_del_item (pl->contr, tmp->count);
594 tmp->destroy (); 582 tmp->destroy ();
595 } 583 }
596 else 584 else
597 { 585 {
598 tmp->remove (); 586 tmp->remove ();
599
600 if (pouch->type == PLAYER)
601 esrv_del_item (pl->contr, tmp->count);
602
603 coin_objs[i] = tmp; 587 coin_objs[i] = tmp;
604 } 588 }
605 589
606 break; 590 break;
607 } 591 }
649 } 633 }
650 634
651 for (i = 0; i < NUM_COINS; i++) 635 for (i = 0; i < NUM_COINS; i++)
652 { 636 {
653 if (coin_objs[i]->nrof) 637 if (coin_objs[i]->nrof)
654 {
655 object *tmp = insert_ob_in_ob (coin_objs[i], pouch); 638 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 639 else
667 coin_objs[i]->destroy (); 640 coin_objs[i]->destroy ();
668 } 641 }
669} 642}
670 643
693 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP); 666 unpaid_price += query_cost (item, pl, F_BUY | F_SHOP);
694 } 667 }
695 668
696 if (unpaid_price > player_wealth) 669 if (unpaid_price > player_wealth)
697 { 670 {
698 char buf[MAX_BUF]; 671 dynbuf_text buf;
699 char cost[MAX_BUF];
700 char missing[MAX_BUF];
701 672
702 snprintf (cost, MAX_BUF, "%s", cost_string_from_value (unpaid_price, 0)); 673 buf << "You have " << unpaid_count
703 snprintf (missing, MAX_BUF, "%s", cost_string_from_value (unpaid_price - player_wealth, 0)); 674 << " unpaid item(s) that would cost you " << cost_string_from_value (unpaid_price, 0)
675 << ". You need another " << cost_string_from_value (unpaid_price - player_wealth, 0)
676 << " to be able to afford that.";
704 677
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.", 678 pl->failmsg (buf);
706 unpaid_count, cost, missing);
707 new_draw_info (NDI_UNIQUE, 0, pl, buf);
708 679
709 return 0; 680 return 0;
710 } 681 }
711 else 682 else
712 return 1; 683 return 1;
741 SET_FLAG (op, FLAG_UNPAID); 712 SET_FLAG (op, FLAG_UNPAID);
742 return 0; 713 return 0;
743 } 714 }
744 else 715 else
745 { 716 {
746 object *tmp;
747
748 CLEAR_FLAG (op, FLAG_UNPAID); 717 CLEAR_FLAG (op, FLAG_UNPAID);
749 CLEAR_FLAG (op, FLAG_PLAYER_SOLD); 718 CLEAR_FLAG (op, FLAG_PLAYER_SOLD);
750 new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op)); 719 new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op));
751 tmp = merge_ob (op, NULL);
752 720
753 if (pl->type == PLAYER) 721 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); 722 esrv_update_item (UPD_FLAGS, pl, op);
762 }
763 723
764 goto next_item; 724 goto next_item;
765 } 725 }
766 } 726 }
767 } 727 }
786{ 746{
787 int count = 0; 747 int count = 0;
788 archetype *at = 0; 748 archetype *at = 0;
789 object *pouch = 0, *tmp = 0; 749 object *pouch = 0, *tmp = 0;
790 750
791 for (count = 0; coins[count] != NULL; count++) 751 for (count = 0; coins[count]; count++)
792 { 752 {
793 at = archetype::find (coins[count]); 753 at = archetype::find (coins[count]);
794 754
795 if (at == NULL) 755 if (at == NULL)
796 LOG (llevError, "Could not find %s archetype\n", coins[count]); 756 LOG (llevError, "Could not find %s archetype\n", coins[count]);
809 if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit)) 769 if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit))
810 { 770 {
811 if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) 771 if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n)
812 n = (pouch->weight_limit - pouch->carrying) / w; 772 n = (pouch->weight_limit - pouch->carrying) / w;
813 773
814 tmp = arch_to_object (at); 774 object *tmp = arch_to_object (at);
815 tmp->nrof = n; 775 tmp->nrof = n;
816 amount -= tmp->nrof * tmp->value; 776 amount -= tmp->nrof * tmp->value;
817 tmp = insert_ob_in_ob (tmp, pouch); 777 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 } 778 }
823 } 779 }
824 } 780 }
825 781
826 if (amount / at->value > 0) 782 if (amount / at->value > 0)
827 { 783 {
828 tmp = arch_to_object (at); 784 object *tmp = arch_to_object (at);
829 tmp->nrof = amount / tmp->value; 785 tmp->nrof = amount / tmp->value;
830 amount -= tmp->nrof * tmp->value; 786 amount -= tmp->nrof * tmp->value;
831 tmp = insert_ob_in_ob (tmp, pl); 787 pl->insert (tmp);
832 esrv_send_item (pl, tmp);
833 esrv_send_item (pl, pl);
834 } 788 }
835 } 789 }
836 } 790 }
837 791
838 if (amount != 0) 792 if (amount != 0)
841 795
842/* elmex: this is for the bank plugin :( */ 796/* elmex: this is for the bank plugin :( */
843sint64 797sint64
844pay_player_arch (object *pl, const char *arch, sint64 amount) 798pay_player_arch (object *pl, const char *arch, sint64 amount)
845{ 799{
846 archetype *at = archetype::find (arch);
847 object *tmp = NULL;
848
849 if (at == NULL)
850 return 0;
851
852 if (amount > 0) 800 if (amount)
853 { 801 {
854 tmp = arch_to_object (at); 802 object *ob = archetype::get (arch);
803
804 if (!ob)
805 return 0;
806
855 tmp->nrof = amount; 807 ob->nrof = amount;
856 tmp = insert_ob_in_ob (tmp, pl); 808 pl->insert (ob);
857 esrv_send_item (pl, tmp);
858 esrv_send_item (pl, pl);
859 } 809 }
860 810
861 return 1; 811 return 1;
862} 812}
863 813
866 * buy item. 816 * buy item.
867 * 817 *
868 * Modified to fill available race: gold containers before dumping 818 * Modified to fill available race: gold containers before dumping
869 * remaining coins in character's inventory. 819 * remaining coins in character's inventory.
870 */ 820 */
871void 821bool
872sell_item (object *op, object *pl) 822sell_item (object *op, object *pl)
873{ 823{
874 sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain; 824 sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain;
875 825
876 if (pl == NULL || pl->type != PLAYER) 826 if (pl == NULL || pl->type != PLAYER)
877 { 827 {
878 LOG (llevDebug, "Object other than player tried to sell something.\n"); 828 LOG (llevDebug, "Object other than player tried to sell something.\n");
879 return; 829 return false;
880 } 830 }
881 831
882 op->custom_name = 0; 832 op->custom_name = 0;
883 833
884 if (!amount) 834 if (!amount)
885 { 835 {
886 new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.", query_name (op)); 836 new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.",
887 837 query_name (op));
888 /* Even if the character doesn't get anything for it, it may still be 838 // elmex: change: the player now gets the item back if the shop is not
889 * worth something. If so, make it unpaid 839 // 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; 840 return false;
899 } 841 }
900 842
901 /* We compare the price with the one for a player 843 /* We compare the price with the one for a player
902 * without bargaining skill. 844 * without bargaining skill.
903 * This determins the amount of exp (if any) gained for bargaining. 845 * This determins the amount of exp (if any) gained for bargaining.
908 if (extra_gain > 0) 850 if (extra_gain > 0)
909 change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE); 851 change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE);
910 852
911 pay_player (pl, amount); 853 pay_player (pl, amount);
912 854
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)); 855 new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.",
856 query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op));
857 pl->play_sound (sound_find ("shop_sell"));
914 858
915 SET_FLAG (op, FLAG_UNPAID); 859 SET_FLAG (op, FLAG_UNPAID);
916 identify (op); 860 identify (op);
917}
918 861
862 return true;
863}
919 864
920/* returns a double that is the ratio of the price that a shop will offer for 865/* 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, 866 * item based on the shops specialisation. Does not take account of greed,
922 * returned value is between SPECIALISATION_EFFECT and 1. 867 * returned value is between SPECIALISATION_EFFECT and 1.
923 */ 868 */
1120{ 1065{
1121 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2; 1066 shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2;
1122 1067
1123 if (s1->type < s2->type) 1068 if (s1->type < s2->type)
1124 return -1; 1069 return -1;
1070
1125 if (s1->type > s2->type) 1071 if (s1->type > s2->type)
1126 return 1; 1072 return 1;
1127 1073
1128 /* the type is the same (what atoi gets), so do a strcasecmp to sort 1074 /* the type is the same (what atoi gets), so do a strcasecmp to sort
1129 * via alphabetical order 1075 * via alphabetical order
1179 1125
1180void 1126void
1181shop_listing (object *sign, object *op) 1127shop_listing (object *sign, object *op)
1182{ 1128{
1183 int i, j, numitems = 0, numallocated = 0, x1, x2, y1, y2; 1129 int i, j, numitems = 0, numallocated = 0, x1, x2, y1, y2;
1184 const char *shop_coords = get_ob_key_value (sign, "shop_coords"); 1130 const char *shop_coords = sign->kv (shstr_shop_coords);
1185 object *stack; 1131 object *stack;
1186 shopinv *items; 1132 shopinv *items;
1187 1133
1188 /* Should never happen, but just in case a monster does apply a sign */ 1134 /* Should never happen, but just in case a monster does apply a sign */
1189 if (op->type != PLAYER) 1135 if (op->type != PLAYER)
1262 1208
1263/* elmex: this function checks whether the object is in a shop */ 1209/* elmex: this function checks whether the object is in a shop */
1264bool 1210bool
1265is_in_shop (object *o) 1211is_in_shop (object *o)
1266{ 1212{
1267 if (!o->map) 1213 if (!o->is_on_map ())
1268 return false; 1214 return false;
1269 1215
1270 return is_in_shop (o->map, o->x, o->y); 1216 return is_in_shop (o->map, o->x, o->y);
1271} 1217}
1272 1218
1273/* elmex: this function checks whether we are in a shop or not */ 1219/* elmex: this function checks whether we are in a shop or not
1220 - change 2007-11-26: enhanced the O(n) case by stopping at the first
1221 floor tile. this possibly will make map bugs where shopfloors are above
1222 floors more obvious.
1223*/
1224
1274bool 1225bool
1275is_in_shop (maptile *map, int x, int y) 1226is_in_shop (maptile *map, int x, int y)
1276{ 1227{
1277 for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above) 1228 for (object *floor = GET_MAP_OB (map, x, y); floor; floor = floor->above)
1229 if (QUERY_FLAG (floor, FLAG_IS_FLOOR))
1278 if (floor->type == SHOP_FLOOR) 1230 return floor->type == SHOP_FLOOR;
1279 return true;
1280 1231
1281 return false; 1232 return false;
1282} 1233}
1234

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines