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.82 by root, Thu Sep 11 12:43:17 2008 UTC vs.
Revision 1.87 by sf-marcmagus, Sun Oct 11 01:35:53 2009 UTC

32#include <time.h> 32#include <time.h>
33#include <signal.h> 33#include <signal.h>
34 34
35#include <global.h> 35#include <global.h>
36#include <material.h> 36#include <material.h>
37#include <object.h>
37 38
38#include <sys/time.h> 39#include <sys/time.h>
39#include <sys/resource.h> 40#include <sys/resource.h>
40 41
41#include <glib.h> 42#include <glib.h>
51 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 52 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
52 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; 53 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
53 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; 54 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
54 55
55 for (int i = 11; --i; ) 56 for (int i = 11; --i; )
56 operator ()(); 57 next ();
57} 58}
58 59
59uint32_t 60uint32_t
60tausworthe_random_generator::next () 61tausworthe_random_generator::next ()
61{ 62{
65 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 66 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
66 67
67 return state [0] ^ state [1] ^ state [2] ^ state [3]; 68 return state [0] ^ state [1] ^ state [2] ^ state [3];
68} 69}
69 70
71template<class generator>
70uint32_t 72uint32_t
71tausworthe_random_generator::get_range (uint32_t num) 73random_number_generator<generator>::get_range (uint32_t num)
72{ 74{
73 return (next () * (uint64_t)num) >> 32U; 75 return (this->next () * (uint64_t)num) >> 32U;
74} 76}
75 77
76// return a number within (min .. max) 78// return a number within (min .. max)
79template<class generator>
77int 80int
78tausworthe_random_generator::get_range (int r_min, int r_max) 81random_number_generator<generator>::get_range (int r_min, int r_max)
79{ 82{
80 return r_min + get_range (max (r_max - r_min + 1, 0)); 83 return r_min + get_range (max (r_max - r_min + 1, 0));
81} 84}
85
86template struct random_number_generator<tausworthe_random_generator>;
87template struct random_number_generator<xorshift_random_generator>;
82 88
83/* 89/*
84 * The random functions here take luck into account when rolling random 90 * The random functions here take luck into account when rolling random
85 * dice or numbers. This function has less of an impact the larger the 91 * dice or numbers. This function has less of an impact the larger the
86 * difference becomes in the random numbers. IE, the effect is lessened 92 * difference becomes in the random numbers. IE, the effect is lessened
200} 206}
201 207
202/* convert materialname to materialtype_t */ 208/* convert materialname to materialtype_t */
203 209
204materialtype_t * 210materialtype_t *
205name_to_material (const shstr &name) 211name_to_material (const shstr_cmp name)
206{ 212{
207 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 213 for (materialtype_t *mt = materialt; mt; mt = mt->next)
208 if (name == mt->name) 214 if (name == mt->name)
209 return mt; 215 return mt;
210 216
211 return 0; 217 return 0;
212} 218}
218transmute_materialname (object *op, const object *change) 224transmute_materialname (object *op, const object *change)
219{ 225{
220 materialtype_t *mt; 226 materialtype_t *mt;
221 int j; 227 int j;
222 228
223 if (op->materialname == NULL) 229 if (!op->materialname)
224 return; 230 return;
225 231
226 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 232 if (op->materialname != change->materialname)
227 return; 233 return;
228 234
229 if (!op->is_armor ()) 235 if (!op->is_armor ())
230 return; 236 return;
231 237
251void 257void
252set_materialname (object *op, int difficulty, materialtype_t *nmt) 258set_materialname (object *op, int difficulty, materialtype_t *nmt)
253{ 259{
254 materialtype_t *mt, *lmt; 260 materialtype_t *mt, *lmt;
255 261
256 if (op->materialname != NULL) 262 if (!op->materialname)
257 return; 263 return;
258 264
259 if (nmt == NULL) 265 if (nmt)
266 lmt = nmt;
267 else
260 { 268 {
261 lmt = NULL; 269 lmt = 0;
262 270
263 for (mt = materialt; mt && mt->next; mt = mt->next) 271 for (mt = materialt; mt; mt = mt->next)
264 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 272 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
265 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 273 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
266 { 274 {
267 lmt = mt; 275 lmt = mt;
268 if (!(op->is_weapon () || op->is_armor ())) 276 if (!(op->is_weapon () || op->is_armor ()))
269 break; 277 break;
270 } 278 }
271 } 279 }
272 else
273 lmt = nmt;
274 280
275 if (lmt != NULL) 281 if (lmt)
276 { 282 {
277 if (op->stats.dam && op->is_weapon ()) 283 if (op->stats.dam && op->is_weapon ())
278 { 284 {
279 op->stats.dam += lmt->damage; 285 op->stats.dam += lmt->damage;
280 if (op->stats.dam < 1) 286 if (op->stats.dam < 1)
442 strcpy (input, tmp); 448 strcpy (input, tmp);
443 449
444 return; 450 return;
445} 451}
446 452
453/******************************************************************************/
454
455/* Checks a player-provided string which will become the msg property of
456 * an object for dangerous input.
457 */
458bool
459msg_is_safe (const char *msg)
460{
461 bool safe = true;
462
463 /* Trying to cheat by getting data into the object */
464 if (!strncmp (msg, "endmsg", strlen ("endmsg")) || strstr (msg, "\nendmsg"))
465 safe = false;
466
467 /* Trying to make the object talk, and potentially access arbitrary code */
468 if (object::msg_has_dialogue (msg))
469 safe = false;
470
471 return safe;
472}
473
447///////////////////////////////////////////////////////////////////////////// 474/////////////////////////////////////////////////////////////////////////////
448 475
449void 476void
450fork_abort (const char *msg) 477fork_abort (const char *msg)
451{ 478{
554 581
555#endif 582#endif
556 583
557/******************************************************************************/ 584/******************************************************************************/
558 585
586int
559void assign (char *dst, const char *src, int maxlen) 587assign (char *dst, const char *src, int maxsize)
560{ 588{
561 if (!src) 589 if (!src)
562 src = ""; 590 src = "";
563 591
564 int len = strlen (src); 592 int len = strlen (src);
565 593
566 if (len >= maxlen - 1) 594 if (len >= maxsize)
567 { 595 {
568 if (maxlen <= 4) 596 if (maxsize <= 4)
569 { 597 {
570 memset (dst, '.', maxlen - 1); 598 memset (dst, '.', maxsize - 2);
571 dst [maxlen - 1] = 0; 599 dst [maxsize - 1] = 0;
572 } 600 }
573 else 601 else
574 { 602 {
575 memcpy (dst, src, maxlen - 4); 603 memcpy (dst, src, maxsize - 4);
576 memcpy (dst + maxlen - 4, "...", 4); 604 memcpy (dst + maxsize - 4, "...", 4);
577 } 605 }
606
607 len = maxsize;
578 } 608 }
579 else 609 else
580 memcpy (dst, src, len + 1); 610 memcpy (dst, src, ++len);
611
612 return len;
581} 613}
582 614
583const char * 615const char *
584format (const char *format, ...) 616format (const char *format, ...)
585{ 617{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines