Results 1 to 1 of 1

Thread: [Resource] M2TWEOP Garrison Mod Example, ~30 lines of real code :)

  1. #1

    Default [Resource] M2TWEOP Garrison Mod Example, ~30 lines of real code :)

    I wrote this the other day and it's working well. Unit creation is left to the user. This uses the normal modding style of unit creation, tutorial linked in the example. Requires you add this to an included .lua file in your M2TWEOP.

    Code:
    function onFactionTurnStart(eventData)
        local faction = eventData.faction
        
        if faction.isPlayerControlled == 1 then 
            local campaign=gameDataAll.get().campaignStruct;
            print("---------- START OF TURN "..(campaign.passedTurnsNum + 1).."----------")
        end
    
        -- removes all units marked with 'free_garrison' attribute from a faction.
        -- This keeps the AI from utilizing the garrison for anything but guarding their settlement.
        removeAiGarrison(faction)
    end
    
    
    -- a table that decides how many units are added to a garrison based on how many already exist
    garrisonTable = 
    { 7, 7, 6, 6, 6, 
      5, 5, 5, 4, 4, 
      4, 3, 3, 3, 2, 
      2, 2, 1, 1, 0, 0 }
      
    
      -- the units to add. These should be new units that have the 'free_garrison' attribute, as well as 0 upkeep.
      -- tutorial here: https://medieval2.heavengames.com/m2tw/mod_portal/tutorials/new_units/
    garrisonUnits = 
    {
        england     = {"Northern Garrison", "N Archer Garrison"},
        scotland    = {"Northern Garrison", "N Archer Garrison"},
        france      = {"Northern Garrison", "N Archer Garrison"},
        spain       = {"Northern Garrison", "N Archer Garrison"},
        portugal    = {"Northern Garrison", "N Archer Garrison"},
        hre         = {"Northern Garrison", "N Archer Garrison"},
        denmark     = {"Northern Garrison", "N Archer Garrison"},           
        venice      = {"Italian Garrison", "N Archer Garrison"},
        milan       = {"Italian Garrison", "N Archer Garrison"},
        papal_states = {"Italian Garrison", "N Archer Garrison"},
        sicily      = {"Italian Garrison", "S Archer Garrison"},            
        byzantium   = {"Byz Garrison", "S Archer Garrison"},           
        hungary     = {"EE Garrison", "S Archer Garrison"},
        poland      = {"EE Garrison", "EE Archer Garrison"},
        russia      = {"EE Garrison", "EE Archer Garrison"},            
        turks       = {"ME Garrison", "ME Archer Garrison"},
        egypt       = {"ME Garrison", "ME Archer Garrison"},
        moors       = {"ME Garrison", "ME Archer Garrison"},
        mongols     = {"ME Garrison", "ME Archer Garrison"},
        timurids    = {"ME Garrison", "ME Archer Garrison"}
    }
    
    
    -- add the garrison at the end of the turn, so the garrison always exists for all other factions, but not during the controlling faction's turn.
    -- Again, this keeps it from being used for things other than being a garrison.
    function onSettlementTurnEnd(eventData)
        local settlement = eventData.settlement
        addAiGarrison(settlement)
    end
    
    
    -- adds the new garrison as a mix of archers and troops
    function addAiGarrison(settlement)
        -- it's for the ai!
        if settlement.ownerFaction.isPlayerControlled == 1 then return end;
    
        -- we don't support rebels n mur'cans!
        local facName = settlement.ownerFaction:getFactionName()
        if facName == "slave" then return end;
        if facName == "aztecs" then return end;
    
        local numUnits = 0
        if settlement.army then numUnits = settlement.army.numOfUnits end
        
        local numToProduce = garrisonTable[numUnits]
        if not numToProduce then return end
        local numInfantry = math.ceil(numToProduce / 2)
        local numArchers = math.floor(numToProduce / 2)
        
        local infName = garrisonUnits[facName][1]
        --print('Creating units of '..infName)
        local command_a = "create_unit"
        local command_b = settlement.name..", "..infName..", num "..numInfantry..", exp 0, arm 0, wep 0"
        stratmap.game.scriptCommand(command_a, command_b)
        
        local archerName = garrisonUnits[facName][2]
        --print('Creating units of '..archerName)
        command_a = "create_unit"
        command_b = settlement.name..", "..archerName..", num "..numArchers..", exp 0, arm 0, wep 0"
        stratmap.game.scriptCommand(command_a, command_b)
    
        --print("Added "..numToProduce.." units to "..settlement.name)
    end
    
    
    function removeAiGarrison(faction)
        --destroy_units [faction] [custom attribute]
        local command_a = "destroy_units"
        local command_b = faction:getFactionName().." free_garrison"
        stratmap.game.scriptCommand(command_a, command_b)
    end
    For more interesting garrisons, one could randomly pick from the array (though note that if you do without a suitable psuedo-random seed that is repeated, you'll end up with a different garrison every turn.) And the array could have as many entries as you like, though renaming the ui elements for each info card and such is painful.
    Last edited by tescrin; April 18, 2024 at 07:31 AM. Reason: added missing bugfix

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •