Class: Worldgen::PlateMap::Plate

Inherits:
Object
  • Object
show all
Defined in:
lib/worldgen/platemap.rb

Overview

A class representing a single plate. TODO: using Ruby for this is kinda slow, maybe port a good chunk of this stuff to C

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Attributes:

  • seed - the initial point within the map for this plate

  • map - the map that this plate is in

  • id - the ID number of this plate (how this plate identifies itself)

  • type - the type of plate (continental vs. ocean)



13
14
15
# File 'lib/worldgen/platemap.rb', line 13

def id
  @id
end

#mapObject

Attributes:

  • seed - the initial point within the map for this plate

  • map - the map that this plate is in

  • id - the ID number of this plate (how this plate identifies itself)

  • type - the type of plate (continental vs. ocean)



13
14
15
# File 'lib/worldgen/platemap.rb', line 13

def map
  @map
end

#seedObject

Attributes:

  • seed - the initial point within the map for this plate

  • map - the map that this plate is in

  • id - the ID number of this plate (how this plate identifies itself)

  • type - the type of plate (continental vs. ocean)



13
14
15
# File 'lib/worldgen/platemap.rb', line 13

def seed
  @seed
end

#typeObject

Attributes:

  • seed - the initial point within the map for this plate

  • map - the map that this plate is in

  • id - the ID number of this plate (how this plate identifies itself)

  • type - the type of plate (continental vs. ocean)



13
14
15
# File 'lib/worldgen/platemap.rb', line 13

def type
  @type
end

Instance Method Details

#absorb_frontier!Object

Absorb a single empty point along the frontier



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/worldgen/platemap.rb', line 32

def absorb_frontier!
  # Shuffle the frontier so that we end up grabbing a random point
  @frontier.shuffle!
  value = nil

  # It's possible another plate has absorbed part of our frontier since
  # the last time we set our frontier, so shift until we find an empty
  # spot
  while value == nil and @frontier.length > 0
    value = @frontier.shift
    value = nil unless at(*value) < 0
  end

  if value
    # move it into me
    @map[value[0]][value[1]] = @id

    # add new points onto my frontier
    @frontier += empty_neighbours(value)
  end

  value
end

#empty_neighbours(point) ⇒ Object

Get the empty neighbours around ‘point`



57
58
59
60
61
# File 'lib/worldgen/platemap.rb', line 57

def empty_neighbours point
  neighbours(point).select do |(x, y)|
    at(x, y) < 0
  end
end

#frontier_lengthObject

Get the length of this plate’s frontier



27
28
29
# File 'lib/worldgen/platemap.rb', line 27

def frontier_length
  @frontier ? @frontier.length : 0
end

#has_frontier?Boolean

See if this plate has a frontier or not

Returns:

  • (Boolean)


22
23
24
# File 'lib/worldgen/platemap.rb', line 22

def has_frontier?
  @frontier and @frontier.length > 0
end

#neighbours(point) ⇒ Object

Get the neighbours of ‘point` - directly adjacent only, no diagonals



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/worldgen/platemap.rb', line 64

def neighbours point
  [
    [-1, 0],
    [1, 0],
    [0, 1],
    [0, -1]
  ].map do |(dx, dy)|
    [
      (point[0] + dx + @map.length) % @map.length,
      (point[1] + dy + @map.length) % @map.length
    ]
  end
end