Here's my code:
Code:
Public Sub GameAI()
Dim i As Long
Dim x As Long
Dim y As Long
Dim n As Long
Dim x1 As Long
Dim y1 As Long
Dim TickCount As Long
Dim Damage As Long
Dim DistanceX As Long
Dim DistanceY As Long
Dim NpcNum As Long
Dim Target As Long
Dim DidWalk As Boolean
For y = 1 To MAX_ROOMS
TickCount = GetTickCount
For x = 1 To MAX_ROOM_NPCS
NpcNum = RoomNpc(y, x).Num
' /////////////////////////////////////////
' // This is used for ATTACKING ON SIGHT //
' /////////////////////////////////////////
' Make sure theres a npc with the map
If NpcNum > 0 Then
' If the npc is a attack on sight, search for a player on the Room
If Npc(NpcNum).Behavior = NPC_BEHAVIOR_ATTACKONSIGHT Or Npc(NpcNum).Behavior = NPC_BEHAVIOR_GUARD Then
For i = 1 To MAX_PLAYERS
If IsPlaying(i) Then
If GetPlayerRoom(i) = y Then
If RoomNpc(y, x).Target = 0 Then
If Npc(NpcNum).Behavior = NPC_BEHAVIOR_ATTACKONSIGHT Or GetPlayerPK(i) = YES Then
If LenB(Trim$(Npc(NpcNum).AttackSay)) Then
PlayerMsg i, "A " & Trim$(Npc(NpcNum).Name) & " says, '" & Trim$(Npc(NpcNum).AttackSay) & "' to you."
End If
RoomNpc(y, x).Target = i
End If
End If
End If
End If
Next i
End If
End If
' /////////////////////////////////////////////
' // This is used for npcs to attack players //
' /////////////////////////////////////////////
' Make sure theres a npc with the Room
If NpcNum > 0 Then
Target = RoomNpc(y, x).Target
' Check if the npc can attack the targeted player player
If Target > 0 Then
' Is the target playing and on the same Room?
If IsPlaying(Target) And GetPlayerRoom(Target) = y Then
' Can the npc attack the player?
If CanNpcAttackPlayer(x, Target) Then
If Not CanPlayerBlockHit(Target) Then
Damage = Npc(NpcNum).STR - GetPlayerProtection(Target)
If Damage > 0 Then
NpcAttackPlayer x, Target, Damage
Else 'NOT DAMAGE...
PlayerMsg Target, "The " & Trim$(Npc(NpcNum).Name) & "'s hit didn't even phase you!"
End If
Else 'NOT NOT...
PlayerMsg Target, "Your " & Trim$(Item(GetPlayerInvItemNum(Target, GetPlayerShieldSlot(Target))).Name) & " blocks the " & Trim$(Npc(NpcNum).Name) & "'s hit!"
End If
End If
Else 'NOT ISPLAYING(TARGET)...
' Player left Room or game, set target to 0
RoomNpc(y, x).Target = 0
End If
End If
End If
' ////////////////////////////////////////////
' // This is used for regenerating NPC's HP //
' ////////////////////////////////////////////
' Check to see if we want to regen some of the npc's hp
With RoomNpc(y, x)
If .Num > 0 Then
If TickCount > GiveNPCHPTimer + 6000 Then
If .HP > 0 Then
.HP = .HP + GetNpcHPRegen(NpcNum)
' Check if they have more then they should and if so just set it to max
If .HP > GetNpcMaxHP(NpcNum) Then
.HP = GetNpcMaxHP(NpcNum)
End If
End If
End If
End If
End With 'RoomNpc(y, x)
' //////////////////////////////////////
' // This is used for spawning an NPC //
' //////////////////////////////////////
' Check if we are supposed to spawn an npc or not
If RoomNpc(y, x).Num = 0 Then
If Room(y).Npc(x) > 0 Then
If TickCount > RoomNpc(y, x).SpawnWait + (Npc(Room(y).Npc(x)).SpawnSecs * 1000) Then
SpawnNpc x, y
End If
End If
End If
Next x
DoEvents
Next y
' Make sure we reset the timer for npc hp regeneration
If GetTickCount > GiveNPCHPTimer + 6000 Then
GiveNPCHPTimer = GetTickCount
End If
' Make sure we reset the timer for door closing
If GetTickCount > KeyTimer + 15000 Then
KeyTimer = GetTickCount
End If
End Sub
[code]Public Function CanNpcAttackPlayer(ByVal RoomNpcNum As Long, _
ByVal Index As Long) As Boolean
Dim RoomNum As Long
Dim NpcNum As Long
CanNpcAttackPlayer = False
' Check for subscript out of range
If RoomNpcNum MAX_ROOM_NPCS Or IsPlaying(Index) = False Then
Exit Function
End If
' Check for subscript out of range
If RoomNpc(GetPlayerRoom(Index), RoomNpcNum).Num