

// Note that Javascript commands are case sensitive

// Start Determine Broswer Type
netscape = (document.layers) ? 1 : 0;
internetexplorer = (document.all) ? 1 : 0;
// Scrap that part, anyway it only works in IE
// End Determine Broswer Type


// The three following may be adjusted to fit your needs
var step_inc=3;   // size of steps in pixels
var step_time=175; // periods of steps in milliseconds
var tolerance_letter=60/100; // radius of the ellipse the points are accepted, in proportion of half side of the squares
//var quantity_of_time_to_wait=1000*(0.5*3600+1*Math.round(2*3600*Math.random())); // that should be half an hour plus a random time between 0 and 2 hours
//var quantity_of_time_to_wait=5000; // 5 sec for testing purpose
var quantity_of_time_to_wait=12*60*1000; // 12 minutes


var script_activation=0; // if 1 then script already was launched and shoudln't be launched again
var sprite_direction = 4; // 0 to 7, direction the sprite is facing
var sprite_moving = 1;    // 1 if sprite is moving, 0 if standing still
var raw_key=0;
var str_key="no key pressed yet";
var step_num=-1;  // number of steps before doing the waypoint test (-1 for infinite)

var sprite_x=0;
var sprite_y=0;
var obj_x=0;
var obj_y=0;

var unit_eight=0;   // temp var for the 8 patrol creation
var margin_eight=0; // temp var for the 8 patrol creation

var list_x=new Array() // x of orders
var list_y=new Array() // y of orders
var list_o=new Array() // type orders

var list_click_x=new Array(-10,-50,-50,-50,-50,-50,-50,-50,-10)
var list_click_y=new Array(-10,-50,-50,-50,-50,-50,-50,-50,-10)


/////////////////////////////
//                         //
// 640x480 screenshot      //
//                         //
// Cropping Rectangle:     //
//                         //
// Left: 432  Right: 495   //
// Top: 260   Bottom: 323  //
//                         //
/////////////////////////////


///////////
//       //
//  012  //
//  7 3  //
//  654  //
//       //
///////////






function wait_hours() {
  //status="Hehe";
  //document.onkeypress=process_click;
  //document.onclick=process_click;
  //keepthefocus();

  setTimeout("begin_walking()",quantity_of_time_to_wait); // in milliseconds
}


function keepthefocus() {      // not used anymore because it's too annoying
  if (script_activation==0) {
    bigspan.focus();
    spriteimg.src="nothingreally.gif";
    bigspan.onkeypress=process_click;
    bigspan.onclick=process_click;
    setTimeout("keepthefocus()",1000);
  }
}


function begin_walking() {
  if (script_activation==0) {
    script_activation=1;
    document.onkeypress=process_key;
    update_gfx();
    patrol_eight();
    walking();
    //status="The Commander is comming !";
  }
}



function process_key(e) {

  raw_key=(document.layers)?e.which:window.event.keyCode;
  str_key=String.fromCharCode(raw_key);

  if (str_key=="a" || str_key=="q")
    {sprite_direction-=1;step_num=-1;}

  if (str_key=="z" || str_key=="w")
    {sprite_direction+=1;step_num=-1;}

  if (str_key=="m") {
    mouse_x= window.event.x+document.body.scrollLeft;
    mouse_y= window.event.y+document.body.scrollTop;
    list_x=[mouse_x-32]; // for unknow reason new Arrary(value) won't work
    list_y=[mouse_y-32]; // for unknow reason new Arrary(value) won't work
    list_o=["m"];  // for unknow reason new_arrary("m") does work
    calc_dir_to_obj();
  }


  if (str_key=="M") {
    mouse_x= window.event.x+document.body.scrollLeft;
    mouse_y= window.event.y+document.body.scrollTop;
    list_x[list_x.length]=mouse_x-32;
    list_y[list_y.length]=mouse_y-32;
    list_o[list_o.length]="m";
    if (sprite_moving==0) { // if sprite was standing still
      calc_dir_to_obj(); // moving to waypoint
    }
  }


  if (str_key=="p") {
    mouse_x= window.event.x+document.body.scrollLeft;
    mouse_y= window.event.y+document.body.scrollTop;
    list_x=[mouse_x-32,sprite_x];
    list_y=[mouse_y-32,sprite_y];
    list_o=["p","p"];
    calc_dir_to_obj(); // moving to waypoint
  }


  if (str_key=="P") {
    mouse_x= window.event.x+document.body.scrollLeft;
    mouse_y= window.event.y+document.body.scrollTop;
    list_x[list_x.length]=mouse_x-32;
    list_y[list_y.length]=mouse_y-32;
    list_o[list_o.length]="p";
    if (list_o.length==1) {
      if (sprite_moving!=0)
        {alert("Objective Forgotten !");}
      list_x[list_x.length]=obj_x;
      list_y[list_y.length]=obj_y;
      list_o[list_o.length]="p";
    }
    calc_dir_to_obj();
  }

  if (str_key=="e")
    {patrol_eight();}

  if (str_key=="s") {
    list_o=[];
    list_x=[];
    list_y=[];
    sprite_moving=0;
  }

  update_gfx();
}




function calc_dir_to_obj() {

  obj_x=list_x[0];
  obj_y=list_y[0];
  obj_o=list_o[0]; //shouldn't be needed

  dist_x=Math.abs(obj_x-sprite_x);
  dist_y=Math.abs(obj_y-sprite_y);

  if ( (dist_x<=step_inc) && (dist_y<=step_inc) ) { // if arrived

    // Start rotating or removing previous order

    //alert("Before:"+list_o);
    obj_x=list_x.shift(); // remove and return first element of array
    obj_y=list_y.shift(); // remove and return first element of array
    obj_o=list_o.shift(); // remove and return first element of array
    //alert("After:"+list_o);

    if (list_o.length==1) { // if only two orders enqueued (one already taken)
      if (obj_o=="m" && list_o[0]=="p") { // if they are move (just finished) then 1 patrol
        obj_o="p";}} // then change them to patrol between two points

    if (obj_o=="p") { // if patrol then put order back in the end
      list_x[list_x.length]=obj_x;
      list_y[list_y.length]=obj_y;
      list_o[list_o.length]="p";
    }

    // End rotating or removing previous order

  } // end if arrived


  //alert("list_o.length=="+list_o.length);

  if (list_o.length==0)
    {sprite_moving=0;}  // all orders done

  else {

    obj_x=list_x[0];
    obj_y=list_y[0];
    obj_o=list_o[0]; //shouldn't be needed

    dist_x=Math.abs(obj_x-sprite_x);
    dist_y=Math.abs(obj_y-sprite_y);

    if ( Math.max(dist_x,dist_y)>2*Math.min(dist_x,dist_y) ) { // straight then diagonal

      if (dist_y>dist_x)
        {sprite_direction=1+4*(obj_y>sprite_y);}
      else
        {sprite_direction=7-4*(obj_x>sprite_x);}

      step_num=Math.round(Math.max(dist_x,dist_y)/step_inc)-Math.round(Math.min(dist_x,dist_y)/step_inc);

    } else { // diagonal then straight

      sprite_direction=2*(obj_x>sprite_x)*(obj_y<sprite_y)+(4+2*(obj_x<sprite_x))*(obj_y>sprite_y);

      step_num=Math.round(Math.min(dist_x,dist_y)/step_inc);

    }

    sprite_moving=1;

    if (step_num==0) {
      sprite_moving=0;
      //alert("Trying to move zeros steps !");
      setTimeout("calc_dir_to_obj()",step_time); // Wait a step and go next waypoint
    }

  }
  update_gfx();
}




function walking() {

  if (sprite_moving) {

    sprite_x= parseInt(spritespan.style.left);
    sprite_y= parseInt(spritespan.style.top);

    if ((sprite_direction==0) || (sprite_direction==1) || (sprite_direction==2))
      {sprite_y-=step_inc;}
    if ((sprite_direction==6) || (sprite_direction==5) || (sprite_direction==4))
      {sprite_y+=step_inc;}
    if ((sprite_direction==0) || (sprite_direction==7) || (sprite_direction==6))
      {sprite_x-=step_inc;}
    if ((sprite_direction==2) || (sprite_direction==3) || (sprite_direction==4))
      {sprite_x+=step_inc;}

    spritespan.style.left = sprite_x;
    spritespan.style.top = sprite_y;

    if (step_num>0) {
      step_num-=1;
      if (step_num==0)
        {calc_dir_to_obj();}
    }

  } // endif sprite_moving

  setTimeout("walking()",step_time); // milliseconds
}



function update_gfx() {

  if (sprite_direction<0)
    {sprite_direction+=8;}
  else if (sprite_direction>7)
    {sprite_direction-=8;}

  if (sprite_moving==0)
    {spriteimg.src="http://zwzsg.hosted.luckz.de/img/banner/MovingComm/truc/Still"+sprite_direction+".gif";}
  else
    {spriteimg.src="http://zwzsg.hosted.luckz.de/img/banner/MovingComm/truc/MovF"+sprite_direction+".gif";}

  //status="raw_key="+raw_key+"   "+"str_key="+str_key+"   "+"direction="+sprite_direction+" pos=("+sprite_x+","+sprite_y+") queue: list_o="+list_o+" list_y="+list_y+" list_x="+list_x;
}




function patrol_eight() {

  playgroundlength=document.body.offsetWidth-64; // 64 == sprite_size
  eightpatroltopoffset=document.body.scrollTop;

  unit_eight=Math.floor(playgroundlength/6);
  if (unit_eight>10/step_inc) // if 10 or more steps to walk, then we can shrink area used by eight
    {unit_eight=Math.floor(unit_eight*75/100)};
  margin_eight=Math.round((playgroundlength-6*unit_eight)/2);

  list_x=[5,6,7,7,6,5,4,4,3,2,1,1,2,3,4,4];
  list_y=[4,4,3,2,1,1,2,3,4,4,3,2,1,1,2,3];
  list_o=["p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
  for (ke=0; ke<list_o.length; ke++)
    {list_x[ke]=margin_eight+unit_eight*(list_x[ke]-1);
     list_y[ke]=margin_eight+unit_eight*(list_y[ke]-1)+eightpatroltopoffset;}

  if (parseInt(spritespan.style.left)==-70) { // if it is the first time
    spritespan.style.left=-70;
    spritespan.style.top=-70+eightpatroltopoffset;
    list_x=list_x.concat(list_x,list_x.slice(0,5));
    list_y=list_y.concat(list_y,list_y.slice(0,5));
    list_o=list_o.concat(list_o,list_o.slice(0,5));
    list_x[list_x.length]=-quantity_of_time_to_wait*step_inc/step_time;
    list_y[list_y.length]=-quantity_of_time_to_wait*step_inc/step_time+eightpatroltopoffset;
    list_o[list_o.length]="p";
  }
  sprite_x=parseInt(spritespan.style.left); // that initialisation is not very well placed
  sprite_y=parseInt(spritespan.style.top); // that initialisation is not very well placed
  calc_dir_to_obj(); // moving to waypoint
}



///////////////////////
//                   //
//  1    __   __     //
//  2   /  \ /  \    //
//  3   |   |    |   //
//  4   \__/ \__/    //
//                   //
//      12 345  67   //
///////////////////////



function showhelpspan() {
    // hotfix of the help span content :
    document.all.helpspan.innerHTML="<pre style='font-size:12px; font-family:Arial;'><b><font color=#004000><p align='right'>X</p><p align='left'>   Controles :</p><br><p align='left'>   m: <font color=#800000>m</font>ouvement jusqu'au point où est la souris<br>   p: <font color=#800000>p</font>atrouillle entre le point actuel et le pointeur de souris&nbsp;&nbsp;<br>   s: <font color=#800000>s</font>toppe<br>   e: Faire un b<font color=#800000>e</font>au huit !<br><br>   Maj M: mettre en queue un point de <font color=#800000>M</font>ouvement<br>   Shift P: mettre en queue un point de <font color=#800000>P</font>atrouille</p><br><p align='left'>   Cliquer sur le commandeur s'il ne répond pas !</p></font></b></pre>";
    helpspan.style.left=document.body.offsetWidth-helpspan.clientWidth-40; // Dunno why it goes a little bit outside without the -40
    helpspan.style.top=document.body.scrollTop;
}

function hidehelpspan() {
    helpspan.style.left=0;
    helpspan.style.top=-500;
}






function process_click() {

  if (script_activation) {
    2+2; // do nothing
  } else {

    list_click_x.shift(); // remove first element
    list_click_y.shift(); // remove first element
    list_click_x[list_click_x.length]=window.event.screenX; // add end element
    list_click_y[list_click_y.length]=window.event.screenY; // add end element


    // (clx,cly) == center of the letter
    // (slx,sly) == half-length of sides


    //////////////////////
    //                  //
    //  1-2  5   7   9  //
    //   /    \ / \ /   //
    //  3-4    6   8    //
    //                  //
    //////////////////////


    // Don't be fooled by lists indiced from 0 to length-1
    y4=list_click_y[3];
    y3=list_click_y[2];
    y2=list_click_y[1];
    y1=list_click_y[0];
    x4=list_click_x[3];
    x3=list_click_x[2];
    x2=list_click_x[1];
    x1=list_click_x[0];

    clx=((x1+x2+x3+x4)/4);
    cly=((y1+y2+y3+y4)/4);

    slx=Math.max((Math.abs(x2-x1)+Math.abs(x4-x3))/4,1); // at least one pixel
    sly=Math.max((Math.abs(y3-y1)+Math.abs(y4-y2))/4,1); // at least one pixel

    postol1=Math.sqrt(Math.pow((clx-slx-x1)/slx,2)+Math.pow((cly-sly-y1)/sly,2));
    postol2=Math.sqrt(Math.pow((clx+slx-x2)/slx,2)+Math.pow((cly-sly-y2)/sly,2));
    postol3=Math.sqrt(Math.pow((clx-slx-x3)/slx,2)+Math.pow((cly+sly-y3)/sly,2));
    postol4=Math.sqrt(Math.pow((clx+slx-x4)/slx,2)+Math.pow((cly+sly-y4)/sly,2));

    //////////////////////
    //                  //
    //  1-2  5   7   9  //
    //   /    \ / \ /   //
    //  3-4    6   8    //
    //                  //
    //////////////////////

    // Z done, now on to the W

    // Lists start at 0
    y9=list_click_y[8];
    y8=list_click_y[7];
    y7=list_click_y[6];
    y6=list_click_y[5];
    y5=list_click_y[4];
    x9=list_click_x[8];
    x8=list_click_x[7];
    x7=list_click_x[6];
    x6=list_click_x[5];
    x5=list_click_x[4];

    clx=(x5+x9)/2;
    cly=(y5+y9+y6+y8)/4;

    slx=Math.max(Math.abs(x9-x5)/2,1); // at least one pixel
    sly=Math.max((Math.abs(y6-y5)+Math.abs(y8-y9))/4,1); // at least one pixel

    poss_x6=Math.max(clx-slx,Math.min(clx,x6)) // like x6, but with lower limit clx-slx and upper limit clx
    poss_y7=Math.max(cly-sly,Math.min(cly+sly,y7)) // like y7, limited bewteen cly-sly and cly+sly
    poss_x8=Math.max(clx,Math.min(clx+slx,x8)) // like x8, but with lower limit and upper limit clx+slx


    postol5=Math.sqrt(Math.pow((clx-slx-x5)/slx,2)+Math.pow((cly-sly-y5)/sly,2));
    postol6=Math.sqrt(Math.pow((poss_x6-x6)/slx,2)+Math.pow((cly+sly-y6)/sly,2));
    postol7=Math.sqrt(Math.pow((clx-x7)/slx,2)+Math.pow((poss_y7-y7)/sly,2));
    postol8=Math.sqrt(Math.pow((poss_x8-x8)/slx,2)+Math.pow((cly+sly-y8)/sly,2));
    postol9=Math.sqrt(Math.pow((clx+slx-x9)/slx,2)+Math.pow((cly-sly-y9)/sly,2));

    // W done

    postol=Math.max(postol1,postol2,postol3,postol4,postol5,postol6,postol7,postol8,postol9);

    //status="Max="+Math.round(100*postol)+"  Z= "+Math.round(100*postol1)+" / "+Math.round(100*postol2)+" / "+Math.round(100*postol3)+" / "+Math.round(100*postol4)+" and W= "+Math.round(100*postol5)+" / "+Math.round(100*postol6)+" / "+Math.round(100*postol7)+" / "+Math.round(100*postol8)+" / "+Math.round(100*postol9);

    if (postol<tolerance_letter) {
      begin_walking();
      list_x=list_x.slice(0,15);
      list_y=list_y.slice(0,15);
      list_o=list_o.slice(0,15);
    }
  }
}

