lua.d 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. import std.array;
  15. import std.algorithm;
  16. /*
  17. * This module provides Lua bindings for various engine functionalities.
  18. * Functions are built on top of engine built-in functions for execution from scripts.
  19. * Not all engine functions usable for scripting are yet implemented.
  20. */
  21. /* text window */
  22. extern (C) nothrow int luaL_dialogBox(lua_State* L)
  23. {
  24. showDialog = true;
  25. // parse table with text pages
  26. luaL_checktype(L, 1, LUA_TTABLE);
  27. int textTableLength = cast(int) lua_objlen(L, 1);
  28. messageGlobal = new string[](textTableLength);
  29. foreach (i; 0..textTableLength) {
  30. lua_rawgeti(L, 1, i + 1);
  31. messageGlobal[i] = luaL_checkstring(L, -1).to!string;
  32. backlogText ~= messageGlobal[i];
  33. lua_pop(L, 1);
  34. }
  35. //parse table with choices
  36. luaL_checktype(L, 2, LUA_TTABLE);
  37. int choicesLength = cast(int) lua_objlen(L, 2);
  38. choices = new string[choicesLength];
  39. foreach (i; 0..choicesLength)
  40. {
  41. lua_rawgeti(L, 2, i + 1);
  42. choices[i] = luaL_checkstring(L, -1).to!string;
  43. lua_pop(L, 1);
  44. }
  45. //if provided, get page on which choices must be shown
  46. if (lua_gettop(L) >= 3) {
  47. choicePage = cast(int)luaL_checkinteger(L, 3);
  48. }
  49. if (lua_gettop(L) >= 4 && !lua_istable(L, 4)) {
  50. typingSpeed = cast(float) luaL_checknumber(L, 4);
  51. }
  52. return 0;
  53. }
  54. extern (C) nothrow int luaL_getAnswerValue(lua_State* L)
  55. {
  56. lua_pushinteger(L, selectedChoice);
  57. return 1;
  58. }
  59. extern (C) nothrow int luaL_isDialogExecuted(lua_State *L) {
  60. lua_pushboolean(L, showDialog);
  61. return 1;
  62. }
  63. /* background drawing and loading */
  64. extern (C) nothrow int luaL_load2Dbackground(lua_State* L)
  65. {
  66. try
  67. {
  68. int count = cast(int)luaL_checkinteger(L, 2);
  69. if (count >= backgroundTextures.length) {
  70. backgroundTextures.length = count + 1;
  71. }
  72. if (backgroundTextures[count].texture.id != 0) {
  73. UnloadTexture(backgroundTextures[count].texture);
  74. }
  75. char* filename = cast(char*)luaL_checkstring(L, 1);
  76. backgroundTextures[count].texture = LoadTexture(filename);
  77. SetTextureFilter(backgroundTextures[count].texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  78. }
  79. catch (Exception e)
  80. {
  81. debugWriteln(e.msg);
  82. }
  83. return 0;
  84. }
  85. extern (C) nothrow int luaL_draw2Dbackground(lua_State* L)
  86. {
  87. try
  88. {
  89. int count = cast(int)luaL_checkinteger(L, 4);
  90. debugWriteln(backgroundTextures[count]);
  91. backgroundTextures[count].height = backgroundTextures[count].texture.height;
  92. if (backgroundTextures.length < backgroundTextures.length) {
  93. backgroundTextures.length = backgroundTextures.length;
  94. }
  95. backgroundTextures[count].width = backgroundTextures[count].texture.width;
  96. backgroundTextures[count].x = luaL_checknumber(L, 1);
  97. backgroundTextures[count].y = luaL_checknumber(L, 2);
  98. backgroundTextures[count].scale = luaL_checknumber(L, 3);
  99. backgroundTextures[count].drawTexture = true;
  100. debugWriteln(backgroundTextures[count]);
  101. }
  102. catch (Exception e)
  103. {
  104. debugWriteln(e.msg);
  105. }
  106. return 0;
  107. }
  108. extern (C) nothrow int luaL_stopDraw2Dbackground(lua_State* L)
  109. {
  110. try
  111. {
  112. int count = cast(int)luaL_checkinteger(L, 1);
  113. if (count >= backgroundTextures.length) {
  114. debugWriteln("stop draw not loaded background unavailable");
  115. } else {
  116. backgroundTextures[count].drawTexture = false;
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. debugWriteln(e.msg);
  122. }
  123. return 0;
  124. }
  125. extern (C) nothrow int luaL_unload2Dbackground(lua_State* L)
  126. {
  127. int count = cast(int)luaL_checkinteger(L, 1);
  128. if (count >= backgroundTextures.length) {
  129. debugWriteln("Unloading non-loaded background unavailable");
  130. } else {
  131. backgroundTextures[count].drawTexture = false;
  132. UnloadTexture(backgroundTextures[count].texture);
  133. }
  134. return 0;
  135. }
  136. /* character textures */
  137. extern (C) nothrow int luaL_load2Dcharacter(lua_State *L) {
  138. try
  139. {
  140. int count = cast(int) luaL_checkinteger(L, 2);
  141. if (count >= characterTextures.length) {
  142. characterTextures.length = count + 1;
  143. }
  144. if (characterTextures.length < characterTextures.length) {
  145. characterTextures.length = characterTextures.length;
  146. }
  147. if (count < characterTextures.length && characterTextures[count].texture.id != 0) {
  148. UnloadTexture(characterTextures[count].texture);
  149. }
  150. characterTextures[count].texture = LoadTexture(luaL_checkstring(L, 1));
  151. SetTextureFilter(characterTextures[count].texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  152. characterTextures[count].width = characterTextures[count].texture.width;
  153. characterTextures[count].height = characterTextures[count].texture.height;
  154. characterTextures[count].drawTexture = false;
  155. }
  156. catch (Exception e) {
  157. debugWriteln(e.msg);
  158. }
  159. return 0;
  160. }
  161. extern (C) nothrow int luaL_draw2Dcharacter(lua_State* L)
  162. {
  163. try {
  164. //configuring needed parameters in characterTextures like coordinates, scale and drawTexture(its a boolean value which checks need this texture to be drawn or not)
  165. int count = to!int(luaL_checkinteger(L, 4));
  166. characterTextures[count].scale = luaL_checknumber(L, 3);
  167. characterTextures[count].y = cast(int) luaL_checkinteger(L, 2);
  168. characterTextures[count].x = cast(int) luaL_checkinteger(L, 1);
  169. characterTextures[count].drawTexture = true;
  170. characterTextures[count].justDrawn = true;
  171. if (lua_gettop(L) == 5) {
  172. lua_getfield(L, 5, "r");
  173. characterColor.r = cast(ubyte)lua_tointeger(L, -1);
  174. lua_pop(L, 1);
  175. lua_getfield(L, 5, "g");
  176. characterColor.g = cast(ubyte)lua_tointeger(L, -1);
  177. lua_pop(L, 1);
  178. lua_getfield(L, 5, "b");
  179. characterColor.b = cast(ubyte)lua_tointeger(L, -1);
  180. lua_pop(L, 1);
  181. lua_getfield(L, 5, "a");
  182. //if empty, reset to default
  183. if (!lua_isnil(L, -1)) {
  184. characterColor.a = cast(ubyte)lua_tointeger(L, -1);
  185. }
  186. lua_pop(L, 1);
  187. } else {
  188. characterColor = Colors.WHITE;
  189. }
  190. debugWriteln("Count: ", count, " drawTexture cond: ", characterTextures[count].drawTexture);
  191. debugWriteln("arguments count: ", lua_gettop(L));
  192. } catch (Exception e) {
  193. debugWriteln(e.msg);
  194. }
  195. return 0;
  196. }
  197. extern (C) nothrow int luaL_stopDraw2Dcharacter(lua_State* L)
  198. {
  199. int count = cast(int) luaL_checkinteger(L, 1);
  200. if (count >= characterTextures.length) {
  201. debugWriteln("error stop draw not loaded character");
  202. } else {
  203. characterTextures[count].drawTexture = false;
  204. }
  205. return 0;
  206. }
  207. extern (C) nothrow int luaL_unload2Dcharacter(lua_State *L) {
  208. int count = cast(int) luaL_checkinteger(L, 1);
  209. if (count >= characterTextures.length) {
  210. debugWriteln("error unloading not loaded character");
  211. } else {
  212. characterTextures[count].drawTexture = false;
  213. UnloadTexture(characterTextures[count].texture);
  214. }
  215. return 0;
  216. }
  217. /* music and video */
  218. extern (C) nothrow int luaL_loadMusic(lua_State* L)
  219. {
  220. try
  221. {
  222. musicPath = cast(char*) luaL_checkstring(L, 1);
  223. music = LoadMusicStream(musicPath);
  224. }
  225. catch (Exception e)
  226. {
  227. debugWriteln(e.msg);
  228. }
  229. return 0;
  230. }
  231. extern (C) nothrow int luaL_playMusic(lua_State* L)
  232. {
  233. PlayMusicStream(music);
  234. return 0;
  235. }
  236. extern (C) nothrow int luaL_stopMusic(lua_State* L)
  237. {
  238. StopMusicStream(music);
  239. return 0;
  240. }
  241. extern (C) nothrow int luaL_unloadMusic(lua_State* L)
  242. {
  243. UnloadMusicStream(music);
  244. return 0;
  245. }
  246. extern (C) nothrow int luaL_playSfx(lua_State *L) {
  247. try {
  248. playSfx(to!string(luaL_checkstring(L, 1)));
  249. } catch (Exception e) {
  250. debugWriteln(e.msg);
  251. }
  252. return 0;
  253. }
  254. extern (C) nothrow int luaL_stopSfx(lua_State *L) {
  255. StopSound(sfx);
  256. return 0;
  257. }
  258. extern (C) nothrow int luaL_playVideo(lua_State* L)
  259. {
  260. try
  261. {
  262. videoFinished = false;
  263. playVideo(luaL_checkstring(L, 1).to!string);
  264. }
  265. catch (Exception e)
  266. {
  267. debugWriteln(e.msg);
  268. }
  269. return 0;
  270. }
  271. /* ui animations */
  272. extern (C) nothrow int luaL_moveCamera(lua_State *L) {
  273. try {
  274. oldCamera = camera;
  275. float targetX = cast(float) luaL_checknumber(L, 1);
  276. float targetY = cast(float) luaL_checknumber(L, 2);
  277. float zoom = cast(float) luaL_optnumber(L, 3, 1.0f);
  278. float speed = cast(float) luaL_optnumber(L, 4, 5.0f);
  279. debugWriteln(targetX, targetY, zoom, speed);
  280. cameraTargetX = targetX;
  281. cameraTargetY = targetY;
  282. cameraTargetZoom = zoom;
  283. cameraMoveSpeed = speed;
  284. isCameraMoving = true;
  285. } catch (Exception e) {
  286. debugWriteln(e.msg);
  287. }
  288. return 0;
  289. }
  290. extern (C) nothrow int luaL_restoreCamera(lua_State *L) {
  291. camera = oldCamera;
  292. return 0;
  293. }
  294. extern (C) nothrow int luaL_isCameraMoving(lua_State *L) {
  295. lua_pushboolean(L, isCameraMoving);
  296. return 1;
  297. }
  298. extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
  299. try {
  300. //loads from uifx folder HPFF files, in which png textures are stored
  301. framesUI = loadAnimationFramesUI(to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
  302. if (lua_gettop(L) == 3) {
  303. frameDuration = luaL_checknumber(L, 3);
  304. debug debugWriteln("frameDuration: ", frameDuration);
  305. }
  306. } catch (Exception e) {
  307. debugWriteln(e.msg);
  308. }
  309. return 0;
  310. }
  311. extern (C) nothrow int luaL_playUIAnimation(lua_State *L) {
  312. debug debugWriteln("Animation UI start");
  313. try {
  314. playAnimation = true;
  315. } catch (Exception e) {
  316. debugWriteln(e.msg);
  317. }
  318. return 0;
  319. }
  320. extern (C) nothrow int luaL_stopUIAnimation(lua_State *L) {
  321. playAnimation = false;
  322. debug debugWriteln("Animation UI stop");
  323. frameDuration = 0.016f;
  324. currentFrame = 0;
  325. return 0;
  326. }
  327. extern (C) nothrow int luaL_unloadUIAnimation(lua_State *L) {
  328. try {
  329. for (int i = 0; i < framesUI.length; i++) {
  330. UnloadTexture(framesUI[i]);
  331. }
  332. } catch (Exception e) {
  333. debugWriteln(e.msg);
  334. }
  335. return 0;
  336. }
  337. /* system */
  338. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  339. {
  340. lua_pushinteger(L, GetScreenWidth());
  341. return 1;
  342. }
  343. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  344. {
  345. lua_pushinteger(L, GetScreenHeight());
  346. return 1;
  347. }
  348. extern (C) nothrow int luaL_unloadFont(lua_State *L) {
  349. UnloadFont(textFont);
  350. return 0;
  351. }
  352. extern (C) nothrow int luaL_loadFont(lua_State* L)
  353. {
  354. const char* x = luaL_checkstring(L, 1);
  355. debugWriteln("Setting custom font: ", x.to!string);
  356. int[512] codepoints = 0;
  357. //configuring both cyrillic and latin fonts if available
  358. foreach (i; 0 .. 95)
  359. {
  360. codepoints[i] = 32 + i;
  361. }
  362. foreach (i; 0 .. 255)
  363. {
  364. codepoints[96 + i] = 0x400 + i;
  365. }
  366. textFont = LoadFontEx(x, 40, codepoints.ptr, codepoints.length);
  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. DrawTexture(*texture, x, y, 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_pushinteger(L, texture.width);
  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_pushinteger(L, texture.height);
  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, 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_load2Dcharacter);
  592. lua_register(L, "drawCharacter", &luaL_draw2Dcharacter);
  593. lua_register(L, "stopDrawCharacter", &luaL_stopDraw2Dcharacter);
  594. lua_register(L, "unloadCharacter", &luaL_unload2Dcharacter);
  595. lua_register(L, "loadBackground", &luaL_load2Dbackground);
  596. lua_register(L, "drawBackground", &luaL_draw2Dbackground);
  597. lua_register(L, "stopDrawBackground", &luaL_stopDraw2Dbackground);
  598. lua_register(L, "unloadBackground", &luaL_unload2Dbackground);
  599. lua_register(L, "getTime", &luaL_getTime);
  600. lua_register(L, "getDeltaTime", &luaL_getDeltaTime);
  601. lua_register(L, "loadScript", &luaL_loadScript);
  602. lua_register(L, "loadFont", &luaL_loadFont);
  603. lua_register(L, "unloadFont", &luaL_unloadFont);
  604. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  605. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  606. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  607. lua_register(L, "isMouseButtonPressed", &luaL_isMouseButtonPressed);
  608. lua_register(L, "setGameState", &luaL_setGameState);
  609. //raylib direct bindings
  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. }