Commit d9c633fda2f26a1bb02f81dd80e548db4588fc06

Authored by dg
1 parent 148dd553

add noise generators from libtcod


git-svn-id: http://svn.net-core.org/repos/t-engine4@667 51575b47-30f0-44d4-a5cc-537603b46e54
... ... @@ -822,7 +822,7 @@ end
822 822
823 823 function _M:setAllowedBuild(what, notify)
824 824 -- Do not unlock things in easy mode
825   - if game.difficulty == game.DIFFICULTY_EASY then return end
  825 + --if game.difficulty == game.DIFFICULTY_EASY then return end
826 826
827 827 config.settings.tome = config.settings.tome or {}
828 828 config.settings.tome.allow_build = config.settings.tome.allow_build or {}
... ...
... ... @@ -66,6 +66,7 @@ newBirthDescriptor{
66 66 "Easy game setting",
67 67 "All damage done to the player reduced by 20%",
68 68 "All healing for the player increased by 10%",
  69 + "No achievements possible.",
69 70 },
70 71 copy = { resolvers.generic(function() game.difficulty = game.DIFFICULTY_EASY end) },
71 72 }
... ...
... ... @@ -20,6 +20,7 @@ solution "TEngine"
20 20 _OPTIONS.lua == "default" and "src/lua" or "src/luajit",
21 21 "src/luasocket",
22 22 "src/fov",
  23 + "src/libtcod_import",
23 24 "src/physfs",
24 25 "src/physfs/zlib123",
25 26 "/usr/include/SDL",
... ... @@ -62,7 +63,7 @@ project "TEngine"
62 63 language "C"
63 64 targetname "t-engine"
64 65 files { "src/*.c", }
65   - links { "physfs", "lua".._OPTIONS.lua, "fov", "luasocket", "luaprofiler", "lualanes", "lpeg" }
  66 + links { "physfs", "lua".._OPTIONS.lua, "fov", "luasocket", "luaprofiler", "lualanes", "lpeg", "tcodimport" }
66 67 defines { "_DEFAULT_VIDEOMODE_FLAGS_='SDL_HWSURFACE|SDL_DOUBLEBUF'" }
67 68 defines { [[TENGINE_HOME_PATH='".t-engine"']] }
68 69
... ... @@ -196,3 +197,10 @@ project "lualanes"
196 197 targetname "lualanes"
197 198
198 199 files { "src/lualanes/*.c", }
  200 +
  201 +project "tcodimport"
  202 + kind "StaticLib"
  203 + language "C"
  204 + targetname "tcodimport"
  205 +
  206 + files { "src/libtcod_import/*.c", }
... ...
... ... @@ -93,6 +93,12 @@ inline static double genrand_real1(void)
93 93 return to_real1(gen_rand32());
94 94 }
95 95
  96 +/** generates a random number on [-1,1]-real-interval */
  97 +inline static double genrand_real(double min, double max)
  98 +{
  99 + return gen_rand32() * ((max-min)/4294967295.0)+min;
  100 +}
  101 +
96 102 /** generates a random number on [0,1)-real-interval */
97 103 inline static double to_real2(uint32_t v)
98 104 {
... ...
... ... @@ -973,6 +973,14 @@ static const struct luaL_reg sdl_font_reg[] =
973 973 ******************************************************************
974 974 ******************************************************************/
975 975
  976 +static int rng_float(lua_State *L)
  977 +{
  978 + float min = luaL_checknumber(L, 1);
  979 + float max = luaL_checknumber(L, 2);
  980 + lua_pushnumber(L, genrand_real(min, max));
  981 + return 1;
  982 +}
  983 +
976 984 static int rng_dice(lua_State *L)
977 985 {
978 986 int x = luaL_checknumber(L, 1);
... ... @@ -1185,6 +1193,7 @@ static const struct luaL_reg rnglib[] =
1185 1193 {"chance", rng_chance},
1186 1194 {"percent", rng_percent},
1187 1195 {"normal", rng_normal},
  1196 + {"float", rng_float},
1188 1197 {NULL, NULL},
1189 1198 };
1190 1199
... ...
  1 +/*
  2 +* libtcod 1.5.0
  3 +* Copyright (c) 2008,2009,2010 Jice
  4 +* All rights reserved.
  5 +*
  6 +* Redistribution and use in source and binary forms, with or without
  7 +* modification, are permitted provided that the following conditions are met:
  8 +* * Redistributions of source code must retain the above copyright
  9 +* notice, this list of conditions and the following disclaimer.
  10 +* * Redistributions in binary form must reproduce the above copyright
  11 +* notice, this list of conditions and the following disclaimer in the
  12 +* documentation and/or other materials provided with the distribution.
  13 +* * The name of Jice may not be used to endorse or promote products
  14 +* derived from this software without specific prior written permission.
  15 +*
  16 +* THIS SOFTWARE IS PROVIDED BY Jice ``AS IS'' AND ANY
  17 +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18 +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19 +* DISCLAIMED. IN NO EVENT SHALL Jice BE LIABLE FOR ANY
  20 +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21 +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22 +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23 +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24 +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25 +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26 +*/
  27 +
  28 +#ifndef _TCODLIB_H
  29 +#define _TCODLIB_H
  30 +
  31 +// uncomment to disable unicode support
  32 +//#define NO_UNICODE
  33 +
  34 +// os identification
  35 +// TCOD_WINDOWS : OS is windows
  36 +// TCOD_LINUX : OS is Linux
  37 +// TCOD_MACOSX : OS is Mac OS X
  38 +
  39 +// compiler identification
  40 +// TCOD_VISUAL_STUDIO : compiler is Microsoft Visual Studio
  41 +// TCOD_MINGW32 : compiler is Mingw32
  42 +// TCOD_GCC : compiler is gcc/g++
  43 +
  44 +// word size
  45 +// TCOD_64BITS : 64 bits OS
  46 +// TCOD_WIN64 : 64 bits Windows
  47 +// TCOD_WIN32 : 32 bits Windows
  48 +// TCOD_LINUX64 : 64 bits Linux
  49 +// TCOD_LINUX32 : 32 bits Linux
  50 +
  51 +#if defined( _MSC_VER )
  52 +# define TCOD_VISUAL_STUDIO
  53 +# define TCOD_WINDOWS
  54 +# ifdef _WIN64
  55 +# define TCOD_WIN64
  56 +# define TCOD_64BITS
  57 +# else
  58 +# define TCOD_WIN32
  59 +# endif
  60 +#elif defined( __MINGW32__ )
  61 +# define TCOD_WINDOWS
  62 +# define TCOD_MINGW32
  63 +# define TCOD_WIN32
  64 +#elif defined( __linux )
  65 +# define TCOD_LINUX
  66 +# define TCOD_GCC
  67 +# if __WORDSIZE == 64
  68 +# define TCOD_LINUX64
  69 +# define TCOD_64BITS
  70 +# else
  71 +# define TCOD_LINUX32
  72 +# endif
  73 +#elif defined (__APPLE__) && defined (__MACH__)
  74 +# define TCOD_MACOSX
  75 +# define TCOD_GCC
  76 +#endif
  77 +
  78 +// unicode rendering functions support
  79 +#ifndef NO_UNICODE
  80 +#include <wchar.h>
  81 +#endif
  82 +
  83 +// SDL_main support for OSX
  84 +#ifdef TCOD_MACOSX
  85 +#include "SDL/SDL.h"
  86 +#endif
  87 +
  88 +// base types
  89 +typedef unsigned char uint8;
  90 +typedef char int8;
  91 +typedef unsigned short uint16;
  92 +typedef short int16;
  93 +typedef unsigned int uint32;
  94 +typedef int int32;
  95 +// int with the same size as a pointer (32 or 64 depending on OS)
  96 +typedef long intptr;
  97 +typedef unsigned long uintptr;
  98 +
  99 +#define TCOD_HEXVERSION 0x010500
  100 +#define TCOD_STRVERSION "1.5.0"
  101 +#define TCOD_TECHVERSION 0x01050003
  102 +
  103 +// DLL export
  104 +#ifdef TCOD_WINDOWS
  105 +#ifdef LIBTCOD_EXPORTS
  106 +#define TCODLIB_API __declspec(dllexport)
  107 +#else
  108 +#define TCODLIB_API __declspec(dllimport)
  109 +#endif
  110 +#else
  111 +#define TCODLIB_API
  112 +#endif
  113 +
  114 +#ifdef __cplusplus
  115 +extern "C" {
  116 +#endif
  117 +
  118 +#ifdef TCOD_VISUAL_STUDIO
  119 +#define strdup _strdup
  120 +#define strcasecmp _stricmp
  121 +#define strncasecmp _strnicmp
  122 +#endif
  123 +#if defined(TCOD_WINDOWS)
  124 +char *strcasestr (const char *haystack, const char *needle);
  125 +#endif
  126 +#ifdef TCOD_LINUX
  127 +#define vsnwprintf vswprintf
  128 +#endif
  129 +#ifdef TCOD_WINDOWS
  130 +#define vsnwprintf _vsnwprintf
  131 +#endif
  132 +
  133 +/******************************************
  134 + utility macros
  135 + ******************************************/
  136 +#define MAX(a,b) ((a)<(b)?(b):(a))
  137 +#define MIN(a,b) ((a)>(b)?(b):(a))
  138 +#define ABS(a) ((a)<0?-(a):(a))
  139 +#define CLAMP(a, b, x) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
  140 +#define LERP(a, b, x) ( a + x * (b - a) )
  141 +
  142 +/*
  143 +#include "list.h"
  144 +#include "color.h"
  145 +#include "console.h"
  146 +#include "image.h"
  147 +#include "sys.h"
  148 +#include "mersenne.h"
  149 +#include "mouse.h"
  150 +#include "bresenham.h"
  151 +*/
  152 +#include "noise.h"
  153 +/*
  154 +#include "fov.h"
  155 +#include "path.h"
  156 +#include "lex.h"
  157 +#include "parser.h"
  158 +#include "tree.h"
  159 +#include "bsp.h"
  160 +#include "heightmap.h"
  161 +#include "zip.h"
  162 +#include "namegen.h"
  163 +#include "txtfield.h"
  164 +*/
  165 +#ifdef __cplusplus
  166 +}
  167 +#endif
  168 +
  169 +#endif
... ...
  1 +/*
  2 +* libtcod 1.5.0
  3 +* Copyright (c) 2008,2009,2010 Jice
  4 +* All rights reserved.
  5 +*
  6 +* Redistribution and use in source and binary forms, with or without
  7 +* modification, are permitted provided that the following conditions are met:
  8 +* * Redistributions of source code must retain the above copyright
  9 +* notice, this list of conditions and the following disclaimer.
  10 +* * Redistributions in binary form must reproduce the above copyright
  11 +* notice, this list of conditions and the following disclaimer in the
  12 +* documentation and/or other materials provided with the distribution.
  13 +* * The name of Jice may not be used to endorse or promote products
  14 +* derived from this software without specific prior written permission.
  15 +*
  16 +* THIS SOFTWARE IS PROVIDED BY Jice ``AS IS'' AND ANY
  17 +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18 +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19 +* DISCLAIMED. IN NO EVENT SHALL Jice BE LIABLE FOR ANY
  20 +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21 +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22 +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23 +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24 +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25 +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26 +*/
  27 +
  28 +#ifndef _TCOD_PERLIN_H
  29 +#define _TCOD_PERLIN_H
  30 +
  31 +typedef void *TCOD_noise_t;
  32 +
  33 +#define TCOD_NOISE_MAX_OCTAVES 128
  34 +#define TCOD_NOISE_MAX_DIMENSIONS 4
  35 +#define TCOD_NOISE_DEFAULT_HURST 0.5f
  36 +#define TCOD_NOISE_DEFAULT_LACUNARITY 2.0f
  37 +
  38 +TCODLIB_API TCOD_noise_t TCOD_noise_new(int dimensions, float hurst, float lacunarity);
  39 +// basic perlin noise
  40 +TCODLIB_API float TCOD_noise_perlin( TCOD_noise_t noise, float *f );
  41 +// fractional brownian motion (fractal sum of perlin noises)
  42 +TCODLIB_API float TCOD_noise_fbm_perlin( TCOD_noise_t noise, float *f, float octaves );
  43 +// turbulence (fractal sum of abs(perlin noise) )
  44 +TCODLIB_API float TCOD_noise_turbulence_perlin( TCOD_noise_t noise, float *f, float octaves );
  45 +// simplex noise
  46 +TCODLIB_API float TCOD_noise_simplex( TCOD_noise_t noise, float *f );
  47 +// fractional brownian motion (fractal sum of simplex noises)
  48 +TCODLIB_API float TCOD_noise_fbm_simplex( TCOD_noise_t noise, float *f, float octaves );
  49 +// turbulence (fractal sum of abs(simplex noise) )
  50 +TCODLIB_API float TCOD_noise_turbulence_simplex( TCOD_noise_t noise, float *f, float octaves );
  51 +// wavelet noise
  52 +TCODLIB_API float TCOD_noise_wavelet (TCOD_noise_t noise, float *f);
  53 +// fractional brownian motion (fractal sum of wavelet noises)
  54 +TCODLIB_API float TCOD_noise_fbm_wavelet(TCOD_noise_t noise, float *f, float octaves);
  55 +// turbulence (fractal sum of abs(simplex noise) )
  56 +TCODLIB_API float TCOD_noise_turbulence_wavelet(TCOD_noise_t noise, float *f, float octaves);
  57 +
  58 +TCODLIB_API void TCOD_noise_delete(TCOD_noise_t noise);
  59 +
  60 +#endif
... ...
  1 +#if 1
  2 +/*
  3 +* libtcod 1.5.0
  4 +* Copyright (c) 2008,2009,2010 Jice
  5 +* All rights reserved.
  6 +*
  7 +* Redistribution and use in source and binary forms, with or without
  8 +* modification, are permitted provided that the following conditions are met:
  9 +* * Redistributions of source code must retain the above copyright
  10 +* notice, this list of conditions and the following disclaimer.
  11 +* * Redistributions in binary form must reproduce the above copyright
  12 +* notice, this list of conditions and the following disclaimer in the
  13 +* documentation and/or other materials provided with the distribution.
  14 +* * The name of Jice may not be used to endorse or promote products
  15 +* derived from this software without specific prior written permission.
  16 +*
  17 +* THIS SOFTWARE IS PROVIDED BY Jice ``AS IS'' AND ANY
  18 +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19 +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20 +* DISCLAIMED. IN NO EVENT SHALL Jice BE LIABLE FOR ANY
  21 +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22 +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23 +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24 +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25 +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26 +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27 +*/
  28 +
  29 +#include <math.h>
  30 +#include <stdlib.h>
  31 +#include <string.h>
  32 +#include "SFMT.h"
  33 +#include "libtcod.h"
  34 +#include "noise.h"
  35 +
  36 +#define WAVELET_TILE_SIZE 32
  37 +#define WAVELET_ARAD 16
  38 +
  39 +#define SIMPLEX_SCALE 0.5f
  40 +#define WAVELET_SCALE 2.0f
  41 +
  42 +typedef struct {
  43 + int ndim;
  44 + unsigned char map[256]; // Randomized map of indexes into buffer
  45 + float buffer[256][TCOD_NOISE_MAX_DIMENSIONS]; // Random 256 x ndim buffer
  46 + // fractal stuff
  47 + float H;
  48 + float lacunarity;
  49 + float exponent[TCOD_NOISE_MAX_OCTAVES];
  50 + float *waveletTileData;
  51 +} perlin_data_t;
  52 +
  53 +static float lattice( perlin_data_t *data, int ix, float fx, int iy, float fy, int iz, float fz, int iw, float fw)
  54 +{
  55 + int n[4] = {ix, iy, iz, iw};
  56 + float f[4] = {fx, fy, fz, fw};
  57 + int nIndex = 0;
  58 + int i;
  59 + float value = 0;
  60 + for(i=0; i<data->ndim; i++)
  61 + nIndex = data->map[(nIndex + n[i]) & 0xFF];
  62 + for(i=0; i<data->ndim; i++)
  63 + value += data->buffer[nIndex][i] * f[i];
  64 + return value;
  65 +}
  66 +
  67 +#define DEFAULT_SEED 0x15687436
  68 +#define DELTA 1e-6f
  69 +#define SWAP(a, b, t) t = a; a = b; b = t
  70 +
  71 +#define FLOOR(a) ((a)> 0 ? ((int)a) : (((int)a)-1) )
  72 +#define CUBIC(a) ( a * a * (3 - 2*a) )
  73 +
  74 +static void normalize(perlin_data_t *data, float *f)
  75 +{
  76 + float magnitude = 0;
  77 + int i;
  78 + for(i=0; i<data->ndim; i++)
  79 + magnitude += f[i]*f[i];
  80 + magnitude = 1 / sqrtf(magnitude);
  81 + for(i=0; i<data->ndim; i++)
  82 + f[i] *= magnitude;
  83 +}
  84 +
  85 +
  86 +TCOD_noise_t TCOD_noise_new(int ndim, float hurst, float lacunarity)
  87 +{
  88 + perlin_data_t *data=(perlin_data_t *)calloc(sizeof(perlin_data_t),1);
  89 + int i, j;
  90 + unsigned char tmp;
  91 + float f = 1;
  92 + data->ndim = ndim;
  93 + for(i=0; i<256; i++)
  94 + {
  95 + data->map[i] = (unsigned char)i;
  96 + for(j=0; j<data->ndim; j++)
  97 + data->buffer[i][j] = genrand_real(-0.5, 0.5);
  98 + normalize(data,data->buffer[i]);
  99 + }
  100 +
  101 + while(--i)
  102 + {
  103 + j = rand_div(256);
  104 + SWAP(data->map[i], data->map[j], tmp);
  105 + }
  106 +
  107 + data->H = hurst;
  108 + data->lacunarity = lacunarity;
  109 + for(i=0; i<TCOD_NOISE_MAX_OCTAVES; i++)
  110 + {
  111 + //exponent[i] = powf(f, -H);
  112 + data->exponent[i] = 1.0f / f;
  113 + f *= lacunarity;
  114 + }
  115 + return (TCOD_noise_t)data;
  116 +}
  117 +
  118 +float TCOD_noise_perlin( TCOD_noise_t noise, float *f )
  119 +{
  120 + perlin_data_t *data=(perlin_data_t *)noise;
  121 + int n[TCOD_NOISE_MAX_DIMENSIONS]; // Indexes to pass to lattice function
  122 + int i;
  123 + float r[TCOD_NOISE_MAX_DIMENSIONS]; // Remainders to pass to lattice function
  124 + float w[TCOD_NOISE_MAX_DIMENSIONS]; // Cubic values to pass to interpolation function
  125 + float value;
  126 +
  127 + for(i=0; i<data->ndim; i++)
  128 + {
  129 + n[i] = FLOOR(f[i]);
  130 + r[i] = f[i] - n[i];
  131 + w[i] = CUBIC(r[i]);
  132 + }
  133 +
  134 + switch(data->ndim)
  135 + {
  136 + case 1:
  137 + value = LERP(lattice(data,n[0], r[0],0,0,0,0,0,0),
  138 + lattice(data,n[0]+1, r[0]-1,0,0,0,0,0,0),
  139 + w[0]);
  140 + break;
  141 + case 2:
  142 + value = LERP(LERP(lattice(data,n[0], r[0], n[1], r[1],0,0,0,0),
  143 + lattice(data,n[0]+1, r[0]-1, n[1], r[1],0,0,0,0),
  144 + w[0]),
  145 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1,0,0,0,0),
  146 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1,0,0,0,0),
  147 + w[0]),
  148 + w[1]);
  149 + break;
  150 + case 3:
  151 + value = LERP(LERP(LERP(lattice(data,n[0], r[0], n[1], r[1], n[2], r[2],0,0),
  152 + lattice(data,n[0]+1, r[0]-1, n[1], r[1], n[2], r[2],0,0),
  153 + w[0]),
  154 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1, n[2], r[2],0,0),
  155 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1, n[2], r[2],0,0),
  156 + w[0]),
  157 + w[1]),
  158 + LERP(LERP(lattice(data,n[0], r[0], n[1], r[1], n[2]+1, r[2]-1,0,0),
  159 + lattice(data,n[0]+1, r[0]-1, n[1], r[1], n[2]+1, r[2]-1,0,0),
  160 + w[0]),
  161 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1, n[2]+1, r[2]-1,0,0),
  162 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1, n[2]+1, r[2]-1,0,0),
  163 + w[0]),
  164 + w[1]),
  165 + w[2]);
  166 + break;
  167 + case 4:
  168 + default:
  169 + value = LERP(LERP(LERP(LERP(lattice(data,n[0], r[0], n[1], r[1], n[2], r[2], n[3], r[3]),
  170 + lattice(data,n[0]+1, r[0]-1, n[1], r[1], n[2], r[2], n[3], r[3]),
  171 + w[0]),
  172 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1, n[2], r[2], n[3], r[3]),
  173 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1, n[2], r[2], n[3], r[3]),
  174 + w[0]),
  175 + w[1]),
  176 + LERP(LERP(lattice(data,n[0], r[0], n[1], r[1], n[2]+1, r[2]-1, n[3], r[3]),
  177 + lattice(data,n[0]+1, r[0]-1, n[1], r[1], n[2]+1, r[2]-1, n[3], r[3]),
  178 + w[0]),
  179 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1, n[2]+1, r[2]-1,0,0),
  180 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1, n[2]+1, r[2]-1, n[3], r[3]),
  181 + w[0]),
  182 + w[1]),
  183 + w[2]),
  184 + LERP(LERP(LERP(lattice(data,n[0], r[0], n[1], r[1], n[2], r[2], n[3]+1, r[3]-1),
  185 + lattice(data,n[0]+1, r[0]-1, n[1], r[1], n[2], r[2], n[3]+1, r[3]-1),
  186 + w[0]),
  187 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1, n[2], r[2], n[3]+1, r[3]-1),
  188 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1, n[2], r[2], n[3]+1, r[3]-1),
  189 + w[0]),
  190 + w[1]),
  191 + LERP(LERP(lattice(data,n[0], r[0], n[1], r[1], n[2]+1, r[2]-1, n[3]+1, r[3]-1),
  192 + lattice(data,n[0]+1, r[0]-1, n[1], r[1], n[2]+1, r[2]-1, n[3]+1, r[3]-1),
  193 + w[0]),
  194 + LERP(lattice(data,n[0], r[0], n[1]+1, r[1]-1, n[2]+1, r[2]-1,0,0),
  195 + lattice(data,n[0]+1, r[0]-1, n[1]+1, r[1]-1, n[2]+1, r[2]-1, n[3]+1, r[3]-1),
  196 + w[0]),
  197 + w[1]),
  198 + w[2]),
  199 + w[3]);
  200 + break;
  201 + }
  202 + return CLAMP(-0.99999f, 0.99999f, value);
  203 +}
  204 +
  205 +typedef float (*TCOD_noise_func_t)( TCOD_noise_t noise, float *f );
  206 +
  207 +static float TCOD_noise_fbm_int(TCOD_noise_t noise, float *f, float octaves, TCOD_noise_func_t func ) {
  208 + float tf[TCOD_NOISE_MAX_DIMENSIONS];
  209 + perlin_data_t *data=(perlin_data_t *)noise;
  210 + // Initialize locals
  211 + double value = 0;
  212 + int i,j;
  213 + memcpy(tf,f,sizeof(float)*data->ndim);
  214 +
  215 + // Inner loop of spectral construction, where the fractal is built
  216 + for(i=0; i<(int)octaves; i++)
  217 + {
  218 + value += (double)(func(noise,tf)) * data->exponent[i];
  219 + for (j=0; j < data->ndim; j++) tf[j] *= data->lacunarity;
  220 + }
  221 +
  222 + // Take care of remainder in octaves
  223 + octaves -= (int)octaves;
  224 + if(octaves > DELTA)
  225 + value += (double)(octaves * func(noise,tf)) * data->exponent[i];
  226 + return CLAMP(-0.99999f, 0.99999f, (float)value);
  227 +}
  228 +
  229 +float TCOD_noise_fbm_perlin( TCOD_noise_t noise, float *f, float octaves )
  230 +{
  231 + return TCOD_noise_fbm_int(noise,f,octaves,TCOD_noise_perlin);
  232 +/*
  233 + float tf[TCOD_NOISE_MAX_DIMENSIONS];
  234 + perlin_data_t *data=(perlin_data_t *)noise;
  235 + // Initialize locals
  236 + double value = 0;
  237 + int i,j;
  238 + memcpy(tf,f,sizeof(float)*data->ndim);
  239 +
  240 + // Inner loop of spectral construction, where the fractal is built
  241 + for(i=0; i<(int)octaves; i++)
  242 + {
  243 + value += (double)(TCOD_noise_simplex(noise,tf)) * data->exponent[i];
  244 + for (j=0; j < data->ndim; j++) tf[j] *= data->lacunarity;
  245 + }
  246 +
  247 + // Take care of remainder in octaves
  248 + octaves -= (int)octaves;
  249 + if(octaves > DELTA)
  250 + value += (double)(octaves * TCOD_noise_simplex(noise,tf)) * data->exponent[i];
  251 + return CLAMP(-0.99999f, 0.99999f, (float)value);
  252 +*/
  253 +}
  254 +
  255 +float TCOD_noise_fbm_simplex( TCOD_noise_t noise, float *f, float octaves )
  256 +{
  257 + return TCOD_noise_fbm_int(noise,f,octaves,TCOD_noise_simplex);
  258 +}
  259 +
  260 +static float TCOD_noise_turbulence_int( TCOD_noise_t noise, float *f, float octaves, TCOD_noise_func_t func )
  261 +{
  262 + float tf[TCOD_NOISE_MAX_DIMENSIONS];
  263 + perlin_data_t *data=(perlin_data_t *)noise;
  264 + // Initialize locals
  265 + double value = 0;
  266 + int i,j;
  267 + memcpy(tf,f,sizeof(float)*data->ndim);
  268 +
  269 + // Inner loop of spectral construction, where the fractal is built
  270 + for(i=0; i<(int)octaves; i++)
  271 + {
  272 + float nval=func(noise,tf);
  273 + value += (double)(ABS(nval)) * data->exponent[i];
  274 + for (j=0; j < data->ndim; j++) tf[j] *= data->lacunarity;
  275 + }
  276 +
  277 + // Take care of remainder in octaves
  278 + octaves -= (int)octaves;
  279 + if(octaves > DELTA) {
  280 + float nval=func(noise,tf);
  281 + value += (double)(octaves * ABS(nval)) * data->exponent[i];
  282 + }
  283 + return CLAMP(-0.99999f, 0.99999f, (float)value);
  284 +}
  285 +
  286 +float TCOD_noise_turbulence_perlin( TCOD_noise_t noise, float *f, float octaves ) {
  287 + return TCOD_noise_turbulence_int(noise,f,octaves,TCOD_noise_perlin);
  288 +}
  289 +
  290 +float TCOD_noise_turbulence_simplex( TCOD_noise_t noise, float *f, float octaves ) {
  291 + return TCOD_noise_turbulence_int(noise,f,octaves,TCOD_noise_simplex);
  292 +}
  293 +
  294 +// simplex noise, adapted from Ken Perlin's presentation at Siggraph 2001
  295 +// and Stefan Gustavson implementation
  296 +
  297 +#define TCOD_NOISE_SIMPLEX_GRADIENT_1D(n,h,x) { \
  298 + float grad; \
  299 + h &= 0xF; \
  300 + grad=1.0f+(h & 7); \
  301 + if ( h & 8 ) grad = -grad; \
  302 + n = grad * x; \
  303 +}
  304 +
  305 +#define TCOD_NOISE_SIMPLEX_GRADIENT_2D(n,h,x,y) { \
  306 + float u,v; \
  307 + h &= 0x7; \
  308 + if ( h < 4 ) { \
  309 + u=x; \
  310 + v=2.0f*y; \
  311 + } else { \
  312 + u=y; \
  313 + v=2.0f*x; \
  314 + } \
  315 + n = ((h & 1) ? -u : u) + ((h & 2) ? -v :v ); \
  316 +}
  317 +
  318 +#define TCOD_NOISE_SIMPLEX_GRADIENT_3D(n,h,x,y,z) { \
  319 + float u,v; \
  320 + h &= 0xF; \
  321 + u = (h < 8 ? x : y); \
  322 + v = (h < 4 ? y : ( h == 12 || h == 14 ? x : z ) ); \
  323 + n= ((h & 1) ? -u : u ) + ((h & 2) ? -v : v); \
  324 +}
  325 +
  326 +#define TCOD_NOISE_SIMPLEX_GRADIENT_4D(n,h,x,y,z,t) { \
  327 + float u,v,w; \
  328 + h &= 0x1F; \
  329 + u = (h < 24 ? x:y); \
  330 + v = (h < 16 ? y:z); \
  331 + w = (h < 8 ? z:t); \
  332 + n= ((h & 1) ? -u : u ) + ((h & 2) ? -v : v) + ((h & 4) ? -w : w);\
  333 +}
  334 +
  335 +static float simplex[64][4] = {
  336 + {0,1,2,3},{0,1,3,2},{0,0,0,0},{0,2,3,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,2,3,0},
  337 + {0,2,1,3},{0,0,0,0},{0,3,1,2},{0,3,2,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,3,2,0},
  338 + {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
  339 + {1,2,0,3},{0,0,0,0},{1,3,0,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,3,0,1},{2,3,1,0},
  340 + {1,0,2,3},{1,0,3,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,0,3,1},{0,0,0,0},{2,1,3,0},
  341 + {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
  342 + {2,0,1,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0},
  343 + {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0},
  344 +
  345 +};
  346 +
  347 +float TCOD_noise_simplex(TCOD_noise_t noise, float *f) {
  348 + perlin_data_t *data=(perlin_data_t *)noise;
  349 + switch(data->ndim) {
  350 + case 1 :
  351 + {
  352 + int i0=(int)FLOOR(f[0]*SIMPLEX_SCALE);
  353 + int i1=i0+1;
  354 + float x0 = f[0]*SIMPLEX_SCALE - i0;
  355 + float x1 = x0 - 1.0f;
  356 + float t0 = 1.0f - x0*x0;
  357 + float t1 = 1.0f - x1*x1;
  358 + float n0,n1;
  359 + t0 = t0*t0;
  360 + t1 = t1*t1;
  361 + i0=data->map[i0&0xFF];
  362 + TCOD_NOISE_SIMPLEX_GRADIENT_1D(n0,i0,x0);
  363 + n0*=t0*t0;
  364 + i1=data->map[i1&0xFF];
  365 + TCOD_NOISE_SIMPLEX_GRADIENT_1D(n1,i1,x1);
  366 + n1*=t1*t1;
  367 + return 0.25f * (n0+n1);
  368 + }
  369 + break;
  370 + case 2 :
  371 + {
  372 + #define F2 0.366025403f // 0.5f * (sqrtf(3.0f)-1.0f);
  373 + #define G2 0.211324865f // (3.0f - sqrtf(3.0f))/6.0f;
  374 +
  375 + float s = (f[0]+f[1])*F2*SIMPLEX_SCALE;
  376 + float xs = f[0]*SIMPLEX_SCALE+s;
  377 + float ys = f[1]*SIMPLEX_SCALE+s;
  378 + int i=FLOOR(xs);
  379 + int j=FLOOR(ys);
  380 + float t = (i+j)*G2;
  381 + float xo = i-t;
  382 + float yo = j-t;
  383 + float x0 = f[0]*SIMPLEX_SCALE-xo;
  384 + float y0 = f[1]*SIMPLEX_SCALE-yo;
  385 + int i1,j1,ii = i%256,jj = j%256;
  386 + float n0,n1,n2,x1,y1,x2,y2,t0,t1,t2;
  387 + if ( x0 > y0 ) {
  388 + i1=1;j1=0;
  389 + } else {
  390 + i1=0;j1=1;
  391 + }
  392 + x1 = x0 - i1 + G2;
  393 + y1 = y0 - j1 + G2;
  394 + x2 = x0 - 1.0f + 2.0f * G2;
  395 + y2 = y0 - 1.0f + 2.0f * G2;
  396 + t0 = 0.5f - x0*x0 - y0*y0;
  397 + if ( t0 < 0.0f ) {
  398 + n0 = 0.0f;
  399 + } else {
  400 + int idx = (ii + data->map[jj])&0xFF;
  401 + t0 *= t0;
  402 + idx=data->map[idx];
  403 + TCOD_NOISE_SIMPLEX_GRADIENT_2D(n0,idx,x0,y0);
  404 + n0 *= t0*t0;
  405 + }
  406 + t1 = 0.5f - x1*x1 -y1*y1;
  407 + if ( t1 < 0.0f ) {
  408 + n1 = 0.0f;
  409 + } else {
  410 + int idx = (ii + i1 + data->map[(jj+j1)&0xFF]) & 0xFF;
  411 + t1 *= t1;
  412 + idx=data->map[idx];
  413 + TCOD_NOISE_SIMPLEX_GRADIENT_2D(n1,idx,x1,y1);
  414 + n1 *= t1*t1;
  415 + }
  416 + t2 = 0.5f - x2*x2 -y2*y2;
  417 + if ( t2 < 0.0f ) {
  418 + n2 = 0.0f;
  419 + } else {
  420 + int idx = (ii + 1 + data->map[(jj+1)&0xFF]) & 0xFF;
  421 + t2 *= t2;
  422 + idx=data->map[idx];
  423 + TCOD_NOISE_SIMPLEX_GRADIENT_2D(n2,idx,x2,y2);
  424 + n2 *= t2*t2;
  425 + }
  426 + return 40.0f * (n0+n1+n2);
  427 + }
  428 + break;
  429 + case 3 :
  430 + {
  431 + #define F3 0.333333333f
  432 + #define G3 0.166666667f
  433 + float n0,n1,n2,n3;
  434 + float s =(f[0]+f[1]+f[2])*F3*SIMPLEX_SCALE;
  435 + float xs=f[0]*SIMPLEX_SCALE+s;
  436 + float ys=f[1]*SIMPLEX_SCALE+s;
  437 + float zs=f[2]*SIMPLEX_SCALE+s;
  438 + int i=FLOOR(xs);
  439 + int j=FLOOR(ys);
  440 + int k=FLOOR(zs);
  441 + float t=(float)(i+j+k)*G3;
  442 + float xo = i-t;
  443 + float yo = j-t;
  444 + float zo = k-t;
  445 + float x0 = f[0]*SIMPLEX_SCALE-xo;
  446 + float y0 = f[1]*SIMPLEX_SCALE-yo;
  447 + float z0 = f[2]*SIMPLEX_SCALE-zo;
  448 + int i1,j1,k1,i2,j2,k2,ii,jj,kk;
  449 + float x1,y1,z1,x2,y2,z2,x3,y3,z3,t0,t1,t2,t3;
  450 + if ( x0 >= y0 ) {
  451 + if ( y0 >= z0 ) {
  452 + i1=1;j1=0;k1=0;i2=1;j2=1;k2=0;
  453 + } else if ( x0 >= z0 ) {
  454 + i1=1;j1=0;k1=0;i2=1;j2=0;k2=1;
  455 + } else {
  456 + i1=0;j1=0;k1=1;i2=1;j2=0;k2=1;
  457 + }
  458 + } else {
  459 + if ( y0 < z0 ) {
  460 + i1=0;j1=0;k1=1;i2=0;j2=1;k2=1;
  461 + } else if ( x0 < z0 ) {
  462 + i1=0;j1=1;k1=0;i2=0;j2=1;k2=1;
  463 + } else {
  464 + i1=0;j1=1;k1=0;i2=1;j2=1;k2=0;
  465 + }
  466 + }
  467 + x1 = x0 -i1 + G3;
  468 + y1 = y0 -j1 + G3;
  469 + z1 = z0 -k1 + G3;
  470 + x2 = x0 -i2 + 2.0f*G3;
  471 + y2 = y0 -j2 + 2.0f*G3;
  472 + z2 = z0 -k2 + 2.0f*G3;
  473 + x3 = x0 - 1.0f +3.0f * G3;
  474 + y3 = y0 - 1.0f +3.0f * G3;
  475 + z3 = z0 - 1.0f +3.0f * G3;
  476 + ii = i%256;
  477 + jj = j%256;
  478 + kk = k%256;
  479 + t0 = 0.6f - x0*x0 -y0*y0 -z0*z0;
  480 + if ( t0 < 0.0f ) n0 = 0.0f;
  481 + else {
  482 + int idx = data->map[ (ii + data->map[ (jj + data->map[ kk ]) &0xFF ])& 0xFF ];
  483 + t0 *= t0;
  484 + TCOD_NOISE_SIMPLEX_GRADIENT_3D(n0,idx,x0,y0,z0);
  485 + n0 *= t0*t0;
  486 + }
  487 + t1 = 0.6f - x1*x1 -y1*y1 -z1*z1;
  488 + if ( t1 < 0.0f ) n1 = 0.0f;
  489 + else {
  490 + int idx = data->map[ (ii + i1 + data->map[ (jj + j1 + data->map[ (kk + k1)& 0xFF ]) &0xFF ])& 0xFF ];
  491 + t1 *= t1;
  492 + TCOD_NOISE_SIMPLEX_GRADIENT_3D(n1,idx,x1,y1,z1);
  493 + n1 *= t1*t1;
  494 + }
  495 + t2 = 0.6f - x2*x2 -y2*y2 -z2*z2;
  496 + if ( t2 < 0.0f ) n2 = 0.0f;
  497 + else {
  498 + int idx = data->map[ (ii + i2 + data->map[ (jj + j2 + data->map[ (kk + k2)& 0xFF ]) &0xFF ])& 0xFF ];
  499 + t2 *= t2;
  500 + TCOD_NOISE_SIMPLEX_GRADIENT_3D(n2,idx,x2,y2,z2);
  501 + n2 *= t2*t2;
  502 + }
  503 + t3 = 0.6f - x3*x3 -y3*y3 -z3*z3;
  504 + if ( t3 < 0.0f ) n3 = 0.0f;
  505 + else {
  506 + int idx = data->map[ (ii + 1 + data->map[ (jj + 1 + data->map[ (kk + 1)& 0xFF ]) &0xFF ])& 0xFF ];
  507 + t3 *= t3;
  508 + TCOD_NOISE_SIMPLEX_GRADIENT_3D(n3,idx,x3,y3,z3);
  509 + n3 *= t3*t3;
  510 + }
  511 + return 32.0f * (n0+n1+n2+n3);
  512 +
  513 + }
  514 + break;
  515 + case 4 :
  516 + {
  517 + #define F4 0.309016994f // (sqrtf(5.0f)-1.0f)/4.0f
  518 + #define G4 0.138196601f // (5.0f - sqrtf(5.0f))/20.0f
  519 + float n0,n1,n2,n3,n4;
  520 + float s = (f[0]+f[1]+f[2]+f[3])*F4 * SIMPLEX_SCALE;
  521 + float xs=f[0]*SIMPLEX_SCALE+s;
  522 + float ys=f[1]*SIMPLEX_SCALE+s;
  523 + float zs=f[2]*SIMPLEX_SCALE+s;
  524 + float ws=f[3]*SIMPLEX_SCALE+s;
  525 + int i=FLOOR(xs);
  526 + int j=FLOOR(ys);
  527 + int k=FLOOR(zs);
  528 + int l=FLOOR(ws);
  529 + float t=(float)(i+j+k+l)*G4;
  530 + float xo = i-t;
  531 + float yo = j-t;
  532 + float zo = k-t;
  533 + float wo = l-t;
  534 + float x0 = f[0]*SIMPLEX_SCALE-xo;
  535 + float y0 = f[1]*SIMPLEX_SCALE-yo;
  536 + float z0 = f[2]*SIMPLEX_SCALE-zo;
  537 + float w0 = f[3]*SIMPLEX_SCALE-wo;
  538 + int c1 = (x0 > y0 ? 32 : 0);
  539 + int c2 = (x0 > z0 ? 16 : 0);
  540 + int c3 = (y0 > z0 ? 8 : 0);
  541 + int c4 = (x0 > w0 ? 4 : 0);
  542 + int c5 = (y0 > w0 ? 2 : 0);
  543 + int c6 = (z0 > w0 ? 1 : 0);
  544 + int c = c1+c2+c3+c4+c5+c6;
  545 +
  546 +
  547 + int i1,j1,k1,l1,i2,j2,k2,l2,i3,j3,k3,l3,ii,jj,kk,ll;
  548 + float x1,y1,z1,w1,x2,y2,z2,w2,x3,y3,z3,w3,x4,y4,z4,w4,t0,t1,t2,t3,t4;
  549 + i1 = simplex[c][0] >= 3 ? 1:0;
  550 + j1 = simplex[c][1] >= 3 ? 1:0;
  551 + k1 = simplex[c][2] >= 3 ? 1:0;
  552 + l1 = simplex[c][3] >= 3 ? 1:0;
  553 +
  554 + i2 = simplex[c][0] >= 2 ? 1:0;
  555 + j2 = simplex[c][1] >= 2 ? 1:0;
  556 + k2 = simplex[c][2] >= 2 ? 1:0;
  557 + l2 = simplex[c][3] >= 2 ? 1:0;
  558 +
  559 + i3 = simplex[c][0] >= 1 ? 1:0;
  560 + j3 = simplex[c][1] >= 1 ? 1:0;
  561 + k3 = simplex[c][2] >= 1 ? 1:0;
  562 + l3 = simplex[c][3] >= 1 ? 1:0;
  563 +
  564 + x1 = x0 -i1 + G4;
  565 + y1 = y0 -j1 + G4;
  566 + z1 = z0 -k1 + G4;
  567 + w1 = w0 -l1 + G4;
  568 + x2 = x0 -i2 + 2.0f*G4;
  569 + y2 = y0 -j2 + 2.0f*G4;
  570 + z2 = z0 -k2 + 2.0f*G4;
  571 + w2 = w0 -l2 + 2.0f*G4;
  572 + x3 = x0 -i3 + 3.0f*G4;
  573 + y3 = y0 -j3 + 3.0f*G4;
  574 + z3 = z0 -k3 + 3.0f*G4;
  575 + w3 = w0 -l3 + 3.0f*G4;
  576 + x4 = x0 - 1.0f +4.0f * G4;
  577 + y4 = y0 - 1.0f +4.0f * G4;
  578 + z4 = z0 - 1.0f +4.0f * G4;
  579 + w4 = w0 - 1.0f +4.0f * G4;
  580 +
  581 + ii = i%256;
  582 + jj = j%256;
  583 + kk = k%256;
  584 + ll = l%256;
  585 +
  586 + t0 = 0.6f - x0*x0 -y0*y0 -z0*z0 -w0*w0;
  587 + if ( t0 < 0.0f ) n0 = 0.0f;
  588 + else {
  589 + int idx = data->map[ (ii + data->map[ (jj + data->map[ (kk + data->map[ ll ] ) &0xFF]) &0xFF ])& 0xFF ];
  590 + t0 *= t0;
  591 + TCOD_NOISE_SIMPLEX_GRADIENT_4D(n0,idx,x0,y0,z0,w0);
  592 + n0 *= t0*t0;
  593 + }
  594 + t1 = 0.6f - x1*x1 -y1*y1 -z1*z1 -w1*w1;
  595 + if ( t1 < 0.0f ) n1 = 0.0f;
  596 + else {
  597 + int idx = data->map[ (ii + i1 + data->map[ (jj + j1 + data->map[ (kk + k1 + data->map[ (ll+l1)&0xFF])& 0xFF ]) &0xFF ])& 0xFF ];
  598 + t1 *= t1;
  599 + TCOD_NOISE_SIMPLEX_GRADIENT_4D(n1,idx,x1,y1,z1,w1);
  600 + n1 *= t1*t1;
  601 + }
  602 + t2 = 0.6f - x2*x2 -y2*y2 -z2*z2 -w2*w2;
  603 + if ( t2 < 0.0f ) n2 = 0.0f;
  604 + else {
  605 + int idx = data->map[ (ii + i2 + data->map[ (jj + j2 + data->map[ (kk + k2 + data->map[(ll+l2)&0xFF])& 0xFF ]) &0xFF ])& 0xFF ];
  606 + t2 *= t2;
  607 + TCOD_NOISE_SIMPLEX_GRADIENT_4D(n2,idx,x2,y2,z2,w2);
  608 + n2 *= t2*t2;
  609 + }
  610 + t3 = 0.6f - x3*x3 -y3*y3 -z3*z3 -w3*w3;
  611 + if ( t3 < 0.0f ) n3 = 0.0f;
  612 + else {
  613 + int idx = data->map[ (ii + i3 + data->map[ (jj + j3 + data->map[ (kk + k3 + data->map[(ll+l3)&0xFF])& 0xFF ]) &0xFF ])& 0xFF ];
  614 + t3 *= t3;
  615 + TCOD_NOISE_SIMPLEX_GRADIENT_4D(n3,idx,x3,y3,z3,w3);
  616 + n3 *= t3*t3;
  617 + }
  618 + t4 = 0.6f - x4*x4 -y4*y4 -z4*z4 -w4*w4;
  619 + if ( t4 < 0.0f ) n4 = 0.0f;
  620 + else {
  621 + int idx = data->map[ (ii + 1 + data->map[ (jj + 1 + data->map[ (kk + 1 + data->map[(ll+1)&0xFF])& 0xFF ]) &0xFF ])& 0xFF ];
  622 + t4 *= t4;
  623 + TCOD_NOISE_SIMPLEX_GRADIENT_4D(n4,idx,x4,y4,z4,w4);
  624 + n4 *= t4*t4;
  625 + }
  626 + return 27.0f * (n0+n1+n2+n3+n4);
  627 +
  628 + }
  629 + break;
  630 + }
  631 + return 0.0f;
  632 +}
  633 +
  634 +// wavelet noise, adapted from Robert L. Cook and Tony Derose 'Wavelet noise' paper
  635 +
  636 +static int absmod(int x, int n) {
  637 + int m=x%n;
  638 + return m < 0 ? m+n : m;
  639 +}
  640 +
  641 +static void TCOD_noise_wavelet_downsample(float *from, float *to, int stride) {
  642 + static float acoeffs[2*WAVELET_ARAD]= {
  643 + 0.000334f, -0.001528f, 0.000410f, 0.003545f, -0.000938f, -0.008233f, 0.002172f, 0.019120f,
  644 + -0.005040f,-0.044412f, 0.011655f, 0.103311f, -0.025936f, -0.243780f, 0.033979f, 0.655340f,
  645 + 0.655340f, 0.033979f,-0.243780f,-0.025936f, 0.103311f, 0.011655f,-0.044412f,-0.005040f,
  646 + 0.019120f, 0.002172f,-0.008233f,-0.000938f, 0.003546f, 0.000410f,-0.001528f, 0.000334f,
  647 + };
  648 + static float *a = &acoeffs[WAVELET_ARAD];
  649 + int i;
  650 + for (i=0; i < WAVELET_TILE_SIZE/2; i++) {
  651 + int k;
  652 + to[i*stride]=0;
  653 + for (k=2*i-WAVELET_ARAD; k <2*i+WAVELET_ARAD; k++) {
  654 + to[i*stride] += a[k-2*i]* from[ absmod(k,WAVELET_TILE_SIZE) * stride ];
  655 + }
  656 + }
  657 +}
  658 +
  659 +static void TCOD_noise_wavelet_upsample(float *from, float *to, int stride) {
  660 + static float pcoeffs[4]= { 0.25f, 0.75f, 0.75f, 0.25f };
  661 + static float *p = &pcoeffs[2];
  662 + int i;
  663 + for (i=0; i < WAVELET_TILE_SIZE; i++) {
  664 + int k;
  665 + to[i*stride]=0;
  666 + for (k=i/2; k <i/2+1; k++) {
  667 + to[i*stride] += p[i-2*k]* from[ absmod(k,WAVELET_TILE_SIZE/2) * stride ];
  668 + }
  669 + }
  670 +}
  671 +
  672 +static void TCOD_noise_wavelet_init(TCOD_noise_t pnoise) {
  673 + perlin_data_t *data=(perlin_data_t *)pnoise;
  674 + int ix,iy,iz,i,sz=WAVELET_TILE_SIZE*WAVELET_TILE_SIZE*WAVELET_TILE_SIZE*sizeof(float);
  675 + float *temp1=(float *)malloc(sz);
  676 + float *temp2=(float *)malloc(sz);
  677 + float *noise=(float *)malloc(sz);
  678 + int offset;
  679 + for (i=0; i < WAVELET_TILE_SIZE*WAVELET_TILE_SIZE*WAVELET_TILE_SIZE; i++ ) {
  680 + noise[i]=genrand_real(-1.0f,1.0f);
  681 + }
  682 + for (iy=0; iy < WAVELET_TILE_SIZE; iy++ ) {
  683 + for (iz=0; iz < WAVELET_TILE_SIZE; iz++ ) {
  684 + i = iy * WAVELET_TILE_SIZE + iz * WAVELET_TILE_SIZE * WAVELET_TILE_SIZE;
  685 + TCOD_noise_wavelet_downsample(&noise[i], &temp1[i], 1);
  686 + TCOD_noise_wavelet_upsample(&temp1[i], &temp2[i], 1);
  687 + }
  688 + }
  689 + for (ix=0; ix < WAVELET_TILE_SIZE; ix++ ) {
  690 + for (iz=0; iz < WAVELET_TILE_SIZE; iz++ ) {
  691 + i = ix + iz * WAVELET_TILE_SIZE * WAVELET_TILE_SIZE;
  692 + TCOD_noise_wavelet_downsample(&temp2[i], &temp1[i], WAVELET_TILE_SIZE);
  693 + TCOD_noise_wavelet_upsample(&temp1[i], &temp2[i], WAVELET_TILE_SIZE);
  694 + }
  695 + }
  696 + for (ix=0; ix < WAVELET_TILE_SIZE; ix++ ) {
  697 + for (iy=0; iy < WAVELET_TILE_SIZE; iy++ ) {
  698 + i = ix + iy * WAVELET_TILE_SIZE;
  699 + TCOD_noise_wavelet_downsample(&temp2[i], &temp1[i], WAVELET_TILE_SIZE * WAVELET_TILE_SIZE);
  700 + TCOD_noise_wavelet_upsample(&temp1[i], &temp2[i], WAVELET_TILE_SIZE * WAVELET_TILE_SIZE);
  701 + }
  702 + }
  703 + for (i=0; i < WAVELET_TILE_SIZE*WAVELET_TILE_SIZE*WAVELET_TILE_SIZE; i++ ) {
  704 + noise[i] -= temp2[i];
  705 + }
  706 + offset = WAVELET_TILE_SIZE/2;
  707 + if ( (offset & 1) == 0 ) offset++;
  708 + for (i=0,ix=0; ix < WAVELET_TILE_SIZE; ix++ ) {
  709 + for (iy=0; iy < WAVELET_TILE_SIZE; iy++ ) {
  710 + for (iz=0; iz < WAVELET_TILE_SIZE; iz++ ) {
  711 + temp1[i++]=noise[ absmod(ix+offset,WAVELET_TILE_SIZE)
  712 + + absmod(iy+offset,WAVELET_TILE_SIZE)*WAVELET_TILE_SIZE
  713 + + absmod(iz+offset,WAVELET_TILE_SIZE)*WAVELET_TILE_SIZE*WAVELET_TILE_SIZE
  714 + ];
  715 + }
  716 + }
  717 + }
  718 + for (i=0; i < WAVELET_TILE_SIZE*WAVELET_TILE_SIZE*WAVELET_TILE_SIZE; i++ ) {
  719 + noise[i] += temp1[i];
  720 + }
  721 + data->waveletTileData=noise;
  722 + free(temp1);
  723 + free(temp2);
  724 +}
  725 +
  726 +float TCOD_noise_wavelet (TCOD_noise_t noise, float *f) {
  727 + perlin_data_t *data=(perlin_data_t *)noise;
  728 + float pf[3];
  729 + int i;
  730 + int p[3],c[3],mid[3],n=WAVELET_TILE_SIZE;
  731 + float w[3][3],t,result=0.0f;
  732 + if ( data->ndim > 3 ) return 0.0f; // not supported
  733 + if (! data->waveletTileData ) TCOD_noise_wavelet_init(noise);
  734 + for (i=0; i < data->ndim; i++ ) pf[i]=f[i]*WAVELET_SCALE;
  735 + for (i=data->ndim; i < 3; i++ ) pf[i]=0.0f;
  736 + for (i=0; i < 3; i++ ) {
  737 + mid[i]=(int)ceilf(pf[i]-0.5f);
  738 + t=mid[i] - (pf[i]-0.5f);
  739 + w[i][0]=t*t*0.5f;
  740 + w[i][2]=(1.0f-t)*(1.0f-t)*0.5f;
  741 + w[i][1]=1.0f - w[i][0]-w[i][2];
  742 + }
  743 + for (p[2]=-1; p[2]<=1; p[2]++) {
  744 + for (p[1]=-1; p[1]<=1; p[1]++) {
  745 + for (p[0]=-1; p[0]<=1; p[0]++) {
  746 + float weight=1.0f;
  747 + for (i=0;i<3;i++) {
  748 + c[i]=absmod(mid[i]+p[i],n);
  749 + weight *= w[i][p[i]+1];
  750 + }
  751 + result += weight * data->waveletTileData[ c[2]*n*n + c[1]*n + c[0] ];
  752 + }
  753 + }
  754 + }
  755 + return CLAMP(-1.0f,1.0f,result);
  756 +}
  757 +
  758 +float TCOD_noise_fbm_wavelet(TCOD_noise_t noise, float *f, float octaves) {
  759 + return TCOD_noise_fbm_int(noise,f,octaves,TCOD_noise_wavelet);
  760 +}
  761 +
  762 +float TCOD_noise_turbulence_wavelet(TCOD_noise_t noise, float *f, float octaves) {
  763 + return TCOD_noise_turbulence_int(noise,f,octaves,TCOD_noise_wavelet);
  764 +}
  765 +
  766 +
  767 +void TCOD_noise_delete(TCOD_noise_t noise) {
  768 + free((perlin_data_t *)noise);
  769 +}
  770 +
  771 +#endif
... ...
... ... @@ -98,6 +98,7 @@ extern int luaopen_mime_core(lua_State *L);
98 98 extern int luaopen_struct(lua_State *L);
99 99 extern int luaopen_profiler(lua_State *L);
100 100 extern int luaopen_lpeg(lua_State *L);
  101 +extern int luaopen_noise(lua_State *L);
101 102
102 103 static const luaL_Reg libs[] = {
103 104 { LUA_LOADLIBNAME, luaopen_package },
... ... @@ -117,6 +118,7 @@ static const luaL_Reg libs[] = {
117 118 { "struct", luaopen_struct },
118 119 { "profiler", luaopen_profiler },
119 120 { "lpeg", luaopen_lpeg },
  121 + { "noise", luaopen_noise },
120 122 { NULL, NULL }
121 123 };
122 124
... ...
... ... @@ -462,6 +462,7 @@ void boot_lua(int state, bool rebooting, int argc, char *argv[])
462 462 luaopen_map(L);
463 463 luaopen_particles(L);
464 464 luaopen_sound(L);
  465 + luaopen_noise(L);
465 466
466 467 // Make the uids repository
467 468 lua_newtable(L);
... ...
  1 +/*
  2 + TE4 - T-Engine 4
  3 + Copyright (C) 2009, 2010 Nicolas Casalini
  4 +
  5 + This program is free software: you can redistribute it and/or modify
  6 + it under the terms of the GNU General Public License as published by
  7 + the Free Software Foundation, either version 3 of the License, or
  8 + (at your option) any later version.
  9 +
  10 + This program is distributed in the hope that it will be useful,
  11 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 + GNU General Public License for more details.
  14 +
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program. If not, see <http://www.gnu.org/licenses/>.
  17 +
  18 + Nicolas Casalini "DarkGod"
  19 + darkgod@te4.org
  20 +*/
  21 +#include "lua.h"
  22 +#include "lauxlib.h"
  23 +#include "lualib.h"
  24 +#include "auxiliar.h"
  25 +#include "types.h"
  26 +#include "script.h"
  27 +#include <math.h>
  28 +#include "libtcod.h"
  29 +#include "noise.h"
  30 +
  31 +typedef struct
  32 +{
  33 + TCOD_noise_t noise;
  34 + int ndim;
  35 +} noise_t;
  36 +
  37 +static int noise_new(lua_State *L)
  38 +{
  39 + int ndim = luaL_checknumber(L, 1);
  40 + float hurst = lua_isnumber(L, 2) ? luaL_checknumber(L, 2) : TCOD_NOISE_DEFAULT_HURST;
  41 + float lacunarity = lua_isnumber(L, 3) ? luaL_checknumber(L, 3) : TCOD_NOISE_DEFAULT_LACUNARITY;
  42 +
  43 + noise_t *n = (noise_t*)lua_newuserdata(L, sizeof(noise_t));
  44 + auxiliar_setclass(L, "noise{core}", -1);
  45 +
  46 + n->noise = TCOD_noise_new(ndim, hurst, lacunarity);
  47 + n->ndim = ndim;
  48 +
  49 + return 1;
  50 +}
  51 +
  52 +static int noise_free(lua_State *L)
  53 +{
  54 + noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
  55 +
  56 + TCOD_noise_delete(n->noise);
  57 +
  58 + lua_pushnumber(L, 1);
  59 + return 1;
  60 +}
  61 +
  62 +static int noise_simplex(lua_State *L)
  63 +{
  64 + noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
  65 + float p[4];
  66 + int i;
  67 + for (i = 0; i < n->ndim; i++) p[i] = luaL_checknumber(L, 2 + i);
  68 +
  69 + lua_pushnumber(L, TCOD_noise_simplex(n->noise, p));
  70 + return 1;
  71 +}
  72 +
  73 +static int noise_fbm_simplex(lua_State *L)
  74 +{
  75 + noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
  76 + float octave = luaL_checknumber(L, 2);
  77 + float p[4];
  78 + int i;
  79 + for (i = 0; i < n->ndim; i++) p[i] = luaL_checknumber(L, 3 + i);
  80 +
  81 + lua_pushnumber(L, TCOD_noise_fbm_simplex(n->noise, p, octave));
  82 + return 1;
  83 +}
  84 +
  85 +static int noise_turbulence_simplex(lua_State *L)
  86 +{
  87 + noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
  88 + float octave = luaL_checknumber(L, 2);
  89 + float p[4];
  90 + int i;
  91 + for (i = 0; i < n->ndim; i++) p[i] = luaL_checknumber(L, 3 + i);
  92 +
  93 + lua_pushnumber(L, TCOD_noise_turbulence_simplex(n->noise, p, octave));
  94 + return 1;
  95 +}
  96 +
  97 +static int noise_perlin(lua_State *L)
  98 +{
  99 + noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
  100 + float p[4];
  101 + int i;
  102 + for (i = 0; i < n->ndim; i++) p[i] = luaL_checknumber(L, 2 + i);
  103 +
  104 + lua_pushnumber(L, TCOD_noise_perlin(n->noise, p));
  105 + return 1;
  106 +}
  107 +
  108 +static int noise_fbm_perlin(lua_State *L)
  109 +{
  110 + noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
  111 + float octave = luaL_checknumber(L, 2);
  112 + float p[4];
  113 + int i;
  114 + for (i = 0; i < n->ndim; i++) p[i] = luaL_checknumber(L, 3 + i);
  115 +
  116 + lua_pushnumber(L, TCOD_noise_fbm_perlin(n->noise, p, octave));
  117 + return 1;
  118 +}