lua.d 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. // quantumde1 developed software, licensed under BSD-0-Clause license.
  2. module scripts.lua;
  3. import bindbc.lua;
  4. import raylib;
  5. import variables;
  6. import graphics.effects;
  7. import std.conv;
  8. import system.abstraction;
  9. import system.config;
  10. import std.string;
  11. import std.algorithm;
  12. import graphics.engine;
  13. import graphics.playback;
  14. import std.file;
  15. /*
  16. * This module provides Lua bindings for various engine functionalities.
  17. * Functions are built on top of engine built-in functions for execution from scripts.
  18. * Not all engine functions usable for scripting are yet implemented.
  19. */
  20. /* text window */
  21. extern (C) nothrow int luaL_dialogBox(lua_State* L)
  22. {
  23. showDialog = true;
  24. // parse table with text pages
  25. luaL_checktype(L, 1, LUA_TTABLE);
  26. int textTableLength = cast(int) lua_objlen(L, 1);
  27. messageGlobal = new string[](textTableLength);
  28. foreach (i; 0..textTableLength) {
  29. lua_rawgeti(L, 1, i + 1);
  30. messageGlobal[i] = luaL_checkstring(L, -1).to!string;
  31. lua_pop(L, 1);
  32. }
  33. //parse table with choices
  34. luaL_checktype(L, 2, LUA_TTABLE);
  35. int choicesLength = cast(int) lua_objlen(L, 2);
  36. choices = new string[choicesLength];
  37. foreach (i; 0..choicesLength)
  38. {
  39. lua_rawgeti(L, 2, i + 1);
  40. choices[i] = luaL_checkstring(L, -1).to!string;
  41. lua_pop(L, 1);
  42. }
  43. //if provided, get page on which choices must be shown
  44. if (lua_gettop(L) >= 3) {
  45. choicePage = cast(int)luaL_checkinteger(L, 3);
  46. }
  47. if (lua_gettop(L) >= 4 && !lua_istable(L, 4)) {
  48. typingSpeed = cast(float) luaL_checknumber(L, 4);
  49. }
  50. return 0;
  51. }
  52. extern (C) nothrow int luaL_getAnswerValue(lua_State* L)
  53. {
  54. lua_pushinteger(L, selectedChoice);
  55. return 1;
  56. }
  57. extern (C) nothrow int luaL_isDialogExecuted(lua_State *L) {
  58. lua_pushboolean(L, showDialog);
  59. return 1;
  60. }
  61. /* background drawing and loading */
  62. extern (C) nothrow int luaL_loadBackground(lua_State* L)
  63. {
  64. try
  65. {
  66. int count = cast(int)luaL_checkinteger(L, 2);
  67. if (count >= backgroundTextures.length) {
  68. backgroundTextures.length = count + 1;
  69. }
  70. if (backgroundTextures[count].texture.id != 0) {
  71. UnloadTexture(backgroundTextures[count].texture);
  72. }
  73. char* filename = cast(char*)luaL_checkstring(L, 1);
  74. backgroundTextures[count].texture = LoadTexture(filename);
  75. SetTextureFilter(backgroundTextures[count].texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  76. }
  77. catch (Exception e)
  78. {
  79. debugWriteln(e.msg);
  80. }
  81. return 0;
  82. }
  83. extern (C) nothrow int luaL_drawBackground(lua_State* L)
  84. {
  85. try
  86. {
  87. int count = cast(int)luaL_checkinteger(L, 4);
  88. debugWriteln(backgroundTextures[count]);
  89. backgroundTextures[count].height = backgroundTextures[count].texture.height;
  90. if (backgroundTextures.length < count) {
  91. backgroundTextures.length = backgroundTextures.length;
  92. }
  93. backgroundTextures[count].width = backgroundTextures[count].texture.width;
  94. backgroundTextures[count].x = luaL_checknumber(L, 1);
  95. backgroundTextures[count].y = luaL_checknumber(L, 2);
  96. backgroundTextures[count].scale = luaL_checknumber(L, 3) * scale;
  97. backgroundTextures[count].drawTexture = true;
  98. debugWriteln(backgroundTextures[count]);
  99. }
  100. catch (Exception e)
  101. {
  102. debugWriteln(e.msg);
  103. }
  104. return 0;
  105. }
  106. extern (C) nothrow int luaL_stopDrawBackground(lua_State* L)
  107. {
  108. try
  109. {
  110. int count = cast(int)luaL_checkinteger(L, 1);
  111. if (count >= backgroundTextures.length) {
  112. debugWriteln("stop draw not loaded background unavailable");
  113. } else {
  114. backgroundTextures[count].drawTexture = false;
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. debugWriteln(e.msg);
  120. }
  121. return 0;
  122. }
  123. extern (C) nothrow int luaL_unloadBackground(lua_State* L)
  124. {
  125. int count = cast(int)luaL_checkinteger(L, 1);
  126. if (count >= backgroundTextures.length) {
  127. debugWriteln("Unloading non-loaded background unavailable");
  128. } else {
  129. backgroundTextures[count].drawTexture = false;
  130. UnloadTexture(backgroundTextures[count].texture);
  131. }
  132. return 0;
  133. }
  134. /* character textures */
  135. extern (C) nothrow int luaL_loadCharacter(lua_State *L) {
  136. try
  137. {
  138. int count = cast(int) luaL_checkinteger(L, 2);
  139. if (count >= characterTextures.length) {
  140. characterTextures.length = count + 1;
  141. }
  142. if (characterTextures.length < characterTextures.length) {
  143. characterTextures.length = characterTextures.length;
  144. }
  145. if (count < characterTextures.length && characterTextures[count].texture.id != 0) {
  146. UnloadTexture(characterTextures[count].texture);
  147. }
  148. characterTextures[count].texture = LoadTexture(luaL_checkstring(L, 1));
  149. SetTextureFilter(characterTextures[count].texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  150. characterTextures[count].width = characterTextures[count].texture.width;
  151. characterTextures[count].height = characterTextures[count].texture.height;
  152. characterTextures[count].drawTexture = false;
  153. }
  154. catch (Exception e) {
  155. debugWriteln(e.msg);
  156. }
  157. return 0;
  158. }
  159. extern (C) nothrow int luaL_drawCharacter(lua_State* L)
  160. {
  161. try {
  162. //configuring needed parameters in characterTextures like coordinates, scale and drawTexture(its a boolean value which checks need this texture to be drawn or not)
  163. int count = to!int(luaL_checkinteger(L, 4));
  164. characterTextures[count].scale = luaL_checknumber(L, 3) * scale;
  165. characterTextures[count].y = cast(int) luaL_checknumber(L, 2);
  166. characterTextures[count].x = cast(int) luaL_checknumber(L, 1);
  167. characterTextures[count].drawTexture = true;
  168. characterTextures[count].justDrawn = true;
  169. if (lua_gettop(L) == 5) {
  170. lua_getfield(L, 5, "r");
  171. characterTextures[count].color.r = cast(ubyte)lua_tointeger(L, -1);
  172. lua_pop(L, 1);
  173. lua_getfield(L, 5, "g");
  174. characterTextures[count].color.g = cast(ubyte)lua_tointeger(L, -1);
  175. lua_pop(L, 1);
  176. lua_getfield(L, 5, "b");
  177. characterTextures[count].color.b = cast(ubyte)lua_tointeger(L, -1);
  178. lua_pop(L, 1);
  179. lua_getfield(L, 5, "a");
  180. //if empty, reset to default
  181. if (!lua_isnil(L, -1)) {
  182. characterTextures[count].color.a = cast(ubyte)lua_tointeger(L, -1);
  183. }
  184. lua_pop(L, 1);
  185. } else {
  186. characterTextures[count].color = Colors.WHITE;
  187. }
  188. debugWriteln("Count: ", count, " drawTexture cond: ", characterTextures[count].drawTexture);
  189. debugWriteln("arguments count: ", lua_gettop(L));
  190. } catch (Exception e) {
  191. debugWriteln(e.msg);
  192. }
  193. return 0;
  194. }
  195. extern (C) nothrow int luaL_stopDrawCharacter(lua_State* L)
  196. {
  197. int count = cast(int) luaL_checkinteger(L, 1);
  198. if (count >= characterTextures.length) {
  199. debugWriteln("error stop draw not loaded character");
  200. } else {
  201. characterTextures[count].drawTexture = false;
  202. }
  203. return 0;
  204. }
  205. extern (C) nothrow int luaL_unloadCharacter(lua_State *L) {
  206. int count = cast(int) luaL_checkinteger(L, 1);
  207. if (count >= characterTextures.length) {
  208. debugWriteln("error unloading not loaded character");
  209. } else {
  210. characterTextures[count].drawTexture = false;
  211. UnloadTexture(characterTextures[count].texture);
  212. }
  213. return 0;
  214. }
  215. /* music and video */
  216. extern (C) nothrow int luaL_loadMusic(lua_State* L)
  217. {
  218. try
  219. {
  220. char* musicPath = cast(char*)luaL_checkstring(L, 1);
  221. music = LoadMusicStream(musicPath);
  222. }
  223. catch (Exception e)
  224. {
  225. debugWriteln(e.msg);
  226. }
  227. return 0;
  228. }
  229. extern (C) nothrow int luaL_playMusic(lua_State* L)
  230. {
  231. PlayMusicStream(music);
  232. return 0;
  233. }
  234. extern (C) nothrow int luaL_stopMusic(lua_State* L)
  235. {
  236. StopMusicStream(music);
  237. return 0;
  238. }
  239. extern (C) nothrow int luaL_unloadMusic(lua_State* L)
  240. {
  241. UnloadMusicStream(music);
  242. music = Music();
  243. return 0;
  244. }
  245. extern (C) nothrow int luaL_playSfx(lua_State *L) {
  246. try {
  247. playSfx(to!string(luaL_checkstring(L, 1)));
  248. } catch (Exception e) {
  249. debugWriteln(e.msg);
  250. }
  251. return 0;
  252. }
  253. extern (C) nothrow int luaL_stopSfx(lua_State *L) {
  254. StopSound(sfx);
  255. return 0;
  256. }
  257. extern (C) nothrow int luaL_playVideo(lua_State* L)
  258. {
  259. try
  260. {
  261. videoFinished = false;
  262. playVideo(luaL_checkstring(L, 1).to!string);
  263. }
  264. catch (Exception e)
  265. {
  266. debugWriteln(e.msg);
  267. }
  268. return 0;
  269. }
  270. /* ui animations */
  271. extern (C) nothrow int luaL_moveCamera(lua_State *L) {
  272. try {
  273. oldCamera = camera;
  274. float targetX = cast(float) luaL_checknumber(L, 1);
  275. float targetY = cast(float) luaL_checknumber(L, 2);
  276. float zoom = cast(float) luaL_optnumber(L, 3, 1.0f);
  277. float speed = cast(float) luaL_optnumber(L, 4, 5.0f);
  278. debugWriteln(targetX, targetY, zoom, speed);
  279. cameraTargetX = targetX;
  280. cameraTargetY = targetY;
  281. cameraTargetZoom = zoom;
  282. cameraMoveSpeed = speed;
  283. isCameraMoving = true;
  284. } catch (Exception e) {
  285. debugWriteln(e.msg);
  286. }
  287. return 0;
  288. }
  289. extern (C) nothrow int luaL_restoreCamera(lua_State *L) {
  290. camera = oldCamera;
  291. return 0;
  292. }
  293. extern (C) nothrow int luaL_isCameraMoving(lua_State *L) {
  294. lua_pushboolean(L, isCameraMoving);
  295. return 1;
  296. }
  297. extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
  298. try {
  299. //loads from uifx folder HPFF files, in which png textures are stored
  300. framesUI = loadAnimationFramesUI(to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
  301. if (lua_gettop(L) == 3) {
  302. frameDuration = luaL_checknumber(L, 3);
  303. debug debugWriteln("frameDuration: ", frameDuration);
  304. }
  305. } catch (Exception e) {
  306. debugWriteln(e.msg);
  307. }
  308. return 0;
  309. }
  310. extern (C) nothrow int luaL_playUIAnimation(lua_State *L) {
  311. debug debugWriteln("Animation UI start");
  312. try {
  313. playAnimation = true;
  314. } catch (Exception e) {
  315. debugWriteln(e.msg);
  316. }
  317. return 0;
  318. }
  319. extern (C) nothrow int luaL_stopUIAnimation(lua_State *L) {
  320. playAnimation = false;
  321. debug debugWriteln("Animation UI stop");
  322. frameDuration = 0.016f;
  323. currentFrame = 0;
  324. return 0;
  325. }
  326. extern (C) nothrow int luaL_unloadUIAnimation(lua_State *L) {
  327. try {
  328. for (int i = 0; i < framesUI.length; i++) {
  329. UnloadTexture(framesUI[i]);
  330. }
  331. } catch (Exception e) {
  332. debugWriteln(e.msg);
  333. }
  334. return 0;
  335. }
  336. /* system */
  337. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  338. {
  339. lua_pushinteger(L, GetScreenWidth());
  340. return 1;
  341. }
  342. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  343. {
  344. lua_pushinteger(L, GetScreenHeight());
  345. return 1;
  346. }
  347. extern (C) nothrow int luaL_unloadFont(lua_State *L) {
  348. UnloadFont(textFont);
  349. return 0;
  350. }
  351. extern (C) nothrow int luaL_loadFont(lua_State* L)
  352. {
  353. const char* x = luaL_checkstring(L, 1);
  354. debugWriteln("Setting custom font: ", x.to!string);
  355. int[512] codepoints = 0;
  356. //configuring both cyrillic and latin fonts if available
  357. foreach (i; 0 .. 95)
  358. {
  359. codepoints[i] = 32 + i;
  360. }
  361. foreach (i; 0 .. 255)
  362. {
  363. codepoints[96 + i] = 0x400 + i;
  364. }
  365. int fontSize = max(10, cast(int)(40 * scale));
  366. textFont = LoadFontEx(x, fontSize, null, 0);
  367. return 0;
  368. }
  369. extern (C) nothrow int luaL_getTime(lua_State* L)
  370. {
  371. //getTime() returns current time.
  372. lua_pushnumber(L, GetTime());
  373. return 1;
  374. }
  375. extern (C) nothrow int luaL_getDeltaTime(lua_State* L)
  376. {
  377. //getDeltaTime
  378. lua_pushnumber(L, GetFrameTime());
  379. return 1;
  380. }
  381. float lastKeyPressTime = 0.0;
  382. immutable float keyPressCooldown = 0.2;
  383. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  384. {
  385. try
  386. {
  387. int keyCode = cast(int)luaL_checkinteger(L, 1);
  388. bool isPressed = IsKeyPressed(keyCode).to!bool;
  389. double currentTime = GetTime();
  390. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  391. {
  392. lua_pushboolean(L, false);
  393. }
  394. else if (isPressed)
  395. {
  396. lastKeyPressTime = currentTime;
  397. lua_pushboolean(L, true);
  398. }
  399. else
  400. {
  401. lua_pushboolean(L, false);
  402. }
  403. return 1;
  404. }
  405. catch (Exception e)
  406. {
  407. debugWriteln(e.msg);
  408. lua_pushboolean(L, false);
  409. return 1;
  410. }
  411. }
  412. extern (C) nothrow int luaL_isMouseButtonPressed(lua_State* L)
  413. {
  414. try
  415. {
  416. int keyCode = cast(int)luaL_checkinteger(L, 1);
  417. bool isPressed = IsMouseButtonPressed(keyCode);
  418. double currentTime = GetTime();
  419. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  420. {
  421. lua_pushboolean(L, false);
  422. }
  423. else if (isPressed)
  424. {
  425. lastKeyPressTime = currentTime;
  426. lua_pushboolean(L, true);
  427. }
  428. else
  429. {
  430. lua_pushboolean(L, false);
  431. }
  432. return 1;
  433. }
  434. catch (Exception e)
  435. {
  436. debugWriteln(e.msg);
  437. lua_pushboolean(L, false);
  438. return 1;
  439. }
  440. }
  441. extern (C) nothrow int luaL_loadScript(lua_State* L)
  442. {
  443. try
  444. {
  445. luaExec = luaL_checkstring(L, 1).to!string;
  446. }
  447. catch (Exception e)
  448. {
  449. debugWriteln(e.msg);
  450. }
  451. luaReload = true;
  452. return 0;
  453. }
  454. extern (C) nothrow int luaL_setGameState(lua_State *L) {
  455. currentGameState = cast(int)luaL_checkinteger(L, 1);
  456. return 0;
  457. }
  458. /* raylib direct bindings for graphics */
  459. /* basic */
  460. extern (C) nothrow int luaL_loadTexture(lua_State *L) {
  461. const char* fileName = luaL_checkstring(L, 1);
  462. Texture2D texture = LoadTexture(fileName);
  463. Texture2D* texturePtr = cast(Texture2D*)lua_newuserdata(L, Texture2D.sizeof);
  464. *texturePtr = texture;
  465. if (luaL_newmetatable(L, "Texture")) {
  466. lua_pushcfunction(L, &luaL_textureGC);
  467. lua_setfield(L, -2, "__gc");
  468. }
  469. SetTextureFilter(texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  470. lua_setmetatable(L, -2);
  471. return 1;
  472. }
  473. extern (C) nothrow int luaL_textureGC(lua_State *L) {
  474. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  475. UnloadTexture(*texture);
  476. return 0;
  477. }
  478. extern (C) nothrow int luaL_unloadTexture(lua_State *L) {
  479. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  480. UnloadTexture(*texture);
  481. return 0;
  482. }
  483. extern (C) nothrow int luaL_drawTexture(lua_State *L) {
  484. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  485. int x = cast(int)luaL_checkinteger(L, 2);
  486. int y = cast(int)luaL_checkinteger(L, 3);
  487. Color color = Colors.WHITE;
  488. if (lua_gettop(L) >= 4 && lua_istable(L, 4)) {
  489. lua_getfield(L, 4, "r");
  490. color.r = cast(ubyte)lua_tointeger(L, -1);
  491. lua_pop(L, 1);
  492. lua_getfield(L, 4, "g");
  493. color.g = cast(ubyte)lua_tointeger(L, -1);
  494. lua_pop(L, 1);
  495. lua_getfield(L, 4, "b");
  496. color.b = cast(ubyte)lua_tointeger(L, -1);
  497. lua_pop(L, 1);
  498. lua_getfield(L, 4, "a");
  499. color.a = cast(ubyte)lua_tointeger(L, -1);
  500. lua_pop(L, 1);
  501. }
  502. DrawTextureEx(*texture, Vector2(x, y), 0.0f, scale, color);
  503. return 0;
  504. }
  505. extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
  506. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  507. lua_pushnumber(L, texture.width*variables.scale);
  508. return 1;
  509. }
  510. extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
  511. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  512. lua_pushnumber(L, texture.height*variables.scale);
  513. return 1;
  514. }
  515. extern (C) nothrow int luaL_drawText(lua_State *L) {
  516. const char* text = luaL_checkstring(L, 1);
  517. int x = cast(int)luaL_checkinteger(L, 2);
  518. int y = cast(int)luaL_checkinteger(L, 3);
  519. int fontSize = cast(int)luaL_optinteger(L, 4, 20);
  520. Color color = Colors.WHITE;
  521. if (lua_istable(L, 5)) {
  522. lua_getfield(L, 5, "r");
  523. color.r = cast(ubyte)lua_tointeger(L, -1);
  524. lua_pop(L, 1);
  525. lua_getfield(L, 5, "g");
  526. color.g = cast(ubyte)lua_tointeger(L, -1);
  527. lua_pop(L, 1);
  528. lua_getfield(L, 5, "b");
  529. color.b = cast(ubyte)lua_tointeger(L, -1);
  530. lua_pop(L, 1);
  531. lua_getfield(L, 5, "a");
  532. color.a = cast(ubyte)lua_tointeger(L, -1);
  533. lua_pop(L, 1);
  534. }
  535. DrawTextEx(textFont, text, Vector2(x, y), fontSize, 1.0f, color);
  536. return 0;
  537. }
  538. extern (C) nothrow int luaL_measureTextX(lua_State *L) {
  539. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).x);
  540. return 1;
  541. }
  542. extern (C) nothrow int luaL_measureTextY(lua_State *L) {
  543. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).y);
  544. return 1;
  545. }
  546. /* extended */
  547. extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
  548. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  549. float x = luaL_checknumber(L, 2);
  550. float y = luaL_checknumber(L, 3);
  551. float rotation = luaL_optnumber(L, 4, 0);
  552. float scale = luaL_optnumber(L, 5, 1);
  553. Color color = Colors.WHITE;
  554. if (lua_istable(L, 6)) {
  555. lua_getfield(L, 6, "r");
  556. color.r = cast(ubyte)lua_tointeger(L, -1);
  557. lua_pop(L, 1);
  558. lua_getfield(L, 6, "g");
  559. color.g = cast(ubyte)lua_tointeger(L, -1);
  560. lua_pop(L, 1);
  561. lua_getfield(L, 6, "b");
  562. color.b = cast(ubyte)lua_tointeger(L, -1);
  563. lua_pop(L, 1);
  564. lua_getfield(L, 6, "a");
  565. color.a = cast(ubyte)lua_tointeger(L, -1);
  566. lua_pop(L, 1);
  567. }
  568. DrawTextureEx(*texture, Vector2(x, y), rotation, scale*variables.scale, color);
  569. return 0;
  570. }
  571. /* Register functions */
  572. extern (C) nothrow void luaL_loader(lua_State* L)
  573. {
  574. lua_register(L, "dialogBox", &luaL_dialogBox);
  575. lua_register(L, "isDialogExecuted", &luaL_isDialogExecuted);
  576. lua_register(L, "getAnswerValue", &luaL_getAnswerValue);
  577. lua_register(L, "loadAnimationUI", &luaL_loadUIAnimation);
  578. lua_register(L, "playAnimationUI", &luaL_playUIAnimation);
  579. lua_register(L, "stopAnimationUI", &luaL_stopUIAnimation);
  580. lua_register(L, "unloadAnimationUI", &luaL_unloadUIAnimation);
  581. lua_register(L, "moveCamera", &luaL_moveCamera);
  582. lua_register(L, "restoreCamera", &luaL_restoreCamera);
  583. lua_register(L, "isCameraMoving", &luaL_isCameraMoving);
  584. lua_register(L, "playVideo", &luaL_playVideo);
  585. lua_register(L, "loadMusic", &luaL_loadMusic);
  586. lua_register(L, "playMusic", &luaL_playMusic);
  587. lua_register(L, "stopMusic", &luaL_stopMusic);
  588. lua_register(L, "unloadMusic", &luaL_unloadMusic);
  589. lua_register(L, "playSfx", &luaL_playSfx);
  590. lua_register(L, "stopSfx", &luaL_stopSfx);
  591. lua_register(L, "loadCharacter", &luaL_loadCharacter);
  592. lua_register(L, "drawCharacter", &luaL_drawCharacter);
  593. lua_register(L, "stopDrawCharacter", &luaL_stopDrawCharacter);
  594. lua_register(L, "unloadCharacter", &luaL_unloadCharacter);
  595. lua_register(L, "loadBackground", &luaL_loadBackground);
  596. lua_register(L, "drawBackground", &luaL_drawBackground);
  597. lua_register(L, "stopDrawBackground", &luaL_stopDrawBackground);
  598. lua_register(L, "unloadBackground", &luaL_unloadBackground);
  599. lua_register(L, "loadScript", &luaL_loadScript);
  600. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  601. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  602. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  603. lua_register(L, "isMouseButtonPressed", &luaL_isMouseButtonPressed);
  604. lua_register(L, "setGameState", &luaL_setGameState);
  605. //raylib direct bindings
  606. lua_register(L, "unloadFont", &luaL_unloadFont);
  607. lua_register(L, "loadFont", &luaL_loadFont);
  608. lua_register(L, "getTime", &luaL_getTime);
  609. lua_register(L, "getDeltaTime", &luaL_getDeltaTime);
  610. lua_register(L, "loadTexture", &luaL_loadTexture);
  611. lua_register(L, "drawTexture", &luaL_drawTexture);
  612. lua_register(L, "drawTextureEx", &luaL_drawTextureEx);
  613. lua_register(L, "unloadTexture", &luaL_unloadTexture);
  614. lua_register(L, "drawText", &luaL_drawText);
  615. lua_register(L, "measureTextX", &luaL_measureTextX);
  616. lua_register(L, "measureTextY", &luaL_measureTextY);
  617. lua_register(L, "getTextureWidth", &luaL_getTextureWidth);
  618. lua_register(L, "getTextureHeight", &luaL_getTextureHeight);
  619. //compat
  620. lua_register(L, "setFont", &luaL_loadFont);
  621. debugWriteln("strict mode enabled");
  622. const char* strict_lua =
  623. `local mt = {
  624. __index = function(_, name)
  625. error("attempt to call undefined function: " .. tostring(name), 2)
  626. end
  627. }
  628. setmetatable(_G, mt)`;
  629. if (luaL_dostring(L, strict_lua) != LUA_OK) {
  630. debugWriteln("Failed to set strict mode: ", to!string(lua_tostring(L, -1)));
  631. lua_pop(L, 1); // Pop the error message
  632. }
  633. }
  634. int luaInit(string luaExec)
  635. {
  636. debugWriteln("loading Lua");
  637. L = luaL_newstate();
  638. luaL_openlibs(L);
  639. luaL_loader(L);
  640. debugWriteln("Executing next Lua file: ", luaExec);
  641. if (std.file.exists(luaExec) == false) {
  642. debugWriteln("Script file not found! Exiting.");
  643. return EngineExitCodes.EXIT_FILE_NOT_FOUND;
  644. }
  645. if (luaL_dofile(L, toStringz(luaExec)) != LUA_OK) {
  646. debugWriteln("Lua error: ", to!string(lua_tostring(L, -1)));
  647. return EngineExitCodes.EXIT_SCRIPT_ERROR;
  648. }
  649. return EngineExitCodes.EXIT_OK;
  650. }
  651. void luaEventLoop()
  652. {
  653. lua_getglobal(L, "EventLoop");
  654. if (lua_pcall(L, 0, 0, 0) != LUA_OK)
  655. {
  656. debug debugWriteln("Error in EventLoop: ", to!string(lua_tostring(L, -1)));
  657. }
  658. lua_pop(L, 0);
  659. }