Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Tiles = {}
- local Objects = {}
- local Area = {}
- local AreaObjects = {}
- local AreaTitle = ""
- local inventory = {}
- local inventoryCraftSelected = {}
- local inventorySelected = 1
- local inventoryTab = 0
- local inventoryScroll = 0
- local playerX = 4
- local playerY = 4
- local areaX = 1
- local areaY = 1
- local areaZ = 1
- local showHud = false
- local countDown = 16
- function inventoryAdd(item, amount)
- if inventory[item] ~= nil then amount = amount + inventory[item] end
- inventory[item] = amount
- end
- function inventoryRemove(item, amount)
- inventory[item] = inventory[item] - amount
- if inventory[item] < 0 then
- inventory[item] = 0
- end
- if inventory[item] <= 0 then
- inventoryCraftSelected[item] = false
- end
- end
- function inventoryGetTotal()
- local count = 0
- for i = 1, 999, 1 do
- if inventory[i] ~= nil then if inventory[i] > 0 then count = count + 1 end end
- end
- return count
- end
- function inventoryCraft()
- recipe = 0
- craftItems = ""
- craftItemsList = {}
- for i = 1, 999, 1 do
- if inventoryCraftSelected[i] == 1 then
- if craftItems == "" then
- craftItems = i
- else
- craftItems = craftItems.." "..i
- end
- table.insert(craftItemsList, i)
- end
- end
- for i = 1, 999, 1 do
- if Objects[i] ~= nil then
- if craftItems == Objects[i]["craft"] then
- recipe = i
- break
- end
- end
- end
- if recipe ~= 0 then
- for i = 1, #craftItemsList, 1 do
- inventoryRemove(craftItemsList[i], 1)
- end
- inventoryAdd(recipe, 1)
- end
- end
- function contrastColor(col)
- local returnColor
- if col == 1 then returnColor = 32768
- elseif col == 2 then returnColor = 32768
- elseif col == 4 then returnColor = 32768
- elseif col == 8 then returnColor = 32768
- elseif col == 16 then returnColor = 32768
- elseif col == 32 then returnColor = 32768
- elseif col == 64 then returnColor = 32768
- elseif col == 128 then returnColor = 1
- elseif col == 256 then returnColor = 32768
- elseif col == 512 then returnColor = 1
- elseif col == 1024 then returnColor = 1
- elseif col == 2048 then returnColor = 1
- elseif col == 4096 then returnColor = 1
- elseif col == 8192 then returnColor = 1
- elseif col == 16384 then returnColor = 1
- elseif col == 32768 then returnColor = 1
- else returnColor = color.black
- end
- return returnColor
- end
- function twoToOne(x,y)
- return x + (y * 26) - 26
- end
- function draw(x, y, symbol, backColor, symbolColor)
- term.setCursorPos(x,y)
- if type(symbol) == "table" then
- local count = 1
- for key,value in pairs(symbol) do
- if type(backColor) == "table" then
- term.setBackgroundColor(backColor[key])
- else
- term.setBackgroundColor(backColor)
- end
- if type(symbolColor) == "table" then
- term.setTextColor(symbolColor[key])
- else
- term.setTextColor(symbolColor)
- end
- term.write(value)
- count = count + 1
- if x + count > 27 then
- count = 1
- x = 1
- y = y + 1
- term.setCursorPos(x,y)
- end
- end
- elseif type(symbol) == "string" then
- term.setBackgroundColor(backColor)
- term.setTextColor(symbolColor)
- term.write(symbol)
- end
- end
- function loadTiles(path)
- local fileList = fs.list(path)
- for key, file in ipairs(fileList) do
- print(path.."/"..file)
- local objectFile = fs.open(path.."/"..file,"r")
- local id = tonumber(objectFile.readLine())
- local symbol = objectFile.readLine()
- local backColor = tonumber(objectFile.readLine())
- local symbolColor = tonumber(objectFile.readLine())
- local oType = tonumber(objectFile.readLine())
- Tiles[id] = {symbol = symbol, backColor = backColor, symbolColor = symbolColor, name = file, oType = oType}
- objectFile.close()
- end
- end
- function loadObjects(path)
- local fileList = fs.list(path)
- for key, file in ipairs(fileList) do
- print(path.."/"..file)
- local objectFile = fs.open(path.."/"..file,"r")
- local id = tonumber(objectFile.readLine())
- local symbol = objectFile.readLine()
- local backColor = tonumber(objectFile.readLine())
- local symbolColor = tonumber(objectFile.readLine())
- local required = tonumber(objectFile.readLine())
- local drop = tonumber(objectFile.readLine())
- local amount = tonumber(objectFile.readLine())
- local craft = objectFile.readLine()
- Objects[id] = {symbol = symbol, backColor = backColor, symbolColor = symbolColor, name = file, required = required, drop = drop, amount = amount, craft = craft}
- objectFile.close()
- end
- end
- function loadArea(path, x, y, z)
- local fileList = fs.list(path)
- local matchingArea = nil
- local title = ""
- for key2, file2 in ipairs(fileList) do
- local fileList = fs.list(path.."/"..file2)
- for key, file in ipairs(fileList) do
- local areaFile = fs.open(path.."/"..file2.."/"..file,"r")
- if tonumber(areaFile.readLine()) == x and tonumber(areaFile.readLine()) == y and tonumber(areaFile.readLine()) == z then
- matchingArea = path.."/"..file2.."/"..file
- title = file2
- areaFile.close()
- break
- end
- areaFile.close()
- end
- end
- if matchingArea ~= nil then
- local buffer = {}
- local count = 1
- local areaFile = fs.open(matchingArea,"r")
- areaFile.readLine()
- areaFile.readLine()
- areaFile.readLine()
- local data = areaFile.readAll()
- areaFile.close()
- for i in string.gmatch(data, "%S+") do
- buffer[count] = tonumber(i)
- count = count + 1
- end
- for i=1, (26*20), 1 do
- Area[i] = buffer[i]
- AreaObjects[i] = buffer[i + (26*20)]
- end
- if " "..title.." " ~= AreaTitle then
- countDown = 16
- AreaTitle = " "..title.." "
- end
- return true
- else
- return false
- end
- end
- function drawArea()
- local symbols = {}
- local backColors = {}
- local symbolColors = {}
- for key,value in pairs(Area) do
- symbols[key]= Tiles[value]["symbol"]
- backColors[key] = Tiles[value]["backColor"]
- symbolColors[key] = Tiles[value]["symbolColor"]
- end
- draw(1, 1, symbols, backColors, symbolColors)
- end
- function drawObjects()
- local symbols
- local backColors
- local symbolColors
- for key,value in pairs(AreaObjects) do
- if value ~= 0 then
- if Objects[value]["symbolColor"] ~= 0 then
- symbols = Objects[value]["symbol"]
- else
- symbols = " "
- end
- symbolColors = Objects[value]["symbolColor"]
- if Objects[value]["backColor"] ~= 0 then
- backColors = Objects[value]["backColor"]
- else
- backColors = Tiles[Area[value]]["backColor"]
- end
- draw(key - (math.floor(key / 26) * 26), key / 26 + 1, symbols, backColors, symbolColors)
- end
- end
- end
- function handlePlayer()
- if playerX <= 0 then
- if loadArea("areas", areaX - 1, areaY, areaZ) then
- playerX = 26
- areaX = areaX - 1
- else
- playerX = 1
- end
- elseif playerY <= 0 then
- if loadArea("areas", areaX, areaY - 1, areaZ) then
- playerY = 20
- areaY = areaY - 1
- else
- playerY = 1
- end
- elseif playerX > 26 then
- if loadArea("areas", areaX + 1, areaY, areaZ) then
- playerX = 1
- areaX = areaX + 1
- else
- playerX = 26
- end
- elseif playerY > 20 then
- if loadArea("areas", areaX, areaY + 1, areaZ) then
- playerY = 1
- areaY = areaY + 1
- else
- playerY = 20
- end
- end
- end
- function drawPlayer()
- draw(playerX, playerY, "&", Tiles[Area[playerX + ((playerY * 26) - 26)]]["backColor"], contrastColor(Tiles[Area[playerX + ((playerY * 26) - 26)]]["backColor"]))
- end
- function drawHud()
- if showHud == true then
- paintutils.drawFilledBox(2,2,25,19,128)
- paintutils.drawBox(1,2,26,20,32)
- paintutils.drawBox(1,1,26,19,8192)
- term.setTextColor(1)
- term.setBackgroundColor(128)
- local inventoryDisplayed = {}
- for i = 1, 999, 1 do
- if inventory[i] ~= nil then
- if inventory[i] > 0 then
- table.insert(inventoryDisplayed, i)
- end
- end
- end
- for i = 1 + inventoryScroll, 16 + inventoryScroll, 1 do
- if inventoryDisplayed[i] ~= nil and inventoryDisplayed[i] ~= 0 then
- if inventoryCraftSelected[inventoryDisplayed[i]] == 1 and inventoryDisplayed[i] == inventorySelected then
- term.setBackgroundColor(1)
- term.setTextColor(32768)
- term.setCursorPos(2,2 + i - inventoryScroll)
- term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
- term.setBackgroundColor(128)
- term.setTextColor(1)
- elseif inventoryCraftSelected[inventoryDisplayed[i]] == 1 then
- term.setBackgroundColor(1)
- term.setTextColor(128)
- term.setCursorPos(2,2 + i - inventoryScroll)
- term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
- term.setBackgroundColor(128)
- term.setTextColor(1)
- elseif inventoryDisplayed[i] == inventorySelected then
- term.setBackgroundColor(256)
- term.setTextColor(32768)
- term.setCursorPos(2,2 + i - inventoryScroll)
- term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
- term.setBackgroundColor(128)
- term.setTextColor(1)
- else
- term.setCursorPos(2,2 + i - inventoryScroll)
- term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
- end
- end
- end
- else
- if countDown ~= 0 then
- term.setCursorPos((26 / 2) - (#AreaTitle / 2),10)
- term.setTextColor(1)
- term.setBackgroundColor(32768)
- term.write(AreaTitle)
- countDown = countDown - 1
- end
- end
- end
- function main()
- update_rate = 0.1
- local _timer = os.startTimer(update_rate)
- loadTiles("tiles")
- loadArea("areas", areaX, areaY, areaZ)
- loadObjects("objects")
- while true do
- local event, key
- local lockMovement = false
- while key ~= _timer do
- local oldX = playerX
- local oldY = playerY
- event, key = os.pullEvent()
- if showHud == false then
- if key == keys.w and lockMovement == false then
- playerY = playerY - 1
- lockMovement = true
- elseif key == keys.a and lockMovement == false then
- playerX = playerX - 1
- lockMovement = true
- elseif key == keys.s and lockMovement == false then
- playerY = playerY + 1
- lockMovement = true
- elseif key == keys.d and lockMovement == false then
- playerX = playerX + 1
- lockMovement = true
- elseif key == keys.e then
- showHud = true
- end
- if playerX > 0 and playerX <= 26 and playerY > 0 and playerY <= 20 then
- if Tiles[Area[twoToOne(playerX, playerY)]]["oType"] == 1 then
- playerX = oldX
- playerY = oldY
- lockMovement = false
- elseif Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"] ~= 0 and AreaObjects[twoToOne(playerX, playerY)] ~= 0 then
- if inventory[Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"]] == nil then inventory[Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"]] = 0 end
- if inventory[Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"]] > 0 then
- inventoryRemove(Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"],1)
- inventoryAdd(Objects[AreaObjects[twoToOne(playerX, playerY)]]["drop"],Objects[AreaObjects[twoToOne(playerX, playerY)]]["amount"])
- AreaObjects[twoToOne(playerX, playerY)] = 0
- else
- playerX = oldX
- playerY = oldY
- lockMovement = false
- end
- elseif AreaObjects[twoToOne(playerX, playerY)] ~= 0 then
- inventoryAdd(Objects[AreaObjects[twoToOne(playerX, playerY)]]["drop"],Objects[AreaObjects[twoToOne(playerX, playerY)]]["amount"])
- AreaObjects[twoToOne(playerX, playerY)] = 0
- end
- end
- else
- if key == keys.w then
- local newSelected
- for i = inventorySelected - 1, 0, -1 do
- if inventory[i] ~= nil then
- if inventory[i] > 0 then
- newSelected = i
- break
- end
- end
- end
- if newSelected == nil then
- for i = 999, 0, -1 do
- if inventory[i] ~= nil then
- if inventory[i] > 0 then
- newSelected = i
- break
- end
- end
- end
- end
- if newSelected == nil then
- newSelected = 1
- end
- inventorySelected = newSelected
- elseif key == keys.a then
- elseif key == keys.s then
- local newSelected
- for i = inventorySelected + 1, 999, 1 do
- if inventory[i] ~= nil then
- if inventory[i] > 0 then
- newSelected = i
- break
- end
- end
- end
- if newSelected == nil then
- for i = 1, 999, 1 do
- if inventory[i] ~= nil then
- if inventory[i] > 0 then
- newSelected = i
- break
- end
- end
- end
- end
- if newSelected == nil then
- newSelected = 1
- end
- inventorySelected = newSelected
- elseif key == keys.d then
- elseif key == keys.e then
- showHud = false
- elseif key == keys.enter then
- if inventoryCraftSelected[inventorySelected] == nil then inventoryCraftSelected[inventorySelected] = 0 end
- if inventoryCraftSelected[inventorySelected] == 0 then inventoryCraftSelected[inventorySelected] = 1
- else inventoryCraftSelected[inventorySelected] = 0 end
- elseif key == keys.c then
- inventoryCraft()
- end
- end
- end
- _timer = os.startTimer(update_rate)
- if showHud == false then
- handlePlayer()
- drawArea()
- drawObjects()
- drawPlayer()
- end
- drawHud()
- term.setCursorPos(1,1)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement