ScriptManager.h 14 KB

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