diff options
author | Pete Hurst <pete@streamuniverse.tv> | 2013-03-23 19:11:50 +0000 |
---|---|---|
committer | Pete Hurst <pete@streamuniverse.tv> | 2013-03-23 21:34:33 +0000 |
commit | 20198774c381a9b977a34f09c5181b44cba90d60 (patch) | |
tree | 1747fc273a5c919442a1a93e276ff1ddc606423f /crawl-ref/source/l_crawl.cc | |
parent | 21f6a187dc797ac944df9061f1802c794427ce22 (diff) | |
download | crawl-ref-20198774c381a9b977a34f09c5181b44cba90d60.tar.gz crawl-ref-20198774c381a9b977a34f09c5181b44cba90d60.zip |
Add Lua exports for Simplex and Worley noise, and random_real
Lua API:
- crawl.random_real()
- crawl.worley(x,y,z) -> distance[0],distance[1],id[0],id[1],pos[0].x,pos[0].y,pos[0].z,pos[1].x,pos[1].y,pos[1].z
- crawl.simplex(x,y,z,k) - z and k are optional if you only want 2D or 3D noise
Diffstat (limited to 'crawl-ref/source/l_crawl.cc')
-rw-r--r-- | crawl-ref/source/l_crawl.cc | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/crawl-ref/source/l_crawl.cc b/crawl-ref/source/l_crawl.cc index 0a87710ecd..7412bb98a6 100644 --- a/crawl-ref/source/l_crawl.cc +++ b/crawl-ref/source/l_crawl.cc @@ -33,6 +33,7 @@ module "crawl" #include "options.h" #include "ouch.h" #include "output.h" +#include "perlin.h" #include "player.h" #include "random.h" #include "religion.h" @@ -40,6 +41,7 @@ module "crawl" #include "stuff.h" #include "tutorial.h" #include "view.h" +#include "worley.h" #ifdef UNIX #include <sys/time.h> @@ -670,6 +672,74 @@ LUARET1(crawl_x_chance_in_y, boolean, x_chance_in_y(luaL_checkint(ls, 1), luaL_checkint(ls, 2))) LUARET1(crawl_div_rand_round, number, div_rand_round(luaL_checkint(ls, 1), luaL_checkint(ls, 2))) +LUARET1(crawl_random_real, number, random_real()) + +static int crawl_worley(lua_State *ls) +{ + double px = lua_tonumber(ls,1); + double py = lua_tonumber(ls,2); + double pz = lua_tonumber(ls,3); + + worley::noise_datum n = worley::noise(px,py,pz); + lua_pushnumber(ls, n.distance[0]); + lua_pushnumber(ls, n.distance[1]); + lua_pushnumber(ls, n.id[0]); + lua_pushnumber(ls, n.id[1]); + lua_pushnumber(ls, n.pos[0][0]); + lua_pushnumber(ls, n.pos[0][1]); + lua_pushnumber(ls, n.pos[0][2]); + lua_pushnumber(ls, n.pos[1][0]); + lua_pushnumber(ls, n.pos[1][1]); + lua_pushnumber(ls, n.pos[1][2]); + return 10; +} + +// Supports 2D-4D Simplex noise. The first two parameters are required +// for 2D noise, the next two are optional for 3D or 4D. +// TODO: Could support octaves here but maybe it can be handled more +// flexibly in lua +static int crawl_simplex(lua_State *ls) +{ + int dims = 0; + double vals[4]; + if (lua_isnumber(ls,1)) + { + vals[dims] = lua_tonumber(ls,1); + dims++; + } + if (lua_isnumber(ls,2)) + { + vals[dims] = lua_tonumber(ls,2); + dims++; + } + if (lua_isnumber(ls,3)) + { + vals[dims] = lua_tonumber(ls,3); + dims++; + } + if (lua_isnumber(ls,4)) + { + vals[dims] = lua_tonumber(ls,4); + dims++; + } + + double result; + if (dims < 2) return 0; // TODO: Throw error? + switch (dims) { + case 2: + result = perlin::noise(vals[0],vals[1]); + break; + case 3: + result = perlin::noise(vals[0],vals[1],vals[2]); + break; + case 4: + result = perlin::noise(vals[0],vals[1],vals[2],vals[3]); + break; + } + lua_pushnumber(ls, result); + + return 1; +} static int crawl_is_tiles(lua_State *ls) { @@ -904,6 +974,9 @@ static const struct luaL_reg crawl_clib[] = { "random_range", crawl_random_range }, { "random_element", crawl_random_element }, { "div_rand_round", crawl_div_rand_round }, + { "random_real", crawl_random_real }, + { "worley", crawl_worley }, + { "simplex", crawl_simplex }, { "redraw_screen", crawl_redraw_screen }, { "c_input_line", crawl_c_input_line}, { "getch", crawl_getch }, |