AI?

Blitz3D Forums/Blitz3D Beginners Area/AI?

Happy Llama(Posted 2009) [#1]
I have only read 1 book and trying to find out how to use AI!!! any one?


Drak(Posted 2009) [#2]
Thats a very, very broad topic. What do you want your ai to do? What is your ai controlling? I would recommend reading up some more. AI is both very simple and also very complicated at the same time. You'll have to be way more specific.


Happy Llama(Posted 2009) [#3]
Well would like to have enemies to follow you.Just the problem is I just can't find any books that talks
about Blitz Basic. Have any suggestions?


Nate the Great(Posted 2009) [#4]
check out this program. It should help. source code included
http://www.blitzbasic.com/Community/posts.php?topic=83318

The missiles are the main AI part. check out any function that has the word missile in it and you will see some AI. They dodge landmines while following the other player.

This is how AI for missiles works.

see which direction would get you closest to your target (left 1 degre right one degree or keep going the same direction)

go that direction

see if there are any landmines close
if so then turn away

This can basically be turned into AI for any type of character


Drak(Posted 2009) [#5]
This is AI in it's simplest form. Copy and paste this. The Green sphere will follow you around. Move with the arrow keys.




Ginger Tea(Posted 2009) [#6]
as said above AI is a very broad subject
chess AI is probably one of the most complex as you dont want to be playing against someone randomly moving pieces around the board

without knowing whats chacing what here is an analogy
the pacman ghosts had simple states for chacing pac eg:
one would always turn left a wall
another right
but if they saw pac they would 'chace' pac untill they lost sight then revert back to their wandering state
not quite AI but enough to give them something to do instead of standing around all day

if for example the maze was tall enough to not jump over but short enough to see pac anywhere on the screen then the use of path finding like A* and others could be used

still not quite AI in the accedemic sence and clyde wouldnt pass the turing test but hes more challenging to pac now, in the past you could see everything on screen, they couldnt, now its even and hes plotting his fastest route to you, or your last calculated position, as no doubt as he moves so do you


Ross C(Posted 2009) [#7]
I actually though chess AI was easier (but still tricky) then AI in an open environment, like a fps. At least in chess you know that each piece can only move to certain squares, and you don't need to do any pathfinding stuff with it.


Ginger Tea(Posted 2009) [#8]
true in a way but geting a decent game out of a chess program takes oodles of training befor its sent out into the world
sure a chess program knows that a queen can only move this way a night that so its not going to move the queen like a knight, but it should also not make blatantly game loosing moves
thankfully they can work out a dozzen scenarios in seconds based on your last move and if you move how one of the branches predicted (not hard as there are only a finite number of moves) then it will keep on that track of precalculations untill you move a piece it didnt expect you to make, then its back to the dozzen scenarios again

trying to get a humanoid to do things is harder as we are more complex than moving pieces around a board
but without the context of game type its hard to say what would be the best option to persue


Ross C(Posted 2009) [#9]
Yeah, your right about tweaking the blasted thing... PLus you've got baiting playeras to take pieces, so you can capture a more valuable piece.


GIB3D(Posted 2009) [#10]
If you used the way I play chess for chess AI, it would be the worst ever. I've never won at chess in my life. I usually make random moves because I'm not good at thinking that far ahead when it comes to chess (too many types of movements to focus on). I know which direction each piece is supposed to move, but when it comes to thinking about more than 2 pieces and where they can go in different scenarios, I just can't do it, unless the player is willing to wait for a few hours.


Ginger Tea(Posted 2009) [#11]
thats where computers excell
even without AI even a humble z80 could pre calc dozzens of moves in the time it took to click your piece

I know which direction each piece is supposed to move, but when it comes to thinking about more than 2 pieces and where they can go in different scenarios, I just can't do it, unless the player is willing to wait for a few hours.


same with me, if i could remember where each piece went that is ;)
pawns are easy same as king queen and rooks i always get knights and bishops mixed up on the board


chrisnorris007(Posted 2009) [#12]
what about if you only wanted enemys to follow you if they were a certain distance...say in their field of view


Ross C(Posted 2009) [#13]
You would check the distance each time, between the enemy and player and react according to the above posted code.

The easiest way (i find anyway), if you using an enemies field of view, is to position a cone and scale it appropriatly, to represent the field of view. If a character collides, then he is in the field of view.


chrisnorris007(Posted 2009) [#14]
so basically an invisible cone? explain..im trying to learn this stuff


Ross C(Posted 2009) [#15]
Just an invisible cone, parented to the enemy.

Maybe though, because your doing this in code, you should check the angle between the player and the enemy. If this is within the enemies viewing angle, then check the distance. If that also checks out, then use the above code.


chrisnorris007(Posted 2009) [#16]
is it complicated to check angle and distance between said enemy and you?

as I said...trying to learn as best I can...got a lot of books im reading :)


Ross C(Posted 2009) [#17]
Well, in bliutz3d, you have the EntityDistance command. And i believe, DeltaYaw and DeltaPitch can be used to retrieve angles, based on entities positions.


chrisnorris007(Posted 2009) [#18]
oh ok
thanks, will test this ;)


chrisnorris007(Posted 2009) [#19]
like this:

Graphics3D 800,600
SeedRnd MilliSecs()
AppTitle "SIMPLE AI"

;we'll go ahead and create some vital entities
Global camera = CreateCamera()
PositionEntity camera, 0,0,-50
Global sun = CreateLight()

;set up some collisions
Const type_sphere = 1


;create a player entity
Global player = CreateSphere() 	;this will represent the player
EntityColor player, 100,0,0  	;we'll make him red
EntityType player, type_sphere	;declare the entitytype for collisions
PositionEntity player, -5,0,0	;move yourself to the left side of the screen

;now create an enemy 
Global enemy = CreateSphere()	;this will represent the enemy that chases you
EntityColor enemy, 0,100,0		;we'll make the enemy green
EntityType enemy, type_sphere	;declare the entitytype for collisions
PositionEntity enemy, 5,0,0		;move the enemy to the right side of the screen

;declare the collisions
Collisions 1,1,1,2


;---------
;GAME LOOP
;---------
While Not KeyHit(1)

get_input()		;this function gathers input from the user
run_enemy()		;this will run the enemy's AI

RenderWorld 
UpdateWorld

Flip 0

Wend
;---------
;END LOOP
;---------



;------------------
;GET INPUT FUNCTION
;------------------
Function get_input()
If KeyDown(200) Then MoveEntity player, 0,  .005, 0	;up arrow
If KeyDown(203) Then MoveEntity player, -.005,0,  0	;left arrow
If KeyDown(208) Then MoveEntity player, 0, -.005, 0	;down arrow
If KeyDown(205) Then MoveEntity player, .005, 0,  0	;right arrow
End Function 

;-------------------------
;FUNCTION RUN ENEMY
;-------------------------
;AI at it's very basic level is simple.
;Look at the task at hand.  You want the enemy, (the green sphere) to follow you
;How can we accomplish this?  Ask yourself how you would follow something.
;1. First, you have to see it, or be facing towards it.  
;2. Second, you have to walk towards it
;AI is really that simple.  Now, implement those two steps into your program.

;-------------------------
;FUNCTION RUN ENEMY
;-------------------------
Function run_enemy()

If EntityDistance(player,enemy)<10
;Face the enemy towards its target, which is the player
PointEntity enemy, player

;Now move it towards the player
MoveEntity enemy, 0,0,.004
EndIf
;It's THAT easy
End Function 



chrisnorris007(Posted 2009) [#20]
posted mainly for a finishing of this thread...and for info to anyone who scans later looking for the final answer ;)

thanks for your help greatly, Ross C and the original creator of this code, Drak :)

Chris


Drak(Posted 2009) [#21]
Chrisnorris:

Just in case you're wondering, here's the same code, only using TYPES instead of just 1 enemy. This can run many enemies with only a few minor tweaks to the code:

Error copying/pasting this code... it is fixed further down.



chrisnorris007(Posted 2009) [#22]
that is throwing all kinds of errors


chrisnorris007(Posted 2009) [#23]
i fixed it...wasnt hard..just some small errors

great code writing, Drak :)
thanks

Graphics3D 800,600
SeedRnd MilliSecs()
AppTitle "SIMPLE AI USING TYPES"

;we'll go ahead and create some vital entities
Global camera = CreateCamera()
PositionEntity camera, 0,0,-50
Global sun = CreateLight()
Dim emesh(4)
;set up some collisions
Const sphere = 1


;create a player entity
Global player = CreateSphere() 	;this will represent the player
EntityColor player, 100,0,0  	;we'll make him red
EntityType player, sphere	    ;declare the entitytype for collisions
PositionEntity player, -5,0,0	;move yourself to the left side of the screen


Type enemy 
	Field mesh		;"mesh" refers to the physical aspect of this entity, or what you will see
	Field status	;"status" will define the enemy's state, for this we will use 1 and 2
End Type 			;1 will equal stationary, while 2 will mean the enemy will follow you


;now create an enemy 
Global enemy = CreateSphere()	;this will represent the enemy that chases you
TurnEntity enemy, 0,0,0
EntityColor enemy, 0,100,0		;we'll make the enemy green
EntityType enemy, sphere	    ;declare the entitytype for collisions
HideEntity enemy 				;this "sphere" will be used for reference, so we'll hide it as we won't actually use this one

;declare the collisions
Collisions 1,1,1,2

;here we will create some enemies:
For numberofenemies = 1 To 4	;we'll create 4 enemies, this will always be 1 to #, # being the number created
	e.enemy = New enemy			;declare each one to be of the Type enemy, the letter "e" will define this group
	emesh = CopyEntity(enemy)    ;"copyentity" will copy all of the specifics of the enemy entity, which was set earlier
	estatus = 1				;we'll start each enemy in state 1, which will be defined later as basically standing still.
	PositionEntity emesh, Rand(-20,20),Rand(-20,20),0	;position them randomly on the screen
Next

;---------
;GAME LOOP
;---------
While Not KeyHit(1)

getInput()		;this function gathers input from the user
runenemy()		;this will run the enemy's AI

RenderWorld 
UpdateWorld

Flip 0

Wend
;---------
;END LOOP
;---------



;------------------
;GET INPUT FUNCTION
;------------------
Function getInput()
If KeyDown(200) Then MoveEntity player, 0,  .005, 0	;up arrow
If KeyDown(203) Then MoveEntity player, -.005,0,  0	;left arrow
If KeyDown(208) Then MoveEntity player, 0, -.005, 0	;down arrow
If KeyDown(205) Then MoveEntity player, .005, 0,  0	;right arrow
End Function 

;-------------------------
;FUNCTION RUN ENEMY
;-------------------------
Function runenemy()
;now we'll have to run this code for EACH enemy
For e.enemy = Each enemy	;for each enemy....
	
	dist# = EntityDistance(emesh,player)	;get the distance from this enemy to the player and store it in the dist# variable
		
		If dist <= 10		;if the dist is less than or equal to 10...
			estatus = 2	;change the enemy's status to 2, which is follow
		ElseIf dist > 10	;if the dist is greater than 10...
			estatus = 1	;change the enemy's status to 1, which is stationary
		End If 
		
			;Now, we'll check each's status here:
				Select estatus		;select the correct action using the enemy's status number
					Case 1	;this means if estatus = 1
						;we'll actually do nothing, since the enemy is stationary, but case 1 still needs to be called
					Case 2	;if estatus = 2, we want the enemy to follow the player
						PointEntity emesh, player	;point this mesh at the player
						MoveEntity emesh, 0,0,.004	;and finally move the enemy
				End Select
						
Next
End Function 




chrisnorris007(Posted 2009) [#24]
how would you change this to work in 3 dimensions as opposed to 2?


Drak(Posted 2009) [#25]
I'm not sure why some "" were added, so here it is fixed:

Edit: What the?! Why is it deleting ALL the backslash symbols!


chrisnorris007(Posted 2009) [#26]
holy cow i see what you do...its erasing all 's


chrisnorris007(Posted 2009) [#27]
what about the 3rd dimension..how does this work with a character that can move in all three dimensions?


Ross C(Posted 2009) [#28]
Same way mostly, unless your characters can navigate in the 3d dimensions, like a space flight game. Usually you'll have a game in 3d, but the character only moves along the z and x axis. He could maybe jump, but that shouldn't come into it too much, unless it's a platformer, in which case, it would get more complicated.


stayne(Posted 2009) [#29]
.