Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- local map = {} -- 3x3
- local inventory = {
- food = 0,
- water = 0,
- poison = 0,
- }
- local keybinds = {
- drink = keys.t,
- eat = keys.y,
- poison = keys.u
- }
- local upgrades = {}
- local shop_items = { -- {name = name, price = price}
- {name = "Food", price = 10, id = "food"},
- {name = "Water", price = 25, id = "water"},
- {name = "Alcohol", price = 500, id = "poison"}
- }
- local coins = 25
- local food = 100 --100% = 100 units
- local water = 10 --100% = 10 units.
- local wwpt = 0.5 --Water waste per Turn
- local fwpt = 5 --Water waste per Turn
- local rarity = { --1 in rarity^rarity room. (it's random to it's more like a chance)
- food = 3,
- water = 2,
- coins = 5
- }
- local plrS = "P"
- local shpS = "S"
- local gameOver = false
- local reason = "Something went wrong"
- function possibleToMove(direction)
- if direction == "top" then
- return map[2][2].top
- elseif direction == "right" then
- return map[2][3].left
- elseif direction == "down" then
- return map[3][2].top
- elseif direction == "left" then
- return map[2][2].left
- end
- end
- function init()
- for i=1,3,1 do
- table.insert(map, {})
- for y=1,3,1 do
- table.insert(map[i], nil)
- end
- end
- end
- function generateMap()
- for y=1,3,1 do
- for x=1,3,1 do
- if map[y][x] == nil then
- local variant = math.random(1,3)
- local type = math.random(1,30)
- local inventory = {}
- if type == math.random(1,30) then
- type = "shop"
- else
- type = "room"
- end
- local wC1,wC2 = math.random(1,rarity.water),math.random(1,rarity.water)
- local fC1,fC2 = math.random(1,rarity.food),math.random(1,rarity.food)
- local cC1,cC2 = math.random(1,rarity.coins),math.random(1,rarity.coins)
- if wC1 == wC2 then
- table.insert(inventory,"water")
- end
- if fC1 == fC2 then
- table.insert(inventory,"food")
- end
- if cC1 == cC2 then
- table.insert(inventory,"coin")
- end
- local form = {top = false, left = false, inventory = inventory, type = type}
- if variant == 1 then
- form.top = true
- map[y][x] = form
- elseif variant == 2 then
- form.left = true
- map[y][x] = form
- elseif variant == 3 then
- form.left = true
- form.top = true
- map[y][x] = form
- end
- end
- end
- end
- end
- function shiftMap(direction)
- if direction == "top" then
- for y=2,3,1 do
- for x=1,3,1 do
- map[y][x] = map[y-1][x]
- end
- end
- for x=1,3,1 do
- map[1][x] = nil
- end
- elseif direction == "right" then
- for y=1,3,1 do
- for x=2,3,1 do
- map[y][x] = map[y][x+1]
- end
- end
- for y=1,3,1 do
- map[y][3] = nil
- end
- elseif direction == "down" then
- for y=2,1,-1 do
- for x=1,3,1 do
- map[y][x] = map[y+1][x]
- end
- end
- for x=1,3,1 do
- map[3][x] = nil
- end
- elseif direction == "left" then
- for y=1,3,1 do
- for x=2,1,-1 do
- map[y][x] = map[y][x-1]
- end
- end
- for y=1,3,1 do
- map[y][1] = nil
- end
- end
- end
- init()
- generateMap()
- while true do
- local function draw()
- term.setBackgroundColor(colors.black)
- term.clear()
- --Map draw logic
- local offset = 3
- for y=1,3,1 do
- for x=1,3,1 do
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(x*2-1+offset,y*2-1+offset)
- if map[y][x].top == true then
- term.write("# ")
- else
- term.write("##")
- end
- term.setCursorPos(x*2-1+offset,y*2+offset)
- local s = ""
- if y == 2 and x == 2 then
- s = plrS
- end
- if map[y][x].type == "shop" then
- s = shpS
- end
- if map[y][x].left == true then
- term.write(" "..s)
- else
- term.write("#"..s)
- end
- end
- end
- -- Coins, Food, Water level
- --FOOD
- term.setBackgroundColor(colors.brown)
- term.setCursorPos(1,h)
- term.clearLine()
- term.setTextColor(colors.white)
- term.write("Food level: "..food)
- --Water
- term.setBackgroundColor(colors.cyan)
- term.setCursorPos(1,h-1)
- term.clearLine()
- term.setTextColor(colors.white)
- term.write("Water level: "..water*10)
- --Coins
- term.setBackgroundColor(colors.orange)
- term.setCursorPos(1,h-2)
- term.clearLine()
- term.setTextColor(colors.white)
- term.write("Coins: "..coins)
- --Shop menu display
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if map[2][2].type == "shop" then
- for i=1,#shop_items,1 do
- term.setCursorPos(w-7,i*2-1)
- term.write(shop_items[i].name)
- term.setCursorPos(w-7,i*2)
- term.write(shop_items[i].price)
- end
- end
- --Inventory menu display
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(w-7,8)
- term.write("Food")
- term.setCursorPos(w-7,9)
- term.write(inventory.food.."x")
- term.setCursorPos(w-7,10)
- term.write("Water")
- term.setCursorPos(w-7,11)
- term.write(inventory.water.."x")
- term.setCursorPos(w-7,12)
- term.write("Alcohol")
- term.setCursorPos(w-7,13)
- term.write(inventory.poison.."x")
- --Room inventory display
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if #map[2][2].inventory > 0 then
- for i = 1,#map[2][2].inventory,1 do
- term.setCursorPos(w-7,14+i)
- term.write(map[2][2].inventory[i])
- end
- end
- end
- draw()
- local e,a,d,b,c,x = os.pullEvent()
- if e == "key_up" then
- local key = a
- if key == keys.left or key == keys.a then
- if possibleToMove("left") then
- shiftMap("left")
- generateMap()
- water = water - wwpt
- food = food - fwpt
- end
- end
- if key == keys.up or key == keys.w then
- if possibleToMove("top") then
- shiftMap("top")
- generateMap()
- water = water - wwpt
- food = food - fwpt
- end
- end
- if key == keys.right or key == keys.d then
- if possibleToMove("right") then
- shiftMap("right")
- generateMap()
- water = water - wwpt
- food = food - fwpt
- end
- end
- if key == keys.down or key == keys.s then
- if possibleToMove("down") then
- shiftMap("down")
- generateMap()
- water = water - wwpt
- food = food - fwpt
- end
- end
- if key == keybinds.eat then
- if inventory.food > 0 then
- inventory.food = inventory.food - 1
- food = food + 15
- if food > 110 then
- food = 110
- end
- end
- end
- if key == keybinds.drink then
- if inventory.water > 0 then
- inventory.water = inventory.water - 1
- water = water + 1
- if water > 15 then
- water = 15
- end
- end
- end
- if key == keybinds.poison then
- if inventory.poison > 0 then
- inventory.poison = inventory.poison - 1
- reason = "You drinked alcohol!"
- gameOver = true
- end
- end
- end
- if e == "mouse_click" then
- local button,x,y = a,d,b
- if x >= w-7 then
- --Shop logic
- if map[2][2].type == "shop" then
- if y >= 1 and y <= 6 then
- local item = math.ceil(y/2)
- if coins >= shop_items[item].price then
- inventory[shop_items[item].id] = inventory[shop_items[item].id] + 1
- coins = coins - shop_items[item].price
- end
- end
- end
- -- Inventory logic
- if y >= 8 and y <= 9 then
- if inventory.food > 0 then
- inventory.food = inventory.food - 1
- food = food + 15
- if food > 110 then
- food = 110
- end
- end
- end
- if y >= 10 and y <= 11 then
- if inventory.water > 0 then
- inventory.water = inventory.water - 1
- water = water + 1
- if water > 15 then
- water = 15
- end
- end
- end
- if y >= 12 and y <= 13 then
- if inventory.poison > 0 then
- inventory.poison = inventory.poison - 1
- reason = "You drinked alcohol!"
- gameOver = true
- end
- end
- --Room inventory logic
- if y > 14 and y <= 17 then
- local item = y-14
- if #map[2][2].inventory >= item then
- if map[2][2].inventory[item] ~= "coin" then
- inventory[map[2][2].inventory[item]] = inventory[map[2][2].inventory[item]] + 1
- else
- coins = coins + math.random(1,6)*5
- end
- table.remove(map[2][2].inventory,item)
- end
- end
- end
- end
- if food < 0 or water < 0 then
- reason = "You ran out of food/water"
- gameOver = true
- break
- end
- if gameOver then
- break
- end
- if coins > 666 then
- reason = "You reached 666 coins!"
- break
- end
- end
- if gameOver then
- term.setBackgroundColor(colors.red)
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos((w-string.len("GAME OVER!"))/2,h/2)
- term.write("GAME OVER!")
- term.setTextColor(colors.lightGray)
- term.setCursorPos((w-string.len(reason))/2,h/2+1)
- term.write(reason)
- sleep(3)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- else
- term.setBackgroundColor(colors.green)
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos((w-string.len("YOU WON!"))/2,h/2)
- term.write("YOU WON!")
- term.setTextColor(colors.lightGray)
- term.setCursorPos((w-string.len(reason))/2,h/2+1)
- term.write(reason)
- sleep(3)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
Advertisement
Add Comment
Please, Sign In to add comment