lua.d 21 KB

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