Post subject: Multiple button pushes within one (advance)frame
esthoril
He/Him
Joined: 1/5/2016
Posts: 4
Location: Ghent
Hi all, a long time ago I wrote an AI to play Tetris in Java. Worked really well, often clearing over a million lines at 1000 lines per second (without knowing what the next piece is). I now wanted to port that AI to Lua and hook it up to Bizhawk. Everything seems to work really well except... Once I hit level 29 though (the max speed level for NES Tetris), the speed becomes too high the way I implemented the pushing of the buttons. Example, I need to push A Left Left Left Left Left to put an I tetromino vertically on the far left.
"push one button"
emu.advanceframe
With the example above I need to push 6 buttons. Meaning 6 emu.advanceframes. At lvl 29 this means my tetromino has dropped already way too far to comfortably make the entire move. So question is, how can I do several pushes of the same button before doing an emu.advanceframe and have the game know I pushed Left 5 times? If needed or someone is interested in it, I can put a link to the source code here.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
that depends completely on the game and how often it polls input. i cant imagine the game intentionally set itself up to poll more often than once a frame in order to cater to the most demanding players, its beyond any human's abilities. And even if it is, it isn't possible straightforwardly in bizhawk since it's input is frame-based. However there's a chance, by debugging the game, you can hook the right instructions or IOs in lua and swap out the input right then, but you might need to work with us to make sure the input can actually get refreshed that fast. And we're not going to do that work unless you prove the game can actually take it.
esthoril
He/Him
Joined: 1/5/2016
Posts: 4
Location: Ghent
That's what I was afraid of. Lvl 28 softdrop speed is 1 line per 2 frames. This increases to 1 line per frame at lvl 29. So without "cheating" some way or making sure your algorithm never ever piles up higher than about 8 blocks, you have a problem. Guess the easiest way to circumvent it somehow is to set the correct bytes for x,y positions and rotation of the tetromino after calculating the move with memory.writebyte and then advance the frame. Or a bit cleaner might be to do something like: actually do all the moves but use writebyte to update the y-position of the tetromino with -1 after each sub-move so the tetromino doesn't move down before you complete the entire manouver.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
why don't you just hack down the level 29's speed to level 1 while youre at it? once youre hacking the game, anything goes.
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3573)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
event.oninputpoll might be useful for you. Just note that the game may not care that you are pushing buttons more than once per frame. Also note, that you won't be able to record your results into a .bk2 file this way.
It's hard to look this good. My TAS projects
Masterjun
He/Him
Site Developer, Skilled player (1987)
Joined: 10/12/2010
Posts: 1185
Location: Germany
Just edit your AI so that avoids building too high, and tell him that much higher is really really bad. I've dealt with this problem before. Also you might want to pay attention for when you reach G00 lines...
Warning: Might glitch to credits I will finish this ACE soon as possible (or will I?)
esthoril
He/Him
Joined: 1/5/2016
Posts: 4
Location: Ghent
Masterjun wrote:
Just edit your AI so that avoids building too high, and tell him that much higher is really really bad. I've dealt with this problem before
Well the game over in that video is the exact problem I described :) So how did you deal with it? Maybe training the algorithm using a 10x10 instead of 20x10 playing field as you are sure to go game over if you ever go above height 10 at lvl 29+. Which would be a game over because of movement limitations of the game, not because the algorithm messed up since the AI could move the tetrominos much faster. But.. the algorithm I use also has no knowledge of the next piece and I'd like to keep it that way. In Java it easily clear a million lines in just about 16 minutes running at 40000 FPS. So I'd like to find a workaround without changing the algorithm, as it performs just fine.
Masterjun
He/Him
Site Developer, Skilled player (1987)
Joined: 10/12/2010
Posts: 1185
Location: Germany
esthoril wrote:
But.. the algorithm I use also has no knowledge of the next piece and I'd like to keep it that way. In Java it easily clear a million lines in just about 16 minutes running at 40000 FPS. So I'd like to find a workaround without changing the algorithm, as it performs just fine.
Yeah but if it performs fine in Java Tetris, why would you want to switch to NES Tetris? There is a fundamental problem with your logics there. NES Tetris obviously has other limitations you have to consider if you write an AI. This holds even if this problem doesn't have anything to do with the concept of Tetris and is more of an issue in the game mechanics itself. If you hack the original NES game then you might as well simply use your Java version of the AI. That's the logic error you made right there.
Warning: Might glitch to credits I will finish this ACE soon as possible (or will I?)
esthoril
He/Him
Joined: 1/5/2016
Posts: 4
Location: Ghent
Masterjun wrote:
Yeah but if it performs fine in Java Tetris, why would you want to switch to NES Tetris? There is a fundamental problem with your logics there. NES Tetris obviously has other limitations you have to consider if you write an AI. This holds even if this problem doesn't have anything to do with the concept of Tetris and is more of an issue in the game mechanics itself.
Because I love the old NES as much as I love programming. And always up to learn something new. But yes, the algorithm is written for speed (hence not taking the next piece into account and doing all playfield checks on bitlevel) and not with any limitations another platform might have in mind. Too bad a straight conversion didn't work out. But for now I'd rather try something else rather than writing another Tetris AI. Well it still was a good first exercise to get more familiar with Lua and scripting for Emulators :)