Lua - Paradrop

Post new mods and scenarios here.

Moderator: MOD_Command

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

Lua - Paradrop

Post by Gunner98 »

This is primarily for CK and michaelm but if any other Lua wizard is out there, please chime in.

Looking to do a para drop. 4x DZ, 4x Coy, 2 C-130's per DZ.

Based on your conversation here: tm.asp?m=4092628

I think I'm on the right track but have a couple questions


Event: Scenario Initialised

-- set up keys
ScenEdit_SetKeyValue("Recon/A", "dbid=714, maxNum=4, curNum=0");
ScenEdit_SetKeyValue("Recon/B", "dbid=714, maxNum=4, curNum=0");
ScenEdit_SetKeyValue("Recon/C", "dbid=714, maxNum=4, curNum=0");
ScenEdit_SetKeyValue("Recon/HQ", "dbid=714, maxNum=4, curNum=0");

Event: Drop troops and recall Hurc This will have to be done once for each DZ

local drop = ScenEdit_GetKeyValue("Recon/A");print("Drop: "..drop );
local t = {}
for k, v in string.gmatch(drop, "(%w+)=(%w+)")
do
t[k] = v
end
-- deploy one unit
local lastN = tonumber(t['curNum']);
if lastN == nil then
lastN = 0;
end
lastN = lastN +1;
if lastN > tonumber( t['maxNum']) then
ScenEdit_MsgBox('All troops landed',0)
return
end

-- deploy 2nd unit This is where I am not sure, each Hurc will carry 2x Pl this is the 2nd
local lastN = tonumber(t['curNum']);
if lastN == nil then
lastN = 0;
end
lastN = lastN +1;
if lastN > tonumber( t['maxNum']) then
ScenEdit_MsgBox('All troops landed',0)
return
end

local unit = ScenEdit_UnitX()
local world = -999999
local new ={}
local retries = 10
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
unit.altitude = world

if world >0 then
local veh =ScenEdit_AddUnit({type = 'Facility', name = 'AirDrop #' .. lastN,
heading = 0, dbid = t['dbid'], side = 'RAN', Lat=new.lat, Lon=new.lon});print(veh)
local b="curNum="..tostring(lastN)
local a=string.gsub(drop,"curNum=%d", b)
ScenEdit_SetKeyValue("CanberraAir", a); -- last id added successfully
end

-- unable to change loadout unless landed
-- if loadout could be interrogated and changed, then only troop carrying loadout could be checked with this script
-- and once troops deployed, an 'empty' loadout substituted
--ScenEdit_SetLoadout ({guid = unit.guid, loadoutid=9277, timetoready_minutes=0, ignoreMagzines=true})

Also in the Sandbox scenario you had a condition, which told a unit to wait if there is another in the process ahead of it. I don't want that but is it needed to ensure there is no Lua conflict or something?

Thanks guys - This is just a trial run before the major Regt size Amphib ... youch...

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/
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: Lua - Paradrop

Post by ckfinite »

First off, I would suggest using my KeyValue library to make the code easier to write - no need for all the gsub anymore, the library does that. You can get it here. There's two more libraries that will make writing this easier, my math library and my scene utilities library, here and here, respectively. You'll need to put those into the scene initialized event, with the key store library being loaded first.

Now, for the actual coding:

Don't do initialization of units in scene initialized. If you do, then when the scene is reopened, those initialization events will fire again. With the libraries, you can write

for i=1,8 do
ScenEdit_GetUnit({name="UNIT NAME HERE #"..i,side="YOUR SIDE HERE"}).spawn = {{name="Recon Airdrop", dbid=714, num=2}}
end

Just run this in the script console before you save the scene, and those values will stick (be warned, the libraries need to run first, so add them first, then reopen the scene). You will also need the following line in the initialization event, along with the libraries:

Units.init()

Then, in the unit entered area drop script, you then have

local function DropUnit(point, side, name, type, dbid, error, tries)
for i=1,tries do
local center = (Quaternion.fromEuler(math.random()*error,0,0)*Quaternion.fromEuler(0,0,math.random() * 2 * math.pi)) * Quaternion.new(point)
rp = center:toPoint()
if World_GetElevation(rp^{}) >= 0 then
return ScenEdit_AddUnit(rp^{side=side, name=name, dbid=dbid, type=type})
end
end
end

local unit = ScenEdit_UnitX()
for _,u in ipairs(unit.spawn) do
for i=1,u.num do
DropUnit(Point.new(unit), "YOUR SIDE NAME HERE", u.name, "Facility", u.dbid, 1/6371, 10)
end
end



This script shouldn't care if there's another ahead of it - though sadly, SetLoadout doesn't work for flying aircraft for some reason. I've attached a demo scene.


Attachments
Test scene.zip
(12.16 KiB) Downloaded 12 times
User avatar
Gunner98
Posts: 5966
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua - Paradrop

Post by Gunner98 »

Thanks CK

Ran your test, worked. Couple observations:

-the game seemed to run quite slow, is that a result of the libraries?
-there were a couple RPs created (Foo Bar) is that a by product
-The units dropped quite close together, can I change those values in the script?
-The units were difficult to select afterwards, kinda weird, had to scope right in to get them then drag to select could not click.

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/
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: Lua - Paradrop

Post by ckfinite »

-the game seemed to run quite slow, is that a result of the libraries?

Shouldn't be - they only run when the unit enters the area.
there were a couple RPs created (Foo Bar) is that a by product

Sorry, that was accidentally included from my debugging session. The script I posted above doesn't make those RPs. If you want to modify the one in the scene, remove the line in the unit entered area event that contains ScenEdit_AddReferencePoint.
The units dropped quite close together, can I change those values in the script?

Yes, just increase the value 1/6371 - the numerator is the number of km you want them to disperse over.
The units were difficult to select afterwards, kinda weird, had to scope right in to get them then drag to select could not click.
No idea, best idea would be an artifact of the small separation between them.
User avatar
Gunner98
Posts: 5966
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua - Paradrop

Post by Gunner98 »

Thanks CK

Sorry to be a pain.

Cut the initialize action from the 'test' scenario above and put it in an action in my scenario. Loaded up and received this error

6:00:00 PM - Lua script execution error: [string "chunk"]:895: bad argument #1 to 'pairs' (table expected, got nil)
6:00:00 PM - Event: 'Scenario Startup' has been fired.
6:00:00 PM - Switched side to: USMC

Any ideas?

Tx

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: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua - Paradrop

Post by michaelm75au »

ORIGINAL: Gunner98

Thanks CK

Sorry to be a pain.

Cut the initialize action from the 'test' scenario above and put it in an action in my scenario. Loaded up and received this error

6:00:00 PM - Lua script execution error: [string "chunk"]:895: bad argument #1 to 'pairs' (table expected, got nil)
6:00:00 PM - Event: 'Scenario Startup' has been fired.
6:00:00 PM - Switched side to: USMC

Any ideas?

Tx

B
Could you post the scenario? Easier to see it 'in situ'.
Michael
User avatar
Gunner98
Posts: 5966
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua - Paradrop

Post by Gunner98 »

Sure

Here is the scenario as is.

B
Attachments
NorthernF..dDeadly.zip
(330.52 KiB) Downloaded 11 times
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/
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: Lua - Paradrop

Post by ckfinite »

There's a bug in the library code - it doesn't like being started up in a scene that doesn't have any unit data already. I've patched the bug in the attached file.

I also realized that there's a similar bug in the airdrop script. You'll need to add the line

if unit.spawn == nil then return end

just after the line that goes "local unit = ScenEdit_UnitX()".
Attachments
NorthernF..dDeadly.zip
(330.48 KiB) Downloaded 15 times
Post Reply

Return to “Mods and Scenarios”