Mirage Source http://miragesource.net/forums/ |
|
GameAI using individual timers http://miragesource.net/forums/viewtopic.php?f=184&t=6257 |
Page 1 of 55 |
Author: | Xlithan [ Thu Oct 01, 2009 12:42 am ] |
Post subject: | GameAI using individual timers |
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 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. |
Author: | Robin [ Thu Oct 01, 2009 12:46 am ] |
Post subject: | Re: GameAI using individual timers |
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 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. |
Author: | Xlithan [ Thu Oct 01, 2009 1:12 am ] |
Post subject: | Re: GameAI using individual timers |
I'm using Mirage Source 3.0.3 |
Author: | Xlithan [ Thu Oct 01, 2009 1:19 am ] |
Post subject: | Re: GameAI using individual timers |
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 <= 0 Or 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 <= 0 Then Exit Function End If RoomNum = GetPlayerRoom(Index) NpcNum = RoomNpc(RoomNum, RoomNpcNum).Num ' Make sure the npc isn't already dead If RoomNpc(RoomNum, RoomNpcNum).HP <= 0 Then Exit Function End If ' Make sure npcs dont attack more then once a second If GetTickCount < RoomNpc(RoomNum, RoomNpcNum).AttackTimer + 1000 Then Exit Function End If ' Make sure we dont attack the player if they are switching maps 'If Player(Index).GettingMap = YES Then ' Exit Function 'End If RoomNpc(RoomNum, RoomNpcNum).AttackTimer = GetTickCount ' Make sure they are on the same map If IsPlaying(Index) Then If NpcNum > 0 Then ' Check if at same coordinates If GetPlayerRoom(Index) = RoomNum Then CanNpcAttackPlayer = True Else 'NOT (GETPLAYERY(INDEX)... Exit Function End If End If End If End Function |
Author: | wanai [ Fri Jan 21, 2022 5:41 am ] |
Post subject: | Re: GameAI using individual timers |
MPEG |
Author: | wanai [ Fri Jan 21, 2022 5:42 am ] |
Post subject: | Re: GameAI using individual timers |
156.8 |
Author: | wanai [ Fri Jan 21, 2022 5:43 am ] |
Post subject: | Re: GameAI using individual timers |
Bett |
Author: | wanai [ Fri Jan 21, 2022 5:44 am ] |
Post subject: | Re: GameAI using individual timers |
Bett |
Author: | wanai [ Fri Jan 21, 2022 5:45 am ] |
Post subject: | Re: GameAI using individual timers |
Fion |
Author: | wanai [ Fri Jan 21, 2022 5:46 am ] |
Post subject: | Re: GameAI using individual timers |
Eras |
Author: | wanai [ Fri Jan 21, 2022 5:48 am ] |
Post subject: | Re: GameAI using individual timers |
Remi |
Author: | wanai [ Fri Jan 21, 2022 5:49 am ] |
Post subject: | Re: GameAI using individual timers |
Mika |
Author: | wanai [ Fri Jan 21, 2022 5:50 am ] |
Post subject: | Re: GameAI using individual timers |
Ivan |
Author: | wanai [ Fri Jan 21, 2022 5:51 am ] |
Post subject: | Re: GameAI using individual timers |
Chac |
Author: | wanai [ Fri Jan 21, 2022 5:52 am ] |
Post subject: | Re: GameAI using individual timers |
Widt |
Author: | wanai [ Fri Jan 21, 2022 5:53 am ] |
Post subject: | Re: GameAI using individual timers |
Rond |
Author: | wanai [ Fri Jan 21, 2022 5:54 am ] |
Post subject: | Re: GameAI using individual timers |
Chri |
Author: | wanai [ Fri Jan 21, 2022 5:55 am ] |
Post subject: | Re: GameAI using individual timers |
Atla |
Author: | wanai [ Fri Jan 21, 2022 5:56 am ] |
Post subject: | Re: GameAI using individual timers |
Alai |
Author: | wanai [ Fri Jan 21, 2022 5:58 am ] |
Post subject: | Re: GameAI using individual timers |
Orie |
Author: | wanai [ Fri Jan 21, 2022 5:59 am ] |
Post subject: | Re: GameAI using individual timers |
Kath |
Author: | wanai [ Fri Jan 21, 2022 6:00 am ] |
Post subject: | Re: GameAI using individual timers |
Bren |
Author: | wanai [ Fri Jan 21, 2022 6:01 am ] |
Post subject: | Re: GameAI using individual timers |
Stou |
Author: | wanai [ Fri Jan 21, 2022 6:02 am ] |
Post subject: | Re: GameAI using individual timers |
Erne |
Page 1 of 55 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |