Module: Ledmon::Monster::Movement

Extended by:
ActiveSupport::Concern
Included in:
Executor
Defined in:
lib/ledmon/monster/movement.rb

Instance Method Summary collapse

Instance Method Details

#distance(a, b) ⇒ Object



63
64
65
# File 'lib/ledmon/monster/movement.rb', line 63

def distance(a, b)
  Math.sqrt((a[0].to_f-b[0].to_f)**2.0+(a[1].to_f-b[1].to_f)**2.0)
end

#get_headingObject



130
131
132
# File 'lib/ledmon/monster/movement.rb', line 130

def get_heading
  @heading
end

#get_positionObject



127
128
129
# File 'lib/ledmon/monster/movement.rb', line 127

def get_position
  @position
end

#mapWalkable(x, y, look_distance, tiles) ⇒ Object



57
58
59
60
61
# File 'lib/ledmon/monster/movement.rb', line 57

def mapWalkable(x, y, look_distance, tiles)
  row = tiles[y-get_position.y+look_distance]
  return nil if row.nil?
  row[x-get_position.x+look_distance]&.walkable
end

#move_backwardObject



11
12
13
14
15
# File 'lib/ledmon/monster/movement.rb', line 11

def move_backward
  res = client.move_backward(Ledmon::Empty.new)
  update_movement(res)
  res
end

#move_forwardObject



5
6
7
8
9
# File 'lib/ledmon/monster/movement.rb', line 5

def move_forward
  res = client.move_forward(Ledmon::Empty.new)
  update_movement(res)
  res
end

#path_to(x, y) ⇒ Object



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
115
116
117
118
119
120
# File 'lib/ledmon/monster/movement.rb', line 51

def path_to(x, y)
  return nil if get_position.x == x && get_position.y == y

  tiles = look
  look_distance = (tiles.size-1)/2

  def mapWalkable(x, y, look_distance, tiles)
    row = tiles[y-get_position.y+look_distance]
    return nil if row.nil?
    row[x-get_position.x+look_distance]&.walkable
  end

  def distance(a, b)
    Math.sqrt((a[0].to_f-b[0].to_f)**2.0+(a[1].to_f-b[1].to_f)**2.0)
  end
  def tileId(pos)
    pos[0] + 1000*pos[1]
  end

  start = [get_position.x, get_position.y]
  stop = [x, y]
  openset = [start]
  gScore = []
  fScore = []
  cameFrom = {}
  gScore[tileId(start)] = 0
  fScore[tileId(start)] = distance(start, stop)

  nextPosition = nil

  while !openset.empty?
    current = openset.min_by {|tile|fScore[tileId(tile)] || Float::INFINITY}
    if current == stop || mapWalkable(current[0], current[1], look_distance, tiles).nil?
      walk = current
      while cameFrom[tileId(walk)] != start
        walk = cameFrom[tileId(walk)]
      end
      nextPosition = walk
      break
    end
    openset.delete(current)
    for offset in [[1, 0], [-1, 0], [0, 1], [0, -1]]
      neighbor = [current[0]+offset[0], current[1]+offset[1]]
      walkable = mapWalkable(neighbor[0], neighbor[1], look_distance, tiles)
      if walkable.nil? || walkable == true
        tg = (gScore[tileId(current)]  || Float::INFINITY) + distance(current, neighbor)
        if tg < (gScore[tileId(neighbor)] || Float::INFINITY)
          cameFrom[tileId(neighbor)] = current
          gScore[tileId(neighbor)] = tg
          fScore[tileId(neighbor)] = tg + distance(stop, neighbor)
          unless openset.include?(neighbor)
            openset << neighbor
          end
        end
      end
    end
  end

  return nil if nextPosition.nil?
  case([nextPosition[0]-get_position.x, nextPosition[1]-get_position.y])
  when [1, 0]
    "east"
  when [-1, 0]
    "west"
  when [0, 1]
    "south"
  when [0, -1]
    "north"
  end
end

#tileId(pos) ⇒ Object



66
67
68
# File 'lib/ledmon/monster/movement.rb', line 66

def tileId(pos)
  pos[0] + 1000*pos[1]
end

#turn_leftObject



17
18
19
20
21
# File 'lib/ledmon/monster/movement.rb', line 17

def turn_left
  res = client.turn_left(Ledmon::Empty.new)
  update_movement(res)
  res
end

#turn_rightObject



23
24
25
26
27
# File 'lib/ledmon/monster/movement.rb', line 23

def turn_right
  res = client.turn_right(Ledmon::Empty.new)
  update_movement(res)
  res
end

#turn_towards(new_heading) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ledmon/monster/movement.rb', line 29

def turn_towards(new_heading)
  return if new_heading == get_heading

  left = case get_heading
  when "north"
    "west"
  when "east"
    "north"
  when "south"
    "east"
  when "west"
    "south"
  end

  if new_heading==left
    turn_left
  else
    turn_right
    turn_towards(new_heading)
  end
end

#update_movement(movement) ⇒ Object



122
123
124
125
# File 'lib/ledmon/monster/movement.rb', line 122

def update_movement(movement)
  @position = movement.position
  @heading = movement.heading
end