Class: FortressCreator

Inherits:
Object
  • Object
show all
Defined in:
app/models/fortress_creator.rb

Overview

Factory class of Fortress.

Defined Under Namespace

Classes: Area

Constant Summary

ROOM_BASE_PERCENTAGE =

Base room size percentage in area.

40
ROOM_MAX_ADD_PERCENTAGE =

Additional room size percentage in area.

40
MINIMUM_SIZE =

Minimum size of fortress.

9
SIZE_COEFFICIENT =
Note:

fortress_size = MINIMUM_SIZE + MiniMap.find(mini_map_id).size * SIZE_COEFFICIENT

Size coefficient of fortress size.

2
ENTRANCE_MAX =

Maximum number of entrance of fortress.

4
UNEVEN_PATH_LENGTH =

Minimum length of vertical pathway is created.

4

Class Method Summary (collapse)

Class Method Details

+ (Object) random_cell(mini_map_id)

Create random fortress cells.

Parameters:

  • mini_map_id (Integer)

    MiniMap.id



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/fortress_creator.rb', line 25

def self.random_cell(mini_map_id)
  FortressCell.destroy_all(["mini_map_id = ?", mini_map_id])
  fortress_size = MiniMap.find(mini_map_id).size * Params::FORTRESS_SIZE_MULTIPLIER - 1
  fortress_x_cursor = 0
  cluster_side_num = (fortress_size / MINIMUM_SIZE).truncate
  cluster_side_length = (fortress_size / cluster_side_num).truncate
  modified_fortress_size = fortress_size - (fortress_size % cluster_side_num)
  fortress_cells = Array.new(modified_fortress_size).map!{ Array.new(modified_fortress_size, :blank) }
  areas = Array.new(cluster_side_num).map!{ Array.new(cluster_side_num, nil) }

  exit_directions = Params::DIRECTION_SYMBOLS.sample(rand(ENTRANCE_MAX) + 1)
  exit = Struct.new(:direction, :x, :y)
  exit_paths = Array.new
  exit_directions.each do |path|
    case path
    when :north
      exit_paths << exit.new(path, rand(cluster_side_num), 0)
    when :south
      exit_paths << exit.new(path, rand(cluster_side_num), cluster_side_num - 1)
    when :east
      exit_paths << exit.new(path, cluster_side_num - 1  , rand(cluster_side_num))
    when :west
      exit_paths << exit.new(path, 0               , rand(cluster_side_num))
    end
  end

  # create areas
  cluster_side_num.times do |y|
    cluster_side_num.times do |x|
      exit_direction = nil
      exit_paths.each do |exit_path|
        if exit_path[:x] == x and exit_path[:y] == y
          exit_direction = exit_path[:direction]
          break
        end
      end
      areas[x][y] = Area.new(x * cluster_side_length, y * cluster_side_length, cluster_side_length, exit_direction, fortress_cells)
    end
  end

  area_path_directions = [:east, :south]
  # create path between areas
  cluster_side_num.times do |y|
    cluster_side_num.times do |x|
      room_path_directions = Array.new
      # not east and south end 
      if x + 1 < cluster_side_num and y + 1 < cluster_side_num
        #room_path_directions = area_path_directions.sample(rand(area_path_directions.size) + 1)
        room_path_directions = area_path_directions.sample(area_path_directions.size)
      # east end and not south end 
      elsif x + 1 == cluster_side_num and y + 1 < cluster_side_num
        # if areas[x - 1][y].is_connected_east?
        #   room_path_directions = [:south].sample(rand([:south].size))
        # else
          room_path_directions = [:south]
        # end
      # not east end and south end 
      elsif x + 1 < cluster_side_num and y + 1 == cluster_side_num
        # if areas[x][y - 1].is_connected_south?
        #   room_path_directions = [:east].sample(rand([:east].size))
        # else
          room_path_directions = [:east]
        # end
      end

      room_path_directions.each do |room_path_direction|
        if room_path_direction == :east
          p "area(" + x.to_s + "," + y.to_s + "):east path"
          create_room_path(areas[x][y], areas[x + 1][y], :east, fortress_cells)
        elsif room_path_direction == :south
          p "area(" + x.to_s + "," + y.to_s + "):south path"
          create_room_path(areas[x][y], areas[x][y + 1], :south, fortress_cells)
        end
        areas[x][y].connected_directions << room_path_direction
      end
    end
  end

  # create fortress_cells
  modified_fortress_size.times do |y|
    modified_fortress_size.times do |x|
      fortress_cell = FortressCell.new
      fortress_cell.mini_map_id = mini_map_id
      fortress_cell.x = x
      fortress_cell.y = y
      fortress_cell.symbol = fortress_cells[x][y]
      fortress_cell.save
    end
  end
end