|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_utils_c = |
3 | * static char *rcsid_utils_c = |
3 | * "$Id: utils.C,v 1.8 2006/09/09 21:48:28 root Exp $"; |
4 | * "$Id: utils.C,v 1.11 2006/09/12 19:20:06 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
50 | * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
51 | * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
51 | * Generally, op should be the player/caster/hitter requesting the roll, |
52 | * Generally, op should be the player/caster/hitter requesting the roll, |
52 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
53 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
53 | */ |
54 | */ |
54 | |
55 | |
|
|
56 | int |
55 | int random_roll(int min, int max, const object *op, int goodbad) { |
57 | random_roll (int min, int max, const object *op, int goodbad) |
|
|
58 | { |
56 | int omin, diff, luck, base, ran; |
59 | int omin, diff, luck, base, ran; |
57 | |
60 | |
58 | omin = min; |
61 | omin = min; |
59 | diff = max - min + 1; |
62 | diff = max - min + 1; |
60 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
63 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
61 | |
64 | |
62 | if (max < 1 || diff < 1) { |
65 | if (max < 1 || diff < 1) |
|
|
66 | { |
63 | LOG(llevError, "Calling random_roll with min=%d max=%d\n", min, max); |
67 | LOG (llevError, "Calling random_roll with min=%d max=%d\n", min, max); |
64 | return(min); /* avoids a float exception */ |
68 | return (min); /* avoids a float exception */ |
65 | } |
69 | } |
66 | |
70 | |
67 | ran = RANDOM(); |
71 | ran = RANDOM (); |
68 | |
72 | |
69 | if (op->type != PLAYER) |
73 | if (op->type != PLAYER) |
70 | return((ran%diff)+min); |
74 | return ((ran % diff) + min); |
71 | |
75 | |
72 | luck = op->stats.luck; |
76 | luck = op->stats.luck; |
73 | if (RANDOM()%base < MIN(10, abs(luck))) { |
77 | if (RANDOM () % base < MIN (10, abs (luck))) |
|
|
78 | { |
74 | /* we have a winner */ |
79 | /* we have a winner */ |
75 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
80 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
76 | diff -= luck; |
81 | diff -= luck; |
77 | if (diff < 1) |
82 | if (diff < 1) |
78 | return(omin); /*check again*/ |
83 | return (omin); /*check again */ |
79 | ((goodbad) ? (min += luck) : (diff)); |
84 | ((goodbad) ? (min += luck) : (diff)); |
80 | |
85 | |
81 | return(MAX(omin, MIN(max, (ran%diff)+min))); |
86 | return (MAX (omin, MIN (max, (ran % diff) + min))); |
82 | } |
87 | } |
83 | return((ran%diff)+min); |
88 | return ((ran % diff) + min); |
84 | } |
89 | } |
85 | |
90 | |
86 | /* |
91 | /* |
87 | * This is a 64 bit version of random_roll above. This is needed |
92 | * This is a 64 bit version of random_roll above. This is needed |
88 | * for exp loss calculations for players changing religions. |
93 | * for exp loss calculations for players changing religions. |
89 | */ |
94 | */ |
90 | |
95 | |
|
|
96 | sint64 |
91 | sint64 random_roll64(sint64 min, sint64 max, const object *op, int goodbad) { |
97 | random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) |
|
|
98 | { |
92 | sint64 omin, diff, luck, ran; |
99 | sint64 omin, diff, luck, ran; |
93 | int base; |
100 | int base; |
94 | |
101 | |
95 | omin = min; |
102 | omin = min; |
96 | diff = max - min + 1; |
103 | diff = max - min + 1; |
97 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
104 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
98 | |
105 | |
99 | if (max < 1 || diff < 1) { |
106 | if (max < 1 || diff < 1) |
|
|
107 | { |
100 | #ifndef WIN32 |
108 | #ifndef WIN32 |
101 | LOG(llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); |
109 | LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); |
102 | #else |
110 | #else |
103 | LOG(llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); |
111 | LOG (llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); |
104 | #endif |
112 | #endif |
105 | return(min); /* avoids a float exception */ |
113 | return (min); /* avoids a float exception */ |
106 | } |
114 | } |
107 | |
115 | |
108 | /* Don't know of a portable call to get 64 bit random values. |
116 | /* Don't know of a portable call to get 64 bit random values. |
109 | * So make a call to get two 32 bit random numbers, and just to |
117 | * So make a call to get two 32 bit random numbers, and just to |
110 | * a little byteshifting. Do make sure the first one is only |
118 | * a little byteshifting. Do make sure the first one is only |
111 | * 32 bit, so we don't get skewed results |
119 | * 32 bit, so we don't get skewed results |
112 | */ |
120 | */ |
113 | ran = (RANDOM() & 0xffffffff) | ((sint64)RANDOM() << 32); |
121 | ran = (RANDOM () & 0xffffffff) | ((sint64) RANDOM () << 32); |
114 | |
122 | |
115 | if (op->type != PLAYER) |
123 | if (op->type != PLAYER) |
116 | return((ran%diff)+min); |
124 | return ((ran % diff) + min); |
117 | |
125 | |
118 | luck = op->stats.luck; |
126 | luck = op->stats.luck; |
119 | if (RANDOM()%base < MIN(10, abs(luck))) { |
127 | if (RANDOM () % base < MIN (10, abs (luck))) |
|
|
128 | { |
120 | /* we have a winner */ |
129 | /* we have a winner */ |
121 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
130 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
122 | diff -= luck; |
131 | diff -= luck; |
123 | if (diff < 1) |
132 | if (diff < 1) |
124 | return(omin); /*check again*/ |
133 | return (omin); /*check again */ |
125 | ((goodbad) ? (min += luck) : (diff)); |
134 | ((goodbad) ? (min += luck) : (diff)); |
126 | |
135 | |
127 | return(MAX(omin, MIN(max, (ran%diff)+min))); |
136 | return (MAX (omin, MIN (max, (ran % diff) + min))); |
128 | } |
137 | } |
129 | return((ran%diff)+min); |
138 | return ((ran % diff) + min); |
130 | } |
139 | } |
131 | |
140 | |
132 | /* |
141 | /* |
133 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
142 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
134 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
143 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
135 | * Generally, op should be the player/caster/hitter requesting the roll, |
144 | * Generally, op should be the player/caster/hitter requesting the roll, |
136 | * not the recipient (ie, the poor slob getting hit). |
145 | * not the recipient (ie, the poor slob getting hit). |
137 | * The args are num D size (ie 4d6) [garbled 20010916] |
146 | * The args are num D size (ie 4d6) [garbled 20010916] |
138 | */ |
147 | */ |
139 | |
148 | |
|
|
149 | int |
140 | int die_roll(int num, int size, const object *op, int goodbad) { |
150 | die_roll (int num, int size, const object *op, int goodbad) |
|
|
151 | { |
141 | int min, diff, luck, total, i, gotlucky, base, ran; |
152 | int min, diff, luck, total, i, gotlucky, base, ran; |
142 | |
153 | |
143 | diff = size; |
154 | diff = size; |
144 | min = 1; |
155 | min = 1; |
145 | luck = total = gotlucky = 0; |
156 | luck = total = gotlucky = 0; |
146 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
157 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
147 | if (size < 2 || diff < 1) { |
158 | if (size < 2 || diff < 1) |
|
|
159 | { |
148 | LOG(llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
160 | LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
149 | return(num); /* avoids a float exception */ |
161 | return (num); /* avoids a float exception */ |
150 | } |
162 | } |
151 | |
163 | |
152 | if (op->type == PLAYER) |
164 | if (op->type == PLAYER) |
153 | luck = op->stats.luck; |
165 | luck = op->stats.luck; |
154 | |
166 | |
155 | for (i = 0; i < num; i++) { |
167 | for (i = 0; i < num; i++) |
|
|
168 | { |
156 | if (RANDOM()%base < MIN(10, abs(luck)) && !gotlucky) { |
169 | if (RANDOM () % base < MIN (10, abs (luck)) && !gotlucky) |
|
|
170 | { |
157 | /* we have a winner */ |
171 | /* we have a winner */ |
158 | gotlucky++; |
172 | gotlucky++; |
159 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
173 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
160 | diff -= luck; |
174 | diff -= luck; |
161 | if (diff < 1) |
175 | if (diff < 1) |
162 | return(num); /*check again*/ |
176 | return (num); /*check again */ |
163 | ((goodbad) ? (min += luck) : (diff)); |
177 | ((goodbad) ? (min += luck) : (diff)); |
164 | ran = RANDOM(); |
178 | ran = RANDOM (); |
165 | total += MAX(1, MIN(size, (ran%diff)+min)); |
179 | total += MAX (1, MIN (size, (ran % diff) + min)); |
|
|
180 | } |
166 | } else { |
181 | else |
|
|
182 | { |
167 | total += RANDOM()%size+1; |
183 | total += RANDOM () % size + 1; |
168 | } |
184 | } |
169 | } |
185 | } |
170 | return(total); |
186 | return (total); |
171 | } |
187 | } |
172 | |
188 | |
173 | /* |
189 | /* |
174 | * Another convenience function. Returns a number between min and max. |
190 | * Another convenience function. Returns a number between min and max. |
175 | * It is suggested one use these functions rather than RANDOM()%, as it |
191 | * It is suggested one use these functions rather than RANDOM()%, as it |
176 | * would appear that a number of off-by-one-errors exist due to improper |
192 | * would appear that a number of off-by-one-errors exist due to improper |
177 | * use of %. This should also prevent SIGFPE. |
193 | * use of %. This should also prevent SIGFPE. |
178 | */ |
194 | */ |
179 | |
195 | |
|
|
196 | int |
180 | int rndm(int min, int max) |
197 | rndm (int min, int max) |
181 | { |
198 | { |
182 | int diff; |
199 | int diff; |
183 | |
200 | |
184 | diff = max - min + 1; |
201 | diff = max - min + 1; |
185 | if (max < 1 || diff < 1) |
202 | if (max < 1 || diff < 1) |
186 | return(min); |
203 | return (min); |
187 | |
204 | |
188 | return(RANDOM()%diff+min); |
205 | return (RANDOM () % diff + min); |
189 | } |
206 | } |
190 | |
207 | |
191 | /* decay and destroy persihable items in a map */ |
208 | /* decay and destroy persihable items in a map */ |
192 | |
209 | |
|
|
210 | void |
193 | void decay_objects(mapstruct *m) |
211 | decay_objects (mapstruct *m) |
194 | { |
212 | { |
195 | int x, y, destroy; |
213 | int x, y, destroy; |
196 | object *op, *otmp; |
214 | object *op, *otmp; |
197 | |
215 | |
198 | if (m->unique) |
216 | if (m->unique) |
199 | return; |
217 | return; |
200 | |
218 | |
201 | for (x=0; x < MAP_WIDTH(m); x++) |
219 | for (x = 0; x < MAP_WIDTH (m); x++) |
202 | for (y=0; y < MAP_HEIGHT(m); y++) |
220 | for (y = 0; y < MAP_HEIGHT (m); y++) |
203 | for (op = get_map_ob(m, x, y); op; op = otmp) { |
221 | for (op = get_map_ob (m, x, y); op; op = otmp) |
|
|
222 | { |
|
|
223 | destroy = 0; |
|
|
224 | otmp = op->above; |
|
|
225 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) |
|
|
226 | break; |
|
|
227 | if (QUERY_FLAG (op, FLAG_IS_FLOOR) || |
|
|
228 | QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || |
|
|
229 | QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || |
|
|
230 | QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) |
|
|
231 | continue; |
|
|
232 | /* otherwise, we decay and destroy */ |
|
|
233 | if (IS_WEAPON (op)) |
|
|
234 | { |
|
|
235 | op->stats.dam--; |
|
|
236 | if (op->stats.dam < 0) |
204 | destroy = 0; |
237 | destroy = 1; |
205 | otmp = op->above; |
|
|
206 | if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) |
|
|
207 | break; |
|
|
208 | if (QUERY_FLAG(op, FLAG_IS_FLOOR) || |
|
|
209 | QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) || |
|
|
210 | QUERY_FLAG(op, FLAG_OBJ_SAVE_ON_OVL) || |
|
|
211 | QUERY_FLAG(op, FLAG_UNIQUE) || |
|
|
212 | QUERY_FLAG(op, FLAG_OVERLAY_FLOOR) || |
|
|
213 | QUERY_FLAG(op, FLAG_UNPAID) || IS_LIVE(op)) |
|
|
214 | continue; |
|
|
215 | /* otherwise, we decay and destroy */ |
|
|
216 | if (IS_WEAPON(op)) { |
|
|
217 | op->stats.dam--; |
|
|
218 | if (op->stats.dam < 0) |
|
|
219 | destroy = 1; |
|
|
220 | } else if (IS_ARMOR(op)) { |
|
|
221 | op->stats.ac--; |
|
|
222 | if (op->stats.ac < 0) |
|
|
223 | destroy = 1; |
|
|
224 | } else if (op->type == FOOD) { |
|
|
225 | op->stats.food -= rndm(5,20); |
|
|
226 | if (op->stats.food < 0) |
|
|
227 | destroy = 1; |
|
|
228 | } else { |
|
|
229 | if (op->material & M_PAPER || op->material & M_LEATHER || |
|
|
230 | op->material & M_WOOD || op->material & M_ORGANIC || |
|
|
231 | op->material & M_CLOTH || op->material & M_LIQUID) |
|
|
232 | destroy = 1; |
|
|
233 | if (op->material & M_IRON && rndm(1,5) == 1) |
|
|
234 | destroy = 1; |
|
|
235 | if (op->material & M_GLASS && rndm(1,2) == 1) |
|
|
236 | destroy = 1; |
|
|
237 | if ((op->material & M_STONE || op->material & M_ADAMANT) && |
|
|
238 | rndm(1,10) == 1) |
|
|
239 | destroy = 1; |
|
|
240 | if ((op->material & M_SOFT_METAL || op->material & M_BONE) && |
|
|
241 | rndm(1,3) == 1) |
|
|
242 | destroy = 1; |
|
|
243 | if (op->material & M_ICE && MAP_TEMP(m) > 32) |
|
|
244 | destroy = 1; |
|
|
245 | } |
|
|
246 | /* adjust overall chance below */ |
|
|
247 | if (destroy && rndm(0, 1)) { |
|
|
248 | remove_ob(op); |
|
|
249 | free_object(op); |
|
|
250 | } |
|
|
251 | } |
238 | } |
|
|
239 | else if (IS_ARMOR (op)) |
|
|
240 | { |
|
|
241 | op->stats.ac--; |
|
|
242 | if (op->stats.ac < 0) |
|
|
243 | destroy = 1; |
|
|
244 | } |
|
|
245 | else if (op->type == FOOD) |
|
|
246 | { |
|
|
247 | op->stats.food -= rndm (5, 20); |
|
|
248 | if (op->stats.food < 0) |
|
|
249 | destroy = 1; |
|
|
250 | } |
|
|
251 | else |
|
|
252 | { |
|
|
253 | if (op->material & M_PAPER || op->material & M_LEATHER || |
|
|
254 | op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) |
|
|
255 | destroy = 1; |
|
|
256 | if (op->material & M_IRON && rndm (1, 5) == 1) |
|
|
257 | destroy = 1; |
|
|
258 | if (op->material & M_GLASS && rndm (1, 2) == 1) |
|
|
259 | destroy = 1; |
|
|
260 | if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) |
|
|
261 | destroy = 1; |
|
|
262 | if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) |
|
|
263 | destroy = 1; |
|
|
264 | if (op->material & M_ICE && MAP_TEMP (m) > 32) |
|
|
265 | destroy = 1; |
|
|
266 | } |
|
|
267 | /* adjust overall chance below */ |
|
|
268 | if (destroy && rndm (0, 1)) |
|
|
269 | { |
|
|
270 | remove_ob (op); |
|
|
271 | free_object (op); |
|
|
272 | } |
|
|
273 | } |
252 | } |
274 | } |
253 | |
275 | |
254 | /* convert materialname to materialtype_t */ |
276 | /* convert materialname to materialtype_t */ |
255 | |
277 | |
|
|
278 | materialtype_t * |
256 | materialtype_t *name_to_material(const char *name) |
279 | name_to_material (const char *name) |
257 | { |
280 | { |
258 | materialtype_t *mt, *nmt; |
281 | materialtype_t *mt, *nmt; |
259 | |
282 | |
260 | mt = NULL; |
283 | mt = NULL; |
261 | for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt=nmt->next) { |
284 | for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt = nmt->next) |
|
|
285 | { |
262 | if (strcmp(name, nmt->name) == 0) { |
286 | if (strcmp (name, nmt->name) == 0) |
|
|
287 | { |
263 | mt = nmt; |
288 | mt = nmt; |
264 | break; |
289 | break; |
265 | } |
290 | } |
266 | } |
291 | } |
267 | return mt; |
292 | return mt; |
268 | } |
293 | } |
269 | |
294 | |
270 | /* when doing transmutation of objects, we have to recheck the resistances, |
295 | /* when doing transmutation of objects, we have to recheck the resistances, |
271 | * as some that did not apply previously, may apply now. |
296 | * as some that did not apply previously, may apply now. |
272 | */ |
297 | */ |
273 | |
298 | |
|
|
299 | void |
274 | void transmute_materialname(object *op, const object *change) |
300 | transmute_materialname (object *op, const object *change) |
275 | { |
301 | { |
276 | materialtype_t *mt; |
302 | materialtype_t *mt; |
277 | int j; |
303 | int j; |
278 | |
304 | |
279 | if (op->materialname == NULL) |
305 | if (op->materialname == NULL) |
280 | return; |
306 | return; |
281 | |
307 | |
282 | if (change->materialname != NULL && |
308 | if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) |
283 | strcmp(op->materialname, change->materialname)) |
|
|
284 | return; |
309 | return; |
285 | |
310 | |
286 | if (!IS_ARMOR(op)) |
311 | if (!IS_ARMOR (op)) |
287 | return; |
312 | return; |
288 | |
313 | |
289 | mt = name_to_material(op->materialname); |
314 | mt = name_to_material (op->materialname); |
290 | if (!mt) { |
315 | if (!mt) |
|
|
316 | { |
291 | LOG(llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->name, &op->name, &op->materialname); |
317 | LOG (llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->name, &op->name, &op->materialname); |
292 | return; |
318 | return; |
293 | } |
319 | } |
294 | |
320 | |
295 | for (j=0; j < NROFATTACKS; j++) |
321 | for (j = 0; j < NROFATTACKS; j++) |
296 | if (op->resist[j] == 0 && change->resist[j] != 0) { |
322 | if (op->resist[j] == 0 && change->resist[j] != 0) |
|
|
323 | { |
297 | op->resist[j] += mt->mod[j]; |
324 | op->resist[j] += mt->mod[j]; |
298 | if (op->resist[j] > 100) |
325 | if (op->resist[j] > 100) |
299 | op->resist[j] = 100; |
326 | op->resist[j] = 100; |
300 | if (op->resist[j] < -100) |
327 | if (op->resist[j] < -100) |
301 | op->resist[j] = -100; |
328 | op->resist[j] = -100; |
302 | } |
329 | } |
303 | } |
330 | } |
304 | |
331 | |
305 | /* set the materialname and type for an item */ |
332 | /* set the materialname and type for an item */ |
|
|
333 | void |
306 | void set_materialname(object *op, int difficulty, materialtype_t *nmt) |
334 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
307 | { |
335 | { |
308 | materialtype_t *mt, *lmt; |
336 | materialtype_t *mt, *lmt; |
|
|
337 | |
309 | #ifdef NEW_MATERIAL_CODE |
338 | #ifdef NEW_MATERIAL_CODE |
310 | int j; |
339 | int j; |
311 | #endif |
340 | #endif |
312 | |
341 | |
313 | if (op->materialname != NULL) |
342 | if (op->materialname != NULL) |
314 | return; |
343 | return; |
315 | |
344 | |
316 | |
345 | |
317 | |
346 | |
318 | if (nmt == NULL) { |
347 | if (nmt == NULL) |
|
|
348 | { |
319 | lmt = NULL; |
349 | lmt = NULL; |
320 | #ifndef NEW_MATERIAL_CODE |
350 | #ifndef NEW_MATERIAL_CODE |
321 | for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { |
351 | for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) |
|
|
352 | { |
322 | if (op->material & mt->material) { |
353 | if (op->material & mt->material) |
|
|
354 | { |
323 | lmt = mt; |
355 | lmt = mt; |
|
|
356 | break; |
|
|
357 | } |
|
|
358 | } |
|
|
359 | |
|
|
360 | #else |
|
|
361 | for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) |
|
|
362 | { |
|
|
363 | if (op->material & mt->material && rndm (1, 100) <= mt->chance && |
|
|
364 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
|
|
365 | { |
|
|
366 | lmt = mt; |
|
|
367 | if (!(IS_WEAPON (op) || IS_ARMOR (op))) |
324 | break; |
368 | break; |
325 | } |
369 | } |
326 | } |
370 | } |
|
|
371 | #endif |
|
|
372 | } |
|
|
373 | else |
|
|
374 | { |
|
|
375 | lmt = nmt; |
|
|
376 | } |
327 | |
377 | |
|
|
378 | if (lmt != NULL) |
|
|
379 | { |
|
|
380 | #ifndef NEW_MATERIAL_CODE |
|
|
381 | op->materialname = lmt->name; |
|
|
382 | return; |
328 | #else |
383 | #else |
329 | for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { |
|
|
330 | if (op->material & mt->material && rndm(1, 100) <= mt->chance && |
|
|
331 | difficulty >= mt->difficulty && |
|
|
332 | (op->magic >= mt->magic || mt->magic == 0)) { |
|
|
333 | lmt = mt; |
|
|
334 | if (!(IS_WEAPON(op) || IS_ARMOR(op))) |
|
|
335 | break; |
|
|
336 | } |
|
|
337 | } |
|
|
338 | #endif |
|
|
339 | } else { |
|
|
340 | lmt = nmt; |
|
|
341 | } |
|
|
342 | |
384 | |
343 | if (lmt != NULL) { |
|
|
344 | #ifndef NEW_MATERIAL_CODE |
|
|
345 | op->materialname = lmt->name; |
|
|
346 | return; |
|
|
347 | #else |
|
|
348 | |
|
|
349 | if (op->stats.dam && IS_WEAPON(op)) { |
385 | if (op->stats.dam && IS_WEAPON (op)) |
|
|
386 | { |
350 | op->stats.dam += lmt->damage; |
387 | op->stats.dam += lmt->damage; |
351 | if (op->stats.dam < 1) |
388 | if (op->stats.dam < 1) |
352 | op->stats.dam = 1; |
389 | op->stats.dam = 1; |
353 | } |
390 | } |
354 | if (op->stats.sp && op->type == BOW) |
391 | if (op->stats.sp && op->type == BOW) |
355 | op->stats.sp += lmt->sp; |
392 | op->stats.sp += lmt->sp; |
356 | if (op->stats.wc && IS_WEAPON(op)) |
393 | if (op->stats.wc && IS_WEAPON (op)) |
357 | op->stats.wc += lmt->wc; |
394 | op->stats.wc += lmt->wc; |
358 | if (IS_ARMOR(op)) { |
395 | if (IS_ARMOR (op)) |
|
|
396 | { |
359 | if (op->stats.ac) |
397 | if (op->stats.ac) |
360 | op->stats.ac += lmt->ac; |
398 | op->stats.ac += lmt->ac; |
361 | for (j=0; j < NROFATTACKS; j++) |
399 | for (j = 0; j < NROFATTACKS; j++) |
362 | if (op->resist[j] != 0) { |
400 | if (op->resist[j] != 0) |
|
|
401 | { |
363 | op->resist[j] += lmt->mod[j]; |
402 | op->resist[j] += lmt->mod[j]; |
364 | if (op->resist[j] > 100) |
403 | if (op->resist[j] > 100) |
365 | op->resist[j] = 100; |
404 | op->resist[j] = 100; |
366 | if (op->resist[j] < -100) |
405 | if (op->resist[j] < -100) |
367 | op->resist[j] = -100; |
406 | op->resist[j] = -100; |
368 | } |
407 | } |
369 | } |
408 | } |
370 | op->materialname = add_string(lmt->name); |
409 | op->materialname = add_string (lmt->name); |
371 | /* dont make it unstackable if it doesn't need to be */ |
410 | /* dont make it unstackable if it doesn't need to be */ |
372 | if (IS_WEAPON(op) || IS_ARMOR(op)) { |
411 | if (IS_WEAPON (op) || IS_ARMOR (op)) |
|
|
412 | { |
373 | op->weight = (op->weight * lmt->weight)/100; |
413 | op->weight = (op->weight * lmt->weight) / 100; |
374 | op->value = (op->value * lmt->value)/100; |
414 | op->value = (op->value * lmt->value) / 100; |
375 | } |
415 | } |
376 | #endif |
416 | #endif |
377 | } |
417 | } |
378 | } |
418 | } |
379 | |
419 | |
380 | /* |
420 | /* |
381 | * Strip out the media tags from a String. |
421 | * Strip out the media tags from a String. |
382 | * Warning the input string will contain the result string |
422 | * Warning the input string will contain the result string |
383 | */ |
423 | */ |
|
|
424 | void |
384 | void strip_media_tag(char *message){ |
425 | strip_media_tag (char *message) |
|
|
426 | { |
385 | int in_tag=0; |
427 | int in_tag = 0; |
386 | char* dest; |
428 | char *dest; |
387 | char* src; |
429 | char *src; |
|
|
430 | |
388 | src=dest=message; |
431 | src = dest = message; |
389 | while (*src!='\0'){ |
432 | while (*src != '\0') |
|
|
433 | { |
390 | if (*src=='['){ |
434 | if (*src == '[') |
|
|
435 | { |
391 | in_tag=1; |
436 | in_tag = 1; |
|
|
437 | } |
392 | } else if (in_tag && (*src==']')) |
438 | else if (in_tag && (*src == ']')) |
393 | in_tag=0; |
439 | in_tag = 0; |
394 | else if (!in_tag){ |
440 | else if (!in_tag) |
|
|
441 | { |
395 | *dest=*src; |
442 | *dest = *src; |
396 | dest++; |
443 | dest++; |
397 | } |
444 | } |
398 | src++; |
445 | src++; |
399 | } |
446 | } |
400 | *dest='\0'; |
447 | *dest = '\0'; |
401 | } |
448 | } |
402 | |
449 | |
|
|
450 | const char * |
403 | const char* strrstr(const char* haystack, const char* needle){ |
451 | strrstr (const char *haystack, const char *needle) |
|
|
452 | { |
404 | const char* lastneedle; |
453 | const char *lastneedle; |
|
|
454 | |
405 | lastneedle=NULL; |
455 | lastneedle = NULL; |
406 | while((haystack=strstr(haystack,needle))!=NULL){ |
456 | while ((haystack = strstr (haystack, needle)) != NULL) |
|
|
457 | { |
407 | lastneedle=haystack; |
458 | lastneedle = haystack; |
408 | haystack++; |
459 | haystack++; |
409 | } |
460 | } |
410 | return lastneedle; |
461 | return lastneedle; |
411 | |
462 | |
412 | } |
463 | } |
|
|
464 | |
413 | #define EOL_SIZE (sizeof("\n")-1) |
465 | #define EOL_SIZE (sizeof("\n")-1) |
|
|
466 | void |
414 | void strip_endline(char* buf){ |
467 | strip_endline (char *buf) |
|
|
468 | { |
415 | if (strlen(buf)<sizeof("\n")){ |
469 | if (strlen (buf) < sizeof ("\n")) |
|
|
470 | { |
416 | return; |
471 | return; |
417 | } |
472 | } |
418 | if (!strcmp(buf+strlen(buf)-EOL_SIZE,"\n")) |
473 | if (!strcmp (buf + strlen (buf) - EOL_SIZE, "\n")) |
419 | buf[strlen(buf)-EOL_SIZE]='\0'; |
474 | buf[strlen (buf) - EOL_SIZE] = '\0'; |
420 | } |
475 | } |
421 | |
476 | |
422 | /** |
477 | /** |
423 | * Replace in string src all occurrences of key by replacement. The resulting |
478 | * Replace in string src all occurrences of key by replacement. The resulting |
424 | * string is put into result; at most resultsize characters (including the |
479 | * string is put into result; at most resultsize characters (including the |
425 | * terminating null character) will be written to result. |
480 | * terminating null character) will be written to result. |
426 | */ |
481 | */ |
|
|
482 | void |
427 | void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize) |
483 | replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize) |
428 | { |
484 | { |
429 | size_t resultlen; |
485 | size_t resultlen; |
430 | size_t keylen; |
486 | size_t keylen; |
431 | |
487 | |
432 | /* special case to prevent infinite loop if key==replacement=="" */ |
488 | /* special case to prevent infinite loop if key==replacement=="" */ |
433 | if(strcmp(key, replacement) == 0) |
489 | if (strcmp (key, replacement) == 0) |
434 | { |
490 | { |
435 | snprintf(result, resultsize, "%s", src); |
491 | snprintf (result, resultsize, "%s", src); |
436 | return; |
492 | return; |
437 | } |
493 | } |
438 | |
494 | |
439 | keylen = strlen(key); |
495 | keylen = strlen (key); |
440 | |
496 | |
441 | resultlen = 0; |
497 | resultlen = 0; |
442 | while(*src != '\0' && resultlen+1 < resultsize) |
498 | while (*src != '\0' && resultlen + 1 < resultsize) |
443 | { |
499 | { |
444 | if(strncmp(src, key, keylen) == 0) |
500 | if (strncmp (src, key, keylen) == 0) |
445 | { |
501 | { |
446 | snprintf(result+resultlen, resultsize-resultlen, "%s", replacement); |
502 | snprintf (result + resultlen, resultsize - resultlen, "%s", replacement); |
447 | resultlen += strlen(result+resultlen); |
503 | resultlen += strlen (result + resultlen); |
448 | src += keylen; |
504 | src += keylen; |
449 | } |
505 | } |
450 | else |
506 | else |
451 | { |
|
|
452 | result[resultlen++] = *src++; |
|
|
453 | } |
|
|
454 | } |
507 | { |
|
|
508 | result[resultlen++] = *src++; |
|
|
509 | } |
|
|
510 | } |
455 | result[resultlen] = '\0'; |
511 | result[resultlen] = '\0'; |
456 | } |
512 | } |
457 | |
513 | |
458 | /** |
514 | /** |
459 | * Taking a string as an argument, mutate it into a string that looks like a list. |
515 | * Taking a string as an argument, mutate it into a string that looks like a list. |
460 | * a 'list' for the purposes here, is a string of items, seperated by commas, except |
516 | * a 'list' for the purposes here, is a string of items, seperated by commas, except |
461 | * for the last entry, which has an 'and' before it, and a full stop (period) after it. |
517 | * for the last entry, which has an 'and' before it, and a full stop (period) after it. |
462 | * This function will also strip all trailing non alphanumeric characters. |
518 | * This function will also strip all trailing non alphanumeric characters. |
463 | * It does not insert an oxford comma. |
519 | * It does not insert an oxford comma. |
464 | */ |
520 | */ |
465 | |
521 | |
|
|
522 | void |
466 | void make_list_like(char *input) { |
523 | make_list_like (char *input) |
|
|
524 | { |
467 | char *p, tmp[MAX_BUF]; |
525 | char *p, tmp[MAX_BUF]; |
468 | int i; |
526 | int i; |
|
|
527 | |
469 | if (!input || strlen(input) > MAX_BUF-5) return; |
528 | if (!input || strlen (input) > MAX_BUF - 5) |
470 | /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ |
|
|
471 | |
|
|
472 | strncpy(tmp, input, MAX_BUF-5); |
|
|
473 | /*trim all trailing commas, spaces etc.*/ |
|
|
474 | for (i=strlen(tmp); !isalnum(tmp[i]) && i >= 0; i--) |
|
|
475 | tmp[i]='\0'; |
|
|
476 | strcat(tmp, "."); |
|
|
477 | |
|
|
478 | p=strrchr(tmp, ','); |
|
|
479 | if (p) { |
|
|
480 | *p='\0'; |
|
|
481 | strcpy(input, tmp); |
|
|
482 | p++; |
|
|
483 | strcat(input, " and"); |
|
|
484 | strcat(input, p); |
|
|
485 | } |
|
|
486 | else strcpy(input, tmp); |
|
|
487 | return; |
529 | return; |
|
|
530 | /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ |
|
|
531 | |
|
|
532 | strncpy (tmp, input, MAX_BUF - 5); |
|
|
533 | /*trim all trailing commas, spaces etc. */ |
|
|
534 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
|
|
535 | tmp[i] = '\0'; |
|
|
536 | |
|
|
537 | strcat (tmp, "."); |
|
|
538 | |
|
|
539 | p = strrchr (tmp, ','); |
|
|
540 | if (p) |
|
|
541 | { |
|
|
542 | *p = '\0'; |
|
|
543 | strcpy (input, tmp); |
|
|
544 | p++; |
|
|
545 | strcat (input, " and"); |
|
|
546 | strcat (input, p); |
|
|
547 | } |
|
|
548 | else |
|
|
549 | strcpy (input, tmp); |
|
|
550 | |
|
|
551 | return; |
|
|
552 | } |
|
|
553 | |
|
|
554 | void * |
|
|
555 | zero_initialised::operator new (size_t s, void *p) |
|
|
556 | { |
|
|
557 | memset (p, 0, s); |
|
|
558 | return p; |
488 | } |
559 | } |
489 | |
560 | |
490 | void * |
561 | void * |
491 | zero_initialised::operator new (size_t s) |
562 | zero_initialised::operator new (size_t s) |
492 | { |
563 | { |
493 | //return calloc (1, s); |
564 | //return calloc (1, s); |
494 | return g_slice_alloc0 (s); |
565 | return g_slice_alloc0 (s); |
495 | } |
566 | } |
496 | |
567 | |
497 | void * |
568 | void * |
498 | zero_initialised::operator new [] (size_t s) |
569 | zero_initialised::operator new[] (size_t s) |
499 | { |
570 | { |
500 | //return calloc (1, s); |
571 | //return calloc (1, s); |
501 | return g_slice_alloc0 (s); |
572 | return g_slice_alloc0 (s); |
502 | } |
573 | } |
503 | |
574 | |
504 | void |
575 | void |
… | |
… | |
507 | //free (p); return; |
578 | //free (p); return; |
508 | g_slice_free1 (s, p); |
579 | g_slice_free1 (s, p); |
509 | } |
580 | } |
510 | |
581 | |
511 | void |
582 | void |
512 | zero_initialised::operator delete [] (void *p, size_t s) |
583 | zero_initialised::operator delete[] (void *p, size_t s) |
513 | { |
584 | { |
514 | //free (p); return; |
585 | //free (p); return; |
515 | g_slice_free1 (s, p); |
586 | g_slice_free1 (s, p); |
516 | } |
587 | } |
517 | |
588 | |
|
|
589 | void assign (char *dst, const char *src, int maxlen) |
|
|
590 | { |
|
|
591 | if (!src) |
|
|
592 | src = ""; |
|
|
593 | |
|
|
594 | int len = strlen (src); |
|
|
595 | |
|
|
596 | if (len >= maxlen - 1) |
|
|
597 | { |
|
|
598 | if (maxlen <= 4) |
|
|
599 | { |
|
|
600 | memset (dst, '.', maxlen - 1); |
|
|
601 | dst [maxlen - 1] = 0; |
|
|
602 | } |
|
|
603 | else |
|
|
604 | { |
|
|
605 | memcpy (dst, src, maxlen - 4); |
|
|
606 | memcpy (dst + maxlen - 4, "...", 4); |
|
|
607 | } |
|
|
608 | } |
|
|
609 | else |
|
|
610 | memcpy (dst, src, len + 1); |
|
|
611 | } |
|
|
612 | |