xXm0dzXx

Temporary Minecraft

Oct 21st, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.67 KB | None | 0 0
  1. local x,y = term.getSize()
  2. local charX, charY = 4,4
  3. local selectedSlot = 1
  4. local health = 10
  5. local debug1 = false
  6. local debugX = 1
  7. local debugY = 1
  8. local scrollX = 0
  9. local scrollY = 0
  10. local hillyGen = math.random(1,9)
  11. local biome = "desert"
  12. local worldTransition = nil
  13. local screenData = {}
  14. biomes = {
  15.     [1] = "desert",
  16.     [2] = "flatland",
  17. }
  18. world = {
  19. }
  20. inventory = {
  21. }
  22.  
  23. blocks = {}
  24. chunks = {}
  25.  
  26. local function cPrint(text)
  27.     local x,y = term.getSize()
  28.     x2,y2 = term.getCursorPos()
  29.     term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  30.     write(text.. "\n")
  31. end
  32.  
  33. function registerBlock( id, name, color, hardness, info )
  34.     blocks[id] = {
  35.                     ["DisplayName"] = name,
  36.                     ["Color"] = color,
  37.                     ["Hardness"] = hardness,
  38.                     ["Others"] = info,
  39.                 }
  40. end
  41.  
  42. function getColorByName( name )
  43.     for i=1,256 do
  44.         blockInfo = blocks[i]
  45.         if blockInfo then
  46.             if blockInfo["DisplayName"] == name then
  47.                 return blockInfo["Color"]
  48.             end
  49.         end
  50.     end
  51.    
  52.     return false
  53. end
  54.  
  55. function getHardnessByName( name )
  56.     for i=1,256 do
  57.         blockInfo = blocks[i]
  58.         if blockInfo then
  59.             if blockInfo["DisplayName"] == name then
  60.                 return blockInfo["Hardness"]
  61.             end
  62.         end
  63.     end
  64.    
  65.     return false
  66. end
  67.  
  68. function drawPixel( x22, y22, color )
  69.     if screenData[ x22.. "/" ..y22 ] ~= color then
  70.         screenData[ x22.. "/" ..y22 ] = color
  71.         local x1,y1 = term.getCursorPos()
  72.         term.setCursorPos(x22,y22)
  73.         if tostring( color ) == color then
  74.             if getColorByName( color ) then
  75.                 term.setBackgroundColour( getColorByName( color ) )
  76.             else
  77.                 term.setBackgroundColour( colors.magenta )
  78.             end
  79.         else
  80.             term.setBackgroundColour( color )
  81.         end
  82.         write(" ")
  83.         term.setCursorPos(x1,y1)
  84.         term.setBackgroundColour( colors.black )
  85.     end
  86. end
  87.  
  88. function drawCharacter()
  89.     drawPixel(charX, charY-1, colors.orange)
  90.     drawPixel(charX, charY, colors.cyan)
  91. end
  92.  
  93. function hasBlock( name, inventory1 )
  94.     for i=1,8 do
  95.         if inventory[i] then
  96.             if (inventory[i])["name"] == name then
  97.                 if inventory1 and inventory[i]["quantity"] == 9 then
  98.                 else
  99.                     return i, inventory[i]["quantity"]
  100.                 end
  101.             end
  102.         end
  103.     end
  104.     return false
  105. end
  106.  
  107. function breakBlock( x, y )
  108.     x = x - scrollX
  109.     y = y - scrollY
  110.     for i=1,#world do
  111.         blockInfo = world[i]
  112.         if blockInfo["x"] == x and blockInfo["y"] == y then
  113.             inventoryNum = #inventory+1
  114.             inv, amm = hasBlock( blockInfo["name"], true )
  115.             tValue = math.random(1,3)
  116.             if blockInfo["name"] ~= "Sapling" then
  117.                 tValue = 3
  118.             end
  119.             if tValue == 3  then
  120.                 if inv and amm ~= 9 then
  121.                     inventory[inv]["quantity"] = amm +1
  122.                 else
  123.                     inventory[inventoryNum] = {
  124.                             ["name"] = blockInfo["name"],
  125.                             ["quantity"] = 1,
  126.                         }
  127.                 end
  128.             end
  129.             world[i] = nil
  130.         end
  131.     end
  132.     cleanupWorld()
  133. end
  134.  
  135. function getBlockInfo( x,y )
  136.     x = x - scrollX
  137.     y = y - scrollY
  138.     for i=1,#world do
  139.         blockInfo = world[i]
  140.         if blockInfo["x"] == x and blockInfo["y"] == y then
  141.             return blockInfo["x"], blockInfo["y"], blockInfo["name"]
  142.         end
  143.     end
  144. end
  145.  
  146. function cleanupWorld()
  147.     local nWorld = {}
  148.     for k,v in pairs( world ) do
  149.         if v then
  150.             nWorld[#nWorld+1] = v
  151.         end
  152.     end
  153.     world = nWorld
  154. end
  155.  
  156. function clear()
  157.     term.setBackgroundColour( colors.lightBlue )
  158.     screenData = {}
  159.     drawPixel(1,1, colors.yellow )
  160.     drawPixel(2,1, colors.orange )
  161.     drawPixel(2,2, colors.orange )
  162.     drawPixel(1,2, colors.orange )
  163. end
  164.  
  165. function redrawWorld()
  166.     local oldScreenData = screenData
  167.    
  168.     clear()
  169.     drawWorld()
  170.     drawCharacter()
  171.     drawGUI()
  172.    
  173.     if oldScreenData ~= screenData then
  174.         for i,v in pairs( oldScreenData ) do
  175.             if screenData[ i ] ~= nil then
  176.                 local tWords = {}
  177.                 for match in string.gmatch( i, "[^/\t]+" ) do
  178.                     table.insert( tWords, match )
  179.                 end
  180.                
  181.                 local screenX = tonumber(tWords[1])
  182.                 local screenY = tonumber(tWords[2])
  183.                
  184.                 term.setCursorPos( screenX, screenY )
  185.                 term.setBackgroundColour( colors.lightBlue )
  186.                 write(" ")
  187.                 term.setBackgroundColour( colors.black )
  188.             end
  189.         end
  190.     end
  191. end
  192.  
  193. function placeBlock( x,y,name )
  194.     x = x - scrollX
  195.     y = y - scrollY
  196.     world[#world+1] = {
  197.                         ["x"] = x,
  198.                         ["y"] = y,
  199.                         ["name"] = name,
  200.                     }
  201. end
  202.  
  203. function drawWorld()
  204.     for i=1,#world do
  205.         local blockInfo = world[i]
  206.         --if blocksAbove(blockInfo["x"],blockInfo["y"]) ~= 5 then
  207.             if (blockInfo["x"] + scrollX < 1 or blockInfo["x"] < x) or (blockInfo["y"] + scrollY < 1 or blockInfo["y"] + scrollY < y or blockInfo["y"] + scrollY == y) then
  208.                 if getBlockInfo( blockInfo["x"] + scrollX, blockInfo["y"] -1 + scrollY ) then
  209.                     if blockInfo["name"] == "Grass" then
  210.                         blockInfo["name"] = "Dirt"
  211.                     end
  212.                 end
  213.                 drawPixel(blockInfo["x"]+scrollX,blockInfo["y"]+scrollY,blockInfo["name"])
  214.             elseif blockInfo["x"] + scrollX == x and blockInfo["y"] + scrollY == y then
  215.                 drawPixel(blockInfo["x"]+scrollX,blockInfo["y"]+scrollY,blockInfo["name"])
  216.             end
  217.         --[[else
  218.             drawPixel(blockInfo["x"],blockInfo["y"], "Darkness")
  219.         end]]--
  220.     end
  221.        
  222.     if debug1 then
  223.         term.setBackgroundColour( colors.white )
  224.         term.setTextColour( colors.gray )
  225.         term.setCursorPos(1,1)
  226.         write("               ")
  227.         term.setCursorPos(1,1)
  228.         write("Debugging...")
  229.         term.setCursorPos(1,2)
  230.         write("               ")
  231.         term.setCursorPos(1,2)
  232.         if getBlockInfo( debugX, debugY ) then
  233.             term.setTextColour( colors.red )
  234.         end
  235.         write("MouseX: " ..debugX)
  236.         term.setCursorPos(1,3)
  237.         write("               ")
  238.         term.setCursorPos(1,3)
  239.         write("MouseY: " ..debugY)
  240.         term.setTextColour( colors.gray )
  241.         term.setCursorPos(1,4)
  242.         write("               ")
  243.         term.setCursorPos(1,4)
  244.         write("CharX: " ..charX)
  245.         term.setCursorPos(1,5)
  246.         write("               ")
  247.         term.setCursorPos(1,5)
  248.         write("CharY: " ..charY)
  249.         term.setBackgroundColour( colors.black )
  250.         term.setTextColour( colors.white )
  251.     end
  252. end
  253.  
  254. function registerMob()
  255. end
  256.  
  257. function generateWorld( type_ )
  258.     if type_ == "flatland" then
  259.         generateTree(math.random(5,12), 4)
  260.         for i=1,x do
  261.             generateChunk(type_, i)
  262.         end
  263.     elseif type_ == "desert" then
  264.         for i=1,x do
  265.             generateChunk(type_, i)
  266.         end
  267.     end
  268.    
  269.     local biomeTransition = math.random(1,30)
  270.     if biomeTransition == 30 then
  271.         biome = math.random(1,#biomes)
  272.     end
  273. end
  274.  
  275. function generateChunk( type_, i )
  276.     if type_ == "flatland" then
  277.         placeBlock(i,5, "Grass")
  278.         placeBlock(i,6, "Grass")
  279.         placeBlock(i,7, "Dirt")
  280.         blockType = math.random(1,2)
  281.         if blockType == 1 then
  282.             placeBlock(i,8, "Dirt")
  283.         else
  284.             placeBlock(i,8, "Stone")
  285.         end
  286.            
  287.         for v=9,y do
  288.             placeBlock(i,v, "Stone")
  289.         end
  290.            
  291.         chunks[i] = type_
  292.     elseif type_ == "desert" then
  293.         if hillyGen == 1 then
  294.             placeBlock(i,4, "Sand")
  295.             placeBlock(i,5, "Sand")
  296.             local hillyGone = math.random(1,8)
  297.             if hillyGone == 1 then
  298.                 hillyGen = 2
  299.             end
  300.         else
  301.             hillyGen = math.random(1,9)
  302.         end
  303.         placeBlock(i,5, "Sand")
  304.         placeBlock(i,6, "Sand")
  305.         placeBlock(i,7, "Sand")
  306.         blockType = math.random(1,2)
  307.         if blockType == 1 then
  308.             placeBlock(i,8, "Sand")
  309.         else
  310.             placeBlock(i,8, "Stone")
  311.         end
  312.            
  313.         for v=9,y do
  314.             placeBlock(i,v, "Stone")
  315.         end
  316.            
  317.         chunks[i] = type_
  318.     end
  319. end
  320.  
  321. function generateTree( posX, posY )
  322.     placeBlock( posX, posY, "Wood" )
  323.     placeBlock( posX, posY-1, "Wood" )
  324.     placeBlock( posX, posY-2, "Sapling" )
  325.     placeBlock( posX, posY-3, "Sapling" )
  326.     placeBlock( posX-1, posY-2, "Sapling" )
  327.     placeBlock( posX+1, posY-2, "Sapling" )
  328. end
  329.  
  330. function drawGUI()
  331.     for i=1,8 do
  332.         drawPixel( i, y , colors.white )
  333.     end
  334.     for i=1,20 do
  335.         drawPixel( i, y-1 , colors.black )
  336.     end
  337.     drawPixel( 9, y , colors.black )
  338.     drawPixel( 20, y , colors.black )
  339.     for i=1,health do
  340.         drawPixel( 9+i, y, colors.red )
  341.     end
  342.     term.setCursorPos(10, y-1)
  343.     write("Health")
  344.     term.setCursorPos(1, y-2)
  345.     if inventory[selectedSlot] then
  346.         write( inventory[selectedSlot]["name"].. "(" ..inventory[selectedSlot]["quantity"].. ")" )
  347.     end
  348.     for i=1,8 do
  349.         if inventory[i] then
  350.             drawPixel( i, y, getColorByName( inventory[i]["name"] ) )
  351.             term.setCursorPos(i,y-1)
  352.             write( inventory[i]["quantity"] )
  353.         end
  354.     end
  355.     drawPixel( selectedSlot, y-1, colors.lime )
  356. end
  357.    
  358. function blocksAbove( x123, y123 )
  359.     local abuv = 0
  360.     for i=1,5 do
  361.         if getBlockInfo( x123, y123-i ) then
  362.             abuv = abuv +1
  363.         end
  364.     end
  365.     return abuv
  366. end
  367.  
  368. function blockBelow( x123, y123 )
  369.     if getBlockInfo( x123, y123+1 ) then
  370.         return true
  371.     end
  372.     return false
  373. end
  374.  
  375. fs.makeDir(".minecraft")
  376. fs.makeDir(".minecraft/mods")
  377. fs.makeDir(".minecraft/saves")
  378. fs.makeDir(".minecraft/screenshots")
  379.  
  380. registerBlock( 1, "Stone", colors.gray, 3)
  381. registerBlock( 2, "Dirt", colors.brown, 1)
  382. registerBlock( 3, "Grass", colors.green, 1)
  383. registerBlock( 4, "IronOre", colors.orange, 1)
  384. registerBlock( 5, "GoldOre", colors.yellow, 1)
  385. registerBlock( 6, "CoalOre", colors.black, 1)
  386. registerBlock( 7, "Diamond", colors.blue, 1)
  387. registerBlock( 8, "Darkness", colors.black, -1)
  388. registerBlock( 9, "Wood", colors.brown, 1)
  389. registerBlock( 10, "Sapling", colors.lime, 1)
  390. registerBlock( 11, "Planks", colors.orange, 1)
  391. registerBlock( 12, "Sand", colors.yellow, 1)
  392. registerBlock( 13, "Gravel", colors.lightGray, 1)
  393.  
  394. biome = biomes[ math.random(1,#biomes) ]
  395. --[[print("generating world...")
  396. print("biome: " ..biome)
  397. sleep(1)]]--
  398. generateWorld( biome ) --Generate spawn Biome
  399.  
  400. term.setBackgroundColour( colors.lightBlue )
  401. term.clear()
  402. redrawWorld()
  403. term.setTextColour( colors.gray )
  404. term.setCursorPos(1,1)
  405. write("Mineception v0.01")
  406. term.setCursorPos(1,3)
  407. cPrint("Mineception")
  408. term.setCursorPos(1,5)
  409. term.setBackgroundColour( colors.lightGray )
  410. term.setTextColour( colors.white )
  411. cPrint("      Singleplayer      ")
  412. term.setCursorPos(1,7)
  413. term.setTextColour( colors.gray )
  414. term.setBackgroundColour( colors.black )
  415. cPrint("      Multi-player      ")
  416.  
  417. while true do
  418.     local _, lor, mX, mY = os.pullEvent("mouse_click")
  419.     if lor == 1 then
  420.         if mY == 5 then
  421.             if mX < 37 or mX > 14 then
  422.                 break
  423.             end
  424.         end
  425.     end
  426. end
  427.  
  428. term.setBackgroundColour( colors.white )
  429. term.setTextColour( colors.white )
  430.  
  431. redrawWorld()
  432.  
  433. while true do
  434.     local event, key, posX, posY = os.pullEvent()
  435.     oldslot = selectedSlot
  436.     if event == "key" then
  437.         if key == keys.left then
  438.             if charX ~= 3 then
  439.                 if getBlockInfo( charX -1, charY ) or getBlockInfo( charX -1, charY-1 ) then
  440.                     if getBlockInfo( charX -1, charY-1) and getBlockInfo( charX -1, charY-2) and getBlockInfo( charX -1, charY) then
  441.                     else
  442.                         drawPixel(charX, charY, colors.lightBlue)
  443.                         drawPixel(charX, charY-1, colors.lightBlue)
  444.                         charX = charX -1
  445.                         charY = charY -1
  446.                     end
  447.                 else
  448.                     local x11, y11, name = getBlockInfo( charX, charY-1)
  449.                     local _x11, _y11, _name = getBlockInfo( charX, charY)
  450.                     if name then
  451.                         drawPixel(charX, charY-1, name)
  452.                     else
  453.                         drawPixel(charX, charY-1, colors.lightBlue)
  454.                     end
  455.                     if _name then
  456.                         drawPixel(charX, charY, _name)
  457.                     else
  458.                         drawPixel(charX, charY, colors.lightBlue)
  459.                     end
  460.                     charX = charX -1
  461.                 end
  462.             else
  463.                 biomeTransition = math.random(1,15)
  464.                 if biomeTransition == 1 then
  465.                     biome = biomes[ math.random(1,#biomes) ]
  466.                 end
  467.                 scrollX = scrollX +1
  468.                 if chunks[charX-2] then
  469.                     generateChunk( biome, charX-2 )
  470.                 end
  471.                
  472.                 redrawWorld()
  473.                
  474.             end
  475.         elseif key == keys.right then
  476.             if charX < x -3 then
  477.                 if getBlockInfo( charX +1, charY ) or getBlockInfo( charX +1, charY-1 ) then
  478.                     if getBlockInfo( charX +1, charY-1) and getBlockInfo( charX +1, charY-2) and getBlockInfo( charX +1, charY) then
  479.                     else
  480.                         drawPixel(charX, charY, colors.lightBlue)
  481.                         drawPixel(charX, charY-1, colors.lightBlue)
  482.                         charX = charX +1
  483.                         charY = charY -1
  484.                     end
  485.                 else
  486.                     local x11, y11, name = getBlockInfo( charX, charY-1)
  487.                     local _x11, _y11, _name = getBlockInfo( charX, charY)
  488.                     if name then
  489.                         drawPixel(charX, charY-1, name)
  490.                     else
  491.                         drawPixel(charX, charY-1, colors.lightBlue)
  492.                     end
  493.                     if _name then
  494.                         drawPixel(charX, charY, _name)
  495.                     else
  496.                         drawPixel(charX, charY, colors.lightBlue)
  497.                     end
  498.                     charX = charX +1
  499.                 end
  500.             else
  501.                 biomeTransition = math.random(1,15)
  502.                 if biomeTransition == 1 then
  503.                     biome = biomes[ math.random(1,#biomes) ]
  504.                 end
  505.                 scrollX = scrollX -1
  506.                 if chunks[charX+2] then
  507.                     generateChunk( biome, charX+2 )
  508.                 end
  509.                
  510.                 redrawWorld()
  511.                
  512.             end
  513.         elseif key == 2 then
  514.             selectedSlot = 1
  515.         elseif key == 3 then
  516.             selectedSlot = 2
  517.         elseif key == 4 then
  518.             selectedSlot = 3
  519.         elseif key == 5 then
  520.             selectedSlot = 4
  521.         elseif key == 6 then
  522.             selectedSlot = 5
  523.         elseif key == 7 then
  524.             selectedSlot = 6
  525.         elseif key == 8 then
  526.             selectedSlot = 7
  527.         elseif key == 9 then
  528.             selectedSlot = 8
  529.         elseif key == 61 then
  530.             if debug1 then debug1 = false else debug1 = true end
  531.             clear()
  532.             drawWorld()
  533.         end
  534.     elseif event == "mouse_click" then
  535.         debugX = posX
  536.         debugY = posY
  537.         if key == 1 then
  538.             local _, _, nameyy = getBlockInfo( posX, posY )
  539.             if getHardnessByName( nameyy ) ~= -1 then
  540.                 breakBlock( posX, posY )
  541.                 drawPixel( posX, posY, colors.lightBlue )
  542.             end
  543.         elseif key == 2 then
  544.             if inventory[selectedSlot] then
  545.                 if posY == charY and posX == charX then
  546.                     if not getBlockInfo(charX,charY -2) then
  547.                         placeBlock( charX, charY, inventory[selectedSlot]["name"] )
  548.                         inventory[selectedSlot]["quantity"] = inventory[selectedSlot]["quantity"] -1
  549.                         drawPixel(charX,charY,inventory[selectedSlot]["name"])
  550.                         if inventory[selectedSlot]["quantity"] == 0 then
  551.                             inventory[selectedSlot] = nil
  552.                         end
  553.                        
  554.                         drawPixel(charX,charY-1,colors.lightBlue)
  555.                         charY = charY -1
  556.                     end
  557.                 elseif not getBlockInfo( posX, posY ) then
  558.                     if posY == charY+1 and posX == charX then else
  559.                         placeBlock( posX, posY, inventory[selectedSlot]["name"] )
  560.                         inventory[selectedSlot]["quantity"] = inventory[selectedSlot]["quantity"] -1
  561.                         drawPixel( posX, posY, inventory[selectedSlot]["name"] )
  562.                         if inventory[selectedSlot]["quantity"] == 0 then
  563.                             inventory[selectedSlot] = nil
  564.                         end
  565.                     end
  566.                 end
  567.             end
  568.         end
  569.     elseif event == "timer" then
  570.         if key == worldTransition then
  571.             worldTransition = nil
  572.         elseif key == gravity then
  573.             local x11, y11, name = getBlockInfo( charX, charY-1)
  574.             local _x11, _y11, _name = getBlockInfo( charX, charY)
  575.             if name then
  576.                 drawPixel(charX, charY-1, name)
  577.             else
  578.                 drawPixel(charX, charY-1, colors.lightBlue)
  579.             end
  580.             if _name then
  581.                 drawPixel(charX, charY, _name)
  582.             else
  583.                 drawPixel(charX, charY, colors.lightBlue)
  584.             end
  585.             charY = charY +1
  586.             gravity = nil
  587.         end
  588.     end
  589.        
  590.     if oldslot ~= selectedSlot then
  591.         clear()
  592.         drawWorld()
  593.     end
  594.    
  595.     if blockBelow( charX, charY ) ~= true and gravity == nil then
  596.         gravity = os.startTimer(0)
  597.     end
  598.    
  599.     drawCharacter()
  600.     drawGUI()
  601. end
Advertisement
Add Comment
Please, Sign In to add comment