Advertisement
Guest User

game

a guest
Aug 22nd, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.44 KB | None | 0 0
  1. --BY WIGGLE1000! PLEASE DO NOT MODIFY OR REDISTRIBUTE UNDER YOUR NAME!
  2.  
  3. PX = 30
  4. PY = 1
  5. P2X = 5
  6. P2Y = 1
  7. invX = 10
  8. invY = 4
  9. invSlotsH=6
  10. invSlotsV=3
  11. jumptimer=0
  12. gravityOn=true
  13. hotbarIndex=1
  14. mapWidth = 100
  15. mapHeight = 50
  16. screenx,screeny = term.getSize()
  17. camWidth = screenx-3
  18. camHeight = screeny-7
  19. camX=0
  20. camY=0
  21. editingBG=false
  22. map = {}
  23. mapBG = {}
  24. --[[ old map
  25. map[1] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  26. map[2] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  27. map[3] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  28. map[4] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  29. map[5] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  30. map[6] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  31. map[7] = {1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,1,1,1,1,1,1,1,1}
  32. map[8] = {3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,3,3,3,3,3,3,3,3}
  33. map[9] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
  34. map[10]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
  35. map[11]= {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}
  36. ]]--
  37.  
  38. sandboxEnv={moveBlocks,setBlock,math.random,math.sin,math.round}
  39.  
  40. blocks = {}--NAME, COLLIDES?, CHAR, FGC, BGC
  41. blocks[1] = {"air",false, " ", colors.lightBlue, colors.lightBlue}
  42. blocks[2] = {"dirt",true, " ", colors.brown, colors.brown}
  43. blocks[3] = {"grass",true, "~", colors.lime, colors.brown}
  44. blocks[4] = {"stone",true, "_", colors.lightGray, colors.gray}
  45. --5 is cloud
  46. blocks[6] = {"log",true, "^", colors.black, colors.brown}
  47. blocks[7] = {"leaves",true, "*", colors.green, colors.lime}
  48. blocks[8] = {"planks",true, "=", colors.brown, colors.orange}
  49. blocks[9] = {"coal",true, "*", colors.black, colors.gray}
  50. blocks[10] = {"iron",true, "*", colors.orange, colors.gray}
  51.  
  52.  
  53. --{ID,PERCENT}
  54. ores={{9,50},{10,10}}
  55.  
  56. mouseX=0
  57. mouseY=0
  58.  
  59. invOpen=false
  60.  
  61. inventory = {}
  62. inventory[1]={1,2,3,4,5,6}
  63. inventory[2]={7,8,9,10,11,12}
  64. inventory[3]={13,14,15,16,17,18}
  65.  
  66. hotbarItems = {}
  67. hotbarItems[1]=2
  68. hotbarItems[2]=3
  69. hotbarItems[3]=4
  70. hotbarItems[4]=5
  71. hotbarItems[5]=6
  72. hotbarItems[6]=7
  73. hotbarItems[7]=8
  74. oldfenv=getfenv()
  75.  
  76. --utility funcs
  77.  
  78. function split(str, pat)
  79.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  80.    local fpat = "(.-)" .. pat
  81.    local last_end = 1
  82.    local s, e, cap = str:find(fpat, 1)
  83.    while s do
  84.       if s ~= 1 or cap ~= "" then
  85.      table.insert(t,cap)
  86.       end
  87.       last_end = e+1
  88.       s, e, cap = str:find(fpat, last_end)
  89.    end
  90.    if last_end <= #str then
  91.       cap = str:sub(last_end)
  92.       table.insert(t, cap)
  93.    end
  94.    return t
  95. end
  96. --by wiggle1000
  97. function strToCol(color)
  98.     if(color=="white")then
  99.         return colors.white
  100.     end
  101.     if(color=="orange")then
  102.         return colors.orange
  103.     end
  104.     if(color=="magenta")then
  105.         return colors.magenta
  106.     end
  107.     if(color=="lightBlue")then
  108.         return colors.lightBlue
  109.     end
  110.     if(color=="yellow")then
  111.         return colors.yellow
  112.     end
  113.     if(color=="lime")then
  114.         return colors.lime
  115.     end
  116.     if(color=="pink")then
  117.         return colors.pink
  118.     end
  119.     if(color=="gray" or color == "grey")then
  120.         return colors.gray
  121.     end
  122.     if(color=="lightGray" or color == "lightGrey")then
  123.         return colors.lightGray
  124.     end
  125.     if(color=="cyan")then
  126.         return colors.cyan
  127.     end
  128.     if(color=="purple")then
  129.         return colors.purple
  130.     end
  131.     if(color=="blue")then
  132.         return colors.blue
  133.     end
  134.     if(color=="brown")then
  135.         return colors.brown
  136.     end
  137.     if(color=="green")then
  138.         return colors.green
  139.     end
  140.     if(color=="red")then
  141.         return colors.red
  142.     end
  143.     if(color=="black")then
  144.         return colors.black
  145.     end
  146.     return colors.black
  147. end
  148.  
  149. --game funcs
  150. function growTree(baseX,baseY,height,width,leafHeight,leafWidth,rootDepth)
  151.     for x = math.ceil(baseX-width),math.floor(baseX+(width)) do
  152.         for y = baseY-height,baseY+rootDepth do
  153.             if(mapBG[y]~=nil and mapBG[y][x]~=nil)then
  154.                 mapBG[y][x]=6 --log
  155.             end
  156.         end
  157.     end
  158.     for x = math.floor(baseX-math.floor(leafWidth)),math.floor(baseX+(leafWidth)) do
  159.         for y = (baseY-height-leafHeight)-math.floor(leafHeight/2),baseY-height do
  160.             if(map[y]~=nil and map[y][x]~=nil)then
  161.                 map[y][x]=7 --leaves
  162.             end
  163.         end
  164.     end
  165.     for x = math.floor(baseX-math.floor(leafWidth/2)),math.floor(baseX+(leafWidth/2)) do
  166.         for y = (baseY-height-leafHeight)-(leafHeight),(baseY-height-leafHeight)-math.floor(leafHeight/2) do
  167.             if(map[y]~=nil and map[y][x]~=nil)then
  168.                 map[y][x]=7 --leaves
  169.             end
  170.         end
  171.     end
  172. end
  173.  
  174. function spawnRandomOre(dictionary,x,y)
  175.     ore=nil
  176.     for i,arr in pairs(dictionary) do
  177.         if(ore == nil and math.random(0,100)<arr[2])then
  178.             ore=arr[1]
  179.         end
  180.     end
  181.     if(ore~=nil)then
  182.         map[y][x]=ore
  183.         if(map[y]~=nil and map[y][x-1]~=nil)then
  184.             map[y][x-1]=ore
  185.         end
  186.         if(map[y]~=nil and map[y][x+1]~=nil)then
  187.             map[y][x+1]=ore
  188.         end
  189.         if(map[y-1]~=nil and map[y-1][x]~=nil)then
  190.             map[y-1][x]=ore
  191.         end
  192.         if(map[y+1]~=nil and map[y+1][x]~=nil)then
  193.             map[y+1][x]=ore
  194.         end
  195.     end
  196. end
  197.  
  198. function genWorld(minHeight,maxHeight,w,h,top,mid,stone,stoneHeight)
  199.     for y=1,mapHeight do
  200.         map[y]={}
  201.         for x=1,mapWidth do
  202.             map[y][x]=1
  203.             if(y == mapHeight)then
  204.                 map[y][x]=stone
  205.             end
  206.         end
  207.     end
  208.     --BG
  209.     for y=1,mapHeight do
  210.         mapBG[y]={}
  211.         for x=1,mapWidth do
  212.             mapBG[y][x]=1
  213.             if(y == mapHeight)then
  214.                 mapBG[y][x]=stone
  215.             end
  216.         end
  217.     end
  218.     local CX = (mapHeight/2)
  219.     for x=1,mapWidth do
  220.         if(CX>maxHeight)then
  221.             CX = maxHeight+math.random(2)-1
  222.         elseif(CX<minHeight)then
  223.             CX = minHeight+math.random(2)-1
  224.         else
  225.             CX=CX+math.random(3)-2
  226.         end
  227.         for y=1,mapHeight do
  228.             if(y>=CX)then
  229.                 map[y][x]=mid
  230.                 if(y >= stoneHeight)then
  231.                     map[y][x]=stone
  232.                     if(math.random(1,100)<=5)then
  233.                         spawnRandomOre(ores,x,y)
  234.                     end
  235.                 end
  236.                 if(y == CX)then
  237.                     map[y][x]=top
  238.                     if(math.random(1,100)<=6)then
  239.                         if(math.random(1,100)<=20)then
  240.                             growTree(x,y-1,5+math.random(1,3),math.random(1,2),math.random(2,4),math.random(4,7),2)
  241.                         else
  242.                             growTree(x,y-1,2+math.random(1,3),math.random(5,8)/10,math.random(2,3),math.random(2,3),1)
  243.                         end
  244.                     end
  245.                 end
  246.                 if(y == CX+1)then
  247.                     map[y][x]=mid
  248.                 end
  249.             end
  250.         end
  251.     end
  252.    
  253. end
  254.  
  255. function drawGround(xOff, yOff)
  256.  
  257.     for y = 0,camHeight do
  258.         for x =0,camWidth do
  259.             term.setCursorPos((xOff+x),(yOff+y))
  260.             if((map[y+camY]~=nil)and(map[y+camY][x+camX]~=nil)and(mapBG[y+camY]~=nil)and(mapBG[y+camY][x+camX]~=nil))then
  261.                 if(y<=camY+camHeight and  x<=camX+camWidth)then
  262.                     if(map[y+camY][x+camX]==1)then
  263.                         term.setTextColor(tonumber(blocks[mapBG[y+camY][x+camX]][4]))
  264.                         term.setBackgroundColor(tonumber(blocks[mapBG[y+camY][x+camX]][5]))
  265.                         term.write(tostring(blocks[mapBG[y+camY][x+camX]][3]))
  266.                     else
  267.                         term.setTextColor(tonumber(blocks[map[y+camY][x+camX]][4]))
  268.                         term.setBackgroundColor(tonumber(blocks[map[y+camY][x+camX]][5]))
  269.                         term.write(tostring(blocks[map[y+camY][x+camX]][3]))
  270.                     end
  271.                 end
  272.             else
  273.                 term.setBackgroundColor(colors.black)
  274.                 term.write(" ")
  275.             end
  276.         end
  277.     end
  278. end
  279.  
  280. function input(mOffX,mOffY)
  281.     event,p1,p2,p3,p4 = os.pullEvent()
  282.     --W:17  A:30  S:31  D:32  Q:16  E:18  SPACE:57  ENTER:28
  283.     if(event == "key") then
  284.         if(p1==30) then--A
  285.            if (blocks[map[PY][PX-1]][2]==false)then
  286.                 PX = PX - 1
  287.             end
  288.         elseif(p1==32) then--D
  289.            if (blocks[map[PY][PX+1]][2]==false)then
  290.                 PX = PX + 1
  291.             end
  292.         elseif(p1==57) then--SPACE
  293.            if (blocks[map[PY+1][PX]][2]==true and blocks[map[PY-1][PX]][2]==false)then
  294.                 PY = PY - 1
  295.                 gravityOn=false
  296.                 jumptimer = 4
  297.             end
  298.         elseif(p1==28)then--enter
  299.             editingBG=not editingBG
  300.         elseif(p1==18)then--E
  301.             invOpen=not invOpen
  302.         end
  303.     elseif(event=="mouse_click")then
  304.         if(invOpen==false)then --WORLD(inv closed)
  305.             if(editingBG==false)then
  306.                 if(map[p3+camY-mOffY]~=nil and map[p3+camY-mOffY][p2+camX-mOffX]~=nil)then
  307.                     if(p1==1)then
  308.                         map[p3+camY-mOffY][p2+camX-mOffX]=1
  309.                     end
  310.                     if(p1==2)then
  311.                         map[p3+camY-mOffY][p2+camX-mOffX]=hotbarItems[hotbarIndex+1]
  312.                     end
  313.                 end
  314.             else
  315.                 if(mapBG[p3+camY-mOffY][p2+camX-mOffX]~=nil)then
  316.                     if(p1==1)then
  317.                         mapBG[p3+camY-mOffY][p2+camX-mOffX]=1
  318.                     end
  319.                     if(p1==2)then
  320.                         mapBG[p3+camY-mOffY][p2+camX-mOffX]=hotbarItems[hotbarIndex+1]
  321.                     end
  322.                 end
  323.             end
  324.         else  --INVENTORY
  325.             for x = 1,(invSlotsH) do
  326.                 for y = 1,(invSlotsV) do
  327.                     if(inventory[y]~=nil and inventory[y][x]~=nil and blocks[inventory[y][x]]~=nil)then
  328.                         if(p2==(x*3)+invX and p3==(y*3)+invY-2)then
  329.                             hotbarItems[hotbarIndex+1]=inventory[y][x]
  330.                             invOpen=false
  331.                         end
  332.                     end
  333.                 end
  334.             end
  335.         end
  336.     elseif(event=="mouse_scroll")then
  337.         if(p1<0)then
  338.             if(hotbarIndex>0)then
  339.                 hotbarIndex = hotbarIndex - 1
  340.             else
  341.                 hotbarIndex=6
  342.             end
  343.         elseif(p1>0)then
  344.             if(hotbarIndex<6)then
  345.                 hotbarIndex = hotbarIndex + 1
  346.             else
  347.                 hotbarIndex=0
  348.             end
  349.         end
  350.     end
  351.     if(jumptimer==2)then
  352.         if (blocks[map[PY-1][PX]][2]==false)then
  353.             PY = PY - 1
  354.         end
  355.     end
  356.     if(jumptimer<=0)then
  357.         jumptimer=0
  358.         gravityOn=true
  359.     end
  360.    
  361.    
  362.    
  363. end
  364. --by wiggle1000
  365. function drawBuildModeInfo(offX,offY)
  366.     term.setCursorPos(offX,offY)
  367.     term.setTextColor(colors.white)
  368.     term.setBackgroundColor(colors.black)
  369.     if(editingBG==false)then
  370.         term.write("Editing: FG")
  371.     else
  372.         term.write("Editing: BG")
  373.     end
  374. end
  375.  
  376. function drawHotbar(offX,offY)
  377.     for x = offX,offX+(7*3) do
  378.         for y = offY, offY+2 do
  379.             term.setCursorPos(x,y)
  380.             term.setBackgroundColor(colors.gray)
  381.             term.setTextColor(colors.lightGray)
  382.             term.write(" ")
  383.         end
  384.     end
  385.     for x = offX+(hotbarIndex*3),offX+(hotbarIndex*3)+2 do
  386.         for y = offY, offY+2 do
  387.             term.setCursorPos(x,y)
  388.             term.setBackgroundColor(colors.lightGray)
  389.             term.setTextColor(colors.white)
  390.             term.write(" ")
  391.         end
  392.     end
  393.     for x = offX+(hotbarIndex*3),offX+(hotbarIndex*3) do
  394.         if(hotbarItems[hotbarIndex+1]~=nil and blocks[hotbarItems[hotbarIndex+1]]~=nil)then
  395.             term.setCursorPos(x+1,offY+1)
  396.             term.setTextColor(tonumber(blocks[hotbarItems[hotbarIndex+1]][4]))
  397.             term.setBackgroundColor(tonumber(blocks[hotbarItems[hotbarIndex+1]][5]))
  398.             term.write(tostring(blocks[hotbarItems[hotbarIndex+1]][3]))
  399.         end
  400.     end
  401.    
  402.    
  403. end
  404.  
  405. function drawPlayer(offX,offY)
  406.     term.setCursorPos((camWidth/2)+offX,(camHeight/2)+offY)
  407.     term.setBackgroundColor(tonumber(blocks[mapBG[PY][PX]][5]))
  408.     term.setTextColor(playerColor)
  409.     term.write("&")
  410. end
  411.  
  412. function gravity()
  413.     if(gravityOn==true)then
  414.         if (blocks[map[PY+1][PX]][2]==false)then
  415.             PY = PY + 1
  416.         end
  417.      end
  418. end
  419.  
  420. function loadBlocks()
  421.     if(not fs.exists("WC") or not fs.exists("WC/blocks"))then
  422.         if(not not fs.exists("WC"))then
  423.             fs.makeDir("WC")
  424.         end
  425.         print("GETTING EXAMPLE BLOCK... (DIRECTORY: WC/blocks)")
  426.         shell.run("pastebin get VYh3rNRM WC/blocks")
  427.         sleep(1)
  428.         print("GETTING EXAMPLE SCRIPT... (DIRECTORY: WC/cloudscript)")
  429.         shell.run("pastebin get SmH4KCc0 WC/cloudscript")
  430.         sleep(5)
  431.     end
  432.    
  433.     local filehandle = fs.open("WC/blocks","r")
  434.     line1=filehandle.readLine()
  435.     while (line1~=nil)do
  436.         local parts = split(line1,",")
  437.         isSolid=false
  438.         if(tostring(parts[3])=="true")then
  439.             isSolid=true
  440.         end
  441.         fcol = strToCol(tostring(parts[5]))
  442.         bcol = strToCol(tostring(parts[6]))
  443.         blocks[tonumber(parts[1])]={tostring(parts[2]),isSolid,tostring(parts[4]),fcol,bcol,tostring(parts[7])}
  444.         line1=filehandle.readLine()
  445.     end
  446.     filehandle:close()
  447. end
  448.  
  449. function moveBlocks(xoff,yoff,cblock,replaces)
  450.     for xs = 1, mapWidth do
  451.         for ys = 1, mapHeight do
  452.             if(map[ys][xs]==cblock)then
  453.                 map[ys][xs]=1
  454.                 if(replaces)then
  455.                     if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil)then
  456.                         map[ys+yoff][xs+xoff]=cblock
  457.                     end
  458.                 else
  459.                     if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil and map[ys+yoff][xs+xoff]==1)then
  460.                         map[ys+yoff][xs+xoff]=cblock
  461.                     end
  462.                 end
  463.             end
  464.            
  465.             if(mapBG[ys][xs]==cblock)then
  466.                 mapBG[ys][xs]=1
  467.                 if(replaces)then
  468.                     if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil)then
  469.                         mapBG[ys+yoff][xs+xoff]=cblock
  470.                     end
  471.                 else
  472.                     if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil and mapBG[ys+yoff][xs+xoff]==1)then
  473.                         mapBG[ys+yoff][xs+xoff]=cblock
  474.                     end
  475.                 end
  476.             end
  477.         end
  478.     end
  479. end
  480.  
  481. function setBlock(xoff,yoff,cblock,sblock,replaces)
  482.     for xs = 1, mapWidth do
  483.         for ys = 1, mapHeight do
  484.             if(map[ys][xs]==cblock)then
  485.                 map[ys][xs]=1
  486.                 if(replaces)then
  487.                     if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil)then
  488.                         map[ys+yoff][xs+xoff]=sblock
  489.                     end
  490.                 else
  491.                     if(map[ys+yoff]~=nil and map[ys+yoff][xs+xoff]~=nil and map[ys+yoff][xs+xoff]==1)then
  492.                         map[ys+yoff][xs+xoff]=sblock
  493.                     end
  494.                 end
  495.             end
  496.            
  497.             if(mapBG[ys][xs]==cblock)then
  498.                 mapBG[ys][xs]=1
  499.                 if(replaces)then
  500.                     if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil)then
  501.                         mapBG[ys+yoff][xs+xoff]=sblock
  502.                     end
  503.                 else
  504.                     if(mapBG[ys+yoff]~=nil and mapBG[ys+yoff][xs+xoff]~=nil and mapBG[ys+yoff][xs+xoff]==1)then
  505.                         mapBG[ys+yoff][xs+xoff]=sblock
  506.                     end
  507.                 end
  508.             end
  509.         end
  510.     end
  511. end
  512.  
  513. function drawInv(offX,offY,hSlots,vSlots,inv)
  514.     if(invOpen)then
  515.         for x = offX,offX+(hSlots*3)-1 do
  516.             for y = offY, offY+(vSlots*3) do
  517.                 term.setCursorPos(x+2,y)
  518.                 term.setBackgroundColor(colors.gray)
  519.                 term.setTextColor(colors.white)
  520.                 term.write("|")
  521.             end
  522.         end
  523.         for x = 1,(hSlots) do
  524.             for y = 1,(vSlots) do
  525.                 if(inventory[y]~=nil and inventory[y][x]~=nil and blocks[inventory[y][x]]~=nil)then
  526.                     term.setCursorPos((x*3)+offX,(y*3)+offY-2)
  527.                     term.setTextColor(tonumber(blocks[inventory[y][x]][4]))
  528.                     term.setBackgroundColor(tonumber(blocks[inventory[y][x]][5]))
  529.                     term.write(tostring(blocks[inventory[y][x]][3]))
  530.                 end
  531.             end
  532.         end
  533.     end
  534. end
  535.  
  536. function doBlockCode()
  537.     for i,block in pairs(blocks) do
  538.         if(block[6]~=nil)then
  539.             file = block[6]
  540.             setfenv(2,sandboxEnv)
  541.             dofile("WC/"..block[6])
  542.             setfenv(2,oldfenv)
  543.             blockFunc()
  544.         end
  545.     end
  546. end
  547. --by wiggle1000
  548. function tickLoop()
  549.     while true do
  550.         os.startTimer(0.15)
  551.         input(1,4)
  552.         camX=PX-(camWidth/2)
  553.         camY=PY-(camHeight/2)
  554.         gravity()
  555.         term.setCursorPos(1,1)
  556.         doBlockCode()
  557.         if(not invOpen)then
  558.             drawGround(1,4)
  559.             drawPlayer(1,4)
  560.         end
  561.         drawHotbar(1,1)
  562.         drawBuildModeInfo(25,1)
  563.         drawInv(invX,invY,invSlotsH,invSlotsV,inventory)       
  564.         sleep(0.1)
  565.         if(jumptimer>0)then
  566.             jumptimer = jumptimer - 1
  567.         end
  568.     end
  569. end
  570. --by wiggle1000
  571. term.clear()
  572. term.setCursorPos(1,1)
  573. term.write("Enter world seed(WIP):")
  574. math.randomseed = tonumber(read())
  575. term.clear()
  576. term.setCursorPos(1,1)
  577. term.write("Enter player color (EG. red):")
  578. playerColor = strToCol(read())
  579. term.clear()
  580. loadBlocks()
  581. genWorld(10,20,mapWidth,mapHeight,3,2,4,mapHeight/2)
  582. term.clear()
  583. term.setCursorPos(1,1)
  584. tickLoop()
  585. --by wiggle1000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement