Mischiefblog
I make apps for other people

Python

Unpacking sequences in Python when used as function arguments

Posted by Chris Jones
On November 22nd, 2006 at 11:57

Permalink | Trackback | Links In |

Comments Off on Unpacking sequences in Python when used as function arguments
Posted in Python

D’oh. Next time, I’ll just search the docs instead of thrusting blindly forward in an inefficient direction.

See 4.7.4 Unpacking Argument Lists in the Python Tutorial. It just goes to show that even when you think you know a language pretty well, there’s always something new to learn, especially if it’s a language feature you haven’t used before.

Instead of doing this:

values = self.get_field_values()
 
# build the value string
value_str = ''
 
for value in values:
    if len(value_str) > 0:
        value_str += ','
    if isinstance(value, str):
        value = replace("'", "''")
        value_str += "'" + value + "'"
    else:
        value_str += str(value)
 
result = eval("somefunc(" + value_str + ")")

you can simply do this:

somefunc(*values)

and Python will unpack the sequence into an argument list.

Thank goodness OS X is becoming popular

Posted by Chris Jones
On November 22nd, 2006 at 00:13

Permalink | Trackback | Links In |

Comments Off on Thank goodness OS X is becoming popular
Posted in Python, Tech

For the first time, I’ve had no problem finding a good binary distribution of Stackless Python for OS X. It should be everything you need to run it on either PPC or i386 Macs. It might not have all the synchronization controls I’m used to, but I’m willing to learn how to use it to write (slightly) higher performance Python apps.

Encrypting/decrypting messages in Python

Posted by Chris Jones
On November 13th, 2006 at 19:03

Permalink | Trackback | Links In |

Comments Off on Encrypting/decrypting messages in Python
Posted in Design Journal, Python, Tech

A tested implementation follows. Omitted are the unit tests and exception classes.

Scalability not guaranteed.
(more…)

The smart way to write binary structures in Python

Posted by Chris Jones
On November 10th, 2006 at 17:35

Permalink | Trackback | Links In |

Comments Off on The smart way to write binary structures in Python
Posted in Python, Tech

I’ve done some investigation this week and found the smart way (and not so smart way) to write binary structures in Python. Somehow, I’ve glossed over the Python struct library for years–that’s the smart way. The not so smart way is what I did before by writing utilities to pack and unpack Python objects into binary structures and hope that the byte ordering was right.

Of course, once you’ve written your data into a structure, and you’ve figured out how to unpack it, what do you do with it? If you’re writing static handlers for each type of structure, you’re all set: just unpack the structure into a tuple and put the values in the fields in very clear, concise, and obvious code.

My implementation begs to be considered non-obvious. Below is a taste of the code in progress (mostly missing extended exception handling).
(more…)

Accurate client representation of state

Posted by Chris Jones
On November 10th, 2006 at 12:12

Permalink | Trackback | Links In |

Comments Off on Accurate client representation of state
Posted in Design Journal, Python

Given that a client should accurately (as much as possible) represent world state, the client should be written to process events in a consistent, single-handler way. Player input into the client should be turned into game events and passed into the client’s event queue rather than through a separate, privileged channel.

Ideally, player generated events would be sent to the server for verification. Failure to acknowledge and provide permission for actions leads to cheats common in World of Warcraft: teleporting, wall hacks, etc. The client is given broad latitude to perform collision checks and report its location (reducing rubberbanding) at the cost of making it easier to violate movement and location rules. But, as Lum and many others have stated, a balance needs to be struck between client and server load. The server can’t be expected to validate every location and perform collision detection for thousands of objects, but it should be there to provide permission for or acknowledgement of a movement request.

Should collision detection be done by consensus? Network latency would indicate this isn’t an appropriate way to handle this in an MMO. Collision detection is going to have to, unfortunately, remain on the client. However, the server should be able to tell when a client reports its location out of bounds, of the difference in distance or rate of change is outside threshold (without an appropriate cause, such as a teleport).
(more…)

Minor update

Posted by Chris Jones
On June 13th, 2006 at 14:27

Permalink | Trackback | Links In |

Comments Off on Minor update
Posted in General, Java, Pets, Python, Work

I’ve been preoccupied with a few things lately, so I haven’t had much creativity left to spare on the blog, game design, or development. Someday, though, I’ll come back with a vengeance.
(more…)

Beaten by my Mac

Posted by Chris Jones
On June 7th, 2006 at 13:04

Permalink | Trackback | Links In |

Comments Off on Beaten by my Mac
Posted in Java, Python, Tech

I have a little programming project I’m trying to do in C++ instead of C, Java, or Python (so you know I’ll code it slowly). Yesterday morning, I picked up my PowerBook, opened up Eclipse, and started coding from my “specs,” such as they are, to get one of the basic classes completed and tested. Oddly enough, Eclipse didn’t bother compiling anything, and obvious compile errors didn’t get flagged and the build failed. Something must be up with the environment, so I indulged a whim and moved to XCode.

To practice my nascent XCode development skills, I created the project from scratch, reentered the class I was writing, and got about 50% of the way through typing when I got the beach ball of death. I decided that with under 30 minutes of battery left and my wife wanting to talk to me, this would be a good time to plug the laptop back in and let it work out its own issues.

This morning, it still had issues.

I’ve been defeated. (See? Now that was easy!) I’m moving back to C++ development on the PC. My copy of Visual Studio 2005 should be arriving sometime within the next eight weeks and I’ll see if I can recompile there. In the meantime, I’ll use Eclipse on the PC and actually get the thing done.

Event handlers, message recipients, and event strategies

Posted by Chris Jones
On May 25th, 2006 at 08:33

Permalink | Trackback | Links In |

Comments Off on Event handlers, message recipients, and event strategies
Posted in Design Journal, Java, Python, Tech

Event handlers register to receive particular messages. The Java AWT 1.1 event model, for instance, is based on the Observer pattern, with a small wrinkle.
Observer Pattern UML
The small wrinkle is the use of an event class which describes the event in detail (when, where, who, how, and sometimes why) and distinguished the Observer pattern from the Listener pattern. This is a reasonable model for many games to adopt, not just for handling UI events, but also those generated within the game engine or server, and those that arrive over the network.
(more…)

Best way to embed Python?

Posted by Chris Jones
On May 22nd, 2006 at 14:43

Permalink | Trackback | Links In |

Comments (5) |
Posted in Python, Tech

I’ve been thinking about the best way to embed Python as a scripting language. The first approach that comes to my mind is A (image, below), where Python code blocks are invoked from an engine, run some logic, call some native methods, and perform processing. The second approach, B, is where Python is the engine and invokes native compiled functions in order to perform time or computationally critical operations.

Python embedding models

What are some of the advantages or drawbacks to one model or the other? Is Python “good enough” for some operations, like network I/O in a multiplayer game, or should something like that (invoked frequently) instead be done in native code? Should message conversion (from network to in-memory type) be done in native code or in scripting?

Some of these answers may best be found through profiling. That’s the best way to identify critical code, as it distinguishes between what you believe is critical code and what is actually called often and takes up considerable processing time.

Moving or swapping?

Posted by Chris Jones
On May 21st, 2006 at 19:48

Permalink | Trackback | Links In |

Comments (1) |
Posted in General, Python

My mother always called it “moving clothes from the washer to the dryer.”

I call it “swapping.”

In other news:

  • Hat tip to Psychochild for pointing out that Stackless Python is alive and well. I’ll need to reimplement one of my threaded Python message servers to work with Stackless and see if there’s a performance improvement. I’m betting on something neglible, under 10%.
  • DDO is better with two people. Thank goodness for the ten day friend pass. It’s not perfect by any stretch, and even to experienced MMO players, a lot of the interface is non-obvious. EQ2 and Guild Wars were easier to figure out.
  • When joining a guild, the last thing you want to see or hear is, “All these new people are ruining the guild. They’re just joining because they saw us in epic gear and that’s all they want. I don’t want to group or raid with any of these new people.” On the night that you join. When the friend you joined to play with has just left for a week. It’s enough to make you reroll on another server.
  • C# doesn’t look that bad. It might, just might, make a decent client. How well do Irrlicht, DirectSound, and DirectInput work together?