Crossfire Server, Trunk
shop_inventory.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
19 #include "global.h"
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "ob_methods.h"
25 #include "ob_types.h"
26 #include "sounds.h"
27 #include "sproto.h"
28 
29 static method_ret shop_inventory_type_apply(object *lighter, object *applier, int aflags);
30 
36 }
37 
41 typedef struct shopinv {
42  char *item_sort;
43  char *item_real;
44  uint16_t type;
45  uint32_t nrof;
46 } shopinv;
47 
61 static int shop_sort(const void *a1, const void *a2) {
62  const shopinv *s1 = (const shopinv *)a1, *s2 = (const shopinv *)a2;
63 
64  if (s1->type < s2->type)
65  return -1;
66  if (s1->type > s2->type)
67  return 1;
68  /* the type is the same (what atoi gets), so do a strcasecmp to sort
69  * via alphabetical order
70  */
71  return strcasecmp(s1->item_sort, s2->item_sort);
72 }
73 
83 static void add_shop_item(object *tmp, shopinv *items, size_t *numitems) {
84  /* clear unpaid flag so that doesn't come up in query
85  * string. We clear nrof so that we can better sort
86  * the object names.
87  */
88  char name[MAX_BUF];
89 
91  items[*numitems].nrof = tmp->nrof;
92  /* Non mergable items have nrof of 0, but count them as one
93  * so the display is properly.
94  */
95  if (tmp->nrof == 0)
96  items[*numitems].nrof++;
97  items[*numitems].type = tmp->type;
99  items[*numitems].item_sort = strdup_local(name);
101  items[*numitems].item_real = strdup_local(name);
102  (*numitems)++;
103 
105 }
106 
119 static method_ret shop_inventory_type_apply(object *lighter, object *applier, int aflags) {
120  size_t i, j, numitems = 0, numallocated = 0;
121  object *stack;
122  shopinv *items;
123  (void)lighter;
124  (void)aflags;
125 
126  if (applier->type != PLAYER)
127  return METHOD_UNHANDLED;
128 
130  "\nThe shop contains:");
131 
132  items = malloc(40*sizeof(shopinv));
133  numallocated = 40;
134 
135  /* Find all the appropriate items */
136  for (i = 0; i < MAP_WIDTH(applier->map); i++) {
137  for (j = 0; j < MAP_HEIGHT(applier->map); j++) {
138  stack = GET_MAP_OB(applier->map, i, j);
139 
140  while (stack) {
141  if (QUERY_FLAG(stack, FLAG_UNPAID)) {
142  if (numitems == numallocated) {
143  items = realloc(items, sizeof(shopinv)*(numallocated+10));
144  if (!items) {
145  LOG(llevError, "shop_inventory: couldn't allocate memory!\n");
147  }
148  numallocated += 10;
149  }
150  add_shop_item(stack, items, &numitems);
151  }
152  stack = stack->above;
153  }
154  }
155  }
156  if (numitems == 0) {
158  "The shop is currently empty.\n");
159  free(items);
160  return METHOD_OK;
161  }
162  qsort(items, numitems, sizeof(shopinv), (int (*)(const void *, const void *))shop_sort);
163 
164  for (i = 0; i < numitems; i++) {
165  /* Collapse items of the same name together */
166  if ((i+1) < numitems && !strcmp(items[i].item_real, items[i+1].item_real)) {
167  items[i+1].nrof += items[i].nrof;
168  free(items[i].item_sort);
169  free(items[i].item_real);
170  } else {
172  "%d %s",
173  items[i].nrof ? items[i].nrof : 1,
174  items[i].nrof == 1 ? items[i].item_sort : items[i].item_real);
175  free(items[i].item_sort);
176  free(items[i].item_real);
177  }
178  }
179  free(items);
180  return METHOD_OK;
181 }
GET_MAP_OB
#define GET_MAP_OB(M, X, Y)
Definition: map.h:173
PLAYER
@ PLAYER
Definition: object.h:107
global.h
llevError
@ llevError
Definition: logger.h:11
shopinv::item_sort
char * item_sort
Definition: shop_inventory.c:42
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
strdup_local
#define strdup_local
Definition: compat.h:29
obj::map
struct mapdef * map
Definition: object.h:300
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
shopinv
struct shopinv shopinv
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
shopinv
Definition: shop_inventory.c:41
Ice.tmp
int tmp
Definition: Ice.py:207
shop_sort
static int shop_sort(const void *a1, const void *a2)
Definition: shop_inventory.c:61
shopinv::nrof
uint32_t nrof
Definition: shop_inventory.c:45
add_shop_item
static void add_shop_item(object *tmp, shopinv *items, size_t *numitems)
Definition: shop_inventory.c:83
METHOD_UNHANDLED
#define METHOD_UNHANDLED
Definition: ob_methods.h:16
fatal
void fatal(enum fatal_error err)
Definition: utils.c:580
init_type_shop_inventory
void init_type_shop_inventory(void)
Definition: shop_inventory.c:34
sproto.h
shopinv::type
uint16_t type
Definition: shop_inventory.c:44
MSG_TYPE_SHOP
#define MSG_TYPE_SHOP
Definition: newclient.h:403
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
MAP_WIDTH
#define MAP_WIDTH(m)
Definition: map.h:78
MAX_BUF
#define MAX_BUF
Definition: define.h:35
register_apply
void register_apply(int ob_type, apply_func method)
Definition: ob_types.c:62
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
sounds.h
obj::type
uint8_t type
Definition: object.h:343
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:262
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
shopinv::item_real
char * item_real
Definition: shop_inventory.c:43
query_base_name
void query_base_name(const object *op, int plural, char *buf, size_t size)
Definition: item.c:686
SHOP_INVENTORY
@ SHOP_INVENTORY
Definition: object.h:238
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
MSG_TYPE_SHOP_LISTING
#define MSG_TYPE_SHOP_LISTING
Definition: newclient.h:508
MAP_HEIGHT
#define MAP_HEIGHT(m)
Definition: map.h:80
strcasecmp
int strcasecmp(const char *s1, const char *s2)
draw_ext_info
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:309
FLAG_UNPAID
#define FLAG_UNPAID
Definition: define.h:236
ob_methods.h
obj::above
struct obj * above
Definition: object.h:291
shop_inventory_type_apply
static method_ret shop_inventory_type_apply(object *lighter, object *applier, int aflags)
Definition: shop_inventory.c:119
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
draw_ext_info_format
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:319
give.name
name
Definition: give.py:27