/src/QuestLog.cpp
C++ | 155 lines | 83 code | 27 blank | 45 comment | 26 complexity | f9e60df16dbefd6aab84351e107eaf6c MD5 | raw file
Possible License(s): GPL-3.0
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 * class QuestLog 20 * 21 * Helper text to remind the player of active quests 22 */ 23 24#include "QuestLog.h" 25#include "FileParser.h" 26#include "SharedResources.h" 27#include "UtilsFileSystem.h" 28 29#include <fstream> 30 31using namespace std; 32 33 34QuestLog::QuestLog(CampaignManager *_camp, MenuLog *_log) { 35 camp = _camp; 36 log = _log; 37 38 newQuestNotification = false; 39 resetQuestNotification = false; 40 quest_count = 0; 41 loadAll(); 42} 43 44/** 45 * Load each [mod]/quests/index.txt file 46 */ 47void QuestLog::loadAll() { 48 string test_path; 49 50 // load each items.txt file. Individual item IDs can be overwritten with mods. 51 for (unsigned int i = 0; i < mods->mod_list.size(); i++) { 52 53 test_path = PATH_DATA + "mods/" + mods->mod_list[i] + "/quests/index.txt"; 54 55 if (fileExists(test_path)) { 56 this->loadIndex(test_path); 57 } 58 } 59 60} 61 62/** 63 * Load all the quest files from the given index 64 * It simply contains a list of quest files 65 * Generally each quest arc has its own file 66 * 67 * @param filename The full path and filename to the [mod]/quests/index.txt file 68 */ 69void QuestLog::loadIndex(const std::string& filename) { 70 ifstream infile; 71 string line; 72 73 infile.open(filename.c_str(), ios::in); 74 75 if (infile.is_open()) { 76 while (!infile.eof()) { 77 line = getLine(infile); 78 if (line.length() > 0) { 79 80 // each line contains a quest file name 81 load(line); 82 } 83 } 84 infile.close(); 85 } 86} 87 88/** 89 * Load the quests in the specific quest file. 90 * Searches for the last-defined such file in all mods 91 * 92 * @param filename The quest file name and extension, no path 93 */ 94void QuestLog::load(const std::string& filename) { 95 96 FileParser infile; 97 int event_count = 0; 98 99 if (infile.open(mods->locate("quests/" + filename))) { 100 while (infile.next()) { 101 if (infile.new_section) { 102 if (infile.section == "quest") { 103 quest_count++; 104 event_count = 0; 105 } 106 } 107 108 quests[quest_count-1][event_count].type = infile.key; 109 quests[quest_count-1][event_count].s = msg->get(infile.val); 110 event_count++; 111 112 } 113 infile.close(); 114 } 115} 116 117void QuestLog::logic() { 118 if (camp->quest_update) { 119 resetQuestNotification = true; 120 camp->quest_update = false; 121 createQuestList(); 122 } 123} 124 125/** 126 * All active quests are placed in the Quest tab of the Log Menu 127 */ 128void QuestLog::createQuestList() { 129 log->clear(LOG_TYPE_QUESTS); 130 131 for (int i=0; i<quest_count; i++) { 132 for (int j=0; j<MAX_QUEST_EVENTS; j++) { 133 134 // check requirements 135 // break (skip to next dialog node) if any requirement fails 136 // if we reach an event that is not a requirement, succeed 137 138 if (quests[i][j].type == "requires_status") { 139 if (!camp->checkStatus(quests[i][j].s)) break; 140 } 141 else if (quests[i][j].type == "requires_not") { 142 if (camp->checkStatus(quests[i][j].s)) break; 143 } 144 else if (quests[i][j].type == "quest_text") { 145 log->add(quests[i][j].s, LOG_TYPE_QUESTS); 146 newQuestNotification = true; 147 break; 148 } 149 else if (quests[i][j].type == "") { 150 break; 151 } 152 } 153 } 154 155}