Tiramisu
Warlord
Workshoplink: https://steamcommunity.com/sharedfiles/filedetails/?id=1400319331
I did not create this mod, but I want to help improving it. So I wrote a Lua script for this request:
Now this is my Lua script that makes this work:
I have tested this script a lot and it seems to have no bugs.
How this script works:
Every time the Ottoman empire disbands a Janissar (no matter if by manual deleting or by having a negative money income) the Barbarians get a Janissar unit with negative map coordinates, i.e. offmap where they will not interact with the actual game.
Killed Janissars will create offmap Janissars for Free Cities.
If at least one offmap Barbarian Janissars exists, there will be a chance that all the offmap Janissars will spawn near the Ottoman's capital city as Barbarian rebels. If Barbarian Janissars already exist on the actual map, then all remaining offmap Janissars will spawn on the next turn for sure.
This is how it looks like:
I did not create this mod, but I want to help improving it. So I wrote a Lua script for this request:
Captain Lime said:It was my deepest regret that I could not make it so that the Janissaries would all turn barbarian if you tried to disband them
Now this is my Lua script that makes this work:
Spoiler :
Code:
--Edited by Tiramisu on 5. Juny 2018
local strOttomanCivName = "LOC_CIVILIZATION_LIME_OTTOMANS_NAME"
local strJanissaryUnitName = "UNIT_OTTOMAN_JANISSARY"
local iBarbJanissarSpawnProbability = 20
local bAlsoSpawnKilledJanissars = true
local iBarbJanissaryMaxSpawnRadius = 100
local iBarbarianPlayerID = 63
local iFreeCityPlayerID = 62
local PlayerUnitsList = {}
local kDeletedUnitDatas :table = {
iTurn = 0,
iUnit = 0,
sUnit = "",
pUnit = nil,
iRelatedUnit = 0,
iRelatedPlayer = 0
};
--[[How this script works:
Every time the Ottoman empire disbands a Janissar (no matter if by manual deleting or by having a negative money income) the Barbarians get a Janissar unit with negative map coordinates, i.e. offmap where they will not interact with the actual game.
Killed Janissars will create offmap Janissars for Free Cities.
If at least one offmap Barbarian Janissars exists, there will be a chance that all the offmap Janissars will spawn near the Ottoman's capital city as Barbarian rebels. If Barbarian Janissars already exist on the actual map, then all remaining offmap Janissars will spawn on the next turn for sure.
]]--
--SUPPORT FUNCTIONS:
function GetUnitDatas( iPlayerID )
local pPlayerUnits = Players[iPlayerID]:GetUnits()
for ii, pLoopUnit in pPlayerUnits:Members() do
local kUnitDatas :table = {
iTurn = Game.GetCurrentGameTurn(),
iPlayer = iPlayerID,
iUnit = pLoopUnit:GetID(),
sUnit = GameInfo.Units[pLoopUnit:GetType()].UnitType,
pUnit = pLoopUnit --is always a nil pointer if unit dies!
};
table.insert(PlayerUnitsList, kUnitDatas)
end
end
function PlayerIsLimesOttomanCiv( iPlayerID )
local pPlayerConfig = PlayerConfigurations[iPlayerID];
local strPlayer = pPlayerConfig:GetCivilizationShortDescription()
if( strPlayer == strOttomanCivName ) then
return true;
else
return false;
end
end
function OffMapBarbarianJanissarsExist()
local pUnits = Players[iBarbarianPlayerID]:GetUnits();
for _, pUnit in pUnits:Members() do
strUnit = GameInfo.Units[pUnit:GetType()].UnitType
if( strUnit == strJanissaryUnitName and pUnit:GetX()<0 and pUnit:GetY()<0 ) then
return true;
end
end
return false;
end
function OnMapBarbarianJanissarsExist()
local pUnits = Players[iBarbarianPlayerID]:GetUnits();
for _, pUnit in pUnits:Members() do
strUnit = GameInfo.Units[pUnit:GetType()].UnitType
if( strUnit == strJanissaryUnitName and pUnit:GetX()>=0 and pUnit:GetY()>=0 ) then
print("Revolting Janissars are calling remaining rebels to arms!")
return true;
end
end
return false;
end
function DeleteAllJanissars( iPlayerID )
local pPlayerUnits = Players[iPlayerID]:GetUnits();
for _, pUnit in pPlayerUnits:Members() do
strUnit = GameInfo.Units[pUnit:GetType()].UnitType
if( strUnit == strJanissaryUnitName ) then
UnitManager.Kill(pUnit, false);
end
end
end
function SpawnOffmapUnitsOnMap( iPlayerID, pCity)
local pPlayerUnits = Players[iPlayerID]:GetUnits();
for _, pUnit in pPlayerUnits:Members() do
strUnit = GameInfo.Units[pUnit:GetType()].UnitType
if( strUnit == strJanissaryUnitName and pUnit:GetX()<0 and pUnit:GetY()<0 ) then
UnitManager.InitUnitValidAdjacentHex(iBarbarianPlayerID, strJanissaryUnitName, pCity:GetX(), pCity:GetY(), iBarbJanissaryMaxSpawnRadius)
UnitManager.Kill(pUnit, false);
end
end
end
function SpawnBarbarianJanissars( iPlayerID )
DeleteAllJanissars( iPlayerID )
local pCity = Players[iPlayerID]:GetCities():GetCapitalCity()
SpawnOffmapUnitsOnMap( iBarbarianPlayerID, pCity)
if( bAlsoSpawnKilledJanissars ) then
SpawnOffmapUnitsOnMap( iFreeCityPlayerID, pCity)
end
end
-- MAIN FUNCTIONS:
function OnUnitRemovedFromMap( iPlayerID: number, iUnitID : number )
local strUnit
for _, kUnitDatas in ipairs(PlayerUnitsList) do
if( kUnitDatas.iPlayer == iPlayerID and kUnitDatas.iUnit == iUnitID ) then
strUnit = kUnitDatas.sUnit
end
end
if( PlayerIsLimesOttomanCiv(iPlayerID) and strUnit == strJanissaryUnitName ) then
local pOffmapUnit = UnitManager.InitUnit(iBarbarianPlayerID, strJanissaryUnitName, -1, -1)
kDeletedUnitDatas.iTurn = Game.GetCurrentGameTurn()
kDeletedUnitDatas.iUnit = pOffmapUnit:GetID()
kDeletedUnitDatas.sUnit = GameInfo.Units[pOffmapUnit:GetType()].UnitType
kDeletedUnitDatas.pUnit = pOffmapUnit
kDeletedUnitDatas.iRelatedUnit = iUnitID
kDeletedUnitDatas.iRelatedPlayer = iPlayerID
end
end
Events.UnitRemovedFromMap.Add( OnUnitRemovedFromMap );
function OnUnitKilledInCombat( iVictimID, iKilledUnitID, iKillerID, iUnitAttackerID)
--thanks to FearSunns Unhappines Script for the function arguments
if( kDeletedUnitDatas.iRelatedPlayer == iVictimID
and kDeletedUnitDatas.iRelatedUnit == iKilledUnitID ) then --if removed unit actually died in combat
UnitManager.Kill(kDeletedUnitDatas.pUnit, false);
UnitManager.InitUnit(iFreeCityPlayerID, strJanissaryUnitName, -1, -1)
end
end
Events.UnitKilledInCombat.Add( OnUnitKilledInCombat );
function OnPlayerTurnActivated( iPlayerID )
if( PlayerIsLimesOttomanCiv(iPlayerID) ) then
GetUnitDatas( iPlayerID )
local iDiceRoll = math.random(100)
if( ( iDiceRoll <= iBarbJanissarSpawnProbability or OnMapBarbarianJanissarsExist() )
and OffMapBarbarianJanissarsExist() ) then
SpawnBarbarianJanissars( iPlayerID )
end
end
end
GameEvents.PlayerTurnStartComplete.Add(OnPlayerTurnActivated);
function Initialize()
for iPlayerID = 0, GameDefines.MAX_PLAYERS-1, 1 do
if( PlayerIsLimesOttomanCiv(iPlayerID) ) then
GetUnitDatas( iPlayerID )
end
end
end
Initialize()
I have tested this script a lot and it seems to have no bugs.
How this script works:
Every time the Ottoman empire disbands a Janissar (no matter if by manual deleting or by having a negative money income) the Barbarians get a Janissar unit with negative map coordinates, i.e. offmap where they will not interact with the actual game.
Killed Janissars will create offmap Janissars for Free Cities.
If at least one offmap Barbarian Janissars exists, there will be a chance that all the offmap Janissars will spawn near the Ottoman's capital city as Barbarian rebels. If Barbarian Janissars already exist on the actual map, then all remaining offmap Janissars will spawn on the next turn for sure.
This is how it looks like:
Last edited: