Merchant creation script not working? (Full Version)

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



Message


jmax -> Merchant creation script not working? (11/2/2017 11:30:01 PM)

I'm trying to stitch together some other folks' code into a merchant generation script, but I get an error trying to add the unit because it indexes new_merch which is a nil value... can anyone help me with this?

math.randomseed( os.time() )

PORTTABLE = {'Singapore Port', 'Singapore Port', 'Singapore Port', 'Shanghai Port', 'Shanghai Port', 'Shenzhen Port', 'Hong Kong Port', 'Busan Port', 'Dalian Port', 'Kobe Port' }
PORT = PORTTABLE[math.random( 1, #PORTTABLE)]

DBIDTABLE = { 259, 275, 339, 145, 144, 2776, 2775, 2774, 2773, 2023, 2031, 2029, 2030, 2028, 2027, 339, 2023, 2027 }
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]

MISSIONTABLE = {'Singapore to Shanghai', 'Singapore to Kaohsiung', 'Singapore to Busan', 'Shanghai to Singapore'}
MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]

lastCount = ScenEdit_GetKeyValue('MerchCounter');
lastCountN = tonumber(lastCount);
if lastCountN == nil then
	lastCountN = 0;
end
lastCountN = lastCountN +1;
ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
b=ScenEdit_GetKeyValue('MerchCounter');

if b ~= 0 then
	local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT})
end
ScenEdit_AssignUnitToMission( new_merch.name, 'MISSION')
print (new_merch.name..' with dbid '..DBID..' was created in '..PORT..' on mission '..MISSION..'')





michaelm75au -> RE: Merchant creation script not working? (11/3/2017 5:58:39 AM)

Move the 'local new_merch' outside the IF loop




michaelm75au -> RE: Merchant creation script not working? (11/3/2017 6:04:31 AM)

I did noticed that the AddShip() can fail if the 'base' is overland. This is due to the group location being on land, even though the base check is succeeding for a dock for the ship.
I will log this.

If this is still failing, I would suggest for the time being, to use one of the docks as the base.
[Check the exception log (in folder Logs) for this error to see if failing due to this.
3/11/2017 5:11:29 PM -- B977 -- You cannot place a ship overland!
Exception: You cannot place a ship overland!
Stack Trace: at Command_Core.Scenario.AddNewShip(Side theSide, Int32 ShipDBID, String theName, Double Longitude, Double Latitude, Boolean IgnoreElevationCheck, String theGUID)
]




jmax -> RE: Merchant creation script not working? (11/3/2017 1:43:22 PM)

Thanks for the reply, I've tried that already and it still doesn't work.

This is one of the scripts mine is based on:

merch_num = math.random(8,16) --change 8 or 16 to your specified minimum and maximum number of merchants 

for i = 1,merch_num do 
redo_count = 0 
::redo:: 
local lat_var = math.random(1,(10^13)) 
local lon_var = math.random(1,(10^13)) 
v_lat = math.random(27,32) + (lat_var/(10^13)) --change 27 and 32 to your specified minimum and maximum latitude values; it's important that the first number is smaller than the second. 
v_lon = math.random(119,127) + (lon_var/(10^13)) --change 119 and 127 to your specified minimum and maximum longitude values; it's important that the first number is smaller than the second. 
elevation = World_GetElevation({latitude=v_lat, longitude=v_lon}) 
if elevation > -10 then --Checks to see if the water is deep enough, adjust as you please 
redo_count = redo_count + 1 
if redo_count > 50 then 
print ('A ship was not able to find a suitable spot for placement. Re-check latitude and longitude settings') 
break --this cuts the loop if there are no suitable positions found after 50 tries, prevents infinite loop/game freeze 
else 
goto redo --retries the placement if the water is too shallow 
end 
end 
DBIDTABLE = { 775, 2027, 2029, 2028, 2030, 774, 2026, 2031, 2775, 2023, 773, 2774, 1001, 1374, 2773, 2776, 1006, 222, 1599, 2034, 1002, 1317, 144, 339, 275, 145, 2022, 259 } --list of DBIDs 
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)] 
local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..i, dbid=DBID,latitude=v_lat,longitude=v_lon}) 
local fuel = new_merch.fuel 
fuel[3001].current = fuel[3001].max*math.random(600, 800)/1000 
new_merch.fuel = fuel 
ScenEdit_AssignUnitToMission( new_merch.name, 'Singapore to Busan') 
print (new_merch.name..' with dbid '..DBID..' was created in water with a depth of '..elevation..'m') 
end




jmax -> RE: Merchant creation script not working? (11/3/2017 3:24:42 PM)

Well I removed the offending nil values just to see if it could spawn a ship in port—the event fires but ships will not spawn unless a latitude/longitude is specified, then they will spawn in their assigned base, sometimes with a mission and sometimes not (??). Now I need to figure out what's wrong with missions and how to assign a unique number to the merchants that can be tracked globally...

math.randomseed( os.time() )

DBIDTABLE = { 259, 275, 339, 145, 144, 2776, 2775, 2774, 2773, 2023, 2031, 2029, 2030, 2028, 2027, 339, 2023, 2027 }
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]

PORTTABLE = {'Singapore Port', 'Singapore Port', 'Singapore Port', 'Shanghai Port', 'Shanghai Port', 'Shenzhen Port', 'Hong Kong Port', 'Busan Port', 'Dalian Port', 'Kobe Port' }
PORT = PORTTABLE[math.random( 1, #PORTTABLE)]

MISSIONTABLE = {'Singapore to Shanghai', 'Singapore to Kaohsiung', 'Singapore to Busan', 'Singapore to Hong Kong'}
MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]

ScenEdit_AddUnit({side='Civilian', type='Ship', name='Merchant', dbid=DBID, base=PORT, latitude='1.31990947790077', longitude='104.414712217895'})
ScenEdit_AssignUnitToMission('Merchant', ''..MISSION..'')

print ('Merchant with dbid '..DBID..' was created in '..PORT..' on mission '..MISSION..'')




jmax -> RE: Merchant creation script not working? (11/3/2017 6:16:26 PM)

Made the assign mission script reference homeport and destination, which works well, but I'm still banging my head against the wall trying to figure out how to assign unique numbers to merchants, as identical names are screwing with the mission assignment... there must be an easy way to do this??




somi83 -> RE: Merchant creation script not working? (11/3/2017 8:12:27 PM)

Hi,

have you tried putting the name in the loop, maybe something like this:

math.randomseed( os.time() )

DBIDTABLE = { 259, 275, 339, 145, 144, 2776, 2775, 2774, 2773, 2023, 2031, 2029, 2030, 2028, 2027, 339, 2023, 2027 }
PORTTABLE = {'Singapore Port', 'Singapore Port', 'Singapore Port', 'Shanghai Port', 'Shanghai Port', 'Shenzhen Port', 'Hong Kong Port', 'Busan Port', 'Dalian Port', 'Kobe Port' }
MISSIONTABLE = {'Singapore to Shanghai', 'Singapore to Kaohsiung', 'Singapore to Busan', 'Singapore to Hong Kong'}

for i = 1, math.random (5,10) do
local DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
local PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
local MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]
local merchant_name = string.format( "Merchant %d", i) -- try putting this code inside
new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',
name= merchant_name, -- and here as the name
dbid=DBID, base=PORT, latitude='1.31990947790077', longitude='104.414712217895'})
print ( 'dbid: '..DBID..', base: '..PORT..', mission: '..MISSION..', name: '..merchant_name..', number: '..i)
end




jmax -> RE: Merchant creation script not working? (11/3/2017 8:24:43 PM)

Well I gave up trying to make a counter script and just assigned new merchants a random number between 1 and 999,999,999,999 and hope I don't get duplicates. It's a temporary solution, but surely somebody must know how to set up a counter to assign them unique numbers that keep track of the total?

math.randomseed( os.time() )

local UID = math.random(1,999999999999)

DBIDTABLE = { 259, 275, 339, 145, 144, 2776, 2775, 2774, 2773, 2023, 2031, 2029, 2030, 2028, 2027, 339, 2023, 2027 }
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]

PORTTABLE = {'Singapore', 'Singapore', 'Shanghai',}
PORT = PORTTABLE[math.random( 1, #PORTTABLE)]

MISSIONTABLE = {'Busan', 'Kaohsiung', 'Dalian', 'Hong Kong', 'Incheon', 'Manila'}
MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]

ScenEdit_AddUnit({side='Civilian', type='Ship', name=UID, dbid=DBID, base=PORT, latitude='1.31990947790077', longitude='104.414712217895'})
ScenEdit_AssignUnitToMission(''..UID..'', ''..PORT..' to '..MISSION..'')

print ('Merchant '..UID..' with dbid '..DBID..' was assigned '..PORT..' to '..MISSION..'')




michaelm75au -> RE: Merchant creation script not working? (11/3/2017 8:51:58 PM)

I used you original script with the additional fix to get around the 'over land' issue, and it worked for creating a new ship each time.

quote:


local new_merch
if b ~= 0 then
new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, long=0, lat=0})
if new_merch ~= nil then
ScenEdit_HostUnitToParent({HostedUnitNameOrID=new_merch.guid , SelectedHostNameOrID=PORT})
ScenEdit_AssignUnitToMission( new_merch.name, 'MISSION')
print (new_merch.name..' with dbid '..DBID..' was created in '..PORT..' on mission '..MISSION..'')
end
end




jmax -> RE: Merchant creation script not working? (11/3/2017 10:34:02 PM)

Aha! Thank you so much, michaelm75au, that did the trick. My issue wasn't with units spawning over land—as long as any pair of coordinates are present, it will spawn to base—but whatever you did made the counter work so I can make Merchant #1, #2 in order to track the total...

Thanks to somi83 as well—your script works too, but I'm only trying to create one merchant at a time. My plan is to use the combination of base/mission tables to regularly feed merchant traffic into support missions in a steady stream for an operational scenario potentially lasting weeks.

math.randomseed( os.time() )

local PORTTABLE = {'Singapore', 'Singapore', 'Singapore', 'Shanghai', 'Shanghai', 'Shenzhen', 'Hong Kong' }
local PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
local DBIDTABLE = { 259, 275, 339, 145, 144, 2776, 2775, 2774, 2773, 2023, 2031, 2029, 2030, 2028, 2027, 339, 2023, 2027 }
local DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
local MISSIONTABLE = {'Xiamen', 'Kaohsiung', 'Busan', 'Dalian'}
local MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]

lastCount = ScenEdit_GetKeyValue('MerchCounter');
lastCountN = tonumber(lastCount);
if lastCountN == nil then
	lastCountN = 0;
end
lastCountN = lastCountN +1;
ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
b=ScenEdit_GetKeyValue('MerchCounter');

local new_merch 
if b ~= 0 then 
local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT, long=0, lat=0}) 
ScenEdit_AssignUnitToMission( new_merch.name, ''..PORT..' to '..MISSION..'') 
print (new_merch.name..' with dbid '..DBID..' was assigned route '..PORT..' to '..MISSION..'') 
end 




jmax -> RE: Merchant creation script not working? (11/6/2017 11:40:03 PM)

Works well now, though occasionally the event is fired without creating a merchant for reasons unknown. Pretty satisfying watching the shipping lanes fill up with realistic traffic—now I just need to impose some limits so it doesn't overload my CPU after running for a while.

math.randomseed( os.time() )
a = math.random(1,4)
if a == 1 then
	local PORTTABLE = {'Singapore', 'Singapore', 'Singapore', 'Singapore', 'Manila' }
	local PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
	local DBIDTABLE = { 259, 275, 275, 339, 339, 339, 145, 145, 145, 144, 144, 2776, 2775, 2774, 2773, 2029, 2030, 2028, 2027, 2023, 222 }
	local DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
	local MISSIONTABLE = {'Ningbo', 'Ningbo', 'Ningbo', 'Busan', 'Busan', 'Hong Kong', 'Shanghai', 'Dalian', 'Yokohama', 'Kaohsiung' }
	local MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]
	lastCount = ScenEdit_GetKeyValue('MerchCounter');
	lastCountN = tonumber(lastCount);
		if lastCountN == nil then
		lastCountN = 0;
		end
	lastCountN = lastCountN +1;
	ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
	b=ScenEdit_GetKeyValue('MerchCounter');
	local new_merch
	if b ~= 0 then
	local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT, long=0, lat=0}) 
	ScenEdit_AssignUnitToMission( new_merch.name, ''..PORT..' to '..MISSION..'') 
	print (new_merch.name..' with dbid '..DBID..' was assigned route '..PORT..' to '..MISSION..'') 
	end 
end
--^Tanker heavy
if a == 2 then
	local PORTTABLE = {'Xiamen', 'Manila', 'Manila'}
	local PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
	local DBIDTABLE = { 2776, 2775, 2775, 2774, 2774, 2774, 2773, 2773, 2023, 2023, 2030, 2028, 2027, 145, 144 }
	local DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
	local MISSIONTABLE = {'Singapore', 'Singapore', 'Shanghai', 'Shanghai', 'Ningbo', 'Yokohama', 'Kaohsiung', 'Hong Kong', 'Busan' }
	local MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]
	lastCount = ScenEdit_GetKeyValue('MerchCounter');
	lastCountN = tonumber(lastCount);
		if lastCountN == nil then
		lastCountN = 0;
		end
	lastCountN = lastCountN +1;
	ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
	b=ScenEdit_GetKeyValue('MerchCounter');
	local new_merch 
	if b ~= 0 then 
	local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT, long=0, lat=0}) 
	ScenEdit_AssignUnitToMission( new_merch.name, ''..PORT..' to '..MISSION..'') 
	print (new_merch.name..' with dbid '..DBID..' was assigned route '..PORT..' to '..MISSION..'') 
	end 
end
--^Bulk heavy
if a == 3 then
	local PORTTABLE = {'Shanghai', 'Shanghai', 'Hong Kong', 'Hong Kong', 'Ningbo', 'Busan', 'Kaohsiung' }
	local PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
	local DBIDTABLE = { 2031, 2029, 2030, 2030, 2030, 2028, 2027, 2774, 2773, 2023, 2023, 2034, 1599 }
	local DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
	local MISSIONTABLE = {'Singapore', 'Singapore', 'Xiamen', 'Manila', 'Yokohama', 'Yokohama' }
	local MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]
	lastCount = ScenEdit_GetKeyValue('MerchCounter');
	lastCountN = tonumber(lastCount);
		if lastCountN == nil then
		lastCountN = 0;
		end
	lastCountN = lastCountN +1;
	ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
	b=ScenEdit_GetKeyValue('MerchCounter');
	local new_merch 
	if b ~= 0 then 
	local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT, long=0, lat=0}) 
	ScenEdit_AssignUnitToMission( new_merch.name, ''..PORT..' to '..MISSION..'') 
	print (new_merch.name..' with dbid '..DBID..' was assigned route '..PORT..' to '..MISSION..'') 
	end 
end
--^Container heavy
if a == 4 then
	local PORTTABLE = {'Hong Kong', 'Shanghai' }
	local PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
	local DBIDTABLE = { 2774, 2773, 2023, 2023, 2030, 2027, 2027, 145, 144, 1599 }
	local DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
	local MISSIONTABLE = {'Manila', 'Ningbo', 'Yokohama', 'Xiamen', 'Kaohsiung', 'Busan' }
	local MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]
	lastCount = ScenEdit_GetKeyValue('MerchCounter');
	lastCountN = tonumber(lastCount);
		if lastCountN == nil then
		lastCountN = 0;
		end
	lastCountN = lastCountN +1;
	ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
	b=ScenEdit_GetKeyValue('MerchCounter');
	local new_merch 
	if b ~= 0 then 
	local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT, long=0, lat=0}) 
	ScenEdit_AssignUnitToMission( new_merch.name, ''..PORT..' to '..MISSION..'') 
	print (new_merch.name..' with dbid '..DBID..' was assigned route '..PORT..' to '..MISSION..'') 
	end 
end
--^Mixed




bearhunter007 -> RE: Merchant creation script not working? (11/8/2017 8:40:43 PM)

Nice script there jmax. Always helps to look at others code to learn from.

Cheers




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.75