- 28
- Posts
- 9
- Years
- Seen May 11, 2024
Hi Everyone. Recently, I setup my game so I could toggle a switch and change the encounters on a map mid-game. I followed this Tutorial:
https://pokemonessentials.wikia.com/wiki/Tutorial:Changing_encounters_mid-game
as a baseline. It didn't work at first because of version differences I'm assuming. What I ended up doing was copying the sections listed above the changed areas and renaming the files referenced to encounters_2.txt and encounters_2.dat. I placed the changed scripts below.
Amazingly, this works for the most part. I just have two problems and I was hoping someone would have a fix.
1) I can only change the encounters listed by using the editor in the debug menu in a test game. In other words changing the encounters_2.txt file doesn't work. In fact If I compile the game for testing, the text file will change back to whatever was listed from the editor. So does anyone know how to get the text file to change the encounters instead of the editor.
2) in order for the switch to take effect you need to leave the map and come back. So, is there a way to refresh the map or force it to reload with an event script instead of leaving the map and coming back. I would hate to have to transfer the player out and in of the map each time the switch is toggled.
Thanks for any help guys!
https://pokemonessentials.wikia.com/wiki/Tutorial:Changing_encounters_mid-game
as a baseline. It didn't work at first because of version differences I'm assuming. What I ended up doing was copying the sections listed above the changed areas and renaming the files referenced to encounters_2.txt and encounters_2.dat. I placed the changed scripts below.
Amazingly, this works for the most part. I just have two problems and I was hoping someone would have a fix.
1) I can only change the encounters listed by using the editor in the debug menu in a test game. In other words changing the encounters_2.txt file doesn't work. In fact If I compile the game for testing, the text file will change back to whatever was listed from the editor. So does anyone know how to get the text file to change the encounters instead of the editor.
2) in order for the switch to take effect you need to leave the map and come back. So, is there a way to refresh the map or force it to reload with an event script instead of leaving the map and coming back. I would hate to have to transfer the player out and in of the map each time the switch is toggled.
Thanks for any help guys!
Code:
def pbSaveEncounterData()
encdata=load_data("Data/encounters.dat") rescue nil
return if !encdata
mapinfos=pbLoadRxData("Data/MapInfos")
File.open("PBS/encounters.txt","wb"){|f|
sortedkeys=encdata.keys.sort{|a,b| a<=>b}
for i in sortedkeys
if encdata[i]
e=encdata[i]
mapname=""
if mapinfos[i]
map=mapinfos[i].name
mapname=" # #{map}"
end
f.write(sprintf("#########################\r\n"))
f.write(sprintf("%03d%s\r\n",i,mapname))
f.write(sprintf("%d,%d,%d\r\n",e[0][EncounterTypes::Land],
e[0][EncounterTypes::Cave],e[0][EncounterTypes::Water]))
for j in 0...e[1].length
enc=e[1][j]
next if !enc
f.write(sprintf("%s\r\n",EncounterTypes::Names[j]))
for k in 0...EncounterTypes::EnctypeChances[j].length
encentry=(enc[k]) ? enc[k] : [1,5,5]
species=getConstantName(PBSpecies,encentry[0]) rescue pbGetSpeciesConst(encentry[0])
if encentry[1]==encentry[2]
f.write(sprintf("%s,%d\r\n",species,encentry[1]))
else
f.write(sprintf("%s,%d,%d\r\n",species,encentry[1],encentry[2]))
end
end
end
end
end
}
end
def pbSaveEncounterData2()
encdata=load_data("Data/encounters_2.dat") rescue nil
return if !encdata
mapinfos=pbLoadRxData("Data/MapInfos")
File.open("PBS/encounters_2.txt","wb"){|f|
sortedkeys=encdata.keys.sort{|a,b| a<=>b}
for i in sortedkeys
if encdata[i]
e=encdata[i]
mapname=""
if mapinfos[i]
map=mapinfos[i].name
mapname=" # #{map}"
end
f.write(sprintf("#########################\r\n"))
f.write(sprintf("%03d%s\r\n",i,mapname))
f.write(sprintf("%d,%d,%d\r\n",e[0][EncounterTypes::Land],
e[0][EncounterTypes::Cave],e[0][EncounterTypes::Water]))
for j in 0...e[1].length
enc=e[1][j]
next if !enc
f.write(sprintf("%s\r\n",EncounterTypes::Names[j]))
for k in 0...EncounterTypes::EnctypeChances[j].length
encentry=(enc[k]) ? enc[k] : [1,5,5]
species=getConstantName(PBSpecies,encentry[0]) rescue pbGetSpeciesConst(encentry[0])
if encentry[1]==encentry[2]
f.write(sprintf("%s,%d\r\n",species,encentry[1]))
else
f.write(sprintf("%s,%d,%d\r\n",species,encentry[1],encentry[2]))
end
end
end
end
end
}
end
Code:
elsif cmd=="setencounters"
encdata=load_data("Data/encounters.dat")
oldencdata=Marshal.dump(encdata)
mapedited=false
map=pbDefaultMap()
loop do
map=pbListScreen(_INTL("SET ENCOUNTERS"),MapLister.new(map))
break if map<=0
mapedited=true if map==pbDefaultMap()
pbEncounterEditorMap(encdata,map)
end
save_data(encdata,"Data/encounters.dat")
pbSaveEncounterData()
pbClearData()
elsif cmd=="setencounters2"
encdata=load_data("Data/encounters_2.dat")
oldencdata=Marshal.dump(encdata)
mapedited=false
map=pbDefaultMap()
loop do
map=pbListScreen(_INTL("SET ENCOUNTERS 2"),MapLister.new(map))
break if map<=0
mapedited=true if map==pbDefaultMap()
pbEncounterEditorMap(encdata,map)
end
save_data(encdata,"Data/encounters_2.dat")
pbSaveEncounterData2()
pbClearData()
Code:
commands.add("setencounters",_INTL("Set Encounters"))
commands.add("setencounters2",_INTL("Set Encounters 2"))
Code:
[CODE]def pbCompileEncounters
lines=[]
linenos=[]
FileLineData.file="PBS/encounters.txt"
File.open("PBS/encounters.txt","rb"){|f|
lineno=1
f.each_line {|line|
if lineno==1 && line[0]==0xEF && line[1]==0xBB && line[2]==0xBF
line=line[3,line.length-3]
end
line=prepline(line)
if line.length!=0
lines[lines.length]=line
linenos[linenos.length]=lineno
end
lineno+=1
}
}
encounters={}
thisenc=nil
lastenc=-1
lastenclen=0
needdensity=false
lastmapid=-1
i=0;
while i<lines.length
line=lines[i]
FileLineData.setLine(line,linenos[i])
mapid=line[/^\d+$/]
if mapid
lastmapid=mapid
if thisenc && (thisenc[1][EncounterTypes::Land] ||
thisenc[1][EncounterTypes::LandMorning] ||
thisenc[1][EncounterTypes::LandDay] ||
thisenc[1][EncounterTypes::BugContest] ||
thisenc[1][EncounterTypes::LandNight]) &&
thisenc[1][EncounterTypes::Cave]
raise _INTL("Can't define both Land and Cave encounters in the same area (map ID {1})",mapid)
end
thisenc=[EncounterTypes::EnctypeDensities.clone,[]]
encounters[mapid.to_i]=thisenc
needdensity=true
i+=1
next
end
enc=findIndex(EncounterTypes::Names){|val| val==line}
if enc>=0
needdensity=false
enclines=EncounterTypes::EnctypeChances[enc].length
encarray=[]
j=i+1; k=0
while j<lines.length && k<enclines
line=lines[j]
FileLineData.setLine(lines[j],linenos[j])
splitarr=strsplit(line,/\s*,\s*/)
if !splitarr || splitarr.length<2
raise _INTL("In encounters.txt, expected a species entry line,\r\ngot \"{1}\" instead (probably too few entries in an encounter type).\r\nPlease check the format of the section numbered {2},\r\nwhich is just before this line.\r\n{3}",
line,lastmapid,FileLineData.linereport)
end
splitarr[2]=splitarr[1] if splitarr.length==2
splitarr[1]=splitarr[1].to_i
splitarr[2]=splitarr[2].to_i
maxlevel=PBExperience::MAXLEVEL
if splitarr[1]<=0 || splitarr[1]>maxlevel
raise _INTL("Level number is not valid: {1}\r\n{2}",splitarr[1],FileLineData.linereport)
end
if splitarr[2]<=0 || splitarr[2]>maxlevel
raise _INTL("Level number is not valid: {1}\r\n{2}",splitarr[2],FileLineData.linereport)
end
if splitarr[1]>splitarr[2]
raise _INTL("Minimum level is greater than maximum level: {1}\r\n{2}",line,FileLineData.linereport)
end
splitarr[0]=parseSpecies(splitarr[0])
linearr=splitarr
encarray.push(linearr)
thisenc[1][enc]=encarray
j+=1
k+=1
end
if j==lines.length && k<enclines
raise _INTL("Reached end of file unexpectedly. There were too few entries in the last section, expected {1} entries.\r\nPlease check the format of the section numbered {2}.\r\n{3}",
enclines,lastmapid,FileLineData.linereport)
end
i=j
elsif needdensity
needdensity=false
nums=strsplit(line,/,/)
if nums && nums.length>=3
for j in 0...EncounterTypes::EnctypeChances.length
next if !EncounterTypes::EnctypeChances[j] ||
EncounterTypes::EnctypeChances[j].length==0
next if EncounterTypes::EnctypeCompileDens[j]==0
thisenc[0][j]=nums[EncounterTypes::EnctypeCompileDens[j]-1].to_i
end
else
raise _INTL("Wrong syntax for densities in encounters.txt; got \"{1}\"\r\n{2}",line,FileLineData.linereport)
end
i+=1
else
raise _INTL("Undefined encounter type {1}, expected one of the following:\r\n{2}\r\n{3}",
line,EncounterTypes::Names.inspect,FileLineData.linereport)
end
end
save_data(encounters,"Data/encounters.dat")
end
def pbCompileEncounters2
lines=[]
linenos=[]
FileLineData.file="PBS/encounters_2.txt"
File.open("PBS/encounters_2.txt","rb"){|f|
lineno=1
f.each_line {|line|
if lineno==1 && line[0]==0xEF && line[1]==0xBB && line[2]==0xBF
line=line[3,line.length-3]
end
line=prepline(line)
if line.length!=0
lines[lines.length]=line
linenos[linenos.length]=lineno
end
lineno+=1
}
}
encounters={}
thisenc=nil
lastenc=-1
lastenclen=0
needdensity=false
lastmapid=-1
i=0;
while i<lines.length
line=lines[i]
FileLineData.setLine(line,linenos[i])
mapid=line[/^\d+$/]
if mapid
lastmapid=mapid
if thisenc && (thisenc[1][EncounterTypes::Land] ||
thisenc[1][EncounterTypes::LandMorning] ||
thisenc[1][EncounterTypes::LandDay] ||
thisenc[1][EncounterTypes::BugContest] ||
thisenc[1][EncounterTypes::LandNight]) &&
thisenc[1][EncounterTypes::Cave]
raise _INTL("Can't define both Land and Cave encounters in the same area (map ID {1})",mapid)
end
thisenc=[EncounterTypes::EnctypeDensities.clone,[]]
encounters[mapid.to_i]=thisenc
needdensity=true
i+=1
next
end
enc=findIndex(EncounterTypes::Names){|val| val==line}
if enc>=0
needdensity=false
enclines=EncounterTypes::EnctypeChances[enc].length
encarray=[]
j=i+1; k=0
while j<lines.length && k<enclines
line=lines[j]
FileLineData.setLine(lines[j],linenos[j])
splitarr=strsplit(line,/\s*,\s*/)
if !splitarr || splitarr.length<2
raise _INTL("In encounters_2.txt, expected a species entry line,\r\ngot \"{1}\" instead (probably too few entries in an encounter type).\r\nPlease check the format of the section numbered {2},\r\nwhich is just before this line.\r\n{3}",
line,lastmapid,FileLineData.linereport)
end
splitarr[2]=splitarr[1] if splitarr.length==2
splitarr[1]=splitarr[1].to_i
splitarr[2]=splitarr[2].to_i
maxlevel=PBExperience::MAXLEVEL
if splitarr[1]<=0 || splitarr[1]>maxlevel
raise _INTL("Level number is not valid: {1}\r\n{2}",splitarr[1],FileLineData.linereport)
end
if splitarr[2]<=0 || splitarr[2]>maxlevel
raise _INTL("Level number is not valid: {1}\r\n{2}",splitarr[2],FileLineData.linereport)
end
if splitarr[1]>splitarr[2]
raise _INTL("Minimum level is greater than maximum level: {1}\r\n{2}",line,FileLineData.linereport)
end
splitarr[0]=parseSpecies(splitarr[0])
linearr=splitarr
encarray.push(linearr)
thisenc[1][enc]=encarray
j+=1
k+=1
end
if j==lines.length && k<enclines
raise _INTL("Reached end of file unexpectedly. There were too few entries in the last section, expected {1} entries.\r\nPlease check the format of the section numbered {2}.\r\n{3}",
enclines,lastmapid,FileLineData.linereport)
end
i=j
elsif needdensity
needdensity=false
nums=strsplit(line,/,/)
if nums && nums.length>=3
for j in 0...EncounterTypes::EnctypeChances.length
next if !EncounterTypes::EnctypeChances[j] ||
EncounterTypes::EnctypeChances[j].length==0
next if EncounterTypes::EnctypeCompileDens[j]==0
thisenc[0][j]=nums[EncounterTypes::EnctypeCompileDens[j]-1].to_i
end
else
raise _INTL("Wrong syntax for densities in encounters_2.txt; got \"{1}\"\r\n{2}",line,FileLineData.linereport)
end
i+=1
else
raise _INTL("Undefined encounter type {1}, expected one of the following:\r\n{2}\r\n{3}",
line,EncounterTypes::Names.inspect,FileLineData.linereport)
end
end
save_data(encounters,"Data/encounters_2.dat")
end
Code:
def setup(mapID)
@density=nil
@stepcount=0
@enctypes=[]
begin
if $game_switches[169]
data=load_data("Data/encounters_2.dat")
elsif
data=load_data("Data/encounters.dat")
end
if data.is_a?(Hash) && data[mapID]
@density=data[mapID][0]
@enctypes=data[mapID][1]
else
@density=nil
@enctypes=[]
end
rescue
@density=nil
@enctypes=[]
end
end
def pbMapHasEncounter?(mapID,enctype)
if $game_switches[169]
data=load_data("Data/encounters_2.dat")
elsif
data=load_data("Data/encounters.dat")
end
if data.is_a?(Hash) && data[mapID]
enctypes=data[mapID][1]
density=data[mapID][0]
else
return false
end
return false if density==nil || enctype<0
return enctypes[enctype] ? true : false
end
def pbMapEncounter(mapID,enctype)
if enctype<0 || enctype>EncounterTypes::EnctypeChances.length
raise ArgumentError.new(_INTL("Encounter type out of range"))
end
if $game_switches[169]
data=load_data("Data/encounters_2.dat")
elsif
data=load_data("Data/encounters.dat")
end
if data.is_a?(Hash) && data[mapID]
enctypes=data[mapID][1]
else
return nil
end
return nil if enctypes[enctype]==nil
chances=EncounterTypes::EnctypeChances[enctype]
chancetotal=0
chances.each {|a| chancetotal+=a}
rnd=rand(chancetotal)
chosenpkmn=0
chance=0
for i in 0...chances.length
chance+=chances[i]
if rnd<chance
chosenpkmn=i
break
end
end
encounter=enctypes[enctype][chosenpkmn]
level=encounter[1]+rand(1+encounter[2]-encounter[1])
return [encounter[0],level]
end
Code:
@point=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
PokemonRegionMapScene::SQUAREHEIGHT+4)
@point.fill_rect(0,0,
PokemonRegionMapScene::SQUAREWIDTH+4,
PokemonRegionMapScene::SQUAREHEIGHT+4,Color.new(255,0,0))
@point2=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
PokemonRegionMapScene::SQUAREHEIGHT+4)
@point2.fill_rect(4,0,
PokemonRegionMapScene::SQUAREWIDTH,
PokemonRegionMapScene::SQUAREHEIGHT+4,Color.new(255,0,0))
@point3=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
PokemonRegionMapScene::SQUAREHEIGHT+4)
@point3.fill_rect(0,4,
PokemonRegionMapScene::SQUAREWIDTH+4,
PokemonRegionMapScene::SQUAREHEIGHT,Color.new(255,0,0))
@point4=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
PokemonRegionMapScene::SQUAREHEIGHT+4)
@point4.fill_rect(4,4,
PokemonRegionMapScene::SQUAREWIDTH,
PokemonRegionMapScene::SQUAREHEIGHT,Color.new(255,0,0))
if $game_switches[169]
encdata=load_data("Data/encounters_2.dat")
elsif
encdata=load_data("Data/encounters.dat")
end