-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmoongame.c
More file actions
executable file
·240 lines (211 loc) · 7.13 KB
/
moongame.c
File metadata and controls
executable file
·240 lines (211 loc) · 7.13 KB
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
/* moongame.c */
#include "script.h"
#include "trace.h"
#include "luautil.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
extern lua_State *L;
static void BuildPiles(struct Baize *const baize)
{
(void)baize;
if (lua_getglobal(L, "BuildPiles") != LUA_TFUNCTION) { // push value of "BuildPiles" onto the stack, return type
CSOL_ERROR("%s", "BuildPiles is not a function");
lua_pop(L, 1); // remove "BuildPiles" from stack
} else {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
CSOL_ERROR("running Lua function: %s", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
// fprintf(stderr, "BuildPiles called ok\n");
}
}
}
static void StartGame(struct Baize *const baize)
{
(void)baize;
if (lua_getglobal(L, "StartGame") != LUA_TFUNCTION) { // push Lua function name onto the stack
CSOL_INFO("%s", "StartGame is not a function");
lua_pop(L, 1); // remove function name
} else {
// no args, no returns
if ( lua_pcall(L, 0, 0, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
// nothing
}
}
}
static void AfterMove(struct Baize *const baize)
{
(void)baize;
if (lua_getglobal(L, "AfterMove") != LUA_TFUNCTION) { // push Lua function name onto the stack
// CSOL_INFO("%s", "AfterMove is not a function");
lua_pop(L, 1); // remove func from stack
} else {
// no args, no returns
if ( lua_pcall(L, 0, 0, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
// nothing
}
}
}
static const char* TailMoveError(struct Array *const tail)
{
struct Card *const c0 = ArrayGet(tail, 0);
struct Pile *const pile = CardOwner(c0);
const char *result = (void*)0;
if (LuaUtilSetupTableMethod(pile->category, "TailMoveError")) {
lua_pushlightuserdata(L, tail);
// one arg (tail), one return (error string)
if ( lua_pcall(L, 1, 1, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
if (lua_isstring(L, -1)) {
result = lua_tostring(L, -1);
} else if (lua_isnil(L, -1)) {
;
} else {
CSOL_ERROR("%s", "unexpected return");
}
lua_pop(L, 1);
}
}
return result;
}
static const char* TailAppendError(struct Pile *const pile, struct Array *const tail)
{
const char *result = (void*)0;
if (LuaUtilSetupTableMethod(pile->category, "TailAppendError")) {
lua_pushlightuserdata(L, pile);
lua_pushlightuserdata(L, tail);
// two args (pile, tail), one return (error string)
if ( lua_pcall(L, 2, 1, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
if (lua_isstring(L, -1)) {
result = lua_tostring(L, -1);
} else if (lua_isnil(L, -1)) {
;
} else {
CSOL_ERROR("%s", "unexpected return");
}
lua_pop(L, 1);
}
}
return result;
}
static int PileUnsortedPairs(struct Pile *const pile)
{
int unsorted = 0;
if (LuaUtilSetupTableMethod(pile->category, "UnsortedPairs")) {
lua_pushlightuserdata(L, pile);
// one arg (pile), one return (integer)
if ( lua_pcall(L, 1, 1, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
if (lua_isinteger(L, -1)) {
unsorted = lua_tointeger(L, -1);
} else {
CSOL_ERROR("%s", "expecting an integer");
}
lua_pop(L, 1);
}
}
return unsorted;
}
static void TailTapped(struct Array *const tail)
{
struct Card* c0 = ArrayGet(tail, 0);
struct Pile* pile = CardOwner(c0);
if (!LuaUtilSetupTableMethod(pile->category, "TailTapped")) {
pile->vtable->TailTapped(tail);
} else {
// push one arg, the tail
lua_pushlightuserdata(L, tail);
// one arg (tail), no return (function can Toast itself for all I care)
if ( lua_pcall(L, 1, 0, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
}
static void PileTapped(struct Pile *const pile)
{
if (LuaUtilSetupTableMethod(pile->category, "PileTapped")) {
// push one arg, the (non-existant) tail
lua_pushnil(L);
// one arg (nil tail), no return (function can Toast itself for all I care)
if ( lua_pcall(L, 1, 0, 0) != LUA_OK ) {
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
}
static int PercentComplete(struct Baize *const baize)
{
int percent = 0;
// let Lua have first dibs at this by calling function PercentComplete()
if (lua_getglobal(L, "PercentComplete") == LUA_TFUNCTION) { // push Lua function name onto the stack
if ( lua_pcall(L, 0, 1, 0) != LUA_OK ) { // no args, one return
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
if (lua_isinteger(L, -1)) {
percent = lua_tointeger(L, -1);
} else {
CSOL_ERROR("%s", "integer expected");
lua_pop(L, 1);
}
}
} else {
lua_pop(L, 1); // remove "PercentComplete"
int pairs = 0, unsorted = 0;
size_t index;
for ( struct Pile *pile = ArrayFirst(baize->piles, &index); pile; pile = ArrayNext(baize->piles, &index) ) {
if (PileLen(pile) > 1) {
pairs += PileLen(pile) - 1;
}
unsorted += pile->vtable->UnsortedPairs(pile);
}
// CSOL_INFO("pairs:%d unsorted:%d", pairs, unsorted);
percent = (int)(100.0f - UtilMapValue((float)unsorted, 0, (float)pairs, 0.0f, 100.0f));
}
return percent;
}
static const char* Wikipedia(void)
{
const char *str = (void*)0;
if (lua_getglobal(L, "Wikipedia") == LUA_TFUNCTION) { // push Lua function name onto the stack
if ( lua_pcall(L, 0, 1, 0) != LUA_OK ) { // no args, one return
CSOL_ERROR("running Lua function: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // remove error
} else {
str = lua_tostring(L, -1);
}
}
return str;
}
static struct ScriptInterface moonGameVtable = {
&BuildPiles,
&StartGame,
&AfterMove,
&TailMoveError,
&TailAppendError,
&PileUnsortedPairs,
&TailTapped,
&PileTapped,
&PercentComplete,
&Wikipedia,
};
// get the interface to run a variant through a Lua script
struct ScriptInterface* GetMoonGameInterface(void)
{
return &moonGameVtable;
}