Advertisement

Space Battle!!

Started by October 06, 2003 05:44 PM
14 comments, last by fixxorion 21 years, 4 months ago
Can you send me the example as well, please?
I would really appreciate it - I am working on a star control type melee game and am still strugling with the AI.

mail: 13436740@sun.ac.za

thanks,
neil
Ok, I've sent the example - twice, because I sent it... then decided to improve it so that you can create your own custom battles etc. The second version is much better. Anyhow... for how the AI works
In the main loop for the game, we do the AI for every spaceship:
Set our target to point at nothing (target being a pointer)If any enemy spaceships exists, we find the closest one and set our target to it.Now we ask if target still equals nothing, and if it doesnt, then we do this:Take the direction to our target and call it P.Take the distance to our target and call it D.Note: When I say direction, I refer to directly right as 0, up as 90, left as 180 etc in an anti-clockwise circleTake the difference between the current direction that our ship is facing and P, and call this ANote: At this point I will metion that each spaceship has the following variables describing its cababilities - acceleration, max-speed and turnspeedWe now ask if D is bellow a certain number (I use 30 in the example, buy this number will differer depending on the scale of the spaceships and the range of the weapons) and if it is, we do this:   if(A<80)direction+=turnspeed;   if(speed<maxspeed)speed+=acceleration;Thus, if we are too close to our enemy we will turn away to our left (or right, it doesnt mater).We now ask if A is above 5, and if it is, we turn toward our target. You may notice that this contradicts the lines of code above, but suprisingly, it works well - you have the effect of 'swerving' while an enemy craft tails.If a is not above 5, however, we are staring straight at our target. So in this case, we ask if D<100(this nuber will vary depending on scale/ weapon range), and if it is, we fire our guns (provided our guns are ready to be fired).The last part is this:  if(A>30){if(speed>1)speed-=acceleration/2;}  else{if(speed<maxspeed)speed+=acceleration;}This is a very important part: if we are facing our target, or near facing our target, we will speed up. Otherwise, we will be turning, so we should slow down.Now this AI is not without flaws - often you will see two oposing fighters flying alongside, both swerving - they are on eachother's tail, or so they both think. But this situation is resolved withing a few seconds.There is something I strongly suggest when programming AI. You should give each ship variables such as:int accel;int turndir;What these do is stop conflicts between different parts of the AI script. After the script is executed, change the ship's position, direction, speed based on the variables.direction+=turnspeed*turndir.if(accel==1)speed+=acceleration; else if(accel==-1) speed-=accelaration/2;Each loop, set these variables to 0, then executed the script, then update the spacecraft based on new information. Instead of this:  if(A>30){if(speed>1)speed-=accel/2;}  else{if(speed<maxspeed)speed+=acceleration;}do this:  if(A>30){if(speed>1)accel=-1;}  else{if(speed<maxspeed)accel=1;}Then if the speed changes later on in the script it doesn't mess everything up. Also, I makes it extreamely easy to take player input. Take a look at this (executed for each ship every loop):accel=0;turndir=0;shooting=0;if(control=0) //humanTakePlayerInput();elseAIScript();//Now update the ship based on new infodirection+=turnspeed*turndir.if(accel==1)speed+=acceleration; else if(accel==-1) speed-=accelaration/2;if(shooting)//make bullet etcThis gives also gives the feel that the AI is 'piloting' the ship, and makes sure that AI ships follow the same physics as you do.Man, this post is getting really long... I feel like I writing a tutorial or something. Oh well, next part: improvements.While the AI I have described works well enough for simuating large space battles (suitable for stratergy games etc), I the player is going to be flying against these ships, I'm not sure how the AI would react. So the AI needs to be improved. Note that I perousfully kept it simple, so I could have many ships in the air at the same time.The first improvement that comes off the top of my head involves this:If any enemy spaceships exists, we find the closest one and set our target to it.Obviously, the closest target is not always the best one. Targest should be chosen from a combination of information - mainly distance and difference between our direction and the direction the the enemy, but also take into account things like a ship's health, and a really smart thing to do would be to take into account how many enemy ships are nearby the possible target compared to how many friendly ships are nearby our possible target... we dont want to willingly fly into an ambush. However, this sort of this has a heavy proscessor cost. When pick a target, you must go through each enemy, give the enemy a value based on all the stuff, and then pick the best one (normally the lowest valued one). Or, you could simply chose the closest enemy, if you want to keep it simple.Another way of doing things would be to calculate the best target for each ship, then calculate the ships into small groups (based on distance between ships), then, for each group, you could go through each ships target, pick the best one for the group (using the groups average xy position, direction, health etc), and then assign the target to the entire group. I have never tried this, but it would make for some interesting squad-based combat - quite enteratining to watch I would think.Another way to improve your ship's AI is to create more realistic flight tactics - using more ifs and else's, and a lot of testing.An obvious way to improve the ships AI (ecspecially when facing human pilots) is to have the AI aim at a point a ahead of a moving target based on the target's direction/velocity. However, this involves some complicated mathamatics so I'm not going to go into that here.I feel that I should metion that I have chosen not to include vector movement (I think that is what it is called). Ie, ships simply fly straight forward in whatever direction they are facing, as apposed to difting like something would were he in space.

Anyhow... now that I have finished writing this book... ah... post, I certainly hope that you have benifited from it, considering it has taken half an hour to explain. lol.

Edit: I have put everything in source tags because for some reason the text turned out wrong if I didn't...

[edited by - jack_1313 on October 7, 2003 7:58:33 PM]
Advertisement
Thanks for the info! Much appreciated!

fixxorion
Grate job man , thanks for the info

moon
Great example, bud!

Out of curiosity, I ran the sim several times and the same outcome occurred everytime. Do you use the same random seed or did you just not use any random numbers?
Jason Arorajason@pubism.comhttp://www.pubism.com
Hi JasonA. The AI uses no random numbers, which causes the same result every time you run the same battle. Did you get my second mail, where I have changed the program so that you lay down the ships, then run the battle? I sent it, but the got a message back from the hotmail ppl saying that the mail could not be sent. Is you inbox full? If you want the newer program, say so and I will send it again. It is quite a lot of fun just to test different formations of spaceships against eachother.

This topic is closed to new replies.

Advertisement