ScriptManager.h 18 KB

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