/src/QuestLog.cpp

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