ScriptManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 event[in] The event to handle.
  149. */
  150. void Input(const QGears::Event& event);
  151. /**
  152. * Updates the state of all scripts of a given type.
  153. *
  154. * @param type[in] 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 lua[in] 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 file[in] 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 type[in] Type of entity to add.
  188. * @param entity_name[in] The entity name.
  189. * @param entity[in] 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 type[in] Type of the entity to remove.
  201. * @param entity_name[in] 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 entity_name[in] Name of the entity to add a script to.
  208. * @param function_name[in] Name of the script to add.
  209. * @param priority[in] 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 entiry[in] Enthity whose first script to remove.
  220. */
  221. void RemoveEntityTopScript(ScriptEntity& entity);
  222. /**
  223. * @todo Understand and document.
  224. *
  225. * @param type[in] Type of script.
  226. * @param name[in] Script name.
  227. * @param state[in] Initial script state.
  228. * @return @todo.
  229. */
  230. luabind::object GetTableByEntityName(
  231. const ScriptManager::Type type, const Ogre::String& name,
  232. lua_State* state
  233. ) const;
  234. /**
  235. * Retrieves a script from it's ID.
  236. *
  237. * @param script[in] Script ID.
  238. * @return The script with the corresponding ID, or nullptr if there is
  239. * none
  240. */
  241. QueueScript* GetScriptByScriptId(const ScriptId& script) const;
  242. /**
  243. * Retrieves a script entity by it's name and type.
  244. *
  245. * @param type[in] Entity type.
  246. * @param entity_name[in] The entity name.
  247. * @return The entity by that type and name, or nullptr if there is
  248. * no one that matches.
  249. */
  250. ScriptEntity* GetScriptEntityByName(
  251. const Type type, const Ogre::String& entity_name
  252. ) const;
  253. /**
  254. * Retrieves the current script ID.
  255. *
  256. * @return The currently executing script ID.
  257. */
  258. const ScriptId GetCurrentScriptId() const;
  259. /**
  260. * continues the execution of a script.
  261. *
  262. * @param script[in] The ID of the script to resume.
  263. */
  264. void ContinueScriptExecution(const ScriptId& script);
  265. /**
  266. * Makes an script wait.
  267. *
  268. * @param seconds[in] Number of seconds to wait.
  269. * @todo Does it refer to the current script?
  270. */
  271. int ScriptWait(const float seconds);
  272. /**
  273. * Request an script execution.
  274. *
  275. * @param type[in] Script type.
  276. * @param entity[in] Entity the scripts belong to.
  277. * @param function[in] Name of the function of the selected entity to
  278. * execute.
  279. * @param priority[in] Execution priority.
  280. */
  281. void ScriptRequest(
  282. const Type type, const char* entity,
  283. const char* function, const int priority
  284. );
  285. /**
  286. * Request a synchronous script execution to start.
  287. *
  288. * @param type[in] Script type.
  289. * @param entity[in] Entity the scripts belong to.
  290. * @param function[in] Name of the function of the selected entity to
  291. * execute.
  292. * @param priority[in] Execution priority.
  293. * @return -1 on success, 1 if the entity or the script don't exist.
  294. */
  295. int ScriptRequestStartSync(
  296. const Type type, const char* entity,
  297. const char* function, const int priority);
  298. /**
  299. * Request a synchronous script execution to end.
  300. *
  301. * @param type[in] Script type.
  302. * @param entity[in] Entity the scripts belong to.
  303. * @param function[in] Name of the function of the selected entity to
  304. * execute.
  305. * @param priority[in] Execution priority.
  306. * @return -1 if the execution stops or if the script was not running,
  307. * 1 if the entity or the script don't exist.
  308. */
  309. int ScriptRequestEndSync(
  310. const Type type, const char* entity,
  311. const char* function, const int priority
  312. );
  313. /**
  314. * Request a script execution.
  315. *
  316. * @param script_entity[in] Entity the scripts belong to.
  317. * @param function[in] Name of the function of the selected entity to
  318. * execute.
  319. * @param priority[in] Execution priority.
  320. * @param argument1[in] First argument for the script.
  321. * @param argument2[in] Second argument for the script.
  322. * @param start_sync[in] If true, the script will be started
  323. * synchronously.
  324. * @param end_sync[in] @todo Understand and document.
  325. * @return True on success, false on error (i.e. if the entity or the
  326. * script don't exist)
  327. */
  328. bool ScriptRequest(
  329. ScriptEntity* script_entity, const Ogre::String& function,
  330. const int priority, const Ogre::String& argument1,
  331. const Ogre::String& argument2, bool start_sync, bool end_sync
  332. );
  333. /**
  334. * Adds a script to the stack.
  335. *
  336. * @param value[in] ID of the script to add.
  337. * @todo Verify this documentation.
  338. */
  339. void AddValueToStack(const float value);
  340. private:
  341. /**
  342. * Lua state.
  343. */
  344. lua_State* lua_state_;
  345. /**
  346. * The system script table name.
  347. */
  348. Ogre::String system_table_name_;
  349. /**
  350. * The entity script table name.
  351. */
  352. Ogre::String entity_table_name_;
  353. /**
  354. * The UI script table name.
  355. */
  356. Ogre::String ui_table_name_;
  357. /**
  358. * The list of script entities.
  359. */
  360. std::vector<ScriptEntity> script_entity_;
  361. /**
  362. * The current script ID.
  363. */
  364. ScriptId current_script_id_;
  365. };
  366. struct ScriptEntity{
  367. /**
  368. * Constructor.
  369. *
  370. *By default, the type is {@see ScriptManager::SYSTEM}.
  371. */
  372. ScriptEntity():
  373. name(""),
  374. type(ScriptManager::SYSTEM),
  375. resort(false)
  376. {}
  377. /**
  378. * The script name.
  379. */
  380. Ogre::String name;
  381. /**
  382. * The script type.
  383. */
  384. ScriptManager::Type type;
  385. /**
  386. * The script queue.
  387. */
  388. std::vector<QueueScript> queue;
  389. /**
  390. * @todo Understand and document.
  391. */
  392. bool resort;
  393. };