LUA Help Random Merchant w (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series



Message


bearhunter007 -> LUA Help Random Merchant w (3/11/2017 4:55:15 AM)

Trying to use three different courses for the modified random merchant script by Apache85

My attempt as coded fails. What did I do wrong?

--Random Merchant by Apache85 modified JJM
math.random(os.time()) --reset random seed
math.random(os.time())
merch_number = math.random(8,16) --random number between 8 and 16 determines number of merchants
counter = 0 --counter init

--course 1
local courseset_one={{lat=24.55,lon=122.37},{lat=21.56,lon=121.78},
{lat=17.39,lon=117.38},{lat=4.511,lon=106.64},{lat=1.287,lon=104.35},
{lat=1.186,lon=103.84},{lat=4.790,lon=98.906}}
--course 2
local courseset_two={{lat=30.37,lon=129.52},{lat=23.03,lon=-164.0}}
--course 3
local courseset_three={{lat=30.11,lon=129.42},{lat=18.03,lon=-158.0}}



repeat
local lat_var = math.random(1,(10^13)) --random number between 1 and 10^13
local long_var = math.random(1,(10^13)) --random number between 1 and 10^13

local pos_lat = math.random(27,32) + (lat_var/(10^13)) --latitude; currently in East China Sea
local pos_long = math.random(123,127) + (long_var/(10^13)) --longitude; currently in East China Sea

local crse=math.random(1,3)
if crse==1 then local crseplot=crourseset_one end
if crse==2 then local crseplot=crourseset_two end
if crse==3 then local crseplot=crourseset_three end



local merch_suffix = math.random(1000,2000) --name suffix
local merch_name = ('Merchant ' .. merch_suffix) --adds merchant name with numbered suffix as above
local DBIDTABLE = { 775, 2027, 2029, 2028, 2030, 774, 2026, 2031, 384, 2024, 2775, 2023, 773, 2774, 1001, 1374, 2773, 2776, 1006, 222, 1599, 2034,
1002, 1317, 144, 339, 275, 145, 2022, 259 } --defines the possible db choices

ScenEdit_AddUnit({
side='Merchant',
type='Ship',
name=merch_name,
heading = 270,
speed=10,
dbid=DBIDTABLE[math.random( 1, #DBIDTABLE)],
latitude=pos_lat,
longitude=pos_long,
course=crseplot
}) --adds the merchant using the variables above --adds the merchant using the variables above

print (merch_name) --prints the name of the merchant created; this can be deleted without any detrimental effect


this_merch = ScenEdit_GetUnit({
side='Merchant',
type='Ship',
name=merch_name}) --assigns the unit table from created unit to a variable

local fuel_state = (math.random(60,90))/100 --random decimal to reflect 60-90% fuel
local current_fuel = (this_merch.fuel[3001].current) --the starting fuel level
local f = current_fuel * fuel_state --multiplies starting fuel by fuel state decimal
local f = math.floor(f + 0.5) --rounds resulting number to an integer
ScenEdit_SetUnit({side='Merchant',name=merch_name, fuel={ {3001, f} } }) --changes the fuel level

--ScenEdit_AssignUnitToMission(merch_name, "Vladivostok") --assigns created unit to mission Vladivostok

counter = counter + 1 --adds to counter

until counter == merch_number --stop when counter reaches merchant number




Rory Noonan -> RE: LUA Help Random Merchant w (3/11/2017 6:54:59 AM)

What kind of error are you getting?

When I run into trouble I've found it really helps to use a program like Notepad++; that way you can trace the error message back to the line that's causing problems.




michaelm75au -> RE: LUA Help Random Merchant w (3/11/2017 8:56:39 AM)

I copied the code into the Scenario Lua console and it ran fine.
With the latest version, the error line is usually shown if it can:




michaelm75au -> RE: LUA Help Random Merchant w (3/11/2017 9:40:01 AM)

I actually think your problem is that the course is not getting set on the ships rather than an error showing
Went thru it line by line. You need to change the following:
1) Make crseplot local to the overall function, and 2) correct the spelling mistakes 'crourseset_' [:D]
quote:

local crseplot
if crse==1 then crseplot=courseset_one end
if crse==2 then crseplot=courseset_two end
if crse==3 then crseplot=courseset_three end





bearhunter007 -> RE: LUA Help Random Merchant w (3/11/2017 4:33:21 PM)

Thanks michaelm and Apache85 for your responses.

I'm embarrassed that I didn't pick-up the spelling error. [:(]

Apache85 you are right notepad++ is an excellent tool and I am using it. It helps to trace errors down.

It's a shame that a lot of michaelm's lua examples no longer display on baloogan's lua resource site. Some sort of php error.

J




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
1.09375