Apply the new ground_level method.
[crawl.git] / crawl-ref / source / l_colour.cc
blob78955bfeaf527ea4cb10a667667f8d74e7fa0511
1 /*
2 * File: l_colour.cc
3 * Summary: Colour related functions
4 */
6 #include "AppHdr.h"
8 #include "cluautil.h"
9 #include "colour.h"
10 #include "dlua.h"
12 typedef int (*lua_element_colour_calculator)(int, const coord_def&, lua_datum);
14 static int _lua_element_colour(int rand, const coord_def& loc,
15 lua_datum function);
17 struct lua_element_colour_calc : public element_colour_calc
19 lua_element_colour_calc(element_type _type, std::string _name,
20 lua_datum _function)
21 : element_colour_calc(_type, _name, (element_colour_calculator)_lua_element_colour),
22 function(_function)
23 {};
25 virtual int get(const coord_def& loc = coord_def(),
26 bool non_random = false);
28 protected:
29 lua_datum function;
32 int lua_element_colour_calc::get(const coord_def& loc, bool non_random)
34 // casting function pointers from other function pointers is guaranteed
35 // to be safe, but calling them on pointers not of their type isn't, so
36 // assert here to be safe - add to this assert if something different is
37 // needed
38 ASSERT((lua_element_colour_calculator)calc == _lua_element_colour);
39 lua_element_colour_calculator real_calc =
40 (lua_element_colour_calculator)calc;
41 return (*real_calc)(rand(non_random), loc, function);
44 static int next_colour = ETC_FIRST_LUA;
46 static int _lua_element_colour(int rand, const coord_def& loc,
47 lua_datum function)
49 lua_State *ls = dlua.state();
51 function.push();
52 lua_pushinteger(ls, rand);
53 lua_pushinteger(ls, loc.x);
54 lua_pushinteger(ls, loc.y);
55 if (!dlua.callfn(NULL, 3, 1))
57 mpr(dlua.error.c_str(), MSGCH_WARN);
58 return BLACK;
61 std::string colour = luaL_checkstring(ls, -1);
62 lua_pop(ls, 1);
64 return str_to_colour(colour);
67 LUAFN(l_add_colour)
69 const std::string &name = luaL_checkstring(ls, 1);
70 if (lua_gettop(ls) != 2 || !lua_isfunction(ls, 2))
71 luaL_error(ls, "Expected colour generation function.");
73 CLua& vm(CLua::get_vm(ls));
74 lua_datum function(vm, 2);
76 add_element_colour(
77 new lua_element_colour_calc((element_type)(next_colour++),
78 name, function)
81 return 0;
84 static const struct luaL_reg colour_lib[] =
86 { "add_colour", l_add_colour },
88 { NULL, NULL }
91 void dluaopen_colour(lua_State *ls)
93 luaL_openlib(ls, "colour", colour_lib, 0);