From 845cafd61c12142a353119a6304845370037d7a5 Mon Sep 17 00:00:00 2001
From: Marc Zinnschlag <marc@zpages.de>
Date: Tue, 25 Aug 2015 12:40:40 +0200
Subject: [PATCH] fixed record state issues after merge

---
 apps/opencs/model/tools/mergestages.hpp | 2 +-
 apps/opencs/model/world/refiddata.cpp   | 7 +++++--
 apps/opencs/model/world/refiddata.hpp   | 3 ++-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/apps/opencs/model/tools/mergestages.hpp b/apps/opencs/model/tools/mergestages.hpp
index 308ebe1c35..5c2243d438 100644
--- a/apps/opencs/model/tools/mergestages.hpp
+++ b/apps/opencs/model/tools/mergestages.hpp
@@ -66,7 +66,7 @@ namespace CSMTools
         const CSMWorld::Record<RecordType>& record = source.getRecord (stage);
 
         if (!record.isDeleted())
-            target.appendRecord (CSMWorld::Record<RecordType> (CSMWorld::RecordBase::State_BaseOnly, &record.get()));
+            target.appendRecord (CSMWorld::Record<RecordType> (CSMWorld::RecordBase::State_ModifiedOnly, 0, &record.get()));
     }
 
 
diff --git a/apps/opencs/model/world/refiddata.cpp b/apps/opencs/model/world/refiddata.cpp
index 70d968efbd..14f580d39b 100644
--- a/apps/opencs/model/world/refiddata.cpp
+++ b/apps/opencs/model/world/refiddata.cpp
@@ -2,6 +2,7 @@
 #include "refiddata.hpp"
 
 #include <cassert>
+#include <memory>
 
 #include <components/misc/stringops.hpp>
 
@@ -345,7 +346,7 @@ const CSMWorld::RefIdDataContainer< ESM::Static >& CSMWorld::RefIdData::getStati
     return mStatics;
 }
 
-void CSMWorld::RefIdData::insertRecord(CSMWorld::RecordBase& record, CSMWorld::UniversalId::Type type, const std::string& id)
+void CSMWorld::RefIdData::insertRecord (CSMWorld::RecordBase& record, CSMWorld::UniversalId::Type type, const std::string& id)
 {
   std::map<UniversalId::Type, RefIdDataContainerBase *>::iterator iter =
         mRecordContainers.find (type);
@@ -367,5 +368,7 @@ void CSMWorld::RefIdData::copyTo (int index, RefIdData& target) const
 
     std::string id = source->getId (localIndex.first);
 
-    target.insertRecord (source->getRecord (localIndex.first), localIndex.second, id);
+    std::auto_ptr<CSMWorld::RecordBase> newRecord (source->getRecord (localIndex.first).modifiedCopy());
+
+    target.insertRecord (*newRecord, localIndex.second, id);
 }
diff --git a/apps/opencs/model/world/refiddata.hpp b/apps/opencs/model/world/refiddata.hpp
index 4507f6028d..8909ae4fb0 100644
--- a/apps/opencs/model/world/refiddata.hpp
+++ b/apps/opencs/model/world/refiddata.hpp
@@ -210,7 +210,8 @@ namespace CSMWorld
 
             void erase (int index, int count);
 
-            void insertRecord(CSMWorld::RecordBase& record, CSMWorld::UniversalId::Type type, const std::string& id);
+            void insertRecord (CSMWorld::RecordBase& record, CSMWorld::UniversalId::Type type,
+                const std::string& id);
 
             const RecordBase& getRecord (const LocalIndex& index) const;