helpful progress reporting
[IRC.git] / Robust / src / Benchmarks / MMG / Tag / Map.java
1 public class Map {
2     flag init;
3     flag updateGhost;
4     flag updatePac;
5     flag next;
6     flag finish;
7     
8     // maze
9     private int m_nrofblocks;
10     public int[] m_map;
11     
12     // pacmen information
13     public int m_nrofpacs;
14     public int[] m_pacMenX;
15     public int[] m_pacMenY;
16     public int[] m_directions;
17     public int[] m_pacOriX;
18     public int[] m_pacOriY;
19     public int[] m_leftLives;
20     public int[] m_leftLevels;
21     //public int[] m_destinationX; // for ghosts to expact pamen's behaviour
22     //public int[] m_destinationY;
23     public int[] m_desX;
24     public int[] m_desY;
25     public int m_paccount;
26     public int m_deathcount;
27     
28     // ghosts information
29     public int m_nrofghosts;
30     public int[] m_ghostsX;
31     public int[] m_ghostsY;
32     public int[] m_ghostdirections;
33     public int[] m_targets;
34     public int m_ghostcount;
35     public int m_failghostcount;
36     
37     // helper member
38     public Random m_r;
39     
40     public Map(int nrofpacs, int nrofghosts) {
41         //System.printString("step 1\n");
42         this.m_nrofblocks = 15;
43         this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];
44         
45         this.m_nrofpacs = nrofpacs;
46         this.m_pacMenX = new int[this.m_nrofpacs];
47         this.m_pacMenY = new int[this.m_nrofpacs];
48         this.m_directions = new int[this.m_nrofpacs];
49         this.m_pacOriX = new int[this.m_nrofpacs];
50         this.m_pacOriY = new int[this.m_nrofpacs];
51         this.m_leftLives = new int[this.m_nrofpacs];
52         this.m_leftLevels = new int[this.m_nrofpacs];
53         //this.m_destinationX = new int[this.m_nrofpacs * 2];
54         //this.m_destinationY = new int[this.m_nrofpacs * 2];
55         this.m_desX = new int[this.m_nrofpacs];
56         this.m_desY = new int[this.m_nrofpacs];
57         this.m_paccount = 0;
58         this.m_deathcount = 0;
59         
60         this.m_nrofghosts = nrofghosts;
61         this.m_ghostsX = new int[this.m_nrofghosts];
62         this.m_ghostsY = new int[this.m_nrofghosts];
63         this.m_ghostdirections = new int[this.m_nrofghosts];
64         this.m_targets = new int[this.m_nrofghosts];
65         this.m_ghostcount = 0;
66         this.m_failghostcount = 0;
67         
68         this.m_r = new Random();
69         
70         for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
71             this.m_map[i] = -1;
72         }
73         
74         //System.printString("step 2\n");
75         for(int i = 0; i < this.m_nrofpacs; i++) {
76             this.m_pacMenX[i] = this.m_pacMenY[i] = -1;
77             this.m_directions[i] = 0;
78             this.m_pacOriX[i] = this.m_pacOriY[i] = -1;
79             this.m_leftLives[i] = this.m_leftLevels[i] = 0;
80             //this.m_destinationX[i] = this.m_destinationY[i] = -1;
81             //this.m_destinationX[this.m_nrofpacs + i] = this.m_destinationY[this.m_nrofpacs + i] = -1;
82             this.m_desX[i] = this.m_desY[i] = -1;
83         }
84         //System.printString("step 3\n");
85         for(int i = 0; i < this.m_nrofghosts; i++) {
86             this.m_ghostsX[i] = this.m_ghostsY[i] = -1;
87             this.m_targets[i] = -1;
88         }
89         //System.printString("step 4\n");
90     }
91     
92     public void init() {
93         // initilize the maze
94         int i = 0;
95         this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;
96         this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=15;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;
97         this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=6;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=3;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
98         this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=5;
99         this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=8;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=8;this.m_map[i++]=12;this.m_map[i++]=5;
100         this.m_map[i++]=9;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=8;this.m_map[i++]=2;this.m_map[i++]=12;this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=2;this.m_map[i++]=8;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=12;
101         this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=8;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=3;
102         this.m_map[i++]=4;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=1;
103         this.m_map[i++]=12;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=10;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=9;
104         this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=0;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=0;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;
105         this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=2;this.m_map[i++]=2;this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=15;this.m_map[i++]=5;this.m_map[i++]=15;this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=2;this.m_map[i++]=2;this.m_map[i++]=6;this.m_map[i++]=5;
106         this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=8;this.m_map[i++]=8;this.m_map[i++]=4;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=1;this.m_map[i++]=8;this.m_map[i++]=8;this.m_map[i++]=12;this.m_map[i++]=5;
107         this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=2;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
108         this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;
109         this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12; // 15*15
110     } 
111
112     public void placePacman(Pacman t) {
113         this.m_pacMenX[t.m_index] = t.m_locX;
114         this.m_pacMenY[t.m_index] = t.m_locY;
115         this.m_paccount++;
116     }
117     
118     public void placeGhost(Ghost t) {
119         this.m_ghostsX[t.m_index] = t.m_locX;
120         this.m_ghostsY[t.m_index] = t.m_locY;
121         this.m_ghostcount++;
122     }
123     
124     public boolean check(Pacman t) {
125         boolean death = false;
126         int i = 0;
127         while((!death) && (i < this.m_ghostsX.length)) {
128             if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {
129                 death = true;
130                 t.m_death = true;
131                 t.m_leftLives--;
132                 //System.printString("Pacman " + t.m_index + " caught! " + t.m_leftLives + "\n");
133             }
134             i++;
135         }
136         if((!death) && (t.m_locX == t.m_tx) && (t.m_locY == t.m_ty)) {
137             // reach the destination
138             //System.printString("Hit destination!\n");
139             death = true;
140             t.m_success = true;
141             t.m_leftLevels--;
142             //System.printString("Pacman " + t.m_index + " hit, upgrade! " + t.m_leftLevels + "\n");
143         }
144         if(death) {
145             if(t.isFinish()) {
146                 // pacman has no more lives or no more levels
147                 // kick it out
148                 this.m_deathcount++;
149                 this.m_pacMenX[t.m_index] = -1;
150                 this.m_pacMenY[t.m_index] = -1;
151             } else {
152                 if(t.m_death) {
153                     this.m_leftLives[t.m_index]--;
154                 } else if(t.m_success) {
155                     this.m_leftLevels[t.m_index]--;
156                 }
157                 t.reset();
158                 this.m_pacMenX[t.m_index] = t.m_locX;
159                 this.m_pacMenY[t.m_index] = t.m_locY;
160                 this.m_directions[t.m_index] = 0;
161                 //System.printString("Pacman " + t.m_index + " reset: (" + t.m_locX + ", " + t.m_locY + ")\n");
162             }
163             //this.m_destinationX[t.m_index] = this.m_destinationY[t.m_index] = -1;
164             //this.m_destinationX[this.m_nrofblocks + t.m_index] = this.m_destinationY[this.m_nrofblocks + t.m_index] = -1;
165         }
166         return death;
167     }
168     
169     public boolean isfinish() {
170         return this.m_nrofpacs == 0;
171     }
172     
173     public Vector getNeighbours(int index) {
174         Vector neighbours = new Vector();
175         int tmp = this.m_map[index];
176         int locX = index % this.m_nrofblocks;
177         int locY = index / this.m_nrofblocks;
178         if((int)(tmp & 1) == 0) {
179             // can go left
180             if(locX == 0) {
181                 neighbours.addElement(new Integer(locY * this.m_nrofblocks + this.m_nrofblocks - 1));
182             } else {
183                 neighbours.addElement(new Integer(index - 1));
184             }
185         } 
186         if((int)(tmp & 2) == 0) {
187             // can go up
188             if(locY == 0) {
189                 neighbours.addElement(new Integer((this.m_nrofblocks - 1) * this.m_nrofblocks + locX));
190             } else {
191                 neighbours.addElement(new Integer((locY - 1) * this.m_nrofblocks + locX));
192             }
193         }
194         if((int)(tmp & 4) == 0) {
195             // can go right
196             if(locX == this.m_nrofblocks - 1) {
197                 neighbours.addElement(new Integer(locY * this.m_nrofblocks));
198             } else {
199                 neighbours.addElement(new Integer(index + 1));
200             }
201         }
202         if((int)(tmp & 8) == 0) {
203             // can go down
204             if(locY == this.m_nrofblocks - 1) {
205                 neighbours.addElement(new Integer(locX));
206             } else {
207                 neighbours.addElement(new Integer((locY + 1) * this.m_nrofblocks + locX));
208             }
209         }
210         return neighbours;
211     }
212     
213     /*public int[] getDominatePoint(int locX, int locY) {
214         int[] point = new int[2];
215         point[0] = locX;
216         point[1] = locY;
217         if(((int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 2) + 
218                 (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 1) +
219                 (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 8) +
220                 (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 4)) == 4) {
221             // can not reach this point
222             point[0] = -1;
223             point[1] = -1;
224         } else if (((int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 2) + 
225                 (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 1) +
226                 (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 8) +
227                 (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 4)) < 3)  {
228             // can only reach it from multiple place
229         } else {
230             if (((int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 2) {
231                 
232             }
233         }
234         return point;
235     }*/
236 }