How to add random AI controlled heroes to your map (FAQ)

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Field Commander
User avatar
Posts: 943
Joined: Sat Dec 13, 2008 8:17 am

How to add random AI controlled heroes to your map (FAQ)

Postby YaNkFaN » Tue Jun 23, 2009 8:37 pm

Even though Mav semi covered this in his DT tutorial this is just to add random AI controlled heroes to any SP map.

This requires a basic knowledge of scripting and how to add in units but otherwise not much else.
Note when I refer to .lua I am referring too abcc_con.lua or abcg_con.lua found in data_abc/common/scripts/abc abc=mod3 letter name

First download archer01's hero support script and read all the readmes and add in the hero support lines to your .lua

so now your script (relavent parts should look like this)
Hidden/Spoiler:
Code: Select all
--
-- Copyright (c) 2005 Pandemic Studios, LLC. All rights reserved.
--

-- load the gametype script
ScriptCB_DoFile("ObjectiveConquest")
ScriptCB_DoFile("setup_teams")
 ScriptCB_DoFile("AIHeroSupport")
   
   --  REP Attacking (attacker is always #1)
    REP = 1;
    CIS = 2;
    --  These variables do not change
    ATT = REP;
    DEF = CIS;


function ScriptPostLoad()      
   
   
    --This defines the CPs.  These need to happen first
    cp1 = CommandPost:New{name = "cp1"}
    cp2 = CommandPost:New{name = "cp2"}
    cp3 = CommandPost:New{name = "cp3"}
    cp4 = CommandPost:New{name = "cp4"}
   
   
   
    --This sets up the actual objective.  This needs to happen after cp's are defined
    conquest = ObjectiveConquest:New{teamATT = ATT, teamDEF = DEF,
                                     textATT = "game.modes.con",
                                     textDEF = "game.modes.con2",
                                     multiplayerRules = true}
   
    --This adds the CPs to the objective.  This needs to happen after the objective is set up
    conquest:AddCommandPost(cp1)
    conquest:AddCommandPost(cp2)
    conquest:AddCommandPost(cp3)
    conquest:AddCommandPost(cp4)   
   
    conquest:Start()
 herosupport = AIHeroSupport:New{AIATTHeroHealth = 5000,   AIDEFHeroHealth = 3000, gameMode = "NonConquest",}
 herosupport:SetHeroClass(REP, "rep_hero_nameofhero")
 herosupport:SetHeroClass(CIS, "cis_hero_nameofhero")
herosupport:AddSpawnCP("cp1","cp1_spawn")
herosupport:AddSpawnCP("cp2","cp2_spawn")
herosupport:AddSpawnCP("cp3","cp3_spawn")
herosupport:AddSpawnCP("cp4","cp4_spawn")
herosupport:Start()
   
 end



Remember
9- Now this is a VERY important part, the "SetHeroClass" function can only be used once for each team. If it is used a second time, nasty stuff will likely happen. Go through the script and remove all uses of the "SetHeroClass" function, except for the two you added earlier of course, so there won't be any problems. Your final script should have at most only two (2) mentions of "SetHeroClass". Don't forget about your text editor's search function when making sure this is so.

Now as of this point there should be AI controlled heroes running around your map but they should only be the ones that you called from the cis and rep side and put in the hero support script here

herosupport:SetHeroClass(REP, "rep_hero_nameofhero")
herosupport:SetHeroClass(CIS, "cis_hero_nameofhero")

Now for the randomness...
If you take math or know math at all you are familiar with probability and the nCr (read n choose r) function defined as n!/r!(n-r)! anyway done with the math lesson we will be using a .lua function which does just this

it is called math.random now you need to decide how many random heroes you would like in your map for the sake of this tutorial we will have 3

this script will tell the game how many heros we want paste this into the .lua right under
function ScriptPostLoad()

Hidden/Spoiler:
Code: Select all
heroRep = 0
      heroCis = 0
                 
     
     
      herocisand = math.random(1, 3)
   

      if herocisand == 1 then
         heroCis = "cis_hero_heroname1"
         
      elseif herocisand == 2 then
         heroCis = "cis_hero_heroname2"
         
      elseif herocisand == 3 then
         heroCis = "cis_hero_heroname3"
             
      end   
     
     
      herorepand = math.random(1, 3)
     

      if herorepand == 1 then   
         heroRep = "rep_hero_heroname1"
                 
      elseif herorepand == 2 then
         heroRep = "rep_hero_heroname2"
         
      elseif herorepand == 3 then
         heroRep = "rep_hero_heroname3"   
   
      end


what this does is assign each hero to a number 1, 2, or 3 then it tells the game to pick one of those numbers for each team and that is the hero

this can be adapted for ALL and IMP just add this in instead

Hidden/Spoiler:
Code: Select all
heroImp = 0
      heroAll = 0
                 
     
     
      heroalland = math.random(1, 3)
   

      if heroalland == 1 then
         heroAll = "all_hero_heroname3"
         
      elseif heroalland == 2 then
         heroAll = "all_hero_heroname2"
         
      elseif heroalland == 3 then
         heroAll = "all_hero_heroname3"
             
      end   
     
     
      heroimpand = math.random(1, 3)
     

      if heroimpand == 1 then   
         heroImp = "imp_hero_heroname1"
                 
      elseif heroimpand == 2 then
         heroImp = "imp_hero_heroname2"
         
      elseif heroimpand == 3 then
         heroImp = "imp_hero_heroname3"   
   
      end


again this can be adapted for more than 3 heros just add more numbers
ie heroimpand = math.random(1, #) #=integer >1 and then add

elseif heroimpand == # then
heroImp = "imp_hero_heroname3" for the number of numbers you have (make it so it looks like the rest of the hero calls above it)

now this script isn't done yet you must call your heros with the hero support script

change these lines
herosupport:SetHeroClass(REP, "rep_hero_nameofhero")
herosupport:SetHeroClass(CIS, "cis_hero_nameofhero")
to
herosupport:SetHeroClass(CIS, heroCis)
herosupport:SetHeroClass(REP, heroRep)

this will tell the .lua to apply hero support on the hero that pops out of our random function

now we must make sure that the map will work in MP random heroes will crash multiplayer so we must add if not ScriptCB_InMultiplayer() then above where the hero support starts

all and all it should look like this

Hidden/Spoiler:
Code: Select all
if not ScriptCB_InMultiplayer() then   
 herosupport = AIHeroSupport:New{AIATTHeroHealth = 5000,   AIDEFHeroHealth = 3000, gameMode = "Conquest",}
herosupport:SetHeroClass(CIS, heroCis)
    herosupport:SetHeroClass(REP, heroRep)
    herosupport:AddSpawnCP("cp1","cp1_spawn")   
    herosupport:AddSpawnCP("cp2","cp2_spawn")
    herosupport:AddSpawnCP("cp3","cp3_spawn")
    herosupport:AddSpawnCP("cp4","cp4_spawn")

    herosupport:Start()
else
end
end


pay attention to the ends at the end there should be 2 of them

now since there are only heroes during SP how about adding them in MP (they will not be random)
copy these lines

if ScriptCB_InMultiplayer() then
heroCis = "cis_hero_heroname"
heroRep = "rep_hero_heroname"
end
right under
ReadDataFile("ingame.lvl")

you can pick any hero you want to be here it will just not be random

now lastly under where the setupteams section is add in the hero lines that you prieviously deleted for acher's script

all and all it should look like this under where the cis units are called
Hidden/Spoiler:
Code: Select all
cis = {
         team = CIS,
         units = 20,
         reinforcements = 150,
         soldier  = { "cis_inf_rifleman",9, 25},
         assault  = { "cis_inf_rocketeer",1, 4},
         engineer = { "cis_inf_engineer",1, 4},
         sniper   = { "cis_inf_sniper",1, 4},
         officer = {"cis_inf_officer",1, 4},
         special = { "cis_inf_droideka",1, 4},
      }
   }
     
     if ScriptCB_InMultiplayer() then
SetHeroClass(CIS, heroCis)   
SetHeroClass(REP, heroRep)
else
end


this will allow heroes to appear in multiplayer and not crash multiplayer


the final script will look like this
Hidden/Spoiler:
Code: Select all
--
-- Copyright (c) 2005 Pandemic Studios, LLC. All rights reserved.
--

-- load the gametype script
ScriptCB_DoFile("ObjectiveConquest")
ScriptCB_DoFile("setup_teams")
ScriptCB_DoFile("AIHeroSupport")
   
   --  REP Attacking (attacker is always #1)
    REP = 1;
    CIS = 2;
    --  These variables do not change
    ATT = REP;
    DEF = CIS;


function ScriptPostLoad()      
    heroRep = 0
      heroCis = 0
                 
     
     
      herocisand = math.random(1, 3)
   

      if herocisand == 1 then
         heroCis = "cis_hero_heroname1"
         
      elseif herocisand == 2 then
         heroCis = "cis_hero_heroname2"
         
      elseif herocisand == 3 then
         heroCis = "cis_hero_heroname3"
             
      end   
     
     
      herorepand = math.random(1, 3)
     

      if herorepand == 1 then   
         heroRep = "rep_hero_heroname1"
                 
      elseif herorepand == 2 then
         heroRep = "rep_hero_heroname2"
         
      elseif herorepand == 3 then
         heroRep = "rep_hero_heroname3"   
   
      end
   
    --This defines the CPs.  These need to happen first
    cp1 = CommandPost:New{name = "cp1"}
    cp2 = CommandPost:New{name = "cp2"}
    cp3 = CommandPost:New{name = "cp3"}
    cp4 = CommandPost:New{name = "cp4"}
   
   
   
    --This sets up the actual objective.  This needs to happen after cp's are defined
    conquest = ObjectiveConquest:New{teamATT = ATT, teamDEF = DEF,
                                     textATT = "game.modes.con",
                                     textDEF = "game.modes.con2",
                                     multiplayerRules = true}
   
    --This adds the CPs to the objective.  This needs to happen after the objective is set up
    conquest:AddCommandPost(cp1)
    conquest:AddCommandPost(cp2)
    conquest:AddCommandPost(cp3)
    conquest:AddCommandPost(cp4)   
   
    conquest:Start()
if not ScriptCB_InMultiplayer() then   
 herosupport = AIHeroSupport:New{AIATTHeroHealth = 5000,   AIDEFHeroHealth = 3000, gameMode = "Conquest",}
herosupport:SetHeroClass(CIS, heroCis)
    herosupport:SetHeroClass(REP, heroRep)
    herosupport:AddSpawnCP("cp1","cp1_spawn")   
    herosupport:AddSpawnCP("cp2","cp2_spawn")
    herosupport:AddSpawnCP("cp3","cp3_spawn")
    herosupport:AddSpawnCP("cp4","cp4_spawn")

    herosupport:Start()
else
end
end
   
   
 end


---------------------------------------------------------------------------
-- FUNCTION:    ScriptInit
-- PURPOSE:     This function is only run once
-- INPUT:
-- OUTPUT:
-- NOTES:       The name, 'ScriptInit' is a chosen convention, and each
--              mission script must contain a version of this function, as
--              it is called from C to start the mission.
---------------------------------------------------------------------------
function ScriptInit()
   
    ReadDataFile("ingame.lvl")
   
   if ScriptCB_InMultiplayer() then
         heroCis = "cis_hero_heroname1"
         heroRep = "rep_hero_heroname1"
end 
    SetMaxFlyHeight(30)
    SetMaxPlayerFlyHeight (30)


    SetMemoryPoolSize("SoldierAnimation", 600)

    SetMemoryPoolSize ("Combo",50)              -- should be ~ 2x number of jedi classes
    SetMemoryPoolSize ("Combo::State",650)      -- should be ~12x #Combo
    SetMemoryPoolSize ("Combo::Transition",650) -- should be a bit bigger than #Combo::State
    SetMemoryPoolSize ("Combo::Condition",650)  -- should be a bit bigger than #Combo::State
    SetMemoryPoolSize ("Combo::Attack",550)     -- should be ~8-12x #Combo
    SetMemoryPoolSize ("Combo::DamageSample",6000)  -- should be ~8-12x #Combo::Attack
    SetMemoryPoolSize ("Combo::Deflect",100)     -- should be ~1x #combo 
   
    ReadDataFile("sound\\yav.lvl;yav1cw")
    ReadDataFile("SIDE\\rep.lvl",
                             "rep_inf_ep3_rifleman",
                             "rep_inf_ep3_rocketeer",
                             "rep_inf_ep3_engineer",
                             "rep_inf_ep3_sniper",
                             "rep_inf_ep3_officer",
                             "rep_inf_ep3_jettrooper",
                             "rep_hover_fightertank",
                             "rep_hero_anakin",
                             "rep_hero_heroname1", --name of first hero
                             "rep_hero_heroname2", --name of second hero
                             "rep_hero_heroname3", --name of third hero
                             "rep_hover_barcspeeder")
    ReadDataFile("SIDE\\cis.lvl",
                             "cis_inf_rifleman",
                             "cis_inf_rocketeer",
                             "cis_inf_engineer",
                             "cis_inf_sniper",
                             "cis_hero_heroname1", --name of first hero
                             "cis_hero_heroname2", --name of second hero
                             "cis_hero_heroname3", --name of third hero
                             "cis_inf_officer",
                             "cis_inf_droideka",
                             "cis_hero_darthmaul",
                             "cis_hover_aat")
                             
                             
    ReadDataFile("SIDE\\tur.lvl",
             "tur_bldg_laser",
             "tur_bldg_tower")         
                             
   SetupTeams{
      rep = {
         team = REP,
         units = 20,
         reinforcements = 150,
         soldier  = { "rep_inf_ep3_rifleman",9, 25},
         assault  = { "rep_inf_ep3_rocketeer",1, 4},
         engineer = { "rep_inf_ep3_engineer",1, 4},
         sniper   = { "rep_inf_ep3_sniper",1, 4},
         officer = {"rep_inf_ep3_officer",1, 4},
         special = { "rep_inf_ep3_jettrooper",1, 4},
          
      },
      cis = {
         team = CIS,
         units = 20,
         reinforcements = 150,
         soldier  = { "cis_inf_rifleman",9, 25},
         assault  = { "cis_inf_rocketeer",1, 4},
         engineer = { "cis_inf_engineer",1, 4},
         sniper   = { "cis_inf_sniper",1, 4},
         officer = {"cis_inf_officer",1, 4},
         special = { "cis_inf_droideka",1, 4},
      }
   }
     
    if ScriptCB_InMultiplayer() then
SetHeroClass(CIS, heroCis)   
SetHeroClass(REP, heroRep)
else
end
   

    --  Level Stats
    --  ClearWalkers()
    AddWalkerType(0, 4) -- special -> droidekas
    AddWalkerType(1, 0) -- 1x2 (1 pair of legs)
    AddWalkerType(2, 0) -- 2x2 (2 pairs of legs)
    AddWalkerType(3, 0) -- 3x2 (3 pairs of legs)
    local weaponCnt = 1024
    SetMemoryPoolSize("Aimer", 75)
    SetMemoryPoolSize("AmmoCounter", weaponCnt)
    SetMemoryPoolSize("BaseHint", 1024)
    SetMemoryPoolSize("EnergyBar", weaponCnt)
   SetMemoryPoolSize("EntityCloth", 32)
   SetMemoryPoolSize("EntityFlyer", 32)
    SetMemoryPoolSize("EntityHover", 32)
    SetMemoryPoolSize("EntityLight", 200)
 SetMemoryPoolSize ("ClothData",20)
    SetMemoryPoolSize("EntitySoundStream", 4)
    SetMemoryPoolSize("EntitySoundStatic", 32)
    SetMemoryPoolSize("MountedTurret", 32)
   SetMemoryPoolSize("Navigator", 128)
    SetMemoryPoolSize("Obstacle", 1024)
   SetMemoryPoolSize("PathNode", 1024)
    SetMemoryPoolSize("SoundSpaceRegion", 64)
    SetMemoryPoolSize("TreeGridStack", 1024)
   SetMemoryPoolSize("UnitAgent", 128)
   SetMemoryPoolSize("UnitController", 128)
   SetMemoryPoolSize("Weapon", weaponCnt)
   
    SetSpawnDelay(10.0, 0.25)
    --ReadDataFile("dc:@#$\\@#$.lvl", "@#$_conquest")
    ReadDataFile("dc:@#$\\@#$.lvl", "@#$_conquest")
    SetDenseEnvironment("false")




    --  Sound
   
    SetSoundEffect("ScopeDisplayZoomIn",  "binocularzoomin")
    SetSoundEffect("ScopeDisplayZoomOut", "binocularzoomout")

    voiceSlow = OpenAudioStream("sound\\global.lvl", "rep_unit_vo_slow")
    AudioStreamAppendSegments("sound\\global.lvl", "cis_unit_vo_slow", voiceSlow)
    AudioStreamAppendSegments("sound\\global.lvl", "global_vo_slow", voiceSlow)
   
    voiceQuick = OpenAudioStream("sound\\global.lvl", "rep_unit_vo_quick")
    AudioStreamAppendSegments("sound\\global.lvl", "cis_unit_vo_quick", voiceQuick)
   
    OpenAudioStream("sound\\global.lvl",  "cw_music")
    -- OpenAudioStream("sound\\global.lvl",  "global_vo_quick")
    -- OpenAudioStream("sound\\global.lvl",  "global_vo_slow")
    OpenAudioStream("sound\\yav.lvl",  "yav1")
    OpenAudioStream("sound\\yav.lvl",  "yav1")
    OpenAudioStream("sound\\yav.lvl",  "yav1_emt")

    SetBleedingVoiceOver(REP, REP, "rep_off_com_report_us_overwhelmed", 1)
    SetBleedingVoiceOver(REP, CIS, "rep_off_com_report_enemy_losing",   1)
    SetBleedingVoiceOver(CIS, REP, "cis_off_com_report_enemy_losing",   1)
    SetBleedingVoiceOver(CIS, CIS, "cis_off_com_report_us_overwhelmed", 1)

    SetOutOfBoundsVoiceOver(2, "cisleaving")
    SetOutOfBoundsVoiceOver(1, "repleaving")

    SetAmbientMusic(REP, 1.0, "rep_yav_amb_start",  0,1)
    SetAmbientMusic(REP, 0.8, "rep_yav_amb_middle", 1,1)
    SetAmbientMusic(REP, 0.2, "rep_yav_amb_end",    2,1)
    SetAmbientMusic(CIS, 1.0, "cis_yav_amb_start",  0,1)
    SetAmbientMusic(CIS, 0.8, "cis_yav_amb_middle", 1,1)
    SetAmbientMusic(CIS, 0.2, "cis_yav_amb_end",    2,1)

    SetVictoryMusic(REP, "rep_yav_amb_victory")
    SetDefeatMusic (REP, "rep_yav_amb_defeat")
    SetVictoryMusic(CIS, "cis_yav_amb_victory")
    SetDefeatMusic (CIS, "cis_yav_amb_defeat")

    SetSoundEffect("ScopeDisplayZoomIn",      "binocularzoomin")
    SetSoundEffect("ScopeDisplayZoomOut",     "binocularzoomout")
    --SetSoundEffect("BirdScatter",             "birdsFlySeq1")
    --SetSoundEffect("WeaponUnableSelect",      "com_weap_inf_weaponchange_null")
    --SetSoundEffect("WeaponModeUnableSelect",  "com_weap_inf_modechange_null")
    SetSoundEffect("SpawnDisplayUnitChange",       "shell_select_unit")
    SetSoundEffect("SpawnDisplayUnitAccept",       "shell_menu_enter")
    SetSoundEffect("SpawnDisplaySpawnPointChange", "shell_select_change")
    SetSoundEffect("SpawnDisplaySpawnPointAccept", "shell_menu_enter")
    SetSoundEffect("SpawnDisplayBack",             "shell_menu_exit")


--OpeningSateliteShot
    AddCameraShot(0.908386, -0.209095, -0.352873, -0.081226, -45.922508, -19.114113, 77.022636);

    AddCameraShot(-0.481173, 0.024248, -0.875181, -0.044103, 14.767292, -30.602322, -144.506851);
    AddCameraShot(0.999914, -0.012495, -0.004416, -0.000055, 1.143253, -33.602314, -76.884430);
    AddCameraShot(0.839161, 0.012048, -0.543698, 0.007806, 19.152437, -49.802273, 24.337317);
    AddCameraShot(0.467324, 0.006709, -0.883972, 0.012691, 11.825212, -49.802273, -7.000720);
    AddCameraShot(0.861797, 0.001786, -0.507253, 0.001051, -11.986043, -59.702248, 23.263165);
    AddCameraShot(0.628546, -0.042609, -0.774831, -0.052525, 20.429928, -48.302277, 9.771714);
    AddCameraShot(0.765213, -0.051873, 0.640215, 0.043400, 57.692474, -48.302277, 16.540724);
    AddCameraShot(0.264032, -0.015285, -0.962782, -0.055734, -16.681797, -42.902290, 129.553268);
    AddCameraShot(-0.382320, 0.022132, -0.922222, -0.053386, 20.670977, -42.902290, 135.513001);
end



That is pretty much it just make sure that you have all 3 heroes called or however many you want.

Just a note this will cause the hero to appear below the last unit on the select screen so if you have 7 units it will appear somewhat off the screen this cannot be fixed.

If you have any questions email me at yankfan1950 at gmail dot com or just PM me here at GT if you use this credit Maveritchell for the TFU herosupport script which is what this is based of of Archer01 for his script and I would like credit as well for putting it all together

2008 Most Technically Challenging Avatar
User avatar
Posts: 2376
Joined: Tue Feb 12, 2008 7:46 pm
Location: NJ, USA
Projects :: No Mod project currently.
Games I'm Playing :: League of Legends
xbox live or psn: No gamertag set

Re: How to add random AI controlled heroes to your map

Postby obiboba3po » Tue Jun 23, 2009 10:30 pm

nice job yank, im sure many people will use this :D

Recruit Womprat Killer
Posts: 7
Joined: Thu Oct 08, 2009 7:15 am

Re: How to add random AI controlled heroes to your map (FAQ)

Postby Was_ser » Fri Aug 06, 2010 8:14 am

Where can I find Archer's script?

Master Bounty Hunter
User avatar
Posts: 1464
Joined: Fri Dec 11, 2009 6:24 pm
Location: minisnowta
Projects :: BFSM
Games I'm Playing :: SWBFII
xbox live or psn: skelltor95

Re: How to add random AI controlled heroes to your map (FAQ)

Postby skelltor » Fri Aug 06, 2010 8:29 am

coulde this be modded to use units instead of heros?(the random part)

Master of the Force
User avatar
Posts: 6069
Joined: Wed Sep 27, 2006 11:05 am
Location: Duckburg, Germany

Re: How to add random AI controlled heroes to your map (FAQ)

Postby DarthD.U.C.K. » Fri Aug 06, 2010 11:11 am

Was_ser wrote:WCIF Archer's script?


what do you mean? in case you are asking where you can donwload it: i would check either the assetslistthread or the everythingyouneedthread

@skelltor:yes, it would just like people already said in this thread

Recruit Womprat Killer
Posts: 7
Joined: Thu Oct 08, 2009 7:15 am

Re: How to add random AI controlled heroes to your map (FAQ)

Postby Was_ser » Fri Aug 06, 2010 12:46 pm

I found script topic here
and download is here

Return to SWBF2 Modding

Who is online

Users browsing this forum: No registered users and 4 guests