?? full tutorial.htm
字號:
<CENTER><H3><FONT COLOR="#AOAO99">WinBattle - A Multi-player Game Tutorial and Reusable Framework</FONT></H3></CENTER><HR><h3>Introduction</h3><p>This is WinBattle. It's mainly a tutorial in writing a multi-playernetwork game but it also provides re-useable client/server framework codethat can form the basis of subsequent projects. Oh, and it resurrectsa game from the early 90s that I (and a lot of other people) used tofind great fun: XBattle.<h3>The Tutorial Game</h3> <p>It's quite simple. There is a playing board that represents alandscape. Sea is blue, flat land is green and darker shades of brownrepresents higher ground. Each player has bases (shown as circles)that generate troops which are shown as coloured circles. The moretroops present in a hexagonal cell the bigger the circle. Troops canbe made to move across the board by clicking on them to set movementvectors which show up as small lines. Click near the side of thehexagon in the direction you want to send your troops (you must havetroops in the hexagonal cell in order to set the vector).<p>A few minutes into a two-player game might look something like this:<!-- Sample image - gif or jpg --><P><IMG SRC="Full Tutorial_files/image001.jpg" ><p>The idea, of course, is to attack your opponent's troops, capturetheir bases and wipe them off the board.<p>To attack, just point your movement vectors at a cell occupied by theenemy. However, you'll find that you need to attack from more than oneside to successfully occupy a cell. <h3>Origin and Acknowledgments</h3><p>Back in the early 90s real-time multi-player games were pretty muchunheard of. Computers were expensive and the only place you would findthem connected to a network was at work and they would usually berunning UNIX (or some manufacturer's proprietary operatingsystem). Games were available but they were almost exclusively singleplayer.<p>Then Steve Lehar released XBattle on the comp.sources.xnewsgroup. This was a two-player battle simulation that was picked upby Greg Lesher and developed (over a number of years) into amulti-player real-time battle simulation game that was quite unlikeanything seen before. And great fun to play.<p>You can find an excellent description of the game here:<A HREF="http://cns-web.bu.edu/pub/xpip/html/xbattle.html">The XBattle Home Page</A><BR><p>Greg Lesher stopped working on XBattle back in 1996. However, it isstill available for download for UNIX systems on the website (at leastit was when I wrote this). <p>Anyway, one wet weekend I was feeling nostalgic and reminiscing aboutthe old games. XBattle came to mind and a quick trip to Googledelivered me to the XBattle home page. After browsing the site Ithought it would be fun to play it again but I no longer have a UNIXworkstation so off I went in search of a Windows version thinkingsomebody must have done a PC version over the years. I didn't findone. There was a hint of a PC version on the XBattle home page (aprogram called Energy Battle) but that has long since disappeared. So,as I was looking for a hobby project at the time, I thought I'd portXBattle to windows thinking "how hard could it be?".<p>The answer is <i>very</i>. XBattle was written for the UNIX X-Window systemand is so tied into the way that works that getting the original codeworking under Windows would have taken quite a while. Also, theoriginal code is written in C when I prefer C++. So I wrote a Windowsversion from scratch.<p>Although I didn't use any of Greg's original code I wanted to carry onthe spirit of XBattle and release my version as free software. Ane-mail exchange with Greg verified he had no problem with this as longas I changed the name slightly to indicate that it is not an"official" follow-on from XBattle. Hence WinBattle. <p>The demo project is, of course, a very fun game. However, I wanted torelease it as free software so that budding programmers have a(relatively) simple example of a way to build multi-player games. Justgetting programs to talk to each other has always been hard work andWinBattle includes an easy to use mechanism that can be reused inother projects. <h3>Trying Out The Demo</h3><p>You'll find client and server programs available for download(WinBattle_demo.zip). Run the server on a PC and start up to 6 clientson networked PCs with the command:<p> winbattle <i>server computer name</i><p>Where <i>server computer name</i> is the name of the computer where theserver is running.<p> Or create a shortcut that does the same thing (by the way, the defaultserver computer name is <i>localhost</i> so if you want to try it out on justone PC just start the server and one or two clients on that machine toplay). Press the start button on the server and away you go.<p>The server has a small number of options that are available in thedialog that looks like this:<P><IMG SRC="Full Tutorial_files/image004.jpg" ><p>The <i>Explore</i> option means your troops have to scout out the terrainbefore you can see what's there. Turn it off and all players see thewhole board from the start.<p>The <i>Attrition</i> option will cause troops to slowly waste away. Thisprevents the build up of totally massive amounts of troops.<p>The <i>Group Bases</i> option does just that. A player's bases will bepositioned close together when this option is set and randomlyscattered when it isn't.<p>The <i>Disrupt</i> option will cause enemy movement vectors to becancelled when attacked. This allows a small number of troops to wrecksupply lines that have to be constantly repaired.<p>The <i>Hide Enemy Vectors</i> option means you can only see an enemy'smovement vectors if there are troops present in the cell (thisprovides a stealthier start to games as tracks back to your basesaren't obviously visible if you scout with a small number of troops).<p>Finally, you can set the number of bases each player is given.<p>On the client side the commands implemented are:<p> <b>Mouse:</b><p>left mouse button - Set a movement vector adding to any already set<br>right mouse button - Set a movement vector cancelling any others<br>shift + left mouse - March troops automatically across the board<br>Click in centre of cell - Cancel all movement vectors<p> <b>Keyboard:</b><p> a - Attack: position cursor over cell to attack<br>f - Fill-in sea: set one movement vector to point to the sea cell to fill in<br>s - Scuttle a base: position cursor over base to scuttle <p>And that's basically it. If you would like a considerably moredetailed description of how to play I recommend you visit the<A HREF="http://cns-web.bu.edu/pub/xpip/html/xbattle.html">XBattle Home Page</A>and read the first few pages of the tutorial. As far as thebasic operation is concerned XBattle and WinBattle are prettyidentical.<h3>Project Contents</h3>The source code zip file expands to produce the following folders:<p>Client - Contains the client-side specific code<br>Common - Contains utility functions used by the client and server<br>Game - All of the code that is specific to WinBattle is held here<br>Notes - Holds the full tutorial (in HTML format)<br>Server - Contains the server-side specific code<p>The project was built with Visual Studio version 6 although it hasalso been tried with Visual Studio .Net. Should you want to compilethe source code you only need to compile and build the client andserver components. They will automatically pull in what they need fromthe <i>common</i> and <i>game</i> folders. <h3>Tutorial Introduction</h3> <p>This tutorial section talks about the WinBattle client server programsin general (how they start-up, communicate, etc.). There is nothingrelated to the game itself here because the client and server codedoesn't really care which game it's running.<p>What this means is that if you want to write a new game all you haveto do is write the game code. You can re-use the client and servercode pretty much as is. Of course, it isn't <i>quite</i> that simple but moreon this later. <p>Note, although you can read this tutorial in isolation, to get themost from it you really should take a peek at the code in the clientand server folders to get a feel for what's going on. Pretty mucheverything else can be ignored.<p>Anyway, as should be apparent by now, there are two programs used torun the game: a server and a client. The server actually runs thegame, updating the board in real-time and sending the current state toall the clients. The client does very little. It displays the currentstate of play and accepts commands from the player which it justpasses on to the server.<p>The clients and server communicate using sockets. Sockets can betricky to get right so all of the detail is hidden in a <i>Socket</i>class. As far as using it is concerned, it's just like reading andwriting from a file. For example, to send some value from a clientprogram to the server just requires the following line of code in theclient:<pre>server << some_value << "\n";</pre><p>In order to read this value in the server, again, just requires oneline of code:<pre>client >> some_value;</pre><p>You can find more details on using the Socket class later on in thetutorial. For now, we'll concentrate on how the client and serverprograms interact.<h3>Start-up</h3> <p>The server main program can be found in <i>server.cpp</i>. All thisdoes is pop up a dialog box that allows a number of options to be setsuch as the number of bases, whether bases are grouped together,etc. All of the dialog handling can be found in<i>main_dialog.cpp</i>. It's pretty much standard Windows programmingalthough some of the details of the Windows API calls are tucked awayin the class <i>Control</i> for readability.As part of the dialog initialisation, the server starts two threads:<i>game_handler</i> and <i>update_handler</i>. The first threadhandles commands going back and forth between the clients and theserver and the second thread sends game updates to the clients oncethe game is actually started.<p>Actually establishing the network communication is probably thehardest part so let's begin by looking at the <i>game_handler</i>thread, the core of its operation is the following code:<pre>Socket server(server_port);while (true) { server.listen(); for (int i = 0; i < max_players; i++) { if (! clients[i].in_use) { clients[i].connection = server.get_connection(); clients[i].in_use = true; DWORD thread_id (0); CreateThread(0, 0, client_handler, reinterpret_cast<LPVOID>(i), 0, &thread_id); break; } }}</pre><p>The code creates a socket telling it the port number to use. Portnumbers are just a unique number so that many programs can use socketswithout worrying about getting other program's messages. As long asthe clients and servers both use the same number then everythingshould work just fine. The default for the WinBattle client andservers is set at 3333 as there isn't much chance of that number beingin use on most PCs. However, if it is and you get a "port in use"error when you start the server, you can change it to any free port(you'll find that the port number is defined in the <i>common</i>folder in the file<i>universal.h</i>). <p>You can find out which ports are in use on your PC by issuing thefollowing command at a DOS prompt (the port numbers are the ones afterthe ":" in the command output):<pre>netstat -a</pre><p>The thread then calls <i>listen</i>. This function will simply waituntil some client program says it wants to communicate with the serveron port 3333 (this is done from the client with a call to<i>connect</i> in <i>client.cpp</i>). Once a client has connected the<i>listen</i> function returns and the code runs through a table of
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -