From 14bedb405b363d8f35b910dbb0b3c8e8fa567dec Mon Sep 17 00:00:00 2001 From: Timo Paulssen Date: Mon, 18 May 2009 00:54:14 +0200 Subject: [PATCH] yet another WIP commit. --- lib/gamestate.py | 36 ++++++++++++++++++++++++++++++++++-- lib/stateobjects.py | 8 +++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/gamestate.py b/lib/gamestate.py index f201a97..77f14fa 100644 --- a/lib/gamestate.py +++ b/lib/gamestate.py @@ -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. @@ -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) @@ -393,6 +397,34 @@ def __repr__(self): else: return "" % (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, diff --git a/lib/stateobjects.py b/lib/stateobjects.py index 03fb9bb..bf01c75 100644 --- a/lib/stateobjects.py +++ b/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): @@ -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): @@ -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): @@ -66,3 +70,5 @@ def tick(self, dt): self.lifetimeLeft -= dt if self.lifetimeLeft < 0: self.die = True + +typeKnowledgeBase["in"] = IntrinsicState