2011-01-20 16:56:30 +01:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
|
|
|
// Copyright (C) 2001-2008 Martio Figueroa
|
|
|
|
//
|
|
|
|
// 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 "tech_tree.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "resource.h"
|
|
|
|
#include "faction_type.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "xml_parser.h"
|
|
|
|
#include "platform_util.h"
|
|
|
|
#include "game_util.h"
|
2011-01-20 09:19:14 +01:00
|
|
|
#include "window.h"
|
2011-01-20 16:56:30 +01:00
|
|
|
#include "leak_dumper.h"
|
|
|
|
|
|
|
|
using namespace Shared::Util;
|
|
|
|
using namespace Shared::Xml;
|
|
|
|
|
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class TechTree
|
|
|
|
// =====================================================
|
2010-03-18 22:26:40 +01:00
|
|
|
|
2011-03-15 16:30:28 +01:00
|
|
|
Checksum TechTree::loadTech(const vector<string> pathList, const string &techName,
|
|
|
|
set<string> &factions, Checksum* checksum, std::map<string,int> &loadedFileList) {
|
2011-01-09 05:49:21 +01:00
|
|
|
Checksum techtreeChecksum;
|
2010-03-18 22:26:40 +01:00
|
|
|
for(int idx = 0; idx < pathList.size(); idx++) {
|
2011-03-13 09:23:43 +01:00
|
|
|
string currentPath = pathList[idx];
|
|
|
|
endPathWithSlash(currentPath);
|
|
|
|
|
|
|
|
string path = currentPath + techName;
|
2010-03-18 22:26:40 +01:00
|
|
|
if(isdir(path.c_str()) == true) {
|
2011-03-15 16:30:28 +01:00
|
|
|
load(path, factions, checksum, &techtreeChecksum, loadedFileList);
|
2010-03-18 22:26:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-09 05:49:21 +01:00
|
|
|
return techtreeChecksum;
|
2010-03-18 22:26:40 +01:00
|
|
|
}
|
2011-01-20 16:56:30 +01:00
|
|
|
|
2011-03-15 16:30:28 +01:00
|
|
|
void TechTree::load(const string &dir, set<string> &factions, Checksum* checksum,
|
|
|
|
Checksum *techtreeChecksum, std::map<string,int> &loadedFileList) {
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-01-20 16:56:30 +01:00
|
|
|
|
2011-03-15 16:30:28 +01:00
|
|
|
string currentPath = dir;
|
|
|
|
endPathWithSlash(currentPath);
|
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
vector<string> filenames;
|
2011-03-15 16:30:28 +01:00
|
|
|
string name= lastDir(currentPath);
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
Logger::getInstance().add("TechTree: "+ formatString(name), true);
|
|
|
|
|
|
|
|
//load resources
|
2011-03-15 16:30:28 +01:00
|
|
|
string str= currentPath + "resources/*.";
|
2011-01-20 16:56:30 +01:00
|
|
|
|
2011-03-15 16:30:28 +01:00
|
|
|
try {
|
2011-01-20 16:56:30 +01:00
|
|
|
findAll(str, filenames);
|
|
|
|
resourceTypes.resize(filenames.size());
|
|
|
|
|
|
|
|
for(int i=0; i<filenames.size(); ++i){
|
2011-03-15 16:30:28 +01:00
|
|
|
str= currentPath + "resources/" + filenames[i];
|
|
|
|
resourceTypes[i].load(str, checksum, &checksumValue, loadedFileList);
|
2011-01-20 16:56:30 +01:00
|
|
|
Window::handleEvent();
|
|
|
|
SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup pixmap memory
|
2011-03-15 16:30:28 +01:00
|
|
|
for(int i = 0; i < filenames.size(); ++i) {
|
2011-01-20 16:56:30 +01:00
|
|
|
resourceTypes[i].deletePixels();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const exception &e){
|
|
|
|
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what());
|
2011-03-15 16:30:28 +01:00
|
|
|
throw runtime_error("Error loading Resource Types in: [" + currentPath + "]\n" + e.what());
|
2011-01-20 16:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// give CPU time to update other things to avoid apperance of hanging
|
|
|
|
sleep(0);
|
|
|
|
Window::handleEvent();
|
|
|
|
SDL_PumpEvents();
|
|
|
|
|
|
|
|
//load tech tree xml info
|
2011-03-15 16:30:28 +01:00
|
|
|
try {
|
2011-01-20 16:56:30 +01:00
|
|
|
XmlTree xmlTree;
|
2011-03-13 09:23:43 +01:00
|
|
|
string currentPath = dir;
|
|
|
|
endPathWithSlash(currentPath);
|
|
|
|
string path = currentPath + lastDir(dir) + ".xml";
|
2011-01-20 16:56:30 +01:00
|
|
|
|
2011-01-09 05:49:21 +01:00
|
|
|
checksum->addFile(path);
|
2011-01-20 16:56:30 +01:00
|
|
|
checksumValue.addFile(path);
|
|
|
|
|
|
|
|
xmlTree.load(path);
|
2011-03-15 16:30:28 +01:00
|
|
|
loadedFileList[path]++;
|
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
const XmlNode *techTreeNode= xmlTree.getRootNode();
|
|
|
|
|
|
|
|
//attack types
|
|
|
|
const XmlNode *attackTypesNode= techTreeNode->getChild("attack-types");
|
|
|
|
attackTypes.resize(attackTypesNode->getChildCount());
|
2011-03-15 16:30:28 +01:00
|
|
|
for(int i = 0; i < attackTypes.size(); ++i) {
|
2011-01-20 16:56:30 +01:00
|
|
|
const XmlNode *attackTypeNode= attackTypesNode->getChild("attack-type", i);
|
|
|
|
attackTypes[i].setName(attackTypeNode->getAttribute("name")->getRestrictedValue());
|
|
|
|
attackTypes[i].setId(i);
|
2011-03-15 16:30:28 +01:00
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
Window::handleEvent();
|
|
|
|
SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// give CPU time to update other things to avoid apperance of hanging
|
|
|
|
sleep(0);
|
|
|
|
//SDL_PumpEvents();
|
|
|
|
|
|
|
|
//armor types
|
|
|
|
const XmlNode *armorTypesNode= techTreeNode->getChild("armor-types");
|
|
|
|
armorTypes.resize(armorTypesNode->getChildCount());
|
2011-03-15 16:30:28 +01:00
|
|
|
for(int i = 0; i < armorTypes.size(); ++i) {
|
2011-01-20 16:56:30 +01:00
|
|
|
const XmlNode *armorTypeNode= armorTypesNode->getChild("armor-type", i);
|
|
|
|
armorTypes[i].setName(armorTypeNode->getAttribute("name")->getRestrictedValue());
|
|
|
|
armorTypes[i].setId(i);
|
2011-03-15 16:30:28 +01:00
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
Window::handleEvent();
|
|
|
|
SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
//damage multipliers
|
|
|
|
damageMultiplierTable.init(attackTypes.size(), armorTypes.size());
|
|
|
|
const XmlNode *damageMultipliersNode= techTreeNode->getChild("damage-multipliers");
|
2011-03-15 16:30:28 +01:00
|
|
|
for(int i = 0; i < damageMultipliersNode->getChildCount(); ++i) {
|
2011-01-20 16:56:30 +01:00
|
|
|
const XmlNode *damageMultiplierNode= damageMultipliersNode->getChild("damage-multiplier", i);
|
|
|
|
const AttackType *attackType= getAttackType(damageMultiplierNode->getAttribute("attack")->getRestrictedValue());
|
|
|
|
const ArmorType *armorType= getArmorType(damageMultiplierNode->getAttribute("armor")->getRestrictedValue());
|
|
|
|
float multiplier= damageMultiplierNode->getAttribute("value")->getFloatValue();
|
|
|
|
damageMultiplierTable.setDamageMultiplier(attackType, armorType, multiplier);
|
2011-03-15 16:30:28 +01:00
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
Window::handleEvent();
|
|
|
|
SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const exception &e){
|
|
|
|
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what());
|
2011-03-15 16:30:28 +01:00
|
|
|
throw runtime_error("Error loading Tech Tree: "+ currentPath + "\n" + e.what());
|
2011-01-20 16:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// give CPU time to update other things to avoid apperance of hanging
|
|
|
|
sleep(0);
|
|
|
|
//SDL_PumpEvents();
|
|
|
|
|
|
|
|
//load factions
|
2011-03-15 16:30:28 +01:00
|
|
|
str = currentPath + "factions/*.";
|
2011-01-20 16:56:30 +01:00
|
|
|
try{
|
|
|
|
factionTypes.resize(factions.size());
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
for ( set<string>::iterator it = factions.begin(); it != factions.end(); ++it ) {
|
|
|
|
string factionName = *it;
|
|
|
|
|
|
|
|
char szBuf[1024]="";
|
2011-03-15 16:30:28 +01:00
|
|
|
sprintf(szBuf,"%s %s [%d / %d] - %s",Lang::getInstance().get("Loading").c_str(),
|
|
|
|
Lang::getInstance().get("Faction").c_str(),
|
|
|
|
i+1,
|
|
|
|
(int)factions.size(),
|
|
|
|
factionName.c_str());
|
2011-01-20 16:56:30 +01:00
|
|
|
Logger &logger= Logger::getInstance();
|
2011-01-09 11:03:33 +01:00
|
|
|
logger.setState(szBuf);
|
2011-01-20 16:56:30 +01:00
|
|
|
logger.setProgress((int)((((double)i) / (double)factions.size()) * 100.0));
|
|
|
|
|
2011-03-15 16:30:28 +01:00
|
|
|
str = currentPath + "factions/" + factionName;
|
|
|
|
factionTypes[i++].load(str, this, checksum,&checksumValue,loadedFileList);
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
// give CPU time to update other things to avoid apperance of hanging
|
|
|
|
sleep(0);
|
|
|
|
Window::handleEvent();
|
|
|
|
SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const exception &e){
|
|
|
|
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what());
|
2011-03-15 16:30:28 +01:00
|
|
|
throw runtime_error("Error loading Faction Types: "+ currentPath + "\n" + e.what());
|
2011-01-09 05:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(techtreeChecksum != NULL) {
|
|
|
|
*techtreeChecksum = checksumValue;
|
|
|
|
}
|
2011-01-20 16:56:30 +01:00
|
|
|
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-01-20 16:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TechTree::~TechTree() {
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-01-20 16:56:30 +01:00
|
|
|
Logger::getInstance().add("Tech tree", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> TechTree::validateFactionTypes() {
|
|
|
|
std::vector<std::string> results;
|
|
|
|
for (int i = 0; i < factionTypes.size(); ++i) {
|
|
|
|
std::vector<std::string> factionResults = factionTypes[i].validateFactionType();
|
2011-04-06 20:03:22 +02:00
|
|
|
if(factionResults.size() > 0) {
|
|
|
|
results.insert(results.end(), factionResults.begin(), factionResults.end());
|
|
|
|
}
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
factionResults = factionTypes[i].validateFactionTypeUpgradeTypes();
|
2011-04-06 20:03:22 +02:00
|
|
|
if(factionResults.size() > 0) {
|
|
|
|
results.insert(results.end(), factionResults.begin(), factionResults.end());
|
|
|
|
}
|
2011-01-20 16:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> TechTree::validateResourceTypes() {
|
|
|
|
std::vector<std::string> results;
|
2011-04-06 19:50:20 +02:00
|
|
|
ResourceTypes resourceTypesNotUsed = resourceTypes;
|
|
|
|
for (unsigned int i = 0; i < factionTypes.size(); ++i) {
|
|
|
|
//printf("Validating [%d / %d] faction [%s]\n",i,(int)factionTypes.size(),factionTypes[i].getName().c_str());
|
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
std::vector<std::string> factionResults = factionTypes[i].validateFactionTypeResourceTypes(resourceTypes);
|
2011-04-06 19:50:20 +02:00
|
|
|
if(factionResults.size() > 0) {
|
|
|
|
results.insert(results.end(), factionResults.begin(), factionResults.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the faction uses the resources in this techtree
|
|
|
|
// Now lets find a matching faction resource type for the unit
|
|
|
|
for(int j = resourceTypesNotUsed.size() -1; j >= 0; --j) {
|
|
|
|
const ResourceType &rt = resourceTypesNotUsed[j];
|
|
|
|
//printf("Validating [%d / %d] resourcetype [%s]\n",j,(int)resourceTypesNotUsed.size(),rt.getName().c_str());
|
|
|
|
|
|
|
|
if(factionTypes[i].factionUsesResourceType(&rt) == true) {
|
|
|
|
//printf("FOUND FACTION CONSUMER FOR RESOURCE - [%d / %d] faction [%s] resource [%d / %d] resourcetype [%s]\n",i,(int)factionTypes.size(),factionTypes[i].getName().c_str(),j,(int)resourceTypesNotUsed.size(),rt.getName().c_str());
|
|
|
|
resourceTypesNotUsed.erase(resourceTypesNotUsed.begin() + j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resourceTypesNotUsed.size() > 0) {
|
|
|
|
//printf("FOUND unused resource Types [%d]\n",(int)resourceTypesNotUsed.size());
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < resourceTypesNotUsed.size(); ++i) {
|
|
|
|
const ResourceType &rt = resourceTypesNotUsed[i];
|
|
|
|
char szBuf[4096]="";
|
|
|
|
sprintf(szBuf,"The Resource type [%s] is not used by any units in this techtree!",rt.getName().c_str());
|
|
|
|
results.push_back(szBuf);
|
|
|
|
}
|
2011-01-20 16:56:30 +01:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==================== get ====================
|
|
|
|
|
|
|
|
FactionType *TechTree::getTypeByName(const string &name) {
|
|
|
|
for(int i=0; i < factionTypes.size(); ++i) {
|
|
|
|
if(factionTypes[i].getName() == name) {
|
|
|
|
return &factionTypes[i];
|
|
|
|
}
|
|
|
|
}
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-01-20 16:56:30 +01:00
|
|
|
throw runtime_error("Faction not found: "+name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const FactionType *TechTree::getType(const string &name) const {
|
|
|
|
for(int i=0; i < factionTypes.size(); ++i) {
|
|
|
|
if(factionTypes[i].getName() == name) {
|
|
|
|
return &factionTypes[i];
|
|
|
|
}
|
|
|
|
}
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-01-20 16:56:30 +01:00
|
|
|
throw runtime_error("Faction not found: "+name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ResourceType *TechTree::getTechResourceType(int i) const{
|
|
|
|
for(int j=0; j < getResourceTypeCount(); ++j){
|
|
|
|
const ResourceType *rt= getResourceType(j);
|
|
|
|
assert(rt != NULL);
|
|
|
|
if(rt == NULL) {
|
|
|
|
throw runtime_error("rt == NULL");
|
|
|
|
}
|
|
|
|
if(rt->getResourceNumber() == i && rt->getClass() == rcTech)
|
|
|
|
return getResourceType(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getFirstTechResourceType();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ResourceType *TechTree::getFirstTechResourceType() const{
|
|
|
|
for(int i=0; i<getResourceTypeCount(); ++i){
|
|
|
|
const ResourceType *rt= getResourceType(i);
|
|
|
|
assert(rt != NULL);
|
|
|
|
if(rt->getResourceNumber()==1 && rt->getClass()==rcTech)
|
|
|
|
return getResourceType(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw runtime_error("This tech tree has not tech resources, one at least is required");
|
|
|
|
}
|
|
|
|
|
|
|
|
const ResourceType *TechTree::getResourceType(const string &name) const{
|
|
|
|
|
|
|
|
for(int i=0; i<resourceTypes.size(); ++i){
|
|
|
|
if(resourceTypes[i].getName()==name){
|
|
|
|
return &resourceTypes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw runtime_error("Resource Type not found: "+name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ArmorType *TechTree::getArmorType(const string &name) const{
|
|
|
|
for(int i=0; i<armorTypes.size(); ++i){
|
|
|
|
if(armorTypes[i].getName()==name){
|
|
|
|
return &armorTypes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw runtime_error("Armor Type not found: "+name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const AttackType *TechTree::getAttackType(const string &name) const{
|
|
|
|
for(int i=0; i<attackTypes.size(); ++i){
|
|
|
|
if(attackTypes[i].getName()==name){
|
|
|
|
return &attackTypes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw runtime_error("Attack Type not found: "+name);
|
|
|
|
}
|
|
|
|
|
|
|
|
float TechTree::getDamageMultiplier(const AttackType *att, const ArmorType *art) const{
|
|
|
|
return damageMultiplierTable.getDamageMultiplier(att, art);
|
|
|
|
}
|
|
|
|
|
|
|
|
}}//end namespace
|