lua.d 12 KB

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