lua.d 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. string hint;
  22. extern (C) nothrow int luaL_showHint(lua_State* L)
  23. {
  24. hint = "" ~ to!string(luaL_checkstring(L, 1));
  25. hintNeeded = true;
  26. return 0;
  27. }
  28. /* text window */
  29. extern (C) nothrow int luaL_dialogBox(lua_State* L)
  30. {
  31. showDialog = true;
  32. luaL_checktype(L, 2, LUA_TTABLE);
  33. choicePage = cast(int)luaL_checkinteger(L, 4);
  34. int textTableLength = cast(int) lua_objlen(L, 2);
  35. messageGlobal = new string[](textTableLength);
  36. for (int i = 0; i < textTableLength; i++) {
  37. lua_rawgeti(L, 2, i + 1);
  38. messageGlobal[i] = luaL_checkstring(L, -1).to!string;
  39. lua_pop(L, 1);
  40. }
  41. luaL_checktype(L, 5, LUA_TTABLE);
  42. int choicesLength = cast(int) lua_objlen(L, 5);
  43. choices = new string[choicesLength];
  44. for (int i = 0; i < choicesLength; i++)
  45. {
  46. lua_rawgeti(L, 5, i + 1);
  47. choices[i] = luaL_checkstring(L, -1).to!string;
  48. lua_pop(L, 1);
  49. }
  50. if (lua_gettop(L) == 7)
  51. {
  52. typingSpeed = cast(float) luaL_checknumber(L, 7);
  53. }
  54. return 0;
  55. }
  56. extern (C) nothrow int luaL_getAnswerValue(lua_State* L)
  57. {
  58. lua_pushinteger(L, selectedChoice);
  59. return 1;
  60. }
  61. extern (C) nothrow int luaL_isDialogExecuted(lua_State *L) {
  62. lua_pushboolean(L, showDialog);
  63. return 1;
  64. }
  65. extern (C) nothrow int luaL_dialogAnswerValue(lua_State* L)
  66. {
  67. lua_pushinteger(L, selectedChoice);
  68. return 1;
  69. }
  70. /* background drawing and loading */
  71. extern (C) nothrow int luaL_load2Dbackground(lua_State* L)
  72. {
  73. try
  74. {
  75. int index = cast(int) luaL_checkinteger(L, 2);
  76. //if index too big, extending array
  77. if (index >= backgrounds.length)
  78. {
  79. backgrounds.length = index + 1;
  80. }
  81. // if texture with same Index already loaded, unloading it
  82. if (index < backgrounds.length && backgrounds[index].id != 0)
  83. {
  84. UnloadTexture(backgrounds[index]);
  85. }
  86. backgrounds[index] = LoadTexture(luaL_checkstring(L, 1));
  87. }
  88. catch (Exception e)
  89. {
  90. debugWriteln(e.msg);
  91. }
  92. return 0;
  93. }
  94. extern (C) nothrow int luaL_draw2Dbackground(lua_State* L)
  95. {
  96. try
  97. {
  98. backgroundTexture = backgrounds[luaL_checkinteger(L, 1)];
  99. neededDraw2D = true;
  100. }
  101. catch (Exception e)
  102. {
  103. debugWriteln(e.msg);
  104. }
  105. return 0;
  106. }
  107. extern (C) nothrow int luaL_unload2Dbackground(lua_State* L)
  108. {
  109. UnloadTexture(backgrounds[cast(int) luaL_checkinteger(L, 1)]);
  110. return 0;
  111. }
  112. /* character textures */
  113. extern (C) nothrow int luaL_load2Dcharacter(lua_State *L) {
  114. try
  115. {
  116. int count = cast(int) luaL_checkinteger(L, 2);
  117. if (count >= characterTextures.length)
  118. {
  119. characterTextures.length = count + 1;
  120. }
  121. characterTextures[count].texture = LoadTexture(luaL_checkstring(L, 1));
  122. characterTextures[count].width = characterTextures[count].texture.width;
  123. characterTextures[count].height = characterTextures[count].texture.height;
  124. characterTextures[count].drawTexture = false;
  125. }
  126. catch (Exception e) {
  127. debugWriteln(e.msg);
  128. }
  129. return 0;
  130. }
  131. extern (C) nothrow int luaL_draw2Dcharacter(lua_State* L)
  132. {
  133. try {
  134. int count = to!int(luaL_checkinteger(L, 4));
  135. characterTextures[count].scale = luaL_checknumber(L, 3);
  136. characterTextures[count].y = cast(int) luaL_checkinteger(L, 2);
  137. characterTextures[count].x = cast(int) luaL_checkinteger(L, 1);
  138. characterTextures[count].drawTexture = true;
  139. debugWriteln("Count: ", count, " drawTexture cond: ", characterTextures[count].drawTexture);
  140. } catch (Exception e) {
  141. debugWriteln(e.msg);
  142. }
  143. return 0;
  144. }
  145. extern (C) nothrow int luaL_stopDraw2Dcharacter(lua_State* L)
  146. {
  147. int count = cast(int) luaL_checkinteger(L, 1);
  148. characterTextures[count].drawTexture = false;
  149. return 0;
  150. }
  151. extern (C) nothrow int luaL_unload2Dcharacter(lua_State *L) {
  152. int count = cast(int) luaL_checkinteger(L, 1);
  153. UnloadTexture(characterTextures[count].texture);
  154. return 0;
  155. }
  156. /* music and video */
  157. extern (C) nothrow int luaL_LoadMusic(lua_State* L)
  158. {
  159. try
  160. {
  161. musicPath = cast(char*) luaL_checkstring(L, 1);
  162. music = LoadMusicStream(musicPath);
  163. }
  164. catch (Exception e)
  165. {
  166. debugWriteln(e.msg);
  167. }
  168. return 0;
  169. }
  170. extern (C) nothrow int luaL_PlayMusic(lua_State* L)
  171. {
  172. PlayMusicStream(music);
  173. return 0;
  174. }
  175. extern (C) nothrow int luaL_StopMusic(lua_State* L)
  176. {
  177. StopMusicStream(music);
  178. return 0;
  179. }
  180. extern (C) nothrow int luaL_playSfx(lua_State *L) {
  181. try {
  182. playSfx(to!string(luaL_checkstring(L, 1)));
  183. } catch (Exception e) {
  184. debugWriteln(e.msg);
  185. }
  186. return 0;
  187. }
  188. extern (C) nothrow int luaL_stopSfx(lua_State *L) {
  189. StopSound(sfx);
  190. return 0;
  191. }
  192. extern (C) nothrow int luaL_playVideo(lua_State* L)
  193. {
  194. try
  195. {
  196. videoFinished = false;
  197. playVideo(luaL_checkstring(L, 1).to!string);
  198. }
  199. catch (Exception e)
  200. {
  201. debugWriteln(e.msg);
  202. }
  203. return 0;
  204. }
  205. /* ui animations */
  206. extern (C) nothrow int luaL_moveCamera(lua_State *L) {
  207. try {
  208. float targetX = cast(float) luaL_checknumber(L, 1);
  209. float targetY = cast(float) luaL_checknumber(L, 2);
  210. float zoom = cast(float) luaL_optnumber(L, 3, 1.0f);
  211. float speed = cast(float) luaL_optnumber(L, 4, 5.0f);
  212. cameraTargetX = targetX;
  213. cameraTargetY = targetY;
  214. cameraTargetZoom = zoom;
  215. cameraMoveSpeed = speed;
  216. isCameraMoving = true;
  217. } catch (Exception e) {
  218. debugWriteln(e.msg);
  219. }
  220. return 0;
  221. }
  222. extern (C) nothrow int luaL_isCameraMoving(lua_State *L) {
  223. lua_pushboolean(L, isCameraMoving);
  224. return 1;
  225. }
  226. extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
  227. try {
  228. framesUI = loadAnimationFramesUI("res/uifx/"~to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
  229. if (lua_gettop(L) == 3) {
  230. frameDuration = luaL_checknumber(L, 3);
  231. debug debugWriteln("frameDuration: ", frameDuration);
  232. }
  233. } catch (Exception e) {
  234. debugWriteln(e.msg);
  235. }
  236. return 0;
  237. }
  238. extern (C) nothrow int luaL_playUIAnimation(lua_State *L) {
  239. debug debugWriteln("Animation UI start");
  240. try {
  241. playAnimation = true;
  242. } catch (Exception e) {
  243. debugWriteln(e.msg);
  244. }
  245. return 0;
  246. }
  247. extern (C) nothrow int luaL_stopUIAnimation(lua_State *L) {
  248. playAnimation = false;
  249. debug debugWriteln("Animation UI stop");
  250. frameDuration = 0.016f;
  251. currentFrame = 0;
  252. return 0;
  253. }
  254. extern (C) nothrow int luaL_unloadUIAnimation(lua_State *L) {
  255. try {
  256. for (int i = 0; i < framesUI.length; i++) {
  257. UnloadTexture(framesUI[i]);
  258. }
  259. } catch (Exception e) {
  260. debugWriteln(e.msg);
  261. }
  262. return 0;
  263. }
  264. /* system */
  265. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  266. {
  267. lua_pushinteger(L, GetScreenWidth());
  268. return 1;
  269. }
  270. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  271. {
  272. lua_pushinteger(L, GetScreenHeight());
  273. return 1;
  274. }
  275. extern (C) nothrow int luaL_getUsedLanguage(lua_State* L)
  276. {
  277. lua_pushstring(L, usedLang.toStringz());
  278. return 1;
  279. }
  280. extern (C) nothrow int luaL_2dModeEnable(lua_State* L)
  281. {
  282. neededDraw2D = true;
  283. return 0;
  284. }
  285. extern (C) nothrow int luaL_2dModeDisable(lua_State* L)
  286. {
  287. neededDraw2D = false;
  288. return 0;
  289. }
  290. extern (C) nothrow int luaL_setGameFont(lua_State* L)
  291. {
  292. const char* x = luaL_checkstring(L, 1);
  293. debugWriteln("Setting custom font: ", x.to!string);
  294. int[512] codepoints = 0;
  295. foreach (i; 0 .. 95)
  296. {
  297. codepoints[i] = 32 + i;
  298. }
  299. foreach (i; 0 .. 255)
  300. {
  301. codepoints[96 + i] = 0x400 + i;
  302. }
  303. textFont = LoadFontEx(x, 40, codepoints.ptr, codepoints.length);
  304. return 0;
  305. }
  306. extern (C) nothrow int luaL_getTime(lua_State* L)
  307. {
  308. lua_pushnumber(L, GetTime());
  309. return 1;
  310. }
  311. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  312. {
  313. try
  314. {
  315. if (IsKeyPressed(cast(int)(luaL_checkinteger(L, 1))))
  316. {
  317. lua_pushboolean(L, true);
  318. }
  319. else
  320. {
  321. lua_pushboolean(L, false);
  322. }
  323. }
  324. catch (Exception e)
  325. {
  326. debugWriteln(e.msg);
  327. }
  328. return 1;
  329. }
  330. extern (C) nothrow int luaL_loadScript(lua_State* L)
  331. {
  332. for (int i = cast(int) characterTextures.length; i < characterTextures.length; i++)
  333. {
  334. UnloadTexture(characterTextures[i].texture);
  335. }
  336. for (int i = cast(int) backgrounds.length; i < backgrounds.length; i++)
  337. {
  338. UnloadTexture(backgrounds[i]);
  339. }
  340. try
  341. {
  342. luaExec = to!string(luaL_checkstring(L, 1));
  343. resetAllScriptValues();
  344. }
  345. catch (Exception e)
  346. {
  347. debugWriteln(e.msg);
  348. }
  349. luaReload = true;
  350. return 0;
  351. }
  352. /* Register functions */
  353. extern (C) nothrow void luaL_loader(lua_State* L)
  354. {
  355. lua_register(L, "dialogBox", &luaL_dialogBox);
  356. lua_register(L, "dialogAnswerValue", &luaL_dialogAnswerValue);
  357. lua_register(L, "isDialogExecuted", &luaL_isDialogExecuted);
  358. lua_register(L, "getAnswerValue", &luaL_getAnswerValue);
  359. lua_register(L, "loadAnimationUI", &luaL_loadUIAnimation);
  360. lua_register(L, "playAnimationUI", &luaL_playUIAnimation);
  361. lua_register(L, "stopAnimationUI", &luaL_stopUIAnimation);
  362. lua_register(L, "unloadAnimationUI", &luaL_unloadUIAnimation);
  363. lua_register(L, "moveCamera", &luaL_moveCamera);
  364. lua_register(L, "isCameraMoving", &luaL_isCameraMoving);
  365. lua_register(L, "playVideo", &luaL_playVideo);
  366. lua_register(L, "loadMusic", &luaL_LoadMusic);
  367. lua_register(L, "playMusic", &luaL_PlayMusic);
  368. lua_register(L, "stopMusic", &luaL_StopMusic);
  369. lua_register(L, "playSfx", &luaL_playSfx);
  370. lua_register(L, "stopSfx", &luaL_stopSfx);
  371. lua_register(L, "Begin2D", &luaL_2dModeEnable);
  372. lua_register(L, "End2D", &luaL_2dModeDisable);
  373. lua_register(L, "load2Dcharacter", &luaL_load2Dcharacter);
  374. lua_register(L, "draw2Dcharacter", &luaL_draw2Dcharacter);
  375. lua_register(L, "stopDraw2Dcharacter", &luaL_stopDraw2Dcharacter);
  376. lua_register(L, "unload2Dcharacter", &luaL_unload2Dcharacter);
  377. lua_register(L, "load2Dtexture", &luaL_load2Dbackground);
  378. lua_register(L, "draw2Dtexture", &luaL_draw2Dbackground);
  379. lua_register(L, "unload2Dtexture", &luaL_unload2Dbackground);
  380. lua_register(L, "getTime", &luaL_getTime);
  381. lua_register(L, "loadScript", &luaL_loadScript);
  382. lua_register(L, "setFont", &luaL_setGameFont);
  383. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  384. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  385. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  386. lua_register(L, "getLanguage", &luaL_getUsedLanguage);
  387. }
  388. int luaInit(string luaExec)
  389. {
  390. debugWriteln("loading Lua");
  391. L = luaL_newstate();
  392. luaL_openlibs(L);
  393. luaL_loader(L);
  394. debugWriteln("Executing next Lua file: ", luaExec);
  395. if (std.file.exists(luaExec) == false) {
  396. debugWriteln("Script file not found! Exiting.");
  397. return EngineExitCodes.EXIT_FILE_NOT_FOUND;
  398. }
  399. if (luaL_dofile(L, toStringz(luaExec)) != LUA_OK) {
  400. debugWriteln("Lua error: ", to!string(lua_tostring(L, -1)));
  401. return EngineExitCodes.EXIT_SCRIPT_ERROR;
  402. }
  403. return EngineExitCodes.EXIT_OK;
  404. }
  405. void luaEventLoop()
  406. {
  407. lua_getglobal(L, "EventLoop");
  408. if (lua_pcall(L, 0, 0, 0) != LUA_OK)
  409. {
  410. debug debugWriteln("Error in EventLoop: ", to!string(lua_tostring(L, -1)));
  411. }
  412. lua_pop(L, 0);
  413. }