Created and implemented GameData::Type

This commit is contained in:
Maruno17
2020-12-12 21:26:46 +00:00
parent c8790bafc9
commit bc13517cb7
50 changed files with 698 additions and 784 deletions

View File

@@ -254,123 +254,73 @@ module Compiler
# Compile types
#=============================================================================
def compile_types
typechart = []
types = []
requiredtypes = {
"Name" => [1, "s"],
"InternalName" => [2, "s"],
}
optionaltypes = {
"IsPseudoType" => [3, "b"],
"IsSpecialType" => [4, "b"],
"Weaknesses" => [5, "*s"],
"Resistances" => [6, "*s"],
"Immunities" => [7, "*s"]
}
currentmap = -1
foundtypes = []
pbCompilerEachCommentedLine("PBS/types.txt") { |line,lineno|
if line[/^\s*\[\s*(\d+)\s*\]\s*$/]
sectionname = $~[1]
if currentmap>=0
for reqtype in requiredtypes.keys
if !foundtypes.include?(reqtype)
raise _INTL("Required value '{1}' not given in section [{2}].\r\n{3}",reqtype,currentmap,FileLineData.linereport)
GameData::Type::DATA.clear
type_names = []
# Read from PBS file
File.open("PBS/types.txt", "rb") { |f|
FileLineData.file = "PBS/types.txt" # For error reporting
# Read a whole section's lines at once, then run through this code.
# contents is a hash containing all the XXX=YYY lines in that section, where
# the keys are the XXX and the values are the YYY (as unprocessed strings).
pbEachFileSection(f) { |contents, type_number|
schema = GameData::Type::SCHEMA
# Go through schema hash of compilable data and compile this section
for key in schema.keys
FileLineData.setSection(type_number, key, contents[key]) # For error reporting
# Skip empty properties, or raise an error if a required property is
# empty
if contents[key].nil?
if ["Name", "InternalName"].include?(key)
raise _INTL("The entry {1} is required in PBS/types.txt section {2}.", key, type_id)
end
next
end
# Compile value for key
value = pbGetCsvRecord(contents[key], key, schema[key])
value = nil if value.is_a?(Array) && value.length == 0
contents[key] = value
# Ensure weaknesses/resistances/immunities are in arrays and are symbols
if value && ["Weaknesses", "Resistances", "Immunities"].include?(key)
contents[key] = [contents[key]] if !contents[key].is_a?(Array)
contents[key].map! { |x| x.to_sym }
contents[key].uniq!
end
foundtypes.clear
end
currentmap = sectionname.to_i
types[currentmap] = [currentmap,nil,nil,false,false,[],[],[]]
else
if currentmap<0
raise _INTL("Expected a section at the beginning of the file.\r\n{1}",FileLineData.linereport)
end
if !line[/^\s*(\w+)\s*=\s*(.*)$/]
raise _INTL("Bad line syntax (expected syntax like XXX=YYY).\r\n{1}",FileLineData.linereport)
end
matchData = $~
schema = nil
FileLineData.setSection(currentmap,matchData[1],matchData[2])
if requiredtypes.keys.include?(matchData[1])
schema = requiredtypes[matchData[1]]
foundtypes.push(matchData[1])
else
schema = optionaltypes[matchData[1]]
end
if schema
record = pbGetCsvRecord(matchData[2],lineno,schema)
types[currentmap][schema[0]] = record
end
end
# Construct type hash
type_symbol = contents["InternalName"].to_sym
type_hash = {
:id => type_symbol,
:id_number => type_number,
:name => contents["Name"],
:pseudo_type => contents["IsPseudoType"],
:special_type => contents["IsSpecialType"],
:weaknesses => contents["Weaknesses"],
:resistances => contents["Resistances"],
:immunities => contents["Immunities"]
}
# Add type's data to records
GameData::Type::DATA[type_number] = GameData::Type::DATA[type_symbol] = GameData::Type.new(type_hash)
type_names[type_number] = type_hash[:name]
}
}
types.compact!
maxValue = 0
for type in types; maxValue = [maxValue,type[0]].max; end
pseudotypes = []
specialtypes = []
typenames = []
typeinames = []
typehash = {}
for type in types
pseudotypes.push(type[0]) if type[3]
typenames[type[0]] = type[1]
typeinames[type[0]] = type[2]
typehash[type[0]] = type
end
for type in types
n = type[1]
for w in type[5]
if !typeinames.include?(w)
raise _INTL("'{1}' is not a defined type (PBS/types.txt, {2}, Weaknesses).",w,n)
end
# Ensure all weaknesses/resistances/immunities are valid types
GameData::Type.each do |type|
type.weaknesses.each do |other_type|
next if GameData::Type.exists?(other_type)
raise _INTL("'{1}' is not a defined type (PBS/types.txt, section {2}, Weaknesses).", other_type.to_s, type.id_number)
end
for w in type[6]
if !typeinames.include?(w)
raise _INTL("'{1}' is not a defined type (PBS/types.txt, {2}, Resistances).",w,n)
end
type.resistances.each do |other_type|
next if GameData::Type.exists?(other_type)
raise _INTL("'{1}' is not a defined type (PBS/types.txt, section {2}, Resistances).", other_type.to_s, type.id_number)
end
for w in type[7]
if !typeinames.include?(w)
raise _INTL("'{1}' is not a defined type (PBS/types.txt, {2}, Immunities).",w,n)
end
type.immunities.each do |other_type|
next if GameData::Type.exists?(other_type)
raise _INTL("'{1}' is not a defined type (PBS/types.txt, section {2}, Immunities).", other_type.to_s, type.id_number)
end
end
for i in 0..maxValue
pseudotypes.push(i) if !typehash[i]
end
pseudotypes.sort!
types.each { |type| specialtypes.push(type[0]) if type[4] }
specialtypes.sort!
count = maxValue+1
for i in 0...count
type = typehash[i]
j = 0; k = i
while j<count
typechart[k] = PBTypeEffectiveness::NORMAL_EFFECTIVE_ONE
atype = typehash[j]
if type && atype
typechart[k] = PBTypeEffectiveness::SUPER_EFFECTIVE_ONE if type[5].include?(atype[2]) # weakness
typechart[k] = PBTypeEffectiveness::NOT_EFFECTIVE_ONE if type[6].include?(atype[2]) # resistance
typechart[k] = PBTypeEffectiveness::INEFFECTIVE if type[7].include?(atype[2]) # immune
end
j += 1; k += count
end
end
MessageTypes.setMessages(MessageTypes::Types,typenames)
code = "class PBTypes\r\n"
for type in types
code += "#{type[2]}=#{type[0]}\r\n"
end
code += "def self.getName(id)\r\n"
code += "id=getID(PBTypes,id)\r\n"
code += "return pbGetMessage(MessageTypes::Types,id); end\r\n"
code += "def self.getCount; return #{types.length}; end\r\n"
code += "def self.maxValue; return #{maxValue}; end\r\n"
code += "end\r\n"
eval(code, TOPLEVEL_BINDING)
save_data([pseudotypes,specialtypes,typechart],"Data/types.dat")
pbAddScript(code,"PBTypes")
# Save all data
GameData::Type.save
MessageTypes.setMessages(MessageTypes::Types, type_names)
Graphics.update
end
@@ -513,7 +463,7 @@ module Compiler
# Read each line of moves.txt at a time and compile it into an move
pbCompilerEachPreppedLine("PBS/moves.txt") { |line, line_no|
line = pbGetCsvRecord(line, line_no, [0, "vnssueeuuuyiss",
nil, nil, nil, nil, nil, PBTypes, ["Physical", "Special", "Status"],
nil, nil, nil, nil, nil, :Type, ["Physical", "Special", "Status"],
nil, nil, nil, PBTargets, nil, nil, nil
])
move_number = line[0]