contract BattleboardsSupport is AccessControl, SafeMath  {

    /*** DATA TYPES ***/

    address public medalDataContract =  0x33A104dCBEd81961701900c06fD14587C908EAa3;
    address public battleboardDataContract =0xE60fC4632bD6713E923FE93F8c244635E6d5009e;

    
    
    uint8 public medalBoost = 50;
    uint8 public numBarriersPerBoard = 6;
    uint public barrierPrice = 1000000000000000;
    uint8 public maxMedalsBurned = 3;
    uint8 public barrierStrength = 75;
    
      
          // Utility Functions
    function DataContacts(address _medalDataContract, address _battleboardDataContract) onlyCREATOR external {
      
        medalDataContract = _medalDataContract;
        battleboardDataContract = _battleboardDataContract;
    }
    
    function setVariables( uint8 _medalBoost, uint8 _numBarriersPerBoard, uint8 _maxMedalsBurned, uint8 _barrierStrength, uint _barrierPrice) onlyCREATOR external {
        medalBoost = _medalBoost;
        numBarriersPerBoard = _numBarriersPerBoard;
        maxMedalsBurned = _maxMedalsBurned;
        barrierStrength = _barrierStrength;
        barrierPrice = _barrierPrice;
        
    }
    
      
      
        //Can be called by anyone at anytime,    
       function erectBarrier(uint16 battleboardId, uint8 _barrierType, uint8 _position) external payable {
           IBattleboardData battleboardData = IBattleboardData(battleboardDataContract);
           uint8 numBarriers = battleboardData.getBarrierNum(battleboardId);
           if (battleboardData.getTileIDbyPosition(battleboardId, _position) != 0 ) {revert();}  //Can't put a barrier on top of another tile
           if (numBarriers >= numBarriersPerBoard) {revert();} //can't put too many barriers on one board. 
           if (msg.value < barrierPrice) {revert();}
           if ((_barrierType <2) || (_barrierType >4)) {revert();} //can't create another tile instead of a barrier. 
          battleboardData.createTile(battleboardId,_barrierType, barrierStrength, _position, 0, 0, 0, 0, address(this),0);
       }
       
       
                
          function checkExistsOwnedMedal (uint64 medalId) public constant returns (bool) {
          IMedalData medalData = IMedalData(medalDataContract);
       
        if ((medalId < 0) || (medalId > medalData.totalSupply())) {return false;}
        if (medalData.ownerOf(medalId) == msg.sender) {return true;}
        
       else  return false;
}
       
             function medalBoostAndBurn(uint16 battleboardId, uint64 medalId) public  {
               
               //IMPORTANT: Before burning a medal in this function, you must APPROVE this address
               //in the medal data contract to unlock it. 
               
                IBattleboardData battleboardData = IBattleboardData(battleboardDataContract);

                uint8 tileId = battleboardData.getTileIDByOwner(battleboardId,msg.sender);
                //can't resurrect yourself. 
                if (battleboardData.isTileLive(battleboardId,tileId) == false) {revert();}
                
               if  (checkExistsOwnedMedal(medalId)== false) {revert();}
               
               //make sure the max number of medals haven't already been burned. 
               if (battleboardData.getMedalsBurned(battleboardId) >= maxMedalsBurned) {revert();}
              battleboardData.addMedalBurned(battleboardId);
                 //this first takes and then burns the medal. 
               IMedalData medalData = IMedalData(medalDataContract);
               uint8 medalType = medalData.getMedalType(medalId);
               medalData.takeOwnership(medalId);
               medalData._burn(medalId);
            uint32 hp = battleboardData.getTileHp(battleboardId, tileId);
           
          battleboardData.setTileHp(battleboardId, tileId, hp + (medalType * medalBoost));
       }
       
         
           function kill() onlyCREATOR external {
        selfdestruct(creatorAddress);
    }
 
        
function withdrawEther()  onlyCREATOR external {
    creatorAddress.transfer(this.balance);
}
       
       
}
function erectBarrier(uint16 battleboardId, uint8 _barrierType, uint8 _position) external payable {
    IBattleboardData battleboardData = IBattleboardData(battleboardDataContract);
    uint8 numBarriers = battleboardData.getBarrierNum(battleboardId);
    if (battleboardData.getTileIDbyPosition(battleboardId, _position) != 0 ) {revert();}  //Can't put a barrier on top of another tile
    if (numBarriers >= numBarriersPerBoard) {revert();} //can't put too many barriers on one board. 
    if (msg.value < barrierPrice) {revert();}
    if ((_barrierType <2) || (_barrierType >4)) {revert();} //can't create another tile instead of a barrier. 
   battleboardData.createTile(battleboardId,_barrierType, barrierStrength, _position, 0, 0, 0, 0, address(this),0);
}

//Can be called by anyone at anytime,    