AI behavior

  • 7 Replies
  • 4026 Views
AI behavior
« on: December 16, 2013, 02:09:11 »
Hi, I was just wondering, how certain enemies are programmed? I've noticed that some of the enemies will always fire at the same time when you first enter a screen, but then if you return to the screen at a later time, it changes. It isn't time based because you can wait before entering the screen and it will still be the same. My theory is that there's a universal timer, but that the universal timer for that screen ONLY starts once you have actually entered that screen. Attached is a picture of an enemy that does that. You can pass through on your first pass (if you don't slow down) and it's unlikely to kill you because it only fires when you get to the very end of the screen. Obviously this doesn't apply to enemies that depend on the actions of the player (like those large jumper things that jump when you do).

*

Offline Healy

  • 352
  • 50
    • View Profile
Re: AI behavior
« Reply #1 on: December 16, 2013, 04:38:53 »
Oh yeah, I've been replaying A Strange Dream and I noticed this too.

*

Offline LPChip

  • You can only truly help other people by allowing them to fail.
  • 3510
  • 138
  • Excel at the thing you're the best at!
    • View Profile
    • LPChip Interactive
Re: AI behavior
« Reply #2 on: December 16, 2013, 12:59:25 »
I can't answer this for sure, but I bet it has something to do with how MMF applications deal with random.

Often when you program with random, you work with a seed to get random numbers. The first number is usually always the same number, and I bet this is why the first time its always that way, but not the rest of the times.
on the left, above my avatar.

MODPlug Central Forum
"If I tried to kill you, I'd end up with a big fat hole through my laptop." - Chironex

*

Offline egomassive

  • 1852
  • 250
    • View Profile
    • egomassive games
Re: AI behavior
« Reply #3 on: December 17, 2013, 04:13:04 »
I've worked with the source for Knytt Stories a lot, and I concur with LPChip's suggestion. The inner workings of the random function are hidden from programmers, so I can't give definitive information on how it works, but I can tell you that Nifflas made extensive use of it in KS. Nifflas wrote the program so that the AI of an enemy is contained within it. This is probably why each type of shooting enemy has it's own delay the first time it's encountered; each type has to initiate it's own random number.

As for timing, shooters typically use a timer coupled with a random length of time like, "shoot every 2.7 seconds and 0.1 to 0.8 seconds." This gives you a noticeable pattern with bit of unpredictability. It's possible that, "every 2.7 seconds," means, "every 2.7 seconds since the start of the program," rather than, "every 2.7 seconds since the creation of the object," as a player might expect.
« Last Edit: December 17, 2013, 04:18:18 by egomassive »

Re: AI behavior
« Reply #4 on: December 17, 2013, 09:30:54 »
egomassive, that's actually really interesting. What do you mean by "shoot every 2.7 seconds and 0.1 to 0.8 seconds"? 2.7 plus or minus 0.8 seconds? And do you mean once you stay on the screen? The enemies with patterns have never varied their firing patterns if you don't leave screens, according to what I've observed.

And since you've worked with the source code, can I ask another question? How is momentum factored into the game? I know it's a general question, but it seems to me like Juni moves faster when she's jumping as opposed to when she's merely walking or running. And this has caused me to do some stuff where momentum would be the only explanation for the discrepancy.

*

Offline egomassive

  • 1852
  • 250
    • View Profile
    • egomassive games
Re: AI behavior
« Reply #5 on: December 18, 2013, 06:20:36 »
egomassive, that's actually really interesting. What do you mean by "shoot every 2.7 seconds and 0.1 to 0.8 seconds"
Now that I've thought about it more, I don't think the "Every nth seconds" command is coupled with random numbers. However there are many event which use a frame-rate based counter where events happen after a random number of frames. These are set up like:
If counter <= 0, then set counter to 100 + (a random number between 0 and 20).
If counter == 1, then act.
Subtract 1 from counter.

I couldn't tell you which enemies use which methods or what the actual times/frame counts are, but if the shooters have no variation in their timing, then there probably isn't a random number involved there. However there are still random factors at work in their erratic movements and choosing which one will fire. At least that's how I recall it working.

Quote
How is momentum factored into the game? I know it's a general question, but it seems to me like Juni moves faster when she's jumping as opposed to when she's merely walking or running. And this has caused me to do some stuff where momentum would be the only explanation for the discrepancy.
Juni has momentum, but it's very small. All her movement is controlled by an extension for MMF. Programmers can't see how extensions work (which is incidentally why Nifflas is moving away from programming with MMF,) so I can't say for certain if jumping makes you run faster, but I don't think it does. There are maximum speeds for running, walking, and falling. But, these maximums are not speed limits. There is also a set rate of acceleration, which is very high. It takes almost no time to reach maximum speed or to stop. Theoretically, an enemy could be designed to throw Juni at a much faster speed. You can see this in upward movement. If Juni climbs and jumps she'll gain greater height than by jumping alone. Also, updrafts and spring platforms can accelerate her. In the source, when Juni jumps a speed value is added to her upward velocity (instant acceleration). Then, the gravity value reduces upward velocity until she lands or reaches her max falling speed. It's possible that the extension leaks some of this velocity into the horizontal direction, but doubtful.

edit: As an aside, I'd like to point out that Juni does move faster if you jump/run across the screen rather than only run, however she'll cross the screen in the same amount of time. All those jump arcs add to the distance traveled, so Juni will travel a greater distance in the same amount of time, therefore Juni moves faster.
« Last Edit: December 19, 2013, 01:24:40 by egomassive »

Re: AI behavior
« Reply #6 on: December 18, 2013, 16:51:02 »
I've always sort of justified it by applying real world physics to the game and while recognizing that vertical and horizontal motion have nothing to do with each other, if you jump you impart momentum to yourself. I'm not quite sure how jumping adds extra distance. I actually think, based on observations, that if it's a large distance the difference would be very small (like a long stretch of flat ground) whereas if you get off an object and jump into the next screen (or something) then there's momentum straight away after the jump (although that's pretty hard to justify). In some cases jumping may be preferable because of the geometry of the screen, ie you may get boosts from certain objects. This is all pretty vague, I know.

The movement of the enemies, yeah, that's pretty random and I haven't bothered to check whether that can be determined or not.

There are a lot of anomalies in this game that I don't quite understand and it's definitely not as simple as it looks.

Re: AI behavior
« Reply #7 on: December 21, 2013, 11:29:20 »
Never really thought of it. But now yes I noticed it! It must be really difficult to program something like this. Definitely there must be a Universal time in place there.