ScriptManagerBinds.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "Console.h"
  2. #include "Logger.h"
  3. #include "Entity.h"
  4. #include "EntityManager.h"
  5. #include "Timer.h"
  6. #include "UiManager.h"
  7. #include "UiWidget.h"
  8. #include "XmlMapFile.h"
  9. #include "XmlMapsFile.h"
  10. #include "DialogsManager.h"
  11. #include "modules/worldmap/WorldMapModule.h"
  12. void
  13. ScriptPrint( const char* text )
  14. {
  15. Console::getSingleton().AddTextToOutput( text );
  16. }
  17. void
  18. ScriptMap( const char* text )
  19. {
  20. EntityManager::getSingleton().Clear();
  21. XmlMapsFile xml( "./data/maps.xml" );
  22. Ogre::String file_name = xml.GetMapFileNameByName( text );
  23. XmlMapFile xml_map( "./data/" + file_name );
  24. xml_map.LoadMap();
  25. }
  26. void
  27. ScriptConsole( const char* text )
  28. {
  29. Console::getSingleton().ExecuteCommand( text );
  30. }
  31. void
  32. ScriptManager::InitBinds()
  33. {
  34. // globals
  35. luabind::module( m_LuaState )
  36. [
  37. luabind::def( "print", ( void( * )( const char* ) ) &ScriptPrint ),
  38. luabind::def( "map", ( void( * )( const char* ) ) &ScriptMap ),
  39. luabind::def( "console", ( void( * )( const char* ) ) &ScriptConsole )
  40. ];
  41. // entity access
  42. luabind::module( m_LuaState )
  43. [
  44. luabind::class_< Entity >( "Entity" )
  45. .def( "set_position", ( void( Entity::* )( const float, const float, const float ) ) &Entity::ScriptSetPosition )
  46. .def( "get_position", ( void( Entity::* )() ) &Entity::ScriptGetPosition ) // return 3 values internaly
  47. .def( "set_rotation", ( void( Entity::* )( const float ) ) &Entity::ScriptSetRotation )
  48. .def( "get_rotation", ( float( Entity::* )() ) &Entity::ScriptGetRotation )
  49. .def( "set_solid_radius", ( void( Entity::* )( const float ) ) &Entity::SetSolidRadius )
  50. .def( "get_solid_radius", ( float( Entity::* )() ) &Entity::GetSolidRadius )
  51. .def( "set_solid", ( void( Entity::* )( const bool ) ) &Entity::SetSolid )
  52. .def( "is_solid", ( bool( Entity::* )() ) &Entity::IsSolid )
  53. .def( "set_talk_radius", ( void( Entity::* )( const float ) ) &Entity::SetTalkRadius )
  54. .def( "get_talk_radius", ( float( Entity::* )() ) &Entity::GetTalkRadius )
  55. // Some old test script use this, its just an alias for SetTalkable
  56. .def("set_interactable", (void(Entity::*)(const bool)) &Entity::SetTalkable)
  57. .def( "set_talkable", ( void( Entity::* )( const bool ) ) &Entity::SetTalkable )
  58. .def( "is_talkable", ( bool( Entity::* )() ) &Entity::IsTalkable )
  59. .def( "set_visible", ( void( Entity::* )( const bool ) ) &Entity::SetVisible )
  60. .def( "is_visible", ( bool( Entity::* )() ) &Entity::IsVisible )
  61. .def( "set_move_auto_speed", ( void( Entity::* )( const float ) ) &Entity::SetMoveAutoSpeed )
  62. .def( "get_move_auto_speed", ( float( Entity::* )() ) &Entity::GetMoveAutoSpeed )
  63. .def( "get_move_triangle_id", ( int( Entity::* )() ) &Entity::GetMoveTriangleId )
  64. .def( "move_auto_rotation", ( void( Entity::* )( const bool ) ) &Entity::SetMoveAutoRotation )
  65. .def( "move_auto_animation", ( void( Entity::* )( const bool ) ) &Entity::SetMoveAutoAnimation )
  66. .def( "move_to_position", ( void( Entity::* )( const float, const float ) ) &Entity::ScriptMoveToPosition )
  67. .def( "move_to_entity", ( void( Entity::* )( Entity* ) ) &Entity::ScriptMoveToEntity )
  68. .def( "move_sync", ( int( Entity::* )() ) &Entity::ScriptMoveSync, luabind::yield )
  69. .def( "linear_to_position", ( void( Entity::* )( const float, const float, const float, const LinearMovement, const char* ) ) &Entity::ScriptLinearToPosition )
  70. .def( "linear_sync", ( int( Entity::* )() ) &Entity::ScriptLinearSync, luabind::yield )
  71. .def( "jump_to_position", ( void( Entity::* )( const float, const float, const float, const float ) )&Entity::ScriptJumpToPosition)
  72. .def( "jump_sync", ( int( Entity::* )() ) &Entity::ScriptJumpSync, luabind::yield )
  73. .def( "offset_to_position", ( void( Entity::* )( const float, const float, const float, const ActionType, const float ) ) &Entity::ScriptOffsetToPosition )
  74. .def( "offset_sync", ( int( Entity::* )() ) &Entity::ScriptOffsetSync, luabind::yield )
  75. .def( "turn_to_entity", ( void( Entity::* )( Entity*, const TurnDirection, const float ) )&Entity::ScriptTurnToEntity )
  76. .def( "turn_to_direction", ( void( Entity::* )( const float, const TurnDirection, const ActionType, const float ) )&Entity::ScriptTurnToDirection )
  77. .def( "turn_sync", ( int( Entity::* )() ) &Entity::ScriptTurnSync, luabind::yield )
  78. .def( "set_animation_speed", ( void( Entity::* )( const float ) ) &Entity::ScriptSetAnimationSpeed )
  79. .def( "play_animation", ( void( Entity::* )( const char* ) ) &Entity::ScriptPlayAnimation )
  80. .def( "play_animation_stop", ( void( Entity::* )( const char* ) ) &Entity::ScriptPlayAnimationStop )
  81. .def( "play_animation", ( void( Entity::* )( const char*, const float, const float ) ) &Entity::ScriptPlayAnimation )
  82. .def( "play_animation_stop", ( void( Entity::* )( const char*, const float, const float ) ) &Entity::ScriptPlayAnimationStop )
  83. .def( "set_default_animation", ( void( Entity::* )( const char* ) ) &Entity::ScriptSetDefaultAnimation )
  84. .def( "animation_sync", ( int( Entity::* )()) &Entity::ScriptAnimationSync, luabind::yield )
  85. .enum_( "constants" )
  86. [
  87. luabind::value( "NONE", AT_NONE ),
  88. luabind::value( "LINEAR", AT_LINEAR ),
  89. luabind::value( "SMOOTH", AT_SMOOTH ),
  90. luabind::value( "CLOCKWISE", TD_CLOCKWISE ),
  91. luabind::value( "ANTICLOCKWISE", TD_ANTICLOCKWISE ),
  92. luabind::value( "CLOSEST", TD_CLOSEST ),
  93. luabind::value( "UP_TO_DOWN", LM_UP_TO_DOWN ),
  94. luabind::value( "DOWN_TO_UP", LM_DOWN_TO_UP ),
  95. luabind::value( "LEFT_TO_RIGHT", LM_LEFT_TO_RIGHT ),
  96. luabind::value( "RIGHT_TO_LEFT", LM_RIGHT_TO_LEFT )
  97. ]
  98. ];
  99. // game access
  100. luabind::module( m_LuaState )
  101. [
  102. luabind::class_< EntityPoint >( "EntityPoint" )
  103. .def( "get_position", ( void( EntityPoint::* )() ) &EntityPoint::ScriptGetPosition ) // return 3 values internaly
  104. .def( "get_rotation", ( float( EntityPoint::* )() ) &EntityPoint::ScriptGetRotation )
  105. ];
  106. // game access
  107. luabind::module( m_LuaState )
  108. [
  109. luabind::class_< EntityManager >( "EntityManager" )
  110. .def( "set_paused", ( void( EntityManager::* )( const bool ) ) &EntityManager::ScriptSetPaused )
  111. .def( "add_entity", ( void( EntityManager::* )( const char*, const char*, const float, const float, const float, const float ) ) &EntityManager::ScriptAddEntity )
  112. .def( "add_entity_script", ( void( EntityManager::* )( const char* ) ) &EntityManager::ScriptAddEntityScript )
  113. .def( "get_entity", ( Entity*( EntityManager::* )( const char* ) ) &EntityManager::ScriptGetEntity )
  114. .def( "get_entity_point", ( EntityPoint*( EntityManager::* )( const char* ) ) &EntityManager::ScriptGetEntityPoint )
  115. .def( "set_player_entity", ( void( EntityManager::* )( const char* ) ) &EntityManager::ScriptSetPlayerEntity )
  116. .def( "unset_player_entity", ( void( EntityManager::* )() ) &EntityManager::ScriptUnsetPlayerEntity )
  117. .def( "player_lock", ( void( EntityManager::* )( const bool ) ) &EntityManager::ScriptPlayerLock )
  118. ];
  119. // 2d background access
  120. luabind::module( m_LuaState )
  121. [
  122. luabind::class_< Background2D >( "Background2D" )
  123. .def( "autoscroll_to_entity", ( void( Background2D::* )( Entity* ) ) &Background2D::ScriptAutoScrollToEntity )
  124. .def( "scroll_to_position", ( void( Background2D::* )( const float, const float, const Background2D::ScrollType, const float ) ) &Background2D::ScriptScrollToPosition )
  125. .def( "scroll_sync", ( int( Background2D::* )() ) &Background2D::ScriptScrollSync, luabind::yield )
  126. .def( "play_animation_looped", ( void( Background2D::* )( const char* ) ) &Background2D::ScriptPlayAnimationLooped )
  127. .def( "play_animation_once", ( void( Background2D::* )( const char* ) ) &Background2D::ScriptPlayAnimationOnce )
  128. .def( "animation_sync", ( int( Background2D::* )( const char* ) ) &Background2D::ScriptAnimationSync, luabind::yield )
  129. .enum_( "constants" )
  130. [
  131. luabind::value( "NONE", AT_NONE ),
  132. luabind::value( "LINEAR", AT_LINEAR ),
  133. luabind::value( "SMOOTH", AT_SMOOTH )
  134. ]
  135. ];
  136. // walkmesh access
  137. luabind::module( m_LuaState )
  138. [
  139. luabind::class_< Walkmesh >( "Walkmesh" )
  140. .def( "lock_walkmesh", ( void( Walkmesh ::* )( unsigned int, bool ) ) &Walkmesh ::LockWalkmesh)
  141. .def( "is_locked", ( bool( Walkmesh ::* )( unsigned int ) ) &Walkmesh ::IsLocked)
  142. ];
  143. luabind::module( m_LuaState )
  144. [
  145. luabind::class_< DialogsManager >( "Dialog" )
  146. .def( "dialog_open", ( void( DialogsManager::* )( const char*, int, int, int, int ) ) &DialogsManager::OpenDialog )
  147. .def( "dialog_set_text", ( void( DialogsManager::* )( const char*, const char* ) ) &DialogsManager::SetText )
  148. .def( "dialog_wait_for_close", ( int( DialogsManager::* )( const char* ) ) &DialogsManager::Sync, luabind::yield )
  149. .def( "dialog_close", (void(DialogsManager::*)(const char*)) &DialogsManager::Hide)
  150. .def( "set_variable", ( void( DialogsManager::* )( const char*, const char*, const char* ) ) &DialogsManager::SetVariable )
  151. .def( "set_clickable", ( void( DialogsManager::* )( const char*, const bool ) ) &DialogsManager::SetClickable )
  152. .def( "set_cursor", ( void( DialogsManager::* )( const char*, const int, const int ) ) &DialogsManager::SetCursor )
  153. .def( "get_cursor", ( int( DialogsManager::* )( const char* ) ) &DialogsManager::GetCursor )
  154. .enum_("constants")
  155. [
  156. luabind::value("SOLID", MSL_SOLID),
  157. luabind::value("TRANSPARENT", MSL_TRANSPARENT),
  158. luabind::value("NONE", MSL_NONE)
  159. ]
  160. ];
  161. // ui widget access
  162. luabind::module( m_LuaState )
  163. [
  164. luabind::class_< UiWidget >( "UiWidget" )
  165. .def( "set_visible", ( void( UiWidget::* )( const bool ) ) &UiWidget::SetVisible )
  166. .def( "is_visible", ( bool( UiWidget::* )() ) &UiWidget::IsVisible )
  167. .def( "play_animation", ( void( UiWidget::* )( const char* ) ) &UiWidget::ScriptPlayAnimation )
  168. .def( "play_animation_stop", ( void( UiWidget::* )( const char* ) ) &UiWidget::ScriptPlayAnimationStop )
  169. .def( "play_animation", ( void( UiWidget::* )( const char*, const float, const float ) ) &UiWidget::ScriptPlayAnimation )
  170. .def( "play_animation_stop", ( void( UiWidget::* )( const char*, const float, const float ) ) &UiWidget::ScriptPlayAnimationStop )
  171. .def( "set_default_animation", ( void( UiWidget::* )( const char* ) ) &UiWidget::ScriptSetDefaultAnimation )
  172. .def( "animation_sync", ( int( UiWidget::* )() ) &UiWidget::ScriptAnimationSync, luabind::yield )
  173. .def( "set_colour", ( void( UiWidget::* )( const float, const float, const float ) ) &UiWidget::SetColour )
  174. .def( "set_alpha", ( void( UiWidget::* )( const float ) ) &UiWidget::SetAlpha )
  175. .def( "set_x", ( void( UiWidget::* )( const float, const float ) ) &UiWidget::SetX )
  176. .def( "set_y", ( void( UiWidget::* )( const float, const float ) ) &UiWidget::SetY )
  177. .def( "set_z", ( void( UiWidget::* )( const float ) ) &UiWidget::SetZ )
  178. .def( "set_width", ( void( UiWidget::* )( const float, const float ) ) &UiWidget::SetWidth )
  179. .def( "set_height", ( void( UiWidget::* )( const float, const float ) ) &UiWidget::SetHeight )
  180. ];
  181. // ui access
  182. luabind::module( m_LuaState )
  183. [
  184. luabind::class_< UiManager >( "UiManager" )
  185. .def( "get_widget", ( UiWidget*( UiManager::* )( const char* ) ) &UiManager::ScriptGetWidget )
  186. ];
  187. // timer access
  188. luabind::module( m_LuaState )
  189. [
  190. luabind::class_< Timer >( "Timer" )
  191. .def( "get_game_time_total", ( float( Timer::* )() ) &Timer::GetGameTimeTotal )
  192. .def( "set_timer", ( float( Timer::* )( const float ) ) &Timer::SetGameTimer )
  193. .def( "get_timer", ( int( Timer::* )() ) &Timer::GetGameTimer )
  194. ];
  195. // script access
  196. luabind::module( m_LuaState )
  197. [
  198. luabind::class_< ScriptManager >( "Script" )
  199. .def( "wait", ( int( ScriptManager::* )( const float ) ) &ScriptManager::ScriptWait, luabind::yield )
  200. .def( "request", ( void( ScriptManager::* )( const ScriptManager::Type, const char*, const char*, const int ) ) &ScriptManager::ScriptRequest )
  201. .def( "request_start_sync", ( int( ScriptManager::* )( const ScriptManager::Type, const char*, const char*, const int ) ) &ScriptManager::ScriptRequestStartSync, luabind::yield )
  202. .def( "request_end_sync", ( int( ScriptManager::* )( const ScriptManager::Type, const char*, const char*, const int ) ) &ScriptManager::ScriptRequestEndSync, luabind::yield )
  203. .enum_( "constants" )
  204. [
  205. luabind::value( "SYSTEM", ScriptManager::SYSTEM ),
  206. luabind::value( "ENTITY", ScriptManager::ENTITY ),
  207. luabind::value( "UI", ScriptManager::UI )
  208. ]
  209. ];
  210. luabind::module( m_LuaState )
  211. [
  212. luabind::class_< QGears::WorldMapModule >( "world_map_module" )
  213. .def( "init", ( void( QGears::WorldMapModule::* )() ) &QGears::WorldMapModule::Init )
  214. ];
  215. luabind::globals( m_LuaState )[ "entity_manager" ] = boost::ref( *( EntityManager::getSingletonPtr() ) );
  216. luabind::globals( m_LuaState )[ "background2d" ] = boost::ref( *( EntityManager::getSingletonPtr()->GetBackground2D() ) );
  217. luabind::globals( m_LuaState )[ "walkmesh" ] = boost::ref( *( EntityManager::getSingletonPtr()->GetWalkmesh() ) );
  218. luabind::globals( m_LuaState )[ "dialog" ] = boost::ref( *( DialogsManager::getSingletonPtr() ) );
  219. luabind::globals( m_LuaState )[ "ui_manager" ] = boost::ref( *( UiManager::getSingletonPtr() ) );
  220. luabind::globals( m_LuaState )[ "world_map_module" ] = boost::ref( *( QGears::WorldMapModule::getSingletonPtr() ) );
  221. luabind::globals( m_LuaState )[ "timer" ] = boost::ref( *( Timer::getSingletonPtr() ) );
  222. luabind::globals( m_LuaState )[ "script" ] = boost::ref( *this );
  223. }