CalcNewPosition.emam 1.88 KB
Newer Older
Bernhard Rumpe's avatar
BR-sy    
Bernhard Rumpe committed
1
/* (c) https://github.com/MontiCore/monticore */
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
package de.rwth.pacman.heithoff2.BFS.single;

component CalcNewPosition {
    ports
      in Q(0m: 20m) currentX,
      in Q(1m: 23m) currentY,
      in Q(0m: 20m) oldX,
      in Q(1m: 23m) oldY,
      in Z^{22,19} map,
      in B searchFinished,
      in B positionIsSafe,
      
      out Q(0m: 20m) newX,
      out Q(1m: 23m) newY,
      out Z newDirection,
      out B safeFound;
    
    
    implementation Math {
        newX = currentX;
        newY = currentY;
        newDirection = 0;
        safeFound = 0;
        if (searchFinished == 0) && (positionIsSafe == 1)
            // check for intersection or calculate the next tile
            Z^{1,4} xOffSet = [0,0,-1,1];
            Z^{1,4} yOffSet = [-1,1,0,0];
            safeFound = 0;
            Z newPathsFound = 0;
            for i = 0:3
                Z indexY = 0;
                Z indexX = i;
                Z xOff = xOffSet(indexY, indexX);
                Z yOff = yOffSet(indexY, indexX);
                Q xT = currentX + xOff;
                Q yT = currentY + yOff;
                
                if (abs(xT - oldX) >= 1) || (abs(yT - oldY) >= 1)
                    Z nextTile = map(yT, xT);
                    // if (yT < 23) && (yT > 0) && (xT < 20) && (xT > 0)
                    //     nextTile = map(yT, xT);
                    // end
                    if (nextTile != 0) && (nextTile != 3) // a non-blocking tile was found
                        newPathsFound = newPathsFound + 1;
                        newX = xT;
                        newY = yT;
                        newDirection = i;
                        if newPathsFound > 1
                            safeFound = 1;
                            newX = currentX;
                            newY = currentY;
                        end
                    end
                end
            end
        end
    }
Bernhard Rumpe's avatar
BR-sy    
Bernhard Rumpe committed
59
}