Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

LUA Help Random Merchant w

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> LUA Help Random Merchant w Page: [1]
Login
Message << Older Topic   Newer Topic >>
LUA Help Random Merchant w - 3/11/2017 4:55:15 AM   
bearhunter007

 

Posts: 23
Joined: 3/11/2017
Status: offline
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
Post #: 1
RE: LUA Help Random Merchant w - 3/11/2017 6:54:59 AM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
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.

(in reply to bearhunter007)
Post #: 2
RE: LUA Help Random Merchant w - 3/11/2017 8:56:39 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
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:


_____________________________

Michael

(in reply to bearhunter007)
Post #: 3
RE: LUA Help Random Merchant w - 3/11/2017 9:40:01 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
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_'
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



_____________________________

Michael

(in reply to michaelm75au)
Post #: 4
RE: LUA Help Random Merchant w - 3/11/2017 4:33:21 PM   
bearhunter007

 

Posts: 23
Joined: 3/11/2017
Status: offline
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

(in reply to michaelm75au)
Post #: 5
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> LUA Help Random Merchant w Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

1.078