3 * Summary: Colour related functions
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
,
17 struct lua_element_colour_calc
: public element_colour_calc
19 lua_element_colour_calc(element_type _type
, std::string _name
,
21 : element_colour_calc(_type
, _name
, (element_colour_calculator
)_lua_element_colour
),
25 virtual int get(const coord_def
& loc
= coord_def(),
26 bool non_random
= false);
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
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
,
49 lua_State
*ls
= dlua
.state();
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
);
61 std::string colour
= luaL_checkstring(ls
, -1);
64 return str_to_colour(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);
77 new lua_element_colour_calc((element_type
)(next_colour
++),
84 static const struct luaL_reg colour_lib
[] =
86 { "add_colour", l_add_colour
},
91 void dluaopen_colour(lua_State
*ls
)
93 luaL_openlib(ls
, "colour", colour_lib
, 0);