text: /**
   * Created by Exbo on 2015/11/19.
   * 和输入有关的函数
   */
  var _key_pool=[];
  var _key_map=stg_const.KEY_MAP;
  var _key_snapshot=[];
  var stg_full_screen_key=70;
  var _o_width;
  var _o_height;
  var _last_full_screen;
  
  function _keyDownEvent(event){
      _key_pool[event.keyCode]=1;
      if(event.keyCode==stg_full_screen_key){
          document.body.webkitRequestFullScreen();
      }
  }
  function _keyUpEvent(event){
      _key_pool[event.keyCode]=0;
  }
  
  document.body.onkeydown=_keyDownEvent;
  document.body.onkeyup=_keyUpEvent;
  
  function snapshotInput(){
      _key_snapshot=[];
      _gamepad();
      for(var i=0;i<16;i++){
          _key_snapshot[i]=0;
          if(_key_map[i])
          for(var j=0;j<_key_map[i].length;j++){
              if(_key_pool[_key_map[i][j]])_key_snapshot[i]=1;
          }
      }
  }
  
  var _gamepad_sector=window.localStorage.getItem("gamepad");
  if(!_gamepad_sector){
      _gamepad_sector={
          header:1000,
          pads:{}
      };
  }else{
      _gamepad_sector=JSON.parse(_gamepad_sector);
      console.log("Gamepads Loaded.");
  }
  function _savegamepad(){
      window.localStorage.setItem("gamepad",JSON.stringify(_gamepad_sector));
      console.log("New Gamepad Saved.");
  }
  function _gamepad(){
      if(!navigator.getGamepads)return;
      var j;
      var a=navigator.getGamepads();
      if(!a)return;
      for(var i=0;i< a.length;i++){
          if(a[i]){
              var t=_gamepad_sector.pads[a[i].id];
              var t2=a[i].buttons.length;
              var t3=a[i].axes.length
              if(!t){
                  t=_gamepad_sector.header;
                  _gamepad_sector.pads[a[i].id]=_gamepad_sector.header;
                  _gamepad_sector.header+=t3*2+t2;
                  _savegamepad();
              }else{
                  for(j=0;j<t2;j++){
                      _key_pool[t+j]= a[i].buttons[j].value;
                  }
                  for(j=0;j<t3;j++){
                      _key_pool[t+j+t2]= ((a[i].axes[j]>0.5)?1:0);
                  }
                  for(j=0;j<t3;j++){
                      _key_pool[t+j+t2+t3]= ((a[i].axes[j]<-0.5)?1:0);
                  }
              }
          }
      }
  }
  var _last_key=[];
  function checkKeyChange(){
      var k=[];
      for(var i=0;i<_key_pool.length;i++){
          if(_key_pool[i]===undefined){
  
          }else{
              if(_key_pool[i]!=_last_key[i]){
                  if(_key_pool[i]==1) {
                      k.push(i);
                  }
                  _last_key[i]=_key_pool[i];
              }
          }
      }
      return k;
  }
  
  var _key_latency=2;
  var _key_latency_pool=[];
  var _key_bottom=0;//将获取的输入位置
  var _key_sync_pool=[];
  var _key_sync_ready=[];
  var _key_sync_id_head=3;//将发送的sync号
  var _key_sync_id_bottom=0;//将获取的sync号
  
  function stgCreateInput(iInputDelay){
      _stg_no_input=false;
      _key_latency=iInputDelay;
      _key_latency_pool=[];
      _key_sync_pool=[];
      _key_sync_ready=[];
      _key_sync_id_head=_key_latency;
      _key_bottom=0;
      for(var i=0;i<=_key_latency*3+1;i++){
          _key_latency_pool[i]=[];
          for(var j=0;j<stg_players_number;j++){
              _key_latency_pool[i][j]=0;
          }
          _key_sync_pool[i]=i;
          _key_sync_ready[i]=0;
      }
      for(var i=0;i<=_key_latency-1;i++) {
          _key_sync_ready[i]=5;
      }
      for(var i=0;i<_key_latency-1;i++){
          _key_sync_ready[i]=stg_players_number;
      }
  }
  
  
  function  _stgMainLoop_SendInput(){
      snapshotInput();
     // if(!stg_in_replay){
          var t=_packInput(_key_snapshot);
          if(t){
              t=t;
          }
          var th=_key_sync_pool[_key_sync_id_head];
          _sendInput(_key_sync_id_head,t);
  
          _receiveInput(stg_local_player_pos,th,t);
          
          _key_sync_id_head++;
          _key_sync_id_head=_key_sync_id_head%_key_latency_pool.length;
          _key_sync_pool[_key_sync_id_head]=th+1;
          //_key_sync_ready[_key_sync_id_head]-=stg_players_number;
    //  }else{
  
     // }
  }
  
  function _stgMainLoop_GetInput(){
      var flag=0;
      _stg_save_input=(stg_game_state==stg_const.GAME_RUNNING)?1:0;
      if(stg_ignore_input)_stg_save_input=0;
  
      if(!stg_in_replay){
          var t=0;
          if(_key_sync_ready[_key_bottom]>=stg_players_number){
              _key_sync_ready[_key_bottom]=0;
              if(_stg_save_input){
                  _replay_pool[_replay_header]=[];
              }
              for (var i = 0; i < stg_players_number; i++) {
                  if(!stg_players[i])stg_players[i]={};
                  stg_players[i].key=_unpackInput(_key_latency_pool[_key_bottom][i]);
                  t=t|_key_latency_pool[_key_bottom][i];
  
                  if(_stg_save_input){
                      _replay_pool[_replay_header][i]=_key_latency_pool[_key_bottom][i];
                  }
              }
              if(stg_players_number==0){
                  stg_system_input=_key_snapshot;
              }else {
                  stg_system_input = _unpackInput(t);
              }
              var th=_key_sync_pool[(_key_bottom-1+_key_latency_pool.length)%_key_latency_pool.length];
              _key_sync_pool[_key_bottom]=th+1;
              _key_bottom=(_key_bottom+1)%_key_latency_pool.length;
  
              flag=1;
              if(_stg_save_input){
                 _replay_header++;
              }
          }else{
  
          }
      }else {
          var t=0;
          if(_key_sync_ready[_key_bottom]>=_replay_watchers){
              _key_sync_ready[_key_bottom]=0;
              for (var i = 0; i < _replay_watchers; i++) {
                  t=t|_key_latency_pool[_key_bottom][i];
              }
              if(_replay_watchers==0){
                  stg_system_input=_key_snapshot;
              }else {
                  stg_system_input = _unpackInput(t);
              }
              var th=_key_sync_pool[(_key_bottom-1+_key_latency_pool.length)%_key_latency_pool.length];
              _key_sync_pool[_key_bottom]=th+1;
              _key_bottom=(_key_bottom+1)%_key_latency_pool.length;
              flag=1;
              if(_stg_save_input) {
                  for (var k = 0; k < stg_players_number; k++) {
                      if (!stg_players[k])stg_players[k] = {};
                      stg_players[k].key = _unpackInput(_replay_pool[_replay_header][k]);
                  }
  
                  _replay_header++;
                  if (_replay_header >= _replay_pool.length) {
                      stg_replay_end = 1;
                      stg_in_replay = 0;
                      stg_players_number=_replay_watchers;
                  }
              }else{
  
              }
          }else{
  
          }
  
      }
      return flag;
  }
  
  function stgSaveKeyMap(){
      window.localStorage.setItem("keymap",JSON.stringify(_key_map));
  }
  function stgLoadKeyMap(){
      var t=window.localStorage.getItem("keymap");
      if(!t){
          _key_map=clone(stg_const.KEY_MAP);
      }else{
          _key_map=JSON.parse(t);
      }
  }
  
  function stgResetKeyMap(){
      _key_map=clone(stg_const.KEY_MAP);
  }
  
  function _sendInput(iSync,iInput){
      if(stg_mp_status!=2)return;
      var th=_key_sync_pool[iSync];
     // console.log("Sending input sync ="+th);
      mpSendData({ipt:1,code:iInput,slt:stg_local_player_pos,sync:th});
  }
  
  function _receiveInput(iPlayerSlot,iSync,iInput){
      var i;
      var j;
      i=_key_bottom;
      j=0;
      while(j<_key_latency_pool.length){
          if(_key_sync_pool[i]==iSync){
             // console.log("Recv : ",iSync," slot ",iPlayerSlot," ",_key_sync_ready[i]+1);
              _key_sync_ready[i]++;
              _key_latency_pool[i][iPlayerSlot]=iInput;
              break;
          }
          i++;
          i=i%_key_latency_pool.length;
          j++;
      }
      if(j==_key_latency_pool.length){
        //  console.log(_key_sync_ready,_key_sync_pool,iSync,_key_bottom,_key_sync_id_head);
      }
  }
  
  function _receiveAllInput(iSync,iInput){
      var i;
      var j;
      i=_key_bottom;
      j=0;
      while(j<_key_latency_pool.length){
          if(_key_sync_pool[i]==iSync){
              _key_sync_ready[i]=_key_latency_pool.length;
              _key_latency_pool[i]=iInput;
              break;
          }
          i++;
          i=i%_key_latency_pool.length;
          j++;
      }
  }
  
  function _packInput(vaInput){
      var t=0;
      for(var i=15;i>=0;i--){
          t=(t<<1)+(vaInput[i]||0);
      }
      return t;
  }
  
  function _unpackInput(t){
      var ipt=[];
      for(var i=0;i<16;i++){
          ipt[i]=(t%2);
          t=t>>1;
      }
      return ipt;
  }
  
  var _replay_pool=[];
  var _replay_header=0;
  
  function _packReplayPool(){
      var d=new ArrayBuffer(stg_players_number*_replay_pool.length*2+4)
      var a=new Int16Array(d);
      for(var i=0;i<_replay_pool.length;i++){
          a.set(_replay_pool[i],i*stg_players_number+2);
      }
      var c=new Int32Array(d,0,1);
      c[0]=_replay_pool.length;
      return d;
  }
  
  function _unpackReplayPool(d,r0,pb){
      var a=new Int16Array(d);
      for(var i=0;i<r0;i++){
          _replay_pool[i]=[];
          for(var j=0;j<pb;j++){
              _replay_pool[i].push(a[i*pb+j]);
          }
      }
      return d;
  }