Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GameAI using individual timers
#1
Currently, the GameAI sub is triggered using a timer on the server which is set to run every 4 seconds. As some of you know I am creating a text-based mud game. The problem here is that if there are 3 monsters in the same room, all 3 monsters will attack you at the same time every 4 seconds.
What I want to do is add a timer to the individual room NPC (Known as MapNPC() on normal MS engines), which works using GetTickCount. This means that when the NPC dies, the timer is reset for that room NPC until they respawn.
Hopefully, this will stop NPCs from attacking all at the same time, and also allow me to set different attack intervals for different monsters Smile

I'm not familiar with how GetTickCount timers work, but I'm sure I can use it to my advantage if it was to be explained.
Reply
#2
Xlithan Wrote:Currently, the GameAI sub is triggered using a timer on the server which is set to run every 4 seconds. As some of you know I am creating a text-based mud game. The problem here is that if there are 3 monsters in the same room, all 3 monsters will attack you at the same time every 4 seconds.
What I want to do is add a timer to the individual room NPC (Known as MapNPC() on normal MS engines), which works using GetTickCount. This means that when the NPC dies, the timer is reset for that room NPC until they respawn.
Hopefully, this will stop NPCs from attacking all at the same time, and also allow me to set different attack intervals for different monsters Smile

I'm not familiar with how GetTickCount timers work, but I'm sure I can use it to my advantage if it was to be explained.

They shouldn't all be attacking at the same time >_>; Each MapNpc has it's own attack timer.
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply
#3
I'm using Mirage Source 3.0.3
Reply
#4
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)