ScriptManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #pragma once
  16. #include <OgreSingleton.h>
  17. #include <OgreString.h>
  18. #include "Event.h"
  19. #include "LuaIncludes.h"
  20. class Entity;
  21. /**
  22. * Script identifier.
  23. */
  24. struct ScriptId{
  25. /**
  26. * Constructor.
  27. */
  28. ScriptId(): entity(""), function(""){}
  29. /**
  30. * Entity name.
  31. */
  32. Ogre::String entity;
  33. /**
  34. * Function (script) name.
  35. */
  36. Ogre::String function;
  37. };
  38. /**
  39. * A script entity.
  40. */
  41. struct ScriptEntity;
  42. /**
  43. * A script queue.
  44. */
  45. struct QueueScript{
  46. /**
  47. * Constructor.
  48. */
  49. QueueScript():
  50. function(""),
  51. argument1(""),
  52. argument2(""),
  53. priority(0),
  54. state(NULL),
  55. seconds_to_wait(0),
  56. wait(false),
  57. yield(false)
  58. {}
  59. /**
  60. * Function name.
  61. */
  62. Ogre::String function;
  63. /**
  64. * First function argument.
  65. */
  66. Ogre::String argument1;
  67. /**
  68. * Second function argument.
  69. */
  70. Ogre::String argument2;
  71. /**
  72. * Function priority.
  73. */
  74. int priority;
  75. /**
  76. * Current state.
  77. */
  78. lua_State* state;
  79. /**
  80. * State thread identifier.
  81. *
  82. * Used to store and delete thread.
  83. */
  84. int state_id;
  85. /**
  86. * Seconds to wait for completion.
  87. */
  88. float seconds_to_wait;
  89. /**
  90. * Indicates if the script completion should be waited for,
  91. */
  92. bool wait;
  93. /**
  94. * @todo Understand and document.
  95. */
  96. bool yield;
  97. /**
  98. * The script paused by call of this script
  99. */
  100. ScriptId paused_script_start;
  101. /**
  102. * The script paused by call of this script
  103. */
  104. ScriptId paused_script_end;
  105. };
  106. class ScriptManager : public Ogre::Singleton<ScriptManager>{
  107. public:
  108. /**
  109. * Script types.
  110. */
  111. enum Type{
  112. /**
  113. * System script.
  114. *
  115. * The script is triggered by a system event.
  116. */
  117. SYSTEM,
  118. /**
  119. * A field entity script.
  120. *
  121. * The script belongs to any of the enities on a map.
  122. */
  123. ENTITY,
  124. /**
  125. * A UI element script.
  126. *
  127. * The script is triggered by a UI element (menu, cursor...)
  128. */
  129. UI,
  130. /**
  131. * A field script.
  132. *
  133. * The script ios triggered by the map itself
  134. */
  135. FIELD
  136. };
  137. /**
  138. * Constructor.
  139. */
  140. ScriptManager();
  141. /**
  142. * Destructor.
  143. */
  144. virtual ~ScriptManager();
  145. /**
  146. * Handles an input event.
  147. *
  148. * @param[in] event The event to handle.
  149. */
  150. void Input(const VGears::Event& event);
  151. /**
  152. * Updates the state of all scripts of a given type.
  153. *
  154. * @param[in] type Type of the scripts to update.
  155. */
  156. void Update(const Type type);
  157. /**
  158. * Runs a lua command string.
  159. *
  160. * No errors are handled, and nothing is returned
  161. *
  162. * @param[in] lua Lua string to run.
  163. */
  164. void RunString(const Ogre::String& lua);
  165. /**
  166. * Runs a lua file.
  167. *
  168. * No errors are handled, and nothing is returned
  169. *
  170. * @param[in] file Path to the lua file to run (relative to the
  171. * data directory).
  172. */
  173. void RunFile(const Ogre::String& file);
  174. /**
  175. * Initializes Lua binds.
  176. *
  177. * It relates the command available in the field maps to C++ functions.
  178. */
  179. void InitBinds();
  180. /**
  181. * Initializes command bindings
  182. */
  183. void InitCmd();
  184. /**
  185. * Adds an entity to the manager.
  186. *
  187. * @param[in] type Type of entity to add.
  188. * @param[in] entity_name The entity name.
  189. * @param[in] entity The entity to add.
  190. */
  191. void AddEntity(
  192. const Type type, const Ogre::String& entity_name, Entity* entity
  193. );
  194. /**
  195. * Deletes an entity from the manager.
  196. *
  197. * If there is no entity with name ENTITY_NAME and type TYPE, nothing
  198. * will be done.
  199. *
  200. * @param[in] type Type of the entity to remove.
  201. * @param[in] entity_name NAme of the entity to remove.
  202. */
  203. void RemoveEntity(const Type type, const Ogre::String& entity_name);
  204. /**
  205. * Adds an script to an entity.
  206. *
  207. * @param[in] entity_name Name of the entity to add a script to.
  208. * @param[in] function_name Name of the script to add.
  209. * @param[in] priority Script priority. Lower numbers have higher
  210. * priority.
  211. */
  212. void AddEntityScript(
  213. const Ogre::String& entity_name, const Ogre::String& function_name,
  214. int priority
  215. );
  216. /**
  217. * Removes the top script of an entity.
  218. *
  219. * @param[in] entity Enthity whose first script to remove.
  220. */
  221. void RemoveEntityTopScript(ScriptEntity& entity);
  222. /**
  223. * Retrieves a table.
  224. *
  225. * @param[in] type Type of script.
  226. * @param[in] name Script name.
  227. * @param[in] state Initial script state.
  228. * @return The table.
  229. * @todo Understand and document.
  230. */
  231. luabind::object GetTableByEntityName(
  232. const ScriptManager::Type type, const Ogre::String& name,
  233. lua_State* state
  234. ) const;
  235. /**
  236. * Retrieves a script from it's ID.
  237. *
  238. * @param[in] script Script ID.
  239. * @return The script with the corresponding ID, or nullptr if there is
  240. * none
  241. */
  242. QueueScript* GetScriptByScriptId(const ScriptId& script) const;
  243. /**
  244. * Retrieves a script entity by it's name and type.
  245. *
  246. * @param[in] type Entity type.
  247. * @param[in] entity_name The entity name.
  248. * @return The entity by that type and name, or nullptr if there is
  249. * no one that matches.
  250. */
  251. ScriptEntity* GetScriptEntityByName(
  252. const Type type, const Ogre::String& entity_name
  253. ) const;
  254. /**
  255. * Retrieves the current script ID.
  256. *
  257. * @return The currently executing script ID.
  258. */
  259. const ScriptId GetCurrentScriptId() const;
  260. /**
  261. * continues the execution of a script.
  262. *
  263. * @param[in] script The ID of the script to resume.
  264. */
  265. void ContinueScriptExecution(const ScriptId& script);
  266. /**
  267. * Makes an script wait.
  268. *
  269. * @param[in] seconds Number of seconds to wait.
  270. * @todo Does it refer to the current script?
  271. */
  272. int ScriptWait(const float seconds);
  273. /**
  274. * Request an script execution.
  275. *
  276. * The script will execute only if it's not already being executed.
  277. *
  278. * @param[in] type Script type.
  279. * @param[in] entity Entity the scripts belong to.
  280. * @param[in] function Name of the function of the selected entity to
  281. * execute.
  282. * @param[in] priority Execution priority.
  283. */
  284. void ScriptRequest(
  285. const Type type, const char* entity, const char* function, const int priority
  286. );
  287. /**
  288. * Request a synchronous script execution to start.
  289. *
  290. * @param[in] type Script type.
  291. * @param[in] entity Entity the scripts belong to.
  292. * @param[in] function Name of the function of the selected entity to
  293. * execute.
  294. * @param[in] priority Execution priority.
  295. * @return -1 on success, 1 if the entity or the script don't exist.
  296. */
  297. int ScriptRequestStartSync(
  298. const Type type, const char* entity,
  299. const char* function, const int priority);
  300. /**
  301. * Request a synchronous script execution to end.
  302. *
  303. * @param[in] type Script type.
  304. * @param[in] entity Entity the scripts belong to.
  305. * @param[in] function Name of the function of the selected entity to
  306. * execute.
  307. * @param[in] priority Execution priority.
  308. * @return -1 if the execution stops or if the script was not running,
  309. * 1 if the entity or the script don't exist.
  310. */
  311. int ScriptRequestEndSync(
  312. const Type type, const char* entity,
  313. const char* function, const int priority
  314. );
  315. /**
  316. * Request a script execution.
  317. *
  318. * @param[in] script_entity Entity the scripts belong to.
  319. * @param[in] function Name of the function of the selected entity to
  320. * execute.
  321. * @param[in] priority Execution priority.
  322. * @param[in] argument1 First argument for the script.
  323. * @param[in] argument2 Second argument for the script.
  324. * @param[in] start_sync If true, the script will be started
  325. * synchronously.
  326. * @param[in] end_sync @todo Understand and document.
  327. * @return True on success, false on error (i.e. if the entity or the
  328. * script don't exist)
  329. */
  330. bool ScriptRequest(
  331. ScriptEntity* script_entity, const Ogre::String& function,
  332. const int priority, const Ogre::String& argument1,
  333. const Ogre::String& argument2, bool start_sync, bool end_sync
  334. );
  335. /**
  336. * Adds a script to the stack.
  337. *
  338. * @param[in] value ID of the script to add.
  339. * @todo Verify this documentation.
  340. */
  341. void AddValueToStack(const float value);
  342. private:
  343. /**
  344. * Lua state.
  345. */
  346. lua_State* lua_state_;
  347. /**
  348. * The system script table name.
  349. */
  350. Ogre::String system_table_name_;
  351. /**
  352. * The entity script table name.
  353. */
  354. Ogre::String entity_table_name_;
  355. /**
  356. * The UI script table name.
  357. */
  358. Ogre::String ui_table_name_;
  359. /**
  360. * The list of script entities.
  361. */
  362. std::vector<ScriptEntity> script_entity_;
  363. /**
  364. * The current script ID.
  365. */
  366. ScriptId current_script_id_;
  367. };
  368. struct ScriptEntity{
  369. /**
  370. * Constructor.
  371. *
  372. *By default, the type is {@see ScriptManager::SYSTEM}.
  373. */
  374. ScriptEntity():
  375. name(""),
  376. type(ScriptManager::SYSTEM),
  377. resort(false)
  378. {}
  379. /**
  380. * The script name.
  381. */
  382. Ogre::String name;
  383. /**
  384. * The script type.
  385. */
  386. ScriptManager::Type type;
  387. /**
  388. * The script queue.
  389. */
  390. std::vector<QueueScript> queue;
  391. /**
  392. * @todo Understand and document.
  393. */
  394. bool resort;
  395. };