Enemy A.I. Code???
Blitz3D Forums/Blitz3D Programming/Enemy A.I. Code???
| ||
Hi All... Just wanted to ask some of you if you know where there might be a fairly decent code sample I might be able to start with in order to create my enemy A.I. code as a base? This would help me get a little bit of a head start on the game you know! But if not! That's cool too! :) If someone might have a A.I. sample lying around... Please let me know! Thank's Guys! ;) |
| ||
For what kind of game? AI is going to be totally different for different games. Simple AI is IF [Certain conditions] then [do stuff] Of course take this a stage further and you get something like IF [certain conditions] and [in a certain state] then [go into this state] If [in a certain state] then [do stuff] Makeing it more flexible still if [certain conditions] then [change state on a sliding scale] Then you have actions based on sliding scales rather than specific states. In my lander game, for the bad guy fighter AI, they have fixed states of Retreat, Roam, defend, evade and attack. So you end up with stuff like If roaming and can see a goodguy then attack said goodguy If attacking and health low then retreat if attacking and a goodguy is in my sights then shoot the bugger If I can see someone attacking another badguy then defend said badguy If retreating and getting shot then evade if retreating and can't see any goodguy then roam if roaming and can't see anyone then find a newpoint to roam to on top of this you had the non state ai that was: If too close to the ground then pull up If too high then push down if too far off course then turn back on course if there is a mountain in front of me pull up and so on. The scouts had a different set of states and ai conditions, likewise the defence cannons. I hope that helps some!? |
| ||
Dam! Im' sorry Rob!! I should of been more specific!! Im' needing an A.I. code sample for my Egyptian game for a FPS type!!! Basically! I need the enemies to chase the player when the player gets within a certain range of the enemy! Then ofcourse... Once the enemy gets so close to the player... start attacking him! I have an idea on how to write this code and im' working on a short sample now of my own... But I just started this too! Any suggestion's or possibly a code sample to start with??? This would help me out alot!!! :) |
| ||
entitydistance, I too am looking to write something like this, At the moment my guys follow a path, and some path have a "TREE" node that can split off in to any amount of paths, the AI chooses by randomly picking out the path. |
| ||
Hmmmmm.... Sounds interesting!!! Would love to take a look at it! :) |
| ||
That's basic state AI You code would look something like: If entitydistance(badguy,player)<chasedistance and badguystate = roam If the badguy can see the player (linepick) badguy go into chase mode, chasing player endif endif if entitydistance(badguy,player)<attackdistance and badguystate=chase attack player endif if entitydistance(badguy,player)>chasedistance then badguystate=roam if badguystate=roam then bumble around if badguystate=attack then smack,smash,destroy if badguystate=chase where's the good guy. Point at him Move towards him endif |
| ||
Yeah, but surely you need to factor in: 1. Can the enemy hear him 2. Is the enemy injured. If so, would he attack or hide. 3. Teaming - if multiple enemy's. 1 might attack face on but the others might try to go around the player and attack from the back. 4. Types of attack. - Some weapons might be better in some circumstances then others. - When to use shields. 5. Protect team mates. - If another enemy is attacking you close range, does it really make sense for the other enemy to throw in a grenade. 6. Different styles of enemy. - Frontal attack (maniac) - Steath sniper etc... You get the idea. I think the ideal is to create some kind of enemy automaton with personality/strengths/weakness with good path finding skills and a mini mission of it's own (ie - patrol and protect this area) and then let it loose in your level. Should provide a more realistic playing experience. |
| ||
Just put together a very basic snippet to demonstrate state based AI, use the arrow keys to move around, you're the P! The enemy has roam, chase and attack states, and will show it's state with it's colour and a R,C, or A. Graphics 640,480,32,2 ; simplistic ai ; Rob Farley 2004 px#=320 py#=240 pspd#=1 Const roam=1 Const chase=2 Const attack=3 ex#=Rand(0,640) ey#=Rand(0,480) espd#=.5 estate=roam echasedis = 100 eattackdis = 20 roamx=Rand(0,640) roamy=Rand(0,480) kup = 200 kdown = 208 kleft = 203 kright = 205 SetBuffer BackBuffer() Repeat Cls ; player control If KeyDown(kup) Then py=py-pspd If KeyDown(kdown) Then py=py+pspd If KeyDown(kleft) Then px=px-pspd If KeyDown(kright) Then px=px+pspd ; find the distance from the player to the enemy dis# = Sqr(((px-ex)*(px-ex)) + ((py-ey)*(py-ey))) ; enemy AI If estate=roam And dis<echasedis Then estate=chase If estate=chase And dis>echasedis Then estate=roam If estate=chase And dis<eattackdis Then estate=attack If estate=attack And dis>eattackdis Then estate=chase If estate=roam ; go to a random place on the screen If ex<roamx Then ex=ex+espd If ex>roamx Then ex=ex-espd If ey>roamy Then ey=ey-espd If ey<roamy Then ey=ey+espd dis# = Sqr(((roamx-ex)*(roamx-ex)) + ((roamy-ey)*(roamy-ey))) If dis<50 Then roamx=Rand(0,640) roamy=Rand(0,480) EndIf EndIf If estate=chase ; chase the player If ex<px Then ex=ex+espd If ex>px Then ex=ex-espd If ey>py Then ey=ey-espd If ey<py Then ey=ey+espd EndIf If estate=attack ; erm... chase the player hacking 'n' slashing If ex<px Then ex=ex+espd If ex>px Then ex=ex-espd If ey>py Then ey=ey-espd If ey<py Then ey=ey+espd EndIf ; draw advanced graphics Color 0,255,0 Oval px-10,py-10,21,21,False Text px,py,"P",True,True If estate=roam Then Color 255,255,0 If estate=chase Then Color 255,160,0 If estate=attack Then Color 255,0,0 Oval ex-10,ey-10,21,21,False If estate=roam Then Text ex,ey,"R",True,True If estate=chase Then Text ex,ey,"C",True,True If estate=attack Then Text ex,ey,"A",True,True Flip Until KeyHit(1) |
| ||
Thank's Rob!!!! This will be an excellent base to work from!! I was working with the collide demo sample in the mak directory of Blitz3D! But this looks like a good start!!! Thank you very much!!! ;) I'll make sure your name get's proper credit in the credits section of the game!! ;) |
| ||
Pleased I could help and there's really no need to credit me for about 3 minutes work! But I apprecaite the sentiment. |
| ||
The book "A.I. Game programming" by Alex J. Champandard see also the site "artificial intelligence depot" should be focused on "enemy A.I " even though your question is actually a little bit generic |
| ||
this is a clip from the one i wrote, its messy, but they dudes wont interact with the rest of the world if i have it different have a look, it may help you come up with something better For e.tEnemies = Each tEnemies tx# = EntityX#(e\mass) ty# = EntityY#(e\mass) tz# = EntityZ#(e\mass) PositionEntity e\body, tx#, ty#, tz# PositionEntity e\pivot, tx#, ty#, tz# ryaw# = EntityYaw#(e\pivot, 0) cyaw# = EntityYaw#(e\body, 0) cr# = CurveValue#(cyaw#, ryaw#, 15) RotateEntity e\body, 0, cr#, 0 If Not Animating(e\model) Then Animate(e\model,1,.23,enemy_mode_walk) Else If e\mode = 1 e\woddle = (e\woddle + .12) Mod AnimLength(e\model) at# = AnimTime(e\model) PositionEntity e\model, 0, -3 + (Cos(at * 110)/ 2), 0 ;TurnEntity e\model, (Cos(at * 110)/ 2),0,0 RotateEntity e\model, (Cos(at * 110)*2),EntityYaw(e\model),-(Cos(e\woddle * 110)*3) EndIf End If If e\currentnode=0 Then e\mode = 3 ;If e\timer>0 Then e\timer = e\timer - 1 If e\mode = 3 If e\direction = 0 Then ; going from 1 ... 2... 3... oldnode = e\currentnode nextnode = e\currentnode + 1 Else; going to 3... 2... 1... nextnode = e\currentnode - 1 EndIf vring = 0 e\newnode = e\newnode + 1 veld% = 1 For n.tNodes = Each tNodes If e\nodegroup$ = n\nodegroup$ vring = vring + 1 If n\nodeid = oldnode Then If (n\choose$ <> "") And (e\newnode>2) ; Bot can choose e\newnode = 0 If Int(Rnd(1))=1 Then veld% = 0 ; Not to set the next node! npath$ = n\choose$ npathid$ = n\choosepath$ For jn.tNodes = Each tNodes If jn\nodegroup$ = npath$ If jn\nodeid = Int(npathid$) e\targetnode = jn\pivot e\currentnode = jn\nodeid e\nodegroup$ = npath$ End If End If Next End If End If EndIf If veld% = 1 Then If n\nodeid = nextnode Then e\targetnode = n\pivot e\currentnode = n\nodeid End If EndIf EndIf Next If veld% = 1 Then If (nextnode > vring) e\direction = 1 ElseIf (nextnode < 1) e\direction = 0 EndIf EndIf e\mode = 1 End If If e\mode = 1 Then If e\targetnode<>0 Then PointEntity e\pivot, e\targetnode MoveEntity e\mass,0,0,.24 ryaw# = EntityYaw(e\pivot) RotateEntity e\mass, 0, ryaw#, 0 d# = EntityDistance(e\targetnode,e\mass) If d#<4 Then ; hes reached his target e\mode = 3 e\timer = 60 End If EndIf EndIf e\y_vel#=(ty-e\PlayerY) e\PlayerY=ty e\y_vel=e\y_vel-.07 TranslateEntity e\mass,0,e\y_vel,0 Next |
| ||
Also from what i read above, that you want the enemies to chase, i believe your going to need A* path finding - which is worth reading into. |
| ||
there is my 3d a* path finding lib in the code archives with a basic demo or there are 2d path finding libs some where. |
| ||
yea, there is a 3d lib on blitzcoder iirc :P |
| ||
There's a finite state machine tutorial here as well. |