Skip to content

Commit

Permalink
yet another WIP commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
timo committed May 17, 2009
1 parent eb43fac commit 14bedb4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
36 changes: 34 additions & 2 deletions lib/gamestate.py
Expand Up @@ -46,7 +46,6 @@ def __exit__(self, a, b, c):
"links": self.so.links,
"len": struct.calcsize(self.so.statevars_format}
serializeKnowledgeBase[type(self.so)] = d
typeKnowledgeBase[self.so.typename] = type(self.so)

# using this object with the "with" statement, the type of the included vars
# can be predetermined, instead of letting the magic find it out.
Expand Down Expand Up @@ -152,7 +151,12 @@ def deserialize(self, data):
data = data[2:]

# cut the next N bytes out of the data.
objlen = struct.calcsize(obj.statevars_format)
if isinstance(obj, LinkList):
# the first integer of the thingie is the number of items in the list,
# so we add 4 bytes for the first integer and 4 for each following one.
objlen = struct.unpack("!2i", data[:8])[1] * 4 + 4
else:
objlen = self.getSerializedLen(data)
objdat, data = data[:objlen], data[objlen:]

obj.bind(self)
Expand Down Expand Up @@ -393,6 +397,34 @@ def __repr__(self):
else:
return "<Link to (%d): %s>" % (self.proxy_id, `self.proxy_objref`)

class LinkList(StateObject):
typename = "ll"
def __init__(self, statedata):
self.links = []
self.work = []
self.state = None
self.id = 0

if statedata:
self.deserialize(statedata)

def serialize(self):
pass

def deserialize(self, data):
self.id, amount = struct.unpack("!2i", data[:8])
while data:
chunk, data = data[:4], data[4:]
self.work.append(lambda: self.links.append(Link(self.state.getById(struct.unpack("!i", chunk)))))

if self.state:
for wo in self.work:
wo()
self.work = []

def pre_serialize(self): pass
def post_deserialize(self): pass

# 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
# latency. this allows for fair treatment of all players, except for cheaters,
Expand Down
8 changes: 7 additions & 1 deletion lib/stateobjects.py
@@ -1,5 +1,5 @@
from __future__ import with_statement
from gamestate import StateObject, stateVars, prescribedType, Link
from gamestate import StateObject, stateVars, prescribedType, Link, typeKnowledgeBase
from stuffdb import itemDb

class PlayerState(StateObject):
Expand Down Expand Up @@ -32,6 +32,8 @@ def collide(self, other, vec):
def command(self, cmd):
pass

typeKnowledgeBase["pc"] = PlayerState

class ItemState(StateObject):
typename = "it"
def __init__(self, statedata = None):
Expand All @@ -49,6 +51,8 @@ def __init__(self, statedata = None):
def translateSerializedData(self):
itemDb.initItem(self)

typeKnowledgeBase["it"] = ItemState

class IntrinsicState(StateObject):
typename = "in"
def __init__(self, statedata = None):
Expand All @@ -66,3 +70,5 @@ def tick(self, dt):
self.lifetimeLeft -= dt
if self.lifetimeLeft < 0:
self.die = True

typeKnowledgeBase["in"] = IntrinsicState

0 comments on commit 14bedb4

Please sign in to comment.