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.92 by root, Tue Nov 3 23:18:23 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/*
25 * General convenience functions for crossfire. 26 * General convenience functions for deliantra.
26 */ 27 */
27 28
28#include <cstdlib> 29#include <cstdlib>
29#include <sys/types.h> 30#include <sys/types.h>
30#include <unistd.h> 31#include <unistd.h>
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)
403 } 410 }
404 } 411 }
405 result[resultlen] = '\0'; 412 result[resultlen] = '\0';
406} 413}
407 414
408/** 415/******************************************************************************/
409 * Taking a string as an argument, mutate it into a string that looks like a list.
410 * a 'list' for the purposes here, is a string of items, seperated by commas, except
411 * for the last entry, which has an 'and' before it, and a full stop (period) after it.
412 * This function will also strip all trailing non alphanumeric characters.
413 * It does not insert an oxford comma.
414 */
415void
416make_list_like (char *input)
417{
418 char *p, tmp[MAX_BUF];
419 int i;
420 416
421 if (!input || strlen (input) > MAX_BUF - 5) 417/* Checks a player-provided string which will become the msg property of
422 return; 418 * an object for dangerous input.
423 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ 419 */
420bool
421msg_is_safe (const char *msg)
422{
423 bool safe = true;
424 424
425 strncpy (tmp, input, MAX_BUF - 5); 425 /* Trying to cheat by getting data into the object */
426 /*trim all trailing commas, spaces etc. */ 426 if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1)
427 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) 427 || strstr (msg, "\nendmsg"))
428 tmp[i] = '\0'; 428 safe = false;
429 429
430 strcat (tmp, "."); 430 /* Trying to make the object talk, and potentially access arbitrary code */
431 if (object::msg_has_dialogue (msg))
432 safe = false;
431 433
432 p = strrchr (tmp, ',');
433 if (p)
434 {
435 *p = '\0';
436 strcpy (input, tmp);
437 p++;
438 strcat (input, " and");
439 strcat (input, p);
440 }
441 else
442 strcpy (input, tmp);
443
444 return; 434 return safe;
445} 435}
446 436
447///////////////////////////////////////////////////////////////////////////// 437/////////////////////////////////////////////////////////////////////////////
448 438
449void 439void
554 544
555#endif 545#endif
556 546
557/******************************************************************************/ 547/******************************************************************************/
558 548
549int
559void assign (char *dst, const char *src, int maxlen) 550assign (char *dst, const char *src, int maxsize)
560{ 551{
561 if (!src) 552 if (!src)
562 src = ""; 553 src = "";
563 554
564 int len = strlen (src); 555 int len = strlen (src);
565 556
566 if (len >= maxlen - 1) 557 if (len >= maxsize)
567 { 558 {
568 if (maxlen <= 4) 559 if (maxsize <= 4)
569 { 560 {
570 memset (dst, '.', maxlen - 1); 561 memset (dst, '.', maxsize - 2);
571 dst [maxlen - 1] = 0; 562 dst [maxsize - 1] = 0;
572 } 563 }
573 else 564 else
574 { 565 {
575 memcpy (dst, src, maxlen - 4); 566 memcpy (dst, src, maxsize - 4);
576 memcpy (dst + maxlen - 4, "...", 4); 567 memcpy (dst + maxsize - 4, "...", 4);
577 } 568 }
569
570 len = maxsize;
578 } 571 }
579 else 572 else
580 memcpy (dst, src, len + 1); 573 memcpy (dst, src, ++len);
581}
582 574
583const char * 575 return len;
576}
577
578char *
579vformat (const char *format, va_list ap)
580{
581 static dynbuf_text buf; buf.clear ();
582 buf.vprintf (format, ap);
583 return buf;
584}
585
586char *
584format (const char *format, ...) 587format (const char *format, ...)
585{ 588{
586 static dynbuf_text buf;
587
588 buf.clear ();
589
590 va_list ap; 589 va_list ap;
591 va_start (ap, format); 590 va_start (ap, format);
592 buf.vprintf (format, ap); 591 char *buf = vformat (format, ap);
593 va_end (ap); 592 va_end (ap);
594 593
595 return buf; 594 return buf;
596} 595}
597 596

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines