map script js source


/************************************************************
(c) Alain Bertrand - 2005
Please contact the author before using it for you own needs:
bertrand@nodalideas.com
************************************************************/

var kb = {LEFT:37, RIGHT:39, UP:38, DOWN:40, BACKSPACE:8, CAPSLOCK:20, CONTROL:17, DELETEKEY:46, END:35, ENTER:13, ESCAPE:27, HOME:36, INSERT:45, TAB:9, PGDN:34, PGUP:33, SPACE:32, SHIFT:16};

var comedir=-1;
var isherbs=new Array();
var isinit=false;
var map_dialogs=new Array();
var inmapdialog=false;

// Structure which stores the map dialogs
function map_dialog(x,y,dialog)
{
this.x=x;
this.y=y;
this.dialog=dialog;
}

function add_map_dialog(x,y,dialog)
{
map_dialogs[map_dialogs.length]=new map_dialog(x,y,dialog)
}

function draw_dialogs(rx,ry,x,y)
{
var div=document.getElementById("DIALOG_DIV");
for(var i=0;i < map_dialogs.length;i++)
{
var a=x-map_dialogs[i].x;
var b=y-map_dialogs[i].y;

var d=Math.round(Math.sqrt(a*a+b*b));
if(d < 2)
{
var dx;
var dy;
if(zoom)
{
dx=(map_dialogs[i].x-rx)*128-10;
dy=(map_dialogs[i].y-ry)*128-10;
}
else
{
dx=(map_dialogs[i].x-rx)*64+185-40;
dy=(map_dialogs[i].y-ry)*64+80-10;
}
div.style.top=dy;
div.style.left=dx;
div.style.visibility="visible";
div.innerHTML=map_dialogs[i].dialog;
return;
}
}
div.style.visibility="hidden";
}

function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}

function change_hp()
{
document.images['HP'].width=Math.ceil(hp/maxhp*99)+1;
text=String(hp)+"/"+String(maxhp);
try
{
document.getElementById("HPVIEW").innerHTML=text;
}
catch(e)
{
}
}

function disable_move()
{
disablemove=true;
if(timeID != 0)
clearTimeout(timeID);
divtiles=document.getElementById("BG_TILES");
divwait=document.getElementById("WAIT_LOADING");
if(divtiles == null)
return;
divtiles.style.visibility="hidden";
divwait.style.visibility="visible";
if(document.getElementById("TIME") != null)
document.getElementById("TIME").style.visibility="hidden";

// Hidde objects
for(var i=0;i < 7;i++)
{
for(var j=0;j < 7;j++)
{
divobj=document.getElementById("OVER_OBJ_"+i+"_"+j);
divobj.style.visibility="hidden";
}
}
divwait.innerHTML="


Loading new map... Please wait.";
}

function init_map()
{
// Init the keyboard
init();

// Check if the startup position is not walkable... In which case try to find a better position.
for(var i=0;i < 600;i++)
isherbs[i]=false;
for(var i=0;i < herbs.length;i++)
isherbs[herbs[i]]=true;

divtiles=document.getElementById("BG_TILES");
if(divtiles.style.visibility == "visible")
return;
divtiles.style.visibility="visible";
if(document.getElementById("TIME") != null)
document.getElementById("TIME").style.visibility="visible";
if(y == 0)
comedir=0;
else if(x == 0)
comedir=1;
else if(x == 98)
comedir=2;
else if(y == 98)
comedir=3;

if(check_walkable_map(map[x][y],boat) == false)
{
for(var i=1;i < 99;i++)
{
for(var j=1;j < 99;j++)
{
if(check_walkable_map(map[i][j],boat) == true)
{
x=i;
y=j;
break;
}
}
if(check_walkable_map(map[i][j],boat) == true)
break;
}
}

// Show the two layers now that we are loaded...
divwait=document.getElementById("WAIT_LOADING");
document.getElementById("MESSAGES").style.backgroundColor="#E0E0E0";
document.getElementById("MESSAGES").innerHTML="Message window:\n";

if(showminimap == true && ismappable == false)
document.getElementById("MESSAGES").innerHTML="Message window:
It seems the minimap doesn't work here.\n";
else if(document.getElementById("TIME") != null)
document.getElementById("MESSAGES").innerHTML="Message window:\n
It's now "+hour+" in the realm.\n";

divwait.style.visibility="hidden";

if(nbmonsters == 0 && isplayerdongeon == false)
{
Display();
return;
}

if(restore_monsters() == true)
{
paint_monsters();
if(isplayerdongeon == true && owner == userid)
{
for(var i=0;i < 100;i++)
for(var j=0;j < 100;j++)
if(mapobj[i][j] == 143) // Switch on the dongeon manager.
{
mapobj[i][j]=142;
Display();
return;
}
}
Display();
return;
}

placed=0;

for(var i=0;i < 100;i++)
{
for(var j=0;j < 100;j++)
{
if(mapobj[i][j] == 143 && isplayerdongeon == true && owner == userid) // Switch on the dongeon manager.
mapobj[i][j]=142;
// No objects Not on the walls of the cave :-) And with some random
else if(nbmonsters > 0 && density > 0 && mapobj[i][j] == 0 && check_walkable_map(map[i][j],false) == true && Math.round(Math.random()*density) == 0)
{
placed++;
mapobj[i][j]=-monsterid[Math.floor(Math.random()*nbmonsters)];
}
}
}
paint_monsters();
Display();
}

function paint_monsters()
{
if(showminimonsters == false)
return;
html="";
for(var i=0;i < 99;i++)
for(var j=0;j < 99;j++)
if(mapobj[i][j] < 0)
{
if(zoom)
html+="
\n";
else
html+="
\n";
}
document.getElementById("MINIMONSTERS").innerHTML=html;
}

function move_monsters()
{
if(repulse == true)
{
paint_monsters();
return;
}
a=x-5;b=y-5;
c=x+5;d=y+5;

donepos=new Array(27);
for(var i=0;i < 27;i++)
donepos[i]=new Array(11);
for(var i=0;i < 27;i++)
for(var j=0;j < 27;j++)
donepos[i][j]=0;

timeID=setTimeout("move_monsters()",700);

if(a < 0) a=0;
if(b < 0) b=0;
if(c > 99) c=99;
if(d > 99) d=99;

// Display pos
rx=x-3;
ry=y-3;
if(rx < 0)
rx=0;
if(ry < 0)
ry=0;
if(rx > 99-7)
rx=99-7;
if(ry > 99-7)
ry=99-7;

changed=false;

for(var i=a;i <= c;i++)
{
for(var j=b;j <= d;j++)
{
if(mapobj[i][j] < 0 && donepos[i+8-rx][j+8-ry] == 0) // it's a monster... we didn't touched it so far, so let move it
{
changed=true;
e=i;f=j;
if(invisible != 0)
{
switch(Math.round(Math.random()*4))
{
case 0:
if(mapobj[e+1][f] == 0 && check_walkable_map(map[e+1][f],false) == true)
e++;
break;
case 1:
if(mapobj[e-1][f] == 0 && check_walkable_map(map[e-1][f],false) == true)
e--;
break;
case 2:
if(mapobj[e][f+1] == 0 && check_walkable_map(map[e][f+1],false) == true)
f++;
break;
case 3:
if(mapobj[e][f-1] == 0 && check_walkable_map(map[e][f-1],false) == true)
f--;
break;
}
}
else if(e < x && mapobj[e+1][f] == 0 && check_walkable_map(map[e+1][f],false) == true)
e++;
else if(e > x && mapobj[e-1][f] == 0 && check_walkable_map(map[e-1][f],false) == true)
e--;
else if(f < y && mapobj[e][f+1] == 0 && check_walkable_map(map[e][f+1],false) == true)
f++;
else if(f > y && mapobj[e][f-1] == 0 && check_walkable_map(map[e][f-1],false) == true)
f--;
if(e == x && f == y) // The monster attacked us
{
//newloc="game.php?COMBAT="+(-mapobj[i][j])+"&X="+x+"&Y="+y
document.forms["COMM"].COMBAT.value=(-mapobj[i][j]);
document.forms["COMM"].X.value=x;
document.forms["COMM"].Y.value=y;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
mapobj[i][j]=0;
disable_move();
needtostore=false;
//document.location=newloc;
document.forms["COMM"].submit();
return;
}
if(e != i || f != j)
{
mapobj[e][f]=mapobj[i][j];
mapobj[i][j]=0;
if((i-rx) >= 0 && (i-rx) < 7 && (j-ry) >= 0 && (j-ry) < 7)
{
document.images["OBJ_"+(i-rx)+"_"+(j-ry)].src=loader+tileset+"objects/0.gif";
divobj=document.getElementById("OVER_OBJ_"+(i-rx)+"_"+(j-ry));
if(divobj.style != null)
divobj.style.visibility="hidden";
else
divobj.visibility="hidden";
}
if((e-rx) >= 0 && (e-rx) < 7 && (f-ry) >= 0 && (f-ry) < 7)
{
if((-mapobj[e][f]) >= 10000)
document.images["OBJ_"+(e-rx)+"_"+(f-ry)].src=cacheloader+"monsters/monster_"+(-mapobj[e][f])+"_s.png";
else
document.images["OBJ_"+(e-rx)+"_"+(f-ry)].src=loader+"monsters/monster_"+(-mapobj[e][f])+"_s.gif";
divobj=document.getElementById("OVER_OBJ_"+(e-rx)+"_"+(f-ry));
if(divobj.style != null)
divobj.style.visibility="visible";
else
divobj.visibility="visible";
}
//Display();
//return;
donepos[e+8-rx][f+8-ry]=1;
}
}
}
}
if(changed == true)
paint_monsters();
}

function check_map(a,b,px,py)
{
if(map[a][b] == 102)
{
document.forms["COMM"].ACTION.value='DOOR';
document.forms["COMM"].X.value=px;
document.forms["COMM"].Y.value=py;
document.forms["COMM"].DX.value=a;
document.forms["COMM"].DY.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
}
return true;
}

function check_walkable_map(type,flagboat)
{
if(flagboat == true && type >= 52 && type <= 64) // Water
return true;
if(flagboat == false && type >= 121 && type <= 132) // Ice & Water
return true;
if(flagboat == false && type >= 165 && type <= 172) // Ice & Water & Sand
return true;

if(bgwalk.charAt(type) == "N")
return false;
return true;
}

function check_walkable_obj(a,b)
{
type=mapobj[a][b];

if(type < 0) // Moved over a monster
{
document.forms["COMM"].COMBAT.value=(-type);
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
mapobj[a][b]=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
}

switch(type)
{
case 115: // Down
case 117:
if(randommap == true)
{
disable_move();
needtostore=false;
document.location=downlink+"&PREVLOC="+loc;
}
return true;
break;
case 114: // Up
case 118:
if(randommap == true)
{
disable_move();
needtostore=false;
document.location=uplink+"&PREVLOC="+loc;
}
return true;
break;
case 149: // Crystal ball
if(isplayerdongeon == true)
{
document.forms["COMM"].ACTION.value='CRYSTALBALL';
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
}
return false;
case 155: // Piano
case 170: // Organ
document.forms["COMM"].ACTION.value='PLAYORGAN';
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
case 116: // Tresore box
if(isplayerdongeon == true)
{
document.forms["COMM"].ACTION.value='MANAGECHEST';
document.forms["COMM"].OWNER.value=owner;
document.forms["COMM"].LEVEL.value=1;
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
}
else if(randommap == true)
{
if(owner == 0)
{
mapobj[a][b]=0;
harverst("CHEST",a,b);
}
else
{
document.forms["COMM"].ACTION.value='CHEST';
document.forms["COMM"].OWNER.value=owner;
document.forms["COMM"].LEVEL.value=1;
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
}
return true;
}
return true;
case 132: // Firefern (if in underground)
if(randommap == true)
{
mapobj[a][b]=0;
harverst("GETFIREFERN",a,b);
return true;
}
return false;
case 134: // Big stone
if(injail == true)
{
harverst("JAILMETAL",a,b);
return false;
}
else if(randommap == true && weapon == 24)
{
if(hp >= 3)
{
mapobj[a][b]=0;
harverst("GETMETAL",a,b);
hp-=1;
change_hp();
}
else
{
display_message("You are too weak to continue to harvest metals.");
}
return true;
}
else if(randommap == true && weapon != 24)
{
display_message("You must have equiped a pick in order to harverst metals.");
return false;
}
return false;
case 220: // Mytril stone
if(randommap == true && weapon == 24)
{
if(hp >= 3)
{
mapobj[a][b]=0;
harverst("GETMYTRIL",a,b);
hp-=1;
change_hp();
}
else
{
display_message("You are too weak to continue to harvest metals.");
}
return true;
}
else if(randommap == true && weapon != 24)
{
display_message("You must have equiped a pick in order to harverst metals.");
return false;
}
return false;
case 61: // Thumbstone
if(injail == true)
alert("Here lies Tom, He died trying to work off what he stole.\nHis lesson has been learned.\n-The Guard");
return true;
case 5: // Trees
case 57:
case 58:
case 62:
case 63:
if(weapon == 1)
{
if(hp >= 3)
hp-=1;
change_hp();
harverst("GETWOOD",a,b);
}
else
display_message("You must have equiped an hand axe in order to harverst wood.");
return false;
case 138: // Green Egg
if(randommap == true)
{
harverst("GETEGG",a,b);
mapobj[a][b]=0;
return true;
}
return false;
case 139: // Red mushroom
if(randommap == true)
{
harverst("GETREDMUSHROOM",a,b);
mapobj[a][b]=0;
return true;
}
return false;
case 140: // Red mushroom
if(randommap == true)
{
harverst("GETBLUEMUSHROOM",a,b);
mapobj[a][b]=0;
return true;
}
return false;
case 141: // Augmentatium
if(randommap == true)
{
harverst("GETAUGMENT",a,b);
mapobj[a][b]=0;
return true;
}
return false;
case 142:
if(isplayerdongeon == true)
{
document.forms["COMM"].ACTION.value='MANAGEDONGEON';
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
}
return false;
case 124: // Beds
case 125:
case 126:
case 168:
if(isplayerdongeon == true)
{
document.forms["COMM"].ACTION.value='SLEEP';
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
}
return false;
case 187: // Junkyard
harverst("JUNKYARD",a,b);
return false;
case 250: // Trap
document.forms["COMM"].ACTION.value='TRAP';
document.forms["COMM"].X.value=a;
document.forms["COMM"].Y.value=b;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return true;
default:
if(isherbs[type] == true)
{
harverst("HERBS&TYPE="+type,a,b);
mapobj[a][b]=0;
}

if(objwalk.charAt(type) == 'Y')
return true;
return false;
}
}

function GoUp()
{
if(disablemove == true)
return;
if(!monsterstarted)
{
monsterstarted=true;
timeID=setTimeout("move_monsters()",500);
}
var nx=x;
var ny=y;

ny--;
playerdir='n';
if(ny < 0)
ny=0;
ok=true;
if(check_map(nx,ny,x,y) == false)
ok=false;
if(ok == true && (check_walkable_map(map[nx][ny],boat) == false || (mapobj[nx][ny] != 0 && check_walkable_obj(nx,ny) == false)))
ok=false;
else
y=ny;


if(ok == true)
handle_sickness();
}

function GoDown()
{
if(disablemove == true)
return;
if(!monsterstarted)
{
monsterstarted=true;
timeID=setTimeout("move_monsters()",500);
}
var nx=x;
var ny=y;
ny++;
playerdir='s';
if(ny > 98)
ny=98;
ok=true;
if(check_map(nx,ny,x,y) == false)
ok=false;
if(ok == true && (check_walkable_map(map[nx][ny],boat) == false || (mapobj[nx][ny] != 0 && check_walkable_obj(nx,ny) == false)))
ok=false;
else
y=ny;

if(ok == true)
handle_sickness();
}

function GoLeft()
{
if(disablemove == true)
return;
if(!monsterstarted)
{
monsterstarted=true;
timeID=setTimeout("move_monsters()",500);
}

var nx=x;
var ny=y;
nx--;
playerdir='w';
if(nx < 0)
nx=0;
ok=true;
if(check_map(nx,ny,x,y) == false)
ok=false;
if(ok == true && (check_walkable_map(map[nx][ny],boat) == false || (mapobj[nx][ny] != 0 && check_walkable_obj(nx,ny) == false)))
ok=false;
else
x=nx;

if(ok == true)
handle_sickness();
}

function GoRight()
{
if(disablemove == true)
return;
if(!monsterstarted)
{
monsterstarted=true;
timeID=setTimeout("move_monsters()",500);
}
var nx=x;
var ny=y;
nx++;
playerdir='e';
if(nx > 98)
nx=98;
ok=true;
if(check_map(nx,ny,x,y) == false)
ok=false;
if(ok == true && (check_walkable_map(map[nx][ny],boat) == false || (mapobj[nx][ny] != 0 && check_walkable_obj(nx,ny) == false)))
ok=false;
else
x=nx;

if(ok == true)
handle_sickness();
}

function CheckLinks(checkborder)
{
// Check if there is a link at the current pos
for(var i=0;i < linkx.length;i++)
{
if(linkx[i] == x && linky[i] == y)
{
newloc=maplinks[i];
needtostore=false;
needtostoremonsters=false;

//if(inmap[i] == true)
if(maplinks[i].indexOf("&INMAP=TRUE") != -1)
{
document.getElementById("NPCDIALOG").src=newloc+"&PREVLOC="+loc+"&X="+x+"&Y="+y;
if(zoom == true)
{
document.getElementById("NPCDIALOG").style.top=document.body.clientHeight/2+64;
var l=(document.body.clientWidth-185)/2-document.getElementById("NPCDIALOG").clientWidth/2;
document.getElementById("NPCDIALOG").style.left=l+121;
}
else
{
document.getElementById("NPCDIALOG").style.top=339;
document.getElementById("NPCDIALOG").style.left=211;
}
document.getElementById("NPCDIALOG").style.visibility="visible";
inmapdialog=true;
}
else
{
disable_move();
document.location=newloc+"&PREVLOC="+loc;
}
return true;
}
}
// Over a water monster ? Totaly random :-)
if(map[x][y] == 52 && Math.floor(Math.random()*50) == 0)
{
document.forms["COMM"].ACTION.value="WATERCOMBAT";
document.forms["COMM"].X.value=x;
document.forms["COMM"].Y.value=y;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
}
if(mapobj[x][y] == 116 && randommap == false) // Tresor chest !
{
document.forms["COMM"].ACTION.value='CHEST';
document.forms["COMM"].X.value=x;
document.forms["COMM"].Y.value=y;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return true;
}
if(checkborder == true && x == 0 && leftlink != null)
{
disable_move();
needtostore=false;
needtostoremonsters=false;
document.location=leftlink+"&X=98&Y="+y+"&PREVLOC="+loc;
return true;
}
else if(checkborder == true && x == 98 && rightlink != null)
{
disable_move();
needtostore=false;
needtostoremonsters=false;
document.location=rightlink+"&X=0&Y="+y+"&PREVLOC="+loc;
return true;
}
else if(checkborder == true && y == 0 && toplink != null)
{
disable_move();
needtostore=false;
needtostoremonsters=false;
document.location=toplink+"&X="+x+"&Y=98&PREVLOC="+loc;
return true;
}
else if(checkborder == true && y == 98 && bottomlink != null)
{
disable_move();
needtostore=false;
needtostoremonsters=false;
document.location=bottomlink+"&X="+x+"&Y=0&PREVLOC="+loc;
return true;
}
return false;
}

function Display()
{
if(disablemove == true || isinit == false)
return;

var rx=x-3;
var ry=y-3;
if(rx < 0)
rx=0;
if(ry < 0)
ry=0;
if(rx > 99-7)
rx=99-7;
if(ry > 99-7)
ry=99-7;
var ux=x-rx;
var uy=y-ry;

draw_dialogs(rx,ry,x,y);

if(showminimap == true) // Should update the minimap
{
cell=document.getElementById("MINIMAPPOS");
var a=0;
var b=0;
if(zoom)
{
a=x*2;
b=y*2+1;

var w=document.body.clientWidth-200;
if(w > 882) w=882;
a+=w;
}
else
{
a=x*2+644;
b=y*2+324;
}
if(cell.style != null)
{
cell.style.top=b;
cell.style.left=a;
}
else
{
cell.top=b;
cell.left=a;
}
}

for(var i=0;i < 7;i++)
{
for(var j=0;j < 7;j++)
{
document.images["IMG_"+i+"_"+j].src=loader+tileset+"tiles/"+map[i+rx][j+ry]+".gif";

if(mapobj[i+rx][j+ry] == 0 && (i+rx) != x && (j+ry) != y)
document.getElementById("OVER_OBJ_"+i+"_"+j).style.visibility="hidden";
else
document.getElementById("OVER_OBJ_"+i+"_"+j).style.visibility="visible";

if(mapobj[i+rx][j+ry] <= -10000)
document.images["OBJ_"+i+"_"+j].src=cacheloader+"monsters/monster_"+(-mapobj[i+rx][j+ry])+"_s.png";
else if(mapobj[i+rx][j+ry] < 0)
document.images["OBJ_"+i+"_"+j].src=loader+"monsters/monster_"+(-mapobj[i+rx][j+ry])+"_s.gif";
else
document.images["OBJ_"+i+"_"+j].src=loader+tileset+"objects/"+mapobj[i+rx][j+ry]+".gif";
}
}


document.getElementById("OVER_OBJ_"+ux+"_"+uy).style.visibility="visible";
if((invisible > 0 || invisible == -100) && map[x][y] >= 52 && map[x][y] <= 64) // Water
document.images["OBJ_"+ux+"_"+uy].src=cacheloader+"players/"+boattype+"boat_0_"+playerdir+".gif";
else if(invisible > 0 || invisible == -100)
document.images["OBJ_"+ux+"_"+uy].src=cacheloader+"players/player_0_"+playerdir+".gif";
else if(map[x][y] >= 52 && map[x][y] <= 64) // Water
document.images["OBJ_"+ux+"_"+uy].src=cacheloader+"players/"+boattype+"boat_"+iconid+"_"+playerdir+".gif";
else
document.images["OBJ_"+ux+"_"+uy].src=cacheloader+"players/player_"+iconid+"_"+playerdir+".gif";
}
function RPGKeyPress(e)
{
key=e.keyCode;
if(e.keyCode == 0 && e.which != null)
key=String.fromCharCode(e.which);
else if(e.keyCode > 60)
key=String.fromCharCode(e.keyCode);
execKey(key);
}

function execKey(key)
{
if(disablemove == true || inmapdialog == true)
return;

prevx=x;
prevy=y;

switch(key)
{
case 56:
case 38:
case '8':
case 'w':
case 'W':
case 'z':
case 'Z':
GoUp();
break;
case 'a':
case 'A':
case 'q':
case 'Q':
case '4':
case 52:
case 37:
GoLeft();
break;
case 'd':
case 'D':
case '6':
case 54:
case 39:
GoRight();
break;
case 'x':
case 'X':
case '2':
case 40:
case 50:
GoDown();
break;
}
Display();
if((comedir == 0 && y == 0 && y == prevy) || (comedir == 1 && x == 0 && x == prevx) || (comedir == 2 && x == 98 && x == prevx) || (comedir == 3 && y == 98 && y == prevy))
CheckLinks(false);
else if(prevx != x || prevy != y)
CheckLinks(true);
}

function str2nb(str,pos)
{
return (str.charCodeAt(pos)-97)*26+(str.charCodeAt(pos+1)-97);
}

function nb2str(nb)
{
return String.fromCharCode(Math.floor(nb/26)+97,(nb%26)+97);
}

function store_monsters()
{
str="";
for(var i=0;i < 100;i++)
for(var j=0;j < 100;j++)
if(mapobj[i][j] < 0)
str=str+nb2str(i)+nb2str(j)+nb2str(Math.floor(-mapobj[i][j]/600))+nb2str((-mapobj[i][j])%600);
document.cookie="monsters="+nb2str(Math.floor(Math.abs(loc)/600))+nb2str(Math.abs(loc)%600)+str;
}

function known_monster(c)
{
for(var i=0;i < monsters.length;i++)
if(monsterid[i] == c)
return true;
return false;
}

function restore_monsters()
{
str=getCookie("monsters");
if(str == null)
return false;
oldloc=str2nb(str,0)*600+str2nb(str,2);
if(oldloc != Math.abs(loc))
return false;
for(var i=4;i < str.length;i+=8)
{
a=str2nb(str,i);
b=str2nb(str,i+2);
c=str2nb(str,i+4)*600+str2nb(str,i+6);

if(mapobj[a][b] != 0 || check_walkable_map(map[a][b],false) == false || !known_monster(c))// This location is not valid... cleanup the mess and say no :-)
{
for(var j=0;j != 100;j++)
for(var k=0;k != 100;k++)
if(mapobj[j][k] < 0)
mapobj[j][k] == 0;
return false;
}
mapobj[a][b]=-c;
}
return true;
}

function store_pos()
{
var http;
var url;

if(needtostoremonsters == true)
store_monsters();
else
document.cookie="monsters=";

if(needtostore == false)
return;

try
{
http = new XMLHttpRequest();
}
catch (e)
{
try
{
http = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
http=false;
}
}
if(!http)
{
return;
}
if(invisible != -100)
url = 'game.php?ACTION=STORE&LOC='+loc+'&X='+x+'&Y='+y+"&INVISIBILITY="+invisible;
else
url = 'game.php?ACTION=STORE&LOC='+loc+'&X='+x+'&Y='+y+"&INVISIBILITY=0";
http.open("GET", url);
http.onreadystatechange = function ()
{
if (http.readyState==4)
{
ok=true;
}
}

http.send(null);
}

function handle_sickness()
{
if(invisible > 0)
{
invisible--;
if(invisible == 0)
{
harverst("LOSTINVISIBLE",x,y);
}
}

if(sickness == 0)
return;
nbsteps++;
if(nbsteps > sickness)
{
hp--;
change_hp();
if(hp <= 0)
{
needtostore=false;
//document.location="game.php?ACTION=DIE";
document.forms["COMM"].ACTION.value='DIE';
disable_move();
needtostore=false;
document.forms["COMM"].submit();
}
else
harverst("SICKNESS",x,y);
nbsteps=0;
}
}

function harverst(what,a,b)
{
var http;
var url;

disablemove=true;
msgwin=document.getElementById("MESSAGES");
msgwin.style.backgroundColor="#FFA0A0";
msgwin.innerHTML="Message window:

\nWaiting for an answer...";

try
{
http = new XMLHttpRequest();
}
catch (e)
{
try
{
http = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
http=false;
}
}
if(!http)
{
return;
}
if(timeID != 0)
clearTimeout(timeID);
url = 'game.php?ACTION='+what+'&LOC='+loc+'&X='+a+'&Y='+b;
http.open("GET", url);
http.onreadystatechange = function ()
{
if (http.readyState==4)
{
if(http.responseText.substring(0,13) == "##RELOAD PAGE")
{
alert(http.responseText.substring(14));
document.location='game.php';
}
else
{
disablemove=false;
Display();
msgwin=document.getElementById("MESSAGES");
msgwin.innerHTML="Message window:

\n"+http.responseText;
msgwin.style.backgroundColor="#E0E0E0";
timeID=setTimeout("move_monsters()",700);
}
}
}

http.send(null);
}

function normal_message_color()
{
msgwin=document.getElementById("MESSAGES");
msgwin.style.backgroundColor="#E0E0E0";
}

function display_message(msg)
{
msgwin=document.getElementById("MESSAGES");
msgwin.innerHTML="Message window:

\n"+msg;
msgwin.style.backgroundColor="#FFA0A0";
setTimeout("normal_message_color()",250);
}

function link_to(q)
{
document.forms["COMM"].ACTION.value=q;
document.forms["COMM"].X.value=x;
document.forms["COMM"].Y.value=y;
if(invisible != -100)
document.forms["COMM"].INVISIBILITY.value=invisible;
else
document.forms["COMM"].INVISIBILITY.value=0;
disable_move();
needtostore=false;
document.forms["COMM"].submit();
return false;
}

function kDown(e)
{
e = e?e:event;
if(disablemove)
return;
RPGKeyPress(e);

// IE
try
{
e.cancelBubble = true;
e.returnValue = false;
}
catch(er)
{
}
// Firefox
try
{
e.preventDefault();
e.stopPropagation();
}
catch(er)
{
}
return false;
}

function kUp(e)
{
e = e?e:event;

// IE
try
{
e.cancelBubble = true;
e.returnValue = false;
}
catch(er)
{
}
// Firefox
try
{
e.preventDefault();
e.stopPropagation();
}
catch(er)
{
}
return false;
}

function kPress(e)
{
// IE
try
{
e.cancelBubble = true;
e.returnValue = false;
}
catch(er)
{
}
// Firefox
try
{
e.preventDefault();
e.stopPropagation();
}
catch(er)
{
}
}

function init()
{
if(isinit == true)
return;

isinit=true;

if (!document.addEventListener && document.attachEvent) {
document.addEventListener = function (t, f) {
document.attachEvent("on"+ t, f);
};
document.removeEventListener = function (t, f) {
document.detachEvent("on"+ t, f);
};
}
if (typeof kDown == "function") document.addEventListener("keydown", kDown, false);
if (typeof kUp == "function") document.addEventListener("keyup", kUp, false);
document.onkeypress=kPress;
document.onclick=null;
}

Wyszukiwarka