def _add_sectors(self, textmap: Textmap): cycle_count = len(textmap.cycles()) # TODO memoization sector_list = [ Block("sector", self._s2blocklist(s)) for s in textmap.sectors ] sector_list *= cycle_count return sector_list
def _add_things(self, textmap: Textmap): # TODO multiplicity things = { t: Block("thing", [ Assignment("x", Decimal("{0:.3f}".format(t.x))), Assignment("y", Decimal("{0:.3f}".format(t.y))), Assignment("type", t.type), ]) for t in textmap.things } return list(things.values()) + self._add_vertices(textmap)
def _add_vertices(self, textmap: Textmap): vertices = { v: Block("vertex", [ Assignment("x", Decimal("{0:.3f}".format(v.x))), Assignment("y", Decimal("{0:.3f}".format(v.y))), ]) for i, v in enumerate( sorted(textmap.vertices, key=lambda e: (e.y, e.x))) } vertex_list = sorted(vertices.items(), key=lambda e: (e[0].y, e[0].x)) vertex_list = [b for _, b in vertex_list] return vertex_list + self._add_linedefs(textmap)
def _to_sidedefs(cls, cycles_sides, linedef_list): cycle_to_sectorid = cls._assign_cycle_to_sectorid( linedef_list, cycles_sides) front_sector_ids = cls._ld2sdid(linedef_list, cycle_to_sectorid) sidefronts = [ Block("sidedef", [ Assignment("sector", sector_id), Assignment("texturemiddle", "MARBFACE"), ]) for sector_id in front_sector_ids ] back_sector_ids = cls._ld2sdid_back(linedef_list, cycle_to_sectorid) sidebacks = [ Block("sidedef", [ Assignment("sector", sector_id), Assignment("texturemiddle", "MARBFACE"), ]) for sector_id in back_sector_ids ] return sidefronts + sidebacks
def _add_linedefs(self, textmap: Textmap): v2id = { v: i for i, v in enumerate( sorted(textmap.vertices, key=lambda e: (e.y, e.x))) } # Add v1, v2 indices linedefs = {(v2id[ld.v1], v2id[ld.v2], ld) for ld in textmap.linedefs} # Sort by v1, v2 indices linedefs = list(sorted(linedefs, key=lambda e: (e[0], e[1]))) # To list of dicts linedefs = [(ld, dict(v1=v1, v2=v2)) for v1, v2, ld in linedefs] # Add sidefront indices for sdid, (ld, dct) in enumerate(linedefs): dct['sidefront'] = sdid # Add sideback indices first_sideback_index = max(dct['sidefront'] for ld, dct in linedefs) + 1 sideback_linedefs = ((ld, dct) for ld, dct in linedefs if ld.sideback) for sdid, (ld, dct) in enumerate(sideback_linedefs, first_sideback_index): dct['sideback'] = sdid linedefs = {(v2id[ld.v1], v2id[ld.v2], Block("linedef", self._to_block(dct, ld, v2id))) for ld, dct in linedefs} linedef_list = [ b for _, _, b in sorted(linedefs, key=lambda e: (e[0], e[1])) ] return linedef_list + self._add_sidedefs(textmap)
def parse_udmf(textmap_string: str): """ translation_unit := global_expr_list global_expr_list := global_expr global_expr_list global_expr := block | assignment_expr block := identifier '{' expr_list '}' expr_list := assignment_expr expr_list assignment_expr := identifier '=' value ';' | nil identifier := [A-Za-z_]+[A-Za-z0-9_]* value := integer | float | quoted_string | keyword integer := [+-]?[1-9]+[0-9]* | 0[0-9]+ | 0x[0-9A-Fa-f]+ float := [+-]?[0-9]+'.'[0-9]*([eE][+-]?[0-9]+)? quoted_string := "([^"\\]*(\\.[^"\\]*)*)" keyword := [^{}();"'\n\t ]+ :param textmap_string: :return: pyparsing instance parsed from @textmap_string """ _plusorminus = Literal('+') | Literal('-') identifier = Word(alphas + '_', alphanums + '_') _uinteger = Word(nums) # TODO hexadecimal, octal integers _integer = Combine(Optional(_plusorminus) + _uinteger) _float = Combine(_integer + Optional(Literal('.') + Optional(_uinteger))) keyword = Word(alphas) # [^{}();"'\n\t ]+ value = _float | QuotedString('"') | keyword assignment_expr = Assignment.group(identifier + '=' + value + ';') expr_list = ZeroOrMore(assignment_expr) block = Block.group(identifier + '{' + expr_list + '}') global_expr = block | assignment_expr global_expr_list = ZeroOrMore(global_expr) translation_unit = TranslationUnit.group(global_expr_list) ast = translation_unit.parseString(textmap_string)[0] return ast
def ast(self) -> TranslationUnit: return TranslationUnit( Assignment("namespace", "zdoom"), Block("thing", [ Assignment("x", Decimal('32.000')), Assignment("y", Decimal('32.000')), Assignment("type", 1), ]), Block("vertex", [ Assignment("x", Decimal('0.000')), Assignment("y", Decimal('0.000')), ]), Block("vertex", [ Assignment("x", Decimal('64.000')), Assignment("y", Decimal('0.000')), ]), Block("vertex", [ Assignment("x", Decimal('128.000')), Assignment("y", Decimal('0.000')), ]), Block("vertex", [ Assignment("x", Decimal('0.000')), Assignment("y", Decimal('64.000')), ]), Block("vertex", [ Assignment("x", Decimal('64.000')), Assignment("y", Decimal('64.000')), ]), Block("vertex", [ Assignment("x", Decimal('128.000')), Assignment("y", Decimal('64.000')), ]), Block("vertex", [ Assignment("x", Decimal('0.000')), Assignment("y", Decimal('128.000')), ]), Block("vertex", [ Assignment("x", Decimal('64.000')), Assignment("y", Decimal('128.000')), ]), Block("vertex", [ Assignment("x", Decimal('128.000')), Assignment("y", Decimal('128.000')), ]), Block("linedef", [ Assignment("v1", 0), Assignment("v2", 3), Assignment("sidefront", 0), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 1), Assignment("v2", 0), Assignment("sidefront", 1), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 2), Assignment("v2", 1), Assignment("sidefront", 2), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 3), Assignment("v2", 4), Assignment("sidefront", 3), Assignment("sideback", 12), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 3), Assignment("v2", 6), Assignment("sidefront", 4), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 4), Assignment("v2", 1), Assignment("sidefront", 5), Assignment("sideback", 13), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 4), Assignment("v2", 5), Assignment("sidefront", 6), Assignment("sideback", 14), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 5), Assignment("v2", 2), Assignment("sidefront", 7), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 6), Assignment("v2", 7), Assignment("sidefront", 8), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 7), Assignment("v2", 4), Assignment("sidefront", 9), Assignment("sideback", 15), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 7), Assignment("v2", 8), Assignment("sidefront", 10), Assignment("blocking", True), ]), Block("linedef", [ Assignment("v1", 8), Assignment("v2", 5), Assignment("sidefront", 11), Assignment("blocking", True), ]), Block("sidedef", [ Assignment("sector", 0), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 0), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 1), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 0), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 2), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 0), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 1), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 1), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 2), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 2), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 3), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 3), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 2), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 1), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 3), Assignment("texturemiddle", "MARBFACE"), ]), Block("sidedef", [ Assignment("sector", 3), Assignment("texturemiddle", "MARBFACE"), ]), Block("sector", [ Assignment("heightceiling", 128), Assignment("textureceiling", "CEIL3_3"), Assignment("texturefloor", "CEIL3_3"), ]), Block("sector", [ Assignment("heightceiling", 128), Assignment("textureceiling", "CEIL3_3"), Assignment("texturefloor", "CEIL3_3"), ]), Block("sector", [ Assignment("heightceiling", 128), Assignment("textureceiling", "CEIL3_3"), Assignment("texturefloor", "CEIL3_3"), ]), Block("sector", [ Assignment("heightceiling", 128), Assignment("textureceiling", "CEIL3_3"), Assignment("texturefloor", "CEIL3_3"), ]), )