1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/**
* @file
* @brief Colour related functions
**/
/*
--- Colour related functions
module "colour"
*/
#include "AppHdr.h"
#include "l_libs.h"
#include "cluautil.h"
#include "colour.h"
#include "dlua.h"
typedef int (*lua_element_colour_calculator)(int, const coord_def&, lua_datum);
static int _lua_element_colour(int rand, const coord_def& loc,
lua_datum function);
struct lua_element_colour_calc : public element_colour_calc
{
lua_element_colour_calc(element_type _type, string _name,
lua_datum _function)
: element_colour_calc(_type, _name, (element_colour_calculator)_lua_element_colour),
function(_function)
{};
virtual int get(const coord_def& loc = coord_def(),
bool non_random = false);
protected:
lua_datum function;
};
int lua_element_colour_calc::get(const coord_def& loc, bool non_random)
{
// casting function pointers from other function pointers is guaranteed
// to be safe, but calling them on pointers not of their type isn't, so
// assert here to be safe - add to this assert if something different is
// needed
ASSERT((lua_element_colour_calculator)calc == _lua_element_colour);
lua_element_colour_calculator real_calc =
(lua_element_colour_calculator)calc;
return (*real_calc)(rand(non_random), loc, function);
}
static int _lua_element_colour(int rand, const coord_def& loc,
lua_datum function)
{
lua_State *ls = dlua.state();
function.push();
lua_pushinteger(ls, rand);
lua_pushinteger(ls, loc.x);
lua_pushinteger(ls, loc.y);
if (!dlua.callfn(NULL, 3, 1))
{
mprf(MSGCH_WARN, "%s", dlua.error.c_str());
return BLACK;
}
string colour = luaL_checkstring(ls, -1);
lua_pop(ls, 1);
return str_to_colour(colour);
}
/*
--- Define a new elemental colour.
-- See <code>COLOUR:</code> in <tt>docs/develop/levels/syntax.txt</tt> for details.
function add_colour(name, fun)
*/
LUAFN(l_add_colour)
{
const string &name = luaL_checkstring(ls, 1);
if (lua_gettop(ls) != 2 || !lua_isfunction(ls, 2))
luaL_error(ls, "Expected colour generation function.");
int col = str_to_colour(name, -1, false);
if (col == -1)
luaL_error(ls, ("Unknown colour: " + name).c_str());
CLua& vm(CLua::get_vm(ls));
lua_datum function(vm, 2);
add_element_colour(
new lua_element_colour_calc((element_type)col, name, function)
);
return 0;
}
static const struct luaL_reg colour_lib[] =
{
{ "add_colour", l_add_colour },
{ NULL, NULL }
};
void dluaopen_colour(lua_State *ls)
{
luaL_openlib(ls, "colour", colour_lib, 0);
}
|