- Joined
- Sep 12, 2008
- Messages
- 657
hey.. i finised my gui code of the computer ai, which went terribly wrong...
so i tried remaking it in vjass.. somehow i managed to remember some old stuff
anyways, it managed to create heroes, store data, etc.. but it wont work correctly ;/
it uses 2 libraries, 1 i made, and the second is grouputils.
the problem:
it wont go thru the loop i supose.. cant really find it out =]
it creates hero, and wont do anything else, move, attack, etc.
heres the code:
so i tried remaking it in vjass.. somehow i managed to remember some old stuff
anyways, it managed to create heroes, store data, etc.. but it wont work correctly ;/
it uses 2 libraries, 1 i made, and the second is grouputils.
the problem:
it wont go thru the loop i supose.. cant really find it out =]
it creates hero, and wont do anything else, move, attack, etc.
heres the code:
JASS:
library ComputerAI initializer init requires GroupUtils, GetClosestUnit
globals
//Declared Data
private timer tim = null
private integer array index
private integer array heroList
private integer count
//Used inloop data
private unit array botHero
private player array botOwner
private string array botState
private unit array botTarget
private group array botGroup
private real array botHealSpotX
private real array botHealSpotY
//
endglobals
//List of indexes:
//Index[0] = Hero List Count
//Index[1] = Bots In Total
//Index[2] = counting units in group
//
public function AddHeroToList takes integer unitid returns integer
set index[0] = index[0] + 1
set heroList[index[0]] = unitid
return index[0]
endfunction
public function RemoveHeroFromList takes integer index returns nothing
set heroList[index] = 0
endfunction
public function LocateUnitsBoolexpr takes nothing returns boolean
return GetWidgetLife(GetFilterUnit()) > 0.405 and IsUnitEnemy(GetFilterUnit(), botOwner[count]) and IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) == false
endfunction
public function CreateNewBot takes player p, location startLoc, location healLoc returns integer
local boolean pickedHero = false
local integer num = 0
set index[1] = index[1] + 1
loop
set num = GetRandomInt(1, index[0])
if heroList[num] != 0 then
set pickedHero = true
endif
exitwhen pickedHero == true
endloop
set botHero[index[1]] = CreateUnit(p, heroList[num], GetLocationX(startLoc), GetLocationY(startLoc), 0)
set botOwner[index[1]] = p
set botHealSpotX[index[1]] = GetLocationX(healLoc)
set botHealSpotY[index[1]] = GetLocationY(healLoc)
set botGroup[index[1]] = NewGroup()
return index[1]
endfunction
private function HandleActions takes nothing returns nothing
if botState[count] == "moving to heal" then
call IssuePointOrder(botHero[count], "move", botHealSpotX[count], botHealSpotY[count])
if GetUnitStatePercent(botHero[count], UNIT_STATE_LIFE, UNIT_STATE_MAX_LIFE) < 98 then
set botState[count] = "healing"
endif
else
if botState[count] == "attacking enemy" then
call IssueTargetOrder(botHero[count], "attack", botTarget[count])
endif
endif
endfunction
private function PrepareActions takes nothing returns nothing
if GetUnitStatePercent(botHero[count], UNIT_STATE_LIFE, UNIT_STATE_MAX_LIFE) <= 30 or botState[count] == "healing" then
set botState[count] = "moving to heal"
set botTarget[count] = null
if GetUnitStatePercent(botHero[count], UNIT_STATE_LIFE, UNIT_STATE_MAX_LIFE) >= 98 then
set botState[count] = "no actions"
endif
else
if GetUnitState(botHero[count], UNIT_STATE_LIFE) <= 0 or index[2] <= 0 then
if index[2] <= 0 then
call GroupEnumUnitsInRect(botGroup[count], bj_mapInitialPlayableArea, function LocateUnitsBoolexpr)
call GroupRefresh(botGroup[count])
set index[2] = CountUnitsInGroup(botGroup[count])
endif
if GetUnitState(botHero[count], UNIT_STATE_LIFE) <= 0 then
set index[2] = index[2] - 1
set botTarget[count] = FindClosestUnit(botGroup[count], GetUnitX(botHero[count]), GetUnitY(botHero[count]))
set botState[count] = "attacking enemy"
endif
endif
endif
endfunction
private function ComputerAIActions takes nothing returns nothing
set count = 0
if index[1] != 0 then
loop
set count = count + 1
if GetUnitState(botHero[count], UNIT_STATE_LIFE) > 0 then
call PrepareActions()
call HandleActions()
endif
exitwhen count == index[1]
endloop
endif
endfunction
private function init takes nothing returns nothing
set tim = CreateTimer()
call TimerStart(tim, 1, true, function ComputerAIActions)
endfunction
endlibrary