Turned Town Map PBS data into a GameData class

This commit is contained in:
Maruno17
2022-11-20 21:44:53 +00:00
parent f33eb4d896
commit 4d147a7bf7
27 changed files with 127 additions and 205 deletions

View File

@@ -246,6 +246,7 @@ module GameData
# A bulk loader method for all data stored in .dat files in the Data folder.
#=============================================================================
def self.load_all
TownMap.load
Type.load
Ability.load
Move.load

View File

@@ -2,7 +2,6 @@
# Data caches.
#===============================================================================
class Game_Temp
attr_accessor :town_map_data
attr_accessor :regional_dexes_data
attr_accessor :battle_animations_data
attr_accessor :move_to_battle_animation_data
@@ -11,7 +10,6 @@ end
def pbClearData
if $game_temp
$game_temp.town_map_data = nil
$game_temp.regional_dexes_data = nil
$game_temp.battle_animations_data = nil
$game_temp.move_to_battle_animation_data = nil
@@ -24,17 +22,6 @@ def pbClearData
end
end
#===============================================================================
# Method to get Town Map data.
#===============================================================================
def pbLoadTownMapData
$game_temp = Game_Temp.new if !$game_temp
if !$game_temp.town_map_data
$game_temp.town_map_data = load_data("Data/town_map.dat")
end
return $game_temp.town_map_data
end
#===============================================================================
# Method to get Regional Dexes data.
#===============================================================================

View File

@@ -0,0 +1,40 @@
module GameData
class TownMap
attr_reader :id
attr_reader :real_name
attr_reader :filename
attr_reader :point
attr_reader :flags
DATA = {}
DATA_FILENAME = "town_map.dat"
SCHEMA = {
"SectionName" => [:id, "u"],
"Name" => [:real_name, "s"],
"Filename" => [:filename, "s"],
"Point" => [:point, "^uussUUUU"],
"Flags" => [:flags, "*s"]
}
extend ClassMethodsIDNumbers
include InstanceMethods
def initialize(hash)
@id = hash[:id]
@real_name = hash[:real_name] || "???"
@filename = hash[:filename]
@point = hash[:point] || []
@flags = hash[:flags] || []
end
# @return [String] the translated name of this region
def name
return pbGetMessage(MessageTypes::RegionNames, @id)
end
def has_flag?(flag)
return @flags.any? { |f| f.downcase == flag.downcase }
end
end
end

View File

@@ -17,13 +17,13 @@ class PokemonPokedexInfo_Scene
@sprites["infosprite"].setOffset(PictureOrigin::CENTER)
@sprites["infosprite"].x = 104
@sprites["infosprite"].y = 136
@mapdata = pbLoadTownMapData
mappos = $game_map.metadata&.town_map_position
if @region < 0 # Use player's current region
@region = (mappos) ? mappos[0] : 0 # Region 0 default
end
@mapdata = GameData::TownMap.get(@region)
@sprites["areamap"] = IconSprite.new(0, 0, @viewport)
@sprites["areamap"].setBitmap("Graphics/UI/Town Map/#{@mapdata[@region][1]}")
@sprites["areamap"].setBitmap("Graphics/UI/Town Map/#{@mapdata.filename}")
@sprites["areamap"].x += (Graphics.width - @sprites["areamap"].bitmap.width) / 2
@sprites["areamap"].y += (Graphics.height + 32 - @sprites["areamap"].bitmap.height) / 2
Settings::REGION_MAP_EXTRAS.each do |hidden|
@@ -296,7 +296,7 @@ class PokemonPokedexInfo_Scene
# defined point in town_map.txt, and which either have no Self Switch
# controlling their visibility or whose Self Switch is ON)
visible_points = []
@mapdata[@region][2].each do |loc|
@mapdata.point.each do |loc|
next if loc[7] && !$game_switches[loc[7]] # Point is not visible
visible_points.push([loc[0], loc[1]])
end
@@ -375,7 +375,7 @@ class PokemonPokedexInfo_Scene
)
textpos.push([_INTL("Area unknown"), Graphics.width / 2, (Graphics.height / 2) + 6, 2, base, shadow])
end
textpos.push([pbGetMessage(MessageTypes::RegionNames, @region), 414, 50, 2, base, shadow])
textpos.push([@mapdata.name, 414, 50, 2, base, shadow])
textpos.push([_INTL("{1}'s area", GameData::Species.get(@species).name),
Graphics.width / 2, 358, 2, base, shadow])
pbDrawTextPositions(overlay, textpos)

View File

@@ -72,27 +72,26 @@ class PokemonRegionMap_Scene
@viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
@viewport.z = 99999
@sprites = {}
@map_data = pbLoadTownMapData
@fly_map = fly_map
@mode = fly_map ? 1 : 0
map_metadata = $game_map.metadata
playerpos = (map_metadata) ? map_metadata.town_map_position : nil
if !playerpos
mapindex = 0
@map = @map_data[0]
@map = GameData::TownMap.get(0)
@map_x = LEFT
@map_y = TOP
elsif @region >= 0 && @region != playerpos[0] && @map_data[@region]
elsif @region >= 0 && @region != playerpos[0] && GameData::TownMap.exists?(@region)
mapindex = @region
@map = @map_data[@region]
@map = GameData::TownMap.get(@region)
@map_x = LEFT
@map_y = TOP
else
mapindex = playerpos[0]
@map = @map_data[playerpos[0]]
@map_x = playerpos[1]
@map_y = playerpos[2]
mapsize = map_metadata.town_map_size
@map = GameData::TownMap.get(playerpos[0])
@map_x = playerpos[1]
@map_y = playerpos[2]
mapsize = map_metadata.town_map_size
if mapsize && mapsize[0] && mapsize[0] > 0
sqwidth = mapsize[0]
sqheight = (mapsize[1].length.to_f / mapsize[0]).ceil
@@ -106,7 +105,7 @@ class PokemonRegionMap_Scene
end
addBackgroundOrColoredPlane(@sprites, "background", "Town Map/bg", Color.black, @viewport)
@sprites["map"] = IconSprite.new(0, 0, @viewport)
@sprites["map"].setBitmap("Graphics/UI/Town Map/#{@map[1]}")
@sprites["map"].setBitmap("Graphics/UI/Town Map/#{@map.filename}")
@sprites["map"].x += (Graphics.width - @sprites["map"].bitmap.width) / 2
@sprites["map"].y += (Graphics.height - @sprites["map"].bitmap.height) / 2
Settings::REGION_MAP_EXTRAS.each do |graphic|
@@ -122,7 +121,7 @@ class PokemonRegionMap_Scene
)
end
@sprites["mapbottom"] = MapBottomSprite.new(@viewport)
@sprites["mapbottom"].mapname = pbGetMessage(MessageTypes::RegionNames, mapindex)
@sprites["mapbottom"].mapname = @map.name
@sprites["mapbottom"].maplocation = pbGetMapLocation(@map_x, @map_y)
@sprites["mapbottom"].mapdetails = pbGetMapDetails(@map_x, @map_y)
if playerpos && mapindex == playerpos[0]
@@ -177,27 +176,13 @@ class PokemonRegionMap_Scene
end
def pbSaveMapData
File.open("PBS/town_map.txt", "wb") { |f|
Compiler.add_PBS_header_to_file(f)
@map_data.length.times do |i|
map = @map_data[i]
next if !map
f.write("\#-------------------------------\r\n")
f.write(sprintf("[%d]\r\n", i))
f.write(sprintf("Name = %s\r\n", Compiler.csvQuote(map[0])))
f.write(sprintf("Filename = %s\r\n", Compiler.csvQuote(map[1])))
map[2].each do |loc|
f.write("Point = ")
Compiler.pbWriteCsvRecord(loc, f, [nil, "uussUUUU"])
f.write("\r\n")
end
end
}
GameData::TownMap.save
Compiler.write_town_map
end
def pbGetMapLocation(x, y)
return "" if !@map[2]
@map[2].each do |point|
return "" if !@map.point
@map.point.each do |point|
next if point[0] != x || point[1] != y
return "" if point[7] && (@wallmap || point[7] <= 0 || !$game_switches[point[7]])
name = pbGetMessageFromHash(MessageTypes::PlaceNames, point[2])
@@ -207,25 +192,25 @@ class PokemonRegionMap_Scene
end
def pbChangeMapLocation(x, y)
return "" if !@editor || !@map[2]
map = @map[2].select { |loc| loc[0] == x && loc[1] == y }[0]
currentobj = map
currentname = (map) ? map[2] || "" : ""
return "" if !@editor || !@map.point
point = @map.point.select { |loc| loc[0] == x && loc[1] == y }[0]
currentobj = point
currentname = (point) ? point[2] : ""
currentname = pbMessageFreeText(_INTL("Set the name for this point."), currentname, false, 250) { pbUpdate }
if currentname
if currentobj
currentobj[2] = currentname
else
newobj = [x, y, currentname, ""]
@map[2].push(newobj)
@map.point.push(newobj)
end
@changed = true
end
end
def pbGetMapDetails(x, y) # From Wichu, with my help
return "" if !@map[2]
@map[2].each do |point|
return "" if !@map.point
@map.point.each do |point|
next if point[0] != x || point[1] != y
return "" if point[7] && (@wallmap || point[7] <= 0 || !$game_switches[point[7]])
mapdesc = pbGetMessageFromHash(MessageTypes::PlaceDescriptions, point[3])
@@ -235,8 +220,8 @@ class PokemonRegionMap_Scene
end
def pbGetHealingSpot(x, y)
return nil if !@map[2]
@map[2].each do |point|
return nil if !@map.point
@map.point.each do |point|
next if point[0] != x || point[1] != y
return nil if point[7] && (@wallmap || point[7] <= 0 || !$game_switches[point[7]])
return (point[4] && point[5] && point[6]) ? [point[4], point[5], point[6]] : nil

View File

@@ -105,9 +105,8 @@ class RegionMapSprite
end
def createRegionMap(map)
@mapdata = pbLoadTownMapData
@map = @mapdata[map]
bitmap = AnimatedBitmap.new("Graphics/UI/Town Map/#{@map[1]}").deanimate
town_map = GameData::TownMap.get(map)
bitmap = AnimatedBitmap.new("Graphics/UI/Town Map/#{town_map.filename}").deanimate
retbitmap = BitmapWrapper.new(bitmap.width / 2, bitmap.height / 2)
retbitmap.stretch_blt(
Rect.new(0, 0, bitmap.width / 2, bitmap.height / 2),

View File

@@ -769,15 +769,10 @@ module RegionMapCoordsProperty
selregion = regions[0][0]
else
cmds = []
regions.each do |region|
cmds.push(region[1])
end
regions.each { |region| cmds.push(region[1]) }
selcmd = pbMessage(_INTL("Choose a region map."), cmds, -1)
if selcmd >= 0
selregion = regions[selcmd][0]
else
return oldsetting
end
return oldsetting if selcmd < 0
selregion = regions[selcmd][0]
end
mappoint = chooseMapPoint(selregion, true)
return (mappoint) ? [selregion, mappoint[0], mappoint[1]] : oldsetting
@@ -788,12 +783,8 @@ module RegionMapCoordsProperty
end
def self.getMapNameList
mapdata = pbLoadTownMapData
ret = []
mapdata.length.times do |i|
next if !mapdata[i]
ret.push([i, pbGetMessage(MessageTypes::RegionNames, i)])
end
GameData::TownMap.each { |town_map| ret.push([town_map.id, town_map.name]) }
return ret
end
end

View File

@@ -60,80 +60,28 @@ module Compiler
# Compile Town Map data
#=============================================================================
def compile_town_map(path = "PBS/town_map.txt")
compile_pbs_file_message_start(path)
sections = []
# Read from PBS file
File.open(path, "rb") { |f|
FileLineData.file = path # 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).
schema = {
"SectionName" => [:id, "u"],
"Name" => [:real_name, "s"],
"Filename" => [:filename, "s"],
"Point" => [:point, "^uussUUUU"]
}
idx = 0
pbEachFileSection(f, schema) { |contents, section_name|
echo "." if idx % 50 == 0
Graphics.update if idx % 250 == 0
idx += 1
data_hash = {:id => section_name.to_sym}
# Go through schema hash of compilable data and compile this section
schema.each_key do |key|
FileLineData.setSection(section_name, key, contents[key]) # For error reporting
if key == "SectionName"
data_hash[schema[key][0]] = pbGetCsvRecord(section_name, key, schema[key])
next
end
# Skip empty properties
next if contents[key].nil?
# Compile value for key
if schema[key][1][0] == "^"
contents[key].each do |val|
value = pbGetCsvRecord(val, key, schema[key])
value = nil if value.is_a?(Array) && value.empty?
data_hash[schema[key][0]] ||= []
data_hash[schema[key][0]].push(value)
end
data_hash[schema[key][0]].compact!
else
value = pbGetCsvRecord(contents[key], key, schema[key])
value = nil if value.is_a?(Array) && value.empty?
data_hash[schema[key][0]] = value
end
end
# Validate and modify the compiled data
validate_compiled_town_map(data_hash)
if sections[data_hash[:id]]
raise _INTL("Region ID '{1}' is used twice.\r\n{2}", data_hash[:id], FileLineData.linereport)
end
# Add town map messages to records
sections[data_hash[:id]] = [data_hash[:real_name], data_hash[:filename], data_hash[:point]]
}
}
validate_all_compiled_town_maps(sections)
# Save all data
save_data(sections, "Data/town_map.dat")
process_pbs_file_message_end
compile_PBS_file_generic(GameData::TownMap, path) do |final_validate, hash|
(final_validate) ? validate_all_compiled_town_maps : validate_compiled_town_map(hash)
end
end
def validate_compiled_town_map(hash)
end
def validate_all_compiled_town_maps(sections)
def validate_all_compiled_town_maps
# Get town map names and descriptions for translating
region_names = []
point_names = []
interest_names = []
sections.each_with_index do |region, i|
region_names[i] = region[0]
region[2].each do |point|
GameData::TownMap.each do |town_map|
region_names[town_map.id] = town_map.real_name
town_map.point.each do |point|
point_names.push(point[2])
interest_names.push(point[3])
end
end
point_names.uniq!
interest_names.uniq!
MessageTypes.setMessages(MessageTypes::RegionNames, region_names)
MessageTypes.setMessagesAsHash(MessageTypes::PlaceNames, point_names)
MessageTypes.setMessagesAsHash(MessageTypes::PlaceDescriptions, interest_names)
@@ -182,43 +130,6 @@ module Compiler
process_pbs_file_message_end
end
#=============================================================================
# Compile phone messages
#=============================================================================
def compile_phone(path = "PBS/phone.txt")
compile_PBS_file_generic(GameData::PhoneMessage, path) do |final_validate, hash|
(final_validate) ? validate_all_compiled_phone_contacts : validate_compiled_phone_contact(hash)
end
end
def validate_compiled_phone_contact(hash)
# Split trainer type/name/version into their own values, generate compound ID from them
if hash[:id].strip.downcase == "default"
hash[:id] = "default"
hash[:trainer_type] = hash[:id]
else
line_data = pbGetCsvRecord(hash[:id], -1, [0, "esU", :TrainerType])
hash[:trainer_type] = line_data[0]
hash[:real_name] = line_data[1]
hash[:version] = line_data[2] || 0
hash[:id] = [hash[:trainer_type], hash[:real_name], hash[:version]]
end
end
def validate_all_compiled_phone_contacts
# Get all phone messages for translating
messages = []
GameData::PhoneMessage.each do |contact|
[:intro, :intro_morning, :intro_afternoon, :intro_evening, :body, :body1,
:body2, :battle_request, :battle_remind, :end].each do |msg_type|
msgs = contact.send(msg_type)
next if !msgs || msgs.length == 0
msgs.each { |msg| messages.push(msg) }
end
end
MessageTypes.setMessagesAsHash(MessageTypes::PhoneMessages, messages)
end
#=============================================================================
# Compile type data
#=============================================================================
@@ -1220,6 +1131,43 @@ module Compiler
def validate_all_compiled_dungeon_parameters
end
#=============================================================================
# Compile phone messages
#=============================================================================
def compile_phone(path = "PBS/phone.txt")
compile_PBS_file_generic(GameData::PhoneMessage, path) do |final_validate, hash|
(final_validate) ? validate_all_compiled_phone_contacts : validate_compiled_phone_contact(hash)
end
end
def validate_compiled_phone_contact(hash)
# Split trainer type/name/version into their own values, generate compound ID from them
if hash[:id].strip.downcase == "default"
hash[:id] = "default"
hash[:trainer_type] = hash[:id]
else
line_data = pbGetCsvRecord(hash[:id], -1, [0, "esU", :TrainerType])
hash[:trainer_type] = line_data[0]
hash[:real_name] = line_data[1]
hash[:version] = line_data[2] || 0
hash[:id] = [hash[:trainer_type], hash[:real_name], hash[:version]]
end
end
def validate_all_compiled_phone_contacts
# Get all phone messages for translating
messages = []
GameData::PhoneMessage.each do |contact|
[:intro, :intro_morning, :intro_afternoon, :intro_evening, :body, :body1,
:body2, :battle_request, :battle_remind, :end].each do |msg_type|
msgs = contact.send(msg_type)
next if !msgs || msgs.length == 0
msgs.each { |msg| messages.push(msg) }
end
end
MessageTypes.setMessagesAsHash(MessageTypes::PhoneMessages, messages)
end
#=============================================================================
# Compile battle animations
#=============================================================================

View File

@@ -48,36 +48,7 @@ module Compiler
# Save Town Map data to PBS file
#=============================================================================
def write_town_map(path = "PBS/town_map.txt")
write_pbs_file_message_start(path)
schema = {
"Name" => [0, "s"],
"Filename" => [1, "s"],
"Point" => [2, "^uussUUUU"]
}
File.open(path, "wb") { |f|
add_PBS_header_to_file(f)
# Write each element in turn
pbLoadTownMapData.each_with_index do |element, i|
f.write("\#-------------------------------\r\n")
f.write(sprintf("[%d]\r\n", i))
schema.each_key do |key|
val = element[schema[key][0]]
next if val.nil?
if schema[key][1][0] == "^" && val.is_a?(Array)
val.each do |sub_val|
f.write(sprintf("%s = ", key))
pbWriteCsvRecord(sub_val, f, schema[key])
f.write("\r\n")
end
else
f.write(sprintf("%s = ", key))
pbWriteCsvRecord(val, f, schema[key])
f.write("\r\n")
end
end
end
}
process_pbs_file_message_end
write_PBS_file_generic(GameData::TownMap, path)
end
#=============================================================================
@@ -140,13 +111,6 @@ module Compiler
process_pbs_file_message_end
end
#=============================================================================
# Save phone messages to PBS file
#=============================================================================
def write_phone(path = "PBS/phone.txt")
write_PBS_file_generic(GameData::PhoneMessage, path)
end
#=============================================================================
# Save type data to PBS file
#=============================================================================
@@ -712,6 +676,13 @@ module Compiler
write_PBS_file_generic(GameData::DungeonParameters, path)
end
#=============================================================================
# Save phone messages to PBS file
#=============================================================================
def write_phone(path = "PBS/phone.txt")
write_PBS_file_generic(GameData::PhoneMessage, path)
end
#=============================================================================
# Save all data to PBS files
#=============================================================================