Network Tetris an Interactive Peer to Peer Approach
Total Page:16
File Type:pdf, Size:1020Kb
Michael Kotovsky Operating Systems Project Comp512, Spring 2008 Penn State Harrisburg Network Tetris An interactive peer to peer approach Table of Contents Introduction & History of Tetris..................................................................................................... 3 Project Goals................................................................................................................................... 4 Design Decisions ............................................................................................................................ 5 Implementation ............................................................................................................................... 6 An Event Driven Game................................................................................................................... 8 The flow of Logic ....................................................................................................................... 8 Description of an Event ............................................................................................................ 10 Lifecycle of an Event................................................................................................................ 10 Rollbacks....................................................................................................................................... 12 Lag & Screen Synchronization ..................................................................................................... 13 The Kotovsky-Barber Lag Decay Heuristic ............................................................................. 14 Data Structures & Concepts.......................................................................................................... 15 Random Numbers ..................................................................................................................... 15 Sockets ...................................................................................................................................... 18 Event Factory............................................................................................................................ 19 Event Queue.............................................................................................................................. 19 Event Manager.......................................................................................................................... 19 Event History ............................................................................................................................ 20 Ack Queue ................................................................................................................................ 20 Game Logic & Threading ............................................................................................................. 21 Closing Thoughts & Future Work ................................................................................................ 23 References..................................................................................................................................... 24 Figures Tetrominoes..................................................................................................................................... 3 Screenshot of user interface............................................................................................................. 7 Game Flow....................................................................................................................................... 8 Event Lifecycle................................................................................................................................ 4 Rollback Example.......................................................................................................................... 12 NRandom....................................................................................................................................... 16 Thread Tree.................................................................................................................................... 22 2 Abstract. Tetris has been a classic staple of video games, dating only a decade older than the era of Pong. This project adds the common twist of multiplayer play, the uncommon ability to actively interfere in what would otherwise be a parallel series of single player games and uniquely accomplishes this in a peer-to-peer environment across a TCP capable network. This multiplatform project emphasizes state consistency in an event driven environment with minimal message passing. Outlined below is a history of Tetris, project goals and the details of a Java implementation of Network Tetris. Introduction & History of Tetris Tetris was originally developed by Alexey Pazhitnov for the Electronica 60 while attending the Moscow Academy of Science in June, 19851, using Pentominos as a basis for his game2. Rights to the game were sold outside of the USSR without prior approval from the game’s developer, creating early ports to the Apple II, Commodore 64 and IBM PC before a contract for the game was official signed a couple of years later. The game’s popularity continues to spread internationally over the years like wildfire. After much legal debate, Nintendo manages to get the rights to the game in 1989, forcing other competitors such as Atari out of the Tetris market – all the while, Pazhitnov receives nothing in the way of royalties, the profits going to the socialist Soviet government. After a few more years of lengthy legal debate, the rights to the game are finally settled, but Pazhitnov still has yet to see royalties for his creation until the disseverment of the USSR and the creation of The Tetris Company, LLC in 19961, now an acquisition of Electronic Arts2. The game play for Tetris is relatively simple. A random sequence of Tetrominoes (a shape composed of four blocks, see Figure 1) appears at the top of a 20 tall by 10 long grid. Theses shapes slowly drift downward until they hit the bottom of the grid or another piece, where they set in place. The user has the ability to move these pieces left, right and down or to rotate the pieces 90 degrees. When an entire row is filled with these Tetrominoes, that line is cleared and everything above it is shifted downward 3 by one row. The game ends when pieces pile up to the top of the grid there is no room to spawn any new pieces. As the game progresses, pieces begin to drop at a faster rate, making survival ultimately impossible. Many versions add their own scoring calculations and other features to enhance this basic concept2. Since it’s inception over twenty years ago, Tetris has been ported to just about every platform available – video game consoles, handhelds, arcade machines and PC releases. It has seen special attention and, thanks to its grid like structure, has even been played using the lights of buildings at Brown University and MIT. Thanks to its simplistic nature, obfuscated code for the game has been written – that would take up less than half this page – to implement the most basic features of the game. A lot of theoretical work has gone into the study of this simple game as well, encouraging the development of advanced AI for not only this game in a traditional sense, but in a one (trivial), three and even N (arbitrary) dimensional space3. While versions of the game mutate the playing field (such as playing on a sphere mesh rather than a simple grid), the more popular adaptations to this classic have typically been the introduction of multiplayer elements. Putting boards side by side and competing for points is often a challenge enough; some variants add the ability for players to actively interfere with the normal game flow of their opponent, be it hindering the flow of their game or altering the state of the board4. More interestingly are the networked versions that run on a computer such as NeTris and TetriNET. These systems allow multiple players across the world to connect take actions that would change the state of their opponent’s board. However, these run on a Client Server model where centralized servers are responsible for arbitrating decisions regarding timing and the player’s game states5. Removing the Client/Server model from the game became the focus of this project. Project Goals The goal for this project was to create a two-player networked version of Tetris, allowing the players to actively clash with each other without the need for a central server to handle decisions. Removing the necessity of a server allows two players, anywhere in the world, to play without a single player having an advantage due to network latency or any other sort of bias. For example: A nearby client connecting to a server one state away is likely to have a significant advantage 4 over someone halfway around the world, simply because the closer connection is likely to have less latency between it and the server. This is only exacerbated if one of the clients is acting as a server, prioritizing their moves ahead of anything else that rolls down the network. In a strictly peer-to-peer model, any action taken would need to be agreed upon in some way by both clients and no action taken would be shown bias over another. It would be simple to somehow package game states and allow clients to arbitrate them as they were sent, but sending an entire board worth of data in a fast paced game such as this would suffer