From 900532a6ca2e5bb8394c1a96fb6f6d36792ef27b Mon Sep 17 00:00:00 2001
From: Marc Zinnschlag <marc@zpages.de>
Date: Thu, 30 Jan 2014 12:37:33 +0100
Subject: [PATCH] store additional state of lights in saved game files

---
 apps/openmw/mwclass/light.cpp     | 21 +++++++++++++++++++++
 apps/openmw/mwclass/light.hpp     |  8 ++++++++
 apps/openmw/mwworld/cellstore.cpp |  5 +++--
 components/CMakeLists.txt         |  2 +-
 components/esm/lightstate.cpp     | 21 +++++++++++++++++++++
 components/esm/lightstate.hpp     | 19 +++++++++++++++++++
 6 files changed, 73 insertions(+), 3 deletions(-)
 create mode 100644 components/esm/lightstate.cpp
 create mode 100644 components/esm/lightstate.hpp

diff --git a/apps/openmw/mwclass/light.cpp b/apps/openmw/mwclass/light.cpp
index cc56ec4c8..ddb2c16d6 100644
--- a/apps/openmw/mwclass/light.cpp
+++ b/apps/openmw/mwclass/light.cpp
@@ -2,6 +2,7 @@
 #include "light.hpp"
 
 #include <components/esm/loadligh.hpp>
+#include <components/esm/lightstate.hpp>
 
 #include "../mwbase/environment.hpp"
 #include "../mwbase/world.hpp"
@@ -269,4 +270,24 @@ namespace MWClass
         }
         return std::make_pair(1,"");
     }
+
+    void Light::readAdditionalState (const MWWorld::Ptr& ptr, const ESM::ObjectState& state)
+        const
+    {
+        const ESM::LightState& state2 = dynamic_cast<const ESM::LightState&> (state);
+
+        ensureCustomData (ptr);
+
+        dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mTime = state2.mTime;
+    }
+
+    void Light::writeAdditionalState (const MWWorld::Ptr& ptr, ESM::ObjectState& state)
+        const
+    {
+        ESM::LightState& state2 = dynamic_cast<ESM::LightState&> (state);
+
+        ensureCustomData (ptr);
+
+        state2.mTime = dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mTime;
+    }
 }
diff --git a/apps/openmw/mwclass/light.hpp b/apps/openmw/mwclass/light.hpp
index c15228a6a..5568e1727 100644
--- a/apps/openmw/mwclass/light.hpp
+++ b/apps/openmw/mwclass/light.hpp
@@ -71,6 +71,14 @@ namespace MWClass
             virtual bool canSell (const MWWorld::Ptr& item, int npcServices) const;
 
             std::pair<int, std::string> canBeEquipped(const MWWorld::Ptr &ptr, const MWWorld::Ptr &npc) const;
+
+            virtual void readAdditionalState (const MWWorld::Ptr& ptr, const ESM::ObjectState& state)
+                const;
+            ///< Read additional state from \a state into \a ptr.
+
+            virtual void writeAdditionalState (const MWWorld::Ptr& ptr, ESM::ObjectState& state)
+                const;
+            ///< Write additional state from \a ptr into \a state.
     };
 }
 
diff --git a/apps/openmw/mwworld/cellstore.cpp b/apps/openmw/mwworld/cellstore.cpp
index 88c241e1a..77fdc971d 100644
--- a/apps/openmw/mwworld/cellstore.cpp
+++ b/apps/openmw/mwworld/cellstore.cpp
@@ -6,6 +6,7 @@
 #include <components/esm/cellid.hpp>
 #include <components/esm/esmwriter.hpp>
 #include <components/esm/objectstate.hpp>
+#include <components/esm/lightstate.hpp>
 
 #include "../mwbase/environment.hpp"
 #include "../mwbase/world.hpp"
@@ -331,7 +332,7 @@ namespace MWWorld
         writeReferenceCollection<ESM::ObjectState> (writer, mIngreds);
         writeReferenceCollection<ESM::ObjectState> (writer, mCreatureLists);
         writeReferenceCollection<ESM::ObjectState> (writer, mItemLists);
-        writeReferenceCollection<ESM::ObjectState> (writer, mLights);
+        writeReferenceCollection<ESM::LightState> (writer, mLights);
         writeReferenceCollection<ESM::ObjectState> (writer, mLockpicks);
         writeReferenceCollection<ESM::ObjectState> (writer, mMiscItems);
         writeReferenceCollection<ESM::ObjectState> (writer, mNpcs);
@@ -413,7 +414,7 @@ namespace MWWorld
 
                 case ESM::REC_LIGH:
 
-                    readReferenceCollection<ESM::ObjectState> (reader, mLights, contentFileMap);
+                    readReferenceCollection<ESM::LightState> (reader, mLights, contentFileMap);
                     break;
 
                 case ESM::REC_LOCK:
diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt
index d73bcaf74..f37a537c5 100644
--- a/components/CMakeLists.txt
+++ b/components/CMakeLists.txt
@@ -40,7 +40,7 @@ add_component_dir (esm
     loadinfo loadingr loadland loadlevlist loadligh loadlock loadprob loadrepa loadltex loadmgef loadmisc loadnpcc
     loadnpc loadpgrd loadrace loadregn loadscpt loadskil loadsndg loadsoun loadspel loadsscr loadstat
     loadweap records aipackage effectlist spelllist variant variantimp loadtes3 cellref filter
-    savedgame journalentry queststate locals globalscript player objectstate cellid cellstate globalmap
+    savedgame journalentry queststate locals globalscript player objectstate cellid cellstate globalmap lightstate
     )
 
 add_component_dir (misc
diff --git a/components/esm/lightstate.cpp b/components/esm/lightstate.cpp
new file mode 100644
index 000000000..1ef040823
--- /dev/null
+++ b/components/esm/lightstate.cpp
@@ -0,0 +1,21 @@
+
+#include "lightstate.hpp"
+
+#include "esmreader.hpp"
+#include "esmwriter.hpp"
+
+void ESM::LightState::load (ESMReader &esm)
+{
+    ObjectState::load (esm);
+
+    mTime = 0;
+    esm.getHNOT (mTime, "LTIM");
+}
+
+void ESM::LightState::save (ESMWriter &esm, bool inInventory) const
+{
+    ObjectState::save (esm, inInventory);
+
+    if (mTime)
+        esm.writeHNT ("LTIM", mTime);
+}
\ No newline at end of file
diff --git a/components/esm/lightstate.hpp b/components/esm/lightstate.hpp
new file mode 100644
index 000000000..a22735e07
--- /dev/null
+++ b/components/esm/lightstate.hpp
@@ -0,0 +1,19 @@
+#ifndef OPENMW_ESM_LIGHTSTATE_H
+#define OPENMW_ESM_LIGHTSTATE_H
+
+#include "objectstate.hpp"
+
+namespace ESM
+{
+    // format 0, saved games only
+
+    struct LightState : public ObjectState
+    {
+        float mTime;
+
+        virtual void load (ESMReader &esm);
+        virtual void save (ESMWriter &esm, bool inInventory = false) const;
+    };
+}
+
+#endif