Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
timo committed May 15, 2009
1 parent d0be0a2 commit eb43fac
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
23 changes: 15 additions & 8 deletions lib/gamestate.py
Expand Up @@ -43,7 +43,8 @@ def __exit__(self, a, b, c):
d = {"statevars": self.so.statevars,
"statevars_format": self.so.statevars_format,
"tuples": self.so.tuples,
"links": self.so.links}
"links": self.so.links,
"len": struct.calcsize(self.so.statevars_format}
serializeKnowledgeBase[type(self.so)] = d
typeKnowledgeBase[self.so.typename] = type(self.so)

Expand Down Expand Up @@ -108,7 +109,7 @@ def spawn(self, object, obvious=False):
self.objects.append(object.bind(self))
if not obvious:
self.spawns.append(object)
print "spawned:", object.__repr__()
print "spawned:", `object`

def registerLink(self, link):
self.links.append(link)
Expand All @@ -124,13 +125,16 @@ def getSerializeType(self, dataFragment):
if type in typeKnowledgeBase:
obj = typeKnowledgeBase[type]()
else:
print "got unknown type:", type.__repr__()
print "got unknown type:", `type`

return obj

def getSerializedLen(self, dataFragment):
if isinstance(dataFragment, str):
return struct.calcsize(self.getSerializeType(dataFragment).statevars_format)
try:
return serializeKnowledgeBase[self.getSerializeType(dataFragment)]["len"]
except IndexError:
return struct.calcsize(self.getSerializeType(dataFragment).statevars_format)
else:
return struct.calcsize(dataFragment.statevars_format)

Expand All @@ -155,7 +159,7 @@ def deserialize(self, data):
try:
obj.deserialize(objdat)
except:
print "could not deserialize", odata.__repr__(), "- chunk:", objdat.__repr__()
print "could not deserialize", `odata`, "- chunk:", `objdat`
raise

self.objects.append(obj)
Expand Down Expand Up @@ -302,12 +306,15 @@ def deserialize(self, data):
try:
vals = struct.unpack(self.statevars_format, data)
except:
print "error while unpacking a", self.typename, self.__repr__()
print "error while unpacking a", self.typename, `self`
raise
for k, v in zip(self.statevars, vals):
setattr(self, k, v)

self.post_deserialize()

self.translateSerializedData()

return self

def command(self, cmd):
Expand Down Expand Up @@ -350,7 +357,7 @@ def __setattr__(self, attr, value):
self.objref.__setattr__(attr, value)

def __repr__(self):
return "<Proxy of (%d): %s>" % (self.proxy_id, self.proxy_objref.__repr__())
return "<Proxy of (%d): %s>" % (self.proxy_id, `self.proxy_objref`)

# the Link class is basically like a StateObjectProxy, but limited to
# one gamestate, rather than the history. it is managed by the GameState.
Expand Down Expand Up @@ -384,7 +391,7 @@ def __repr__(self):
if isinstance(self.proxy_objref, EmptyLinkTarget):
return "<Link to Nothing>"
else:
return "<Link to (%d): %s>" % (self.proxy_id, self.proxy_objref.__repr__())
return "<Link to (%d): %s>" % (self.proxy_id, `self.proxy_objref`)

# the StateHistory object saves a backlog of GameState objects in order to
# interpret input data at the time it happened, even if received with a
Expand Down
16 changes: 0 additions & 16 deletions lib/itemdb.py

This file was deleted.

24 changes: 22 additions & 2 deletions lib/stateobjects.py
@@ -1,6 +1,6 @@
from __future__ import with_statement
from gamestate import StateObject, stateVars, prescribedType, Link
from items import itemDb
from stuffdb import itemDb

class PlayerState(StateObject):
typename = "pc"
Expand Down Expand Up @@ -37,12 +37,32 @@ class ItemState(StateObject):
def __init__(self, statedata = None):
StateObject.__init__(self)
with stateVars(self):
self.itemType = 0
self.type = 0
self.grantType = 0
self.mod = 0

if statedata:
self.deserialize(statedata)
else:
self.translateSerializedData()

def translateSerializedData(self):
itemDb.initItem(self)

class IntrinsicState(StateObject):
typename = "in"
def __init__(self, statedata = None):
StateObject.__init__(self)
with stateVars(self):
self.type = 0
self.lifetimeLeft = -1

if statedata:
self.deserialize(statedata)
else:
self.translateSerializedData()

def tick(self, dt):
self.lifetimeLeft -= dt
if self.lifetimeLeft < 0:
self.die = True
26 changes: 26 additions & 0 deletions lib/stuffdb.py
@@ -0,0 +1,26 @@
class objectdb(object):
def __init__(self):
db = {}

def register(self, cls, typeId):
self.db[typeId] = cls
return cls

def initItem(self, obj):
self.db[obj.type].assimilate(item)

itemDb = objectdb()

class Item(object):
def assimilate(self, other):
other.melee = self.melee
other.thrown = self.thrown
other.thrownDistance =

def melee(self, user):


class Weapon(Item):
def __init__(self):


0 comments on commit eb43fac

Please sign in to comment.