I have a problem with my code. I made so much code, and now I need to place ends, which I cannot figure out how to do because I need to line everything up and then place at least 5 or 6 ends in the right places. I am fairly new to Lua and I'm sure that there's a better way to go about this, so if there is, tell me in your answer. Also, this code is unfinished.
math.randomseed(os.clock())
math.random();math.random();math.random()
local function navigate()
print("Where would you like to go?")
print("Somewhere peaceful(type peaceful), somewhere dangerous(type dangerous), or somewhere civilized?")
local destination = io.read()
if destination == "peaceful" then
local event = math.random(5)
if event == 1 then
print("You find a leafy forest.")
print("What would you like to do? Search, Cut tree, Inventory")
local action = io.read()
if action == "search" then
local searchevent = math.random(3)
if searchevent == 1 then
require("battle")
elseif searchevent == 2 then
lootdrop = math.random(5)
if lootdrop == 1 then
print("You found an abandoned sword. You pick it up.")
if g_inventory1 == nil then
g_inventory1 = "a sword"
elseif g_inventory2 == nil then
g_inventory2 = "a sword"
elseif g_inventory3 == nil then
g_inventory3 = "a sword"
else
print("You don't have room for this sword.Type 'leave it' or replace items. Items to replace:")
print(g_inventory1)
print(g_inventory2)
print(g_inventory3)
replace = io.read()
if replace == g_inventory1 then
io.write("You replaced the "); io.write(replace); io.write(" with a sword.")
g_inventory1 = "a sword"
elseif replace == g_inventory2 then
io.write("You replaced the "); io.write(replace); io.write(" with a sword.")
inventory2 = "a sword"
elseif replace == g_inventory3 then
io.write("You replaced the "); io.write(replace); io.write(" with a sword.")
inventory3 = "a sword"
elseif replace == "leave it" then
print("You leave the sword.")
else
print("You randomly thought of a pointless word and moved on ahead.")
end
if lootdrop == 2 then
print("A hatchet sticks out of a tree stump, and you take it out.")
if inventory1 == nil then
inventory1 = "a hatchet"
elseif inventory2 == nil then
inventory2 = "a hatchet"
elseif inventory3 == nil then
inventory3 = "a hatchet"
else
print("You don't have room for this hatchet. Replace items or 'leave it'? Items to replace:")
print(g_inventory1)
print(g_inventory2)
print(g_inventory3)
replace = io.read()
if replace == g_inventory1 then
io.write("You replaced the "); io.write(replace); io.write(" with the hatchet.")
g_inventory1 = "a hatchet"
elseif replace == g_inventory2 then
io.write("You replaced the "); io.write(replace); io.write(" with the hatchet.")
inventory2 = "a hatchet"
elseif replace == g_inventory3 then
io.write("You replaced the "); io.write(replace); io.write(" with the hatchet.")
inventory3 = "a hatchet"
elseif replace == "leave it" then
print("You leave the hatchet.")
else
print("You randomly thought of a pointless word and moved on ahead.")
end
end
if lootdrop == 3 then
if g_inventory1 == nil then
print("You have found a carving knife. You pick it up.")
g_inventory1 = "a carving knife"
elseif g_inventory2 == nil then
print("You have found a carving knife. You pick it up.")
g_inventory2 = "a carving knife"
elseif g_inventory3 == nil then
print("You have found a carving knife. You pick it up.")
g_inventory3 = "a carving knife"
else
print("You have no room for this carving knife. 'leave it' or replace an item? Items to replace:")
print(g_inventory1)
print(g_inventory2)
print(g_inventory3)
replace = io.read()
if replace == g_inventory1 then
io.write("You replaced the "); io.write(inventory1); io.write(" with a carving knife.")
g_inventory1 = "a carving knife"
elseif replace == g_inventory2 then
io.write("You replaced the "); io.write(inventory1); io.write(" with a carving knife.")
g_inventory2 = "a carving knife"
elseif replace == g_inventory3 then
io.write("You replaced the "); io.write(inventory1); io.write(" with a carving knife.")
g_inventory3 = "a carving knife"
elseif replace == "leave it" then
print("You leave the sword.")
else
print("You think of a pointless word and move on ahead.")
end
if lootdrop == 4 then
if g_inventory1 == nil then
print("You have found a magical amulet. You pick it up.")
g_inventory1 = "a magical amulet"
elseif g_inventory2 == nil then
print("You have found a magical amulet. You pick it up.")
g_inventory2 = "a magical amulet"
elseif g_inventory3 == nil then
print("You have found a carving knife. You pick it up.")
g_inventory3 = "a magical amulet"
else
print("You have no room for this magical amulet. 'leave it' or replace an item? Items to replace:")
print(g_inventory1)
print(g_inventory2)
print(g_inventory3)
replace = io.read()
if replace == g_inventory1 then
io.write("You replaced the "); io.write(inventory1); io.write(" with a magical amulet.")
g_inventory1 = "a magical amulet"
elseif replace == g_inventory2 then
io.write("You replaced the "); io.write(inventory1); io.write(" with a magical amulet.")
g_inventory2 = "a magical amulet"
elseif replace == g_inventory3 then
io.write("You replaced the "); io.write(inventory1); io.write(" with a magical amulet.")
g_inventory3 = "a magical amulet"
elseif replace == "leave it" then
print("You leave the amulet.")
else
print("You think of a pointless word and move on ahead.")
if lootdrop == 5 then
print("You failed to find anything useful.")
end
elseif searchevent == 3 then
print("You find nothing.")