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.89 by root, Mon Oct 12 14:00:57 2009 UTC

3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24/* 25/*
32#include <time.h> 33#include <time.h>
33#include <signal.h> 34#include <signal.h>
34 35
35#include <global.h> 36#include <global.h>
36#include <material.h> 37#include <material.h>
38#include <object.h>
37 39
38#include <sys/time.h> 40#include <sys/time.h>
39#include <sys/resource.h> 41#include <sys/resource.h>
40 42
41#include <glib.h> 43#include <glib.h>
51 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 53 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; 54 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; 55 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
54 56
55 for (int i = 11; --i; ) 57 for (int i = 11; --i; )
56 operator ()(); 58 next ();
57} 59}
58 60
59uint32_t 61uint32_t
60tausworthe_random_generator::next () 62tausworthe_random_generator::next ()
61{ 63{
65 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 67 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
66 68
67 return state [0] ^ state [1] ^ state [2] ^ state [3]; 69 return state [0] ^ state [1] ^ state [2] ^ state [3];
68} 70}
69 71
72template<class generator>
70uint32_t 73uint32_t
71tausworthe_random_generator::get_range (uint32_t num) 74random_number_generator<generator>::get_range (uint32_t num)
72{ 75{
73 return (next () * (uint64_t)num) >> 32U; 76 return (this->next () * (uint64_t)num) >> 32U;
74} 77}
75 78
76// return a number within (min .. max) 79// return a number within (min .. max)
80template<class generator>
77int 81int
78tausworthe_random_generator::get_range (int r_min, int r_max) 82random_number_generator<generator>::get_range (int r_min, int r_max)
79{ 83{
80 return r_min + get_range (max (r_max - r_min + 1, 0)); 84 return r_min + get_range (max (r_max - r_min + 1, 0));
81} 85}
86
87template struct random_number_generator<tausworthe_random_generator>;
88template struct random_number_generator<xorshift_random_generator>;
82 89
83/* 90/*
84 * The random functions here take luck into account when rolling random 91 * 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 92 * 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 93 * difference becomes in the random numbers. IE, the effect is lessened
200} 207}
201 208
202/* convert materialname to materialtype_t */ 209/* convert materialname to materialtype_t */
203 210
204materialtype_t * 211materialtype_t *
205name_to_material (const shstr &name) 212name_to_material (const shstr_cmp name)
206{ 213{
207 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 214 for (materialtype_t *mt = materialt; mt; mt = mt->next)
208 if (name == mt->name) 215 if (name == mt->name)
209 return mt; 216 return mt;
210 217
211 return 0; 218 return 0;
212} 219}
218transmute_materialname (object *op, const object *change) 225transmute_materialname (object *op, const object *change)
219{ 226{
220 materialtype_t *mt; 227 materialtype_t *mt;
221 int j; 228 int j;
222 229
223 if (op->materialname == NULL) 230 if (!op->materialname)
224 return; 231 return;
225 232
226 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 233 if (op->materialname != change->materialname)
227 return; 234 return;
228 235
229 if (!op->is_armor ()) 236 if (!op->is_armor ())
230 return; 237 return;
231 238
251void 258void
252set_materialname (object *op, int difficulty, materialtype_t *nmt) 259set_materialname (object *op, int difficulty, materialtype_t *nmt)
253{ 260{
254 materialtype_t *mt, *lmt; 261 materialtype_t *mt, *lmt;
255 262
256 if (op->materialname != NULL) 263 if (!op->materialname)
257 return; 264 return;
258 265
259 if (nmt == NULL) 266 if (nmt)
267 lmt = nmt;
268 else
260 { 269 {
261 lmt = NULL; 270 lmt = 0;
262 271
263 for (mt = materialt; mt && mt->next; mt = mt->next) 272 for (mt = materialt; mt; mt = mt->next)
264 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 273 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
265 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 274 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
266 { 275 {
267 lmt = mt; 276 lmt = mt;
268 if (!(op->is_weapon () || op->is_armor ())) 277 if (!(op->is_weapon () || op->is_armor ()))
269 break; 278 break;
270 } 279 }
271 } 280 }
272 else
273 lmt = nmt;
274 281
275 if (lmt != NULL) 282 if (lmt)
276 { 283 {
277 if (op->stats.dam && op->is_weapon ()) 284 if (op->stats.dam && op->is_weapon ())
278 { 285 {
279 op->stats.dam += lmt->damage; 286 op->stats.dam += lmt->damage;
280 if (op->stats.dam < 1) 287 if (op->stats.dam < 1)
442 strcpy (input, tmp); 449 strcpy (input, tmp);
443 450
444 return; 451 return;
445} 452}
446 453
454/******************************************************************************/
455
456/* Checks a player-provided string which will become the msg property of
457 * an object for dangerous input.
458 */
459bool
460msg_is_safe (const char *msg)
461{
462 bool safe = true;
463
464 /* Trying to cheat by getting data into the object */
465 if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1)
466 || strstr (msg, "\nendmsg"))
467 safe = false;
468
469 /* Trying to make the object talk, and potentially access arbitrary code */
470 if (object::msg_has_dialogue (msg))
471 safe = false;
472
473 return safe;
474}
475
447///////////////////////////////////////////////////////////////////////////// 476/////////////////////////////////////////////////////////////////////////////
448 477
449void 478void
450fork_abort (const char *msg) 479fork_abort (const char *msg)
451{ 480{
554 583
555#endif 584#endif
556 585
557/******************************************************************************/ 586/******************************************************************************/
558 587
588int
559void assign (char *dst, const char *src, int maxlen) 589assign (char *dst, const char *src, int maxsize)
560{ 590{
561 if (!src) 591 if (!src)
562 src = ""; 592 src = "";
563 593
564 int len = strlen (src); 594 int len = strlen (src);
565 595
566 if (len >= maxlen - 1) 596 if (len >= maxsize)
567 { 597 {
568 if (maxlen <= 4) 598 if (maxsize <= 4)
569 { 599 {
570 memset (dst, '.', maxlen - 1); 600 memset (dst, '.', maxsize - 2);
571 dst [maxlen - 1] = 0; 601 dst [maxsize - 1] = 0;
572 } 602 }
573 else 603 else
574 { 604 {
575 memcpy (dst, src, maxlen - 4); 605 memcpy (dst, src, maxsize - 4);
576 memcpy (dst + maxlen - 4, "...", 4); 606 memcpy (dst + maxsize - 4, "...", 4);
577 } 607 }
608
609 len = maxsize;
578 } 610 }
579 else 611 else
580 memcpy (dst, src, len + 1); 612 memcpy (dst, src, ++len);
613
614 return len;
581} 615}
582 616
583const char * 617const char *
584format (const char *format, ...) 618format (const char *format, ...)
585{ 619{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines