use more sophiscated strategy for character moving
[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     for(i = 0; i < map.m_nrofpacs; i++) {
26           Pacman pacman = new Pacman(5, 7, map){move};
27           pacman.setTarget(tx*(i/2), ty*(i%2));
28           pacman.m_index = i;
29           map.placePacman(pacman);
30           map.m_desX[i] = tx*(i/2);
31           map.m_desY[i] = ty*(i%2);
32     }
33     
34     map.m_ghostcount = 0;
35     map.m_paccount = 0;
36     
37     taskexit(map{!init, updateGhost});
38 }
39
40 task moveGhost(Ghost g{move}) {
41     //System.printString("Task moveGhost\n");
42     
43     g.tryMove();
44     
45     taskexit(g{!move, update});
46 }
47
48 task movePacman(Pacman p{move}) {
49     //System.printString("Task movePacman\n");
50     
51     p.tryMove();
52     
53     taskexit(p{!move, update});
54 }
55
56 task updateGhost(Map map{updateGhost}, /*optional*/ Ghost g{update}) {
57     //System.printString("Task updateGhost\n");
58     
59     //if(isavailable(g)) {
60         g.doMove();
61         map.placeGhost(g);
62     /*} else {
63         map.m_ghostcount++;
64     }*/
65     
66     if(map.m_ghostcount == map.m_nrofghosts) {
67         //map.m_nrofghosts -= map.m_failghostcount;
68         map.m_ghostcount = 0;
69         map.m_failghostcount = 0;
70         /*for(int i = 0; i < map.m_ghostsX.length; i++) {
71             System.printString("(" + map.m_ghostsX[i] + "," + map.m_ghostsY[i] + ") ");
72         }
73         System.printString("\n");*/
74         taskexit(map{updatePac, !updateGhost}, g{!update});
75     }
76     taskexit(g{!update});
77 }
78
79 task updatePac(Map map{updatePac}, /*optional*/ Pacman p{update}) {
80     //System.printString("Task updatePac\n");
81     
82     //if(isavailable(p)) {
83         p.doMove();
84         map.placePacman(p);
85         //System.printString("Pacman " + p.m_index + ": (" + map.m_pacMenX[p.m_index] + "," + map.m_pacMenY[p.m_index] + ")\n");
86         boolean death = map.check(p);
87         /*if(death) {
88             System.printString("Pacman " + p.m_index + " caught!\n");
89         }*/
90     /*} else {
91         map.m_deathcount++;
92         map.m_paccount++;
93     }*/
94     
95     boolean finish = map.m_paccount == map.m_nrofpacs;
96     
97     if(finish) {
98         map.m_nrofpacs -= map.m_deathcount;
99         //System.printString(map.m_nrofpacs + " pacmen left. \n");
100         if(map.isfinish()) {
101             taskexit(map{finish, !updatePac}, p{!update, !move});
102         } else {
103             taskexit(map{next, !updatePac}, p{!update, !move});
104         }
105     } else {
106         taskexit(p{!move, !update});
107     }
108 }
109
110 task next(Map map{next}) {
111     //System.printString("Task next\n");
112     
113     int i = 0;
114     for(i = 0; i < map.m_nrofghosts; i++) {
115         Ghost ghost = new Ghost(map.m_ghostsX[i], map.m_ghostsY[i], map){move};
116         ghost.m_index = i;
117         ghost.m_direction = map.m_ghostdirections[i];
118     }
119     for(i = 0; i < map.m_pacMenX.length; i++) {
120         if(map.m_pacMenX[i] != -1) {
121             // still in the map
122             //System.printString("new Pacman\n");
123             Pacman pacman = new Pacman(map.m_pacMenX[i], map.m_pacMenY[i], map){move};
124             pacman.setTarget(map.m_desX[i], map.m_desY[i]);
125             pacman.m_index = i;
126             pacman.m_direction = map.m_directions[i];
127         }
128     }
129     
130     map.m_paccount = 0;
131     map.m_deathcount = 0;
132     
133     taskexit(map{!next, updateGhost});
134 }
135
136 task finish(Map map{finish}) {
137     System.printString("Task Finish\n");
138     taskexit(map{!finish});
139 }