Advertisement
guitarplayer616

Crossroads Optimized (runs in game!)

Jun 5th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.62 KB | None | 0 0
  1. local debug = false
  2. local oLog = fs.open("console", "w")
  3. local function log(string)
  4.     if debug then
  5.         local string = string or ""
  6.         oLog.writeLine(string)
  7.         oLog.flush()
  8.     end
  9. end
  10. local tArgs = {...}
  11.  
  12. local type, prevtype = 1, 1
  13. local sChanged = true
  14. local tickToggle = true
  15. local tickNum = 0
  16. local distance = 0
  17. local theme = "default"
  18. local w, h = term.getSize()
  19. local roadString = " " .. string.rep("- ", w/2)
  20. local waterString = " " .. string.rep(" ~", w/2)
  21. local midW, midH = math.floor(w/2), math.floor(h/2)
  22. local timers = {
  23.     ["gametick"] = os.startTimer(.05),
  24.     ["screenscroll"] = os.startTimer(2)
  25. }
  26.  
  27. local pos = {
  28.     ["x"] = midW,
  29.     ["y"] = 5
  30. }
  31.  
  32.  
  33.  
  34. local themes = {
  35.     --["theme name"] = {grass = grass color, tree = {leaves, trunk}, road = road color, dashes = color of road dashes, car = {any colors that cars could be}, water = water color, waves = color of tildes in water, log = log color, player = {color of square, color of "O"}},
  36.     ["default"] = {grass = colors.lime, tree = {colors.green, colors.brown}, road = colors.gray, dashes = colors.yellow, car = {2^0, 2^2, 2^3, 2^4, 2^6, 2^8, 2^9, 2^10, 2^14, 2^15}, water = colors.blue, waves = colors.lightBlue, log = colors.brown, player = {colors.orange, colors.black}},
  37.     ["hell"] = {grass = colors.red, tree = {_, colors.gray}, road = colors.gray, dashes = colors.magenta, car = {colors.black}, water = colors.orange, waves = colors.yellow, log = colors.lightGray, player = {colors.white, colors.black}},
  38.     ["snow"] = {grass = colors.white, tree = {_, colors.green}, road = colors.gray, dashes = colors.yellow, car = {colors.red, colors.green}, water = colors.lightBlue, waves = colors.white, log = colors.brown, player = {colors.green, colors.red}},
  39.     ["fallout"] = {grass = colors.brown, tree = {_, colors.black}, road = colors.gray, dashes = colors.lightGray, car = {colors.black, colors.magenta, colors.pink}, water = colors.lime, log = colors.green, waves = colors.green, player = {colors.red, colors.black}},
  40.     ["space"] = {grass = colors.black, tree = {_, colors.white}, road = colors.black, dashes = colors.yellow, car = {colors.brown, colors.white, colors.gray, colors.lightGray}, water = colors.purple, waves = colors.magenta, log = colors.black, player = {colors.black, colors.yellow}},
  41.     ["sky"] = {grass = colors.white, tree = {colors.lightGray, colors.gray}, road = colors.yellow, dashes = colors.yellow, car = {colors.orange}, water = colors.lightBlue, log = colors.white, waves = colors.lightBlue, player = {colors.white, colors.yellow}},
  42.     ["candy"] = {grass = colors.magenta, tree = {colors.pink, colors.red}, road = colors.red, dashes = colors.magenta, car = {colors.orange, colors.purple, colors.blue}, water = colors.pink, waves = colors.magenta, log = colors.yellow, player = {colors.yellow, colors.pink}},
  43.     ["cave"] = {grass = colors.lightGray, tree = {colors.gray, colors.black}, road = colors.gray, dashes = colors.lightGray, car = {colors.green, colors.lime, colors.white, colors.black}, water = colors.orange, waves = colors.yellow, log = colors.red, player = {colors.blue, colors.lime}},
  44.         ["end"] = {grass = colors.yellow, tree = {colors.black, colors.purple}, road = colors.white, dashes = colors.purple, car = {colors.purple, colors.magenta, colors.black}, water = colors.black, waves = colors.lightBlue, log = colors.purple, player = {colors.black, colors.purple}},
  45.     ["village"] = {grass = colors.lime, tree = {colors.brown, colors.yellow}, road = colors.lightGray, dashes = colors.gray, car = {colors.brown, colors.green, colors.purple, colors.white, colors.gray}, water = colors.lightBlue, waves = colors.blue, log = colors.brown, player = {colors.blue, colors.white}}
  46. }
  47.  
  48. if themes[tArgs[1]] then
  49.     theme = tArgs[1]
  50. end
  51.  
  52.  
  53.  
  54. local player = {
  55.     ["isAlive"] = true,
  56.     ["onWater"] = false,
  57.     ["onCar"] = false,
  58.     ["onLog"] = false
  59. }
  60.  
  61. local map = {"grass", "grass", "grass", "grass", "grass"}
  62. local mapDirs = {false, false, false, false, false}
  63. local mapItems = {}   --# = tree      colornum = car or log
  64. for i = 1, 5 do
  65.     mapItems[#mapItems + 1] = {}
  66.     for n = 1, w do
  67.         local choice = math.random(1, 4)
  68.         if choice == 1 and not (i == 5 and n == midW) then
  69.             mapItems[#mapItems][n] = "#"
  70.         end
  71.     end
  72. end
  73.  
  74. local function resetTimers()
  75.     timers = {
  76.         ["gametick"] = os.startTimer(.05),
  77.         ["screenscroll"] = os.startTimer(2)
  78.     }
  79. end
  80.  
  81. local function translate(number)
  82.     local alphabet = {"a","b","c","d","e","f"}
  83.     res = math.log(number)/math.log(2)
  84.     if res > 9 then
  85.         res = alphabet[res-9]
  86.     end
  87.     res = tostring(res)
  88.     return res
  89. end
  90.  
  91. local function drawB(string, xPos, yPos, txtcol, bakcol)
  92.     local string = string or ""
  93.     local txtcol = txtcol or colors.white
  94.     local bakcol = bakcol or colors.black
  95.     term.setCursorPos(xPos, yPos)
  96.     term.setBackgroundColor(bakcol)
  97.     term.setTextColor(txtcol)
  98.     term.write(string)
  99. end
  100.  
  101. local function draw(str, xPos, yPos, txtcol, bakcol)
  102.     local str = str or ""
  103.     local txtcol = txtcol or colors.white
  104.     local bakcol = bakcol or colors.black
  105.     term.setCursorPos(xPos, yPos)
  106.     txtcol = translate(txtcol)
  107.     bakcol = translate(bakcol)
  108.     term.blit(str,string.rep(txtcol,#str),string.rep(bakcol,#str))
  109. end
  110.  
  111. local function blit(str, txtcol, bakcol)
  112.     str = str or ""
  113.     txtcol = translate(txtcol)
  114.     bakcol = translate(bakcol)
  115.     term.blit(str,string.rep(txtcol,#str),string.rep(bakcol,#str))
  116. end
  117.  
  118. local function fill(bakcol)
  119.   bakcol = translate(bakcol)
  120.   for i = 1,19 do
  121.     term.setCursorPos(1,i)
  122.     term.blit(string.rep(" ",w),string.rep(bakcol,w),string.rep(bakcol,w))
  123.   end
  124.   term.setCursorPos(1,1)
  125. end
  126.  
  127. local function fillLine(bakcol)
  128.   bakcol = translate(bakcol)
  129.   local q,s = term.getCursorPos()
  130.   term.blit(string.rep(" ",w),string.rep(bakcol,w),string.rep(bakcol,w))
  131.   term.setCursorPos(q,w+1)
  132. end
  133.  
  134. local function generateNewChunk()
  135.     repeat
  136.         type = math.random(1, 3)
  137.     until type ~= prevtype
  138.     local length = math.random(1, 5)
  139.     prevtype = type
  140.     if type == 1 then type = "grass" end
  141.     if type == 2 then type = "road" end
  142.     if type == 3 then type = "water" end
  143.     local currentLen = #map
  144.     for i = 1, length do
  145.         map[#map+1] = type
  146.         mapItems[#mapItems + 1] = {}
  147.         mapDirs[#mapDirs + 1] = false
  148.         if type == "grass" then
  149.             for n = 1, w do
  150.                 local choice = math.random(1, 4)
  151.                 if choice == 1 then
  152.                     mapItems[#mapItems][n] = "#"
  153.                 else
  154.                     mapItems[#mapItems][n] = false
  155.                 end
  156.             end
  157.         elseif type == "road" then
  158.             local choice = math.random(0, 1)
  159.             if choice == 0 then choice = "left" else choice = "right" end
  160.             mapDirs[#mapDirs] = choice
  161.             mapItems[#mapItems] = {}
  162.             for n = 1, w do
  163.                 local choice = math.random(1, 4)
  164.                 if choice == 1 then
  165.                     mapItems[#mapItems][n] = themes[theme].car[math.random(1, #themes[theme].car)]
  166.                 else
  167.                     mapItems[#mapItems][n] = false
  168.                 end
  169.             end
  170.         elseif type == "water" then
  171.             local choice = math.random(0, 1)
  172.             if choice == 0 then choice = "left" else choice = "right" end
  173.             mapDirs[#mapDirs] = choice
  174.             mapItems[#mapItems] = {}
  175.             for n = 1, w do
  176.                 local choice = math.random(1, 4)
  177.                 if choice == 1 then
  178.                     mapItems[#mapItems][n] = 128
  179.                 else
  180.                     mapItems[#mapItems][n] = false
  181.                 end
  182.             end
  183.         end
  184.         log(type)
  185.     end
  186.     return length
  187. end
  188.  
  189. local function initialMapGen()
  190.     local genNum = 5
  191.     while genNum < h + 3 do
  192.         genNum = genNum + generateNewChunk()
  193.     end
  194. end
  195.  
  196. local function moveScreenUp()
  197.     sChanged = true
  198.     table.remove(map, 1)
  199.     table.remove(mapItems, 1)
  200.     table.remove(mapDirs, 1)
  201.     pos.y = pos.y - 1
  202. end
  203.  
  204. local function checkCollision(dir)
  205.     if dir == 1 then xOff, yOff = 0, 1 end
  206.     if dir == 2 then xOff, yOff = 1, 0 end
  207.     if dir == 3 then xOff, yOff = 0, -1 end
  208.     if dir == 4 then xOff, yOff = -1, 0 end
  209.     if mapItems[pos.y + yOff][pos.x + xOff] and mapItems[pos.y + yOff][pos.x + xOff] == "#" then
  210.         return false
  211.     end
  212.     return true
  213. end
  214.  
  215. local function eventHandler(events)
  216.     sChanged = true
  217.     if events[2] == keys.up and checkCollision(1) then
  218.         if pos.y == 5 then
  219.             moveScreenUp()
  220.             timers.screenscroll = os.startTimer(2)
  221.         end
  222.         pos.y = pos.y + 1
  223.         distance = distance + 1
  224.     elseif events[2] == keys.down and (pos.y == 1 or checkCollision(3)) then
  225.         pos.y = pos.y - 1
  226.         distance = distance - 1
  227.     elseif events[2] == keys.left and pos.x > 1 and checkCollision(4) then
  228.         pos.x = pos.x - 1
  229.     elseif events[2] == keys.right and pos.x < w and checkCollision(2) then
  230.         pos.x = pos.x + 1
  231.     end
  232. end
  233.  
  234. local function updateGame()
  235.     --== player stuff ==--
  236.     if map[pos.y] == "water" then
  237.         if mapItems[pos.y][pos.x] then
  238.             player.onWater = false
  239.             player.onLog = true
  240.         else
  241.             player.onWater = true
  242.             player.onLog = false
  243.         end
  244.     else
  245.         player.onWater = false
  246.         player.onLog = false
  247.     end
  248.     if player.onLog and tickToggle and player.isAlive then
  249.         if mapDirs[pos.y] == "right" and pos.x < w then pos.x = pos.x + 1 end
  250.         if mapDirs[pos.y] == "left" and pos.x > 1 then pos.x = pos.x - 1 end
  251.     end
  252.     if map[pos.y] == "road" and mapItems[pos.y][pos.x] then
  253.         player.onCar = true
  254.     else
  255.         player.onCar = false
  256.     end
  257.     if pos.y == 0 or player.onCar or player.onWater then
  258.         player.isAlive = false
  259.     end
  260.     if not map[22] then
  261.         generateNewChunk()
  262.     end
  263.     --== cars and logs ==--
  264.     if tickToggle then
  265.         for i = 1, h do
  266.             local choice
  267.             if map[i] == "road" then
  268.                 choice = math.random(1, 10)
  269.             elseif map[i] == "water" then
  270.                 choice = math.random(1, 2)
  271.             end
  272.             if map[i] == "road" or map[i] == "water" then
  273.                 if mapDirs[i] == "left" then
  274.                     for n = 1, w - 1 do
  275.                         mapItems[i][n] = mapItems[i][n + 1]
  276.                     end
  277.                     if mapItems[i][w - 1] then
  278.                         local chance = map[i] == "road" and math.random(0, 2) or math.random(1, 3)
  279.                         mapItems[i][w] = chance > 1 and mapItems[i][w - 1]
  280.                     elseif choice == 1 then
  281.                         mapItems[i][w] = themes[theme].car[math.random(1, #themes[theme].car)]
  282.                     else
  283.                         mapItems[i][w] = false
  284.                     end
  285.                 elseif mapDirs[i] == "right" then
  286.                     for n = w, 2, -1 do
  287.                         mapItems[i][n] = mapItems[i][n - 1]
  288.                     end
  289.                     if mapItems[i][2] then
  290.                         local chance = map[i] == "road" and math.random(0, 2) or math.random(1, 3)
  291.                         mapItems[i][1] = chance > 1 and mapItems[i][2]
  292.                     elseif choice == 1 then
  293.                         mapItems[i][1] = themes[theme].car[math.random(1, #themes[theme].car)]
  294.                     else
  295.                         mapItems[i][1] = false
  296.                     end
  297.                 end
  298.             end
  299.         end
  300.         tickToggle = false
  301.     else
  302.         tickToggle = true
  303.     end
  304.     --== stuff done differently once dead ==--
  305.     if not player.isAlive and not tickToggle and map[pos.y] == "water" then
  306.         if mapDirs[pos.y] == "right" and pos.x < w + 1 then pos.x = pos.x + 1 end
  307.         if mapDirs[pos.y] == "left" and pos.x > 0 then pos.x = pos.x - 1 end
  308.     end
  309. end
  310.  
  311. local function drawPlayer()
  312.     term.setCursorPos(pos.x, h - pos.y + 1)
  313.     if player.isAlive then
  314.         --term.setBackgroundColor(themes[theme].player[1])
  315.         --term.setTextColor(themes[theme].player[2])
  316.         --term.write("O")
  317.         blit("O",themes[theme].player[2],themes[theme].player[1])
  318.     else
  319.         --term.setBackgroundColor(themes[theme][map[pos.y]])
  320.         --term.setTextColor(themes[theme].player[1])
  321.         --term.write("X")
  322.         blit("X",themes[theme].player[1],themes[theme][map[pos.y]])
  323.     end
  324. end
  325.  
  326. local function drawInfo()
  327.     draw("Score", w-4, 1, themes[theme].player[2], themes[theme].player[1])
  328.     draw("     ", w-4, 2, themes[theme].player[2], themes[theme].player[1])
  329.     draw(tostring(distance), w-4, 2, themes[theme].player[2], themes[theme].player[1])
  330. end
  331.  
  332. local function drawDeath()
  333.     --term.setBackgroundColor(colors.red)
  334.     term.setCursorPos(1, midH)
  335.     --term.clearLine()
  336.     fillLine(colors.red)
  337.     term.setCursorPos(1, midH + 1)
  338.     --term.clearLine()
  339.     fillLine(colors.red)
  340.     draw("You Died", (w/2) - 3, midH, colors.white, colors.red)
  341.     draw("Score: " .. tostring(distance), (w/2) - (7 + #tostring(distance))/2 + 1, midH + 1, colors.white, colors.red)
  342. end
  343.  
  344. local function updateScreen()
  345.     local start
  346.     for i = 1, h do
  347.         term.setBackgroundColor(themes[theme][map[i]])
  348.         local background = themes[theme][map[i]]
  349.         term.setCursorPos(1, h - i + 1)
  350.         if map[i] == "road" then
  351.             term.setTextColor(themes[theme].dashes)
  352.             term.write(roadString)
  353.             --blit(roadstring,themes[theme].dashes,background)
  354.         elseif map[i] == "water" then
  355.             term.setTextColor(themes[theme].waves)
  356.             term.write(waterString)
  357.             --blit(waterString,themes[theme].waves,background)
  358.         else
  359.             --term.clearLine()
  360.             fillLine(term.getBackgroundColor())
  361.         end
  362.         if i == pos.y and not player.isAlive and map[pos.y] == "road" then
  363.             drawPlayer()
  364.             start = true
  365.         end
  366.         if map[i] == "grass" then
  367.             term.setBackgroundColor(themes[theme].tree[1] or themes[theme].grass)
  368.             term.setTextColor(themes[theme].tree[2])
  369.             for k, v in pairs(mapItems[i]) do
  370.                 if mapItems[i][k] == "#" then
  371.                     term.setCursorPos(k, h - i + 1)
  372.                     term.write("#")
  373.                     --blit("#",themes[theme].tree[2],themes[theme].tree[1] or themes[theme].grass)
  374.                 end
  375.             end
  376.         elseif map[i] == "road" then
  377.             for k, e in pairs(mapItems[i]) do
  378.                 if tonumber(mapItems[i][k]) then
  379.                     term.setCursorPos(k, h - i + 1)
  380.                     term.setBackgroundColor(mapItems[i][k])
  381.                     term.write(" ")
  382.                     --blit(" ",mapItems[i][k],mapItems[i][k])
  383.                 end
  384.             end
  385.         elseif map[i] == "water" then
  386.             for k, e in pairs(mapItems[i]) do
  387.                 if tonumber(mapItems[i][k]) then
  388.                     term.setCursorPos(k, h - i + 1)
  389.                     term.setBackgroundColor(themes[theme].log)
  390.                     term.write(" ")
  391.                     --blit(" ",term.getTextColor(),term.getBackgroundColor())
  392.                 end
  393.             end
  394.         end
  395.         if i == pos.y and not start then
  396.             drawPlayer()
  397.         end
  398.     end
  399.     if player.isAlive then
  400.         drawInfo()
  401.     else
  402.         drawDeath()
  403.     end
  404. end
  405.    
  406. if tArgs[1] == "list" then
  407.   print("")
  408.   for i in pairs(themes) do
  409.     write(i .. " ")
  410.   end
  411. else
  412.  
  413. initialMapGen()
  414. while player.isAlive do
  415.     local events = {os.pullEvent()}
  416.     if events[1] == "timer" then
  417.         if events[2] == timers.gametick then
  418.             sChanged = true
  419.             updateGame()
  420.             if sChanged then
  421.                 updateScreen()
  422.                 sChanged = false
  423.             end
  424.             timers.gametick = os.startTimer(.05)
  425.         elseif events[2] == timers.screenscroll then
  426.             moveScreenUp()
  427.             timers.screenscroll = os.startTimer(2)
  428.         end
  429.     elseif events[1] == "key" then
  430.         eventHandler(events)
  431.     end
  432. end
  433.  
  434.  
  435. term.setBackgroundColor(colors.red)
  436. term.clear()
  437. sleep(.2)
  438. resetTimers()
  439. timers.deathTimer = os.startTimer(1)
  440. local exitable = false
  441.  
  442. while true do
  443.     local events = {os.pullEvent()}
  444.     if events[1] == "timer" then
  445.         if events[2] == timers.gametick then
  446.             sChanged = true
  447.             updateGame()
  448.             if sChanged then
  449.                 updateScreen()
  450.                 sChanged = false
  451.             end
  452.             timers.gametick = os.startTimer(.05)
  453.         end
  454.         if events[2] == timers.deathTimer then
  455.             exitable = true
  456.         end
  457.     elseif events[1] == "key" and exitable then
  458.         term.setBackgroundColor(colors.black)
  459.         term.clear()
  460.         term.setCursorPos(1, 1)
  461.         break
  462.     end
  463. end
  464. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement