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

[WIP] Technical Training Program Thread

Started by Czarified, 16 May 2013 - 07:26 PM
Czarified #1
Posted 16 May 2013 - 09:26 PM
Hello! This is my project thread for all my computercraft programs for my upcoming custom map "Technical Training." The goal of the map is to simulate a model railroad, and give the player an assignment to complete as a train engineer. I'm using Traincraft, but I believe the majority of work for this map will be on the computer programs.

So my plan is to have a master computer at spawn and the central railyard which will give players their assignment. Players will push a button (either in game, or on a touchscreen) and receive a printout of their job. My current conundrum is how to assign which cars for the job number. I figured I'd use global variables and Rednet to make the job easier.

Global variables:
Stops - Table containing all locations which could possibly have cars.
StopName - Table containing all cars currently in the local railyard.


My plan is to identify cars by what they're carrying. Each car will only have 1 type of resource. Cars in the table will be stored as "Oak Logs" , "White Wool" , "Advanced Rails" , "Milk" , etc. The program would pick 5 random strings from "Stops" and then pick so many cars from each "StopName" to be picked up and dropped off at other Stops. I don't know exactly how to implement this, but I think it would be the most efficient way to do it, if I used global variables like that. I'm open to other methods, currently I'm still in the brainstorming and planning phases of the project.

As I said, this is my project thread. I don't have any code yet, because I'm still trying to figure out what method would be the easiest to implement and most efficient. I will be updating this post with code as I write it. I'll try to include as many comments as I can to help you guys understand what I'm trying to do. The only question I currently have is: Does anyone know how real life programs for model railroading generate task lists like this? I know they exist because I've been to a big model railroad several times and they have a whole computer system which generates and prints the jobs and tracks all car and engine locations.

I'm sorry if this is in the wrong spot. I put it in Ask a Pro because I know I'll need help with this project later on, and I figured it would be more efficient to keep it all in one thread. I'm really excited about this project and I'm looking forward to working on it! All help and constructive criticism is appreciated and encouraged! :lol:/>



Code:

Pseudo Code
Spoiler


function Assignment()
	Pick 0 to 3 random from Stop1 for Stop2
	Pick 0 to 3 random from Stop1 for Stop3
	Pick 0 to 3 random from Stop1 for Stop4
	Pick 0 to 3 random from Stop1 for Stop5

	Pick 0 to 4 random from Stop2 for Stop3
	Pick 0 to 3 random from Stop2 for Stop4
	Pick 0 to 2 random from Stop2 for Stop5
	Pick 0 to 1 random from Stop2 for Stop6

	Pick 0 to 3 random from Stop3 for Stop4
	Pick 0 to 2 random from Stop3 for Stop5
	Pick 0 to 1 random from Stop3 for Stop6

	Pick 0 to 2 random from Stop4 for Stop5
	Pick 0 to 1 random from Stop4 for Stop6
	
	Print Results
	Update Incoming and Outgoing Lists for each stop as necessary
end

5/18 6:15pm
Spoiler


-- Technical Training
-- Description: This program is for use with the Technical Training custom map. This is the main program for
--			  use in the mainframe computer system.
-- Author: Ben Crews (Czarified)


-- Variable Definition
StopNames = { "Stop1" , "Stop2" , "Stop3" , "Stop4" , "Stop5" , "Stop6" , "Stop7" }
Stops = { Stop1 , Stop2 , Stop3 , Stop4 , Stop5 , Stop6 , Stop7 }
Stop1 = { in = {} , available = {} , out = {} }			 -- Currently all these tables are equal and contain nothing,
Stop2 = { in = {} , available = {} , out = {} }			 -- but they will change as the program is completed.
Stop3 = { in = {} , available = {} , out = {} }
Stop4 = { in = {} , available = {} , out = {} }
Stop5 = { in = {} , available = {} , out = {} }
Stop6 = { in = {} , available = {} , out = {} }
Stop7 = { in = {} , available = {} , out = {} }


--Functions

function assignment()					   -- Determines the assignment given for completion.
local StopsLeft = 5					 -- # of Stops assigned per run.
local Temp = Stops					  -- Creation of temporary Stops list copy for modification.
local Jobstop = {}					  -- List of Stops for job to be assigned.

for i = 1, StopsLeft do
val = math.random(1, #Temp)				  -- Computes random index value from list of locations.
local Jobstop[i] = table.remove(Temp, val)   -- Removes indexed value from Temp and places in Jobstop list.
end

for i = 1, StopsLeft do
table.insert( Stop1[3].out[#Stop1[2].out + 1],  table.remove(Stop1[2], math.random(0,4) )			  -- Will this work properly and can it be automated for each value generated in Jobstop?
end

end

function reward()		-- Determines reward received when completed.

end

function checkcomplete() -- Checks for completion of required task.

end



-- Main Program

Edited on 19 May 2013 - 05:59 PM
Lyqyd #2
Posted 17 May 2013 - 12:46 AM
Moved to General.
Czarified #3
Posted 18 May 2013 - 01:30 PM
First question: Can I concatenate a string stored in a table to form an already existing variable for manipulation? Example of what I want to do:


Table = { Stop1, Stop2, Stop3 }
Table[2] .. Incoming = [ 1, 2, 3, 4, 5 ]
Table[2] .. Outgoing = [ 2, 4, 5, 7 ]

I'm currently looking through the wiki and Lua documentation for an answer. Basically I'd like to have an easy way to call variables and insert things into the Stop2Incoming and Stop2Outgoing tables as it loops.
Symmetryc #4
Posted 18 May 2013 - 01:46 PM
Is this what you're looking for?

stops = {
   {
       out = {
           1;
           2;
           5;
       };
       in = {
           11;
       };
   };
   {
       out = {
             3;
             7;
        };
        in = {
             9;
        };
    };
}
superaxander #5
Posted 18 May 2013 - 01:55 PM
First question: Can I concatenate a string stored in a table to form an already existing variable for manipulation? Example of what I want to do:


Table = { Stop1, Stop2, Stop3 }
Table[2] .. Incoming = [ 1, 2, 3, 4, 5 ]
Table[2] .. Outgoing = [ 2, 4, 5, 7 ]

I'm currently looking through the wiki and Lua documentation for an answer. Basically I'd like to have an easy way to call variables and insert things into the Stop2Incoming and Stop2Outgoing tables as it loops.
use table.insert(data, table)
Czarified #6
Posted 18 May 2013 - 02:02 PM
First question: Can I concatenate a string stored in a table to form an already existing variable for manipulation? Example of what I want to do:


Table = { Stop1, Stop2, Stop3 }
Table[2] .. Incoming = [ 1, 2, 3, 4, 5 ]
Table[2] .. Outgoing = [ 2, 4, 5, 7 ]

I'm currently looking through the wiki and Lua documentation for an answer. Basically I'd like to have an easy way to call variables and insert things into the Stop2Incoming and Stop2Outgoing tables as it loops.
use table.insert(data, table)

that's what I was planning to do, but can i do something like table.insert(3, Table[2] .. "Incoming" )?
Engineer #7
Posted 18 May 2013 - 02:54 PM
–[[ snip ]]–
Well, lets imagine we have this table:

local t = {
   [1] = { 1, 2, 3 },
   [2] = { 4, 5 },
   [3] = { 7, 8, 9 }
}
We can use table.insert( tablename, [index,] information ) like mentioned earlier. The index is optional.
If the index is blank, it will just add to the table, NOT REPLACE!

However, we cannot set the index of the index. So not the the first table inside t and there the index from. So we must replace the whole table like so:

table.insert(t, 2, {4, 5, 6})
And now we have this table:

local t = {
    [1] = { 1, 2, 3 },
    [2] = { 4, 5, 6 },
    [3] = { 7, 8, 9 }
}

And to concatenate a string we can easily do this:

local randomString = "Hello World!"
--# Insert it in the table:
table.insert( t, 3, randomString .. " No, hello city!" )

After that the table looks like this:

local t = {
    [1] = { 1, 2, 3 },
    [2] = { 4, 5, 6 },
    [3] = "Hello World! No, hello city!"
}

I hope I helped you quite a lot!
Edited on 18 May 2013 - 12:57 PM
Lyqyd #8
Posted 18 May 2013 - 03:05 PM
First question: Can I concatenate a string stored in a table to form an already existing variable for manipulation? Example of what I want to do:


Table = { Stop1, Stop2, Stop3 }
Table[2] .. Incoming = [ 1, 2, 3, 4, 5 ]
Table[2] .. Outgoing = [ 2, 4, 5, 7 ]

I'm currently looking through the wiki and Lua documentation for an answer. Basically I'd like to have an easy way to call variables and insert things into the Stop2Incoming and Stop2Outgoing tables as it loops.
use table.insert(data, table)

that's what I was planning to do, but can i do something like table.insert(3, Table[2] .. "Incoming" )?

SymmetryC's post above has what you're actually looking for.
Czarified #9
Posted 18 May 2013 - 05:28 PM
Is this what you're looking for?

stops = {
   {
	   out = {
		   1;
		   2;
		   5;
	   };
	   in = {
		   11;
	   };
   };
   {
	   out = {
			 3;
			 7;
		};
		in = {
			 9;
		};
	};
}

How would one insert data into one of the "nested" tables? Sorry I don't quite know the term for this. But say i wanted to insert another piece of data into the first "out" table? are the outs and ins in the example keys? So something like this?

table.insert( stops[out], 2)

would result in:

stops = {
   {
	   out = {
		   1;
		   2;
		   5;
		   2;	  -- New value inserted
	   };
	   in = {
		   11;
	   };
   };
   {
	   out = {
			 3;
			 7;
		};
		in = {
			 9;
		};
	};
}


EDIT: Updated opening post with my current code. Perhaps that will help give more context to people as I need help. I will try and keep this updated every time I encounter a new problem.
Symmetryc #10
Posted 18 May 2013 - 05:35 PM
To change it you would do stops[1].out = {1, 3, 4}. Or you could do stops[1].out[1] = 7.
Czarified #11
Posted 18 May 2013 - 05:45 PM
To change it you would do stops[1].out = {1, 3, 4}. Or you could do stops[1].out[1] = 7.

thank you very much! I think I can get the project assignment done now… :D/>


Code updated again. At line 32 (where I have the commented question), I know currently there's no reason for it to be in the for loop, but will that line of code even do what I want? And just for clarification, I want to select 0-5 random values from one table and append them to the other table while removing them from their original table. I'd like to use the for loop to do this for each location in JobStop.
Czarified #12
Posted 19 May 2013 - 07:48 PM
Opening post updated with pseudo code for the assignment function.