-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlua_naev.c
257 lines (214 loc) · 5.33 KB
/
nlua_naev.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file nlua_naev.c
*
* @brief Contains Naev generic Lua bindings.
*/
#include "nlua_naev.h"
#include "naev.h"
#include <lauxlib.h>
#include "nlua.h"
#include "nluadef.h"
#include "nlua_evt.h"
#include "nlua_misn.h"
#include "log.h"
#include "nstd.h"
#include "input.h"
#include "land.h"
/* Naev methods. */
static int naev_lang( lua_State *L );
static int naev_keyGet( lua_State *L );
static int naev_keyEnable( lua_State *L );
static int naev_keyEnableAll( lua_State *L );
static int naev_keyDisableAll( lua_State *L );
static int naev_eventStart( lua_State *L );
static int naev_missionStart( lua_State *L );
static const luaL_reg naev_methods[] = {
{ "lang", naev_lang },
{ "keyGet", naev_keyGet },
{ "keyEnable", naev_keyEnable },
{ "keyEnableAll", naev_keyEnableAll },
{ "keyDisableAll", naev_keyDisableAll },
{ "eventStart", naev_eventStart },
{ "missionStart", naev_missionStart },
{0,0}
}; /**< Naev Lua methods. */
/**
* @brief Loads the Naev Lua library.
*
* @param L Lua state.
* @return 0 on success.
*/
int nlua_loadNaev( lua_State *L )
{
luaL_register(L, "naev", naev_methods);
return 0;
}
/**
* @brief Naev generic Lua bindings.
*
* An example would be:
* @code
* if naev.lang() == "en" then
* --Language is English.
* end
* @endcode
*
* @luamod naev
*/
/**
* @brief Gets the language Naev is currently using.
*
* @usage if naev.lang() == "en" then -- Language is english
*
* @luareturn Two character identifier of the language.
* @luafunc lang()
*/
static int naev_lang( lua_State *L )
{
/** @todo multilanguage stuff */
lua_pushstring(L,"en");
return 1;
}
/**
* @brief Gets the keybinding value by name.
*
* @usage bindname = naev.keyGet( "accel" )
*
* @luaparam keyname Name of the keybinding to get value of.
* @luafunc keyGet( keyname )
*/
static int naev_keyGet( lua_State *L )
{
int p;
const char *keyname;
SDLKey key;
KeybindType type;
SDLMod mod;
char buf[128];
/* Get parameters. */
keyname = luaL_checkstring( L, 1 );
/* Get the keybinding. */
key = input_getKeybind( keyname, &type, &mod );
/* Handle type. */
switch (type) {
case KEYBIND_NULL:
lua_pushstring( L, "Not bound" );
break;
case KEYBIND_KEYBOARD:
p = 0;
/* Handle mod. */
if ((mod != NMOD_NONE) && (mod != NMOD_ALL))
p += snprintf( &buf[p], sizeof(buf)-p, "%s + ", input_modToText(mod) );
/* Print key. */
if (nstd_isalpha(key))
p += snprintf( &buf[p], sizeof(buf)-p, "%c", nstd_toupper(key) );
else
p += snprintf( &buf[p], sizeof(buf)-p, "%s", SDL_GetKeyName(key) );
lua_pushstring( L, buf );
break;
case KEYBIND_JBUTTON:
snprintf( buf, sizeof(buf), "joy button %d", key );
lua_pushstring( L, buf );
break;
case KEYBIND_JAXISPOS:
snprintf( buf, sizeof(buf), "joy axis %d-", key );
lua_pushstring( L, buf );
break;
case KEYBIND_JAXISNEG:
snprintf( buf, sizeof(buf), "joy axis %d+", key );
lua_pushstring( L, buf );
break;
}
return 1;
}
/**
* @brief Disables or enables a specific keybinding.
*
* Use with caution, this can make the player get stuck.
*
* @usage naev.keyEnable( "accel", false ) -- Disables the acceleration key
* @luaparam keyname Name of the key to disable (for example "accel").
* @luaparam enable Whether to enable or disable (if omitted disables).
* @luafunc keyEnable( keyname, enable )
*/
static int naev_keyEnable( lua_State *L )
{
const char *key;
int enable;
/* Parameters. */
key = luaL_checkstring(L,1);
enable = lua_toboolean(L,2);
input_toggleEnable( key, enable );
return 0;
}
/**
* @brief Enables all inputs.
*
* @usage naev.keyEnableAll() -- Enables all inputs
* @luafunc keyEnableAll()
*/
static int naev_keyEnableAll( lua_State *L )
{
(void) L;
input_enableAll();
return 0;
}
/**
* @brief Disables all inputs.
*
* @usage naev.keyDisableAll() -- Disables all inputs
* @luafunc keyDisableAll()
*/
static int naev_keyDisableAll( lua_State *L )
{
(void) L;
input_disableAll();
return 0;
}
/**
* @brief Starts an event, does not start check conditions.
*
* @usage naev.eventStart( "Some Event" )
* @luaparam evtname Name of the event to start.
* @luareturn true on success.
* @luafunc eventStart( evtname )
*/
static int naev_eventStart( lua_State *L )
{
int ret;
const char *str;
str = luaL_checkstring(L, 1);
ret = event_start( str, NULL );
/* Get if console. */
lua_getglobal(L, "__cli");
if (lua_toboolean(L,-1) && landed)
bar_regen();
lua_pop(L,1);
lua_pushboolean( L, !ret );
return 1;
}
/**
* @brief Starts a mission, does no check start conditions.
*
* @usage naev.missionStart( "Some Event" )
* @luaparam misnname Name of the mission to start.
* @luareturn true on success.
* @luafunc missionStart( misnname )
*/
static int naev_missionStart( lua_State *L )
{
int ret;
const char *str;
str = luaL_checkstring(L, 1);
ret = mission_start( str, NULL );
/* Get if console. */
lua_getglobal(L, "__cli");
if (lua_toboolean(L,-1) && landed)
bar_regen();
lua_pop(L,1);
lua_pushboolean( L, !ret );
return 1;
}