Crossfire Server, Trunk
converter.cpp
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 <string.h>
22 #include <stdlib.h>
23 
24 #include "ob_methods.h"
25 #include "ob_types.h"
26 #include "shop.h"
27 #include "sounds.h"
28 #include "sproto.h"
29 
30 /*
31  * convert_item() returns 1 if anything was converted, 0 if the item was not
32  * what the converter wants, -1 if the converter is broken.
33  */
34 #define CONV_FROM(xyz) xyz->slaying
35 #define CONV_TO(xyz) xyz->other_arch
36 #define CONV_NR(xyz) (unsigned char)xyz->stats.sp
37 #define CONV_LIMIT(xyz) ((xyz->stats.wc > 0) ? ((unsigned long)xyz->stats.wc) : INT32_MAX)
38 #define CONV_NEED(xyz) (unsigned long)xyz->stats.food
39 #define CONV_FROM_MATCH(xyz,_match) (CONV_FROM(xyz) == (_match) || ((strchr(CONV_FROM(xyz),'*')) && wildcard_match(CONV_FROM(xyz),(_match))))
40 
41 static int convert_item(object *item, object *converter);
42 
46 static int wildcard_match(const char *string,const char *candidate)
47 {
48  bool head=FALSE,tail=FALSE;
49  char *str;
50  char *m;
51  bool ret;
52 
53  if ( string[0]=='*' && string[1]==0 ) return 1; // Trivial match-all case
54  if ( string[0] == '*' ) head=TRUE;
55  if ( string[strlen(string)-1] == '*' ) tail=TRUE;
56  str=strdup(&string[head?1:0]);
57  if ( !str ) return FALSE;
58  if ( tail ) str[strlen(str)-1]=0;
59  /* 'str' is now the text to match without the wildcard */
60  ret=FALSE;
61  if ( head && tail ) {
62  m=strstr(str,candidate);
63  if ( m ) ret=TRUE;
64  } else if ( tail ) {
65  if ( strncmp(str,candidate,strlen(str)) == 0 ) ret=TRUE;
66  } else {
67  if ( strlen(candidate) >= strlen(str) &&
68  strcmp(&candidate[strlen(candidate)-strlen(str)],str) == 0 ) ret=TRUE;
69  }
70  free(str);
71  return ret;
72 }
73 
74 static method_ret converter_type_move_on(object *trap, object *victim, object *originator);
75 
79 void init_type_converter(void) {
81 }
82 
92 static int convert_item(object *item, object *converter) {
93  int nr = 0;
94  uint32_t price_in;
95 
96  /* We make some assumptions - we assume if it takes money as it type,
97  * it wants some amount. We don't make change (ie, if something costs
98  * 3 gp and player drops a platinum, tough luck)
99  */
100  if (!strcmp(CONV_FROM(converter), "money")) {
101  int cost;
102 
103  if (item->type != MONEY)
104  return 0;
105 
106  nr = MIN(CONV_LIMIT(converter), (item->nrof*item->value)/CONV_NEED(converter));
107  if (!nr)
108  return 0;
109 
110  cost = nr*CONV_NEED(converter)/item->value;
111  /* take into account rounding errors */
112  if (nr*CONV_NEED(converter)%item->value)
113  cost++;
115 
116  price_in = cost*item->value;
117  } else {
118  if (item->type == PLAYER
119  || !CONV_FROM_MATCH(converter,item->arch->name)
120  || (CONV_NEED(converter) && CONV_NEED(converter) > item->nrof))
121  return 0;
122 
123  /* silently burn unpaid items (only if they match what we want) */
124  if (QUERY_FLAG(item, FLAG_UNPAID)) {
127  item = create_archetype("burnout");
128  if (item != NULL)
129  object_insert_in_map_at(item, converter->map, converter, 0, converter->x, converter->y);
130  return 1;
131  }
132 
133  if (CONV_NEED(converter)) {
134  nr = MIN(CONV_LIMIT(converter), item->nrof/CONV_NEED(converter));
135  object_decrease_nrof(item, nr*CONV_NEED(converter));
136  price_in = nr*CONV_NEED(converter)*item->value;
137  } else {
138  price_in = item->value;
141  }
142  }
143 
144  if (converter->inv != NULL) {
145  int i;
146  object *ob_to_copy;
147 
148  /* select random object from inventory to copy */
149  ob_to_copy = converter->inv;
150  i = 1;
151  FOR_BELOW_PREPARE(converter->inv, ob) {
152  if (rndm(0, i) == 0)
153  ob_to_copy = ob;
154  i++;
155  } FOR_BELOW_FINISH();
156  item = object_create_clone(ob_to_copy);
159  } else {
160  if (converter->other_arch == NULL) {
161  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);
162  return -1;
163  }
164  item = object_create_arch(converter->other_arch);
165  fix_generated_item(item, converter, 0, 0, GT_MINIMAL);
166  }
167 
168  if (CONV_NR(converter))
169  item->nrof = CONV_NR(converter);
170  if (nr)
171  item->nrof *= nr;
172  if (item->type != MONEY && shop_contains(converter))
174  else if (price_in < item->nrof*item->value && settings.allow_broken_converters == FALSE) {
175  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);
177  return -1;
178  }
179  object_insert_in_map_at(item, converter->map, converter, 0, converter->x, converter->y);
180  return 1;
181 }
182 
190 static method_ret converter_type_move_on(object *trap, object *victim, object *originator) {
191  if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
192  return METHOD_OK;
193  if (convert_item(victim, trap) < 0) {
194  object *op;
195  char name[MAX_BUF];
196 
197  query_name(trap, name, MAX_BUF);
199  "The %s seems to be broken!", name);
200 
201  op = create_archetype("burnout");
202  if (op != NULL)
203  object_insert_in_map_at(op, trap->map, trap, 0, trap->x, trap->y);
204  }
205  common_post_ob_move_on(trap, victim, originator);
206  return METHOD_OK;
207 }
PLAYER
@ PLAYER
Definition: object.h:110
global.h
settings
struct Settings settings
Definition: init.cpp:139
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:51
Settings::allow_broken_converters
int allow_broken_converters
Definition: global.h:320
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
object::inv
object * inv
Definition: object.h:296
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
FALSE
#define FALSE
Definition: compat.h:14
FOR_BELOW_PREPARE
#define FOR_BELOW_PREPARE(op_, it_)
Definition: define.h:704
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
object::x
int16_t x
Definition: object.h:333
object::map
struct mapstruct * map
Definition: object.h:303
guildjoin.ob
ob
Definition: guildjoin.py:42
register_move_on
void register_move_on(int ob_type, move_on_func method)
Definition: ob_types.cpp:89
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,...) PRINTF_ARGS(6
fix_generated_item
void fix_generated_item(object *op, object *creator, int difficulty, int max_magic, int flags)
Definition: treasure.cpp:898
MIN
#define MIN(x, y)
Definition: compat.h:21
converter_type_move_on
static method_ret converter_type_move_on(object *trap, object *victim, object *originator)
Definition: converter.cpp:190
rndm
int rndm(int min, int max)
Definition: utils.cpp:162
mapstruct::path
char path[HUGE_BUF]
Definition: map.h:357
FOR_BELOW_FINISH
#define FOR_BELOW_FINISH()
Definition: define.h:711
FLAG_IS_A_TEMPLATE
#define FLAG_IS_A_TEMPLATE
Definition: define.h:366
object::y
int16_t y
Definition: object.h:333
m
static event_registration m
Definition: citylife.cpp:425
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.cpp:1560
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.cpp:585
CONV_NEED
#define CONV_NEED(xyz)
Definition: converter.cpp:38
common_pre_ob_move_on
method_ret common_pre_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.cpp:35
make_face_from_files.str
str
Definition: make_face_from_files.py:24
CONV_FROM_MATCH
#define CONV_FROM_MATCH(xyz, _match)
Definition: converter.cpp:39
CONVERTER
@ CONVERTER
Definition: object.h:219
object_create_arch
object * object_create_arch(archetype *at)
Definition: arch.cpp:298
CONV_NR
#define CONV_NR(xyz)
Definition: converter.cpp:36
sproto.h
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.cpp:2098
object::other_arch
struct archetype * other_arch
Definition: object.h:421
object_create_clone
object * object_create_clone(object *asrc)
Definition: object.cpp:3902
MAX_BUF
#define MAX_BUF
Definition: define.h:35
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:278
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
sounds.h
object_decrease_nrof
object * object_decrease_nrof(object *op, uint32_t i)
Definition: object.cpp:2670
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:262
object::name
sstring name
Definition: object.h:317
item
Definition: item.py:1
reputation.victim
victim
Definition: reputation.py:14
give.op
op
Definition: give.py:33
shop.h
object_unset_flag_inv
void object_unset_flag_inv(object *op, int flag)
Definition: object.cpp:3249
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
MSG_TYPE_APPLY_FAILURE
#define MSG_TYPE_APPLY_FAILURE
Definition: newclient.h:604
wildcard_match
static int wildcard_match(const char *string, const char *candidate)
Definition: converter.cpp:46
CONV_FROM
#define CONV_FROM(xyz)
Definition: converter.cpp:34
object_remove
void object_remove(object *op)
Definition: object.cpp:1833
FLAG_UNPAID
#define FLAG_UNPAID
Definition: define.h:236
init_type_converter
void init_type_converter(void)
Definition: converter.cpp:79
METHOD_ERROR
#define METHOD_ERROR
Definition: ob_methods.h:17
ob_methods.h
diamondslots.cost
int cost
Definition: diamondslots.py:21
convert_item
static int convert_item(object *item, object *converter)
Definition: converter.cpp:92
shop_contains
bool shop_contains(object *ob)
Definition: shop.cpp:1296
TRUE
#define TRUE
Definition: compat.h:11
MSG_TYPE_APPLY
#define MSG_TYPE_APPLY
Definition: newclient.h:408
common_post_ob_move_on
void common_post_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.cpp:67
GT_MINIMAL
@ GT_MINIMAL
Definition: treasure.h:36
MONEY
@ MONEY
Definition: object.h:140
give.name
name
Definition: give.py:27
CONV_LIMIT
#define CONV_LIMIT(xyz)
Definition: converter.cpp:37