Spawning units

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

Spawning units

Post by Gunner98 »

Hay guys

Looking for some fine tuning on this one.

Able to spawn some militia based on this script Michaelm helped me with last year:

------Random unit
local unit = ScenEdit_UnitX()
local unit = ScenEdit_GetReferencePoint({side='WP',name='spawn'});print(unit)
local PlatoonNumber = ScenEdit_GetKeyValue("AssaultForce");print("Militia #: ");print(PlatoonNumber);
local lastN = tonumber(PlatoonNumber);
if lastN == nil then
lastN = 0;
end
lastN = lastN +1;
local world = -999999
--rp = ScenEdit_GetReferencePoint({side='WP',name='spawn'});print(rp)
local new ={}
local retries = 50
while world <0 and retries > 0
do
v = math.random () / 100; y = math.random() /100;print(v);print(y)
new.lat= tonumber(unit.latitude)+v-y;new.lon=tonumber(unit.longitude)-v+y
world = World_GetElevation({latitude=new.lat, longitude=new.lon});print(world)
retries = retries - 1
end
if world >0 then
local Pl =ScenEdit_AddUnit({type = 'Facility', name = 'Militia #' .. lastN,
heading = 045, dbid = 626, side = 'WP', Lat=new.lat, Lon=new.lon});print(Pl)
ScenEdit_SetKeyValue("AssaultForce", tostring(lastN)); -- last id added successfully
end

I'd like to do two things with it but am not entirely sure how:
1. Would like to losten up the spawn point a bit. Right now they come on very tight to the RP, would like to randomize that up to about 5 miles in any direction.
2. Would like to assign the newly spawned unit to a mission

The script above counts the number of units and will stop at 50 - that's OK and I'd like to keep that but it is not critical - key is to losen up the spawn location and assign to a mission.

Thanks

B
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Spawning units

Post by michaelm75au »

Assigning to mission should be simple.
Just use: ScenEdit_AssignUnitToMission (unitname, mission, escort)

Example
....
local Pl =ScenEdit_AddUnit({type = 'Facility', name = 'Militia #' .. lastN,
heading = 045, dbid = 626, side = 'WP', Lat=new.lat, Lon=new.lon});print(Pl)
ScenEdit_SetKeyValue("AssaultForce", tostring(lastN)); -- last id added successfully
local ok = ScenEdit_AssignUnitToMission (P1.guid, "Doing something on existing mission")
if ok == false then
print('Unable to add to mission');
end
...
Michael
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Spawning units

Post by michaelm75au »

For the distance, you need to find a nice fudge factor for v,y. Workout a suitable number of degrees to add to the Lat/Lon numbers, and randomize on that.
...
v = math.random () / 100; y = math.random() /100;print(v);print(y)
new.lat= tonumber(unit.latitude)+v-y;new.lon=tonumber(unit.longitude)-v+y
...
Try dividing by 10 and see how spread out that is.
Michael
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Spawning units

Post by Gunner98 »

Thanks will try those tonight
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Spawning units

Post by michaelm75au »

BTW, I am looking at one of the NF scenario (#3) and getting overwhelmed by all the messages about ships sinking and needing to send help.
The main problem being I can't find where the ships are. [:D]
So to help with this, I have added an option to the 'ScenEdit_SpecialMessage' to allow you to specify a location so the 'jump to' on the message popup can take you there.
I know you don't use Lua script as the action in most of these cases, but it may be useful in future to use the Lua function rather than the Event Action message.
Michael
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Spawning units

Post by Gunner98 »

If there is any scenario screaming for more Lua - its that one! I will eventually go back and rebuild it and could definitely use that function.

I am using the Lua message function now whenever it makes sense to do so. Long messages with quotes, bold, italics and stuff (although I am sure there is a way to do it in Lua), or where it is linked to standard (non lua) action is the only place I tend to use Action Messages now.

That function would be fantastic.

Bart
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Spawning units

Post by Gunner98 »

and getting overwhelmed by all the messages

BTW - that was exactly the feeling I was hoping to send [;)] -- a not so quiet Sunday morning shift [8D]
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Spawning units

Post by Gunner98 »

OK the distance works great. Thank you.

When adding to a mission however I get the error:

ERROR: [string "Console"]:25: attempt to index global 'P1' (a nil value)

------Random unit
local unit = ScenEdit_UnitX()
local unit = ScenEdit_GetReferencePoint({side='WP',name='spawn'});print(unit)
local PlatoonNumber = ScenEdit_GetKeyValue("AssaultForce");print("Assault Pl #: ");print(PlatoonNumber);
local lastN = tonumber(PlatoonNumber);
if lastN == nil then
lastN = 0;
end
lastN = lastN +1;
local world = -999999
--rp = ScenEdit_GetReferencePoint({side='WP',name='spawn'});print(rp)
local new ={}
local retries = 50
while world <0 and retries > 0
do
v = math.random () / 10; y = math.random() /10;print(v);print(y)
new.lat= tonumber(unit.latitude)+v-y;new.lon=tonumber(unit.longitude)-v+y
world = World_GetElevation({latitude=new.lat, longitude=new.lon});print(world)
retries = retries - 1
end
if world >0 then
local Pl =ScenEdit_AddUnit({type = 'Facility', name = 'Militia #' .. lastN,
heading = 045, dbid = 626, side = 'WP', Lat=new.lat, Lon=new.lon});print(Pl)
ScenEdit_SetKeyValue("AssaultForce", tostring(lastN)); -- last id added successfully
local ok = ScenEdit_AssignUnitToMission (P1.guid, "Militia Atk")
if ok == false then
print('Unable to add to mission');
end


Any thoughts?
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Spawning units

Post by michaelm75au »

It is the result of "Pl =ScenEdit_AddUnit(...".
Must have misread the variable as P1.
Is it Pl? - P lowercase L
Michael
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Spawning units

Post by Gunner98 »

Got it thanks. I should have caught that one.

Armed and dangerous now [:D]
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
Post Reply

Return to “Lua Legion”