SHARE
TWEET

Genenate Maze 0.001

a guest Sep 21st, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # --------------------------------------
  2. # Genenate Level
  3. # --------------------------------------
  4.  
  5. class Game_Party < Game_Unit
  6.  
  7.   attr_reader   :maze_layer_1
  8.   attr_reader   :maze_layer_2
  9.   attr_reader   :maze_layer_3
  10.  
  11.   alias jojo741963_maze_party_init initialize unless $@
  12.   def initialize
  13.     @maze_layer_1 = []
  14.     @maze_layer_2 = []
  15.     @maze_layer_3 = []
  16.     jojo741963_maze_party_init
  17.   end
  18.   #---------------------------------------------------------
  19.   # layer_1 - สถานะเขาวงกต 0 ผ่านไม่ได้ 1 ผ่านได้
  20.   # layer_2 - เก็บค่าทางเชื่อมต่อ (Door) ว่ามีทางเชื่อมกี่ทาง
  21.   # layer_3 - เก็บค่ารทิศที่เชื่อมต่อกับห้องนั้น [0,0,0,0]
  22.   #---------------------------------------------------------
  23.   def start_genenate_maze
  24.     @max_m = 5
  25.     @max_n = 5
  26.     @path = 15
  27.     @new_xy = [0,0]
  28.     initialize_maze
  29.     create_first_room
  30.     genenate_path(@path)
  31.   end
  32.   #---------------------------------------------------------
  33.   def initialize_maze(m = 5, n = 5)
  34.     @maze_layer_1 = []
  35.     for i in 0...m
  36.       @maze_layer_1.push(Array.new(n) {|i| 0 })
  37.     end
  38.     @maze_layer_2 = []
  39.     for i in 0...m
  40.       @maze_layer_2.push(Array.new(n) {|i| 0 })
  41.     end
  42.     @maze_layer_3 = []
  43.     for i in 0...(m*n)
  44.       @maze_layer_3.push(Array.new(3) {|i| 0 })
  45.     end
  46.     #msgbox(@maze_layer_1)
  47.     #msgbox(@maze_layer_1[0][1])
  48.   end
  49.   #---------------------------------------------------------
  50.   def create_first_room
  51.     @MAZE_x = rand(@max_m)
  52.     @MAZE_y = rand(@max_n)
  53.     @maze_layer_1[@MAZE_x][@MAZE_y] = 5
  54.     #@maze_layer_2[@MAZE_x][@MAZE_y] = genenate_door(@MAZE_x,@MAZE_y)
  55.    
  56.     printmaze
  57.    
  58.     #next x , y
  59.     @new_xy = find_next_room_dir(@MAZE_x, @MAZE_y)
  60.     @MAZE_x = @new_xy[0]
  61.     @MAZE_y = @new_xy[1]
  62.    
  63.     puts ("new @MAZE_x is "+@MAZE_x.to_s + " and new @MAZE_y is" + @MAZE_y.to_s )
  64.   end
  65.   #---------------------------------------------------------
  66.   def genenate_path(path = 15)
  67.     @path = path
  68.     while(@path > 1)
  69.       newpath = false
  70.       if @maze_layer_1[@MAZE_x][@MAZE_y] == 0
  71.         @maze_layer_1[@MAZE_x][@MAZE_y] = 1
  72.         #@maze_layer_2[@MAZE_x][@MAZE_y] = genenate_door(@MAZE_x,@MAZE_y)
  73.         @path -= 1
  74.         newpath = true
  75.         #msgbox(@maze_layer_1)
  76.         printmaze
  77.       end
  78.       if newpath && cal_empty_room_around(@MAZE_x,@MAZE_y) > 1
  79.         # รอบ ๆ ห้องนั้น ยังมีห้องว่างอยู่
  80.         @new_xy = find_next_room_dir(@MAZE_x, @MAZE_y)
  81.         @MAZE_x = @new_xy[0]
  82.         @MAZE_y = @new_xy[1]
  83.         #msgbox(@maze_layer_1)
  84.         puts ("new @MAZE_x is "+@MAZE_x.to_s + " and new @MAZE_y is" + @MAZE_y.to_s )
  85.       else
  86.         puts ("no room around! find new room" )
  87.         a = 0
  88.         while(a == 1)
  89.           @MAZE_x = rand(5)
  90.           @MAZE_y = rand(5)
  91.           a = 1 if @maze_layer_1[@MAZE_x][@MAZE_y] == 1 && cal_empty_room_around(@MAZE_x,@MAZE_y) > 1
  92.         end
  93.         @new_xy = find_next_room_dir(@MAZE_x, @MAZE_y)
  94.         @MAZE_x = @new_xy[0]
  95.         @MAZE_y = @new_xy[1]
  96.         puts ("new @MAZE_x is "+@MAZE_x.to_s + " and new @MAZE_y is" + @MAZE_y.to_s )
  97.       end        
  98.     end
  99.     msgbox(@maze_layer_1)
  100.   end
  101.   #---------------------------------------------------------
  102.   def genenate_door(x,y)
  103.     #@path
  104.     door = 0
  105.     loop do
  106.       if @path == 1 # กรณีที่เหลืออีกห้องเดียว
  107.         door = 1
  108.       end
  109.       if @path == 2 # กรณีที่เหลืออีก 2 ห้อง
  110.         if rand(100) > 50
  111.           door = 1
  112.         else
  113.           door = 2
  114.         end
  115.       end
  116.       #@path > 2 , กรณีที่เหลือห้องมากกว่า 2
  117.       # 15% - 1 , 50% - 2 , 20% - 3 , 15% - 4
  118.       r = rand(100) + 1
  119.       if r <= 15
  120.         door = 1
  121.       elsif r <= 65
  122.         door = 2
  123.       elsif r <= 85
  124.         door = 3
  125.       else
  126.         door = 4
  127.       end
  128.       if door > cal_empty_room_around(x,y)
  129.         return
  130.       end
  131.       break
  132.     end    
  133.     return door
  134.   end
  135.   #---------------------------------------------------------
  136.   def random_dir
  137.     case rand(4)
  138.     when 0 then dir = 2
  139.     when 1 then dir = 4
  140.     when 2 then dir = 6
  141.     when 3 then dir = 8
  142.     end
  143.     return dir
  144.   end
  145.   #---------------------------------------------------------
  146.   def find_next_room_dir(x,y)
  147.     while(true)
  148.       r = random_dir
  149.       puts ("random_dir : " + random_dir.to_s)
  150.       if can_room_exist?(x,y,random_dir)
  151.         puts "OK this room can be new path"
  152.         break
  153.       end
  154.       puts "not this room"
  155.     end
  156.     case r
  157.     when 2 then x += 1
  158.     when 4 then y -= 1
  159.     when 6 then y += 1
  160.     when 8 then x -= 1
  161.     end    
  162.     puts [x,y].to_s
  163.     return [x,y]
  164.   end
  165.   #---------------------------------------------------------
  166.   def cal_empty_room_around(x,y)
  167.     empty = 4
  168.     # เช็คห้องข้าง ๆ
  169.     for i in [1,2,3,4]
  170.       dir = 2 if i == 1
  171.       dir = 4 if i == 2
  172.       dir = 6 if i == 3
  173.       dir = 8 if i == 4
  174.       if not can_room_exist?(x,y,i)
  175.         empty -= 1
  176.       end
  177.     end
  178.     return empty
  179.   end
  180.   #---------------------------------------------------------
  181.   def can_room_exist?(x,y,dir)
  182.     return false if y == 0 && dir == 4
  183.     return false if y == (@max_n-1) && dir == 6
  184.     return false if x == 0 && dir == 8
  185.     return false if x == (@max_m-1) && dir == 2
  186.     case dir
  187.     when 2
  188.       return false if (@maze_layer_1[x+1][y]) == nil or (@maze_layer_1[x+1][y]).to_int > 0
  189.     when 4
  190.       return false if (@maze_layer_1[x][y-1]) == nil or (@maze_layer_1[x][y-1]).to_int > 0  
  191.     when 6
  192.       return false if (@maze_layer_1[x][y+1]) == nil or (@maze_layer_1[x][y+1]).to_int > 0  
  193.     when 8
  194.       return false if (@maze_layer_1[x-1][y]) == nil or (@maze_layer_1[x-1][y]).to_int > 0  
  195.     end
  196.     return true
  197.   end
  198.   #---------------------------------------------------------
  199. end
  200.  
  201. def printmaze
  202.   puts ("@MAZE_x : "+@MAZE_x.to_s + " @MAZE_y : " + @MAZE_y.to_s )
  203.   msgbox ($game_party.maze_layer_1[0].to_s+"\n"+$game_party.maze_layer_1[1].to_s+"\n"+$game_party.maze_layer_1[2].to_s+"\n"+$game_party.maze_layer_1[3].to_s+"\n"+$game_party.maze_layer_1[4].to_s+"\n")
  204. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top