Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiplayer RTS Tutorial  (Read 5807 times)
0 Members and 1 Guest are viewing this topic.
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Posted 2006-03-19 00:02:53 »

Hello all,

I'm new here. Looks like this is a really good forum to be hanging out at. Anyway, on to my question...
I'm wondering if any of you guys know where I could find a tutorial on how to create a Multiplayer RTS (Real Time Strategy) game in Java. It needs to be played over the Internet, and preferably using Sockets (which I have some expirience with, but I tried to make an RTS but I had problems: lag and out-of-sync problems).

All I really need is how to make a Pong game for example. Once I know how to get everything working without lag and synchronization problems, I can make my own multiplayer game with the network code provided or described in the tutorial.

I've scoured the web for tutorials on this subject, but can't find a single thing on this specific topic. I've only found tutorials on turn-based games (which is how I learn how to use Sockets in Java), and I tried changing the code into a Multiplayer Pong game, but one problem I had was lag then I fixed that and had a problem with synchronization (both the client and server, or host, were both seeing completely differant things).

Your support is greatly appreciated,
Jamison

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline Kova

Senior Member





« Reply #1 - Posted 2006-03-19 01:00:20 »

for start, I apologize for not being able to help you directly
I don't think pong game is quite comparable with RTS. Pong is very simple and predictible, RTSes are much, much more... Don't know why you had sync problems, maybe you shouldn't give up and first finish that. For RTS you need a real server, not just something that responds when message is sent to them like in most tutorials. I suggest you first try to make smaller real time multiplayer game where you have basic elements of RTSes, like sync moving of objects. I'm now working on a extreamly simple soccer game with same reason as you, to learn networking and move on to bigger projects. You're right about lack of tutorials, couldn't find any myself so I'm learning by myself, which is very slow and I spam forum  Wink Hope someone will be able to help you more...
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #2 - Posted 2006-03-19 01:16:54 »

Pong is an RTS. An RTS game is anything that allows both players to move and react simultaneously (at the same time) without waiting for the other player to first make his/her move. Well, I guess for pong, the S should be removed from RTS. Since Pong isn't really a strategy game, so it's really a real-time game than.

I think I can pin point the problem I had. I think it was because I used Thread.sleep() in order to limit the frames per second. But I can't find any code on how to make an FPS limiter that uses the current time.

I don't like you. Check out my site Smiley www.gamedevforums.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2006-03-19 05:38:14 »

I feel like I've talked to you before... Wink

I haven't quite looked through that *entire* code posting in the other thread. perhaps you could strip it down to just the networking so i can understand what you are doing.

how have you structured your network? Does the server constantly send messages to the client? or is the client querying the server? I see you made the switch to NIO.. but still have lag troubles? maybe we should troubleshoot the structure of the networking..
Offline Kova

Senior Member





« Reply #4 - Posted 2006-03-19 11:17:54 »

pong... real time arcade I would say
again sorry for straying from your desired path of finding RTS tutorial...

So you're using thread.sleep(). You need to calculate how much ms you need to sleep. Before game update and render (and anything else) at the start, you get the time. After all of that you again get the time and you sleep(desired_sleep_time - (after_time - before_time)). So if you want to sleep 25 ms every time and your updates and render lasted 5 ms, you'll sleep 20 ms. You have a really detailed explaination and much more complicated formula in "Killer Game Programming", it's free, find it.


Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #5 - Posted 2006-03-19 17:48:20 »

I feel like I've talked to you before... Wink

I haven't quite looked through that *entire* code posting in the other thread. perhaps you could strip it down to just the networking so i can understand what you are doing.

how have you structured your network? Does the server constantly send messages to the client? or is the client querying the server? I see you made the switch to NIO.. but still have lag troubles? maybe we should troubleshoot the structure of the networking..
Haha, yup we have. Anywho, it's kind of hard to strip it down to only my network code as it's kind of scattered around. But I'll try. Oh and both the client and the server (host) are always sending and recieving bytes. Every frame update (33 seconds).

Okay, here's a stripped down version of just my network code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
   .....

   // Sockets
  SocketChannel client;
   ServerSocketChannel server;

   int port = 4789;

   .....

   // ByteBuffer for reading/writing
  ByteBuffer bb;

   .....

   public void run() {
      while (true) {
         // Waiting for an opponent
        if (client == null && server != null && host && !looking) {
            try {
               if (server.socket().isBound()) {
                  client = server.accept();
               }

               if (client != null) {
                  client.configureBlocking(false);
                  client.socket().setTcpNoDelay(false);

                  System.out.println("A client has connected.");
               }
            } catch(IOException e){}
         }

         // Server
        else if (client != null && host && !looking) {
            try {
               // Write
              bb = ByteBuffer.allocate(20);
               bb.putInt(0, paddle1Diry);
               bb.putDouble(4, ballX);
               bb.putDouble(12, ballY);
               client.write(bb);

               // Read
              bb = ByteBuffer.allocate(4);
               client.read(bb);
               paddle2Diry = bb.getInt(0);
            } catch(IOException e) {}
         }

         // Client
        else if (client != null && !looking) {
            try {
               // Write
              bb = ByteBuffer.allocate(4);
               bb.putInt(0, paddle2Diry);
               client.write(bb);

               // Read
              bb = ByteBuffer.allocate(20);
               client.read(bb);
               paddle1Diry = bb.getInt(0);
               ballX = bb.getDouble(4);
               ballY = bb.getDouble(12);
            } catch(IOException e){}
         }

         // Refresh and sleep
        try {
            repaint();
            t.sleep(1000/fps);
         } catch(InterruptedException e){}
      }
   }

   .....

   public void stop() {
      t.stop();
      try {
         /*if (out != null) {
            out.close();
         }
         if (in != null) {
            in.close();
         }*/

         if (client != null) {
            client.close();
         }
         if (server != null) {
            server.close();

            String gameLine = EndGame();
            if (gameLine == "CANT_DELETE") {
               error = "Unable to delete game session.";
            }
         }
         System.out.println("All open streams have been closed.");
      } catch(IOException e){}
   }

   .....

   public void JoinGame(int id) {
      if (player_name.length() > 0) {
         try {
            client = SocketChannel.open(new InetSocketAddress(gameIPs[id], port));
            client.configureBlocking(false);
            client.socket().setTcpNoDelay(false);

            SetupGame();

            System.out.println("Joined game session "+(id+1)+".");
         } catch(IOException e) {
            error = "Unable to join game session "+(id+1)+".";
            System.out.println(error);
            System.out.println(e);
         }
         repaint();
      }
   }

   .....

   public void HostGame() {
      if (player_name.length() > 0) {
         host = true;
         looking = false;
         repaint();

         // Create the server
        try {
            String gameLine = UpdateGame();
            if (gameLine.equals("OKAY")) {
               server = ServerSocketChannel.open();
               server.configureBlocking(false);
               server.socket().bind(new InetSocketAddress(port));

               SetupGame();

               System.out.println("Hosting game on port "+port+" was successful.");
            }
            else if (gameLine.equals("CANT_CREATE")) {
               error = "Unable to create game session. Please try again later.";
            }
            else if (gameLine.equals("CANT_UPDATE")) {
               error = "Unable to update game session. Please try again later.";
            } else {
               error = "An unknown error has occurred. Please try again later.";
            }
         } catch(IOException e) {
            error = "Unable to host game. Port "+port+" may already be in use.";
         }
      }
   }

   .....


pong... real time arcade I would say
again sorry for straying from your desired path of finding RTS tutorial...

So you're using thread.sleep(). You need to calculate how much ms you need to sleep. Before game update and render (and anything else) at the start, you get the time. After all of that you again get the time and you sleep(desired_sleep_time - (after_time - before_time)). So if you want to sleep 25 ms every time and your updates and render lasted 5 ms, you'll sleep 20 ms. You have a really detailed explaination and much more complicated formula in "Killer Game Programming", it's free, find it.



It's okay. As long as I can atleast get this multiplayer pong game working, than I can make other real-time arcade/strategy games.

I've seen and read some of that "Killer Game Programming" book, but I didn't know it had anything on timed steps. I'll have to look and read through it more thoroughly. Thanks!

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline thijs

Junior Member




Lava games rock!


« Reply #6 - Posted 2006-03-19 20:51:17 »

I also feel like I talked with you before: http://www.gamedev.net/community/forums/topic.asp?topic_id=381609 , good to see you made it in these forums Wink

As for your pong game I haven't anything else to comment than I did on your post at gamedev, however for a RTS game this might be of interest for you: http://www.btinternet.com/~duncan.jauncey/old/javagame/

They provide full source for the game as well as a small description on how the different parts are designed.

Also, again the article at gamasutra ( http://www.gamasutra.com/features/20010322/terrano_01.htm ) is an excelent read on this subject.

Thijs

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #7 - Posted 2006-03-19 22:45:45 »

Thanks very much, thijs! I almost forgot about those links you gave me. I will certainly download the source for that game and take a look at that article on AOE's network code. Hopefully that'l help me.

Oh yeah, thijs, that YARTS game doesn't have multiplayer capability. I can create an RTS, so I'm sure that game won't fit my need since I just need to know how to code the network end.

Argh, I've even changed my code (well, a differant test) to use UDP sockets and I'm still having the same problems! thijs, on GameDev.net, you said something about predicting the next move of the paddles and ball. How am I supposed to predict movements?

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline thijs

Junior Member




Lava games rock!


« Reply #8 - Posted 2006-03-20 09:02:05 »

Quote
Oh yeah, thijs, that YARTS game doesn't have multiplayer capability. I can create an RTS, so I'm sure that game won't fit my need since I just need to know how to code the network end.

I do believe YARTS has networking support (maybe that demo applet doesn't):
http://www.btinternet.com/~duncan.jauncey/old/javagame/arch_network.html

Quote
Argh, I've even changed my code (well, a differant test) to use UDP sockets and I'm still having the same problems! thijs, on GameDev.net, you said something about predicting the next move of the paddles and ball. How am I supposed to predict movements?

Contrary to popular believe, UDP ain't faster than TCP (for the transmission time). It has some properties that can be taken advantage of in some games. ( see http://www.java-gaming.org/forums/index.php?topic=608.0 and http://www.java-gaming.org/forums/index.php?topic=12452.0 )

Now you use non-blocking sockets your game is out-of-sync because it goes on. Updates from the other player are always received later, no way around that (network latency). It was perfectly synchronized when you used a blocking socket, because the game wouldn't proceed with the gameloop until it got another network update... but that means the game update rate is determined by the network lag and you really don't want that for a networked game Wink

Predicting motion for the ball is straightforward assuming its just a linear motion (y = ax + c). Based on the previous location you can calculate the pos for this step. For the bats it would be a little more difficult, as their movement is very unpredictable. Try changing the network updates / sec to see what gives a high enough resolution to capture their movement. Then use a technique called linear interpolation (lerp) to smooth their motion between these points (or else you'd see the game run at 30fps fx but the bats run at 10fps assuming 10 network updates / sec).

I can't really tell you all aspects invloved in create a networked game in a forum post, try browsing this forum and see if you see relevant topics, browse gamasutra (they have some nice articles on this), and do a google on "dead reckoning multiplayer" and "interpolation multiplayer".

Thijs

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #9 - Posted 2006-03-20 18:58:11 »

Thanks again for the info, thijs. I'll try googling for those topics.

And by the way, I tried to download the JAR of Y*A*R*T*S and it still doesn't have network play. They must've forgotten that?

I don't like you. Check out my site Smiley www.gamedevforums.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline thijs

Junior Member




Lava games rock!


« Reply #10 - Posted 2006-03-20 21:13:36 »

Quote
And by the way, I tried to download the JAR of Y*A*R*T*S and it still doesn't have network play. They must've forgotten that?

I downloaded the source for YARTS and it clearly has some classes for networking support, dunno if it's enabled in the compiled version...
http://www.btinternet.com/~duncan.jauncey/old/javagame/files/YARTS-sourcecode.zip

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #11 - Posted 2006-03-20 22:34:24 »

Oh yeah. I see now. It does have networking code in the source but they must not have enabled it in the last release.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #12 - Posted 2006-09-11 12:32:57 »

Okay, yes, old topic - I know... But I have FINALLY fixed my lag and out-of-synchronization problem! What I did was used blocking Sockets, and instead of using a PrintWriter that flushed the output stream every print command, I ONLY flushed it after ALL the print commands have been done. Before, I was using multiple println(String x) commands to send data to the client/server, but since it was flushing the data after every command, it waited for each command. But flushing only once, made it wait only once!

Well anyway, I have to go to work now. That's why I haven't been on the forums much lately; too much work and school. See yall later!

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline JAW

Senior Member


Medals: 2



« Reply #13 - Posted 2006-09-13 18:52:41 »

There are 2 nice books for Java Game Programming on Amazon. They can give you a lot of information. Then you need to work yourself up to the ability to make a RTS. RTS is like Pong, players are controlling entities in realtime. But an RTS has a shitload more stuff like AI, Pathfinding, complicated multiplayer communication, etc. I would not say if you can make Pong, you can make Starcraft or something like that. But the basics are similar. Process player input, move entities, process events and collisions, display graphics, play sound, maybe send network data. The RTS has just a little more other stuff.



-JAW
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #14 - Posted 2006-09-14 23:32:24 »

If you read my last post, you would have known that I have already fixed my problem. And btw, a java game programming book wouldn't do much - I already know how to program games in Java. I've been programming games for six years, Java for about a year and a half.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline JAW

Senior Member


Medals: 2



« Reply #15 - Posted 2006-09-17 09:59:27 »

I read some articles about network multiplayer in the book Game Programming Gems 3. There are a lot of other interesting articles. If you are interested, maybe have a look into that book. But I cannot say if it can teach you something you dont yet know.

-JAW
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #16 - Posted 2006-09-17 12:18:39 »

I've had no luck trying to find that book. Could you please provide a link?

I don't like you. Check out my site Smiley www.gamedevforums.com
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!