def parse_mob(text): fields = [line.rstrip() for line in text.strip().split('\n')] d = dict() d['id'] = int(fields[0]) d['aliases'] = fields[1].rstrip('~').split() d['short_desc'] = fields[2].rstrip('~') d['long_desc'] = text.split('~')[2].strip('\n') d['detail_desc'] = text.split('~')[3].strip('\n') tildes = [i for i, a in enumerate(text) if a == '~'] start_bottom_matter = tildes[3] + 1 bottom_fields = text[start_bottom_matter:].strip('\n').split('\n') vector_line = bottom_fields[0] action, affect, alignment, mob_type = vector_line.split() d['mob_type'] = mob_type d['alignment'] = int(alignment) action = clean_bitvector(action) d['flags'] = bitvector_to_flags(action, MOB_ACTION_FLAGS) affect = clean_bitvector(affect) d['affects'] = bitvector_to_flags(affect, MOB_AFFECT_FLAGS) level, thac0, ac, max_hp, bare_hand_dmg = bottom_fields[1].split() gold, xp = bottom_fields[2].split() load_position, default_position, gender = bottom_fields[3].split() d['level'] = int(level) d['thac0'] = int(thac0) d['armor_class'] = int(ac) d['max_hit_points'] = parse_dice_roll_string_to_dict(max_hp) d['bare_hand_damage'] = parse_dice_roll_string_to_dict(bare_hand_dmg) d['gold'] = int(gold) d['xp'] = int(xp) d['position'] = { 'load': lookup_value_to_dict(int(load_position), MOB_POSITION), 'default': lookup_value_to_dict(int(load_position), MOB_POSITION) } d['gender'] = lookup_value_to_dict(int(gender), MOB_GENDER) extra_spec = dict() if len(bottom_fields) > 4: assert mob_type == 'E' assert bottom_fields[-1] == 'E' for line in bottom_fields[4:]: if line == 'E': break # we reached the end of this E-type mob key, value = line.split(': ') extra_spec[key] = int(value) d['extra_spec'] = extra_spec return d
def parse_room(text): parts = text.split('~') vnum, name = parts[0].split('\n') desc = parts[1].strip() zone, flags, sector = parts[2].strip() \ .split('\n')[0].strip().split(' ') d = dict() d['id'] = int(vnum) d['name'] = name.strip() d['desc'] = desc.strip('\n') d['zone_number'] = int(zone) flags = clean_bitvector(flags) d['flags'] = [] if flags: d['flags'] = bitvector_to_flags(flags, ROOM_FLAGS) # sector type flag is always an int d['sector_type'] = lookup_value_to_dict(int(sector), ROOM_SECTOR_TYPES) bottom_matter = '~'.join(parts[2:]) d['exits'] = parse_exits(bottom_matter) d['extra_descs'] = parse_extra_descs(bottom_matter) return d
def parse_object(text): fields = [line.rstrip() for line in text.strip().split('\n')] d = dict() # easy fields d['id'] = int(fields[0]) d['aliases'] = fields[1].rstrip('~').split() d['short_desc'] = fields[2].rstrip('~') d['long_desc'] = fields[3].rstrip('~') d['values'] = [int(v) for v in fields[6].split()] weight, cost, rent, a, b, c = [int(v) for v in fields[7].split()] d['weight'] = weight d['cost'] = cost d['rent'] = rent d['a'] = a d['b'] = b d['c'] = c type_flag, effects_bits, wear_bitvector = fields[5].split() # type flag is always an int d['type'] = lookup_value_to_dict(int(type_flag), OBJECT_TYPE_FLAGS) # parse the bitvectors effects_bits = clean_bitvector(effects_bits) effects = bitvector_to_flags(effects_bits, OBJECT_EXTRA_EFFECTS_FLAGS) d['effects'] = effects wear_bitvector = clean_bitvector(wear_bitvector) d['wear'] = bitvector_to_flags(wear_bitvector, OBJECT_WEAR_FLAGS) action_desc = fields[4].rstrip('~') if action_desc: d['action_desc'] = action_desc d['affects'] = [] d['extra_descs'] = [] if len(fields) > 8: extra_fields = fields[8:] d['affects'] = list(parse_affects(extra_fields)) d['extra_descs'] = list(parse_extra_descs(extra_fields)) return d
def parse_object(text): fields = [line.rstrip() for line in text.strip().split('\n')] d = dict() # easy fields d['id'] = int(fields[0]) d['aliases'] = fields[1].rstrip('~').split() d['short_desc'] = fields[2].rstrip('~') d['long_desc'] = fields[3].rstrip('~') d['values'] = [int(v) for v in fields[6].split()] weight, cost, rent = [int(v) for v in fields[7].split()] d['weight'] = weight d['cost'] = cost d['rent'] = rent type_flag, effects_bits, wear_bitvector = fields[5].split() # type flag is always an int d['type'] = lookup_value_to_dict(int(type_flag), OBJECT_TYPE_FLAGS) # parse the bitvectors effects_bits = clean_bitvector(effects_bits) effects = bitvector_to_flags(effects_bits, OBJECT_EXTRA_EFFECTS_FLAGS) d['effects'] = effects wear_bitvector = clean_bitvector(wear_bitvector) d['wear'] = bitvector_to_flags(wear_bitvector, OBJECT_WEAR_FLAGS) action_desc = fields[4].rstrip('~') if action_desc: d['action_desc'] = action_desc d['affects'] = [] d['extra_descs'] = [] if len(fields) > 8: extra_fields = fields[8:] d['affects'] = list(parse_affects(extra_fields)) d['extra_descs'] = list(parse_extra_descs(extra_fields)) return d
def parse_shop(text): d = dict() fields = [line.rstrip() for line in text.strip().split('\n')] delimiters = [i for i, field in enumerate(fields) if field == '-1'] d['id'] = int(fields[0].lstrip('#').rstrip('~')) objects_start, objects_stop = 1, delimiters[0] d['objects'] = [int(f) for f in fields[objects_start:objects_stop]] d['sell_rate'] = float(fields[objects_stop + 1]) d['buy_rate'] = float(fields[objects_stop + 2]) types_start, types_stop = objects_stop + 3, delimiters[1] buy_types = [buy_type_to_dict(t) for t in fields[types_start:types_stop]] d['buy_types'] = buy_types messages_start, messages_stop = delimiters[1] + 1, delimiters[1] + 8 messages = fields[messages_start:messages_stop] d['messages'] = raw_messages_to_dict(messages) d['temper'] = int(fields[messages_stop]) shop_bitvector = clean_bitvector(fields[messages_stop + 1]) d['flags'] = bitvector_to_flags(shop_bitvector, SHOP_FLAGS) d['shopkeeper'] = int(fields[messages_stop + 2]) trades_with = clean_bitvector(fields[messages_stop + 3]) d['trades_with'] = bitvector_to_flags(trades_with, SHOP_TRADES_WITH) rooms_start, rooms_stop = messages_stop + 4, delimiters[2] d['rooms'] = [int(r) for r in fields[rooms_start:rooms_stop]] times = [int(t) for t in fields[rooms_stop + 1:]] d['times'] = times_to_dict(times) return d
def test_bitvector_to_flags(self): test_flags = { 1: 'GLOWING', 2: 'BUZZING', 4: 'OOZING', 8: 'BUBBLING' } bitvector = 6 expected = [ {"note": "BUZZING", "value": 2}, {"note": "OOZING", "value": 4} ] actual = bitvector_to_flags(bitvector, test_flags) self.assertListEqual(actual, expected)