/src/SaveLoad.cpp

http://github.com/clintbellanger/flare · C++ · 194 lines · 109 code · 38 blank · 47 comment · 48 complexity · 4ccf5de247d159fe7102dd76d2c17441 MD5 · raw file

  1. /*
  2. Copyright 2011 Clint Bellanger
  3. This file is part of FLARE.
  4. FLARE is free software: you can redistribute it and/or modify it under the terms
  5. of the GNU General Public License as published by the Free Software Foundation,
  6. either version 3 of the License, or (at your option) any later version.
  7. FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  9. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. FLARE. If not, see http://www.gnu.org/licenses/
  12. */
  13. /**
  14. * Save and Load functions for the GameStatePlay.
  15. *
  16. * I put these in a separate cpp file just to keep GameStatePlay.cpp devoted to its core.
  17. *
  18. * class GameStatePlay
  19. */
  20. #include "GameStatePlay.h"
  21. #include "UtilsParsing.h"
  22. #include <fstream>
  23. #include <iostream>
  24. #include <sstream>
  25. using namespace std;
  26. /**
  27. * Before exiting the game, save to file
  28. */
  29. void GameStatePlay::saveGame() {
  30. // game slots are currently 1-4
  31. if (game_slot == 0) return;
  32. ofstream outfile;
  33. stringstream ss;
  34. ss.str("");
  35. ss << PATH_USER << "save" << game_slot << ".txt";
  36. outfile.open(ss.str().c_str(), ios::out);
  37. if (outfile.is_open()) {
  38. // hero name
  39. outfile << "name=" << pc->stats.name << "\n";
  40. // hero visual option
  41. outfile << "option=" << pc->stats.base << "," << pc->stats.head << "," << pc->stats.portrait << "\n";
  42. // current experience
  43. outfile << "xp=" << pc->stats.xp << "\n";
  44. // stat spec
  45. outfile << "build=" << pc->stats.physical_character << "," << pc->stats.mental_character << "," << pc->stats.offense_character << "," << pc->stats.defense_character << "\n";
  46. // current gold
  47. outfile << "gold=" << menu->inv->gold << "\n";
  48. // equipped gear
  49. outfile << "equipped=" << menu->inv->inventory[EQUIPMENT].getItems() << "\n";
  50. outfile << "equipped_quantity=" << menu->inv->inventory[EQUIPMENT].getQuantities() << "\n";
  51. // carried items
  52. outfile << "carried=" << menu->inv->inventory[CARRIED].getItems() << "\n";
  53. outfile << "carried_quantity=" << menu->inv->inventory[CARRIED].getQuantities() << "\n";
  54. // spawn point
  55. outfile << "spawn=" << map->respawn_map << "," << map->respawn_point.x/UNITS_PER_TILE << "," << map->respawn_point.y/UNITS_PER_TILE << "\n";
  56. // action bar
  57. outfile << "actionbar=";
  58. for (int i=0; i<12; i++) {
  59. outfile << menu->act->hotkeys[i];
  60. if (i<11) outfile << ",";
  61. }
  62. outfile << "\n";
  63. // campaign data
  64. outfile << "campaign=";
  65. outfile << camp->getAll();
  66. outfile << endl;
  67. outfile.close();
  68. }
  69. }
  70. /**
  71. * When loading the game, load from file if possible
  72. */
  73. void GameStatePlay::loadGame() {
  74. // game slots are currently 1-4
  75. if (game_slot == 0) return;
  76. FileParser infile;
  77. int hotkeys[12];
  78. for (int i=0; i<12; i++) {
  79. hotkeys[i] = -1;
  80. }
  81. stringstream ss;
  82. ss.str("");
  83. ss << PATH_USER << "save" << game_slot << ".txt";
  84. if (infile.open(ss.str())) {
  85. while (infile.next()) {
  86. if (infile.key == "name") pc->stats.name = infile.val;
  87. else if (infile.key == "option") {
  88. pc->stats.base = infile.nextValue();
  89. pc->stats.head = infile.nextValue();
  90. pc->stats.portrait = infile.nextValue();
  91. }
  92. else if (infile.key == "xp") pc->stats.xp = atoi(infile.val.c_str());
  93. else if (infile.key == "build") {
  94. pc->stats.physical_character = atoi(infile.nextValue().c_str());
  95. pc->stats.mental_character = atoi(infile.nextValue().c_str());
  96. pc->stats.offense_character = atoi(infile.nextValue().c_str());
  97. pc->stats.defense_character = atoi(infile.nextValue().c_str());
  98. }
  99. else if (infile.key == "gold") {
  100. menu->inv->gold = atoi(infile.val.c_str());
  101. }
  102. else if (infile.key == "equipped") {
  103. menu->inv->inventory[EQUIPMENT].setItems(infile.val);
  104. }
  105. else if (infile.key == "equipped_quantity") {
  106. menu->inv->inventory[EQUIPMENT].setQuantities(infile.val);
  107. }
  108. else if (infile.key == "carried") {
  109. menu->inv->inventory[CARRIED].setItems(infile.val);
  110. }
  111. else if (infile.key == "carried_quantity") {
  112. menu->inv->inventory[CARRIED].setQuantities(infile.val);
  113. }
  114. else if (infile.key == "spawn") {
  115. map->teleport_mapname = infile.nextValue();
  116. if (fileExists(mods->locate("maps/" + map->teleport_mapname))) {
  117. map->teleport_destination.x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
  118. map->teleport_destination.y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
  119. map->teleportation = true;
  120. // prevent spawn.txt from putting us on the starting map
  121. map->clearEvents();
  122. }
  123. else {
  124. map->teleport_mapname = "spawn.txt";
  125. map->teleport_destination.x = 1;
  126. map->teleport_destination.y = 1;
  127. map->teleportation = true;
  128. }
  129. }
  130. else if (infile.key == "actionbar") {
  131. for (int i=0; i<12; i++)
  132. hotkeys[i] = atoi(infile.nextValue().c_str());
  133. menu->act->set(hotkeys);
  134. }
  135. else if (infile.key == "campaign") camp->setAll(infile.val);
  136. }
  137. infile.close();
  138. }
  139. // initialize vars
  140. pc->stats.recalc();
  141. menu->inv->applyEquipment(menu->inv->inventory[EQUIPMENT].storage);
  142. pc->stats.hp = pc->stats.maxhp;
  143. pc->stats.mp = pc->stats.maxmp;
  144. // reset character menu
  145. menu->chr->refreshStats();
  146. // just for aesthetics, turn the hero to face the camera
  147. pc->stats.direction = 6;
  148. // set up MenuTalker for this hero
  149. menu->talker->setHero(pc->stats.name, pc->stats.portrait);
  150. // load sounds (gender specific)
  151. pc->loadSounds();
  152. }