PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/CampaignManager.cpp

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