From 5b2b378f25f4c94d7885879899508cea70df140e Mon Sep 17 00:00:00 2001
From: greye <greye@null.net>
Date: Sat, 18 Aug 2012 18:05:10 +0400
Subject: [PATCH] tvm script instruction

---
 apps/openmw/mwbase/world.hpp              |  1 +
 apps/openmw/mwrender/player.hpp           |  4 ++++
 apps/openmw/mwrender/renderingmanager.hpp | 10 ++++++---
 apps/openmw/mwscript/docs/vmformat.txt    |  3 ++-
 apps/openmw/mwscript/miscextensions.cpp   | 27 +++++++++++++++++++++++
 apps/openmw/mwworld/worldimp.hpp          |  4 ++++
 6 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/apps/openmw/mwbase/world.hpp b/apps/openmw/mwbase/world.hpp
index 9c801cbff..5e97b4922 100644
--- a/apps/openmw/mwbase/world.hpp
+++ b/apps/openmw/mwbase/world.hpp
@@ -254,6 +254,7 @@ namespace MWBase
             virtual bool toggleVanityMode(bool enable, bool force) = 0;
             virtual void allowVanityMode(bool allow) = 0;
             virtual void togglePlayerLooking(bool enable) = 0;
+            virtual bool isVanityEnabled() = 0;
 
             virtual void renderPlayer() = 0;
     };
diff --git a/apps/openmw/mwrender/player.hpp b/apps/openmw/mwrender/player.hpp
index 8dd313b7f..e56abc17d 100644
--- a/apps/openmw/mwrender/player.hpp
+++ b/apps/openmw/mwrender/player.hpp
@@ -107,6 +107,10 @@ namespace MWRender
         void getSightAngles(float &pitch, float &yaw);
 
         void togglePlayerLooking(bool enable);
+
+        bool isVanityEnabled() {
+            return mVanity.enabled;
+        }
     };
 }
 
diff --git a/apps/openmw/mwrender/renderingmanager.hpp b/apps/openmw/mwrender/renderingmanager.hpp
index 17a4d5280..003a61a39 100644
--- a/apps/openmw/mwrender/renderingmanager.hpp
+++ b/apps/openmw/mwrender/renderingmanager.hpp
@@ -64,15 +64,19 @@ class RenderingManager: private RenderingInterface, public Ogre::WindowEventList
         mPlayer->togglePreviewMode(enable);
     }
 
-    virtual bool toggleVanityMode(bool enable, bool force) {
+    bool toggleVanityMode(bool enable, bool force) {
         return mPlayer->toggleVanityMode(enable, force);
     }
 
-    virtual void allowVanityMode(bool allow) {
+    bool isVanityEnabled() {
+        return mPlayer->isVanityEnabled();
+    }
+
+    void allowVanityMode(bool allow) {
         mPlayer->allowVanityMode(allow);
     }
 
-    virtual void togglePlayerLooking(bool enable) {
+    void togglePlayerLooking(bool enable) {
         mPlayer->togglePlayerLooking(enable);
     }
 
diff --git a/apps/openmw/mwscript/docs/vmformat.txt b/apps/openmw/mwscript/docs/vmformat.txt
index 1ea5f8e3e..dbc8159d1 100644
--- a/apps/openmw/mwscript/docs/vmformat.txt
+++ b/apps/openmw/mwscript/docs/vmformat.txt
@@ -181,4 +181,5 @@ op 0x2000170: user4, explicit reference (console only, requires --script-console
 op 0x2000171: user4 (implicit reference, console only, requires --script-console switch)
 op 0x2000172: GetStartingAngle
 op 0x2000173: GetStartingAngle, explicit reference
-opcodes 0x2000174-0x3ffffff unused
+op 0x2000174: ToggleVanityMode
+opcodes 0x2000175-0x3ffffff unused
diff --git a/apps/openmw/mwscript/miscextensions.cpp b/apps/openmw/mwscript/miscextensions.cpp
index 0c328e0da..e9e29bdae 100644
--- a/apps/openmw/mwscript/miscextensions.cpp
+++ b/apps/openmw/mwscript/miscextensions.cpp
@@ -207,6 +207,29 @@ namespace MWScript
                 }
         };
 
+        class OpToggleVanityMode : public Interpreter::Opcode0
+        {
+        public:
+        
+            virtual void execute(Interpreter::Runtime &runtime)
+            {
+                InterpreterContext& context =
+                    static_cast<InterpreterContext&> (runtime.getContext());
+
+                MWBase::World *world =
+                    MWBase::Environment::get().getWorld();
+
+                bool value = !world->isVanityEnabled();
+                if (world->toggleVanityMode(value, true)) {
+                    context.report(
+                        (value) ? "Vanity Mode -> On" : "Vanity Mode -> Off"
+                    );
+                } else {
+                    context.report("Vanity Mode -> No");
+                }
+            }
+        };
+
         const int opcodeXBox = 0x200000c;
         const int opcodeOnActivate = 0x200000d;
         const int opcodeActivate = 0x2000075;
@@ -222,6 +245,7 @@ namespace MWScript
         const int opcodeToggleWater = 0x2000144;
         const int opcodeTogglePathgrid = 0x2000146;
         const int opcodeDontSaveObject = 0x2000153;
+        const int opcodeToggleVanityMode = 0x2000174;
 
         void registerExtensions (Compiler::Extensions& extensions)
         {
@@ -244,6 +268,8 @@ namespace MWScript
             extensions.registerInstruction ("togglepathgrid", "", opcodeTogglePathgrid);
             extensions.registerInstruction ("tpg", "", opcodeTogglePathgrid);
             extensions.registerInstruction ("dontsaveobject", "", opcodeDontSaveObject);
+            extensions.registerInstruction ("togglevanitymode", "", opcodeToggleVanityMode);
+            extensions.registerInstruction ("tvm", "", opcodeToggleVanityMode);
         }
 
         void installOpcodes (Interpreter::Interpreter& interpreter)
@@ -263,6 +289,7 @@ namespace MWScript
             interpreter.installSegment5 (opcodeTogglePathgrid, new OpTogglePathgrid);
             interpreter.installSegment5 (opcodeToggleWater, new OpToggleWater);
             interpreter.installSegment5 (opcodeDontSaveObject, new OpDontSaveObject);
+            interpreter.installSegment5 (opcodeToggleVanityMode, new OpToggleVanityMode);
         }
     }
 }
diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp
index 51216e3a5..889741989 100644
--- a/apps/openmw/mwworld/worldimp.hpp
+++ b/apps/openmw/mwworld/worldimp.hpp
@@ -298,6 +298,10 @@ namespace MWWorld
             }
 
             virtual void renderPlayer();
+
+            virtual bool isVanityEnabled() {
+                return mRendering->isVanityEnabled();
+            }
     };
 }