Advertisement
Guest User

CC game

a guest
May 5th, 2012
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.96 KB | None | 0 0
  1. term.clear()
  2.  
  3. if fs.exists("RPGConfig") then
  4.     config = fs.open("RPGConfig", "r")
  5.     rate = config:readLine();
  6.     if rate ~= "rate=" then
  7.         term.clear()
  8.         term.setCursorPos(1, 1)
  9.         print([[Error: line one of the
  10.         config file should be "rate=".
  11.         ]])
  12.         sleep(5)
  13.         error()
  14.     end
  15.     rate = config.readLine()
  16.     config.close()
  17. else
  18.     textutils.slowPrint("Error: could not open config file. ")
  19.     textutils.slowPrint("Creating a config file...")
  20.     config = fs.open("RPGConfig", "w")
  21.     config.write([[rate=
  22. 0.0001
  23.     ]])
  24.     config.close()
  25.     sleep(1)
  26.     rate = 0.001
  27. end
  28. scrX, scrY = term.getSize()
  29. tmpPosX = scrX / 2
  30. tmpPosY = scrY / 2
  31. tmpMinus = scrX / 8
  32. tmpMinus2 = math.floor(tmpMinus)
  33. borderRight = scrX - tmpMinus2
  34. tmpMinusY = scrY / 50
  35. tmpMinusY2 = math.floor(tmpMinusY)
  36. borderDown = scrY - tmpMinusY2
  37. x = tmpPosX
  38. y = tmpPosY
  39. sceneDown = 0
  40. sceneRight = 0
  41. playerHasBow = false
  42. blocksCount = 0
  43. currentCors = {}
  44. rot = "up"
  45. blocks = {}
  46. doors = {}
  47. currentDoors = {}
  48. doorsCount = 0
  49. NPCs = {}
  50. currentNPCs = {}
  51. NPCCount = 0
  52. bow = {}
  53. currentBow = {}
  54. bowCount = 0
  55. openWorld = true
  56. bowPickup = false
  57. arrow = {}
  58. currentArrow = {}
  59. arrowCount = 0
  60. changeMainWorldCors = false
  61. deleteNPC = false
  62. arrowMove = 0
  63. dying = {}
  64. currentDying = {}
  65. dyingCount = 0
  66. function addBlock(blX, blY)
  67.     blocksCountTmp = blocksCount + 1
  68.     table.insert(blocks, blocksCountTmp, {blX, blY})
  69.     blocksCount = blocksCount + 1
  70. end
  71. function clear()
  72.     term.setCursorPos(x, y)
  73.     term.write(" ")
  74. end
  75. function move()
  76.     wall = false
  77.     door = false
  78.     bowPickup = false
  79.     if rot == "up" then
  80.         tmpX = x
  81.         tmpY = y - 1
  82.     elseif rot == "left" then
  83.         tmpX = x - 1
  84.         tmpY = y
  85.     elseif rot == "down" then
  86.         tmpX = x
  87.         tmpY = y + 1
  88.     elseif rot == "right" then
  89.         tmpX = x + 1
  90.         tmpY = y
  91.     end
  92.     for i=1, blocksCount do
  93.         if currentCors[i][1] == tmpX and
  94.         currentCors[i][2] == tmpY then
  95.             wall = true
  96.         end
  97.     end
  98.     for i=1, doorsCount do
  99.         if currentDoors[i][1] == tmpX and
  100.         currentDoors[i][2] == tmpY then
  101.             door = true
  102.             tmpLevN = currentDoors[i][3]
  103.         end
  104.     end
  105.     bowPickUp = false
  106.     for i=1, bowCount do
  107.         if currentBow[i][1] == tmpX and
  108.         currentBow[i][2] == tmpY then
  109.             bowPickUp = true
  110.             bowId = i
  111.             tmpLevN = currentDoors[i][3]
  112.         end
  113.     end
  114.     if bowPickUp == true then
  115.         playerHasBow = true
  116.         table.remove(currentBow, bowId)
  117.         table.insert(currentBow, bowId, {0, 0, "deleted"})
  118.         table.remove(bow, bowId)
  119.         table.insert(bow, {0, 0, bowId, "deleted"})
  120.     end
  121.     if wall == true then
  122.         return false
  123.     else
  124.         if door == false and bowPickUp == false then
  125.             return true
  126.         end
  127.     end
  128.     if bowPickUp then
  129.         playerHasBow = true
  130.         table.remove(currentBow, bowId)
  131.         table.insert(currentBow, bowId, {0, 0, "deleted"})
  132.         return true
  133.     end
  134.     if door then
  135.         loadLevel(tmpLevN)
  136.     end
  137. end
  138. function loadLevel(levelName)
  139.     for i=1,blocksCount do
  140.         table.remove(blocks, i)
  141.     end
  142.     for i=1,doorsCount do
  143.         table.remove(doors, i)
  144.     end
  145.     for i=1, NPCCount do
  146.         table.remove(NPCs, i)
  147.     end
  148.     for i=1, bowCount do
  149.         table.remove(bow, i)
  150.     end
  151.     for i=1, arrowCount do
  152.         table.remove(arrow, i)
  153.         table.remove(currentArrow, i)
  154.     end
  155.     x = tmpPosX
  156.     y = tmpPosY
  157.     sceneDown = 0
  158.     sceneRight = 0
  159.     blocksCount = 0
  160.     doorsCount = 0
  161.     NPCCount = 0
  162.     bowCount = 0
  163.     arrowCount = 0
  164.     createLevel(levelName)
  165.     arrow = {}
  166.     for i=1,dyingCount do
  167.         table.remove(dying, i)
  168.         table.remove(currentDying, i)
  169.     end
  170. currentArrow = {}
  171. arrowCount = 0
  172. arrowMove = 0
  173. dyingCount = 0
  174. end
  175. function drawScene()
  176.     term.clear()
  177.     for i=1, blocksCount do
  178.         if blocksCount > 0 then
  179.             drawX = blocks[i][1]
  180.             drawY = blocks[i][2]
  181.             drawX = drawX + sceneRight
  182.             drawY = drawY + sceneDown  
  183.             table.insert(currentCors, i, {drawX, drawY, "exists"})
  184.             term.setCursorPos(drawX, drawY)
  185.             term.write("X")
  186.         else
  187.             print("error")
  188.             term.setCursorPos(1, 1)
  189.             term.write(blocksCount)
  190.         end
  191.     end
  192.     for i=1,doorsCount do
  193.         if doorsCount > 0 then
  194.             drawX = doors[i][1]
  195.             drawY = doors[i][2]
  196.             drawLev = doors[i][3]
  197.             drawX = drawX + sceneRight
  198.             drawY = drawY + sceneDown
  199.             table.insert(currentDoors, i, {drawX, drawY, drawLev})
  200.             term.setCursorPos(drawX, drawY)
  201.             term.write("|")
  202.         end
  203.     end
  204.     for i=1,bowCount do
  205.         if bowCount > 0 then
  206.             drawX = bow[i][1]
  207.             drawY = bow[i][2]
  208.             bowExists = bow[i][3]
  209.             drawX = drawX + sceneRight
  210.             drawY = drawY + sceneDown
  211.             if bowExists == "exists" then
  212.                 table.insert(currentBow, i, {drawX, drawY})
  213.                 term.setCursorPos(drawX, drawY)
  214.                 term.write("B")
  215.             end
  216.         end
  217.     end
  218.     for i=1,NPCCount do
  219.         if NPCCount > 0 then
  220.             drawX = NPCs[i][1]
  221.             drawY = NPCs[i][2]
  222.             NPCType = NPCs[i][3]
  223.             spNPC = NPCs[i][4]
  224.             NPCUsed = NPCs[i][5]
  225.             if spNPC then
  226.             if NPCUsed == 1 then
  227.                 drawX = currentNPCs[i][1]
  228.                 drawY = currentNPCs[i][2]
  229.             end
  230.             drawX = drawX + sceneRight
  231.             drawY = drawY + sceneDown
  232.             tmpDrawX = drawX
  233.             tmpDrawY = drawY
  234.             randNPCMov = math.random(0, 100)
  235.             if NPCType == "dumb" then
  236.                 curNPC = "dumb"
  237.                 if randNPCMov == 1 then
  238.                     drawX = drawX - 1
  239.                 elseif randNPCMov == 2 then
  240.                     drawX = drawX + 1
  241.                 elseif randNPCMov == 3 then
  242.                     drawY = drawY - 1
  243.                 elseif randNPCMov == 4 then
  244.                     drawY = drawY + 1
  245.                 end
  246.             end
  247.             term.setCursorPos(drawX, drawY)
  248.             term.write("A")
  249.             if arrowCount > 0 then
  250.                 for b=1,arrowCount do
  251.                 if arrow[b][6] == 1 then
  252.                     if tmpDrawX == currentArrow[b][1] and
  253.                     tmpDrawY == currentArrow[b][2] then
  254.                         addDying(drawX, drawY, "bow", i)
  255.                         addItemBow(currentNPCs[i][1], currentNPCs[i][2])
  256.                         table.remove(NPCs, i)
  257.                         table.insert(NPCs, i, {0, 0, "dumb", false})
  258.                         table.remove(currentNPCs, i)
  259.                         table.insert(NPCs, i, {0, 0, "dumb", false})
  260.                         table.remove(arrow, b)
  261.                         table.insert(arrow, b, {0, 0, "up", 0, false})
  262.                         table.remove(currentArrow, b)
  263.                         table.insert(currentArrow, b, {0, 0, "up", 0, false})
  264.                         deleteNPC = true
  265.                         tmpDrawX = tmpDrawX - 1
  266.                     end
  267.                     end
  268.                 end
  269.             end
  270.             if NPCUsed == 0 then
  271.                 table.remove(NPCs, i)
  272.                 table.insert(NPCs, i, {drawX, drawY, "dumb", true, 1})
  273.             end
  274.             drawX = drawX - sceneRight
  275.             drawY = drawY - sceneDown
  276.             table.remove(currentNPCs, i)
  277.             table.insert(currentNPCs, i, {drawX, drawY, NPCType})
  278.             end
  279.         end
  280.     end
  281.     for i=1,arrowCount do
  282.         if arrowCount > 0 then
  283.             if arrowMove == 1 then
  284.             drawX = arrow[i][1]
  285.             drawY = arrow[i][2]
  286.             arrowType = arrow[i][3]
  287.             arrowPos = arrow[i][4]
  288.             spAr = arrow[i][5]
  289.             arrowUsed = arrow[i][6]
  290.             if spAr then
  291.             if arrowUsed == 1 then
  292.                 drawX = currentArrow[i][1]
  293.                 drawY = currentArrow[i][2]
  294.             end
  295.             term.setCursorPos(drawX, drawY)
  296.             term.write(" ")
  297.             if arrowType == "up" then
  298.                 drawY = drawY - arrowPos
  299.                 drawX = x
  300.                 term.setCursorPos(drawX, drawY)
  301.                 term.write("|")
  302.                 arrowPos = arrowPos + 1
  303.             elseif arrowType == "down" then
  304.                 drawY = drawY + arrowPos
  305.                 term.setCursorPos(drawX, drawY)
  306.                 term.write("|")
  307.                 arrowPos = arrowPos + 1
  308.             elseif arrowType == "left" then
  309.                 drawX = drawX - arrowPos
  310.                 term.setCursorPos(drawX, drawY)
  311.                 term.write("-")
  312.                 arrowPos = arrowPos + 1
  313.             elseif arrowType == "right" then
  314.                 drawX = drawX + arrowPos
  315.                 term.setCursorPos(drawX, drawY)
  316.                 term.write("-")
  317.                 arrowPos = arrowPos + 1
  318.             end
  319.             if arrowPos > 15 then
  320.                 table.remove(arrow, i)
  321.                 table.insert(arrow, i, {0, 0, "up", 0, false})
  322.                 table.remove(currentArrow, i)
  323.                 table.insert(currentArrow, i, {0, 0, "up", 0, false})
  324.             end
  325.             if arrowUsed == 0 then
  326.                 table.remove(arrow, i)
  327.                 table.insert(arrow, i, {drawX, drawY, arrowType, arrowPos, spAr, 1})
  328.             end
  329.             table.remove(currentArrow, i)
  330.             table.insert(currentArrow, i, {drawX, drawY, arrowType, arrowPos})
  331.             if NPCCount > 0 then
  332.                 for b=1,NPCCount do
  333.                     if currentNPCs[b][5] == 1 then
  334.                     if drawX == currentNPCs[b][1] and
  335.                     drawY == currentNPCs[b][2] then
  336.                         tmpNPCX = currentNPCs[b][1]
  337.                         tmpNPCY = currentNPCs[b][2]
  338.                         addItemBow(currentNPCs[b][1], currentNPCs[b][2])
  339.                         addDying(drawX, drawY, "bow", b)
  340.                         table.remove(NPCs, b)
  341.                         table.insert(NPCs, b, {0, 0, "dumb", false})
  342.                         table.remove(currentNPCs, b)
  343.                         table.insert(NPCs, b, {0, 0, "dumb", false})
  344.                         table.remove(arrow, i)
  345.                         table.insert(arrow, i, {0, 0, "up", 0, false})
  346.                         table.remove(currentArrow, i)
  347.                         table.insert(currentArrow, i, {0, 0, "up", 0, false})
  348.                     end
  349.                     end
  350.                 end
  351.             end
  352.             end
  353.             else
  354.             arrowMove = 1
  355.             end
  356.         end
  357.     end
  358.     for i=1, dyingCount do
  359.         ID = dying[i][7]
  360.         drawX = dying[i][1]
  361.         drawY = dying[i][2]
  362.         animFrame = dying[i][3]
  363.         dExists = dying[i][4]
  364.         used = dying[i][5]
  365.         drop = dying[i][6]
  366.         if used == 0 then
  367.             table.remove(dying, i)
  368.             table.insert(dying, i, {drawX, drawY, animFrame, dExists, 1})
  369.         end
  370.         if dExists == "exists" then
  371.             if used == 1 then
  372.                 drawX = currentDying[i][1]
  373.                 drawY = currentDying[i][2]
  374.                 animFrame = currentDying[i][3]
  375.             end
  376.             drawX = drawX + sceneRight
  377.             drawY = drawY + sceneDown
  378.             term.setCursorPos(drawX, drawY)
  379.             term.write("@")
  380.             animFrame = animFrame - 1
  381.             if animFrame == 1 then
  382.                 if drop == "bow" then
  383.                     addItemBow(drawX, drawY)
  384.                 end
  385.             end
  386.             if animFrame <= 0 then
  387.                 if drop == "bow" then
  388.                     addItemBow(drawX, drawY)
  389.                 end
  390.                 table.remove(dying, i)
  391.                 table.insert(dying, i, {drawX, drawY, animFrame, "deleted", 1})
  392.                 table.remove(currentDying, i)
  393.                 table.insert(currentDying, i, {drawX, drawY, animFrame, "deleted", 0})
  394.             end
  395.             drawX = drawX - sceneRight
  396.             drawY = drawY - sceneDown
  397.             table.remove(currentDying, i)
  398.             table.insert(currentDying, i, {drawX, drawY, animFrame, dExists, 0})
  399.         end
  400.     end
  401. end
  402.  
  403. function update()
  404.     drawScene()
  405.     term.setCursorPos(x, y)
  406.     if rot == "up" then
  407.         term.write("^")
  408.     elseif rot == "left" then
  409.         term.write("<")
  410.     elseif rot == "down" then
  411.         term.write("V")
  412.     elseif rot == "right" then
  413.         term.write(">")
  414.     end
  415. end
  416. function addLine(bX, bY, dir, l)
  417.     tmpLX = 0
  418.     tmpLY = 0
  419.     for i=1,l do
  420.         bX = bX + tmpLX
  421.         bY = bY + tmpLY
  422.         addBlock(bX, bY)
  423.         if dir == "right" then
  424.             tmpLX = 1
  425.         elseif dir == "up" then
  426.             tmpLY = -1
  427.         elseif dir == "left" then
  428.             tmpLX = -1
  429.         elseif diir == "down" then
  430.             tmpLY = 1
  431.         end
  432.     end
  433.     update()
  434. end
  435. function addDoor(dX, dY, level)
  436.     table.insert(doors, {dX, dY, level})
  437.     doorsCount = doorsCount + 1
  438. end
  439. function addNPC(NPCX, NPCY, aiType)
  440.     table.insert(NPCs, {NPCX, NPCY, aiType, true, 0})
  441.     NPCCount = NPCCount + 1
  442. end
  443. function addItemBow(BX, BY)
  444.     table.insert(bow, {BX, BY, "exists"})
  445.     bowCount = bowCount + 1
  446. end
  447. function addDying(dX, dY, drop, ID)
  448.     table.insert(dying, {dX, dY, 50, "exists", 0, drop, ID})
  449.     dyingCount = dyingCount + 1
  450. end
  451. function createArrow()
  452.     if rot == "up" then
  453.         tmpAX = x
  454.         tmpAY = y - 1
  455.         table.insert(arrow, {tmpAX, tmpAY, "up", 0, true, 0})
  456.     elseif rot == "down" then
  457.         tmpAX = x
  458.         tmpAY = y + 1
  459.         table.insert(arrow, {tmpAX, tmpAY, "down", 0, true, 0})
  460.     elseif rot == "left" then
  461.         tmpAX = x - 1
  462.         tmpAY = y
  463.         table.insert(arrow, {tmpAX, tmpAY, "left", 0, true, 0})
  464.     elseif rot == "right" then
  465.         tmpAX = x + 1
  466.         tmpAY = y
  467.         table.insert(arrow, {tmpAX, tmpAY, "right", 0, true, 0})
  468.     end
  469.     arrowCount = arrowCount + 1
  470. end
  471. -- Level editor start
  472. function openWorld()
  473. addLine(1, 6, "up", 5)
  474. addLine(2, 2, "right", 5)
  475. addLine(7, 6, "up", 5)
  476. addBlock(1, 6)
  477. addBlock(2, 6)
  478. addBlock(3, 6)
  479. addBlock(5, 6)
  480. addBlock(6, 6)
  481. addDoor(4, 6, "test")
  482. addBlock(7, 6)
  483. addNPC(10, 10, "dumb")
  484. if changeMainWorldCors then
  485.     x = mainWorldX
  486.     y = mainWorldY
  487. end
  488. update()
  489. end
  490. function createLevel(levelCreate)
  491. if levelCreate == "test" then
  492.     addLine(1, 10, "up", 9)
  493.     addLine(2, 2, "right", 10)
  494.     addLine(12, 10, "up", 9)
  495.     addBlock(2, 10)
  496.     addBlock(3, 10)
  497.     addBlock(4, 10)
  498.     addDoor(5, 10, "openWorld")
  499.     addLine(6, 10, "right", 6)
  500.     addItemBow(6, 6)
  501.     x = 5
  502.     y = 9
  503.     changeMainWorldCors = true
  504.     mainWorldX = 4
  505.     mainWorldY = 7
  506. end
  507. -- Level editor end
  508. if levelCreate == "openWorld" then
  509.     openWorld()
  510. end
  511. end
  512. openWorld()
  513. updateTimer = os.startTimer(rate)
  514. repeat
  515. event, a, b = os.pullEvent()
  516. if a == 203 then
  517.     tmpRot = rot
  518.     rot = "left"
  519.     clear()
  520.     if move() then
  521.         if x > tmpMinus then
  522.             x = x - 1
  523.         else
  524.             sceneRight = sceneRight + 1
  525.         end
  526.     else
  527.         rot = tmpRot
  528.     end
  529. elseif a == 205 then
  530.     tmpRot = rot
  531.     rot = "right"
  532.     clear()
  533.     if move() then
  534.         if x < borderRight then
  535.             x = x + 1
  536.         else
  537.             sceneRight = sceneRight - 1
  538.         end
  539.     else
  540.         rot = tmpRot
  541.     end
  542. elseif a == 208 then
  543.     tmpRot = rot
  544.     rot = "down"
  545.     if move() then
  546.         if y < 17 then
  547.             y = y + 1
  548.         else
  549.             sceneDown = sceneDown - 1
  550.         end
  551.     else
  552.         rot = tmpRot
  553.     end
  554. elseif a == 200 then -- if you pressed up then
  555.     tmpRot = rot
  556.     rot = "up"
  557.     if move() then
  558.         if y > 3 then
  559.             y = y - 1
  560.         else
  561.             sceneDown = sceneDown + 1
  562.         end
  563.     else
  564.         rot = tmpRot
  565.     end
  566. elseif a == 57 and playerHasBow then
  567.         createArrow()
  568. end -- close if statement
  569. if event == "timer" then
  570.     updateTimer = os.startTimer(rate)
  571.     update()
  572. end
  573. if event == "redstone" then
  574.     update()
  575. end
  576. until stop == true -- until I want the program to stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement