Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- if fs.exists("RPGConfig") then
- config = fs.open("RPGConfig", "r")
- rate = config:readLine();
- if rate ~= "rate=" then
- term.clear()
- term.setCursorPos(1, 1)
- print([[Error: line one of the
- config file should be "rate=".
- ]])
- sleep(5)
- error()
- end
- rate = config.readLine()
- config.close()
- else
- textutils.slowPrint("Error: could not open config file. ")
- textutils.slowPrint("Creating a config file...")
- config = fs.open("RPGConfig", "w")
- config.write([[rate=
- 0.0001
- ]])
- config.close()
- sleep(1)
- rate = 0.001
- end
- scrX, scrY = term.getSize()
- tmpPosX = scrX / 2
- tmpPosY = scrY / 2
- tmpMinus = scrX / 8
- tmpMinus2 = math.floor(tmpMinus)
- borderRight = scrX - tmpMinus2
- tmpMinusY = scrY / 50
- tmpMinusY2 = math.floor(tmpMinusY)
- borderDown = scrY - tmpMinusY2
- x = tmpPosX
- y = tmpPosY
- sceneDown = 0
- sceneRight = 0
- playerHasBow = false
- blocksCount = 0
- currentCors = {}
- rot = "up"
- blocks = {}
- doors = {}
- currentDoors = {}
- doorsCount = 0
- NPCs = {}
- currentNPCs = {}
- NPCCount = 0
- bow = {}
- currentBow = {}
- bowCount = 0
- openWorld = true
- bowPickup = false
- arrow = {}
- currentArrow = {}
- arrowCount = 0
- changeMainWorldCors = false
- deleteNPC = false
- arrowMove = 0
- dying = {}
- currentDying = {}
- dyingCount = 0
- function addBlock(blX, blY)
- blocksCountTmp = blocksCount + 1
- table.insert(blocks, blocksCountTmp, {blX, blY})
- blocksCount = blocksCount + 1
- end
- function clear()
- term.setCursorPos(x, y)
- term.write(" ")
- end
- function move()
- wall = false
- door = false
- bowPickup = false
- if rot == "up" then
- tmpX = x
- tmpY = y - 1
- elseif rot == "left" then
- tmpX = x - 1
- tmpY = y
- elseif rot == "down" then
- tmpX = x
- tmpY = y + 1
- elseif rot == "right" then
- tmpX = x + 1
- tmpY = y
- end
- for i=1, blocksCount do
- if currentCors[i][1] == tmpX and
- currentCors[i][2] == tmpY then
- wall = true
- end
- end
- for i=1, doorsCount do
- if currentDoors[i][1] == tmpX and
- currentDoors[i][2] == tmpY then
- door = true
- tmpLevN = currentDoors[i][3]
- end
- end
- bowPickUp = false
- for i=1, bowCount do
- if currentBow[i][1] == tmpX and
- currentBow[i][2] == tmpY then
- bowPickUp = true
- bowId = i
- tmpLevN = currentDoors[i][3]
- end
- end
- if bowPickUp == true then
- playerHasBow = true
- table.remove(currentBow, bowId)
- table.insert(currentBow, bowId, {0, 0, "deleted"})
- table.remove(bow, bowId)
- table.insert(bow, {0, 0, bowId, "deleted"})
- end
- if wall == true then
- return false
- else
- if door == false and bowPickUp == false then
- return true
- end
- end
- if bowPickUp then
- playerHasBow = true
- table.remove(currentBow, bowId)
- table.insert(currentBow, bowId, {0, 0, "deleted"})
- return true
- end
- if door then
- loadLevel(tmpLevN)
- end
- end
- function loadLevel(levelName)
- for i=1,blocksCount do
- table.remove(blocks, i)
- end
- for i=1,doorsCount do
- table.remove(doors, i)
- end
- for i=1, NPCCount do
- table.remove(NPCs, i)
- end
- for i=1, bowCount do
- table.remove(bow, i)
- end
- for i=1, arrowCount do
- table.remove(arrow, i)
- table.remove(currentArrow, i)
- end
- x = tmpPosX
- y = tmpPosY
- sceneDown = 0
- sceneRight = 0
- blocksCount = 0
- doorsCount = 0
- NPCCount = 0
- bowCount = 0
- arrowCount = 0
- createLevel(levelName)
- arrow = {}
- for i=1,dyingCount do
- table.remove(dying, i)
- table.remove(currentDying, i)
- end
- currentArrow = {}
- arrowCount = 0
- arrowMove = 0
- dyingCount = 0
- end
- function drawScene()
- term.clear()
- for i=1, blocksCount do
- if blocksCount > 0 then
- drawX = blocks[i][1]
- drawY = blocks[i][2]
- drawX = drawX + sceneRight
- drawY = drawY + sceneDown
- table.insert(currentCors, i, {drawX, drawY, "exists"})
- term.setCursorPos(drawX, drawY)
- term.write("X")
- else
- print("error")
- term.setCursorPos(1, 1)
- term.write(blocksCount)
- end
- end
- for i=1,doorsCount do
- if doorsCount > 0 then
- drawX = doors[i][1]
- drawY = doors[i][2]
- drawLev = doors[i][3]
- drawX = drawX + sceneRight
- drawY = drawY + sceneDown
- table.insert(currentDoors, i, {drawX, drawY, drawLev})
- term.setCursorPos(drawX, drawY)
- term.write("|")
- end
- end
- for i=1,bowCount do
- if bowCount > 0 then
- drawX = bow[i][1]
- drawY = bow[i][2]
- bowExists = bow[i][3]
- drawX = drawX + sceneRight
- drawY = drawY + sceneDown
- if bowExists == "exists" then
- table.insert(currentBow, i, {drawX, drawY})
- term.setCursorPos(drawX, drawY)
- term.write("B")
- end
- end
- end
- for i=1,NPCCount do
- if NPCCount > 0 then
- drawX = NPCs[i][1]
- drawY = NPCs[i][2]
- NPCType = NPCs[i][3]
- spNPC = NPCs[i][4]
- NPCUsed = NPCs[i][5]
- if spNPC then
- if NPCUsed == 1 then
- drawX = currentNPCs[i][1]
- drawY = currentNPCs[i][2]
- end
- drawX = drawX + sceneRight
- drawY = drawY + sceneDown
- tmpDrawX = drawX
- tmpDrawY = drawY
- randNPCMov = math.random(0, 100)
- if NPCType == "dumb" then
- curNPC = "dumb"
- if randNPCMov == 1 then
- drawX = drawX - 1
- elseif randNPCMov == 2 then
- drawX = drawX + 1
- elseif randNPCMov == 3 then
- drawY = drawY - 1
- elseif randNPCMov == 4 then
- drawY = drawY + 1
- end
- end
- term.setCursorPos(drawX, drawY)
- term.write("A")
- if arrowCount > 0 then
- for b=1,arrowCount do
- if arrow[b][6] == 1 then
- if tmpDrawX == currentArrow[b][1] and
- tmpDrawY == currentArrow[b][2] then
- addDying(drawX, drawY, "bow", i)
- addItemBow(currentNPCs[i][1], currentNPCs[i][2])
- table.remove(NPCs, i)
- table.insert(NPCs, i, {0, 0, "dumb", false})
- table.remove(currentNPCs, i)
- table.insert(NPCs, i, {0, 0, "dumb", false})
- table.remove(arrow, b)
- table.insert(arrow, b, {0, 0, "up", 0, false})
- table.remove(currentArrow, b)
- table.insert(currentArrow, b, {0, 0, "up", 0, false})
- deleteNPC = true
- tmpDrawX = tmpDrawX - 1
- end
- end
- end
- end
- if NPCUsed == 0 then
- table.remove(NPCs, i)
- table.insert(NPCs, i, {drawX, drawY, "dumb", true, 1})
- end
- drawX = drawX - sceneRight
- drawY = drawY - sceneDown
- table.remove(currentNPCs, i)
- table.insert(currentNPCs, i, {drawX, drawY, NPCType})
- end
- end
- end
- for i=1,arrowCount do
- if arrowCount > 0 then
- if arrowMove == 1 then
- drawX = arrow[i][1]
- drawY = arrow[i][2]
- arrowType = arrow[i][3]
- arrowPos = arrow[i][4]
- spAr = arrow[i][5]
- arrowUsed = arrow[i][6]
- if spAr then
- if arrowUsed == 1 then
- drawX = currentArrow[i][1]
- drawY = currentArrow[i][2]
- end
- term.setCursorPos(drawX, drawY)
- term.write(" ")
- if arrowType == "up" then
- drawY = drawY - arrowPos
- drawX = x
- term.setCursorPos(drawX, drawY)
- term.write("|")
- arrowPos = arrowPos + 1
- elseif arrowType == "down" then
- drawY = drawY + arrowPos
- term.setCursorPos(drawX, drawY)
- term.write("|")
- arrowPos = arrowPos + 1
- elseif arrowType == "left" then
- drawX = drawX - arrowPos
- term.setCursorPos(drawX, drawY)
- term.write("-")
- arrowPos = arrowPos + 1
- elseif arrowType == "right" then
- drawX = drawX + arrowPos
- term.setCursorPos(drawX, drawY)
- term.write("-")
- arrowPos = arrowPos + 1
- end
- if arrowPos > 15 then
- table.remove(arrow, i)
- table.insert(arrow, i, {0, 0, "up", 0, false})
- table.remove(currentArrow, i)
- table.insert(currentArrow, i, {0, 0, "up", 0, false})
- end
- if arrowUsed == 0 then
- table.remove(arrow, i)
- table.insert(arrow, i, {drawX, drawY, arrowType, arrowPos, spAr, 1})
- end
- table.remove(currentArrow, i)
- table.insert(currentArrow, i, {drawX, drawY, arrowType, arrowPos})
- if NPCCount > 0 then
- for b=1,NPCCount do
- if currentNPCs[b][5] == 1 then
- if drawX == currentNPCs[b][1] and
- drawY == currentNPCs[b][2] then
- tmpNPCX = currentNPCs[b][1]
- tmpNPCY = currentNPCs[b][2]
- addItemBow(currentNPCs[b][1], currentNPCs[b][2])
- addDying(drawX, drawY, "bow", b)
- table.remove(NPCs, b)
- table.insert(NPCs, b, {0, 0, "dumb", false})
- table.remove(currentNPCs, b)
- table.insert(NPCs, b, {0, 0, "dumb", false})
- table.remove(arrow, i)
- table.insert(arrow, i, {0, 0, "up", 0, false})
- table.remove(currentArrow, i)
- table.insert(currentArrow, i, {0, 0, "up", 0, false})
- end
- end
- end
- end
- end
- else
- arrowMove = 1
- end
- end
- end
- for i=1, dyingCount do
- ID = dying[i][7]
- drawX = dying[i][1]
- drawY = dying[i][2]
- animFrame = dying[i][3]
- dExists = dying[i][4]
- used = dying[i][5]
- drop = dying[i][6]
- if used == 0 then
- table.remove(dying, i)
- table.insert(dying, i, {drawX, drawY, animFrame, dExists, 1})
- end
- if dExists == "exists" then
- if used == 1 then
- drawX = currentDying[i][1]
- drawY = currentDying[i][2]
- animFrame = currentDying[i][3]
- end
- drawX = drawX + sceneRight
- drawY = drawY + sceneDown
- term.setCursorPos(drawX, drawY)
- term.write("@")
- animFrame = animFrame - 1
- if animFrame == 1 then
- if drop == "bow" then
- addItemBow(drawX, drawY)
- end
- end
- if animFrame <= 0 then
- if drop == "bow" then
- addItemBow(drawX, drawY)
- end
- table.remove(dying, i)
- table.insert(dying, i, {drawX, drawY, animFrame, "deleted", 1})
- table.remove(currentDying, i)
- table.insert(currentDying, i, {drawX, drawY, animFrame, "deleted", 0})
- end
- drawX = drawX - sceneRight
- drawY = drawY - sceneDown
- table.remove(currentDying, i)
- table.insert(currentDying, i, {drawX, drawY, animFrame, dExists, 0})
- end
- end
- end
- function update()
- drawScene()
- term.setCursorPos(x, y)
- if rot == "up" then
- term.write("^")
- elseif rot == "left" then
- term.write("<")
- elseif rot == "down" then
- term.write("V")
- elseif rot == "right" then
- term.write(">")
- end
- end
- function addLine(bX, bY, dir, l)
- tmpLX = 0
- tmpLY = 0
- for i=1,l do
- bX = bX + tmpLX
- bY = bY + tmpLY
- addBlock(bX, bY)
- if dir == "right" then
- tmpLX = 1
- elseif dir == "up" then
- tmpLY = -1
- elseif dir == "left" then
- tmpLX = -1
- elseif diir == "down" then
- tmpLY = 1
- end
- end
- update()
- end
- function addDoor(dX, dY, level)
- table.insert(doors, {dX, dY, level})
- doorsCount = doorsCount + 1
- end
- function addNPC(NPCX, NPCY, aiType)
- table.insert(NPCs, {NPCX, NPCY, aiType, true, 0})
- NPCCount = NPCCount + 1
- end
- function addItemBow(BX, BY)
- table.insert(bow, {BX, BY, "exists"})
- bowCount = bowCount + 1
- end
- function addDying(dX, dY, drop, ID)
- table.insert(dying, {dX, dY, 50, "exists", 0, drop, ID})
- dyingCount = dyingCount + 1
- end
- function createArrow()
- if rot == "up" then
- tmpAX = x
- tmpAY = y - 1
- table.insert(arrow, {tmpAX, tmpAY, "up", 0, true, 0})
- elseif rot == "down" then
- tmpAX = x
- tmpAY = y + 1
- table.insert(arrow, {tmpAX, tmpAY, "down", 0, true, 0})
- elseif rot == "left" then
- tmpAX = x - 1
- tmpAY = y
- table.insert(arrow, {tmpAX, tmpAY, "left", 0, true, 0})
- elseif rot == "right" then
- tmpAX = x + 1
- tmpAY = y
- table.insert(arrow, {tmpAX, tmpAY, "right", 0, true, 0})
- end
- arrowCount = arrowCount + 1
- end
- -- Level editor start
- function openWorld()
- addLine(1, 6, "up", 5)
- addLine(2, 2, "right", 5)
- addLine(7, 6, "up", 5)
- addBlock(1, 6)
- addBlock(2, 6)
- addBlock(3, 6)
- addBlock(5, 6)
- addBlock(6, 6)
- addDoor(4, 6, "test")
- addBlock(7, 6)
- addNPC(10, 10, "dumb")
- if changeMainWorldCors then
- x = mainWorldX
- y = mainWorldY
- end
- update()
- end
- function createLevel(levelCreate)
- if levelCreate == "test" then
- addLine(1, 10, "up", 9)
- addLine(2, 2, "right", 10)
- addLine(12, 10, "up", 9)
- addBlock(2, 10)
- addBlock(3, 10)
- addBlock(4, 10)
- addDoor(5, 10, "openWorld")
- addLine(6, 10, "right", 6)
- addItemBow(6, 6)
- x = 5
- y = 9
- changeMainWorldCors = true
- mainWorldX = 4
- mainWorldY = 7
- end
- -- Level editor end
- if levelCreate == "openWorld" then
- openWorld()
- end
- end
- openWorld()
- updateTimer = os.startTimer(rate)
- repeat
- event, a, b = os.pullEvent()
- if a == 203 then
- tmpRot = rot
- rot = "left"
- clear()
- if move() then
- if x > tmpMinus then
- x = x - 1
- else
- sceneRight = sceneRight + 1
- end
- else
- rot = tmpRot
- end
- elseif a == 205 then
- tmpRot = rot
- rot = "right"
- clear()
- if move() then
- if x < borderRight then
- x = x + 1
- else
- sceneRight = sceneRight - 1
- end
- else
- rot = tmpRot
- end
- elseif a == 208 then
- tmpRot = rot
- rot = "down"
- if move() then
- if y < 17 then
- y = y + 1
- else
- sceneDown = sceneDown - 1
- end
- else
- rot = tmpRot
- end
- elseif a == 200 then -- if you pressed up then
- tmpRot = rot
- rot = "up"
- if move() then
- if y > 3 then
- y = y - 1
- else
- sceneDown = sceneDown + 1
- end
- else
- rot = tmpRot
- end
- elseif a == 57 and playerHasBow then
- createArrow()
- end -- close if statement
- if event == "timer" then
- updateTimer = os.startTimer(rate)
- update()
- end
- if event == "redstone" then
- update()
- end
- until stop == true -- until I want the program to stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement