Okay, this will be a bit more complicated.
I will try to explain it by using function Taxis() from the vanilla game.
This function checks the approach of german units on Paris that come within a range of 3 hexes.
If a german unit is found, then the event will be triggered.
In your case no event will trigger but rather the unit's movement will be set to 0.
First we need to define the center from where the check will be taken. We'll take Paris for this example
Code: Select all
local paris = game:GetHex(82, 30)
Then we need to define the range of hexes around the center
All hexes within the range of that value will be checked.
The range must be at least 1 or the check will not work (obviously)
All these hexes will be recorded into an array called "hexes".
An array is a table with values in it.
In our case these values are all pairs of X and Y coordinates of the hexes within the defined range of 3.
Code: Select all
local hexes = game.map:GetHexesInRange(paris, 3)
Now all the hexes in that array must be checked using a "for" loop command.
A "for" loop must be closed with "end" just like any other check or function.
Code: Select all
for _, hex in pairs(hexes) do
end
Into this loop you must enter the conditions that must be met.
If the conditions are met, then the loop will be terminated using the "break" command
In the "Taxis" function the loop will be terminated once a hex contains a unit
and the unit belongs to germany, which has the faction.id of 2
then the event triggers and the loop is terminated with a break
Code: Select all
SetEvent("Taxis", game.turn)
break
The whole function reads:
Code: Select all
function Taxis()
local paris = game:GetHex(82, 30)
if GetEvent("Taxis") == 0 then
local hexes = game.map:GetHexesInRange(paris, 3)
for _, hex in pairs(hexes) do
if hex.unit ~= nil
and hex.unit.faction.id == 2 then
SetEvent("Taxis", game.turn)
break
end
end
end
end
In your case you don't want to trigger an event but rather want to immobilize the Koenigsberg unit once Russian units come into range of let's say 4 hexes so it would read:
Code: Select all
local koenigsberg= game:GetHex(112, 17)
local hexes = game.map:GetHexesInRange(koenigsberg, 4)
for _, hex in pairs(hexes) do
if hex.unit ~= nil
and hex.unit.faction.id == 4 then
if koenigsberg.unit ~= nil
and koenigsberg.unit.alliance.id == 2 then
koenigsberg.unit.mp = 0
break
end
end
end
To avoid a crash if there is no unit on koenigsberg there is an additional check included that makes sure the conditions are only met if Koenigsberg has a unit and the unit has alliance.id == 2 because CP has alliance.id = 2. This makes sure no Entente unit will be immobilized once Koenigsberg was captured.
Code: Select all
if koenigsberg.unit ~= nil
and koenigsberg.unit.alliance.id == 2 then
koenigsberg.unit.mp = 0
break
end
All this must be inserted into "game.supply" script, it will not work in normal event functions.