Lua for counting mines

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
Correcaminos
Posts: 210
Joined: Sun Jun 01, 2025 7:25 am

Lua for counting mines

Post by Correcaminos »

I was wondering if there's a way to count destroyed mines using Lua, as the game treats them as weapons instead of units. I was thinking of developing a mine-clearing Lua where destroying a certain number of units triggers an event. Any ideas? I've tried a few things, but I'm not sure how CMO handles them in the game.
"Si vis pacem, para bellum."
User avatar
Correcaminos
Posts: 210
Joined: Sun Jun 01, 2025 7:25 am

Re: Lua for counting mines

Post by Correcaminos »

Just following up on my own question. I finally got a working script to count destroyed mines

Since CMO treats mines as 'Weapons' rather than standard units, standard unit triggers don't always catch them properly.

Here is a ready-to-use guide and script if anyone needs it for their own scenarios.

How to set it up in the Editor:

Go to Events and create a new Event (e.g., "Count Destroyed Mines").

Create a Trigger: Select "Unit is Destroyed". Set the Target Filter to "Weapon" and select the enemy side (e.g., IRAN).

Create an Action: Select "Lua Script" and paste the code below.

The Script:

Lua
-- 1. Get the unit that triggered the event
local u = ScenEdit_UnitX()
if not u then return end

-- 2. Define basic variables
local side = u.side:upper()
local type = u.type
local n = (u.name or ""):lower() .. " " .. (u.classname or ""):lower()

-- SETUP: Change these variables to match your scenario
local sideToScore = 'USA' -- The side that gets the points
local targetSide = 'IRAN' -- The side that owns the mines
local currentScore = ScenEdit_GetScore(sideToScore)
local scoreChange = 0
local msg = ""

-- 3. Check if the destroyed weapon is a mine (add your own keywords if needed)
if side == targetSide and type == 'Weapon' and (string.find(n, "mine") or string.find(n, "sadaf") or string.find(n, "mdm") or string.find(n, "m-08")) then
scoreChange = 10 -- Points awarded per mine
msg = "MCM SUCCESS: Mine cleared (+10 pts)"

-- 4. Persistent Counter using KeyValue store
local KV_MINE_COUNT = "enemy_mine_kill_count"
local currentCount = tonumber(ScenEdit_GetKeyValue(KV_MINE_COUNT)) or 0
currentCount = currentCount + 1

-- Save the new count (this survives saving/loading the game)
ScenEdit_SetKeyValue(KV_MINE_COUNT, tostring(currentCount))

-- Log to the message log
print("[MCM INTEL] Enemy mine detonated/cleared. Total cleaned: " .. currentCount)

-- Update score
ScenEdit_SetScore(sideToScore, currentScore + scoreChange, msg)
end



This creates a hidden counter (enemy_mine_kill_count) that tracks the progress perfectly. You can easily adapt the side names, points, and mine keywords to fit your scenario.
"Si vis pacem, para bellum."
Nikel
Posts: 2804
Joined: Tue Mar 24, 2009 10:51 am

Re: Lua for counting mines

Post by Nikel »

Thanks Correcaminos.

Have you noticed that some mines are counted as losses, others as expenditures and others as both?

From the testing of v2.5

SIDE: IRAN
===========================================================

LOSSES:
-------------------------------
8x 23mm ZU-23-2
1x 901 Taregh [PL-877EKM Kilo]
3x 942 Ghadir [IS-120]
1x Behshad
2x Building (Sector Control Station)
4x C-802 Triple
2x C-802 Twin
24x CM-200 Ghader Twin
6x Fattah-2 TEL
24x Infantry Section [7.62mm MG/Unguided Infantry Anti Tank Weapon]
4x Khalij Fars [Fateh 110 Mod] TEL
4x Mine [Floating, Contact Fuze]
1x Mine [MDM-5]
12x Misagh-1 MANPADS
5x Mohajer-6 UAV
10x Peykaap II [Bavar]
3x Peykaap III [Zolfaghar]
10x Peykaap III [Zolfaghar]
1x Radar (Ghadir OTH-B)
1x Radar (Sepehr OTH-B)
6x SA-15b Gauntlet [9A331] TELAR
16x Sevom Khordad TEL
8x Sevom Khordad TELAR
5x Shahab-1 TEL
5x Shahed-129 UCAV
12x Shahed-136 TEL
2x Shahed-191 UCAV
4x Talash-4 TEL
19x Toragh [Boghammar Mod]
2x Vehicle (Bashir Radar)
2x Vehicle (C2/C3)
7x Vehicle (China Type 352 Square Tie)
2x Vehicle (Najm-802)
4x Zolfaghar Basir ASBM TEL
13x Zulfiqar Air Defense Boat


EXPENDITURES:
------------------
350x 23mm ZU-23-2 Burst [20 rnds]
6x Fattah-2 [200kg HE]
6x Fattah-2 [200kg HE] HGV
2x Generic Acoustic Decoy
1x Mine [Floating, Contact Fuze]
1x Mine [MDM-5]
1x Mine [SADAF-02]
35x Misagh-1
13x Shahed-136
2x TEST-71ME
4x YT-534-UW1 Valfajr [PT-97W]
4x Zoubin
User avatar
Correcaminos
Posts: 210
Joined: Sun Jun 01, 2025 7:25 am

Re: Lua for counting mines

Post by Correcaminos »

That exact inconsistency in how the game logs them (sometimes as losses, sometimes as expenditures, or both) is precisely why we made the script this way, filtering specifically by the 'Weapon' type and name.

This way, we ensure a clean and accurate count for scoring purposes without relying on the game engine's native after-action reports
"Si vis pacem, para bellum."
Post Reply

Return to “Lua Legion”