Bugfixes:

- final network fixes for mega-glest multiplayer performance improvements
- added ability for windows builds to "ignore" hidden files so that glest wont try to read svn files
- bugfix for loading the map editor
This commit is contained in:
Mark Vejvoda 2010-02-15 17:07:21 +00:00
parent ca20784740
commit 012644273e
8 changed files with 1518 additions and 1454 deletions

View File

@ -252,7 +252,7 @@ END_EVENT_TABLE()
// =====================================================
GlCanvas::GlCanvas(MainWindow * mainWindow):
wxGLCanvas(mainWindow, -1)
wxGLCanvas(mainWindow, -1, wxDefaultPosition)
{
this->mainWindow = mainWindow;
}

View File

@ -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<string> unitFilenames;
findAll(unitsPath, unitFilenames);
unitTypes.resize(unitFilenames.size());
for(int i=0; i<unitTypes.size(); ++i){
string str= dir + "/units/" + unitFilenames[i];
unitTypes[i].preLoad(str);
}
// a2) preload upgrades
string upgradesPath= dir + "/upgrades/*.";
vector<string> upgradeFilenames;
findAll(upgradesPath, upgradeFilenames);
upgradeTypes.resize(upgradeFilenames.size());
for(int i=0; i<upgradeTypes.size(); ++i){
string str= dir + "/upgrades/" + upgradeFilenames[i];
upgradeTypes[i].preLoad(str);
}
// b1) load units
try{
for(int i=0; i<unitTypes.size(); ++i){
string str= dir + "/units/" + unitTypes[i].getName();
unitTypes[i].load(i, str, techTree, this, checksum);
}
}
catch(const exception &e){
throw runtime_error("Error loading units: "+ dir + "\n" + e.what());
}
// b2) load upgrades
try{
for(int i=0; i<upgradeTypes.size(); ++i){
string str= dir + "/upgrades/" + upgradeTypes[i].getName();
upgradeTypes[i].load(str, techTree, this, checksum);
}
}
catch(const exception &e){
throw runtime_error("Error loading upgrades: "+ dir + "\n" + e.what());
}
//open xml file
string path= dir+"/"+name+".xml";
checksum->addFile(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; i<startingResources.size(); ++i){
const XmlNode *resourceNode= startingResourcesNode->getChild("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; i<startingUnitsNode->getChildCount(); ++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<unitTypes.size();i++){
if(unitTypes[i].getName()==name){
return &unitTypes[i];
}
}
throw runtime_error("Unit not found: "+name);
}
const UpgradeType *FactionType::getUpgradeType(const string &name) const{
for(int i=0; i<upgradeTypes.size();i++){
if(upgradeTypes[i].getName()==name){
return &upgradeTypes[i];
}
}
throw runtime_error("Upgrade not found: "+name);
}
int FactionType::getStartingResourceAmount(const ResourceType *resourceType) const{
for(int i=0; i<startingResources.size(); ++i){
if(startingResources[i].getType()==resourceType){
return startingResources[i].getAmount();
}
}
return 0;
}
}}//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 "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<string> unitFilenames;
findAll(unitsPath, unitFilenames);
unitTypes.resize(unitFilenames.size());
for(int i=0; i<unitTypes.size(); ++i){
string str= dir + "/units/" + unitFilenames[i];
unitTypes[i].preLoad(str);
}
// a2) preload upgrades
string upgradesPath= dir + "/upgrades/*.";
vector<string> upgradeFilenames;
findAll(upgradesPath, upgradeFilenames);
upgradeTypes.resize(upgradeFilenames.size());
for(int i=0; i<upgradeTypes.size(); ++i){
string str= dir + "/upgrades/" + upgradeFilenames[i];
upgradeTypes[i].preLoad(str);
}
// b1) load units
try{
for(int i=0; i<unitTypes.size(); ++i){
string str= dir + "/units/" + unitTypes[i].getName();
unitTypes[i].load(i, str, techTree, this, checksum);
}
}
catch(const exception &e){
throw runtime_error("Error loading units: "+ dir + "\n" + e.what());
}
// b2) load upgrades
try{
for(int i=0; i<upgradeTypes.size(); ++i){
string str= dir + "/upgrades/" + upgradeTypes[i].getName();
upgradeTypes[i].load(str, techTree, this, checksum);
}
}
catch(const exception &e){
throw runtime_error("Error loading upgrades: "+ dir + "\n" + e.what());
}
//open xml file
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);
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; i<startingResources.size(); ++i){
const XmlNode *resourceNode= startingResourcesNode->getChild("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; i<startingUnitsNode->getChildCount(); ++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<unitTypes.size();i++){
if(unitTypes[i].getName()==name){
return &unitTypes[i];
}
}
throw runtime_error("Unit not found: "+name);
}
const UpgradeType *FactionType::getUpgradeType(const string &name) const{
for(int i=0; i<upgradeTypes.size();i++){
if(upgradeTypes[i].getName()==name){
return &upgradeTypes[i];
}
}
throw runtime_error("Upgrade not found: "+name);
}
int FactionType::getStartingResourceAmount(const ResourceType *resourceType) const{
for(int i=0; i<startingResources.size(); ++i){
if(startingResources[i].getType()==resourceType){
return startingResources[i].getAmount();
}
}
return 0;
}
}}//end namespace

View File

@ -1,145 +1,155 @@
// ==============================================================
// 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";
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
// ==============================================================
// 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

View File

@ -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 <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"
#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<string> &factions, Checksum* checksum){
string str;
vector<string> 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; i<filenames.size(); ++i){
str=dir+"/resources/"+filenames[i];
resourceTypes[i].load(str, checksum);
}
}
catch(const exception &e){
throw runtime_error("Error loading Resource Types: "+ dir + "\n" + e.what());
}
//load tech tree xml info
try{
XmlTree xmlTree;
string path= dir+"/"+lastDir(dir)+".xml";
checksum->addFile(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; i<attackTypes.size(); ++i){
const XmlNode *attackTypeNode= attackTypesNode->getChild("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; i<armorTypes.size(); ++i){
const XmlNode *armorTypeNode= armorTypesNode->getChild("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; i<damageMultipliersNode->getChildCount(); ++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<string>::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; i<factionTypes.size(); ++i){
if(factionTypes[i].getName()==name){
return &factionTypes[i];
}
}
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);
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);
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
// ==============================================================
// 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 <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"
#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<string> &factions, Checksum* checksum){
string str;
vector<string> 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; i<filenames.size(); ++i){
str=dir+"/resources/"+filenames[i];
resourceTypes[i].load(str, checksum);
}
}
catch(const exception &e){
throw runtime_error("Error loading Resource Types: "+ dir + "\n" + e.what());
}
//load tech tree xml info
try{
XmlTree xmlTree;
string path= dir+"/"+lastDir(dir)+".xml";
bool bCanProcessFile = true;
#ifdef _WINDOWS
DWORD fileAttributes = GetFileAttributes(path.c_str());
if( (fileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN)
{
bCanProcessFile = false;
}
#endif
if(bCanProcessFile == true)
{
checksum->addFile(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; i<attackTypes.size(); ++i){
const XmlNode *attackTypeNode= attackTypesNode->getChild("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; i<armorTypes.size(); ++i){
const XmlNode *armorTypeNode= armorTypesNode->getChild("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; i<damageMultipliersNode->getChildCount(); ++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<string>::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; i<factionTypes.size(); ++i){
if(factionTypes[i].getName()==name){
return &factionTypes[i];
}
}
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);
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);
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

File diff suppressed because it is too large Load Diff

View File

@ -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 <algorithm>
#include <cassert>
#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; i<unitRequirementsNode->getChildCount(); ++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; i<upgradeRequirementsNode->getChildCount(); ++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; i<costs.size(); ++i){
const XmlNode *resourceNode= resourceRequirementsNode->getChild("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; i<effectsNode->getChildCount(); ++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; i<getEffectCount(); ++i){
str+= getEffect(i)->getName()+"\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 <algorithm>
#include <cassert>
#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; i<unitRequirementsNode->getChildCount(); ++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; i<upgradeRequirementsNode->getChildCount(); ++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; i<costs.size(); ++i){
const XmlNode *resourceNode= resourceRequirementsNode->getChild("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; i<effectsNode->getChildCount(); ++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; i<getEffectCount(); ++i){
str+= getEffect(i)->getName()+"\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

View File

@ -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
// ==============================================================

View File

@ -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 <cassert>
#include <ctime>
#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; i<surfCount; ++i){
const XmlNode *surfaceNode= surfacesNode->getChild("surface", i);
int childCount= surfaceNode->getChildCount();
surfPixmaps[i].resize(childCount);
surfProbs[i].resize(childCount);
for(int j=0; j<childCount; ++j){
const XmlNode *textureNode= surfaceNode->getChild("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; i<objCount; ++i){
const XmlNode *objectNode= objectsNode->getChild("object", i);
int childCount= objectNode->getChildCount();
objectTypes[i].init(childCount, i, objectNode->getAttribute("walkable")->getBoolValue());
for(int j=0; j<childCount; ++j){
const XmlNode *modelNode= objectNode->getChild("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; i<waterFrameCount; ++i){
const XmlNode *waterFrameNode= waterNode->getChild("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<sunnyProb){
weather= wSunny;
}
else if(rnd<rainyProb){
weather= wRainy;
}
else{
weather= wSnowy;
}
}
//Exception handling (conversions and so on);
catch(const exception &e){
throw runtime_error("Error: " + path + "\n" + e.what());
}
}
Tileset::~Tileset(){
Logger::getInstance().add("Tileset", true);
}
const Pixmap2D *Tileset::getSurfPixmap(int type, int var) const{
int vars= surfPixmaps[type].size();
return &surfPixmaps[type][var % vars];
}
void Tileset::addSurfTex(int leftUp, int rightUp, int leftDown, int rightDown, Vec2f &coord, const Texture2D *&texture){
//center textures
if(leftUp==rightUp && leftUp==leftDown && leftUp==rightDown){
//texture variation according to probability
float r= random.randRange(0.f, 1.f);
int var= 0;
float max= 0.f;
for(int i=0; i<surfProbs[leftUp].size(); ++i){
max+= surfProbs[leftUp][i];
if(r<=max){
var= i;
break;
}
}
SurfaceInfo si(getSurfPixmap(leftUp, var));
surfaceAtlas.addSurface(&si);
coord= si.getCoord();
texture= si.getTexture();
}
//spatted textures
else{
int var= random.randRange(0, transitionVars);
SurfaceInfo si(
getSurfPixmap(leftUp, var),
getSurfPixmap(rightUp, var),
getSurfPixmap(leftDown, var),
getSurfPixmap(rightDown, var));
surfaceAtlas.addSurface(&si);
coord= si.getCoord();
texture= si.getTexture();
}
}
}}// 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 "tileset.h"
#include <cassert>
#include <ctime>
#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; i<surfCount; ++i){
const XmlNode *surfaceNode= surfacesNode->getChild("surface", i);
int childCount= surfaceNode->getChildCount();
surfPixmaps[i].resize(childCount);
surfProbs[i].resize(childCount);
for(int j=0; j<childCount; ++j){
const XmlNode *textureNode= surfaceNode->getChild("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; i<objCount; ++i){
const XmlNode *objectNode= objectsNode->getChild("object", i);
int childCount= objectNode->getChildCount();
objectTypes[i].init(childCount, i, objectNode->getAttribute("walkable")->getBoolValue());
for(int j=0; j<childCount; ++j){
const XmlNode *modelNode= objectNode->getChild("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; i<waterFrameCount; ++i){
const XmlNode *waterFrameNode= waterNode->getChild("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<sunnyProb){
weather= wSunny;
}
else if(rnd<rainyProb){
weather= wRainy;
}
else{
weather= wSnowy;
}
}
//Exception handling (conversions and so on);
catch(const exception &e){
throw runtime_error("Error: " + path + "\n" + e.what());
}
}
Tileset::~Tileset(){
Logger::getInstance().add("Tileset", true);
}
const Pixmap2D *Tileset::getSurfPixmap(int type, int var) const{
int vars= surfPixmaps[type].size();
return &surfPixmaps[type][var % vars];
}
void Tileset::addSurfTex(int leftUp, int rightUp, int leftDown, int rightDown, Vec2f &coord, const Texture2D *&texture){
//center textures
if(leftUp==rightUp && leftUp==leftDown && leftUp==rightDown){
//texture variation according to probability
float r= random.randRange(0.f, 1.f);
int var= 0;
float max= 0.f;
for(int i=0; i<surfProbs[leftUp].size(); ++i){
max+= surfProbs[leftUp][i];
if(r<=max){
var= i;
break;
}
}
SurfaceInfo si(getSurfPixmap(leftUp, var));
surfaceAtlas.addSurface(&si);
coord= si.getCoord();
texture= si.getTexture();
}
//spatted textures
else{
int var= random.randRange(0, transitionVars);
SurfaceInfo si(
getSurfPixmap(leftUp, var),
getSurfPixmap(rightUp, var),
getSurfPixmap(leftDown, var),
getSurfPixmap(rightDown, var));
surfaceAtlas.addSurface(&si);
coord= si.getCoord();
texture= si.getTexture();
}
}
}}// end namespace