Multiplayer Game Programming Check the Web for Updates To check for updates or corrections relevant to this book and/or CD-ROM, visit our updates page on the Web at http://www.prima-tech.com/support Send Us Your Comments To comment on this book or any other PRIMA TECH title, visit our reader response page on the Web at http://www.prima-tech.com/comments How to Order For information on quantity discounts, contact the publisher: Prima Publishing, P.O Box 1260BK, Rocklin, CA 95677-1260; (916) 787-7000 On your letterhead, include information concerning the intended use of the books and the number of books you want to purchase Multiplayer Game Programming Todd Barron A DIVISION OF PRIMA PUBLISHING © 2001 by Prima Publishing All rights reserved No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Prima Publishing, except for the inclusion of brief quotations in a review A Division of Prima Publishing Prima Publishing and colophon are registered trademarks of Prima Communications, Inc PRIMA TECH is a registered trademark of Prima Communications, Inc., Roseville, California 95661 Publisher: Stacy L Hiquet Associate Marketing Manager: Jennifer Breece Managing Editor: Sandy Doell Acquisitions Editor: Emi Smith Project Editor: Cathleen D Snyder Technical Reviewer: Greg Perry Copy Editor: Kris Simmons Interior Layout: Marian Hartsough Associates Cover Design: Prima Design Team CD-ROM Producer: Arlie Hartman Indexer: Katherine Stimson Proofreader: Erin Hartshorn Command & Conquer ©1999 Electronic Arts Inc Command & Conquer is a trademark of Electronic Arts Inc in the U.S and/or other countries All rights reserved Ultima Online Renaissance ©2000 Electronic Arts Inc ORIGIN, the ORIGIN logo, We create worlds, Ultima, the UO logo, Ultima Online Renaissance, Are You With Us? and Britannia are trademarks or registered trademarks of Electronic Arts Inc in the U.S and/or other countries All rights reserved ORIGIN™ is an Electronic Arts™ brand Translation software included under license from Systran Software ©2000 Systran Software, Inc All rights reserved SYSTRAN is a registered trademark of Systran Software, Inc in the United States and in other countries Microsoft, Windows, DirectX, DirectAudio, DirectInput, Direct3D, Internet Explorer, Visual C++ and the DirectX SDK are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries 3D Studio Max is a registered trademark and Discreet is a trademark of Autodesk/Discreet in the USA and/or other countries WinZip is a registed trademark of WinZip Computing, Inc All other trademarks are the property of their respective owners Important: Prima Publishing cannot provide software support Please contact the appropriate software manufacturer’s technical support line or Web site for assistance Prima Publishing and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer Information contained in this book has been obtained by Prima Publishing from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, Prima Publishing, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information Readers should be particularly aware of the fact that the Internet is an ever-changing entity Some facts may have changed since this book went to press ISBN: 0-7615-3298-6 Library of Congress Catalog Card Number: 00-111679 Printed in the United States of America 00 01 02 03 04 DD 10 For the person who has always given me support, guidance, and love: My father Acknowledgments F irst and foremost, I would like to thank my wife Jennie for taking on most of the household duties while I worked on this book Without her support and encouragement, I never would have been able to accomplish the project I would also like to give a big thanks to Prima Publishing for giving me the opportunity to put my thoughts on paper Emi Smith and Cathleen Snyder have been a joy to work with I really appreciate the fact that they have always been prompt to answer my many questions and provide valuable assistance Tech Editor Greg Perry and Copy Editor Kris Simmons also deserve many thanks for their help in editing the book Next, I would like to thank Bob Miller for the in-book cartoons and the 3D robot model in SpacePirates Matt Darr receives my next kudos for helping me debug video initialization routines and providing general testing Of course, I cannot go without thanking everyone on GameDev.net for their help with testing each revision of the in-book games Last, I would like to thank André LaMothe for writing so many books that have contributed to my game programming skills Oh yes, I almost forgot Thanks to my mother for buying me my first computer, a Commodore Vic-20 My father also deserves a lot of credit for my love of computers You see, when I was writing my first programs at age ten, he told me to quit wasting my time with computers, because they were a passing fad Thanks Dad— my inherited rebellious nature paid off About the Author Todd Barron started writing computer games at the age of ten, using a friend’s Timex Sinclair computer In 1990, he deviated from the path of a programmer and started what was to end up as Vector X Studios, a company specializing in commercial 3D animation In 1996, he sold his share of the company and pursued his dream of being a video game programmer During his time in Philadelphia as Game Development Manager at Merit Industries, he wrote multiple video games and worked on their custom networking solution In 1999, Todd and his wife moved back to Arkansas, where he accepted a job at Acxiom, a leading information company Today, Todd works as a Lead Developer at Acxiom, architecting and implementing network solutions for Acxiom’s online products In order to satisfy his game development needs, he operates Lost Logic Inc., a company specializing in multiplayer game development You can reach Todd at webmaster@lostlogic.com Contents at a Glance Introduction xxviii Part One Understanding the Technology Chapter Sup, Wait! It’s the History of Online Gaming! Chapter Understanding the Magic 39 Chapter Introduction to Network Communication 73 Part Two Sockets-Based Network Programming 103 Chapter Writing Your First Network Programs 105 Chapter Creating a Sockets Class Object 149 Chapter Introduction to Windows Programming 207 Chapter Practical Windows Programming 249 Part Three It’s All About DirectX, Baby! 285 Chapter What the Heck Is DirectPlay Anyway? 287 Chapter Chatting with DirectPlay 327 Contents at a Glance Chapter 10 Lions and Clients and Servers, Oh My! 379 Chapter 11 DirectX Graphics 423 Chapter 12 Inside Direct3D Rendering 463 Chapter 13 Now Showing, DirectInput and DirectAudio! 523 Chapter 14 SpacePirates: Your First Complete Game 565 Part Four Developing Online Worlds 641 Chapter 15 MMORPG Design and Implementation 643 Chapter 16 Writing the RPGQuest Game Server 671 Chapter 17 Writing the RPGQuest Game Server 699 Part Five Appendixes 733 Appendix A Game-Development Resources 735 Appendix B Installing the Software 738 Appendix C ASCII Chart 745 Appendix D What’s On the CD 750 Index 752 ix ... one of our goals with the Game Development series is to have all of the books cover game programming concepts and teach the main theme of the book, and Multiplayer Game Programming is no exception... types of games Now, luckily for us, the man who wrote this book, Todd Barron, is just such a person He is a professional game programmer and an expert in networking and multiplayer game programming. .. all glad Multiplayer Game Programming contains information that you will not find anywhere else— period! There are no books, no journals, nothing, nada, zilch about this kind of game programming