Flylib.com

Books Software

 
 
 

MUD Game Programming (Premier Press Game Development) - page 130

[ LiB ]

Summary

Interfacing two completely different languages is an interesting, fun topic. You can do much more with Python and C combined, especially if you get the boost::python library working. The BetterMUD requires only a small subset of all the things that you can possibly do, however.

I hope I've given you a good introduction to Python, and I doubt you'll have much trouble with it, since it's incredibly simple. If you're ever stuck, the Python website has a great tutorial. Or you can look at all the scripts I'm including with the BetterMUD for examples of how things are done.

And now, we're off the next chapter!

[ LiB ]
[ LiB ]

Chapter 18. Making the Game

The previous seven chapters were concerned with coding the C++ core of the BetterMUD. In Chapter 11, I told you about how the C++ core is the physical part of the gamehow the core manages entities, how entities move around, and how entities are saved to disk.

So far, you've got thousands of lines of code, but no game! The problem with designing a system like the BetterMUD is that you can't put it together in bits and piecesat least not at first. Once the physical game core is up and running, however, you can go nuts and start adding scripts left and right.

This chapter is designed primarily to show you how to create basic scripts to make your physical engine into an actual game. I'm not going to go bananas with scripts; in fact the version of the BetterMUD that will be on the CD is going to be just a tiny bit more advanced than the SimpleMUD.

However, with all of the flexibility of the BetterMUD at your fingertips, you can immediately start playing around with scripts.

I would like to invite you again to join my own MUD server at http://dune.net and play around (if I change servers, you can find out about it on my website, http://www.ronpenton.net. I'll be continuously adding scripts and updates, so if you're interested in seeing some of the really cool things that can be done, I'll have my own scripts available to download.

In this chapter, you will learn to create:

  • A login script to manage new characters

  • A base Python script class for your modules to use

  • Command scripts

  • Logic scripts

  • A script to refuse taking dangerous items

  • A script to manage encumbrance

  • A script to manage arming weapons

  • A script to manage currency and merchants

  • A script to perform combat between characters

  • AI scripts for non-player characters

[ LiB ]
[ LiB ]

Login Script

I mentioned in Chapter 16 that the login process accesses a Python script that issued to perform various login tasks , such as printing the races you can choose from and initializing your character whenever you create a new one.

This script is located in the /data/logon/logon.py file.

Listing Available Races

Whenever you want to create a new character, the game's logon module contacts the logon script and asks for a string representing the races your character can participate in.

Here's the function for requesting races:

def listchars():
    s =  "<#FFFFFF>------------------------------------------------\r\n"
    s += "<#00FF00> Please Choose a Race For Your Character:\r\n"
    s += "<#FFFFFF>------------------------------------------------\r\n"
    s += "<$reset> 0 - Go Back\r\n"
    s += "<$reset> 1 - Human\r\n"
    s += "<$reset> 2 - Elf\r\n"
    s += "<#FFFFFF>------------------------------------------------\r\n"
    s += "<#FFFFFF> Enter Choice: <$reset>"
    return s

After the options are printed to the user , he can choose one, and the choice is sent to the logon script again, this time to the following function:

def gettemplateid( option ):
    if option == 1: return 1
    if option == 2: return 2
    return 0

This function takes an option number and translates it into the template ID of the new player's character. Incidentally, I have the database set up right now so that options 1-2 correspond to template IDs 12, but that's a coincidence . Later on, you may want to make an option 3 that creates characters with a template ID of 100 or something like that.

Setting Up the New Character

Once the logon module creates your new character, it sends the ID of that new character into the logon script once morethis time to give the character all the commands he needs, and to put him in the right room. Here's a condensed version of the function:

def setup( id ):
    c = BetterMUD.character( id )
    a = BetterMUD.account( c.GetAccount() )
    l = a.AccessLevel();

    c.SetRoom( 1 )
    c.SetRegion( 1 )
    if( l >= 0 ):
        c.AddCommand( "north" )
... <SNIP> ...
        c.AddCommand( "say" )

    if( l >= 2 ):
        c.AddCommand( "kick" )
        c.AddCommand( "announce" )

    if( l >= 3 ):
        c.AddCommand( "shutdown" )
... <SNIP> ...
        c.AddCommand( "destroyitem" )

The script sets the room and region of the character and then assigns commands based on your account's access level. You can do whatever else you want here. It's up to you. That's the beauty of scripts.

[ LiB ]