This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jordanlm98's profile picture

I'm New To Lua, And I Want To Make A Hotel Booking Computer...

Started by jordanlm98, 27 February 2012 - 07:37 PM
jordanlm98 #1
Posted 27 February 2012 - 08:37 PM
At the title says.

I'm not sure if I did everything correctly, as it is the first time I've ever coded using lua(I know java well).

This is my code:

Spoiler

availablerooms = {r1=nil,r2=nil,r3=nil,r4=nil,r5=nil,r6=nil,r7=nil,8=nil,9=nil,r10=nil,r11=nil,r12=nil}
term.clear()
textutils.slowPrint( "Welcome to the hotel booking network! Please enter the password." )
hotelpassword()
function hotelpassword()
t = io.read()
if t == "password" then
  doyouwanttobook()
else
  print( "That password is incorrect. Try again." )
  hotelpassword()
end
end
function doyouwanttobook()
term.clear()
print("Welcome!")
print("Would you like to book a room? (Y/N)")
bookinganswer = io.read()
if bookinganswer == "Y" then
  bookroom()
else
  print("Would you like to list currently available rooms? (Y/N)")
  listinganswer = io.read()
  if listinganswer == "Y" then
   listavailable()
  else
   print("Returning to main menu...")
   sleep(3)
   term.clear()
   textutils.slowPrint( "Welcome to the hotel network! Please enter the password." )
   hotelpassword()
  end
end
end
function bookroom()
term.clear()
print("Welcome to room booking!")
print("Enter the room number(Put 'r' before the number(e.g. r1). Rooms 6-11 are large. Penthouse is r12): ")
wantedroom = io.read()
if availablerooms[wantedroom]==nil then
  availablerooms[wantedroom]=b
  sleep(2)
  print("Room has been booked! There are " + # bookedrooms + " booked rooms")
else
  print("That room isn't available, please pick another room.")
  bookroom()
end
end
function listavailable()
print("Number of available rooms: " + # availablerooms ".")
end


I'd really like some help.

1) I don't know exactly what file extension to use, do I use .lua?
2) Where do I put my file?
3) Did I make any major mistakes?
Liraal #2
Posted 27 February 2012 - 08:58 PM
your table declaration's weird… basically you just declare tables like tab={nil,nil,nil,whatever}
second: io.read() would return a string not a number. use tostring(io.read()) or simply tostring(read())
third: availablerooms[wantedroom]=b b is undeclared, thus equal nil.
fourth: table length operator is set like #tab not # tab. idk if that would work, but no space=better
fifth: save as normal file (no extensions whatsoever) in .minecraft/saves/savename/computer/computerID/ folder
that's about it. just dont know what the bookedrooms table looks like. you never declared it.

But if that's your first try in CC, you did certainly do better than me :P/>/>
jordanlm98 #3
Posted 27 February 2012 - 09:27 PM
your table declaration's weird… basically you just declare tables like tab={nil,nil,nil,whatever}
second: io.read() would return a string not a number. use tostring(io.read()) or simply tostring(read())
third: availablerooms[wantedroom]=b b is undeclared, thus equal nil.
fourth: table length operator is set like #tab not # tab. idk if that would work, but no space=better
fifth: save as normal file (no extensions whatsoever) in .minecraft/saves/savename/computer/computerID/ folder
that's about it. just dont know what the bookedrooms table looks like. you never declared it.

But if that's your first try in CC, you did certainly do better than me :P/>/>

It is my very first try :)/>/>

I remade the code while I was waiting for a reply, and I applied some of the things you told me, so this is my new code:


term.clear()
rooms = {r1=nil,r2=nil,r3=nil,r4=nil,r5=nil,r6=nil,r7=nil,r8=nil,r9=nil,r10=nil,r11=nil,r12=nil}
print("Hotel Computer Network.")
print("Do you want to book or unbook?")
bookorunbook = tostring(io.read())
if bookorunbook == "book" then
bookroom()
elseif bookorunbook == "unbook" then
unbookroom()
end
function bookroom()
print("Please enter the room id of the room you want to book.")
roomid = tostring(io.read())
if rooms[roomid]==nil then
rooms[roomid]="b"
else
print("That room is already booked.")
end
end
function unbookroom()
print("Please enter the room id of the room you want to unbook.")
unbookid = tostring(io.read())
if rooms[unbookid]=="b" then
rooms[unbookid]=nil
else
print("That room is already booked")
end
end


I'm really hoping I did everything right.
Liraal #4
Posted 27 February 2012 - 09:31 PM
the last print is a bit weird, if you want to unbook an unbooked room it should say this room's unbooked, i think

and why do you declare table fields like r1=nil? write only nil i think. and change tostring into tonumber. I dont think a table can be accessed like that.
Advert #5
Posted 27 February 2012 - 09:51 PM
The table declaration is valid; but assigning nil to a variable is basically the same as unassigning it; nil is a non-value (meaning that, if a varaible is nil, it isn't assigned).

I would recommend assigning it to "" instead; then, you can use a for loop to see if it exists:


rooms = {} 
rooms = {r1=nil,r2=nil,r3=nil,r4=nil,r5=nil,r6=nil,r7=nil,r8=nil,r9=nil,r10=nil,r11=nil,r12=nil} -- Is the exact same as above!

for k, v in pairs(rooms) do
 print("Room: ", k, " Value: ", v)
end
-- Above will not print anything!
rooms = {r1 = "", r2 = "", r3 = "", r4 = nil, r5 = "",r6 = "", r7 = "" , r8 = "", r9 = "" , r10 = "", r11 = "", r12 = ""}

for k, v in pairs(rooms) do
 print("Room: ", k, " Value: ", v)
end
-- This'll print each of the rooms

In addition, if you do it like that, you could do this:

if rooms[roomid] == "" then -- The room is free!
 ...
elseif rooms[roomid] ~= nil then -- The room is booked!
 ...
else -- The room doesn't exist!
 ...
end

There's lots more fun stuff you can do, so try these things first, and see how they work for you. :P/>/>
jordanlm98 #6
Posted 28 February 2012 - 12:15 PM
The table declaration is valid; but assigning nil to a variable is basically the same as unassigning it; nil is a non-value (meaning that, if a varaible is nil, it isn't assigned).

I would recommend assigning it to "" instead; then, you can use a for loop to see if it exists:


rooms = {}
rooms = {r1=nil,r2=nil,r3=nil,r4=nil,r5=nil,r6=nil,r7=nil,r8=nil,r9=nil,r10=nil,r11=nil,r12=nil} -- Is the exact same as above!

for k, v in pairs(rooms) do
print("Room: ", k, " Value: ", v)
end
-- Above will not print anything!
rooms = {r1 = "", r2 = "", r3 = "", r4 = nil, r5 = "",r6 = "", r7 = "" , r8 = "", r9 = "" , r10 = "", r11 = "", r12 = ""}

for k, v in pairs(rooms) do
print("Room: ", k, " Value: ", v)
end
-- This'll print each of the rooms

In addition, if you do it like that, you could do this:

if rooms[roomid] == "" then -- The room is free!
...
elseif rooms[roomid] ~= nil then -- The room is booked!
...
else -- The room doesn't exist!
...
end

There's lots more fun stuff you can do, so try these things first, and see how they work for you. :(/>/>

I followed what you did and now My program works like a charm :)/>/> Is there an way for me to save booked rooms to a .txt file so that if the computer restarts or something it won't reset what rooms are booked?


And I have another mostly unrelated question: How do I use rednet to make room service system that uses a turtle to deliver things like cakes to rooms…?
Advert #7
Posted 28 February 2012 - 06:13 PM
The table declaration is valid; but assigning nil to a variable is basically the same as unassigning it; nil is a non-value (meaning that, if a varaible is nil, it isn't assigned).

I would recommend assigning it to "" instead; then, you can use a for loop to see if it exists:


rooms = {}
rooms = {r1=nil,r2=nil,r3=nil,r4=nil,r5=nil,r6=nil,r7=nil,r8=nil,r9=nil,r10=nil,r11=nil,r12=nil} -- Is the exact same as above!

for k, v in pairs(rooms) do
print("Room: ", k, " Value: ", v)
end
-- Above will not print anything!
rooms = {r1 = "", r2 = "", r3 = "", r4 = nil, r5 = "",r6 = "", r7 = "" , r8 = "", r9 = "" , r10 = "", r11 = "", r12 = ""}

for k, v in pairs(rooms) do
print("Room: ", k, " Value: ", v)
end
-- This'll print each of the rooms

In addition, if you do it like that, you could do this:

if rooms[roomid] == "" then -- The room is free!
...
elseif rooms[roomid] ~= nil then -- The room is booked!
...
else -- The room doesn't exist!
...
end

There's lots more fun stuff you can do, so try these things first, and see how they work for you. :D/>/>

I followed what you did and now My program works like a charm ;)/>/> Is there an way for me to save booked rooms to a .txt file so that if the computer restarts or something it won't reset what rooms are booked?


And I have another mostly unrelated question: How do I use rednet to make room service system that uses a turtle to deliver things like cakes to rooms…?

You could serialize the table, then save it to a file:


local serialized = textutils.serialize(rooms) -- serialize (turn into text form) the room data

local handle = io.open("rooms.dat", "w") -- Open the rooms in WRITE-mode; this will also clear the file
handle:write(serialized) -- Write the data to the file
handle:close() -- Close the file after you're done with it; this is important, because file handles are limited, and if you don't close it, you'll either have to wait until it's closed by the garbage collector, or restart minecraft (if on a server; the server).

-- To read the data:
local handle = io.open("rooms.dat", "r") -- Opens in READ-mode, you can't write to the file; but you can now read it.
local serialized = handle:read("*a") -- Reads 'all' of the file
handle:close() -- Close the file handle, since we're done with it.
rooms = textutils.unserialize(serialized) -- turns the serialized string back into a table




Making a room-service system will be a bit more complicated; but you'd want something like the following:

local xPos, yPos, zPos = 0, 0, 0 -- Keep track of where the turtle is
local facing = 0 -- Keep track of where the turtle is facing
local faces = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}} -- The turtle can face foward, to the left, to the back, to the right, etc.
-- add custom functions to update above when you move here; e.g
function moveForward()
 if turtle.forward() then -- If we move forward
  local face = faces[(facing%4) + 1] -- Modulo the face to be safe, incase it overflows; add 1 because lua defaults at 1, not 0. (4%4 = 0; 4%7 = 3; modulo gives you the remainder after division)
  xPos = xPos + face[0] -- Add the X component of the face to X,
  yPos = yPos + face[1] -- Add the Y component (not using notch's coordinates here, mind you; the idle position of the turtle should be 0,0,0)
  return true -- we moved sucessfully
 end
 return false -- we couldn't move
end
-- Do this for turnleft/turnright, but you'll be adding/subtracting to face instead, and to move up/down you simply add/subtract to zPos, and to go backwards, you do the same you did for forwards, but you subtract the face instead of add it :)/>/>
-- Then, remember to use these fuctions instead of the turtle.forward() etc functions, so you can keep track of your position!
-- You might also want to save this data to a file after every move (including what it's doing, etc), because your turtle could potentially get stuck, if the chunk was unloaded, or if minecraft is restarted).

-- Then, you'll want to do figure out how your turtle can move, so it doesn't break stuff:
-- I'd recommend having a place where you can move up/down safely (basically a 1x1 shaft that goes down, exclusively for the turtle's use); so on a map, it'd look something like this (# denotes blocked space):
-- Floor 0:
--###
--# #
--# # -- Turtle service area here, this could be in the basement

-- Going up, z level 1+, depending on how high your ceiling is:
--###
--# #
--###

-- On the ground of the first floor with rooms in it:
--###
--# #
--# ############# -- 1x1 hole for the turtle to come out of
-- -- Hallway; you'll have to have the turtle move around here without breaking stuff, then into a room to service (you'd want a small turtle hole for that, again).

local floors = {0 = {loading = true}, 5 = {1 = true, 2 = true, 3 = true, 4 = true}, 10 = {5 = true, 6 = true, 7 = true, 8 = true}} -- etc; this is basically just to to tell the turtle which Z levels the different floors are on.
-- The reason I'm using {roomname = true, roomname2 = true} is because we can then simply check like this:
if floors[zPos] and floors[zPos][targetRoom] then
 -- We're at the right height to get to the target room
end
-- To see what floors we have access to, we can also do this:
if floors[zPos] then
 for name, _ in pairs(floors[zPos]) do
  print("We have acess to room: ", name)
 end
end
-- so, there's still stuff left to do here; you'll want to make your turtle able to move around on a floor, then get back to the turtle-shaft, go up/down if the turtle has more to deliver, etc.

So, basically what you'll want to do is:
1) keep track of where the turtle is (and maybe save it to a file, incase of restarts)

2) (not really mentioned above, either) have some loading-bay for the turtle, this would probably consist of a line of chests with transposers on them (so the turtle will go and toggle the transposer to pick up a cake, for example, then move to a different transposer to pick up some bread), or you could give stuff to the turtle manually, but then you'd need to tell the turtle what it's carrying.

3) (not mentioned above, as that's more focused on moving the turtle around) use rednet to send data to the turtle); you could read this guide: http://www.computercraft.info/forums2/index.php?/topic/244-13-how-to-use-modems-rednet/

3+) You'll also want to take a look at the string library of functions, or use textutils.(un)serialize to make it easier to parse the data you're sending to the turtle over rednet.

This is a fairly complicated task you want to do, but if you break it down, and focus on one thing at a time (e.g moving between rooms, managing inventory, etc), you should be able to get it working :)/>/>

Hope this helps, though I feel like I've missed some things (but I don't want to make this too big a wall of text), so please post if you need help with anything else :)/>/>
jordanlm98 #8
Posted 28 February 2012 - 08:30 PM
The table declaration is valid; but assigning nil to a variable is basically the same as unassigning it; nil is a non-value (meaning that, if a varaible is nil, it isn't assigned).

I would recommend assigning it to "" instead; then, you can use a for loop to see if it exists:


rooms = {}
rooms = {r1=nil,r2=nil,r3=nil,r4=nil,r5=nil,r6=nil,r7=nil,r8=nil,r9=nil,r10=nil,r11=nil,r12=nil} -- Is the exact same as above!

for k, v in pairs(rooms) do
print("Room: ", k, " Value: ", v)
end
-- Above will not print anything!
rooms = {r1 = "", r2 = "", r3 = "", r4 = nil, r5 = "",r6 = "", r7 = "" , r8 = "", r9 = "" , r10 = "", r11 = "", r12 = ""}

for k, v in pairs(rooms) do
print("Room: ", k, " Value: ", v)
end
-- This'll print each of the rooms

In addition, if you do it like that, you could do this:

if rooms[roomid] == "" then -- The room is free!
...
elseif rooms[roomid] ~= nil then -- The room is booked!
...
else -- The room doesn't exist!
...
end

There's lots more fun stuff you can do, so try these things first, and see how they work for you. :D/>/>

I followed what you did and now My program works like a charm :D/>/> Is there an way for me to save booked rooms to a .txt file so that if the computer restarts or something it won't reset what rooms are booked?


And I have another mostly unrelated question: How do I use rednet to make room service system that uses a turtle to deliver things like cakes to rooms…?

You could serialize the table, then save it to a file:


local serialized = textutils.serialize(rooms) -- serialize (turn into text form) the room data

local handle = io.open("rooms.dat", "w") -- Open the rooms in WRITE-mode; this will also clear the file
handle:write(serialized) -- Write the data to the file
handle:close() -- Close the file after you're done with it; this is important, because file handles are limited, and if you don't close it, you'll either have to wait until it's closed by the garbage collector, or restart minecraft (if on a server; the server).

-- To read the data:
local handle = io.open("rooms.dat", "r") -- Opens in READ-mode, you can't write to the file; but you can now read it.
local serialized = handle:read("*a") -- Reads 'all' of the file
handle:close() -- Close the file handle, since we're done with it.
rooms = textutils.unserialize(serialized) -- turns the serialized string back into a table




Making a room-service system will be a bit more complicated; but you'd want something like the following:

local xPos, yPos, zPos = 0, 0, 0 -- Keep track of where the turtle is
local facing = 0 -- Keep track of where the turtle is facing
local faces = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}} -- The turtle can face foward, to the left, to the back, to the right, etc.
-- add custom functions to update above when you move here; e.g
function moveForward()
if turtle.forward() then -- If we move forward
  local face = faces[(facing%4) + 1] -- Modulo the face to be safe, incase it overflows; add 1 because lua defaults at 1, not 0. (4%4 = 0; 4%7 = 3; modulo gives you the remainder after division)
  xPos = xPos + face[0] -- Add the X component of the face to X,
  yPos = yPos + face[1] -- Add the Y component (not using notch's coordinates here, mind you; the idle position of the turtle should be 0,0,0)
  return true -- we moved sucessfully
end
return false -- we couldn't move
end
-- Do this for turnleft/turnright, but you'll be adding/subtracting to face instead, and to move up/down you simply add/subtract to zPos, and to go backwards, you do the same you did for forwards, but you subtract the face instead of add it :(/>/>
-- Then, remember to use these fuctions instead of the turtle.forward() etc functions, so you can keep track of your position!
-- You might also want to save this data to a file after every move (including what it's doing, etc), because your turtle could potentially get stuck, if the chunk was unloaded, or if minecraft is restarted).

-- Then, you'll want to do figure out how your turtle can move, so it doesn't break stuff:
-- I'd recommend having a place where you can move up/down safely (basically a 1x1 shaft that goes down, exclusively for the turtle's use); so on a map, it'd look something like this (# denotes blocked space):
-- Floor 0:
--###
--# #
--# # -- Turtle service area here, this could be in the basement

-- Going up, z level 1+, depending on how high your ceiling is:
--###
--# #
--###

-- On the ground of the first floor with rooms in it:
--###
--# #
--# ############# -- 1x1 hole for the turtle to come out of
-- -- Hallway; you'll have to have the turtle move around here without breaking stuff, then into a room to service (you'd want a small turtle hole for that, again).

local floors = {0 = {loading = true}, 5 = {1 = true, 2 = true, 3 = true, 4 = true}, 10 = {5 = true, 6 = true, 7 = true, 8 = true}} -- etc; this is basically just to to tell the turtle which Z levels the different floors are on.
-- The reason I'm using {roomname = true, roomname2 = true} is because we can then simply check like this:
if floors[zPos] and floors[zPos][targetRoom] then
-- We're at the right height to get to the target room
end
-- To see what floors we have access to, we can also do this:
if floors[zPos] then
for name, _ in pairs(floors[zPos]) do
  print("We have acess to room: ", name)
end
end
-- so, there's still stuff left to do here; you'll want to make your turtle able to move around on a floor, then get back to the turtle-shaft, go up/down if the turtle has more to deliver, etc.

So, basically what you'll want to do is:
1) keep track of where the turtle is (and maybe save it to a file, incase of restarts)

2) (not really mentioned above, either) have some loading-bay for the turtle, this would probably consist of a line of chests with transposers on them (so the turtle will go and toggle the transposer to pick up a cake, for example, then move to a different transposer to pick up some bread), or you could give stuff to the turtle manually, but then you'd need to tell the turtle what it's carrying.

3) (not mentioned above, as that's more focused on moving the turtle around) use rednet to send data to the turtle); you could read this guide: http://www.computerc...-modems-rednet/

3+) You'll also want to take a look at the string library of functions, or use textutils.(un)serialize to make it easier to parse the data you're sending to the turtle over rednet.

This is a fairly complicated task you want to do, but if you break it down, and focus on one thing at a time (e.g moving between rooms, managing inventory, etc), you should be able to get it working :)/>/>

Hope this helps, though I feel like I've missed some things (but I don't want to make this too big a wall of text), so please post if you need help with anything else :)/>/>

Thank you a ton! I've just started out, but lua isn't the first language I've ever learned, and in many ways it is similar to visual basic.

The first thing I will do is work on making the turtle navigate up and down floors, because if it can't do that.. well.. I can't have room service.