2005-08-07 23:58:36 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-11-12 05:02:36 +01:00
|
|
|
#include <vector>
|
2005-08-07 23:58:36 +02:00
|
|
|
#include <string>
|
|
|
|
|
2006-11-12 05:02:36 +01:00
|
|
|
#include "simulation/Entity.h"
|
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
#include "TerrainProperties.h"
|
|
|
|
#include "TextureManager.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Overlay.h"
|
2005-08-07 23:58:36 +02:00
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Parser.h"
|
2005-08-15 01:50:37 +02:00
|
|
|
#include "ps/XML/XeroXMB.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2005-08-07 23:58:36 +02:00
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/CLogger.h"
|
2005-08-07 23:58:36 +02:00
|
|
|
#define LOG_CATEGORY "graphics"
|
|
|
|
|
2006-11-12 05:02:36 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2005-10-28 03:43:16 +02:00
|
|
|
CTerrainProperties::CTerrainProperties(CTerrainPropertiesPtr parent):
|
2005-08-07 23:58:36 +02:00
|
|
|
m_pParent(parent),
|
|
|
|
m_BaseColor(0),
|
|
|
|
m_HasBaseColor(false)
|
|
|
|
{
|
|
|
|
if (m_pParent)
|
|
|
|
m_Groups = m_pParent->m_Groups;
|
|
|
|
}
|
|
|
|
|
2005-10-28 03:43:16 +02:00
|
|
|
CTerrainPropertiesPtr CTerrainProperties::FromXML(CTerrainPropertiesPtr parent, const char* path)
|
2005-08-07 23:58:36 +02:00
|
|
|
{
|
|
|
|
CXeromyces XeroFile;
|
|
|
|
if (XeroFile.Load(path) != PSRETURN_OK)
|
2005-10-28 03:43:16 +02:00
|
|
|
return CTerrainPropertiesPtr();
|
2005-08-07 23:58:36 +02:00
|
|
|
|
|
|
|
XMBElement root = XeroFile.getRoot();
|
|
|
|
CStr rootName = XeroFile.getElementString(root.getNodeName());
|
|
|
|
|
|
|
|
// Check that we've got the right kind of xml document
|
# Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions.
- Modified Xeromyces to no longer automatically convert element and
attribute names to lowercase, so that we can have camelCase names. We
should now be able to convert all the multi-word entity properties, like
pass_through_allies, to camelCase, like passThroughAllies, which is more
consistent with the rest of our JavaScript naming conventions. To
support the existing code that assumes lowercase element names, I made
the getElementID and getAttributeID methods (used in the EL and AT
macros) ignore case, and I changed any code that directly accessed
element names to use the right case. CEntityTemplate now converts
Names_LikeThis to names_likeThis (changing each separate "word" in the
name to camelCase). Changed the version letter in XMB filenames from A
to B to support this without requiring people to delete old XMBs.
- Enhanced the pathfinder's handling of contact paths, resulting in a
very large speedup for actions like attacking, construction, etc. The
problem was that the pathfinder used to not count a given state as the
goal unless it was exactly coincident with the target location. This is
fine when you order a unit to go exactly to a certain spot, but if
you're ordering a unit to build, gather or attack something, then the
target tile is impassable (because your target is there) and therefore
the pathfinder never declares a state final. As a result, the pathfinder
tries hundreds of extra tiles in case there is a long path that gets to
the goal, and after failing to find any path that reaches the goal, it
gives you one to the closest point it got to. To fix it, I made the
pathfinder take into account a radius around the goal in which it's OK
to be, which depends on the size of the target unit and the range of
your action.
This was SVN commit r4186.
2006-08-01 05:41:21 +02:00
|
|
|
if (rootName != "Terrains")
|
2005-08-07 23:58:36 +02:00
|
|
|
{
|
|
|
|
LOG(ERROR,
|
|
|
|
LOG_CATEGORY,
|
|
|
|
"TextureManager: Loading %s: Root node is not terrains (found \"%s\")",
|
2005-10-20 00:39:54 +02:00
|
|
|
path,
|
2005-08-07 23:58:36 +02:00
|
|
|
rootName.c_str());
|
2005-10-28 03:43:16 +02:00
|
|
|
return CTerrainPropertiesPtr();
|
2005-08-07 23:58:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#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; i<children.Count; ++i)
|
|
|
|
{
|
|
|
|
//debug_printf("Object %d\n", i);
|
|
|
|
XMBElement child = children.item(i);
|
|
|
|
|
|
|
|
if (child.getNodeName() == el_terrain)
|
|
|
|
{
|
2005-10-28 03:43:16 +02:00
|
|
|
CTerrainPropertiesPtr ret (new CTerrainProperties(parent));
|
2006-11-12 05:02:36 +01:00
|
|
|
ret->LoadXML(child, &XeroFile, path);
|
2005-08-07 23:58:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG(WARNING, LOG_CATEGORY,
|
|
|
|
"TerrainProperties: Loading %s: Unexpected node %s\n",
|
2005-10-20 00:39:54 +02:00
|
|
|
path,
|
2005-08-07 23:58:36 +02:00
|
|
|
XeroFile.getElementString(child.getNodeName()).c_str());
|
|
|
|
// Keep reading - typos shouldn't be showstoppers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-28 03:43:16 +02:00
|
|
|
return CTerrainPropertiesPtr();
|
2005-08-07 23:58:36 +02:00
|
|
|
}
|
|
|
|
|
2006-11-12 05:02:36 +01:00
|
|
|
void CTerrainProperties::LoadXML(XMBElement node, CXeromyces *pFile, const char *path)
|
2005-08-07 23:58:36 +02:00
|
|
|
{
|
|
|
|
#define ELMT(x) int elmt_##x = pFile->getElementID(#x)
|
|
|
|
#define ATTR(x) int attr_##x = pFile->getAttributeID(#x)
|
|
|
|
ELMT(doodad);
|
|
|
|
ELMT(passable);
|
2006-11-12 05:02:36 +01:00
|
|
|
ELMT(impassable);
|
2005-08-07 23:58:36 +02:00
|
|
|
ELMT(event);
|
|
|
|
// Terrain Attribs
|
|
|
|
ATTR(mmap);
|
|
|
|
ATTR(groups);
|
|
|
|
ATTR(properties);
|
|
|
|
// Doodad Attribs
|
|
|
|
ATTR(name);
|
|
|
|
ATTR(max);
|
|
|
|
// Event attribs
|
|
|
|
ATTR(on);
|
|
|
|
#undef ELMT
|
|
|
|
#undef ATTR
|
2005-10-31 17:26:51 +01:00
|
|
|
|
|
|
|
// stomp on "unused" warnings
|
|
|
|
UNUSED2(attr_name);
|
|
|
|
UNUSED2(attr_on);
|
|
|
|
UNUSED2(attr_max);
|
|
|
|
UNUSED2(elmt_event);
|
|
|
|
UNUSED2(elmt_passable);
|
|
|
|
UNUSED2(elmt_doodad);
|
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
XMBAttributeList attribs = node.getAttributes();
|
|
|
|
for (int i=0;i<attribs.Count;i++)
|
|
|
|
{
|
|
|
|
XMBAttribute attr = attribs.item(i);
|
|
|
|
|
|
|
|
if (attr.Name == attr_groups)
|
|
|
|
{
|
|
|
|
// Parse a comma-separated list of groups, add the new entry to
|
|
|
|
// each of them
|
|
|
|
CParser parser;
|
|
|
|
CParserLine parserLine;
|
|
|
|
parser.InputTaskType("GroupList", "<_$value_,>_$value_");
|
|
|
|
|
|
|
|
if (!parserLine.ParseString(parser, (CStr)attr.Value))
|
|
|
|
continue;
|
|
|
|
m_Groups.clear();
|
|
|
|
for (size_t i=0;i<parserLine.GetArgCount();i++)
|
|
|
|
{
|
2006-09-26 03:44:20 +02:00
|
|
|
std::string value;
|
2005-08-07 23:58:36 +02:00
|
|
|
if (!parserLine.GetArgString(i, value))
|
|
|
|
continue;
|
|
|
|
CTerrainGroup *pType = g_TexMan.FindGroup(value);
|
|
|
|
m_Groups.push_back(pType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (attr.Name == attr_mmap)
|
|
|
|
{
|
|
|
|
CColor col;
|
|
|
|
if (!col.ParseString((CStr)attr.Value, 255))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// m_BaseColor is BGRA
|
|
|
|
u8 *baseColor = (u8*)&m_BaseColor;
|
|
|
|
baseColor[0] = (u8)(col.b*255);
|
|
|
|
baseColor[1] = (u8)(col.g*255);
|
|
|
|
baseColor[2] = (u8)(col.r*255);
|
|
|
|
baseColor[3] = (u8)(col.a*255);
|
|
|
|
m_HasBaseColor = true;
|
|
|
|
}
|
|
|
|
else if (attr.Name == attr_properties)
|
|
|
|
{
|
|
|
|
// TODO Parse a list of properties and store them somewhere
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-12 05:02:36 +01:00
|
|
|
XMBElementList children = node.getChildNodes();
|
|
|
|
for (int i=0; i<children.Count; ++i)
|
|
|
|
{
|
|
|
|
XMBElement child = children.item(i);
|
|
|
|
|
|
|
|
if (child.getNodeName() == elmt_passable)
|
|
|
|
{
|
|
|
|
ReadPassability(true, child, pFile, path);
|
|
|
|
}
|
|
|
|
else if (child.getNodeName() == elmt_impassable)
|
|
|
|
{
|
|
|
|
ReadPassability(false, child, pFile, path);
|
|
|
|
}
|
|
|
|
// TODO Parse information about doodads and events and store it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerrainProperties::ReadPassability(bool passable, XMBElement node, CXeromyces *pFile, const char *UNUSED(path))
|
|
|
|
{
|
|
|
|
#define ATTR(x) int attr_##x = pFile->getAttributeID(#x)
|
|
|
|
// Passable Attribs
|
|
|
|
ATTR(type);
|
|
|
|
ATTR(speed);
|
|
|
|
ATTR(effect);
|
|
|
|
ATTR(prints);
|
|
|
|
#undef ATTR
|
|
|
|
|
|
|
|
STerrainPassability pass(passable);
|
2006-12-01 21:34:28 +01:00
|
|
|
bool hasType = false;
|
2006-11-12 05:02:36 +01:00
|
|
|
bool hasSpeed;
|
|
|
|
XMBAttributeList attribs = node.getAttributes();
|
|
|
|
for (int i=0;i<attribs.Count;i++)
|
|
|
|
{
|
|
|
|
XMBAttribute attr = attribs.item(i);
|
|
|
|
|
|
|
|
if (attr.Name == attr_type)
|
|
|
|
{
|
|
|
|
// FIXME Should handle lists of types as well!
|
|
|
|
pass.m_Type = attr.Value;
|
|
|
|
hasType = true;
|
|
|
|
}
|
|
|
|
else if (attr.Name == attr_speed)
|
|
|
|
{
|
|
|
|
CStr val=attr.Value;
|
|
|
|
CStr trimmedVal=val.Trim(PS_TRIM_BOTH);
|
|
|
|
pass.m_SpeedFactor = trimmedVal.ToDouble();
|
|
|
|
if (trimmedVal[trimmedVal.size()-1] == '%')
|
|
|
|
{
|
|
|
|
pass.m_SpeedFactor /= 100.0;
|
|
|
|
}
|
2006-11-12 20:55:10 +01:00
|
|
|
// FIXME speed=0 could/should be made to set the terrain impassable
|
2006-11-12 05:02:36 +01:00
|
|
|
hasSpeed = true;
|
|
|
|
}
|
|
|
|
else if (attr.Name == attr_effect)
|
|
|
|
{
|
|
|
|
// TODO Parse and store list of effects
|
|
|
|
}
|
|
|
|
else if (attr.Name == attr_prints)
|
|
|
|
{
|
|
|
|
// TODO Parse and store footprint effect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasType)
|
|
|
|
{
|
|
|
|
m_DefaultPassability = pass;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Passabilities.push_back(pass);
|
|
|
|
}
|
2005-08-07 23:58:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CTerrainProperties::HasBaseColor()
|
|
|
|
{
|
|
|
|
return m_HasBaseColor || (m_pParent && m_pParent->HasBaseColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2007-01-01 22:25:47 +01:00
|
|
|
return 0xFFFFFFFF;
|
2005-08-07 23:58:36 +02:00
|
|
|
}
|
2006-11-12 05:02:36 +01:00
|
|
|
|
|
|
|
const STerrainPassability &CTerrainProperties::GetPassability(HEntity entity)
|
|
|
|
{
|
|
|
|
vector<STerrainPassability>::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;
|
|
|
|
}
|