-
Notifications
You must be signed in to change notification settings - Fork 7
/
cold_clear_wrapper.c
376 lines (347 loc) · 13.3 KB
/
cold_clear_wrapper.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "coldclear.h"
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
#define DEBUG_CC
#ifdef DEBUG_CC
static void *check_userdata(lua_State *L, int index, const char *tname) {
void *ud = luaL_checkudata(L, index, tname);
luaL_argcheck(L, ud != NULL, index, tname);
return ud;
}
#define lua_tointeger luaL_checkinteger
#define lua_tostring luaL_checkstring
#define lua_tonumber luaL_checknumber
#else /* DEBUG_CC */
static void *check_userdata(lua_State *L, int index, const char *tname) {
return lua_touserdata(L, index);
}
#endif /* DEBUG_CC */
//CCAsyncBot *cc_launch_async(CCOptions *options, CCWeights *weights);
static int launch_async(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
CCWeights *weights = (CCWeights *)check_userdata(L, 2, "CCWeights");
CCAsyncBot *bot = cc_launch_async(options, weights, 0, 0, 0);
CCAsyncBot **botdata = (CCAsyncBot **)lua_newuserdata(L, sizeof(CCAsyncBot *));
/* evil double pointers appear because the cc API gives us a pointer,
and don't provide the chance for lua to allocate memory itself.
See https://www.lua.org/pil/28.1.html#:~:text=If%20for%20some%20reason%20you%20need%20to%20allocate%20memory%20by%20other%20means%2C%20it%20is%20very%20easy%20to%20create%20a%20userdatum%20with%20the%20size%20of%20a%20pointer%20and%20to%20store%20there%20a%20pointer%20to%20the%20real%20memory%20block.%20We%20will%20see%20examples%20of%20this%20technique%20in%20the%20next%20chapter. */
*botdata = bot;
luaL_getmetatable(L, "CCBot");
lua_setmetatable(L, -2);
return 1;
}
//void cc_destroy_async(CCAsyncBot *bot);
static int destroy_async(lua_State *L) {
CCAsyncBot *bot = *(CCAsyncBot **)check_userdata(L, 1, "CCBot");
cc_destroy_async(bot);
return 0;
}
//void cc_reset_async(CCAsyncBot *bot, bool *field, int32_t b2b_gauge,
// uint32_t combo, uint32_t pc_combo, uint32_t lines, int32_t spawn);
static int reset_async(lua_State *L) {
CCAsyncBot *bot = *(CCAsyncBot **)check_userdata(L, 1, "CCBot");
int b2b = lua_tointeger(L, 3);
int combo = lua_tointeger(L, 4);
int pc_combo = lua_tointeger(L, 5);
int lines = lua_tointeger(L, 6);
int spawn = lua_tointeger(L, 7);
bool field[400];
int size = lua_objlen(L, 2);
int i;
for (i = 0; i < 400; ++i) {
field[i] = false;
}
for (i = 1; i <= size && i <= 400; i++) {
lua_rawgeti(L, 2, i);
field[i-1] = lua_toboolean(L, -1);
lua_pop(L, 1);
}
cc_reset_async(bot, field, b2b, combo, pc_combo, lines, spawn);
return 0;
}
//void cc_add_next_piece_async(CCAsyncBot *bot, CCPiece piece);
static int add_next_piece_async(lua_State *L) {
CCAsyncBot *bot = *(CCAsyncBot **)check_userdata(L, 1, "CCBot");
int piece = lua_tointeger(L, 2);
cc_add_next_piece_async(bot, 7 - piece);
return 0;
}
//void cc_request_next_move(CCAsyncBot *bot, uint32_t incoming);
static int request_next_move(lua_State *L) {
CCAsyncBot *bot = *(CCAsyncBot **)check_userdata(L, 1, "CCBot");
if (lua_isnumber(L, 2)) {
cc_request_next_move(bot, lua_tointeger(L, 2));
} else {
cc_request_next_move(bot, 0);
}
return 0;
}
static int return_cc_move(lua_State *L, CCBotPollStatus ret, CCMove *move, CCPlanPlacement *plan, uint32_t *plan_length) {
lua_pushnumber(L, ret); //成功否
if (CC_MOVE_PROVIDED == ret) {
lua_newtable(L);
int k, table1 = lua_gettop(L);
for (k = 0; k < 4; ++k) {
lua_pushnumber(L, k + 1);
lua_newtable(L);
int j, table2 = lua_gettop(L);
lua_pushnumber(L, 1);
lua_pushnumber(L, move->expected_x[k]);
lua_settable(L, table2);
lua_pushnumber(L, 2);
lua_pushnumber(L, move->expected_y[k]);
lua_settable(L, table2);
lua_settable(L, table1);
}
lua_pushboolean(L, move->hold); //hold否
lua_newtable(L);
int i, table = lua_gettop(L);
int len = move->movement_count;
for (i = 0; i < len; i++) {
lua_pushnumber(L, i + 1);
lua_pushnumber(L, move->movements[i]);
lua_settable(L, table);
}
if (plan) {
lua_pushnumber(L, plan->b2b_gauge);
lua_pushnumber(L, plan->attack);
lua_pushnumber(L, plan->extra);
} else {
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
}
lua_pushnumber(L, move->spawn);
} else {
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
}
return 8;
}
//CCBotPollStatus cc_poll_next_move(
// CCAsyncBot *bot,
// CCMove *move,
// CCPlanPlacement* plan,
// uint32_t *plan_length
//);
static int poll_next_move(lua_State *L) {
CCAsyncBot *bot = *(CCAsyncBot **)check_userdata(L, 1, "CCBot");
CCMove move;
CCPlanPlacement plans[1];
uint32_t size = 1;
CCBotPollStatus ret = cc_poll_next_move(bot, &move, plans, &size);
return return_cc_move(L, ret, &move, plans, &size);
}
//CCBotPollStatus cc_block_next_move(
// CCAsyncBot *bot,
// CCMove *move,
// CCPlanPlacement* plan,
// uint32_t *plan_length
//);
static int block_next_move(lua_State *L) {
CCAsyncBot *bot = *(CCAsyncBot **)check_userdata(L, 1, "CCBot");
CCMove move;
CCPlanPlacement plans[1];
uint32_t size = 1;
CCBotPollStatus ret = cc_block_next_move(bot, &move, plans, &size);
return return_cc_move(L, ret, &move, plans, &size);
}
//void cc_default_options(CCOptions *options);
static int default_options(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
cc_default_options(options);
return 0;
}
//void cc_default_weights(CCWeights *weights);
static int default_weights(lua_State *L) {
CCWeights *weights = (CCWeights *)check_userdata(L, 1, "CCWeights");
cc_default_weights(weights);
return 0;
}
//void cc_fast_weights(CCWeights *weights);
static int fast_weights(lua_State *L) {
CCWeights *weights = (CCWeights *)check_userdata(L, 1, "CCWeights");
cc_fast_weights(weights);
return 0;
}
//供lua创建新的默认选项数据
static int get_default_config(lua_State *L) {
CCOptions *options = (CCOptions *)lua_newuserdata(L, sizeof(CCOptions));
luaL_getmetatable(L, "CCOptions");
lua_setmetatable(L, -2);
CCWeights *weights = (CCWeights *)lua_newuserdata(L, sizeof(CCWeights));
luaL_getmetatable(L, "CCWeights");
lua_setmetatable(L, -2);
cc_default_options(options);
options->spawn_rule = CC_ROW_VAR;
cc_default_weights(weights);
return 2;
}
//供lua调用的hold 20g bag7 pcf设置
// options:set_options(hold, 20g, bag7, pcloop)
static int set_options(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
bool hold = lua_toboolean(L, 2);
bool _20g = lua_toboolean(L, 3);
bool bag7 = lua_toboolean(L, 4);
bool pcloop = lua_toboolean(L, 5);
options->use_hold = hold;
options->mode = _20g;
options->speculate = bag7;
options->pcloop = pcloop;
return 0;
}
// options:set_hold(hold)
static int set_hold(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
bool hold = lua_toboolean(L, 2);
options->use_hold = hold;
return 0;
}
// options:set_20g(20g)
static int set_20g(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
bool _20g = lua_toboolean(L, 2);
options->mode = _20g;
return 0;
}
// options:set_bag7(bag7)
static int set_bag7(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
bool bag7 = lua_toboolean(L, 2);
options->speculate = bag7;
return 0;
}
// options:set_pcloop(pcloop)
static int set_pcloop(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
bool pcloop = lua_toboolean(L, 2);
options->pcloop = pcloop;
return 0;
}
// options:set_min_nodes(min_nodes)
static int set_min_nodes(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
int min_nodes = lua_tointeger(L, 2);
options->min_nodes = min_nodes;
return 0;
}
// options:set_max_nodes(max_nodes)
static int set_max_nodes(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
int max_nodes = lua_tointeger(L, 2);
options->max_nodes = max_nodes;
return 0;
}
// options:set_threads(threads)
static int set_threads(lua_State *L) {
CCOptions *options = (CCOptions *)check_userdata(L, 1, "CCOptions");
int threads = lua_tointeger(L, 2);
options->threads = threads;
return 0;
}
// weights:set_weights(weights table)
static int set_weights(lua_State *L) {
CCWeights *weights = (CCWeights *)check_userdata(L, 1, "CCWeights");
while (lua_next(L, 2)) {
const char *key = lua_tostring(L, -2);
int value = lua_tonumber(L, -1);
// clang-format off
if(!strcmp(key,"back_to_back")) weights->back_to_back=value;
else if(!strcmp(key,"bumpiness")) weights->bumpiness=value;
else if(!strcmp(key,"bumpiness_sq")) weights->bumpiness_sq=value;
else if(!strcmp(key,"height")) weights->height=value;
else if(!strcmp(key,"top_half")) weights->top_half=value;
else if(!strcmp(key,"top_quarter")) weights->top_quarter=value;
else if(!strcmp(key,"cavity_cells")) weights->cavity_cells=value;
else if(!strcmp(key,"cavity_cells_sq")) weights->cavity_cells_sq=value;
else if(!strcmp(key,"overhang_cells")) weights->overhang_cells=value;
else if(!strcmp(key,"overhang_cells_sq"))weights->overhang_cells_sq=value;
else if(!strcmp(key,"covered_cells")) weights->covered_cells=value;
else if(!strcmp(key,"covered_cells_sq")) weights->covered_cells_sq=value;
//else if(!strcmp(key,"tslot")) weights->tslot=value;
else if(!strcmp(key,"well_depth")) weights->well_depth=value;
else if(!strcmp(key,"max_well_depth")) weights->max_well_depth=value;
//else if(!strcmp(key,"well_column")) weights->well_column=value;
else if(!strcmp(key,"b2b_clear")) weights->b2b_clear=value;
else if(!strcmp(key,"clear1")) weights->clear1=value;
else if(!strcmp(key,"clear2")) weights->clear2=value;
else if(!strcmp(key,"clear3")) weights->clear3=value;
else if(!strcmp(key,"clear4")) weights->clear4=value;
else if(!strcmp(key,"tspin1")) weights->tspin1=value;
else if(!strcmp(key,"tspin2")) weights->tspin2=value;
else if(!strcmp(key,"tspin3")) weights->tspin3=value;
else if(!strcmp(key,"mini_tspin1")) weights->mini_tspin1=value;
else if(!strcmp(key,"mini_tspin2")) weights->mini_tspin2=value;
else if(!strcmp(key,"perfect_clear")) weights->perfect_clear=value;
else if(!strcmp(key,"combo_garbage")) weights->combo_garbage=value;
else if(!strcmp(key,"move_time")) weights->move_time=value;
else if(!strcmp(key,"wasted_t")) weights->wasted_t=value;
// clang-format on
lua_pop(L, 2);
}
return 0;
}
static int about(lua_State *L) {
lua_pushstring(L, "wrapper by 26F-Studio");
return 1;
}
/* TODO: Revise the naming convention. These strings should really be named as in lua
Changing these is as easy as a Find+Replace, so I'm not deciding for now. */
static const struct luaL_Reg funcList[] = {
{"about", about},
{"launchAsync", launch_async},
{"getDefaultConfig", get_default_config},
{0, 0}};
static const struct luaL_Reg methList[] = {
{"__gc", destroy_async},
{"reset", reset_async},
{"addNext", add_next_piece_async},
{"think", request_next_move},
{"getMove", poll_next_move},
{"blockNextMove", block_next_move},
{0, 0}};
static const struct luaL_Reg optionsList[] = {
{"defaultOptions", default_options},
{"setOptions", set_options},
{"setHold", set_hold},
{"set20G", set_20g},
{"setBag", set_bag7},
{"setPCLoop", set_pcloop},
{"setNode", set_max_nodes},
{0, 0}};
static const struct luaL_Reg weightsList[] = {
{"defaultWeights", default_weights},
{"setWeights", set_weights},
{"fastWeights", fast_weights},
{0, 0}};
int luaopen_CCloader(lua_State *L) {
luaL_newmetatable(L, "CCOptions");
lua_pushstring(L, "__index"); // see comments below
lua_pushvalue(L, -2);
lua_settable(L, -3);
luaL_register(L, 0, optionsList);
luaL_newmetatable(L, "CCWeights");
lua_pushstring(L, "__index"); // see comments below
lua_pushvalue(L, -2);
lua_settable(L, -3);
luaL_register(L, 0, weightsList);
luaL_newmetatable(L, "CCBot");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable again */
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_register(L, 0, methList); /* registers methList into the metatable */
// we don't need to return these metatables, because the corresponding objects
// will automatically have those metatables set.
luaL_register(L, "cc", funcList);
return 1;
}