#===============================================================================# TSBS Addon - Grid Battle System (Pre-Alpha)# Version : 0.1#-------------------------------------------------------------------------------# * Configuration part#===============================================================================module Grid MaxRow = 3 MaxCol = 4 PosDebug = true # Display dummy box for positioning Party = [ [302,169],[356,169],[414,169],[471,169], [304,222],[360,222],[422,222],[482,222], [306,270],[364,270],[430,270],[493,270], ] # Troop grid coordinate should be reversed Troop = [ [244,169],[188,169],[132,169],[ 77,169], [242,222],[184,222],[126,222],[ 66,222], [240,270],[180,270],[114,270],[ 55,270], ] end#===============================================================================# * Grid Counting Module#===============================================================================class << Grid #---------------------------------------------------------------------------- # * Grid direction rules #---------------------------------------------------------------------------- # 2 = DOWNWARD # 4 = FORWARD # 6 = BACKWARD # 8 = UPWARD # # 1 = DOWN-LEFT # 3 = DOWN-RIGHT # 7 = UP-LEFT # 9 = UP-RIGHT #---------------------------------------------------------------------------- # * Get neighbor grid #---------------------------------------------------------------------------- def neighbor(index, dir) coordinate = index coordinate = point(index) unless index.is_a?(Array) case dir when 2; coordinate[1] += 1 # DOWN when 4; coordinate[0] -= 1 # FORWARD when 6; coordinate[0] += 1 # BACKWARD when 8; coordinate[1] -= 1 # UP # Diagonal direction when 1 coordinate[0] -= 1 coordinate[1] += 1 when 3 coordinate[0] += 1 coordinate[1] += 1 when 7 coordinate[0] -= 1 coordinate[1] -= 1 when 9 coordinate[0] += 1 coordinate[1] -= 1 end return cell(*coordinate) end #----------------------------------------------------------------------------- # Translate point coordinate [x,y] into Cell Index # > Column equal as X axis # > Row equal as Y axis #----------------------------------------------------------------------------- def cell(col, row) return nil if out_of_bound?(row, 0, Grid::MaxRow - 1) return nil if out_of_bound?(col, 0, Grid::MaxCol - 1) return (Grid::MaxCol * row) + col end #----------------------------------------------------------------------------- # * Translate cell index into point [x,y] #----------------------------------------------------------------------------- def point(index) return [index % Grid::MaxCol, index / Grid::MaxCol] end #----------------------------------------------------------------------------- # * Simply check if the value is out of bound #----------------------------------------------------------------------------- def out_of_bound?(value, min, max) return value > max || value < min end #----------------------------------------------------------------------------- # * Max Index #----------------------------------------------------------------------------- def max_index Grid::MaxRow * Grid::MaxCol end #----------------------------------------------------------------------------- # TARGETING PART! #----------------------------------------------------------------------------- # * Surrounding grid #----------------------------------------------------------------------------- def surrounding(index, directions = Grid::Movement, compact = true) result = directions.collect {|dir| neighbor(index, dir)} + [index] return result.compact.uniq if compact return result.uniq end #----------------------------------------------------------------------------- # * Spread search. Expand node using BFS iteration #----------------------------------------------------------------------------- def spread_search(index,directions = Grid::Movement,limit = 1,compact = true) i = 0 result = [index] iteration = [index] until i == limit temp_res = [] iteration.each do |it| cells = surrounding(it, directions, compact) cells.delete_if {|c| result.include?(c)} temp_res += cells end temp_res.uniq! iteration = temp_res result += temp_res i += 1 end return result.compact.uniq if compact return result.uniq end #----------------------------------------------------------------------------- # * Linear repeated search #----------------------------------------------------------------------------- def linear_search(index,directions = Grid::Movement,limit = 1,compact = true) result = [] directions.each do |dir| result += spread_search(index, [dir], limit, compact) end return result.uniq end #----------------------------------------------------------------------------- # * Random grid drop #----------------------------------------------------------------------------- def random_grid(index = nil) return rand(max_index) unless index result = nil result = rand(max_index) until result != index return result end #----------------------------------------------------------------------------- # * Horizontal line #----------------------------------------------------------------------------- def horizontal(index, limit = Grid::MaxCol) linear_search(index, [4,6], limit) end #----------------------------------------------------------------------------- # * Vertical line #----------------------------------------------------------------------------- def vertical(index, limit = Grid::MaxRow) linear_search(index, [2,8], limit) end #----------------------------------------------------------------------------- # * Eight direction spread #----------------------------------------------------------------------------- def dir8(index, limit = 1) spread_search(index, [1,2,3,4,6,7,8,9], limit) end #----------------------------------------------------------------------------- # * Four direction spread #----------------------------------------------------------------------------- def dir4(index, limit = 1) spread_search(index, [2,4,6,8], limit) end #----------------------------------------------------------------------------- # * Cross shaped area #----------------------------------------------------------------------------- def cross_shape(index, limit = 1) linear_search(index, [1,3,7,9], limit) end #----------------------------------------------------------------------------- # * Plus shaped area #----------------------------------------------------------------------------- def plus_shape(index, limit = 1) linear_search(index, [2,4,6,8], limit) end #----------------------------------------------------------------------------- # * All area #----------------------------------------------------------------------------- def all_area Array.new(Grid::MaxRow * Grid::MaxCol) {|i| i} end end