All pastes #177140 Raw Edit

Stuff

public text v1 · immutable
#177140 ·published 2006-09-20 06:22 UTC
rendered paste body
#include <windows.h>
#include <stdio.h>

typedef struct _MAPHEAD
{
    char szMapID[9];
    char szPortalCount[2];
    char szMapName[1];
} *pMapHead, tMapHead;

typedef struct _PORTALHEAD
{
    char szX[10];
    char szY[10];
    char szDestMap[9];
} *pPortalHead, tPortalHead;

typedef struct _MAP
{
    char szMapName[100];
    int nMapID;
    int mapLinks[20];    // 20 max portals on a map (says me)
    bool bSearched;
} *pMap, tMap;
    
tMap LoadedMaps[100];    // this should expand to the total number of maps once they're all added in

void LoadMapList(char *szFile);
void OutputLinks(char *szFile);
void RecursivePrintLinks(FILE *fOut, int nMapID, int nThisDepth);
bool RecursiveSearch(FILE *fOut, int nCurrent, int nTarget);
void SolvePath(char *szFile, int nIDStart, int nIDEnd);
char *GetMapName(int nMapID);

char szFailed[] = "Couldn\'t locate";

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Syntax Error\n  Usage: mparse.exe [file]\n\n");
        return 1;
    }

    printf("Map Data Parser for iBot\n  %s\n\n", argv[1]);

    ZeroMemory(LoadedMaps, sizeof(tMap) * 100);
    LoadMapList(argv[1]);

    OutputLinks("output.txt");
    SolvePath("path.txt", 105040300, 105090301);

    system("pause");

    return 0;
}

void SolvePath(char *szFile, int nIDStart, int nIDEnd)
{
    FILE *fOut = fopen(szFile, "w");

    for (int i = 0; i < 100; i++)
        LoadedMaps[i].bSearched = false;

    if (RecursiveSearch(fOut, nIDEnd, nIDStart))
        printf("Path %s -> %s solved!\n", GetMapName(nIDStart), GetMapName(nIDEnd));
    else
        printf("Path %s -> %s couldn't be solved\n", GetMapName(nIDStart), GetMapName(nIDEnd));

    fclose(fOut);
}

char *GetMapName(int nMapID)
{
    int nSelMap = 0;
    int i = 0;

    while (nSelMap = LoadedMaps[i++].nMapID)
    {
        if (nMapID == nSelMap)
            return LoadedMaps[i-1].szMapName;
    }

    return szFailed;
}

bool RecursiveSearch(FILE *fOut, int nCurrent, int nTarget)
{
    int i = 0;
    int nSelMap = 0;
    int nSelLink = 0;
    bool bFound;

    while (nSelMap = LoadedMaps[i++].nMapID)
    {
        if (nSelMap == nCurrent)
        {
            i--;
            bFound = true;
            
            //check if the map has already been searched
            if (!LoadedMaps[i].bSearched)
            {
                LoadedMaps[i].bSearched = true;

                for (int z = 0; ((z < 20) && (nSelLink = LoadedMaps[i].mapLinks[z])); z++)
                {
                    if ((nCurrent == nTarget) || (RecursiveSearch(fOut, nSelLink, nTarget)))
                    {
                        fprintf(fOut, "->%s", GetMapName(nCurrent));
                        return true;
                    }
                }
            }

            break;
        }
    }

    if (!bFound)
        fprintf(fOut, "Map ID %i not found!\n", nCurrent);

    return false;
}

void OutputLinks(char *szFile)
{
    FILE *fOut = fopen(szFile, "w");

    for (int i = 0; i < 100; i++)
        LoadedMaps[i].bSearched = false;

    RecursivePrintLinks(fOut, LoadedMaps[0].nMapID, 0);

    fclose(fOut);
}

void RecursivePrintLinks(FILE *fOut, int nMapID, int nThisDepth)
{
    int i = 0;
    int nSelMap = 0;
    int nSelLink = 0;
    bool bFound = false;

    i = 0;

    //find the map
    while (nSelMap = LoadedMaps[i++].nMapID)
    {
        if (nSelMap == nMapID)
        {
            bFound = true;
            
            //check if the map has already been listed
            if (!LoadedMaps[i-1].bSearched)
            {
                //print indentation
                for (int nIndent = 0; nIndent < nThisDepth; nIndent++)
                    fprintf(fOut, " ");

                fprintf(fOut, "%s\n", LoadedMaps[i-1].szMapName);

                LoadedMaps[i-1].bSearched = true;
                for (int z = 0; ((z < 20) && (nSelLink = LoadedMaps[i-1].mapLinks[z])); z++)
                {
                    RecursivePrintLinks(fOut, nSelLink, nThisDepth+1);
                }
            }

            break;
        }
    }

    if (!bFound)
        fprintf(fOut, "Map ID %i not found!\n", nMapID);
        
    return;
}

void LoadMapList(char *szFile)
{
    char *szBuffer = NULL;
    char *szTemp = NULL;
    char *szCurLine = NULL;

    FILE *fData = fopen(szFile, "r");

    if (fData == NULL)
    {
        printf("There was a problem opening the file \'%s\'\n", szFile);
        return;
    }

    // read the file into memory
    szBuffer = szTemp = new char [1024 * 1024]; //1mb of memory

    ZeroMemory(szBuffer, 1024*1024);

    while (!feof(fData))
        fread(szTemp++, 1, 1, fData);

    fclose(fData);
    
    // cycle through the lines
    szCurLine = strtok(szBuffer, "\n\0");

    bool bInMapDef = false;
    
    pMapHead pHeader;
    pPortalHead pPortal;

    pMap pCurMap;

    int nPortals = 0;
    int nCurPortal = 0;
    int nCurMap = 0;

    char szMapID[10];
    char szPortalCount[3];

    char szX[11];
    char szY[11];
    char szDestMap[10];

    // load all data into memory

    while (szCurLine)
    {
        if (strncmp(szCurLine, ";", 1) && (strlen(szCurLine) > 16))
        {
            if (!bInMapDef)
            {
                pCurMap = &LoadedMaps[nCurMap];

                ZeroMemory(szMapID, 10);
                ZeroMemory(szPortalCount, 3);

                pHeader = (pMapHead)szCurLine;
                strncpy(pCurMap->szMapName, pHeader->szMapName, 99);
                strncpy(szMapID, pHeader->szMapID, 9);
                pCurMap->nMapID = atoi(szMapID);
                
                strncpy(szPortalCount, pHeader->szPortalCount, 2);
                
                printf("\"%s\" has %s portals\n", pCurMap->szMapName, szPortalCount);

                nPortals = atoi(szPortalCount);
                bInMapDef = (nPortals > 0);
                nCurPortal = 0;
                nCurMap++;
            } else {
                nPortals--;

                ZeroMemory(szX, 11);
                ZeroMemory(szY, 11);
                ZeroMemory(szDestMap, 10);

                pPortal = (pPortalHead)szCurLine;
                //strncpy(szX, pPortal->szX, 10);
                //strncpy(szY, pPortal->szY, 10);
                strncpy(szDestMap, pPortal->szDestMap, 9);

                pCurMap->mapLinks[nCurPortal] = atoi(szDestMap);

                nCurPortal++;
                //printf("  Portal %i at %i, %i leads to map %09i\n",
                //    nCurPortal, atoi(szX), atoi(szY), atoi(szDestMap));

                if (nPortals <= 0)
                    bInMapDef = false;
            }
        }

        szCurLine = strtok(NULL, "\n\0");
    }

    printf("\nProcessed %i maps\n", nCurMap);

    delete [] szBuffer;
}