Basic AI for Games

NoMoreSecrets
02-28-2002, 12:57 PM
Hi guys thake a look at my code
it's only a snapshot of my Enemy.Class please
tell me what u think about it

It's only a basic state machine in wich is possible to include
some advanced pathfindind functions:
Public Function Brain(ByRef wld As WorldMap) As String

Dim s As String
Dim rndLng As Long


' What is the current status?
Select Case myStatus

' Search for a valid path
'--NOT IMPLEMENTED------
Case Status.FindingPath


Case Status.Moving
' Let's move
Call MoveMe(wld)
s = "Moving"

' Shoot to enemy or figth them
Case Status.ShootingPlayer
'Call DoShot
s = "Shooting"

' Search for a random direction
Case Status.wait

rndLng = Int(104 * Rnd) + 1

Select Case rndLng
Case Is <= 13
Direction = Directions.MELEFT
Case Is <= 26
Direction = Directions.MEUP
Case Is <= 39
Direction = Directions.MERIGHT
Case Is <= 52
Direction = Directions.MEDOWN
Case Is <= 65
Direction = Directions.MEDOWNRIGHT
Case Is <= 79
Direction = Directions.MEDOWNLEFT
Case Is <= 92
Direction = Directions.MEUPLEFT
Case Is <= 105
Direction = Directions.MEUPRIGHT

End Select
' change status so next time we start to move
myStatus = Status.Moving
s = "Thinking............."

'Sleep to recover my stamina
Case Status.Sleeping
If mvarStamina <= mvarMaxStamina Then
mvarStamina = mvarStamina + StaminaIncrease
Else
' if stamina is at max go in WAIT status to
' search for a random direction
myStatus = Status.wait
End If
s = "I'm spleeping to recover energy"
End Select


Brain = s

End Function

Private Function MoveMe(ByRef wld As WorldMap) As String
Dim rndLng As Long

'We can move only once every 50 mls
If GetTickCount() - lasttime < 50 Then
Exit Function
End If

'----Randomly change status with probability of 3/100
rndLng = Int(Rnd * 100) + 1
If rndLng > 97 Then
myStatus = Status.wait
Exit Function
End If

'Store Old position

Old_pos_X = pos_X
Old_pos_Y = pos_Y

'Change posistion according to direction stepping by velocity
Select Case Direction

Case Directions.MEUP
pos_Y = pos_Y - velocity
Case Directions.MEDOWN
pos_Y = pos_Y + velocity
Case Directions.MELEFT
pos_X = pos_X - velocity
Case Directions.MERIGHT
pos_X = pos_X + velocity
Case Directions.MEUPLEFT
pos_X = pos_X - velocity
pos_Y = pos_Y - velocity
Case Directions.MEUPRIGHT
pos_X = pos_X + velocity
pos_Y = pos_Y - velocity
Case Directions.MEDOWNLEFT
pos_X = pos_X - velocity
pos_Y = pos_Y + velocity

Case Directions.MEDOWNRIGHT
pos_X = pos_X + velocity
pos_Y = pos_Y + velocity

End Select

' Update cell Position (cell_x is a property where cell_X=pos_x/tilewidth)
Cell_Y = pos_Y
Cell_X = pos_X

' Let me know the cell coord
frmScreen.Label1 = "Cella attiva (X,Y): " & Cell_X & "<->" & Cell_Y

' If cell is not walkable then revert to old position and move in
' WAIT status to random search for another direction
If wld.GetCells(Cell_X, Cell_Y) <> CellType.Walkable Then
myStatus = Status.wait
pos_Y = Old_pos_Y
pos_X = Old_pos_X
Cell_Y = Old_pos_Y
Cell_X = Old_pos_X

End If
' Decrease my stamina every time i move
mvarStamina = mvarStamina - StaminaDecrease
' if stamina is 0 then move to SLEEPING status
If mvarStamina <= 0 Then myStatus = Status.Sleeping
' Update time
lasttime = GetTickCount()



End Function


byez
Lorenzo

NoMoreSecrets
02-28-2002, 01:31 PM
'---------------CODE IN MODULE.BAS---------------------------------
' General Variables and enums


Public Enum Status
Moving
FindingPath
ShootingPlayer
wait
Sleeping
End Enum

Public Const CELL_WIDTH = 32
Public Const CELL_HEIGHT = 32

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Enum Directions
MEUP = 1
MELEFT
MERIGHT
MEDOWN
MEDOWNLEFT
MEDOWNRIGHT
MEUPLEFT
MEUPRIGHT
End Enum

Public Declare Function GetTickCount Lib "kernel32" () As Long

Public Const MAX_CELLSY = 40
Public Const MAX_CELLSX = 60

Public Enum CellType
Walkable
NotWalkable
Teleporter
End Enum

Public Const StaminaIncrease = 0.1
Public Const StaminaDecrease = 0.5

Isno
02-28-2002, 04:35 PM
edit your messages using and tags

Teric
02-28-2002, 04:39 PM
It appears that he already has, Isno.

Squirm
02-28-2002, 05:24 PM
NoMoreSecrets it might be easier to evaluate and comment on your code if we could see it in context. If you could post the whole project then we could get some idea of how this is all related. :D

NoMoreSecrets
03-01-2002, 07:39 AM
Hi guys this is the code,
i've managed it a little bit to give u a small working application
just to see how code work.

Please help me to improve the code and let me know what u think about it.

i've commented it a bit but i'm ready to answer to any question.

The code is simple but not optimized at all and i've done it just to do some research on NPC AI before implementing it in my game.
However i think could be a good starting point for people the want to write their own game.

Code is VB6

Byez
Lorenzo
---------------------------------------------------
Dumb is who dumb does sir

Computer_Guy
03-01-2002, 05:46 PM
Okay, I've downloaded your game, and I'm getting an error that says...

Invalid Outside Procedure


Now, it comes when I hit start, it highlights the variable with the problem, telling me that Multiuse= -1 is the problem.

Now I noticed that this error comes in a class module, and I'm not familiar at all with Class Modules, just regular ones, and don't know how to use them, so I really don't want to mess with anything, but has anyone downloaded this and know what is going wrong?

Thanks
Ed

Squirm
03-01-2002, 05:49 PM
Let me guess, you're using VB5, yes?

Just delete everything before the End statement. :D

Computer_Guy
03-01-2002, 08:59 PM
Well, kind of, I'm using the Control Creation Edition, but could someone tell me why I must delete them? Are they not compatible with VB5 and lower?

NoMoreSecrets
03-04-2002, 02:48 AM
Computer_Guy the code is vb6 and as i know vb cce only support a subset of vb5 language and so i'm going to include a compiled executable .. just rename the mypac.dat to mypac.exe

however tell me what is the part of the code that u don't understand and i'll try to explain you.


This is the first update, it's only a small upadate
i've implemented a small LineOfSigth routine using
Pitagora Theorem.

Basically the barain() routine now check also for enemy_proxymity
using a circular line of sigth with radius R

in the new app now there is another circle around the first
one.
It rapresent the LOS of the creature, u can set the radius in the start button click sub



so take a look at the code and please help me to imprve the AI
HEELPPPPPPPPPPPPPPPPPPPPPPP!
..

tnx
Lorenzo

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum