Create a new script called CryptsAndCaverns. This script will contain functions to help load the NFT and do the conversion so it can be passed to the Build function of the MapBuilder API.
After creating the script, you will need to add the 3 scripts that were imported previously so that the CryptsAndCaverns code has references to them.
Select the CryptsAndCaverns script in My Scripts found in Project Content.
Add the Base64 script as a custom property.
Add the JSON script as a custom property.
Add the SVGParser script as a custom property.
Edit CryptsAndCaverns Script
Open up the CryptsAndCaverns script and add the following code that will require the libraries that were added as custom properties.
local BASE64 = require(script:GetCustomProperty("Base64"))
local JSON = require(script:GetCustomProperty("JSON"))
local SVG_PARSER = require(script:GetCustomProperty("SVGParser"))
Code language:Lua(lua)
Create CryptsAndCaverns Table
Create a table called CryptsAndCaverns and set up the properties for the contract address and the collection count. The reason for the collection count is so that a random token ID can be retrieved to load a random map if required.
Create a new function called Load. This function will accept an optional parameter that will either load the NFT based on the token ID passed in, or load a random NFT. The function will use the libraries that have been required to decode the data and convert it into something usable that will be returned back.
Using the Blockchain API, you can retrieve a token using the GetToken function. This function request the smart contract address, and a token ID. When passing the token ID to the GetToken function, make sure it is a String type.
After fetching the token, you need to fetch the rawMetadata and extract the first Bas64 encoded string. When decoded, it will contain a table that contains information about the NFT, but most importantly it contains another Base64 encoded string that contains the image data in SVG format. The SVG string is sent to the SVG Parser which will create a map based on the data. This is then in a compatiable format that can be converted into ASCII.
functionCryptsAndCaverns.Load(tokenId)local token = Blockchain.GetToken(CryptsAndCaverns.SMART_CONTRACT_ADDRESS, tostring(tokenId ormath.random(CryptsAndCaverns.COLLECTION_COUNT)))
if(token ~= nil) thenlocal metadata = token.rawMetadata
local _, base64String = CoreString.Split(metadata, ",")
local decodedBase64String = BASE64.decode(base64String)
local objMetadata = JSON.decode(decodedBase64String)
local _, SVGBase64String = CoreString.Split(objMetadata.image, ",")
local rawSVG = BASE64.decode(SVGBase64String)
local parser = SVG_PARSER:New(rawSVG)
local mapData = parser:GetMap()
local rows = parser:GetRows()
local cols = parser:GetCols()
local convertedMapData = CryptsAndCaverns.ConvertData(mapData, rows, cols)
local convertedASCII = CryptsAndCaverns.GetConvertedASCII(convertedMapData)
return convertedASCII, rows, cols
else warn("Could not load token.")
endreturn"", 0, 0endCode language:Lua(lua)
Create ConvertData Function
Cretae a new function called ConvertData that will be called from within the Load function. It will receive the map data from the SVG Parser, the total columns and rows. This function will go through each cell of the mapData table to make sure it is all valid. It will add random enemies spawn points and a random player spawn point to the map. This is then a valid map that will then be converted to ASCII to be used to build the 3D map in world.
functionCryptsAndCaverns.ConvertData(mapData, cols, rows)local wallString = nillocal converted = {}
local hasSpawn = falsefor row = 1, #mapData dofor col = 1, #mapData[row] doif mapData[row][col] ~= "-"andnot wallString then wallString = mapData[row][col]
breakendendendlocal allZeroTiles = {}
for row = 1, rows dotable.insert(converted, {})
for col = 1, cols dolocal entry = mapData[row][col]
if entry == wallString then converted[row][col] = "1"elselocal spawnEnemy = math.random(1, 100)
if spawnEnemy <= 10then converted[row][col] = "E"elselocal value = "0"if(not hasSpawn) thenlocal isSpawn = math.random(30)
if(isSpawn == 1) then value = "S" hasSpawn = trueendend converted[row][col] = value
if value == "0"then allZeroTiles[#allZeroTiles + 1] = { row, col }
endendendendendif(not hasSpawn and #allZeroTiles > 0) thenlocal spawnTile = allZeroTiles[math.random(#allZeroTiles)]
converted[spawnTile[1]][spawnTile[2]] = "S"endreturn converted
endCode language:Lua(lua)
Create GetConvertedASCII Function
Create a new function called GetConvertedASCII that will turn the map data into a valid ASCII string that can then be sent to the MapBuilder script to build the map.
Whenever you created a Lua library/API you need to return something back so that scripts which require the library/api can use the functions within it. In this case you want to return the CryptsAndCaverns table and the end of the script.
return CryptsAndCaverns
Code language:Lua(lua)
The CryptsAndCaverns Script
CryptsAndCaverns
local BASE64 = require(script:GetCustomProperty("Base64"))
local JSON = require(script:GetCustomProperty("JSON"))
local SVG_PARSER = require(script:GetCustomProperty("SVGParser"))
local CryptsAndCaverns = {
SMART_CONTRACT_ADDRESS = "0x86f7692569914b5060ef39aab99e62ec96a6ed45",
COLLECTION_COUNT = 8784}
functionCryptsAndCaverns.Load(tokenId)local token = Blockchain.GetToken(CryptsAndCaverns.SMART_CONTRACT_ADDRESS, tostring(tokenId ormath.random(CryptsAndCaverns.COLLECTION_COUNT)))
if(token ~= nil) thenlocal metadata = token.rawMetadata
local _, base64String = CoreString.Split(metadata, ",")
local decodedBase64String = BASE64.decode(base64String)
local objMetadata = JSON.decode(decodedBase64String)
local _, SVGBase64String = CoreString.Split(objMetadata.image, ",")
local rawSVG = BASE64.decode(SVGBase64String)
local parser = SVG_PARSER:New(rawSVG)
local mapData = parser:GetMap()
local rows = parser:GetRows()
local cols = parser:GetCols()
local convertedMapData = CryptsAndCaverns.ConvertData(mapData, rows, cols)
local convertedASCII = CryptsAndCaverns.GetConvertedASCII(convertedMapData)
return convertedASCII, rows, cols
else warn("Could not load token.")
endreturn"", 0, 0endfunctionCryptsAndCaverns.ConvertData(mapData, cols, rows)local wallString = nillocal converted = {}
local hasSpawn = falsefor row = 1, #mapData dofor col = 1, #mapData[row] doif mapData[row][col] ~= "-"andnot wallString then wallString = mapData[row][col]
breakendendendlocal allZeroTiles = {}
for row = 1, rows dotable.insert(converted, {})
for col = 1, cols dolocal entry = mapData[row][col]
if entry == wallString then converted[row][col] = "1"elselocal spawnEnemy = math.random(1, 100)
if spawnEnemy <= 10then converted[row][col] = "E"elselocal value = "0"if(not hasSpawn) thenlocal isSpawn = math.random(30)
if(isSpawn == 1) then value = "S" hasSpawn = trueendend converted[row][col] = value
if value == "0"then allZeroTiles[#allZeroTiles + 1] = { row, col }
endendendendendif(not hasSpawn and #allZeroTiles > 0) thenlocal spawnTile = allZeroTiles[math.random(#allZeroTiles)]
converted[spawnTile[1]][spawnTile[2]] = "S"endreturn converted
endfunctionCryptsAndCaverns.GetConvertedASCII(converted)local str = ""for r = 1, #converted dofor c = 1, #converted[r] do str = str .. string.sub(tostring(converted[r][c]), 1, 1) .. ""end str = str .. "\n"endreturn str
endreturn CryptsAndCaverns
Code language:Lua(lua)
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!