/* Copyright (C) 2009 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it 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. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include #include #include "simulation/Entity.h" #include "TerrainProperties.h" #include "TextureManager.h" #include "ps/Overlay.h" #include "ps/Parser.h" #include "ps/XML/XeroXMB.h" #include "ps/XML/Xeromyces.h" #include "ps/CLogger.h" #define LOG_CATEGORY L"graphics" CTerrainProperties::CTerrainProperties(CTerrainPropertiesPtr parent): m_pParent(parent), m_BaseColor(0), m_HasBaseColor(false) { if (m_pParent) m_Groups = m_pParent->m_Groups; } CTerrainPropertiesPtr CTerrainProperties::FromXML(const CTerrainPropertiesPtr& parent, const VfsPath& pathname) { CXeromyces XeroFile; if (XeroFile.Load(pathname) != PSRETURN_OK) return CTerrainPropertiesPtr(); XMBElement root = XeroFile.GetRoot(); CStr rootName = XeroFile.GetElementString(root.GetNodeName()); // Check that we've got the right kind of xml document if (rootName != "Terrains") { LOG(CLogger::Error, LOG_CATEGORY, L"TextureManager: Loading %ls: Root node is not terrains (found \"%hs\")", pathname.string().c_str(), rootName.c_str()); return CTerrainPropertiesPtr(); } #define ELMT(x) int el_##x = XeroFile.GetElementID(#x) #define ATTR(x) int at_##x = XeroFile.GetAttributeID(#x) ELMT(terrain); #undef ELMT #undef ATTR // Ignore all non-terrain nodes, loading the first terrain node and // returning it. // Really, we only expect there to be one child and it to be of the right // type, though. XMBElementList children = root.GetChildNodes(); for (int i=0; iLoadXml(child, &XeroFile, pathname); return ret; } else { LOG(CLogger::Warning, LOG_CATEGORY, L"TerrainProperties: Loading %ls: Unexpected node %hs\n", pathname.string().c_str(), XeroFile.GetElementString(child.GetNodeName()).c_str()); // Keep reading - typos shouldn't be showstoppers } } return CTerrainPropertiesPtr(); } void CTerrainProperties::LoadXml(XMBElement node, CXeromyces *pFile, const VfsPath& pathname) { #define ELMT(x) int elmt_##x = pFile->GetElementID(#x) #define ATTR(x) int attr_##x = pFile->GetAttributeID(#x) ELMT(doodad); ELMT(passable); ELMT(impassable); ELMT(event); // Terrain Attribs ATTR(mmap); ATTR(groups); ATTR(properties); // Doodad Attribs ATTR(name); ATTR(max); // Event attribs ATTR(on); #undef ELMT #undef ATTR // stomp on "unused" warnings UNUSED2(attr_name); UNUSED2(attr_on); UNUSED2(attr_max); UNUSED2(elmt_event); UNUSED2(elmt_passable); UNUSED2(elmt_doodad); XMBAttributeList attribs = node.GetAttributes(); for (int i=0;i_$value_"); if (!parserLine.ParseString(parser, CStr(attr.Value))) continue; m_Groups.clear(); for (size_t i=0;iGetAttributeID(#x) // Passable Attribs ATTR(type); ATTR(speed); ATTR(effect); ATTR(prints); #undef ATTR STerrainPassability pass(passable); // Set default speed pass.m_SpeedFactor = 100; bool hasType = false; XMBAttributeList attribs = node.GetAttributes(); for (int i=0;iHasBaseColor()); } u32 CTerrainProperties::GetBaseColor() { if (m_HasBaseColor || !m_pParent) return m_BaseColor; else if (m_pParent) return m_pParent->GetBaseColor(); else // White, full opacity.. but this value shouldn't ever be used return 0xFFFFFFFF; } const STerrainPassability &CTerrainProperties::GetPassability(HEntity entity) { std::vector::iterator it=m_Passabilities.begin(); for (;it != m_Passabilities.end();++it) { if (entity->m_classes.IsMember(it->m_Type)) return *it; } return m_DefaultPassability; } bool CTerrainProperties::IsPassable(HEntity entity) { return GetPassability(entity).m_Passable; } double CTerrainProperties::GetSpeedFactor(HEntity entity) { return GetPassability(entity).m_SpeedFactor; }