فهرست منبع

Removed the upward dependencies between the source layers and a game file

- Removed wm0.ev, a copy of a game file that nothing used.
- Moved XmlFile to common, the walkmesh triangle to formats, the enemy and formation loaders to the battle module, the game loop to app and the game state to the engine.
- The engine and the modules use the application through ApplicationInterface, and use the battle and world map modules through BattleInterface and WorldMapInterface, which the modules implement.
- The Lua bindings of the battle and world map modules are now part of the modules.
- Added a note to the TODO about the display breaking when the resolution is changed.
Iñigo Valentin 1 روز پیش
والد
کامیت
d76799d573
59فایلهای تغییر یافته به همراه676 افزوده شده و 282 حذف شده
  1. 2 1
      .github/copilot-instructions.md
  2. 6 0
      doc/TODO.md
  3. 7 5
      src/CMakeLists.txt
  4. 1 1
      src/app/Application.cpp
  5. 4 3
      src/app/Application.h
  6. 2 2
      src/app/GameFrameListener.cpp
  7. 0 0
      src/app/GameFrameListener.h
  8. 7 3
      src/app/main.cpp
  9. 1 1
      src/common/XmlFile.cpp
  10. 0 0
      src/common/XmlFile.h
  11. 80 0
      src/engine/ApplicationInterface.h
  12. 68 0
      src/engine/BattleInterface.h
  13. 1 1
      src/engine/GameState.cpp
  14. 0 0
      src/engine/GameState.h
  15. 94 0
      src/engine/WorldMapInterface.h
  16. 4 4
      src/engine/config/ConfigCmdHandlerCommands.h
  17. 6 10
      src/engine/entity/Entity.cpp
  18. 1 40
      src/engine/entity/Walkmesh.h
  19. 2 2
      src/engine/graphics/CameraManager.cpp
  20. 1 1
      src/engine/save/Savemap.cpp
  21. 2 1
      src/engine/script/ScriptManager.cpp
  22. 9 1
      src/engine/script/ScriptManager.h
  23. 4 163
      src/engine/script/ScriptManagerBinds.h
  24. 3 3
      src/engine/ui/Console.cpp
  25. 2 2
      src/engine/ui/UiWidget.cpp
  26. 1 1
      src/engine/xml/XmlAttackFile.h
  27. 1 1
      src/engine/xml/XmlBackground2DFile.h
  28. 1 1
      src/engine/xml/XmlBattleCharactersFile.h
  29. 1 1
      src/engine/xml/XmlBattleScenesFile.h
  30. 1 1
      src/engine/xml/XmlFontFile.h
  31. 1 1
      src/engine/xml/XmlFontsFile.h
  32. 1 1
      src/engine/xml/XmlMapFile.h
  33. 1 1
      src/engine/xml/XmlMapsFile.h
  34. 1 1
      src/engine/xml/XmlMusicsFile.h
  35. 1 1
      src/engine/xml/XmlPrototypesFile.h
  36. 1 1
      src/engine/xml/XmlScreenFile.h
  37. 1 1
      src/engine/xml/XmlScreensFile.h
  38. 1 1
      src/engine/xml/XmlScriptsFile.h
  39. 1 1
      src/engine/xml/XmlSoundsFile.h
  40. 1 1
      src/engine/xml/XmlTextFile.h
  41. 1 1
      src/engine/xml/XmlTextsFile.h
  42. 1 1
      src/engine/xml/XmlWalkmeshFile.h
  43. 1 1
      src/formats/FF7NameLookup.h
  44. 1 1
      src/formats/field/WalkmeshFile.h
  45. 58 0
      src/formats/field/WalkmeshTriangle.h
  46. 155 0
      src/modules/battle/BattleBinds.cpp
  47. 28 0
      src/modules/battle/BattleBinds.h
  48. 6 4
      src/modules/battle/BattleManager.cpp
  49. 4 2
      src/modules/battle/BattleManager.h
  50. 1 1
      src/modules/battle/Enemy.cpp
  51. 1 1
      src/modules/battle/XmlEnemyFile.cpp
  52. 1 1
      src/modules/battle/XmlEnemyFile.h
  53. 1 1
      src/modules/battle/XmlFormationFile.cpp
  54. 1 1
      src/modules/battle/XmlFormationFile.h
  55. 53 0
      src/modules/worldmap/WorldmapBinds.cpp
  56. 30 0
      src/modules/worldmap/WorldmapBinds.h
  57. 3 2
      src/modules/worldmap/WorldmapModule.cpp
  58. 8 6
      src/modules/worldmap/WorldmapModule.h
  59. BIN
      test/installer/decompiler/world/wm0.ev

+ 2 - 1
.github/copilot-instructions.md

@@ -10,11 +10,12 @@ V-Gears is a free, cross-platform reimplementation of the Final Fantasy VII engi
 
 ## Architecture
 
-- `src/app/` — the game entry point, the application class and the game state.
+- `src/app/` — the game entry point, the application class and the game loop.
 - `src/common/` — files, logger, strings, types and other utilities shared by all targets.
 - `src/formats/` — FF7 binary format parsers, by kind: `lgp/`, `field/`, `model/`, `texture/` and `worldmap/`. Most formats follow a triplet: `*File` (data), `*FileSerializer` (read/write), `*FileManager` (Ogre resource loading).
 - `src/engine/` — engine runtime, by subsystem: `audio/`, `config/`, `entity/`, `graphics/`, `input/`, `particles/`, `save/`, `script/` (`ScriptManager`, Lua bindings via sol2 and system LuaJIT), `ui/` and `xml/` (XML loaders).
 - `src/modules/` — game modules: `battle/` and `worldmap/`.
+- Layers, from low to high: `common/`, `formats/`, `engine/`, `modules/`, `app/`. A layer never includes a higher one: the engine reaches the application and the modules through its interfaces (`engine/*Interface.h`), and each module binds itself to Lua (`*Binds.cpp`).
 - `src/installer/` — Qt data installer (`v-gears-installer`): `gui/`, `cli/`, `steps/` (the installation process), `extractors/` (game data conversion), `formats/`, `support/`, `tables/` (data tables) and `decompiler/` (converts field and world scripts into Lua).
 - `src/third_party/sol/` — vendored sol2 single header. Do not restyle, modernize, or refactor it.
 - LuaJIT is a system dependency.

+ 6 - 0
doc/TODO.md

@@ -139,3 +139,9 @@ The fonts are built by the installer from WINDOW.BIN (see `WindowFonts`) and dra
 - The PC version has a font twice the resolution (24x24 cells) in `usfont_a_h.tex` and `usfont_b_h.tex` of menu_us.lgp, split in two textures. The installer could merge them in one image and write the XML for it, for sharper text at high resolutions.
 - The installer could write `ffvii_en.xml` from the width table in WINDOW.BIN (third file, one byte per character: 3 bits of lead, 5 bits of width) instead of keeping it by hand. The XML in the repository matches the table except for `! " , . : ?`, which were made tighter by hand.
 - Loading TTF files makes sense only to support fonts made by users, not for the fonts of the game.
+
+## Changing the resolution breaks the display
+
+The console command `/resolution <width> <height> [full screen]` leaves menus, fields, the world map and battles broken. Screenshots (`/screenshot`) work. It doesn't seem new: the resize functions (`CmdResolution`, `GameFrameListener::windowResized` and the `OnResize` of the managers and widgets) are the same as before the code reorganization, but it hasn't been compared in the game with an older build.
+
+To check, not verified: whether `windowResized` is called at all when the size is changed with `Ogre::RenderWindow::resize` or `setFullscreen`, and not by the window system. Several `OnResize` functions are also empty (battle, world map, dialogs).

+ 7 - 5
src/CMakeLists.txt

@@ -587,7 +587,7 @@ set(VGEARS_SOURCE_FILES
     app/main.cpp
     common/FF7Character.cpp
     common/FF7Common.cpp
-    app/GameState.cpp
+    engine/GameState.cpp
     common/File.cpp
     common/FileSystem.cpp
     formats/FF7NameLookup.cpp
@@ -609,6 +609,7 @@ set(VGEARS_SOURCE_FILES
     engine/graphics/MoviePlayer.cpp
     engine/graphics/MovieState.cpp
     engine/graphics/ScreenFadeState.cpp
+    modules/battle/BattleBinds.cpp
     modules/battle/BattleManager.cpp
     engine/graphics/CameraManager.cpp
     engine/config/ConfigCmd.cpp
@@ -627,7 +628,7 @@ set(VGEARS_SOURCE_FILES
     engine/entity/EntityModel.cpp
     engine/entity/EntityPoint.cpp
     engine/entity/EntityTrigger.cpp
-    engine/GameFrameListener.cpp
+    app/GameFrameListener.cpp
     engine/input/InputManager.cpp
     engine/Manager.cpp
     engine/particles/Particle.cpp
@@ -666,11 +667,11 @@ set(VGEARS_SOURCE_FILES
     engine/xml/XmlBackground2DFile.cpp
     engine/xml/XmlBattleCharactersFile.cpp
     engine/xml/XmlBattleScenesFile.cpp
-    engine/xml/XmlEnemyFile.cpp
-    engine/xml/XmlFile.cpp
+    modules/battle/XmlEnemyFile.cpp
+    common/XmlFile.cpp
     engine/xml/XmlFontFile.cpp
     engine/xml/XmlFontsFile.cpp
-    engine/xml/XmlFormationFile.cpp
+    modules/battle/XmlFormationFile.cpp
     engine/xml/XmlMapFile.cpp
     engine/xml/XmlMapsFile.cpp
     engine/xml/XmlMusicsFile.cpp
@@ -736,6 +737,7 @@ set(VGEARS_SOURCE_FILES
     formats/field/WalkmeshFileManager.cpp
     formats/field/WalkmeshFileXMLSerializer.cpp
     formats/field/FF7WalkmeshFileSerializer.cpp
+    modules/worldmap/WorldmapBinds.cpp
     modules/worldmap/WorldmapModule.cpp
 )
 endif()

+ 1 - 1
src/app/Application.cpp

@@ -24,7 +24,7 @@
 #include "app/Application.h"
 #include "common/DataDirectory.h"
 #include "common/OgrePlugins.h"
-#include "app/GameState.h"
+#include "engine/GameState.h"
 #include "formats/model/AFileManager.h"
 #include "formats/field/BackgroundFileManager.h"
 #include "formats/field/CameraMatrixFileManager.h"

+ 4 - 3
src/app/Application.h

@@ -23,13 +23,14 @@
 #include <OgreTextureManager.h>
 #include <memory>
 #include "common/TypeDefine.h"
+#include "engine/ApplicationInterface.h"
 
 namespace VGears{
 
     /**
      * The V-Gears main applicacion
      */
-    class Application : public Ogre::Singleton<Application>{
+    class Application : public Ogre::Singleton<Application>, public ApplicationInterface{
 
         public:
 
@@ -94,12 +95,12 @@ namespace VGears{
              *
              * @return Pointer to the Ogre window.
              */
-            Ogre::RenderWindow* getRenderWindow(void);
+            Ogre::RenderWindow* getRenderWindow() override;
 
             /**
              * The overlay system (it must be added to a scene manager as a render queue listener to draw overlays).
              */
-            Ogre::OverlaySystem* GetOverlaySystem(){return _overlay_system.get();}
+            Ogre::OverlaySystem* GetOverlaySystem() override{return _overlay_system.get();}
 
             /**
              * Retrieves the Ogre resource manager.

+ 2 - 2
src/engine/GameFrameListener.cpp → src/app/GameFrameListener.cpp

@@ -14,9 +14,9 @@
  */
 
 #include <OgreStringConverter.h>
-#include "app/GameState.h"
+#include "engine/GameState.h"
 #include "modules/battle/BattleManager.h"
-#include "engine/GameFrameListener.h"
+#include "app/GameFrameListener.h"
 #include "engine/graphics/CameraManager.h"
 #include "engine/config/ConfigVar.h"
 #include "engine/ui/Console.h"

+ 0 - 0
src/engine/GameFrameListener.h → src/app/GameFrameListener.h


+ 7 - 3
src/app/main.cpp

@@ -21,9 +21,10 @@
 #include <Overlay/OgreOverlayManager.h>
 #include <Overlay/OgreFontManager.h>
 #include <OIS/OIS.h>
-#include "app/GameState.h"
+#include "engine/GameState.h"
 #include "app/Application.h"
 #include "engine/audio/AudioManager.h"
+#include "modules/battle/BattleBinds.h"
 #include "modules/battle/BattleManager.h"
 #include "engine/graphics/CameraManager.h"
 #include "engine/config/ConfigCmdHandler.h"
@@ -32,7 +33,7 @@
 #include "engine/ui/Console.h"
 #include "engine/graphics/DebugDraw.h"
 #include "engine/entity/EntityManager.h"
-#include "engine/GameFrameListener.h"
+#include "app/GameFrameListener.h"
 #include "engine/input/InputManager.h"
 #include "common/Logger.h"
 #include "engine/save/SavemapHandler.h"
@@ -45,6 +46,7 @@
 #include "formats/field/LZSFLevelFileManager.h"
 #include "formats/lgp/LGPArchiveFactory.h"
 #include "formats/worldmap/WorldmapFileManager.h"
+#include "modules/worldmap/WorldmapBinds.h"
 #include "modules/worldmap/WorldmapModule.h"
 
 /**
@@ -154,7 +156,9 @@ int main(int argc, char *argv[]){
         auto worldMapModule = std::make_unique<VGears::WorldmapModule>();
 
         // Initialize after game managers because it attaches them to script.
-        auto script_manager = std::make_unique<ScriptManager>();
+        auto script_manager = std::make_unique<ScriptManager>(
+          std::vector<ScriptManager::ModuleBinder>{&BindBattleModule, &VGears::BindWorldmapModule}
+        );
 
         // Set base listener for usual game modules.
         auto frame_listener = std::make_unique<GameFrameListener>(window);

+ 1 - 1
src/engine/xml/XmlFile.cpp → src/common/XmlFile.cpp

@@ -15,7 +15,7 @@
 
 #include <OgreStringConverter.h>
 #include "common/Logger.h"
-#include "engine/xml/XmlFile.h"
+#include "common/XmlFile.h"
 
 XmlFile::XmlFile(const Ogre::String& file): file_(file){
     file_.SetCondenseWhiteSpace(false);

+ 0 - 0
src/engine/xml/XmlFile.h → src/common/XmlFile.h


+ 80 - 0
src/engine/ApplicationInterface.h

@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+namespace Ogre{
+    class OverlaySystem;
+    class RenderWindow;
+}
+
+namespace VGears{
+
+    /**
+     * What the engine and the modules need from the application.
+     *
+     * The application implements it, so the rest of the code doesn't depend on it. There is at
+     * most one instance, which registers itself when it is created.
+     */
+    class ApplicationInterface{
+
+        public:
+
+            /**
+             * Retrieves the application.
+             *
+             * @return The application, or nullptr if there isn't any.
+             */
+            static ApplicationInterface* Get(){return Instance();}
+
+            /**
+             * Retrieves the window of the game.
+             *
+             * @return The render window.
+             */
+            virtual Ogre::RenderWindow* getRenderWindow() = 0;
+
+            /**
+             * Retrieves the system that draws the Ogre overlays.
+             *
+             * @return The overlay system, or nullptr if there isn't any.
+             */
+            virtual Ogre::OverlaySystem* GetOverlaySystem() = 0;
+
+        protected:
+
+            /**
+             * Constructor. Registers the instance.
+             */
+            ApplicationInterface(){Instance() = this;}
+
+            /**
+             * Destructor. Unregisters the instance.
+             */
+            virtual ~ApplicationInterface(){if (Instance() == this) Instance() = nullptr;}
+
+        private:
+
+            /**
+             * Holds the registered instance.
+             *
+             * @return Reference to the instance pointer.
+             */
+            static ApplicationInterface*& Instance(){
+                static ApplicationInterface* instance = nullptr;
+                return instance;
+            }
+    };
+}

+ 68 - 0
src/engine/BattleInterface.h

@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+namespace VGears{
+
+    /**
+     * What the engine needs from the battle module.
+     *
+     * The battle manager implements it, so the engine doesn't depend on the module. There is at
+     * most one instance, which registers itself when it is created.
+     */
+    class BattleInterface{
+
+        public:
+
+            /**
+             * Retrieves the battle module.
+             *
+             * @return The battle module, or nullptr if there isn't any.
+             */
+            static BattleInterface* Get(){return Instance();}
+
+            /**
+             * Sets the battle mode of the following battles.
+             *
+             * @param[in] mode The battle mode.
+             */
+            virtual void ScriptSetMode(const int mode) = 0;
+
+        protected:
+
+            /**
+             * Constructor. Registers the instance.
+             */
+            BattleInterface(){Instance() = this;}
+
+            /**
+             * Destructor. Unregisters the instance.
+             */
+            virtual ~BattleInterface(){if (Instance() == this) Instance() = nullptr;}
+
+        private:
+
+            /**
+             * Holds the registered instance.
+             *
+             * @return Reference to the instance pointer.
+             */
+            static BattleInterface*& Instance(){
+                static BattleInterface* instance = nullptr;
+                return instance;
+            }
+    };
+}

+ 1 - 1
src/app/GameState.cpp → src/engine/GameState.cpp

@@ -13,7 +13,7 @@
  * GNU General Public License for more details.
  */
 
-#include "app/GameState.h"
+#include "engine/GameState.h"
 
 namespace VGears{
     GAME_STATE  g_ApplicationState;

+ 0 - 0
src/app/GameState.h → src/engine/GameState.h


+ 94 - 0
src/engine/WorldMapInterface.h

@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <OgreString.h>
+
+namespace VGears{
+
+    /**
+     * What the engine and the other modules need from the world map.
+     *
+     * The world map module implements it, so the rest of the code doesn't depend on the module.
+     * There is at most one instance, which registers itself when it is created.
+     */
+    class WorldMapInterface{
+
+        public:
+
+            /**
+             * Retrieves the world map.
+             *
+             * @return The world map, or nullptr if there isn't any.
+             */
+            static WorldMapInterface* Get(){return Instance();}
+
+            /**
+             * Hides the world map and stops it.
+             */
+            virtual void Suspend() = 0;
+
+            /**
+             * Hides the world map and stops it while a battle is on.
+             */
+            virtual void PauseForBattle() = 0;
+
+            /**
+             * Shows the world map again after a battle.
+             */
+            virtual void ResumeAfterBattle() = 0;
+
+            /**
+             * Retrieves the type of the terrain under an entity.
+             *
+             * @param[in] entity_name Name of the entity.
+             * @return Name of the terrain type, or an empty string if it is unknown.
+             */
+            virtual Ogre::String GetTerrainTypeUnderEntity(const char* entity_name) const = 0;
+
+            /**
+             * Retrieves the terrain triangle under an entity.
+             *
+             * @param[in] entity_name Name of the entity.
+             * @return Index of the triangle, or -1 if it is unknown.
+             */
+            virtual int GetTerrainTriangleUnderEntity(const char* entity_name) const = 0;
+
+        protected:
+
+            /**
+             * Constructor. Registers the instance.
+             */
+            WorldMapInterface(){Instance() = this;}
+
+            /**
+             * Destructor. Unregisters the instance.
+             */
+            virtual ~WorldMapInterface(){if (Instance() == this) Instance() = nullptr;}
+
+        private:
+
+            /**
+             * Holds the registered instance.
+             *
+             * @return Reference to the instance pointer.
+             */
+            static WorldMapInterface*& Instance(){
+                static WorldMapInterface* instance = nullptr;
+                return instance;
+            }
+    };
+}

+ 4 - 4
src/engine/config/ConfigCmdHandlerCommands.h

@@ -24,8 +24,8 @@
 #include "common/Logger.h"
 #include "engine/xml/XmlMapFile.h"
 #include "engine/xml/XmlMapsFile.h"
-#include "app/GameState.h"
-#include "app/Application.h"
+#include "engine/GameState.h"
+#include "engine/ApplicationInterface.h"
 #include "ConfigCmdHandler.h"
 #include "ConfigVarHandler.h"
 
@@ -369,7 +369,7 @@ void CmdResolution(const Ogre::StringVector& params){
         );
         return;
     }
-    Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
+    Ogre::RenderWindow* window = VGears::ApplicationInterface::Get()->getRenderWindow();
     if (params.size() >= 4){
         window->setFullscreen(
           Ogre::StringConverter::parseBool(params[3]), Ogre::StringConverter::parseInt(params[1]),
@@ -412,7 +412,7 @@ void CmdResolutionCompletition(Ogre::StringVector& complete_params){
  * @param[in] params Command parameters. Unused.
  */
 void CmdScreenshot(const Ogre::StringVector& params){
-    Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
+    Ogre::RenderWindow* window = VGears::ApplicationInterface::Get()->getRenderWindow();
     Ogre::String ret = window->writeContentsToTimestampedFile("screenshot_", ".png");
     Console::getSingleton().AddTextToOutput("Screenshot " + ret + " saved.");
 }

+ 6 - 10
src/engine/entity/Entity.cpp

@@ -21,7 +21,7 @@
 #include "engine/entity/Entity.h"
 #include "engine/entity/EntityManager.h"
 #include "common/Logger.h"
-#include "modules/worldmap/WorldmapModule.h"
+#include "engine/WorldMapInterface.h"
 
 ConfigVar cv_debug_entity("debug_entity", "Draw entity debug info", "0");
 
@@ -149,11 +149,9 @@ void Entity::UpdateDebug(){
         if (debug > 1){
             Ogre::String move_state_string[] = {"NONE", "WALKMESH", "LINEAR", "JUMP"};
             DEBUG_DRAW.Text(entity_pos, 0, 24, Ogre::StringConverter::toString(entity_pos));
-            if (EntityManager::getSingleton().IsWorldModule()){
-                const int triangle_index
-                  = VGears::WorldmapModule::getSingleton().GetTerrainTriangleUnderEntity(
-                    name_.c_str()
-                  );
+            const VGears::WorldMapInterface* world_map = VGears::WorldMapInterface::Get();
+            if (EntityManager::getSingleton().IsWorldModule() && world_map != nullptr){
+                const int triangle_index = world_map->GetTerrainTriangleUnderEntity(name_.c_str());
                 DEBUG_DRAW.Text(
                   entity_pos, 0, 36,
                   "World map triangle: "
@@ -168,11 +166,9 @@ void Entity::UpdateDebug(){
                 );
             }
             DEBUG_DRAW.Text(entity_pos, 0, 48, "Move state: " + move_state_string[state_]);
-            if (EntityManager::getSingleton().IsWorldModule()){
+            if (EntityManager::getSingleton().IsWorldModule() && world_map != nullptr){
                 const Ogre::String terrain_name
-                  = VGears::WorldmapModule::getSingleton().GetTerrainTypeUnderEntity(
-                    name_.c_str()
-                  );
+                  = world_map->GetTerrainTypeUnderEntity(name_.c_str());
                 DEBUG_DRAW.Text(
                   entity_pos, 0, 60,
                   "Terrain: " + (terrain_name.empty() ? Ogre::String("UNKNOWN") : terrain_name)

+ 1 - 40
src/engine/entity/Walkmesh.h

@@ -17,46 +17,7 @@
 
 #include <Ogre.h>
 #include <vector>
-
-/**
- * A triangle of a walkmesh.
- */
-struct WalkmeshTriangle{
-
-    /**
-     * Constructor.
-     */
-    WalkmeshTriangle(): a(0, 0, 0), b(0, 0, 0), c(0, 0, 0), locked(false){
-        access_side[0] = -1;
-        access_side[1] = -1;
-        access_side[2] = -1;
-    }
-
-    /**
-     * A side of the triangle.
-     */
-    Ogre::Vector3 a;
-
-    /**
-     * A side of the triangle.
-     */
-    Ogre::Vector3 b;
-
-    /**
-     * A side of the triangle.
-     */
-    Ogre::Vector3 c;
-
-    /**
-     * @todo Understand and document.
-     */
-    int access_side[3];
-
-    /**
-     * Indicates if the triangle is locked (if it's walkable).
-     */
-    bool locked;
-};
+#include "formats/field/WalkmeshTriangle.h"
 
 namespace VGears{
     typedef Ogre::SharedPtr<class WalkmeshFile> WalkmeshFilePtr;

+ 2 - 2
src/engine/graphics/CameraManager.cpp

@@ -20,7 +20,7 @@
 #include <OIS/OIS.h>
 #include "engine/graphics/CameraManager.h"
 #include "engine/graphics/CameraManagerCommands.h"
-#include "app/Application.h"
+#include "engine/ApplicationInterface.h"
 #include "engine/config/ConfigVar.h"
 #include "engine/input/InputManager.h"
 #include "common/Logger.h"
@@ -58,7 +58,7 @@ CameraManager::CameraManager():
     //camera_->lookAt(Ogre::Vector3(0, 0, 0));
     Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode()
       ->lookAt(Ogre::Vector3(0, 0, 0), Ogre::Node::TS_LOCAL);
-    viewport_ = VGears::Application::getSingleton().getRenderWindow()->addViewport(camera_, 0);
+    viewport_ = VGears::ApplicationInterface::Get()->getRenderWindow()->addViewport(camera_, 0);
     viewport_->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
     camera_->setAspectRatio(
       Ogre::Real(viewport_->getActualWidth()) / Ogre::Real(viewport_->getActualHeight())

+ 1 - 1
src/engine/save/Savemap.cpp

@@ -20,7 +20,7 @@
 #include <tinyxml.h>
 #include "engine/save/Savemap.h"
 #include "common/Logger.h"
-#include "engine/xml/XmlFile.h"
+#include "common/XmlFile.h"
 #include "common/TypeDefine.h"
 
 constexpr unsigned int Savemap::MAX_CHARACTERS;

+ 2 - 1
src/engine/script/ScriptManager.cpp

@@ -46,7 +46,7 @@ Ogre::String script_entity_type[] = {"SYSTEM", "ENTITY", "UI", "BATTLE"};
  */
 bool priority_queue_compare(QueueScript a, QueueScript b){return a.priority < b.priority;}
 
-ScriptManager::ScriptManager():
+ScriptManager::ScriptManager(const std::vector<ModuleBinder>& module_binders):
   system_table_name_("System"), entity_table_name_("EntityContainer"), ui_table_name_("UiContainer")
 {
     lua_state_ = lua_open();
@@ -57,6 +57,7 @@ ScriptManager::ScriptManager():
     luaopen_math(lua_state_);
     luaopen_io(lua_state_);
     InitBinds();
+    for (ModuleBinder bind : module_binders) bind(lua_state_);
     InitCmd();
     // TODO: Use the one in data/ ?
     RunFile("system/system.lua");

+ 9 - 1
src/engine/script/ScriptManager.h

@@ -17,6 +17,7 @@
 
 #include <OgreSingleton.h>
 #include <OgreString.h>
+#include <vector>
 #include "engine/Event.h"
 #include "LuaIncludes.h"
 #include "engine/Manager.h"
@@ -185,10 +186,17 @@ class ScriptManager : public Manager, public Ogre::Singleton<ScriptManager>{
             BATTLE
         };
 
+        /**
+         * Function that adds the bindings of a module to the Lua state.
+         */
+        typedef void (*ModuleBinder)(lua_State* state);
+
         /**
          * Constructor.
+         *
+         * @param[in] module_binders Each one adds the bindings of a module before any script runs.
          */
-        ScriptManager();
+        explicit ScriptManager(const std::vector<ModuleBinder>& module_binders = {});
 
         /**
          * Destructor.

+ 4 - 163
src/engine/script/ScriptManagerBinds.h

@@ -15,14 +15,13 @@
 
 #include <iostream>
 
-#include "../modules/worldmap/WorldmapModule.h"
+#include "engine/BattleInterface.h"
+#include "engine/WorldMapInterface.h"
 #include "engine/ui/Console.h"
 #include "common/Logger.h"
 #include "engine/graphics/CameraManager.h"
-#include "modules/battle/Enemy.h"
 #include "engine/entity/Entity.h"
 #include "engine/entity/EntityManager.h"
-#include "modules/battle/BattleManager.h"
 #include "engine/audio/AudioManager.h"
 #include "engine/input/InputManager.h"
 #include "engine/save/SavemapHandler.h"
@@ -53,12 +52,12 @@ void ScriptPrint(const char* text){
  * @param[in] text The new map name.
  */
 void ScriptMap(const char* text){
-  VGears::WorldmapModule::getSingleton().Suspend();
+    if (VGears::WorldMapInterface::Get() != nullptr) VGears::WorldMapInterface::Get()->Suspend();
   EntityManager::getSingleton().SetFieldModule();
     EntityManager::getSingleton().Clear();
     // The battle music and mode are set by the scripts of each field.
     AudioManager::getSingleton().ScriptSetBattleTrack(-1);
-    BattleManager::getSingleton().ScriptSetMode(0);
+    if (VGears::BattleInterface::Get() != nullptr) VGears::BattleInterface::Get()->ScriptSetMode(0);
     XmlMapsFile xml("./data/fields/_fields.xml");
     Ogre::String file_name = xml.GetMapFileNameByName(text);
     XmlMapFile xml_map("./data/fields/" + file_name);
@@ -236,135 +235,6 @@ void ScriptManager::InitBinds(){
       "set_entity_to_character", &EntityManager::SetEntityToCharacter
     );
 
-    // Commands for the battle manager.
-    lua.new_usertype<BattleManager>(
-      "BattleManager",
-      "start_battle", &BattleManager::StartBattle,
-      "end_battle", &BattleManager::EndBattle,
-      "get_enemy_count", &BattleManager::ScriptGetEnemyCount,
-      "get_enemy", &BattleManager::ScriptGetEnemy,
-      "add_attack_model", &BattleManager::ScriptAddAttackModel,
-      "add_effect_model", &BattleManager::ScriptAddEffectModel,
-      "remove_effect_model", &BattleManager::ScriptRemoveEffectModel,
-      "has_attack", &BattleManager::HasAttackData,
-      "set_camera", &BattleManager::ScriptSetCamera,
-      "set_camera_position", &BattleManager::ScriptSetCameraPosition,
-      "get_camera", &BattleManager::ScriptGetCamera,
-      "create_dynamic_mesh", &BattleManager::ScriptCreateDynamicMesh,
-      "set_dynamic_mesh", &BattleManager::ScriptSetDynamicMesh,
-      "remove_dynamic_mesh", &BattleManager::ScriptRemoveDynamicMesh,
-      "get_layout", &BattleManager::ScriptGetLayout,
-      "get_intro_camera", &BattleManager::ScriptGetIntroCamera,
-      "get_attack", [](BattleManager& manager, const int id, sol::this_state state) -> sol::object{
-          sol::state_view view(state);
-          const AttackData* attack = manager.GetAttackData(id);
-          if (attack == nullptr) return sol::make_object(view, sol::nil);
-          sol::table result = view.create_table();
-          result["id"] = attack->id;
-          result["name"] = attack->name;
-          result["mp"] = attack->mp;
-          result["reflectable"] = attack->reflectable;
-          sol::table damage = view.create_table();
-          for (const auto& p : attack->damage) damage[p.first] = p.second;
-          result["damage"] = damage;
-          sol::table target = view.create_table();
-          for (const auto& p : attack->target) target[p.first] = p.second;
-          result["target"] = target;
-          sol::table animation = view.create_table();
-          for (const auto& p : attack->animation) animation[p.first] = p.second;
-          result["animation"] = animation;
-          sol::table effects = view.create_table();
-          int e = 1;
-          for (const auto& effect : attack->effects){
-              sol::table entry = view.create_table();
-              entry["id"] = effect.id;
-              entry["chance"] = effect.chance;
-              entry["mode"] = effect.mode;
-              effects[e ++] = entry;
-          }
-          result["effects"] = effects;
-          sol::table elements = view.create_table();
-          int n = 1;
-          for (int element : attack->elements) elements[n ++] = element;
-          result["elements"] = elements;
-          return result;
-      },
-      "get_next_formation_id", &BattleManager::GetNextFormationId,
-      "set_mode", &BattleManager::ScriptSetMode,
-      "get_mode", &BattleManager::GetMode,
-      "get_formation_id", &BattleManager::GetFormationId,
-      "get_escape_difficulty", &BattleManager::GetEscapeDifficulty,
-      "is_preemptive_allowed", &BattleManager::IsPreemptiveAllowed,
-      "show_victory_pose", &BattleManager::ShowVictoryPose,
-      "show_spoils", &BattleManager::ShowSpoils,
-      "set_enemy_visible", &BattleManager::ScriptSetEnemyVisible,
-      "get_enemy_entity_name", &BattleManager::ScriptGetEnemyEntityName,
-      "get_party_entity_name", &BattleManager::ScriptGetPartyEntityName,
-      "get_party_animation_prefix", &BattleManager::ScriptGetPartyAnimationPrefix,
-      "get_party_model_name", &BattleManager::ScriptGetPartyModelName
-    );
-
-    // Commands for enemy handling.
-    lua.new_usertype<Enemy>(
-      "Enemy",
-      "get_id", &Enemy::GetId,
-      "get_name", &Enemy::GetName,
-      "get_level", &Enemy::GetLevel,
-      "get_ap", &Enemy::GetAp,
-      "get_exp", &Enemy::GetExp,
-      "get_money", &Enemy::GetMoney,
-      "get_str", &Enemy::GetStr,
-      "get_def", &Enemy::GetDef,
-      "get_mag", &Enemy::GetMag,
-      "get_spr", &Enemy::GetSpr,
-      "get_dex", &Enemy::GetDex,
-      "get_lck", &Enemy::GetLck,
-      "get_eva", &Enemy::GetEva,
-      "get_meva", &Enemy::GetMeva,
-      "get_hp", &Enemy::GetHp,
-      "get_hp_max", &Enemy::GetHpMax,
-      "get_mp", &Enemy::GetMp,
-      "get_mp_max", &Enemy::GetMpMax,
-      "get_position", &Enemy::ScriptGetPos,
-      "is_front", &Enemy::IsFront,
-      "is_visible", &Enemy::IsVisible,
-      "is_targeteable", &Enemy::IsTargeteable,
-      "is_active", &Enemy::IsActive,
-      "get_attack_count", &Enemy::ScriptGetAttackCount,
-      "get_attack", &Enemy::ScriptGetAttack,
-      "get_camera_for_attack", &Enemy::ScriptGetCameraForAttack,
-      "get_drop_count", &Enemy::ScriptGetDropCount,
-      "get_animation_count", &Enemy::ScriptGetAnimationCount,
-      "get_attack_animation", &Enemy::ScriptGetAttackAnimation,
-      "get_element_count", &Enemy::ScriptGetElementCount,
-      "get_ai_script", &Enemy::ScriptGetAiScript,
-      "get_immunity_count", &Enemy::ScriptGetImmunityCount,
-      "get_immunity", &Enemy::ScriptGetImmunity,
-      "get_immunity_rate", &Enemy::ScriptGetImmunityRate,
-      "get_element", &Enemy::ScriptGetElement,
-      "get_element_factor", &Enemy::ScriptGetElementFactor,
-      "get_animation", &Enemy::ScriptGetAnimation,
-      "get_model_prefix", &Enemy::ScriptGetModelPrefix,
-      "get_model", &Enemy::GetModel,
-      "get_drop_id", &Enemy::ScriptGetDropId,
-      "get_drop_rate", &Enemy::ScriptGetDropRate,
-      "get_steal_count", &Enemy::ScriptGetStealCount,
-      "get_steal_id", &Enemy::ScriptGetStealId,
-      "get_steal_rate", &Enemy::ScriptGetStealRate,
-      "get_morph", [](const Enemy& enemy){return static_cast<int>(enemy.GetMorph());},
-      "get_manip_count", [](const Enemy& enemy){return static_cast<int>(enemy.GetManipulateAttacks().size());},
-      "get_manip_attack", [](const Enemy& enemy, const int index){
-          const std::vector<unsigned int> attacks = enemy.GetManipulateAttacks();
-          if (index < 0 || index >= static_cast<int>(attacks.size())) return -1;
-          return static_cast<int>(attacks[index]);
-      }
-      // TODO: Implement cover flags.
-      // TODO: Implement immunities
-      // TODO: Implement elements
-      // TODO: Implement stealable items
-      // TODO: Implement manipulability status and attacks.
-    );
-
     // Commands for the audio manager.
     lua.new_usertype<AudioManager>(
       "AudioManager",
@@ -649,38 +519,10 @@ void ScriptManager::InitBinds(){
     script_type["ENTITY"] = ScriptManager::ENTITY;
     script_type["UI"] = ScriptManager::UI;
 
-    // Commands to initiate modules
-    lua.new_usertype<VGears::WorldmapModule>(
-      "world_map_module",
-      "init", sol::overload(
-        [](VGears::WorldmapModule& module){module.Init(0);},
-        [](VGears::WorldmapModule& module, const int map_id){module.Init(map_id);}
-      ),
-      "suspend", &VGears::WorldmapModule::Suspend,
-      "clear_vehicle_terrains", &VGears::WorldmapModule::ClearVehicleTerrains,
-      "add_vehicle_terrain", &VGears::WorldmapModule::AddVehicleTerrain,
-      "get_terrain_type_under_entity", &VGears::WorldmapModule::GetTerrainTypeUnderEntity,
-      "get_terrain_triangle_under_entity", &VGears::WorldmapModule::GetTerrainTriangleUnderEntity,
-      "unboard_vehicle", &VGears::WorldmapModule::UnboardVehicle,
-      "set_encounters_locked", &VGears::WorldmapModule::SetEncountersLocked,
-      "set_model_orientation", &VGears::WorldmapModule::SetModelOrientation,
-      "set_model_heading", &VGears::WorldmapModule::SetModelHeading,
-      "get_model_heading", &VGears::WorldmapModule::GetModelHeading,
-      "set_model_offset", &VGears::WorldmapModule::SetModelOffset,
-      "set_entity_altitude", &VGears::WorldmapModule::SetEntityAltitude,
-      "enter", &VGears::WorldmapModule::Enter,
-      "set_spawn", &VGears::WorldmapModule::SetSpawn,
-      "set_speed", &VGears::WorldmapModule::SetSpeed,
-      "set_camera", &VGears::WorldmapModule::SetCamera,
-      "set_vehicle_hidden", &VGears::WorldmapModule::SetVehicleHidden,
-      "add_world_model", &VGears::WorldmapModule::AddWorldModelFromScript
-    );
-
     // Register all command handlers
     lua["input_manager"] = InputManager::getSingletonPtr();
     lua["camera_manager"] = CameraManager::getSingletonPtr();
     lua["entity_manager"] = EntityManager::getSingletonPtr();
-    lua["battle_manager"] = BattleManager::getSingletonPtr();
     lua["audio_manager"] = AudioManager::getSingletonPtr();
     lua["savemap_manager"] = SavemapHandler::getSingletonPtr();
     lua["background2d"] = EntityManager::getSingletonPtr()->GetBackground2D();
@@ -690,7 +532,6 @@ void ScriptManager::InitBinds(){
     lua["text_manager"] = TextHandler::getSingletonPtr();
     lua["dialog"] = DialogsManager::getSingletonPtr();
     lua["ui_manager"] = UiManager::getSingletonPtr();
-    lua["world_map_module"] = VGears::WorldmapModule::getSingletonPtr();
     lua["timer"] = Timer::getSingletonPtr();
     lua["script"] = this;
 }

+ 3 - 3
src/engine/ui/Console.cpp

@@ -15,7 +15,7 @@
 
 #include <OgreFontManager.h>
 #include <OgreRenderWindow.h>
-#include "app/Application.h"
+#include "engine/ApplicationInterface.h"
 #include "engine/ui/Console.h"
 #include "engine/graphics/DebugDraw.h"
 #include "common/Logger.h"
@@ -55,7 +55,7 @@ Console::Console():
     else LOG_ERROR("Console::frameStarted: Font for console not found.");
 
     // Calculate width and height of console depending on size of application
-    Ogre::RenderTarget *render_target( VGears::Application::getSingleton().getRenderWindow());
+    Ogre::RenderTarget *render_target( VGears::ApplicationInterface::Get()->getRenderWindow());
     console_width_ = render_target->getWidth();
     console_height_ = static_cast<int>(render_target->getHeight() / 2.5f);
     line_width_ = (console_width_ - 20) / 8;
@@ -379,7 +379,7 @@ void Console::UpdateNotification(){
 
 void Console::OnResize(){
     // Calculate width and height of console depending on size of application
-    Ogre::RenderTarget *render_target(VGears::Application::getSingleton().getRenderWindow());
+    Ogre::RenderTarget *render_target(VGears::ApplicationInterface::Get()->getRenderWindow());
     console_width_ = render_target->getWidth();
     console_height_ = static_cast<int>(render_target->getHeight() / 2.5f);
     line_width_ = (console_width_ - 20) / 8;

+ 2 - 2
src/engine/ui/UiWidget.cpp

@@ -17,7 +17,7 @@
 #include <OgreMath.h>
 #include <OgreRoot.h>
 #include <OgreViewport.h>
-#include "app/Application.h"
+#include "engine/ApplicationInterface.h"
 #include "engine/config/ConfigVar.h"
 #include "engine/graphics/CameraManager.h"
 #include "engine/graphics/DebugDraw.h"
@@ -305,7 +305,7 @@ void UiWidget::SetUpdateTransformation(){
 
 void UiWidget::UpdateTransformation(){
 
-    Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
+    Ogre::RenderWindow* window = VGears::ApplicationInterface::Get()->getRenderWindow();
     float res_width = static_cast<float>(window->getWidth());
     float res_height = static_cast<float>(window->getHeight());
     screen_width_ = res_width;

+ 1 - 1
src/engine/xml/XmlAttackFile.h

@@ -18,7 +18,7 @@
 #include <map>
 #include <string>
 #include <vector>
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Data of an attack, as stored in the attack XML files.

+ 1 - 1
src/engine/xml/XmlBackground2DFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles background XML files.

+ 1 - 1
src/engine/xml/XmlBattleCharactersFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles battle character model list xml files.

+ 1 - 1
src/engine/xml/XmlBattleScenesFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles battle scene list xml files.

+ 1 - 1
src/engine/xml/XmlFontFile.h

@@ -16,7 +16,7 @@
 #pragma once
 
 #include "engine/ui/UiWidget.h"
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles an XML font file.

+ 1 - 1
src/engine/xml/XmlFontsFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the fonts main file.

+ 1 - 1
src/engine/xml/XmlMapFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles XML map files.

+ 1 - 1
src/engine/xml/XmlMapsFile.h

@@ -16,7 +16,7 @@
 #pragma once
 
 #include <OgreStringVector.h>
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the main maps file.

+ 1 - 1
src/engine/xml/XmlMusicsFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles tha main musics file.

+ 1 - 1
src/engine/xml/XmlPrototypesFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the main XML prototypes file.

+ 1 - 1
src/engine/xml/XmlScreenFile.h

@@ -16,7 +16,7 @@
 #pragma once
 
 #include "engine/ui/UiWidget.h"
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles a screen file

+ 1 - 1
src/engine/xml/XmlScreensFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the main screens file.

+ 1 - 1
src/engine/xml/XmlScriptsFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the main scripts file

+ 1 - 1
src/engine/xml/XmlSoundsFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the main sounds file.

+ 1 - 1
src/engine/xml/XmlTextFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles a text file.

+ 1 - 1
src/engine/xml/XmlTextsFile.h

@@ -16,7 +16,7 @@
 #pragma once
 
 #include <OgreStringVector.h>
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles the main text file/

+ 1 - 1
src/engine/xml/XmlWalkmeshFile.h

@@ -14,7 +14,7 @@
  */
 
 #pragma once
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles a walkmesh file.

+ 1 - 1
src/formats/FF7NameLookup.h

@@ -17,7 +17,7 @@
 
 #include "common/TypeDefine.h"
 #include "common/StringUtil.h"
-#include "engine/xml/XmlFile.h"
+#include "common/XmlFile.h"
 
 // TODO: Move implementations to cpp file.
 

+ 1 - 1
src/formats/field/WalkmeshFile.h

@@ -16,7 +16,7 @@
 #pragma once
 
 #include "common/Resource.h"
-#include "engine/entity/Walkmesh.h"
+#include "formats/field/WalkmeshTriangle.h"
 
 namespace VGears{
 

+ 58 - 0
src/formats/field/WalkmeshTriangle.h

@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <OgreVector3.h>
+
+/**
+ * A triangle of a walkmesh.
+ */
+struct WalkmeshTriangle{
+
+    /**
+     * Constructor.
+     */
+    WalkmeshTriangle(): a(0, 0, 0), b(0, 0, 0), c(0, 0, 0), locked(false){
+        access_side[0] = -1;
+        access_side[1] = -1;
+        access_side[2] = -1;
+    }
+
+    /**
+     * A side of the triangle.
+     */
+    Ogre::Vector3 a;
+
+    /**
+     * A side of the triangle.
+     */
+    Ogre::Vector3 b;
+
+    /**
+     * A side of the triangle.
+     */
+    Ogre::Vector3 c;
+
+    /**
+     * @todo Understand and document.
+     */
+    int access_side[3];
+
+    /**
+     * Indicates if the triangle is locked (if it's walkable).
+     */
+    bool locked;
+};

+ 155 - 0
src/modules/battle/BattleBinds.cpp

@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include "modules/battle/BattleBinds.h"
+#include "engine/script/LuaIncludes.h"
+#include "modules/battle/BattleManager.h"
+#include "modules/battle/Enemy.h"
+
+void BindBattleModule(lua_State* state){
+    sol::state_view lua_view(state);
+    sol::table lua = lua_view.globals();
+
+    // Commands for the battle manager.
+    lua.new_usertype<BattleManager>(
+      "BattleManager",
+      "start_battle", &BattleManager::StartBattle,
+      "end_battle", &BattleManager::EndBattle,
+      "get_enemy_count", &BattleManager::ScriptGetEnemyCount,
+      "get_enemy", &BattleManager::ScriptGetEnemy,
+      "add_attack_model", &BattleManager::ScriptAddAttackModel,
+      "add_effect_model", &BattleManager::ScriptAddEffectModel,
+      "remove_effect_model", &BattleManager::ScriptRemoveEffectModel,
+      "has_attack", &BattleManager::HasAttackData,
+      "set_camera", &BattleManager::ScriptSetCamera,
+      "set_camera_position", &BattleManager::ScriptSetCameraPosition,
+      "get_camera", &BattleManager::ScriptGetCamera,
+      "create_dynamic_mesh", &BattleManager::ScriptCreateDynamicMesh,
+      "set_dynamic_mesh", &BattleManager::ScriptSetDynamicMesh,
+      "remove_dynamic_mesh", &BattleManager::ScriptRemoveDynamicMesh,
+      "get_layout", &BattleManager::ScriptGetLayout,
+      "get_intro_camera", &BattleManager::ScriptGetIntroCamera,
+      "get_attack", [](BattleManager& manager, const int id, sol::this_state state) -> sol::object{
+          sol::state_view view(state);
+          const AttackData* attack = manager.GetAttackData(id);
+          if (attack == nullptr) return sol::make_object(view, sol::nil);
+          sol::table result = view.create_table();
+          result["id"] = attack->id;
+          result["name"] = attack->name;
+          result["mp"] = attack->mp;
+          result["reflectable"] = attack->reflectable;
+          sol::table damage = view.create_table();
+          for (const auto& p : attack->damage) damage[p.first] = p.second;
+          result["damage"] = damage;
+          sol::table target = view.create_table();
+          for (const auto& p : attack->target) target[p.first] = p.second;
+          result["target"] = target;
+          sol::table animation = view.create_table();
+          for (const auto& p : attack->animation) animation[p.first] = p.second;
+          result["animation"] = animation;
+          sol::table effects = view.create_table();
+          int e = 1;
+          for (const auto& effect : attack->effects){
+              sol::table entry = view.create_table();
+              entry["id"] = effect.id;
+              entry["chance"] = effect.chance;
+              entry["mode"] = effect.mode;
+              effects[e ++] = entry;
+          }
+          result["effects"] = effects;
+          sol::table elements = view.create_table();
+          int n = 1;
+          for (int element : attack->elements) elements[n ++] = element;
+          result["elements"] = elements;
+          return result;
+      },
+      "get_next_formation_id", &BattleManager::GetNextFormationId,
+      "set_mode", &BattleManager::ScriptSetMode,
+      "get_mode", &BattleManager::GetMode,
+      "get_formation_id", &BattleManager::GetFormationId,
+      "get_escape_difficulty", &BattleManager::GetEscapeDifficulty,
+      "is_preemptive_allowed", &BattleManager::IsPreemptiveAllowed,
+      "show_victory_pose", &BattleManager::ShowVictoryPose,
+      "show_spoils", &BattleManager::ShowSpoils,
+      "set_enemy_visible", &BattleManager::ScriptSetEnemyVisible,
+      "get_enemy_entity_name", &BattleManager::ScriptGetEnemyEntityName,
+      "get_party_entity_name", &BattleManager::ScriptGetPartyEntityName,
+      "get_party_animation_prefix", &BattleManager::ScriptGetPartyAnimationPrefix,
+      "get_party_model_name", &BattleManager::ScriptGetPartyModelName
+    );
+
+    // Commands for enemy handling.
+    lua.new_usertype<Enemy>(
+      "Enemy",
+      "get_id", &Enemy::GetId,
+      "get_name", &Enemy::GetName,
+      "get_level", &Enemy::GetLevel,
+      "get_ap", &Enemy::GetAp,
+      "get_exp", &Enemy::GetExp,
+      "get_money", &Enemy::GetMoney,
+      "get_str", &Enemy::GetStr,
+      "get_def", &Enemy::GetDef,
+      "get_mag", &Enemy::GetMag,
+      "get_spr", &Enemy::GetSpr,
+      "get_dex", &Enemy::GetDex,
+      "get_lck", &Enemy::GetLck,
+      "get_eva", &Enemy::GetEva,
+      "get_meva", &Enemy::GetMeva,
+      "get_hp", &Enemy::GetHp,
+      "get_hp_max", &Enemy::GetHpMax,
+      "get_mp", &Enemy::GetMp,
+      "get_mp_max", &Enemy::GetMpMax,
+      "get_position", &Enemy::ScriptGetPos,
+      "is_front", &Enemy::IsFront,
+      "is_visible", &Enemy::IsVisible,
+      "is_targeteable", &Enemy::IsTargeteable,
+      "is_active", &Enemy::IsActive,
+      "get_attack_count", &Enemy::ScriptGetAttackCount,
+      "get_attack", &Enemy::ScriptGetAttack,
+      "get_camera_for_attack", &Enemy::ScriptGetCameraForAttack,
+      "get_drop_count", &Enemy::ScriptGetDropCount,
+      "get_animation_count", &Enemy::ScriptGetAnimationCount,
+      "get_attack_animation", &Enemy::ScriptGetAttackAnimation,
+      "get_element_count", &Enemy::ScriptGetElementCount,
+      "get_ai_script", &Enemy::ScriptGetAiScript,
+      "get_immunity_count", &Enemy::ScriptGetImmunityCount,
+      "get_immunity", &Enemy::ScriptGetImmunity,
+      "get_immunity_rate", &Enemy::ScriptGetImmunityRate,
+      "get_element", &Enemy::ScriptGetElement,
+      "get_element_factor", &Enemy::ScriptGetElementFactor,
+      "get_animation", &Enemy::ScriptGetAnimation,
+      "get_model_prefix", &Enemy::ScriptGetModelPrefix,
+      "get_model", &Enemy::GetModel,
+      "get_drop_id", &Enemy::ScriptGetDropId,
+      "get_drop_rate", &Enemy::ScriptGetDropRate,
+      "get_steal_count", &Enemy::ScriptGetStealCount,
+      "get_steal_id", &Enemy::ScriptGetStealId,
+      "get_steal_rate", &Enemy::ScriptGetStealRate,
+      "get_morph", [](const Enemy& enemy){return static_cast<int>(enemy.GetMorph());},
+      "get_manip_count", [](const Enemy& enemy){return static_cast<int>(enemy.GetManipulateAttacks().size());},
+      "get_manip_attack", [](const Enemy& enemy, const int index){
+          const std::vector<unsigned int> attacks = enemy.GetManipulateAttacks();
+          if (index < 0 || index >= static_cast<int>(attacks.size())) return -1;
+          return static_cast<int>(attacks[index]);
+      }
+      // TODO: Implement cover flags.
+      // TODO: Implement immunities
+      // TODO: Implement elements
+      // TODO: Implement stealable items
+      // TODO: Implement manipulability status and attacks.
+    );
+
+    lua["battle_manager"] = BattleManager::getSingletonPtr();
+}

+ 28 - 0
src/modules/battle/BattleBinds.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+struct lua_State;
+
+/**
+ * Makes the battle manager and the enemies available to the scripts. The manager is
+ * "battle_manager".
+ *
+ * The battle manager must have been created.
+ *
+ * @param[in] state Lua state of the scripts.
+ */
+void BindBattleModule(lua_State* state);

+ 6 - 4
src/modules/battle/BattleManager.cpp

@@ -28,7 +28,7 @@
 #include "common/FF7Character.h"
 #include "engine/audio/AudioManager.h"
 #include "modules/battle/BattleManager.h"
-#include "modules/worldmap/WorldmapModule.h"
+#include "engine/WorldMapInterface.h"
 #include "engine/graphics/CameraManager.h"
 #include "engine/ui/Console.h"
 #include "engine/ui/DialogsManager.h"
@@ -40,7 +40,7 @@
 #include "engine/script/ScriptManager.h"
 #include "engine/ui/TextHandler.h"
 #include "engine/ui/UiManager.h"
-#include "engine/xml/XmlFormationFile.h"
+#include "modules/battle/XmlFormationFile.h"
 
 /**
  * Battle manager singleton.
@@ -132,7 +132,8 @@ void BattleManager::StartBattle(const unsigned int id){
     }
     // Battles can start in the world map, which is hidden and stopped while the battle lasts.
     battle_from_world_ = EntityManager::getSingleton().IsWorldModule();
-    if (battle_from_world_) VGears::WorldmapModule::getSingleton().PauseForBattle();
+    if (battle_from_world_ && VGears::WorldMapInterface::Get() != nullptr)
+        VGears::WorldMapInterface::Get()->PauseForBattle();
     SetBattleModule();
     AudioManager::getSingleton().SetBattleModule();
     CameraManager::getSingleton().SetBattleModule();
@@ -203,7 +204,8 @@ void BattleManager::EndBattle(){
     UiManager::getSingleton().SetPreviousModule();
     if (battle_from_world_){
         battle_from_world_ = false;
-        VGears::WorldmapModule::getSingleton().ResumeAfterBattle();
+        if (VGears::WorldMapInterface::Get() != nullptr)
+            VGears::WorldMapInterface::Get()->ResumeAfterBattle();
     }
 }
 

+ 4 - 2
src/modules/battle/BattleManager.h

@@ -22,6 +22,7 @@
 #include <OgreManualObject.h>
 #include <OgreSingleton.h>
 #include "Enemy.h"
+#include "engine/BattleInterface.h"
 #include "engine/Event.h"
 #include "engine/Manager.h"
 #include "engine/xml/XmlBattleCharactersFile.h"
@@ -31,7 +32,8 @@
 /**
  * The battle manager.
  */
-class BattleManager : public Manager, public Ogre::Singleton<BattleManager>{
+class BattleManager :
+  public Manager, public Ogre::Singleton<BattleManager>, public VGears::BattleInterface{
 
     public:
 
@@ -383,7 +385,7 @@ class BattleManager : public Manager, public Ogre::Singleton<BattleManager>{
          *
          * @param[in] mode The flags.
          */
-        void ScriptSetMode(const int mode);
+        void ScriptSetMode(const int mode) override;
 
         /**
          * Retrieves the battle mode flags.

+ 1 - 1
src/modules/battle/Enemy.cpp

@@ -19,7 +19,7 @@
 #include "engine/config/ConfigVar.h"
 #include "engine/graphics/DebugDraw.h"
 #include "common/Logger.h"
-#include "engine/xml/XmlEnemyFile.h"
+#include "modules/battle/XmlEnemyFile.h"
 
 Enemy::Enemy(const unsigned int id):
   id_(id),

+ 1 - 1
src/engine/xml/XmlEnemyFile.cpp → src/modules/battle/XmlEnemyFile.cpp

@@ -13,7 +13,7 @@
  * GNU General Public License for more details.
  */
 
-#include "engine/xml/XmlEnemyFile.h"
+#include "modules/battle/XmlEnemyFile.h"
 #include "modules/battle/Enemy.h"
 #include "common/Logger.h"
 

+ 1 - 1
src/engine/xml/XmlEnemyFile.h → src/modules/battle/XmlEnemyFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 #include "modules/battle/Enemy.h"
 
 /**

+ 1 - 1
src/engine/xml/XmlFormationFile.cpp → src/modules/battle/XmlFormationFile.cpp

@@ -13,7 +13,7 @@
  * GNU General Public License for more details.
  */
 
-#include "engine/xml/XmlFormationFile.h"
+#include "modules/battle/XmlFormationFile.h"
 #include "modules/battle/BattleManager.h"
 #include "common/Logger.h"
 

+ 1 - 1
src/engine/xml/XmlFormationFile.h → src/modules/battle/XmlFormationFile.h

@@ -15,7 +15,7 @@
 
 #pragma once
 
-#include "XmlFile.h"
+#include "common/XmlFile.h"
 
 /**
  * Handles formation files.

+ 53 - 0
src/modules/worldmap/WorldmapBinds.cpp

@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include "modules/worldmap/WorldmapBinds.h"
+#include "engine/script/LuaIncludes.h"
+#include "modules/worldmap/WorldmapModule.h"
+
+namespace VGears{
+
+    void BindWorldmapModule(lua_State* state){
+        sol::state_view lua_view(state);
+        sol::table lua = lua_view.globals();
+
+        lua.new_usertype<WorldmapModule>(
+          "world_map_module",
+          "init", sol::overload(
+            [](WorldmapModule& module){module.Init(0);},
+            [](WorldmapModule& module, const int map_id){module.Init(map_id);}
+          ),
+          "suspend", &WorldmapModule::Suspend,
+          "clear_vehicle_terrains", &WorldmapModule::ClearVehicleTerrains,
+          "add_vehicle_terrain", &WorldmapModule::AddVehicleTerrain,
+          "get_terrain_type_under_entity", &WorldmapModule::GetTerrainTypeUnderEntity,
+          "get_terrain_triangle_under_entity", &WorldmapModule::GetTerrainTriangleUnderEntity,
+          "unboard_vehicle", &WorldmapModule::UnboardVehicle,
+          "set_encounters_locked", &WorldmapModule::SetEncountersLocked,
+          "set_model_orientation", &WorldmapModule::SetModelOrientation,
+          "set_model_heading", &WorldmapModule::SetModelHeading,
+          "get_model_heading", &WorldmapModule::GetModelHeading,
+          "set_model_offset", &WorldmapModule::SetModelOffset,
+          "set_entity_altitude", &WorldmapModule::SetEntityAltitude,
+          "enter", &WorldmapModule::Enter,
+          "set_spawn", &WorldmapModule::SetSpawn,
+          "set_speed", &WorldmapModule::SetSpeed,
+          "set_camera", &WorldmapModule::SetCamera,
+          "set_vehicle_hidden", &WorldmapModule::SetVehicleHidden,
+          "add_world_model", &WorldmapModule::AddWorldModelFromScript
+        );
+        lua["world_map_module"] = WorldmapModule::getSingletonPtr();
+    }
+}

+ 30 - 0
src/modules/worldmap/WorldmapBinds.h

@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+struct lua_State;
+
+namespace VGears{
+
+    /**
+     * Makes the world map module available to the scripts, as "world_map_module".
+     *
+     * The module must have been created.
+     *
+     * @param[in] state Lua state of the scripts.
+     */
+    void BindWorldmapModule(lua_State* state);
+}

+ 3 - 2
src/modules/worldmap/WorldmapModule.cpp

@@ -40,7 +40,8 @@
 #include <OgreHardwarePixelBuffer.h>
 #include <OgreLogManager.h>
 #include <OgreTextureManager.h>
-#include "app/Application.h"
+#include <OgreOverlaySystem.h>
+#include "engine/ApplicationInterface.h"
 #include <Overlay/OgreOverlay.h>
 #include <Overlay/OgreOverlayContainer.h>
 #include <Overlay/OgreOverlayManager.h>
@@ -808,7 +809,7 @@ namespace VGears {
         if (overlay == nullptr){
             if (!visible) return;
             // The engine doesn't draw the overlays of Ogre unless its system listens to the scene.
-            Ogre::OverlaySystem* overlay_system = VGears::Application::getSingleton().GetOverlaySystem();
+            Ogre::OverlaySystem* overlay_system = VGears::ApplicationInterface::Get()->GetOverlaySystem();
             if (overlay_system != nullptr){
                 Root::getSingleton().getSceneManager("Scene")->addRenderQueueListener(overlay_system);
             }

+ 8 - 6
src/modules/worldmap/WorldmapModule.h

@@ -23,6 +23,7 @@
 #include <set>
 #include <vector>
 #include "common/TypeDefine.h"
+#include "engine/WorldMapInterface.h"
 
 class EntityModel;
 
@@ -31,7 +32,8 @@ namespace VGears {
     /**
      * The world map module.
      */
-    class WorldmapModule : public Ogre::Singleton<WorldmapModule>, public Ogre::FrameListener{
+    class WorldmapModule :
+      public Ogre::Singleton<WorldmapModule>, public Ogre::FrameListener, public WorldMapInterface{
 
         public:
 
@@ -55,25 +57,25 @@ namespace VGears {
              * was already loaded, it is replaced.
              */
             void Init(const int map_id = 0);
-            void Suspend();
+            void Suspend() override;
 
             bool frameStarted(const Ogre::FrameEvent& event) override;
 
             void ClearVehicleTerrains();
             void AddVehicleTerrain(const char* terrain_name);
-            Ogre::String GetTerrainTypeUnderEntity(const char* entity_name) const;
-            int GetTerrainTriangleUnderEntity(const char* entity_name) const;
+            Ogre::String GetTerrainTypeUnderEntity(const char* entity_name) const override;
+            int GetTerrainTriangleUnderEntity(const char* entity_name) const override;
             bool UnboardVehicle(const char* vehicle_name);
 
             /**
              * Hides the world map and stops it while a battle is on.
              */
-            void PauseForBattle();
+            void PauseForBattle() override;
 
             /**
              * Shows the world map again after a battle.
              */
-            void ResumeAfterBattle();
+            void ResumeAfterBattle() override;
 
             /**
              * Prevents or allows random encounters (the SetBattleLock opcode).

BIN
test/installer/decompiler/world/wm0.ev