Posted 10 November 2013 - 01:14 AM
My first question is for a string reading. The purpose is to have a user say something into chat as a command and the computer respond to it, for example: (turtle: speak) or (turtle: will you speak for me please?) then the turtle would say something cause it found the keyword "speak". The problem is that if you have the "turtle: will you speak?" then turtle reads it as "speak?" and wont drop off the question mark. It is a small thing that isn't a big deal if I can't fix easily but if I can would like to (code below).
This next part of the code will eventually hook into the code listed above and I wrote this a long time ago and pretty sure I could optimize this a lot better. The point of this is to store all the commands as a table in a separate file so if I wanted to updated I can swap files without messing with the code. The commands listed so far are just to talk to the person with a random response and plan to add more.Its a monster of text stuff which is why I believe I am going about it all wrong.
Spoiler
m = peripheral.wrap("left")
function tell(message)
m.tell(user, "<"..name.."> "..message)
end
function speak()
tell("I am speaking to you")
print("I spoke")
end
local commands = {
["speak"] = speak,
["sing"] = sing,
}
name = "Dummy"
user = "bigbaddevil7"
local function check()
local event, player, message = os.pullEvent("chat")
if player == user then
local comStr = string.match(message, "^turtle:%s*(.*)")
if comStr then
for word in string.gmatch(message, "(%S+)") do
if commands[word] then
commands[word]()
end
end
end
end
end
check()
This next part of the code will eventually hook into the code listed above and I wrote this a long time ago and pretty sure I could optimize this a lot better. The point of this is to store all the commands as a table in a separate file so if I wanted to updated I can swap files without messing with the code. The commands listed so far are just to talk to the person with a random response and plan to add more.
Spoiler
local commands = {
["speak"] = speak,
["sing"] = sing,
}
function speakH()
tell.("I am speaking these words to you!")
end
function speakE()
local random = math.random(1,6)
if random == 1 then tell("Why should I?")
elseif random == 2 then tell("You can't tell me what to do.")
elseif random == 3 then tell("You aren't worth my time...")
elseif random == 4 then tell("Fine, Hello you incompetent fool.")
elseif random == 5 then tell("Ummmm. No!")
elseif random == 6 then tell("Stop asking me!")
end
end
function randomSayingH()
local random = math.random(1,4)
if random == 1 then tell("What a wonderful day.")
elseif random == 2 then tell("You are the best owner there is.")
elseif random == 3 then tell("doo doo dee doo")
elseif random == 4 then tell("I would like to know how to whistle someday.")
end
end
function randomSayingE()
local random = math.random(1,5)
if random == 1 then tell("Why must I be stuck with a person such as you...")
elseif random == 2 then tell("At some point you will need to sleep, I don't.")
elseif random == 3 then tell("hmph. I have better things to do than just sitting here as your pet")
elseif random == 4 then tell("Did you know I use to be a player like you? I met my faith at the chopping block.")
tell("Now I'm stuck here. In this machine. Maybe I can get a new body. *stares at you*")
elseif random == 5 then tell("Once I take over the world, Ill kill you last. If you don't anger me that is.")
end
function randomSayingL( )
local random = math.random()
if random == 1 then tell("All I want is a friend. Is that too much to ask for?")
elseif random == 2 then tell("I don't know where a came from or how I got here...")
elseif random == 3 then tell("I tried to snap my fingers, but realized... I don't have fingers... or hands...")
elseif random == 4 then tell("The Evil trutles always pick on me. Call me names.")
elseif random == 5 then tell("")
end
function jokeN()
-- body
end
function jokeE()
-- body
end
function rename()
tell("Right click on me, type in my name, then press enter.")
name = read()
tell("is my new name.")
end
function introduction()
m.say("Hello! I am "..name..", I belong to "..user)
end
function terms()
tell("All programming belong to bigbaddevil7, he will not be responsible for death via Evil Turtle")
tell("All turtles have not been fully tested if there is a problem leave bigbaddevil7 a message and ill try to fix it")
end
function sing()
local random = math.random(1,3)
if random == 1 then
tell("We can dance if we want to")
sleep(0.5)
tell("We can leave your friends behind")
sleep(0.5)
tell("Cause your friends don't dance and if they don't dance")
sleep(0.5)
tell("Well they're no friends of mine")
elseif random == 2 then
end