2011-01-14 22:16:43 +01:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
2011-12-14 08:40:48 +01:00
|
|
|
// Copyright (C) 2001-2008 Martiño Figueroa
|
2011-01-14 22:16:43 +01:00
|
|
|
//
|
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
#include "conversion.h"
|
|
|
|
#include "resource_type.h"
|
|
|
|
#include "checksum.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "util.h"
|
2012-03-13 00:08:22 +01:00
|
|
|
#include "tech_tree.h"
|
2011-01-14 22:16:43 +01:00
|
|
|
#include "leak_dumper.h"
|
|
|
|
|
|
|
|
using namespace Shared::Graphics;
|
|
|
|
using namespace Shared::Util;
|
|
|
|
|
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class Resource
|
|
|
|
// =====================================================
|
|
|
|
|
|
|
|
Resource::Resource() {
|
|
|
|
this->type= NULL;
|
|
|
|
this->amount= 0;
|
|
|
|
pos= Vec2i(0);
|
|
|
|
balance= 0;
|
|
|
|
|
|
|
|
addItemToVault(&this->amount,this->amount);
|
|
|
|
addItemToVault(&this->balance,this->balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::init(const ResourceType *rt, int amount) {
|
|
|
|
this->type= rt;
|
|
|
|
this->amount= amount;
|
|
|
|
pos= Vec2i(0);
|
|
|
|
balance= 0;
|
|
|
|
|
|
|
|
addItemToVault(&this->amount,this->amount);
|
|
|
|
addItemToVault(&this->balance,this->balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::init(const ResourceType *rt, const Vec2i &pos) {
|
|
|
|
this->type=rt;
|
|
|
|
amount=rt->getDefResPerPatch();
|
|
|
|
this->pos= pos;
|
|
|
|
|
|
|
|
addItemToVault(&this->amount,this->amount);
|
|
|
|
addItemToVault(&this->balance,this->balance);
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:55:48 +02:00
|
|
|
string Resource::getDescription(bool translatedValue) const {
|
2011-01-14 22:16:43 +01:00
|
|
|
string str;
|
|
|
|
|
2013-06-13 10:55:48 +02:00
|
|
|
str+= type->getName(translatedValue);
|
2011-01-14 22:16:43 +01:00
|
|
|
str+="\n";
|
|
|
|
str+= intToStr(amount);
|
|
|
|
str+="/";
|
|
|
|
str+= intToStr(type->getDefResPerPatch());
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Resource::getAmount() const {
|
|
|
|
checkItemInVault(&this->amount,this->amount);
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Resource::getBalance() const {
|
|
|
|
checkItemInVault(&this->balance,this->balance);
|
|
|
|
return balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::setAmount(int amount) {
|
|
|
|
checkItemInVault(&this->amount,this->amount);
|
|
|
|
this->amount= amount;
|
|
|
|
addItemToVault(&this->amount,this->amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::setBalance(int balance) {
|
|
|
|
checkItemInVault(&this->balance,this->balance);
|
|
|
|
this->balance= balance;
|
|
|
|
addItemToVault(&this->balance,this->balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::decAmount(int i) {
|
|
|
|
checkItemInVault(&this->amount,this->amount);
|
|
|
|
amount -= i;
|
|
|
|
addItemToVault(&this->amount,this->amount);
|
|
|
|
|
|
|
|
if(amount > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:27:25 +01:00
|
|
|
void Resource::saveGame(XmlNode *rootNode) const {
|
|
|
|
std::map<string,string> mapTagReplacements;
|
|
|
|
XmlNode *resourceNode = rootNode->addChild("Resource");
|
|
|
|
|
|
|
|
// int amount;
|
|
|
|
resourceNode->addAttribute("amount",intToStr(amount), mapTagReplacements);
|
|
|
|
// const ResourceType *type;
|
|
|
|
resourceNode->addAttribute("type",type->getName(), mapTagReplacements);
|
|
|
|
// Vec2i pos;
|
|
|
|
resourceNode->addAttribute("pos",pos.getString(), mapTagReplacements);
|
|
|
|
// int balance;
|
|
|
|
resourceNode->addAttribute("balance",intToStr(balance), mapTagReplacements);
|
|
|
|
}
|
|
|
|
|
2012-03-13 16:21:25 +01:00
|
|
|
void Resource::loadGame(const XmlNode *rootNode, int index,const TechTree *techTree) {
|
2012-03-13 00:08:22 +01:00
|
|
|
vector<XmlNode *> resourceNodeList = rootNode->getChildList("Resource");
|
|
|
|
|
2013-11-19 07:14:06 +01:00
|
|
|
if(index < (int)resourceNodeList.size()) {
|
2012-03-13 00:08:22 +01:00
|
|
|
XmlNode *resourceNode = resourceNodeList[index];
|
|
|
|
|
|
|
|
amount = resourceNode->getAttribute("amount")->getIntValue();
|
|
|
|
type = techTree->getResourceType(resourceNode->getAttribute("type")->getValue());
|
|
|
|
pos = Vec2i::strToVec2(resourceNode->getAttribute("pos")->getValue());
|
|
|
|
balance = resourceNode->getAttribute("balance")->getIntValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 02:43:14 +02:00
|
|
|
std::string Resource::toString() const {
|
|
|
|
std::string result = "resource name = " + this->getDescription(false) + "\n";
|
|
|
|
result += "amount = " + intToStr(this->amount) + "\n";
|
|
|
|
result += "type = " + this->type->getName(false) + "\n";
|
|
|
|
result += "type resources per patch = " + intToStr(type->getDefResPerPatch()) + "\n";
|
|
|
|
result += "pos = " + this->pos.getString() + "\n";
|
|
|
|
result += "balance = " + intToStr(this->balance) + "\n";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-12 05:33:43 +02:00
|
|
|
Checksum Resource::getCRC() {
|
|
|
|
Checksum crcForResource;
|
|
|
|
|
|
|
|
crcForResource.addInt(amount);
|
|
|
|
crcForResource.addString(type->getName(false));
|
|
|
|
crcForResource.addInt(pos.x);
|
|
|
|
crcForResource.addInt(pos.y);
|
|
|
|
crcForResource.addInt(balance);
|
|
|
|
|
|
|
|
return crcForResource;
|
|
|
|
}
|
|
|
|
|
2011-01-14 22:16:43 +01:00
|
|
|
}}//end namespace
|