API types

MOB_NONE = 0 MOB_PRIMUS = 1 MOB_COMPANION = 2 MOB_NPC = 3 MOB_OBJECT = 4

DIR_NONE = 0 DIR_NORTH = 1 DIR_EAST = 2 DIR_SOUTH = 3 DIR_WEST = 4

SLOT_NONE = 0 SLOT_HEAD = 1 SLOT_ARMS = 2 SLOT_BODY = 3 SLOT_LEGS = 4 SLOT_INTERACT = 5

class State(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.mobs = {m['id']:Mobile(m) for m in self._dict.get('mobs', [])} self.actions = [Action(a) for a in self._dict.get('actions', [])] self.effects = [Effect(e) for e in self._dict.get('effects', [])] self.tilemap = Tilemap(self._dict.get('tilemap', {})) for m in self.mobs.values: self.tilemap._place(m) self.__dict__.update(kwargs)

def dump(self): for (id, m) in self.mobs.items: m.id = id       return { 'mobs': [m.dump for m in self.mobs.values], 'actions': [a.dump for a in self.actions], 'effects': [e.dump for e in self.effects], 'tilemap': self.tilemap.dump, }

class Tilemap(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.alias = self._dict.get('alias', None) self.west_x = self._dict.get('west_x', 0) self.north_y = self._dict.get('north_y', 0) self.span_x = self._dict.get('span_x', 0) self.span_y = self._dict.get('span_y', 0) self.blocks = self._dict.get('blocks', []) self.beacons = {} self.beacons_by_name = {} self.mobs = {}

for bo in self._dict.get('beacons', []): b = Beacon(bo) beaconsAtCoord = self.beacons.get((b.x, b.y), []) beaconsAtCoord.append(b) self.beacons[(b.x, b.y)] = beaconsAtCoord self.beacons_by_name[b.name] = b

self.__dict__.update(kwargs)

def dump(self): # tilemaps are immutable, we don't spend the CPU cycles to remarshal into Go. return {} def index(self, x, y): ox = x - self.west_x oy = -y + self.north_y if ox < 0 or oy = self.span_x or oy >= self.span_y: return None return ox + oy * self.span_x

def blocked(self, x, y): i = self.index(x, y)       if i is None or i >= len(self.blocks): return True return self.blocks[i]

def _place(self, mob): ms = self.mobs.get((mob.x, mob.y), []) ms.append(mob) self.mobs[(mob.x, mob.y)] = ms

def _placeMobs(self, mobs): self.mobs = {} for mob in mobs.values: self._place(mob) def find(self, x, y, blocking=True): ms = self.mobs.get((x, y), []) if not blocking: return ms       return [m for m in ms if not m.condition.nonblocking]

class Beacon(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.name = self._dict.get('name', None) self.kind = self._dict.get('kind', None) self.x = self._dict.get('x', 0) self.y = self._dict.get('y', 0) self.dx = self._dict.get('dx', 0) self.dy = self._dict.get('dy', 0) self.properties = [Property(p) for p in self._dict.get('properties', [])] self.__dict__.update(kwargs)

def dump(self): return { 'name': self.name, 'kind': self.kind, 'x': self.x,           'y': self.y,            'dx': self.dx, 'dy': self.dy, 'properties': [p.dump for p in self.properties], }

class Property(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.key = self._dict.get('key', None) self.value = self._dict.get('value', None)

def dump(self): return { 'key': self.key, 'value': self.value, }

class Mobile(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.id = self._dict.get('id', None) self.x = self._dict.get('x', 0) self.y = self._dict.get('y', 0) self.px = self._dict.get('px', 0) self.py = self._dict.get('py', 0) self.controller_id = self._dict.get('controller_id', None) self.sprite_asset = self._dict.get('sprite_asset', None) self.type = self._dict.get('type', MOB_NONE) self.attrs = Attributes(self._dict.get('attrs', {})) self.cooldown = self._dict.get('cooldown', 0) self.inventory = [Item(i) for i in self._dict.get('inventory', [])] self.equip_head = Item(self._dict.get('equip_head', {})) self.equip_body = Item(self._dict.get('equip_body', {})) self.equip_arms = Item(self._dict.get('equip_arms', {})) self.equip_legs = Item(self._dict.get('equip_legs', {})) self.condition = Condition(self._dict.get('condition', {})) self.channeled_mana = [Mana(m) for m in self._dict.get('channeled_mana', [])] self.properties = [Property(p) for p in self._dict.get('properties', [])] self.__dict__.update(kwargs)

def dump(self): return { 'id': self.id, 'x': self.x,           'y': self.y,            'px': self.px, 'py': self.py, 'controller_id': self.controller_id, 'sprite_asset': self.sprite_asset, 'type': self.type, 'attrs': self.attrs.dump, 'cooldown': self.cooldown, 'inventory': [i.dump for i in self.inventory], 'equip_head': self.equip_head.dump, 'equip_body': self.equip_body.dump, 'equip_arms': self.equip_arms.dump, 'equip_legs': self.equip_legs.dump, 'condition': self.condition.dump, 'channeled_mana': [m.dump for m in self.channeled_mana], 'properties': [p.dump for p in self.properties], }

class Mana(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.type = self._dict.get('type', None) self.value = self._dict.get('value', 0) self.__dict__.update(kwargs)

def dump(self): return { 'type': self.type, 'value': self.value, }

class Attributes(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.hit_points = self._dict.get('hit_points', 0) self.health = Attribute(self._dict.get('health', {})) self.evasion = Attribute(self._dict.get('evasion', {})) self.accuracies = [Accuracy(a) for a in self._dict.get('accuracies', [])] self.__dict__.update(kwargs)

def dump(self): return { 'hit_points': self.hit_points, 'health': self.health.dump, 'evasion': self.evasion.dump, 'accuracies': [a.dump for a in self.accuracies], }

class Attribute(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.value = self._dict.get('value', 0) self.experience = self._dict.get('experience', 0) self.__dict__.update(kwargs)

def dump(self): return { 'value': self.value, 'experience': self.experience, }

class Accuracy(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.type = self._dict.get('type', None) self.attr = Attribute(self._dict.get('attr', {})) self.__dict__.update(kwargs)

def dump(self): return { 'type': self.type, 'attr': self.attr.dump, }

class Action(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.mob_id = self._dict.get('mob_id', None) self.movement = self._dict.get('movement', DIR_NONE) self.use = self._dict.get('use', 0) self.target_id = self._dict.get('target_id', None) self.command = self._dict.get('command', None) self.__dict__.update(kwargs) def dump(self): return { 'mob_id': self.mob_id, 'movement': self.movement, 'use': self.use, 'target_id': self.target_id, 'command': self.command, }

class Item(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.id = self._dict.get('id', None) self.name = self._dict.get('name', None) self.type = self._dict.get('type', None) self.slot = self._dict.get('slot', 0) self.range = self._dict.get('range', 0) self.damage_min = self._dict.get('damage_min', 0) self.damage_max = self._dict.get('damage_max', 0) self.cooldown = self._dict.get('cooldown', 0) self.active = [Impact(i) for i in self._dict.get('active', [])] self.passive = [Impact(i) for i in self._dict.get('passive', [])] self.__dict__.update(kwargs) def dump(self): return { 'id': self.id, 'name': self.name, 'type': self.type, 'slot': self.slot, 'range': self.range, 'damage_min': self.damage_min, 'damage_max': self.damage_max, 'cooldown': self.cooldown, 'active': [i.dump for i in self.active], 'passive': [i.dump for i in self.passive], }

class Impact(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.type = self._dict.get('type', None) self.value = self._dict.get('value', 0) self.duration = self._dict.get('duration', 0) self.__dict__.update(kwargs) def dump(self): return { 'type': self.type, 'value': self.value, 'duration': self.duration, }

class Condition(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.dead = self._dict.get('dead', False) self.nonblocking = self._dict.get('nonblocking', False) self.shell = self._dict.get('shell', False) self.__dict__.update(kwargs) def dump(self): return { 'dead': self.dead, 'nonblocking': self.nonblocking, 'shell': self.shell, }

class Effect(object):

def __init__(self, _dict={}, **kwargs): self._dict = _dict self.source = self._dict.get('source', None) self.target = self._dict.get('target', None) self.source_x = self._dict.get('source_x', 0) self.source_y = self._dict.get('source_y', 0) self.target_x = self._dict.get('target_x', 0) self.target_y = self._dict.get('target_y', 0) self.type = self._dict.get('type', '') self.value = self._dict.get('value', 0) self.data = self._dict.get('data', None) self.__dict__.update(kwargs) def dump(self): return { 'source': self.source, 'target': self.target, 'source_x': self.source_x, 'source_y': self.source_y, 'target_x': self.target_x, 'target_y': self.target_y, 'type': self.type, 'value': self.value, 'data': self.data, }