PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/extras/replace_dimensions.py

https://bitbucket.org/iimarckus/pokered-greenified
Python | 245 lines | 194 code | 15 blank | 36 comment | 9 complexity | 38d7b9ecd056a231773d8c7b3492dc41 MD5 | raw file
  1. #author: Bryan Bishop <kanzure@gmail.com>
  2. #date: 2012-01-15
  3. #replace dimensions with constants
  4. import sys #for non-newline-terminated output :/
  5. from add_map_labels_to_map_headers import find_with_start_of_line
  6. from pretty_map_headers import map_name_cleaner, spacing, offset_to_pointer, map_constants
  7. from connection_helper import print_connections
  8. from ctypes import c_int8
  9. # X/Y_Movement_Of_Connection
  10. #~~~~~~~~~~~~~~~~~~~~~~~~~
  11. #
  12. # A X movement is how many map blocks there are to the left of one of your north/south connections.
  13. # A Y movement is how many map blocks there are above your west/east connection.
  14. #===============================================================================
  15. # #4-#5 : Current Map Position
  16. #===============================================================================
  17. #
  18. # This points to the part of the current map (further up in RAM)
  19. # that the connection strips upperleft block is placed on the current map.
  20. #
  21. # ____________________
  22. # Connection |
  23. # Direction | Formula
  24. # ___________|_______
  25. #
  26. # North: C6EB + X_Movement_of_Connection Strip
  27. #
  28. # South: C6EB + (Height of Map + 3) * (Width of Map + 6) +
  29. # X_Movement_of_Connection Strip
  30. #
  31. # West: C6E8 + (Width of Map + 6) * (Y_Movement_of_"Connection Strip" + 3)
  32. #
  33. # East: C6E5 + (Width of Map + 6) * (Y_Movement_of_"Connection Strip" + 4)
  34. asm = None
  35. asm_lines = None
  36. def load_asm():
  37. global asm, asm_lines
  38. asm = open("../main.asm", "r").read()
  39. asm_lines = asm.split("\n")
  40. def get_xy_movement_of_connection_strip(map_id, connection_id):
  41. map1 = extract_maps.map_headers[map_id]
  42. connections = map1["connections"]
  43. connection = connections[connection_id]
  44. direction = connection["direction"]
  45. current_map_location = int(connection["current_map_tile_pointer"], 16)
  46. map2 = extract_maps.map_headers[connection["map_id"]]
  47. map2_height = int(map2["y"], 16)
  48. map2_width = int(map2["x"], 16)
  49. y_mov = None
  50. #if direction == "WEST":
  51. # y_mov = ((current_map_location - 0xC6E8) / (map2_width + 6)) - 3
  52. #elif direction == "EAST":
  53. # y_mov = ((current_map_location - 0xC6E5) / (map2_width + 6)) - 4
  54. if direction in ["WEST", "EAST"]:
  55. y_mov = c_int8(connection["y"]).value / -2
  56. x_mov = None
  57. #if direction == "NORTH":
  58. # x_mov = current_map_location - 0xC6EB
  59. #elif direction == "SOUTH":
  60. # x_mov = current_map_location - 0xC6EB - ((map2_height + 3) * (map2_width + 6))
  61. if direction in ["NORTH", "SOUTH"]:
  62. x_mov = c_int8(connection["x"]).value / -2
  63. return {"y_mov": y_mov, "x_mov": x_mov}
  64. def find_line_starting_with(value):
  65. global asm_lines
  66. id = 0
  67. for line in asm_lines:
  68. if len(line) < len(value): continue
  69. if line[:len(value)] == value:
  70. return asm_lines.index(line)
  71. id += 1
  72. return False #not found
  73. def current_map_position_formula(map_id, connection_id):
  74. map1_id = map_id
  75. map1 = extract_maps.map_headers[map_id]
  76. connections = map1["connections"]
  77. connection = connections[connection_id]
  78. map1_height = int(map1["y"], 16)
  79. map1_width = int(map1["x"], 16)
  80. map1_name = map1["name"]
  81. map1_name = map_name_cleaner(map1_name, None)[:-2]
  82. direction = connection["direction"]
  83. current_map_location = int(connection["current_map_tile_pointer"], 16)
  84. map2_id = connection["map_id"]
  85. map2 = extract_maps.map_headers[map2_id]
  86. map2_name = map2["name"]
  87. map2_name = map_name_cleaner(map2_name, None)[:-2]
  88. map2_height = int(map2["y"], 16)
  89. map2_width = int(map2["x"], 16)
  90. y_mov = None
  91. if direction == "WEST":
  92. y_mov = ((current_map_location - 0xC6E8) / (map1_width + 6)) - 3
  93. elif direction == "EAST":
  94. y_mov = ((current_map_location - 0xC6E5) / (map1_width + 6)) - 4
  95. x_mov = None
  96. if direction == "NORTH":
  97. x_mov = current_map_location - 0xC6EB
  98. elif direction == "SOUTH":
  99. x_mov = current_map_location - 0xC6EB - ((map1_height + 3) * (map1_width + 6))
  100. formula = ""
  101. if direction == "NORTH":
  102. formula = "$C6EB + " + str(x_mov)
  103. elif direction == "SOUTH":
  104. formula = "$C6EB + (" + map1_name + "Height + 3) * (" + map1_name + "Width + 6) + " + str(x_mov)
  105. elif direction == "WEST":
  106. formula = "$C6E8 + (" + map1_name + "Width + 6) * (" + str(y_mov) + " + 3)"
  107. elif direction == "EAST":
  108. formula = "$C6E5 + (" + map1_name + "Width + 6) * (" + str(y_mov) + " + 4)"
  109. return formula
  110. def replace_values():
  111. global asm_lines #0-15 ok
  112. for map_id in [3]: #extract_maps.map_headers.keys():
  113. if map_id in extract_maps.bad_maps: continue #skip
  114. if map_id == 12: continue #skip Route 1
  115. map1 = extract_maps.map_headers[map_id]
  116. label_name = map_name_cleaner(map1["name"], None)
  117. clean_name = label_name[:-2]
  118. line_number = find_line_starting_with(label_name)
  119. if line_number == False: continue #skip, not found
  120. #replace dimensions if necessary
  121. if "dimensions" in asm_lines[line_number + 2] and "$" in asm_lines[line_number + 2] and not "\t" in asm_lines[line_number+2]:
  122. asm_lines[line_number + 2] = spacing + "db " + clean_name + "Height, " + clean_name + "Width ; dimensions (y, x)"
  123. #skip the rest of this if there are no connections
  124. if len(map1["connections"]) == 0: continue
  125. if not "; connections data" in asm_lines[line_number + 6]: continue
  126. connection_offset = line_number + 8
  127. for connection_id in map1["connections"]:
  128. connection = map1["connections"][connection_id]
  129. direction = connection["direction"]
  130. map2_id = connection["map_id"]
  131. map2 = extract_maps.map_headers[map2_id]
  132. map2_name = map_name_cleaner(map2["name"], None)[:-2]
  133. map2_height = int(map2["y"], 16)
  134. map2_width = int(map2["x"], 16)
  135. movements = get_xy_movement_of_connection_strip(map_id, connection_id)
  136. y_mov = movements["y_mov"]
  137. x_mov = movements["x_mov"]
  138. #replace the first two pointers
  139. if " dw " in asm_lines[connection_offset + 1]:
  140. formula = print_connections(map_id, in_connection_id=connection_id)
  141. formula2 = current_map_position_formula(map_id, connection_id)
  142. temp_line = asm_lines[connection_offset + 1]
  143. temp_line = spacing + "dw " + formula + " ; connection strip location\n" #connection strip location
  144. temp_line += spacing + "dw " + formula2 + " ; current map position" #current map position
  145. asm_lines[connection_offset + 1] = temp_line
  146. #bigness, width
  147. if "bigness, width" in asm_lines[connection_offset + 2]:
  148. temp_line = spacing + "db "
  149. if int(connection["bigness"],16) == map2_width:
  150. temp_line += map2_name + "Width"
  151. elif int(connection["bigness"],16) == map2_height:
  152. temp_line += map2_name + "Height"
  153. else: #dunno wtf to do
  154. temp_line += "$" + hex(int(connection["bigness"],16))[2:]
  155. #if direction in ["NORTH", "SOUTH"]:
  156. # temp_line += map2_name + "Width"
  157. #elif direction in ["WEST", "EAST"]:
  158. # temp_line += map2_name + "Height"
  159. temp_line += ", " + map2_name + "Width"
  160. temp_line += " ; bigness, width"
  161. asm_lines[connection_offset + 2] = temp_line
  162. #alignments (y, x)
  163. if "alignments (y, x)" in asm_lines[connection_offset + 3]:
  164. temp_line = spacing + "db "
  165. if direction == "NORTH":
  166. temp_line += "(" + map2_name + "Height * 2) - 1"
  167. elif direction == "SOUTH":
  168. temp_line += "0"
  169. elif direction in ["WEST", "EAST"]:
  170. #TODO: this might be y_mov/4 ??
  171. temp_line += "(" + str(y_mov) + " * -2)"
  172. temp_line += ", "
  173. #Relative X-Position of player after entering connected map.
  174. if direction in ["NORTH", "SOUTH"]:
  175. temp_line += "(" + str(x_mov) + " * -2)"
  176. elif direction == "WEST":
  177. temp_line += "(" + map2_name + "Width * 2) - 1"
  178. elif direction == "EAST":
  179. temp_line += "0"
  180. temp_line += " ; alignments (y, x)"
  181. asm_lines[connection_offset + 3] = temp_line
  182. #window
  183. if "; window" in asm_lines[connection_offset + 4]:
  184. temp_line = spacing + "dw "
  185. if direction == "NORTH":
  186. temp_line += "$C6E9 + " + map2_name + "Height * (" + map2_name + "Width + 6)"
  187. elif direction in ["SOUTH", "EAST"]:
  188. temp_line += "$C6EF + " + map2_name + "Width"
  189. elif direction == "WEST":
  190. temp_line += "$C6EE + 2 * " + map2_name + "Width"
  191. temp_line += " ; window"
  192. asm_lines[connection_offset + 4] = temp_line
  193. #jump to the next connection
  194. connection_offset += 6
  195. if __name__ == "__main__":
  196. import extract_maps
  197. extract_maps.load_rom()
  198. extract_maps.load_map_pointers()
  199. extract_maps.read_all_map_headers()
  200. load_asm()
  201. replace_values()
  202. sys.stdout.write("\n".join(asm_lines))