AI Targetting
BlitzMax Forums/BlitzMax Beginners Area/AI Targetting
| ||
I'm having a bit of trouble with my AI for a car arena game (cars shoot cars, 'nuff said). I'm using the Sprite Behaviors mod by Scott Shaver for movement and all that, and while there are a few issues with it and wall avoidance, it's been running pretty smoothly for me. The real problem for me at the moment is AI targeting, which really seems sporadic. My basic code is to have the AI weight all of his actions (get ammo, run away, wander, etc). I then have him search for the closest opponent vehicle, and if he is within a certain range, shoot him. My search code scans all available vehicles, and compares their distances. The shortest one wins and is assigned to the Target field. Then the shoot code examines the target's distance, and if it within the shoot range, fire on him based on the type of AI. Method searchForTarget() If Target <> Null Then Return Local tempX = 0 For Local tVehicle:Vehicle = EachIn Vehicle.List If tempX = 0 tempX = Distance(myVehicle.X,myVehicle.Y,tVehicle.X,tVehicle.Y) target = tVehicle EndIf If tempX < Distance(myVehicle.X,myVehicle.Y,tVehicle.X,tVehicle.Y) tempX = Distance(myVehicle.X,myVehicle.Y,tVehicle.X,tVehicle.Y) target = tVehicle EndIf Next If Target = myVehicle Then Target = Null EndMethod Method Shoot() If Target = Null Then Return If Distance(myVehicle.X,myVehicle.Y,target.X,target.Y) < 650 Local tempX:Float = Target.X - (myVehicle.X) Local tempY:Float = Target.Y - (myVehicle.Y) Local Angle:Float = ATan2(tempY,tempX) 'Shoot Code Here, doesn't really relate EndMethod This code results in pretty sporadic behavior. Sometimes he finds the target, sometimes he doesn't. It usually depends on the AI spawning near the player (right now, I just have one AI in the game, playing against one human player). Am I approaching this correctly, or are you horrified by the n00bliness of the code. This is only my second game in BlitzMax (the other was a basic platformer), but I have several years of hobby experience with programming. This is my first attempt to develop a competitive search-and-destroy AI, so I'm open to any and all suggestions (even completely re-writing the AI!). Thanks a bunch in advance :) |
| ||
Might need to see some more code but couldn't you say Method searchForTarget() If Target <> Null Then Return Local tempX = 999999 For Local tVehicle:Vehicle = EachIn Vehicle.List If tvehicle = Self Exit If tempX < Distance(myVehicle.X,myVehicle.Y,tVehicle.X,tVehicle.Y) tempX = Distance(myVehicle.X,myVehicle.Y,tVehicle.X,tVehicle.Y) target = tVehicle EndIf Next EndMethod |
| ||
Hmmm...I think that worked. I had to make some modifications (if tVehicle = Self Exit could cause some long-term problems, and tempX was set to too high a value), but now the AI seems much more accurate in his targeting. Thanks a bunch! |