Question

Alright, so here's what I'm trying to do, and I'm almost sure I don't know what phrase to use to find what I'm looking for so I'll do my best to be as clear as possible with limited terminology knowledge.

I'm using lua (or at least attempting to) to generate race tracks/segments for a D&D game.

So here's what I've done, but I cant figure out how to get one table to reference another. And no matter how hard I research or play around it just wont work.

Table Dump:

--Track Tables
local raceClass =   { 'SS', 'S', 'A', 'B', 'C', 'D', 'N' }
local trackLength = { 50, 30, 25, 20, 15, 10, 5 }
local trackDifficulty = { 3, 3, 3, 2, 2, 1, 1 }
local trackTypes = { 'Straightaway', 'Curve', 'Hill', 'Water', 'Jump' }

So, just to explain a little here. First off, we have the class of the race. N for novice, SS for most difficult. Next, we have the length of the resulting track. SS is a 50 segment track. N is a 5 segment track. Each race class has a difficulty cap on each segment of track. SS, S and A all have a cap of 3. D and N have a cap of 1. Then, each segment of track is further broken down into it's type. Those are generated using this slab of code;

--Track Generation
math.randomseed(os.time())
for i = 1, trackLength do
    local trackTypeIndex = math.random(1, #trackTypes)
    local SP = math.random(1, trackDifficulty)  --SP is Stamina Cost for that segment.
    print(tracktypes[trackTypeIndex]..' of SP '..SP)
end
io.read() --So it doesn't just close the window but waits for some user input.

Now it gets into the part that I start to loose myself in. I want the DM to be able to input the selected race class and get a generated list of the resulting race track.

--DM Input
print('Race Class? N, D, C, B, A, S, SS")
io.flush()
local classChoice = io.read()

So, the DM puts in the class choice, lets go with N. What I can't find is a piece of code that'll take the value for classChoice and pair it to raceClass. Then use that position to select the positions in trackLength, and trackDifficulty and finally run the remainder of the code segment Track Generation extrapolating the proper variables and print the results getting something along the lines of;

Straightaway of SP 1
Curve of SP 1
Water of SP 1
Water of SP 1
Jump of SP 1

For a low end novice race, which is only 5 segments long and has a max difficulty of 1. But with the higher classes would still generate the longer, more difficult tracks. I'm trying to be as specific as I can to minimize any confusion my inexperience in code could cost.

Was it helpful?

Solution

I think you'd be better off with a different table structures:

local raceClass = {
    SS = {50, 3},
    S = {30, 3},
    A = {25, 3},
    B = 20, 2},
    C = {15, 2},
    D = {10, 1},
    N = {5, 1},
}

Now, you can access all the data for a raceClass easily. The code would be like:

print "Race Class? N, D, C, B, A, S, SS"
io.flush()
local classChoice = (io.read "*line"):upper()    -- To convert the input to upper case characters
if not raceClass[classChoice] then
    -- Wrong input was given
end
local SP, Length = raceClass[classChoice][2], raceClass[classChoice][1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top