lua.d 12 KB

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