ledmon 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa51679f40d2362bf2da0a15423c8e4c1d429fbbd251712b34da77860763cbe2
4
- data.tar.gz: c3bb466c875167945f57679a5d5240817a2c242bf5decb6359a7cda7a9a678c4
3
+ metadata.gz: c43f33956f6f89bb1395db53322a618825b1ead5644db9dc15ae4059d6d25e47
4
+ data.tar.gz: b8e086c450a5b161f82678e637560d6dea5b2a55684b7192cbf26526cdb9cda0
5
5
  SHA512:
6
- metadata.gz: 82484fce2c6034c1a94fb01266145beac95e5e71ddda57857b1f60b539fb80c68390b5b492d74ed075525914dbe352e4c7fda4663b99bebe0c507dd04a599a56
7
- data.tar.gz: de76543c3ec225ec19bbc1e67fed1ffb819c040909ac82dc90d0694185e930ecc8980b34968471ca62cf6ba615567e45f7446713fd1ce78ddeb3fd6ca7300912
6
+ metadata.gz: 0ae6579842cc73b1222893a15d757154f3284d4e72a70f0e8fa0b132347943f21b0c05a0ee74b7f4db87a39b6faf0965c24cc8d38e9a67259ca33307fe494d41
7
+ data.tar.gz: c753136022675e48813c5f104273b7c1a39c5aca8ea9828b3a6199347d619ab974474d2830005bcf6823e29ba577ad7ae5101d0deb7159a5fc3ff2e6fbd7a7ba
data/DOCS.md CHANGED
@@ -119,6 +119,22 @@ if key == 'm'
119
119
  end
120
120
  ```
121
121
 
122
+ ### `path_to(x, y)`
123
+ Nájde, akým smerom máš ísť k zadanému cieľu. Táto metóda vidí iba mapu o veľkosti look-u, takže sa môže v prípade steny väčšej ako váš rozhľad príšerka nasledujúca túto metódu zaseknúť.
124
+
125
+ ```ruby
126
+ if key == 'b'
127
+ x = ask("x: ").to_i
128
+ y = ask("y: ").to_i
129
+ new_heading = path_to(x, y)
130
+ while !new_heading.nil?
131
+ turn_towards(new_heading)
132
+ move_forward
133
+ new_heading = path_to(x, y)
134
+ end
135
+ end
136
+ ```
137
+
122
138
  ## 👀 Rozhliadanie sa
123
139
 
124
140
  ### `look`
@@ -48,6 +48,77 @@ module Ledmon::Monster
48
48
  end
49
49
  end
50
50
 
51
+ def path_to(x, y)
52
+ return nil if get_position.x == x && get_position.y == y
53
+
54
+ tiles = look
55
+ look_distance = (tiles.size-1)/2
56
+
57
+ def mapWalkable(x, y, look_distance, tiles)
58
+ row = tiles[y-get_position.y+look_distance]
59
+ return nil if row.nil?
60
+ row[x-get_position.x+look_distance]&.walkable
61
+ end
62
+
63
+ def distance(a, b)
64
+ Math.sqrt((a[0].to_f-b[0].to_f)**2.0+(a[1].to_f-b[1].to_f)**2.0)
65
+ end
66
+ def tileId(pos)
67
+ pos[0] + 1000*pos[1]
68
+ end
69
+
70
+ start = [get_position.x, get_position.y]
71
+ stop = [x, y]
72
+ openset = [start]
73
+ gScore = []
74
+ fScore = []
75
+ cameFrom = {}
76
+ gScore[tileId(start)] = 0
77
+ fScore[tileId(start)] = distance(start, stop)
78
+
79
+ nextPosition = nil
80
+
81
+ while !openset.empty?
82
+ current = openset.min_by {|tile|fScore[tileId(tile)] || Float::INFINITY}
83
+ if current == stop || mapWalkable(current[0], current[1], look_distance, tiles).nil?
84
+ walk = current
85
+ while cameFrom[tileId(walk)] != start
86
+ walk = cameFrom[tileId(walk)]
87
+ end
88
+ nextPosition = walk
89
+ break
90
+ end
91
+ openset.delete(current)
92
+ for offset in [[1, 0], [-1, 0], [0, 1], [0, -1]]
93
+ neighbor = [current[0]+offset[0], current[1]+offset[1]]
94
+ walkable = mapWalkable(neighbor[0], neighbor[1], look_distance, tiles)
95
+ if walkable.nil? || walkable == true
96
+ tg = (gScore[tileId(current)] || Float::INFINITY) + distance(current, neighbor)
97
+ if tg < (gScore[tileId(neighbor)] || Float::INFINITY)
98
+ cameFrom[tileId(neighbor)] = current
99
+ gScore[tileId(neighbor)] = tg
100
+ fScore[tileId(neighbor)] = tg + distance(stop, neighbor)
101
+ unless openset.include?(neighbor)
102
+ openset << neighbor
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ return nil if nextPosition.nil?
110
+ case([nextPosition[0]-get_position.x, nextPosition[1]-get_position.y])
111
+ when [1, 0]
112
+ "east"
113
+ when [-1, 0]
114
+ "west"
115
+ when [0, 1]
116
+ "south"
117
+ when [0, -1]
118
+ "north"
119
+ end
120
+ end
121
+
51
122
  def update_movement(movement)
52
123
  @position = movement.position
53
124
  @heading = movement.heading
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ledmon
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.9"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ledmon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmed Al Hafoudh