enemy AI which would go through a maze and find the player (Page 2) ● SmileBASIC Source Forums

Register

# enemy AI which would go through a maze and find the player

1 2 3
• #21 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
I would calculate the door to enter from with the Pythagorean Theorem. For a right triangle with sides a, and b and hypotenuse c it states that a^2 + b^2 = c^2. Let x1, y1 be the location the monster was (globally) at when you left them. Then for each door in the current room let x2, y2 be the (global) location of the door. dx = x2 - x1, and dy = y2 - y1 so calculate dx*dx + dy*dy and use the door that has the smallest resulting c^2 value. All that being said, having a monster chasing you from room to room is pretty non-standard game behavior. I can't think of many games that do it, as it is pretty brutal. So I would of course use it very sparingly.
Yes, that could work, but i actually think i have another problem... I'm kinda trying to remake a PC game and the monster screams (a lot lmao) when approaching the player and i want the sound of it screaming get louder the closer the monster gets to the player, just so it to be accurate and actually spice it up a bit. Not to mention there are much more monsters that chase the player through rooms (but they work a little differently) and will probably need at least some simple path finding algorithm. Don't wanna say how exactly they would work because that would spoil a lot (but you can could already tell what am i actually trying to remake by now). Some of you might say that you can't help me if you don't know how the other monsters are supposed to work but it would be totally sufficent if you helped me the first monster we're talking about. By the way thanks very much for all the help i got by now
Posted
• #22 ✎ 136 Vakore You can set the noise of a beep like this: ``` 'Which beeping, pitch, volume(0-128, 64 is default) Beep 5, 200, 84 ``` Posted
• #23 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
You can set the noise of a beep like this: ``` 'Which beeping, pitch, volume(0-128, 64 is default) Beep 5, 200, 84 ```
Yes, i know how to do that. But how would i measure the distance between the player and the monster, if the monster is gonna teleport to the player at some point (without the monster approaching the player)
Posted
• #24 ✎ 136 Vakore Subtract the monster x and y values from the player. Posted
• #25 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
Subtract the monster x and y values from the player.
Wait i can actually do that with the timer + the time it takes for the monster to reach the player in the room... But there's still other monsters, which can't just work by teleporting to the room, where the player is... The A* algorithm looks sufficent but after reading all the explanations i found online (including the one you sent me), i still don't get how to actually code it lol
Posted
• #26 ✎ 1126 random_god Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Second Year My account is over 2 years old Website Forum Leader Hidden Achievements I would a^2+b^2=c^2 it. Have one point of the right triangle be the player and another point be the enemy. Draw the lines to a point (PX,EY or EX,PY PX/PY being player and EX/EY being enemy) and find c. Then reflect that across 0 and add 127 for the volume. I may have described this wrong, so I'll make a test program. K here: K8XJ3WD just focus on how I made the sound get louder as it gets closer. Posted Edited by random_god
• #27 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
I would a^2+b^2=c^2 it. Have one point of the right triangle be the player and another point be the enemy. Draw the lines to a point (PX,EY or EX,PY PX/PY being player and EX/EY being enemy) and find c. Then reflect that across 0 and add 127 for the volume. I may have described this wrong, so I'll make a test program. K here: K8XJ3WD just focus on how I made the sound get louder as it gets closer.
Yes that's a pretty cool program, but the problem is that at some point a timer is gonna run and after that timer the monster is gonna teleport to some door in the player's room (depending on where the monster initially spawned) so that's kinda useless for that. I think i'll rather store every door the player walks in and then make the monster go the same path
Posted
• #28 ✎ 187 seggiepants Any reason you can't keep the whole map in memory and just A* for the monster AI. That might work better. I think the Pythagorean formula actually works better for the distance based volume than it does for the door chooser. I would have a maximum (squared) distance. If the distance is larger than that don't play a scream. Otherwise do a linear interpolation to map distance to volume and use that instead. Zero distance = max volume, maximum distance = minimum volume. Posted
• #29 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
Any reason you can't keep the whole map in memory and just A* for the monster AI. That might work better. I think the Pythagorean formula actually works better for the distance based volume than it does for the door chooser. I would have a maximum (squared) distance. If the distance is larger than that don't play a scream. Otherwise do a linear interpolation to map distance to volume and use that instead. Zero distance = max volume, maximum distance = minimum volume.
...and how do i store the entire map into a variable? Lol
Posted
• #30 ✎ 187 seggiepants How about using a 2 dimensional array DIM map[MAP_HEIGHT, MAP_WIDTH]? You could also use an array of strings DIM map\$[MAP_HEIGHT] where each string in the array is MAP_WIDTH characters long. If you are willing to do a bit of math you could say DIM map[MAP_HEIGHT * MAP_WIDTH] and if you want location x, y it is map[y * MAP_WIDTH + x]. Similarly you could have a string of MAP_HEIGHT * MAP_WIDTH characters and again say MID\$(map\$, y * MAP_WIDTH + x, 1). Of course if you whole map fits in the BGSCREEN's 16383 tile maximum (just under 128x128 tiles) you could just read it from there too. If you have problems with arrays let me know, and I can try to explain. Posted
• #31 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
How about using a 2 dimensional array DIM map[MAP_HEIGHT, MAP_WIDTH]? You could also use an array of strings DIM map\$[MAP_HEIGHT] where each string in the array is MAP_WIDTH characters long. If you are willing to do a bit of math you could say DIM map[MAP_HEIGHT * MAP_WIDTH] and if you want location x, y it is map[y * MAP_WIDTH + x]. Similarly you could have a string of MAP_HEIGHT * MAP_WIDTH characters and again say MID\$(map\$, y * MAP_WIDTH + x, 1). Of course if you whole map fits in the BGSCREEN's 16383 tile maximum (just under 128x128 tiles) you could just read it from there too. If you have problems with arrays let me know, and I can try to explain.
Alright. thanks for your help, i'll have to check the arrays first, but i'll let you know when i get stuck again... Which leads me to another problem/s... I planned to make the doors sprites, but there's a way to place the door as a background tile and then change it according to variables holding the values of it's position when the background is gonna move as the player is gonna move and i don't really know how would make the monster actually pick and enter the right door which would lead him to the player, so idk if sprites or bg tiles would be better for this scenario
Posted
• #32 ✎ 1126 random_god Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Second Year My account is over 2 years old Website Forum Leader Hidden Achievements
You could also use an array of strings DIM map\$[MAP_HEIGHT] where each string in the array is MAP_WIDTH characters long.
This is my favorite way to do this bc. you can apply the DATA instruction and keep all your stuff in the same place. It's also how I learned to make maps with the DATA instruction (idk if there is another way to use DATA for maps lol) And it's really easy to edit without opening other programs to edit all the map data.
Posted Edited by random_god
• #33 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself Yes, that could work... I'll check that out... But how do i actually calculate all these path lenghts? Lol None of the explanations of A* i found online did not tell me how do i calculate all the paths. Posted Edited by ChaseCZ
• #34 ✎ 1126 random_god Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Second Year My account is over 2 years old Website Forum Leader Hidden Achievements Also I kinda understand A* lol And by that I mean I understand Djikstra's pathfinding algorithm. Basically you have your nodes (I would have those be the corners and where the path splits into more than one path) and your edges (I would make them the direct path between two nodes without a wall). You would also have the enemy location be a node. The enemy node would be the source node. Then you need a destination node. That would be where the player is. First I would clear the enemy path array, which I would use as the locations the enemy would go straight to when the path is figured out..Then you calculate the smallest distance between the source node and it's surrounding nodes (at max 4 if you have it as a grid, which is probably the best option). After this, you travel to the closest node, and cross out the last one. I wouldn't move the enemy yet though, we are still trying to figure the whole rout. I would just add this location to your route array we talked about earlier. We would then repeat the stuff with finding the shortest path and adding it to our route, until we reach our destination. Then we would have our heuristic function magic. I don't understand this much yet, but this is what makes Djikstra's method the A* algorithm. What I know about it is kinda background about which paths get you where you are going, like what direction the goal is in, but I'm not completely sure yet. I will be thankful if someone could go into more depth on that, so that I can get my head around it. Posted Edited by random_god
• #35 ✎ 177 ChaseCZ Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself
Also I kinda understand A* lol And by that I mean I understand Djikstra's pathfinding algorithm. Basically you have your nodes (I would have those be the corners and where the path splits into more than one path) and your edges (I would make them the direct path between two nodes without a wall). You would also have the enemy location be a node. The enemy node would be the source node. Then you need a destination node. That would be where the player is. First I would clear the enemy path array, which I would use as the locations the enemy would go straight to when the path is figured out..Then you calculate the smallest distance between the source node and it's surrounding nodes (at max 4 if you have it as a grid, which is probably the best option). After this, you travel to the closest node, and cross out the last one. I wouldn't move the enemy yet though, we are still trying to figure the whole rout. I would just add this location to your route array we talked about earlier. We would then repeat the stuff with finding the shortest path and adding it to our route, until we reach our destination. Then we would have our heuristic function magic. I don't understand this much yet, but this is what makes Djikstra's method the A* algorithm. What I know about it is kinda background about which paths get you where you are going, like what direction the goal is in, but I'm not completely sure yet. I will be thankful if someone could go into more depth on that, so that I can get my head around it.
Thank you very much. I think i'm starting to understand it. And for the nodes, i could use some invisible sprites (the player and enemy nodes would be their own sprites) and i would then calculate the distance between each node to reach the player, right? I doubt this is the only way, but it's just what i thought wouldn't be a bad idea
Posted
• #36 ✎ 1126 random_god Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Second Year My account is over 2 years old Website Forum Leader Hidden Achievements
Also I kinda understand A* lol And by that I mean I understand Djikstra's pathfinding algorithm. Basically you have your nodes (I would have those be the corners and where the path splits into more than one path) and your edges (I would make them the direct path between two nodes without a wall). You would also have the enemy location be a node. The enemy node would be the source node. Then you need a destination node. That would be where the player is. First I would clear the enemy path array, which I would use as the locations the enemy would go straight to when the path is figured out..Then you calculate the smallest distance between the source node and it's surrounding nodes (at max 4 if you have it as a grid, which is probably the best option). After this, you travel to the closest node, and cross out the last one. I wouldn't move the enemy yet though, we are still trying to figure the whole rout. I would just add this location to your route array we talked about earlier. We would then repeat the stuff with finding the shortest path and adding it to our route, until we reach our destination. Then we would have our heuristic function magic. I don't understand this much yet, but this is what makes Djikstra's method the A* algorithm. What I know about it is kinda background about which paths get you where you are going, like what direction the goal is in, but I'm not completely sure yet. I will be thankful if someone could go into more depth on that, so that I can get my head around it.
Thank you very much. I think i'm starting to understand it. And for the nodes, i could use some invisible sprites (the player and enemy nodes would be their own sprites) and i would then calculate the distance between each node to reach the player, right? I doubt this is the only way, but it's just what i thought wouldn't be a bad idea
It could be a good idea, the only problem with this is that you can only have 512 nodes, including the player and enemy. The way I would do it is to have an array containing all of the node's positions, and have a function that calculates which nodes the AI can go directly to. That would be an edge. Either way probably works, just the array is more fit for bigger maps.
Posted Edited by random_god