Last active
March 3, 2023 13:39
-
-
Save EddieEldridge/32975175a06bb28e278f0e3c3bc61415 to your computer and use it in GitHub Desktop.
M2TWEOP Lua Script for randomly changing bodyguards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
math.randomseed(os.time()); | |
RANDOM_BODYGUARD_ENABLED = true | |
DEFAULT_BODYGUARDS = { | |
["sicily"] = { | |
defaultBg = "Gondor Bodyguard", | |
newBgs = { | |
"Territorial Guardsmen", | |
"Gondor Militia", | |
"Anfalas Pikemen", | |
"Osgiliath Veterans" | |
} | |
}, | |
["milan"] = { | |
defaultBg = "Rohan Bodyguard", | |
newBgs = { | |
"Peasant Militia", | |
"Helmingas", | |
"Riddermark Cavalary" | |
}, | |
}, | |
["scotland"] = { | |
defaultBg = "Royal Guardsmen", | |
newBgs = { | |
"Dalian Longbowmen", | |
"Dalien Billmen", | |
"Aihwothiuda Horseguards", | |
"Northmen Archers", | |
"Northmen Militia", | |
"Dalian Swordsmen", | |
"Dale Cavalry", | |
"Barding Marksmen", | |
"Barons", | |
"Earls", | |
"Erebor Infantry", | |
-- "Rhovanion Spearmen", | |
-- "Rhovanion Hunters", | |
"Westron Ballista" | |
} | |
}, | |
["turks"] = { | |
defaultBg = "Dunedain Bodyguard", | |
newBgs = {}, | |
}, | |
["byzantium"] = { | |
defaultBg = "High Paladins", | |
newBgs = {}, | |
}, | |
["spain"] = { | |
defaultBg = "Black Snake Guard", | |
newBgs = {}, | |
}, | |
["venice"] = { | |
defaultBg = "Loke Rim Bodyguard", | |
newBgs = {}, | |
}, | |
["england"] = { | |
defaultBg = "Uruk Bodyguard", | |
newBgs = {}, | |
}, | |
["france"] = { | |
defaultBg = "Uruk-hai Bodyguards", | |
newBgs = {}, | |
}, | |
["hre"] = { | |
defaultBg = "Goblin Bodyguards", | |
newBgs = {}, | |
}, | |
["portugal"] = { | |
defaultBg = "Angmar Bodyguards", | |
newBgs = {}, | |
}, | |
["mongols"] = { | |
defaultBg = "Tawar Areiniyr", | |
newBgs = {}, | |
}, | |
["moors"] = { | |
defaultBg = "Durins Guard", | |
newBgs = {}, | |
}, | |
["denmark"]= { | |
defaultBg = "Royal Swan-Guard", | |
newBgs = {}, | |
}, | |
["poland"] = { | |
defaultBg = "Mirkwood Bodyguard", | |
newBgs = {}, | |
}, | |
["russia"] = { | |
defaultBg = "Naru n'Aru Household Guard", | |
newBgs = {}, | |
}, | |
["ireland"] = { | |
defaultBg = "Elbereths Sentinels", | |
newBgs = {}, | |
}, | |
["gundabad"] = { | |
defaultBg = "Mountain Guard", | |
newBgs = {}, | |
}, | |
["saxons"] = { | |
defaultBg = "Calaquendi Lords", | |
newBgs = {}, | |
}, | |
["normans"] = { | |
defaultBg = "Breeland Bodyguard", | |
newBgs = {}, | |
}, | |
["hungary"] = { | |
defaultBg = "Tumunzahar Nobles", | |
newBgs = {}, | |
}, | |
["norway"] = { | |
defaultBg = "Zenith Bodyguard", | |
newBgs = {}, | |
}, | |
["khand"] = { | |
defaultBg = "Variag Nobles", | |
newBgs = {}, | |
}, | |
["teutonic_order"] = { | |
defaultBg = "Elder Guildsmen", | |
newBgs = {}, | |
}, | |
["aztecs"] = { | |
defaultBg = "Chieftains Bodyguards", | |
newBgs = {}, | |
}, | |
["timurids"] = { | |
defaultBg = "Skin-Changers", | |
newBgs = {}, | |
}, | |
} | |
-- Helper Functions | |
function setBodyguard(character, newBodyguardType, expLvl, armourLvl, weaponLvl) | |
print('Setting random new bodyguard for') | |
print(character.name) | |
print(newBodyguardType) | |
local expLvl = expLvl or 0; | |
local armourLvl = armourLvl or 0; | |
local weaponLvl = weaponLvl or 0; | |
local originalBodyguard = character.bodyguards; | |
-- does the stack have space for a new unit? | |
if originalBodyguard.army.numOfUnits < 20 then | |
newBodyguard = originalBodyguard.army:createUnit(newBodyguardType, expLvl, armourLvl, weaponLvl); | |
character:setBodyguardUnit(newBodyguard); | |
originalBodyguard:kill(); | |
else | |
local tempBodyguard = nil; | |
for i=0, originalBodyguard.army.numOfUnits1, 1 do | |
unit = originalBodyguard.army:getUnit(i); | |
if unit.character == nil then | |
tempBodyguard = unit; | |
break | |
end | |
end | |
-- if this is nil, your stack is full of generals (for some reason) | |
if tempBodyguard then | |
character:setBodyguardUnit(tempBodyguard); | |
originalBodyguard:kill(); | |
newBodyguard = tempBodyguard.army:createUnit(newBodyguardType, expLvl, armourLvl, weaponLvl); | |
character:setBodyguardUnit(newBodyguard); | |
end | |
end | |
end | |
function getFactionbyName(name) | |
local factionsNum = stratmap.game.getFactionsCount(); | |
for i = 0, factionsNum - 1 do | |
local faction = stratmap.game.getFaction(i); | |
local factionName = faction:getFactionName(); | |
if factionName == (name) then | |
return faction; | |
end | |
end | |
return nil | |
end | |
function randomFactionBodyguard(namedCharacter) | |
print(namedCharacter.character:getTypeID()) | |
-- Check if the character is a general | |
if namedCharacter.character:getTypeID() ~= 7 then return end | |
local factionName = namedCharacter.faction:getFactionName() | |
local bodyguardType = namedCharacter.character.bodyguards.eduEntry.Type | |
print(DEFAULT_BODYGUARDS[factionName].defaultBg) | |
print(namedCharacter.character.bodyguards.eduEntry) | |
-- If the character has a boring old default bodyguard, let's change it | |
if DEFAULT_BODYGUARDS[factionName].defaultBg == bodyguardType then | |
print('Default BG detected') | |
local numNewBgs = #DEFAULT_BODYGUARDS[factionName].newBgs | |
local newRandomBgType = DEFAULT_BODYGUARDS[factionName].newBgs[math.random(1, numNewBgs)] | |
local armourLvl = math.random(0, 3) | |
local weaponLvl = math.random(0, 3) | |
local xpLvl = math.random(1, 6) | |
setBodyguard(namedCharacter.character, newRandomBgType, xpLvl, armourLvl, weaponLvl) | |
end | |
end | |
function onCharacterSelected(namedCharacter) | |
if RANDOM_BODYGUARD_ENABLED == true then | |
randomFactionBodyguard(namedCharacter) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment