--- deliantra/server/include/util.h 2009/01/01 20:49:48 1.85
+++ deliantra/server/include/util.h 2009/10/15 21:09:32 1.91
@@ -3,18 +3,19 @@
*
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
*
- * Deliantra is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * Deliantra is free software: you can redistribute it and/or modify it under
+ * the terms of the Affero GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
+ * You should have received a copy of the Affero GNU General Public License
+ * and the GNU General Public License along with this program. If not, see
+ * .
*
* The authors can be reached via e-mail to
*/
@@ -120,13 +121,25 @@
template
static inline T sign0 (T v) { return v ? sign (v) : 0; }
+// div* only work correctly for div > 0
// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
-template static inline T div (T val, T div) { return (val + div / 2) / div; }
+template static inline T div (T val, T div)
+{
+ return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div;
+}
// div, round-up
-template static inline T div_ru (T val, T div) { return (val + div - 1) / div; }
+template static inline T div_ru (T val, T div)
+{
+ return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div;
+}
// div, round-down
-template static inline T div_rd (T val, T div) { return (val ) / div; }
+template static inline T div_rd (T val, T div)
+{
+ return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div;
+}
+// lerp* only work correctly for min_in < max_in
+// Linear intERPolate, scales val from min_in..max_in to min_out..max_out
template
static inline T
lerp (T val, T min_in, T max_in, T min_out, T max_out)
@@ -411,6 +424,7 @@
// http://www.jstatsoft.org/v08/i14/paper
// this one is about 40% faster than the tausworthe one above (i.e. not much),
// despite the inlining, and has the issue of only creating 2**32-1 numbers.
+// see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf
struct xorshift_random_generator
{
uint32_t x, y;
@@ -677,13 +691,14 @@
};
// basically does what strncpy should do, but appends "..." to strings exceeding length
-void assign (char *dst, const char *src, int maxlen);
+// returns the number of bytes actually used (including \0)
+int assign (char *dst, const char *src, int maxsize);
// type-safe version of assign
template
-inline void assign (char (&dst)[N], const char *src)
+inline int assign (char (&dst)[N], const char *src)
{
- assign ((char *)&dst, src, N);
+ return assign ((char *)&dst, src, N);
}
typedef double tstamp;
@@ -693,8 +708,12 @@
int similar_direction (int a, int b);
-// like sprintf, but returns a "static" buffer
-const char *format (const char *format, ...);
+// like v?sprintf, but returns a "static" buffer
+char *vformat (const char *format, va_list ap);
+char *format (const char *format, ...);
+
+// safety-check player input which will become object->msg
+bool msg_is_safe (const char *msg);
/////////////////////////////////////////////////////////////////////////////
// threads, very very thin wrappers around pthreads