• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

AI for RPGs?

Status
Not open for further replies.
Level 7
Joined
Dec 3, 2006
Messages
339
JASS:
function AILearnSkill takes unit h, string a returns nothing
    local integer i = GetTableInt(a, "LearnSkillOrder")+1
    if i == 1 or i == 4 or i == 8 then
        call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "BaseSkill1"))
    elseif i == 2 or i == 5 or i == 9 then
        call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "BaseSkill2"))
    elseif i == 3 or i == 7 or i == 10 then
        call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "BaseSkill3"))
    elseif i == 6 then
        call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "UltimateSkill"))
    endif
    call SetTableInt(a, "LearnSkillOrder", i)
endfunction

function AISetItem takes nothing returns nothing
    set bj_lastRemovedItem=GetEnumItem()
endfunction

function AIItemFilter takes nothing returns boolean
    return IsItemVisible(GetFilterItem()) and GetWidgetLife(GetFilterItem()) > 0
endfunction

function AIHasEmptyInventorySlot takes unit u returns boolean
    return UnitItemInSlot(u, 0) == null or UnitItemInSlot(u, 1) == null or UnitItemInSlot(u, 2) == null or UnitItemInSlot(u, 3) == null or UnitItemInSlot(u, 4) == null or UnitItemInSlot(u, 5) == null
endfunction

function AIFilterEnemyConditions takes nothing returns boolean
    return GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0 and IsPlayerEnemy(GetOwningPlayer(GetFilterUnit()), GetOwningPlayer(GetAttachedUnit(GetExpiredTimer(), "hero")))
endfunction

function AILoop takes nothing returns nothing
    local string a = GetAttachmentTable(GetExpiredTimer())
    local unit h = GetTableUnit(a, "hero")
    local rect i
    local location r
    local real x = GetUnitX(h)
    local real y = GetUnitY(h)
    local group g
    local boolexpr b
    local boolexpr be
    local unit f
    local string o = OrderId2String(GetUnitCurrentOrder(h))
    local real l = GetUnitState(h, UNIT_STATE_LIFE)
    local real e = 5
    if l <= 0 then
        set e = 1.5
    endif
    if l < GetUnitState(h, UNIT_STATE_MAX_LIFE)/5 then
        call IssuePointOrder(h, "move", GetUnitX(gg_unit_nfoh_0001), GetUnitY(gg_unit_nfoh_0001))
        set e = 3
    else
        if ((o == "smart") or (o == "attack") or (o == "patrol") or (o == "move") or (o == "stop") or (o == "hold") or (o == null)) then
            set g = CreateGroup()
            set b = Condition(function AIFilterEnemyConditions)
            call GroupEnumUnitsInRange(g, x, y, 500, b)
            set f = FirstOfGroup(g)
            if f == null then
                set i = Rect(x-800, y-800, x+800, y+800)
                set be = Condition(function AIItemFilter)
                set bj_lastRemovedItem=null
                call EnumItemsInRect(i, be, function AISetItem)
                if bj_lastRemovedItem != null and (GetItemType(bj_lastRemovedItem) == ITEM_TYPE_POWERUP or AIHasEmptyInventorySlot(h)) then
                    call IssueTargetOrder(h, "smart", bj_lastRemovedItem)
                else
                    set r = GetRandomLocInRect(bj_mapInitialPlayableArea)
                    call IssuePointOrderLoc(h, "patrol", r)
                    call RemoveLocation(r)
                endif
                call RemoveRect(i)
                call DestroyBoolExpr(be)
            else
                call IssueTargetOrder(h, "attack", f)
            endif
            call DestroyGroup(g)
            call DestroyBoolExpr(b)
        endif
    endif
    if GetHeroSkillPoints(h) > 0 and l > 0 then
        call AILearnSkill(h, a)
    endif
    call TimerStart(GetExpiredTimer(), e, true, function AILoop)
    set h = null
    set i = null
    set r = null
    set g = null
    set b = null
    set f = null
    set be = null
endfunction

function StartAI takes unit hero returns nothing
    local timer m = CreateTimer()
    call AttachObject(m, "hero", hero)
    call TimerStart(m, 0, false, function AILoop)
    set m = null
endfunction

function PlayerLeaves takes nothing returns nothing
    local player p = GetTriggerPlayer()
    call DisplayTextToForce(bj_FORCE_ALL_PLAYERS, GetPlayerName(p)+" has left the game.")
    if udg_Hero[GetPlayerId(p)] != null then
        call StartAI(udg_Hero[GetPlayerId(p)])
    endif
    set p = null
endfunction

function SetupSkills takes nothing returns nothing
    local string h
// Paladin
    set h = UnitId2String('Hpal')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AHhb')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AHds')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AHad')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AHre')
// Archmage
    set h = UnitId2String('Hamg')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AHbz')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AHab')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AHwe')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AHmt')
// Mountain King
    set h = UnitId2String('Hmkg')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AHtc')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AHtb')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AHbh')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AHav')
// Blood Mage
    set h = UnitId2String('Hblm')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AHfs')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AHbn')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AHdr')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AHpx')
// Blademaster
    set h = UnitId2String('Obla')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AOwk')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AOcr')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AOmi')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AOww')
// Far Seer
    set h = UnitId2String('Ofar')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AOfs')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AOsf')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AOcl')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AOeq')
// Tauren Chieftain
    set h = UnitId2String('Otch')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AOsh')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AOae')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AOws')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AOre')
// Shadow Hunter
    set h = UnitId2String('Oshd')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AOhw')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AOhx')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AOsw')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AOvd')
// Death Knight
    set h = UnitId2String('Udea')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AUdc')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AUdp')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AUau')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AUan')
// Lich
    set h = UnitId2String('Ulic')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AUfn')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AUfu')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AUdr')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AUdd')
// Dreadlord
    set h = UnitId2String('Udre')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AUav')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AUsl')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AUcs')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AUin')
// Crypt Lord
    set h = UnitId2String('Ucrl')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AUim')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AUts')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AUcb')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AUls')
// Keeper of the Grove
    set h = UnitId2String('Ekee')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AEer')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AEfn')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AEah')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AEtq')
// Priestess of the Moon
    set h = UnitId2String('Emoo')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AEfa')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AEst')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AEar')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AEsf')
// Demon Hunter
    set h = UnitId2String('Edem')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AEmb')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AEim')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AEev')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AEme')
// Warden
    set h = UnitId2String('Ewar')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AEbl')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AEfk')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AEsh')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AEsv')
// Alchemist
    set h = UnitId2String('Nalc')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANhs')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANab')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANcr')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANtm')
// Naga Sea Witch
    set h = UnitId2String('Nngs')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANfl')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANfa')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANms')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANto')
// Tinker
    set h = UnitId2String('Ntin')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANsy')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANcs')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANeg')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANrg')
// Beast Master
    set h = UnitId2String('Nbst')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANsg')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANsq')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANsw')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANst')
// Dark Ranger
    set h = UnitId2String('Nbrn')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANsi')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANba')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANdr')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANch')
// Firelord
    set h = UnitId2String('Nfir')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANic')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANso')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANlm')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANvc')
// Pandaren Brewmaster
    set h = UnitId2String('Npbm')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANbf')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANdh')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANdb')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANef')
// Pit Lord
    set h = UnitId2String('Nplh')
    call StoreInteger(udg_GameCache, h, "BaseSkill1", 'ANrf')
    call StoreInteger(udg_GameCache, h, "BaseSkill2", 'ANht')
    call StoreInteger(udg_GameCache, h, "BaseSkill3", 'ANca')
    call StoreInteger(udg_GameCache, h, "UltimateSkill", 'ANdo')
endfunction

//===========================================================================
function InitTrig_AI takes nothing returns nothing
    local integer i = 0
    set gg_trg_AI = CreateTrigger(  )
    loop
        exitwhen i > 11
        call TriggerRegisterPlayerEventLeave( gg_trg_AI, Player(i) )
        set i = i + 1
    endloop
    call TriggerAddAction( gg_trg_AI, function PlayerLeaves )
    call ExecuteFunc("SetupSkills")
endfunction

This is an old system but has some good ideas. I'd try to base it off of something like this.
 
Level 8
Joined
Mar 20, 2011
Messages
264
Hmm, not bad... but too simple don't you think...
I'm making a RPG type map and there are quests, The AI needn't do 'em but I want them to kill creeps, return to base and follow commands such as '-help','-attack boss','-gold XXXX'
If they respond to signals, much better. :D
 
Level 7
Joined
Dec 3, 2006
Messages
339
Well, the old trigger was more like creep type heroes that level up(in the sense that they mostly just camp). It could easily be adapted to respond to commands etc. The coding is awfully outdated though relying on gamecache. But I thought it has a good idea that it's not hero specific in that it will learn abilities for a hero without having to do anything too crazy.
 
Status
Not open for further replies.
Top