diff --git a/source/g3d_viewer/main.cpp b/source/g3d_viewer/main.cpp index 5a47ce3aa..0fa79042c 100644 --- a/source/g3d_viewer/main.cpp +++ b/source/g3d_viewer/main.cpp @@ -252,7 +252,7 @@ END_EVENT_TABLE() // ===================================================== GlCanvas::GlCanvas(MainWindow * mainWindow): - wxGLCanvas(mainWindow, -1) + wxGLCanvas(mainWindow, -1, wxDefaultPosition) { this->mainWindow = mainWindow; } diff --git a/source/glest_game/types/faction_type.cpp b/source/glest_game/types/faction_type.cpp index e4ea8a103..f1d830833 100644 --- a/source/glest_game/types/faction_type.cpp +++ b/source/glest_game/types/faction_type.cpp @@ -1,156 +1,166 @@ -// ============================================================== -// This file is part of Glest (www.glest.org) -// -// Copyright (C) 2001-2008 Martiņo 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 "faction_type.h" - -#include "logger.h" -#include "util.h" -#include "xml_parser.h" -#include "tech_tree.h" -#include "resource.h" -#include "platform_util.h" -#include "game_util.h" -#include "leak_dumper.h" - -using namespace Shared::Util; -using namespace Shared::Xml; - -namespace Glest{ namespace Game{ - -// ====================================================== -// Class FactionType -// ====================================================== - -FactionType::FactionType(){ - music= NULL; -} - -//load a faction, given a directory -void FactionType::load(const string &dir, const TechTree *techTree, Checksum* checksum){ - - name= lastDir(dir); - - Logger::getInstance().add("Faction type: "+ formatString(name), true); - - // a1) preload units - string unitsPath= dir + "/units/*."; - vector unitFilenames; - findAll(unitsPath, unitFilenames); - unitTypes.resize(unitFilenames.size()); - for(int i=0; i upgradeFilenames; - findAll(upgradesPath, upgradeFilenames); - upgradeTypes.resize(upgradeFilenames.size()); - for(int i=0; iaddFile(path); - - XmlTree xmlTree; - xmlTree.load(path); - const XmlNode *factionNode= xmlTree.getRootNode(); - - //read starting resources - const XmlNode *startingResourcesNode= factionNode->getChild("starting-resources"); - - startingResources.resize(startingResourcesNode->getChildCount()); - for(int i=0; igetChild("resource", i); - string name= resourceNode->getAttribute("name")->getRestrictedValue(); - int amount= resourceNode->getAttribute("amount")->getIntValue(); - startingResources[i].init(techTree->getResourceType(name), amount); - } - - //read starting units - const XmlNode *startingUnitsNode= factionNode->getChild("starting-units"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *unitNode= startingUnitsNode->getChild("unit", i); - string name= unitNode->getAttribute("name")->getRestrictedValue(); - int amount= unitNode->getAttribute("amount")->getIntValue(); - startingUnits.push_back(PairPUnitTypeInt(getUnitType(name), amount)); - } - - //read music - const XmlNode *musicNode= factionNode->getChild("music"); - bool value= musicNode->getAttribute("value")->getBoolValue(); - if(value){ - music= new StrSound(); - music->open(dir+"/"+musicNode->getAttribute("path")->getRestrictedValue()); - } -} - -FactionType::~FactionType(){ - delete music; -} - -// ==================== get ==================== - -const UnitType *FactionType::getUnitType(const string &name) const{ - for(int i=0; i unitFilenames; + findAll(unitsPath, unitFilenames); + unitTypes.resize(unitFilenames.size()); + for(int i=0; i upgradeFilenames; + findAll(upgradesPath, upgradeFilenames); + upgradeTypes.resize(upgradeFilenames.size()); + for(int i=0; iaddFile(path); + + XmlTree xmlTree; + xmlTree.load(path); + const XmlNode *factionNode= xmlTree.getRootNode(); + + //read starting resources + const XmlNode *startingResourcesNode= factionNode->getChild("starting-resources"); + + startingResources.resize(startingResourcesNode->getChildCount()); + for(int i=0; igetChild("resource", i); + string name= resourceNode->getAttribute("name")->getRestrictedValue(); + int amount= resourceNode->getAttribute("amount")->getIntValue(); + startingResources[i].init(techTree->getResourceType(name), amount); + } + + //read starting units + const XmlNode *startingUnitsNode= factionNode->getChild("starting-units"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *unitNode= startingUnitsNode->getChild("unit", i); + string name= unitNode->getAttribute("name")->getRestrictedValue(); + int amount= unitNode->getAttribute("amount")->getIntValue(); + startingUnits.push_back(PairPUnitTypeInt(getUnitType(name), amount)); + } + + //read music + const XmlNode *musicNode= factionNode->getChild("music"); + bool value= musicNode->getAttribute("value")->getBoolValue(); + if(value){ + music= new StrSound(); + music->open(dir+"/"+musicNode->getAttribute("path")->getRestrictedValue()); + } +} + +FactionType::~FactionType(){ + delete music; +} + +// ==================== get ==================== + +const UnitType *FactionType::getUnitType(const string &name) const{ + for(int i=0; iaddFile(path); - - //tree - XmlTree xmlTree; - xmlTree.load(path); - const XmlNode *resourceNode= xmlTree.getRootNode(); - - //image - const XmlNode *imageNode= resourceNode->getChild("image"); - image= renderer.newTexture2D(rsGame); - image->load(dir+"/"+imageNode->getAttribute("path")->getRestrictedValue()); - - //type - const XmlNode *typeNode= resourceNode->getChild("type"); - resourceClass= strToRc(typeNode->getAttribute("value")->getRestrictedValue()); - - switch(resourceClass) - { - case rcTech: - { - //model - const XmlNode *modelNode= typeNode->getChild("model"); - string path=dir+"/" + modelNode->getAttribute("path")->getRestrictedValue(); - - model= renderer.newModel(rsGame); - model->load(path); - - //default resources - const XmlNode *defaultAmountNode= typeNode->getChild("default-amount"); - defResPerPatch= defaultAmountNode->getAttribute("value")->getIntValue(); - - //resource number - const XmlNode *resourceNumberNode= typeNode->getChild("resource-number"); - resourceNumber= resourceNumberNode->getAttribute("value")->getIntValue(); - - } - break; - - case rcTileset: - { - //resource number - const XmlNode *defaultAmountNode= typeNode->getChild("default-amount"); - defResPerPatch= defaultAmountNode->getAttribute("value")->getIntValue(); - - //resource number - const XmlNode *tilesetObjectNode= typeNode->getChild("tileset-object"); - tilesetObject= tilesetObjectNode->getAttribute("value")->getIntValue(); - } - break; - - case rcConsumable: - { - //interval - const XmlNode *intervalNode= typeNode->getChild("interval"); - interval= intervalNode->getAttribute("value")->getIntValue(); - } - break; - - case rcStatic: - { - //recoup_cost - if(typeNode->hasChild("recoup_cost") == true) - { - const XmlNode *recoup_costNode= typeNode->getChild("recoup_cost"); - if(recoup_costNode != NULL) - { - recoup_cost= recoup_costNode->getAttribute("value")->getBoolValue(); - } - } - } - break; - - default: - break; - } - } - catch(const exception &e){ - throw runtime_error("Error loading resource type: " + path + "\n" + e.what()); - } -} - - -// ==================== misc ==================== - -ResourceClass ResourceType::strToRc(const string &s){ - if(s=="tech"){ - return rcTech; - } - if(s=="tileset"){ - return rcTileset; - } - if(s=="static"){ - return rcStatic; - } - if(s=="consumable"){ - return rcConsumable; - } - throw runtime_error("Error converting from string ro resourceClass, found: " + s); -} - -}}//end namespace +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo 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 "resource_type.h" + +#include "util.h" +#include "element_type.h" +#include "logger.h" +#include "renderer.h" +#include "xml_parser.h" +#include "game_util.h" +#include "leak_dumper.h" + +using namespace Shared::Util; +using namespace Shared::Xml; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class ResourceType +// ===================================================== + +void ResourceType::load(const string &dir, Checksum* checksum){ + + string path, str; + Renderer &renderer= Renderer::getInstance(); + + try + { + recoup_cost = true; + + name= lastDir(dir); + + Logger::getInstance().add("Resource type: "+ formatString(name), true); + path= dir+"/"+name+".xml"; + +#ifdef _WINDOWS + + DWORD fileAttributes = GetFileAttributes(path.c_str()); + if( (fileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) + { + return; + } + +#endif + + checksum->addFile(path); + + //tree + XmlTree xmlTree; + xmlTree.load(path); + const XmlNode *resourceNode= xmlTree.getRootNode(); + + //image + const XmlNode *imageNode= resourceNode->getChild("image"); + image= renderer.newTexture2D(rsGame); + image->load(dir+"/"+imageNode->getAttribute("path")->getRestrictedValue()); + + //type + const XmlNode *typeNode= resourceNode->getChild("type"); + resourceClass= strToRc(typeNode->getAttribute("value")->getRestrictedValue()); + + switch(resourceClass) + { + case rcTech: + { + //model + const XmlNode *modelNode= typeNode->getChild("model"); + string path=dir+"/" + modelNode->getAttribute("path")->getRestrictedValue(); + + model= renderer.newModel(rsGame); + model->load(path); + + //default resources + const XmlNode *defaultAmountNode= typeNode->getChild("default-amount"); + defResPerPatch= defaultAmountNode->getAttribute("value")->getIntValue(); + + //resource number + const XmlNode *resourceNumberNode= typeNode->getChild("resource-number"); + resourceNumber= resourceNumberNode->getAttribute("value")->getIntValue(); + + } + break; + + case rcTileset: + { + //resource number + const XmlNode *defaultAmountNode= typeNode->getChild("default-amount"); + defResPerPatch= defaultAmountNode->getAttribute("value")->getIntValue(); + + //resource number + const XmlNode *tilesetObjectNode= typeNode->getChild("tileset-object"); + tilesetObject= tilesetObjectNode->getAttribute("value")->getIntValue(); + } + break; + + case rcConsumable: + { + //interval + const XmlNode *intervalNode= typeNode->getChild("interval"); + interval= intervalNode->getAttribute("value")->getIntValue(); + } + break; + + case rcStatic: + { + //recoup_cost + if(typeNode->hasChild("recoup_cost") == true) + { + const XmlNode *recoup_costNode= typeNode->getChild("recoup_cost"); + if(recoup_costNode != NULL) + { + recoup_cost= recoup_costNode->getAttribute("value")->getBoolValue(); + } + } + } + break; + + default: + break; + } + } + catch(const exception &e){ + throw runtime_error("Error loading resource type: " + path + "\n" + e.what()); + } +} + + +// ==================== misc ==================== + +ResourceClass ResourceType::strToRc(const string &s){ + if(s=="tech"){ + return rcTech; + } + if(s=="tileset"){ + return rcTileset; + } + if(s=="static"){ + return rcStatic; + } + if(s=="consumable"){ + return rcConsumable; + } + throw runtime_error("Error converting from string ro resourceClass, found: " + s); +} + +}}//end namespace diff --git a/source/glest_game/types/tech_tree.cpp b/source/glest_game/types/tech_tree.cpp index 1054777e4..5fcad4d01 100644 --- a/source/glest_game/types/tech_tree.cpp +++ b/source/glest_game/types/tech_tree.cpp @@ -1,190 +1,204 @@ -// ============================================================== -// This file is part of Glest (www.glest.org) -// -// Copyright (C) 2001-2008 Martiņo 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 - -#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" -#include "leak_dumper.h" - -using namespace Shared::Util; -using namespace Shared::Xml; - -namespace Glest{ namespace Game{ - -// ===================================================== -// class TechTree -// ===================================================== - -void TechTree::load(const string &dir, set &factions, Checksum* checksum){ - - string str; - vector filenames; - string name= lastDir(dir); - - Logger::getInstance().add("TechTree: "+ formatString(name), true); - - //load resources - str= dir+"/resources/*."; - - try{ - findAll(str, filenames); - resourceTypes.resize(filenames.size()); - - for(int i=0; iaddFile(path); - - xmlTree.load(path); - const XmlNode *techTreeNode= xmlTree.getRootNode(); - - //attack types - const XmlNode *attackTypesNode= techTreeNode->getChild("attack-types"); - attackTypes.resize(attackTypesNode->getChildCount()); - for(int i=0; igetChild("attack-type", i); - attackTypes[i].setName(attackTypeNode->getAttribute("name")->getRestrictedValue()); - attackTypes[i].setId(i); - } - - //armor types - const XmlNode *armorTypesNode= techTreeNode->getChild("armor-types"); - armorTypes.resize(armorTypesNode->getChildCount()); - for(int i=0; igetChild("armor-type", i); - armorTypes[i].setName(armorTypeNode->getAttribute("name")->getRestrictedValue()); - armorTypes[i].setId(i); - } - - //damage multipliers - damageMultiplierTable.init(attackTypes.size(), armorTypes.size()); - const XmlNode *damageMultipliersNode= techTreeNode->getChild("damage-multipliers"); - for(int i=0; igetChildCount(); ++i){ - 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); - } - } - catch(const exception &e){ - throw runtime_error("Error loading Tech Tree: "+ dir + "\n" + e.what()); - } - - //load factions - str= dir+"/factions/*."; - try{ - factionTypes.resize(factions.size()); - - int i=0; - for ( set::iterator it = factions.begin(); it != factions.end(); ++it ) { - str=dir+"/factions/" + *it; - factionTypes[i++].load(str, this, checksum); - } - } - catch(const exception &e){ - throw runtime_error("Error loading Faction Types: "+ dir + "\n" + e.what()); - } - -} - -TechTree::~TechTree(){ - Logger::getInstance().add("Tech tree", true); -} - - -// ==================== get ==================== - -const FactionType *TechTree::getType(const string &name) const{ - for(int i=0; igetResourceNumber()==i && rt->getClass()==rcTech) - return getResourceType(j); - } - - return getFirstTechResourceType(); -} - -const ResourceType *TechTree::getFirstTechResourceType() const{ - for(int i=0; igetResourceNumber()==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 + +#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" +#include "leak_dumper.h" + +using namespace Shared::Util; +using namespace Shared::Xml; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class TechTree +// ===================================================== + +void TechTree::load(const string &dir, set &factions, Checksum* checksum){ + + string str; + vector filenames; + string name= lastDir(dir); + + Logger::getInstance().add("TechTree: "+ formatString(name), true); + + //load resources + str= dir+"/resources/*."; + + try{ + findAll(str, filenames); + resourceTypes.resize(filenames.size()); + + for(int i=0; iaddFile(path); + + xmlTree.load(path); + const XmlNode *techTreeNode= xmlTree.getRootNode(); + + //attack types + const XmlNode *attackTypesNode= techTreeNode->getChild("attack-types"); + attackTypes.resize(attackTypesNode->getChildCount()); + for(int i=0; igetChild("attack-type", i); + attackTypes[i].setName(attackTypeNode->getAttribute("name")->getRestrictedValue()); + attackTypes[i].setId(i); + } + + //armor types + const XmlNode *armorTypesNode= techTreeNode->getChild("armor-types"); + armorTypes.resize(armorTypesNode->getChildCount()); + for(int i=0; igetChild("armor-type", i); + armorTypes[i].setName(armorTypeNode->getAttribute("name")->getRestrictedValue()); + armorTypes[i].setId(i); + } + + //damage multipliers + damageMultiplierTable.init(attackTypes.size(), armorTypes.size()); + const XmlNode *damageMultipliersNode= techTreeNode->getChild("damage-multipliers"); + for(int i=0; igetChildCount(); ++i){ + 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); + } + } + } + catch(const exception &e){ + throw runtime_error("Error loading Tech Tree: "+ dir + "\n" + e.what()); + } + + //load factions + str= dir+"/factions/*."; + try{ + factionTypes.resize(factions.size()); + + int i=0; + for ( set::iterator it = factions.begin(); it != factions.end(); ++it ) { + str=dir+"/factions/" + *it; + factionTypes[i++].load(str, this, checksum); + } + } + catch(const exception &e){ + throw runtime_error("Error loading Faction Types: "+ dir + "\n" + e.what()); + } + +} + +TechTree::~TechTree(){ + Logger::getInstance().add("Tech tree", true); +} + + +// ==================== get ==================== + +const FactionType *TechTree::getType(const string &name) const{ + for(int i=0; igetResourceNumber()==i && rt->getClass()==rcTech) + return getResourceType(j); + } + + return getFirstTechResourceType(); +} + +const ResourceType *TechTree::getFirstTechResourceType() const{ + for(int i=0; igetResourceNumber()==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 - -#include "util.h" -#include "upgrade_type.h" -#include "resource_type.h" -#include "sound.h" -#include "logger.h" -#include "xml_parser.h" -#include "tech_tree.h" -#include "resource.h" -#include "renderer.h" -#include "game_util.h" -#include "leak_dumper.h" - -using namespace Shared::Xml; -using namespace Shared::Graphics; -using namespace Shared::Util; - -namespace Glest{ namespace Game{ - -// =============================== -// class Level -// =============================== - -void Level::init(string name, int kills){ - this->name= name; - this->kills= kills; -} - -// ===================================================== -// class UnitType -// ===================================================== - -// ===================== PUBLIC ======================== - -const char *UnitType::propertyNames[]= {"burnable", "rotated_climb"}; - -// ==================== creation and loading ==================== - -UnitType::UnitType(){ - - lightColor= Vec3f(0.f); - light= false; - multiSelect= false; - armorType= NULL; - - for(int i=0; iid= id; - string path; - - try{ - - Logger::getInstance().add("Unit type: " + formatString(name), true); - - //file load - path= dir+"/"+name+".xml"; - - checksum->addFile(path); - - XmlTree xmlTree; - xmlTree.load(path); - const XmlNode *unitNode= xmlTree.getRootNode(); - - const XmlNode *parametersNode= unitNode->getChild("parameters"); - - //size - size= parametersNode->getChild("size")->getAttribute("value")->getIntValue(); - - //height - height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); - - //maxHp - maxHp= parametersNode->getChild("max-hp")->getAttribute("value")->getIntValue(); - - //hpRegeneration - hpRegeneration= parametersNode->getChild("max-hp")->getAttribute("regeneration")->getIntValue(); - - //maxEp - maxEp= parametersNode->getChild("max-ep")->getAttribute("value")->getIntValue(); - - if(maxEp!=0){ - //wpRegeneration - epRegeneration= parametersNode->getChild("max-ep")->getAttribute("regeneration")->getIntValue(); - } - - //armor - armor= parametersNode->getChild("armor")->getAttribute("value")->getIntValue(); - - //armor type string - string armorTypeName= parametersNode->getChild("armor-type")->getAttribute("value")->getRestrictedValue(); - armorType= techTree->getArmorType(armorTypeName); - - //sight - sight= parametersNode->getChild("sight")->getAttribute("value")->getIntValue(); - - //prod time - productionTime= parametersNode->getChild("time")->getAttribute("value")->getIntValue(); - - //multi selection - multiSelect= parametersNode->getChild("multi-selection")->getAttribute("value")->getBoolValue(); - - //cellmap - const XmlNode *cellMapNode= parametersNode->getChild("cellmap"); - bool hasCellMap= cellMapNode->getAttribute("value")->getBoolValue(); - if(hasCellMap){ - cellMap= new bool[size*size]; - for(int i=0; igetChild("row", i); - string row= rowNode->getAttribute("value")->getRestrictedValue(); - if(row.size()!=size){ - throw runtime_error("Cellmap row has not the same length as unit size"); - } - for(int j=0; jgetChild("levels"); - levels.resize(levelsNode->getChildCount()); - for(int i=0; igetChild("level", i); - levels[i].init( - levelNode->getAttribute("name")->getRestrictedValue(), - levelNode->getAttribute("kills")->getIntValue()); - } - - //fields - const XmlNode *fieldsNode= parametersNode->getChild("fields"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *fieldNode= fieldsNode->getChild("field", i); - string fieldName= fieldNode->getAttribute("value")->getRestrictedValue(); - if(fieldName=="land"){ - fields[fLand]= true; - } - else if(fieldName=="air"){ - fields[fAir]= true; - } - else{ - throw runtime_error("Not a valid field: "+fieldName+": "+ path); - } - } - - //properties - const XmlNode *propertiesNode= parametersNode->getChild("properties"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *propertyNode= propertiesNode->getChild("property", i); - string propertyName= propertyNode->getAttribute("value")->getRestrictedValue(); - bool found= false; - for(int i=0; igetChild("light"); - light= lightNode->getAttribute("enabled")->getBoolValue(); - if(light){ - lightColor.x= lightNode->getAttribute("red")->getFloatValue(0.f, 1.f); - lightColor.y= lightNode->getAttribute("green")->getFloatValue(0.f, 1.f); - lightColor.z= lightNode->getAttribute("blue")->getFloatValue(0.f, 1.f); - } - - //unit requirements - const XmlNode *unitRequirementsNode= parametersNode->getChild("unit-requirements"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *unitNode= unitRequirementsNode->getChild("unit", i); - string name= unitNode->getAttribute("name")->getRestrictedValue(); - unitReqs.push_back(factionType->getUnitType(name)); - } - - //upgrade requirements - const XmlNode *upgradeRequirementsNode= parametersNode->getChild("upgrade-requirements"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *upgradeReqNode= upgradeRequirementsNode->getChild("upgrade", i); - string name= upgradeReqNode->getAttribute("name")->getRestrictedValue(); - upgradeReqs.push_back(factionType->getUpgradeType(name)); - } - - //resource requirements - const XmlNode *resourceRequirementsNode= parametersNode->getChild("resource-requirements"); - costs.resize(resourceRequirementsNode->getChildCount()); - for(int i=0; igetChild("resource", i); - string name= resourceNode->getAttribute("name")->getRestrictedValue(); - int amount= resourceNode->getAttribute("amount")->getIntValue(); - costs[i].init(techTree->getResourceType(name), amount); - } - - //resources stored - const XmlNode *resourcesStoredNode= parametersNode->getChild("resources-stored"); - storedResources.resize(resourcesStoredNode->getChildCount()); - for(int i=0; igetChild("resource", i); - string name= resourceNode->getAttribute("name")->getRestrictedValue(); - int amount= resourceNode->getAttribute("amount")->getIntValue(); - storedResources[i].init(techTree->getResourceType(name), amount); - } - - //image - const XmlNode *imageNode= parametersNode->getChild("image"); - image= Renderer::getInstance().newTexture2D(rsGame); - image->load(dir+"/"+imageNode->getAttribute("path")->getRestrictedValue()); - - //image cancel - const XmlNode *imageCancelNode= parametersNode->getChild("image-cancel"); - cancelImage= Renderer::getInstance().newTexture2D(rsGame); - cancelImage->load(dir+"/"+imageCancelNode->getAttribute("path")->getRestrictedValue()); - - //meeting point - const XmlNode *meetingPointNode= parametersNode->getChild("meeting-point"); - meetingPoint= meetingPointNode->getAttribute("value")->getBoolValue(); - if(meetingPoint){ - meetingPointImage= Renderer::getInstance().newTexture2D(rsGame); - meetingPointImage->load(dir+"/"+meetingPointNode->getAttribute("image-path")->getRestrictedValue()); - } - - //selection sounds - const XmlNode *selectionSoundNode= parametersNode->getChild("selection-sounds"); - if(selectionSoundNode->getAttribute("enabled")->getBoolValue()){ - selectionSounds.resize(selectionSoundNode->getChildCount()); - for(int i=0; igetChild("sound", i); - string path= soundNode->getAttribute("path")->getRestrictedValue(); - StaticSound *sound= new StaticSound(); - sound->load(dir + "/" + path); - selectionSounds[i]= sound; - } - } - - //command sounds - const XmlNode *commandSoundNode= parametersNode->getChild("command-sounds"); - if(commandSoundNode->getAttribute("enabled")->getBoolValue()){ - commandSounds.resize(commandSoundNode->getChildCount()); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *soundNode= commandSoundNode->getChild("sound", i); - string path= soundNode->getAttribute("path")->getRestrictedValue(); - StaticSound *sound= new StaticSound(); - sound->load(dir + "/" + path); - commandSounds[i]= sound; - } - } - - //skills - const XmlNode *skillsNode= unitNode->getChild("skills"); - skillTypes.resize(skillsNode->getChildCount()); - for(int i=0; igetChild("skill", i); - const XmlNode *typeNode= sn->getChild("type"); - string classId= typeNode->getAttribute("value")->getRestrictedValue(); - SkillType *skillType= SkillTypeFactory::getInstance().newInstance(classId); - skillType->load(sn, dir, techTree, factionType); - skillTypes[i]= skillType; - } - - //commands - const XmlNode *commandsNode= unitNode->getChild("commands"); - commandTypes.resize(commandsNode->getChildCount()); - for(int i=0; igetChild("command", i); - const XmlNode *typeNode= commandNode->getChild("type"); - string classId= typeNode->getAttribute("value")->getRestrictedValue(); - CommandType *commandType= CommandTypeFactory::getInstance().newInstance(classId); - commandType->load(i, commandNode, dir, techTree, factionType, *this); - commandTypes[i]= commandType; - } - - computeFirstStOfClass(); - computeFirstCtOfClass(); - - if(getFirstStOfClass(scStop)==NULL){ - throw runtime_error("Every unit must have at least one stop skill: "+ path); - } - if(getFirstStOfClass(scDie)==NULL){ - throw runtime_error("Every unit must have at least one die skill: "+ path); - } - - } - //Exception handling (conversions and so on); - catch(const exception &e){ - throw runtime_error("Error loading UnitType: " + path + "\n" + e.what()); - } -} - -// ==================== get ==================== - -const CommandType *UnitType::getFirstCtOfClass(CommandClass commandClass) const{ - return firstCommandTypeOfClass[commandClass]; -} - -const SkillType *UnitType::getFirstStOfClass(SkillClass skillClass) const{ - return firstSkillTypeOfClass[skillClass]; -} - -const HarvestCommandType *UnitType::getFirstHarvestCommand(const ResourceType *resourceType) const{ - for(int i=0; igetClass()== ccHarvest){ - const HarvestCommandType *hct= static_cast(commandTypes[i]); - if(hct->canHarvest(resourceType)){ - return hct; - } - } - } - return NULL; -} - -const AttackCommandType *UnitType::getFirstAttackCommand(Field field) const{ - for(int i=0; igetClass()== ccAttack){ - const AttackCommandType *act= static_cast(commandTypes[i]); - if(act->getAttackSkillType()->getAttackField(field)){ - return act; - } - } - } - return NULL; -} - -const RepairCommandType *UnitType::getFirstRepairCommand(const UnitType *repaired) const{ - for(int i=0; igetClass()== ccRepair){ - const RepairCommandType *rct= static_cast(commandTypes[i]); - if(rct->isRepairableUnitType(repaired)){ - return rct; - } - } - } - return NULL; -} - -int UnitType::getStore(const ResourceType *rt) const{ - for(int i=0; igetName()==skillName){ - if(skillTypes[i]->getClass()==skillClass){ - return skillTypes[i]; - } - else{ - throw runtime_error("Skill \""+skillName+"\" is not of class \""+SkillType::skillClassToStr(skillClass)); - } - } - } - throw runtime_error("No skill named \""+skillName+"\""); -} - -// ==================== totals ==================== - -int UnitType::getTotalMaxHp(const TotalUpgrade *totalUpgrade) const{ - return maxHp + totalUpgrade->getMaxHp(); -} - -int UnitType::getTotalMaxEp(const TotalUpgrade *totalUpgrade) const{ - return maxEp + totalUpgrade->getMaxEp(); -} - -int UnitType::getTotalArmor(const TotalUpgrade *totalUpgrade) const{ - return armor + totalUpgrade->getArmor(); -} - -int UnitType::getTotalSight(const TotalUpgrade *totalUpgrade) const{ - return sight + totalUpgrade->getSight(); -} - -// ==================== has ==================== - -bool UnitType::hasSkillClass(SkillClass skillClass) const{ - return firstSkillTypeOfClass[skillClass]!=NULL; -} - -bool UnitType::hasCommandType(const CommandType *commandType) const{ - assert(commandType!=NULL); - for(int i=0; igetClass()== SkillClass(j)){ - firstSkillTypeOfClass[j]= skillTypes[i]; - break; - } - } - } -} - -void UnitType::computeFirstCtOfClass(){ - for(int j=0; jgetClass()== CommandClass(j)){ - firstCommandTypeOfClass[j]= commandTypes[i]; - break; - } - } - } -} - -const CommandType* UnitType::findCommandTypeById(int id) const{ - for(int i=0; igetId()==id){ - return commandType; - } - } - return NULL; -} - -}}//end namespace +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo 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 "unit_type.h" + +#include + +#include "util.h" +#include "upgrade_type.h" +#include "resource_type.h" +#include "sound.h" +#include "logger.h" +#include "xml_parser.h" +#include "tech_tree.h" +#include "resource.h" +#include "renderer.h" +#include "game_util.h" +#include "leak_dumper.h" + +using namespace Shared::Xml; +using namespace Shared::Graphics; +using namespace Shared::Util; + +namespace Glest{ namespace Game{ + +// =============================== +// class Level +// =============================== + +void Level::init(string name, int kills){ + this->name= name; + this->kills= kills; +} + +// ===================================================== +// class UnitType +// ===================================================== + +// ===================== PUBLIC ======================== + +const char *UnitType::propertyNames[]= {"burnable", "rotated_climb"}; + +// ==================== creation and loading ==================== + +UnitType::UnitType(){ + + lightColor= Vec3f(0.f); + light= false; + multiSelect= false; + armorType= NULL; + + for(int i=0; iid= id; + string path; + + try{ + + Logger::getInstance().add("Unit type: " + formatString(name), true); + + //file load + path= dir+"/"+name+".xml"; + +#ifdef _WINDOWS + + DWORD fileAttributes = GetFileAttributes(path.c_str()); + if( (fileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) + { + return; + } + +#endif + + checksum->addFile(path); + + XmlTree xmlTree; + xmlTree.load(path); + const XmlNode *unitNode= xmlTree.getRootNode(); + + const XmlNode *parametersNode= unitNode->getChild("parameters"); + + //size + size= parametersNode->getChild("size")->getAttribute("value")->getIntValue(); + + //height + height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); + + //maxHp + maxHp= parametersNode->getChild("max-hp")->getAttribute("value")->getIntValue(); + + //hpRegeneration + hpRegeneration= parametersNode->getChild("max-hp")->getAttribute("regeneration")->getIntValue(); + + //maxEp + maxEp= parametersNode->getChild("max-ep")->getAttribute("value")->getIntValue(); + + if(maxEp!=0){ + //wpRegeneration + epRegeneration= parametersNode->getChild("max-ep")->getAttribute("regeneration")->getIntValue(); + } + + //armor + armor= parametersNode->getChild("armor")->getAttribute("value")->getIntValue(); + + //armor type string + string armorTypeName= parametersNode->getChild("armor-type")->getAttribute("value")->getRestrictedValue(); + armorType= techTree->getArmorType(armorTypeName); + + //sight + sight= parametersNode->getChild("sight")->getAttribute("value")->getIntValue(); + + //prod time + productionTime= parametersNode->getChild("time")->getAttribute("value")->getIntValue(); + + //multi selection + multiSelect= parametersNode->getChild("multi-selection")->getAttribute("value")->getBoolValue(); + + //cellmap + const XmlNode *cellMapNode= parametersNode->getChild("cellmap"); + bool hasCellMap= cellMapNode->getAttribute("value")->getBoolValue(); + if(hasCellMap){ + cellMap= new bool[size*size]; + for(int i=0; igetChild("row", i); + string row= rowNode->getAttribute("value")->getRestrictedValue(); + if(row.size()!=size){ + throw runtime_error("Cellmap row has not the same length as unit size"); + } + for(int j=0; jgetChild("levels"); + levels.resize(levelsNode->getChildCount()); + for(int i=0; igetChild("level", i); + levels[i].init( + levelNode->getAttribute("name")->getRestrictedValue(), + levelNode->getAttribute("kills")->getIntValue()); + } + + //fields + const XmlNode *fieldsNode= parametersNode->getChild("fields"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *fieldNode= fieldsNode->getChild("field", i); + string fieldName= fieldNode->getAttribute("value")->getRestrictedValue(); + if(fieldName=="land"){ + fields[fLand]= true; + } + else if(fieldName=="air"){ + fields[fAir]= true; + } + else{ + throw runtime_error("Not a valid field: "+fieldName+": "+ path); + } + } + + //properties + const XmlNode *propertiesNode= parametersNode->getChild("properties"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *propertyNode= propertiesNode->getChild("property", i); + string propertyName= propertyNode->getAttribute("value")->getRestrictedValue(); + bool found= false; + for(int i=0; igetChild("light"); + light= lightNode->getAttribute("enabled")->getBoolValue(); + if(light){ + lightColor.x= lightNode->getAttribute("red")->getFloatValue(0.f, 1.f); + lightColor.y= lightNode->getAttribute("green")->getFloatValue(0.f, 1.f); + lightColor.z= lightNode->getAttribute("blue")->getFloatValue(0.f, 1.f); + } + + //unit requirements + const XmlNode *unitRequirementsNode= parametersNode->getChild("unit-requirements"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *unitNode= unitRequirementsNode->getChild("unit", i); + string name= unitNode->getAttribute("name")->getRestrictedValue(); + unitReqs.push_back(factionType->getUnitType(name)); + } + + //upgrade requirements + const XmlNode *upgradeRequirementsNode= parametersNode->getChild("upgrade-requirements"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *upgradeReqNode= upgradeRequirementsNode->getChild("upgrade", i); + string name= upgradeReqNode->getAttribute("name")->getRestrictedValue(); + upgradeReqs.push_back(factionType->getUpgradeType(name)); + } + + //resource requirements + const XmlNode *resourceRequirementsNode= parametersNode->getChild("resource-requirements"); + costs.resize(resourceRequirementsNode->getChildCount()); + for(int i=0; igetChild("resource", i); + string name= resourceNode->getAttribute("name")->getRestrictedValue(); + int amount= resourceNode->getAttribute("amount")->getIntValue(); + costs[i].init(techTree->getResourceType(name), amount); + } + + //resources stored + const XmlNode *resourcesStoredNode= parametersNode->getChild("resources-stored"); + storedResources.resize(resourcesStoredNode->getChildCount()); + for(int i=0; igetChild("resource", i); + string name= resourceNode->getAttribute("name")->getRestrictedValue(); + int amount= resourceNode->getAttribute("amount")->getIntValue(); + storedResources[i].init(techTree->getResourceType(name), amount); + } + + //image + const XmlNode *imageNode= parametersNode->getChild("image"); + image= Renderer::getInstance().newTexture2D(rsGame); + image->load(dir+"/"+imageNode->getAttribute("path")->getRestrictedValue()); + + //image cancel + const XmlNode *imageCancelNode= parametersNode->getChild("image-cancel"); + cancelImage= Renderer::getInstance().newTexture2D(rsGame); + cancelImage->load(dir+"/"+imageCancelNode->getAttribute("path")->getRestrictedValue()); + + //meeting point + const XmlNode *meetingPointNode= parametersNode->getChild("meeting-point"); + meetingPoint= meetingPointNode->getAttribute("value")->getBoolValue(); + if(meetingPoint){ + meetingPointImage= Renderer::getInstance().newTexture2D(rsGame); + meetingPointImage->load(dir+"/"+meetingPointNode->getAttribute("image-path")->getRestrictedValue()); + } + + //selection sounds + const XmlNode *selectionSoundNode= parametersNode->getChild("selection-sounds"); + if(selectionSoundNode->getAttribute("enabled")->getBoolValue()){ + selectionSounds.resize(selectionSoundNode->getChildCount()); + for(int i=0; igetChild("sound", i); + string path= soundNode->getAttribute("path")->getRestrictedValue(); + StaticSound *sound= new StaticSound(); + sound->load(dir + "/" + path); + selectionSounds[i]= sound; + } + } + + //command sounds + const XmlNode *commandSoundNode= parametersNode->getChild("command-sounds"); + if(commandSoundNode->getAttribute("enabled")->getBoolValue()){ + commandSounds.resize(commandSoundNode->getChildCount()); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *soundNode= commandSoundNode->getChild("sound", i); + string path= soundNode->getAttribute("path")->getRestrictedValue(); + StaticSound *sound= new StaticSound(); + sound->load(dir + "/" + path); + commandSounds[i]= sound; + } + } + + //skills + const XmlNode *skillsNode= unitNode->getChild("skills"); + skillTypes.resize(skillsNode->getChildCount()); + for(int i=0; igetChild("skill", i); + const XmlNode *typeNode= sn->getChild("type"); + string classId= typeNode->getAttribute("value")->getRestrictedValue(); + SkillType *skillType= SkillTypeFactory::getInstance().newInstance(classId); + skillType->load(sn, dir, techTree, factionType); + skillTypes[i]= skillType; + } + + //commands + const XmlNode *commandsNode= unitNode->getChild("commands"); + commandTypes.resize(commandsNode->getChildCount()); + for(int i=0; igetChild("command", i); + const XmlNode *typeNode= commandNode->getChild("type"); + string classId= typeNode->getAttribute("value")->getRestrictedValue(); + CommandType *commandType= CommandTypeFactory::getInstance().newInstance(classId); + commandType->load(i, commandNode, dir, techTree, factionType, *this); + commandTypes[i]= commandType; + } + + computeFirstStOfClass(); + computeFirstCtOfClass(); + + if(getFirstStOfClass(scStop)==NULL){ + throw runtime_error("Every unit must have at least one stop skill: "+ path); + } + if(getFirstStOfClass(scDie)==NULL){ + throw runtime_error("Every unit must have at least one die skill: "+ path); + } + + } + //Exception handling (conversions and so on); + catch(const exception &e){ + throw runtime_error("Error loading UnitType: " + path + "\n" + e.what()); + } +} + +// ==================== get ==================== + +const CommandType *UnitType::getFirstCtOfClass(CommandClass commandClass) const{ + return firstCommandTypeOfClass[commandClass]; +} + +const SkillType *UnitType::getFirstStOfClass(SkillClass skillClass) const{ + return firstSkillTypeOfClass[skillClass]; +} + +const HarvestCommandType *UnitType::getFirstHarvestCommand(const ResourceType *resourceType) const{ + for(int i=0; igetClass()== ccHarvest){ + const HarvestCommandType *hct= static_cast(commandTypes[i]); + if(hct->canHarvest(resourceType)){ + return hct; + } + } + } + return NULL; +} + +const AttackCommandType *UnitType::getFirstAttackCommand(Field field) const{ + for(int i=0; igetClass()== ccAttack){ + const AttackCommandType *act= static_cast(commandTypes[i]); + if(act->getAttackSkillType()->getAttackField(field)){ + return act; + } + } + } + return NULL; +} + +const RepairCommandType *UnitType::getFirstRepairCommand(const UnitType *repaired) const{ + for(int i=0; igetClass()== ccRepair){ + const RepairCommandType *rct= static_cast(commandTypes[i]); + if(rct->isRepairableUnitType(repaired)){ + return rct; + } + } + } + return NULL; +} + +int UnitType::getStore(const ResourceType *rt) const{ + for(int i=0; igetName()==skillName){ + if(skillTypes[i]->getClass()==skillClass){ + return skillTypes[i]; + } + else{ + throw runtime_error("Skill \""+skillName+"\" is not of class \""+SkillType::skillClassToStr(skillClass)); + } + } + } + throw runtime_error("No skill named \""+skillName+"\""); +} + +// ==================== totals ==================== + +int UnitType::getTotalMaxHp(const TotalUpgrade *totalUpgrade) const{ + return maxHp + totalUpgrade->getMaxHp(); +} + +int UnitType::getTotalMaxEp(const TotalUpgrade *totalUpgrade) const{ + return maxEp + totalUpgrade->getMaxEp(); +} + +int UnitType::getTotalArmor(const TotalUpgrade *totalUpgrade) const{ + return armor + totalUpgrade->getArmor(); +} + +int UnitType::getTotalSight(const TotalUpgrade *totalUpgrade) const{ + return sight + totalUpgrade->getSight(); +} + +// ==================== has ==================== + +bool UnitType::hasSkillClass(SkillClass skillClass) const{ + return firstSkillTypeOfClass[skillClass]!=NULL; +} + +bool UnitType::hasCommandType(const CommandType *commandType) const{ + assert(commandType!=NULL); + for(int i=0; igetClass()== SkillClass(j)){ + firstSkillTypeOfClass[j]= skillTypes[i]; + break; + } + } + } +} + +void UnitType::computeFirstCtOfClass(){ + for(int j=0; jgetClass()== CommandClass(j)){ + firstCommandTypeOfClass[j]= commandTypes[i]; + break; + } + } + } +} + +const CommandType* UnitType::findCommandTypeById(int id) const{ + for(int i=0; igetId()==id){ + return commandType; + } + } + return NULL; +} + +}}//end namespace diff --git a/source/glest_game/types/upgrade_type.cpp b/source/glest_game/types/upgrade_type.cpp index 8409990cb..1dd54222c 100644 --- a/source/glest_game/types/upgrade_type.cpp +++ b/source/glest_game/types/upgrade_type.cpp @@ -1,209 +1,219 @@ -// ============================================================== -// This file is part of Glest (www.glest.org) -// -// Copyright (C) 2001-2008 Martiņo 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 "upgrade_type.h" - -#include -#include - -#include "unit_type.h" -#include "util.h" -#include "logger.h" -#include "lang.h" -#include "xml_parser.h" -#include "tech_tree.h" -#include "faction_type.h" -#include "resource.h" -#include "renderer.h" -#include "game_util.h" -#include "leak_dumper.h" - -using namespace Shared::Util; -using namespace Shared::Xml; - -namespace Glest{ namespace Game{ - -// ===================================================== -// class UpgradeType -// ===================================================== - -// ==================== get ==================== - -bool UpgradeType::isAffected(const UnitType *unitType) const{ - return find(effects.begin(), effects.end(), unitType)!=effects.end(); -} - -// ==================== misc ==================== - -void UpgradeType::preLoad(const string &dir){ - name=lastDir(dir); -} - -void UpgradeType::load(const string &dir, const TechTree *techTree, const FactionType *factionType, Checksum* checksum){ - string path; - - Logger::getInstance().add("Upgrade type: "+ formatString(name), true); - - path=dir+"/"+name+".xml"; - - try{ - checksum->addFile(path); - - XmlTree xmlTree; - xmlTree.load(path); - const XmlNode *upgradeNode= xmlTree.getRootNode(); - - //image - const XmlNode *imageNode= upgradeNode->getChild("image"); - image= Renderer::getInstance().newTexture2D(rsGame); - image->load(dir+"/"+imageNode->getAttribute("path")->getRestrictedValue()); - - //image cancel - const XmlNode *imageCancelNode= upgradeNode->getChild("image-cancel"); - cancelImage= Renderer::getInstance().newTexture2D(rsGame); - cancelImage->load(dir+"/"+imageCancelNode->getAttribute("path")->getRestrictedValue()); - - //upgrade time - const XmlNode *upgradeTimeNode= upgradeNode->getChild("time"); - productionTime= upgradeTimeNode->getAttribute("value")->getIntValue(); - - //unit requirements - const XmlNode *unitRequirementsNode= upgradeNode->getChild("unit-requirements"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *unitNode= unitRequirementsNode->getChild("unit", i); - string name= unitNode->getAttribute("name")->getRestrictedValue(); - unitReqs.push_back(factionType->getUnitType(name)); - } - - //upgrade requirements - const XmlNode *upgradeRequirementsNode= upgradeNode->getChild("upgrade-requirements"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *upgradeReqNode= upgradeRequirementsNode->getChild("upgrade", i); - string name= upgradeReqNode->getAttribute("name")->getRestrictedValue(); - upgradeReqs.push_back(factionType->getUpgradeType(name)); - } - - //resource requirements - const XmlNode *resourceRequirementsNode= upgradeNode->getChild("resource-requirements"); - costs.resize(resourceRequirementsNode->getChildCount()); - for(int i=0; igetChild("resource", i); - string name= resourceNode->getAttribute("name")->getRestrictedValue(); - int amount= resourceNode->getAttribute("amount")->getIntValue(); - costs[i].init(techTree->getResourceType(name), amount); - } - - //effects - const XmlNode *effectsNode= upgradeNode->getChild("effects"); - for(int i=0; igetChildCount(); ++i){ - const XmlNode *unitNode= effectsNode->getChild("unit", i); - string name= unitNode->getAttribute("name")->getRestrictedValue(); - effects.push_back(factionType->getUnitType(name)); - } - - //values - maxHp= upgradeNode->getChild("max-hp")->getAttribute("value")->getIntValue(); - maxEp= upgradeNode->getChild("max-ep")->getAttribute("value")->getIntValue(); - sight= upgradeNode->getChild("sight")->getAttribute("value")->getIntValue(); - attackStrength= upgradeNode->getChild("attack-strenght")->getAttribute("value")->getIntValue(); - attackRange= upgradeNode->getChild("attack-range")->getAttribute("value")->getIntValue(); - armor= upgradeNode->getChild("armor")->getAttribute("value")->getIntValue(); - moveSpeed= upgradeNode->getChild("move-speed")->getAttribute("value")->getIntValue(); - prodSpeed= upgradeNode->getChild("production-speed")->getAttribute("value")->getIntValue(); - - } - catch(const exception &e){ - throw runtime_error("Error loading UpgradeType: "+ dir + "\n" +e.what()); - } -} - -string UpgradeType::getReqDesc() const{ - - string str; - int i; - Lang &lang= Lang::getInstance(); - - str= ProducibleType::getReqDesc(); - if(getEffectCount()>0){ - str+= "\n"+ lang.get("Upgrades")+":\n"; - for(i=0; igetName()+"\n"; - } - } - - if(maxHp!=0){ - str+= lang.get("Hp")+" +"+intToStr(maxHp); - } - if(sight!=0){ - str+= lang.get("Sight")+" +"+intToStr(sight); - } - if(maxEp!=0){ - str+= lang.get("Ep")+" +"+intToStr(maxEp)+"\n"; - } - if(attackStrength!=0){ - str+= lang.get("AttackStrenght")+" +"+intToStr(attackStrength)+"\n"; - } - if(attackRange!=0){ - str+= lang.get("AttackDistance")+" +"+intToStr(attackRange)+"\n"; - } - if(armor!=0){ - str+= lang.get("Armor")+" +"+intToStr(armor)+"\n"; - } - if(moveSpeed!=0){ - str+= lang.get("WalkSpeed")+"+ "+intToStr(moveSpeed)+"\n"; - } - if(prodSpeed!=0){ - str+= lang.get("ProductionSpeed")+" +"+intToStr(prodSpeed)+"\n"; - } - - return str; -} - - - -// =============================== -// class TotalUpgrade -// =============================== - -TotalUpgrade::TotalUpgrade(){ - reset(); -} - -void TotalUpgrade::reset(){ - maxHp= 0; - maxEp= 0; - sight=0; - armor= 0; - attackStrength= 0; - attackRange= 0; - moveSpeed= 0; - prodSpeed=0; -} - -void TotalUpgrade::sum(const UpgradeType *ut){ - maxHp+= ut->getMaxHp(); - maxEp+= ut->getMaxEp(); - sight+= ut->getSight(); - armor+= ut->getArmor(); - attackStrength+= ut->getAttackStrength(); - attackRange+= ut->getAttackRange(); - moveSpeed+= ut->getMoveSpeed(); - prodSpeed+= ut->getProdSpeed(); -} - -void TotalUpgrade::incLevel(const UnitType *ut){ - maxHp+= ut->getMaxHp()*50/100; - maxEp+= ut->getMaxEp()*50/100; - sight+= ut->getSight()*20/100; - armor+= ut->getArmor()*50/100; -} - -}}//end namespace +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo 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 "upgrade_type.h" + +#include +#include + +#include "unit_type.h" +#include "util.h" +#include "logger.h" +#include "lang.h" +#include "xml_parser.h" +#include "tech_tree.h" +#include "faction_type.h" +#include "resource.h" +#include "renderer.h" +#include "game_util.h" +#include "leak_dumper.h" + +using namespace Shared::Util; +using namespace Shared::Xml; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class UpgradeType +// ===================================================== + +// ==================== get ==================== + +bool UpgradeType::isAffected(const UnitType *unitType) const{ + return find(effects.begin(), effects.end(), unitType)!=effects.end(); +} + +// ==================== misc ==================== + +void UpgradeType::preLoad(const string &dir){ + name=lastDir(dir); +} + +void UpgradeType::load(const string &dir, const TechTree *techTree, const FactionType *factionType, Checksum* checksum){ + string path; + + Logger::getInstance().add("Upgrade type: "+ formatString(name), true); + + path=dir+"/"+name+".xml"; + +#ifdef _WINDOWS + + DWORD fileAttributes = GetFileAttributes(path.c_str()); + if( (fileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) + { + return; + } + +#endif + + try{ + checksum->addFile(path); + + XmlTree xmlTree; + xmlTree.load(path); + const XmlNode *upgradeNode= xmlTree.getRootNode(); + + //image + const XmlNode *imageNode= upgradeNode->getChild("image"); + image= Renderer::getInstance().newTexture2D(rsGame); + image->load(dir+"/"+imageNode->getAttribute("path")->getRestrictedValue()); + + //image cancel + const XmlNode *imageCancelNode= upgradeNode->getChild("image-cancel"); + cancelImage= Renderer::getInstance().newTexture2D(rsGame); + cancelImage->load(dir+"/"+imageCancelNode->getAttribute("path")->getRestrictedValue()); + + //upgrade time + const XmlNode *upgradeTimeNode= upgradeNode->getChild("time"); + productionTime= upgradeTimeNode->getAttribute("value")->getIntValue(); + + //unit requirements + const XmlNode *unitRequirementsNode= upgradeNode->getChild("unit-requirements"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *unitNode= unitRequirementsNode->getChild("unit", i); + string name= unitNode->getAttribute("name")->getRestrictedValue(); + unitReqs.push_back(factionType->getUnitType(name)); + } + + //upgrade requirements + const XmlNode *upgradeRequirementsNode= upgradeNode->getChild("upgrade-requirements"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *upgradeReqNode= upgradeRequirementsNode->getChild("upgrade", i); + string name= upgradeReqNode->getAttribute("name")->getRestrictedValue(); + upgradeReqs.push_back(factionType->getUpgradeType(name)); + } + + //resource requirements + const XmlNode *resourceRequirementsNode= upgradeNode->getChild("resource-requirements"); + costs.resize(resourceRequirementsNode->getChildCount()); + for(int i=0; igetChild("resource", i); + string name= resourceNode->getAttribute("name")->getRestrictedValue(); + int amount= resourceNode->getAttribute("amount")->getIntValue(); + costs[i].init(techTree->getResourceType(name), amount); + } + + //effects + const XmlNode *effectsNode= upgradeNode->getChild("effects"); + for(int i=0; igetChildCount(); ++i){ + const XmlNode *unitNode= effectsNode->getChild("unit", i); + string name= unitNode->getAttribute("name")->getRestrictedValue(); + effects.push_back(factionType->getUnitType(name)); + } + + //values + maxHp= upgradeNode->getChild("max-hp")->getAttribute("value")->getIntValue(); + maxEp= upgradeNode->getChild("max-ep")->getAttribute("value")->getIntValue(); + sight= upgradeNode->getChild("sight")->getAttribute("value")->getIntValue(); + attackStrength= upgradeNode->getChild("attack-strenght")->getAttribute("value")->getIntValue(); + attackRange= upgradeNode->getChild("attack-range")->getAttribute("value")->getIntValue(); + armor= upgradeNode->getChild("armor")->getAttribute("value")->getIntValue(); + moveSpeed= upgradeNode->getChild("move-speed")->getAttribute("value")->getIntValue(); + prodSpeed= upgradeNode->getChild("production-speed")->getAttribute("value")->getIntValue(); + + } + catch(const exception &e){ + throw runtime_error("Error loading UpgradeType: "+ dir + "\n" +e.what()); + } +} + +string UpgradeType::getReqDesc() const{ + + string str; + int i; + Lang &lang= Lang::getInstance(); + + str= ProducibleType::getReqDesc(); + if(getEffectCount()>0){ + str+= "\n"+ lang.get("Upgrades")+":\n"; + for(i=0; igetName()+"\n"; + } + } + + if(maxHp!=0){ + str+= lang.get("Hp")+" +"+intToStr(maxHp); + } + if(sight!=0){ + str+= lang.get("Sight")+" +"+intToStr(sight); + } + if(maxEp!=0){ + str+= lang.get("Ep")+" +"+intToStr(maxEp)+"\n"; + } + if(attackStrength!=0){ + str+= lang.get("AttackStrenght")+" +"+intToStr(attackStrength)+"\n"; + } + if(attackRange!=0){ + str+= lang.get("AttackDistance")+" +"+intToStr(attackRange)+"\n"; + } + if(armor!=0){ + str+= lang.get("Armor")+" +"+intToStr(armor)+"\n"; + } + if(moveSpeed!=0){ + str+= lang.get("WalkSpeed")+"+ "+intToStr(moveSpeed)+"\n"; + } + if(prodSpeed!=0){ + str+= lang.get("ProductionSpeed")+" +"+intToStr(prodSpeed)+"\n"; + } + + return str; +} + + + +// =============================== +// class TotalUpgrade +// =============================== + +TotalUpgrade::TotalUpgrade(){ + reset(); +} + +void TotalUpgrade::reset(){ + maxHp= 0; + maxEp= 0; + sight=0; + armor= 0; + attackStrength= 0; + attackRange= 0; + moveSpeed= 0; + prodSpeed=0; +} + +void TotalUpgrade::sum(const UpgradeType *ut){ + maxHp+= ut->getMaxHp(); + maxEp+= ut->getMaxEp(); + sight+= ut->getSight(); + armor+= ut->getArmor(); + attackStrength+= ut->getAttackStrength(); + attackRange+= ut->getAttackRange(); + moveSpeed+= ut->getMoveSpeed(); + prodSpeed+= ut->getProdSpeed(); +} + +void TotalUpgrade::incLevel(const UnitType *ut){ + maxHp+= ut->getMaxHp()*50/100; + maxEp+= ut->getMaxEp()*50/100; + sight+= ut->getSight()*20/100; + armor+= ut->getArmor()*50/100; +} + +}}//end namespace diff --git a/source/glest_game/world/scenario.cpp b/source/glest_game/world/scenario.cpp index ede4976aa..cff9b9df8 100644 --- a/source/glest_game/world/scenario.cpp +++ b/source/glest_game/world/scenario.cpp @@ -3,8 +3,8 @@ // // Copyright (C) 2001-2005 Martiņo Figueroa // -// You can redistribute this code and/or modify it under -// the terms of the GNU General Public License as published +// 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 // ============================================================== diff --git a/source/glest_game/world/tileset.cpp b/source/glest_game/world/tileset.cpp index c05f266e8..ea92a8e40 100644 --- a/source/glest_game/world/tileset.cpp +++ b/source/glest_game/world/tileset.cpp @@ -1,252 +1,262 @@ -// ============================================================== -// This file is part of Glest (www.glest.org) -// -// Copyright (C) 2001-2008 Martiņo 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 "tileset.h" - -#include -#include - -#include "logger.h" -#include "util.h" -#include "renderer.h" -#include "game_util.h" -#include "leak_dumper.h" - -using namespace Shared::Util; -using namespace Shared::Xml; -using namespace Shared::Graphics; - -namespace Glest{ namespace Game{ - -// ===================================================== -// class AmbientSounds -// ===================================================== - -void AmbientSounds::load(const string &dir, const XmlNode *xmlNode){ - string path; - - //day - const XmlNode *dayNode= xmlNode->getChild("day-sound"); - enabledDay= dayNode->getAttribute("enabled")->getBoolValue(); - if(enabledDay){ - path= dayNode->getAttribute("path")->getRestrictedValue(); - day.open(dir + "/" + path); - alwaysPlayDay= dayNode->getAttribute("play-always")->getBoolValue(); - } - - //night - const XmlNode *nightNode= xmlNode->getChild("night-sound"); - enabledNight= nightNode->getAttribute("enabled")->getBoolValue(); - if(enabledNight){ - path= nightNode->getAttribute("path")->getRestrictedValue(); - night.open(dir + "/" + path); - alwaysPlayNight= nightNode->getAttribute("play-always")->getBoolValue(); - } - - //rain - const XmlNode *rainNode= xmlNode->getChild("rain-sound"); - enabledRain= rainNode->getAttribute("enabled")->getBoolValue(); - if(enabledRain){ - path= rainNode->getAttribute("path")->getRestrictedValue(); - rain.open(dir + "/" + path); - } - - //snow - const XmlNode *snowNode= xmlNode->getChild("snow-sound"); - enabledSnow= snowNode->getAttribute("enabled")->getBoolValue(); - if(enabledSnow){ - path= snowNode->getAttribute("path")->getRestrictedValue(); - snow.open(dir + "/" + path); - } - - //dayStart - const XmlNode *dayStartNode= xmlNode->getChild("day-start-sound"); - enabledDayStart= dayStartNode->getAttribute("enabled")->getBoolValue(); - if(enabledDayStart){ - path= dayStartNode->getAttribute("path")->getRestrictedValue(); - dayStart.load(dir + "/" + path); - } - - //nightStart - const XmlNode *nightStartNode= xmlNode->getChild("night-start-sound"); - enabledNightStart= nightStartNode->getAttribute("enabled")->getBoolValue(); - if(enabledNightStart){ - path= nightStartNode->getAttribute("path")->getRestrictedValue(); - nightStart.load(dir + "/" + path); - } - -} - -// ===================================================== -// class Tileset -// ===================================================== - -void Tileset::load(const string &dir, Checksum *checksum){ - - random.init(time(NULL)); - - string name= lastDir(dir); - string path= dir+"/"+name+".xml"; - - checksum->addFile(path); - - try{ - Logger::getInstance().add("Tileset: "+formatString(name), true); - Renderer &renderer= Renderer::getInstance(); - - //parse xml - XmlTree xmlTree; - xmlTree.load(path); - const XmlNode *tilesetNode= xmlTree.getRootNode(); - - //surfaces - const XmlNode *surfacesNode= tilesetNode->getChild("surfaces"); - for(int i=0; igetChild("surface", i); - - int childCount= surfaceNode->getChildCount(); - surfPixmaps[i].resize(childCount); - surfProbs[i].resize(childCount); - for(int j=0; jgetChild("texture", j); - surfPixmaps[i][j].init(3); - surfPixmaps[i][j].load(dir +"/"+textureNode->getAttribute("path")->getRestrictedValue()); - surfProbs[i][j]= textureNode->getAttribute("prob")->getFloatValue(); - } - } - - //object models - const XmlNode *objectsNode= tilesetNode->getChild("objects"); - for(int i=0; igetChild("object", i); - int childCount= objectNode->getChildCount(); - objectTypes[i].init(childCount, i, objectNode->getAttribute("walkable")->getBoolValue()); - for(int j=0; jgetChild("model", j); - const XmlAttribute *pathAttribute= modelNode->getAttribute("path"); - objectTypes[i].loadModel(dir +"/"+ pathAttribute->getRestrictedValue()); - } - } - - //ambient sounds - ambientSounds.load(dir, tilesetNode->getChild("ambient-sounds")); - - //parameters - const XmlNode *parametersNode= tilesetNode->getChild("parameters"); - - //water - const XmlNode *waterNode= parametersNode->getChild("water"); - waterTex= renderer.newTexture3D(rsGame); - waterTex->setMipmap(false); - waterTex->setWrapMode(Texture::wmRepeat); - waterEffects= waterNode->getAttribute("effects")->getBoolValue(); - - int waterFrameCount= waterNode->getChildCount(); - waterTex->getPixmap()->init(waterFrameCount, 4); - for(int i=0; igetChild("texture", i); - waterTex->getPixmap()->loadSlice(dir +"/"+ waterFrameNode->getAttribute("path")->getRestrictedValue(), i); - } - - //fog - const XmlNode *fogNode= parametersNode->getChild("fog"); - fog= fogNode->getAttribute("enabled")->getBoolValue(); - if(fog){ - fogMode= fogNode->getAttribute("mode")->getIntValue(1, 2); - fogDensity= fogNode->getAttribute("density")->getFloatValue(); - fogColor.x= fogNode->getAttribute("color-red")->getFloatValue(0.f, 1.f); - fogColor.y= fogNode->getAttribute("color-green")->getFloatValue(0.f, 1.f); - fogColor.z= fogNode->getAttribute("color-blue")->getFloatValue(0.f, 1.f); - } - - //sun and moon light colors - const XmlNode *sunLightColorNode= parametersNode->getChild("sun-light"); - sunLightColor.x= sunLightColorNode->getAttribute("red")->getFloatValue(); - sunLightColor.y= sunLightColorNode->getAttribute("green")->getFloatValue(); - sunLightColor.z= sunLightColorNode->getAttribute("blue")->getFloatValue(); - - const XmlNode *moonLightColorNode= parametersNode->getChild("moon-light"); - moonLightColor.x= moonLightColorNode->getAttribute("red")->getFloatValue(); - moonLightColor.y= moonLightColorNode->getAttribute("green")->getFloatValue(); - moonLightColor.z= moonLightColorNode->getAttribute("blue")->getFloatValue(); - - - //weather - const XmlNode *weatherNode= parametersNode->getChild("weather"); - float sunnyProb= weatherNode->getAttribute("sun")->getFloatValue(0.f, 1.f); - float rainyProb= weatherNode->getAttribute("rain")->getFloatValue(0.f, 1.f) + sunnyProb; - float rnd= fabs(random.randRange(-1.f, 1.f)); - - if(rnd +#include + +#include "logger.h" +#include "util.h" +#include "renderer.h" +#include "game_util.h" +#include "leak_dumper.h" + +using namespace Shared::Util; +using namespace Shared::Xml; +using namespace Shared::Graphics; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class AmbientSounds +// ===================================================== + +void AmbientSounds::load(const string &dir, const XmlNode *xmlNode){ + string path; + + //day + const XmlNode *dayNode= xmlNode->getChild("day-sound"); + enabledDay= dayNode->getAttribute("enabled")->getBoolValue(); + if(enabledDay){ + path= dayNode->getAttribute("path")->getRestrictedValue(); + day.open(dir + "/" + path); + alwaysPlayDay= dayNode->getAttribute("play-always")->getBoolValue(); + } + + //night + const XmlNode *nightNode= xmlNode->getChild("night-sound"); + enabledNight= nightNode->getAttribute("enabled")->getBoolValue(); + if(enabledNight){ + path= nightNode->getAttribute("path")->getRestrictedValue(); + night.open(dir + "/" + path); + alwaysPlayNight= nightNode->getAttribute("play-always")->getBoolValue(); + } + + //rain + const XmlNode *rainNode= xmlNode->getChild("rain-sound"); + enabledRain= rainNode->getAttribute("enabled")->getBoolValue(); + if(enabledRain){ + path= rainNode->getAttribute("path")->getRestrictedValue(); + rain.open(dir + "/" + path); + } + + //snow + const XmlNode *snowNode= xmlNode->getChild("snow-sound"); + enabledSnow= snowNode->getAttribute("enabled")->getBoolValue(); + if(enabledSnow){ + path= snowNode->getAttribute("path")->getRestrictedValue(); + snow.open(dir + "/" + path); + } + + //dayStart + const XmlNode *dayStartNode= xmlNode->getChild("day-start-sound"); + enabledDayStart= dayStartNode->getAttribute("enabled")->getBoolValue(); + if(enabledDayStart){ + path= dayStartNode->getAttribute("path")->getRestrictedValue(); + dayStart.load(dir + "/" + path); + } + + //nightStart + const XmlNode *nightStartNode= xmlNode->getChild("night-start-sound"); + enabledNightStart= nightStartNode->getAttribute("enabled")->getBoolValue(); + if(enabledNightStart){ + path= nightStartNode->getAttribute("path")->getRestrictedValue(); + nightStart.load(dir + "/" + path); + } + +} + +// ===================================================== +// class Tileset +// ===================================================== + +void Tileset::load(const string &dir, Checksum *checksum){ + + random.init(time(NULL)); + + string name= lastDir(dir); + string path= dir+"/"+name+".xml"; + +#ifdef _WINDOWS + + DWORD fileAttributes = GetFileAttributes(path.c_str()); + if( (fileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) + { + return; + } + +#endif + + checksum->addFile(path); + + try{ + Logger::getInstance().add("Tileset: "+formatString(name), true); + Renderer &renderer= Renderer::getInstance(); + + //parse xml + XmlTree xmlTree; + xmlTree.load(path); + const XmlNode *tilesetNode= xmlTree.getRootNode(); + + //surfaces + const XmlNode *surfacesNode= tilesetNode->getChild("surfaces"); + for(int i=0; igetChild("surface", i); + + int childCount= surfaceNode->getChildCount(); + surfPixmaps[i].resize(childCount); + surfProbs[i].resize(childCount); + for(int j=0; jgetChild("texture", j); + surfPixmaps[i][j].init(3); + surfPixmaps[i][j].load(dir +"/"+textureNode->getAttribute("path")->getRestrictedValue()); + surfProbs[i][j]= textureNode->getAttribute("prob")->getFloatValue(); + } + } + + //object models + const XmlNode *objectsNode= tilesetNode->getChild("objects"); + for(int i=0; igetChild("object", i); + int childCount= objectNode->getChildCount(); + objectTypes[i].init(childCount, i, objectNode->getAttribute("walkable")->getBoolValue()); + for(int j=0; jgetChild("model", j); + const XmlAttribute *pathAttribute= modelNode->getAttribute("path"); + objectTypes[i].loadModel(dir +"/"+ pathAttribute->getRestrictedValue()); + } + } + + //ambient sounds + ambientSounds.load(dir, tilesetNode->getChild("ambient-sounds")); + + //parameters + const XmlNode *parametersNode= tilesetNode->getChild("parameters"); + + //water + const XmlNode *waterNode= parametersNode->getChild("water"); + waterTex= renderer.newTexture3D(rsGame); + waterTex->setMipmap(false); + waterTex->setWrapMode(Texture::wmRepeat); + waterEffects= waterNode->getAttribute("effects")->getBoolValue(); + + int waterFrameCount= waterNode->getChildCount(); + waterTex->getPixmap()->init(waterFrameCount, 4); + for(int i=0; igetChild("texture", i); + waterTex->getPixmap()->loadSlice(dir +"/"+ waterFrameNode->getAttribute("path")->getRestrictedValue(), i); + } + + //fog + const XmlNode *fogNode= parametersNode->getChild("fog"); + fog= fogNode->getAttribute("enabled")->getBoolValue(); + if(fog){ + fogMode= fogNode->getAttribute("mode")->getIntValue(1, 2); + fogDensity= fogNode->getAttribute("density")->getFloatValue(); + fogColor.x= fogNode->getAttribute("color-red")->getFloatValue(0.f, 1.f); + fogColor.y= fogNode->getAttribute("color-green")->getFloatValue(0.f, 1.f); + fogColor.z= fogNode->getAttribute("color-blue")->getFloatValue(0.f, 1.f); + } + + //sun and moon light colors + const XmlNode *sunLightColorNode= parametersNode->getChild("sun-light"); + sunLightColor.x= sunLightColorNode->getAttribute("red")->getFloatValue(); + sunLightColor.y= sunLightColorNode->getAttribute("green")->getFloatValue(); + sunLightColor.z= sunLightColorNode->getAttribute("blue")->getFloatValue(); + + const XmlNode *moonLightColorNode= parametersNode->getChild("moon-light"); + moonLightColor.x= moonLightColorNode->getAttribute("red")->getFloatValue(); + moonLightColor.y= moonLightColorNode->getAttribute("green")->getFloatValue(); + moonLightColor.z= moonLightColorNode->getAttribute("blue")->getFloatValue(); + + + //weather + const XmlNode *weatherNode= parametersNode->getChild("weather"); + float sunnyProb= weatherNode->getAttribute("sun")->getFloatValue(0.f, 1.f); + float rainyProb= weatherNode->getAttribute("rain")->getFloatValue(0.f, 1.f) + sunnyProb; + float rnd= fabs(random.randRange(-1.f, 1.f)); + + if(rnd