Skip to content

Commit

Permalink
Merge branch '18ch'
Browse files Browse the repository at this point in the history
  • Loading branch information
ollybh committed Jun 26, 2024
2 parents c219e1a + 69ffc6a commit 25c1d66
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
6 changes: 4 additions & 2 deletions assets/app/view/game/hex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ def render
children << hex_highlight if @highlight

if (color = @tile&.stripes&.color)
Lib::Hex.stripe_points.each do |stripe|
stripes = Lib::Hex.stripe_points.map do |stripe|
attrs = {
fill: Lib::Hex::COLOR[color],
points: stripe,
}
children << h(:polygon, attrs: attrs)
h(:polygon, attrs: attrs)
end
attrs = @hex.layout == :flat ? { attrs: { transform: 'rotate(60)' } } : {}
children << h(:g, attrs, stripes)
end

if @tile
Expand Down
17 changes: 12 additions & 5 deletions lib/engine/game/g_1858/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative 'map'
require_relative 'entities'
require_relative 'market'
require_relative 'graph'
require_relative 'trains'
require_relative '../base'
require_relative '../stubs_are_restricted'
Expand Down Expand Up @@ -50,6 +51,8 @@ class Game < Game::Base
{ lay: true, upgrade: true, cost: 20, cannot_reuse_same_hex: true },
].freeze

GRAPH_CLASS = G1858::Graph

# These are the train types that determine the rusting timing of phase 4 trains.
GREY_TRAINS = %w[7E 6M 5D].freeze

Expand Down Expand Up @@ -91,8 +94,8 @@ def setup
# - @graph uses any track. This is going to include illegal routes
# (using both broad and metre gauge track) but will just be used
# by things like the auto-router where the route will get rejected.
@graph_broad = Graph.new(self, skip_track: :narrow, home_as_token: true)
@graph_metre = Graph.new(self, skip_track: :broad, home_as_token: true)
@graph_broad = self.class::GRAPH_CLASS.new(self, skip_track: :narrow, home_as_token: true)
@graph_metre = self.class::GRAPH_CLASS.new(self, skip_track: :broad, home_as_token: true)

# The rusting event for 6H/4M trains is triggered by the number of
# grey trains purchased, so track the number of these sold.
Expand Down Expand Up @@ -327,7 +330,11 @@ def home_token_locations(corporation)
end

# Returns true if the hex is this private railway's home hex.
def home_hex?(operator, hex)
# The gauge parameter is only used when this method is called from
# `corporation_private_connected?`. It is set to :broad or :narrow
# when testing whether this hex is part of the broad or narrow gauge
# graph. 1858 ignores the value of this parameter.
def home_hex?(operator, hex, _gauge = nil)
operator.coordinates.include?(hex.coordinates)
end

Expand Down Expand Up @@ -612,8 +619,8 @@ def corporation_private_connected?(corporation, minor)
return false if corporation.closed?
return false unless corporation.floated?

@graph_broad.reachable_hexes(corporation).any? { |hex, _| home_hex?(minor, hex) } ||
@graph_metre.reachable_hexes(corporation).any? { |hex, _| home_hex?(minor, hex) } ||
@graph_broad.reachable_hexes(corporation).any? { |hex, _| home_hex?(minor, hex, :broad) } ||
@graph_metre.reachable_hexes(corporation).any? { |hex, _| home_hex?(minor, hex, :narrow) } ||
corporation.placed_tokens.any? { |token| home_hex?(minor, token.city.hex) }
end

Expand Down
20 changes: 18 additions & 2 deletions lib/engine/game/g_1858/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,34 @@ def home_hex_nodes(entity)
cities = Array(entity.city)
hexes.zip(cities).each do |hex, city_idx|
if city_idx
nodes[hex.tile.cities[city_idx]] = true
cities = hex.tile.cities
# Need to handle the case where multiple cities on a hex have
# merged into a single city, but a private railway that started
# in city #1 or #2 still exists.
city = city_idx < cities.size ? cities[city_idx] : cities.first
nodes[city] = true
elsif hex.tile.city_towns.empty?
# Plain track in a home hex (or no tile or track). Create a
# node for each track path to allow routes to be traced out
# from this hex.
hex.tile.paths.each { |path| nodes[G1858::Part::PathNode.new(path)] = true }
hex.tile.paths.each do |path|
node = path_node(path, entity)
next unless node

nodes[node] = true
end
else
hex.tile.city_towns.each { |ct| nodes[ct] = true }
end
end
nodes
end

private

def path_node(path, _entity)
G1858::Part::PathNode.new(path)
end
end
end
end
Expand Down
29 changes: 28 additions & 1 deletion lib/engine/game/g_18_ch/game.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'entities'
require_relative 'graph'
require_relative 'map'
require_relative 'meta'
require_relative 'tiles'
Expand All @@ -15,6 +16,7 @@ class Game < G1858::Game
include Map
include Tiles

GRAPH_CLASS = G18CH::Graph
CURRENCY_FORMAT_STR = '%ssfr'

BANK_CASH = 8_000
Expand Down Expand Up @@ -199,7 +201,7 @@ def mountain_bonus(entity, routes)
def all_potential_upgrades(tile, tile_manifest: false, selected_company: nil)
return super unless mountain_hex?(tile)

super + [@tiles.find { |t| t.name == MOUNTAIN_RAILWAY_TILE }]
super + Array(@tiles.find { |t| t.name == MOUNTAIN_RAILWAY_TILE })
end

def upgrades_to?(from, to, special, selected_company: nil)
Expand All @@ -221,6 +223,31 @@ def after_lay_tile(_hex, tile, entity)
# there are two players. This does not happen in 18CH.
def setup_unbuyable_privates; end

def gotthard
@gotthard ||= hex_by_id('H11')
end

def fob_minor
@fob_minor ||= minor_by_id('FOB')
end

def gb_minor
@gb_minor ||= minor_by_id('GB')
end

def home_hex?(operator, hex, gauge = nil)
home_hex = super
return home_hex if !home_hex || hex != gotthard

# Gotthard (H11) is different from other hexes. A public company
# doesn't just to have to have a connection to anywhere on the hex to
# be able to absorb a private railway. To absorb the GB private it
# needs to have a connection to the broad gauge track, to absorb the
# FOB it needs to have a connection to the metre (narrow) gauge track.
(operator == fob_minor && gauge == :narrow) ||
(operator == gb_minor && gauge == :broad)
end

private

def hexes_by_id(coordinates)
Expand Down
26 changes: 26 additions & 0 deletions lib/engine/game/g_18_ch/graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative '../g_1858/graph'

module Engine
module Game
module G18CH
class Graph < G1858::Graph
private

def path_node(path, entity)
node = G1858::Part::PathNode.new(path)
return node unless path.hex == @game.gotthard

# The pre-printed Gotthard hex is a special case. It is a home hex
# for two private railways (FOB and GB) and it has two paths, one
# broad gauge and one metre (narrow) gauge. FOB is only allowed to
# trace routes from this hex using the metre gauge path, and GB can
# only use the broad gauge path.
return node if (path.track == :broad && entity == @game.gb_minor) ||
(path.track == :narrow && entity == @game.fob_minor)
end
end
end
end
end
12 changes: 10 additions & 2 deletions public/icons/18_ch/mountain.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 25c1d66

Please sign in to comment.