function onActionServerSide(action, ifvar){
  //if (action=="hurt"){
    if (player.clientr.health<=0){
      player.clientr.health=0;
      player.triggerClient("gui", name, NULL);
      sleep 10;
      player.clientr.health=100;
    }
  //}
  if (action=="heal"){
    //player.clientr.health=100;
    if (player.clientr.health<100){
      player.clientr.health=player.clientr.health+15;
    }
    if (player.clientr.health>100){
      player.clientr.health=100;
    }
  }
}

//#CLIENTSIDE
function onCreated(){
  disabledefmovement();
  //gani/movement vars
  this.frozen=1;
  this.arrowspressed=0;
  this.frozegani=0;
  this.walkspeed=0.4;
  this.dontmove=0;
  this.pushx="";
  this.pushy="";
  this.pushtimer=0;
  this.oldwalk="walk";
  player.client.walk="walk";  player.client.idle="idle";
  ///hud vars
  this.hudout=1;
  this.inv={"","","","","","","","","","","","",""};this.inv[0]="Gun"; this.inv[1]="Gun"; this.inv[2]="Machete"; this.inv[3]="Soda"; this.inv[4]="Rain";
  //attacks
  this.punch=1;
  this.punchrecharge=0;
  //heart monitor/health vars
  this.damtextrem=0;
  this.damtext=0;
  this.wave=0;
  this.heartspeed=14;
  this.nextbeat=1;
  setarray hb,28;
  for(temp.n=0;temp.n<=28;temp.n++){
    hb[n]=0;
  }
  this.healthhurt=clientr.health;
  //disables default hud/health/systems
  enablefeatures(allfeatures-(4)-(2));  
  showstats(allstats & ~( 0x200 ) & ~( 2 ) &~( 4 )&~( 8 )&~( 0x10 )&~( 0x20 )&~( 1 )&~( 0x40 )); 
  if (clientr.health<=0){
    triggerserver("gui", this.name, "hurt", ifhurt);
  }
  setarray dampl,50;
  setarray damplcheck,50;
  setarray damt,30;
  setarray damtx,30;
  setarray damty,30;
  for(temp.n=0;temp.n<230;temp.n++){
    damt[n]="";
  }
  onTimeout();
} 
function onPlayerChats(){
  if (player.chat=="test"){
    triggerserver("gui", this.name, "heal");
  }
}
///////////still gani control/getting hurt///////////////
public function onOncePlay(temp.gani,temp.freezepl,ifhurt){
  setani(player.client.idle,NULL);
  setani(temp.gani,NULL);
  this.frozegani=1;
  this.dontmove=freezepl;
  if (ifhurt>0){
    triggerserver("gui", this.name, "hurt", ifhurt);
  }
  onTimeout();
}
function onTimeout(){
  //////////////gani control///////////////////
  if (this.frozegani==0){
    if (keydown(0) || keydown(1) || keydown(2) || keydown(3)){
      setani(player.client.walk,NULL);
    }else{
      setani(player.client.idle,NULL);
    }
  }
  /////////////arrowkeys/movement//////////////
  if (this.dontmove<=0 && this.frozen==1){
  if (keydown(0)){
    player.dir=0;
    for(temp.n=0.2;temp.n<=1.8;temp.n=temp.n+0.2){
      if (onwall(player.x+0.5+n,player.y+0.8)){
        temp.wall0=1;
      }
    }
    if (temp.wall0!=1) player.y=player.y-this.walkspeed;
    this.frozegani=0;
  }
  if (keydown(1)){
    player.dir=1;
    for(temp.n=0.2;temp.n<=1.8;temp.n=temp.n+0.2){
      if (onwall(player.x+0.2,player.y+1+n)){
        temp.wall1=1;
      }
    }
    if (temp.wall1!=1) player.x=player.x-this.walkspeed;
    this.frozegani=0;
  }
  if (keydown(2)){
    player.dir=2;
    for(temp.n=0.2;temp.n<=1.8;temp.n=temp.n+0.2){
      if (onwall(player.x+0.5+n,player.y+3.2)){
        temp.wall2=1;
      }
    }
    if (temp.wall2!=1) player.y=player.y+this.walkspeed;
    this.frozegani=0;
  }
  if (keydown(3)){
    player.dir=3;
    for(temp.n=0.2;temp.n<=1.8;temp.n=temp.n+0.2){
      if (onwall(player.x+2.7,player.y+1+n)){
        temp.wall3=1;
      }
    }
    if (temp.wall3!=1) player.x=player.x+this.walkspeed;
    this.frozegani=0;
  }
  
  for(temp.n=0.2;temp.n<=1.8;temp.n=temp.n+0.2){
    if (onwall(player.x+0.5+n,player.y+1) && wall0==1) wall0=2;
    if (onwall(player.x+0.4,player.y+1+n) && wall1==1) wall1=2;
    if (onwall(player.x+0.5+n,player.y+3) && wall2==1) wall2=2;
    if (onwall(player.x+2.5,player.y+1+n) && wall3==1) wall3=2;
  }
    if (wall0==1) player.y=player.y-0.1;
    if (wall1==1) player.x=player.x-0.1;
    if (wall2==1) player.y=player.y+0.1;
    if (wall3==1) player.x=player.x+0.1;
    
    if (player.client.walk!="push") this.oldwalk=player.client.walk;
    if ((player.ani=="walk" || player.ani=="push") && player.x==this.pushx && player.y==this.pushy && player.ani!="swim"){
      if (this.pushtimer<15) this.pushtimer=this.pushtimer+1;
      else player.client.walk="push";
    }else{
      this.pushtimer=0;
      player.client.walk=this.oldwalk;
    }
    //else player.client.walk="walk";
    this.pushx=player.x;
    this.pushy=player.y;
  }
  ///////////////////////////Skill Display
  if (player.level!="frontier_menu.nw" && player.level!="skill_select.nw"){
    showimg(500,"frontier_hud1.png",57,220);
  changeimgvis(500,9);
  drawoverplayer();
  for(temp.a=0;temp.a<5;temp.a++){
    showimg(501+a,"frontier_test.png",67+20*a,230);
    changeimgvis(501+a,9);
    if (mousescreenx>67+20*a && mousescreenx<67+15+20*a && mousescreeny>230 && mousescreeny<230+15){
      if (temp.a==0) showText(601+a, mousescreenx+15, mousescreeny,"Adventure","b", clientr.skill1).layer=4;  
      if (temp.a==1) showText(601+a, mousescreenx+15, mousescreeny,"Adventure","b", clientr.skill2).layer=4;    
      if (temp.a==2) showText(601+a, mousescreenx+15, mousescreeny,"Adventure","b", clientr.skill3).layer=4;    
      if (temp.a==3) showText(601+a, mousescreenx+15, mousescreeny,"Adventure","b", clientr.skill4).layer=4;    
      if (temp.a==4) showText(601+a, mousescreenx+15, mousescreeny,"Adventure","b", clientr.skill5).layer=4;      
      changeimgvis(601+a,9);
      with(findimg(601+a)){
        zoom=0.75;  
        textshadow=true;   
      }
    }else{
      showText(601+a, mousescreenx+15, mousescreeny,"Adventure",NULL, "").layer=4;
    }
  }  
  ////////////////Heart Monitor///////////////////// 
  if (clientr.health<0){
    for(temp.n=0;temp.n<=28;temp.n++){
      hb[n]=0;
    }
    triggerserver("gui", this.name, NULL); 
  }
  this.heartspeed=14;
  if (clientr.health<=50) this.heartspeed=10;
  if (clientr.health<=20) this.heartspeed=5;
  this.wave=this.wave+4;
  if (this.wave>112) this.wave=0;
  this.nextbeat=this.nextbeat+1;
  if (this.nextbeat>=this.heartspeed && clientr.health>0){
    hb[this.wave/4]=2;
    hb[this.wave/4+1]=-40;
    hb[this.wave/4+2]=30;
    hb[this.wave/4+3]=-5;
    hb[this.wave/4+4]=-1;
    this.nextbeat=0;
  }
  hb[0]=0; hb[27]=0;
  for(temp.n=0;temp.n<=112;temp.n=temp.n+4){
    showpoly(3000+n,{50+n,80+64+hb[n/4-1],50+n+4,80+64+hb[n/4]});
    changeimgvis(3000+n,9);
    if (this.wave==n){ temp.m=n; }
    with(findimg(3000+n)){  
      mode=1; alpha=(n/300-m/112)*3;
    }
    this.wave2=112+(this.wave-112);
    if (this.wave2==n && hb[n/4]==0){ hb[n/4-1]=0; hb[n/4]=0; hb[n/4+1]=0; hb[n/4+2]=0;hb[n/4+3]=0;hb[n/4+4]=0; }
  }
  showimg(350,"frontier_hudmoniter.png",50,80); 
  showpoly(349,{50,70,50,70-10,   50+125-(100-clientr.health)*1.25, 70-10, 50+125-(100-clientr.health)*1.25,70}); 
  changeimgvis(350,9);
  changeimgvis(349,9);
  with(findimg(349)){
    blue=0; green=0;
  }
 }else{
  //////////////////////////////////UNSHOWS TOP LEFT HUD//////////////////////
  for(temp.n=0;temp.n<=112;temp.n=temp.n+1){
    showimg(3000+n,"",0,0);
    showimg(500+n,"",0,0);
  }
  showimg(350,"",0,0); showpoly(349,{0,0,0,0});
  showText(601+a, mousescreenx+15, mousescreeny,"Adventure",NULL, "").layer=4;
  setani("frontierinvisible",NULL);
  enablefeatures(allfeatures-(4)-(2)-(0x80)-(0x200)-(0x800));  
}
  
  ////////////////Item Selection Menu//////////////////////////
  if (this.hudout==2){
    showimg(351,"frontier_itemselect.png",player.x-2.5,player.y+3.5);
    showimg(362,"frontier_itemselect.png",player.x-2.5,player.y+5.3);
    showimg(363,"frontier_itemselect.png",player.x-2.5,player.y+7.1);
    showimg(352,"frontier_itemselected.png",player.x+0.5,player.y-2.5);
    showimg(353,findweapon(this.inv[0]).icon,player.x+0.5,player.y-2.4);
    showimg(354,findweapon(this.inv[1]).icon,player.x-2.4,player.y+3.6);
    showimg(355,findweapon(this.inv[2]).icon,player.x-0.5,player.y+3.6);
    showimg(356,findweapon(this.inv[3]).icon,player.x+1.4,player.y+3.6);
    showimg(357,findweapon(this.inv[4]).icon,player.x+3.4,player.y+3.6);
    
    showimg(358,findweapon(this.inv[5]).icon,player.x-2.4,player.y+5.4);
    showimg(359,findweapon(this.inv[6]).icon,player.x-0.5,player.y+5.4);
    showimg(360,findweapon(this.inv[7]).icon,player.x+1.4,player.y+5.4);
    showimg(361,findweapon(this.inv[8]).icon,player.x+3.4,player.y+5.4);
    
    showimg(364,findweapon(this.inv[9]).icon,player.x-2.4,player.y+7.2);
    showimg(365,findweapon(this.inv[10]).icon,player.x-0.5,player.y+7.2);
    showimg(366,findweapon(this.inv[11]).icon,player.x+1.4,player.y+7.2);
    showimg(367,findweapon(this.inv[12]).icon,player.x+3.4,player.y+7.2);
  }else{
    for(temp.i=1; temp.i<=18; temp.i++){
      showimg(350+i,"",0,0);
    }
  }
  
  ////////////////Recharge/DamText Fade//////////////////////////////
  if (this.punchrecharge>0) this.punchrecharge=this.punchrecharge-1;
  if (this.dontmove>0) this.dontmove=this.dontmove-0.05;

  ////////////////ifhurt//////////////////////////////////
   if (this.healthhurt!=clientr.health && clientr.health>0-(this.healthhurt-clienr.health)){
   if (clientr.health!=100){
      temp.a=this.healthhurt;
      temp.hh=this.healthhurt-clientr.health;
      this.healthhurt=clientr.health;
      if (temp.a>clientr.health){
        findweapon("-Movement").onOncePlay("hurt",0.1,hh); 
      }else{
        findweapon("-Movement").onOncePlay(player.client.idle,0.1,hh); 
      }
    }else{
      this.healthhurt=clientr.health;
      findweapon("-Movement").onOncePlay(player.client.idle,0.3,hh); 
    }
  }

  settimer(0.05);
}

///////////////////////Mouse Use////////////////////////////
/////////////////////Inventory Select//////////////////////
function GraalControl.onMouseUp(){
  if (this.hudout==2){
    if (mousey>=player.y+3.5 && mousey<=player.y+5.5 && mousex>=player.x-2.5 && mousex<=player.x+5.5){
       if (mousex<player.x-0.5){temp.invhold=this.inv[1]; this.inv[1]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x-0.5 && mousex<=player.x+1.5){temp.invhold=this.inv[2]; this.inv[2]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x+1.5 && mousex<=player.x+3.5){temp.invhold=this.inv[3]; this.inv[3]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x+3.5 && mousex<=player.x+5.5){temp.invhold=this.inv[4]; this.inv[4]=this.inv[0]; this.inv[0]=temp.invhold;}
    }
    if (mousey>player.y+5.5 && mousey<=player.y+7.5 && mousex>=player.x-2.5 && mousex<=player.x+5.5){
       if (mousex<=player.x-0.5){temp.invhold=this.inv[5]; this.inv[5]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x-0.5 && mousex<=player.x+1.5){temp.invhold=this.inv[6]; this.inv[6]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x+1.5 && mousex<=player.x+3.5){temp.invhold=this.inv[7]; this.inv[7]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x+3.5 && mousex<=player.x+5.5){temp.invhold=this.inv[8]; this.inv[8]=this.inv[0]; this.inv[0]=temp.invhold;}    
    }
    if (mousey>player.y+7.5 && mousey<=player.y+9.5 && mousex>=player.x-2.5 && mousex<=player.x+5.5){
       if (mousex<=player.x-0.5){temp.invhold=this.inv[9]; this.inv[9]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x-0.5 && mousex<=player.x+1.5){temp.invhold=this.inv[10]; this.inv[10]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x+1.5 && mousex<=player.x+3.5){temp.invhold=this.inv[11]; this.inv[11]=this.inv[0]; this.inv[0]=temp.invhold;}
       if (mousex>player.x+3.5 && mousex<=player.x+5.5){temp.invhold=this.inv[12]; this.inv[12]=this.inv[0]; this.inv[0]=temp.invhold;}    
    }
  }
}


//////////Item Use//////////////////////////////////////////
function onKeyPressed(code,key){
  if (key == "a" && this.dontmove<=0 && player.ani!="swim"){
    findweapon("-Movement").onOncePlay("grab",0.2);     
  }
  if (key == "q" && this.dontmove<=0){
    this.hudout=3-this.hudout;
  }
  if (key == "s" && this.punchrecharge<=0 && this.dontmove<=0 && player.ani!="swim"){
    this.punchrecharge=5;
    findweapon("Punch").onWeaponFired();
  }
  if (key == "d" && this.dontmove<=0 && player.ani!="swim"){
    findweapon(this.inv[0]).onWeaponFired();
  }
}

////////////////If Dead/////////////////////////////////
function onActionClientside(){
  findweapon("-Movement").onOncePlay("betterdead",10); 
} 