--- deliantra/server/include/util.h 2009/01/03 01:04:19 1.86 +++ deliantra/server/include/util.h 2009/10/12 14:00:58 1.90 @@ -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) @@ -678,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; @@ -697,6 +711,9 @@ // like sprintf, but returns a "static" buffer const 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