summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/l_crawl.cc
blob: 01ea5ce8f092bfb428f71a77788132462e70f23e (plain) (blame)
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
#include "AppHdr.h"

#include "dlua.h"
#include "l_libs.h"
#include "initfile.h"
#include "itemname.h"
#include "stuff.h"
#include "view.h"

#ifdef UNIX
#include <sys/time.h>
#include <time.h>
#endif

LUAFN(_crawl_args)
{
    return dlua_stringtable(ls, SysEnv.cmd_args);
}

LUAFN(_crawl_milestone)
{
#ifdef DGL_MILESTONES
    mark_milestone(luaL_checkstring(ls, 1),
                   luaL_checkstring(ls, 2));
#endif
    return (0);
}

LUAFN(_crawl_redraw_view)
{
    viewwindow(true, false);
    return (0);
}

#ifdef UNIX
LUAFN(_crawl_millis)
{
    struct timeval tv;
    struct timezone tz;
    const int error = gettimeofday(&tv, &tz);
    if (error)
        luaL_error(ls, make_stringf("Failed to get time: %s",
                                    strerror(error)).c_str());
    
    lua_pushnumber(ls, tv.tv_sec * 1000 + tv.tv_usec / 1000);
    return (1);
}
#endif

std::string _crawl_make_name (lua_State *ls)
{
    // A quick wrapper around itemname:make_name. Seed is random_int().
    // Possible parameters: all caps, max length, char start. By default
    // these are false, -1, and 0 as per make_name.
    if (lua_gettop(ls) == 0) 
        return make_name (random_int(), false);
    else
    {
        bool all_caps (false);
        int maxlen = -1;
        char start = 0;
        if (lua_gettop(ls) >= 1 && lua_isboolean(ls, 1)) // Want all caps. 
            all_caps = lua_toboolean(ls, 1);
        if (lua_gettop(ls) >= 2 && lua_isnumber(ls, 2))  // Specified a maxlen.
            maxlen = lua_tonumber(ls, 2);
        if (lua_gettop(ls) >= 3 && lua_isstring(ls, 3))  // Specificied a start character
            start = lua_tostring(ls, 3)[0];
        return make_name (random_int(), all_caps, maxlen, start);
    }
}

LUARET1(crawl_make_name, string, _crawl_make_name(ls).c_str())

const struct luaL_reg crawl_lib[] =
{
{ "args", _crawl_args },
{ "mark_milestone", _crawl_milestone },
{ "redraw_view", _crawl_redraw_view },
#ifdef UNIX
{ "millis", _crawl_millis },
#endif
{ "make_name", crawl_make_name },
{ NULL, NULL }
};