CommandOps
A player created website for CMANO - Command: Modern Air/Naval Operations
Useful Links:
CMANO Database Viewer CMANO Lua Docs Matrix CMANO Forum baloogancampaign.com inst Web Based Designer Command Theater AI - Ares v1.0 (forum post) Downed Pilot/Survivor Script Lua Scoring Example Video - CMANO basics Starter Scenario Special ActionsLua Code Samples
Below are some handy Lua code samples. Please note that they may need some interpretation and may not work perfectly as is due to a variety of reasons, but hopefully they can give you some ideas.
Remove Sensors from all Air Units with a Specific DBID
local a,b,c,d = W_GetSideUnitTotalsTable('Blue') --special function in starter scen
for k,v in ipairs(a) do
local u = ScenEdit_GetUnit({guid=v})
if u.dbid == 310 then
print(u.name)
ScenEdit_UpdateUnit({guid=v,mode='remove_sensor',dbid=937})
ScenEdit_UpdateUnit({guid=v,mode='remove_sensor',dbid=444})
end
--print(u.name)
end
Create a handful of sides via script
local W_sides ={ {side='Blue',awareness='Normal',proficiency='Regular'}, {side='Red', awareness='Normal',proficiency='Regular'},{side='Targets',awareness='Blind',proficiency='Regular'},{side='Bios',awareness='Blind',proficiency='Regular'},{side='Survivors',awareness='Blind',proficiency='Regular'} }
--proficiency values - Novice, Cadet, Regular, Veteran, Ace
function W_SetupSides()
for k,v in ipairs(W_sides) do
print('Creating side: '..v.side)
ScenEdit_AddSide({side=v.side})
ScenEdit_SetSideOptions({side=v.side,awareness=v.awareness,proficiency=v.proficiency})
end
ScenEdit_SetSidePosture ('Blue', 'Red', 'H')
ScenEdit_SetSidePosture ('Red', 'Blue', 'H')
ScenEdit_SetSidePosture ('Survivors', 'Blue', 'F')
ScenEdit_SetSidePosture ('Blue', 'Survivors', 'F')
ScenEdit_SetSidePosture ('Blue', 'Targets', 'U')
ScenEdit_SetSidePosture ('Targets', 'Blue', 'U')
ScenEdit_MsgBox ('The sides should be setup, please double check them. Also you should change the Bios, Targets and Survivors to Computer only. If you plan to use the Create Targets Special Action I think it works better with Collective Responsibility set to No.', 1)
end--function end
W_SetupSides()
Add a handful of units, add some to a group, change WCS to engage opportunities - yes. Using this to pretend to offload units from a ship and set them up where they are needed.
Change proficiency of AC by randomly selecting a set number of Flight names from a table.
Better logic for returning ground units when they run out of ammo. If unit is armor or artillery ignore small arms in ammo count. Return if total ammo is less than 5.
Add weapon/ammo to an ammo pad. Trick is that the ID listed in the add weapon UI is not the DBID - you need to search for Weapons in the Weapons DB.
Check land units on a mission to see if they still have ammo - if not return them to a specific location.
Manpads on mission - stop every minute (40% of the time), stay stopped if firing at something.
Get units from a particular mission and change their speed. For instance to stop manpads so they can actually shoot. Probably need to turn their speed on and off at an interval, otherwise they never stop and can never shoot.
Special action for enemy losses - start with known pre war stats, subtract current number of ships,subs and ac to get losses.
loop thru cargo on a pier and unload it. Unloads all of one type into one unit. If you have something like a SA-10 it doesn't have radar, somehow need to have that in the cargo and then pair it together so it could work?
Simple OOB of enemy contacts - just listing AC and SAMs so far. Could make this a special action.
Move 2 ref points a random number of miles from where they are, in this case only changing the latitude.
Check a group of units detectability - can optionally only change some units based on their name like 'runway'.
Function to make a simple airbase.
Print out unit name and dbid of units in a group.
Change a group/base to a different side.
Check to see if an AC is on the ground and if so change its mission and loadout. Airbornetime is a string for some reason.
After cargo drop off, assign units to go to certain co-ordinates - assuming they are recon units.
World_GetPointFromBearing example. Can add a ref point there, or use to set course waypoint.
Working on AI ground units that return to an area when they are out of ammo. Would use this with a trigger of every x minutes once the cargo drop takes place? and maybe checking an area for the units? not sure how to get UnitX yet. Could build a table as the units are dropped off but you wouldn't be able to save it as a key value. Or could maybe rename the unit as it is dropped off by the cargo mission - that gives you unitX, then the name could have a specific part to it which would allow you to loop thru all land units using sting.match to single out the land units you want to control in this way.
Playing with the Sin function, working on a weather model.
Playing around with creating a table of weather in lua. This is basically how to make a table of tables programatically.
Take a course and modify the way points by adding or subtracting a random number. Used this as part of an event to help ships leave port and have random paths.
Exception: [string "ScenLoadOverlays"]:1: unexpected symbol near char(145) - of you see something like this where you copied code from somewhere it is possible the Quote mark is an illegal character - it is very hard to see, but some quotes are curly and these are not actual quotes as far as lua is concerned.
Example of changing path of AI ship to get it out of a land locked port. You would need to change the course to the actual course needed to get out. Use this as the lua part of an action with a trigger of unit enters area that is set around a port but not including the port (ships in port are already in the area I think). You also need to change the ignore plotted course to no or else it may go off on its own if it is engaged offensive.
Get all properties of side - as a function. Some properties look like they are functions that could be run with additional parameters.
Same - Get all properties from unit wrapper - except as a function, can pass in either just the name as a string or the unit id with the name and guid as a table.
Get all properties from unit wrapper
Working code for creating random bios and false contacts using 4 RP's in a rectangle. You can pass in the spacing and some jitter to make it less of a perfect grid and control how close together the units are. The script will set up a sea control patrol for the bios to be in if it does not exist. You need to set up a side called 'Biologics' set to computer only and Blind awareness.
Not quite perfect code to lay out bios or false contacts in a grid with some jitter on the angle and distance apart. having trouble with the depth - they get assigned a nice random depth but then go to something else. Seems to be different based on if they are on a mission or not (sea control). Places a Cessna if the depth is not right, need to delete those at the end.
Function to place a ship/sub/group at a random distance from its current location. You can set the minimum acceptable depth as well as a bearing range to control which direction - if it is 1 and 360 then it can be anywhere around. If it is 180 and 270 then it would be anywhere southwest of the current location.
Get table of guids for units in a given group.
If all 8 Orel bombers are dead then change sub to nuke strike mission.
Set a subs diesel engine to medium damage. Just need the last line, first part is to see what components there are. 2 apparently means medium damage. Could also be "none" for no damage.
Print damaged unit info to lua log. Create triggers for each side you want to show in the log. Use snaketail log viewer as supplemental log viewer.
Print destroyed unit info to lua log - [Red] <<-- change to side name it is for. Also prints current score.
Get total number of units for a side, and break down of how many ships/subs/AC/facilities. This is a memory hog if there are a lot of units, game restart may be needed afterwards. For me 1100 units sucks up 300 or so MB, so after running it a couple times I need to restart the game to free the memory.
Change all groups and units from one side to another, basically merging 2 sides. The side you are changing to must exist. It is setup as 2 separate functions, one that changes groups, and then one that changes units after that. When I tried to do them at the same time it would always error at some point. Caveats: barely tested, uses up significant memory (150MB on a few hundred units).
In game timer using Special Actions. Create a Special Action named Timer and use this in the lua part.
Check if a unit is damaged and set to Unavailable loadout if it is. Could use this on a trigger of Unit enters Area around an Airfield, then when the unit lands if it is damaged set it to Unavailable rather than let it be repaired.
Not 100% sure about this as sometimes the startdp seems to be 0?
Lua to see if a string contains a sub string. Works well except for special characters (such as .()[]+-) which have to be escaped by putting a % in front of them.
This could be used as a score keeping mechanism - all scoring could be in one bit of lua, comparing the UnitX.classname to things - so if classname contains F-15 then score = 10, or reaper then score = 3. F-15 would need to be F%-15.
Get contents of an existing trigger.
Mission status reverser - if the mission is active, make it inactive. If it is inactive, make it active. Use this in an event to turn on and off a mission at a regular time.
Adding a unit to an escort mission, you just have true - no escort = true
Set a units loadout and ready time.
updated code for random bios. Sets a random speed between 0 and 4 and a random depth (max 270m or so). Use with a mission named 'Whales' set to patrol - sea control.
Change current fuel on a unit to a random value around 60-80% of max fuel for that unit. Each unit has a type of fuel for itself, and sometimes other types of fuel for units it hosts. You have to match the correct type of fuel. This example is for an aircraft, with only one type (AviationFuel) which is type 2001. The fuel object will tell you max fuel and current fuel, you'll want to change current fuel.
Rename a unit using their guid - can also use their name but if the name is not unique you'll probably have trouble.
Get the name and guid of all hosted units - Aircraft or Boats, but you need to specify one it looks like.
Take a list of units and change them from one side to another. If the units are in a group you can just pass in the group name. Doesn't seem to work with bases.
Spawn units at random locations within an area.
Unloading cargo from a pier to random locations within an area. Uses the same code for random merchants more or less. By putting in your lat/long as 4 digits with no decimal - 1234 instead of 12.34 and then dividing by 100 you can get a more precise area. You need to know the dbid of the cargo (and the name of the pier), and what the unit will be called, the first one is like `inf #100` and then they go in sequence. Can only do one db type at a time.
Set a units course, assumes you know the unit name or guid. This example is for a land unit but I think it would work for a ship or maybe ac if they were airborne? First it gets the current location, and uses that as one end of the course to be set.
Unload cargo from unit - requires the name of the port/unit with the cargo as well as the dbid of the cargo you want to unload - and the qty of that unit. So rather than the cargo all unloading into one unit you can control how many get stuck together. The unit unloaded gets a generic name like Mech Inf #100 which seems to go in sequence.
Weather function - returns weather report, not just cloud number. You can run this every hour or 4 hours to give a current weather report. Some of the message is hard coded (area I think).
Random ship/merchant/fish generator
Cargo - Here is an example to transform different type of units as they disembark (Mech, Inf, Armored, Arty)
Weather: Night and Morning Low Clouds Part 2 - This goes in an event that triggers every 30 minutes.
Weather: Night and Morning Low Clouds Part 1 - This is the setup for the weather, goes in the `On Scenario
Loaded` event