I have another one question about a monster AI.
I am creating the AI code of my mummy monster and when it walks in the map and approaches in front of a closed door , the Artificial Intelligence code , checks if there is a door in front of the square of the monster and if the door is closed but not locked , the monster opens the door.
If the door is closed in front of the monster the monster tries to open it and then changes direction , if for example goes NORTH , then I call the random direction code and the monster change direction , and I check again if the random function was selected was the same as current direction and then I random it again until the new direction will be different than the current one. So the monster turns to other direction and continue walking until find another obstacle , wall or closed door or decor or player etc...
My monster according to its direction walk NORTH , SOUTH , WEST , EAST.
My type monsters(party.level,all_monsters).direction variable take values from 1 to 4 for each direction.
So if my monster walking North , I am checking the y axis value on the map minus one.
for all monsters=1 to 100
if monsters(party.level,all_monsters).y-1=doors(party.level,all_monsters).x then do something
Next
If the monster goes NORTH I check the value Y-1
If the monster goes SOUTH I check the value Y+1
If the monster goes WEST I check the value X-1
If the monster goes EAST I check the value X+1
Whole AI monster code is here:
Monster_AI_Functions:
For All_Monsters=1 to 100
If Timer()=>Monsters(party.level,All_Monsters).delay+1700 then Monsters(party.level,All_Monsters).Updater=1:Monsters(party.level,All_Monsters).delay=Timer()
if Timer()=>Monsters(party.level,All_Monsters).frame_delay+400 then Monsters(party.level,All_Monsters).frame_delay=Timer() : Monsters(party.level,All_Monsters).Frame_Updater=1
If Monsters(party.level,All_Monsters).Updater=1
Monsters(party.level,All_Monsters).Updater=0
if Monsters(party.level,All_Monsters).number="01" `My monster is a mummy
if Monsters(party.level,All_Monsters).x>0 and Monsters(party.level,All_Monsters).y>0 `monster is inside the map
MONSTER_AI_NORTH:
if Monsters(party.level,All_Monsters).Direction=1 `NORTH
`If there is a wall in front
if Levels(party.level,Monsters(party.level,All_Monsters).x,Monsters(party.level,All_Monsters).y-1,1)<>"00" `Wall
Monster_Direction1:
Monsters(party.level,All_Monsters).delay=Timer()
Monsters(party.level,All_Monsters).direction=rnd(3)+1 `Tries to turn somewhere
if Monsters(party.level,All_Monsters).direction=1 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction1
Goto AFTER_MONSTER_AI
Endif
`============================
`If there is a close door in front
Monster_Opendoor_North:
if Monsters(party.level,All_Monsters).y-1=Doors(Party.level,All_Monsters).y and Monsters(party.level,All_Monsters).x=Doors(Party.level,All_Monsters).x
if Doors(Party.level,All_Monsters).frame<5 `Closed DOOR only
if Doors(Party.level,All_Monsters).state=5 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction1
if Doors(Party.level,All_Monsters).state=1 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=3 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=4 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
endif
Endif
`============================
`if monster arrived in front of player in any direction then attacks
if Monsters(party.level,All_Monsters).x=party.x and Monsters(party.level,All_Monsters).y-1=party.y
Monsters(party.level,All_Monsters).attack=1
Goto AFTER_MONSTER_AI
`else Monsters(party.level,All_Monsters).attack=0
endif
`======================================================================
`If there is an empty square in front
if Levels(party.level,Monsters(party.level,All_Monsters).x,Monsters(party.level,All_Monsters).y-1,1)="00" `Empty Square
Dec Monsters(party.level,All_Monsters).y,1
Endif
`============================
Endif
MONSTER_AI_SOUTH:
if Monsters(party.level,All_Monsters).Direction=2 `SOUTH
`If there is a wall in front
if Levels(party.level,Monsters(party.level,All_Monsters).x,Monsters(party.level,All_Monsters).y+1,2)<>"00" `Wall
Monster_Direction2:
Monsters(party.level,All_Monsters).delay=Timer()
Monsters(party.level,All_Monsters).direction=rnd(3)+1 `Tries to turn somewhere
if Monsters(party.level,All_Monsters).direction=2 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction2
Goto AFTER_MONSTER_AI
Endif
`============================
`If there is a close door in front
if Monsters(party.level,All_Monsters).y+1=Doors(Party.level,All_Monsters).y and Monsters(party.level,All_Monsters).x=Doors(Party.level,All_Monsters).x
if Doors(Party.level,All_Monsters).frame<5 `Closed DOOR only
if Doors(Party.level,All_Monsters).state=5 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction2
if Doors(Party.level,All_Monsters).state=1 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=3 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=4 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
endif
Endif
`============================
`if monster arrived in front of player in any direction then attacks
if Monsters(party.level,All_Monsters).x=party.x and Monsters(party.level,All_Monsters).y+1=party.y
Monsters(party.level,All_Monsters).attack=1
Goto AFTER_MONSTER_AI
`else Monsters(party.level,All_Monsters).attack=0
endif
`======================================================================
`If there is an empty square in front
if Levels(party.level,Monsters(party.level,All_Monsters).x,Monsters(party.level,All_Monsters).y+1,2)="00" `Empty Square
Inc Monsters(party.level,All_Monsters).y,1
Endif
`============================
Endif
MONSTER_AI_WEST:
if Monsters(party.level,All_Monsters).Direction=3 `WEST
`If there is a wall in front
if Levels(party.level,Monsters(party.level,All_Monsters).x-1,Monsters(party.level,All_Monsters).y,3)<>"00" `Wall
Monster_Direction3:
Monsters(party.level,All_Monsters).delay=Timer()
Monsters(party.level,All_Monsters).direction=rnd(3)+1 `Tries to turn somewhere
if Monsters(party.level,All_Monsters).direction=3 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction3
Goto AFTER_MONSTER_AI
Endif
`============================
`If there is a close door in front
if Monsters(party.level,All_Monsters).y=Doors(Party.level,All_Monsters).y and Monsters(party.level,All_Monsters).x-1=Doors(Party.level,All_Monsters).x
if Doors(Party.level,All_Monsters).frame<5 `Closed DOOR only
if Doors(Party.level,All_Monsters).state=5 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction3
if Doors(Party.level,All_Monsters).state=1 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=3 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=4 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
endif
Endif
`============================
`if monster arrived in front of player in any direction then attacks
if Monsters(party.level,All_Monsters).x-1=party.x and Monsters(party.level,All_Monsters).y=party.y
Monsters(party.level,All_Monsters).attack=1
Goto AFTER_MONSTER_AI
`else Monsters(party.level,All_Monsters).attack=0
endif
`======================================================================
`If there is an empty square in front
if Levels(party.level,Monsters(party.level,All_Monsters).x-1,Monsters(party.level,All_Monsters).y,3)="00" `Empty Square
Dec Monsters(party.level,All_Monsters).x,1
Endif
`============================
Endif
MONSTER_AI_EAST:
if Monsters(party.level,All_Monsters).Direction=4 `EAST
`If there is a wall in front
if Levels(party.level,Monsters(party.level,All_Monsters).x+1,Monsters(party.level,All_Monsters).y,4)<>"00" `Wall
Monster_Direction4:
Monsters(party.level,All_Monsters).delay=Timer()
Monsters(party.level,All_Monsters).direction=rnd(3)+1 `Tries to turn somewhere
if Monsters(party.level,All_Monsters).direction=4 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction4
Goto AFTER_MONSTER_AI
Endif
`============================
`============================
`If there is a close door in front
if Monsters(party.level,All_Monsters).y=Doors(Party.level,All_Monsters).y and Monsters(party.level,All_Monsters).x+1=Doors(Party.level,All_Monsters).x
if Doors(Party.level,All_Monsters).frame<5 `Closed DOOR only
if Doors(Party.level,All_Monsters).state=5 then Monsters(party.level,All_Monsters).delay=Timer():Goto Monster_Direction4
if Doors(Party.level,All_Monsters).state=1 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0 `:Inc Monsters(party.level,All_Monsters).y,1:
if Doors(Party.level,All_Monsters).state=3 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
if Doors(Party.level,All_Monsters).state=4 then Doors(Party.level,All_Monsters).state=3: Monsters(party.level,All_Monsters).Updater=0:Monsters(party.level,All_Monsters).delay=Timer():Goto AFTER_MONSTER_AI
endif
Endif
`============================
`if monster arrived in front of player in any direction then attacks
if Monsters(party.level,All_Monsters).x+1=party.x and Monsters(party.level,All_Monsters).y=party.y
Monsters(party.level,All_Monsters).attack=1
Goto AFTER_MONSTER_AI
`else Monsters(party.level,All_Monsters).attack=0
endif
`======================================================================
`If there is an empty square in front
if Levels(party.level,Monsters(party.level,All_Monsters).x+1,Monsters(party.level,All_Monsters).y,4)="00" `Empty Square
Inc Monsters(party.level,All_Monsters).x,1
Endif
`============================
Endif
Endif
Endif
Endif
Next
AFTER_MONSTER_AI:
For All_Monsters=1 to 100
if Monsters(party.level,All_Monsters).Frame_Updater=1
Monsters(party.level,All_Monsters).Frame_Updater=0
`check what monster I have and play its sound effects.
`===============================================================================
if Monsters(party.level,All_Monsters).number="01" `My monster is a mummy
if Monsters(party.level,All_Monsters).x>0 and Monsters(party.level,All_Monsters).y>0
if Monsters(party.level,All_Monsters).attack=0 and Monsters(party.level,All_Monsters).frame=0
`Mummy monster foot steps sounds
inc Monsters(party.level,All_Monsters).foot_step,1
If Monsters(party.level,All_Monsters).foot_step>2 then Monsters(party.level,All_Monsters).foot_step=0
`If Monsters(party.level,All_Monsters).foot_step=0 then set sound volume Step01a,Monsters(party.level,All_Monsters).sound_volume:set sound pan Step01a,Monsters(party.level,All_Monsters).sound_pan:play sound Step01a,1
If Monsters(party.level,All_Monsters).foot_step=1 then set sound volume M01_step1a,Monsters(party.level,All_Monsters).sound_volume:set sound pan M01_step1a,Monsters(party.level,All_Monsters).sound_pan:play sound M01_step1a,1
If Monsters(party.level,All_Monsters).foot_step=2 then set sound volume M01_step1b,Monsters(party.level,All_Monsters).sound_volume:set sound pan M01_step1b,Monsters(party.level,All_Monsters).sound_pan:Play Sound M01_step1b,1
endif
endif
if Monsters(party.level,All_Monsters).direction=1 `NORTH
if Monsters(party.level,All_Monsters).y-1=party.y
`Here I am checking if my monster "01" attacks
if Monsters(party.level,All_Monsters).attack=1
Monsters(party.level,All_Monsters).walk=0
if Monsters(party.level,All_Monsters).frame=1 then Play Sound M01_attack1a,1
if Monsters(party.level,All_Monsters).frame=0 then Play Sound M01_hit1,1
inc Monsters(party.level,All_Monsters).frame,1
if Monsters(party.level,All_Monsters).frame>2
Monsters(party.level,All_Monsters).frame=0:Monsters(party.level,All_Monsters).frame_delay=Timer():Monsters(party.level,All_Monsters).attack=0
Endif
endif
endif
endif
if Monsters(party.level,All_Monsters).direction=2 `SOUTH
if Monsters(party.level,All_Monsters).y+1=party.y
`Here I am checking if my monster "01" attacks
if Monsters(party.level,All_Monsters).attack=1
Monsters(party.level,All_Monsters).walk=0
if Monsters(party.level,All_Monsters).frame=1 then Play Sound M01_attack1a,1
if Monsters(party.level,All_Monsters).frame=0 then Play Sound M01_hit1,1
inc Monsters(party.level,All_Monsters).frame,1
if Monsters(party.level,All_Monsters).frame>2
Monsters(party.level,All_Monsters).frame=0:Monsters(party.level,All_Monsters).frame_delay=Timer():Monsters(party.level,All_Monsters).attack=0
Endif
endif
endif
Endif
if Monsters(party.level,All_Monsters).direction=3 `WEST
if Monsters(party.level,All_Monsters).x-1=party.x
`Here I am checking if my monster "01" attacks
if Monsters(party.level,All_Monsters).attack=1
Monsters(party.level,All_Monsters).walk=0
if Monsters(party.level,All_Monsters).frame=1 then Play Sound M01_attack1a,1
if Monsters(party.level,All_Monsters).frame=0 then Play Sound M01_hit1,1
inc Monsters(party.level,All_Monsters).frame,1
if Monsters(party.level,All_Monsters).frame>2
Monsters(party.level,All_Monsters).frame=0:Monsters(party.level,All_Monsters).frame_delay=Timer():Monsters(party.level,All_Monsters).attack=0
Endif
endif
endif
Endif
if Monsters(party.level,All_Monsters).direction=4 `EAST
if Monsters(party.level,All_Monsters).x+1=party.x
`Here I am checking if my monster "01" attacks
if Monsters(party.level,All_Monsters).attack=1
Monsters(party.level,All_Monsters).walk=0
if Monsters(party.level,All_Monsters).frame=1 then Play Sound M01_attack1a,1
if Monsters(party.level,All_Monsters).frame=0 then Play Sound M01_hit1,1
inc Monsters(party.level,All_Monsters).frame,1
if Monsters(party.level,All_Monsters).frame>2
Monsters(party.level,All_Monsters).frame=0:Monsters(party.level,All_Monsters).frame_delay=Timer():Monsters(party.level,All_Monsters).attack=0
Endif
endif
endif
Endif
Endif
`===============================================================================
if Monsters(party.level,All_Monsters).attack=0
inc Monsters(party.level,All_Monsters).frame,1
if Monsters(party.level,All_Monsters).frame>2 then Monsters(party.level,All_Monsters).frame=0
endif
Endif
Next
Return
`==================================================================================================
But I have one problem , my monster opens only NORTH and SOUTH doors and ignores my WEST and EAST doors.
I have one file to see my monster and problem in action.
You are in front of a door, the door is closed and locked , the monster comes closer and you see it behind the door , when the monster arrives , tries to open the door , you can avoid it and close the door , it will try to open the door again.
If the monster arrive inside the door and you try to close the door , the the door will start close and re-open , because the mummy blocks the door mechanism , this is a good tip to kill monsters if are between doors (Remember dungeon master).
If you let the mummy pass you , will arrive at the wall and will try to change some direction , so , I have 2 doors in both directions , in my case , the mummy , must open one of these both doors , but ignore them and pass through (BUG) , but my code is correct , why North and South doors work and WEST and EAST doors got ignored by my monster?
Where is my fault?
There is a game demo attached please download an see.
Pick up a key from the alcove from the left or from your inventory.
Unlock the door from the right to release the mummy.
See my Bug