summaryrefslogtreecommitdiff
path: root/src/map.cpp
blob: 17f9bd83464d571d3279d94517a859ac3e95a96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "map.h"
#include "cell.h"
#include "position.h"

#include <stdio.h>
#include <string.h>
#include <exception>

Map::Map( const Position &rpMax ) :
    iDims( rpMax.getDims() ),
    aiSize( 0 ),
    acMap( 0 )
{
    int iTotal = 1;
    aiSize = new int[iDims];
    for( int j = 0; j < iDims; j++ )
    {
        aiSize[j] = rpMax[j];
        iTotal *= aiSize[j];
    }
    acMap = new Cell[iTotal];
}

Map::~Map()
{
    delete[] aiSize;
    delete[] acMap;
}

Cell &Map::operator[]( const Position &rpIdx ) const
{
    return acMap[getIndex( rpIdx )];
}

Cell &Map::operator[]( const Position &rpIdx )
{
    return acMap[getIndex( rpIdx )];
}

int Map::getDims() const
{
    return iDims;
}

int Map::getSize( int iDim ) const
{
    return aiSize[iDim];
}

int Map::getIndex( const Position &rpIdx ) const
{
    if( !isInside( rpIdx ) )
        throw std::exception();

    int iIdx = 0;
    int iScale = 1;
    for( int j = 0; j < iDims; j++ )
    {
        iIdx += rpIdx[j]*iScale;
        iScale *= aiSize[j];
    }
    return iIdx;
}

bool Map::isInside( const Position &rpIdx ) const
{
    if( rpIdx.getDims() != iDims )
        throw std::exception();

    for( int j = 0; j < iDims; j++ )
    {
        if( rpIdx[j] < 0 )
            return false;
        if( rpIdx[j] >= aiSize[j] )
            return false;
    }

    return true;
}

int opposite( int iDir );

void Map::connect( int iId1, int iId2 )
{
    Position p( iDims );

    Position pMax1, pMax2;
    int iDistMax = 0;
    int iDirMax = 0;

    int iDim;
    for(;;)
    {
        Cell &c = (*this)[p];
        if( c.iPath == iId1 || c.iPath == iId2 )
        {
            // Could be the thing!
            for( iDim = 0; iDim < iDims; iDim++ )
            {
                Position t( p );
                t[iDim]--;
                for( int iDir = 0; iDir < 2; iDir++ )
                {
//                    int w = (1<<(iDim*2+iDir));
                    if( t[iDim] >= 0 && t[iDim] < aiSize[iDim] )
                    {
                        Cell &c2 = (*this)[t];
                        if( c.iPath != c2.iPath &&
                            (c2.iPath == iId1 || c2.iPath == iId2) )
                        {
                            int iDist = c.iDist+c2.iDist;
                            if( iDist > iDistMax )
                            {
                                iDistMax = iDist;
                                pMax1 = p;
                                pMax2 = t;
                                iDirMax = iDim*2+iDir;
                            }
                        }
                    }
                    t[iDim] += 2;
                }
            }
        }

        for( iDim = 0; iDim < iDims; iDim++ )
        {
            if( ++p[iDim] < aiSize[iDim] )
                break;
            p[iDim] = 0;
        }
        if( iDim == iDims )
            break;
    }

    (*this)[pMax1].iWalls |= (1<<iDirMax);
    (*this)[pMax2].iWalls |= (1<<opposite(iDirMax));
//    printf("iDistMax: %d, iDirMax: %d\n", iDistMax, iDirMax );
}