ST_engine  0.3-ALPHA
lua_backend.cpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <game_manager/lua_backend/lua_backend.hpp>
11 
12 //The following trick is used to replace the game_manager with a mock implementation when testing.
13 #ifndef TESTING_LUA_BACKEND
14 
15 #include <game_manager/game_manager.hpp>
16 
17 #elif defined(TESTING_LUA_BACKEND)
18 #include <test/game_manager/lua_backend/game_manager_mock.hpp>
19 #endif
20 
21 #include <ST_util/string_util.hpp>
22 #include <game_manager/level/light.hpp>
23 #include <fstream>
24 #include <SDL_timer.h>
25 
26 //local to the file, as lua bindings cannot be in a class
27 static message_bus *gMessage_busLua;
28 static game_manager *gGame_managerLua;
29 static lua_backend *gLua_backendLua;
30 
31 static bool singleton_initialized = false;
32 
33 //TODO: Most of the functions here should be moved to the game_manager class and only act as simple proxies
34 
42  if (singleton_initialized) {
43  throw std::runtime_error("The lua backend cannot be initialized more than once!");
44  } else {
45  singleton_initialized = true;
46  }
47 
48  //Set the external dependencies.
49  gLua_backendLua = this;
50  gGame_managerLua = game_mngr;
51  gMessage_busLua = msg_bus;
52  gMessage_bus = msg_bus;
53 
54  //Initialize Lua
55  L = luaL_newstate();
56  if (L == nullptr) {
57  fprintf(stderr, "ERROR: Could not initialize a Lua context");
58  exit(1);
59  }
60  luaL_openlibs(L);
61 
62  //register lua binding functions
63 
64  lua_register(L, "logLua", logLua);
65  lua_register(L, "showCollisions", showCollisionsLua);
66  lua_register(L, "showFps", showFpsLua);
67  lua_register(L, "showMetrics", showMetricsLua);
68  lua_register(L, "consoleClear", consoleClearLua);
69 
70  //General Functions
71  lua_register(L, "saveGame", saveGameLua);
72  lua_register(L, "setFullscreenLua", setFullscreenLua);
73  lua_register(L, "getFullscreenStatus", getFullscreenStatusLua);
74  lua_register(L, "hashString", hashStringLua);
75  lua_register(L, "delay", delayLua);
76  lua_register(L, "use", useLua);
77  lua_register(L, "setVsyncLua", setVsyncLua);
78  lua_register(L, "getVsyncState", getVsyncStateLua);
79  lua_register(L, "setBrightness", setBrightnessLua);
80  lua_register(L, "startLevelLua", startLevelLua);
81  lua_register(L, "reloadLevelLua", reloadLevelLua);
82  lua_register(L, "showMouseCursor", showMouseCursorLua);
83  lua_register(L, "hideMouseCursor", hideMouseCursorLua);
84  lua_register(L, "endGame", endGameLua);
85  lua_register(L, "centerCamera", centerCameraLua);
86  lua_register(L, "centreCamera", centerCameraLua);
87  lua_register(L, "setLevelSize", setLevelSizeLua);
88  lua_register(L, "setLevelFloor", setLevelFloorLua);
89  lua_register(L, "loadLevel", load_levelLua);
90  lua_register(L, "unloadLevel", unload_levelLua);
91  lua_register(L, "loadAsset", loadAssetLua);
92  lua_register(L, "unloadAsset", unloadAssetLua);
93  lua_register(L, "setInternalResolution", setInternalResolutionLua);
94  lua_register(L, "getInternalResolution", getInternalResolutionLua);
95  lua_register(L, "setWindowResolution", setWindowResolutionLua);
96  lua_register(L, "getWindowResolution", getWindowResolutionLua);
97 
98  //Physics functions.
99  lua_register(L, "setGravity", setGravityLua);
100  lua_register(L, "getGravity", getGravityLua);
101  lua_register(L, "pausePhysics", pausePhysicsLua);
102  lua_register(L, "unpausePhysics", unpausePhysicsLua);
103 
104  //Drawing functions
105  lua_register(L, "setBackground", setBackgroundLua);
106  lua_register(L, "setBackgroundColor", setBackgroundColorLua);
107  lua_register(L, "setOverlay", setOverlayLua);
108 
109  //Input functions
110  lua_register(L, "getMouseX", getMouseXLua);
111  lua_register(L, "getMouseY", getMouseYLua);
112  lua_register(L, "keyHeld", keyHeldLua);
113  lua_register(L, "keyPressed", keyPressedLua);
114  lua_register(L, "keyReleased", keyReleasedLua);
115  lua_register(L, "leftTrigger", leftTriggerLua);
116  lua_register(L, "rightTrigger", rightTriggerLua);
117  lua_register(L, "rightStickHorizontal", rightStickHorizontalLua);
118  lua_register(L, "rightStickVertical", rightStickVerticalLua);
119  lua_register(L, "leftStickHorizontal", leftStickHorizontalLua);
120  lua_register(L, "leftStickVertical", leftStickVerticalLua);
121  lua_register(L, "controllerRumble", controllerRumbleLua);
122  lua_register(L, "setLeftStickHorizontalThreshold", setLeftStickHorizontalThresholdLua);
123  lua_register(L, "setLeftStickVerticalThreshold", setLeftStickVerticalThresholdLua);
124  lua_register(L, "setRightStickHorizontalThreshold", setRightStickHorizontalThresholdLua);
125  lua_register(L, "setRightStickVerticalThreshold", setRightStickVerticalThresholdLua);
126  lua_register(L, "setLeftTriggerThreshold", setLeftTriggerThresholdLua);
127  lua_register(L, "setRightTriggerThreshold", setRightTriggerThresholdLua);
128 
129  //Audio functions
130  lua_register(L, "playSound", playSoundLua);
131  lua_register(L, "playMusic", playMusicLua);
132  lua_register(L, "stopMusic", stopMusicLua);
133  lua_register(L, "pauseMusic", pauseMusicLua);
134  lua_register(L, "setAudioEnabledLua", setAudioEnabledLua);
135  lua_register(L, "isAudioEnabled", isAudioEnabledLua);
136  lua_register(L, "getSoundsVolume", getSoundsVolumeLua);
137  lua_register(L, "getMusicVolume", getMusicVolumeLua);
138  lua_register(L, "setSoundsVolume", setSoundsVolumeLua);
139  lua_register(L, "setMusicVolume", setMusicVolumeLua);
140  lua_register(L, "stopAllSounds", stopAllSoundsLua);
141 
142  //lights
143  lua_register(L, "setDarkness", setDarknessLua);
144  lua_register(L, "enableLighting", enableLightingLua);
145  lua_register(L, "createLight", createLightLua);
146  lua_register(L, "setLightOriginX", setLightOriginXLua);
147  lua_register(L, "getLightOriginX", getLightOriginXLua);
148  lua_register(L, "setLightOriginY", setLightOriginYLua);
149  lua_register(L, "getLightOriginY", getLightOriginYLua);
150  lua_register(L, "setLightIntensity", setLightIntensityLua);
151  lua_register(L, "getLightIntensity", getLightIntensityLua);
152  lua_register(L, "setLightBrightness", setLightBrightnessLua);
153  lua_register(L, "getLightBrightness", getLightBrightnessLua);
154  lua_register(L, "setLightRadius", setLightRadiusLua);
155  lua_register(L, "getLightRadius", getLightRadiusLua);
156  lua_register(L, "setLightStatic", setLightStaticLua);
157  lua_register(L, "isLightStatic", isLightStaticLua);
158 
159  //Text funtions
160  lua_register(L, "createTextObject", createTextObjectLua);
161  lua_register(L, "setTextObjectColor", setTextObjectColorLua);
162  lua_register(L, "setTextObjectText", setTextObjectTextLua);
163  lua_register(L, "setTextObjectFont", setTextObjectFontLua);
164  lua_register(L, "setTextObjectX", setTextObjectXLua);
165  lua_register(L, "setTextObjectY", setTextObjectYLua);
166  lua_register(L, "setTextObjectVisible", setTextObjectVisibleLua);
167 
168  //Entity functions
169 
170  //general
171  lua_register(L, "createEntity", createEntityLua);
172  lua_register(L, "setEntityActive", setEntityActiveLua);
173  lua_register(L, "setEntityX", setEntityXLua);
174  lua_register(L, "setEntityY", setEntityYLua);
175  lua_register(L, "getEntityX", getEntityXLua);
176  lua_register(L, "getEntityY", getEntityYLua);
177  lua_register(L, "setEntityStatic", setEntityStaticLua);
178  lua_register(L, "setEntityVelocityX", setEntityVelocityXLua);
179  lua_register(L, "setEntityVelocityY", setEntityVelocityYLua);
180  lua_register(L, "getEntityVelocityX", getEntityVelocityXLua);
181  lua_register(L, "getEntityVelocityY", getEntityVelocityYLua);
182 
183  //texture
184  lua_register(L, "setEntityTexture", setEntityTextureLua);
185  lua_register(L, "getEntityTexW", getEntityTexWLua);
186  lua_register(L, "getEntityTexH", getEntityTexHLua);
187  lua_register(L, "setEntityTexW", setEntityTexWLua);
188  lua_register(L, "setEntityTexH", setEntityTexHLua);
189  lua_register(L, "setEntityVisible", setEntityVisibleLua);
190  lua_register(L, "setEntityTextureScale", setEntityTextureScaleLua);
191 
192  //physics
193  lua_register(L, "setEntityCollisionBox", setEntityCollisionBoxLua);
194  lua_register(L, "entityCollides", entityCollidesLua);
195  lua_register(L, "setEntityAffectedByPhysics", setEntityAffectedByPhysicsLua);
196  lua_register(L, "getEntityColX", getEntityColXLua);
197  lua_register(L, "getEntityColY", getEntityColYLua);
198  lua_register(L, "getEntityColXOffset", getEntityColXOffsetLua);
199  lua_register(L, "getEntityColYOffset", getEntityColYOffsetLua);
200 
201  //animation
202  lua_register(L, "setEntityAnimation", setEntityAnimationLua);
203  lua_register(L, "setEntityAnimationNum", setEntityAnimationNumLua);
204  lua_register(L, "setEntitySpriteNum", setEntitySpriteNumLua);
205 
206  //mouse
207  lua_register(L, "mouseOverLua", mouseOverLua);
208  lua_register(L, "mouseOverTextureLua", mouseOverTextureLua);
209 
210  luaL_dofile(L, "lua/global_properties.lua");
211  luaL_dofile(L, "lua/general.lua");
212  luaL_dofile(L, "lua/debug.lua");
213  luaL_dofile(L, "lua/objects/entity.lua");
214  luaL_dofile(L, "lua/objects/text.lua");
215  luaL_dofile(L, "lua/objects/light.lua");
216  luaL_dofile(L, "lua/ui/button.lua");
217  luaL_dofile(L, "lua/ui/label.lua");
218  luaL_dofile(L, "lua/ui/checkbox.lua");
219 
220  //bootstrap game
221  luaL_dofile(L, "lua/main.lua");
222 
223  return 0;
224 }
225 
231 int8_t lua_backend::run_file(const std::string &file) {
232  std::string temp = hash_file(file);
233  int status = luaL_loadbuffer(L, temp.c_str(), temp.size(), file.c_str());
234  if (status == LUA_ERRSYNTAX || status == LUA_ERRFILE || lua_pcall(L, 0, 0, 0)) {
235  fprintf(stderr, "cannot compile script\n");
236  lua_error(L);
237  return -1;
238  } else [[likely]] {
239  return 0;
240  }
241 }
242 
248 int8_t lua_backend::load_file(const std::string &file) {
249  std::string temp = hash_file(file);
250  int status = luaL_loadbuffer(L, temp.c_str(), temp.size(), file.c_str());
251  if (status == LUA_ERRSYNTAX || status == LUA_ERRFILE) {
252  fprintf(stderr, "cannot compile script\n");
253  lua_error(L);
254  return -1;
255  } else {
256  return 0;
257  }
258 }
259 
265 int8_t lua_backend::run_script(const std::string &script) {
266  std::string temp = hash_string(script);
267  if (luaL_dostring(L, temp.c_str())) {
268  gMessage_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("Cannot run script")));
269  return -1;
270  } else [[likely]] {
271  return 0;
272  }
273 }
274 
279 void lua_backend::set_global(const std::string &arg) {
280  lua_setglobal(L, arg.c_str());
281 }
282 
287 void lua_backend::run_global(const std::string &arg) {
288  lua_getglobal(L, arg.c_str());
289  lua_pcall(L, 0, 0, 0);
290 }
291 
296  lua_close(L);
297  singleton_initialized = false;
298 }
299 
306 std::string lua_backend::hash_file(const std::string &path) {
307  std::ifstream file;
308  file.open(path.c_str());
309  std::string result;
310  if (file.is_open()) {
311  std::string temp;
312  while (!file.eof()) {
313  getline(file, temp);
314  if (!temp.empty()) {
315  temp.erase(temp.begin(), std::find_if(temp.begin(), temp.end(),
316  std::bind(std::not_equal_to<>(), ' ', std::placeholders::_1)));
317  while (temp.find("playSound(\"") != std::string::npos) {
318  std::string to_find = "playSound(\"";
319  std::string temp_buf;
320  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
321  temp_buf.push_back((temp.at(i)));
322  }
323  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
324  std::string temp_buf_2 = "\"" + temp_buf + "\"";
325  ST::replace_string(temp, temp_buf_2, string_hash);
326  }
327  while (temp.find("playMusic(\"") != std::string::npos) {
328  std::string to_find = "playMusic(\"";
329  std::string temp_buf;
330  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
331  temp_buf.push_back((temp.at(i)));
332  }
333  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
334  std::string temp_buf_2 = "\"" + temp_buf + "\"";
335  ST::replace_string(temp, temp_buf_2, string_hash);
336  }
337  while (temp.find("keyHeld(\"") != std::string::npos) {
338  std::string to_find = "keyHeld(\"";
339  std::string temp_buf;
340  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
341  temp_buf.push_back((temp.at(i)));
342  }
343  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
344  std::string temp_buf_2 = "\"" + temp_buf + "\"";
345  ST::replace_string(temp, temp_buf_2, string_hash);
346  }
347  while (temp.find("keyPressed(\"") != std::string::npos) {
348  std::string to_find = "keyPressed(\"";
349  std::string temp_buf;
350  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
351  temp_buf.push_back((temp.at(i)));
352  }
353  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
354  std::string temp_buf_2 = "\"" + temp_buf + "\"";
355  ST::replace_string(temp, temp_buf_2, string_hash);
356  }
357  while (temp.find("keyReleased(\"") != std::string::npos) {
358  std::string to_find = "keyReleased(\"";
359  std::string temp_buf;
360  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
361  temp_buf.push_back((temp.at(i)));
362  }
363  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
364  std::string temp_buf_2 = "\"" + temp_buf + "\"";
365  ST::replace_string(temp, temp_buf_2, string_hash);
366  }
367  while (temp.find("setClickKey(\"") != std::string::npos) {
368  std::string to_find = "setClickKey(\"";
369  std::string temp_buf;
370  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
371  temp_buf.push_back((temp.at(i)));
372  }
373  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
374  std::string temp_buf_2 = "\"" + temp_buf + "\"";
375  ST::replace_string(temp, temp_buf_2, string_hash);
376  }
377  //Process annotations
378  while (temp.find("----@Key") != std::string::npos) {
379  std::string to_find = "\"";
380  std::string temp_buf;
381  std::string next_line;
382  getline(file, next_line);
383  for (uint64_t i = next_line.find(to_find) + to_find.size(); next_line.at(i) != '\"'; i++) {
384  temp_buf.push_back((next_line.at(i)));
385  }
386  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
387  std::string temp_buf_2 = "\"" + temp_buf + "\"";
388  ST::replace_string(next_line, temp_buf_2, string_hash);
389  temp = next_line;
390  }
391  while (temp.find("----@Audio") != std::string::npos) {
392  std::string to_find = "\"";
393  std::string temp_buf;
394  std::string next_line;
395  getline(file, next_line);
396  for (uint64_t i = next_line.find(to_find) + to_find.size(); next_line.at(i) != '\"'; i++) {
397  temp_buf.push_back((next_line.at(i)));
398  }
399  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
400  std::string temp_buf_2 = "\"" + temp_buf + "\"";
401  ST::replace_string(next_line, temp_buf_2, string_hash);
402  temp = next_line;
403  }
404  result += temp + "\n";
405  }
406  }
407  file.close();
408  return result;
409  } else {
410  gMessage_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("File " + path + " not found")));
411  return "";
412  }
413 }
414 
421 std::string lua_backend::hash_string(const std::string &arg) {
422  std::string result;
423  std::string temp = arg;
424  if (!temp.empty()) {
425  temp.erase(temp.begin(), std::find_if(temp.begin(), temp.end(),
426  std::bind(std::not_equal_to<>(), ' ', std::placeholders::_1)));
427  while (temp.find("playSound(\"") != std::string::npos) {
428  std::string to_find = "playSound(\"";
429  std::string temp_buf;
430  for (uint64_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
431  temp_buf.push_back((temp.at(i)));
432  }
433  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
434  std::string temp_buf_2 = "\"" + temp_buf + "\"";
435  ST::replace_string(temp, temp_buf_2, string_hash);
436  }
437  while (temp.find("playMusic(\"") != std::string::npos) {
438  std::string to_find = "playMusic(\"";
439  std::string temp_buf;
440  for (size_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
441  temp_buf.push_back((temp.at(i)));
442  }
443  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
444  std::string temp_buf_2 = "\"" + temp_buf + "\"";
445  ST::replace_string(temp, temp_buf_2, string_hash);
446  }
447  while (temp.find("keyHeld(\"") != std::string::npos) {
448  std::string to_find = "keyHeld(\"";
449  std::string temp_buf;
450  for (size_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
451  temp_buf.push_back((temp.at(i)));
452  }
453  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
454  std::string temp_buf_2 = "\"" + temp_buf + "\"";
455  ST::replace_string(temp, temp_buf_2, string_hash);
456  }
457  while (temp.find("keyPressed(\"") != std::string::npos) {
458  std::string to_find = "keyPressed(\"";
459  std::string temp_buf;
460  for (size_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
461  temp_buf.push_back((temp.at(i)));
462  }
463  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
464  std::string temp_buf_2 = "\"" + temp_buf + "\"";
465  ST::replace_string(temp, temp_buf_2, string_hash);
466  }
467  while (temp.find("keyReleased(\"") != std::string::npos) {
468  std::string to_find = "keyReleased(\"";
469  std::string temp_buf;
470  for (size_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
471  temp_buf.push_back((temp.at(i)));
472  }
473  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
474  std::string temp_buf_2 = "\"" + temp_buf + "\"";
475  ST::replace_string(temp, temp_buf_2, string_hash);
476  }
477  while (temp.find("setClickKey(\"") != std::string::npos) {
478  std::string to_find = "setClickKey(\"";
479  std::string temp_buf;
480  for (size_t i = temp.find(to_find) + to_find.size(); temp.at(i) != '\"'; i++) {
481  temp_buf.push_back((temp.at(i)));
482  }
483  std::string string_hash = std::to_string(ST::hash_string(temp_buf));
484  std::string temp_buf_2 = "\"" + temp_buf + "\"";
485  ST::replace_string(temp, temp_buf_2, string_hash);
486  }
487  return temp;
488  }
489  return "Error\n";
490 }
491 
492 
493 //External lua API
494 
495 //TODO: Docs
496 //TODO: Test
497 extern "C" int saveGameLua(lua_State *L) {
498  auto arg = std::string(lua_tostring(L, 1));
499  gGame_managerLua->save_state(arg);
500  return 0;
501 }
502 
509 extern "C" int setDarknessLua(lua_State *L) {
510  auto arg = static_cast<uint8_t>(lua_tointeger(L, 1));
511  gMessage_busLua->send_msg(new message(SET_DARKNESS, arg));
512  return 0;
513 }
514 
515 extern "C" int enableLightingLua(lua_State *L) {
516  auto arg = static_cast<bool>(lua_toboolean(L, 1));
517  gMessage_busLua->send_msg(new message(ENABLE_LIGHTING, arg));
518  return 0;
519 }
520 
527 extern "C" int createLightLua(lua_State *L) {
528  auto origin_x = static_cast<int32_t>(lua_tointeger(L, 1));
529  auto origin_y = static_cast<int32_t>(lua_tointeger(L, 2));
530  auto radius = static_cast<uint16_t>(lua_tointeger(L, 3));
531  auto intensity = static_cast<uint16_t>(lua_tointeger(L, 4));
532  auto brightness = static_cast<uint16_t>(lua_tointeger(L, 5));
533  gGame_managerLua->get_level()->lights.emplace_back(origin_x, origin_y, radius, intensity, brightness);
534  return 0;
535 }
536 
543 extern "C" int setLightOriginXLua(lua_State *L) {
544  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
545  auto origin_x = static_cast<int32_t>(lua_tointeger(L, 2));
546  gGame_managerLua->get_level()->lights[id].origin_x = origin_x;
547  return 0;
548 }
549 
556 extern "C" int getLightOriginXLua(lua_State *L) {
557  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
558  lua_pushinteger(L, gGame_managerLua->get_level()->lights[id].origin_x);
559  return 1;
560 }
561 
568 extern "C" int setLightOriginYLua(lua_State *L) {
569  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
570  auto origin_y = static_cast<int32_t>(lua_tointeger(L, 2));
571  gGame_managerLua->get_level()->lights[id].origin_y = origin_y;
572  return 0;
573 }
574 
581 extern "C" int getLightOriginYLua(lua_State *L) {
582  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
583  lua_pushinteger(L, gGame_managerLua->get_level()->lights[id].origin_y);
584  return 1;
585 }
586 
593 extern "C" int getLightRadiusLua(lua_State *L) {
594  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
595  lua_pushinteger(L, gGame_managerLua->get_level()->lights[id].radius);
596  return 1;
597 }
598 
605 extern "C" int setLightRadiusLua(lua_State *L) {
606  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
607  auto radius = static_cast<uint16_t>(lua_tointeger(L, 2));
608  gGame_managerLua->get_level()->lights[id].radius = radius;
609  return 0;
610 }
611 
618 extern "C" int getLightIntensityLua(lua_State *L) {
619  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
620  lua_pushinteger(L, gGame_managerLua->get_level()->lights[id].intensity);
621  return 1;
622 }
623 
630 extern "C" int setLightIntensityLua(lua_State *L) {
631  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
632  auto intensity = static_cast<uint16_t>(lua_tointeger(L, 2));
633  gGame_managerLua->get_level()->lights[id].intensity = intensity;
634  return 0;
635 }
636 
643 extern "C" int getLightBrightnessLua(lua_State *L) {
644  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
645  lua_pushinteger(L, gGame_managerLua->get_level()->lights[id].brightness);
646  return 1;
647 }
648 
655 extern "C" int setLightBrightnessLua(lua_State *L) {
656  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
657  auto brightness = static_cast<uint16_t>(lua_tointeger(L, 2));
658  gGame_managerLua->get_level()->lights[id].brightness = brightness;
659  return 0;
660 }
661 
668 extern "C" int setLightStaticLua(lua_State *L) {
669  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
670  auto is_static = static_cast<bool>(lua_toboolean(L, 2));
671  gGame_managerLua->get_level()->lights[id].is_static = is_static;
672  return 0;
673 }
674 
681 extern "C" int isLightStaticLua(lua_State *L) {
682  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
683  lua_pushboolean(L, gGame_managerLua->get_level()->lights[id].is_static);
684  return 1;
685 }
686 
693 extern "C" int hashStringLua(lua_State *L) {
694  auto str = static_cast<std::string>(lua_tostring(L, 1));
695  lua_pushinteger(L, (lua_Integer) ST::hash_string(str));
696  return 1;
697 }
698 
705 extern "C" int setFullscreenLua(lua_State *L) {
706  auto arg = static_cast<bool>(lua_toboolean(L, 1));
707  gMessage_busLua->send_msg(new message(SET_FULLSCREEN, arg));
708  return 0;
709 }
710 
717 extern "C" int getFullscreenStatusLua(lua_State *L) {
718  lua_pushboolean(L, gGame_managerLua->fullscreen_status);
719  return 1;
720 }
721 
722 
723 //Text Object Lua bindings
724 
731 extern "C" int createTextObjectLua(lua_State *L) {
732  auto x = static_cast<int>(lua_tointeger(L, 1));
733  auto y = static_cast<int>(lua_tointeger(L, 2));
734  auto text_string = static_cast<std::string>(lua_tostring(L, 3));
735  auto font = static_cast<std::string>(lua_tostring(L, 4));
736  auto size = static_cast<uint8_t>(lua_tointeger(L, 5));
737  SDL_Color temp_color = {255, 255, 255, 255};
738  gGame_managerLua->get_level()->text_objects.emplace_back(x, y, temp_color, text_string,
739  ST::hash_string(font + " " + std::to_string(size)));
740  return 0;
741 }
742 
749 extern "C" int setTextObjectColorLua(lua_State *L) {
750  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
751  auto r = static_cast<uint8_t>(lua_tointeger(L, 2));
752  auto g = static_cast<uint8_t>(lua_tointeger(L, 3));
753  auto b = static_cast<uint8_t>(lua_tointeger(L, 4));
754  auto a = static_cast<uint8_t>(lua_tointeger(L, 5));
755  gGame_managerLua->get_level()->text_objects[id].color = {r, g, b, a};
756  return 0;
757 }
758 
765 extern "C" int setTextObjectTextLua(lua_State *L) {
766  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
767  auto text = static_cast<std::string>(lua_tostring(L, 2));
768  gGame_managerLua->get_level()->text_objects[id].text_string = text;
769  return 0;
770 }
771 
778 extern "C" int setTextObjectFontLua(lua_State *L) {
779  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
780  auto font = static_cast<std::string>(lua_tostring(L, 2));
781  gGame_managerLua->get_level()->text_objects[id].font = ST::hash_string(font);
782  return 0;
783 }
784 
791 extern "C" int setTextObjectXLua(lua_State *L) {
792  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
793  auto x = static_cast<int>(lua_tointeger(L, 2));
794  gGame_managerLua->get_level()->text_objects[id].x = x;
795  return 0;
796 }
797 
804 extern "C" int setTextObjectYLua(lua_State *L) {
805  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
806  auto y = static_cast<int>(lua_tointeger(L, 2));
807  gGame_managerLua->get_level()->text_objects[id].y = y;
808  return 0;
809 }
810 
817 extern "C" int setTextObjectVisibleLua(lua_State *L) {
818  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
819  auto arg = static_cast<bool>(lua_toboolean(L, 2));
820  gGame_managerLua->get_level()->text_objects[id].is_visible = arg;
821  return 0;
822 }
823 
824 
825 //Entity Lua bindings
826 
833 extern "C" int createEntityLua(lua_State *) {
834  gGame_managerLua->get_level()->entities.emplace_back();
835  return 0;
836 }
837 
844 extern "C" int setEntityXLua(lua_State *L) {
845  auto id = static_cast<unsigned long>(lua_tointeger(L, 1));
846  auto x = static_cast<int>(lua_tointeger(L, 2));
847  gGame_managerLua->get_level()->entities[id].x = x;
848  return 0;
849 }
850 
857 extern "C" int setEntityActiveLua(lua_State *L) {
858  auto id = static_cast<unsigned long>(lua_tointeger(L, 1));
859  auto arg = static_cast<bool>(lua_toboolean(L, 2));
860  gGame_managerLua->get_level()->entities[id].set_active(arg);
861  return 0;
862 }
863 
870 extern "C" int setEntityYLua(lua_State *L) {
871  auto id = static_cast<unsigned long>(lua_tointeger(L, 1));
872  auto y = static_cast<int>(lua_tointeger(L, 2));
873  gGame_managerLua->get_level()->entities[id].y = y;
874  return 0;
875 }
876 
883 extern "C" int setEntityVelocityXLua(lua_State *L) {
884  auto id = static_cast<unsigned long>(lua_tointeger(L, 1));
885  auto arg = static_cast<int8_t>(lua_tointeger(L, 2));
886  gGame_managerLua->get_level()->entities[id].velocity_x = arg;
887  return 0;
888 }
889 
896 extern "C" int setEntityVelocityYLua(lua_State *L) {
897  auto id = static_cast<unsigned long>(lua_tointeger(L, 1));
898  auto arg = static_cast<int8_t>(lua_tointeger(L, 2));
899  gGame_managerLua->get_level()->entities[id].velocity_y = arg;
900  return 0;
901 }
902 
909 extern "C" int getEntityVelocityXLua(lua_State *L) {
910  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
911  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].velocity_x);
912  return 1;
913 }
914 
921 extern "C" int getEntityVelocityYLua(lua_State *L) {
922  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
923  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].velocity_y);
924  return 1;
925 }
926 
933 extern "C" int setEntityStaticLua(lua_State *L) {
934  auto id = static_cast<unsigned long>(lua_tointeger(L, 1));
935  auto arg = static_cast<bool>(lua_toboolean(L, 2));
936  gGame_managerLua->get_level()->entities[id].set_static(arg);
937  return 0;
938 }
939 
946 extern "C" int getEntityXLua(lua_State *L) {
947  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
948  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].x);
949  return 1;
950 }
951 
958 extern "C" int getEntityYLua(lua_State *L) {
959  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
960  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].y);
961  return 1;
962 }
963 
964 //TEXTURE//
965 
972 extern "C" int getEntityTexWLua(lua_State *L) {
973  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
974  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].tex_w);
975  return 1;
976 }
977 
984 extern "C" int getEntityTexHLua(lua_State *L) {
985  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
986  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].tex_h);
987  return 1;
988 }
989 
996 extern "C" int setEntityTexWLua(lua_State *L) {
997  auto id = static_cast<unsigned long>(lua_tonumber(L, 1));
998  auto tex_w = static_cast<uint16_t>(lua_tonumber(L, 2));
999  gGame_managerLua->get_level()->entities[id].tex_w = tex_w;
1000  return 0;
1001 }
1002 
1009 extern "C" int setEntityTexHLua(lua_State *L) {
1010  auto id = static_cast<uint64_t>(lua_tonumber(L, 1));
1011  auto tex_h = static_cast<uint16_t>(lua_tonumber(L, 2));
1012  gGame_managerLua->get_level()->entities[id].tex_h = tex_h;
1013  return 0;
1014 }
1015 
1022 extern "C" int setEntityTextureScaleLua(lua_State *L) {
1023  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1024  auto scale_x = static_cast<float>(lua_tonumber(L, 2));
1025  auto scale_y = static_cast<float>(lua_tonumber(L, 3));
1026  auto ent = &gGame_managerLua->get_level()->entities[id];
1027  ent->tex_scale_x = scale_x;
1028  ent->tex_scale_y = scale_y;
1029  return 0;
1030 }
1031 
1038 extern "C" int setEntityVisibleLua(lua_State *L) {
1039  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1040  auto arg = static_cast<bool>(lua_toboolean(L, 2));
1041  gGame_managerLua->get_level()->entities[id].set_visible(arg);
1042  return 0;
1043 }
1044 
1051 extern "C" int setEntityTextureLua(lua_State *L) {
1052  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1053  std::string arg = static_cast<std::string>(lua_tostring(L, 2));
1054  gGame_managerLua->get_level()->entities[id].texture = ST::hash_string(arg);
1055  return 0;
1056 }
1057 
1058 
1059 //TODO: Is this needed?
1060 extern "C" int mouseOverTextureLua(lua_State *L) {
1061  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1062  int32_t mouse_x = gGame_managerLua->get_mouse_x();
1063  int32_t mouse_y = gGame_managerLua->get_mouse_y();
1064  ST::entity *object = &gGame_managerLua->get_level()->entities[id];
1065  int32_t object_x = object->x;
1066  int32_t object_y = object->y;
1067  lua_pushboolean(L, mouse_x < object->tex_w + object_x && mouse_x > object_x && mouse_y > object_y + object->tex_h &&
1068  mouse_y < object_y);
1069  return 1;
1070 }
1071 
1072 extern "C" int mouseOverLua(lua_State *L) {
1073  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1074  int32_t mouse_x = gGame_managerLua->get_mouse_x();
1075  int32_t mouse_y = gGame_managerLua->get_mouse_y();
1076  ST::entity *object = &gGame_managerLua->get_level()->entities[id];
1077  int32_t object_x = object->x;
1078  int32_t object_y = object->y;
1079  int32_t col_x_offset = object->get_col_x_offset();
1080  int32_t col_y_offset = object->get_col_y_offset();
1081  lua_pushboolean(L, mouse_x < object->get_col_x() + object_x + col_x_offset && mouse_x > object_x + col_x_offset
1082  && mouse_y > object_y + col_y_offset + object->get_col_y() && mouse_y < object_y + col_y_offset);
1083  return 1;
1084 }
1085 
1086 //PHYSICS//
1087 
1094 extern "C" int setEntityAffectedByPhysicsLua(lua_State *L) {
1095  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1096  auto arg = static_cast<bool>(lua_toboolean(L, 2));
1097  if (gGame_managerLua->get_level()->entities[id].is_affected_by_physics()) {
1098  if (!arg) {
1099  --gGame_managerLua->get_level()->physics_objects_count;
1100  }
1101  } else if (arg) {
1102  ++gGame_managerLua->get_level()->physics_objects_count;
1103  }
1104 
1105  gGame_managerLua->get_level()->entities[id].set_affected_by_physics(arg);
1106  return 0;
1107 }
1108 
1115 extern "C" int entityCollidesLua(lua_State *L) {
1116  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1117  auto id2 = static_cast<uint64_t>(lua_tointeger(L, 2));
1118  ST::level *temp = gGame_managerLua->get_level();
1119  lua_pushboolean(L, temp->entities[id].is_active() && temp->entities[id].collides(temp->entities[id2]));
1120  return 1;
1121 }
1122 
1129 extern "C" int setEntityCollisionBoxLua(lua_State *L) {
1130  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1131  auto offset_x = static_cast<int16_t>(lua_tonumber(L, 2));
1132  auto offset_y = static_cast<int16_t>(lua_tonumber(L, 3));
1133  auto x = static_cast<int16_t>(lua_tonumber(L, 4));
1134  auto y = static_cast<int16_t>(lua_tonumber(L, 5));
1135  gGame_managerLua->get_level()->entities[id].set_collision_box(offset_x, offset_y, x, y);
1136  return 0;
1137 }
1138 
1145 extern "C" int getEntityColXLua(lua_State *L) {
1146  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1147  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].get_col_x());
1148  return 1;
1149 }
1150 
1157 extern "C" int getEntityColYLua(lua_State *L) {
1158  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1159  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].get_col_y());
1160  return 1;
1161 }
1162 
1169 extern "C" int getEntityColXOffsetLua(lua_State *L) {
1170  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1171  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].get_col_x_offset());
1172  return 1;
1173 }
1174 
1181 extern "C" int getEntityColYOffsetLua(lua_State *L) {
1182  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1183  lua_pushnumber(L, gGame_managerLua->get_level()->entities[id].get_col_y_offset());
1184  return 1;
1185 }
1186 
1192 extern "C" int pausePhysicsLua(lua_State *) {
1193  gMessage_busLua->send_msg(new message(PAUSE_PHYSICS));
1194  return 0;
1195 }
1196 
1202 extern "C" int unpausePhysicsLua(lua_State *) {
1203  gMessage_busLua->send_msg(new message(UNPAUSE_PHYSICS));
1204  return 0;
1205 }
1206 
1207 //ANIMATION//
1208 
1215 extern "C" int setEntityAnimationLua(lua_State *L) {
1216  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1217  auto arg = static_cast<uint8_t>(lua_tointeger(L, 2));
1218  gGame_managerLua->get_level()->entities[id].animation = arg;
1219  return 0;
1220 }
1221 
1228 extern "C" int setEntityAnimationNumLua(lua_State *L) {
1229  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1230  auto arg = static_cast<uint8_t>(lua_tointeger(L, 2));
1231  gGame_managerLua->get_level()->entities[id].animation_num = arg;
1232  return 0;
1233 }
1234 
1241 extern "C" int setEntitySpriteNumLua(lua_State *L) {
1242  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1243  auto arg = static_cast<uint8_t>(lua_tointeger(L, 2));
1244  gGame_managerLua->get_level()->entities[id].sprite_num = arg;
1245  return 0;
1246 }
1247 
1248 
1249 //OTHER
1250 
1257 extern "C" int setGravityLua(lua_State *L) {
1258  auto arg = static_cast<int8_t>(lua_tointeger(L, 1));
1259  gGame_managerLua->gravity = arg;
1260  gMessage_busLua->send_msg(new message(SET_GRAVITY, arg));
1261  return 0;
1262 }
1263 
1270 extern "C" int getGravityLua(lua_State *L) {
1271  lua_pushinteger(L, gGame_managerLua->gravity);
1272  return 1;
1273 }
1274 
1281 extern "C" int setLevelFloorLua(lua_State *L) {
1282  auto arg = static_cast<int32_t>(lua_tointeger(L, 1));
1283  gMessage_busLua->send_msg(new message(SET_FLOOR, arg));
1284  return 0;
1285 }
1286 
1292 extern "C" int setVsyncLua(lua_State *L) {
1293  auto arg = static_cast<bool>(lua_toboolean(L, 1));
1294  gMessage_busLua->send_msg(new message(SET_VSYNC, arg));
1295  return 0;
1296 }
1297 
1304 extern "C" int getVsyncStateLua(lua_State *L) {
1305  lua_pushboolean(L, gGame_managerLua->vsync_flag);
1306  return 1;
1307 }
1308 
1315 extern "C" int setInternalResolutionLua(lua_State *L) {
1316  auto width = static_cast<int16_t>(lua_tointeger(L, 1));
1317  auto height = static_cast<int16_t>(lua_tointeger(L, 2));
1318  uint32_t width_height = width | (height << 16U);
1319  gMessage_busLua->send_msg(new message(SET_INTERNAL_RESOLUTION, width_height));
1320  return 0;
1321 }
1322 
1329 extern "C" int getInternalResolutionLua(lua_State *L) {
1330  lua_pushnumber(L, gGame_managerLua->get_internal_width());
1331  lua_pushnumber(L, gGame_managerLua->get_internal_height());
1332  return 2;
1333 }
1334 
1335 
1342 extern "C" int setWindowResolutionLua(lua_State *L) {
1343  auto width = static_cast<int16_t>(lua_tointeger(L, 1));
1344  auto height = static_cast<int16_t>(lua_tointeger(L, 2));
1345  uint32_t width_height = width | (height << 16U);
1346  gMessage_busLua->send_msg(new message(SET_WINDOW_RESOLUTION, width_height));
1347  return 0;
1348 }
1349 
1356 extern "C" int getWindowResolutionLua(lua_State *L) {
1357  lua_pushnumber(L, gGame_managerLua->get_window_width());
1358  lua_pushnumber(L, gGame_managerLua->get_window_height());
1359  return 2;
1360 }
1361 
1368 extern "C" int setBrightnessLua(lua_State *L) {
1369  auto arg = static_cast<float>(lua_tonumber(L, 1));
1370  gMessage_busLua->send_msg(new message(SET_WINDOW_BRIGHTNESS, make_data<>(arg)));
1371  return 0;
1372 }
1373 
1380 extern "C" int centerCameraLua(lua_State *L) {
1381  auto id = static_cast<uint64_t>(lua_tointeger(L, 1));
1382  gGame_managerLua->center_camera_on_entity(id);
1383  return 0;
1384 }
1385 
1392 extern "C" int setLevelSizeLua(lua_State *L) {
1393  auto x = static_cast<int32_t>(lua_tointeger(L, 1));
1394  auto y = static_cast<int32_t>(lua_tointeger(L, 2));
1395  gGame_managerLua->get_level()->camera.limitX2 = x;
1396  gGame_managerLua->get_level()->camera.limitY2 = y;
1397  return 0;
1398 }
1399 
1405 extern "C" int endGameLua(lua_State *) {
1406  gMessage_busLua->send_msg(new message(END_GAME));
1407  return 0;
1408 }
1409 
1416 extern "C" int startLevelLua(lua_State *L) {
1417  std::string level = static_cast<std::string>(lua_tostring(L, 1));
1418  gMessage_busLua->send_msg(new message(START_LEVEL, make_data<std::string>(level)));
1419  return 0;
1420 }
1421 
1428 extern "C" int reloadLevelLua(lua_State *L) {
1429  std::string level = static_cast<std::string>(lua_tostring(L, 1));
1430  gMessage_busLua->send_msg(new message(RELOAD_LEVEL, make_data<std::string>(level)));
1431  return 0;
1432 }
1433 
1440 extern "C" int load_levelLua(lua_State *L) {
1441  std::string arg = static_cast<std::string>(lua_tostring(L, 1));
1442  gMessage_busLua->send_msg(new message(LOAD_LEVEL, make_data<std::string>(arg)));
1443  return 0;
1444 }
1445 
1452 extern "C" int unload_levelLua(lua_State *L) {
1453  std::string level = static_cast<std::string>(lua_tostring(L, 1));
1454  gMessage_busLua->send_msg(new message(UNLOAD_LEVEL, make_data<std::string>(level)));
1455  return 0;
1456 }
1457 
1464 extern "C" int loadAssetLua(lua_State *L) {
1465  std::string path = static_cast<std::string>(lua_tostring(L, 1));
1466  gMessage_busLua->send_msg(new message(LOAD_ASSET, make_data<std::string>(path)));
1467  return 0;
1468 }
1469 
1476 extern "C" int unloadAssetLua(lua_State *L) {
1477  std::string path = static_cast<std::string>(lua_tostring(L, 1));
1478  gMessage_busLua->send_msg(new message(UNLOAD_ASSET, make_data<std::string>(path)));
1479  return 0;
1480 }
1481 
1488 extern "C" int delayLua(lua_State *L) {
1489  auto ms = static_cast<unsigned int>(lua_tointeger(L, 1));
1490  SDL_Delay(ms);
1491  return 0;
1492 }
1493 
1500 extern "C" int useLua(lua_State *L) {
1501  std::string arg = static_cast<std::string>(lua_tostring(L, 1));
1502  std::string temp = "game/levels/";
1503  temp = temp + gGame_managerLua->get_active_level();
1504  temp = temp + "/" + arg;
1505  gLua_backendLua->run_file(temp);
1506  return 0;
1507 }
1508 
1514 extern "C" int showMouseCursorLua(lua_State *) {
1515  gMessage_busLua->send_msg(new message(SHOW_MOUSE, true));
1516  return 0;
1517 }
1518 
1524 extern "C" int hideMouseCursorLua(lua_State *) {
1525  gMessage_busLua->send_msg(new message(SHOW_MOUSE, false));
1526  return 0;
1527 }
1528 
1535 extern "C" int setBackgroundLua(lua_State *L) {
1536  std::string texture = static_cast<std::string>(lua_tostring(L, 1));
1537  auto index = static_cast<uint16_t>(lua_tointeger(L, 2));
1538  auto parallax_speed = static_cast<uint8_t>(lua_tointeger(L, 3));
1539  gGame_managerLua->get_level()->background[index] = ST::hash_string(texture);
1540  gGame_managerLua->get_level()->parallax_speed[index] = parallax_speed;
1541  return 0;
1542 }
1543 
1550 extern "C" int setBackgroundColorLua(lua_State *L) {
1551  auto r = static_cast<uint8_t>(lua_tointeger(L, 1));
1552  auto g = static_cast<uint8_t>(lua_tointeger(L, 2));
1553  auto b = static_cast<uint8_t>(lua_tointeger(L, 3));
1554  auto a = static_cast<uint8_t>(lua_tointeger(L, 4));
1555 
1556  gGame_managerLua->get_level()->background_color = {r, g, b, a};
1557  return 0;
1558 }
1559 
1566 extern "C" int setOverlayLua(lua_State *L) {
1567  std::string arg = static_cast<std::string>(lua_tostring(L, 1));
1568  auto spriteNum = static_cast<uint8_t>(lua_tointeger(L, 2));
1569  gGame_managerLua->get_level()->overlay = ST::hash_string(arg);
1570  gGame_managerLua->get_level()->overlay_sprite_num = spriteNum;
1571  return 0;
1572 }
1573 
1574 //INPUT
1575 
1582 extern "C" int getMouseXLua(lua_State *L) {
1583  lua_pushnumber(L, gGame_managerLua->get_mouse_x());
1584  return 1;
1585 }
1586 
1593 extern "C" int getMouseYLua(lua_State *L) {
1594  lua_pushnumber(L, gGame_managerLua->get_mouse_y());
1595  return 1;
1596 }
1597 
1598 //TODO: Docs
1599 extern "C" int rightTriggerLua(lua_State *L) {
1600  lua_pushnumber(L, gGame_managerLua->get_right_trigger());
1601  return 1;
1602 }
1603 
1604 extern "C" int leftTriggerLua(lua_State *L) {
1605  lua_pushnumber(L, gGame_managerLua->get_left_trigger());
1606  return 1;
1607 }
1608 
1609 extern "C" int rightStickHorizontalLua(lua_State *L) {
1610  lua_pushnumber(L, gGame_managerLua->get_right_stick_horizontal());
1611  return 1;
1612 }
1613 
1614 extern "C" int leftStickHorizontalLua(lua_State *L) {
1615  lua_pushnumber(L, gGame_managerLua->get_left_stick_horizontal());
1616  return 1;
1617 }
1618 
1619 extern "C" int rightStickVerticalLua(lua_State *L) {
1620  lua_pushnumber(L, gGame_managerLua->get_right_stick_vertical());
1621  return 1;
1622 }
1623 
1624 extern "C" int leftStickVerticalLua(lua_State *L) {
1625  lua_pushnumber(L, gGame_managerLua->get_left_stick_vertical());
1626  return 1;
1627 }
1628 
1629 extern "C" int controllerRumbleLua(lua_State *L) {
1630  auto strength = static_cast<float>(lua_tonumber(L, 1));
1631  auto duration = static_cast<uint16_t>(lua_tonumber(L, 2));
1632  auto msg = new message(CONTROLLER_RUMBLE, *reinterpret_cast<uint32_t *>(&strength));
1633  msg->base_data1 = duration;
1634  gMessage_busLua->send_msg(msg);
1635  return 0;
1636 }
1637 
1644 extern "C" int keyHeldLua(lua_State *L) {
1645  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1646  lua_pushboolean(L, gGame_managerLua->key_held(arg));
1647  return 1;
1648 }
1649 
1656 extern "C" int keyPressedLua(lua_State *L) {
1657  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1658  lua_pushboolean(L, gGame_managerLua->key_pressed(arg));
1659  return 1;
1660 }
1661 
1668 extern "C" int keyReleasedLua(lua_State *L) {
1669  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1670  lua_pushboolean(L, gGame_managerLua->key_released(arg));
1671  return 1;
1672 }
1673 
1674 extern "C" int setLeftStickVerticalThresholdLua(lua_State *L) {
1675  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1676  auto msg = new message(SET_LEFT_JOYSTICK_VERTICAL_THRESHOLD, static_cast<uint32_t>(arg));
1677  gMessage_busLua->send_msg(msg);
1678  return 0;
1679 }
1680 
1681 extern "C" int setLeftStickHorizontalThresholdLua(lua_State *L) {
1682  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1683  auto msg = new message(SET_LEFT_JOYSTICK_HORIZONTAL_THRESHOLD, static_cast<uint32_t>(arg));
1684  gMessage_busLua->send_msg(msg);
1685  return 0;
1686 }
1687 
1688 extern "C" int setRightStickVerticalThresholdLua(lua_State *L) {
1689  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1690  auto msg = new message(SET_RIGHT_JOYSTICK_VERTICAL_THRESHOLD, static_cast<uint32_t>(arg));
1691  gMessage_busLua->send_msg(msg);
1692  return 0;
1693 }
1694 
1695 extern "C" int setRightStickHorizontalThresholdLua(lua_State *L) {
1696  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1697  auto msg = new message(SET_RIGHT_JOYSTICK_HORIZONTAL_THRESHOLD, static_cast<uint32_t>(arg));
1698  gMessage_busLua->send_msg(msg);
1699  return 0;
1700 }
1701 
1702 extern "C" int setRightTriggerThresholdLua(lua_State *L) {
1703  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1704  auto msg = new message(SET_RIGHT_TRIGGER_THRESHOLD, static_cast<uint32_t>(arg));
1705  gMessage_busLua->send_msg(msg);
1706  return 0;
1707 }
1708 
1709 extern "C" int setLeftTriggerThresholdLua(lua_State *L) {
1710  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1711  auto msg = new message(SET_LEFT_TRIGGER_THRESHOLD, static_cast<uint32_t>(arg));
1712  gMessage_busLua->send_msg(msg);
1713  return 0;
1714 }
1715 
1716 
1717 //AUDIO
1718 
1724 extern "C" int setAudioEnabledLua(lua_State *L) {
1725  auto arg = static_cast<bool>(lua_toboolean(L, 1));
1726  gMessage_busLua->send_msg(new message(SET_AUDIO_ENABLED, arg));
1727  return 0;
1728 }
1729 
1730 
1737 extern "C" int playMusicLua(lua_State *L) {
1738  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1739  auto volume = static_cast<uint8_t>(lua_tointeger(L, 2));
1740  auto loops = static_cast<int8_t>(lua_tointeger(L, 3));
1741  uint32_t result = arg | (volume << 16U) | (static_cast<uint8_t>(loops) << 24U);
1742  gMessage_busLua->send_msg(new message(PLAY_MUSIC, result));
1743  return 0;
1744 }
1745 
1752 extern "C" int playSoundLua(lua_State *L) {
1753  auto arg = static_cast<uint16_t>(lua_tointeger(L, 1));
1754  auto volume = static_cast<uint8_t>(lua_tointeger(L, 2));
1755  auto loops = static_cast<int8_t>(lua_tointeger(L, 3));
1756  uint32_t result = arg | (volume << 16U) | (static_cast<uint8_t>(loops) << 24U);
1757  gMessage_busLua->send_msg(new message(PLAY_SOUND, result));
1758  return 0;
1759 }
1760 
1766 extern "C" int stopMusicLua(lua_State *) {
1767  auto msg_temp = new message(STOP_MUSIC);
1768  gMessage_busLua->send_msg(msg_temp);
1769  return 0;
1770 }
1771 
1777 extern "C" int stopAllSoundsLua(lua_State *) {
1778  gMessage_busLua->send_msg(new message(STOP_ALL_SOUNDS));
1779  return 0;
1780 }
1781 
1782 extern "C" int isAudioEnabledLua(lua_State *L) {
1783  lua_pushboolean(L, gGame_managerLua->audio_enabled);
1784  return 1;
1785 }
1786 
1793 extern "C" int setMusicVolumeLua(lua_State *L) {
1794  auto arg = static_cast<uint8_t>(lua_tointeger(L, 1));
1795  gMessage_busLua->send_msg(new message(SET_MUSIC_VOLUME, arg));
1796  return 0;
1797 }
1798 
1805 extern "C" int setSoundsVolumeLua(lua_State *L) {
1806  auto arg = static_cast<uint8_t>(lua_tointeger(L, 1));
1807  gMessage_busLua->send_msg(new message(SET_SOUNDS_VOLUME, arg));
1808  return 0;
1809 }
1810 
1817 extern "C" int getSoundsVolumeLua(lua_State *L) {
1818  lua_pushnumber(L, gGame_managerLua->sounds_volume_level);
1819  return 1;
1820 }
1821 
1828 extern "C" int getMusicVolumeLua(lua_State *L) {
1829  lua_pushnumber(L, gGame_managerLua->music_volume_level);
1830  return 1;
1831 }
1832 
1838 extern "C" int pauseMusicLua(lua_State *) {
1839  gMessage_busLua->send_msg(new message(PAUSE_MUSIC));
1840  return 0;
1841 }
1842 
1849 extern "C" int showCollisionsLua(lua_State *L) {
1850  auto arg = static_cast<bool>(lua_toboolean(L, 1));
1851  gMessage_busLua->send_msg(new message(SHOW_COLLISIONS, arg));
1852  return 0;
1853 }
1854 
1862 extern "C" int logLua(lua_State *L) {
1863  auto type = static_cast<uint8_t>(lua_tointeger(L, 1));
1864  auto arg = static_cast<std::string>(lua_tostring(L, 2));
1865  if (type == 1) {
1866  gMessage_busLua->send_msg(new message(LOG_ERROR, make_data<std::string>(arg)));
1867  } else if (type == 2) {
1868  gMessage_busLua->send_msg(new message(LOG_SUCCESS, make_data<std::string>(arg)));
1869  } else if (type == 4) {
1870  gMessage_busLua->send_msg(new message(LOG_INFO, make_data<std::string>(arg)));
1871  }
1872  return 0;
1873 }
1874 
1881 extern "C" int showFpsLua(lua_State *L) {
1882  auto arg = static_cast<bool>(lua_toboolean(L, 1));
1883  gMessage_busLua->send_msg(new message(SHOW_FPS, arg));
1884  return 0;
1885 }
1886 
1893 extern "C" int showMetricsLua(lua_State *L) {
1894  auto arg = static_cast<bool>(lua_toboolean(L, 1));
1895  gMessage_busLua->send_msg(new message(SHOW_METRICS, arg));
1896  return 0;
1897 }
1898 
1903 extern "C" int consoleClearLua(lua_State *) {
1904  gMessage_busLua->send_msg(new message(CONSOLE_CLEAR));
1905  return 0;
1906 }
This class represents all static or dynamic objects in the game (excluding text, see ST::text)
Definition: entity.hpp:24
int32_t get_col_y() const
Definition: entity.hpp:170
This object contains all the data for a level and provides functions for loading and unloading a leve...
Definition: level.hpp:29
This class is responsible for managing all levels and the lua backend, it is the heart of the engine.
bool key_released(uint16_t arg) const
uint16_t get_internal_width() const
void center_camera_on_entity(uint64_t id)
uint16_t get_window_height() const
int16_t get_left_stick_vertical() const
bool key_held(uint16_t arg) const
int16_t get_left_trigger() const
int32_t get_mouse_x() const
int16_t get_right_stick_vertical() const
int16_t get_left_stick_horizontal() const
ST::level * get_level() const
uint16_t get_window_width() const
uint16_t get_internal_height() const
int32_t get_mouse_y() const
bool key_pressed(uint16_t arg) const
int16_t get_right_trigger() const
int16_t get_right_stick_horizontal() const
std::string get_active_level() const
This class handles all interaction with lua.
Definition: lua_backend.hpp:31
void set_global(const std::string &arg)
int initialize(message_bus *msg_bus, game_manager *game_mngr)
Definition: lua_backend.cpp:41
int8_t load_file(const std::string &)
void run_global(const std::string &arg)
int8_t run_file(const std::string &file)
int8_t run_script(const std::string &script)
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
void send_msg(message *msg)
Definition: message_bus.cpp:19
A message object passed around in the message bus. Holds anything created with make_data<>().
Definition: message.hpp:21