/src/CampaignManager.cpp

http://github.com/clintbellanger/flare · C++ · 172 lines · 101 code · 36 blank · 35 comment · 29 complexity · 2281c85e649e3bef1e28537be4a28f2b 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 CampaignManager
  15. *
  16. * Contains data for story mode
  17. */
  18. #include "CampaignManager.h"
  19. #include "SharedResources.h"
  20. #include <sstream>
  21. using namespace std;
  22. CampaignManager::CampaignManager() {
  23. drop_stack.item = 0;
  24. drop_stack.quantity = 0;
  25. items = NULL;
  26. carried_items = NULL;
  27. currency = NULL;
  28. xp = NULL;
  29. log_msg = "";
  30. quest_update = true;
  31. clearAll();
  32. }
  33. void CampaignManager::clearAll() {
  34. // clear campaign data
  35. for (int i=0; i<MAX_STATUS; i++) {
  36. status[i] = "";
  37. }
  38. status_count = 0;
  39. }
  40. /**
  41. * Take the savefile campaign= and convert to status array
  42. */
  43. void CampaignManager::setAll(std::string s) {
  44. string str = s + ',';
  45. string token;
  46. while (str != "" && status_count < MAX_STATUS) {
  47. token = eatFirstString(str, ',');
  48. if (token != "") this->setStatus(token);
  49. }
  50. quest_update = true;
  51. }
  52. /**
  53. * Convert status array to savefile campaign= (status csv)
  54. */
  55. std::string CampaignManager::getAll() {
  56. stringstream ss;
  57. ss.str("");
  58. for (int i=0; i<status_count; i++) {
  59. ss << status[i];
  60. if (i < status_count-1) ss << ',';
  61. }
  62. return ss.str();
  63. }
  64. bool CampaignManager::checkStatus(std::string s) {
  65. // avoid searching empty statuses
  66. if (s == "") return false;
  67. for (int i=0; i<status_count; i++) {
  68. if (status[i] == s) return true;
  69. }
  70. return false;
  71. }
  72. void CampaignManager::setStatus(std::string s) {
  73. // avoid adding empty statuses
  74. if (s == "") return;
  75. // hit upper limit for status
  76. // TODO: add a warning
  77. if (status_count >= MAX_STATUS) return;
  78. // if it's already set, don't add it again
  79. if (checkStatus(s)) return;
  80. status[status_count++] = s;
  81. quest_update = true;
  82. }
  83. void CampaignManager::unsetStatus(std::string s) {
  84. // avoid searching empty statuses
  85. if (s == "") return;
  86. for (int i=status_count-1; i>=0; i--) {
  87. if (status[i] == s) {
  88. // bubble existing statuses down
  89. for (int j=i; j<status_count-1; j++) {
  90. status[j] = status[j+1];
  91. }
  92. status_count--;
  93. quest_update = true;
  94. return;
  95. }
  96. }
  97. }
  98. bool CampaignManager::checkItem(int item_id) {
  99. return carried_items->contain(item_id);
  100. }
  101. void CampaignManager::removeItem(int item_id) {
  102. carried_items->remove(item_id);
  103. }
  104. void CampaignManager::rewardItem(ItemStack istack) {
  105. if (carried_items->full()) {
  106. drop_stack.item = istack.item;
  107. drop_stack.quantity = istack.quantity;
  108. }
  109. else {
  110. carried_items->add(istack);
  111. if (istack.quantity <= 1)
  112. addMsg(msg->get("You receive %s.", items->items[istack.item].name));
  113. if (istack.quantity > 1)
  114. addMsg(msg->get("You receive %s x%d.", istack.quantity, items->items[istack.item].name));
  115. items->playSound(istack.item);
  116. }
  117. }
  118. void CampaignManager::rewardCurrency(int amount) {
  119. *currency += amount;
  120. addMsg(msg->get("You receive %d gold.", amount));
  121. items->playCoinsSound();
  122. }
  123. void CampaignManager::rewardXP(int amount) {
  124. *xp += amount;
  125. addMsg(msg->get("You receive %d XP.", amount));
  126. }
  127. void CampaignManager::addMsg(const string& new_msg) {
  128. if (log_msg != "") log_msg += " ";
  129. log_msg += new_msg;
  130. }
  131. CampaignManager::~CampaignManager() {
  132. }