Browse Source

Split the savemap and fixed six bugs in it

- The savemap is divided into its values, its reading and its writing. The record types are in SavemapData.h.
- Broke up the reading and the writing of the XML file.
- The control key and the slot were not read from the file.
- Copying a savemap left some of its data behind.
- The key items and the learned enemy skills were not read correctly.
- Saving the game kept items, materia and data that were gone since the last save: save_game empties the savemap first (savemap_manager:clear_contents()).
- All the save slots shared the current savemap, so saving to one slot changed the others.
- The countdown was written depending on the game time instead of on itself.
- Added tests for the savemap and the save slots.
Iñigo Valentin 4 days ago
parent
commit
5955f22a2a

+ 3 - 0
data/engine/scripts/core/save.lua

@@ -13,6 +13,9 @@ end
 -- @param slot The slot to save at (0-15).
 -- @param slot The slot to save at (0-15).
 -- @param force If false, exisiting game data will not be overwritten if control keys are different.
 -- @param force If false, exisiting game data will not be overwritten if control keys are different.
 save_game = function(slot, force)
 save_game = function(slot, force)
+    -- Only the slots that have something are written below, so what the savemap had before has to
+    -- be removed: used items, unequipped materia...
+    savemap_manager:clear_contents()
     savemap_manager:set_control_key(ControlKey)
     savemap_manager:set_control_key(ControlKey)
     -- Settings of the config menu, and the colours of the windows.
     -- Settings of the config menu, and the colours of the windows.
     UiContainer.ConfigMenu:save_settings()
     UiContainer.ConfigMenu:save_settings()

+ 3 - 0
src/engine/save/CMakeLists.txt

@@ -2,4 +2,7 @@
 vgears_sources(
 vgears_sources(
     Savemap.cpp
     Savemap.cpp
     SavemapHandler.cpp
     SavemapHandler.cpp
+    SavemapRead.cpp
+    SavemapValues.cpp
+    SavemapWrite.cpp
 )
 )

+ 17 - 1481
src/engine/save/Savemap.cpp

@@ -13,30 +13,30 @@
  * GNU General Public License for more details.
  * GNU General Public License for more details.
  */
  */
 
 
-#include <boost/filesystem.hpp>
 #include <string>
 #include <string>
 #include <iostream>
 #include <iostream>
 #include <algorithm>
 #include <algorithm>
-#include <tinyxml.h>
 #include "engine/save/Savemap.h"
 #include "engine/save/Savemap.h"
 #include "common/Logger.h"
 #include "common/Logger.h"
-#include "common/XmlFile.h"
 #include "common/TypeDefine.h"
 #include "common/TypeDefine.h"
 
 
-constexpr unsigned int Savemap::MAX_CHARACTERS;
-constexpr unsigned int Savemap::MAX_ITEM_SLOTS;
-constexpr unsigned int Savemap::MAX_KEY_ITEM_SLOTS;
-constexpr unsigned int Savemap::MAX_MATERIA_SLOTS;
-constexpr unsigned int Savemap::MAX_STASH_SLOTS;
-constexpr unsigned int Savemap::MAX_EQUIP_SLOTS;
-constexpr unsigned int Savemap::MAX_ENEMY_SKILLS;
-constexpr unsigned int Savemap::MAX_LIMIT_LEVELS;
-constexpr unsigned int Savemap::MAX_LIMIT_TECHNIQUES;
-constexpr unsigned int Savemap::MAX_LIMIT_BAR;
-constexpr unsigned int Savemap::MAX_PARTY_MEMBERS;
-constexpr unsigned int Savemap::BANK_COUNT;
-constexpr unsigned int Savemap::BANK_ADDRESS_COUNT;
-constexpr unsigned int Savemap::MAX_COLOUR;
+constexpr unsigned int SavemapData::MAX_CHARACTERS;
+constexpr unsigned int SavemapData::MAX_ITEM_SLOTS;
+constexpr unsigned int SavemapData::MAX_KEY_ITEM_SLOTS;
+constexpr unsigned int SavemapData::MAX_MATERIA_SLOTS;
+constexpr unsigned int SavemapData::MAX_STASH_SLOTS;
+constexpr unsigned int SavemapData::MAX_EQUIP_SLOTS;
+constexpr unsigned int SavemapData::MAX_ENEMY_SKILLS;
+constexpr unsigned int SavemapData::MAX_LIMIT_LEVELS;
+constexpr unsigned int SavemapData::MAX_LIMIT_TECHNIQUES;
+constexpr unsigned int SavemapData::MAX_LIMIT_BAR;
+constexpr unsigned int SavemapData::MAX_PARTY_MEMBERS;
+constexpr unsigned int SavemapData::BANK_COUNT;
+constexpr unsigned int SavemapData::BANK_ADDRESS_COUNT;
+constexpr unsigned int SavemapData::MAX_COLOUR;
+
+const char* const SavemapData::CORNER_NAMES[4]
+  = {"top_left", "top_right", "bottom_right", "bottom_left"};
 
 
 Savemap::Savemap():
 Savemap::Savemap():
   empty_(true), control_(""), money_(0), seconds_(0), countdown_(0), slot_(-1)
   empty_(true), control_(""), money_(0), seconds_(0), countdown_(0), slot_(-1)
@@ -134,1467 +134,3 @@ Savemap::Savemap():
 }
 }
 
 
 Savemap::~Savemap(){}
 Savemap::~Savemap(){}
-
-void Savemap::operator = (const Savemap &to_copy){
-    empty_ = to_copy.empty_;
-    control_ = to_copy.control_;
-    for (int i = 0; i < 4; i ++){
-        window_colours_[i][0] = to_copy.window_colours_[i][0];
-        window_colours_[i][1] = to_copy.window_colours_[i][1];
-        window_colours_[i][2] = to_copy.window_colours_[i][2];
-    }
-    for (int i = 0; i < 11; i ++) characters_[i] = to_copy.characters_[i];
-    for (int i = 0; i < 3; i ++) party_[i] = to_copy.party_[i];
-    settings_ = to_copy.settings_;
-}
-
-void Savemap::SetData(const unsigned int bank, const unsigned int address, const int value){
-    if (bank < BANK_COUNT && address < BANK_ADDRESS_COUNT) data_[bank][address] = value;
-}
-
-void Savemap::SetControlKey(const std::string control){
-    control_ = control;
-    empty_ = false;
-}
-
-void Savemap::SetWindowColours(
-  const unsigned int t_l_r, const unsigned int t_l_g, const unsigned int t_l_b,
-  const unsigned int t_r_r, const unsigned int t_r_g, const unsigned int t_r_b,
-  const unsigned int b_r_r, const unsigned int b_r_g, const unsigned int b_r_b,
-  const unsigned int b_l_r, const unsigned int b_l_g, const unsigned int b_l_b
-){
-    window_colours_[0][0] = std::min(MAX_COLOUR, t_l_r);
-    window_colours_[0][1] = std::min(MAX_COLOUR, t_l_g);
-    window_colours_[0][2] = std::min(MAX_COLOUR, t_l_b);
-    window_colours_[1][0] = std::min(MAX_COLOUR, t_r_r);
-    window_colours_[1][1] = std::min(MAX_COLOUR, t_r_g);
-    window_colours_[1][2] = std::min(MAX_COLOUR, t_r_b);
-    window_colours_[2][0] = std::min(MAX_COLOUR, b_r_r);
-    window_colours_[2][1] = std::min(MAX_COLOUR, b_r_g);
-    window_colours_[2][2] = std::min(MAX_COLOUR, b_r_b);
-    window_colours_[3][0] = std::min(MAX_COLOUR, b_l_r);
-    window_colours_[3][1] = std::min(MAX_COLOUR, b_l_g);
-    window_colours_[3][2] = std::min(MAX_COLOUR, b_l_b);
-}
-
-std::string Savemap::GetControlKey() const {return control_;}
-
-void Savemap::SetMoney(const unsigned int money){money_ = money;}
-
-void Savemap::SetGameTime(const unsigned int seconds){seconds_ = seconds;}
-
-void Savemap::SetCountdownTime(const unsigned int seconds){countdown_ = seconds;}
-
-void Savemap::SetKeyItem(const unsigned int item, const bool owned){
-    if (item < MAX_KEY_ITEM_SLOTS) key_items_[item] = owned;
-}
-
-void Savemap::SetParty(const int member_1, const int member_2, const int member_3){
-    if (member_1 == -1 || (member_1 >= 0 && member_1 < MAX_CHARACTERS)) party_[0] = member_1;
-    if (member_2 == -1 || (member_2 >= 0 && member_2 < MAX_CHARACTERS)) party_[1] = member_2;
-    if (member_3 == -1 || (member_3 >= 0 && member_3 < MAX_CHARACTERS)) party_[2] = member_3;
-}
-
-void Savemap::SetItem(const unsigned int slot, const unsigned int id, const unsigned int quantity){
-    if (slot < MAX_ITEM_SLOTS){
-        items_[slot].id = id;
-        items_[slot].quantity = quantity;
-    }
-}
-
-void Savemap::SetMateria(const unsigned int slot, const int id, const unsigned int ap){
-    if (slot < MAX_MATERIA_SLOTS){
-        materia_[slot].id = id;
-        materia_[slot].ap = ap;
-        materia_[slot].enemy_skill = false;
-        for (int i = 0; i < MAX_ENEMY_SKILLS; i ++) materia_[slot].enemy_skill_learned[i] = false;
-    }
-}
-
-void Savemap::SetESkillMateria(const unsigned slot, const unsigned int skill, const bool learned){
-    if (slot < MAX_MATERIA_SLOTS && skill < MAX_ENEMY_SKILLS){
-        // TODO: Maybe set enemy skill materia ID here?
-        materia_[slot].ap = 0;
-        materia_[slot].enemy_skill_learned[skill] = learned;
-        if (learned) materia_[slot].enemy_skill = true;
-    }
-}
-
-void Savemap::SetMateriaStash(const unsigned int slot, const int id, const unsigned int ap){
-    if (slot < MAX_STASH_SLOTS){
-        materia_stash_[slot].id = id;
-        materia_stash_[slot].ap = ap;
-        materia_stash_[slot].enemy_skill = false;
-        for (int i = 0; i < MAX_ENEMY_SKILLS; i ++)
-            materia_stash_[slot].enemy_skill_learned[i] = false;
-    }
-}
-
-void Savemap::SetESkillMateriaStash(
-  const unsigned slot, const unsigned int skill, const bool learned
-){
-    if (slot < MAX_STASH_SLOTS && skill < MAX_ENEMY_SKILLS){
-        // TODO: Maybe set enemy skill materia ID here?
-        materia_stash_[slot].ap = 0;
-        materia_stash_[slot].enemy_skill_learned[skill] = learned;
-        if (learned) materia_stash_[slot].enemy_skill = true;
-    }
-}
-
-void Savemap::SetLocation(
-  const float x, const float y, const float z,
-  const unsigned int triangle, const int angle, std::string field, std::string name
-){
-
-    location_.x = x;
-    location_.y = y;
-    location_.z = z;
-    location_.triangle = triangle;
-    if (angle >= -360 && angle <= 360) location_.angle = angle;
-    location_.field = field;
-    location_.name = name;
-}
-
-void Savemap::SetSetting(const std::string& key, const int value){settings_[key] = value;}
-
-bool Savemap::HasSetting(const std::string& key) const{return settings_.count(key) > 0;}
-
-int Savemap::GetSetting(const std::string& key) const{
-    const auto it = settings_.find(key);
-    return (it == settings_.end()) ? 0 : it->second;
-}
-
-void Savemap::SetCharacterInfo(
-  const unsigned int id, const int char_id, const std::string name,
-  const bool enabled, const bool locked,
-  const unsigned int level, const unsigned int kills,
-  const bool back_row, const unsigned int exp, const unsigned int exp_to_next,
-  const unsigned int limit_level, const unsigned int limit_bar,
-  const unsigned int weapon, const unsigned int armor, const int accessory
-){
-    if (id < MAX_CHARACTERS){
-        characters_[id].id = id;
-        characters_[id].char_id = char_id;
-        characters_[id].name = name;
-        characters_[id].enabled = enabled;
-        characters_[id].locked = locked;
-        characters_[id].level = level;
-        characters_[id].kills = kills;
-        characters_[id].back_row = back_row;
-        characters_[id].exp = exp;
-        characters_[id].exp_to_next = exp_to_next;
-        characters_[id].limit_level = std::min(limit_level, MAX_LIMIT_LEVELS);
-        characters_[id].limit_bar = std::min(limit_bar, MAX_LIMIT_BAR);
-        characters_[id].weapon.id = weapon;
-        characters_[id].armor.id = armor;
-        characters_[id].accessory = std::max(accessory, -1);
-    }
-}
-
-void Savemap::SetCharacterStat(
-  const unsigned int id, const unsigned int stat,
-  const unsigned int base, const unsigned int extra
-){
-    if (id < MAX_CHARACTERS){
-        if (stat == 0){
-            characters_[id].str.base = base;
-            characters_[id].str.extra = extra;
-        }
-        else if (stat == 1){
-            characters_[id].vit.base = base;
-            characters_[id].vit.extra = extra;
-        }
-        else if (stat == 2){
-            characters_[id].mag.base = base;
-            characters_[id].mag.extra = extra;
-        }
-        else if (stat == 3){
-            characters_[id].spr.base = base;
-            characters_[id].spr.extra = extra;
-        }
-        else if (stat == 4){
-            characters_[id].dex.base = base;
-            characters_[id].dex.extra = extra;
-        }
-        else if (stat == 5){
-            characters_[id].lck.base = base;
-            characters_[id].lck.extra = extra;
-        }
-        else if (stat == 6){
-            characters_[id].hp.base = base;
-            characters_[id].hp.extra = extra;
-        }
-        else if (stat == 7){
-            characters_[id].mp.base = base;
-            characters_[id].mp.extra = extra;
-        }
-    }
-}
-void Savemap::SetCharacterLimitLearned(
-  const unsigned int id, const unsigned int level,
-  const unsigned int technique, const bool learned, const unsigned int uses
-){
-    if (id < MAX_CHARACTERS && level < MAX_LIMIT_LEVELS && technique < MAX_LIMIT_TECHNIQUES){
-        characters_[id].limits_learned[level][technique] = learned;
-        characters_[id].limit_uses[level] = uses;
-    }
-}
-
-void Savemap::SetCharacterMateria(
-  const unsigned int id, const bool weapon, const unsigned int slot,
-  const int materia, const unsigned int ap
-){
-    if (id < MAX_CHARACTERS && slot < MAX_EQUIP_SLOTS){
-        if (weapon){
-            characters_[id].weapon.materia[slot].id = materia;
-            characters_[id].weapon.materia[slot].ap = ap;
-            characters_[id].weapon.materia[slot].enemy_skill = false;
-            for (int i = 0; i < MAX_ENEMY_SKILLS; i ++)
-                characters_[id].weapon.materia[slot].enemy_skill_learned[i] = false;
-        }
-        else{
-            characters_[id].armor.materia[slot].id = materia;
-            characters_[id].armor.materia[slot].ap = ap;
-            characters_[id].armor.materia[slot].enemy_skill = false;
-            for (int i = 0; i < MAX_ENEMY_SKILLS; i ++)
-                characters_[id].armor.materia[slot].enemy_skill_learned[i] = false;
-        }
-    }
-}
-
-void Savemap::SetCharacterESkillMateria(
-  const unsigned int id, const bool weapon, const unsigned int slot,
-  const unsigned int skill, const bool learned
-){
-    if (id < MAX_CHARACTERS && slot < MAX_EQUIP_SLOTS && skill < MAX_ENEMY_SKILLS){
-        if (weapon){
-            // TODO: Maybe set enemy skill materia ID here?
-            characters_[id].weapon.materia[slot].ap = 0;
-            characters_[id].weapon.materia[slot].enemy_skill_learned[skill] = learned;
-            if (learned) characters_[id].weapon.materia[slot].enemy_skill = true;
-        }
-        else{
-            // TODO: Maybe set enemy skill materia ID here?
-            characters_[id].armor.materia[slot].ap = 0;
-            characters_[id].armor.materia[slot].enemy_skill_learned[skill] = learned;
-            if (learned) characters_[id].armor.materia[slot].enemy_skill = true;
-        }
-    }
-}
-
-void Savemap::SetCharacterStatus(
-  const unsigned int id, const unsigned int status, const bool inflicted
-){
-    //TODO
-}
-
-bool Savemap::IsEmpty(){return empty_;}
-
-std::string Savemap::GetControlKey(){return control_;}
-
-unsigned int Savemap::GetWindowCornerColourComponent(
-  const unsigned int corner, const unsigned int comp
-){
-    if (corner < 4 && comp < 3) return window_colours_[corner][comp];
-    else return 0;
-}
-
-unsigned int Savemap::GetMoney(){return money_;}
-
-unsigned int Savemap::GetGameTime(){return seconds_;}
-
-unsigned int Savemap::GetCountdownTime(){return countdown_;}
-
-int Savemap::GetPartyMember(const unsigned int pos){
-    if (pos < MAX_PARTY_MEMBERS) return party_[pos];
-    else return -1;
-}
-
-unsigned int Savemap::GetItemAtPosId(const unsigned int pos){
-    if (pos >= MAX_ITEM_SLOTS) return 0;
-    else if (items_[pos].quantity == 0) return 0;
-    else return items_[pos].id;
-}
-
-unsigned int Savemap::GetItemAtPosQty(const unsigned int pos){
-    if (pos >= MAX_ITEM_SLOTS) return 0;
-    else return items_[pos].quantity;
-}
-
-bool Savemap::GetKeyItem(const unsigned int id){
-    if (id >= MAX_KEY_ITEM_SLOTS) return false;
-    else return key_items_[id];
-}
-
-int Savemap::GetMateriaAtPosId(const unsigned int pos){
-    if (pos >= MAX_MATERIA_SLOTS) return -1;
-    else return materia_[pos].id;
-}
-
-unsigned int Savemap::GetMateriaAtPosAp(const unsigned int pos){
-    if (pos >= MAX_MATERIA_SLOTS) return 0;
-    else if (materia_[pos].id < 0) return 0;
-    else if (materia_[pos].enemy_skill) return 0;
-    else return materia_[pos].ap;
-}
-
-bool Savemap::IsMateriaAtPosESkill(const unsigned int pos){
-    if (pos >= MAX_MATERIA_SLOTS) return false;
-    else if (materia_[pos].id < 0) return false;
-    else return materia_[pos].enemy_skill;
-}
-
-bool Savemap::IsMateriaAtPosESkillLearned(const unsigned int pos, const unsigned int skill){
-    if (pos >= MAX_MATERIA_SLOTS) return false;
-    else if (materia_[pos].id < 0) return false;
-    else if (materia_[pos].enemy_skill == false) return false;
-    else if (skill >= MAX_ENEMY_SKILLS) return false;
-    else return materia_[pos].enemy_skill_learned[skill];
-}
-
-int Savemap::GetStashAtPosId(const unsigned int pos){
-    if (pos >= MAX_STASH_SLOTS) return -1;
-    else return materia_stash_[pos].id;
-}
-
-unsigned int Savemap::GetStashAtPosAp(const unsigned int pos){
-    if (pos >= MAX_STASH_SLOTS) return 0;
-    else if (materia_stash_[pos].id < 0) return 0;
-    else if (materia_stash_[pos].enemy_skill) return 0;
-    else return materia_stash_[pos].ap;
-}
-
-bool Savemap::IsStashAtPosESkill(const unsigned int pos){
-    if (pos >= MAX_STASH_SLOTS) return false;
-    else if (materia_stash_[pos].id < 0) return false;
-    else return materia_stash_[pos].enemy_skill;
-}
-
-bool Savemap::IsStashAtPosESkillLearned(const unsigned int pos, const unsigned int skill){
-    if (pos >= MAX_STASH_SLOTS) return false;
-    else if (materia_stash_[pos].id < 0) return false;
-    else if (materia_stash_[pos].enemy_skill == false) return false;
-    else if (skill >= MAX_ENEMY_SKILLS) return false;
-    else return materia_stash_[pos].enemy_skill_learned[skill];
-}
-
-float Savemap::GetLocationX(){return location_.x;}
-
-float Savemap::GetLocationY(){return location_.y;}
-
-float Savemap::GetLocationZ(){return location_.z;}
-
-unsigned int Savemap::GetLocationTriangle(){return location_.triangle;}
-
-int Savemap::GetLocationAngle(){return location_.angle;}
-
-std::string Savemap::GetLocationField(){return location_.field;}
-
-std::string Savemap::GetLocationName(){return location_.name;}
-
-int Savemap::GetCharacterCharId(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return -1;
-    else return characters_[id].char_id;
-}
-
-std::string Savemap::GetCharacterName(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return std::string("");
-    else return characters_[id].name;
-}
-
-unsigned int Savemap::GetCharacterLevel(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 1;
-    else return characters_[id].level;
-}
-
-unsigned int Savemap::GetCharacterKills(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].kills;
-}
-
-bool Savemap::IsCharacterEnabled(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return false;
-    else return characters_[id].enabled;
-}
-
-bool Savemap::IsCharacterLocked(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return false;
-    else return characters_[id].locked;
-}
-
-bool Savemap::IsCharacterBackRow(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return false;
-    else return characters_[id].back_row;
-}
-
-unsigned int Savemap::GetCharacterExp(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].exp;
-}
-
-unsigned int Savemap::GetCharacterExpToNext(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].exp_to_next;
-}
-
-unsigned int Savemap::GetCharacterLimitLevel(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].limit_level;
-}
-
-unsigned int Savemap::GetCharacterLimitBar(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].limit_bar;
-}
-
-unsigned int Savemap::GetCharacterWeaponId(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].weapon.id;
-}
-
-unsigned int Savemap::GetCharacterArmorId(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return 0;
-    else return characters_[id].armor.id;
-}
-
-int Savemap::GetCharacterAccessoryId(const unsigned int id){
-    if (id >= MAX_CHARACTERS) return -1;
-    else return characters_[id].accessory;
-}
-
-unsigned int Savemap::GetCharacterStatBase(const unsigned int id, const unsigned int stat){
-    if (id >= MAX_CHARACTERS) return 0;
-    switch (stat){
-        case STAT::STR: return characters_[id].str.base; break;
-        case STAT::VIT: return characters_[id].vit.base; break;
-        case STAT::MAG: return characters_[id].mag.base; break;
-        case STAT::SPR: return characters_[id].spr.base; break;
-        case STAT::DEX: return characters_[id].dex.base; break;
-        case STAT::LCK: return characters_[id].lck.base; break;
-        case STAT::HP: return characters_[id].hp.base; break;
-        case STAT::MP: return characters_[id].mp.base; break;
-        default: return 0;
-    }
-}
-
-unsigned int Savemap::GetCharacterStatExtra(const unsigned int id, const unsigned int stat){
-    if (id >= MAX_CHARACTERS) return 0;
-    switch (stat){
-        case STAT::STR: return characters_[id].str.extra; break;
-        case STAT::VIT: return characters_[id].vit.extra; break;
-        case STAT::MAG: return characters_[id].mag.extra; break;
-        case STAT::SPR: return characters_[id].spr.extra; break;
-        case STAT::DEX: return characters_[id].dex.extra; break;
-        case STAT::LCK: return characters_[id].lck.extra; break;
-        case STAT::HP: return characters_[id].hp.extra; break;
-        case STAT::MP: return characters_[id].mp.extra; break;
-        default: return 0;
-    }
-}
-
-unsigned int Savemap::GetCharacterLimitUses(const unsigned int id, const unsigned int level){
-    if (id >= MAX_CHARACTERS) return 0;
-    else if (level >= MAX_LIMIT_LEVELS) return 0;
-    else return characters_[id].limit_uses[level];
-}
-
-bool Savemap::IsCharacterLimitLearned(
-  const unsigned int id, const unsigned int level, const unsigned int tech
-){
-    if (id >= MAX_CHARACTERS) return false;
-    else if (level >= MAX_LIMIT_LEVELS) return false;
-    else if (tech >= MAX_LIMIT_TECHNIQUES) return false;
-    else return characters_[id].limits_learned[level][tech];
-}
-
-
-int Savemap::GetCharacterMateriaId(
-  const unsigned int id, const bool weapon, const unsigned int pos
-){
-    if (id >= MAX_CHARACTERS) return -1;
-    else if (pos >= MAX_EQUIP_SLOTS) return -1;
-    if (weapon) return characters_[id].weapon.materia[pos].id;
-    else return characters_[id].armor.materia[pos].id;
-}
-
-unsigned int Savemap::GetCharacterMateriaAp(
-  const unsigned int id, const bool weapon, const unsigned int pos
-){
-    if (id >= MAX_CHARACTERS) return 0;
-    else if (pos >= MAX_EQUIP_SLOTS) return 0;
-    if (weapon){
-        if (characters_[id].weapon.materia[pos].id == -1) return 0;
-        else if (characters_[id].weapon.materia[pos].enemy_skill) return 0;
-        else return characters_[id].weapon.materia[pos].ap;
-    }
-    else{
-        if (characters_[id].armor.materia[pos].id == -1) return 0;
-        else if (characters_[id].armor.materia[pos].enemy_skill) return 0;
-        else return characters_[id].armor.materia[pos].ap;
-    }
-}
-
-bool Savemap::IsCharacterMateriaESkill(
-  const unsigned int id, const bool weapon, const unsigned int pos
-){
-    if (id >= MAX_CHARACTERS) return false;
-    else if (pos >= MAX_EQUIP_SLOTS) return false;
-    if (weapon){
-        if (characters_[id].weapon.materia[pos].id == -1) return false;
-        else return characters_[id].weapon.materia[pos].enemy_skill;
-    }
-    else{
-        if (characters_[id].armor.materia[pos].id == -1) return false;
-        else return characters_[id].armor.materia[pos].enemy_skill;
-    }
-}
-
-bool Savemap::IsCharacterMateriaESkillLearned(
-  const unsigned int id, const bool weapon, const unsigned int pos, const unsigned int skill
-){
-    if (id >= MAX_CHARACTERS) return false;
-    else if (pos >= MAX_EQUIP_SLOTS) return false;
-    else if (skill >= MAX_ENEMY_SKILLS) return false;
-    if (weapon){
-        if (characters_[id].weapon.materia[pos].id == -1) return false;
-        else if (characters_[id].weapon.materia[pos].enemy_skill == false) return false;
-        else return characters_[id].weapon.materia[pos].enemy_skill_learned[skill];
-    }
-    else{
-        if (characters_[id].armor.materia[pos].id == -1) return false;
-        else if (characters_[id].armor.materia[pos].enemy_skill == false) return false;
-        else return characters_[id].armor.materia[pos].enemy_skill_learned[skill];
-    }
-}
-
-int Savemap::GetData(const unsigned int bank, const unsigned int address){
-    if (bank >= BANK_COUNT) return 0;
-    else if (address >= BANK_ADDRESS_COUNT) return 0;
-    else return data_[bank][address];
-}
-
-void Savemap::Write(int slot, std::string file_name){
-    TiXmlDocument xml;
-    std::unique_ptr<TiXmlElement> container(new TiXmlElement("Savemap"));
-    // Basic data
-    container->SetAttribute("slot", slot);
-    container->SetAttribute("key", control_);
-
-    // Window colours
-    std::unique_ptr<TiXmlElement> colours(new TiXmlElement("WindowColours"));
-    std::unique_ptr<TiXmlElement> colour_t_l(new TiXmlElement("Corner"));
-    colour_t_l->SetAttribute("position", "top_left");
-    colour_t_l->SetAttribute("red", window_colours_[0][0]);
-    colour_t_l->SetAttribute("green", window_colours_[0][1]);
-    colour_t_l->SetAttribute("blue", window_colours_[0][2]);
-    colours->LinkEndChild(colour_t_l.release());
-    std::unique_ptr<TiXmlElement> colour_t_r(new TiXmlElement("Corner"));
-    colour_t_r->SetAttribute("position", "top_right");
-    colour_t_r->SetAttribute("red", window_colours_[1][0]);
-    colour_t_r->SetAttribute("green", window_colours_[1][1]);
-    colour_t_r->SetAttribute("blue", window_colours_[1][2]);
-    colours->LinkEndChild(colour_t_r.release());
-    std::unique_ptr<TiXmlElement> colour_b_r(new TiXmlElement("Corner"));
-    colour_b_r->SetAttribute("position", "bottom_right");
-    colour_b_r->SetAttribute("red", window_colours_[2][0]);
-    colour_b_r->SetAttribute("green", window_colours_[2][1]);
-    colour_b_r->SetAttribute("blue", window_colours_[2][2]);
-    colours->LinkEndChild(colour_b_r.release());
-    std::unique_ptr<TiXmlElement> colour_b_l(new TiXmlElement("Corner"));
-    colour_b_l->SetAttribute("position", "bottom_left");
-    colour_b_l->SetAttribute("red", window_colours_[3][0]);
-    colour_b_l->SetAttribute("green", window_colours_[3][1]);
-    colour_b_l->SetAttribute("blue", window_colours_[3][2]);
-    colours->LinkEndChild(colour_b_l.release());
-    container->LinkEndChild(colours.release());
-
-    // Single data (when applicable)
-    if (money_ > 0){
-        std::unique_ptr<TiXmlElement> money(new TiXmlElement("Money"));
-        money->SetAttribute("quantity", money_);
-        container->LinkEndChild(money.release());
-    }
-    if (seconds_ > 0){
-        std::unique_ptr<TiXmlElement> time(new TiXmlElement("GameTime"));
-        time->SetAttribute("seconds", seconds_);
-        container->LinkEndChild(time.release());
-    }
-    if (seconds_ > 0){
-        std::unique_ptr<TiXmlElement> countdown(new TiXmlElement("Countdown"));
-        countdown->SetAttribute("seconds", countdown_);
-        container->LinkEndChild(countdown.release());
-    }
-
-    // Location.
-    std::unique_ptr<TiXmlElement> location(new TiXmlElement("Location"));
-    location->SetDoubleAttribute("x", location_.x);
-    location->SetDoubleAttribute("y", location_.y);
-    if (location_.z < 0) location->SetAttribute("z", -1);
-    else location->SetDoubleAttribute("z", location_.z);
-    location->SetAttribute("triangle", location_.triangle);
-    location->SetAttribute("orientation", location_.angle);
-    location->SetAttribute("field", location_.field);
-    location->SetAttribute("name", location_.name);
-    container->LinkEndChild(location.release());
-
-    // Party.
-    std::unique_ptr<TiXmlElement> party(new TiXmlElement("Party"));
-    for (int p = 0; p < 3; p ++){
-        if (party_[p] >= 0 && party_[p] < MAX_CHARACTERS){
-            std::unique_ptr<TiXmlElement> member(new TiXmlElement("Member"));
-            member->SetAttribute("position", p);
-            member->SetAttribute("character_id", party_[p]);
-            party->LinkEndChild(member.release());
-        }
-    }
-    container->LinkEndChild(party.release());
-
-    // Items.
-    std::unique_ptr<TiXmlElement> items(new TiXmlElement("Items"));
-    for (int i = 0; i < MAX_ITEM_SLOTS; i ++){
-        if (items_[i].quantity > 0){
-            std::unique_ptr<TiXmlElement> item(new TiXmlElement("Item"));
-            item->SetAttribute("slot", i);
-            item->SetAttribute("id", items_[i].id);
-            item->SetAttribute("quantity", items_[i].quantity);
-            items->LinkEndChild(item.release());
-        }
-    }
-    container->LinkEndChild(items.release());
-
-    // Key items.
-    std::unique_ptr<TiXmlElement> key_items(new TiXmlElement("KeyItems"));
-    for (int i = 0; i < MAX_KEY_ITEM_SLOTS; i ++){
-        if (key_items_[i]){
-            std::unique_ptr<TiXmlElement> key_item(new TiXmlElement("Item"));
-            key_item->SetAttribute("id", key_items_[i]);
-            key_item->SetAttribute("owned", true);
-            key_items->LinkEndChild(key_item.release());
-        }
-    }
-    container->LinkEndChild(key_items.release());
-
-    // Materia.
-    std::unique_ptr<TiXmlElement> materias(new TiXmlElement("Materia"));
-    for (int i = 0; i < MAX_MATERIA_SLOTS; i ++){
-        if (materia_[i].id >= 0){
-            std::unique_ptr<TiXmlElement> materia(new TiXmlElement("Materia"));
-            materia->SetAttribute("slot", i);
-            materia->SetAttribute("id", materia_[i].id);
-            materia->SetAttribute("ap", materia_[i].ap);
-            if (materia_[i].enemy_skill){
-                materia->SetAttribute("enemy_skill", true);
-                std::unique_ptr<TiXmlElement> enemy_skills(new TiXmlElement("EnemySkill"));
-                for (int s = 0; s < MAX_ENEMY_SKILLS; s ++){
-                    if (materia_[i].enemy_skill_learned[s]){
-                        std::unique_ptr<TiXmlElement> enemy_skill(new TiXmlElement("Skill"));
-                        enemy_skill->SetAttribute("id", s);
-                        enemy_skill->SetAttribute("learned", true);
-                        enemy_skills->LinkEndChild(enemy_skill.release());
-                    }
-                }
-                materia->LinkEndChild(enemy_skills.release());
-            }
-            materias->LinkEndChild(materia.release());
-        }
-    }
-    container->LinkEndChild(materias.release());
-
-    // Materia stash.
-    std::unique_ptr<TiXmlElement> stash(new TiXmlElement("MateriaStash"));
-    for (int i = 0; i < MAX_STASH_SLOTS; i ++){
-        if (materia_stash_[i].id >= 0){
-            std::unique_ptr<TiXmlElement> materia(new TiXmlElement("Materia"));
-            materia->SetAttribute("slot", i);
-            materia->SetAttribute("id", materia_stash_[i].id);
-            materia->SetAttribute("ap", materia_stash_[i].ap);
-            if (materia_stash_[i].enemy_skill){
-                materia->SetAttribute("enemy_skill", true);
-                std::unique_ptr<TiXmlElement> enemy_skills(new TiXmlElement("EnemySkill"));
-                for (int s = 0; s < MAX_ENEMY_SKILLS; s ++){
-                    if (materia_stash_[i].enemy_skill_learned[s]){
-                        std::unique_ptr<TiXmlElement> enemy_skill(new TiXmlElement("Skill"));
-                        enemy_skill->SetAttribute("id", s);
-                        enemy_skill->SetAttribute("learned", true);
-                        enemy_skills->LinkEndChild(enemy_skill.release());
-                    }
-                }
-                materia->LinkEndChild(enemy_skills.release());
-            }
-            stash->LinkEndChild(materia.release());
-        }
-    }
-    container->LinkEndChild(stash.release());
-
-    // Settings (config menu). The window colours are saved apart.
-    if (!settings_.empty()){
-        std::unique_ptr<TiXmlElement> settings(new TiXmlElement("Settings"));
-        for (const auto& setting : settings_){
-            std::unique_ptr<TiXmlElement> element(new TiXmlElement("Setting"));
-            element->SetAttribute("name", setting.first);
-            element->SetAttribute("value", setting.second);
-            settings->LinkEndChild(element.release());
-        }
-        container->LinkEndChild(settings.release());
-    }
-
-    // Characters
-    std::unique_ptr<TiXmlElement> characters(new TiXmlElement("Characters"));
-    for (int c = 0; c < MAX_CHARACTERS; c ++){
-        if (characters_[c].char_id == -1) continue;
-
-        std::unique_ptr<TiXmlElement> character(new TiXmlElement("Character"));
-
-        // Basic information.
-        character->SetAttribute("id", characters_[c].id);
-        character->SetAttribute("char_id", characters_[c].char_id);
-        character->SetAttribute("name", characters_[c].name);
-        character->SetAttribute("level", characters_[c].level);
-        character->SetAttribute("enabled", characters_[c].enabled);
-        character->SetAttribute("locked", characters_[c].locked);
-        character->SetAttribute("kills", characters_[c].kills);
-        character->SetAttribute("back_row", characters_[c].back_row);
-        character->SetAttribute("exp", characters_[c].exp);
-        character->SetAttribute("exp_to_next", characters_[c].exp_to_next);
-
-        // Stats.
-        std::unique_ptr<TiXmlElement> character_stats(new TiXmlElement("Stats"));
-        std::unique_ptr<TiXmlElement> character_stats_str(new TiXmlElement("Stat"));
-        character_stats_str->SetAttribute("id", "str");
-        character_stats_str->SetAttribute("base", characters_[c].str.base);
-        character_stats_str->SetAttribute("bonus", characters_[c].str.extra);
-        character_stats->LinkEndChild(character_stats_str.release());
-        std::unique_ptr<TiXmlElement> character_stats_vit(new TiXmlElement("Stat"));
-        character_stats_vit->SetAttribute("id", "vit");
-        character_stats_vit->SetAttribute("base", characters_[c].vit.base);
-        character_stats_vit->SetAttribute("bonus", characters_[c].vit.extra);
-        character_stats->LinkEndChild(character_stats_vit.release());
-        std::unique_ptr<TiXmlElement> character_stats_mag(new TiXmlElement("Stat"));
-        character_stats_mag->SetAttribute("id", "mag");
-        character_stats_mag->SetAttribute("base", characters_[c].mag.base);
-        character_stats_mag->SetAttribute("bonus", characters_[c].mag.extra);
-        character_stats->LinkEndChild(character_stats_mag.release());
-        std::unique_ptr<TiXmlElement> character_stats_spr(new TiXmlElement("Stat"));
-        character_stats_spr->SetAttribute("id", "spr");
-        character_stats_spr->SetAttribute("base", characters_[c].spr.base);
-        character_stats_spr->SetAttribute("bonus", characters_[c].spr.extra);
-        character_stats->LinkEndChild(character_stats_spr.release());
-        std::unique_ptr<TiXmlElement> character_stats_dex(new TiXmlElement("Stat"));
-        character_stats_dex->SetAttribute("id", "dex");
-        character_stats_dex->SetAttribute("base", characters_[c].dex.base);
-        character_stats_dex->SetAttribute("bonus", characters_[c].dex.extra);
-        character_stats->LinkEndChild(character_stats_dex.release());
-        std::unique_ptr<TiXmlElement> character_stats_lck(new TiXmlElement("Stat"));
-        character_stats_lck->SetAttribute("id", "lck");
-        character_stats_lck->SetAttribute("base", characters_[c].lck.base);
-        character_stats_lck->SetAttribute("bonus", characters_[c].lck.extra);
-        character_stats->LinkEndChild(character_stats_lck.release());
-        std::unique_ptr<TiXmlElement> character_stats_hp(new TiXmlElement("Stat"));
-        character_stats_hp->SetAttribute("id", "hp");
-        character_stats_hp->SetAttribute("base", characters_[c].hp.base);
-        character_stats_hp->SetAttribute("current", characters_[c].hp.extra);
-        character_stats->LinkEndChild(character_stats_hp.release());
-        std::unique_ptr<TiXmlElement> character_stats_mp(new TiXmlElement("Stat"));
-        character_stats_mp->SetAttribute("id", "mp");
-        character_stats_mp->SetAttribute("base", characters_[c].mp.base);
-        character_stats_mp->SetAttribute("current", characters_[c].mp.extra);
-        character_stats->LinkEndChild(character_stats_mp.release());
-        character->LinkEndChild(character_stats.release());
-
-        // Limit information.
-        std::unique_ptr<TiXmlElement> character_limits(new TiXmlElement("Limits"));
-        character_limits->SetAttribute("current", characters_[c].limit_level);
-        character_limits->SetAttribute("bar", characters_[c].limit_bar);
-        for (int l = 0; l < MAX_LIMIT_LEVELS; l ++){
-            std::unique_ptr<TiXmlElement> character_limits_level(new TiXmlElement("Level"));
-            character_limits_level->SetAttribute("level", l);
-            character_limits_level->SetAttribute("uses", characters_[c].limit_uses[l]);
-            for (int t = 0; t < MAX_LIMIT_TECHNIQUES; t ++){
-                if (characters_[c].limits_learned[l][t]){
-                    std::unique_ptr<TiXmlElement> character_limits_level_technique(
-                      new TiXmlElement("Technique")
-                    );
-                    character_limits_level_technique->SetAttribute("id", t);
-                    character_limits_level_technique->SetAttribute("learned", true);
-                    character_limits_level->LinkEndChild(
-                      character_limits_level_technique.release()
-                    );
-                }
-            }
-            character_limits->LinkEndChild(character_limits_level.release());
-        }
-        character->LinkEndChild(character_limits.release());
-
-        // Weapon.
-        std::unique_ptr<TiXmlElement> character_weapon(new TiXmlElement("Weapon"));
-        character_weapon->SetAttribute("id", characters_[c].weapon.id);
-        std::unique_ptr<TiXmlElement> character_weapon_materia(new TiXmlElement("Materia"));
-        for (int m = 0; m < MAX_EQUIP_SLOTS; m ++){
-            if (characters_[c].weapon.materia[m].id >= 0){
-                std::unique_ptr<TiXmlElement> materia(new TiXmlElement("Materia"));
-                materia->SetAttribute("slot", m);
-                materia->SetAttribute("id", characters_[c].weapon.materia[m].id);
-                materia->SetAttribute("ap", characters_[c].weapon.materia[m].ap);
-                if (characters_[c].weapon.materia[m].enemy_skill){
-                    materia->SetAttribute("enemy_skill", true);
-                    std::unique_ptr<TiXmlElement> character_weapon_enemy_skills(
-                      new TiXmlElement("EnemySkill")
-                    );
-                    for (int s = 0; s < MAX_ENEMY_SKILLS; s ++){
-                        if (characters_[c].weapon.materia[m].enemy_skill_learned[s]){
-                            std::unique_ptr<TiXmlElement> enemy_skill(new TiXmlElement("Skill"));
-                            enemy_skill->SetAttribute("id", s);
-                            enemy_skill->SetAttribute("learned", true);
-                            character_weapon_enemy_skills->LinkEndChild(enemy_skill.release());
-                        }
-                    }
-                    materia->LinkEndChild(character_weapon_enemy_skills.release());
-                }
-                character_weapon_materia->LinkEndChild(materia.release());
-            }
-        }
-        character_weapon->LinkEndChild(character_weapon_materia.release());
-        character->LinkEndChild(character_weapon.release());
-
-        // Armor.
-        std::unique_ptr<TiXmlElement> character_armor(new TiXmlElement("Armor"));
-        character_armor->SetAttribute("id", characters_[c].armor.id);
-        std::unique_ptr<TiXmlElement> character_armor_materia(new TiXmlElement("Materia"));
-        for (int m = 0; m < MAX_EQUIP_SLOTS; m ++){
-            if (characters_[c].armor.materia[m].id >= 0){
-                std::unique_ptr<TiXmlElement> materia(new TiXmlElement("Materia"));
-                materia->SetAttribute("slot", m);
-                materia->SetAttribute("id", characters_[c].armor.materia[m].id);
-                materia->SetAttribute("ap", characters_[c].armor.materia[m].ap);
-                if (characters_[c].armor.materia[m].enemy_skill){
-                    materia->SetAttribute("enemy_skill", true);
-                    std::unique_ptr<TiXmlElement> character_armor_enemy_skills(
-                      new TiXmlElement("EnemySkill")
-                    );
-                    for (int s = 0; s < MAX_ENEMY_SKILLS; s ++){
-                        if (characters_[c].armor.materia[m].enemy_skill_learned[s]){
-                            std::unique_ptr<TiXmlElement> enemy_skill(new TiXmlElement("Skill"));
-                            enemy_skill->SetAttribute("id", s);
-                            enemy_skill->SetAttribute("learned", true);
-                            character_armor_enemy_skills->LinkEndChild(enemy_skill.release());
-                        }
-                    }
-                    materia->LinkEndChild(character_armor_enemy_skills.release());
-                }
-                character_armor_materia->LinkEndChild(materia.release());
-            }
-        }
-        character_armor->LinkEndChild(character_armor_materia.release());
-        character->LinkEndChild(character_armor.release());
-
-        // Accessory.
-        if (characters_[c].accessory >= 0){
-            std::unique_ptr<TiXmlElement> character_accessory(new TiXmlElement("Accessory"));
-            character_accessory->SetAttribute("id", characters_[c].accessory);
-            character->LinkEndChild(character_accessory.release());
-        }
-        characters->LinkEndChild(character.release());
-
-        // TODO: Statuses
-    }
-    container->LinkEndChild(characters.release());
-
-    // Data banks.
-    std::unique_ptr<TiXmlElement> banks(new TiXmlElement("DataBanks"));
-    for (int b = 0; b < BANK_COUNT; b ++){
-        std::unique_ptr<TiXmlElement> bank(new TiXmlElement("Bank"));
-        bank->SetAttribute("id", b);
-        for (int a = 0; a < BANK_ADDRESS_COUNT; a ++){
-            if (data_[b][a] != 0){
-                std::unique_ptr<TiXmlElement> address(new TiXmlElement("Address"));
-                address->SetAttribute("address", a);
-                address->SetAttribute("value", data_[b][a]);
-                bank->LinkEndChild(address.release());
-            }
-        }
-        banks->LinkEndChild(bank.release());
-    }
-    container->LinkEndChild(banks.release());
-
-    // Save
-    xml.LinkEndChild(container.release());
-    // The folder of the saved games may not exist yet (a fresh installation): create it, or the
-    // game would not be saved.
-    const boost::filesystem::path path(file_name);
-    if (path.has_parent_path()){
-        boost::system::error_code error;
-        boost::filesystem::create_directories(path.parent_path(), error);
-    }
-    if (!xml.SaveFile(file_name)) LOG_ERROR("Can't write the saved game \"" + file_name + "\".");
-    slot_ = slot;
-    return;
-
-}
-
-void Savemap::Read(std::string file_name){
-    // TODO: This need a try/catch, it's impossible to validate every value and type in the file.
-    TiXmlDocument xml_file(file_name);
-    if (xml_file.LoadFile() == false){
-        empty_ = true;
-        return;
-    }
-
-    TiXmlNode* node = xml_file.RootElement();
-    if (node == nullptr || node->ValueStr() != "Savemap"){
-        LOG_ERROR(
-          "Savemap: " + file_name + " is not a valid savemap file! No <Savemap> in root."
-        );
-        empty_ = true;
-        return;
-    }
-
-    slot_ = node->ToElement()->QueryIntAttribute("slot", &slot_);
-    control_ = node->ToElement()->QueryStringAttribute("slot", &control_);
-    // Make sure that control is not just a null character!
-    // This would break many things,
-    if (control_.size() < 2) control_ = "X";
-    empty_ = false;
-
-    node = node->FirstChild();
-    while (node != nullptr){
-        if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "WindowColours"){
-            TiXmlNode* colour_node = node->FirstChild();
-            while (colour_node != nullptr){
-                if (
-                  colour_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && colour_node->ValueStr() == "Corner"
-                ){
-                    std::string position;
-                    TiXmlElement* colour_elm = colour_node->ToElement();
-                    colour_elm->QueryStringAttribute("position", &position);
-                    int colour;
-                    if (position == "top_left"){
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("red", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[0][0] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("green", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[0][1] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("blue", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[0][2] = colour;
-                    }
-                    else if (position == "top_right"){
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("red", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[1][0] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("green", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[1][1] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("blue", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[1][2] = colour;
-                    }
-                    else if (position == "bottom_right"){
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("red", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[2][0] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("green", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[2][1] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("blue", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[2][2] = colour;
-                    }
-                    else if (position == "bottom_left"){
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("red", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[3][0] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("green", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[3][1] = colour;
-                        colour = -1;
-                        colour_elm->QueryIntAttribute("blue", &colour);
-                        if (colour >= 0 && colour <= MAX_COLOUR) window_colours_[3][2] = colour;
-                    }
-                }
-                colour_node = colour_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Settings"){
-            TiXmlNode* setting_node = node->FirstChild();
-            while (setting_node != nullptr){
-                if (
-                  setting_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && setting_node->ValueStr() == "Setting"
-                ){
-                    std::string name;
-                    int value = 0;
-                    TiXmlElement* setting_elm = setting_node->ToElement();
-                    if (
-                      setting_elm->QueryStringAttribute("name", &name) == TIXML_SUCCESS
-                      && setting_elm->QueryIntAttribute("value", &value) == TIXML_SUCCESS
-                    ) settings_[name] = value;
-                }
-                setting_node = setting_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Money"){
-            int money = -1;
-            node->ToElement()->QueryIntAttribute("quantity", &money);
-            if (money >= 0) money_ = money;
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "GameTime"){
-            int seconds = -1;
-            node->ToElement()->QueryIntAttribute("seconds", &seconds);
-            if (seconds >= 0) seconds_ = seconds;
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Countdown"){
-            int seconds = -1;
-            node->ToElement()->QueryIntAttribute("seconds", &seconds);
-            if (seconds >= 0) countdown_ = seconds;
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Location"){
-            node->ToElement()->QueryFloatAttribute("x", &location_.x);
-            node->ToElement()->QueryFloatAttribute("y", &location_.y);
-            node->ToElement()->QueryFloatAttribute("z", &location_.z);
-            int val = -1;
-            node->ToElement()->QueryIntAttribute("triangle", &val);
-            if (val >= 0) location_.triangle = val;
-            val = -1;
-            node->ToElement()->QueryIntAttribute("orientation", &val);
-            if (val >= -360 && val <=360) location_.angle = val;
-            node->ToElement()->QueryStringAttribute("field", &location_.field);
-            node->ToElement()->QueryStringAttribute("name", &location_.name);
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Party"){
-            TiXmlNode* party_node = node->FirstChild();
-            while (party_node != nullptr){
-                if (
-                    party_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && party_node->ValueStr() == "Member"
-                ){
-                    int position = -1;
-                    int member = -1;
-                    TiXmlElement* party_elm = party_node->ToElement();
-                    party_elm->QueryIntAttribute("position", &position);
-                    party_elm->QueryIntAttribute("character_id", &member);
-                    if (
-                      position >= 0 && position < MAX_PARTY_MEMBERS
-                      && member >= 0 && member < MAX_CHARACTERS
-                    ){
-                        party_[position] = member;
-                    }
-                }
-                party_node = party_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Items"){
-            TiXmlNode* item_node = node->FirstChild();
-            while (item_node != nullptr){
-                if (
-                  item_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && item_node->ValueStr() == "Item"
-                ){
-                    int slot = -1;
-                    int id = -1;
-                    int quantity = -1;
-                    TiXmlElement* item_elm = item_node->ToElement();
-                    item_elm->QueryIntAttribute("slot", &slot);
-                    item_elm->QueryIntAttribute("id", &id);
-                    item_elm->QueryIntAttribute("quantity", &quantity);
-                    if (slot >= 0 && slot < MAX_ITEM_SLOTS && id >= 0 && quantity > 0){
-                        items_[slot].id = id;
-                        items_[slot].quantity = quantity;
-                    }
-                }
-                item_node = item_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "KeyItems"){
-            TiXmlNode* item_node = node->FirstChild();
-            while (item_node != nullptr){
-                if (
-                  item_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && item_node->ValueStr() == "Item"
-                ){
-                    int id;
-                    int owned;
-                    TiXmlElement* item_elm = item_node->ToElement();
-                    item_elm->QueryIntAttribute("id", &id);
-                    item_elm->QueryIntAttribute("owned", &owned);
-                    if (id >= 0 && id < MAX_KEY_ITEM_SLOTS && owned == 1) key_items_[id] = true;
-                }
-                item_node = item_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Materia"){
-            TiXmlNode* materia_node = node->FirstChild();
-            while (materia_node != nullptr){
-                if (
-                  materia_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && materia_node->ValueStr() == "Materia"
-                ){
-                    int slot = -1;
-                    int id = -2;
-                    int ap = -1;
-                    int eskill = 0;
-                    TiXmlElement* materia_elm = materia_node->ToElement();
-                    materia_elm->QueryIntAttribute("slot", &slot);
-                    materia_elm->QueryIntAttribute("id", &id);
-                    materia_elm->QueryIntAttribute("ap", &ap);
-                    materia_elm->QueryIntAttribute("enemy_skill", &eskill);
-                    if (
-                      slot >= 0 && slot < MAX_MATERIA_SLOTS && id >= -1
-                      && ap >= 0 && (eskill == 0 || eskill == 1)
-                    ){
-                        materia_[slot].id = id;
-                        materia_[slot].ap = ap;
-                        materia_[slot].enemy_skill = (eskill == 1);
-                        // TODO: For enemy skill, get learned.
-                    }
-                }
-                materia_node = materia_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "MateriaStash"){
-            TiXmlNode* materia_node = node->FirstChild();
-            while (materia_node != nullptr){
-                if (
-                    materia_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && materia_node->ValueStr() == "Materia"
-                ){
-                    int slot = -1;
-                    int id = -2;
-                    int ap = -1;
-                    int eskill = 0;
-                    TiXmlElement* materia_elm = materia_node->ToElement();
-                    materia_elm->QueryIntAttribute("slot", &slot);
-                    materia_elm->QueryIntAttribute("id", &id);
-                    materia_elm->QueryIntAttribute("ap", &ap);
-                    materia_elm->QueryIntAttribute("enemy_skill", &eskill);
-                    if (
-                      slot >= 0 && slot < MAX_STASH_SLOTS && id >= -1
-                      && ap >= 0 && (eskill == 0 || eskill == 1)
-                    ){
-                        materia_stash_[slot].id = id;
-                        materia_stash_[slot].ap = ap;
-                        materia_stash_[slot].enemy_skill = (eskill == 1);
-                        // TODO: For enemy skill, get learned.
-                    }
-                }
-                materia_node = materia_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "Characters"){
-            TiXmlNode* character_node = node->FirstChild();
-            while (character_node != nullptr){
-                if (
-                    character_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && character_node->ValueStr() == "Character"
-                ){
-                    int id = -1;
-                    TiXmlElement* character_elm = character_node->ToElement();
-                    character_elm->QueryIntAttribute("id", &id);
-                    if (id >= 0 && id < MAX_CHARACTERS){
-                        characters_[id].id = id;
-                        int val = -2;
-                        character_elm->QueryIntAttribute("char_id", &val);
-                        if (val >= -1) characters_[id].char_id = val;
-                        character_elm->QueryStringAttribute("name", &characters_[id].name);
-                        val = -1;
-                        character_elm->QueryIntAttribute("enabled", &val);
-                        if (val == 1) characters_[id].enabled = true;
-                        else characters_[id].enabled = false;
-                        val = -1;
-                        character_elm->QueryIntAttribute("locked", &val);
-                        characters_[id].locked = (val == 1);
-                        val = -1;
-                        character_elm->QueryIntAttribute("level", &val);
-                        if (val > 0) characters_[id].level = val;
-                        val = -1;
-                        character_elm->QueryIntAttribute("kills", &val);
-                        if (val >= 0) characters_[id].kills = val;
-                        val = -1;
-                        character_elm->QueryIntAttribute("back_row", &val);
-                        characters_[id].back_row = (val == 1);
-                        val = -1;
-                        character_elm->QueryIntAttribute("exp", &val);
-                        if (val >= 0) characters_[id].exp = val;
-                        val = -1;
-                        character_elm->QueryIntAttribute("exp_to_next", &val);
-                        if (val >= 0) characters_[id].exp_to_next = val;
-                        TiXmlNode* char_node = character_node->FirstChild();
-                        while (char_node != nullptr){
-                            if (
-                              char_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && char_node->ValueStr() == "Stats"
-                            ){
-                                TiXmlNode* stat_node = char_node->FirstChild();
-                                while (stat_node != nullptr){
-                                    if (
-                                      stat_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                      && stat_node->ValueStr() == "Stat"
-                                    ){
-                                        std::string stat = "";
-                                        int base = -1;
-                                        int extra = -1;
-                                        TiXmlElement* stat_elm = stat_node->ToElement();
-                                        stat_elm->QueryStringAttribute("id", &stat);
-                                        if (stat == "str"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("bonus", &extra);
-                                            if (base >= 0 && extra >= 0){
-                                                characters_[id].str.base = base;
-                                                characters_[id].str.extra = extra;
-                                            }
-                                        }
-
-                                        else if (stat == "vit"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("bonus", &extra);
-                                            if (base >= 0 && extra >= 0){
-                                                characters_[id].vit.base = base;
-                                                characters_[id].vit.extra = extra;
-                                            }
-                                        }
-                                        else if (stat == "mag"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("bonus", &extra);
-                                            if (base >= 0 && extra >= 0){
-                                                characters_[id].mag.base = base;
-                                                characters_[id].mag.extra = extra;
-                                            }
-                                        }
-                                        else if (stat == "spr"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("bonus", &extra);
-                                            if (base >= 0 && extra >= 0){
-                                                characters_[id].spr.base = base;
-                                                characters_[id].spr.extra = extra;
-                                            }
-                                        }
-                                        else if (stat == "dex"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("bonus", &extra);
-                                            if (base >= 0 && extra >= 0){
-                                                characters_[id].dex.base = base;
-                                                characters_[id].dex.extra = extra;
-                                            }
-                                        }
-                                        else if (stat == "lck"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("bonus", &extra);
-                                            if (base >= 0 && extra >= 0){
-                                                characters_[id].lck.base = base;
-                                                characters_[id].lck.extra = extra;
-                                            }
-                                        }
-                                        else if (stat == "hp"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("current", &extra);
-                                            if (base > 0 && extra >= 0){
-                                                characters_[id].hp.base = base;
-                                                characters_[id].hp.extra = extra;
-                                            }
-                                        }
-                                        else if (stat == "mp"){
-                                            stat_elm->QueryIntAttribute("base", &base);
-                                            stat_elm->QueryIntAttribute("current", &extra);
-                                            if (base > 0 && extra >= 0){
-                                                characters_[id].mp.base = base;
-                                                characters_[id].mp.extra = extra;
-                                            }
-                                        }
-                                    }
-                                    stat_node = stat_node->NextSibling();
-                                }
-                            }
-                            else if (
-                              char_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && char_node->ValueStr() == "Limits"
-                            ){
-                                int val = -1;
-                                char_node->ToElement()->QueryIntAttribute("current", &val);
-                                if (val >= 0 && val < MAX_LIMIT_LEVELS)
-                                    characters_[id].limit_level = val;
-                                val = -1;
-                                char_node->ToElement()->QueryIntAttribute("bar", &val);
-                                if (val >= 0 && val <= MAX_LIMIT_BAR)
-                                    characters_[id].limit_bar = val;
-                                TiXmlNode* limits_node = char_node->FirstChild();
-                                while (limits_node != nullptr){
-                                    if (
-                                        limits_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                      && limits_node->ValueStr() == "Level"
-                                    ){
-                                        int level = -1;
-                                        int uses = -1;
-                                        limits_node->ToElement()->QueryIntAttribute(
-                                          "level", &level
-                                        );
-                                        limits_node->ToElement()->QueryIntAttribute(
-                                          "uses", &uses
-                                        );
-                                        if (level >= 0 && level < MAX_LIMIT_LEVELS){
-                                            if (uses >= 0) characters_[id].limit_uses[level] = uses;
-                                            TiXmlNode* tech_node = limits_node->FirstChild();
-                                            while (tech_node != nullptr){
-                                                if (
-                                                  tech_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                                  && tech_node->ValueStr() == "Technique"
-                                                ){
-                                                    int tech = -1;
-                                                    int learned = -1;
-                                                    tech_node->ToElement()->QueryIntAttribute(
-                                                      "id", &tech
-                                                    );
-                                                    tech_node->ToElement()->QueryIntAttribute(
-                                                      "learned", &learned
-                                                    );
-                                                    if (tech >= 0 && tech < MAX_LIMIT_TECHNIQUES){
-                                                        characters_[id].limits_learned[level][tech]
-                                                          = (learned == 1);
-                                                    }
-                                                }
-                                                tech_node = tech_node->NextSibling();
-                                            }
-                                        }
-                                    }
-                                    limits_node = limits_node->NextSibling();
-                                }
-                            }
-                            else if (
-                              char_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && char_node->ValueStr() == "Weapon"
-                            ){
-                                int wpn = -1;
-                                char_node->ToElement()->QueryIntAttribute("id", &wpn);
-                                if (wpn >= 0) characters_[id].weapon.id = wpn;
-                                TiXmlNode* mats_node = char_node->FirstChild();
-                                if (
-                                  mats_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                  && mats_node->ValueStr() == "Materia"
-                                ){
-                                    TiXmlNode* mat_node = mats_node->FirstChild();
-                                    while (mat_node != nullptr){
-                                        if (
-                                          mat_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                          && mat_node->ValueStr() == "Materia"
-                                        ){
-                                            int slot = -1;
-                                            int materia_id = -2;
-                                            int ap = -1;
-                                            int eskill = 0;
-                                            TiXmlElement* mat_elm = mat_node->ToElement();
-                                            mat_elm->QueryIntAttribute("slot", &slot);
-                                            mat_elm->QueryIntAttribute("id", &materia_id);
-                                            mat_elm->QueryIntAttribute("ap", &ap);
-                                            mat_elm->QueryIntAttribute("enemy_skill", &eskill);
-                                            if (
-                                              slot >= 0 && slot < MAX_EQUIP_SLOTS && materia_id >= -1
-                                              && ap >= 0 && (eskill == 0 || eskill == 1)
-                                            ){
-                                                characters_[id].weapon.materia[slot].id = materia_id;
-                                                characters_[id].weapon.materia[slot].ap = ap;
-                                                characters_[id].weapon.materia[slot].enemy_skill
-                                                  = (eskill == 1);
-                                                // TODO: For enemy skill, get learned.
-                                            }
-                                        }
-                                        mat_node = mat_node->NextSibling();
-                                    }
-                                }
-                            }
-                            else if (
-                              char_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && char_node->ValueStr() == "Armor"
-                            ){
-                                int arm = -1;
-                                char_node->ToElement()->QueryIntAttribute("id", &arm);
-                                if (arm >= 0) characters_[id].armor.id = arm;
-                                TiXmlNode* mats_node = char_node->FirstChild();
-                                if (
-                                  mats_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                  && mats_node->ValueStr() == "Materia"
-                                ){
-                                    TiXmlNode* mat_node = mats_node->FirstChild();
-                                    while (mat_node != nullptr){
-                                        if (
-                                          mat_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                                          && mat_node->ValueStr() == "Materia"
-                                        ){
-                                            int slot = -1;
-                                            int materia_id = -2;
-                                            int ap = -1;
-                                            int eskill = 0;
-                                            TiXmlElement* mat_elm = mat_node->ToElement();
-                                            mat_elm->QueryIntAttribute("slot", &slot);
-                                            mat_elm->QueryIntAttribute("id", &materia_id);
-                                            mat_elm->QueryIntAttribute("ap", &ap);
-                                            mat_elm->QueryIntAttribute("enemy_skill", &eskill);
-                                            if (
-                                              slot >= 0 && slot < MAX_EQUIP_SLOTS && materia_id >= -1
-                                              && ap >= 0 && (eskill == 0 || eskill == 1)
-                                            ){
-                                                characters_[id].armor.materia[slot].id = materia_id;
-                                                characters_[id].armor.materia[slot].ap = ap;
-                                                characters_[id].armor.materia[slot].enemy_skill
-                                                  = (eskill == 1);
-                                                // TODO: For enemy skill, get learned.
-                                            }
-                                        }
-                                        mat_node = mat_node->NextSibling();
-                                    }
-                                }
-                            }
-                            else if (
-                              char_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && char_node->ValueStr() == "Accessory"
-                            ){
-                                int acc = -2;
-                                char_node->ToElement()->QueryIntAttribute("id", &acc);
-                                if (acc >= -1) characters_[id].accessory = acc;
-                            }
-                            else if (
-                              char_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && char_node->ValueStr() == "Status"
-                            ){
-                                  // TODO
-                            }
-                            char_node = char_node->NextSibling();
-                        }
-                    }
-                }
-                character_node = character_node->NextSibling();
-            }
-        }
-        else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "DataBanks"){
-            TiXmlNode* bank_node = node->FirstChild();
-            while (bank_node != nullptr){
-                if (
-                  bank_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                  && bank_node->ValueStr() == "Bank"
-                ){
-                    int bank = -1;
-                    TiXmlElement* bank_elm = bank_node->ToElement();
-                    bank_elm->QueryIntAttribute("id", &bank);
-                    if (bank >= 0 && bank < BANK_COUNT){
-                        TiXmlNode* addr_node = bank_node->FirstChild();
-                        while (addr_node != nullptr){
-                            if (
-                                addr_node->Type() == TiXmlNode::TINYXML_ELEMENT
-                              && addr_node->ValueStr() == "Address"
-                            ){
-                                int addr = -1;
-                                int val = 0;
-                                TiXmlElement* addr_elm = addr_node->ToElement();
-                                addr_elm->QueryIntAttribute("address", &addr);
-                                addr_elm->QueryIntAttribute("value", &val);
-                                if (addr >= 0 && addr < BANK_ADDRESS_COUNT) data_[bank][addr] = val;
-                            }
-                            addr_node = addr_node->NextSibling();
-                        }
-                    }
-                }
-                bank_node = bank_node->NextSibling();
-            }
-        }
-        node = node->NextSibling();
-    }
-}

+ 72 - 294
src/engine/save/Savemap.h

@@ -18,11 +18,14 @@
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 #include "common/TypeDefine.h"
 #include "common/TypeDefine.h"
+#include "engine/save/SavemapData.h"
+
+class TiXmlElement;
 
 
 /**
 /**
  * A savemap.
  * A savemap.
  */
  */
-class Savemap{
+class Savemap : private SavemapData{
 
 
     public:
     public:
 
 
@@ -36,15 +39,6 @@ class Savemap{
          */
          */
         ~Savemap();
         ~Savemap();
 
 
-        /**
-         * Assignment operator.
-         *
-         * Copies a savemap into another, including control data.
-         *
-         * @param[in] to_copy The savemap to copy from.
-         */
-        void operator = (const Savemap &to_copy);
-
         /**
         /**
          * Reads the savemap data from a file.
          * Reads the savemap data from a file.
          *
          *
@@ -334,6 +328,15 @@ class Savemap{
           const unsigned int id, const unsigned int status, const bool inflicted
           const unsigned int id, const unsigned int status, const bool inflicted
         );
         );
 
 
+        /**
+         * Empties the items, the key items, the materia of the inventory and of the equipment of
+         * the characters, and the data banks.
+         *
+         * Used before filling a savemap that has been saved before: only the slots that have something
+         * are set again.
+         */
+        void ClearContents();
+
         /**
         /**
          * Sets data in a data bank.
          * Sets data in a data bank.
          *
          *
@@ -828,334 +831,109 @@ class Savemap{
     private:
     private:
 
 
         /**
         /**
-         * Maximum number of characters.
-         */
-        static constexpr unsigned int MAX_CHARACTERS = 11;
-
-        /**
-         * Maximum number of inventory slots.
-         */
-        static constexpr unsigned int MAX_ITEM_SLOTS = 500;
-
-        /**
-         * Maximum number of inventory slots for key items.
-         */
-        static constexpr unsigned int MAX_KEY_ITEM_SLOTS = 100;
-
-        /**
-         * Maximum number of materia slots.
+         * Writes the colours of the windows.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_MATERIA_SLOTS = 500;
+        void WriteWindowColours(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Maximum number of materia slots in the stash.
+         * Writes the money, the game time and the countdown.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_STASH_SLOTS = 500;
+        void WriteTimes(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Maximum number of materia slots in a weapon or armor.
+         * Writes where the player is.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_EQUIP_SLOTS = 10;
+        void WriteLocation(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Maximum number of skills in an Enemy Skill materia.
+         * Writes the party.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_ENEMY_SKILLS = 32;
+        void WriteParty(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Maximum limit level.
+         * Writes the items and the key items.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_LIMIT_LEVELS = 4;
+        void WriteItems(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Maximum limit techniques per level.
+         * Writes the settings.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_LIMIT_TECHNIQUES = 4;
+        void WriteSettings(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Level at which the limit level is full.
+         * Writes the data of the scripts.
+         *
+         * @param[in] parent The element to add them to.
          */
          */
-        static constexpr unsigned int MAX_LIMIT_BAR = 254;
+        void WriteDataBanks(TiXmlElement& parent) const;
 
 
         /**
         /**
-         * Maximum number of party member.
+         * Reads the colours of the windows.
+         *
+         * @param[in] element The element to read.
          */
          */
-        static constexpr unsigned int MAX_PARTY_MEMBERS = 3;
+        void ReadWindowColours(TiXmlElement& element);
 
 
         /**
         /**
-         * Number of data banks.
+         * Reads the settings.
+         *
+         * @param[in] element The element to read.
          */
          */
-        static constexpr unsigned int BANK_COUNT = 16;
+        void ReadSettings(TiXmlElement& element);
 
 
         /**
         /**
-         * Number of addresses in each data bank.
+         * Reads where the player is.
+         *
+         * @param[in] element The element to read.
          */
          */
-        static constexpr unsigned int BANK_ADDRESS_COUNT = 256;
+        void ReadLocation(TiXmlElement& element);
 
 
         /**
         /**
-         * Max colour component value (they are stored in a byte).
+         * Reads the party.
+         *
+         * @param[in] element The element to read.
          */
          */
-        static constexpr unsigned int MAX_COLOUR = 255;
+        void ReadParty(TiXmlElement& element);
 
 
         /**
         /**
-         * Location information.
+         * Reads the items.
          *
          *
-         * Used for fields and world map.
+         * @param[in] element The element to read.
          */
          */
-        struct Location{
-
-            /**
-             * X coordinate.
-             */
-            float x;
-
-            /**
-             * Y coordinate.
-             */
-            float y;
-
-            /**
-             * Z coordinate.
-             *
-             * Usually unused.
-             */
-            float z;
-
-            /**
-             * Triangle in the walkmesh;
-             */
-            unsigned int triangle;
-
-            /**
-             * Facing direction.
-             */
-            unsigned int angle;
-
-            /**
-             * Current field map.
-             *
-             * If null or empty, it means the player is in in the world map.
-             */
-            std::string field;
-
-            /**
-             * Location name. Used for the saveslot.
-             */
-            std::string name;
-        };
+        void ReadItems(TiXmlElement& element);
 
 
         /**
         /**
-         * A materia structure.
+         * Reads the key items.
+         *
+         * @param[in] element The element to read.
          */
          */
-        struct Materia{
-
-            /**
-             * Materia ID.
-             */
-            int id;
-
-            /**
-             * Total AP in the materia.
-             */
-            unsigned int ap;
-
-            /**
-             * Indicates if the materia is an enemy skill materia.
-             */
-            bool enemy_skill;
-
-            /**
-             * Learned enemy skills (only for Enemy Skill materia).
-             */
-            bool enemy_skill_learned[MAX_ENEMY_SKILLS];
-        };
+        void ReadKeyItems(TiXmlElement& element);
 
 
         /**
         /**
-         * Item structure.
+         * Reads the characters.
+         *
+         * @param[in] element The element to read.
          */
          */
-        struct Item{
-
-            /**
-             * Item ID.
-             */
-            int id;
-
-            /**
-             * Item stock.
-             */
-            unsigned int quantity;
-        };
+        void ReadCharacters(TiXmlElement& element);
 
 
         /**
         /**
-         * A character entri in the savemap.
+         * Reads the data of the scripts.
+         *
+         * @param[in] element The element to read.
          */
          */
-        struct Character{
-
-            /**
-             * A character stat.
-             */
-            struct Stat{
-
-                /**
-                 * Stat base value.
-                 */
-                unsigned int base;
-
-                /**
-                 * For STR, VIT, DEX and LCK, it means the source bonus. For HP and MP, it means
-                 * the current values.
-                 */
-                unsigned int extra;
-            };
-
-            /**
-             * A weapon or armor.
-             */
-            struct Equipment{
-
-                /**
-                 * Item ID of the weapon or armor.
-                 */
-                u16 id;
-
-                /**
-                 * Materia in the weapon/armor.
-                 */
-                Materia materia[MAX_EQUIP_SLOTS];
-            };
-
-            /**
-             * Character ID, sequential.
-             */
-            unsigned int id;
-
-            /**
-             * Character ID, can change with the history. -1 to not use.
-             */
-            int char_id;
-
-            /**
-             * Indicates if the character has been unlocked for the PHS.
-             */
-            bool enabled;
-
-            /**
-             * Indicates if the character is locked in or out of the PHS.
-             */
-            bool locked;
-
-            /**
-             * Character name.
-             */
-            std::string name;
-
-            /**
-             * Character level.
-             */
-            unsigned int level;
-
-            /**
-             * Enemies killed by the character.
-             */
-            unsigned int kills;
-
-            /**
-             * Indicates if the character is in the back_row;
-             */
-            bool back_row;
-
-            /**
-             * Total experience.
-             */
-            unsigned int exp;
-
-            /**
-             * Experience required to reach next level.
-             */
-            unsigned int exp_to_next;
-
-            /**
-             * STR stat.
-             */
-            Stat str;
-
-            /**
-             * VIT stat.
-             */
-            Stat vit;
-
-            /**
-             * MAG stat.
-             */
-            Stat mag;
-
-            /**
-             * SPR stat.
-             */
-            Stat spr;
-
-            /**
-             * DEX stat.
-             */
-            Stat dex;
-
-            /**
-             * LCK stat.
-             */
-            Stat lck;
-
-            /**
-             * HP stat.
-             */
-            Stat hp;
-
-            /**
-             * MP stat.
-             */
-            Stat mp;
-
-            /**
-             * Current limit level.
-             */
-            unsigned int limit_level;
-
-            /**
-             * Current limit bar.
-             */
-            unsigned int limit_bar;
-
-            /**
-             * Learned limits.
-             *
-             * Grouped by level and technique.
-             */
-            bool limits_learned[MAX_LIMIT_LEVELS][MAX_LIMIT_TECHNIQUES];
-
-            /**
-             * Uses of techniques in each limit level.
-             */
-            unsigned int limit_uses[MAX_LIMIT_LEVELS];
-
-            /**
-             * Equipped weapon;
-             */
-            Equipment weapon;
-
-            /**
-             * Equipped weapon;
-             */
-            Equipment armor;
-
-            /**
-             * Equipped accessory ID. -1 if none equipped.
-             */
-            int accessory;
-
-            /**
-             * Current status list.
-             */
-            std::vector<u8> status;
-        };
+        void ReadDataBanks(TiXmlElement& element);
 
 
         /**
         /**
          * Indicates if the savemap is empty or contains actual data.
          * Indicates if the savemap is empty or contains actual data.

+ 361 - 0
src/engine/save/SavemapData.h

@@ -0,0 +1,361 @@
+/*
+ * 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 <string>
+#include <vector>
+#include "common/TypeDefine.h"
+
+/**
+ * The records of a savemap, and their limits.
+ */
+struct SavemapData{
+
+    /**
+     * Maximum number of characters.
+     */
+    static constexpr unsigned int MAX_CHARACTERS = 11;
+
+    /**
+     * Maximum number of inventory slots.
+     */
+    static constexpr unsigned int MAX_ITEM_SLOTS = 500;
+
+    /**
+     * Maximum number of inventory slots for key items.
+     */
+    static constexpr unsigned int MAX_KEY_ITEM_SLOTS = 100;
+
+    /**
+     * Maximum number of materia slots.
+     */
+    static constexpr unsigned int MAX_MATERIA_SLOTS = 500;
+
+    /**
+     * Maximum number of materia slots in the stash.
+     */
+    static constexpr unsigned int MAX_STASH_SLOTS = 500;
+
+    /**
+     * Maximum number of materia slots in a weapon or armor.
+     */
+    static constexpr unsigned int MAX_EQUIP_SLOTS = 10;
+
+    /**
+     * Maximum number of skills in an Enemy Skill materia.
+     */
+    static constexpr unsigned int MAX_ENEMY_SKILLS = 32;
+
+    /**
+     * Maximum limit level.
+     */
+    static constexpr unsigned int MAX_LIMIT_LEVELS = 4;
+
+    /**
+     * Maximum limit techniques per level.
+     */
+    static constexpr unsigned int MAX_LIMIT_TECHNIQUES = 4;
+
+    /**
+     * Level at which the limit level is full.
+     */
+    static constexpr unsigned int MAX_LIMIT_BAR = 254;
+
+    /**
+     * Maximum number of party member.
+     */
+    static constexpr unsigned int MAX_PARTY_MEMBERS = 3;
+
+    /**
+     * Number of data banks.
+     */
+    static constexpr unsigned int BANK_COUNT = 16;
+
+    /**
+     * Number of addresses in each data bank.
+     */
+    static constexpr unsigned int BANK_ADDRESS_COUNT = 256;
+
+    /**
+     * Max colour component value (they are stored in a byte).
+     */
+    static constexpr unsigned int MAX_COLOUR = 255;
+
+    /**
+     * Location information.
+     *
+     * Used for fields and world map.
+     */
+    struct Location{
+
+        /**
+         * X coordinate.
+         */
+        float x;
+
+        /**
+         * Y coordinate.
+         */
+        float y;
+
+        /**
+         * Z coordinate.
+         *
+         * Usually unused.
+         */
+        float z;
+
+        /**
+         * Triangle in the walkmesh;
+         */
+        unsigned int triangle;
+
+        /**
+         * Facing direction.
+         */
+        unsigned int angle;
+
+        /**
+         * Current field map.
+         *
+         * If null or empty, it means the player is in in the world map.
+         */
+        std::string field;
+
+        /**
+         * Location name. Used for the saveslot.
+         */
+        std::string name;
+    };
+
+    /**
+     * A materia structure.
+     */
+    struct Materia{
+
+        /**
+         * Materia ID.
+         */
+        int id;
+
+        /**
+         * Total AP in the materia.
+         */
+        unsigned int ap;
+
+        /**
+         * Indicates if the materia is an enemy skill materia.
+         */
+        bool enemy_skill;
+
+        /**
+         * Learned enemy skills (only for Enemy Skill materia).
+         */
+        bool enemy_skill_learned[MAX_ENEMY_SKILLS];
+    };
+
+    /**
+     * Item structure.
+     */
+    struct Item{
+
+        /**
+         * Item ID.
+         */
+        int id;
+
+        /**
+         * Item stock.
+         */
+        unsigned int quantity;
+    };
+
+    /**
+     * A character entri in the savemap.
+     */
+    struct Character{
+
+        /**
+         * A character stat.
+         */
+        struct Stat{
+
+            /**
+             * Stat base value.
+             */
+            unsigned int base;
+
+            /**
+             * For STR, VIT, DEX and LCK, it means the source bonus. For HP and MP, it means
+             * the current values.
+             */
+            unsigned int extra;
+        };
+
+        /**
+         * A weapon or armor.
+         */
+        struct Equipment{
+
+            /**
+             * Item ID of the weapon or armor.
+             */
+            u16 id;
+
+            /**
+             * Materia in the weapon/armor.
+             */
+            Materia materia[MAX_EQUIP_SLOTS];
+        };
+
+        /**
+         * Character ID, sequential.
+         */
+        unsigned int id;
+
+        /**
+         * Character ID, can change with the history. -1 to not use.
+         */
+        int char_id;
+
+        /**
+         * Indicates if the character has been unlocked for the PHS.
+         */
+        bool enabled;
+
+        /**
+         * Indicates if the character is locked in or out of the PHS.
+         */
+        bool locked;
+
+        /**
+         * Character name.
+         */
+        std::string name;
+
+        /**
+         * Character level.
+         */
+        unsigned int level;
+
+        /**
+         * Enemies killed by the character.
+         */
+        unsigned int kills;
+
+        /**
+         * Indicates if the character is in the back_row;
+         */
+        bool back_row;
+
+        /**
+         * Total experience.
+         */
+        unsigned int exp;
+
+        /**
+         * Experience required to reach next level.
+         */
+        unsigned int exp_to_next;
+
+        /**
+         * STR stat.
+         */
+        Stat str;
+
+        /**
+         * VIT stat.
+         */
+        Stat vit;
+
+        /**
+         * MAG stat.
+         */
+        Stat mag;
+
+        /**
+         * SPR stat.
+         */
+        Stat spr;
+
+        /**
+         * DEX stat.
+         */
+        Stat dex;
+
+        /**
+         * LCK stat.
+         */
+        Stat lck;
+
+        /**
+         * HP stat.
+         */
+        Stat hp;
+
+        /**
+         * MP stat.
+         */
+        Stat mp;
+
+        /**
+         * Current limit level.
+         */
+        unsigned int limit_level;
+
+        /**
+         * Current limit bar.
+         */
+        unsigned int limit_bar;
+
+        /**
+         * Learned limits.
+         *
+         * Grouped by level and technique.
+         */
+        bool limits_learned[MAX_LIMIT_LEVELS][MAX_LIMIT_TECHNIQUES];
+
+        /**
+         * Uses of techniques in each limit level.
+         */
+        unsigned int limit_uses[MAX_LIMIT_LEVELS];
+
+        /**
+         * Equipped weapon;
+         */
+        Equipment weapon;
+
+        /**
+         * Equipped weapon;
+         */
+        Equipment armor;
+
+        /**
+         * Equipped accessory ID. -1 if none equipped.
+         */
+        int accessory;
+
+        /**
+         * Current status list.
+         */
+        std::vector<u8> status;
+    };
+
+    /**
+     * Names of the corners of the windows in the saved games, in the order of their colours.
+     */
+    static const char* const CORNER_NAMES[4];
+};

+ 9 - 2
src/engine/save/SavemapHandler.cpp

@@ -52,7 +52,8 @@ bool SavemapHandler::Save(unsigned int slot, const bool force){
       && current_savemap_->GetControlKey() != saved_savemaps_[slot]->GetControlKey()
       && current_savemap_->GetControlKey() != saved_savemaps_[slot]->GetControlKey()
     ) return false;
     ) return false;
     else{
     else{
-        saved_savemaps_[slot] = current_savemap_;
+        // The slot keeps a copy: the current savemap changes with the next save.
+        *saved_savemaps_[slot] = *current_savemap_;
         saved_savemaps_[slot]->Write(slot, SAVE_PATH + std::to_string(slot) + ".xml");
         saved_savemaps_[slot]->Write(slot, SAVE_PATH + std::to_string(slot) + ".xml");
         return true;
         return true;
     }
     }
@@ -66,8 +67,9 @@ bool SavemapHandler::Save(Savemap savemap, unsigned int slot, bool force){
       && savemap.GetControlKey() != saved_savemaps_[slot]->GetControlKey()
       && savemap.GetControlKey() != saved_savemaps_[slot]->GetControlKey()
     ) return false;
     ) return false;
     else{
     else{
+        if (current_savemap_ == nullptr) current_savemap_ = new Savemap();
         *current_savemap_ = savemap;
         *current_savemap_ = savemap;
-        saved_savemaps_[slot] = current_savemap_;
+        *saved_savemaps_[slot] = savemap;
         saved_savemaps_[slot]->Write(slot, SAVE_PATH + std::to_string(slot) + ".xml");
         saved_savemaps_[slot]->Write(slot, SAVE_PATH + std::to_string(slot) + ".xml");
         return true;
         return true;
     }
     }
@@ -89,6 +91,11 @@ void SavemapHandler::ReadSavemaps(){
     savemaps_read_ = true;
     savemaps_read_ = true;
 }
 }
 
 
+void SavemapHandler::ClearContents(){
+    if (current_savemap_ == nullptr) current_savemap_ = new Savemap();
+    current_savemap_->ClearContents();
+}
+
 void SavemapHandler::SetData(const unsigned int bank, const unsigned int address, const int value){
 void SavemapHandler::SetData(const unsigned int bank, const unsigned int address, const int value){
     if (current_savemap_ == nullptr) current_savemap_ = new Savemap();
     if (current_savemap_ == nullptr) current_savemap_ = new Savemap();
     current_savemap_->SetData(bank, address, value);
     current_savemap_->SetData(bank, address, value);

+ 9 - 0
src/engine/save/SavemapHandler.h

@@ -90,6 +90,15 @@ class SavemapHandler : public Ogre::Singleton<SavemapHandler>{
          */
          */
         void Release();
         void Release();
 
 
+        /**
+         * Empties the items, the key items, the materia of the inventory and of the equipment of
+         * the characters, and the data banks.
+         *
+         * To be called before writing the game into the current savemap, as only the slots that have
+         * something are written.
+         */
+        void ClearContents();
+
         /**
         /**
          * Pushes data to the current savemap memory banks.
          * Pushes data to the current savemap memory banks.
          *
          *

+ 324 - 0
src/engine/save/SavemapRead.cpp

@@ -0,0 +1,324 @@
+/*
+ * 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 <tinyxml.h>
+#include "common/Logger.h"
+#include "engine/save/Savemap.h"
+
+namespace{
+
+/**
+ * Calls a function for each child element of a node that has a name.
+ */
+template<typename Function>
+void ForEachElement(TiXmlNode& parent, const char* name, Function function){
+    for (
+      TiXmlElement* element = parent.FirstChildElement(name); element != nullptr;
+      element = element->NextSiblingElement(name)
+    ) function(*element);
+}
+
+/**
+ * Reads an integer attribute.
+ *
+ * @param[in] missing Value returned if there is no such attribute, or it's not a number.
+ */
+int ReadInt(const TiXmlElement& element, const char* name, const int missing = -1){
+    int value = missing;
+    element.QueryIntAttribute(name, &value);
+    return value;
+}
+
+/**
+ * Sets a value from an attribute, if the attribute has a number that is not negative.
+ */
+void ReadUnsigned(const TiXmlElement& element, const char* name, unsigned int& value){
+    const int read = ReadInt(element, name);
+    if (read >= 0) value = read;
+}
+
+/**
+ * Reads the skills that an Enemy Skill materia has learned.
+ */
+void ReadEnemySkills(TiXmlElement& materia_element, SavemapData::Materia& materia){
+    for (bool& learned : materia.enemy_skill_learned) learned = false;
+    ForEachElement(materia_element, "EnemySkill", [&](TiXmlElement& skills){
+        ForEachElement(skills, "Skill", [&](TiXmlElement& skill){
+            const int id = ReadInt(skill, "id");
+            if (id < 0 || id >= static_cast<int>(SavemapData::MAX_ENEMY_SKILLS)) return;
+            materia.enemy_skill_learned[id] = (ReadInt(skill, "learned") == 1);
+        });
+    });
+}
+
+/**
+ * Reads a list of materia: the ones of the inventory, the stash or a piece of equipment.
+ */
+void ReadMaterias(TiXmlElement& list, SavemapData::Materia* materias, const unsigned int count){
+    ForEachElement(list, "Materia", [&](TiXmlElement& element){
+        const int slot = ReadInt(element, "slot");
+        const int id = ReadInt(element, "id", -2);
+        const int ap = ReadInt(element, "ap");
+        const int enemy_skill = ReadInt(element, "enemy_skill", 0);
+        if (
+          slot < 0 || slot >= static_cast<int>(count) || id < -1 || ap < 0
+          || (enemy_skill != 0 && enemy_skill != 1)
+        ) return;
+        materias[slot].id = id;
+        materias[slot].ap = ap;
+        materias[slot].enemy_skill = (enemy_skill == 1);
+        ReadEnemySkills(element, materias[slot]);
+    });
+}
+
+/**
+ * Reads a stat of a character.
+ */
+void ReadStat(const TiXmlElement& element, SavemapData::Character& character){
+    struct Entry{
+        const char* id;
+        SavemapData::Character::Stat SavemapData::Character::* stat;
+        const char* second;
+        int minimum_base;
+    };
+    static const Entry STATS[] = {
+        {"str", &SavemapData::Character::str, "bonus", 0},
+        {"vit", &SavemapData::Character::vit, "bonus", 0},
+        {"mag", &SavemapData::Character::mag, "bonus", 0},
+        {"spr", &SavemapData::Character::spr, "bonus", 0},
+        {"dex", &SavemapData::Character::dex, "bonus", 0},
+        {"lck", &SavemapData::Character::lck, "bonus", 0},
+        {"hp", &SavemapData::Character::hp, "current", 1},
+        {"mp", &SavemapData::Character::mp, "current", 1}
+    };
+    std::string id;
+    element.QueryStringAttribute("id", &id);
+    for (const Entry& entry : STATS){
+        if (id != entry.id) continue;
+        const int base = ReadInt(element, "base");
+        const int second = ReadInt(element, entry.second);
+        if (base < entry.minimum_base || second < 0) return;
+        (character.*entry.stat).base = base;
+        (character.*entry.stat).extra = second;
+    }
+}
+
+/**
+ * Reads the limit breaks of a character.
+ */
+void ReadLimits(TiXmlElement& limits, SavemapData::Character& character){
+    const int current = ReadInt(limits, "current");
+    if (current >= 0 && current < static_cast<int>(SavemapData::MAX_LIMIT_LEVELS))
+        character.limit_level = current;
+    const int bar = ReadInt(limits, "bar");
+    if (bar >= 0 && bar <= static_cast<int>(SavemapData::MAX_LIMIT_BAR))
+        character.limit_bar = bar;
+    ForEachElement(limits, "Level", [&](TiXmlElement& element){
+        const int level = ReadInt(element, "level");
+        if (level < 0 || level >= static_cast<int>(SavemapData::MAX_LIMIT_LEVELS)) return;
+        ReadUnsigned(element, "uses", character.limit_uses[level]);
+        ForEachElement(element, "Technique", [&](TiXmlElement& technique){
+            const int id = ReadInt(technique, "id");
+            if (id < 0 || id >= static_cast<int>(SavemapData::MAX_LIMIT_TECHNIQUES)) return;
+            character.limits_learned[level][id] = (ReadInt(technique, "learned") == 1);
+        });
+    });
+}
+
+/**
+ * Reads a weapon or an armor of a character, with its materia.
+ */
+void ReadEquipment(TiXmlElement& element, SavemapData::Character::Equipment& equipment){
+    const int id = ReadInt(element, "id");
+    if (id >= 0) equipment.id = id;
+    // The list of materia is the first thing inside.
+    TiXmlNode* first = element.FirstChild();
+    if (first == nullptr || first->ToElement() == nullptr || first->ValueStr() != "Materia")
+        return;
+    ReadMaterias(*first->ToElement(), equipment.materia, SavemapData::MAX_EQUIP_SLOTS);
+}
+
+/**
+ * Reads a character.
+ */
+void ReadCharacter(TiXmlElement& element, SavemapData::Character& character){
+    const int char_id = ReadInt(element, "char_id", -2);
+    if (char_id >= -1) character.char_id = char_id;
+    element.QueryStringAttribute("name", &character.name);
+    character.enabled = (ReadInt(element, "enabled") == 1);
+    character.locked = (ReadInt(element, "locked") == 1);
+    const int level = ReadInt(element, "level");
+    if (level > 0) character.level = level;
+    ReadUnsigned(element, "kills", character.kills);
+    character.back_row = (ReadInt(element, "back_row") == 1);
+    ReadUnsigned(element, "exp", character.exp);
+    ReadUnsigned(element, "exp_to_next", character.exp_to_next);
+    for (
+      TiXmlElement* child = element.FirstChildElement(); child != nullptr;
+      child = child->NextSiblingElement()
+    ){
+        const std::string& name = child->ValueStr();
+        if (name == "Stats"){
+            ForEachElement(*child, "Stat", [&](TiXmlElement& stat){ReadStat(stat, character);});
+        }
+        else if (name == "Limits") ReadLimits(*child, character);
+        else if (name == "Weapon") ReadEquipment(*child, character.weapon);
+        else if (name == "Armor") ReadEquipment(*child, character.armor);
+        else if (name == "Accessory"){
+            const int accessory = ReadInt(*child, "id", -2);
+            if (accessory >= -1) character.accessory = accessory;
+        }
+        // TODO: Status
+    }
+}
+
+}
+
+void Savemap::Read(std::string file_name){
+    // TODO: This need a try/catch, it's impossible to validate every value and type in the file.
+    TiXmlDocument xml_file(file_name);
+    if (xml_file.LoadFile() == false){
+        empty_ = true;
+        return;
+    }
+
+    TiXmlElement* root = xml_file.RootElement();
+    if (root == nullptr || root->ValueStr() != "Savemap"){
+        LOG_ERROR(
+          "Savemap: " + file_name + " is not a valid savemap file! No <Savemap> in root."
+        );
+        empty_ = true;
+        return;
+    }
+
+    root->QueryIntAttribute("slot", &slot_);
+    root->QueryStringAttribute("key", &control_);
+    // Games saved without a control key, or with an empty one, get a placeholder.
+    if (control_.size() < 2) control_ = "X";
+    empty_ = false;
+
+    for (
+      TiXmlElement* element = root->FirstChildElement(); element != nullptr;
+      element = element->NextSiblingElement()
+    ){
+        const std::string& name = element->ValueStr();
+        if (name == "WindowColours") ReadWindowColours(*element);
+        else if (name == "Settings") ReadSettings(*element);
+        else if (name == "Money") ReadUnsigned(*element, "quantity", money_);
+        else if (name == "GameTime") ReadUnsigned(*element, "seconds", seconds_);
+        else if (name == "Countdown") ReadUnsigned(*element, "seconds", countdown_);
+        else if (name == "Location") ReadLocation(*element);
+        else if (name == "Party") ReadParty(*element);
+        else if (name == "Items") ReadItems(*element);
+        else if (name == "KeyItems") ReadKeyItems(*element);
+        else if (name == "Materia") ReadMaterias(*element, materia_, MAX_MATERIA_SLOTS);
+        else if (name == "MateriaStash")
+            ReadMaterias(*element, materia_stash_, MAX_STASH_SLOTS);
+        else if (name == "Characters") ReadCharacters(*element);
+        else if (name == "DataBanks") ReadDataBanks(*element);
+    }
+}
+
+void Savemap::ReadWindowColours(TiXmlElement& element){
+    static const char* const COMPONENTS[3] = {"red", "green", "blue"};
+    ForEachElement(element, "Corner", [&](TiXmlElement& corner){
+        std::string position;
+        corner.QueryStringAttribute("position", &position);
+        for (int c = 0; c < 4; c ++){
+            if (position != CORNER_NAMES[c]) continue;
+            for (int i = 0; i < 3; i ++){
+                const int colour = ReadInt(corner, COMPONENTS[i]);
+                if (colour >= 0 && colour <= static_cast<int>(MAX_COLOUR))
+                    window_colours_[c][i] = colour;
+            }
+        }
+    });
+}
+
+void Savemap::ReadSettings(TiXmlElement& element){
+    ForEachElement(element, "Setting", [&](TiXmlElement& setting){
+        std::string name;
+        int value = 0;
+        if (
+          setting.QueryStringAttribute("name", &name) == TIXML_SUCCESS
+          && setting.QueryIntAttribute("value", &value) == TIXML_SUCCESS
+        ) settings_[name] = value;
+    });
+}
+
+void Savemap::ReadLocation(TiXmlElement& element){
+    element.QueryFloatAttribute("x", &location_.x);
+    element.QueryFloatAttribute("y", &location_.y);
+    element.QueryFloatAttribute("z", &location_.z);
+    ReadUnsigned(element, "triangle", location_.triangle);
+    const int angle = ReadInt(element, "orientation");
+    if (angle >= -360 && angle <= 360) location_.angle = angle;
+    element.QueryStringAttribute("field", &location_.field);
+    element.QueryStringAttribute("name", &location_.name);
+}
+
+void Savemap::ReadParty(TiXmlElement& element){
+    ForEachElement(element, "Member", [&](TiXmlElement& member){
+        const int position = ReadInt(member, "position");
+        const int character = ReadInt(member, "character_id");
+        if (
+          position >= 0 && position < static_cast<int>(MAX_PARTY_MEMBERS)
+          && character >= 0 && character < static_cast<int>(MAX_CHARACTERS)
+        ) party_[position] = character;
+    });
+}
+
+void Savemap::ReadItems(TiXmlElement& element){
+    ForEachElement(element, "Item", [&](TiXmlElement& item){
+        const int slot = ReadInt(item, "slot");
+        const int id = ReadInt(item, "id");
+        const int quantity = ReadInt(item, "quantity");
+        if (slot >= 0 && slot < static_cast<int>(MAX_ITEM_SLOTS) && id >= 0 && quantity > 0){
+            items_[slot].id = id;
+            items_[slot].quantity = quantity;
+        }
+    });
+}
+
+void Savemap::ReadKeyItems(TiXmlElement& element){
+    ForEachElement(element, "Item", [&](TiXmlElement& item){
+        const int id = ReadInt(item, "id");
+        if (
+          id >= 0 && id < static_cast<int>(MAX_KEY_ITEM_SLOTS) && ReadInt(item, "owned") == 1
+        ) key_items_[id] = true;
+    });
+}
+
+void Savemap::ReadCharacters(TiXmlElement& element){
+    ForEachElement(element, "Character", [&](TiXmlElement& character){
+        const int id = ReadInt(character, "id");
+        if (id < 0 || id >= static_cast<int>(MAX_CHARACTERS)) return;
+        characters_[id].id = id;
+        ReadCharacter(character, characters_[id]);
+    });
+}
+
+void Savemap::ReadDataBanks(TiXmlElement& element){
+    ForEachElement(element, "Bank", [&](TiXmlElement& bank){
+        const int id = ReadInt(bank, "id");
+        if (id < 0 || id >= static_cast<int>(BANK_COUNT)) return;
+        ForEachElement(bank, "Address", [&](TiXmlElement& address){
+            const int number = ReadInt(address, "address");
+            const int value = ReadInt(address, "value", 0);
+            if (number >= 0 && number < static_cast<int>(BANK_ADDRESS_COUNT))
+                data_[id][number] = value;
+        });
+    });
+}

+ 553 - 0
src/engine/save/SavemapValues.cpp

@@ -0,0 +1,553 @@
+/*
+ * 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 <algorithm>
+#include "engine/save/Savemap.h"
+
+void Savemap::ClearContents(){
+    for (Item& item : items_) item = {-1, 0};
+    for (bool& owned : key_items_) owned = false;
+    for (Materia& materia : materia_) materia = {-1, 0, false, {}};
+    for (Character& character : characters_){
+        for (Materia& materia : character.weapon.materia) materia = {-1, 0, false, {}};
+        for (Materia& materia : character.armor.materia) materia = {-1, 0, false, {}};
+    }
+    for (auto& bank : data_) for (int& value : bank) value = 0;
+}
+
+void Savemap::SetData(const unsigned int bank, const unsigned int address, const int value){
+    if (bank < BANK_COUNT && address < BANK_ADDRESS_COUNT) data_[bank][address] = value;
+}
+
+void Savemap::SetControlKey(const std::string control){
+    control_ = control;
+    empty_ = false;
+}
+
+void Savemap::SetWindowColours(
+  const unsigned int t_l_r, const unsigned int t_l_g, const unsigned int t_l_b,
+  const unsigned int t_r_r, const unsigned int t_r_g, const unsigned int t_r_b,
+  const unsigned int b_r_r, const unsigned int b_r_g, const unsigned int b_r_b,
+  const unsigned int b_l_r, const unsigned int b_l_g, const unsigned int b_l_b
+){
+    window_colours_[0][0] = std::min(MAX_COLOUR, t_l_r);
+    window_colours_[0][1] = std::min(MAX_COLOUR, t_l_g);
+    window_colours_[0][2] = std::min(MAX_COLOUR, t_l_b);
+    window_colours_[1][0] = std::min(MAX_COLOUR, t_r_r);
+    window_colours_[1][1] = std::min(MAX_COLOUR, t_r_g);
+    window_colours_[1][2] = std::min(MAX_COLOUR, t_r_b);
+    window_colours_[2][0] = std::min(MAX_COLOUR, b_r_r);
+    window_colours_[2][1] = std::min(MAX_COLOUR, b_r_g);
+    window_colours_[2][2] = std::min(MAX_COLOUR, b_r_b);
+    window_colours_[3][0] = std::min(MAX_COLOUR, b_l_r);
+    window_colours_[3][1] = std::min(MAX_COLOUR, b_l_g);
+    window_colours_[3][2] = std::min(MAX_COLOUR, b_l_b);
+}
+
+std::string Savemap::GetControlKey() const {return control_;}
+
+void Savemap::SetMoney(const unsigned int money){money_ = money;}
+
+void Savemap::SetGameTime(const unsigned int seconds){seconds_ = seconds;}
+
+void Savemap::SetCountdownTime(const unsigned int seconds){countdown_ = seconds;}
+
+void Savemap::SetKeyItem(const unsigned int item, const bool owned){
+    if (item < MAX_KEY_ITEM_SLOTS) key_items_[item] = owned;
+}
+
+void Savemap::SetParty(const int member_1, const int member_2, const int member_3){
+    if (member_1 == -1 || (member_1 >= 0 && member_1 < MAX_CHARACTERS)) party_[0] = member_1;
+    if (member_2 == -1 || (member_2 >= 0 && member_2 < MAX_CHARACTERS)) party_[1] = member_2;
+    if (member_3 == -1 || (member_3 >= 0 && member_3 < MAX_CHARACTERS)) party_[2] = member_3;
+}
+
+void Savemap::SetItem(const unsigned int slot, const unsigned int id, const unsigned int quantity){
+    if (slot < MAX_ITEM_SLOTS){
+        items_[slot].id = id;
+        items_[slot].quantity = quantity;
+    }
+}
+
+void Savemap::SetMateria(const unsigned int slot, const int id, const unsigned int ap){
+    if (slot < MAX_MATERIA_SLOTS){
+        materia_[slot].id = id;
+        materia_[slot].ap = ap;
+        materia_[slot].enemy_skill = false;
+        for (int i = 0; i < MAX_ENEMY_SKILLS; i ++) materia_[slot].enemy_skill_learned[i] = false;
+    }
+}
+
+void Savemap::SetESkillMateria(const unsigned slot, const unsigned int skill, const bool learned){
+    if (slot < MAX_MATERIA_SLOTS && skill < MAX_ENEMY_SKILLS){
+        // TODO: Maybe set enemy skill materia ID here?
+        materia_[slot].ap = 0;
+        materia_[slot].enemy_skill_learned[skill] = learned;
+        if (learned) materia_[slot].enemy_skill = true;
+    }
+}
+
+void Savemap::SetMateriaStash(const unsigned int slot, const int id, const unsigned int ap){
+    if (slot < MAX_STASH_SLOTS){
+        materia_stash_[slot].id = id;
+        materia_stash_[slot].ap = ap;
+        materia_stash_[slot].enemy_skill = false;
+        for (int i = 0; i < MAX_ENEMY_SKILLS; i ++)
+            materia_stash_[slot].enemy_skill_learned[i] = false;
+    }
+}
+
+void Savemap::SetESkillMateriaStash(
+  const unsigned slot, const unsigned int skill, const bool learned
+){
+    if (slot < MAX_STASH_SLOTS && skill < MAX_ENEMY_SKILLS){
+        // TODO: Maybe set enemy skill materia ID here?
+        materia_stash_[slot].ap = 0;
+        materia_stash_[slot].enemy_skill_learned[skill] = learned;
+        if (learned) materia_stash_[slot].enemy_skill = true;
+    }
+}
+
+void Savemap::SetLocation(
+  const float x, const float y, const float z,
+  const unsigned int triangle, const int angle, std::string field, std::string name
+){
+
+    location_.x = x;
+    location_.y = y;
+    location_.z = z;
+    location_.triangle = triangle;
+    if (angle >= -360 && angle <= 360) location_.angle = angle;
+    location_.field = field;
+    location_.name = name;
+}
+
+void Savemap::SetSetting(const std::string& key, const int value){settings_[key] = value;}
+
+bool Savemap::HasSetting(const std::string& key) const{return settings_.count(key) > 0;}
+
+int Savemap::GetSetting(const std::string& key) const{
+    const auto it = settings_.find(key);
+    return (it == settings_.end()) ? 0 : it->second;
+}
+
+void Savemap::SetCharacterInfo(
+  const unsigned int id, const int char_id, const std::string name,
+  const bool enabled, const bool locked,
+  const unsigned int level, const unsigned int kills,
+  const bool back_row, const unsigned int exp, const unsigned int exp_to_next,
+  const unsigned int limit_level, const unsigned int limit_bar,
+  const unsigned int weapon, const unsigned int armor, const int accessory
+){
+    if (id < MAX_CHARACTERS){
+        characters_[id].id = id;
+        characters_[id].char_id = char_id;
+        characters_[id].name = name;
+        characters_[id].enabled = enabled;
+        characters_[id].locked = locked;
+        characters_[id].level = level;
+        characters_[id].kills = kills;
+        characters_[id].back_row = back_row;
+        characters_[id].exp = exp;
+        characters_[id].exp_to_next = exp_to_next;
+        characters_[id].limit_level = std::min(limit_level, MAX_LIMIT_LEVELS);
+        characters_[id].limit_bar = std::min(limit_bar, MAX_LIMIT_BAR);
+        characters_[id].weapon.id = weapon;
+        characters_[id].armor.id = armor;
+        characters_[id].accessory = std::max(accessory, -1);
+    }
+}
+
+void Savemap::SetCharacterStat(
+  const unsigned int id, const unsigned int stat,
+  const unsigned int base, const unsigned int extra
+){
+    if (id < MAX_CHARACTERS){
+        if (stat == 0){
+            characters_[id].str.base = base;
+            characters_[id].str.extra = extra;
+        }
+        else if (stat == 1){
+            characters_[id].vit.base = base;
+            characters_[id].vit.extra = extra;
+        }
+        else if (stat == 2){
+            characters_[id].mag.base = base;
+            characters_[id].mag.extra = extra;
+        }
+        else if (stat == 3){
+            characters_[id].spr.base = base;
+            characters_[id].spr.extra = extra;
+        }
+        else if (stat == 4){
+            characters_[id].dex.base = base;
+            characters_[id].dex.extra = extra;
+        }
+        else if (stat == 5){
+            characters_[id].lck.base = base;
+            characters_[id].lck.extra = extra;
+        }
+        else if (stat == 6){
+            characters_[id].hp.base = base;
+            characters_[id].hp.extra = extra;
+        }
+        else if (stat == 7){
+            characters_[id].mp.base = base;
+            characters_[id].mp.extra = extra;
+        }
+    }
+}
+void Savemap::SetCharacterLimitLearned(
+  const unsigned int id, const unsigned int level,
+  const unsigned int technique, const bool learned, const unsigned int uses
+){
+    if (id < MAX_CHARACTERS && level < MAX_LIMIT_LEVELS && technique < MAX_LIMIT_TECHNIQUES){
+        characters_[id].limits_learned[level][technique] = learned;
+        characters_[id].limit_uses[level] = uses;
+    }
+}
+
+void Savemap::SetCharacterMateria(
+  const unsigned int id, const bool weapon, const unsigned int slot,
+  const int materia, const unsigned int ap
+){
+    if (id < MAX_CHARACTERS && slot < MAX_EQUIP_SLOTS){
+        if (weapon){
+            characters_[id].weapon.materia[slot].id = materia;
+            characters_[id].weapon.materia[slot].ap = ap;
+            characters_[id].weapon.materia[slot].enemy_skill = false;
+            for (int i = 0; i < MAX_ENEMY_SKILLS; i ++)
+                characters_[id].weapon.materia[slot].enemy_skill_learned[i] = false;
+        }
+        else{
+            characters_[id].armor.materia[slot].id = materia;
+            characters_[id].armor.materia[slot].ap = ap;
+            characters_[id].armor.materia[slot].enemy_skill = false;
+            for (int i = 0; i < MAX_ENEMY_SKILLS; i ++)
+                characters_[id].armor.materia[slot].enemy_skill_learned[i] = false;
+        }
+    }
+}
+
+void Savemap::SetCharacterESkillMateria(
+  const unsigned int id, const bool weapon, const unsigned int slot,
+  const unsigned int skill, const bool learned
+){
+    if (id < MAX_CHARACTERS && slot < MAX_EQUIP_SLOTS && skill < MAX_ENEMY_SKILLS){
+        if (weapon){
+            // TODO: Maybe set enemy skill materia ID here?
+            characters_[id].weapon.materia[slot].ap = 0;
+            characters_[id].weapon.materia[slot].enemy_skill_learned[skill] = learned;
+            if (learned) characters_[id].weapon.materia[slot].enemy_skill = true;
+        }
+        else{
+            // TODO: Maybe set enemy skill materia ID here?
+            characters_[id].armor.materia[slot].ap = 0;
+            characters_[id].armor.materia[slot].enemy_skill_learned[skill] = learned;
+            if (learned) characters_[id].armor.materia[slot].enemy_skill = true;
+        }
+    }
+}
+
+void Savemap::SetCharacterStatus(
+  const unsigned int id, const unsigned int status, const bool inflicted
+){
+    //TODO
+}
+
+bool Savemap::IsEmpty(){return empty_;}
+
+std::string Savemap::GetControlKey(){return control_;}
+
+unsigned int Savemap::GetWindowCornerColourComponent(
+  const unsigned int corner, const unsigned int comp
+){
+    if (corner < 4 && comp < 3) return window_colours_[corner][comp];
+    else return 0;
+}
+
+unsigned int Savemap::GetMoney(){return money_;}
+
+unsigned int Savemap::GetGameTime(){return seconds_;}
+
+unsigned int Savemap::GetCountdownTime(){return countdown_;}
+
+int Savemap::GetPartyMember(const unsigned int pos){
+    if (pos < MAX_PARTY_MEMBERS) return party_[pos];
+    else return -1;
+}
+
+unsigned int Savemap::GetItemAtPosId(const unsigned int pos){
+    if (pos >= MAX_ITEM_SLOTS) return 0;
+    else if (items_[pos].quantity == 0) return 0;
+    else return items_[pos].id;
+}
+
+unsigned int Savemap::GetItemAtPosQty(const unsigned int pos){
+    if (pos >= MAX_ITEM_SLOTS) return 0;
+    else return items_[pos].quantity;
+}
+
+bool Savemap::GetKeyItem(const unsigned int id){
+    if (id >= MAX_KEY_ITEM_SLOTS) return false;
+    else return key_items_[id];
+}
+
+int Savemap::GetMateriaAtPosId(const unsigned int pos){
+    if (pos >= MAX_MATERIA_SLOTS) return -1;
+    else return materia_[pos].id;
+}
+
+unsigned int Savemap::GetMateriaAtPosAp(const unsigned int pos){
+    if (pos >= MAX_MATERIA_SLOTS) return 0;
+    else if (materia_[pos].id < 0) return 0;
+    else if (materia_[pos].enemy_skill) return 0;
+    else return materia_[pos].ap;
+}
+
+bool Savemap::IsMateriaAtPosESkill(const unsigned int pos){
+    if (pos >= MAX_MATERIA_SLOTS) return false;
+    else if (materia_[pos].id < 0) return false;
+    else return materia_[pos].enemy_skill;
+}
+
+bool Savemap::IsMateriaAtPosESkillLearned(const unsigned int pos, const unsigned int skill){
+    if (pos >= MAX_MATERIA_SLOTS) return false;
+    else if (materia_[pos].id < 0) return false;
+    else if (materia_[pos].enemy_skill == false) return false;
+    else if (skill >= MAX_ENEMY_SKILLS) return false;
+    else return materia_[pos].enemy_skill_learned[skill];
+}
+
+int Savemap::GetStashAtPosId(const unsigned int pos){
+    if (pos >= MAX_STASH_SLOTS) return -1;
+    else return materia_stash_[pos].id;
+}
+
+unsigned int Savemap::GetStashAtPosAp(const unsigned int pos){
+    if (pos >= MAX_STASH_SLOTS) return 0;
+    else if (materia_stash_[pos].id < 0) return 0;
+    else if (materia_stash_[pos].enemy_skill) return 0;
+    else return materia_stash_[pos].ap;
+}
+
+bool Savemap::IsStashAtPosESkill(const unsigned int pos){
+    if (pos >= MAX_STASH_SLOTS) return false;
+    else if (materia_stash_[pos].id < 0) return false;
+    else return materia_stash_[pos].enemy_skill;
+}
+
+bool Savemap::IsStashAtPosESkillLearned(const unsigned int pos, const unsigned int skill){
+    if (pos >= MAX_STASH_SLOTS) return false;
+    else if (materia_stash_[pos].id < 0) return false;
+    else if (materia_stash_[pos].enemy_skill == false) return false;
+    else if (skill >= MAX_ENEMY_SKILLS) return false;
+    else return materia_stash_[pos].enemy_skill_learned[skill];
+}
+
+float Savemap::GetLocationX(){return location_.x;}
+
+float Savemap::GetLocationY(){return location_.y;}
+
+float Savemap::GetLocationZ(){return location_.z;}
+
+unsigned int Savemap::GetLocationTriangle(){return location_.triangle;}
+
+int Savemap::GetLocationAngle(){return location_.angle;}
+
+std::string Savemap::GetLocationField(){return location_.field;}
+
+std::string Savemap::GetLocationName(){return location_.name;}
+
+int Savemap::GetCharacterCharId(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return -1;
+    else return characters_[id].char_id;
+}
+
+std::string Savemap::GetCharacterName(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return std::string("");
+    else return characters_[id].name;
+}
+
+unsigned int Savemap::GetCharacterLevel(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 1;
+    else return characters_[id].level;
+}
+
+unsigned int Savemap::GetCharacterKills(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].kills;
+}
+
+bool Savemap::IsCharacterEnabled(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return false;
+    else return characters_[id].enabled;
+}
+
+bool Savemap::IsCharacterLocked(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return false;
+    else return characters_[id].locked;
+}
+
+bool Savemap::IsCharacterBackRow(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return false;
+    else return characters_[id].back_row;
+}
+
+unsigned int Savemap::GetCharacterExp(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].exp;
+}
+
+unsigned int Savemap::GetCharacterExpToNext(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].exp_to_next;
+}
+
+unsigned int Savemap::GetCharacterLimitLevel(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].limit_level;
+}
+
+unsigned int Savemap::GetCharacterLimitBar(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].limit_bar;
+}
+
+unsigned int Savemap::GetCharacterWeaponId(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].weapon.id;
+}
+
+unsigned int Savemap::GetCharacterArmorId(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return 0;
+    else return characters_[id].armor.id;
+}
+
+int Savemap::GetCharacterAccessoryId(const unsigned int id){
+    if (id >= MAX_CHARACTERS) return -1;
+    else return characters_[id].accessory;
+}
+
+unsigned int Savemap::GetCharacterStatBase(const unsigned int id, const unsigned int stat){
+    if (id >= MAX_CHARACTERS) return 0;
+    switch (stat){
+        case STAT::STR: return characters_[id].str.base; break;
+        case STAT::VIT: return characters_[id].vit.base; break;
+        case STAT::MAG: return characters_[id].mag.base; break;
+        case STAT::SPR: return characters_[id].spr.base; break;
+        case STAT::DEX: return characters_[id].dex.base; break;
+        case STAT::LCK: return characters_[id].lck.base; break;
+        case STAT::HP: return characters_[id].hp.base; break;
+        case STAT::MP: return characters_[id].mp.base; break;
+        default: return 0;
+    }
+}
+
+unsigned int Savemap::GetCharacterStatExtra(const unsigned int id, const unsigned int stat){
+    if (id >= MAX_CHARACTERS) return 0;
+    switch (stat){
+        case STAT::STR: return characters_[id].str.extra; break;
+        case STAT::VIT: return characters_[id].vit.extra; break;
+        case STAT::MAG: return characters_[id].mag.extra; break;
+        case STAT::SPR: return characters_[id].spr.extra; break;
+        case STAT::DEX: return characters_[id].dex.extra; break;
+        case STAT::LCK: return characters_[id].lck.extra; break;
+        case STAT::HP: return characters_[id].hp.extra; break;
+        case STAT::MP: return characters_[id].mp.extra; break;
+        default: return 0;
+    }
+}
+
+unsigned int Savemap::GetCharacterLimitUses(const unsigned int id, const unsigned int level){
+    if (id >= MAX_CHARACTERS) return 0;
+    else if (level >= MAX_LIMIT_LEVELS) return 0;
+    else return characters_[id].limit_uses[level];
+}
+
+bool Savemap::IsCharacterLimitLearned(
+  const unsigned int id, const unsigned int level, const unsigned int tech
+){
+    if (id >= MAX_CHARACTERS) return false;
+    else if (level >= MAX_LIMIT_LEVELS) return false;
+    else if (tech >= MAX_LIMIT_TECHNIQUES) return false;
+    else return characters_[id].limits_learned[level][tech];
+}
+
+
+int Savemap::GetCharacterMateriaId(
+  const unsigned int id, const bool weapon, const unsigned int pos
+){
+    if (id >= MAX_CHARACTERS) return -1;
+    else if (pos >= MAX_EQUIP_SLOTS) return -1;
+    if (weapon) return characters_[id].weapon.materia[pos].id;
+    else return characters_[id].armor.materia[pos].id;
+}
+
+unsigned int Savemap::GetCharacterMateriaAp(
+  const unsigned int id, const bool weapon, const unsigned int pos
+){
+    if (id >= MAX_CHARACTERS) return 0;
+    else if (pos >= MAX_EQUIP_SLOTS) return 0;
+    if (weapon){
+        if (characters_[id].weapon.materia[pos].id == -1) return 0;
+        else if (characters_[id].weapon.materia[pos].enemy_skill) return 0;
+        else return characters_[id].weapon.materia[pos].ap;
+    }
+    else{
+        if (characters_[id].armor.materia[pos].id == -1) return 0;
+        else if (characters_[id].armor.materia[pos].enemy_skill) return 0;
+        else return characters_[id].armor.materia[pos].ap;
+    }
+}
+
+bool Savemap::IsCharacterMateriaESkill(
+  const unsigned int id, const bool weapon, const unsigned int pos
+){
+    if (id >= MAX_CHARACTERS) return false;
+    else if (pos >= MAX_EQUIP_SLOTS) return false;
+    if (weapon){
+        if (characters_[id].weapon.materia[pos].id == -1) return false;
+        else return characters_[id].weapon.materia[pos].enemy_skill;
+    }
+    else{
+        if (characters_[id].armor.materia[pos].id == -1) return false;
+        else return characters_[id].armor.materia[pos].enemy_skill;
+    }
+}
+
+bool Savemap::IsCharacterMateriaESkillLearned(
+  const unsigned int id, const bool weapon, const unsigned int pos, const unsigned int skill
+){
+    if (id >= MAX_CHARACTERS) return false;
+    else if (pos >= MAX_EQUIP_SLOTS) return false;
+    else if (skill >= MAX_ENEMY_SKILLS) return false;
+    if (weapon){
+        if (characters_[id].weapon.materia[pos].id == -1) return false;
+        else if (characters_[id].weapon.materia[pos].enemy_skill == false) return false;
+        else return characters_[id].weapon.materia[pos].enemy_skill_learned[skill];
+    }
+    else{
+        if (characters_[id].armor.materia[pos].id == -1) return false;
+        else if (characters_[id].armor.materia[pos].enemy_skill == false) return false;
+        else return characters_[id].armor.materia[pos].enemy_skill_learned[skill];
+    }
+}
+
+int Savemap::GetData(const unsigned int bank, const unsigned int address){
+    if (bank >= BANK_COUNT) return 0;
+    else if (address >= BANK_ADDRESS_COUNT) return 0;
+    else return data_[bank][address];
+}

+ 254 - 0
src/engine/save/SavemapWrite.cpp

@@ -0,0 +1,254 @@
+/*
+ * 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 <boost/filesystem.hpp>
+#include <tinyxml.h>
+#include "common/Logger.h"
+#include "engine/save/Savemap.h"
+
+namespace{
+
+/**
+ * Adds a new element at the end of another one.
+ */
+TiXmlElement& AddElement(TiXmlElement& parent, const char* name){
+    TiXmlElement* element = new TiXmlElement(name);
+    // The parent takes care of deleting it.
+    parent.LinkEndChild(element);
+    return *element;
+}
+
+/**
+ * Writes a list of materia: the ones of the inventory, the stash or a piece of equipment. The
+ * empty slots are not written.
+ */
+void WriteMaterias(
+  TiXmlElement& parent, const char* name, const SavemapData::Materia* materias,
+  const unsigned int count
+){
+    TiXmlElement& list = AddElement(parent, name);
+    for (unsigned int i = 0; i < count; i ++){
+        const SavemapData::Materia& materia = materias[i];
+        if (materia.id < 0) continue;
+        TiXmlElement& element = AddElement(list, "Materia");
+        element.SetAttribute("slot", i);
+        element.SetAttribute("id", materia.id);
+        element.SetAttribute("ap", materia.ap);
+        if (!materia.enemy_skill) continue;
+        element.SetAttribute("enemy_skill", true);
+        TiXmlElement& skills = AddElement(element, "EnemySkill");
+        for (unsigned int s = 0; s < SavemapData::MAX_ENEMY_SKILLS; s ++){
+            if (!materia.enemy_skill_learned[s]) continue;
+            TiXmlElement& skill = AddElement(skills, "Skill");
+            skill.SetAttribute("id", s);
+            skill.SetAttribute("learned", true);
+        }
+    }
+}
+
+/**
+ * Writes a stat of a character.
+ *
+ * @param[in] second Name of the second value of the stat.
+ */
+void WriteStat(
+  TiXmlElement& parent, const char* id, const SavemapData::Character::Stat& stat,
+  const char* second
+){
+    TiXmlElement& element = AddElement(parent, "Stat");
+    element.SetAttribute("id", id);
+    element.SetAttribute("base", stat.base);
+    element.SetAttribute(second, stat.extra);
+}
+
+/**
+ * Writes a weapon or an armor of a character, with its materia.
+ */
+void WriteEquipment(
+  TiXmlElement& parent, const char* name, const SavemapData::Character::Equipment& equipment
+){
+    TiXmlElement& element = AddElement(parent, name);
+    element.SetAttribute("id", equipment.id);
+    WriteMaterias(element, "Materia", equipment.materia, SavemapData::MAX_EQUIP_SLOTS);
+}
+
+/**
+ * Writes the limit breaks of a character.
+ */
+void WriteLimits(TiXmlElement& parent, const SavemapData::Character& character){
+    TiXmlElement& limits = AddElement(parent, "Limits");
+    limits.SetAttribute("current", character.limit_level);
+    limits.SetAttribute("bar", character.limit_bar);
+    for (unsigned int l = 0; l < SavemapData::MAX_LIMIT_LEVELS; l ++){
+        TiXmlElement& level = AddElement(limits, "Level");
+        level.SetAttribute("level", l);
+        level.SetAttribute("uses", character.limit_uses[l]);
+        for (unsigned int t = 0; t < SavemapData::MAX_LIMIT_TECHNIQUES; t ++){
+            if (!character.limits_learned[l][t]) continue;
+            TiXmlElement& technique = AddElement(level, "Technique");
+            technique.SetAttribute("id", t);
+            technique.SetAttribute("learned", true);
+        }
+    }
+}
+
+/**
+ * Writes a character.
+ */
+void WriteCharacter(TiXmlElement& parent, const SavemapData::Character& character){
+    TiXmlElement& element = AddElement(parent, "Character");
+    element.SetAttribute("id", character.id);
+    element.SetAttribute("char_id", character.char_id);
+    element.SetAttribute("name", character.name);
+    element.SetAttribute("level", character.level);
+    element.SetAttribute("enabled", character.enabled);
+    element.SetAttribute("locked", character.locked);
+    element.SetAttribute("kills", character.kills);
+    element.SetAttribute("back_row", character.back_row);
+    element.SetAttribute("exp", character.exp);
+    element.SetAttribute("exp_to_next", character.exp_to_next);
+
+    TiXmlElement& stats = AddElement(element, "Stats");
+    WriteStat(stats, "str", character.str, "bonus");
+    WriteStat(stats, "vit", character.vit, "bonus");
+    WriteStat(stats, "mag", character.mag, "bonus");
+    WriteStat(stats, "spr", character.spr, "bonus");
+    WriteStat(stats, "dex", character.dex, "bonus");
+    WriteStat(stats, "lck", character.lck, "bonus");
+    WriteStat(stats, "hp", character.hp, "current");
+    WriteStat(stats, "mp", character.mp, "current");
+
+    WriteLimits(element, character);
+    WriteEquipment(element, "Weapon", character.weapon);
+    WriteEquipment(element, "Armor", character.armor);
+    if (character.accessory >= 0)
+        AddElement(element, "Accessory").SetAttribute("id", character.accessory);
+    // TODO: Statuses
+}
+
+}
+
+void Savemap::Write(int slot, std::string file_name){
+    TiXmlDocument xml;
+    std::unique_ptr<TiXmlElement> container(new TiXmlElement("Savemap"));
+    container->SetAttribute("slot", slot);
+    container->SetAttribute("key", control_);
+
+    WriteWindowColours(*container);
+    WriteTimes(*container);
+    WriteLocation(*container);
+    WriteParty(*container);
+    WriteItems(*container);
+    WriteMaterias(*container, "Materia", materia_, MAX_MATERIA_SLOTS);
+    WriteMaterias(*container, "MateriaStash", materia_stash_, MAX_STASH_SLOTS);
+    WriteSettings(*container);
+    TiXmlElement& characters = AddElement(*container, "Characters");
+    for (const Character& character : characters_)
+        if (character.char_id != -1) WriteCharacter(characters, character);
+    WriteDataBanks(*container);
+
+    xml.LinkEndChild(container.release());
+    // The folder of the saved games may not exist yet (a fresh installation): create it, or the
+    // game would not be saved.
+    const boost::filesystem::path path(file_name);
+    if (path.has_parent_path()){
+        boost::system::error_code error;
+        boost::filesystem::create_directories(path.parent_path(), error);
+    }
+    if (!xml.SaveFile(file_name)) LOG_ERROR("Can't write the saved game \"" + file_name + "\".");
+    slot_ = slot;
+}
+
+void Savemap::WriteWindowColours(TiXmlElement& parent) const{
+    TiXmlElement& colours = AddElement(parent, "WindowColours");
+    for (int c = 0; c < 4; c ++){
+        TiXmlElement& corner = AddElement(colours, "Corner");
+        corner.SetAttribute("position", CORNER_NAMES[c]);
+        corner.SetAttribute("red", window_colours_[c][0]);
+        corner.SetAttribute("green", window_colours_[c][1]);
+        corner.SetAttribute("blue", window_colours_[c][2]);
+    }
+}
+
+void Savemap::WriteTimes(TiXmlElement& parent) const{
+    if (money_ > 0) AddElement(parent, "Money").SetAttribute("quantity", money_);
+    if (seconds_ > 0) AddElement(parent, "GameTime").SetAttribute("seconds", seconds_);
+    if (countdown_ > 0) AddElement(parent, "Countdown").SetAttribute("seconds", countdown_);
+}
+
+void Savemap::WriteLocation(TiXmlElement& parent) const{
+    TiXmlElement& location = AddElement(parent, "Location");
+    location.SetDoubleAttribute("x", location_.x);
+    location.SetDoubleAttribute("y", location_.y);
+    if (location_.z < 0) location.SetAttribute("z", -1);
+    else location.SetDoubleAttribute("z", location_.z);
+    location.SetAttribute("triangle", location_.triangle);
+    location.SetAttribute("orientation", location_.angle);
+    location.SetAttribute("field", location_.field);
+    location.SetAttribute("name", location_.name);
+}
+
+void Savemap::WriteParty(TiXmlElement& parent) const{
+    TiXmlElement& party = AddElement(parent, "Party");
+    for (unsigned int p = 0; p < MAX_PARTY_MEMBERS; p ++){
+        if (party_[p] < 0 || party_[p] >= static_cast<int>(MAX_CHARACTERS)) continue;
+        TiXmlElement& member = AddElement(party, "Member");
+        member.SetAttribute("position", p);
+        member.SetAttribute("character_id", party_[p]);
+    }
+}
+
+void Savemap::WriteItems(TiXmlElement& parent) const{
+    TiXmlElement& items = AddElement(parent, "Items");
+    for (unsigned int i = 0; i < MAX_ITEM_SLOTS; i ++){
+        if (items_[i].quantity == 0) continue;
+        TiXmlElement& item = AddElement(items, "Item");
+        item.SetAttribute("slot", i);
+        item.SetAttribute("id", items_[i].id);
+        item.SetAttribute("quantity", items_[i].quantity);
+    }
+    TiXmlElement& key_items = AddElement(parent, "KeyItems");
+    for (unsigned int i = 0; i < MAX_KEY_ITEM_SLOTS; i ++){
+        if (!key_items_[i]) continue;
+        TiXmlElement& item = AddElement(key_items, "Item");
+        item.SetAttribute("id", i);
+        item.SetAttribute("owned", true);
+    }
+}
+
+void Savemap::WriteSettings(TiXmlElement& parent) const{
+    // Settings of the config menu. The window colours are saved apart.
+    if (settings_.empty()) return;
+    TiXmlElement& settings = AddElement(parent, "Settings");
+    for (const auto& setting : settings_){
+        TiXmlElement& element = AddElement(settings, "Setting");
+        element.SetAttribute("name", setting.first);
+        element.SetAttribute("value", setting.second);
+    }
+}
+
+void Savemap::WriteDataBanks(TiXmlElement& parent) const{
+    TiXmlElement& banks = AddElement(parent, "DataBanks");
+    for (unsigned int b = 0; b < BANK_COUNT; b ++){
+        TiXmlElement& bank = AddElement(banks, "Bank");
+        bank.SetAttribute("id", b);
+        for (unsigned int a = 0; a < BANK_ADDRESS_COUNT; a ++){
+            if (data_[b][a] == 0) continue;
+            TiXmlElement& address = AddElement(bank, "Address");
+            address.SetAttribute("address", a);
+            address.SetAttribute("value", data_[b][a]);
+        }
+    }
+}

+ 1 - 0
src/engine/script/ScriptManagerBinds.h

@@ -285,6 +285,7 @@ void ScriptManager::InitBinds(){
       "release", &SavemapHandler::Release,
       "release", &SavemapHandler::Release,
       "get_current_savemap", &SavemapHandler::GetCurrentSavemap,
       "get_current_savemap", &SavemapHandler::GetCurrentSavemap,
       "get_savemap", &SavemapHandler::GetSavemap,
       "get_savemap", &SavemapHandler::GetSavemap,
+      "clear_contents", &SavemapHandler::ClearContents,
       "set_data", &SavemapHandler::SetData,
       "set_data", &SavemapHandler::SetData,
       "set_control_key", &SavemapHandler::SetControlKey,
       "set_control_key", &SavemapHandler::SetControlKey,
       "set_window_colours", &SavemapHandler::SetWindowColours,
       "set_window_colours", &SavemapHandler::SetWindowColours,

+ 1 - 0
test/engine/CMakeLists.txt

@@ -1,4 +1,5 @@
 # Tests of this folder (see vgears_tests and vgears_installer_tests in test/CMakeLists.txt).
 # Tests of this folder (see vgears_tests and vgears_installer_tests in test/CMakeLists.txt).
 add_subdirectory(entity)
 add_subdirectory(entity)
 add_subdirectory(graphics)
 add_subdirectory(graphics)
+add_subdirectory(save)
 add_subdirectory(ui)
 add_subdirectory(ui)

+ 4 - 0
test/engine/save/CMakeLists.txt

@@ -0,0 +1,4 @@
+# Tests of this folder (see vgears_tests and vgears_installer_tests in test/CMakeLists.txt).
+vgears_tests(
+    Savemap.cpp
+)

+ 161 - 0
test/engine/save/Savemap.cpp

@@ -0,0 +1,161 @@
+/*
+ * 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 <boost/filesystem.hpp>
+#include <boost/test/unit_test.hpp>
+#include <memory>
+#include <OgreLogManager.h>
+#include "engine/save/Savemap.h"
+#include "engine/save/SavemapHandler.h"
+
+/**
+ * Writes a savemap to a temporary file and reads it into another one.
+ */
+static std::unique_ptr<Savemap> WriteAndRead(Savemap& savemap){
+    const boost::filesystem::path file
+      = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
+    savemap.Write(3, file.string());
+    auto read = std::make_unique<Savemap>();
+    read->Read(file.string());
+    boost::filesystem::remove(file);
+    return read;
+}
+
+/**
+ * A savemap with a bit of everything. It's too big for the stack.
+ */
+static std::unique_ptr<Savemap> MakeSavemap(){
+    auto savemap = std::make_unique<Savemap>();
+    savemap->SetControlKey("abcdefgh");
+    savemap->SetMoney(1234);
+    savemap->SetGameTime(100);
+    savemap->SetCountdownTime(50);
+    savemap->SetParty(0, -1, 2);
+    savemap->SetItem(4, 17, 9);
+    savemap->SetMateria(2, 5, 3000);
+    savemap->SetData(1, 20, -7);
+    savemap->SetSetting("field_leader", 2);
+    savemap->SetCharacterInfo(1, 1, "Barret", true, false, 12, 3, true, 500, 90, 2, 40, 4, 5, 6);
+    savemap->SetCharacterStat(1, Savemap::STAT::HP, 300, 250);
+    savemap->SetCharacterMateria(1, true, 3, 8, 700);
+    savemap->SetKeyItem(0, true);
+    savemap->SetKeyItem(30, true);
+    savemap->SetMateria(7, 44, 0);
+    savemap->SetESkillMateria(7, 5, true);
+    savemap->SetMateriaStash(1, 44, 0);
+    savemap->SetESkillMateriaStash(1, 23, true);
+    savemap->SetCharacterMateria(1, false, 2, 44, 0);
+    savemap->SetCharacterESkillMateria(1, false, 2, 0, true);
+    return savemap;
+}
+
+BOOST_AUTO_TEST_CASE(TestSavemapIsReadAsItWasWritten){
+    const std::unique_ptr<Savemap> savemap = MakeSavemap();
+    const std::unique_ptr<Savemap> read = WriteAndRead(*savemap);
+    BOOST_CHECK(!read->IsEmpty());
+    BOOST_CHECK_EQUAL(read->GetControlKey(), "abcdefgh");
+    BOOST_CHECK_EQUAL(read->GetMoney(), 1234u);
+    BOOST_CHECK_EQUAL(read->GetGameTime(), 100u);
+    BOOST_CHECK_EQUAL(read->GetCountdownTime(), 50u);
+    BOOST_CHECK_EQUAL(read->GetPartyMember(0), 0);
+    BOOST_CHECK_EQUAL(read->GetPartyMember(1), -1);
+    BOOST_CHECK_EQUAL(read->GetPartyMember(2), 2);
+    BOOST_CHECK_EQUAL(read->GetItemAtPosId(4), 17u);
+    BOOST_CHECK_EQUAL(read->GetItemAtPosQty(4), 9u);
+    BOOST_CHECK_EQUAL(read->GetMateriaAtPosId(2), 5);
+    BOOST_CHECK_EQUAL(read->GetMateriaAtPosAp(2), 3000u);
+    BOOST_CHECK_EQUAL(read->GetData(1, 20), -7);
+    BOOST_CHECK_EQUAL(read->GetSetting("field_leader"), 2);
+    BOOST_CHECK_EQUAL(read->GetCharacterName(1), "Barret");
+    BOOST_CHECK_EQUAL(read->GetCharacterLevel(1), 12u);
+    BOOST_CHECK(read->IsCharacterBackRow(1));
+    BOOST_CHECK_EQUAL(read->GetCharacterStatBase(1, Savemap::STAT::HP), 300u);
+    BOOST_CHECK_EQUAL(read->GetCharacterStatExtra(1, Savemap::STAT::HP), 250u);
+    BOOST_CHECK_EQUAL(read->GetCharacterMateriaId(1, true, 3), 8);
+}
+
+BOOST_AUTO_TEST_CASE(TestSavemapCopyHasEverything){
+    const std::unique_ptr<Savemap> savemap = MakeSavemap();
+    auto copy = std::make_unique<Savemap>();
+    *copy = *savemap;
+    BOOST_CHECK_EQUAL(copy->GetMoney(), 1234u);
+    BOOST_CHECK_EQUAL(copy->GetGameTime(), 100u);
+    BOOST_CHECK_EQUAL(copy->GetItemAtPosId(4), 17u);
+    BOOST_CHECK_EQUAL(copy->GetMateriaAtPosId(2), 5);
+    BOOST_CHECK_EQUAL(copy->GetData(1, 20), -7);
+}
+
+BOOST_AUTO_TEST_CASE(TestSavemapKeepsKeyItemsAndEnemySkills){
+    const std::unique_ptr<Savemap> savemap = MakeSavemap();
+    const std::unique_ptr<Savemap> read = WriteAndRead(*savemap);
+    for (unsigned int item = 0; item < 40; item ++)
+        BOOST_CHECK_EQUAL(read->GetKeyItem(item), item == 0 || item == 30);
+    BOOST_CHECK(read->IsMateriaAtPosESkillLearned(7, 5));
+    BOOST_CHECK(!read->IsMateriaAtPosESkillLearned(7, 4));
+    BOOST_CHECK(read->IsStashAtPosESkillLearned(1, 23));
+    BOOST_CHECK(read->IsCharacterMateriaESkillLearned(1, false, 2, 0));
+    BOOST_CHECK(!read->IsCharacterMateriaESkillLearned(1, false, 2, 1));
+}
+
+BOOST_AUTO_TEST_CASE(TestSavemapClearedContentsAreNotSaved){
+    const std::unique_ptr<Savemap> savemap = MakeSavemap();
+    savemap->ClearContents();
+    // Saving the game again with less than before.
+    savemap->SetItem(6, 20, 1);
+    const std::unique_ptr<Savemap> read = WriteAndRead(*savemap);
+    BOOST_CHECK_EQUAL(read->GetItemAtPosQty(4), 0u);
+    BOOST_CHECK_EQUAL(read->GetItemAtPosQty(6), 1u);
+    BOOST_CHECK(!read->GetKeyItem(30));
+    BOOST_CHECK_EQUAL(read->GetMateriaAtPosId(2), -1);
+    BOOST_CHECK(!read->IsMateriaAtPosESkillLearned(7, 5));
+    BOOST_CHECK_EQUAL(read->GetCharacterMateriaId(1, true, 3), -1);
+    BOOST_CHECK(!read->IsCharacterMateriaESkillLearned(1, false, 2, 0));
+    BOOST_CHECK_EQUAL(read->GetData(1, 20), 0);
+    // The rest stays.
+    BOOST_CHECK_EQUAL(read->GetMoney(), 1234u);
+    BOOST_CHECK_EQUAL(read->GetSetting("field_leader"), 2);
+    BOOST_CHECK_EQUAL(read->GetCharacterName(1), "Barret");
+    BOOST_CHECK(read->IsStashAtPosESkillLearned(1, 23));
+}
+
+BOOST_AUTO_TEST_CASE(TestSavemapHandlerSlotsDoNotShareTheirData){
+    // The handler writes to the log, which needs a log manager.
+    std::unique_ptr<Ogre::LogManager> log_manager;
+    if (Ogre::LogManager::getSingletonPtr() == nullptr){
+        log_manager = std::make_unique<Ogre::LogManager>();
+        log_manager->createLog("test", true, false, true);
+    }
+    // The saved games are in a folder relative to the current one.
+    const boost::filesystem::path previous = boost::filesystem::current_path();
+    const boost::filesystem::path folder
+      = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
+    boost::filesystem::create_directories(folder);
+    boost::filesystem::current_path(folder);
+    {
+        auto handler = std::make_unique<SavemapHandler>();
+        handler->SetControlKey("abcdefgh");
+        handler->SetMoney(100);
+        BOOST_CHECK(handler->Save(0, false));
+        // Playing on, and saving in another slot.
+        handler->SetMoney(200);
+        BOOST_CHECK_EQUAL(handler->GetSlotMoney(0), 100u);
+        BOOST_CHECK(handler->Save(1, false));
+        handler->SetMoney(300);
+        BOOST_CHECK_EQUAL(handler->GetSlotMoney(0), 100u);
+        BOOST_CHECK_EQUAL(handler->GetSlotMoney(1), 200u);
+    }
+    boost::filesystem::current_path(previous);
+    boost::filesystem::remove_all(folder);
+}