Crossfire Server, Trunk  R21466
converter.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 
18 #include "global.h"
19 
20 #include <string.h>
21 #include <stdlib.h>
22 
23 #include "ob_methods.h"
24 #include "ob_types.h"
25 #include "shop.h"
26 #include "sounds.h"
27 #include "sproto.h"
28 
29 /*
30  * convert_item() returns 1 if anything was converted, 0 if the item was not
31  * what the converter wants, -1 if the converter is broken.
32  */
33 #define CONV_FROM(xyz) xyz->slaying
34 #define CONV_TO(xyz) xyz->other_arch
35 #define CONV_NR(xyz) (unsigned char)xyz->stats.sp
36 #define CONV_NEED(xyz) (unsigned long)xyz->stats.food
37 #define CONV_FROM_MATCH(xyz,_match) (CONV_FROM(xyz) == (_match) || (strchr(CONV_FROM(xyz),'*')) && wildcard_match(CONV_FROM(xyz),(_match)))
38 
39 static int convert_item(object *item, object *converter);
40 
44 static int wildcard_match(const char *string,const char *candidate)
45 {
46  bool head=FALSE,tail=FALSE;
47  char *str;
48  char *m;
49  bool ret;
50 
51  if ( string[0]=='*' && string[1]==0 ) return 1; // Trivial match-all case
52  if ( string[0] == '*' ) head=TRUE;
53  if ( string[strlen(string)-1] == '*' ) tail=TRUE;
54  str=strdup(&string[head?1:0]);
55  if ( !str ) return FALSE;
56  if ( tail ) str[strlen(str)-1]=0;
57  /* 'str' is now the text to match without the wildcard */
58  ret=FALSE;
59  if ( head && tail ) {
60  m=strstr(str,candidate);
61  if ( m ) ret=TRUE;
62  } else if ( tail ) {
63  if ( strncmp(str,candidate,strlen(str)) == 0 ) ret=TRUE;
64  } else {
65  if ( strlen(candidate) >= strlen(str) &&
66  strcmp(&candidate[strlen(candidate)-strlen(str)],str) == 0 ) ret=TRUE;
67  }
68  free(str);
69  return ret;
70 }
71 
72 static method_ret converter_type_move_on(ob_methods *context, object *trap, object *victim, object *originator);
73 
77 void init_type_converter(void) {
79 }
80 
90 static int convert_item(object *item, object *converter) {
91  int nr = 0;
92  uint32_t price_in;
93 
94  /* We make some assumptions - we assume if it takes money as it type,
95  * it wants some amount. We don't make change (ie, if something costs
96  * 3 gp and player drops a platinum, tough luck)
97  */
98  if (!strcmp(CONV_FROM(converter), "money")) {
99  int cost;
100 
101  if (item->type != MONEY)
102  return 0;
103 
104  nr = (item->nrof*item->value)/CONV_NEED(converter);
105  if (!nr)
106  return 0;
107 
108  cost = nr*CONV_NEED(converter)/item->value;
109  /* take into account rounding errors */
110  if (nr*CONV_NEED(converter)%item->value)
111  cost++;
112  object_decrease_nrof(item, cost);
113 
114  price_in = cost*item->value;
115  } else {
116  if (item->type == PLAYER
117  || !CONV_FROM_MATCH(converter,item->arch->name)
118  || (CONV_NEED(converter) && CONV_NEED(converter) > item->nrof))
119  return 0;
120 
121  /* silently burn unpaid items (only if they match what we want) */
122  if (QUERY_FLAG(item, FLAG_UNPAID)) {
123  object_remove(item);
125  item = create_archetype("burnout");
126  if (item != NULL)
127  object_insert_in_map_at(item, converter->map, converter, 0, converter->x, converter->y);
128  return 1;
129  }
130 
131  if (CONV_NEED(converter)) {
132  nr = item->nrof/CONV_NEED(converter);
133  object_decrease_nrof(item, nr*CONV_NEED(converter));
134  price_in = nr*CONV_NEED(converter)*item->value;
135  } else {
136  price_in = item->value;
137  object_remove(item);
139  }
140  }
141 
142  if (converter->inv != NULL) {
143  int i;
144  object *ob_to_copy;
145 
146  /* select random object from inventory to copy */
147  ob_to_copy = converter->inv;
148  i = 1;
149  FOR_BELOW_PREPARE(converter->inv, ob) {
150  if (rndm(0, i) == 0)
151  ob_to_copy = ob;
152  i++;
153  } FOR_BELOW_FINISH();
154  item = object_create_clone(ob_to_copy);
157  } else {
158  if (converter->other_arch == NULL) {
159  LOG(llevError, "move_creator: Converter doesn't have other arch set: %s (%s, %d, %d)\n", converter->name ? converter->name : "(null)", converter->map->path, converter->x, converter->y);
160  return -1;
161  }
162  item = object_create_arch(converter->other_arch);
163  fix_generated_item(item, converter, 0, 0, GT_MINIMAL);
164  }
165 
166  if (CONV_NR(converter))
167  item->nrof = CONV_NR(converter);
168  if (nr)
169  item->nrof *= nr;
170  if (item->type != MONEY && shop_contains(converter))
171  SET_FLAG(item, FLAG_UNPAID);
172  else if (price_in < item->nrof*item->value && settings.allow_broken_converters == FALSE) {
173  LOG(llevError, "Broken converter %s at %s (%d, %d) in value %d, out value %d for %s\n", converter->name, converter->map->path, converter->x, converter->y, price_in, item->nrof*item->value, item->name);
175  return -1;
176  }
177  object_insert_in_map_at(item, converter->map, converter, 0, converter->x, converter->y);
178  return 1;
179 }
180 
189 static method_ret converter_type_move_on(ob_methods *context, object *trap, object *victim, object *originator) {
190  if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
191  return METHOD_OK;
192  if (convert_item(victim, trap) < 0) {
193  object *op;
194  char name[MAX_BUF];
195 
196  query_name(trap, name, MAX_BUF);
198  "The %s seems to be broken!", name);
199 
200  op = create_archetype("burnout");
201  if (op != NULL)
202  object_insert_in_map_at(op, trap->map, trap, 0, trap->x, trap->y);
203  }
204  common_post_ob_move_on(trap, victim, originator);
205  return METHOD_OK;
206 }
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:316
char path[HUGE_BUF]
Definition: map.h:365
#define FLAG_UNPAID
Definition: define.h:236
bool shop_contains(object *ob)
Definition: shop.c:1267
#define SET_FLAG(xyz, p)
Definition: define.h:223
method_ret common_pre_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:51
#define MSG_TYPE_APPLY_FAILURE
Definition: newclient.h:599
#define METHOD_ERROR
Definition: ob_methods.h:17
Definition: object.h:137
int allow_broken_converters
Definition: global.h:319
#define TRUE
Definition: compat.h:10
static int wildcard_match(const char *string, const char *candidate)
Definition: converter.c:44
#define FALSE
Definition: compat.h:11
struct archt * other_arch
Definition: object.h:415
#define MSG_TYPE_APPLY
Definition: newclient.h:384
char method_ret
Definition: ob_methods.h:14
int rndm(int min, int max)
Definition: utils.c:162
void object_free_drop_inventory(object *ob)
Definition: object.c:1391
int16_t y
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1935
void init_type_converter(void)
Definition: converter.c:77
object * create_archetype(const char *name)
Definition: arch.c:620
void register_move_on(int ob_type, move_on_func method)
Definition: ob_types.c:89
static method_ret converter_type_move_on(ob_methods *context, object *trap, object *victim, object *originator)
Definition: converter.c:189
#define METHOD_OK
Definition: ob_methods.h:15
struct mapdef * map
Definition: object.h:297
#define CONV_FROM_MATCH(xyz, _match)
Definition: converter.c:37
const char * name
Definition: object.h:311
static int convert_item(object *item, object *converter)
Definition: converter.c:90
#define FLAG_IS_A_TEMPLATE
Definition: define.h:375
object * object_create_clone(object *asrc)
Definition: object.c:3853
uint32_t nrof
Definition: object.h:333
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
void fix_generated_item(object *op, object *creator, int difficulty, int max_magic, int flags)
Definition: treasure.c:1108
#define MAX_BUF
Definition: define.h:35
object * object_create_arch(archetype *at)
Definition: arch.c:736
int16_t x
Definition: object.h:326
void common_post_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:87
Definition: object.h:107
unsigned int uint32_t
Definition: win32.h:162
const char * name
Definition: treasure.h:83
object * object_decrease_nrof(object *op, uint32_t i)
Definition: object.c:2522
struct archt * arch
Definition: object.h:414
uint8_t type
Definition: object.h:339
struct Settings settings
Definition: init.c:39
void object_unset_flag_inv(object *op, int flag)
Definition: object.c:3119
#define CONV_NEED(xyz)
Definition: converter.c:36
#define CONV_NR(xyz)
Definition: converter.c:35
char * strdup(const char *str)
Definition: porting.c:200
struct obj * inv
Definition: object.h:290
#define NDI_UNIQUE
Definition: newclient.h:245
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
#define CONV_FROM(xyz)
Definition: converter.c:33
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:626
#define FOR_BELOW_PREPARE(op_, it_)
Definition: define.h:742
int32_t value
Definition: object.h:351
const char * name
Definition: object.h:468
#define FOR_BELOW_FINISH()
Definition: define.h:749
void object_remove(object *op)
Definition: object.c:1668