// File: KongISland.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        assembly {
            size := extcodesize(account)
        }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

interface IKongIsland {
  function addManyToKongIslandAndPack(address account, uint16[] calldata tokenIds) external;
  function randomKongOwner(uint256 seed) external view returns (address);
}

interface IFOOD{
    function burn(address account, uint256 amount) external;
    function mint(address account, uint256 amount) external;
}

interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

interface ITraits {
  function tokenURI(uint256 tokenId) external view returns (string memory);
}

interface IKONGS {

  // struct to store each token's traits
    struct KaijuKong {
        bool isKaiju;
        uint8 background;
        uint8 species;
        uint8 companion;
        uint8 fur;
        uint8 hat;
        uint8 face;
        uint8 weapons;
        uint8 accessories;        
        uint8 kingScore;
    }


  function getPaidTokens() external view returns (uint256);
  function getTokenTraits(uint256 tokenId) external view returns (KaijuKong memory);
}

abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _setOwner(_msgSender());
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _setOwner(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _setOwner(newOwner);
    }

    function _setOwner(address newOwner) private {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

library Strings {
    bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0x00";
        }
        uint256 temp = value;
        uint256 length = 0;
        while (temp != 0) {
            length++;
            temp >>= 8;
        }
        return toHexString(value, length);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = _HEX_SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }
}

abstract contract Pausable is Context {
    /**
     * @dev Emitted when the pause is triggered by `account`.
     */
    event Paused(address account);

    /**
     * @dev Emitted when the pause is lifted by `account`.
     */
    event Unpaused(address account);

    bool private _paused;

    /**
     * @dev Initializes the contract in unpaused state.
     */
    constructor() {
        _paused = false;
    }

    /**
     * @dev Returns true if the contract is paused, and false otherwise.
     */
    function paused() public view virtual returns (bool) {
        return _paused;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is not paused.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    modifier whenNotPaused() {
        require(!paused(), "Pausable: paused");
        _;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is paused.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    modifier whenPaused() {
        require(paused(), "Pausable: not paused");
        _;
    }

    /**
     * @dev Triggers stopped state.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    function _pause() internal virtual whenNotPaused {
        _paused = true;
        emit Paused(_msgSender());
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual whenPaused {
        _paused = false;
        emit Unpaused(_msgSender());
    }
}

abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;
}


interface IERC721Enumerable is IERC721 {
    /**
     * @dev Returns the total amount of tokens stored by the contract.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns a token ID owned by `owner` at a given `index` of its token list.
     * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId);

    /**
     * @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
     * Use along with {totalSupply} to enumerate all tokens.
     */
    function tokenByIndex(uint256 index) external view returns (uint256);
}

interface IERC721Metadata is IERC721 {
    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

contract Traits is Ownable, ITraits {

  using Strings for uint256;

  // struct to store each trait's data for metadata and rendering
  struct Trait {
    string name;
    string png;
  }

  // mapping from trait type (index) to its name
  string[9] _traitTypes = [
    "Background",
    "Species",
	"Companion",
    "Fur",
    "Hat",
    "Face",
    "Weapons",
    "Accessories",
    "King Score"
  ];
  // storage of each traits name and base64 PNG data
  mapping(uint8 => mapping(uint8 => Trait)) public traitData;
  // mapping from KingScore to its score
  string[6] _KingScore = [
    "8",
    "8",
    "8",
    "7",
    "6",
    "5"
  ];

  IKONGS public kongs;

  constructor() {}

  /** ADMIN */

  function setKONGS(address _KONGS) external onlyOwner {
    kongs = IKONGS(_KONGS);
  }

  /**
   * administrative to upload the names and images associated with each trait
   * @param traitType the trait type to upload the traits for (see traitTypes for a mapping)
   * @param traits the names and base64 encoded PNGs for each trait
   */
  function uploadTraits(uint8 traitType, uint8[] calldata traitIds, Trait[] calldata traits) external onlyOwner {
    require(traitIds.length == traits.length, "Mismatched inputs");
    for (uint i = 0; i < traits.length; i++) {
      traitData[traitType][traitIds[i]] = Trait(
        traits[i].name,
        traits[i].png
      );
    }
  }

  /** RENDER */

  /**
   * generates an <image> element using base64 encoded PNGs
   * @param trait the trait storing the PNG data
   * @return the <image> element
   */
  function drawTrait(Trait memory trait) internal pure returns (string memory) {
    return string(abi.encodePacked(
      '<image x="4" y="4" width="32" height="32" image-rendering="pixelated" preserveAspectRatio="xMidYMid" xlink:href="data:image/png;base64,',
      trait.png,
      '"/>'
    ));
  }

  /**
   * generates an entire SVG by composing multiple <image> elements of PNGs
   * @param tokenId the ID of the token to generate an SVG for
   * @return a valid SVG of the Kaiju / Kong
   */
  function drawSVG(uint256 tokenId) public view returns (string memory) {
    IKONGS.KaijuKong memory s = kongs.getTokenTraits(tokenId);
    uint8 shift = s.isKaiju ? 0 : 9;

    string memory svgString = string(abi.encodePacked(
      drawTrait(traitData[0][s.background]),
      s.isKaiju ? drawTrait(traitData[1 + shift][s.species]) : '',
      s.isKaiju ? drawTrait(traitData[2 + shift][s.companion]) : '',
      s.isKaiju ? '' : drawTrait(traitData[4 + shift][s.kingScore]),
      s.isKaiju ? '' : drawTrait(traitData[5 + shift][s.hat]),
      s.isKaiju ? '' : drawTrait(traitData[6 + shift][s.face]),
	  s.isKaiju ? '' : drawTrait(traitData[7 + shift][s.weapons]),
	  s.isKaiju ? '' : drawTrait(traitData[8 + shift][s.accessories])
    ));

    return string(abi.encodePacked(
      '<svg id="woolf" width="100%" height="100%" version="1.1" viewBox="0 0 40 40" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">',
      svgString,
      "</svg>"
    ));
  }

  /**
   * generates an attribute for the attributes array in the ERC721 metadata standard
   * @param traitType the trait type to reference as the metadata key
   * @param value the token's trait associated with the key
   * @return a JSON dictionary for the single attribute
   */
  function attributeForTypeAndValue(string memory traitType, string memory value) internal pure returns (string memory) {
    return string(abi.encodePacked(
      '{"trait_type":"',
      traitType,
      '","value":"',
      value,
      '"}'
    ));
  }

  /**
   * generates an array composed of all the individual traits and values
   * @param tokenId the ID of the token to compose the metadata for
   * @return a JSON array of all of the attributes for given token ID
   */
  function compileAttributes(uint256 tokenId) public view returns (string memory) {
    IKONGS.KaijuKong memory s = kongs.getTokenTraits(tokenId);
    string memory traits;
    if (s.isKaiju) {
      traits = string(abi.encodePacked(
        attributeForTypeAndValue(_traitTypes[0], traitData[0][s.background].name),',',
        attributeForTypeAndValue(_traitTypes[1], traitData[1][s.species].name),',',
        attributeForTypeAndValue(_traitTypes[2], traitData[2][s.companion].name),','
      ));
    } else {
      traits = string(abi.encodePacked(
        attributeForTypeAndValue(_traitTypes[0], traitData[0][s.background].name),',',
        attributeForTypeAndValue(_traitTypes[3], traitData[13][s.kingScore].name),',',
        attributeForTypeAndValue(_traitTypes[4], traitData[14][s.hat].name),',',
        attributeForTypeAndValue(_traitTypes[5], traitData[15][s.face].name),',',
        attributeForTypeAndValue(_traitTypes[6], traitData[16][s.weapons].name),',',
		attributeForTypeAndValue(_traitTypes[7], traitData[17][s.accessories].name),',',
        attributeForTypeAndValue("King Score", _KingScore[s.kingScore]),','
      ));
    }
    return string(abi.encodePacked(
      '[',
      traits,
      '{"trait_type":"Generation","value":',
      tokenId <= kongs.getPaidTokens() ? '"Gen 0"' : '"Gen 1"',
      '},{"trait_type":"Type","value":',
      s.isKaiju ? '"Kaiju"' : '"Kong"',
      '}]'
    ));
  }

  /**
   * generates a base64 encoded metadata response without referencing off-chain content
   * @param tokenId the ID of the token to generate the metadata for
   * @return a base64 encoded JSON dictionary of the token's metadata and SVG
   */
  function tokenURI(uint256 tokenId) public view override returns (string memory) {
    IKONGS.KaijuKong memory s = kongs.getTokenTraits(tokenId);

    string memory metadata = string(abi.encodePacked(
      '{"name": "',
      s.isKaiju ? 'Kaiju #' : 'Kong #',
      tokenId.toString(),
      '", "description": "Thousands of Kaiju and Wolves compete on Kong Island. A tempting prize of $FOOD awaits, with deadly high stakes. All the metadata and images are generated and stored 100% on-chain. No IPFS. NO API. Just the Ethereum blockchain.", "image": "data:image/svg+xml;base64,',
      base64(bytes(drawSVG(tokenId))),
      '", "attributes":',
      compileAttributes(tokenId),
      "}"
    ));

    return string(abi.encodePacked(
      "data:application/json;base64,",
      base64(bytes(metadata))
    ));
  }

  /** BASE 64 - Written by Brech Devos */
  
  string internal constant TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

  function base64(bytes memory data) internal pure returns (string memory) {
    if (data.length == 0) return '';
    
    // load the table into memory
    string memory table = TABLE;

    // multiply by 4/3 rounded up
    uint256 encodedLen = 4 * ((data.length + 2) / 3);

    // add some extra buffer at the end required for the writing
    string memory result = new string(encodedLen + 32);

    assembly {
      // set the actual output length
      mstore(result, encodedLen)
      
      // prepare the lookup table
      let tablePtr := add(table, 1)
      
      // input ptr
      let dataPtr := data
      let endPtr := add(dataPtr, mload(data))
      
      // result ptr, jump over length
      let resultPtr := add(result, 32)
      
      // run over the input, 3 bytes at a time
      for {} lt(dataPtr, endPtr) {}
      {
          dataPtr := add(dataPtr, 3)
          
          // read 3 bytes
          let input := mload(dataPtr)
          
          // write 4 characters
          mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(18, input), 0x3F)))))
          resultPtr := add(resultPtr, 1)
          mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(12, input), 0x3F)))))
          resultPtr := add(resultPtr, 1)
          mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr( 6, input), 0x3F)))))
          resultPtr := add(resultPtr, 1)
          mstore(resultPtr, shl(248, mload(add(tablePtr, and(        input,  0x3F)))))
          resultPtr := add(resultPtr, 1)
      }
      
      // padding with '='
      switch mod(mload(data), 3)
      case 1 { mstore(sub(resultPtr, 2), shl(240, 0x3d3d)) }
      case 2 { mstore(sub(resultPtr, 1), shl(248, 0x3d)) }
    }
    
    return result;
  }
}

contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
    using Address for address;
    using Strings for uint256;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count
    mapping(address => uint256) private _balances;

    // Mapping from token ID to approved address
    mapping(uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    /**
     * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return
            interfaceId == type(IERC721).interfaceId ||
            interfaceId == type(IERC721Metadata).interfaceId ||
            super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC721-balanceOf}.
     */
    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: balance query for the zero address");
        return _balances[owner];
    }

    /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERC721: owner query for nonexistent token");
        return owner;
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

        string memory baseURI = _baseURI();
        return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
    }

    /**
     * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
     * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
     * by default, can be overriden in child contracts.
     */
    function _baseURI() internal view virtual returns (string memory) {
        return "";
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "ERC721: approved query for nonexistent token");

        return _tokenApprovals[tokenId];
    }

    /**
     * @dev See {IERC721-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        require(operator != _msgSender(), "ERC721: approve to caller");

        _operatorApprovals[_msgSender()][operator] = approved;
        emit ApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * @dev See {IERC721-isApprovedForAll}.
     */
    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");

        _transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
        _safeTransfer(from, to, tokenId, _data);
    }

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * `_data` is additional data, it has no specified format and it is sent in call to `to`.
     *
     * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
     * implement alternative mechanisms to perform token transfer, such as signature-based.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeTransfer(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     * and stop existing when they are burned (`_burn`).
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _owners[tokenId] != address(0);
    }

    /**
     * @dev Returns whether `spender` is allowed to manage `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "ERC721: operator query for nonexistent token");
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
    }

    /**
     * @dev Safely mints `tokenId` and transfers it to `to`.
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
     * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
     */
    function _safeMint(
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _mint(to, tokenId);
        require(
            _checkOnERC721Received(address(0), to, tokenId, _data),
            "ERC721: transfer to non ERC721Receiver implementer"
        );
    }

    /**
     * @dev Mints `tokenId` and transfers it to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - `to` cannot be the zero address.
     *
     * Emits a {Transfer} event.
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals
        _approve(address(0), tokenId);

        _balances[owner] -= 1;
        delete _owners[tokenId];

        emit Transfer(owner, address(0), tokenId);
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *  As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner
        _approve(address(0), tokenId);

        _balances[from] -= 1;
        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);
    }

    /**
     * @dev Approve `to` to operate on `tokenId`
     *
     * Emits a {Approval} event.
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
     * The call is not executed if the target address is not a contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param _data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkOnERC721Received(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) private returns (bool) {
        if (to.isContract()) {
            try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
                return retval == IERC721Receiver.onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    revert("ERC721: transfer to non ERC721Receiver implementer");
                } else {
                    assembly {
                        revert(add(32, reason), mload(reason))
                    }
                }
            }
        } else {
            return true;
        }
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {}
}

abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
    // Mapping from owner to list of owned token IDs
    mapping(address => mapping(uint256 => uint256)) private _ownedTokens;

    // Mapping from token ID to index of the owner tokens list
    mapping(uint256 => uint256) private _ownedTokensIndex;

    // Array with all token ids, used for enumeration
    uint256[] private _allTokens;

    // Mapping from token id to position in the allTokens array
    mapping(uint256 => uint256) private _allTokensIndex;

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC721) returns (bool) {
        return interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721.balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
        return _ownedTokens[owner][index];
    }

    /**
     * @dev See {IERC721Enumerable-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _allTokens.length;
    }

    /**
     * @dev See {IERC721Enumerable-tokenByIndex}.
     */
    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721Enumerable.totalSupply(), "ERC721Enumerable: global index out of bounds");
        return _allTokens[index];
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual override {
        super._beforeTokenTransfer(from, to, tokenId);

        if (from == address(0)) {
            _addTokenToAllTokensEnumeration(tokenId);
        } else if (from != to) {
            _removeTokenFromOwnerEnumeration(from, tokenId);
        }
        if (to == address(0)) {
            _removeTokenFromAllTokensEnumeration(tokenId);
        } else if (to != from) {
            _addTokenToOwnerEnumeration(to, tokenId);
        }
    }

    /**
     * @dev Private function to add a token to this extension's ownership-tracking data structures.
     * @param to address representing the new owner of the given token ID
     * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
     */
    function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
        uint256 length = ERC721.balanceOf(to);
        _ownedTokens[to][length] = tokenId;
        _ownedTokensIndex[tokenId] = length;
    }

    /**
     * @dev Private function to add a token to this extension's token tracking data structures.
     * @param tokenId uint256 ID of the token to be added to the tokens list
     */
    function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
        _allTokensIndex[tokenId] = _allTokens.length;
        _allTokens.push(tokenId);
    }

    /**
     * @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
     * while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
     * gas optimizations e.g. when performing a transfer operation (avoiding double writes).
     * This has O(1) time complexity, but alters the order of the _ownedTokens array.
     * @param from address representing the previous owner of the given token ID
     * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
     */
    function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
        // To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
        // then delete the last slot (swap and pop).

        uint256 lastTokenIndex = ERC721.balanceOf(from) - 1;
        uint256 tokenIndex = _ownedTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary
        if (tokenIndex != lastTokenIndex) {
            uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];

            _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
            _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
        }

        // This also deletes the contents at the last position of the array
        delete _ownedTokensIndex[tokenId];
        delete _ownedTokens[from][lastTokenIndex];
    }

    /**
     * @dev Private function to remove a token from this extension's token tracking data structures.
     * This has O(1) time complexity, but alters the order of the _allTokens array.
     * @param tokenId uint256 ID of the token to be removed from the tokens list
     */
    function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
        // To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
        // then delete the last slot (swap and pop).

        uint256 lastTokenIndex = _allTokens.length - 1;
        uint256 tokenIndex = _allTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
        // rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
        // an 'if' statement (like in _removeTokenFromOwnerEnumeration)
        uint256 lastTokenId = _allTokens[lastTokenIndex];

        _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
        _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index

        // This also deletes the contents at the last position of the array
        delete _allTokensIndex[tokenId];
        _allTokens.pop();
    }
}

contract KONGS is IKONGS, ERC721Enumerable, Ownable, Pausable {
    uint256 public MINT_PRICE = .02 ether;
    uint256 public MAX_TOKENS;
    uint256 public PAID_TOKENS;
    uint16 public minted;
    uint256 reserved;
    uint256 reserveLimit;
    uint256 nonReserved;

    struct whitelist{
        uint256 amount;
        }

    mapping (address => whitelist) whitelisted;
    mapping (address => whitelist) reservedMinters;
    bool whitelistMode;

 
    mapping(uint256 => uint256) private mintBlock;
    mapping(uint256 => KaijuKong) private tokenTraits;
    mapping(uint256 => uint256) private existingCombinations;
    uint8[][19] public rarities;
    uint8[][19] public aliases;
    // reference to the KongIsland for choosing random Kong thieves
    IKongIsland public kongIsland;
    // reference to $FOOD for burning on mint
    IFOOD public food;
    // reference to Traits
    ITraits public traits;


    /**
     * instantiates contract and rarity tables
     */
    constructor(
        address _food,
        address _traits,
        uint256 _maxTokens,
        uint256 _reserved
    ) ERC721("Kong Game", "KGAME") {
        food = IFOOD(_food);
        traits = ITraits(_traits);
        MAX_TOKENS = _maxTokens;
        PAID_TOKENS = _maxTokens / 5;
        nonReserved = _reserved;
        reserveLimit = _reserved;
        rarities[0] = [255, 229, 204,178, 153, 127, 101, 76, 50, 24];
        aliases[0] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        rarities[1] = [255,242,229,216,203,190,177,164,151,138,125,112,99,86,73,60,54,48,42,36,30,24,18,12,3,4,5,5];
        aliases[1] = [27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,10,9,8,7,6,5,4,3,2,1,0];
        rarities[2] = [255,15,24,31,39,54,57,59,62,67,71,73,78,81,83,85,95,98,105,110,112,118,121,126,128];
        aliases[2] = [25,24,23,22,21,20,19,18,17,16,15,14,13,12,0,0,9,0,0,0,0,0,0,0,0,0];
        rarities[3] = [255];
        aliases[3] = [0];

        rarities[4] = [255];
        aliases[4] = [0];
        rarities[5] = [255];
        aliases[5] = [0];
        rarities[6] = [255];
        aliases[6] = [0];
        rarities[7] = [255];
        aliases[7] = [0];
        rarities[8] = [255];
        aliases[8] = [0];
        rarities[9] = [255];
        aliases[9] = [0];
        rarities[10] = [255];
        aliases[10] = [0];
        rarities[11] = [255];
        aliases[11] = [0];
        rarities[12] = [255];
        aliases[12] = [0];

        rarities[13] = [255,165,51,43,18,5];
        aliases[13] = [0,1,2,3,4,5];


        rarities[14] = [255,15,41,54,69,78,114,117];
        aliases[14] = [0,1,2,3,4,5,6,7];

        rarities[15] = [255,3,6,15,30,63,78,102,132];
        aliases[15] = [0,1,2,3,4,5,6,7,8,9,10];

        rarities[16] = [255,3,15,15,15,15,15,15,9,6,9,6,3];
        aliases[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12];


        rarities[17] = [255,3,18,27,27,3,27,9,9,9,3,3,3];
        aliases[17] = [0,1,2,3,4,5,6,7,8,9,10,11,12];

        rarities[18] = [255,165,51,43,18,5];
        aliases[18] = [3,3,3,2,1,0];
    }

    /** EXTERNAL */
    function mint(uint256 amount) external payable whenNotPaused {
    require(amount <= 5, "Maxmimum 5 mints at a time");
    require(tx.origin == _msgSender(), "Only EOA");
    require(minted + amount <= MAX_TOKENS, "All tokens minted");
    if(whitelistMode){require(amount <= whitelisted[_msgSender()].amount, "Whitelisted can mint maximum of 3");}
    if (minted < PAID_TOKENS) {
      require(minted + amount <= PAID_TOKENS, "All tokens on-sale already sold");
      require(amount * MINT_PRICE >= msg.value, "Invalid payment amount");
    } else {
      require(msg.value == 0);
    }
    uint256 totalFOODCost = 0;
    uint256 seed;
        for (uint256 i = 0; i < amount; i++) {
            minted++;
            nonReserved++;
            if(whitelistMode){whitelisted[ _msgSender()].amount--;}
	        mintBlock[nonReserved] = block.number;
            seed = random(nonReserved);
            generate(nonReserved, seed);
            address recipient = selectRecipient(seed);
            totalFOODCost += mintCost(nonReserved);
            _safeMint(recipient, nonReserved);
        }
        if (totalFOODCost > 0) food.burn(_msgSender(), totalFOODCost);
    }

    function reservedMint(uint256 amount) external {
    require(amount <= 5, "Maxmimum 5 mints at a time");
    require(amount <= reservedMinters[msg.sender].amount);
    uint256 seed;
        for (uint256 i = 0; i < amount; i++) {
            minted++;
            reserved++;
	        mintBlock[reserved] = block.number;
            seed = random(reserved);
            generate(reserved, seed);
            _safeMint(msg.sender,reserved);
        }
    }

  function mintCost(uint256 tokenId) public view returns (uint256) {
    if (tokenId <= PAID_TOKENS) return 0;
    if (tokenId <= MAX_TOKENS * 2 / 5) return 20000 ether;
    if (tokenId <= MAX_TOKENS * 3 / 5) return 40000 ether;
    if (tokenId <= MAX_TOKENS * 4 / 5) return 80000 ether;
    return 100000 ether;
  }

    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        if (_msgSender() != address(kongIsland)) {
            require(
                _isApprovedOrOwner(_msgSender(), tokenId),
                "ERC721: transfer caller is not owner nor approved"
            );
        }
        _transfer(from, to, tokenId);
    }

    /** INTERNAL */

    /**
     * generates traits for a specific token, checking to make sure it's unique
     * @param tokenId the id of the token to generate traits for
     * @param seed a pseudorandom 256 bit number to derive traits from
     * @return t - a struct of traits for the given token ID
     */
    function generate(uint256 tokenId, uint256 seed)
        internal
        returns (KaijuKong memory t)
    {
        t = selectTraits(seed);
        if (existingCombinations[structToHash(t)] == 0) {
            tokenTraits[tokenId] = t;
            existingCombinations[structToHash(t)] = tokenId;
            return t;
        }
        return generate(tokenId, random(seed));
    }

    /**
     * uses A.J. Walker's Alias algorithm for O(1) rarity table lookup
     * ensuring O(1) instead of O(n) reduces mint cost by more than 50%
     * probability & alias tables are generated off-chain beforehand
     * @param seed portion of the 256 bit seed to remove trait correlation
     * @param traitType the trait type to select a trait for
     * @return the ID of the randomly selected trait
     */
    function selectTrait(uint16 seed, uint8 traitType)
        internal
        view
        returns (uint8)
    {
        uint8 trait = uint8(seed) % uint8(rarities[traitType].length);
        if (seed >> 9 < rarities[traitType][trait]) return trait;
        return aliases[traitType][trait];
    }

    /**
     * the first 20% (ETH purchases) go to the minter
     * the remaining 80% have a 10% chance to be given to a random staked Kong
     * @param seed a random value to select a recipient from
     * @return the address of the recipient (either the minter or the Kong thief's owner)
     */

     
  function selectRecipient(uint256 seed) internal view returns (address) {
    if (minted <= PAID_TOKENS || minted < 30000 && ((seed >> 245) % 10) != 0 || minted >30000 && ((seed >> 245) % 10) >=2) return _msgSender(); // top 10 bits haven't been used
    address thief = kongIsland.randomKongOwner(seed >> 144); // 144 bits reserved for trait selection
    if (thief == address(0x0)) return _msgSender();
    return thief;
  }

    /**
     * selects the species and all of its traits based on the seed value
     * @param seed a pseudorandom 256 bit number to derive traits from
     * @return t -  a struct of randomly selected traits
     */
    function selectTraits(uint256 seed)
        internal
        view
        returns (KaijuKong memory t)
    {
        t.isKaiju = (seed & 0xFFFF) % 10 != 0;
        uint8 shift = t.isKaiju ? 0 : 9;
        seed >>= 16;
        t.background = selectTrait(uint16(seed & 0xFFFF), 0);
        seed >>= 16;
        t.species = selectTrait(uint16(seed & 0xFFFF), 1 + shift);
        seed >>= 16;
        t.companion = selectTrait(uint16(seed & 0xFFFF), 2 + shift);
        seed >>= 16;
        t.kingScore = selectTrait(uint16(seed & 0xFFFF), 9 + shift);
        seed >>= 16;
        t.hat = selectTrait(uint16(seed & 0xFFFF), 5 + shift);
        seed >>= 16;
        t.face = selectTrait(uint16(seed & 0xFFFF), 6 + shift);
        seed >>= 16;
        t.weapons = selectTrait(uint16(seed & 0xFFFF), 7 + shift);
        seed >>= 16;
        t.accessories = selectTrait(uint16(seed & 0xFFFF), 8 + shift);
        seed >>= 16;
        t.fur = t.kingScore;
    }

    function structToHash(KaijuKong memory s) internal pure returns (uint256) {
        return
            uint256(
                bytes32(
                    abi.encodePacked(
                        s.isKaiju,
                        s.background,
                        s.species,
                        s.companion,
                        s.fur,
                        s.hat,
                        s.face,
                        s.weapons,
                        s.accessories,
                        s.kingScore
                    )
                )
            );
    }

  function random(uint256 seed) internal view returns (uint256) {
    return uint256(keccak256(abi.encodePacked(
      tx.origin,
      blockhash(block.number - 1),
      block.timestamp,
      seed
    )));
  }

    /** READ */

    function getTokenTraits(uint256 tokenId)
        external
        view
        override
        returns (KaijuKong memory)
    {
        require(mintBlock[tokenId] + 1 < block.number);
        return tokenTraits[tokenId];
    }

    function getPaidTokens() external view override returns (uint256) {
        return PAID_TOKENS;
    }

    function isKaiju(uint256 tokenId) external view returns (bool){
        require(mintBlock[tokenId] + 1 < block.number);
        return(tokenTraits[tokenId].isKaiju);
    }

    function KingScore(uint256 tokenId) external view returns (uint8) {
        require(mintBlock[tokenId] + 1 < block.number);
        return(tokenTraits[tokenId].kingScore);
    }

    function getIDsOwnedby(address _address) external view returns(uint256[] memory) {
        uint256[] memory tokensOwned = new uint256[](balanceOf(_address));
        for(uint256 i = 0; i < tokensOwned.length; i++) {
            tokensOwned[i] = tokenOfOwnerByIndex(_address, i);
        }
        return tokensOwned;
    }

    /** ADMIN */

    function setKongIsland(address _KongIsland) external onlyOwner {
        kongIsland = IKongIsland(_KongIsland);
    }

    function addToWhitelist(address _whitelisted) external onlyOwner {
        whitelisted[_whitelisted].amount = 3;
    }

    function bulkWhitelist(address[] calldata _whitelisted) external onlyOwner {
        for (uint256 i = 0; i < _whitelisted.length; i++) {
        whitelisted[_whitelisted[i]].amount = 3;
        }
    }

    function addToReservedMinters(address _reserved, uint256 _amount) external onlyOwner {
        reservedMinters[_reserved].amount = _amount;
    }

    function bulkWhitelist(address[] calldata _reserved,uint256[] calldata _amount) external onlyOwner {
        for (uint256 i = 0; i < _reserved.length; i++) {
        reservedMinters[_reserved[i]].amount = _amount[i];
        }
    }

    function withdraw() external onlyOwner {
        payable(owner()).transfer(address(this).balance);
    }

    function whitelistMinting(bool _whitelistMode)external onlyOwner {
        whitelistMode = _whitelistMode;
    }

    function setPaidTokens(uint256 _paidTokens) external onlyOwner {
        PAID_TOKENS = _paidTokens;
    }

    function setPaused(bool _paused) external onlyOwner {
        if (_paused) _pause();
        else _unpause();
    }

  function setMintPrice(uint256 mintprice) external onlyOwner{
        MINT_PRICE = mintprice;
    }

  function updateFoodContract(address _food) external onlyOwner {
        food = IFOOD(_food);
    }

    /** RENDER */

  function tokenURI(uint256 tokenId) public view override returns (string memory) {
    require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
    require(mintBlock[tokenId] + 1 < block.number);
    return traits.tokenURI(tokenId);
  }

}

contract KongIsland is Ownable, IERC721Receiver, Pausable {
  
  // maximum King score for a Kong
  uint8 public constant MAX_King = 8;

  // struct to store a stake's token, owner, and earning values
  struct Stake {
    uint16 tokenId;
    uint80 value;
    address owner;
  }

  struct StakedTokens {
    uint256[] ID;
  }

  event TokenStaked(address owner, uint256 tokenId, uint256 value);
  event KaijuClaimed(uint256 tokenId, bool unstaked);
  event KongClaimed(uint256 tokenId, uint256 earned, bool unstaked);

  // reference to the contracts
  KONGS kongs;
  IFOOD food;

    mapping(address => StakedTokens) StakedByAddress;   
  // maps tokenId to stake
    mapping(uint256 => Stake) public kongIsland; 
  // maps King to all Kong stakes with that King
     mapping(uint256 => Stake[]) public pack; 
  // tracks location of each Kong in Pack
    mapping(uint256 => uint256) public packIndices; 
  // total King scores staked
    uint256 public totalKingStaked = 0; 
    // total Kongs scores staked
    uint256 public totalKongsStaked = 0; 
  // any rewards distributed when no Kongs are staked
    uint256 public unaccountedRewards = 0; 
  // amount of $FOOD due for each King point staked
    uint256 public FOODPerKing = 0; 

  mapping(address => uint256) private _lastUnstakeBlock;
  mapping(address => uint256) private _owed;

  // Kaiju earn 10000 $FOOD per day
  uint256 public constant DAILY_FOOD_RATE = 10000 ether; //10000 ether;
  // Kaiju must have 2 days worth of $FOOD to unstake or else it's too cold
  uint256 public MINIMUM_TO_EXIT = 0 days; //2 days;
  // Kongs take a 20% tax on all $FOOD claimed
  uint256 public constant FOOD_CLAIM_TAX_PERCENTAGE = 20;
  // there will only ever be (roughly) 2.4 billion $FOOD earned through staking
  uint256 public constant MAXIMUM_GLOBAL_FOOD = 2400000000 ether;

  // amount of $FOOD earned so far
  uint256 public totalFOODEarned;
  // number of Kaiju stake on  KongIsland
  uint256 public totalKaijuStaked;
  uint256  totalKaijuStakedOG;
  uint256  totalKaijuStakedGen1;
  uint256  totalKaijuStakedGen2;
  // the last time $FOOD was claimed
  uint256 public lastClaimTimestamp;

  // emergency rescue to allow unstaking without any checks but without $FOOD
  bool public rescueEnabled = false;

  /**
   * @param _KONGS reference to the KONGS NFT contract
   * @param _FOOD reference to the $FOOD token
   */
  constructor(address _KONGS, address _FOOD) { 
    kongs = KONGS(_KONGS);
    food = IFOOD(_FOOD);
  }

  /** STAKING */

  /**
   * adds Kaiju and Kongs to the KongIsland and Pack
   * @param account the address of the staker
   * @param tokenIds the IDs of the Kaiju and Kongs to stake
   */
  function addManyToKongIslandAndPack(address account, uint16[] calldata tokenIds) external {
    require(account == tx.origin && _msgSender() == tx.origin, "DONT GIVE YOUR TOKENS AWAY");
    for (uint i = 0; i < tokenIds.length; i++) {
        require(kongs.ownerOf(tokenIds[i]) == _msgSender() && kongs.ownerOf(tokenIds[i]) == tx.origin, "AINT YO TOKEN");
        kongs.transferFrom(_msgSender(), address(this), tokenIds[i]);
        StakedByAddress[msg.sender].ID.push(tokenIds[i]);

      if (isKaiju(tokenIds[i])) 
        _addKaijuToKongIsland(account, tokenIds[i]);
      else 
        _addKongToPack(account, tokenIds[i]);
    }
  }

  /**
   * adds a single Kaiju to the KongIsland
   * @param account the address of the staker
   * @param tokenId the ID of the Kaiju to add to the KongIsland
   */
  function _addKaijuToKongIsland(address account, uint256 tokenId) internal whenNotPaused _updateEarnings {
    kongIsland[tokenId] = Stake({
      owner: account,
      tokenId: uint16(tokenId),
      value: uint80(block.timestamp)
    });
    totalKaijuStaked += 1;
    if(tokenId >30000){totalKaijuStakedGen2 += 1;}
    else if (tokenId >=20000&& tokenId <30000){totalKaijuStakedGen1 += 1;}
    else {totalKaijuStakedOG += 1;}

    emit TokenStaked(account, tokenId, block.timestamp);
  }

  /**
   * adds a single Kong to the Pack
   * @param account the address of the staker
   * @param tokenId the ID of the Kong to add to the Pack
   */
  function _addKongToPack(address account, uint256 tokenId) internal {
    uint256 King = _KingForKong(tokenId);
    totalKingStaked += King;
    totalKongsStaked += 1;
    packIndices[tokenId] = pack[King].length; // Store the location of the Kong in the Pack
    pack[King].push(Stake({
      owner: account,
      tokenId: uint16(tokenId),
      value: uint80(FOODPerKing)
    })); // Add the Kong to the Pack
    kongIsland[tokenId] = Stake({
      owner: account,
      tokenId: uint16(tokenId),
      value: uint80(block.timestamp)
    });
    emit TokenStaked(account, tokenId, FOODPerKing);
  }

  /** CLAIMING / UNSTAKING */

  function unstakeFromKongIslandAndPack(uint16[] calldata tokenIds) external whenNotPaused _updateEarnings {
    require(_lastUnstakeBlock[_msgSender()] + 1 < block.number, "NoReEntrancy");
    _lastUnstakeBlock[_msgSender()] = block.number;
    for (uint i = 0; i < tokenIds.length; i++) {
      if (isKaiju(tokenIds[i]))
       _owed[_msgSender()] += _claimKaijuFromKongIsland(tokenIds[i]);
      else
        _owed[_msgSender()] += _claimKongFromPack(tokenIds[i]);
    }
  }

  function realiseEarningsFromKongIslandAndPack(uint16[] calldata tokenIds) external whenNotPaused  _updateEarnings {
    require(_lastUnstakeBlock[_msgSender()] + 1 < block.number, "No Reentrancy");
    _lastUnstakeBlock[_msgSender()] = block.number;
    for (uint i = 0; i < tokenIds.length; i++) {
      if (isKaiju(tokenIds[i]))
       _owed[_msgSender()] += _claimKaijuFood(tokenIds[i]);
      else
        _owed[_msgSender()] += _claimKongFood(tokenIds[i]);
    }
  }

  function claimFOOD() external whenNotPaused  {
    require(_lastUnstakeBlock[_msgSender()] + 1 < block.number, "No Reentrancy");
    require(_owed[_msgSender()] > 0, "Nothing to claim");
    _lastUnstakeBlock[_msgSender()] = block.number;
    uint256 owed = _owed[_msgSender()];
    _owed[_msgSender()] = 0;
    food.mint(_msgSender(), owed);
   }

  function _claimKaijuFromKongIsland(uint256 tokenId) internal returns (uint256 owed) {
    Stake memory stake = kongIsland[tokenId];
    require(stake.owner == _msgSender() && stake.owner == tx.origin, "No swiping");
    require(!(block.timestamp - stake.value < MINIMUM_TO_EXIT), "GONNA BE COLD WITHOUT TWO DAY'S FOOD");
    if (totalFOODEarned < MAXIMUM_GLOBAL_FOOD) {
      owed = (block.timestamp - stake.value) * DAILY_FOOD_RATE / 1 days;
    } else if (stake.value > lastClaimTimestamp) {
      owed = 0; // $FOOD production stopped already
    } else {
      owed = (lastClaimTimestamp - stake.value) * DAILY_FOOD_RATE / 1 days; // stop earning additional $FOOD if it's all been earned
    }
    if(tokenId >= 20000 && tokenId < 30000 ){owed = ((owed*80)/100);}
    else if(tokenId >= 30000){owed = ((owed*60)/100);} 
    uint256 stealChance = (random(tokenId) % 10);
      if (tokenId <20000 && stealChance >= 5||tokenId >=20000 && stealChance >= 7 ) { // 50% chance of all $FOOD stolen
        _payKongTax(owed);
        owed = 0;
      }
      delete kongIsland[tokenId];
      totalKaijuStaked -= 1;
      removeTokenID(tokenId);
      kongs.safeTransferFrom(address(this), _msgSender(), tokenId, ""); // send back Kaiju
     emit KaijuClaimed(tokenId, true);
  }


  function _claimKongFromPack(uint256 tokenId) internal returns (uint256 owed) {
    require(kongs.ownerOf(tokenId) == address(this), "AINT A PART OF THE PACK");
    uint256 King = _KingForKong(tokenId);
    Stake memory stake = pack[King][packIndices[tokenId]];
    require(stake.owner == _msgSender(), "SWIPER, NO SWIPING");
    owed = (King) * (FOODPerKing - stake.value); // Calculate portion of tokens based on King
      totalKingStaked -= King; // Remove King from total staked
      totalKongsStaked -= 1; 
      Stake memory lastStake = pack[King][pack[King].length - 1];
      pack[King][packIndices[tokenId]] = lastStake; // Shuffle last Kong to current position
      packIndices[lastStake.tokenId] = packIndices[tokenId];
      pack[King].pop(); // Remove duplicate
      delete packIndices[tokenId]; // Delete old mapping
      delete kongIsland[tokenId];
      removeTokenID(tokenId);
      kongs.safeTransferFrom(address(this), _msgSender(), tokenId, ""); // Send back Kong
    
    emit KongClaimed(tokenId, owed, true);  }
	
	 function _claimKaijuFood(uint256 tokenId) internal returns (uint256 owed) {
    Stake memory stake = kongIsland[tokenId];
    require(stake.owner == _msgSender() && stake.owner == tx.origin, "No swiping");
    require(!(block.timestamp - stake.value < MINIMUM_TO_EXIT), "GONNA BE COLD WITHOUT TWO DAY'S FOOD");
    if (totalFOODEarned < MAXIMUM_GLOBAL_FOOD) {
      owed = (block.timestamp - stake.value) * DAILY_FOOD_RATE / 1 days;
    } else if (stake.value > lastClaimTimestamp) {
      owed = 0; // $FOOD production stopped already
    } else {
      owed = (lastClaimTimestamp - stake.value) * DAILY_FOOD_RATE / 1 days; // stop earning additional $FOOD if it's all been earned
    }
    if(tokenId >= 20000 && tokenId < 30000 ){owed = ((owed*80)/100);}
    else if(tokenId >= 30000){owed = ((owed*60)/100);} 

      _payKongTax(owed * FOOD_CLAIM_TAX_PERCENTAGE / 100); // percentage tax to staked Kongs
      owed = owed * (100 - FOOD_CLAIM_TAX_PERCENTAGE) / 100; // remainder goes to Kaiju owner
      kongIsland[tokenId] = Stake({
        owner: _msgSender(),
        tokenId: uint16(tokenId),
        value: uint80(block.timestamp)
      }); // reset stake
    emit KaijuClaimed(tokenId, false);
  }

  function _claimKongFood(uint256 tokenId) internal returns (uint256 owed) {
    require(kongs.ownerOf(tokenId) == address(this), "AINT A PART OF THE PACK");
    uint256 King = _KingForKong(tokenId);
    Stake memory stake = pack[King][packIndices[tokenId]];
    require(stake.owner == _msgSender(), "SWIPER, NO SWIPING");
    owed = (King) * (FOODPerKing - stake.value); // Calculate portion of tokens based on King
      pack[King][packIndices[tokenId]] = Stake({
        owner: _msgSender(),
        tokenId: uint16(tokenId),
        value: uint80(FOODPerKing)
      }); // reset stake
    emit KongClaimed(tokenId, owed, false);  }

  /**
   * emergency unstake tokens
   * @param tokenIds the IDs of the tokens to claim earnings from
   */
  function rescue(uint256[] calldata tokenIds) external {
    require(rescueEnabled, "RESCUE DISABLED");
    require(_lastUnstakeBlock[tx.origin] != block.number, "No ReEntry");
    _lastUnstakeBlock[tx.origin] = block.number;
    uint256 tokenId;
    Stake memory stake;
    Stake memory lastStake;
    uint256 King;
    for (uint i = 0; i < tokenIds.length; i++) {
      tokenId = tokenIds[i];
      delete kongIsland[tokenId];
      if (isKaiju(tokenId)) {
        stake = kongIsland[tokenId];
        require(stake.owner == _msgSender(), "SWIPER, NO SWIPING");
        totalKaijuStaked -= 1;
        removeTokenID(tokenId);
        kongs.safeTransferFrom(address(this), _msgSender(), tokenId, ""); // send back Kaiju
        emit KaijuClaimed(tokenId, true);
      } else {
        King = _KingForKong(tokenId);
        stake = pack[King][packIndices[tokenId]];
        require(stake.owner == _msgSender(), "SWIPER, NO SWIPING");
        totalKingStaked -= King; // Remove King from total staked
        lastStake = pack[King][pack[King].length - 1];
        pack[King][packIndices[tokenId]] = lastStake; // Shuffle last Kong to current position
        packIndices[lastStake.tokenId] = packIndices[tokenId];
        pack[King].pop(); // Remove duplicate
        delete packIndices[tokenId]; // Delete old mapping
        removeTokenID(tokenId);
        kongs.safeTransferFrom(address(this), _msgSender(), tokenId, ""); // Send back Kong
        emit KongClaimed(tokenId,0, true);
      }
    }
  }

    function removeTokenID(uint256 tokenID) internal {
       for (uint256 i = 0; i < StakedByAddress[msg.sender].ID.length; i++) {
            if (StakedByAddress[msg.sender].ID[i] == tokenID) {
                StakedByAddress[msg.sender].ID[i] = StakedByAddress[msg.sender].ID[StakedByAddress[msg.sender].ID.length -1];
                StakedByAddress[msg.sender].ID.pop();
                break;
            }
        }
    }

  /** ACCOUNTING */

  /** 
   * add $FOOD to claimable pot for the Pack
   * @param amount $FOOD to add to the pot
   */
  function _payKongTax(uint256 amount) internal {
    if (totalKingStaked == 0) { // if there's no staked Kongs
      unaccountedRewards += amount; // keep track of $FOOD due to Kongs
      return;
    }
    // makes sure to include any unaccounted $FOOD 
    FOODPerKing += (amount + unaccountedRewards) / totalKingStaked;
    unaccountedRewards = 0;
  }

  /**
   * tracks $FOOD earnings to ensure it stops once 2.4 billion is eclipsed
   */
  modifier _updateEarnings() {
    if (totalFOODEarned < MAXIMUM_GLOBAL_FOOD) {
      totalFOODEarned += 
        ((block.timestamp - lastClaimTimestamp)
        * totalKaijuStakedOG
        * DAILY_FOOD_RATE / 1 days) + 
        ((block.timestamp - lastClaimTimestamp)
        * totalKaijuStakedGen1
        * (((DAILY_FOOD_RATE / 1 days)*80)/100))
        + ((block.timestamp - lastClaimTimestamp)
        * totalKaijuStakedGen2
        * (((DAILY_FOOD_RATE / 1 days)*60)/100));

      lastClaimTimestamp = block.timestamp;
    }
    _;
  }

  /** ADMIN */

  /**
   * allows owner to enable "rescue mode"
   * simplifies accounting, prioritizes tokens out in emergency
   */
  function setRescueEnabled(bool _enabled) external onlyOwner {
    rescueEnabled = _enabled;
  }


  function UpdateContracts(address _KONGS, address _FOOD) external onlyOwner{ 
    kongs = KONGS(_KONGS);
    food = IFOOD(_FOOD);
  }

  /**
   * enables owner to pause / unpause minting
   */
  function setPaused(bool _paused) external onlyOwner {
    if (_paused) _pause();
    else _unpause();
  }

  function setMinDaysToStake(uint256 _days) external onlyOwner {
    MINIMUM_TO_EXIT = _days * (1 days);
  }

  /** READ ONLY */

  /**
   * checks if a token is a Kaiju
   * @param tokenId the ID of the token to check
   * @return Kaiju - whether or not a token is a Kaiju
   */
  function isKaiju(uint256 tokenId) public view returns (bool Kaiju) {
        return kongs.isKaiju(tokenId);
  }

  function stakedIDsbyAddress(address _staker) public view returns (uint256[] memory) {
        return StakedByAddress[_staker].ID;
  }

  /**
   * gets the King score for a Kong
   * @param tokenId the ID of the Kong to get the King score for
   * @return the King score of the Kong (5-8)
   */
  function _KingForKong(uint256 tokenId) internal view returns (uint8) {
    return MAX_King - kongs.KingScore(tokenId); // King index is 0-4
  }

  function gainsUnclaimed(uint256 tokenId) external view returns(uint256 owed){
        require(kongIsland[tokenId].value >0, "Token not Staked");
        if(isKaiju(tokenId)){
        if (totalFOODEarned < MAXIMUM_GLOBAL_FOOD) {
        owed = (block.timestamp - kongIsland[tokenId].value) * DAILY_FOOD_RATE / 1 days;}
        else if (kongIsland[tokenId].value > lastClaimTimestamp) {
        owed = 0;} else {owed = (lastClaimTimestamp - kongIsland[tokenId].value) * DAILY_FOOD_RATE / 1 days;}
        }else{
        owed = (_KingForKong(tokenId)) * (FOODPerKing - kongIsland[tokenId].value);
        }
        if(isKaiju(tokenId) && tokenId >= 30000){return ((owed*60)/100);}
        else if(isKaiju(tokenId) && tokenId >= 20000){return ((owed*80)/100);}
        return owed;
  }

  /**
   * chooses a random Kong thief when a newly minted token is stolen
   * @param seed a random value to choose a Kong from
   * @return the owner of the randomly selected Kong thief
   */
  function randomKongOwner(uint256 seed) external view returns (address) {
    if (totalKingStaked == 0) return address(0x0);
    uint256 bucket = (seed & 0xFFFFFFFF) % totalKingStaked; // choose a value from 0 to total King staked
    uint256 cumulative;
    seed >>= 32;
    // loop through each bucket of Kongs with the same King score
    for (uint i = MAX_King - 3; i <= MAX_King; i++) {
      cumulative += pack[i].length * i;
      // if the value is not inside of that bucket, keep going
      if (bucket >= cumulative) continue;
      // get the address of a random Kong with that King score
      return pack[i][seed % pack[i].length].owner;
    }
    return address(0x0);
  }

  /**
   * generates a pseudorandom number
   * @param seed a value ensure different outcomes for different sources in the same block
   * @return a pseudorandom value
   */
  function random(uint256 seed) internal view returns (uint256) {
    return uint256(keccak256(abi.encodePacked(
      tx.origin,
      blockhash(block.number - 1),
      block.timestamp,
      seed
    )));
  }

  function onERC721Received(
        address,
        address from,
        uint256,
        bytes calldata
    ) external pure override returns (bytes4) {
      require(from == address(0x0), "Cannot send tokens to KongIsland directly");
      return IERC721Receiver.onERC721Received.selector;
    }

  
}