Skip to content

Commit

Permalink
linting, doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewstyler committed Aug 8, 2023
1 parent 8b7f560 commit 76ece5e
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 33 deletions.
6 changes: 5 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Metrics/AbcSize:
Metrics/ClassLength:
Exclude:
- 'lib/CLI/command.rb'
Max: 197
- 'lib/biome.rb'
- 'test/**/*'
Max: 150

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand All @@ -45,6 +47,8 @@ Metrics/MethodLength:
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Max: 6
Exclude:
- 'lib/tile.rb'

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ See Command line Usage for full customization, below are some examples. Alter th
--rs=int The seed for generating roads
(default 100)

--elevation=float Adjust each generated elevation by this percent (0 -
--elevation=float Adjust each generated elevation by this percent (-100 -
100) (default 0.0)
--moisture=float Adjust each generated moisture by this percent (0 -
--moisture=float Adjust each generated moisture by this percent (-100 -
100) (default 0.0)
--temp=float Adjust each generated temperature by this percent (0
--temp=float Adjust each generated temperature by this percent (-100
- 100) (default 0.0)
```

Expand Down Expand Up @@ -148,7 +148,7 @@ Keywords:

Options:
--elevation=float Adjust each generated elevation by
this percent (0 - 100) (default 0.0)
this percent (-100 - 100) (default 0.0)
--fhx=float The frequency for height generation
across the x-axis (default 2.5)
--fhy=float The frequency for height generation
Expand All @@ -169,7 +169,7 @@ Options:
--hs=int The seed for a terrains height perlin
generation (default 10)
--moisture=float Adjust each generated moisture by
this percent (0 - 100) (default 0.0)
this percent (-100 - 100) (default 0.0)
--ms=int The seed for a terrains moist perlin
generation (default 300)
--oh=int Octaves for height generation
Expand Down Expand Up @@ -200,7 +200,7 @@ Options:
--rs=int The seed for generating roads
(default 100)
--temp=float Adjust each generated temperature by
this percent (0 - 100) (default 0.0)
this percent (-100 - 100) (default 0.0)
--ts=int The seed for a terrains temperature
perlin generation (default 3000)
--width=int The width of the generated map
Expand Down
12 changes: 5 additions & 7 deletions lib/CLI/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Command

option :roads_to_make do
arity one
long "--roads_to_make ints"
long '--roads_to_make ints'
convert :int_list
validate ->(v) { v >= 0 }
desc 'Attempt to create a road from a start and end point (4 integers), can be supplied multiple paths'
Expand Down Expand Up @@ -222,7 +222,7 @@ class Command
long '--temp float'
long '--temp=float'

desc 'Adjust each generated temperature by this percent (0 - 100)'
desc 'Adjust each generated temperature by this percent (-100 - 100)'
convert ->(val) { val.to_f / 100.0 }
validate ->(val) { val >= -1.0 && val <= 1.0 }
default MapConfig::DEFAULT_TEMP_ADJUSTMENT
Expand All @@ -232,7 +232,7 @@ class Command
long '--elevation float'
long '--elevation=float'

desc 'Adjust each generated elevation by this percent (0 - 100)'
desc 'Adjust each generated elevation by this percent (-100 - 100)'
convert ->(val) { val.to_f / 100.0 }
validate ->(val) { val >= -1.0 && val <= 1.0 }
default MapConfig::DEFAULT_HEIGHT_ADJUSTMENT
Expand All @@ -242,7 +242,7 @@ class Command
long '--moisture float'
long '--moisture=float'

desc 'Adjust each generated moisture by this percent (0 - 100)'
desc 'Adjust each generated moisture by this percent (-100 - 100)'
convert ->(val) { val.to_f / 100.0 }
validate ->(val) { val >= -1.0 && val <= 1.0 }
default MapConfig::DEFAULT_MOIST_ADJUSTMENT
Expand Down Expand Up @@ -317,9 +317,7 @@ def execute_command
map = Map.new(map_config: MapConfig.new(
width: params[:width],
height: params[:height],
perlin_height_config: perlin_height_config,
perlin_moist_config: perlin_moist_config,
perlin_temp_config: perlin_temp_config,
all_perlin_configs: MapConfig::AllPerlinConfigs.new(perlin_height_config, perlin_moist_config, perlin_temp_config),
generate_flora: params[:generate_flora],
road_config: MapConfig::RoadConfig.new(*params.to_h.slice(:road_seed, :roads, :road_exclude_water_path, :road_exclude_mountain_path, :road_exclude_flora_path, :roads_to_make).values)
))
Expand Down
25 changes: 18 additions & 7 deletions lib/map_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,35 @@ class MapConfig
DEFAULT_ROADS_TO_MAKE = [].freeze

PERLIN_CONFIG_OPTIONS = %i[width height noise_seed octaves x_frequency y_frequency persistance adjustment].freeze
ROAD_CONFIG_OPTIONS = %i[road_seed roads road_exclude_water_path road_exclude_mountain_path road_exclude_flora_path roads_to_make].freeze
ALL_PERLIN_CONFIGS = %i[perlin_height_config perlin_moist_config perlin_temp_config].freeze
ROAD_CONFIG_OPTIONS = %i[road_seed roads road_exclude_water_path road_exclude_mountain_path road_exclude_flora_path roads_to_make].freeze

PerlinConfig = Struct.new(*PERLIN_CONFIG_OPTIONS)
AllPerlinConfigs = Struct.new(*ALL_PERLIN_CONFIGS)
RoadConfig = Struct.new(*ROAD_CONFIG_OPTIONS)

attr_reader :generate_flora, :perlin_height_config, :perlin_moist_config, :perlin_temp_config, :width, :height, :road_config

def initialize(perlin_height_config: default_perlin_height_config, perlin_moist_config: default_perlin_moist_config, perlin_temp_config: default_perlin_temp_config, width: DEFAULT_TILE_COUNT,
def initialize(all_perlin_configs: default_perlin_configs, width: DEFAULT_TILE_COUNT,
height: DEFAULT_TILE_COUNT, generate_flora: DEFAULT_GENERATE_FLORA, road_config: default_road_config)
raise ArgumentError unless perlin_height_config.is_a?(PerlinConfig) && perlin_moist_config.is_a?(PerlinConfig)

validate(all_perlin_configs)
@generate_flora = generate_flora
@perlin_height_config = perlin_height_config
@perlin_moist_config = perlin_moist_config
@perlin_temp_config = perlin_temp_config
@perlin_height_config = all_perlin_configs.perlin_height_config
@perlin_moist_config = all_perlin_configs.perlin_moist_config
@perlin_temp_config = all_perlin_configs.perlin_temp_config
@width = width
@height = height
@road_config = road_config
end

private

def validate(all_perlin_configs)
unless all_perlin_configs.perlin_height_config.is_a?(PerlinConfig) && all_perlin_configs.perlin_moist_config.is_a?(PerlinConfig) && all_perlin_configs.perlin_temp_config.is_a?(PerlinConfig)
raise ArgumentError
end
end

def default_perlin_height_config
PerlinConfig.new(DEFAULT_TILE_COUNT, DEFAULT_TILE_COUNT, DEFAULT_HEIGHT_SEED, DEFAULT_HEIGHT_OCTAVES,
DEFAULT_HEIGHT_X_FREQUENCY, DEFAULT_HEIGHT_Y_FREQUENCY, DEFAULT_HEIGHT_PERSISTANCE, DEFAULT_HEIGHT_ADJUSTMENT)
Expand All @@ -73,4 +80,8 @@ def default_perlin_temp_config
def default_road_config
RoadConfig.new(DEFAULT_ROAD_SEED, DEFAULT_NUM_OF_ROADS, DEFAULT_ROAD_EXCLUDE_WATER_PATH, DEFAULT_ROAD_EXCLUDE_MOUNTAIN_PATH, DEFAULT_ROAD_EXCLUDE_FLORA_PATH, DEFAULT_ROADS_TO_MAKE)
end

def default_perlin_configs
AllPerlinConfigs.new(default_perlin_height_config, default_perlin_moist_config, default_perlin_temp_config)
end
end
4 changes: 4 additions & 0 deletions lib/pathfinding/a_star_finder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

module Pathfinding
#
# An A* Pathfinder to build roads/paths between two coordinates containing
# different path costs, the heuristic behaviour that can be altered via configuration
#
class AStarFinder
def find_path(start_node, end_node, grid)
open_set = [start_node]
Expand Down
6 changes: 6 additions & 0 deletions lib/pathfinding/grid.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# frozen_string_literal: true

module Pathfinding
#
# Responsible for manipulating and encapsulating behaviour of tiles related
# to pathfinding
#
class Grid
attr_reader :nodes

def initialize(nodes)
@nodes = nodes
end

# rubocop:disable Naming/MethodParameterName:
def node(x, y)
nodes[y][x]
end
# rubocop:enable Naming/MethodParameterName:

def neighbors(node)
neighbors = []
Expand Down
3 changes: 3 additions & 0 deletions lib/road_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require 'pathfinding/grid'
require 'pathfinding/a_star_finder'

#
# Generates roads across map tiles, randomly or given specific coordinates
#
class RoadGenerator
attr_reader :grid, :finder

Expand Down
12 changes: 3 additions & 9 deletions test/map_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def setup
@generate_flora = false

@map_config = MapConfig.new(
perlin_height_config: @perlin_height_config,
perlin_moist_config: @perlin_moist_config,
perlin_temp_config: @perlin_temp_config,
all_perlin_configs: MapConfig::AllPerlinConfigs.new(@perlin_height_config, @perlin_moist_config, @perlin_temp_config),
width: @width,
height: @height,
generate_flora: @generate_flora
Expand All @@ -65,9 +63,7 @@ def test_initialize_with_invalid_perlin_height_config
invalid_perlin_height_config = :invalid
assert_raises(ArgumentError) do
MapConfig.new(
perlin_height_config: invalid_perlin_height_config,
perlin_moist_config: @perlin_moist_config,
perlin_temp_config: @perlin_temp_config
all_perlin_configs: MapConfig::AllPerlinConfigs.new(invalid_perlin_height_config, @perlin_moist_config, @perlin_temp_config)
)
end
end
Expand All @@ -76,9 +72,7 @@ def test_initialize_with_invalid_perlin_moist_config
invalid_perlin_moist_config = :invalid
assert_raises(ArgumentError) do
MapConfig.new(
perlin_height_config: @perlin_height_config,
perlin_moist_config: invalid_perlin_moist_config,
perlin_temp_config: @perlin_temp_config
all_perlin_configs: MapConfig::AllPerlinConfigs.new(@perlin_height_config, invalid_perlin_moist_config, @perlin_temp_config)
)
end
end
Expand Down
8 changes: 5 additions & 3 deletions test/pathfinding/a_star_finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class TestAStarFinder < Minitest::Test
class Node
attr_reader :x, :y

def initialize(x, y, can_contain_road = true)
# rubocop:disable Naming/MethodParameterName:
def initialize(x, y, can_contain_road: true)
@x = x
@y = y
@can_contain_road = can_contain_road
end
# rubocop:enable Naming/MethodParameterName:

def path_heuristic
0
Expand Down Expand Up @@ -100,8 +102,8 @@ def test_finds_path_with_obstacles

def test_finds_path_with_non_walkable_paths
nodes = [
[Node.new(0, 0), Node.new(1, 0), Node.new(2, 0, false), Node.new(3, 0)],
[Node.new(0, 1), Node.new(1, 1, false), Node.new(2, 1, false), Node.new(3, 1)],
[Node.new(0, 0), Node.new(1, 0), Node.new(2, 0, can_contain_road: false), Node.new(3, 0)],
[Node.new(0, 1), Node.new(1, 1, can_contain_road: false), Node.new(2, 1, can_contain_road: false), Node.new(3, 1)],
[Node.new(0, 2), Node.new(1, 2), Node.new(2, 2), Node.new(3, 2)],
[Node.new(0, 3), Node.new(1, 3), Node.new(2, 3), Node.new(3, 3)]
]
Expand Down
2 changes: 2 additions & 0 deletions test/pathfinding/grid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class Node
# Replace this with your Node class implementation or use a mock/fake Node class.
attr_reader :x, :y

# rubocop:disable Naming/MethodParameterName:
def initialize(x, y, road)
@x = x
@y = y
@road = road
end
# rubocop:enable Naming/MethodParameterName:

def can_contain_road?
@road
Expand Down

0 comments on commit 76ece5e

Please sign in to comment.