ScriptManagerBinds.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Q-Gears
  3. * Copyright (C) 2022 Q-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <iostream>
  19. #include "Console.h"
  20. #include "Logger.h"
  21. #include "Entity.h"
  22. #include "EntityManager.h"
  23. #include "Timer.h"
  24. #include "UiManager.h"
  25. #include "UiWidget.h"
  26. #include "XmlMapFile.h"
  27. #include "XmlMapsFile.h"
  28. #include "DialogsManager.h"
  29. #include "modules/worldmap/WorldMapModule.h"
  30. /**
  31. * Prints to the game console.
  32. *
  33. * @param text[in] The text to be printed.
  34. */
  35. void ScriptPrint(const char* text){
  36. Console::getSingleton().AddTextToOutput(text);
  37. }
  38. /**
  39. * Changes the current map.
  40. *
  41. * Reads the data for the next map, initializes all it's entities, scripts, etc
  42. * and loads it.
  43. *
  44. * @param text The new map name.
  45. */
  46. void ScriptMap(const char* text){
  47. EntityManager::getSingleton().Clear();
  48. XmlMapsFile xml("./data/maps.xml");
  49. Ogre::String file_name = xml.GetMapFileNameByName(text);
  50. XmlMapFile xml_map("./data/" + file_name);
  51. xml_map.LoadMap();
  52. }
  53. /**
  54. * Executes a script in the game console.
  55. *
  56. * It should not be related to the game itself. The script output will be
  57. * written to the game console.
  58. *
  59. * @param text[in] The script command to execute.
  60. */
  61. void ScriptConsole(const char* text){
  62. Console::getSingleton().ExecuteCommand(text);
  63. }
  64. /**
  65. * Initializes the lua command binds
  66. *
  67. * It relates the command available in the field maps to C++ functions
  68. */
  69. void ScriptManager::InitBinds(){
  70. std::cout << "[INIT BINDS] Begin " << std::endl;
  71. // Global functions.
  72. luabind::module(m_LuaState)[
  73. luabind::def("print", (void(*)(const char*)) &ScriptPrint),
  74. luabind::def("map", (void(*)(const char*)) &ScriptMap),
  75. luabind::def("console", (void(*)(const char*)) &ScriptConsole)
  76. ];
  77. std::cout << "[INIT BINDS] 1 " << std::endl;
  78. // Individual entity commands.
  79. luabind::module(m_LuaState)[
  80. luabind::class_< Entity >("Entity")
  81. .def("set_position", (void(Entity::*)(const float, const float, const float)) &Entity::ScriptSetPosition)
  82. .def("get_position", (void(Entity::*)()) &Entity::ScriptGetPosition) // return 3 values internaly
  83. .def("set_rotation", (void(Entity::*)(const float)) &Entity::ScriptSetRotation)
  84. .def("get_rotation", (float(Entity::*)()) &Entity::ScriptGetRotation)
  85. .def("set_solid_radius", (void(Entity::*)(const float)) &Entity::SetSolidRadius)
  86. .def("get_solid_radius", (float(Entity::*)()) &Entity::GetSolidRadius)
  87. .def("set_solid", (void(Entity::*)(const bool)) &Entity::SetSolid)
  88. .def("is_solid", (bool(Entity::*)()) &Entity::IsSolid)
  89. .def("set_talk_radius", (void(Entity::*)(const float)) &Entity::SetTalkRadius)
  90. .def("get_talk_radius", (float(Entity::*)()) &Entity::GetTalkRadius)
  91. // Some old test script use this, its just an alias for SetTalkable
  92. .def("set_interactable", (void(Entity::*)(const bool)) &Entity::SetTalkable)
  93. .def("set_talkable", (void(Entity::*)(const bool)) &Entity::SetTalkable)
  94. .def("is_talkable", (bool(Entity::*)()) &Entity::IsTalkable)
  95. .def("set_visible", (void(Entity::*)(const bool)) &Entity::SetVisible)
  96. .def("is_visible", (bool(Entity::*)()) &Entity::IsVisible)
  97. .def("set_move_auto_speed", (void(Entity::*)(const float)) &Entity::SetMoveAutoSpeed)
  98. .def("get_move_auto_speed", (float(Entity::*)()) &Entity::GetMoveAutoSpeed)
  99. .def("get_move_triangle_id", (int(Entity::*)()) &Entity::GetMoveTriangleId)
  100. .def("move_auto_rotation", (void(Entity::*)(const bool)) &Entity::SetMoveAutoRotation)
  101. .def("move_auto_animation", (void(Entity::*)(const bool)) &Entity::SetMoveAutoAnimation)
  102. .def("move_to_position", (void(Entity::*)(const float, const float)) &Entity::ScriptMoveToPosition)
  103. .def("move_to_entity", (void(Entity::*)(Entity*)) &Entity::ScriptMoveToEntity)
  104. .def("move_sync", (int(Entity::*)()) &Entity::ScriptMoveSync, luabind::yield)
  105. .def("linear_to_position", (void(Entity::*)(const float, const float, const float, const LinearMovement, const char*)) &Entity::ScriptLinearToPosition)
  106. .def("linear_sync", (int(Entity::*)()) &Entity::ScriptLinearSync, luabind::yield)
  107. .def("jump_to_position", (void(Entity::*)(const float, const float, const float, const float))&Entity::ScriptJumpToPosition)
  108. .def("jump_sync", (int(Entity::*)()) &Entity::ScriptJumpSync, luabind::yield)
  109. .def("offset_to_position", (void(Entity::*)(const float, const float, const float, const ActionType, const float)) &Entity::ScriptOffsetToPosition)
  110. .def("offset_sync", (int(Entity::*)()) &Entity::ScriptOffsetSync, luabind::yield)
  111. .def("turn_to_entity", (void(Entity::*)(Entity*, const TurnDirection, const float))&Entity::ScriptTurnToEntity)
  112. .def("turn_to_direction", (void(Entity::*)(const float, const TurnDirection, const ActionType, const float))&Entity::ScriptTurnToDirection)
  113. .def("turn_sync", (int(Entity::*)()) &Entity::ScriptTurnSync, luabind::yield)
  114. .def("set_animation_speed", (void(Entity::*)(const float)) &Entity::ScriptSetAnimationSpeed)
  115. .def("play_animation", (void(Entity::*)(const char*)) &Entity::ScriptPlayAnimation)
  116. .def("play_animation_stop", (void(Entity::*)(const char*)) &Entity::ScriptPlayAnimationStop)
  117. .def("play_animation", (void(Entity::*)(const char*, const float, const float)) &Entity::ScriptPlayAnimation)
  118. .def("play_animation_stop", (void(Entity::*)(const char*, const float, const float)) &Entity::ScriptPlayAnimationStop)
  119. .def("set_default_animation", (void(Entity::*)(const char*)) &Entity::ScriptSetDefaultAnimation)
  120. .def("animation_sync", (int(Entity::*)()) &Entity::ScriptAnimationSync, luabind::yield)
  121. .enum_("constants")[
  122. luabind::value("NONE", AT_NONE),
  123. luabind::value("LINEAR", AT_LINEAR),
  124. luabind::value("SMOOTH", AT_SMOOTH),
  125. luabind::value("CLOCKWISE", TD_CLOCKWISE),
  126. luabind::value("ANTICLOCKWISE", TD_ANTICLOCKWISE),
  127. luabind::value("CLOSEST", TD_CLOSEST),
  128. luabind::value("UP_TO_DOWN", LM_UP_TO_DOWN),
  129. luabind::value("DOWN_TO_UP", LM_DOWN_TO_UP),
  130. luabind::value("LEFT_TO_RIGHT", LM_LEFT_TO_RIGHT),
  131. luabind::value("RIGHT_TO_LEFT", LM_RIGHT_TO_LEFT)
  132. ]
  133. ];
  134. std::cout << "[INIT BINDS] 1 " << std::endl;
  135. // Field commands.
  136. // TODO: Duplicated in group EntityManager, this can probably be deleted
  137. luabind::module(m_LuaState)[
  138. luabind::class_< EntityManager >("Field")
  139. .def("random_encounters_on", (float(EntityManager::*)(bool)) &EntityManager::SetRandomEncounters)
  140. .def("start_battle", (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle)
  141. // TODO: Run? Set battle flags
  142. .def("battle_run", (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle)
  143. ];
  144. // Entity individual point commands
  145. luabind::module(m_LuaState)[
  146. luabind::class_< EntityPoint >("EntityPoint")
  147. .def("get_position", (void(EntityPoint::*)()) &EntityPoint::ScriptGetPosition) // return 3 values internaly
  148. .def("get_rotation", (float(EntityPoint::*)()) &EntityPoint::ScriptGetRotation)
  149. ];
  150. // Commands for the entity manager, not related to any particular entity.
  151. luabind::module(m_LuaState)[
  152. luabind::class_< EntityManager >("EntityManager")
  153. .def("set_paused", (void(EntityManager::*)(const bool)) &EntityManager::ScriptSetPaused)
  154. .def("add_entity", (void(EntityManager::*)(const char*, const char*, const float, const float, const float, const float)) &EntityManager::ScriptAddEntity)
  155. .def("add_entity_script", (void(EntityManager::*)(const char*)) &EntityManager::ScriptAddEntityScript)
  156. .def("get_entity", (Entity*(EntityManager::*)(const char*)) &EntityManager::ScriptGetEntity)
  157. .def("get_entity_point", (EntityPoint*(EntityManager::*)(const char*)) &EntityManager::ScriptGetEntityPoint)
  158. .def("set_player_entity", (void(EntityManager::*)(const char*)) &EntityManager::ScriptSetPlayerEntity)
  159. .def("unset_player_entity", (void(EntityManager::*)()) &EntityManager::ScriptUnsetPlayerEntity)
  160. .def("player_lock", (void(EntityManager::*)(const bool)) &EntityManager::ScriptPlayerLock)
  161. .def("random_encounters_on", (float(EntityManager::*)(bool)) &EntityManager::SetRandomEncounters)
  162. .def("start_battle", (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle)
  163. // TODO: Run? Set battle flags
  164. .def("battle_run", (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle)
  165. ];
  166. // 2D background and camera commands
  167. luabind::module(m_LuaState)[
  168. luabind::class_< Background2D >("Background2D")
  169. .def("autoscroll_to_entity", (void(Background2D::*)(Entity*)) &Background2D::ScriptAutoScrollToEntity)
  170. .def("scroll_to_position", (void(Background2D::*)(const float, const float, const Background2D::ScrollType, const float)) &Background2D::ScriptScrollToPosition)
  171. .def("scroll_sync", (int(Background2D::*)()) &Background2D::ScriptScrollSync, luabind::yield)
  172. .def("play_animation_looped", (void(Background2D::*)(const char*)) &Background2D::ScriptPlayAnimationLooped)
  173. .def("play_animation_once", (void(Background2D::*)(const char*)) &Background2D::ScriptPlayAnimationOnce)
  174. .def("animation_sync", (int(Background2D::*)(const char*)) &Background2D::ScriptAnimationSync, luabind::yield)
  175. .enum_("constants")[
  176. luabind::value("NONE", AT_NONE),
  177. luabind::value("LINEAR", AT_LINEAR),
  178. luabind::value("SMOOTH", AT_SMOOTH)
  179. ]
  180. ];
  181. // Walkmesh commands
  182. luabind::module(m_LuaState)[
  183. luabind::class_< Walkmesh >("Walkmesh")
  184. .def("lock_walkmesh", (void(Walkmesh ::*)(unsigned int, bool)) &Walkmesh ::LockWalkmesh)
  185. .def("is_locked", (bool(Walkmesh ::*)(unsigned int)) &Walkmesh ::IsLocked)
  186. ];
  187. // Dialog commands
  188. luabind::module(m_LuaState)[
  189. luabind::class_< DialogsManager >("Dialog")
  190. .def("dialog_open", (void(DialogsManager::*)(const char*, int, int, int, int)) &DialogsManager::OpenDialog)
  191. .def("dialog_set_text", (void(DialogsManager::*)(const char*, const char*)) &DialogsManager::SetText)
  192. .def("dialog_wait_for_close", (int(DialogsManager::*)(const char*)) &DialogsManager::Sync, luabind::yield)
  193. .def("dialog_close", (void(DialogsManager::*)(const char*)) &DialogsManager::Hide)
  194. .def("set_variable", (void(DialogsManager::*)(const char*, const char*, const char*)) &DialogsManager::SetVariable)
  195. .def("set_clickable", (void(DialogsManager::*)(const char*, const bool)) &DialogsManager::SetClickable)
  196. .def("set_cursor", (void(DialogsManager::*)(const char*, const int, const int)) &DialogsManager::SetCursor)
  197. .def("get_cursor", (int(DialogsManager::*)(const char*)) &DialogsManager::GetCursor)
  198. .enum_("constants")[
  199. luabind::value("SOLID", MSL_SOLID),
  200. luabind::value("TRANSPARENT", MSL_TRANSPARENT),
  201. luabind::value("NONE", MSL_NONE)
  202. ]
  203. ];
  204. // UI widget commands
  205. luabind::module(m_LuaState)[
  206. luabind::class_< UiWidget >("UiWidget")
  207. .def("set_visible", (void(UiWidget::*)(const bool)) &UiWidget::SetVisible)
  208. .def("is_visible", (bool(UiWidget::*)()) &UiWidget::IsVisible)
  209. .def("play_animation", (void(UiWidget::*)(const char*)) &UiWidget::ScriptPlayAnimation)
  210. .def("play_animation_stop", (void(UiWidget::*)(const char*)) &UiWidget::ScriptPlayAnimationStop)
  211. .def("play_animation", (void(UiWidget::*)(const char*, const float, const float)) &UiWidget::ScriptPlayAnimation)
  212. .def("play_animation_stop", (void(UiWidget::*)(const char*, const float, const float)) &UiWidget::ScriptPlayAnimationStop)
  213. .def("set_default_animation", (void(UiWidget::*)(const char*)) &UiWidget::ScriptSetDefaultAnimation)
  214. .def("animation_sync", (int(UiWidget::*)()) &UiWidget::ScriptAnimationSync, luabind::yield)
  215. .def("set_colour", (void(UiWidget::*)(const float, const float, const float)) &UiWidget::SetColour)
  216. .def("set_alpha", (void(UiWidget::*)(const float)) &UiWidget::SetAlpha)
  217. .def("set_x", (void(UiWidget::*)(const float, const float)) &UiWidget::SetX)
  218. .def("set_y", (void(UiWidget::*)(const float, const float)) &UiWidget::SetY)
  219. .def("set_z", (void(UiWidget::*)(const float)) &UiWidget::SetZ)
  220. .def("set_width", (void(UiWidget::*)(const float, const float)) &UiWidget::SetWidth)
  221. .def("set_height", (void(UiWidget::*)(const float, const float)) &UiWidget::SetHeight)
  222. ];
  223. // UI manager commands. Use to get a specific widget.
  224. luabind::module(m_LuaState)[
  225. luabind::class_< UiManager >("UiManager")
  226. .def("get_widget", (UiWidget*(UiManager::*)(const char*)) &UiManager::ScriptGetWidget)
  227. ];
  228. // Timer command. To show a in-game timer.
  229. luabind::module(m_LuaState)[
  230. luabind::class_< Timer >("Timer")
  231. .def("get_game_time_total", (float(Timer::*)()) &Timer::GetGameTimeTotal)
  232. .def("set_timer", (float(Timer::*)(const float)) &Timer::SetGameTimer)
  233. .def("get_timer", (int(Timer::*)()) &Timer::GetGameTimer)
  234. ];
  235. // Commands that control script execution
  236. luabind::module(m_LuaState)[
  237. luabind::class_< ScriptManager >("Script")
  238. .def("wait", (int(ScriptManager::*)(const float)) &ScriptManager::ScriptWait, luabind::yield)
  239. .def("request", (void(ScriptManager::*)(const ScriptManager::Type, const char*, const char*, const int)) &ScriptManager::ScriptRequest)
  240. .def("request_start_sync", (int(ScriptManager::*)(const ScriptManager::Type, const char*, const char*, const int)) &ScriptManager::ScriptRequestStartSync, luabind::yield)
  241. .def("request_end_sync", (int(ScriptManager::*)(const ScriptManager::Type, const char*, const char*, const int)) &ScriptManager::ScriptRequestEndSync, luabind::yield)
  242. .enum_("constants")[
  243. luabind::value("SYSTEM", ScriptManager::SYSTEM),
  244. luabind::value("ENTITY", ScriptManager::ENTITY),
  245. luabind::value("UI", ScriptManager::UI)
  246. ]
  247. ];
  248. // Commnds to initiate modules
  249. luabind::module(m_LuaState)[
  250. luabind::class_< QGears::WorldMapModule >("world_map_module")
  251. .def("init", (void(QGears::WorldMapModule::*)()) &QGears::WorldMapModule::Init)
  252. ];
  253. std::cout << "[INIT BINDS] REGISTER " << std::endl;
  254. // Register all command handlers
  255. auto a = boost::ref(*(EntityManager::getSingletonPtr()));
  256. std::cout << "[INIT BINDS] REGISTER 0" << std::endl;
  257. luabind::globals(m_LuaState);
  258. std::cout << "[INIT BINDS] REGISTER 0.5" << std::endl;
  259. /*auto b = luabind::globals(m_LuaState)["entity_manager"];
  260. if (b == NULL){
  261. std::cout << "[INIT BINDS] B NULL" << std::endl;
  262. }*/
  263. std::cout << "[INIT BINDS] REGISTER 0.6" << std::endl;
  264. luabind::globals(m_LuaState)["entity_manager"] = boost::ref(*(EntityManager::getSingletonPtr()));
  265. std::cout << "[INIT BINDS] REGISTER 1" << std::endl;
  266. luabind::globals(m_LuaState)["background2d"] = boost::ref(*(EntityManager::getSingletonPtr()->GetBackground2D()));
  267. luabind::globals(m_LuaState)["walkmesh"] = boost::ref(*(EntityManager::getSingletonPtr()->GetWalkmesh()));
  268. luabind::globals(m_LuaState)["dialog"] = boost::ref(*(DialogsManager::getSingletonPtr()));
  269. luabind::globals(m_LuaState)["ui_manager"] = boost::ref(*(UiManager::getSingletonPtr()));
  270. luabind::globals(m_LuaState)["world_map_module"] = boost::ref(*(QGears::WorldMapModule::getSingletonPtr()));
  271. luabind::globals(m_LuaState)["timer"] = boost::ref(*(Timer::getSingletonPtr()));
  272. luabind::globals(m_LuaState)["script"] = boost::ref(*this);
  273. std::cout << "[INIT BINDS] END " << std::endl;
  274. }