Lua - Paradrop (Full Version)

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



Message


Gunner98 -> Lua - Paradrop (7/24/2016 3:50:54 PM)

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: http://www.matrixgames.com/forums/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




ckfinite -> RE: Lua - Paradrop (7/24/2016 5:26:56 PM)

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.






Gunner98 -> RE: Lua - Paradrop (7/24/2016 5:59:01 PM)

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




ckfinite -> RE: Lua - Paradrop (7/24/2016 6:19:28 PM)

quote:


-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.

quote:

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.

quote:


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.

quote:


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.




Gunner98 -> RE: Lua - Paradrop (7/24/2016 8:43:52 PM)

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




michaelm75au -> RE: Lua - Paradrop (7/25/2016 10:43:50 AM)


quote:

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'.




Gunner98 -> RE: Lua - Paradrop (7/25/2016 12:13:24 PM)

Sure

Here is the scenario as is.

B




ckfinite -> RE: Lua - Paradrop (7/25/2016 1:11:04 PM)

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()".




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
3.71875