Advertisement
LDDestroier

BN CC game test

Nov 18th, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.37 KB | None | 0 0
  1. --[[
  2.  megaman battle network game test for ComputerCraft
  3.  pastebin get aFuFVA2R bntest
  4.  
  5. to do:
  6.     +get the game working
  7.     +add battle chips
  8.     +add independently moving/acting enemies
  9.     +make it play megaman battle network NBS music
  10. --]]
  11.  
  12. --BEGIN PUE API
  13.  
  14. local round = function(x) return x + 0.5 - (x + 0.5) % 1 end local deepCopy = function(tbl) return {table.unpack(tbl)} end local bcol, ccol = { [0]=" ",[colors.white]="0",[colors.orange]="1",[colors.magenta]="2",[colors.lightBlue]="3",[colors.yellow]="4",[colors.lime]="5",[colors.pink]="6",[colors.gray]="7",[colors.lightGray]="8",[colors.cyan]="9",[colors.purple]="a",[colors.blue]="b",[colors.brown]="c",[colors.green]="d",[colors.red]="e",[colors.black]="f"}, {} for k,v in pairs(bcol) do ccol[v] = k end paintutils.flipY = function(image) local output = {} for y = #image, 1, -1 do output[#output+1] = image[y] end return output end paintutils.flipX = function(image) local output,sizeX = {}, 0 for y = 1, #image do sizeX = math.max(sizeX,#image[y]) end for y = 1, #image do output[y] = {} for x = 1, sizeX do output[y][x] = deepCopy(image)[y][sizeX-(x-1)] or 0 end end return output end paintutils.grayOut = function(image) local output,grays = {},{[0] = 0,[colors.white]=colors.white,[colors.orange]=colors.lightGray,[colors.magenta]=colors.lightGray,[colors.lightBlue]=colors.lightGray,[colors.yellow]=colors.white,[colors.lime]=colors.lightGray,[colors.pink]=colors.lightGray,[colors.gray]=colors.gray,[colors.lightGray]=colors.lightGray,[colors.cyan]=colors.lightGray,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.gray,[colors.green]=colors.lightGray,[colors.red]=colors.gray,[colors.black]=colors.black} for y = 1, #image do output[y] = {} for x = 1, #image[y] do output[y][x] = grays[image[y][x]] or image[y][x] end end return output end paintutils.lighten = function(image) local output,lighters = {},{[0] = 0,[colors.white] = colors.white,[colors.orange] = colors.yellow,[colors.magenta] = colors.pink,[colors.lightBlue] = colors.white,[colors.yellow] = colors.white,[colors.lime] = colors.white,[colors.pink] = colors.white,[colors.gray] = colors.lightGray,[colors.lightGray] = colors.white,[colors.cyan] = colors.lightBlue,[colors.purple] = colors.magenta,[colors.blue] = colors.lightBlue,[colors.brown] = colors.orange,[colors.green] = colors.lime,[colors.red] = colors.magenta,[colors.black] = colors.gray} for y = 1, #image do output[y] = {} for x = 1, #image[y] do output[y][x] = lighters[image[y][x]] or image[y][x] end end return output end paintutils.darken = function(image) local output, darkers = {},{[0]=0,[colors.white]=colors.lightGray,[colors.orange]=colors.brown,[colors.magenta]=colors.purple,[colors.lightBlue]=colors.cyan,[colors.yellow]=colors.orange,[colors.lime]=colors.green,[colors.pink]=colors.magenta,[colors.gray]=colors.black,[colors.lightGray]=colors.gray,[colors.cyan]=colors.blue,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.black,[colors.green]=colors.gray,[colors.red]=colors.gray,[colors.black]=colors.black} for y = 1, #image do output[y] = {} for x = 1, #image[y] do output[y][x] = darkers[image[y][x]] or image[y][x] end end return output end paintutils.getSize = function(image) local xsize = 0 if type(image) ~= "table" then return 0,0 end for y = 1, #image do xsize = math.max(xsize,#image[y]) end return xsize, #image end paintutils.stretchImage = function(_image,sx,sy) local image,output = deepCopy(_image), {} if sx == 0 or sy == 0 then return {{}} end if sx < 0 then image = paintutils.flipX(image) end if sy < 0 then image = paintutils.flipY(image) end sx,sy = round(math.abs(sx)), round(math.abs(sy)) if sx == 0 or sy == 0 then return {{}} end local imX,imY = paintutils.getSize(image) local xcur,ycur for y = 1, sy do output[y] = {} for x = 1, sx do output[y][x] = image[math.max(1,round((y/sy)*imY))][math.max(1,round((x/sx)*imX))] or nil end end return output end paintutils.drawImageBlit = function(image,ix,iy) if (type(image) ~= "table") or (type(ix) ~= "number") or (type(iy) ~= "number") then error("Expected image, x, y", 2) else ix, iy = round(ix), round(iy) local buff for y = 1, #image do buff = "" for x = 1, #image[y] do buff = buff..bcol[image[y][x]] end term.setCursorPos(ix,iy+(y-1)) term.blit(buff,buff,buff) end end end paintutils.centerWithBlankSpace = function(_image,ix,iy,object) local image,scX,scY = deepCopy(_image) if object then scX,scY = object.getSize() else scX,scY = term.getSize() end local imgXsize,imgYsize = paintutils.getSize(image) if imgXsize == 0 or imgYsize == 0 then return {{}} end local incX,incY = math.floor((ix or (scX/2)) - (imgXsize/2)), math.floor((iy or (scY/2)) - (imgYsize/2)) local output = {} for y = 1, imgYsize + incY do output[y] = {} for x = 1, imgXsize + incX do if (x > incX) and (y > incY) then output[y][x] = image[y-incY][x-incX] or 0 else output[y][x] = 0 end end end return output end paintutils.drawImageBlitCenter = function(image,ix,iy,object) local scX,scY if object then scX,scY = object.getSize() else scX,scY = term.getSize() end local imgXsize,imgYsize = paintutils.getSize(image) return paintutils.drawImageBlit(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2)) end  paintutils.drawImageCenter = function(image,ix,iy,object) local scX,scY if object then scX,scY = object.getSize() else scX,scY = term.getSize() end local imgXsize,imgYsize = paintutils.getSize(image) return paintutils.drawImage(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2)) end paintutils.merge = function(...) local output,arg,imgXsize,imgYsize,image,xdiff,ydiff = {}, table.pack(...), 0, 0 for a = 1, #arg do local x,y = paintutils.getSize(arg[a][1]) if not (x == 0 or y == 0) then x, y = x+arg[a][2], y+arg[a][3] imgXsize = math.max(imgXsize,x) imgYsize = math.max(imgYsize,y) end end for a = #arg, 1, -1 do image,xdiff,ydiff = table.unpack(arg[a]) xdiff, ydiff = xdiff - 1, ydiff - 1 for y = 1, imgYsize do output[y] = output[y] or {} for x = 1, imgXsize do output[y][x] = output[y][x] or 0 if image[y-ydiff] then if image[y-ydiff][x-xdiff] and (image[y-ydiff][x-xdiff] ~= 0) then output[y][x] = image[y-ydiff][x-xdiff] end end end end end return output end paintutils.fullScreen = function(bgcol,object) local scr_x,scr_y = (object or term).getSize() local output,l = {},{} bgcol = bgcol or colors.black for x = 1, scr_x do l[x] = bgcol end for y = 1, scr_y do output[y] = l end return output end paintutils.unloadImage = function(image) local output = "" for y = 1, #image do for x = 1, #image[y] do output = output..bcol[image[y][x]] end if y < #image then output = output.."\n" end end return output end paintutils.autocrop = function(image) local output,top,leftings = {},1,{} local isTopped = false for a = 1, #image do if (not isTopped) and (#image[a] > 0) then top = a isTopped = true end if isTopped then output[#output+1] = deepCopy(image[a]) end end for y = 1, #output do leftings[y] = 0 for x = 1, #output[y] do if (output[y][x] == 0) or (output[y][x] == nil) then leftings[y] = leftings[y] + 1 else break end end end local left = math.min(table.unpack(leftings)) for y = 1, #output do for l = 1, left do table.remove(output[y],1) end end return output end
  15.  
  16. --END PUE API
  17.  
  18. local stage = {}
  19. for y = 1, 3 do
  20.     stage[y] = {}
  21.     for x = 1, 6 do
  22.         stage[y][x] = {
  23.             exists = true,
  24.             cracked = false,
  25.             state = "normal",
  26.             damage = 0,
  27.             owner = 1,
  28.         }
  29.     end
  30. end
  31.  
  32. panels = {
  33.     [0] = { --blue
  34.         ["normal"] = {{2048,256,256,256},{2048,256,256,256},{2048,2048,2048,2048}},
  35.         ["cracked"] = {{2048,0,128,0},{2048,128,0,128},{2048,2048,2048,2048}},
  36.         ["broken"] = {{2048,0,0,0},{2048,0,0,0},{2048,2048,2048,2048}},
  37.     },
  38.     [1] = { --red
  39.         ["normal"] = {{256,256,256,16384},{256,256,256,16384},{16384,16384,16384,16384}},
  40.         ["cracked"] = {{0,128,0,16384},{128,0,128,16384},{16384,16384,16384,16384}},
  41.         ["broken"] = {{0,0,0,16384},{0,0,0,16384},{16384,16384,16384,16384}},
  42.     },
  43. }
  44.  
  45. local bgobject = {{16,16,16,16,16,16,16,16},{16,1,32768,32768,32768,32768,32768,16},{16,32768,32768,32768,32768,32768,32768,16},{16,32768,32768,32768,32768,32768,32768,16},{16,32768,32768,32768,32768,32768,32768,16},{16,16,16,16,16,16,16384,16},{16,16,16,16,16,16,16,16}}
  46.  
  47. local backgroundObjects = {}
  48.  
  49. local alternatePosition = false
  50. local addBGObject = function()
  51.     local scr_x,scr_y = term.getSize()
  52.     if alternatePosition then
  53.         backgroundObjects[#backgroundObjects+1] = {x=scr_x+4,y=scr_y-4}
  54.     else
  55.         backgroundObjects[#backgroundObjects+1] = {x=scr_x-4,y=scr_y+4}
  56.     end
  57.     alternatePosition = not alternatePosition
  58. end
  59.  
  60. local backgroundSpin = 1
  61.  
  62. moveBGobjects = function()
  63.     for a = 1, #backgroundObjects do
  64.         backgroundObjects[a].x = backgroundObjects[a].x - 1
  65.         backgroundObjects[a].y = backgroundObjects[a].y - 1
  66.     end
  67.     backgroundSpin = (backgroundSpin + 10) % 360
  68. end
  69.  
  70. makeObjectBackground = function()
  71.     local mergeInto = {}
  72.     for a = 1, #backgroundObjects do
  73.         mergeInto[#mergeInto] = {paintutils.stretchImage(bgobject,math.sin(math.rad(backgroundSpin or 90))*8,8),backgroundObjects[a].x,backgroundObjects[a].y}
  74.     end
  75.     return paintutils.merge(table.unpack(mergeInto),{paintutils.fullScreen(colors.gray),1,1})
  76. end
  77.  
  78. local playerX, playerY = 1, 2 --relative to square grid
  79. local playerHP = 100
  80. local maxPlayerHP = 100
  81. local playerSprite = "stand"
  82. player = {
  83.     ["stand"] = {{0,2048,2048,0},{2048,16,16,0},{0,512,512,0},{512,512,0,512},{0,2048,0,0},{512,0,512,0},{2048,0,0,2048}},
  84.     ["hurt"] = {{0,2048,2048,2048},{0,2048,16,16},{512,512,512,512},{0,512,512,0},{0,2048,2048,0},{512,0,0,512},{2048,0,0,2048}},
  85.     ["shoot"] = {{0,2048,2048,0},{2048,16,16,0},{0,512,512,2048},{512,512,0,0},{0,2048,0,0},{512,0,512,0},{2048,0,0,2048}},
  86. }
  87. hpbar = {
  88.     ["high"] = {{256,256,256,256,256,256,256,256,2048,2048,2048},{256,256,256,256,256,256,256,256,2048,2048,16},{256,256,256,256,256,256,256,256,2048,16,16}},
  89.     ["low"] = {{16384,16384,16384,16384,16384,16384,16384,16384,512,512,512},{16384,16384,16384,16384,16384,16384,16384,16384,512,512,256},{16384,16384,16384,16384,16384,16384,16384,16384,512,256,256}}
  90. }
  91.  
  92. palate = {
  93.     panel = {
  94.         ownerTrim = {
  95.             [0] = colors.blue,
  96.             [1] = colors.red,
  97.         },
  98.         normal = colors.lightGray,
  99.         poison = colors.purple,
  100.         fire = colors.red,
  101.         ice = colors.cyan,
  102.         sand = colors.yellow,
  103.         metal = colors.gray,
  104.     },
  105. }
  106.  
  107. local discolorSquare = function(square,condition)
  108.     local output = {}
  109.     for y = 1, #square do
  110.         output[y] = {}
  111.         for x = 1, #square[y] do
  112.             if square[y][x] ~= palate.panel.ownerTrim[0] and square[y][x] ~= palate.panel.ownerTrim[1] and square[y][x] ~= 0 then
  113.                 output[y][x] = palate.panel[condition]
  114.             else
  115.                 output[y][x] = square[y][x]
  116.             end
  117.         end
  118.     end
  119.     return output
  120. end
  121.  
  122. render = function()
  123.     local scene, square, panel, owner
  124.     local scrollY = 10
  125.     local mergeInto = {}
  126.     local hasDecentHP = playerHP > (maxPlayerHP / 5)
  127.     mergeInto[#mergeInto+1] = {hasDecentHP and hpbar.high or hpbar.low,1,1}
  128.     mergeInto[#mergeInto+1] = {player[playerSprite],((playerX-1)*4)+2, ((playerY-1)*3)-5+scrollY}
  129.     for y = 1, #stage do
  130.         for x = 1, #stage[y] do
  131.             square = stage[y][x]
  132.             owner = stage[y][x].owner
  133.             if square.exists then
  134.                 if square.cracked then
  135.                     panel = panels[owner].cracked
  136.                 else
  137.                     panel = panels[owner].normal
  138.                 end
  139.             else
  140.                 panel = panels[owner].broken
  141.             end
  142.             mergeInto[#mergeInto+1] = {panel, ((x-1)*4)+2, ((y-1)*3)+scrollY}
  143.         end
  144.     end
  145.     --mergeInto[#mergeInto+1] = {paintutils.fullScreen(colors.black),1,1}
  146.     mergeInto[#mergeInto+1] = {makeObjectBackground(),1,1}
  147.     scene = paintutils.merge(table.unpack(mergeInto))
  148.     paintutils.drawImageBlit(scene,1,1)
  149.     term.setCursorPos(2,2)
  150.     if hasDecentHP then
  151.         term.setTextColor(colors.black)
  152.         term.setBackgroundColor(colors.lightGray)
  153.     else
  154.         term.setTextColor(colors.white)
  155.         term.setBackgroundColor(colors.red)
  156.     end
  157.     term.write(playerHP)
  158. end
  159.  
  160. for y = 1, #stage do
  161.     for x = 1, #stage[y] do
  162.         if x <= #stage[y]/2 then
  163.             stage[y][x].owner = 1
  164.         else
  165.             stage[y][x].owner = 0
  166.         end
  167.     end
  168. end
  169.  
  170. local timers = {}
  171.  
  172. local tickTimer = function()
  173.     for k,v in pairs(timers) do
  174.         if v > 0 then
  175.             timers[k] = v-1
  176.             if timers[k] == 0 then
  177.                 os.queueEvent(k)
  178.             end
  179.         end
  180.     end
  181. end
  182.  
  183. local startTimer = function(name, time)
  184.     timers[name or "timer"] = time or 0
  185. end
  186. local canShoot = true
  187.  
  188. startTimer("moveBackground",2)
  189. startTimer("makeNewBGObject",32)
  190. local doIt = function()
  191.     local cpx,cpy
  192.     while true do
  193.         render()
  194.         local evt = {os.pullEvent()}
  195.         if evt[1] == "key" then
  196.             local key = evt[2]
  197.             cpx, cpy = playerX, playerY
  198.             if key == keys.up then playerY = math.max(playerY-1,1)
  199.             elseif key == keys.down then playerY = math.min(playerY+1,3)
  200.             elseif key == keys.left then playerX = math.max(playerX-1,1)
  201.             elseif key == keys.right then playerX = math.min(playerX+1,6) end
  202.             if stage[playerY][playerX].owner == 0 then
  203.                 playerX, playerY = cpx, cpy
  204.             end
  205.             if key == keys.q then
  206.                 return sleep(0)
  207.             elseif key == keys.x then
  208.                 if canShoot then
  209.                     playerSprite = "shoot"
  210.                     startTimer("canShootNow",5)
  211.                     startTimer("backToStand",3)
  212.                     canShoot = false
  213.                 end
  214.             end
  215.         elseif evt[1] == "backToStand" then
  216.             playerSprite = "stand"
  217.         elseif evt[1] == "moveBackground" then
  218.             moveBGobjects()
  219.             startTimer("moveBackground",2)
  220.         elseif evt[1] == "makeNewBGObject" then
  221.             addBGObject()
  222.             startTimer("makeNewBGObject",32)
  223.         elseif evt[1] == "canShootNow" then
  224.             canShoot = true
  225.         end
  226.     end
  227. end
  228.  
  229. runTheTimer = function()
  230.     while true do
  231.         sleep(0.05)
  232.         tickTimer()
  233.     end
  234. end
  235.  
  236. parallel.waitForAny(doIt,runTheTimer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement