CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 87 users online: 1 member, 60 guests and 26 bots.
Members: None.
Bots: MSN/Bing (1), Magpie Crawler (5), Googlebot (19), MSN/Bing (1).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
Author Message
pcb_master


Advanced Newbie


Joined: 17 Apr 2012
Posts: 55

Posted: 02 Oct 2012 02:08:34 pm    Post subject: The Forbidden Caves - A LUA RPG

I am working on a text based RPG called The Forbidden Caves.
I will post my progress here.

Here is the nonfunctional code (the interpereter is broken):

Code:


local c_stage = 0
local c_branch = 0

function scrClear ()
 io.write("\27[2J")
end

function rpg (stage,branch)
 if stage == 0 then print "You are in a small abandoned village. There is a tavern to the WEST, a cave to the NORTH and a   sign to the EAST." end
 if stage == 1 and branch == 1 then print "You enter the cave and walk until it gets too dark too see. You stumble on until you just can't go any further. You turn back." end
 if stage == 1 and branch == 2 then print [[The sign reads: "Welcome to the town of Pleasantville! The happy town of..." The population has been scratched out. Someone has carved into the sign: "run, while you can still can! remember, it can always see you..."]] end
 interp()
end

function interp ()
 c_input = io.read()
 if not string.find(c_input, "GO") == nil then
  arg = string.sub(c_input, 3)
  if arg == "NORTH" then c_branch = 1 end
  if arg == "EAST" then c_branch = 2 end
  if arg == "SOUTH" then c_branch = 3 end
  if arg == "WEST" then c_branch = 4 end
 end
 c_stage = 1
 rpg ()
end

scrClear()
print([[_________          _______ ]])                                                     
print([[\__   __/|\     /|(  ____ \]])                                                     
print([[   ) (   | )   ( || (    \/]])                                                     
print([[   | |   | (___) || (__    ]])                                                     
print([[   | |   |  ___  ||  __)   ]])                                                     
print([[   | |   | (   ) || (      ]])                                                     
print([[   | |   | )   ( || (____/\]])                                                     
print([[   )_(   |/     \|(_______/]])                                                     
print()                                                                                 
print([[ _______  _______  _______  ______  _________ ______   ______   _______  _       ]])
print([[(  ____ \(  ___  )(  ____ )(  ___ \ \__   __/(  __  \ (  __  \ (  ____ \( (    /|]])
print([[| (    \/| (   ) || (    )|| (   ) )   ) (   | (  \  )| (  \  )| (    \/|  \  ( |]])
print([[| (__    | |   | || (____)|| (__/ /    | |   | |   ) || |   ) || (__    |   \ | |]])
print([[|  __)   | |   | ||     __)|  __ (     | |   | |   | || |   | ||  __)   | (\ \) |]])
print([[| (      | |   | || (\ (   | (  \ \    | |   | |   ) || |   ) || (      | | \   |]])
print([[| )      | (___) || ) \ \__| )___) )___) (___| (__/  )| (__/  )| (____/\| )  \  |]])
print([[|/       (_______)|/   \__/|/ \___/ \_______/(______/ (______/ (_______/|/    )_)]])
print()                                                                                 
print([[ _______  _______           _______  _______ ]])                                   
print([[(  ____ \(  ___  )|\     /|(  ____ \(  ____ \]])                                   
print([[| (    \/| (   ) || )   ( || (    \/| (    \/]])                                   
print([[| |      | (___) || |   | || (__    | (_____ ]])                                   
print([[| |      |  ___  |( (   ) )|  __)   (_____  )]])                                   
print([[| |      | (   ) | \ \_/ / | (            ) |]])                                   
print([[| (____/\| )   ( |  \   /  | (____/\/\____) |]])                                   
print([[(_______/|/     \|   \_/   (_______/\_______)]])   
io.read()
exiting = false
repeat
 scrClear()
 save = io.open("SAV.E", "RW")
 print("TFC MENU")
 print("-------------")
 print("NEW GAME")
 if not save == null then print("LOAD GAME") else print("NO SAVED GAME") end
 print("DELETE SAVE")
 print("QUIT")
 print("-------------")
 io.write("OPT: ")
 choice = io.read()

 if choice == "NEW" then
  c_stage = 0
  c_branch = 0
  clear()
  rpg(c_stage,c_branch)
 end

 if choice == "QUIT" then exiting = true end
until exiting == true
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55741
Location: Earth, Sol, Milky Way

Posted: 03 Oct 2012 08:39:31 am    Post subject:

Well, I think you're getting caught up in a 0-indexing/1-indexing confusion. In Lua, strings are indexed from 1, not 0, so consider the command "GO NORTH":
Code:
GO NORTH
12345678
You want to take the substring from 4, not 3. Smile
_________________


Back to top
pcb_master


Advanced Newbie


Joined: 17 Apr 2012
Posts: 55

Posted: 03 Oct 2012 08:40:27 am    Post subject:

Oh. That's interesting...
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55741
Location: Earth, Sol, Milky Way

Posted: 03 Oct 2012 08:42:15 am    Post subject:

pcb_master wrote:
Oh. That's interesting...
I only found it by Googling "lua string sub" and reading through a paragraph of the first result. Wink Yup, Lua indexes everything from 1 instead of 0, just like Matlab.

Edit: You have another potential problem as well, I think:

Code:
if not string.find(c_input, "GO") == nil then
That will apply the not and then the ==, which may not work in all cases. You may want this instead:
Code:
if string.find(c_input,"GO") ~= nil then
Or even faster:
Code:
if string.sub(c_input,1,2) == "GO" then

_________________


Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.033921 seconds.