ddnet/src/tools/map_find_env.cpp

138 lines
3.7 KiB
C++
Raw Normal View History

2022-08-13 16:53:56 +00:00
#include <base/logger.h>
#include <base/system.h>
#include <engine/shared/datafile.h>
#include <engine/storage.h>
#include <game/mapitems.h>
struct EnvelopedQuad
{
int m_GroupId;
int m_LayerId;
2022-08-13 16:53:56 +00:00
int m_TilePosX;
int m_TilePosY;
};
bool OpenMap(const char pMapName[64], CDataFileReader &InputMap)
{
IStorage *pStorage = CreateLocalStorage();
if(!InputMap.Open(pStorage, pMapName, IStorage::TYPE_ABSOLUTE))
{
dbg_msg("map_find_env", "ERROR: unable to open map '%s'", pMapName);
return false;
}
return true;
}
bool GetLayerGroupIds(CDataFileReader &InputMap, const int LayerNumber, int &GroupId, int &LayerRelativeId)
2022-08-13 16:53:56 +00:00
{
int Start, Num;
InputMap.GetType(MAPITEMTYPE_GROUP, &Start, &Num);
for(int i = 0; i < Num; i++)
{
CMapItemGroup *pItem = (CMapItemGroup *)InputMap.GetItem(Start + i);
2022-08-13 16:53:56 +00:00
if(LayerNumber >= pItem->m_StartLayer && LayerNumber <= pItem->m_StartLayer + pItem->m_NumLayers)
{
GroupId = i;
LayerRelativeId = LayerNumber - pItem->m_StartLayer - 1;
2022-08-13 16:53:56 +00:00
return true;
}
}
return false;
}
int FxToTilePos(const int FxPos)
{
return std::floor(fx2f(FxPos) / 32);
2022-08-13 16:53:56 +00:00
}
bool GetEnvelopedQuads(const CQuad *pQuads, const int NumQuads, const int EnvId, const int GroupId, const int LayerId, int &QuadsCounter, EnvelopedQuad pEnvQuads[1024])
2022-08-13 16:53:56 +00:00
{
bool bFound = false;
for(int i = 0; i < NumQuads; i++)
{
if(pQuads[i].m_PosEnv != EnvId && pQuads[i].m_ColorEnv != EnvId)
2022-08-13 16:53:56 +00:00
continue;
pEnvQuads[QuadsCounter].m_GroupId = GroupId;
pEnvQuads[QuadsCounter].m_LayerId = LayerId;
2022-08-13 16:53:56 +00:00
pEnvQuads[QuadsCounter].m_TilePosX = FxToTilePos(pQuads[i].m_aPoints[4].x);
pEnvQuads[QuadsCounter].m_TilePosY = FxToTilePos(pQuads[i].m_aPoints[4].y);
QuadsCounter++;
bFound = true;
}
return bFound;
}
void PrintEnvelopedQuads(const EnvelopedQuad pEnvQuads[1024], const int EnvId, const int QuadsCounter)
2022-08-13 16:53:56 +00:00
{
if(!QuadsCounter)
{
dbg_msg("map_find_env", "No quads found with env number #%d", EnvId + 1);
2022-08-13 16:53:56 +00:00
return;
}
dbg_msg("map_find_env", "Found %d quads with env number #%d:", QuadsCounter, EnvId + 1);
2022-08-13 16:53:56 +00:00
for(int i = 0; i < QuadsCounter; i++)
dbg_msg("map_find_env", "%*d. Group: #%d - Layer: #%d - Pos: %d,%d", (int)(std::log10(absolute(QuadsCounter))) + 1, i + 1, pEnvQuads[i].m_GroupId, pEnvQuads[i].m_LayerId, pEnvQuads[i].m_TilePosX, pEnvQuads[i].m_TilePosY);
2022-08-13 16:53:56 +00:00
}
bool FindEnv(const char aFilename[64], const int EnvId)
2022-08-13 16:53:56 +00:00
{
CDataFileReader InputMap;
if(!OpenMap(aFilename, InputMap))
return false;
int LayersStart, LayersCount, QuadsCounter = 0;
InputMap.GetType(MAPITEMTYPE_LAYER, &LayersStart, &LayersCount);
EnvelopedQuad pEnvQuads[1024];
for(int i = 0; i < LayersCount; i++)
{
CMapItemLayer *pItem;
pItem = (CMapItemLayer *)InputMap.GetItem(LayersStart + i);
2022-08-13 16:53:56 +00:00
if(pItem->m_Type != LAYERTYPE_QUADS)
continue;
CMapItemLayerQuads *pQuadLayer = (CMapItemLayerQuads *)pItem;
CQuad *pQuads = (CQuad *)InputMap.GetDataSwapped(pQuadLayer->m_Data);
int GroupId = 0, LayerRelativeId = 0;
if(!GetLayerGroupIds(InputMap, i + 1, GroupId, LayerRelativeId))
2022-08-13 16:53:56 +00:00
return false;
GetEnvelopedQuads(pQuads, pQuadLayer->m_NumQuads, EnvId, GroupId, LayerRelativeId, QuadsCounter, pEnvQuads);
2022-08-13 16:53:56 +00:00
}
PrintEnvelopedQuads(pEnvQuads, EnvId, QuadsCounter);
2022-08-13 16:53:56 +00:00
return true;
}
int main(int argc, const char **argv)
{
CCmdlineFix CmdlineFix(&argc, &argv);
log_set_global_logger_default();
if(argc < 3)
{
dbg_msg("map_find_env", "Invalid arguments");
dbg_msg("map_find_env", "Usage: %s <input_map> <env_number>", argv[0]);
dbg_msg("map_find_env", "Note: returned quads positions are relative to their layers");
return -1;
}
char aFilename[64];
str_copy(aFilename, argv[1]);
int EnvId = str_toint(argv[2]) - 1;
dbg_msg("map_find_env", "input_map='%s'; env_number='#%d';", aFilename, EnvId + 1);
2022-08-13 16:53:56 +00:00
return FindEnv(aFilename, EnvId);
2022-08-13 16:53:56 +00:00
}