chromium Posted January 4, 2014 Posted January 4, 2014 Hi all, this is a "secondary" scripting problem. I'm preparing a script that help building briefing for our squadron, gathering some info from the mission file. While retrieving information from the mission isn't a very difficult issue, as a newbie with LUA language i'm struggling how to solve this problem: I need to build a randomized communication table that is made of 6 columns, which is build this way: Channel Callsign UHF primary UHF secondary CHAN 1 Hawg 1 251.000 301.000 CHAN 2 Hawg 2 252.000 302.000 CHAN 3 Pig 1 282.000 304.050 ... ... ... ... where Channel, Callsign, UHF and VHF.AM are pre-builded tables themself, i.e.: CHANLIST = { [1] = { ["ID"] = "CHAN 1", }, [2] = { ["ID"] = "CHAN 2", }, [3] = { ["ID"] = "CHAN 3", }, } ... ... ... FREQUHF = { [1] = { ["FRQ"] = 288.000, }, [2] = { ["FRQ"] = 303.500, }, [3] = { ["FRQ"] = 290.200, }, } Can someone help me building a script that merge those tables in a unique table whitout repetition of any value (channels will be at most 10 while frequency will be > 20) ? Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
Bushmanni Posted January 4, 2014 Posted January 4, 2014 Use table.remove(). For example: for i = 1, n do randomNumber = math.random(#CHANLIST) finalTable.CHAN = table.remove(CHANLIST, randomNumber) randomNumber = math.random(#FREQUHF ) finalTable.FREQ = table.remove(FREQUHF , randomNumber) ... end With every iteration this removes the used values from the table so they can't be selected again. Obviously this will alter the CHANLIST and the other tables so if you need to preserve them you need to make a copy that you give to the algorithm. Additionally for this algorithm (as presented) to work you need to remove the extra index in CHANLIST so it looks like this: CHANLIST = { [1] = "CHAN 1", [2] = "CHAN 2", [3] = "CHAN 3", } This algorithm can be made to work with the tables you have now but it's just more unnecessary code (if assuming the tables don't really need to be like they are now). My main point was to present the idea how pick random values from table only once. If this isn't enough feel free to ask. Description of table.remove(): http://www.lua.org/manual/5.1/manual.html#pdf-table.remove DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community -------------------------------------------------- SF Squadron
chromium Posted January 4, 2014 Author Posted January 4, 2014 much thanks! I'll try and report here if unable to solve... to better understand: your code only remove the value or (as it seems to me) it also add the corresponding value to the new table using "CHAN" and "FREQ" as keys? Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
chromium Posted January 4, 2014 Author Posted January 4, 2014 ok I didn't succeded in working with it. I tried to copy and paste your script but it gives me an error: " 'for' limit must be a number ". Also I don't understand how to insert the value in the new table assuming that the table ID is the correct thing. My objective is to have a table that is similar to this: COMM = { [1] = { ["CHAN"] = "CHAN 1", ["CALLSIGN"] = "Hawg 1", ["MAINFREQ"] = 303.700, ["SECFREQ"] = 287.700, }, [2] = { ["CHAN"] = "CHAN 2", ["CALLSIGN"] = (anothervalue), ["MAINFREQ"] = (anothervalue), ["SECFREQ"] = (anothervalue), }, ......... } "CHAN" value is taken from CHANLIST table in numeric order "CALLSIGN" value is taken from CALLSIGN table randomly, if not present result as "-" text "MAINFREQ" value is taken from FREQ table randomly, if not present result as "-" text "SECFREQ" value is taken also from FREQ table randomly, if not present result as "-" text that is the first time I work with tables about removing and adding thing, I usualli only read into them.. :( sorry Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
tintifaxl Posted January 4, 2014 Posted January 4, 2014 replace for i = 1, n do with for i = 1, 10 do Windows 10 64bit, Intel i9-9900@5Ghz, 32 Gig RAM, MSI RTX 3080 TI, 2 TB SSD, 43" 2160p@1440p monitor.
Bushmanni Posted January 4, 2014 Posted January 4, 2014 For-loop is constructed as follows: for i = startValue, endValue do somestuff end where i is a local variable (inside the for-loop) which value runs from startValue to endValue. In my example the variable n is the end index which you should set to be the number of channels you intend to randomly create. table.remove() gives the removed element as a return value so by assigning it to different table as a new element effectively moves the element from one table to an another. table.remove() will also shift the indices after the removed element down by one so they will run from 1 to number of elements without gaps. So if you want to add the channel names in numeric order you just pick the first element every time from the channel table. The algorithm I presented should produce the kind of table you are looking for after you add all the missing pieces. If you are new to building tables then I might add that you need to add a line "COMM = {}" before the for-loop to initialize the COMM as a table so you can add new elements. DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community -------------------------------------------------- SF Squadron
chromium Posted January 4, 2014 Author Posted January 4, 2014 thanks for the explanation, I tried your info but happens something strange: this code: do COMMTABLE = {} for i = 1,10 do randomNumber = math.random(#CHANLIST) COMMTABLE.CHAN = table.remove(CHANLIST , randomNumber) randomNumber = math.random(#IDUHF ) COMMTABLE.ID = table.remove(IDUHF , randomNumber) randomNumber = math.random(#FREQUHF ) COMMTABLE.FREQ = table.remove(FREQUHF , randomNumber) end end where the three named tables exist, gives an error on the 5th line ( "COMMTABLE.CHAN = table.remove(CHANLIST , randomNumber)" ) saying that: " attempt to index field "?" a nil value " as if it's unable to find the field. I attach here the entire code: CHANLIST = { [1] = "CHAN 1", [2] = "CHAN 2", [3] = "CHAN 3", [4] = "CHAN 4", [5] = "CHAN 5", [6] = "CHAN 6", [7] = "CHAN 7", [8] = "CHAN 8", [9] = "CHAN 9", [10] = "CHAN 10", } IDUHF = { [1] = "ASCARI", [2] = "CLARK", [3] = "LAUDA", [4] = "SENNA", [5] = "ANDRETTI", [6] = "PIQUET", [7] = "SCHECKTER", [8] = "STEWART", [8] = "MANSELL", [9] = "HILL", [10] = "ALONSO", [11] = "PROST", [12] = "HUNT", } FREQUHF = { [1] = 288.000, [2] = 303.500, [3] = 290.200, [4] = 307.300, [5] = 276.000, [6] = 287.000, [7] = 292.000, [8] = 295.400, [8] = 289.000, [9] = 293.700, [10] = 288.700, [11] = 302.000, [12] = 300.200, } do COMMTABLE = {} for i = 1,10 do randomNumber = math.random(#CHANLIST) COMMTABLE.CHAN = table.remove(CHANLIST , randomNumber) randomNumber = math.random(#IDUHF ) COMMTABLE.ID = table.remove(IDUHF , randomNumber) randomNumber = math.random(#FREQUHF ) COMMTABLE.FREQ = table.remove(FREQUHF , randomNumber) end end Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
Bushmanni Posted January 4, 2014 Posted January 4, 2014 Here's a working version. CHANLIST = { [1] = "CHAN 1", [2] = "CHAN 2", [3] = "CHAN 3", [4] = "CHAN 4", [5] = "CHAN 5", [6] = "CHAN 6", [7] = "CHAN 7", [8] = "CHAN 8", [9] = "CHAN 9", [10] = "CHAN 10", } IDUHF = { [1] = "ASCARI", [2] = "CLARK", [3] = "LAUDA", [4] = "SENNA", [5] = "ANDRETTI", [6] = "PIQUET", [7] = "SCHECKTER", [8] = "STEWART", [8] = "MANSELL", [9] = "HILL", [10] = "ALONSO", [11] = "PROST", [12] = "HUNT", } FREQUHF = { [1] = 288.000, [2] = 303.500, [3] = 290.200, [4] = 307.300, [5] = 276.000, [6] = 287.000, [7] = 292.000, [8] = 295.400, [8] = 289.000, [9] = 293.700, [10] = 288.700, [11] = 302.000, [12] = 300.200, } do COMMTABLE = {} math.randomseed (os.time()) for i = 1,10 do COMMTABLE[i] = {} COMMTABLE[i].CHAN = table.remove(CHANLIST , 1) randomNumber = math.random(#IDUHF ) COMMTABLE[i].ID = table.remove(IDUHF , randomNumber) randomNumber = math.random(#FREQUHF ) COMMTABLE[i].FREQ = table.remove(FREQUHF , randomNumber) print(COMMTABLE[i].CHAN .. " " .. COMMTABLE[i].ID .. " " .. COMMTABLE[i].FREQ) end end I added the line COMMTABLE = {} to initialize the child-table where CHAN, ID and FREQ variables are stored. I also removed the randomization from CHAN so it picks the channels in the same order as they are in the table. You should remove the print() when you put this in DCS. "math.randomseed (os.time())" could give you an error as I have never tried os functions in DCS. At the moment I don't know any other way to set the seed number for the random number generator through lua so if this fails you need to set a flag to random value in ME and use that value to set the seed in script. You can try this code out with lua demo. http://www.lua.org/cgi-bin/demo 1 DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community -------------------------------------------------- SF Squadron
chromium Posted January 4, 2014 Author Posted January 4, 2014 ok manymanymany thanks, now I'll take a couple of hours to understand what you've done :) Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
chromium Posted January 5, 2014 Author Posted January 5, 2014 adjousted whitout the "math.randomseed (os.time())" part and works like a charm: sadly that function gives an error. Therefore, it work good enough :). Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
Bushmanni Posted January 6, 2014 Posted January 6, 2014 Does the script give different table with every run of the mission or does it give the same "random" table every time? I'm suspecting the latter as the line you removed is supposed to prevent that but DCS might have some measure already in place behind the curtains. DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community -------------------------------------------------- SF Squadron
chromium Posted January 6, 2014 Author Posted January 6, 2014 I can confirm that they are different, I can't tell you if effectiveli randomized, but the tables that the script produce every time aren't exactly the same. Author of DSMC, mod to enable scenario persistency and save updated miz file Stable version & site: https://dsmcfordcs.wordpress.com/ Openbeta: https://github.com/Chromium18/DSMC The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.
Bushmanni Posted January 6, 2014 Posted January 6, 2014 Thats good to hear. DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community -------------------------------------------------- SF Squadron
Recommended Posts