changes
[IRC.git] / Robust / src / Benchmarks / MMG / Nor / 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_desX;
22     public int[] m_desY;
23     public int m_paccount;
24     public int m_deathcount;
25     
26     // ghosts information
27     public int m_nrofghosts;
28     public int[] m_ghostsX;
29     public int[] m_ghostsY;
30     public int[] m_ghostdirections;
31     public int[] m_targets;
32     public int m_ghostcount;
33     public int m_failghostcount;
34     
35     // helper member
36     public Random m_r;
37     
38     public Map(int nrofpacs, int nrofghosts) {
39         //System.printString("step 1\n");
40         this.m_nrofblocks = 15;
41         this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];
42         
43         this.m_nrofpacs = nrofpacs;
44         this.m_pacMenX = new int[this.m_nrofpacs];
45         this.m_pacMenY = new int[this.m_nrofpacs];
46         this.m_directions = new int[this.m_nrofpacs];
47         this.m_pacOriX = new int[this.m_nrofpacs];
48         this.m_pacOriY = new int[this.m_nrofpacs];
49         this.m_leftLives = new int[this.m_nrofpacs];
50         this.m_leftLevels = new int[this.m_nrofpacs];
51         this.m_desX = new int[this.m_nrofpacs];
52         this.m_desY = new int[this.m_nrofpacs];
53         this.m_paccount = 0;
54         this.m_deathcount = 0;
55         
56         this.m_nrofghosts = nrofghosts;
57         this.m_ghostsX = new int[this.m_nrofghosts];
58         this.m_ghostsY = new int[this.m_nrofghosts];
59         this.m_ghostdirections = new int[this.m_nrofghosts];
60         this.m_targets = new int[this.m_nrofghosts];
61         this.m_ghostcount = 0;
62         this.m_failghostcount = 0;
63         
64         this.m_r = new Random();
65         
66         for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
67             this.m_map[i] = -1;
68         }
69         
70         //System.printString("step 2\n");
71         for(int i = 0; i < this.m_nrofpacs; i++) {
72             this.m_pacMenX[i] = this.m_pacMenY[i] = -1;
73             this.m_directions[i] = 0;
74             this.m_pacOriX[i] = this.m_pacOriY[i] = -1;
75             this.m_leftLives[i] = this.m_leftLevels[i] = 0;
76             this.m_desX[i] = this.m_desY[i] = -1;
77         }
78         //System.printString("step 3\n");
79         for(int i = 0; i < this.m_nrofghosts; i++) {
80             this.m_ghostsX[i] = this.m_ghostsY[i] = -1;
81             this.m_targets[i] = -1;
82         }
83         //System.printString("step 4\n");
84     }
85     
86     public void init() {
87         // initilize the maze
88         int i = 0;
89         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;
90         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;
91         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;
92         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;
93         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;
94         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;
95         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;
96         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;
97         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;
98         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;
99         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;
100         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;
101         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;
102         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;
103         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
104     } 
105
106     public void placePacman(Pacman t) {
107         this.m_pacMenX[t.m_index] = t.m_locX;
108         this.m_pacMenY[t.m_index] = t.m_locY;
109         this.m_paccount++;
110     }
111     
112     public void placeGhost(Ghost t) {
113         this.m_ghostsX[t.m_index] = t.m_locX;
114         this.m_ghostsY[t.m_index] = t.m_locY;
115         this.m_ghostcount++;
116     }
117     
118     public boolean check(Pacman t) {
119         boolean death = false;
120         int i = 0;
121         while((!death) && (i < this.m_ghostsX.length)) {
122             if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {
123                 death = true;
124                 t.m_death = true;
125                 t.m_leftLives--;
126                 //System.printString("Pacman " + t.m_index + " caught! " + t.m_leftLives + "\n");
127             }
128             i++;
129         }
130         if((!death) && (t.m_locX == t.m_tx) && (t.m_locY == t.m_ty)) {
131             // reach the destination
132             //System.printString("Hit destination!\n");
133             death = true;
134             t.m_success = true;
135             t.m_leftLevels--;
136             //System.printString("Pacman " + t.m_index + " hit, upgrade! " + t.m_leftLevels + "\n");
137         }
138         if(death) {
139             if(t.isFinish()) {
140                 // pacman has no more lives or no more levels
141                 // kick it out
142                 this.m_deathcount++;
143                 this.m_pacMenX[t.m_index] = -1;
144                 this.m_pacMenY[t.m_index] = -1;
145             } else {
146                 if(t.m_death) {
147                     this.m_leftLives[t.m_index]--;
148                 } else if(t.m_success) {
149                     this.m_leftLevels[t.m_index]--;
150                 }
151                 t.reset();
152                 this.m_pacMenX[t.m_index] = t.m_locX;
153                 this.m_pacMenY[t.m_index] = t.m_locY;
154                 this.m_directions[t.m_index] = 0;
155                 //System.printString("Pacman " + t.m_index + " reset: (" + t.m_locX + ", " + t.m_locY + ")\n");
156             }
157         }
158         return death;
159     }
160     
161     public boolean isfinish() {
162         return this.m_nrofpacs == 0;
163     }
164     
165     public Vector getNeighbours(int index) {
166         Vector neighbours = new Vector();
167         int tmp = this.m_map[index];
168         int locX = index % this.m_nrofblocks;
169         int locY = index / this.m_nrofblocks;
170         if((int)(tmp & 1) == 0) {
171             // can go left
172             if(locX == 0) {
173                 neighbours.addElement(new Integer(locY * this.m_nrofblocks + this.m_nrofblocks - 1));
174             } else {
175                 neighbours.addElement(new Integer(index - 1));
176             }
177         } 
178         if((int)(tmp & 2) == 0) {
179             // can go up
180             if(locY == 0) {
181                 neighbours.addElement(new Integer((this.m_nrofblocks - 1) * this.m_nrofblocks + locX));
182             } else {
183                 neighbours.addElement(new Integer((locY - 1) * this.m_nrofblocks + locX));
184             }
185         }
186         if((int)(tmp & 4) == 0) {
187             // can go right
188             if(locX == this.m_nrofblocks - 1) {
189                 neighbours.addElement(new Integer(locY * this.m_nrofblocks));
190             } else {
191                 neighbours.addElement(new Integer(index + 1));
192             }
193         }
194         if((int)(tmp & 8) == 0) {
195             // can go down
196             if(locY == this.m_nrofblocks - 1) {
197                 neighbours.addElement(new Integer(locX));
198             } else {
199                 neighbours.addElement(new Integer((locY + 1) * this.m_nrofblocks + locX));
200             }
201         }
202         return neighbours;
203     }
204 }