Debian Codegraph @

types/converter/converter.c

raw symbols SCIP
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 <[email protected]>.
12 */
13
14 /**
15 * @file
16 * The implementation of the Converter class of objects.
17 */
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
41 /**
42 * Wildcard match where 'string' contains a '*' at the start, end, or both
43 */
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
74 /**
75 * Initializer for the CONVERTER object type.
76 */
77 void init_type_converter(void) {
78 register_move_on(CONVERTER, converter_type_move_on);
79 }
80
81 /**
82 * Transforms an item into another item.
83 * @param item The object that triggered the converter - if it isn't of a type
84 * accepted by the converter, nothing will happen
85 * @param converter The object that is doing the conversion
86 * @retval -1 If something went wrong when attempting the conversion
87 * @retval 0 If the item was not converted
88 * @retval 1 If the item got converted
89 */
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);
124 object_free_drop_inventory(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);
138 object_free_drop_inventory(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);
155 CLEAR_FLAG(item, FLAG_IS_A_TEMPLATE);
156 object_unset_flag_inv(item, FLAG_IS_A_TEMPLATE);
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);
174 object_free_drop_inventory(item);
175 return -1;
176 }
177 object_insert_in_map_at(item, converter->map, converter, 0, converter->x, converter->y);
178 return 1;
179 }
180
181 /**
182 * Move on this Converter object.
183 * @param context The method context
184 * @param trap The Converter we're moving on
185 * @param victim The object moving over this one
186 * @param originator The object that caused the move_on event
187 * @return METHOD_OK
188 */
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);
197 draw_ext_info_format(NDI_UNIQUE, 0, originator, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
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 }