Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "TerrainProperties.h" 21 : 22 : #include "graphics/Color.h" 23 : #include "graphics/TerrainTextureManager.h" 24 : #include "lib/path.h" 25 : #include "maths/MathUtil.h" 26 : #include "ps/CLogger.h" 27 : #include "ps/CStr.h" 28 : #include "ps/Errors.h" 29 : #include "ps/Filesystem.h" 30 : #include "ps/XMB/XMBData.h" 31 : #include "ps/XMB/XMBStorage.h" 32 : #include "ps/XML/Xeromyces.h" 33 : 34 : #include <boost/iterator/iterator_categories.hpp> 35 : #include <boost/iterator/iterator_facade.hpp> 36 : #include <boost/token_functions.hpp> 37 : #include <boost/tokenizer.hpp> 38 : #include <cmath> 39 : #include <numbers> 40 : #include <string> 41 : 42 0 : CTerrainProperties::CTerrainProperties(CTerrainPropertiesPtr parent): 43 0 : m_pParent(parent), 44 0 : m_BaseColor(0), 45 0 : m_HasBaseColor(false), 46 0 : m_TextureAngle(std::numbers::pi_v<float> / 4.f), 47 0 : m_TextureSize(32.f) 48 : { 49 0 : if (m_pParent) 50 0 : m_Groups = m_pParent->m_Groups; 51 0 : } 52 : 53 0 : CTerrainPropertiesPtr CTerrainProperties::FromXML(const CTerrainPropertiesPtr& parent, const VfsPath& pathname) 54 : { 55 0 : CXeromyces XeroFile; 56 0 : if (XeroFile.Load(g_VFS, pathname, "terrain") != PSRETURN_OK) 57 0 : return CTerrainPropertiesPtr(); 58 : 59 0 : XMBElement root = XeroFile.GetRoot(); 60 0 : CStr rootName = XeroFile.GetElementString(root.GetNodeName()); 61 : 62 : // Check that we've got the right kind of xml document 63 0 : if (rootName != "Terrains") 64 : { 65 0 : LOGERROR("TerrainProperties: Loading {}: Root node is not terrains (found \"{}\")", 66 : pathname.string8(), 67 : rootName); 68 0 : return CTerrainPropertiesPtr(); 69 : } 70 : 71 : #define ELMT(x) int el_##x = XeroFile.GetElementID(#x) 72 0 : ELMT(terrain); 73 : #undef ELMT 74 : 75 : // Ignore all non-terrain nodes, loading the first terrain node and 76 : // returning it. 77 : // Really, we only expect there to be one child and it to be of the right 78 : // type, though. 79 0 : for (XMBElement child : root.GetChildNodes()) 80 : { 81 0 : if (child.GetNodeName() == el_terrain) 82 : { 83 0 : CTerrainPropertiesPtr ret (new CTerrainProperties(parent)); 84 0 : ret->LoadXml(child, &XeroFile, pathname); 85 0 : return ret; 86 0 : } 87 : else 88 : { 89 0 : LOGWARNING("TerrainProperties: Loading {}: Unexpected node {}\n", 90 : pathname.string8(), 91 : XeroFile.GetElementString(child.GetNodeName())); 92 : // Keep reading - typos shouldn't be showstoppers 93 : } 94 : } 95 : 96 0 : return CTerrainPropertiesPtr(); 97 0 : } 98 : 99 0 : void CTerrainProperties::LoadXml(XMBElement node, CXeromyces *pFile, const VfsPath&) 100 : { 101 : #define ELMT(x) int elmt_##x = pFile->GetElementID(#x) 102 : #define ATTR(x) int attr_##x = pFile->GetAttributeID(#x) 103 : // Terrain Attribs 104 0 : ATTR(mmap); 105 0 : ATTR(groups); 106 0 : ATTR(angle); 107 0 : ATTR(size); 108 : #undef ELMT 109 : #undef ATTR 110 : 111 0 : for (XMBAttribute attr : node.GetAttributes()) 112 : { 113 0 : if (attr.Name == attr_groups) 114 : { 115 : // Parse a comma-separated list of groups, add the new entry to 116 : // each of them 117 0 : m_Groups.clear(); 118 0 : boost::char_separator<char> sep(", "); 119 0 : typedef boost::tokenizer<boost::char_separator<char> > tokenizer; 120 0 : tokenizer tok(attr.Value, sep); 121 0 : for(tokenizer::iterator it = tok.begin(); it != tok.end(); ++it) 122 0 : m_Groups.push_back(g_TexMan.FindGroup(*it)); 123 0 : } 124 0 : else if (attr.Name == attr_mmap) 125 : { 126 0 : CColor col; 127 0 : if (!col.ParseString(attr.Value, 255)) 128 0 : continue; 129 : 130 : // m_BaseColor is BGRA 131 0 : std::uint8_t* baseColor = reinterpret_cast<std::uint8_t*>(&m_BaseColor); 132 0 : baseColor[0] = static_cast<std::uint8_t>(col.b * 255); 133 0 : baseColor[1] = static_cast<std::uint8_t>(col.g * 255); 134 0 : baseColor[2] = static_cast<std::uint8_t>(col.r * 255); 135 0 : baseColor[3] = static_cast<std::uint8_t>(col.a * 255); 136 0 : m_HasBaseColor = true; 137 : } 138 0 : else if (attr.Name == attr_angle) 139 : { 140 0 : m_TextureAngle = DEGTORAD(attr.Value.ToFloat()); 141 : } 142 0 : else if (attr.Name == attr_size) 143 : { 144 0 : m_TextureSize = attr.Value.ToFloat(); 145 : } 146 0 : } 147 0 : } 148 : 149 0 : bool CTerrainProperties::HasBaseColor() 150 : { 151 0 : return m_HasBaseColor || (m_pParent && m_pParent->HasBaseColor()); 152 : } 153 : 154 0 : std::uint32_t CTerrainProperties::GetBaseColor() 155 : { 156 0 : if (m_HasBaseColor || !m_pParent) 157 0 : return m_BaseColor; 158 0 : else if (m_pParent) 159 0 : return m_pParent->GetBaseColor(); 160 : else 161 : // White, full opacity.. but this value shouldn't ever be used 162 : return 0xFFFFFFFF; 163 : }