Advertisement
oli414

[CC] Pocket computer adventure game

Mar 27th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.10 KB | None | 0 0
  1. local Tiles = {}
  2. local Objects = {}
  3.  
  4. local Area = {}
  5. local AreaObjects = {}
  6. local AreaTitle = ""
  7.  
  8. local inventory = {}
  9. local inventoryCraftSelected = {}
  10. local inventorySelected = 1
  11. local inventoryTab = 0
  12. local inventoryScroll = 0
  13.  
  14. local playerX = 4
  15. local playerY = 4
  16. local areaX = 1
  17. local areaY = 1
  18. local areaZ = 1
  19.  
  20. local showHud = false
  21.  
  22. local countDown = 16
  23.  
  24. function inventoryAdd(item, amount)
  25.     if inventory[item] ~= nil then amount = amount + inventory[item] end
  26.     inventory[item] = amount
  27. end
  28.  
  29. function inventoryRemove(item, amount)
  30.     inventory[item] = inventory[item] - amount
  31.     if inventory[item] < 0 then
  32.         inventory[item] = 0
  33.     end
  34.     if inventory[item] <= 0 then
  35.         inventoryCraftSelected[item] = false
  36.     end
  37. end
  38.  
  39. function inventoryGetTotal()
  40.     local count = 0
  41.     for i = 1, 999, 1 do
  42.         if inventory[i] ~= nil then if inventory[i] > 0 then count = count + 1 end end
  43.     end
  44.     return count
  45. end
  46.  
  47. function inventoryCraft()
  48.     recipe = 0
  49.     craftItems = ""
  50.     craftItemsList = {}
  51.     for i = 1, 999, 1 do
  52.         if inventoryCraftSelected[i] == 1 then
  53.             if craftItems == "" then
  54.                 craftItems = i
  55.             else
  56.                 craftItems = craftItems.." "..i
  57.             end
  58.             table.insert(craftItemsList, i)
  59.         end
  60.     end
  61.     for i = 1, 999, 1 do
  62.         if Objects[i] ~= nil then
  63.             if craftItems == Objects[i]["craft"] then
  64.                 recipe = i
  65.                 break
  66.             end
  67.         end
  68.     end
  69.     if recipe ~= 0 then
  70.         for i = 1, #craftItemsList, 1 do
  71.             inventoryRemove(craftItemsList[i], 1)
  72.         end
  73.         inventoryAdd(recipe, 1)
  74.     end
  75. end
  76.  
  77. function contrastColor(col)
  78.     local returnColor
  79.     if col == 1 then returnColor = 32768
  80.     elseif col == 2 then returnColor = 32768
  81.     elseif col == 4 then returnColor = 32768
  82.     elseif col == 8 then returnColor = 32768
  83.     elseif col == 16 then returnColor = 32768
  84.     elseif col == 32 then returnColor = 32768
  85.     elseif col == 64 then returnColor = 32768
  86.     elseif col == 128 then returnColor = 1
  87.     elseif col == 256 then returnColor = 32768
  88.     elseif col == 512 then returnColor = 1
  89.     elseif col == 1024 then returnColor = 1
  90.     elseif col == 2048 then returnColor = 1
  91.     elseif col == 4096 then returnColor = 1
  92.     elseif col == 8192 then returnColor = 1
  93.     elseif col == 16384 then returnColor = 1
  94.     elseif col == 32768 then returnColor = 1
  95.     else returnColor = color.black
  96.     end
  97.     return returnColor
  98. end
  99.  
  100. function twoToOne(x,y)
  101.     return x + (y * 26) - 26
  102. end
  103.  
  104. function draw(x, y, symbol, backColor, symbolColor)
  105.     term.setCursorPos(x,y)
  106.     if type(symbol) == "table" then
  107.         local count = 1
  108.         for key,value in pairs(symbol) do
  109.             if type(backColor) == "table" then
  110.                 term.setBackgroundColor(backColor[key])
  111.             else
  112.                 term.setBackgroundColor(backColor)
  113.             end
  114.             if type(symbolColor) == "table" then
  115.                 term.setTextColor(symbolColor[key])
  116.             else
  117.                 term.setTextColor(symbolColor)
  118.             end
  119.             term.write(value)
  120.             count = count + 1
  121.             if x + count > 27 then
  122.                 count = 1
  123.                 x = 1
  124.                 y = y + 1
  125.                 term.setCursorPos(x,y)
  126.             end
  127.         end
  128.     elseif type(symbol) == "string" then
  129.         term.setBackgroundColor(backColor)
  130.         term.setTextColor(symbolColor)
  131.         term.write(symbol)
  132.     end
  133. end
  134.  
  135. function loadTiles(path)
  136.     local fileList = fs.list(path)
  137.     for key, file in ipairs(fileList) do
  138.         print(path.."/"..file)
  139.         local objectFile = fs.open(path.."/"..file,"r")
  140.         local id = tonumber(objectFile.readLine())
  141.         local symbol = objectFile.readLine()
  142.         local backColor = tonumber(objectFile.readLine())
  143.         local symbolColor = tonumber(objectFile.readLine())
  144.         local oType = tonumber(objectFile.readLine())
  145.         Tiles[id] = {symbol = symbol, backColor = backColor, symbolColor = symbolColor, name = file, oType = oType}
  146.         objectFile.close()
  147.     end
  148. end
  149.  
  150.    
  151. function loadObjects(path)
  152.     local fileList = fs.list(path)
  153.     for key, file in ipairs(fileList) do
  154.         print(path.."/"..file)
  155.         local objectFile = fs.open(path.."/"..file,"r")
  156.         local id = tonumber(objectFile.readLine())
  157.         local symbol = objectFile.readLine()
  158.         local backColor = tonumber(objectFile.readLine())
  159.         local symbolColor = tonumber(objectFile.readLine())
  160.         local required = tonumber(objectFile.readLine())
  161.         local drop = tonumber(objectFile.readLine())
  162.         local amount = tonumber(objectFile.readLine())
  163.         local craft = objectFile.readLine()
  164.         Objects[id] = {symbol = symbol, backColor = backColor, symbolColor = symbolColor, name = file, required = required, drop = drop, amount = amount, craft = craft}
  165.         objectFile.close()
  166.     end
  167. end
  168.  
  169. function loadArea(path, x, y, z)
  170.     local fileList = fs.list(path)
  171.     local matchingArea = nil
  172.     local title = ""
  173.     for key2, file2 in ipairs(fileList) do
  174.         local fileList = fs.list(path.."/"..file2)
  175.         for key, file in ipairs(fileList) do
  176.             local areaFile = fs.open(path.."/"..file2.."/"..file,"r")
  177.             if tonumber(areaFile.readLine()) == x and tonumber(areaFile.readLine()) == y and tonumber(areaFile.readLine()) == z then
  178.                 matchingArea = path.."/"..file2.."/"..file
  179.                 title = file2
  180.                 areaFile.close()
  181.                 break
  182.             end
  183.             areaFile.close()
  184.         end
  185.     end
  186.     if matchingArea ~= nil then
  187.         local buffer = {}
  188.         local count = 1
  189.         local areaFile = fs.open(matchingArea,"r")
  190.         areaFile.readLine()
  191.         areaFile.readLine()
  192.         areaFile.readLine()
  193.         local data = areaFile.readAll()
  194.         areaFile.close()
  195.         for i in string.gmatch(data, "%S+") do
  196.           buffer[count] = tonumber(i)
  197.           count = count + 1
  198.         end
  199.         for i=1, (26*20), 1 do
  200.             Area[i] = buffer[i]
  201.             AreaObjects[i] = buffer[i + (26*20)]
  202.         end
  203.         if " "..title.." " ~= AreaTitle then
  204.             countDown = 16
  205.             AreaTitle = " "..title.." "
  206.         end
  207.         return true
  208.     else
  209.         return false
  210.     end
  211. end
  212.  
  213. function drawArea()
  214.     local symbols = {}
  215.     local backColors = {}
  216.     local symbolColors = {}
  217.     for key,value in pairs(Area) do
  218.         symbols[key]= Tiles[value]["symbol"]
  219.         backColors[key] = Tiles[value]["backColor"]
  220.         symbolColors[key] = Tiles[value]["symbolColor"]
  221.     end
  222.     draw(1, 1, symbols, backColors, symbolColors)
  223. end
  224.  
  225. function drawObjects()
  226.     local symbols
  227.     local backColors
  228.     local symbolColors
  229.     for key,value in pairs(AreaObjects) do
  230.         if value ~= 0 then
  231.             if Objects[value]["symbolColor"] ~= 0 then
  232.                 symbols = Objects[value]["symbol"]
  233.             else
  234.                 symbols = " "
  235.             end
  236.             symbolColors = Objects[value]["symbolColor"]
  237.            
  238.             if Objects[value]["backColor"] ~= 0 then
  239.                 backColors = Objects[value]["backColor"]
  240.             else
  241.                 backColors = Tiles[Area[value]]["backColor"]
  242.             end
  243.             draw(key - (math.floor(key / 26) * 26), key / 26 + 1, symbols, backColors, symbolColors)
  244.         end
  245.     end
  246. end
  247.  
  248. function handlePlayer()
  249.     if playerX <= 0 then
  250.         if loadArea("areas", areaX - 1, areaY, areaZ) then
  251.             playerX = 26
  252.             areaX = areaX - 1
  253.         else
  254.             playerX = 1
  255.         end
  256.     elseif playerY <= 0 then
  257.         if loadArea("areas", areaX, areaY - 1, areaZ) then
  258.             playerY = 20
  259.             areaY = areaY - 1
  260.         else
  261.             playerY = 1
  262.         end
  263.     elseif playerX > 26 then
  264.         if loadArea("areas", areaX + 1, areaY, areaZ) then
  265.             playerX = 1
  266.             areaX = areaX + 1
  267.         else
  268.             playerX = 26
  269.         end
  270.     elseif playerY > 20 then
  271.         if loadArea("areas", areaX, areaY + 1, areaZ) then
  272.             playerY = 1
  273.             areaY = areaY + 1
  274.         else
  275.             playerY = 20
  276.         end
  277.     end
  278. end
  279.  
  280. function drawPlayer()
  281.     draw(playerX, playerY, "&", Tiles[Area[playerX + ((playerY * 26) - 26)]]["backColor"], contrastColor(Tiles[Area[playerX + ((playerY * 26) - 26)]]["backColor"]))
  282. end
  283.  
  284. function drawHud()
  285.     if showHud == true then
  286.         paintutils.drawFilledBox(2,2,25,19,128)
  287.         paintutils.drawBox(1,2,26,20,32)
  288.         paintutils.drawBox(1,1,26,19,8192)
  289.         term.setTextColor(1)
  290.         term.setBackgroundColor(128)
  291.        
  292.         local inventoryDisplayed = {}
  293.         for i = 1, 999, 1 do
  294.             if inventory[i] ~= nil then
  295.                 if inventory[i] > 0 then
  296.                     table.insert(inventoryDisplayed, i)
  297.                 end
  298.             end
  299.         end
  300.        
  301.         for i = 1 + inventoryScroll, 16 + inventoryScroll, 1 do
  302.             if inventoryDisplayed[i] ~= nil and inventoryDisplayed[i] ~= 0 then
  303.                 if inventoryCraftSelected[inventoryDisplayed[i]] == 1 and inventoryDisplayed[i] == inventorySelected then
  304.                     term.setBackgroundColor(1)
  305.                     term.setTextColor(32768)
  306.                     term.setCursorPos(2,2 + i - inventoryScroll)
  307.                     term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
  308.                     term.setBackgroundColor(128)
  309.                     term.setTextColor(1)
  310.                 elseif inventoryCraftSelected[inventoryDisplayed[i]] == 1 then
  311.                     term.setBackgroundColor(1)
  312.                     term.setTextColor(128)
  313.                     term.setCursorPos(2,2 + i - inventoryScroll)
  314.                     term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
  315.                     term.setBackgroundColor(128)
  316.                     term.setTextColor(1)
  317.                 elseif inventoryDisplayed[i] == inventorySelected then
  318.                     term.setBackgroundColor(256)
  319.                     term.setTextColor(32768)
  320.                     term.setCursorPos(2,2 + i - inventoryScroll)
  321.                     term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
  322.                     term.setBackgroundColor(128)
  323.                     term.setTextColor(1)
  324.                 else
  325.                     term.setCursorPos(2,2 + i - inventoryScroll)
  326.                     term.write("["..inventory[inventoryDisplayed[i]].."] "..Objects[inventoryDisplayed[i]]["name"])
  327.                 end
  328.             end
  329.            
  330.         end
  331.     else
  332.         if countDown ~= 0 then
  333.             term.setCursorPos((26 / 2) - (#AreaTitle / 2),10)
  334.             term.setTextColor(1)
  335.             term.setBackgroundColor(32768)
  336.             term.write(AreaTitle)
  337.             countDown = countDown - 1
  338.         end
  339.     end
  340. end
  341.  
  342. function main()
  343.  
  344.     update_rate = 0.1
  345.     local _timer = os.startTimer(update_rate)
  346.    
  347.     loadTiles("tiles")
  348.     loadArea("areas", areaX, areaY, areaZ)
  349.     loadObjects("objects")
  350.    
  351.     while true do
  352.         local event, key
  353.         local lockMovement = false
  354.         while key ~= _timer do
  355.             local oldX = playerX
  356.             local oldY = playerY
  357.             event, key = os.pullEvent()
  358.             if showHud == false then
  359.                 if key == keys.w and lockMovement == false then
  360.                     playerY = playerY - 1
  361.                     lockMovement = true
  362.                 elseif key == keys.a and lockMovement == false then
  363.                     playerX = playerX - 1
  364.                     lockMovement = true
  365.                 elseif key == keys.s and lockMovement == false then
  366.                     playerY = playerY + 1
  367.                     lockMovement = true
  368.                 elseif key == keys.d and lockMovement == false then
  369.                     playerX = playerX + 1
  370.                     lockMovement = true
  371.                 elseif key == keys.e then
  372.                     showHud = true
  373.                 end
  374.                 if playerX > 0 and playerX <= 26 and playerY > 0 and playerY <= 20 then
  375.                     if Tiles[Area[twoToOne(playerX, playerY)]]["oType"] == 1 then
  376.                         playerX = oldX
  377.                         playerY = oldY
  378.                         lockMovement = false
  379.                     elseif Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"] ~= 0 and AreaObjects[twoToOne(playerX, playerY)] ~= 0 then
  380.                         if inventory[Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"]] == nil then inventory[Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"]] = 0 end
  381.                         if inventory[Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"]] > 0 then
  382.                             inventoryRemove(Objects[AreaObjects[twoToOne(playerX, playerY)]]["required"],1)
  383.                             inventoryAdd(Objects[AreaObjects[twoToOne(playerX, playerY)]]["drop"],Objects[AreaObjects[twoToOne(playerX, playerY)]]["amount"])
  384.                             AreaObjects[twoToOne(playerX, playerY)] = 0
  385.                         else
  386.                             playerX = oldX
  387.                             playerY = oldY
  388.                             lockMovement = false
  389.                         end
  390.                     elseif AreaObjects[twoToOne(playerX, playerY)] ~= 0 then
  391.                         inventoryAdd(Objects[AreaObjects[twoToOne(playerX, playerY)]]["drop"],Objects[AreaObjects[twoToOne(playerX, playerY)]]["amount"])
  392.                         AreaObjects[twoToOne(playerX, playerY)] = 0
  393.                     end
  394.                 end
  395.             else
  396.                 if key == keys.w then
  397.                     local newSelected
  398.                     for i = inventorySelected - 1, 0, -1 do
  399.                         if inventory[i] ~= nil then
  400.                             if inventory[i] > 0 then
  401.                                 newSelected = i
  402.                                 break
  403.                             end
  404.                         end
  405.                     end
  406.                     if newSelected == nil then
  407.                         for i = 999, 0, -1 do
  408.                             if inventory[i] ~= nil then
  409.                                 if inventory[i] > 0 then
  410.                                     newSelected = i
  411.                                     break
  412.                                 end
  413.                             end
  414.                         end
  415.                     end
  416.                     if newSelected == nil then
  417.                         newSelected = 1
  418.                     end
  419.                     inventorySelected = newSelected
  420.                    
  421.                 elseif key == keys.a then
  422.                
  423.                 elseif key == keys.s then
  424.                     local newSelected
  425.                     for i = inventorySelected + 1, 999, 1 do
  426.                         if inventory[i] ~= nil then
  427.                             if inventory[i] > 0 then
  428.                                 newSelected = i
  429.                                 break
  430.                             end
  431.                         end
  432.                     end
  433.                     if newSelected == nil then
  434.                         for i = 1, 999, 1 do
  435.                             if inventory[i] ~= nil then
  436.                                 if inventory[i] > 0 then
  437.                                     newSelected = i
  438.                                     break
  439.                                 end
  440.                             end
  441.                         end
  442.                     end
  443.                     if newSelected == nil then
  444.                         newSelected = 1
  445.                     end
  446.                     inventorySelected = newSelected
  447.                 elseif key == keys.d then
  448.                    
  449.                 elseif key == keys.e then
  450.                     showHud = false
  451.                 elseif key == keys.enter then
  452.                     if inventoryCraftSelected[inventorySelected] == nil then inventoryCraftSelected[inventorySelected] = 0 end
  453.                     if inventoryCraftSelected[inventorySelected] == 0 then inventoryCraftSelected[inventorySelected] = 1
  454.                     else inventoryCraftSelected[inventorySelected] = 0 end
  455.                 elseif key == keys.c then
  456.                     inventoryCraft()
  457.                 end
  458.             end
  459.         end
  460.         _timer = os.startTimer(update_rate)
  461.        
  462.         if showHud == false then
  463.             handlePlayer()
  464.            
  465.             drawArea()
  466.             drawObjects()
  467.             drawPlayer()
  468.         end
  469.        
  470.         drawHud()
  471.         term.setCursorPos(1,1)
  472.     end
  473. end
  474.  
  475. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement