2011-01-20 16:56:30 +01:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
|
|
|
// Copyright (C) 2001-2008 Martio Figueroa
|
|
|
|
//
|
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#include "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
|
|
|
|
// =====================================================
|
|
|
|
|
|
|
|
ResourceType::ResourceType() {
|
|
|
|
resourceClass=rcTech;
|
|
|
|
tilesetObject=0;
|
|
|
|
resourceNumber=0;
|
|
|
|
interval=0;
|
|
|
|
defResPerPatch=0;
|
|
|
|
recoup_cost = false;
|
|
|
|
model = NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-09 02:32:27 +01:00
|
|
|
ResourceType::~ResourceType(){
|
|
|
|
while(!(particleTypes.empty())){
|
|
|
|
delete particleTypes.back();
|
|
|
|
particleTypes.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-15 16:30:28 +01:00
|
|
|
void ResourceType::load(const string &dir, Checksum* checksum, Checksum *techtreeChecksum,
|
2011-05-06 09:47:31 +02:00
|
|
|
std::map<string,vector<pair<string, string> > > &loadedFileList) {
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
string path, str;
|
|
|
|
Renderer &renderer= Renderer::getInstance();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
recoup_cost = true;
|
|
|
|
|
|
|
|
name= lastDir(dir);
|
|
|
|
|
|
|
|
Logger::getInstance().add("Resource type: "+ formatString(name), true);
|
2011-03-13 09:23:43 +01:00
|
|
|
string currentPath = dir;
|
|
|
|
endPathWithSlash(currentPath);
|
|
|
|
path= currentPath + name + ".xml";
|
2011-05-05 22:18:38 +02:00
|
|
|
string sourceXMLFile = path;
|
2011-01-09 05:49:21 +01:00
|
|
|
checksum->addFile(path);
|
2011-01-20 16:56:30 +01:00
|
|
|
techtreeChecksum->addFile(path);
|
|
|
|
|
|
|
|
//tree
|
|
|
|
XmlTree xmlTree;
|
|
|
|
xmlTree.load(path);
|
2011-05-06 09:47:31 +02:00
|
|
|
loadedFileList[path].push_back(make_pair(currentPath,currentPath));
|
2011-03-15 16:30:28 +01:00
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
const XmlNode *resourceNode= xmlTree.getRootNode();
|
|
|
|
|
|
|
|
//image
|
|
|
|
const XmlNode *imageNode= resourceNode->getChild("image");
|
|
|
|
image= renderer.newTexture2D(rsGame);
|
2011-05-05 09:15:12 +02:00
|
|
|
image->load(imageNode->getAttribute("path")->getRestrictedValue(currentPath));
|
2011-05-06 09:47:31 +02:00
|
|
|
loadedFileList[imageNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(sourceXMLFile,imageNode->getAttribute("path")->getRestrictedValue()));
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
//type
|
|
|
|
const XmlNode *typeNode= resourceNode->getChild("type");
|
2011-03-15 16:30:28 +01:00
|
|
|
resourceClass = strToRc(typeNode->getAttribute("value")->getRestrictedValue());
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
switch(resourceClass)
|
|
|
|
{
|
|
|
|
case rcTech:
|
|
|
|
{
|
|
|
|
//model
|
|
|
|
const XmlNode *modelNode= typeNode->getChild("model");
|
2011-05-05 09:15:12 +02:00
|
|
|
string modelPath= modelNode->getAttribute("path")->getRestrictedValue(currentPath);
|
2011-01-20 16:56:30 +01:00
|
|
|
|
|
|
|
model= renderer.newModel(rsGame);
|
2011-05-05 22:18:38 +02:00
|
|
|
model->load(modelPath, false, &loadedFileList, &sourceXMLFile);
|
2011-05-06 09:47:31 +02:00
|
|
|
loadedFileList[modelPath].push_back(make_pair(sourceXMLFile,modelNode->getAttribute("path")->getRestrictedValue()));
|
2011-01-20 16:56:30 +01:00
|
|
|
|
2011-03-09 02:32:27 +01:00
|
|
|
if(modelNode->hasChild("particles")){
|
|
|
|
const XmlNode *particleNode= modelNode->getChild("particles");
|
|
|
|
bool particleEnabled= particleNode->getAttribute("value")->getBoolValue();
|
2011-03-15 16:30:28 +01:00
|
|
|
if(particleEnabled == true) {
|
|
|
|
for(int k= 0; k < particleNode->getChildCount(); ++k) {
|
2011-03-09 02:32:27 +01:00
|
|
|
const XmlNode *particleFileNode= particleNode->getChild("particle-file", k);
|
2011-03-15 16:30:28 +01:00
|
|
|
string particlePath= particleFileNode->getAttribute("path")->getRestrictedValue();
|
2011-03-09 02:32:27 +01:00
|
|
|
|
|
|
|
ObjectParticleSystemType *objectParticleSystemType= new ObjectParticleSystemType();
|
2011-03-15 16:30:28 +01:00
|
|
|
objectParticleSystemType->load(dir, currentPath + particlePath,
|
2011-05-05 22:18:38 +02:00
|
|
|
&Renderer::getInstance(), loadedFileList, sourceXMLFile);
|
2011-05-06 09:47:31 +02:00
|
|
|
loadedFileList[currentPath + particlePath].push_back(make_pair(sourceXMLFile,particleFileNode->getAttribute("path")->getRestrictedValue()));
|
2011-03-15 16:30:28 +01:00
|
|
|
|
2011-03-09 02:32:27 +01:00
|
|
|
particleTypes.push_back(objectParticleSystemType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-20 16:56:30 +01:00
|
|
|
//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
|
2011-03-15 16:30:28 +01:00
|
|
|
if(typeNode->hasChild("recoup_cost") == true) {
|
2011-01-20 16:56:30 +01:00
|
|
|
const XmlNode *recoup_costNode= typeNode->getChild("recoup_cost");
|
2011-03-15 16:30:28 +01:00
|
|
|
if(recoup_costNode != NULL) {
|
2011-01-20 16:56:30 +01:00
|
|
|
recoup_cost= recoup_costNode->getAttribute("value")->getBoolValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const exception &e){
|
|
|
|
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what());
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceType::deletePixels() {
|
|
|
|
if(model != NULL) {
|
|
|
|
model->deletePixels();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}}//end namespace
|