diff options
author | Mike Buland <mike@xagasoft.com> | 2015-04-02 15:28:31 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2015-04-02 15:28:31 -0600 |
commit | 518619603ab3c49b7fdfcf19c439c1a30668164f (patch) | |
tree | dda7774f51bde150db91dac14718cc78f7571039 /src/map.cpp | |
download | lost-518619603ab3c49b7fdfcf19c439c1a30668164f.tar.gz lost-518619603ab3c49b7fdfcf19c439c1a30668164f.tar.bz2 lost-518619603ab3c49b7fdfcf19c439c1a30668164f.tar.xz lost-518619603ab3c49b7fdfcf19c439c1a30668164f.zip |
Everything works, it could use more stuff.
Diffstat (limited to '')
-rw-r--r-- | src/map.cpp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/map.cpp b/src/map.cpp new file mode 100644 index 0000000..17f9bd8 --- /dev/null +++ b/src/map.cpp | |||
@@ -0,0 +1,140 @@ | |||
1 | #include "map.h" | ||
2 | #include "cell.h" | ||
3 | #include "position.h" | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <string.h> | ||
7 | #include <exception> | ||
8 | |||
9 | Map::Map( const Position &rpMax ) : | ||
10 | iDims( rpMax.getDims() ), | ||
11 | aiSize( 0 ), | ||
12 | acMap( 0 ) | ||
13 | { | ||
14 | int iTotal = 1; | ||
15 | aiSize = new int[iDims]; | ||
16 | for( int j = 0; j < iDims; j++ ) | ||
17 | { | ||
18 | aiSize[j] = rpMax[j]; | ||
19 | iTotal *= aiSize[j]; | ||
20 | } | ||
21 | acMap = new Cell[iTotal]; | ||
22 | } | ||
23 | |||
24 | Map::~Map() | ||
25 | { | ||
26 | delete[] aiSize; | ||
27 | delete[] acMap; | ||
28 | } | ||
29 | |||
30 | Cell &Map::operator[]( const Position &rpIdx ) const | ||
31 | { | ||
32 | return acMap[getIndex( rpIdx )]; | ||
33 | } | ||
34 | |||
35 | Cell &Map::operator[]( const Position &rpIdx ) | ||
36 | { | ||
37 | return acMap[getIndex( rpIdx )]; | ||
38 | } | ||
39 | |||
40 | int Map::getDims() const | ||
41 | { | ||
42 | return iDims; | ||
43 | } | ||
44 | |||
45 | int Map::getSize( int iDim ) const | ||
46 | { | ||
47 | return aiSize[iDim]; | ||
48 | } | ||
49 | |||
50 | int Map::getIndex( const Position &rpIdx ) const | ||
51 | { | ||
52 | if( !isInside( rpIdx ) ) | ||
53 | throw std::exception(); | ||
54 | |||
55 | int iIdx = 0; | ||
56 | int iScale = 1; | ||
57 | for( int j = 0; j < iDims; j++ ) | ||
58 | { | ||
59 | iIdx += rpIdx[j]*iScale; | ||
60 | iScale *= aiSize[j]; | ||
61 | } | ||
62 | return iIdx; | ||
63 | } | ||
64 | |||
65 | bool Map::isInside( const Position &rpIdx ) const | ||
66 | { | ||
67 | if( rpIdx.getDims() != iDims ) | ||
68 | throw std::exception(); | ||
69 | |||
70 | for( int j = 0; j < iDims; j++ ) | ||
71 | { | ||
72 | if( rpIdx[j] < 0 ) | ||
73 | return false; | ||
74 | if( rpIdx[j] >= aiSize[j] ) | ||
75 | return false; | ||
76 | } | ||
77 | |||
78 | return true; | ||
79 | } | ||
80 | |||
81 | int opposite( int iDir ); | ||
82 | |||
83 | void Map::connect( int iId1, int iId2 ) | ||
84 | { | ||
85 | Position p( iDims ); | ||
86 | |||
87 | Position pMax1, pMax2; | ||
88 | int iDistMax = 0; | ||
89 | int iDirMax = 0; | ||
90 | |||
91 | int iDim; | ||
92 | for(;;) | ||
93 | { | ||
94 | Cell &c = (*this)[p]; | ||
95 | if( c.iPath == iId1 || c.iPath == iId2 ) | ||
96 | { | ||
97 | // Could be the thing! | ||
98 | for( iDim = 0; iDim < iDims; iDim++ ) | ||
99 | { | ||
100 | Position t( p ); | ||
101 | t[iDim]--; | ||
102 | for( int iDir = 0; iDir < 2; iDir++ ) | ||
103 | { | ||
104 | // int w = (1<<(iDim*2+iDir)); | ||
105 | if( t[iDim] >= 0 && t[iDim] < aiSize[iDim] ) | ||
106 | { | ||
107 | Cell &c2 = (*this)[t]; | ||
108 | if( c.iPath != c2.iPath && | ||
109 | (c2.iPath == iId1 || c2.iPath == iId2) ) | ||
110 | { | ||
111 | int iDist = c.iDist+c2.iDist; | ||
112 | if( iDist > iDistMax ) | ||
113 | { | ||
114 | iDistMax = iDist; | ||
115 | pMax1 = p; | ||
116 | pMax2 = t; | ||
117 | iDirMax = iDim*2+iDir; | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | t[iDim] += 2; | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | |||
126 | for( iDim = 0; iDim < iDims; iDim++ ) | ||
127 | { | ||
128 | if( ++p[iDim] < aiSize[iDim] ) | ||
129 | break; | ||
130 | p[iDim] = 0; | ||
131 | } | ||
132 | if( iDim == iDims ) | ||
133 | break; | ||
134 | } | ||
135 | |||
136 | (*this)[pMax1].iWalls |= (1<<iDirMax); | ||
137 | (*this)[pMax2].iWalls |= (1<<opposite(iDirMax)); | ||
138 | // printf("iDistMax: %d, iDirMax: %d\n", iDistMax, iDirMax ); | ||
139 | } | ||
140 | |||