-task startup(StartupObject s{initialstate}) {\r
- //System.printString("Task startup\n");\r
- \r
- int nrofpacs = 4;\r
- int nrofghosts = 8;\r
- Map map = new Map(nrofpacs, nrofghosts){init};\r
- taskexit(s{!initialstate});\r
-}\r
-\r
-task initMap(Map map{init}) {\r
- //System.printString("Task initMap\n");\r
- \r
- map.init();\r
- \r
- int i = 0;\r
- // create ghosts\r
- for(i = 0; i < map.nrofghosts; i++) {\r
- Ghost ghost = new Ghost(7, 7, map){move};\r
- ghost.setTarget(i%map.nrofpacs);\r
- ghost.index = i;\r
- map.placeGhost(ghost);\r
- map.targets[i] = ghost.target;\r
- }\r
- // create pacmen\r
- int tx = 14;\r
- int ty = 14;\r
- for(i = 0; i < map.nrofpacs; i++) {\r
- Pacman pacman = new Pacman(5, 7, map){move};\r
- pacman.setTarget(tx*(i/2), ty*(i%2));\r
- pacman.index = i;\r
- map.placePacman(pacman);\r
- map.desX[i] = tx*(i/2);\r
- map.desY[i] = ty*(i%2);\r
- }\r
- \r
- map.ghostcount = 0;\r
- map.paccount = 0;\r
- \r
- taskexit(map{!init, updateGhost});\r
-}\r
-\r
-task moveGhost(Ghost g{move}) {\r
- //System.printString("Task moveGhost\n");\r
- \r
- g.tryMove();\r
- \r
- taskexit(g{!move, update});\r
-}\r
-\r
-task movePacman(Pacman p{move}) {\r
- //System.printString("Task movePacman\n");\r
- \r
- p.tryMove();\r
- \r
- taskexit(p{!move, update});\r
-}\r
-\r
-task updateGhost(Map map{updateGhost}, /*optional*/ Ghost g{update}) {\r
- //System.printString("Task updateGhost\n");\r
- \r
- //if(isavailable(g)) {\r
- g.doMove();\r
- map.placeGhost(g);\r
- map.ghostdirections[g.index] = g.direction;\r
- /*} else {\r
- System.printString("FAILURE ghost!!!\n");\r
- //map.failghostcount++;\r
- map.ghostcount++;\r
- }*/\r
- \r
- if(map.ghostcount == map.nrofghosts) {\r
- //map.nrofghosts -= map.failghostcount;\r
- map.ghostcount = 0;\r
- map.failghostcount = 0;\r
- /*for(int i = 0; i < map.ghostsX.length; i++) {\r
- System.printString("(" + map.ghostsX[i] + "," + map.ghostsY[i] + ") ");\r
- }\r
- System.printString("\n");*/\r
- taskexit(map{updatePac, !updateGhost}, g{!update});\r
- }\r
- taskexit(g{!update});\r
-}\r
-\r
-task updatePac(Map map{updatePac}, /*optional*/ Pacman p{update}) {\r
- //System.printString("Task updatePac\n");\r
- \r
- //if(isavailable(p)) {\r
- p.doMove();\r
- map.placePacman(p);\r
- map.directions[p.index] = p.direction;\r
- //System.printString("Pacman " + p.index + ": (" + map.pacMenX[p.index] + "," + map.pacMenY[p.index] + ")\n");\r
- boolean death = map.check(p);\r
- /*if(death) {\r
- System.printString("Pacman " + p.index + " caught!\n");\r
- }*/\r
- /*} else {\r
- System.printString("FAILURE pacman!!!\n");\r
- map.deathcount++;\r
- map.paccount++;\r
- }*/\r
- \r
- boolean finish = map.paccount == map.nrofpacs;\r
- \r
- if(finish) {\r
- map.nrofpacs -= map.deathcount;\r
- //System.printString(map.nrofpacs + " pacmen left. \n");\r
- if(map.isfinish()) {\r
- taskexit(map{finish, !updatePac}, p{!update, !move});\r
- } else {\r
- taskexit(map{next, !updatePac}, p{!update, !move});\r
- }\r
- } else {\r
- taskexit(p{!move, !update});\r
- }\r
-}\r
-\r
-task next(Map map{next}) {\r
- //System.printString("Task next\n");\r
- \r
- int i = 0;\r
- for(i = 0; i < map.nrofghosts; i++) {\r
- Ghost ghost = new Ghost(map.ghostsX[i], map.ghostsY[i], map){move};\r
- ghost.setTarget(map.targets[i]);\r
- ghost.index = i;\r
- ghost.direction = map.ghostdirections[i];\r
- }\r
- for(i = 0; i < map.pacMenX.length; i++) {\r
- if(map.pacMenX[i] != -1) {\r
- // still in the map\r
- //System.printString("new Pacman\n");\r
- Pacman pacman = new Pacman(map.pacMenX[i], map.pacMenY[i], map){move};\r
- pacman.setTarget(map.desX[i], map.desY[i]);\r
- pacman.index = i;\r
- pacman.direction = map.directions[i];\r
- }\r
- }\r
- \r
- map.paccount = 0;\r
- map.deathcount = 0;\r
- \r
- taskexit(map{!next, updateGhost});\r
-}\r
-\r
-task finish(Map map{finish}) {\r
- System.printString("Task Finish\n");\r
- taskexit(map{!finish});\r
-}\r
+task startup(StartupObject s{initialstate}) {
+ //System.printString("Task startup\n");
+
+ int nrofpacs = 4;
+ int nrofghosts = 8;
+ Map map = new Map(nrofpacs, nrofghosts){init};
+ taskexit(s{!initialstate});
+}
+
+task initMap(Map map{init}) {
+ //System.printString("Task initMap\n");
+
+ map.init();
+
+ int i = 0;
+ // create ghosts
+ for(i = 0; i < map.m_nrofghosts; i++) {
+ Ghost ghost = new Ghost(7, 7, map){move};
+ ghost.m_index = i;
+ map.placeGhost(ghost);
+ }
+ // create pacmen
+ int tx = 14;
+ int ty = 14;
+ //int tmpx = 0;
+ //int tmpy = 0;
+ for(i = 0; i < map.m_nrofpacs; i++) {
+ /*do {
+ tmpx = map.m_r.nextInt() % 14;
+ } while(tmpx < 0);
+ do {
+ tmpy = map.m_r.nextInt() % 14;
+ } while(tmpy < 0);*/
+ Pacman pacman = new Pacman(5, 7, map){move};
+ //Pacman pacman = new Pacman(tmpx, tmpy, map){move};
+ //System.printString("Pacman: (" + tmpx + ", " + tmpy + ")\n");
+ pacman.setTarget(tx*(i/2), ty*(i%2));
+ pacman.m_index = i;
+ map.placePacman(pacman);
+ map.m_desX[i] = tx*(i/2);
+ map.m_desY[i] = ty*(i%2);
+ map.m_pacOriX[i] = pacman.m_locX;
+ map.m_pacOriY[i] = pacman.m_locY;
+ map.m_leftLives[i] = map.m_leftLevels[i] = 20;
+ pacman.m_leftLives = pacman.m_leftLevels = 20;
+ }
+
+ map.m_ghostcount = 0;
+ map.m_paccount = 0;
+
+ taskexit(map{!init, updateGhost});
+}
+
+task moveGhost(Ghost g{move}) {
+ //System.printString("Task moveGhost\n");
+
+ g.tryMove();
+
+ taskexit(g{!move, update});
+}
+
+task movePacman(Pacman p{move}) {
+ //System.printString("Task movePacman\n");
+
+ p.tryMove();
+
+ taskexit(p{!move, update});
+}
+
+task updateGhost(Map map{updateGhost}, /*optional*/ Ghost g{update}) {
+ //System.printString("Task updateGhost\n");
+
+ //System.printString("Ghost: " + g.m_index + "\n");
+
+ //if(isavailable(g)) {
+ g.doMove();
+ map.placeGhost(g);
+ //System.printString("place Ghost\n");
+ /*} else {
+ map.m_ghostcount++;
+ }*/
+
+ if(map.m_ghostcount == map.m_nrofghosts) {
+ //map.m_nrofghosts -= map.m_failghostcount;
+ map.m_ghostcount = 0;
+ map.m_failghostcount = 0;
+ /*for(int i = 0; i < map.m_ghostsX.length; i++) {
+ System.printString("(" + map.m_ghostsX[i] + "," + map.m_ghostsY[i] + ") ");
+ }
+ System.printString("\n");*/
+ taskexit(map{updatePac, !updateGhost}, g{!update});
+ }
+ taskexit(g{!update});
+}
+
+task updatePac(Map map{updatePac}, /*optional*/ Pacman p{update}) {
+ //System.printString("Task updatePac\n");
+
+ //if(isavailable(p)) {
+ p.doMove();
+ map.placePacman(p);
+ //System.printString("Pacman " + p.m_index + ": (" + map.m_pacMenX[p.m_index] + "," + map.m_pacMenY[p.m_index] + ")\n");
+ boolean death = map.check(p);
+ /*} else {
+ map.m_paccount++;
+ }*/
+
+ boolean finish = map.m_paccount == map.m_nrofpacs;
+
+ if(finish) {
+ map.m_nrofpacs -= map.m_deathcount;
+ //System.printString(map.m_nrofpacs + " pacmen left. \n");
+ if(map.isfinish()) {
+ taskexit(map{finish, !updatePac}, p{!update, !move});
+ } else {
+ taskexit(map{next, !updatePac}, p{!update, !move});
+ }
+ } else {
+ taskexit(p{!move, !update});
+ }
+}
+
+task next(Map map{next}) {
+ //System.printString("Task next\n");
+
+ int i = 0;
+ for(i = 0; i < map.m_nrofghosts; i++) {
+ Ghost ghost = new Ghost(map.m_ghostsX[i], map.m_ghostsY[i], map){move};
+ ghost.m_index = i;
+ ghost.m_direction = map.m_ghostdirections[i];
+ }
+ for(i = 0; i < map.m_pacMenX.length; i++) {
+ if(map.m_pacMenX[i] != -1) {
+ // still in the map
+ //System.printString("new Pacman\n");
+ Pacman pacman = new Pacman(map.m_pacMenX[i], map.m_pacMenY[i], map){move};
+ pacman.setTarget(map.m_desX[i], map.m_desY[i]);
+ pacman.m_index = i;
+ pacman.m_direction = map.m_directions[i];
+ pacman.m_oriLocX = map.m_pacOriX[i];
+ pacman.m_oriLocY = map.m_pacOriY[i];
+ pacman.m_leftLives = map.m_leftLives[i];
+ pacman.m_leftLevels = map.m_leftLevels[i];
+ }
+ }
+
+ map.m_paccount = 0;
+ map.m_deathcount = 0;
+
+ taskexit(map{!next, updateGhost});
+}
+
+task finish(Map map{finish}) {
+ //System.printString("Task Finish\n");
+ taskexit(map{!finish});
+}