… | |
… | |
1107 | } |
1107 | } |
1108 | |
1108 | |
1109 | void |
1109 | void |
1110 | shop_listing (object *sign, object *op) |
1110 | shop_listing (object *sign, object *op) |
1111 | { |
1111 | { |
1112 | int i, j, numitems = 0, numallocated = 0, x1, x2, y1, y2; |
1112 | int i, j, x1, x2, y1, y2; |
1113 | const char *shop_coords = sign->kv (shstr_shop_coords); |
1113 | const char *shop_coords = sign->kv (shstr_shop_coords); |
1114 | object *stack; |
1114 | object *stack; |
1115 | shopinv *items; |
1115 | shopinv *items; |
1116 | |
1116 | |
1117 | /* Should never happen, but just in case a monster does apply a sign */ |
1117 | /* Should never happen, but just in case a monster does apply a sign */ |
1118 | if (op->type != PLAYER) |
1118 | if (!op->is_player ()) |
1119 | return; |
1119 | return; |
|
|
1120 | |
|
|
1121 | dynbuf_text buf (4096, 4096); |
1120 | |
1122 | |
1121 | if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) |
1123 | if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) |
1122 | { |
1124 | { |
1123 | x1 = 0; |
1125 | x1 = 0; |
1124 | y1 = 0; |
1126 | y1 = 0; |
1125 | x2 = op->map->width - 1; |
1127 | x2 = op->map->width - 1; |
1126 | y2 = op->map->height - 1; |
1128 | y2 = op->map->height - 1; |
1127 | } |
1129 | } |
1128 | |
1130 | |
1129 | items = (shopinv *) malloc (40 * sizeof (shopinv)); |
|
|
1130 | numallocated = 40; |
1131 | int numallocated = 40; |
|
|
1132 | int numitems = 0; |
|
|
1133 | items = (shopinv *)malloc (sizeof (shopinv) * numallocated); |
1131 | |
1134 | |
1132 | /* Find all the appropriate items */ |
1135 | /* Find all the appropriate items */ |
1133 | for (i = x1; i <= x2; i++) |
1136 | for (i = x1; i <= x2; i++) |
1134 | { |
|
|
1135 | for (j = y1; j < y2; j++) |
1137 | for (j = y1; j < y2; j++) |
|
|
1138 | if (is_in_shop (op->map, i, j)) |
1136 | { |
1139 | { |
1137 | if (is_in_shop (op->map, i, j)) |
1140 | stack = GET_MAP_OB (op->map, i, j); |
|
|
1141 | |
|
|
1142 | while (stack) |
1138 | { |
1143 | { |
1139 | stack = GET_MAP_OB (op->map, i, j); |
1144 | if (QUERY_FLAG (stack, FLAG_UNPAID)) |
1140 | |
|
|
1141 | while (stack) |
|
|
1142 | { |
1145 | { |
1143 | if (QUERY_FLAG (stack, FLAG_UNPAID)) |
|
|
1144 | { |
|
|
1145 | if (numitems == numallocated) |
1146 | if (numitems == numallocated) |
1146 | { |
|
|
1147 | items = (shopinv *) realloc (items, sizeof (shopinv) * (numallocated + 10)); |
1147 | items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2)); |
1148 | numallocated += 10; |
|
|
1149 | } |
|
|
1150 | |
1148 | |
1151 | add_shop_item (stack, items, &numitems, &numallocated); |
1149 | add_shop_item (stack, items, &numitems, &numallocated); |
1152 | } |
|
|
1153 | |
|
|
1154 | stack = stack->above; |
|
|
1155 | } |
1150 | } |
|
|
1151 | |
|
|
1152 | stack = stack->above; |
1156 | } |
1153 | } |
1157 | } |
1154 | } |
1158 | } |
|
|
1159 | |
1155 | |
1160 | if (numitems == 0) |
1156 | buf << (numitems ? "T<This shop contains:>\n\n" |
1161 | { |
1157 | : "T<This shop is currently empty.>"); |
1162 | new_draw_info (NDI_UNIQUE, 0, op, "The shop is currently empty.\n"); |
|
|
1163 | free (items); |
|
|
1164 | return; |
|
|
1165 | } |
|
|
1166 | |
1158 | |
1167 | qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); |
1159 | qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); |
1168 | |
|
|
1169 | new_draw_info (NDI_UNIQUE, 0, op, "\nThe shop contains:"); |
|
|
1170 | |
1160 | |
1171 | for (i = 0; i < numitems; i++) |
1161 | for (i = 0; i < numitems; i++) |
1172 | { |
1162 | { |
1173 | /* Collapse items of the same name together */ |
1163 | /* Collapse items of the same name together */ |
1174 | if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) |
1164 | if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) |
1175 | { |
|
|
1176 | items[i + 1].nrof += items[i].nrof; |
1165 | items[i + 1].nrof += items[i].nrof; |
1177 | free (items[i].item_sort); |
|
|
1178 | free (items[i].item_real); |
|
|
1179 | } |
|
|
1180 | else |
1166 | else |
1181 | { |
|
|
1182 | new_draw_info_format (NDI_UNIQUE, 0, op, "%d %s", |
|
|
1183 | items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); |
1167 | buf.printf (" C<%4d %s>\n", items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); |
|
|
1168 | |
1184 | free (items[i].item_sort); |
1169 | free (items[i].item_sort); |
1185 | free (items[i].item_real); |
1170 | free (items[i].item_real); |
1186 | } |
|
|
1187 | } |
1171 | } |
|
|
1172 | |
|
|
1173 | op->contr->infobox (MSG_CHANNEL ("shopitems"), buf); |
1188 | |
1174 | |
1189 | free (items); |
1175 | free (items); |
1190 | } |
1176 | } |
1191 | |
1177 | |
1192 | /* elmex: this function checks whether the object is in a shop */ |
1178 | /* elmex: this function checks whether the object is in a shop */ |