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,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,1,1,1,1,1,1,1],
[1,1,1,1,1,1,2,1,1,1,1,1,2,1,1,2,1,1,1,1,2,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1] ]
map2=
[ [1,1,1,1,1,1,1,1,1,1],
[1,2,1,1,1,1,2,1,1,1],
[2,2,1,1,1,1,2,1,1,1],
[1,1,1,1,2,1,2,2,1,1],
[1,1,2,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,2,1,1,1,1,1],
[1,1,2,1,1,1,2,2,2,1],
[1,1,2,2,1,1,1,1,1,1],
[1,1,1,1,1,2,1,1,1,1] ]
dep=0;
game=new Object();
char=new Object();
cursor=new Object();
function buatMap(map){
_root.attachMovie("empty","penampung",100);
_root.penampung._x=0;
_root.penampung._y=0;
var panjang=map[0].length;
var lebar=map.length;
for(i=0;i<lebar;i++){
for(j=0;j<panjang;j++){
var nama="t_"+i+"_"+j;
_root.penampung.attachMovie("tile",nama,dep++);
game[nama]={dep:_root.dep , walkable:_root["tile_"+map[i][j]].walkable , frame:_root["tile_"+map[i][j]].frame};
game[nama].clip=_root.penampung[nama];
game[nama].clip.gotoAndStop(game[nama].frame);
game[nama].clip._x=j*20;
game[nama].clip._y=i*20;
}
}
_root.penampung.attachMovie("char","char",dep++);
char.clip=_root.penampung.char;
char.X=0;
char.Y=0;
char.Xtile=Math.floor(char.X/20);
char.Ytile=Math.floor(char.Y/20);
char.clip._x=char.X;
char.clip._y=char.Y;
_root.penampung.attachMovie("cursor","cursor",dep+10);
cursor.clip=_root.penampung.cursor;
cursor.Xtile=0;
cursor.Ytile=0;
cursor.X=cursor.X*20;
cursor.Y=cursor.Y*20;
cursor.clip._x=cursor.X;
cursor.clip._y=cursor.Y;
setIndex(char,map);
}
buatMap(map1);
onMouseMove = function(){
if((_xmouse+_root.penampung._x)<500 && (_xmouse+_root.penampung._x)>0 && (_ymouse+_root.penampung._y)<500 && (_ymouse+_root.penampung._y)>0){
cursor.Xtile=Math.floor((_xmouse+_root.penampung._x)/20);
cursor.Ytile=Math.floor((_ymouse+_root.penampung._y)/20);
cursor.X=cursor.Xtile*20;
cursor.Y=cursor.Ytile*20;
cursor.clip._x=cursor.X;
cursor.clip._y=cursor.Y;
}
}
onMouseDown = function(){
if((_xmouse+_root.penampung._x)<500 && (_xmouse+_root.penampung._x)>0 && (_ymouse+_root.penampung._y)<500 && (_ymouse+_root.penampung._y)>0){
if(char.index["t_"+cursor.Ytile+"_"+cursor.Xtile].shortPath!=undefined){
makePath(char,cursor.Xtile,cursor.Ytile);
goalPoint.text="("+cursor.Ytile+","+cursor.Xtile+")";
shortPath.text=char.index["t_"+cursor.Ytile+"_"+cursor.Xtile].shortPath;
maxPossible.text=char.index["t_"+cursor.Ytile+"_"+cursor.Xtile].maxPossible;
}else{
goalPoint.text="";
shortPath.text="";
maxPossible.text="";
}
}
}
function setIndex(ob,map){
ob.index=new Object();
var panjang=map[0].length;
var lebar=map.length;
for(i=0;i<lebar;i++){
for(j=0;j<panjang;j++){
var nama="t_"+i+"_"+j;
ob.index[nama]=new Object();
ob.index[nama].shortPath=undefined;
ob.index[nama].maxPossible=0;
ob.index[nama].source=new Array();
ob.index[nama].destiny=new Array();
}
}
ob.index["t_"+ob.Ytile+"_"+ob.Xtile].shortPath=0;
ob.index["t_"+ob.Ytile+"_"+ob.Xtile].maxPossible=1;
var dicari=[];
var mencari=[{Ytile:(ob.Ytile),Xtile:(ob.Xtile)}];
while(mencari.length!=0){
dicari=mencari;
mencari=[];
for(n=0;n<dicari.length;n++){
var nama="t_"+(dicari[n].Ytile)+"_"+(dicari[n].Xtile);
var nama_atas="t_"+(dicari[n].Ytile-1)+"_"+(dicari[n].Xtile);
var nama_bawah="t_"+(dicari[n].Ytile+1)+"_"+(dicari[n].Xtile);
var nama_kiri="t_"+(dicari[n].Ytile)+"_"+(dicari[n].Xtile-1);
var nama_kanan="t_"+(dicari[n].Ytile)+"_"+(dicari[n].Xtile+1);
if(game[nama_atas].walkable==true){
if(ob.index[nama_atas].shortPath==undefined){
ob.index[nama_atas].shortPath=ob.index[nama].shortPath+1;
ob.index[nama_atas].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile-1),Xtile:(dicari[n].Xtile)});
ob.index[nama_atas].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
mencari.push({Ytile:(dicari[n].Ytile-1),Xtile:(dicari[n].Xtile)});
} else if(ob.index[nama_atas].shortPath==ob.index[nama].shortPath+1){
ob.index[nama_atas].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile-1),Xtile:(dicari[n].Xtile)});
ob.index[nama_atas].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
}
}
if(game[nama_bawah].walkable==true){
if(ob.index[nama_bawah].shortPath==undefined){
ob.index[nama_bawah].shortPath=ob.index[nama].shortPath+1;
ob.index[nama_bawah].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile+1),Xtile:(dicari[n].Xtile)});
ob.index[nama_bawah].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
mencari.push({Ytile:(dicari[n].Ytile+1),Xtile:(dicari[n].Xtile)});
}else if(ob.index[nama_bawah].shortPath==ob.index[nama].shortPath+1){
ob.index[nama_bawah].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile+1),Xtile:(dicari[n].Xtile)});
ob.index[nama_bawah].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
}
}
if(game[nama_kiri].walkable==true){
if(ob.index[nama_kiri].shortPath==undefined){
ob.index[nama_kiri].shortPath=ob.index[nama].shortPath+1;
ob.index[nama_kiri].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile-1)});
ob.index[nama_kiri].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
mencari.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile-1)});
}else if(ob.index[nama_kiri].shortPath==ob.index[nama].shortPath+1){
ob.index[nama_kiri].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile-1)});
ob.index[nama_kiri].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
}
}
if(game[nama_kanan].walkable==true){
if(ob.index[nama_kanan].shortPath==undefined){
ob.index[nama_kanan].shortPath=ob.index[nama].shortPath+1;
ob.index[nama_kanan].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile+1)});
ob.index[nama_kanan].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
mencari.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile+1)});
}else if(ob.index[nama_kanan].shortPath==ob.index[nama].shortPath+1){
ob.index[nama_kanan].maxPossible+=ob.index[nama].maxPossible;
ob.index[nama].destiny.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile+1)});
ob.index[nama_kanan].source.push({Ytile:(dicari[n].Ytile),Xtile:(dicari[n].Xtile)});
}
}
}
}
}
function makePath(ob,Xgoal,Ygoal){
var nama_goal="t_"+Ygoal+"_"+Xgoal;
ob.path=[{Ytile:Ygoal,Xtile:Xgoal}];
while(ob.path.length<ob.index[nama_goal].shortPath){
var nama_akhir="t_"+(ob.path[ob.path.length-1].Ytile)+"_"+(ob.path[ob.path.length-1].Xtile);
ob.path.push(ob.index[nama_akhir].source[random(ob.index[nama_akhir].source.length)]);
}
var pathCad=new Array();
for(n=0;n<ob.path.length;n++){
pathCad[n]=ob.path[ob.path.length-n-1];
}
ob.path=pathCad;
makeArea(ob);
}
function makeArea(ob){
_root.penampung.attachMovie("goal","goal",(ob.clip.getDepth()+1));
_root.penampung.goal._x=ob.path[ob.path.length-1].Xtile*20;
_root.penampung.goal._y=ob.path[ob.path.length-1].Ytile*20;
_root.penampung.attachMovie("empty","path",(ob.clip.getDepth()+2));
var dep=0;
for(n=0;n<ob.path.length;n++){
var Ytile=ob.path[n].Ytile;
var Xtile=ob.path[n].Xtile;
var nama="a_"+Ytile+"_"+Xtile;
_root.penampung.path.attachMovie("area",nama,dep++);
_root.penampung.path[nama]._y=Ytile*20;
_root.penampung.path[nama]._x=Xtile*20;
}
}
function traceArrayObject(array){
var tulisan=new String();
tulisan+="[";
for(n=0;n<array.length;n++){
tulisan+="[{Xtile:"+array[n].Xtile+",Ytile:"+array[n].Ytile+"}]";
}
tulisan+="]";
trace (tulisan);
};
ada beberapa yang gak diperlukan seh.
hanya untuk percobaan.