-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluautil.c
executable file
·190 lines (172 loc) · 6.41 KB
/
luautil.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
/* luautil.c */
#include <stdlib.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "baize.h"
#include "moon.h"
#include "pile.h"
#include "tableau.h"
#include "luautil.h"
extern lua_State *L;
void OpenLua(struct Baize *const baize)
{
// nb there isn't a luaL_resetstate() so any globals set in one variant end up in the next
L = luaL_newstate();
luaL_openlibs(L);
MoonRegisterFunctions(L);
// create a handle to this Baize inside Lua TODO maybe not needed
lua_pushlightuserdata(L, baize); lua_setglobal(L, "BAIZE");
lua_pushinteger(L, FAN_NONE); lua_setglobal(L, "FAN_NONE");
lua_pushinteger(L, FAN_DOWN); lua_setglobal(L, "FAN_DOWN");
lua_pushinteger(L, FAN_LEFT); lua_setglobal(L, "FAN_LEFT");
lua_pushinteger(L, FAN_RIGHT); lua_setglobal(L, "FAN_RIGHT");
lua_pushinteger(L, FAN_DOWN3); lua_setglobal(L, "FAN_DOWN3");
lua_pushinteger(L, FAN_LEFT3); lua_setglobal(L, "FAN_LEFT3");
lua_pushinteger(L, FAN_RIGHT3); lua_setglobal(L, "FAN_RIGHT3");
lua_pushinteger(L, MOVE_ANY); lua_setglobal(L, "MOVE_ANY");
lua_pushinteger(L, MOVE_ONE); lua_setglobal(L, "MOVE_ONE");
lua_pushinteger(L, MOVE_ONE_PLUS); lua_setglobal(L, "MOVE_ONE_PLUS");
lua_pushinteger(L, MOVE_ONE_OR_ALL); lua_setglobal(L, "MOVE_ONE_OR_ALL");
lua_createtable(L, 0, 0); lua_setglobal(L, "Cell");
lua_createtable(L, 0, 0); lua_setglobal(L, "Discard");
lua_createtable(L, 0, 0); lua_setglobal(L, "Foundation");
lua_createtable(L, 0, 0); lua_setglobal(L, "Label");
lua_createtable(L, 0, 0); lua_setglobal(L, "Reserve");
lua_createtable(L, 0, 0); lua_setglobal(L, "Stock");
lua_createtable(L, 0, 0); lua_setglobal(L, "Tableau");
lua_createtable(L, 0, 0); lua_setglobal(L, "Waste");
}
void CloseLua()
{
if (L) {
lua_close(L);
L = NULL;
}
}
_Bool LuaUtilGetGlobalBool(const char* var, const _Bool def)
{
_Bool result = def;
// fprintf(stderr, "stack %d\n", lua_gettop(L));
int typ = lua_getglobal(L, var); // pushes value onto stack
if ( typ == LUA_TNIL ) {
// fprintf(stderr, "%s is nil\n", var);
result = def;
} else if ( typ != LUA_TBOOLEAN ) {
fprintf(stderr, "ERROR: %s: %s is not a _Boolean\n", __func__, var);
result = def;
} else {
result = lua_toboolean(L, -1); // does not alter stack
}
lua_pop(L, 1); // remove _Boolean from stack
// fprintf(stderr, "%s=%d\n", var, result);
// fprintf(stderr, "stack %d\n", lua_gettop(L));
return result;
}
int LuaUtilGetGlobalInt(const char* var, const int def)
{
int result = def;
// fprintf(stderr, "stack %d\n", lua_gettop(L));
int typ = lua_getglobal(L, var); // pushes value onto stack
if ( typ == LUA_TNIL ) {
// fprintf(stderr, "%s is nil\n", var);
result = def;
} else if ( typ != LUA_TNUMBER ) {
fprintf(stderr, "%s is not a number\n", var);
result = def;
} else {
int isnum;
result = (int)lua_tointegerx(L, -1, &isnum); // does not alter stack
if ( !isnum ) {
fprintf(stderr, "ERROR: %s: %s cannot be converted to an integer\n", __func__, var);
result = def;
}
}
lua_pop(L, 1); // remove integer from stack
// fprintf(stderr, "%s=%d\n", var, result);
// fprintf(stderr, "stack %d\n", lua_gettop(L));
return result;
}
float LuaUtilGetGlobalFloat(const char* var, const float def)
{
float result = def;
// fprintf(stderr, "stack %d\n", lua_gettop(L));
int typ = lua_getglobal(L, var); // pushes value onto stack
if ( typ == LUA_TNIL ) {
// fprintf(stderr, "%s is nil\n", var);
result = def;
} else if ( typ != LUA_TNUMBER ) {
fprintf(stderr, "%s is not a number\n", var);
result = def;
} else {
int isnum;
result = (float)lua_tonumberx(L, -1, &isnum); // does not alter stack
if ( !isnum ) {
fprintf(stderr, "ERROR: %s: %s cannot be converted to a number\n", __func__, var);
result = def;
}
}
lua_pop(L, 1); // remove integer from stack
// fprintf(stderr, "%s=%f\n", var, result);
// fprintf(stderr, "stack %d\n", lua_gettop(L));
return result;
}
const char* LuaUtilGetGlobalString(const char* var, const char* def)
{
const char* result = def;
// fprintf(stderr, "stack %d\n", lua_gettop(L));
int typ = lua_getglobal(L, var); // pushes value onto stack
if ( typ != LUA_TSTRING ) {
fprintf(stderr, "%s is not a string\n", var);
result = def;
} else {
result = lua_tostring(L, -1); // does not alter stack
if ( !result ) {
fprintf(stderr, "ERROR: %s: %s cannot be converted to a string\n", __func__, var);
result = def;
}
}
lua_pop(L, 1); // remove integer from stack
// fprintf(stderr, "%s=%s\n", var, result);
// fprintf(stderr, "stack %d\n", lua_gettop(L));
return result;
}
float LuaUtilGetFieldFloat(const char* key, const float def)
{
// assumes table is on top of stack
float result = def;
int isnum;
int typ = lua_getfield(L, -1, key); // pushes onto the stack the value t[k], where t is the value at the given index
if ( typ == LUA_TNIL ) {
// fprintf(stderr, "%s is nil\n", key);
result = def;
} else if ( typ != LUA_TNUMBER ) {
fprintf(stderr, "ERROR: %s: %s is not a number\n", __func__, key);
result = def;
} else {
result = (float)lua_tonumberx(L, -1, &isnum); // returns a lua_Number
if ( !isnum ) {
fprintf(stderr, "ERROR: %s: %s cannot be converted to a number\n", __func__, key);
result = def;
}
}
lua_pop(L, 1); // remove number
return result;
}
_Bool LuaUtilSetupTableMethod(const char *table, const char *method)
{
int typ = lua_getglobal(L, table);
if (typ != LUA_TTABLE) {
fprintf(stderr, "INFO: %s: %s is not a table\n", __func__, table);
lua_pop(L, 1); // remove table name
return 0;
}
typ = lua_getfield(L, -1, method);
if (typ != LUA_TFUNCTION) {
fprintf(stderr, "INFO: %s: %s.%s is not a function\n", __func__, table, method);
lua_pop(L, 2); // remove table and method names
return 0;
}
return 1;
}