-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaImplementation.h
More file actions
322 lines (247 loc) · 7.69 KB
/
LuaImplementation.h
File metadata and controls
322 lines (247 loc) · 7.69 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
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
#pragma once
/*
lua integration for jfk mario world
*/
// Lua is written in C, so compiler needs to know how to link its libraries
bool lua_loaded = false;
extern "C" {
#include "lua.hpp"
}
lua_State* LUA_STATE;
void lua_print(string text)
{
cout << lua_color << "[Lua] " << text << white << endl;
}
void load_lua_libs(lua_State* L)
{
luaL_openlibs(L);
}
//lua functions to bind to jfk mario world.
static int lua_write(lua_State* L) {
string str = (string)lua_tostring(L, 1); // get function argument
//cout << "Print called" << endl;
lua_print(str);
return 0; // nothing to return!
}
static int lua_write_ram(lua_State* L) {
uint_fast32_t n = (uint_fast32_t)lua_tonumber(L, 1);
uint_fast32_t p = (uint_fast16_t)lua_tointeger(L, 2);
uint_fast8_t s = (uint_fast8_t)lua_tointeger(L, 3);
ASM.Write_To_Ram(p, n, s);
return 0; // nothing to return!
}
extern "C" int lua_get_ram(lua_State* L)
{
uint_fast32_t p = (uint_fast16_t)lua_tointeger(L, 1); // First argument
uint_fast8_t s = (uint_fast8_t)lua_tointeger(L, 2); // First argument
double result = double(int_fast32_t(ASM.Get_Ram(p, s)));
lua_pushnumber(L, result);
//lua_pushinteger(L, result);
return 1; // Count of returned values
}
static int lua_spawn_sprite(lua_State* L) {
uint_fast8_t n = (uint_fast8_t)lua_tointeger(L, 1); // First argument
uint_fast8_t s = (uint_fast8_t)lua_tointeger(L, 2); // First argument
uint_fast16_t x = (uint_fast16_t)lua_tointeger(L, 3); // First argument
uint_fast16_t y = (uint_fast16_t)lua_tointeger(L, 4); // First argument
uint_fast8_t direction = (uint_fast8_t)lua_tointeger(L, 5); // First argument
bool is_l = lua_toboolean(L, 6);
lua_pushnumber(L, spawnSpriteJFKMarioWorld(n, s, x, y, direction, is_l));
return 1;
}
static int draw_to_oam(lua_State* L)
{
uint_fast8_t sprite_index = (uint_fast8_t)lua_tonumber(L, 1);
uint_fast8_t tile = (uint_fast8_t)lua_tonumber(L, 2);
uint_fast8_t size = (uint_fast8_t)lua_tonumber(L, 3);
uint_fast8_t angle = (uint_fast8_t)lua_tonumber(L, 4);
int offset_x = (int)lua_tonumber(L, 5);
int offset_y = (int)lua_tonumber(L, 6);
uint_fast8_t pal = (uint_fast8_t)lua_tonumber(L, 7);
if (!(RAM[0x2A80 + sprite_index] & 2))
{
return 0;
}
uint_fast16_t oam_index = 0;
while (oam_index < 0x400)
{
if (RAM[0x200 + oam_index] == 0 && RAM[0x206 + oam_index] == 0) //Founmd a empty OAM slot
{
break;
}
oam_index += 8;
}
RAM[0x200 + oam_index] = tile;
RAM[0x201 + oam_index] = size;
uint_fast16_t sprite_x_position = uint_fast16_t(int(offset_x + RAM[0x2100 + sprite_index] + int_fast8_t(RAM[0x2180 + sprite_index]) * 256));
uint_fast16_t sprite_y_position = uint_fast16_t(int(offset_y + RAM[0x2280 + sprite_index] + int_fast8_t(RAM[0x2300 + sprite_index]) * 256));
RAM[0x202 + oam_index] = sprite_x_position;
RAM[0x203 + oam_index] = sprite_x_position >> 8;
RAM[0x204 + oam_index] = sprite_y_position;
RAM[0x205 + oam_index] = sprite_y_position >> 8;
RAM[0x206 + oam_index] = pal;
RAM[0x207 + oam_index] = angle;
return 0;
}
static int draw_to_oam_direct(lua_State* L)
{
uint_fast8_t tile = (uint_fast8_t)lua_tonumber(L, 1);
uint_fast8_t size = (uint_fast8_t)lua_tonumber(L, 2);
uint_fast8_t angle = (uint_fast8_t)lua_tonumber(L, 3);
uint_fast16_t sprite_x_position = (uint_fast16_t)lua_tonumber(L, 4);
uint_fast16_t sprite_y_position = (uint_fast16_t)lua_tonumber(L, 5);
uint_fast8_t pal = (uint_fast8_t)lua_tonumber(L, 6);
uint_fast16_t oam_index = 0;
while (oam_index < 0x400)
{
if (RAM[0x200 + oam_index] == 0 && RAM[0x206 + oam_index] == 0) //Founmd a empty OAM slot
{
break;
}
oam_index += 8;
}
RAM[0x200 + oam_index] = tile;
RAM[0x201 + oam_index] = size;
RAM[0x202 + oam_index] = sprite_x_position;
RAM[0x203 + oam_index] = sprite_x_position >> 8;
RAM[0x204 + oam_index] = sprite_y_position;
RAM[0x205 + oam_index] = sprite_y_position >> 8;
RAM[0x206 + oam_index] = pal;
RAM[0x207 + oam_index] = angle;
return 0;
}
static int lua_bitand(lua_State* L) {
uint_fast8_t a = (uint_fast8_t)lua_tointeger(L, 1);
uint_fast8_t b = (uint_fast8_t)lua_tointeger(L, 2);
uint_fast8_t result = 0;
uint_fast8_t bitval = 1;
while (a > 0 && b > 0)
{
if ((a & 1) == 1 && (b & 1) == 1)
{
result = result + bitval;
}
bitval = bitval << 1;
a = a >> 1;
b = b >> 1;
}
lua_pushnumber(L, result);
return 1;
}
static int lua_bitand_new(lua_State* L) {
uint_fast8_t a = (uint_fast8_t)lua_tointeger(L, 1);
uint_fast8_t b = (uint_fast8_t)lua_tointeger(L, 2);
uint_fast8_t result = a & b;
lua_pushnumber(L, result);
return 1;
}
static int drawtohud(lua_State* L)
{
uint_fast8_t tile = (uint_fast8_t)lua_tonumber(L, 1) & 0x7F;
uint_fast8_t prop = (uint_fast8_t)lua_tonumber(L, 2) & 0x0F;
uint_fast8_t x = (uint_fast8_t)lua_tonumber(L, 3) & 0x1F;
uint_fast8_t y = (uint_fast8_t)lua_tonumber(L, 4) & 0x1F;
RAM[0x1B800 + ((x % 32) + (y * 32))*2] = tile;
RAM[0x1B801 + ((x % 32) + (y * 32))*2] = prop;
return 0;
}
static int killPlayer(lua_State* L)
{
int plr = (int)lua_tointeger(L, 1);
death_timer[plr - 1] = 16;
return 0;
}
static int damagePlayer(lua_State* L)
{
int plr = (int)lua_tointeger(L, 1);
death_timer[plr - 1] = 0x80 + 16;
return 0;
}
extern "C" {
int getPlayerX(lua_State* L)
{
int plr = (int)lua_tointeger(L, 1); plr--;
int result = RAM[0x5000 + plr] + RAM[0x5100 + plr] * 256;
lua_pushnumber(L, result);
return 1;
}
int getPlayerY(lua_State* L)
{
int plr = (int)lua_tointeger(L, 1); plr--;
int result = RAM[0x5200 + plr] + RAM[0x5300 + plr] * 256;
lua_pushnumber(L, result);
return 1;
}
int lua_checkbit(lua_State* L)
{
uint_fast32_t p = (uint_fast16_t)lua_tointeger(L, 1);
uint_fast8_t s = (uint_fast8_t)lua_tointeger(L, 2);
bool bit = ((RAM[p] >> s) & 1);
//cout << "Bit " << int(s) << " of 0x" << hex << int(p) << " = " << int(bit) << endl;
lua_pushboolean(L, bit);
return 1;
}
}
/* functions end */
void lua_connect_functions(lua_State* L)
{
//lua_print("Connected functions to 0x" + int_to_;
lua_pushcfunction(L, lua_write); lua_setglobal(L, "marioPrint");
lua_pushcfunction(L, lua_write_ram); lua_setglobal(L, "asmWrite");
lua_pushcfunction(L, lua_spawn_sprite); lua_setglobal(L, "spawnSprite");
lua_pushcfunction(L, draw_to_oam); lua_setglobal(L, "drawOam");
lua_pushcfunction(L, draw_to_oam_direct); lua_setglobal(L, "drawOamDirect");
lua_pushcfunction(L, lua_bitand); lua_setglobal(L, "oldBitand");
lua_pushcfunction(L, lua_bitand_new); lua_setglobal(L, "bitand");
lua_pushcfunction(L, drawtohud); lua_setglobal(L, "drawToHud");
lua_pushcfunction(L, killPlayer); lua_setglobal(L, "killPlayer");
lua_pushcfunction(L, damagePlayer); lua_setglobal(L, "damagePlayer");
lua_register(L, "asmRead", lua_get_ram);
lua_register(L, "getPlayerX", getPlayerX);
lua_register(L, "getPlayerY", getPlayerY);
lua_register(L, "asmCheckBit", lua_checkbit);
}
/*
LUA General
*/
string last_lua_file;
void lua_loadfile(string file)
{
if (LUA_STATE)
{
lua_close(LUA_STATE);
}
last_lua_file = file;
LUA_STATE = luaL_newstate();
load_lua_libs(LUA_STATE);
int ret = luaL_dofile(LUA_STATE, (path + file).c_str());
if (ret != 0)
{
lua_print("Error occurred when calling luaL_dofile()");
lua_print("Error: " + string(lua_tostring(LUA_STATE, -1)));
lua_close(LUA_STATE);
return;
}
//main connectors back to jfk mario world.
lua_connect_functions(LUA_STATE);
lua_print("loaded " + path + file);
}
void lua_run_init()
{
lua_getglobal(LUA_STATE, "Init");
lua_pcall(LUA_STATE, 0, 0, 0); // run script
}
void lua_run_main()
{
lua_getglobal(LUA_STATE, "Main");
lua_pcall(LUA_STATE, 0, 0, 0); // run script
}
void lua_on_chatted(string message, int plr = 0)
{
if (lua_getglobal(LUA_STATE, "OnChatted"))
{
lua_pushstring(LUA_STATE, message.c_str());
lua_pushinteger(LUA_STATE, plr);
lua_pcall(LUA_STATE, 2, 0, 0);
}
}