Advertisement
CaptainSpaceCat

Crossroads

Jul 5th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.03 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 draw(string, xPos, yPos, txtcol, bakcol)
  82.     local string = string or ""
  83.     local txtcol = txtcol or colors.white
  84.     local bakcol = bakcol or colors.black
  85.     term.setCursorPos(xPos, yPos)
  86.     term.setBackgroundColor(bakcol)
  87.     term.setTextColor(txtcol)
  88.     term.write(string)
  89. end
  90.  
  91. local function generateNewChunk()
  92.     repeat
  93.         type = math.random(1, 3)
  94.     until type ~= prevtype
  95.     local length = math.random(1, 5)
  96.     prevtype = type
  97.     if type == 1 then type = "grass" end
  98.     if type == 2 then type = "road" end
  99.     if type == 3 then type = "water" end
  100.     local currentLen = #map
  101.     for i = 1, length do
  102.         map[#map+1] = type
  103.         mapItems[#mapItems + 1] = {}
  104.         mapDirs[#mapDirs + 1] = false
  105.         if type == "grass" then
  106.             for n = 1, w do
  107.                 local choice = math.random(1, 4)
  108.                 if choice == 1 then
  109.                     mapItems[#mapItems][n] = "#"
  110.                 else
  111.                     mapItems[#mapItems][n] = false
  112.                 end
  113.             end
  114.         elseif type == "road" then
  115.             local choice = math.random(0, 1)
  116.             if choice == 0 then choice = "left" else choice = "right" end
  117.             mapDirs[#mapDirs] = choice
  118.             mapItems[#mapItems] = {}
  119.             for n = 1, w do
  120.                 local choice = math.random(1, 4)
  121.                 if choice == 1 then
  122.                     mapItems[#mapItems][n] = themes[theme].car[math.random(1, #themes[theme].car)]
  123.                 else
  124.                     mapItems[#mapItems][n] = false
  125.                 end
  126.             end
  127.         elseif type == "water" then
  128.             local choice = math.random(0, 1)
  129.             if choice == 0 then choice = "left" else choice = "right" end
  130.             mapDirs[#mapDirs] = choice
  131.             mapItems[#mapItems] = {}
  132.             for n = 1, w do
  133.                 local choice = math.random(1, 4)
  134.                 if choice == 1 then
  135.                     mapItems[#mapItems][n] = 128
  136.                 else
  137.                     mapItems[#mapItems][n] = false
  138.                 end
  139.             end
  140.         end
  141.         log(type)
  142.     end
  143.     return length
  144. end
  145.  
  146. local function initialMapGen()
  147.     local genNum = 5
  148.     while genNum < h + 3 do
  149.         genNum = genNum + generateNewChunk()
  150.     end
  151. end
  152.  
  153. local function moveScreenUp()
  154.     sChanged = true
  155.     table.remove(map, 1)
  156.     table.remove(mapItems, 1)
  157.     table.remove(mapDirs, 1)
  158.     pos.y = pos.y - 1
  159. end
  160.  
  161. local function checkCollision(dir)
  162.     if dir == 1 then xOff, yOff = 0, 1 end
  163.     if dir == 2 then xOff, yOff = 1, 0 end
  164.     if dir == 3 then xOff, yOff = 0, -1 end
  165.     if dir == 4 then xOff, yOff = -1, 0 end
  166.     if mapItems[pos.y + yOff][pos.x + xOff] and mapItems[pos.y + yOff][pos.x + xOff] == "#" then
  167.         return false
  168.     end
  169.     return true
  170. end
  171.  
  172. local function eventHandler(events)
  173.     sChanged = true
  174.     if events[2] == keys.up and checkCollision(1) then
  175.         if pos.y == 5 then
  176.             moveScreenUp()
  177.             timers.screenscroll = os.startTimer(2)
  178.         end
  179.         pos.y = pos.y + 1
  180.         distance = distance + 1
  181.     elseif events[2] == keys.down and (pos.y == 1 or checkCollision(3)) then
  182.         pos.y = pos.y - 1
  183.         distance = distance - 1
  184.     elseif events[2] == keys.left and pos.x > 1 and checkCollision(4) then
  185.         pos.x = pos.x - 1
  186.     elseif events[2] == keys.right and pos.x < w and checkCollision(2) then
  187.         pos.x = pos.x + 1
  188.     end
  189. end
  190.  
  191. local function updateGame()
  192.     --== player stuff ==--
  193.     if map[pos.y] == "water" then
  194.         if mapItems[pos.y][pos.x] then
  195.             player.onWater = false
  196.             player.onLog = true
  197.         else
  198.             player.onWater = true
  199.             player.onLog = false
  200.         end
  201.     else
  202.         player.onWater = false
  203.         player.onLog = false
  204.     end
  205.     if player.onLog and tickToggle and player.isAlive then
  206.         if mapDirs[pos.y] == "right" and pos.x < w then pos.x = pos.x + 1 end
  207.         if mapDirs[pos.y] == "left" and pos.x > 1 then pos.x = pos.x - 1 end
  208.     end
  209.     if map[pos.y] == "road" and mapItems[pos.y][pos.x] then
  210.         player.onCar = true
  211.     else
  212.         player.onCar = false
  213.     end
  214.     if pos.y == 0 or player.onCar or player.onWater then
  215.         player.isAlive = false
  216.     end
  217.     if not map[22] then
  218.         generateNewChunk()
  219.     end
  220.     --== cars and logs ==--
  221.     if tickToggle then
  222.         for i = 1, h do
  223.             local choice
  224.             if map[i] == "road" then
  225.                 choice = math.random(1, 10)
  226.             elseif map[i] == "water" then
  227.                 choice = math.random(1, 2)
  228.             end
  229.             if map[i] == "road" or map[i] == "water" then
  230.                 if mapDirs[i] == "left" then
  231.                     for n = 1, w - 1 do
  232.                         mapItems[i][n] = mapItems[i][n + 1]
  233.                     end
  234.                     if mapItems[i][w - 1] then
  235.                         local chance = map[i] == "road" and math.random(0, 2) or math.random(1, 3)
  236.                         mapItems[i][w] = chance > 1 and mapItems[i][w - 1]
  237.                     elseif choice == 1 then
  238.                         mapItems[i][w] = themes[theme].car[math.random(1, #themes[theme].car)]
  239.                     else
  240.                         mapItems[i][w] = false
  241.                     end
  242.                 elseif mapDirs[i] == "right" then
  243.                     for n = w, 2, -1 do
  244.                         mapItems[i][n] = mapItems[i][n - 1]
  245.                     end
  246.                     if mapItems[i][2] then
  247.                         local chance = map[i] == "road" and math.random(0, 2) or math.random(1, 3)
  248.                         mapItems[i][1] = chance > 1 and mapItems[i][2]
  249.                     elseif choice == 1 then
  250.                         mapItems[i][1] = themes[theme].car[math.random(1, #themes[theme].car)]
  251.                     else
  252.                         mapItems[i][1] = false
  253.                     end
  254.                 end
  255.             end
  256.         end
  257.         tickToggle = false
  258.     else
  259.         tickToggle = true
  260.     end
  261.     --== stuff done differently once dead ==--
  262.     if not player.isAlive and not tickToggle and map[pos.y] == "water" then
  263.         if mapDirs[pos.y] == "right" and pos.x < w + 1 then pos.x = pos.x + 1 end
  264.         if mapDirs[pos.y] == "left" and pos.x > 0 then pos.x = pos.x - 1 end
  265.     end
  266. end
  267.  
  268. local function drawPlayer()
  269.     term.setCursorPos(pos.x, h - pos.y + 1)
  270.     if player.isAlive then
  271.         term.setBackgroundColor(themes[theme].player[1])
  272.         term.setTextColor(themes[theme].player[2])
  273.         term.write("O")
  274.     else
  275.         term.setBackgroundColor(themes[theme][map[pos.y]])
  276.         term.setTextColor(themes[theme].player[1])
  277.         term.write("X")
  278.     end
  279. end
  280.  
  281. local function drawInfo()
  282.     draw("Score", w-4, 1, themes[theme].player[2], themes[theme].player[1])
  283.     draw("     ", w-4, 2, themes[theme].player[2], themes[theme].player[1])
  284.     draw(tostring(distance), w-4, 2, themes[theme].player[2], themes[theme].player[1])
  285. end
  286.  
  287. local function drawDeath()
  288.     term.setBackgroundColor(colors.red)
  289.     term.setCursorPos(1, midH)
  290.     term.clearLine()
  291.     term.setCursorPos(1, midH + 1)
  292.     term.clearLine()
  293.     draw("You Died", (w/2) - 3, midH, colors.white, colors.red)
  294.     draw("Score: " .. tostring(distance), (w/2) - (7 + #tostring(distance))/2 + 1, midH + 1, colors.white, colors.red)
  295. end
  296.  
  297. local function updateScreen()
  298.     local start
  299.     for i = 1, h do
  300.         term.setBackgroundColor(themes[theme][map[i]])
  301.         term.setCursorPos(1, h - i + 1)
  302.         if map[i] == "road" then
  303.             term.setTextColor(themes[theme].dashes)
  304.             term.write(roadString)
  305.             --term.blit(roadstring, string.rep("2", w), string.rep("0", w))    --change 2 and 0 to texturepack controlled variables
  306.         elseif map[i] == "water" then
  307.             term.setTextColor(themes[theme].waves)
  308.             term.write(waterString)
  309.         else
  310.             term.clearLine()
  311.         end
  312.         if i == pos.y and not player.isAlive and map[pos.y] == "road" then
  313.             drawPlayer()
  314.             start = true
  315.         end
  316.         if map[i] == "grass" then
  317.             term.setBackgroundColor(themes[theme].tree[1] or themes[theme].grass)
  318.             term.setTextColor(themes[theme].tree[2])
  319.             for k, v in pairs(mapItems[i]) do
  320.                 if mapItems[i][k] == "#" then
  321.                     term.setCursorPos(k, h - i + 1)
  322.                     term.write("#")
  323.                 end
  324.             end
  325.         elseif map[i] == "road" then
  326.             for k, e in pairs(mapItems[i]) do
  327.                 if tonumber(mapItems[i][k]) then
  328.                     term.setCursorPos(k, h - i + 1)
  329.                     term.setBackgroundColor(mapItems[i][k])
  330.                     term.write(" ")
  331.                 end
  332.             end
  333.         elseif map[i] == "water" then
  334.             for k, e in pairs(mapItems[i]) do
  335.                 if tonumber(mapItems[i][k]) then
  336.                     term.setCursorPos(k, h - i + 1)
  337.                     term.setBackgroundColor(themes[theme].log)
  338.                     term.write(" ")
  339.                 end
  340.             end
  341.         end
  342.         if i == pos.y and not start then
  343.             drawPlayer()
  344.         end
  345.     end
  346.     if player.isAlive then
  347.         drawInfo()
  348.     else
  349.         drawDeath()
  350.     end
  351. end
  352.    
  353. if tArgs[1] == "list" then
  354.   print("")
  355.   for i in pairs(themes) do
  356.     write(i .. " ")
  357.   end
  358. else
  359.  
  360. initialMapGen()
  361. while player.isAlive do
  362.     local events = {os.pullEvent()}
  363.     if events[1] == "timer" then
  364.         if events[2] == timers.gametick then
  365.             sChanged = true
  366.             updateGame()
  367.             if sChanged then
  368.                 updateScreen()
  369.                 sChanged = false
  370.             end
  371.             timers.gametick = os.startTimer(.05)
  372.         elseif events[2] == timers.screenscroll then
  373.             moveScreenUp()
  374.             timers.screenscroll = os.startTimer(2)
  375.         end
  376.     elseif events[1] == "key" then
  377.         eventHandler(events)
  378.     end
  379. end
  380.  
  381.  
  382. term.setBackgroundColor(colors.red)
  383. term.clear()
  384. sleep(.2)
  385. resetTimers()
  386. timers.deathTimer = os.startTimer(1)
  387. local exitable = false
  388.  
  389. while true do
  390.     local events = {os.pullEvent()}
  391.     if events[1] == "timer" then
  392.         if events[2] == timers.gametick then
  393.             sChanged = true
  394.             updateGame()
  395.             if sChanged then
  396.                 updateScreen()
  397.                 sChanged = false
  398.             end
  399.             timers.gametick = os.startTimer(.05)
  400.         end
  401.         if events[2] == timers.deathTimer then
  402.             exitable = true
  403.         end
  404.     elseif events[1] == "key" and exitable then
  405.         term.setBackgroundColor(colors.black)
  406.         term.clear()
  407.         term.setCursorPos(1, 1)
  408.         break
  409.     end
  410. end
  411. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement