ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.8 by root, Sat Sep 9 21:48:28 2006 UTC vs.
Revision 1.39 by pippijn, Mon Jan 15 21:06:18 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines