ScriptManagerBinds.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #include <iostream>
  16. #include "Console.h"
  17. #include "Logger.h"
  18. #include "Entity.h"
  19. #include "EntityManager.h"
  20. #include "Timer.h"
  21. #include "UiManager.h"
  22. #include "UiWidget.h"
  23. #include "XmlMapFile.h"
  24. #include "XmlMapsFile.h"
  25. #include "DialogsManager.h"
  26. #include "modules/worldmap/WorldMapModule.h"
  27. /**
  28. * Prints to the game console.
  29. *
  30. * @param text[in] The text to be printed.
  31. */
  32. void ScriptPrint(const char* text){
  33. Console::getSingleton().AddTextToOutput(text);
  34. }
  35. /**
  36. * Changes the current map.
  37. *
  38. * Reads the data for the next map, initializes all it's entities, scripts, etc
  39. * and loads it.
  40. *
  41. * @param text[in] The new map name.
  42. */
  43. void ScriptMap(const char* text){
  44. EntityManager::getSingleton().Clear();
  45. XmlMapsFile xml("./data/maps.xml");
  46. Ogre::String file_name = xml.GetMapFileNameByName(text);
  47. XmlMapFile xml_map("./data/" + file_name);
  48. xml_map.LoadMap();
  49. }
  50. /**
  51. * Executes a script in the game console.
  52. *
  53. * It should not be related to the game itself. The script output will be
  54. * written to the game console.
  55. *
  56. * @param text[in] The script command to execute.
  57. */
  58. void ScriptConsole(const char* text){
  59. Console::getSingleton().ExecuteCommand(text);
  60. }
  61. void ScriptManager::InitBinds(){
  62. std::cout << "[INIT BINDS] Begin " << std::endl;
  63. // Global functions.
  64. luabind::module(lua_state_)[
  65. luabind::def("print", (void(*)(const char*)) &ScriptPrint),
  66. luabind::def("map", (void(*)(const char*)) &ScriptMap),
  67. luabind::def("console", (void(*)(const char*)) &ScriptConsole)
  68. ];
  69. // Individual entity commands.
  70. luabind::module(lua_state_)[
  71. luabind::class_<Entity>("Entity")
  72. .def(
  73. "set_position",
  74. (void(Entity::*)(const float, const float, const float))
  75. &Entity::ScriptSetPosition
  76. )
  77. // Internally returns 3 values:
  78. .def("get_position", (void(Entity::*)()) &Entity::ScriptGetPosition)
  79. .def(
  80. "set_rotation",
  81. (void(Entity::*)(const float)) &Entity::ScriptSetRotation
  82. )
  83. .def("get_rotation", (float(Entity::*)()) &Entity::ScriptGetRotation)
  84. .def(
  85. "set_solid_radius",
  86. (void(Entity::*)(const float)) &Entity::SetSolidRadius
  87. )
  88. .def("get_solid_radius", (float(Entity::*)()) &Entity::GetSolidRadius)
  89. .def("set_solid", (void(Entity::*)(const bool)) &Entity::SetSolid)
  90. .def("is_solid", (bool(Entity::*)()) &Entity::IsSolid)
  91. .def(
  92. "set_talk_radius",
  93. (void(Entity::*)(const float)) &Entity::SetTalkRadius
  94. )
  95. .def("get_talk_radius", (float(Entity::*)()) &Entity::GetTalkRadius)
  96. // Some old test script use this, its just an alias for SetTalkable:
  97. .def(
  98. "set_interactable",
  99. (void(Entity::*)(const bool)) &Entity::SetTalkable
  100. )
  101. .def(
  102. "set_talkable",
  103. (void(Entity::*)(const bool)) &Entity::SetTalkable
  104. )
  105. .def("is_talkable", (bool(Entity::*)()) &Entity::IsTalkable)
  106. .def("set_visible", (void(Entity::*)(const bool)) &Entity::SetVisible)
  107. .def("is_visible", (bool(Entity::*)()) &Entity::IsVisible)
  108. .def(
  109. "set_move_auto_speed",
  110. (void(Entity::*)(const float)) &Entity::SetMoveAutoSpeed
  111. )
  112. .def(
  113. "get_move_auto_speed",
  114. (float(Entity::*)()) &Entity::GetMoveAutoSpeed
  115. )
  116. .def(
  117. "get_move_triangle_id",
  118. (int(Entity::*)()) &Entity::GetMoveTriangleId
  119. )
  120. .def(
  121. "move_auto_rotation",
  122. (void(Entity::*)(const bool)) &Entity::SetMoveAutoRotation
  123. )
  124. .def(
  125. "move_auto_animation",
  126. (void(Entity::*)(const bool)) &Entity::SetMoveAutoAnimation
  127. )
  128. .def(
  129. "move_to_position",
  130. (void(Entity::*)(const float, const float))
  131. &Entity::ScriptMoveToPosition
  132. )
  133. .def(
  134. "move_to_entity",
  135. (void(Entity::*)(Entity*)) &Entity::ScriptMoveToEntity
  136. )
  137. .def(
  138. "move_sync",
  139. (int(Entity::*)()) &Entity::ScriptMoveSync, luabind::yield
  140. )
  141. .def(
  142. "linear_to_position",
  143. (void(Entity::*)(
  144. const float, const float, const float,
  145. const LinearMovement, const char*
  146. )) &Entity::ScriptLinearToPosition)
  147. .def(
  148. "linear_sync",
  149. (int(Entity::*)()) &Entity::ScriptLinearSync, luabind::yield
  150. )
  151. .def(
  152. "jump_to_position",
  153. (void(Entity::*)(
  154. const float, const float, const float, const float
  155. ))&Entity::ScriptJumpToPosition
  156. )
  157. .def(
  158. "jump_sync",
  159. (int(Entity::*)()) &Entity::ScriptJumpSync, luabind::yield
  160. )
  161. .def(
  162. "offset_to_position",
  163. (void(Entity::*)(
  164. const float, const float, const float,
  165. const ActionType, const float
  166. )) &Entity::ScriptOffsetToPosition
  167. )
  168. .def(
  169. "offset_sync",
  170. (int(Entity::*)()) &Entity::ScriptOffsetSync, luabind::yield
  171. )
  172. .def(
  173. "turn_to_entity",
  174. (void(Entity::*)(Entity*, const TurnDirection, const float))
  175. &Entity::ScriptTurnToEntity
  176. )
  177. .def(
  178. "turn_to_direction",
  179. (void(Entity::*)(
  180. const float, const TurnDirection, const ActionType, const float
  181. )) &Entity::ScriptTurnToDirection)
  182. .def(
  183. "turn_sync",
  184. (int(Entity::*)()) &Entity::ScriptTurnSync, luabind::yield
  185. )
  186. .def(
  187. "set_animation_speed",
  188. (void(Entity::*)(const float)) &Entity::ScriptSetAnimationSpeed
  189. )
  190. .def(
  191. "play_animation",
  192. (void(Entity::*)(const char*)) &Entity::ScriptPlayAnimation
  193. )
  194. .def(
  195. "play_animation_stop",
  196. (void(Entity::*)(const char*)) &Entity::ScriptPlayAnimationStop
  197. )
  198. .def(
  199. "play_animation",
  200. (void(Entity::*)(const char*, const float, const float))
  201. &Entity::ScriptPlayAnimation
  202. )
  203. .def(
  204. "play_animation_stop",
  205. (void(Entity::*)(const char*, const float, const float))
  206. &Entity::ScriptPlayAnimationStop
  207. )
  208. .def(
  209. "set_default_animation",
  210. (void(Entity::*)(const char*)) &Entity::ScriptSetDefaultAnimation
  211. )
  212. .def(
  213. "animation_sync",
  214. (int(Entity::*)()) &Entity::ScriptAnimationSync, luabind::yield
  215. )
  216. .enum_("constants")[
  217. luabind::value("NONE", AT_NONE),
  218. luabind::value("LINEAR", AT_LINEAR),
  219. luabind::value("SMOOTH", AT_SMOOTH),
  220. luabind::value("CLOCKWISE", TD_CLOCKWISE),
  221. luabind::value("ANTICLOCKWISE", TD_ANTICLOCKWISE),
  222. luabind::value("CLOSEST", TD_CLOSEST),
  223. luabind::value("UP_TO_DOWN", LM_UP_TO_DOWN),
  224. luabind::value("DOWN_TO_UP", LM_DOWN_TO_UP),
  225. luabind::value("LEFT_TO_RIGHT", LM_LEFT_TO_RIGHT),
  226. luabind::value("RIGHT_TO_LEFT", LM_RIGHT_TO_LEFT)
  227. ]
  228. ];
  229. // Field commands.
  230. // TODO: Duplicated in group EntityManager, this can probably be deleted
  231. luabind::module(lua_state_)[
  232. luabind::class_<EntityManager>("Field")
  233. .def(
  234. "random_encounters_on",
  235. (float(EntityManager::*)(bool)) &EntityManager::SetRandomEncounters
  236. )
  237. .def(
  238. "start_battle",
  239. (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle
  240. )
  241. // TODO: Run? Set battle flags
  242. .def(
  243. "battle_run",
  244. (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle
  245. )
  246. ];
  247. // Entity individual point commands
  248. luabind::module(lua_state_)[
  249. luabind::class_< EntityPoint >("EntityPoint")
  250. .def( // Internally returns 3 values:
  251. "get_position",
  252. (void(EntityPoint::*)()) &EntityPoint::ScriptGetPosition
  253. )
  254. .def(
  255. "get_rotation",
  256. (float(EntityPoint::*)()) &EntityPoint::ScriptGetRotation
  257. )
  258. ];
  259. // Commands for the entity manager, not related to any particular entity.
  260. luabind::module(lua_state_)[
  261. luabind::class_<EntityManager>("EntityManager")
  262. .def(
  263. "set_paused",
  264. (void(EntityManager::*)(const bool))
  265. &EntityManager::ScriptSetPaused
  266. )
  267. .def(
  268. "add_entity",
  269. (void(EntityManager::*)(
  270. const char*, const char*, const float,
  271. const float, const float, const float
  272. )) &EntityManager::ScriptAddEntity
  273. )
  274. .def(
  275. "add_entity_script",
  276. (void(EntityManager::*)(const char*))
  277. &EntityManager::ScriptAddEntityScript
  278. )
  279. .def(
  280. "get_entity",
  281. (Entity*(EntityManager::*)(const char*))
  282. &EntityManager::ScriptGetEntity
  283. )
  284. .def(
  285. "get_entity_point",
  286. (EntityPoint*(EntityManager::*)(const char*))
  287. &EntityManager::ScriptGetEntityPoint
  288. )
  289. .def(
  290. "set_player_entity",
  291. (void(EntityManager::*)(const char*))
  292. &EntityManager::ScriptSetPlayerEntity
  293. )
  294. .def(
  295. "unset_player_entity",
  296. (void(EntityManager::*)()) &EntityManager::ScriptUnsetPlayerEntity
  297. )
  298. .def(
  299. "player_lock",
  300. (void(EntityManager::*)(const bool))
  301. &EntityManager::ScriptPlayerLock
  302. )
  303. .def(
  304. "random_encounters_on",
  305. (float(EntityManager::*)(bool)) &EntityManager::SetRandomEncounters
  306. )
  307. .def(
  308. "start_battle",
  309. (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle
  310. )
  311. // TODO: Run? Set battle flags
  312. .def(
  313. "battle_run",
  314. (void(EntityManager::*)(unsigned int)) &EntityManager::StartBattle
  315. )
  316. ];
  317. // 2D background and camera commands
  318. luabind::module(lua_state_)[
  319. luabind::class_<Background2D>("Background2D")
  320. .def(
  321. "autoscroll_to_entity",
  322. (void(Background2D::*)(Entity*))
  323. &Background2D::ScriptAutoScrollToEntity
  324. )
  325. .def(
  326. "scroll_to_position",
  327. (void(Background2D::*)(
  328. const float, const float,
  329. const Background2D::ScrollType, const float
  330. )) &Background2D::ScriptScrollToPosition
  331. )
  332. .def(
  333. "scroll_sync",
  334. (int(Background2D::*)()) &Background2D::ScriptScrollSync,
  335. luabind::yield
  336. )
  337. .def(
  338. "play_animation_looped",
  339. (void(Background2D::*)(const char*))
  340. &Background2D::ScriptPlayAnimationLooped
  341. )
  342. .def(
  343. "play_animation_once",
  344. (void(Background2D::*)(const char*))
  345. &Background2D::ScriptPlayAnimationOnce
  346. )
  347. .def(
  348. "animation_sync",
  349. (int(Background2D::*)(const char*))
  350. &Background2D::ScriptAnimationSync,
  351. luabind::yield
  352. )
  353. .enum_("constants")[
  354. luabind::value("NONE", AT_NONE),
  355. luabind::value("LINEAR", AT_LINEAR),
  356. luabind::value("SMOOTH", AT_SMOOTH)
  357. ]
  358. ];
  359. // Walkmesh commands
  360. luabind::module(lua_state_)[
  361. luabind::class_<Walkmesh>("Walkmesh")
  362. .def(
  363. "lock_walkmesh",
  364. (void(Walkmesh ::*)(unsigned int, bool)) &Walkmesh ::LockWalkmesh
  365. )
  366. .def(
  367. "is_locked", (bool(Walkmesh ::*)(unsigned int)) &Walkmesh ::IsLocked
  368. )
  369. ];
  370. // Dialog commands
  371. luabind::module(lua_state_)[
  372. luabind::class_<DialogsManager>("Dialog")
  373. .def(
  374. "dialog_open",
  375. (void(DialogsManager::*)(const char*, int, int, int, int))
  376. &DialogsManager::OpenDialog
  377. )
  378. .def(
  379. "dialog_set_text",
  380. (void(DialogsManager::*)(const char*, const char*))
  381. &DialogsManager::SetText
  382. )
  383. .def(
  384. "dialog_wait_for_close",
  385. (int(DialogsManager::*)(const char*)) &DialogsManager::Sync,
  386. luabind::yield
  387. )
  388. .def(
  389. "dialog_close",
  390. (void(DialogsManager::*)(const char*)) &DialogsManager::Hide
  391. )
  392. .def(
  393. "set_variable",
  394. (void(DialogsManager::*)(const char*, const char*, const char*))
  395. &DialogsManager::SetVariable
  396. )
  397. .def(
  398. "set_clickable",
  399. (void(DialogsManager::*)(const char*, const bool))
  400. &DialogsManager::SetClickable
  401. )
  402. .def(
  403. "set_cursor",
  404. (void(DialogsManager::*)(const char*, const int, const int))
  405. &DialogsManager::SetCursor
  406. )
  407. .def(
  408. "get_cursor",
  409. (int(DialogsManager::*)(const char*))
  410. &DialogsManager::GetCursor
  411. )
  412. .enum_("constants")[
  413. luabind::value("SOLID", MSL_SOLID),
  414. luabind::value("TRANSPARENT", MSL_TRANSPARENT),
  415. luabind::value("NONE", MSL_NONE)
  416. ]
  417. ];
  418. // UI widget commands
  419. luabind::module(lua_state_)[
  420. luabind::class_<UiWidget>("UiWidget")
  421. .def(
  422. "set_visible",
  423. (void(UiWidget::*)(const bool)) &UiWidget::SetVisible
  424. )
  425. .def(
  426. "is_visible",
  427. (bool(UiWidget::*)()) &UiWidget::IsVisible
  428. )
  429. .def(
  430. "play_animation",
  431. (void(UiWidget::*)(const char*)) &UiWidget::ScriptPlayAnimation
  432. )
  433. .def(
  434. "play_animation_stop",
  435. (void(UiWidget::*)(const char*)) &UiWidget::ScriptPlayAnimationStop
  436. )
  437. .def(
  438. "play_animation",
  439. (void(UiWidget::*)(const char*, const float, const float))
  440. &UiWidget::ScriptPlayAnimation
  441. )
  442. .def(
  443. "play_animation_stop",
  444. (void(UiWidget::*)(const char*, const float, const float))
  445. &UiWidget::ScriptPlayAnimationStop
  446. )
  447. .def(
  448. "set_default_animation",
  449. (void(UiWidget::*)(const char*))
  450. &UiWidget::ScriptSetDefaultAnimation
  451. )
  452. .def(
  453. "animation_sync",
  454. (int(UiWidget::*)()) &UiWidget::ScriptAnimationSync, luabind::yield
  455. )
  456. .def(
  457. "set_colour",
  458. (void(UiWidget::*)(const float, const float, const float))
  459. &UiWidget::SetColour
  460. )
  461. .def(
  462. "set_alpha",
  463. (void(UiWidget::*)(const float)) &UiWidget::SetAlpha
  464. )
  465. .def(
  466. "set_x",
  467. (void(UiWidget::*)(const float, const float)) &UiWidget::SetX
  468. )
  469. .def(
  470. "set_y", (void(UiWidget::*)(const float, const float))
  471. &UiWidget::SetY
  472. )
  473. .def(
  474. "set_z",
  475. (void(UiWidget::*)(const float)) &UiWidget::SetZ
  476. )
  477. .def(
  478. "set_width", (void(UiWidget::*)(const float, const float))
  479. &UiWidget::SetWidth
  480. )
  481. .def(
  482. "set_height",
  483. (void(UiWidget::*)(const float, const float)) &UiWidget::SetHeight
  484. )
  485. ];
  486. // UI manager commands. Use to get a specific widget.
  487. luabind::module(lua_state_)[
  488. luabind::class_<UiManager>("UiManager")
  489. .def(
  490. "get_widget",
  491. (UiWidget*(UiManager::*)(const char*)) &UiManager::ScriptGetWidget
  492. )
  493. ];
  494. // Timer command. To show a in-game timer.
  495. luabind::module(lua_state_)[
  496. luabind::class_<Timer>("Timer")
  497. .def(
  498. "get_game_time_total", (float(Timer::*)()) &Timer::GetGameTimeTotal
  499. )
  500. .def("set_timer", (float(Timer::*)(const float)) &Timer::SetGameTimer)
  501. .def("get_timer", (int(Timer::*)()) &Timer::GetGameTimer)
  502. ];
  503. // Commands that control script execution
  504. luabind::module(lua_state_)[
  505. luabind::class_<ScriptManager>("Script")
  506. .def(
  507. "wait",
  508. (int(ScriptManager::*)(const float)) &ScriptManager::ScriptWait,
  509. luabind::yield
  510. )
  511. .def(
  512. "request",
  513. (void(ScriptManager::*)(
  514. const ScriptManager::Type, const char*, const char*, const int
  515. )) &ScriptManager::ScriptRequest
  516. )
  517. .def(
  518. "request_start_sync",
  519. (int(ScriptManager::*)(
  520. const ScriptManager::Type, const char*, const char*, const int
  521. )) &ScriptManager::ScriptRequestStartSync,
  522. luabind::yield
  523. )
  524. .def(
  525. "request_end_sync",
  526. (int(ScriptManager::*)
  527. (const ScriptManager::Type, const char*, const char*, const int))
  528. &ScriptManager::ScriptRequestEndSync,
  529. luabind::yield
  530. )
  531. .enum_("constants")[
  532. luabind::value("SYSTEM", ScriptManager::SYSTEM),
  533. luabind::value("ENTITY", ScriptManager::ENTITY),
  534. luabind::value("UI", ScriptManager::UI)
  535. ]
  536. ];
  537. // Commnds to initiate modules
  538. luabind::module(lua_state_)[
  539. luabind::class_<QGears::WorldMapModule>("world_map_module")
  540. .def(
  541. "init",
  542. (void(QGears::WorldMapModule::*)()) &QGears::WorldMapModule::Init
  543. )
  544. ];
  545. // Register all command handlers
  546. auto a = boost::ref(*(EntityManager::getSingletonPtr()));
  547. luabind::globals(lua_state_);
  548. //auto b = luabind::globals(lua_state_)["entity_manager"];
  549. luabind::globals(lua_state_)["entity_manager"]
  550. = boost::ref(*(EntityManager::getSingletonPtr()));
  551. luabind::globals(lua_state_)["background2d"]
  552. = boost::ref(*(EntityManager::getSingletonPtr()->GetBackground2D()));
  553. luabind::globals(lua_state_)["walkmesh"]
  554. = boost::ref(*(EntityManager::getSingletonPtr()->GetWalkmesh()));
  555. luabind::globals(lua_state_)["dialog"]
  556. = boost::ref(*(DialogsManager::getSingletonPtr()));
  557. luabind::globals(lua_state_)["ui_manager"]
  558. = boost::ref(*(UiManager::getSingletonPtr()));
  559. luabind::globals(lua_state_)["world_map_module"]
  560. = boost::ref(*(QGears::WorldMapModule::getSingletonPtr()));
  561. luabind::globals(lua_state_)["timer"]
  562. = boost::ref(*(Timer::getSingletonPtr()));
  563. luabind::globals(lua_state_)["script"] = boost::ref(*this);
  564. std::cout << "[INIT BINDS] END " << std::endl;
  565. }