def run(do_print): f = open("runtime.txt", "w") print("Run dijkstra ...") # dijkstra t0 = time.clock() for i in range(n): dijkstra(i, Matrix, Neighbors, do_print=do_print) td = time.clock() - t0 print("Run bf ...") # bf t0 = time.clock() for i in range(n): bf(i, Matrix, Neighbors, do_print=do_print) tf = time.clock() - t0 print("Run fw ...") # fw t0 = time.clock() fw(Matrix, Neighbors, do_print=do_print) tw = time.clock() - t0 # print td = td / (n * n) tf = tf / (n * n) tw = tw / (n * n) f.write("Dijkstra: " + str(td) + " seconds\n") f.write("Bellman-Ford: " + str(tf) + " seconds\n") f.write("Floyd-Warshall: " + str(tw) + " seconds") f.close()
def test_unbalanced(self): expected = "unbalanced brackets" actual_a = bf("[[++]]]") actual_b = bf("[[[++]]") assert expected == actual_a assert expected == actual_b
def test_unbalanced(self): expected = 'unbalanced brackets' actual_a = bf('[[++]]]') actual_b = bf('[[[++]]') assert expected == actual_a assert expected == actual_b
def __str__(self): octet = bf() octet[4:8] = 6 octet[0:4] = (self.traffic_cls & 0xf0) >> 4 output = chr(octet) octet = bf() octet[4:8] = self.traffic_cls & 0xf octet[0:4] = (self.flow_lbl & 0xf0000) >> 16 output = output + chr(octet) + ns_str(self.flow_lbl) output = output + ns_str(self.payload_len) output = output + chr(self.nexthdr) + chr(self.hop_limit) output = output + self.saddr output = output + self.daddr return output
def test_hello(self): expected = 'Hello world!' actual = bf( '--[>--->->->++>-<<<<<-------]>--.>---------.>--..+++.>---' '-.>+++++++++.<<.+++.------.<-.>>+.' ) assert expected == actual
def single_match_test(): tests = [ ("ABCDABD", "BBCABCDABABCDABCDABDEABCDABD"), # [13,21] ("aa", "aaaaaaaa"), # [0,1,2,4,5,6] ("AAACAAAA", "AAACAAADAAACAAAsAAACAAAAAAACAAAA") # [16,24] ] for (p, s) in tests: print(p, s) start = time.time() print(bf(s, p)) print((time.time() - start) * 1000.)
def __str__(self): octet = bf() octet[6:8] = 2 octet[5] = self.pad octet[4] = self.ext if len(self.csrc) > 16: raise Exception('CSRC count is %d. Exceeds 16' % len(self.csrc)) octet[0:4] = len(self.csrc) output = chr(octet) octet = bf() octet[7] = self.mark octet[0:7] = self.pt output += chr(octet) + ns_str(self.sn) output += nl_str(self.ts) + nl_str(self.ssrc) for csrc in self.csrc: output += nl_str(u32_validator(csrc)) return output
def __init__(self): self.packet_index = 0 self.control = FRCControlByte() self.ds_digital_in = bf(0xff) self.team_id = 0 self.alliance = 'R' self.position = '1' self.sticks = [JoystickState() for i in range(4)] self.analogs = [0] * 4 self.crio_checksum = 0 self.fpga_checksum = [0] * 4 self.ds_version = '10020800'
def __init__(self): self.control = FRCControlByte() self.battery_int = 0x37 self.battery_frac = 0x37 self.ds_digital_out = bf() self.mac_addr = (0x00, 0x80, 0x2f, 0x11, 0x4d, 0xac) self.fpga_version = '06300800' self.last_packet_index = None self.user_update_number = 0 self.user_data_high = bytes() self.errors = '' self.user_data_low = bytes() self.ds_lcd_data = bytes()
def __str__(self): octet = bf() octet[4:8] = 4 octet[0:4] = self.ihl output = chr(octet) + chr(self.tos) output = output + ns_str(self.tot_len) + ns_str(self.id) octet = bf() if self.df: octet[6] = 1 if self.mf: octet[5] = 1 octet[0:5] = (self.frag_off & 0x1f00) >> 8 output = output + chr(octet) + nc_str(self.frag_off) output = output + chr(self.ttl) + chr(self.protocol) output = output + ns_str(self.checksum) output = output + self.saddr + self.daddr #FIXME option and padding #checksum computation if self.checksum == 0: hdr_len = len(output) csum = 0 i = 0 while hdr_len != 0: csum = csum + (ord((output[i])) << 8) + ord(output[i + 1]) i = i + 2 hdr_len = hdr_len - 2 while (csum >> 16) != 0: csum = (csum & 0xffff) + (csum >> 16) csum = ~csum output = output[:10] + ns_str(csum) + output[12:] return output
def test_too_much_output(self): expected = 'a' * 430 actual = bf('+' * ord('a') + '[.]') assert expected == actual
def extract(filename): h3m_data = gzip.open(filename) r = lambda s: struct.unpack(s,h3m_data.read(struct.calcsize(s))) E = objectify.ElementMaker(annotate=False) try: vver = h3m_versions[r("<I")[0]] except KeyError: raise ValueError("Invalid game version or not a HoMM 3 map") if vver == "WoG": raise NotImplementedError("WoG maps not supported yet") (vhas_players, vsize, vunderground) = r("<BIB"); if vunderground not in [0,1]: raise ValueError("Underground specifier not in [0,1]") vunderground = str(bool(vunderground)) m = E.map(version=vver,size=str(vsize),underground=vunderground) m.append(E.conversion_metadata()) (vlength, ) = r("<I") if vlength>30: raise ValueError("Map name longer than 30 characters, possibly an invalid file") vname = h3m_data.read(vlength) enc = edet(vname) vname = vname.decode(enc["encoding"]) m.append(E.name(vname)) m.conversion_metadata.append(E.encoding(enc["encoding"],entity="/map/name[0]",confidence=str(enc["confidence"]))) (vlength, ) = r("<I") vdesc = h3m_data.read(vlength) enc = edet(vdesc) vdesc = vdesc.decode(enc["encoding"]) m.append(E.description(vdesc)) m.conversion_metadata.append(E.encoding(enc["encoding"],entity="/map/description[0]",confidence=str(enc["confidence"]))) try: vdifficulty = h3m_diffictulties[r("<B")[0]] except KeyError: raise ValueError("Invalid map difficulty value") m.append(E.difficulty(vdifficulty)) if m.xpath("/map/@version")[0] in ["AB", "SoD", "WoG"]: (vmax_level,) = r("<B") if vmax_level==0: vmax_level = "Unspecified" else: vmax_level = "Unspecified" m.append(E.max_level(vmax_level)) m.append(E.players()) #player info for pnum in h3m_player_colors: (vis_human, vis_computer) = r("<BB") if vis_human not in [0,1] or vis_computer not in [0,1]: raise ValueError("Invalid player type specifiers %#04x and %#04x" % (vis_human, vis_computer)) if vis_human == 0 and vis_computer == 0: #player is disabled, skipping garbage if m.xpath("/map/@version")[0] == "RoE": h3m_data.read(6) elif m.xpath("/map/@version")[0] == "AB": h3m_data.read(12) elif m.xpath("/map/@version")[0] in ["SoD", "WoG"]: h3m_data.read(13) m.players.append(E.player(color=h3m_player_colors[pnum],type="Disabled")) continue if vis_human == 1 and vis_computer == 1: player_type = "Both" elif vis_human == 1: player_type = "Human" else: player_type = "Computer" player = E.player(color=h3m_player_colors[pnum],type=player_type) try: vai_type = h3m_ai_types[r("<B")[0]] except KeyError: raise ValueError("Invalid AI type") player.append(E.ai(vai_type)) if m.xpath("/map/@version")[0] in ["SoD", "WoG"]: (venforce_alignment,) = r("<B") if venforce_alignment not in [0,1]: raise ValueError("Invalid alignment enforcement specifier %#04x" % venforce_alignment) venforce_alignment = str(bool(venforce_alignment)) else: venforce_alignment = "Unspecified" valignments = bf(r("<B")[0]) if m.xpath("/map/@version")[0] in ["AB", "SoD", "WoG"]: (vconflux,) = r("<B") if vconflux not in [0,1]: raise ValueError("Invalid Conflux specifier %#04x" % vconflux) if vconflux == 1: valignments[8] = 1 (vrandom_alignment,) = r("<B") if vrandom_alignment not in [0,1]: raise ValueError("Invalid random alignment specifier %#04x" % vrandom_alignment) if vrandom_alignment == 1: valignments[9] = 1 alignments = set() for i in range(0,10): if valignments[i] == 1: alignments.add(h3m_alignments[i+1]) player.append(E.alignment(', '.join(alignments),enforced=venforce_alignment)) (vmain_town,) = r("<B") if vmain_town not in [0,1]: raise ValueError("Invalid main town specifier %#04x" % vmain_town) if vmain_town: if m.xpath("/map/@version")[0] in ["AB", "SoD", "WoG"]: (vmain_town_generate_hero, vmain_town_unknown1) = r("<BB") vmain_town_unknown1 = ("%#04x" % vmain_town_unknown1) m.conversion_metadata.append(E.unknown1(vmain_town_unknown1,player_color=h3m_player_colors[pnum])) if vmain_town_generate_hero not in [0,1]: raise ValueError("Invalid main town hero generation specifier %#04x" % vmain_town_generate_hero) vmain_town_generate_hero = str(bool(vmain_town_generate_hero)) else: vmain_town_generate_hero = "Unspecified" vmain_town_unknown1 = "Unspecified" vcoords = r("<BBB") player.append(E.main_town("%d,%d,%d" % vcoords, generate_hero=vmain_town_generate_hero, unknown1=vmain_town_unknown1)) (vgenerate_random_hero, vmain_hero) = r("<BB"); if vgenerate_random_hero not in [0,1]: raise ValueError("Invalid random hero generation specifier %#04x" % vgenerate_random_hero) vgenerate_random_hero = str(bool(vgenerate_random_hero)) player.append(E.generate_random_hero(vgenerate_random_hero)) if vmain_hero != 0xFF: (vmain_hero_portrait, ) = r("<B") if vmain_hero_portrait == 0xFF: vmain_hero_portrait = "Standard" else: vmain_hero_portrait = str(vmain_hero_portrait) (vlength, ) = r("<I") vmain_hero_name = h3m_data.read(vlength) player.append(E.main_hero(str(vmain_hero),portrait=vmain_hero_portrait,name=vmain_hero_name)) if m.xpath("/map/@version")[0] in ["AB", "SoD", "WoG"]: (vhero_placeholders,vheroes_count) = r("<BI") player.append(E.hero_placeholders(vhero_placeholders)); if vheroes_count > 0: player.append(E.heroes()) for i in range(vheroes_count): (vhero_portrait, ) = r("<B") if vhero_portrait == 0xFF: vhero_portrait = "Standard" else: vhero_portrait = str(vhero_portrait) (vlength, ) = r("<I") vhero_name = h3m_data.read(vlength) player.heroes.append(E.hero(portrait=vhero_portrait,name=vhero_name)) m.players.append(player) #special victory condition (vvictory_condition,) = r("<B") try: vvictory_condition = h3m_victory_conditions[vvictory_condition] except KeyError: raise ValueError("Invalid special victory condition %#04x" % vvictory_condition) if vvictory_condition != "None": (vvictory_allow_normal, vvictory_apply_to_computer) = r("<BB") if vvictory_allow_normal not in [0,1]: raise ValueError("Invalid normal victory specifier %#04x" % vvictory_allow_normal) if vvictory_apply_to_computer not in [0,1]: raise ValueError("Invalid special victory for computer specifier %#04x" % vvictory_apply_to_computer) vvictory_allow_normal = str(bool(vvictory_allow_normal)) vvictory_apply_to_computer = str(bool(vvictory_apply_to_computer)) m.append(E.special_victory_condition(type=vvictory_condition, allow_normal=vvictory_allow_normal, apply_to_computer=vvictory_apply_to_computer)) if vvictory_condition == "Acquire artifact": (vvictory_artifact_id,) = r("<B") try: vvictory_artifact_id = h3m_artifact_types[vvictory_artifact_id] except KeyError: raise ValueError("Invalid special victory artifact id %#04x" % vvictory_artifact_id) m.special_victory_condition.append(E.artifact(vvictory_artifact_id)) if m.xpath("/map/@version")[0] in ["AB", "SoD", "WoG"]: (vvictory_artifact_unknown2,) = r("<B") vvictory_artifact_unknown2 = ("%#04x" % vvictory_artifact_unknown2) m.special_victory_condition.append(E.unknown2(vvictory_artifact_unknown2)) m.conversion_metadata.append(E.unknown2(vvictory_artifact_unknown2)) elif vvictory_condition == "Accumulate creatures": (vvictory_creature_id,vvictory_creature_quantity) = r("<BI") try: vvictory_creature_id = h3m_creature_types[vvictory_creature_id] except KeyError: raise ValueError("Invalid special victory creature id %#04x" % vvictory_creature_id) m.special_victory_condition.append(E.creature(vvictory_creature_id,quantity=str(vvictory_creature_quantity))) elif vvictory_condition == "Accumulate resources": (vvictory_resource_type, vvictory_resource_quantity) = r("<BI") try: vvictory_resource_type = h3m_resource_types[vvictory_resource_type] except KeyError: raise ValueError("Invalid special victory resource type %#04x" % vvictory_resource_type) m.special_victory_condition.append(E.resource(vvictory_resource_type,quantity=str(vvictory_resource_quantity))) elif vvictory_condition == "Upgrade town": raise NotImplementedError elif vvictory_condition in ("Build Grail", "Defeat hero", "Capture town", "Defeat monster"): vcoords = r("<BBB") m.special_victory_condition.append(E.location("%d,%d,%d" % vcoords)) elif vvictory_condition in ("Flag all dwellings", "Flag all mines"): pass elif vvictory_condition == "Transport artifact": raise NotImplementedError else: raise NotImplementedError #special loss condition (vloss_condition,) = r("<B") try: vloss_condition = h3m_loss_conditions[vloss_condition] except KeyError: raise ValueError("Invalid special loss condition %#04x" % vloss_condition) if vloss_condition != "None": m.append(E.special_loss_condition(type=vloss_condition)) if vloss_condition in ("Lose town", "Lose hero"): vcoords = r("<BBB") m.special_loss_condition.append(E.location("%d,%d,%d" % vcoords)) elif vloss_condition == "Time limit": (vloss_days,) = r("<H") m.special_loss_condition.append(E.days(str(vloss_days))) else: raise NotImplementedError #Teams (vteam_count, ) = r("<B") if vteam_count > 0: #print dict(zip(range(0,8),r("<8B"))) vteams = r("<8B") for pnum in range(0,8): if m.xpath("/map/players/player[@color=$color]/@type",color=h3m_player_colors[pnum])[0] != "Disabled": m.xpath("/map/players/player[@color=$color]",color=h3m_player_colors[pnum])[0].append(E.team(str(vteams[pnum]))) #guess_str = m.xpath("/map/name")[0].text + ' ' + m.xpath("/map/description")[0].text #print guessLanguage(guess_str) if m.xpath("/map/@version")[0] == "RoE": vavailable_heroes = r("<16s")[0] else: vavailable_heroes = r("<20s")[0] vunknown3 = "0x"+binascii.hexlify(r("<4s")[0]) m.conversion_metadata.append(E.unknown3(vunknown3)) if m.xpath("/map/@version")[0] in ["SoD", "WoG"]: (vdisposed_heroes,) = r("<B") if vdisposed_heroes != 0: raise NotImplementedError("Disposed heroes not implemented") vunknown4 = "0x"+binascii.hexlify(r("<31s")[0]) m.conversion_metadata.append(E.unknown4(vunknown4)) if m.xpath("/map/@version")[0] == "AB": vavailable_artifacts = r("<17s")[0] elif m.xpath("/map/@version")[0] in ["SoD", "WoG"]: vavailable_artifacts = r("<18s")[0] if m.xpath("/map/@version")[0] in ["SoD", "WoG"]: vavailable_spells = r("<9s")[0] if m.xpath("/map/@version")[0] in ["SoD", "WoG"]: vavailable_skills = r("<4s")[0] (vrumors_count,) = r("<B"); vunknown5 = "0x"+binascii.hexlify(r("<3s")[0]) m.conversion_metadata.append(E.unknown5(vunknown5)) for rnum in range(vrumors_count): (vlength, ) = r("<I") vrumor_name = h3m_data.read(vlength) enc = edet(vrumor_name) vrumor_name = vrumor_name.decode(enc["encoding"]) #m.conversion_metadata.append(E.encoding(enc["encoding"],entity="/map/rumor[@name='']",confidence=str(enc["confidence"]))) (vlength, ) = r("<I") vrumor_text = h3m_data.read(vlength) enc = edet(vrumor_text) vrumor_text = vrumor_text.decode(enc["encoding"]) m.append(E.rumor(vrumor_text,name=vrumor_name)) #m.conversion_metadata.append(E.encoding(enc["encoding"],entity="/map/description[0]",confidence=str(enc["confidence"]))) #(map_data["heroes_count"], ) = struct.unpack("<B", h3m_data.read(1)) #if map_data["heroes_count"] > 0: #map_data["free_heroes"] = [] #for i in range(map_data["heroes_count"]): #(hero_id, ) = struct.unpack("<B", h3m_data.read(1)) #(hero_portrait, ) = struct.unpack("<B", h3m_data.read(1)) #(name_length, ) = struct.unpack("<I", h3m_data.read(4)) #hero_name = h3m_data.read(name_length) #(hero_players, ) = struct.unpack("<B", h3m_data.read(1)) #map_data["free_heroes"].append({"id": hero_id, "portrait":hero_portrait, "name":hero_name, "players":hero_players}) #hero options #for i in range(156): #(hero_enable, ) = struct.unpack("<B", h3m_data.read(1)) #if hero_enable == 1: #(isExp, ) = struct.unpack("<B", h3m_data.read(1)) #if isExp == 0x01: #(exp, ) = struct.unpack("<I", h3m_data.read(4)) #(isSecSkill, ) = struct.unpack("<B", h3m_data.read(1)) #if isSecSkill == 0x01: #(skills_count, ) = struct.unpack("<I", h3m_data.read(4)) #for i in range(skills_count): #(skill_id, ) = struct.unpack("<B", h3m_data.read(1)) #(skill_lvl, ) = struct.unpack("<B", h3m_data.read(1)) #(isArtifact, ) = struct.unpack("<B", h3m_data.read(1)) #if isArtifact == 0x01: #raise NotImplementedError #(isBiography, ) = struct.unpack("<B", h3m_data.read(1)) #if isBiography == 0x01: #(length, ) = struct.unpack("<I", h3m_data.read(4)) #biography = h3m_data.read(length) #(gender, ) = struct.unpack("<B", h3m_data.read(1)) #(isSpells, ) = struct.unpack("<B", h3m_data.read(1)) #if isSpells == 0x01: #spells = struct.unpack("<9B", h3m_data.read(9)) #(isPrimarySkills, ) = struct.unpack("<B", h3m_data.read(1)) #if isPrimarySkills == 0x01: #(attack, defense, power, knowledge) = struct.unpack("<4B", h3m_data.read(4)) if m.xpath("/map/@version")[0] in ["SoD", "WoG"]: vunknown6 = "0x"+binascii.hexlify(r("<156s")[0]) m.conversion_metadata.append(E.unknown6(vunknown6)) map_size = int(m.xpath("/map/@size")[0]) upper_terrain = [[] for i in range(map_size)] #read upper world for i in range(map_size**2): x = i%map_size y = (i-x)/map_size upper_terrain[y].append(r("<7B")) #read underworld if m.xpath("/map/@underground")[0] == "True": lower_terrain = [[] for i in range(map_size)] for i in range(map_size**2): x = i%map_size y = (i-x)/map_size lower_terrain[y].append(r("<7B")) (vdef_count, ) = r("<I") print vdef_count defs = [] for i in range(vdef_count): (vlength, ) = r("<I") vdef_filename = h3m_data.read(vlength) #the biggest object is 8x6 tiles big - this is an array that maps #passability to each of this tiles with bits 1 passable, 0 not passable #we store a set with all not passable tiles print vdef_filename vdef_passability = r("<6B") #passability vdef_passability = frozenset([(8-x,6-y) for x in range(8) for y, p in enumerate(vdef_passability) if not (p >> x) & 1]) vdef_accessibility = r("<6B") #passability vdef_accessibility = frozenset([(8-x,6-y) for x in range(8) for y, p in enumerate(vdef_accessibility) if not (p >> x) & 1]) h3m_data.read(2) #landscape h3m_data.read(2) #land_edit_groups (vdef_type,vdef_subtype,vdef_groups,vdef_underlay ) = r("<IIBB") h3m_data.read(16) #junk return m (vobj_count, ) = r("<I") map_data["tunedobj"] = [] for i in range(map_data["tunedobj_count"]): (x, y, z) = struct.unpack("<3B", h3m_data.read(3)) (object_id, ) = struct.unpack("<I", h3m_data.read(4)) #print x,y,z,object_id, junk = h3m_data.read(5) #junk #if junk != "\x00\x00\x00\x00\x00": # for c in junk: # print("%02d"%ord(c), end=' ') # break #print i, map_data["objects"][object_id]["filename"], #print map_data["objects"][object_id]["class"] map_data["tunedobj"].append({"id":object_id, "x":x, "y":y, "z":z}) if object_id in (0,1): pass elif map_data["objects"][object_id]["class"] == 53: if map_data["objects"][object_id]["number"] == 7: h3m_data.read(4) else: h3m_data.read(4) elif map_data["objects"][object_id]["class"] in (76, 79): (isText, ) = struct.unpack("<B", h3m_data.read(1)) if isText == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) (isGuards, ) = struct.unpack("<B", h3m_data.read(1)) if isGuards == 0x01: for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(4) #junk (quantity, ) = struct.unpack("<I", h3m_data.read(4)) h3m_data.read(4) #junk elif map_data["objects"][object_id]["class"] in (34, 70, 62): (hero_id, color, hero) = struct.unpack("<IBB", h3m_data.read(6)) (isName, ) = struct.unpack("<B", h3m_data.read(1)) if isName == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) name = h3m_data.read(length) (isExp, ) = struct.unpack("<B", h3m_data.read(1)) if isExp == 0x01: (exp, ) = struct.unpack("<I", h3m_data.read(4)) (isPortrait, ) = struct.unpack("<B", h3m_data.read(1)) if isPortrait == 0x01: (portrait, ) = struct.unpack("<B", h3m_data.read(1)) (isSecSkill, ) = struct.unpack("<B", h3m_data.read(1)) if isSecSkill == 0x01: (skills_count, ) = struct.unpack("<I", h3m_data.read(4)) for i in range(skills_count): (skill_id, ) = struct.unpack("<B", h3m_data.read(1)) (skill_lvl, ) = struct.unpack("<B", h3m_data.read(1)) (isCreature, ) = struct.unpack("<B", h3m_data.read(1)) if isCreature == 0x01: for i in range(7): (guard_id, ) = struct.unpack("<H", h3m_data.read(2)) (guard_count, ) = struct.unpack("<H", h3m_data.read(2)) (creaturesFormation, ) = struct.unpack("<B", h3m_data.read(1)) (isArtifact, ) = struct.unpack("<B", h3m_data.read(1)) if isArtifact == 0x01: (headID, shouldersID, neckID, rightHandID, leftHandID, trunkID, rightRingID, leftRingID, legsID, misc1ID, misc2ID, misc3ID, misc4ID, machine1ID, machine2ID, machine3ID, machine4ID, magicbook, misc5ID) \ = struct.unpack("<19H", h3m_data.read(38)) (knapsack_count, ) = struct.unpack("<H", h3m_data.read(2)) if knapsack_count > 0: for i in range(knapsack_count): (knapsackID, ) = struct.unpack("<H", h3m_data.read(2)) (zoneRadius, ) = struct.unpack("<B", h3m_data.read(1)) (isBiography, ) = struct.unpack("<B", h3m_data.read(1)) if isBiography == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) biography = h3m_data.read(length) (gender, ) = struct.unpack("<B", h3m_data.read(1)) (isSpells, ) = struct.unpack("<B", h3m_data.read(1)) if isSpells == 0x01: spells = struct.unpack("<9B", h3m_data.read(9)) (isPrimarySkills, ) = struct.unpack("<B", h3m_data.read(1)) if isPrimarySkills == 0x01: (attack, defense, power, knowledge) = struct.unpack("<4B", h3m_data.read(4)) h3m_data.read(16) #unknown elif map_data["objects"][object_id]["class"] in (17, 20, 42): (owner, ) = struct.unpack("<I", h3m_data.read(4)) elif map_data["objects"][object_id]["class"] == 93: (isText, ) = struct.unpack("<B", h3m_data.read(1)) if isText == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) (isGuards, ) = struct.unpack("<B", h3m_data.read(1)) if isGuards == 0x01: for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(4) #junk (spell_id, ) = struct.unpack("<I", h3m_data.read(4)) elif map_data["objects"][object_id]["class"] == 216: (owner, ) = struct.unpack("<I", h3m_data.read(4)) (junk, ) = struct.unpack("<I", h3m_data.read(4)) if junk == 0x00: (towns, ) = struct.unpack("<H", h3m_data.read(2)) (minlevel, maxlevel, ) = struct.unpack("<2B", h3m_data.read(2)) elif map_data["objects"][object_id]["class"] in (54, 71, 72, 73, 74, 75, 162, 163, 164): (monster_id, ) = struct.unpack("<I", h3m_data.read(4)) (monster_count, ) = struct.unpack("<H", h3m_data.read(2)) (mood, ) = struct.unpack("<B", h3m_data.read(1)) (isTreasureOrText, ) = struct.unpack("<B", h3m_data.read(1)) if isTreasureOrText == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) (wood, mercury, ore, sulfur, crystal, gem, gold, artefactID) \ = struct.unpack("<7IH", h3m_data.read(30)) (mosterNeverRunAway, ) = struct.unpack("<B", h3m_data.read(1)) (monsterDontGrowUp, ) = struct.unpack("<B", h3m_data.read(1)) h3m_data.read(2) #junk elif map_data["objects"][object_id]["class"] in (98, 77): h3m_data.read(4) #junk (owner, ) = struct.unpack("<B", h3m_data.read(1)) (isName, ) = struct.unpack("<B", h3m_data.read(1)) if isName == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) name = h3m_data.read(length) (isGuard, ) = struct.unpack("<B", h3m_data.read(1)) if isGuard == 0x01: for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) (formation, ) = struct.unpack("<B", h3m_data.read(1)) (isBuildings, ) = struct.unpack("<B", h3m_data.read(1)) if isBuildings == 0x01: build = struct.unpack("6B", h3m_data.read(6)) active = struct.unpack("6B", h3m_data.read(6)) else: (isFort, ) = struct.unpack("<B", h3m_data.read(1)) mustSpells = struct.unpack("<9B", h3m_data.read(9)) canSpells = struct.unpack("<9B", h3m_data.read(9)) (eventQuantity, ) = struct.unpack("<I", h3m_data.read(4)) if eventQuantity > 0: for i in range(eventQuantity): (length, ) = struct.unpack("<I", h3m_data.read(4)) event_name = h3m_data.read(length) (length, ) = struct.unpack("<I", h3m_data.read(4)) event_text = h3m_data.read(length) (wood, mercury, ore, sulfur, crystal, gem, gold, players_affected, human_affected, ai_affected, day_of_first_event, event_iteration) \ = struct.unpack("<7I3B2H", h3m_data.read(35)) h3m_data.read(16) #junk buildings = struct.unpack("<6B", h3m_data.read(6)) creatures = struct.unpack("<7H", h3m_data.read(14)) h3m_data.read(4) #junk h3m_data.read(4) #junk elif map_data["objects"][object_id]["class"] in (5, 65, 66, 67, 68, 69): (isText, ) = struct.unpack("<B", h3m_data.read(1)) if isText == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) (isGuards, ) = struct.unpack("<B", h3m_data.read(1)) if isGuards == 0x01: for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(4) #junk elif map_data["objects"][object_id]["class"] in (33, 219): (color, ) = struct.unpack("<I", h3m_data.read(4)) for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) (undeleteSoldiers, ) = struct.unpack("<B", h3m_data.read(1)) h3m_data.read(8) elif map_data["objects"][object_id]["class"] == 87: (owner, ) = struct.unpack("<I", h3m_data.read(4)) elif map_data["objects"][object_id]["class"] == 83: (quest, ) = struct.unpack("<B", h3m_data.read(1)) if quest == 0x00: pass elif quest == 0x01: (level, ) = struct.unpack("<I", h3m_data.read(4)) elif quest == 0x02: (offence, defence, power, knowledge) = struct.unpack("4B", h3m_data.read(4)) elif quest == 0x03: (hero_id, ) = struct.unpack("<I", h3m_data.read(4)) elif quest == 0x04: (monster_id, ) = struct.unpack("<I", h3m_data.read(4)) elif quest == 0x05: (art_quantity, ) = struct.unpack("<B", h3m_data.read(1)) for i in range(art_quantity): (art, ) = struct.unpack("<H", h3m_data.read(2)) elif quest == 0x06: (creatures_quantity, ) = struct.unpack("<B", h3m_data.read(1)) for i in range(creatures_quantity): (guard_id, ) = struct.unpack("<H", h3m_data.read(2)) (guard_count, ) = struct.unpack("<H", h3m_data.read(2)) elif quest == 0x07: resources = struct.unpack("7I", h3m_data.read(28)) elif quest == 0x08: (hero_id, ) = struct.unpack("<B", h3m_data.read(1)) elif quest == 0x09: (player, ) = struct.unpack("<B", h3m_data.read(1)) else: raise NotImplementedError if quest: (time_limit, ) = struct.unpack("<I", h3m_data.read(4)) (length, ) = struct.unpack("<I", h3m_data.read(4)) quest_begin = h3m_data.read(length) (length, ) = struct.unpack("<I", h3m_data.read(4)) quest_running = h3m_data.read(length) (length, ) = struct.unpack("<I", h3m_data.read(4)) quest_end = h3m_data.read(length) (reward, ) = struct.unpack("<B", h3m_data.read(1)) if reward == 0x00: pass elif reward == 0x01: (exp, ) = struct.unpack("<I", h3m_data.read(4)) elif reward == 0x02: (spell_points, ) = struct.unpack("<I", h3m_data.read(4)) elif reward == 0x03: (morale, ) = struct.unpack("<B", h3m_data.read(1)) elif reward == 0x04: (lucky, ) = struct.unpack("<B", h3m_data.read(1)) elif reward == 0x05: (resID, ) = struct.unpack("<B", h3m_data.read(1)) (res_quantity, ) = struct.unpack("<I", h3m_data.read(4)) elif reward == 0x06: (priSkillID, ) = struct.unpack("<B", h3m_data.read(1)) (priSkillBonus, ) = struct.unpack("<B", h3m_data.read(1)) elif reward == 0x07: (secSkillID, ) = struct.unpack("<B", h3m_data.read(1)) (secSkillBonus, ) = struct.unpack("<B", h3m_data.read(1)) elif reward == 0x08: (artID, ) = struct.unpack("<H", h3m_data.read(2)) elif reward == 0x09: (spellID, ) = struct.unpack("<B", h3m_data.read(1)) elif reward == 0x0A: (creatureID, ) = struct.unpack("<H", h3m_data.read(2)) (creatureQuantity, ) = struct.unpack("<H", h3m_data.read(2)) else: raise NotImplementedError h3m_data.read(2) #junk elif map_data["objects"][object_id]["class"] in (91, 59): (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) h3m_data.read(4) #junk elif map_data["objects"][object_id]["class"] == 113: (secSkills, ) = struct.unpack("<I", h3m_data.read(4)) elif map_data["objects"][object_id]["class"] in (88, 89, 90): (spellID, ) = struct.unpack("<I", h3m_data.read(4)) elif map_data["objects"][object_id]["class"] == 215: (quest, ) = struct.unpack("<B", h3m_data.read(1)) if quest == 0x00: pass elif quest == 0x01: (level, ) = struct.unpack("<I", h3m_data.read(4)) elif quest == 0x02: (offence, defence, power, knowledge) = struct.unpack("4B", h3m_data.read(4)) elif quest == 0x03: (hero_id, ) = struct.unpack("<I", h3m_data.read(4)) elif quest == 0x04: (monster_id, ) = struct.unpack("<I", h3m_data.read(4)) elif quest == 0x05: (art_quantity, ) = struct.unpack("<B", h3m_data.read(1)) for i in range(art_quantity): (art, ) = struct.unpack("<H", h3m_data.read(2)) elif quest == 0x06: (creatures_quantity, ) = struct.unpack("<B", h3m_data.read(1)) for i in range(creatures_quantity): (guard_id, ) = struct.unpack("<H", h3m_data.read(2)) (guard_count, ) = struct.unpack("<H", h3m_data.read(2)) elif quest == 0x07: resources = struct.unpack("7I", h3m_data.read(28)) elif quest == 0x08: (hero_id, ) = struct.unpack("<B", h3m_data.read(1)) elif quest == 0x09: (player, ) = struct.unpack("<B", h3m_data.read(1)) else: raise NotImplementedError if quest: (time_limit, ) = struct.unpack("<I", h3m_data.read(4)) (length, ) = struct.unpack("<I", h3m_data.read(4)) quest_begin = h3m_data.read(length) (length, ) = struct.unpack("<I", h3m_data.read(4)) quest_running = h3m_data.read(length) (length, ) = struct.unpack("<I", h3m_data.read(4)) quest_end = h3m_data.read(length) elif map_data["objects"][object_id]["class"] == 36: (radius, ) = struct.unpack("<B", h3m_data.read(1)) h3m_data.read(3) #junk elif map_data["objects"][object_id]["class"] == 220: (resources, ) = struct.unpack("<B", h3m_data.read(1)) h3m_data.read(3) #junk elif map_data["objects"][object_id]["class"] == 217: (owner, towns) = struct.unpack("<II", h3m_data.read(8)) if towns==0x00: (towns,) = struct.unpack("<H", h3m_data.read(2)) elif map_data["objects"][object_id]["class"] == 218: (owner, minlvl, maxlvl) = struct.unpack("<IBB", h3m_data.read(6)) elif map_data["objects"][object_id]["class"] == 81: (bonus_type, primaryID) = struct.unpack("<BI", h3m_data.read(5)) h3m_data.read(3) #junk elif map_data["objects"][object_id]["class"] == 6: (isText, ) = struct.unpack("<B", h3m_data.read(1)) if isText == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) (isGuards, ) = struct.unpack("<B", h3m_data.read(1)) if isGuards == 0x01: for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(4) #junk (exp, spell_points, morals, luck, wood, mercury, ore, sulfur, crystal, gem, gold, offence, defence, power, knowledge) = \ struct.unpack("<IIBBIIIIIIIBBBB", h3m_data.read(42)) (secSkills, ) = struct.unpack("<B", h3m_data.read(1)) if secSkills > 0: for i in range(secSkills): (skill_id, skill_lvl) = struct.unpack("<BB", h3m_data.read(2)) (artefacts, ) = struct.unpack("<B", h3m_data.read(1)) if artefacts > 0: for i in range(artefacts): (artID, ) = struct.unpack("<H", h3m_data.read(2)) (spells, ) = struct.unpack("<B", h3m_data.read(1)) if spells > 0: for i in range(spells): (spellID, ) = struct.unpack("<B", h3m_data.read(1)) (monsters, ) = struct.unpack("<B", h3m_data.read(1)) if monsters > 0: for i in range(monsters): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(8) #junk elif map_data["objects"][object_id]["class"] == 26: (isText, ) = struct.unpack("<B", h3m_data.read(1)) if isText == 0x01: (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) (isGuards, ) = struct.unpack("<B", h3m_data.read(1)) if isGuards == 0x01: for i in range(7): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(4) #junk (exp, spell_points, morals, luck, wood, mercury, ore, sulfur, crystal, gem, gold, offence, defence, power, knowledge) = \ struct.unpack("<IIBBIIIIIIIBBBB", h3m_data.read(42)) (secSkills, ) = struct.unpack("<B", h3m_data.read(1)) if secSkills > 0: for i in range(secSkills): (skill_id, skill_lvl) = struct.unpack("<BB", h3m_data.read(2)) (artefacts, ) = struct.unpack("<B", h3m_data.read(1)) if artefacts > 0: for i in range(artefacts): (artID, ) = struct.unpack("<H", h3m_data.read(2)) (spells, ) = struct.unpack("<B", h3m_data.read(1)) if spells > 0: for i in range(spells): (spellID, ) = struct.unpack("<B", h3m_data.read(1)) (monsters, ) = struct.unpack("<B", h3m_data.read(1)) if monsters > 0: for i in range(monsters): (guard_id, guard_count) = struct.unpack("<HH", h3m_data.read(4)) h3m_data.read(8) #junk (players, isAICan, disableAfterFirstDay) = struct.unpack("<BBB", h3m_data.read(3)) h3m_data.read(4) #junk try: (gevents_count, ) = struct.unpack("<I", h3m_data.read(4)) for i in range(gevents_count): (length, ) = struct.unpack("<I", h3m_data.read(4)) name = h3m_data.read(length) (length, ) = struct.unpack("<I", h3m_data.read(4)) text = h3m_data.read(length) h3m_data.read(7*4) #resources (players_affected, ) = struct.unpack("<B", h3m_data.read(1)) (human_affected, ) = struct.unpack("<B", h3m_data.read(1)) (ai_affected, ) = struct.unpack("<B", h3m_data.read(1)) (day_of_first_event,) = struct.unpack("<H", h3m_data.read(2)) (event_iteration,) = struct.unpack("<H", h3m_data.read(2)) h3m_data.read(16) #junk except: print("d'ough...'") h3m_data.read(124) #junk return map_data
def __init__(self): self.axes = [0] * 6 self.buttons = bf()
def test_comment(self): expected = "*" actual = bf("[this is a comment!]++++++[>+++++++<-]>.") assert expected == actual
def test_exceeded(self): expected = 'no output [exceeded 1000 iterations]' actual = bf('+[>,[-]<]', 1000) assert expected == actual
def test_too_much_output(self): expected = "a" * 430 actual = bf("+" * ord("a") + "[.]") assert expected == actual
def test_left_wrap(self): # eventually, wrap around and hit ourselves expected = "aaaaaaa [exceeded 2000 iterations]" actual = bf("+[<[-" + "+" * ord("a") + ".[-]]+]", 2000, buffer_size=5) assert expected == actual
def test_hello(self): expected = "Hello world!" actual = bf("--[>--->->->++>-<<<<<-------]>--.>---------.>--..+++.>---" "-.>+++++++++.<<.+++.------.<-.>>+.") assert expected == actual
def test_exceeded(self): expected = "no output [exceeded 1000 iterations]" actual = bf("+[>,[-]<]", 1000) assert expected == actual
def test_empty(self): expected = "no output" actual = bf("+++[-]") assert expected == actual
def test_unprintable(self): expected = "no printable output" actual = bf("+.") assert expected == actual
def test_unprintable(self): expected = 'no printable output' actual = bf('+.') assert expected == actual
def test_comment(self): expected = '*' actual = bf('[this is a comment!]++++++[>+++++++<-]>.') assert expected == actual
def __init__(self): self.value = bf(0x40)
def test_inf_mem(self): expected = 'no output [exceeded 1000 iterations]' actual = bf('+[>[.-]+]', 1000, buffer_size=10) assert expected == actual
#! /usr/bin/env python from bf import bf import argparse parser = argparse.ArgumentParser(description="Command-Line bf runner") # parser.add_argument() parser.add_argument("-c", "--code", help="code to parse") parser.add_argument("-i", "--input", help="input to use") args = parser.parse_args() if (args.input == None or args.code == None): print("Must provide code and input") interpreter = bf(args.code, args.input) print(interpreter.run())
def test_empty(self): expected = 'no output' actual = bf('+++[-]') assert expected == actual
def test_hello(self): expected = 'Hello world!' actual = bf('--[>--->->->++>-<<<<<-------]>--.>---------.>--..+++.>---' '-.>+++++++++.<<.+++.------.<-.>>+.') assert expected == actual
def on_btn_run_clicked(self, button): code_text = code_buffer.get_text(code_buffer.get_start_iter(), code_buffer.get_end_iter(), False) input_text = txt_input.get_text() lbl_output.set_text(bf(code_text, input_text).run())
def test_left_wrap(self): # eventually, wrap around and hit ourselves expected = 'aaaaaaa [exceeded 2000 iterations]' actual = bf('+[<[-' + '+' * ord('a') + '.[-]]+]', 2000, buffer_size=5) assert expected == actual