#########################################################################
# _ __ _ ___ ___ _ _
# | | / / (_) | \/ | | | ( )
# | |/ / __ _ _ | . . | ___ _ __ | | _____ _ _|/ ___
# | \ / _` | | | |\/| |/ _ \| '_ \| |/ / _ \ | | | / __|
# | |\ \ (_| | | | | | | (_) | | | | < __/ |_| | \__ \
# \_| \_/\__,_|_| \_| |_/\___/|_| |_|_|\_\___|\__, | |___/
# __/ |
# ______ _ |___/
# | ___ \ | |
# | |_/ /__ _ _ __ __| | ___ _ __ ___
# | // _` | '_ \ / _` |/ _ \| '_ ` _ \
# | |\ \ (_| | | | | (_| | (_) | | | | | |
# \_| \_\__,_|_| |_|\__,_|\___/|_| |_| |_|
# ______
# | _ \
# | | | |_ _ _ __ __ _ ___ ___ _ __ ___
# | | | | | | | '_ \ / _` |/ _ \/ _ \| '_ \/ __|
# | |/ /| |_| | | | | (_| | __/ (_) | | | \__ \
# |___/ \__,_|_| |_|\__, |\___|\___/|_| |_|___/
# __/ |
# |___/
#####################################################################
# Version 1.1
#####################################################################

class Game_Interpreter
def map_switch
i = rand(3)
j = 1
clone = load_data(sprintf("Data/Map002.rvdata2", @map_id))
origX = 6
origY = 4
x = 1+6*i
y = 1+6*j
w = 5
h = 5
clone_map($game_map,clone,origX,origY,1,1,w,h)
clone_map($game_map,clone,origX,origY,x,y,w,h)
end

def clone_map(dest,source,origX,origY,sorX,sorY,w,h)
for x in 0..w-1
for y in 0..h-1
for z in 0..6
$game_map.data = source.data unless
source.data == 0 and source.data >> 8!=63
end
end
end
end

def clone_events(map,source_id,origX,origY,sorX,sorY,w,h,use_rooms=false)
source = load_data(sprintf("Data/Map"+source_id+".rvdata2", @map_id))
source.events.each do |i, event|
if event.x.between?(sorX,sorX+w) and event.y.between?(sorY,sorY+w)
e = Game_Event.new(@map_id, event)
n = e.event.name
new_x = origX+e.x-sorX
new_y = origY+e.y-sorY
if n!=nil and n.include?("<room>")
if(use_rooms)
split = n.split("<room>").split(",")
dungeon_room(split,split.to_i,split.to_i,new_x,new_y)
end
else
e.moveto(new_x,new_y)
map.events = e.clone
set_event_id(map.events,map.events.length)
map.clone_list = if map.clone_list == nil
map.clone_list.push(map.events.event.id)
end
end
end
SceneManager.scene.get_spriteset.refresh_characters
end

def reset_clone_id()
if $game_map.clone_list != nil
for i in $game_map.clone_list
for c in
key =
$game_self_switches = false
end
end
end
$game_map.clone_list =
end

def wipe_map(w)
for x in 0..$game_map.width-1
for y in 1.. (($game_map.height- 1)/w)*w
for z in 0..10
$game_map.data = 0
end
end
end
end

def rand_dungeon(map_id, detail_id, w)
#put map_id into array
map_id = unless map_id.kind_of?(Array)

##Ready and run Primms
dungeon_width = $game_map.width/w -1
dungeon_height = ($game_map.height-1)/w-1
start_point = move_player_to_center(dungeon_width,dungeon_height,w)
end_point = Point.new(rand(dungeon_width),rand(dungeon_height))
while end_point.is?(start_point.x,start_point.y)
end_point = Point.new(rand(dungeon_width),rand(dungeon_height))
end
map = run_primms(dungeon_width,dungeon_height)

#Ready Map for new dungeon
wipe_map(w)
reset_clone_id()
remove_all_events()

#print dungeon
detail = load_data(sprintf("Data/Map"+detail_id+".rvdata2", @map_id))
for tx in 0..dungeon_width
for ty in 0..dungeon_height

#get tile index
i = (map%16) % 4
j = (map%16) / 4
x = 1+(w+1)*i
y = 1+(w+1)*j

#Copy tile from tile map
is_special = end_point.is?(tx,ty) || start_point.is?(tx,ty)
clone_id = is_special ? map_id : map_id.shuffle
clone = load_data("Data/Map"+clone_id+".rvdata2")
clone_map($game_map,clone,w*tx,w*ty+1,x,y,w,w)

#copy events from map, replacing <room> events with random rooms
#(unless the tile is the start or end tile)
clone_events($game_map,clone_id,w*tx,w*ty+1,x,y,w,w,!is_special)

#If the tile is the start or end tile, copy the events and tile overlay
#for that tile.
if(end_point.is?(tx,ty))
clone_map($game_map,detail,w*tx,w*ty+1,1,1,w,w)
clone_events($game_map,detail_id,w*tx,w*ty+1,1,1,w,w,false)
elsif(start_point.is?(tx,ty))
clone_map($game_map,detail,w*tx,w*ty+1,2+w,1,w,w)
clone_events($game_map,detail_id,w*tx,w*ty+1,2+w,1,w,w,false)
end
end
end
move_player_to_closest_tile()
end

def run_primms(dungeon_width,dungeon_height)
map = Array.new
points = Array.new
for tx in 0..dungeon_width
map = Array.new
for ty in 0..dungeon_height
map = 0
points.push(Point.new(tx,ty))
end
end

#seed
map |= (4)
map |= (1)

remaining = (dungeon_width+1)*(dungeon_height+1)-2
while remaining > 0
remaining -= 1
options = Array.new
for p in points
options.push(p) if map == 0 and p.x != 0 and map != 0
options.push(p) if map == 0 and p.x != dungeon_width and map != 0
options.push(p) if map == 0 and p.y != 0 and map != 0
options.push(p) if map == 0 and p.y != dungeon_height and map != 0
end
break if options.length == 0
p = options.shuffle

dirs = Array.new

dirs.push(0) if p.x != 0 and map != 0
dirs.push(1) if p.x != dungeon_width and map != 0
dirs.push(2) if p.y != 0 and map != 0
dirs.push(3) if p.y != dungeon_height and map != 0

dir = dirs.shuffle
if dir==0
map |= (8)
map |= (2)
elsif dir==1
map |= (2)
map |= (8)
elsif dir==2
map |= (1)
map |= (4)
elsif dir==3
map |= (4)
map |= (1)
end
end
return map
end

def remove_all_events
$game_map.events.reject!{ |e| !$game_map.events.event.name.include?("<KEEP>")}
end

def is_deadend?(i)
return .include?(i%16)
end

def dungeon_room(map_id,w,h,x,y)
map = load_data("Data/Map"+map_id+".rvdata2")

i_range = (map.width-1)/(w+1)
j_range = (map.height-1)/(h+1)

i = rand(i_range)
j = rand(j_range)
sorX = 1+(w+1)*i
sorY = 1+(h+1)*j

clone_map($game_map,map,x,y,sorX,sorY,w,h)
clone_events($game_map,map_id,x,y,sorX,sorY,w,h,true)
end

def set_event_id(event,id)
event.id = id
event.event.id = id
end

def move_player_to_center(width,height,w)
p = Point.new($game_player.x/w,$game_player.y/w)
x = ($game_player.x/w)*w
y = ($game_player.y/w)*w
$game_player.moveto(x+w/2,y+1+w/2)
return p
end

def move_player_to_closest_tile()
x = $game_player.x
y = $game_player.y
return if $game_map.airship_land_ok?(x,y)
for i in 1..5
return $game_player.moveto(x+i,y) if $game_map.airship_land_ok?(x+i,y)
return $game_player.moveto(x-i,y) if $game_map.airship_land_ok?(x-i,y)
return $game_player.moveto(x,y+i) if $game_map.airship_land_ok?(x,y+i)
return $game_player.moveto(x,y-i) if $game_map.airship_land_ok?(x,y-i)
return $game_player.moveto(x+i,y+i) if $game_map.airship_land_ok?(x+i,y+i)
return $game_player.moveto(x-i,y-i) if $game_map.airship_land_ok?(x-i,y-i)
return $game_player.moveto(x-i,y+i) if $game_map.airship_land_ok?(x-i,y+i)
return $game_player.moveto(x+i,y-i) if $game_map.airship_land_ok?(x+i,y-i)
end
end
end

class Point
attr_accessor :x
attr_accessor :y
def initialize(x,y)
@x = x
@y = y
end

def is?(x,y)
return true if (@x==x and @y==y)
return false
end
end

class Scene_Map < Scene_Base
def get_spriteset
return @spriteset
end
end

class Game_Event < Game_Character
attr_accessor :id
attr_accessor :event
end

class Game_Map
attr_accessor :clone_list
attr_accessor :map_id
end