changes
[IRC.git] / Robust / src / Benchmarks / MMG / Nor / MMG.java
1 task startup(StartupObject s{initialstate}) {
2     //System.printString("Task startup\n");
3     
4     int nrofpacs = 4;
5     int nrofghosts = 8;
6     Map map = new Map(nrofpacs, nrofghosts){init};
7     taskexit(s{!initialstate});
8 }
9
10 task initMap(Map map{init}) {
11     //System.printString("Task initMap\n");
12     
13     map.init();
14     
15     int i = 0;
16     // create ghosts
17     for(i = 0; i < map.m_nrofghosts; i++) {
18         Ghost ghost = new Ghost(7, 7, map){move};
19         ghost.m_index = i;
20         map.placeGhost(ghost);
21     }
22     // create pacmen
23     int tx = 14;
24     int ty = 14;
25     //int tmpx = 0;
26     //int tmpy = 0;
27     for(i = 0; i < map.m_nrofpacs; i++) {
28         /*do {
29             tmpx = map.m_r.nextInt() % 14;
30         } while(tmpx < 0);
31         do {
32             tmpy = map.m_r.nextInt() % 14;
33         } while(tmpy < 0);*/
34         Pacman pacman = new Pacman(5, 7, map){move};
35         //Pacman pacman = new Pacman(tmpx, tmpy, map){move};
36         //System.printString("Pacman: (" + tmpx + ", " + tmpy + ")\n");
37         pacman.setTarget(tx*(i/2), ty*(i%2));
38         pacman.m_index = i;
39         map.placePacman(pacman);
40         map.m_desX[i] = tx*(i/2);
41         map.m_desY[i] = ty*(i%2);
42         map.m_pacOriX[i] = pacman.m_locX;
43         map.m_pacOriY[i] = pacman.m_locY;
44         map.m_leftLives[i] = map.m_leftLevels[i] = 20;
45         pacman.m_leftLives = pacman.m_leftLevels = 20;
46     }
47     
48     map.m_ghostcount = 0;
49     map.m_paccount = 0;
50     
51     taskexit(map{!init, updateGhost});
52 }
53
54 task moveGhost(Ghost g{move}) {
55     //System.printString("Task moveGhost\n");
56     
57     g.tryMove();
58     
59     taskexit(g{!move, update});
60 }
61
62 task movePacman(Pacman p{move}) {
63     //System.printString("Task movePacman\n");
64     
65     p.tryMove();
66     
67     taskexit(p{!move, update});
68 }
69
70 task updateGhost(Map map{updateGhost}, /*optional*/ Ghost g{update}) {
71     //System.printString("Task updateGhost\n");
72     
73     //System.printString("Ghost: " + g.m_index + "\n");
74     
75     //if(isavailable(g)) {
76         g.doMove();
77         map.placeGhost(g);
78         //System.printString("place Ghost\n");
79     /*} else {
80         map.m_ghostcount++;
81     }*/
82     
83     if(map.m_ghostcount == map.m_nrofghosts) {
84         //map.m_nrofghosts -= map.m_failghostcount;
85         map.m_ghostcount = 0;
86         map.m_failghostcount = 0;
87         /*for(int i = 0; i < map.m_ghostsX.length; i++) {
88             System.printString("(" + map.m_ghostsX[i] + "," + map.m_ghostsY[i] + ") ");
89         }
90         System.printString("\n");*/
91         taskexit(map{updatePac, !updateGhost}, g{!update});
92     }
93     taskexit(g{!update});
94 }
95
96 task updatePac(Map map{updatePac}, /*optional*/ Pacman p{update}) {
97     //System.printString("Task updatePac\n");
98     
99     //if(isavailable(p)) {
100         p.doMove();
101         map.placePacman(p);
102         //System.printString("Pacman " + p.m_index + ": (" + map.m_pacMenX[p.m_index] + "," + map.m_pacMenY[p.m_index] + ")\n");
103         boolean death = map.check(p);
104     /*} else {
105         map.m_paccount++;
106     }*/
107     
108     boolean finish = map.m_paccount == map.m_nrofpacs;
109     
110     if(finish) {
111         map.m_nrofpacs -= map.m_deathcount;
112         //System.printString(map.m_nrofpacs + " pacmen left. \n");
113         if(map.isfinish()) {
114             taskexit(map{finish, !updatePac}, p{!update, !move});
115         } else {
116             taskexit(map{next, !updatePac}, p{!update, !move});
117         }
118     } else {
119         taskexit(p{!move, !update});
120     }
121 }
122
123 task next(Map map{next}) {
124     //System.printString("Task next\n");
125     
126     int i = 0;
127     for(i = 0; i < map.m_nrofghosts; i++) {
128         Ghost ghost = new Ghost(map.m_ghostsX[i], map.m_ghostsY[i], map){move};
129         ghost.m_index = i;
130         ghost.m_direction = map.m_ghostdirections[i];
131     }
132     for(i = 0; i < map.m_pacMenX.length; i++) {
133         if(map.m_pacMenX[i] != -1) {
134             // still in the map
135             //System.printString("new Pacman\n");
136             Pacman pacman = new Pacman(map.m_pacMenX[i], map.m_pacMenY[i], map){move};
137             pacman.setTarget(map.m_desX[i], map.m_desY[i]);
138             pacman.m_index = i;
139             pacman.m_direction = map.m_directions[i];
140             pacman.m_oriLocX = map.m_pacOriX[i];
141             pacman.m_oriLocY = map.m_pacOriY[i];
142             pacman.m_leftLives = map.m_leftLives[i];
143             pacman.m_leftLevels = map.m_leftLevels[i];
144         }
145     }
146     
147     map.m_paccount = 0;
148     map.m_deathcount = 0;
149     
150     taskexit(map{!next, updateGhost});
151 }
152
153 task finish(Map map{finish}) {
154     //System.printString("Task Finish\n");
155     taskexit(map{!finish});
156 }