More LUA stuff...

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
User avatar
tjhkkr
Posts: 2430
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

More LUA stuff...

Post by tjhkkr »

Here is some code in LUA for placing a unit in a random location on the map.
To use it, you would need to remove everything after the arrows: they are comments so you understand what I am doing.


r = math.random(1, 17) <== Random number
r = r - 9 <== this allows the number to be + or minus the base longitude.
r = r / 100 <== this takes the number and converts it to the decimal size I want.
longi =107.15 + r <== 107.15 is the base longitude

r = math.random(1, 17) <== Random number
r = r - 9 <== this allows the number to be + or minus the base latitude.
r = r / 100 <== this takes the number and converts it to the decimal size I want.
lati =16.7 + r <== 107.15 is the base latitude


string.strLati = lati; string.strLongi = longi <== number must be converted to a string variable.

ScenEdit_AddUnit({type = 'facility', name = string.name, heading = 0, dbid = 357, side = 'Communist Forces', Latitude= string.strLati ,Longitude= string.strLongi, autodetectable="false",holdfire="false",proficiency=UnitProf})
<== notice how I used the variables for latitude and longitude in the ScenEdit_AddUnit command.

PLEASE NOTE: the number 17 in this case was simply the maximum size on the map I wanted to place the unit. You can adjust to fit your needs.
The r=r-9 gives me a center point.
SOOOOO if you were to do 19, your r=r-10
Also I divide by 100 because that was all the farther I wanted to separate the units. Again, you may need to adjust this if you want a smaller or larger size.
=============================================
Below is the whole routine for placing a Vietcong unit in South Vietnam.
I create a string.name so I can then use it when I add it to a mission
I also have the routine give me a random unit proficiency (which makes sense given the nature of the Vietcong.
The InfVC variable is used so I can make sure that I get UNIQUE names for the units as the game progresses.
This routine when completed will generate number of infantry units once a day for the length of the game.

* WARNING * LUA is very case sensitive. I made a mistake at one point where I spelled UnitProf as unitprof...
AND it took me a while to figure out why the program was blowing up.

I am not finished with this script yet, but it did place units in a random location when I ran it from the LUA Script Console.

I sent this because I know one of your Gurus was trying to figure out how to randomly place a unit on the map, and the code ABOVE will do that.

ENJOY!
=============================================
a = 0
numKey = ScenEdit_GetKeyValue('InfVC')
numNewUnits = math.random(2,7)
workKey = numKey + 1

repeat
a = a + 1
string.name = 'VC' .. workKey
UnitProf = math.random(1,5); UnitProf = UnitProf - 1

r = math.random(1, 17)
r = r - 9
r = r / 100
longi =107.15 + r

r = math.random(1, 17); r = r - 9
r = r / 100
lati =16.7 + r

string.strLati = lati; string.strLongi = longi

ScenEdit_AddUnit({type = 'facility', name = string.name, heading = 0, dbid = 357, side = 'Communist Forces', Latitude= string.strLati ,Longitude= string.strLongi, autodetectable="false",holdfire="false",proficiency=UnitProf})

workKey = workKey + 1
until a >= numNewUnits

FinalNumKey = numKey + numNewUnits + 1
String.StrFinalNumKey = FinalNumKey

ScenEdit_SetKeyValue('InfVC', String.StrFinalNumKey)
=============================================
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
User avatar
tjhkkr
Posts: 2430
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: Using LUA to place units in a random place on the map.

Post by tjhkkr »

This one is designed to give an incentive to destroy bridges as it stops the flow reinforcements.
There are 4 bridges, the more you destroy, the less likely reinforcements will appear.
It also assigns a mission depending on where the reinforcements end up on map; to the west, it goes to YankeeWest; to the East, it goes to YankeeEast.
It assigns randomly whether to give the Chinese a T34-85 or a T55.
====
ScenEdit_SetKeyValue('BridgesInoperative', '0')
ScenEdit_SetKeyValue('ArmChina', '1')
ScenEdit_SetKeyValue('InfChina', '1')

The above is set at the beginning of the game... <===
====

numkey1 = ScenEdit_GetKeyValue('InfChina')
numkey2 = ScenEdit_GetKeyValue('ArmChina')
numbridgedest = ScenEdit_GetKeyValue('BridgesInoperative')
UnitProf = math.random(3,5) - 1

probTrainsArrive = math.random(1,4)
probTrainsArrive = probTrainsArrive - numbridgedest
if probTrainsArrive => 1 then
a = 0
workKey = numkey1 + 1
repeat
a = a + 1
string.name1 = 'PRCArmPlat' .. workKey
string.name2 = 'PRCInfPlat' .. workKey

q = math.random(1, 2)
if q == 1 then
r = math.random(1, 100)
r = r - 50
r = r / 100
longi =102.5 + r

r = math.random(1, 100);
r = r - 50
r = r / 100
lati =19.25 + r
string.missionassigned = 'YankeeWest'
else
r = math.random(1, 100)
r = r - 50
r = r / 100
longi =105.5 + r

r = math.random(1, 100)
r = r - 50
r = r / 100
lati =20.4 + r
string.missionassigned = 'YankeeEast'
end
string.strLati = lati; string.strLongi = longi

q = math.random(1, 3)
if q == 1 then
databasedid = 210
else
databasedid = 1272
end
ScenEdit_AddUnit({type = 'facility', name = string.name1, heading = 0, dbid = databasedid, side = 'Communist Forces', Latitude= string.strLati ,Longitude= string.strLongi, autodetectable="false",holdfire="false",proficiency=UnitProf})
ScenEdit_AddUnit({type = 'facility', name = string.name2, heading = 0, dbid = 369, side = 'Communist Forces', Latitude= string.strLati ,Longitude= string.strLongi, autodetectable="false",holdfire="false",proficiency=UnitProf})
ScenEdit_AssignUnitToMission(string.name1, string.missionassigned)
ScenEdit_AssignUnitToMission(string.name2, string.missionassigned)

workKey = workKey + 1
until a >= 8
end

FinalNumKey = workKey + 1
String.StrFinalNumKey = string.format("%.1f", FinalNumKey)

ScenEdit_SetKeyValue('ArmChina', String.StrFinalNumKey)
ScenEdit_SetKeyValue('InfChina', String.StrFinalNumKey)
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
User avatar
tjhkkr
Posts: 2430
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: Using LUA to place units in a random place on the map.

Post by tjhkkr »

Here is a script to add neutral and enemy tankers etc...
This adds ships to a couple of Missions.
====
a = 0
workKey = 1
numNewUnits = 18

repeat
a = a + 1
junkvar = math.random(1,2)
if junkvar == 1 then
UnitProf = math.random(1,4)
string.UnitNationality = 'ISIS'
string.UnitMissionName = 'Block II'
elseif junkvar == 2 then
UnitProf = math.random(1,3)
string.UnitNationality = 'Neutral'
string.UnitMissionName = 'Lookout'
end

junkvar = math.random(1,4)
if junkvar == 1 then
databaseid = 2029
elseif junkvar == 2 then
databaseid = 2028
elseif junkvar == 3 then
databaseid = 339
elseif junkvar == 4 then
databaseid = 144
end

string.name = 'MV.' .. math.random(1,12000) .. '.' .. workKey

r = math.random(1, 152)
r = r - 76
r = r / 100
longi = 49.5 + r

r = math.random(1, 76)
r = r - 38
r = r / 100
lati = 28.75 + r

string.strLati = lati; string.strLongi = longi

ScenEdit_AddUnit({type = 'ship', name = string.name, heading = 0, dbid = databaseid, side = string.UnitNationality, Latitude= string.strLati ,Longitude= string.strLongi, autodetectable="false", holdfire="false",proficiency=UnitProf})
ScenEdit_AssignUnitToMission(string.name, string.UnitMissionName)

workKey = workKey + 1
until a >= numNewUnits
Attachments
lua_commercialadd.txt
(1.3 KiB) Downloaded 5 times
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
Post Reply

Return to “Mods and Scenarios”