Hi Friends
I want to use btrees from GdxAI for my game AI. I do not know where is the best place to ask this question, on libGdx forum or here ?
Let me just ask it here and hope for the help from nice guys
To get the basic idea of how GdxAI is working please take a look at Behavior Trees Ā· libgdx/gdx-ai Wiki Ā· GitHub (Itās afew pages)
So as an example, this is test for a Dog ai. We have afew dogs and each has an owner and they are loyal to their owners.
(image : GitHub - jsjolund/GdxDemo3D: 3D demo game built with libGDX)
dog.btree
import calculatePathToHuman:"com.mygdx.game.objects.dog.CalculatePathToHumanTask"
import calculatePathToStick:"com.mygdx.game.objects.dog.CalculatePathToStickTask"
import followPath:"com.mygdx.game.objects.dog.FollowPathTask"
import giveStick:"com.mygdx.game.objects.dog.GiveStickTask"
import lieDown:"com.mygdx.game.objects.dog.LieDownTask"
import pickUpStick:"com.mygdx.game.objects.dog.PickUpStickTask"
import piss:"com.mygdx.game.objects.dog.PissTask"
import setThrowButton:"com.mygdx.game.objects.dog.SetThrowButtonTask"
import sit:"com.mygdx.game.objects.dog.SitTask"
import spinAround:"com.mygdx.game.objects.dog.SpinAroundTask"
import spinAroundToFaceHuman:"com.mygdx.game.objects.dog.SpinAroundToFaceHumanTask"
import stand:"com.mygdx.game.objects.dog.StandTask"
import stickThrown?:"com.mygdx.game.objects.dog.StickThrownCondition"
import wander:"com.mygdx.game.objects.dog.WanderTask"
import whine:"com.mygdx.game.objects.dog.WhineTask"
import alreadyCriedForHumanDeath?:"com.mygdx.game.objects.dog.AlreadyCriedForHumanDeathCondition"
import isHumanDead?:"com.mygdx.game.objects.dog.IsHumanDeadCondition"
import isHumanInRange?:"com.mygdx.game.objects.dog.IsHumanInRangeCondition"
import humanWantToPlay?:"com.mygdx.game.objects.dog.HumanWantToPlayCondition"
import setAlreadyCriedForHumanDeath:"com.mygdx.game.objects.dog.SetAlreadyCriedForHumanDeathTask"
subtree name:"shouldFeelSadForHumanDeath?"
sequence
invert
alreadyCriedForHumanDeath?
isHumanDead?
isHumanInRange? meters:20
subtree name:"feelSadForHumanDeath"
sequence
calculatePathToHuman
followPath gait:"run"
whine
parallel policy:"selector"
wait seconds:"uniform,10,25"
lieDown
whine
parallel policy:"selector"
wait seconds:"uniform,5,9"
sit
setAlreadyCriedForHumanDeath # this makes the subtree's guard fail; the dog will start acting on his own
subtree name:"playWithMan"
sequence
calculatePathToHuman
followPath gait:"run"
giveStick
spinAroundToFaceHuman
setThrowButton enabled:true
parallel policy:"selector" # wait for the man to throw the stick
stickThrown?
repeat
sequence
parallel policy:"selector"
wait seconds:"uniform,3,5"
randomSelector
stand
sit
spinAround
setThrowButton enabled:false
calculatePathToStick
followPath gait:"run"
pickUpStick
subtree name:"actOnYourOwn"
selector
(random success:0.1) piss
parallel policy:"selector"
wait seconds:"uniform,3,6"
randomSelector
wander gait:"run"
wander gait:"walk"
lieDown
sit
root
dynamicGuardSelector
($shouldFeelSadForHumanDeath?) $feelSadForHumanDeath
(humanWantToPlay?) $playWithMan
() $actOnYourOwn # fallback behavior (no guard)
This creates behavior tree from text file. First we import LeafTask
s. You can take a look at them here
Then we gather them in a concrete behavior as a subtree
.
Then at the very bottom
root
dynamicGuardSelector
($shouldFeelSadForHumanDeath?) $feelSadForHumanDeath
(humanWantToPlay?) $playWithMan
() $actOnYourOwn # fallback behavior (no guard)
we create root behavior which is running in a loop.
so as an example
($shouldFeelSadForHumanDeath?) $feelSadForHumanDeath
the firs part shouldFeelSadForHumanDeath?
is a Guard
for the feelSadForHumanDeath
task which means it will be executed only if itās guard task be successful.
and the last one
() $actOnYourOwn # fallback behavior (no guard)
which is a task with no guard and indicate a fallback behavior.
Here is the Class hierarchy for tasks
And should mention that tasks can accept a blackboard object (here DogCharacter) which is the entity the task is running on it.
Note that instead of using text file i also can create subtrees
(branch tasks) programatically.
Sorry it is too long ā¦
And i want to know what/how is the correct way to incorporate it with ES ? Any idea is very much appreciated.
I am considering to run AI on server.
So should i just create a DogState and load the the whole tree at once and update it from there ?
Or should i break the tree and create separate states (systems) for each behavior (subtrees
)
for example create one state which create and run/update this behavior ($shouldFeelSadForHumanDeath?) $feelSadForHumanDeath
and another one for (humanWantToPlay?) $playWithMan
?