Command Modern Air Naval Operations : LUA Code Repository #1
This is where I’ll be documenting each and every Lua script that I’m using for both my own simulations and for the Hired Goons LP. Most of these are shamelessly used and when applicable I’ll try to attribute the original author. Though in some cases it’s just a script off of Discord or the Matrix Forums. Eventually I’d like to have a functional example for each and every function in CMANO. For starters you can find every CMANO function at https://commandlua.github.io/ . Most everything works as is posted at the Github site, but some things don’t. Hence my idea to have a working script repository.
Also, do things one step at a time. First prove out the easy things, adding a unit, assigning a mission, then progress to harder stuff, looping through assignments, setting positions. That way when you encounter a problem you aren’t trying to digest it all. How do you eat a cake? One bite at a time.
CMANO has two paths to make functional Lua. One involves the Event Editor and the other is the Lua Script Console. Our first example below will use the Lua Script Console. Further down we’ll put that into the Event Editor system and watch it work. Simple right?
The Design Path
- Make a non-combat scenario where the player must navigate through the Strait of Malacca, without Radar, at night, and not collide with civilian traffic.
- Detect when one ship is within a “collision” circle of another ship.
- Detect if the player turns on radar.
- Detect when the player crosses the exit.
So first off we’ll start with the tricky one. The circles. For now we’ll do one ship and go from there. Luckily Baloogan and a Gentleman named Michal Pielaszkiewicz made a handy little function to do exactly this. I’ve modified it to include the ability to add a reference ship so it will follow our boat.
Create a blank scenario in CMANO, set one side to be Merchants, the other to the USN. Select the Merchant side and then call up the Lua script console. Copy the draw_circle_ref function and paste it into the console. Hit enter. Hopefully you don’t see any errors.
if txt == nil then
txt = ""
end
t = t
tg = t - 1
lat1 = a.latitude
lon1 = a.longitude
r = b / 60
for i=0,tg do
th = 2 * math.pi * i / t
rlat = lat1 + r * math.cos(th)
rlon = lon1 + r * math.sin(th) / math.cos(math.rad(lat1))
ScenEdit_AddReferencePoint({
side=ScenEdit_PlayerSide(),
lat=rlat,
lon=rlon,
name=txt,
relativeto=unitname,
bearingtype=1,
highlighted="yes"})
end
end
This has loaded the function into memory. Now we can reference it below. Now create a unit (INS key) and name it Ship1.
draw_circle_ref({latitude=unit.latitude, longitude=unit.longitude}, .10, 10, "COLLISION", unit.name)
Copy the above code and stick it into the console. Hit enter. You should see 10 points circle Ship1 named COLLISION. Even better they will follow Ship1 as it goes about its business.
Now we need to tie it into a trigger to see when another ship (the players ship) enters into that area. So go to your Event Editor – > Triggers, and we’ll create a “Unit Enters Area” trigger. Note that this trigger is omniscient, even if the target can’t see it the trigger will execute. A Unit is Detected trigger can sometimes be better as you may reward the player for sneaking under the radar or some such.
We’ve created our trigger, the target side is the USN. You can leave everything else blank and any unit that meets the criteria would trip the trigger. In my case I narrowed down for explanations sake. Then I pick the area to be inside. Be sure to Validate the area as sometimes it can criss-cross itself.
Next we will set an action. We’ll ignore conditions for now.
This is a message that will pop-up to the player. You can absolutely set multiple actions for a single trigger. So we could deduct points, activate a mission, or even execute some Lua scripting.
Putting it together you open up the Event Editor and define the trigger and action.
You could set the probability for the event to occur even if the trigger is met, you can make it repeatable, active just means it will still happen and shown in log will mean you, and possibly the player, can see it down the road. I usually disable stuff I don’t want the player to know about.
So lets steer that Arleigh Burke into a container ship!
There we are!
Radar Detection
This one gets trickier. We don’t actually have a trigger that falls into this nicely so we have to cheat a little bit. We’ll add a neutral side and put an ELINT aircraft in the air. This plane will see nothing unless the player goes radar active. For this one we’ll use the “Unit is Detected” trigger. Then we can set a message, drop the points, and end the scenario. We could also start the scenario with the radar’s damaged, but I’d rather give the player the opportunity to sneak a look and probably get caught.
We’ll run it and prove it out. Sometimes if I have a working model I’ll do a save as and increment versions. Test 1.0, Test1.1, Test 1.2 etc. That way when you totally shit the bed with a version you can go back to one that works.
Exit Stage Left
Finally we want to detect when the player crosses a certain zone. Make a few points, add a “Unit Enters Area” trigger, and instead of failure messages add some points and end the scenario. They’ll be greeted with a success.
We’ve got a pretty simple example here. Ideally we’d have a ridiculous quantity of shipping moving in and out of these straits. In case you haven’t guessed we’re recreating the incident that happened in the Straits of Malacca where an Arleigh Burke destroyer collided with a container ship. Not a single shot was fired and yet it shows an opportunity for CMANO to shine.
Future repository posts will be less verbose and more Lua or trick focused. Please contact me on The Strategy Gamer Discord or the CMANO Discord if you have a Lua script to share.
Pingback: Command Modern Air Naval Operations : Lua Repository 2 - The Strategy Gamer