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 +4 -4
- data/DOCS.md +16 -0
- data/lib/ledmon/monster/movement.rb +71 -0
- data/lib/ledmon/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c43f33956f6f89bb1395db53322a618825b1ead5644db9dc15ae4059d6d25e47
|
4
|
+
data.tar.gz: b8e086c450a5b161f82678e637560d6dea5b2a55684b7192cbf26526cdb9cda0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/ledmon/version.rb
CHANGED