TM/HM & Move Tutor Expansion

This commit is contained in:
Skeli
2019-09-17 12:43:21 -04:00
parent 3873d9aa1b
commit 269c47eb80
197 changed files with 51745 additions and 105 deletions

View File

@@ -9,6 +9,7 @@ import subprocess
import sys
from datetime import datetime
from string import StringFileConverter
from tm_tutor import TMDataBuilder
if sys.platform.startswith('win'):
PathVar = os.environ.get('Path')
@@ -245,53 +246,48 @@ def ProcessString(stringFile: str) -> str:
def GetFlagsFromFlagFile(filePath: str) -> [str]:
try:
with open(filePath, "r") as file:
line = file.readline() # Only needs the first line
flags = line.split()
except FileNotFoundError:
print('"{}" could not be found.'.format(filePath))
sys.exit(1)
return flags
try:
with open(filePath, "r") as file:
line = file.readline() # Only needs the first line
flags = line.split()
except FileNotFoundError:
print('"{}" could not be found.'.format(filePath))
sys.exit(1)
return flags
def ProcessSpriteSet(fileListing: [str], flags: [str], outputFile: str, title: str):
assembledFile = os.path.join(ASSEMBLY, 'generated', outputFile)
if (not os.path.isfile(assembledFile)
or max(os.path.getmtime(file) for file in fileListing) > os.path.getmtime(assembledFile)): # If a front sprite has been modified
print("Processing {}.".format(title))
combinedFile = open(assembledFile, 'w')
combinedFile.write('@THIS IS A GENERATED FILE! DO NOT MODIFY IT!\n')
for sprite in fileListing:
assembled = sprite.split('.png')[0] + '.s'
assembledFile = os.path.join(ASSEMBLY, 'generated', outputFile)
if (not os.path.isfile(assembledFile)
or max(list(map(os.path.getmtime, fileListing))) > os.path.getmtime(assembledFile)): # If a sprite has been modified
print("Processing {}.".format(title))
combinedFile = open(assembledFile, 'w')
combinedFile.write('@THIS IS A GENERATED FILE! DO NOT MODIFY IT!\n')
for sprite in fileListing:
assembled = sprite.split('.png')[0] + '.s'
if (not os.path.isfile(assembled)
or os.path.getmtime(sprite) > os.path.getmtime(assembled)):
RunCommand([GR, sprite] + flags + ['-o', assembled])
if (not os.path.isfile(assembled)
or os.path.getmtime(sprite) > os.path.getmtime(assembled)):
RunCommand([GR, sprite] + flags + ['-o', assembled])
with open(assembled, 'r') as tempFile:
combinedFile.write(tempFile.read())
combinedFile.close()
with open(assembled, 'r') as tempFile:
combinedFile.write(tempFile.read())
combinedFile.close()
def ProcessSpriteGraphics():
frontFlags = GetFlagsFromFlagFile(GRAPHICS + "/frontspriteflags.grit")
backFlags = GetFlagsFromFlagFile(GRAPHICS + "/backspriteflags.grit")
iconFlags = GetFlagsFromFlagFile(GRAPHICS + "/iconspriteflags.grit")
frontFlags = GetFlagsFromFlagFile(GRAPHICS + "/frontspriteflags.grit")
backFlags = GetFlagsFromFlagFile(GRAPHICS + "/backspriteflags.grit")
iconFlags = GetFlagsFromFlagFile(GRAPHICS + "/iconspriteflags.grit")
try:
os.makedirs(ASSEMBLY + "/generated")
except FileExistsError:
pass
backsprites = [file for file in glob(GRAPHICS + "/backspr" + "**/*.png", recursive=True)]
frontsprites = [file for file in glob(GRAPHICS + "/frontspr" + "**/*.png", recursive=True)]
iconsprites = [file for file in glob(GRAPHICS + "/pokeicon" + "**/*.png", recursive=True)]
backsprites = [file for file in glob(GRAPHICS + "/backspr" + "**/*.png", recursive=True)]
frontsprites = [file for file in glob(GRAPHICS + "/frontspr" + "**/*.png", recursive=True)]
iconsprites = [file for file in glob(GRAPHICS + "/pokeicon" + "**/*.png", recursive=True)]
ProcessSpriteSet(frontsprites, frontFlags, 'frontsprites.s', "Front Sprites")
ProcessSpriteSet(backsprites, backFlags, 'backsprites.s', "Back Sprites")
ProcessSpriteSet(iconsprites, iconFlags, 'iconsprites.s', "Icon Sprites")
ProcessSpriteSet(frontsprites, frontFlags, 'frontsprites.s', "Front Sprites")
ProcessSpriteSet(backsprites, backFlags, 'backsprites.s', "Back Sprites")
ProcessSpriteSet(iconsprites, iconFlags, 'iconsprites.s', "Icon Sprites")
def ProcessAudio(audioFile: str) -> str:
@@ -378,13 +374,13 @@ def main():
Master.init()
startTime = datetime.now()
globs = {
'**/*.s': ProcessAssembly,
'**/*.c': ProcessC,
'**/*.string': ProcessString,
# '**/*.png': ProcessImage,
# '**/*.bmp': ProcessImage,
'**/*.wav': ProcessAudio,
'**/*.mid': ProcessMusic,
'**/*.s': ProcessAssembly,
'**/*.c': ProcessC,
'**/*.string': ProcessString,
# '**/*.png': ProcessImage,
# '**/*.bmp': ProcessImage,
'**/*.wav': ProcessAudio,
'**/*.mid': ProcessMusic,
}
# Create output directory
@@ -394,7 +390,13 @@ def main():
pass
try:
ProcessSpriteGraphics()
try:
os.makedirs(ASSEMBLY + "/generated")
except FileExistsError:
pass
ProcessSpriteGraphics()
TMDataBuilder()
# Gather source files and process them
objects = itertools.starmap(RunGlob, globs.items())

View File

@@ -1,37 +1,72 @@
#!/usr/bin/env python3
# -*- coding: cp437 -*-
import sys
from insert import TryProcessFileInclusion, TryProcessConditionalCompilation
CharMap = "charmap.tbl"
SpecialBuffers = {
"." : ["B0"],
"BUFFER" : ["FD"],
"ATTACKER" : ["FD", "0F"],
"TARGET" : ["FD", "10"],
"EFFECT_BANK" : ["FD", "11"],
"SCRIPTING_BANK" : ["FD", "13"],
"CURRENT_MOVE" : ["FD", "14"],
"LAST_ITEM" : ["FD", "16"],
"LAST_ABILITY" : ["FD", "17"],
"ATTACKER_ABILITY" : ["FD", "18"],
"TARGET_ABILITY" : ["FD", "19"],
"SCRIPTING_BANK_ABILITY" : ["FD", "1A"],
"PLAYER_NAME" : ["FD", "23"],
".": ["B0"],
"BUFFER": ["FD"],
"ATTACKER": ["FD", "0F"],
"TARGET": ["FD", "10"],
"EFFECT_BANK": ["FD", "11"],
"SCRIPTING_BANK": ["FD", "13"],
"CURRENT_MOVE": ["FD", "14"],
"LAST_ITEM": ["FD", "16"],
"LAST_ABILITY": ["FD", "17"],
"ATTACKER_ABILITY": ["FD", "18"],
"TARGET_ABILITY": ["FD", "19"],
"SCRIPTING_BANK_ABILITY": ["FD", "1A"],
"PLAYER_NAME": ["FD", "23"],
"PLAYER": ["FD", "01"],
"BUFFER1": ["FD", "02"],
"BUFFER2": ["FD", "03"],
"BUFFER3": ["FD", "04"],
"RIVAL": ["FD", "06"],
"WHITE": ["FC", "01", "01"],
"BLACK": ["FC", "01", "02"],
"GRAY": ["FC", "01", "03"],
"RED": ["FC", "01", "04"],
"ORANGE": ["FC", "01", "05"],
"GREEN": ["FC", "01", "06"],
"LIGHT_GREEN": ["FC", "01", "07"],
"BLUE": ["FC", "01", "08"],
"LIGHT_BLUE": ["FC", "01", "09"],
"ARROW_UP": ["79"],
"ARROW_DOWN": ["7A"],
"ARROW_LEFT": ["7B"],
"ARROW_RIGHT": ["7C"],
"ALIGN": ["FC", "13"],
"SHRINK": ["FC", "06", "00"]
}
def StringFileConverter(filename):
def StringFileConverter(fileName: str):
stringToWrite = ".thumb\n.text\n.align 2\n\n"
with open(filename, 'r') as file:
with open(fileName, 'r') as file:
maxLength = 0
fillFF = False
readingState = 0
lineNum = 0
definesDict = {}
conditionals = []
for line in file:
line = line.rstrip("\n\r") #Remove only newline characters
if line == "" or line[:2] == "//": #Ignore blank lines and comment lines
lineNum += 1
line = line.rstrip("\n\r") # Remove only newline characters
if TryProcessFileInclusion(line, definesDict):
continue
if TryProcessConditionalCompilation(line, definesDict, conditionals):
continue
if line.strip() == "" or line[:2] == "//": # Ignore blank lines and comment lines
continue
if readingState == 0: #Only when the file starts
if readingState == 0: # Only when the file starts
line = line.strip()
if line[:6].upper() == "#ORG @" and line[6:] != "":
title = line[6:]
@@ -41,14 +76,16 @@ def StringFileConverter(filename):
try:
maxLength = int(line.split("=")[1])
except:
print('Error reading max length in line: "' + line + '" in file: "' + filename + '"')
print('Error reading max length on line ' + str(lineNum) + ' in file: "' + fileName + '"')
sys.exit(0)
elif "FILL_FF" in line and "=" in line:
try:
fillFF = bool(line.split("=")[1])
except:
print('Error reading FF fill in line: "' + line + '" in file: "' + filename + '"')
print('Error reading FF fill on line ' + str(lineNum) + ' in file: "' + fileName + '"')
sys.exit(0)
else:
print('Warning! Error with line: "' + line + '" in file: "' + filename + '"')
print('Warning! Error on line ' + str(lineNum) + ' in file: "' + fileName + '"')
elif readingState == 1:
if line[:6].upper() == "#ORG @" and line[6:] != "":
@@ -56,14 +93,15 @@ def StringFileConverter(filename):
title = line[6:]
stringToWrite += ".global " + title + "\n" + title + ":\n"
else:
stringToWrite += ProcessString(line, maxLength, fillFF)
stringToWrite += "0xFF\n\n" #Only print line in everything went alright
stringToWrite += ProcessString(line, lineNum, maxLength, fillFF)
stringToWrite += "0xFF\n\n" # Only print line in everything went alright
output = open(filename.split(".string")[0] + '.s', 'w') #Only open file once we know everything went okay.
output = open(fileName.split(".string")[0] + '.s', 'w') # Only open file once we know everything went okay.
output.write(stringToWrite)
output.close()
def ProcessString(string, maxLength = 0, fillWithFF = False):
def ProcessString(string: str, lineNum: int, maxLength=0, fillWithFF=False) -> str:
charMap = PokeByteTableMaker()
stringToWrite = ".byte "
buffer = False
@@ -72,23 +110,29 @@ def ProcessString(string, maxLength = 0, fillWithFF = False):
strLen = 0
for char in string:
if maxLength > 0 and strLen >= maxLength:
print('Warning: The string "' + string + '" has exceeded the maximum length of ' + str(maxLength) + ' and has been truncated!')
if 0 < maxLength <= strLen:
print('Warning: The string "' + string + '" has exceeded the maximum length of '
+ str(maxLength) + ' and has been truncated!')
break
if buffer is True:
if char == ']':
buffer = False
if bufferChars in SpecialBuffers:
for bufferChar in SpecialBuffers[bufferChars]:
if maxLength > 0 and strLen >= maxLength: #End buffer in middle
print('Warning: The string buffer "' + bufferChars + '" has exceeded the maximum length of ' + str(maxLength) + ' and has been truncated!')
if 0 < maxLength <= strLen: # End buffer in middle
print('Warning: The string buffer "' + bufferChars + '" has exceeded the maximum length of '
+ str(maxLength) + ' and has been truncated!')
break
stringToWrite += ("0x" + bufferChar + ", ")
strLen += 1
elif len(bufferChars) > 2: # Unrecognized buffer
print('Warning: The string buffer "' + bufferChars + '" is not recognized!')
stringToWrite += "0x0, " # Place whitespace where the buffer should have gone
strLen += 1
else:
stringToWrite += ("0x" + bufferChars + ", ")
strLen += 1
@@ -104,8 +148,8 @@ def ProcessString(string, maxLength = 0, fillWithFF = False):
strLen += 1
except KeyError:
print('Error parsing string: "' + string + '"')
break
print('Error parsing string: "' + string + '" (Line ' + str(lineNum) + ')')
sys.exit(0)
else:
try:
@@ -113,39 +157,41 @@ def ProcessString(string, maxLength = 0, fillWithFF = False):
strLen += 1
except KeyError:
if (char == '['):
if char == '[':
buffer = True
elif (char == '\\'):
elif char == '\\':
escapeChar = True
elif char == '"':
stringToWrite += hex(charMap["\\" + char])
strLen += 1
else:
print('Error parsing string: "' + string + '"' + ' at character "' + char + '".')
break
print('Error parsing string on line ' + str(lineNum) + ' at character "' + char + '".')
sys.exit(1)
if strLen < maxLength and fillWithFF:
while strLen < maxLength:
stringToWrite += "0xFF, "
strLen += 1
return stringToWrite
def PokeByteTableMaker():
dicty = {}
dictionary = {}
with open(CharMap) as file:
for line in file:
if line.strip() != "/FF" and line.strip() != "":
if (line[2] == '=' and line[3] != ""):
try:
if line[3] == '\\':
dicty[line[3] + line[4]] = int(line.split('=')[0], 16)
else:
dicty[line[3]] = int(line.split('=')[0], 16)
except:
pass
dicty[' '] = 0
dicty["<EFBFBD>"] = 0xB0
dicty["<EFBFBD>"] = 0xB1
return dicty
for line in file:
if line.strip() != "/FF" and line.strip() != "":
if line[2] == '=' and line[3] != "":
try:
if line[3] == '\\':
dictionary[line[3] + line[4]] = int(line.split('=')[0], 16)
else:
dictionary[line[3]] = int(line.split('=')[0], 16)
except:
pass
dictionary[' '] = 0
dictionary["<EFBFBD>"] = 0xB4
dictionary["<EFBFBD>"] = 0xB0
dictionary["<EFBFBD>"] = 0xB1
return dictionary

161
scripts/tm_tutor.py Normal file
View File

@@ -0,0 +1,161 @@
import os
import sys
from glob import glob
# Data
TM_HM_COUNT = 128
TUTOR_COUNT = 64
SPECIES_COUNT = 0x44D
TM_OUTPUT = "assembly/generated/tm_compatibility.s"
TUTOR_OUTPUT = "assembly/generated/tutor_compatibility.s"
TM_COMPATIBILITY = "src/tm_compatibility"
TUTOR_COMPATIBILITY = "src/tutor_compatibility"
SPECIES_DEFINES = "include/species.h"
# Uses pre-made files corresponding to each TM to build species TM Data
def TMDataBuilder():
DataBuilder(TM_COMPATIBILITY, TM_HM_COUNT, TM_OUTPUT, "TMHM")
def TutorDataBuilder():
DataBuilder(TUTOR_COMPATIBILITY, TUTOR_COUNT, TUTOR_OUTPUT, "Tutor")
def DataBuilder(directory: str, numEntries: int, outputFile: str, dataType: str):
fileList = [file for file in glob(directory + "**/*.txt", recursive=True)]
if os.path.isfile(outputFile) and max(list(map(os.path.getmtime, fileList))) < os.path.getmtime(outputFile):
return
print("Processing {} Data.".format(dataType))
output = open(outputFile, 'w')
compatibilityTable = PokemonDataListInitializer(numEntries)
for filePath in fileList:
try:
if sys.platform.startswith('win'):
delimiter = '\\'
else: # OSX, Linux
delimiter = '/'
tmId = filePath.split()[0].split(delimiter)
tmId = int(tmId[len(tmId) - 1])
tmId -= 1
if tmId >= numEntries:
print('Ignoring file: "{}"\nTM number not valid.'.format(filePath))
continue # Don't process this file if not valid
except ValueError:
print('Ignoring file: "{}"\nNot valid TM file.'.format(filePath))
continue # Don't process this file if not proper TM file
with open(filePath, 'r') as file:
for i, line in enumerate(file):
if ':' not in line: # Aka not the first line
try:
lineContents = int(line) # Species entered as integer
compatibilityTable[lineContents][tmId] = 1
except ValueError:
try:
lineContents = int(ReverseSpeciesDict[line.strip()]) # Species entered as species name
compatibilityTable[lineContents][tmId] = 1
except ValueError:
lineContents = int(line, 16) # Species entered as hex
compatibilityTable[lineContents][tmId] = 1
except KeyError: # Species name was not found
try:
lineContents = int(ReverseSpeciesDict["SPECIES_" + line.strip()])
compatibilityTable[lineContents][tmId] = 1
except KeyError:
print('Error with key: {} on line {} in: {}'.format(line.strip(), i, filePath))
output.write(".thumb\n.align 2\n\n@THIS IS A GENERATED FILE! DO NOT MODIFY IT!\n\n"
".global g{}Learnsets\ng{}Learnsets:\n".format(dataType, dataType))
for i, mon in enumerate(compatibilityTable):
output.write(".byte ")
data = FixEndian(''.join(str(a) for a in mon))
byte = ""
for j, bit in enumerate(data):
byte += bit
if j % 8 == 7:
byte = int(byte, 2)
if j + 1 >= len(data):
output.write(hex(byte)) # End of line
else:
output.write(hex(byte) + ",")
byte = ""
output.write("\n")
output.close()
# Utility Functions
def FixEndian(string: str) -> str: # Converts bitlist from big endian to little endian
index, newString = 0, ''
for a in range(len(string) + 1):
if index % 8 == 0 and index != 0:
newString += ReverseString(string[index - 8:index])
index += 1
return newString
def ReverseString(string: str) -> str:
return ''.join(string[len(string)-a-1] for a in range(len(string)))
def PokemonDataListInitializer(numEntries: int) -> [[]]:
outerList = []
for a in range(int(SPECIES_COUNT) + 1):
innerList = [0] * numEntries
outerList.append(innerList)
return outerList
def DefinesDictMaker(definesFile: str) -> {}:
definesDict = {}
with open(definesFile, 'r') as file:
for line in file:
if '#define ' in line:
lineList = line.split()
try:
definesDict[int(lineList[2])] = lineList[1]
except:
try:
definesDict[int(lineList[2], 16)] = lineList[1]
except:
pass
return definesDict
def ReverseDict(dictionary: {}):
reverseDict = {}
for key in dictionary:
reverseDict[dictionary[key]] = key
return reverseDict
def ChangeFileLine(filePath: str, lineToChange: int, replacement: str):
with open(filePath, 'r') as file:
copy = file.read()
file.seek(0x0)
lineNum = 1
for line in file:
if lineNum == lineToChange:
copy = copy.replace(line, replacement)
break
lineNum += 1
with open(filePath, 'w') as file:
file.write(copy)
SpeciesDict = DefinesDictMaker(SPECIES_DEFINES)
ReverseSpeciesDict = ReverseDict(SpeciesDict)
if __name__ == '__main__':
TMDataBuilder()
TutorDataBuilder()