PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/SaveLoad.cpp

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