diff options
Diffstat (limited to 'src/worm.cpp')
| -rw-r--r-- | src/worm.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/worm.cpp b/src/worm.cpp new file mode 100644 index 0000000..31be54d --- /dev/null +++ b/src/worm.cpp | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | #include "worm.h" | ||
| 2 | #include "map.h" | ||
| 3 | #include "cell.h" | ||
| 4 | |||
| 5 | #include <stdlib.h> | ||
| 6 | |||
| 7 | Worm::Worm( int iId, const Position &rpStart, Map &rMap ) : | ||
| 8 | iId( iId ), | ||
| 9 | pCur( rpStart ), | ||
| 10 | rMap( rMap ), | ||
| 11 | iDist( 1 ) | ||
| 12 | { | ||
| 13 | Cell &rCur = rMap[pCur]; | ||
| 14 | rCur.iDist = iDist; | ||
| 15 | rCur.iPath = iId; | ||
| 16 | |||
| 17 | int iCount = 0; | ||
| 18 | int *iDirs = new int[2]; | ||
| 19 | if( pCur[0] == 0 ) | ||
| 20 | iDirs[iCount++] = 1; | ||
| 21 | else if( pCur[0] == rMap.getSize(0)-1 ) | ||
| 22 | iDirs[iCount++] = 2; | ||
| 23 | if( pCur[1] == 0 ) | ||
| 24 | iDirs[iCount++] = 4; | ||
| 25 | else if( pCur[1] == rMap.getSize(1)-1 ) | ||
| 26 | iDirs[iCount++] = 8; | ||
| 27 | |||
| 28 | rCur.iWalls |= iDirs[rand()%iCount]; | ||
| 29 | delete[] iDirs; | ||
| 30 | } | ||
| 31 | |||
| 32 | Worm::~Worm() | ||
| 33 | { | ||
| 34 | } | ||
| 35 | |||
| 36 | int opposite( int iDir ) | ||
| 37 | { | ||
| 38 | if( iDir%2 == 1 ) | ||
| 39 | return iDir-1; | ||
| 40 | return iDir+1; | ||
| 41 | } | ||
| 42 | |||
| 43 | bool Worm::timestep() | ||
| 44 | { | ||
| 45 | int iDims = rMap.getDims(); | ||
| 46 | int iCount = 0; | ||
| 47 | Position *pDirs = new Position[iDims*2]; | ||
| 48 | int *iDirs = new int[iDims*2]; | ||
| 49 | |||
| 50 | for(;;) | ||
| 51 | { | ||
| 52 | iCount = 0; | ||
| 53 | bool bFoundBack = false; | ||
| 54 | Position pBack; | ||
| 55 | for( int j = 0; j < iDims; j++ ) | ||
| 56 | { | ||
| 57 | int iSize = rMap.getSize( j ); | ||
| 58 | Position p1( pCur ); | ||
| 59 | p1[j]--; | ||
| 60 | if( p1[j] >= 0 ) | ||
| 61 | { | ||
| 62 | if( rMap[p1].iPath == 0 ) | ||
| 63 | { | ||
| 64 | iDirs[iCount] = j*2; | ||
| 65 | pDirs[iCount++] = p1; | ||
| 66 | } | ||
| 67 | else if( rMap[p1].iPath == iId && rMap[p1].iDist == iDist-1 ) | ||
| 68 | { | ||
| 69 | pBack = p1; | ||
| 70 | bFoundBack = true; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | Position p2( pCur ); | ||
| 74 | p2[j]++; | ||
| 75 | if( p2[j] < iSize ) | ||
| 76 | { | ||
| 77 | if( rMap[p2].iPath == 0 ) | ||
| 78 | { | ||
| 79 | iDirs[iCount] = j*2+1; | ||
| 80 | pDirs[iCount++] = p2; | ||
| 81 | } | ||
| 82 | else if( rMap[p2].iPath == iId && rMap[p2].iDist == iDist-1 ) | ||
| 83 | { | ||
| 84 | pBack = p2; | ||
| 85 | bFoundBack = true; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | if( iCount > 0 ) | ||
| 91 | { | ||
| 92 | break; | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | if( bFoundBack ) | ||
| 97 | { | ||
| 98 | pCur = pBack; | ||
| 99 | iDist--; | ||
| 100 | } | ||
| 101 | else | ||
| 102 | { | ||
| 103 | delete[] pDirs; | ||
| 104 | delete[] iDirs; | ||
| 105 | return false; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | int iSel = rand()%iCount; | ||
| 111 | Cell &rCur = rMap[pCur]; | ||
| 112 | rCur.iWalls |= (1<<iDirs[iSel]); | ||
| 113 | Cell &rNext = rMap[pDirs[iSel]]; | ||
| 114 | rNext.iWalls |= (1<<opposite(iDirs[iSel])); | ||
| 115 | rNext.iDist = ++iDist; | ||
| 116 | rNext.iPath = iId; | ||
| 117 | pCur = pDirs[iSel]; | ||
| 118 | |||
| 119 | delete[] pDirs; | ||
| 120 | delete[] iDirs; | ||
| 121 | |||
| 122 | return true; | ||
| 123 | } | ||
| 124 | |||
