New account registration is temporarily disabled.

HEFTY CODING QUESTION: GUARDS DETECTING THIEF

Posts

Pages: 1
Okay, this is going to take me a bit to explain, so bear with me.

I'm working in RM2k3. In my game, battles exist as monsters that you visibly see on whatever map you're on. But instead of simply making the monster an event that constantly runs at the hero, I coded them as parallel processes that create a sort of zone of detection around themselves of five squares. So the enemies don't "see" you until you're within five squares of them, and then they come after you.

The code for this is below:


Variable oper: <herox> Set hero x coord
Variable oper: <heroy> Set hero y coord
Variable oper: <herotempx> Set hero x coord
Variable oper: <herotempy> Set hero x coord
Variable oper: <detectorx> Set this event x coord
Variable oper: <detectory> Set this event y coord
Variable oper: <herotempx> - this events x coord value (subtract the value from this event's coord value)
Variable oper: <herotempy> - this events y coord value
Branch if Var <herox> is less than
Variable oper: <herotempx> X -1 (multiplied by -1)
End
Branch if Var <heroy] is less than
Variable oper: <herotempy> X -1
End
Branch if Var <herotempx> is 5 or less
Branch if Var <herotempy> is 5 or less
Move Event: This event, move toward hero
Wait 0.2 sec
Branch if Var <herotempx> is 1
Branch if Var <herotempy> is 1
Wait 0.1 sec
Enemy Encounter (note: this is where I put in whatever battle group I want)
Victory Handler
Flash Sprite: This event 1.0 sec (wait)
Play Sound: (whatever sound I want of the monster dying)
Change event location: (I move the event to the corner of the map, out of sight, where it can't keep going after the hero)
Escape Handler: This event, wait, wait (this is so if you escape from the battle, you don't end up in the same battle immediately because you're still next to it)


Now, there's a point in the game without battles where you're playing a thief sneaking into a castle's dungeon. You have to get past the guards, and I think I can use this code to make guards that will "see" you if you get too close. The problem is I can't figure out how to code it so they'll only see you if you're in front of them (the code above will detect you if you get too close from any direction).

I want to modify the code so that the guards will only see you if they're facing you, and only if you're within a set number of squares, say ten or so. I also want to be able to make it so they can't see around walls.

Any help would be greatly appreciated.
LockeZ
I'd really like to get rid of LockeZ. His play style is way too unpredictable. He's always like this too. If he ran a country, he'd just kill and imprison people at random until crime stopped.
5958
You can set a variable equal to the direction that this event (the guard, that is) is facing. 1=down, 2=left, 3=right, 4=up. Then you can use conditional branches to make it so that if the direction variable is 1, the guard only comes after you if you're within 10 squares AND the hero's y-coordinate is greater than the guard's y-coordinate. If the direction variable is 2, the guard only only comes after you if you're within 10 squares AND the hero's x-coordinate is less than the guard's x-coordinate. Etc.
Actually, it'd be easier to use the sprite facing conditional branch option instead of devoting a variable to it. What I can't figure out is how to incorporate that into the existing code. I know I need to make it so that it's only checking for the direction the event is facing, as well as how close you are to it. I'm getting closer to figuring that part out. What's really tripping me up is how to make sure they can't see through walls.

There's a lot of math here, and math is not my strong suit.
You could take a look at this:
tutorial including how to make guards not be able to look through walls

The tutorial is still pending, but I think you should still be able to access it.
It includes a tiny rm2k3 project which provides some examples for doing different stuff, with a guard of limited sight which can't look through walls being one of them
For the seeing through walls part, you can use terrains. Set the walls to a certain terrain and branch the terrain during the "in vision" branching. tempx-3, store terrain, branch if terrain = 2, then it's a wall, turn on switch, that turns off the line of sight code.
Kazesui: Thank you very much, that's exactly what I'm trying to do. I've downloaded the file, it should help a lot!

King of Games: The terrains idea is interesting. I will have to play around with it and see what I come up with.

Edit: Wow, there's a lot of math here. Trig was never my favorite subject in school. This is going to take me a while to figure out.
It's well worth getting good at trigonometry though, since there's tons of stuff you can use it for. Math is a good tool to get games do what you want. And you're going to have to look into the terrains idea either way, since the example uses it as well.

The tutorial should help you figure out how it works, but if anything is still unclear, feel free to ask. That way I might be able to improve the tutorial to be even more understandable
I'll take my time and go through it until I get it right. It will be a while before I absolutely need it, so I'm not in a hurry. Thank you again, I'm so going to credit you for this!
Pages: 1