-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyser_factorio_coordinate_wrapper.py
305 lines (272 loc) · 13.5 KB
/
analyser_factorio_coordinate_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from __future__ import annotations
import math
from typing import Optional
import analyser
class OrePatchFactorioCoordinateWrapper:
def __init__(self, ore_patch: analyser.OrePatch, tiles_per_pixel: int):
self.wrapped_ore_patch = ore_patch
self._tiles_per_pixel = tiles_per_pixel
@property
def size(self) -> int:
"""Return the size of an ore patch in Factorio tiles"""
return self.wrapped_ore_patch.size * self._tiles_per_pixel * self._tiles_per_pixel
@property
def resource_type(self) -> str: #
"""Return the resource type of an ore patch"""
return self.wrapped_ore_patch.resource_type
@property
def center_point(self) -> tuple[float, float]:
"""Return the weighted center of an ore patch in Factorio coordinates"""
# get weighted center in pixel coordinates
x_px, y_px = self.wrapped_ore_patch.center_point
# convert pixel to Factorio coordinates
min_x_px = -self.wrapped_ore_patch.resource_array.shape[1] // 2
min_y_px = -self.wrapped_ore_patch.resource_array.shape[0] // 2
x = (x_px + min_x_px) * self._tiles_per_pixel
y = (y_px + min_y_px) * self._tiles_per_pixel
return x, y
def display(self) -> None: #
"""This will open the image of the ore patch in your default image viewer. Very slow. Use for debug only"""
self.wrapped_ore_patch.display()
def __lt__(self, other):
return self.wrapped_ore_patch.size < other.wrapped_ore_patch.size
def __le__(self, other):
return self.wrapped_ore_patch.size <= other.wrapped_ore_patch.size
def __gt__(self, other):
return self.wrapped_ore_patch.size > other.wrapped_ore_patch.size
def __ge__(self, other):
return self.wrapped_ore_patch.size >= other.wrapped_ore_patch.size
class MapAnalyserFactorioCoordinateWrapper:
def __init__(self, map_analyser: analyser.MapAnalyser, tiles_per_pixel: int):
self.wrapped_map_analyser = map_analyser
self._tiles_per_pixel = tiles_per_pixel
self._tiles_per_pixel_sq = tiles_per_pixel * tiles_per_pixel
__max_x_px = self.wrapped_map_analyser.dimensions[1]
__max_y_px = self.wrapped_map_analyser.dimensions[0]
self._min_x = (-__max_x_px // 2) * tiles_per_pixel
self._min_y = (-__max_y_px // 2) * tiles_per_pixel
self._max_x = (__max_x_px // 2) * tiles_per_pixel
self._max_y = (__max_y_px // 2) * tiles_per_pixel
self._ore_patches: Optional[dict[str, list[OrePatchFactorioCoordinateWrapper]]] = None # lazy initialization
self._ore_patch_combined: Optional[dict[str, OrePatchFactorioCoordinateWrapper]] = None # lazy initialization
@property
def min_x(self) -> int:
"""Return the minimum x value of the image in Factorio coordinates"""
return self._min_x
@property
def min_y(self) -> int:
"""Return the minimum y value of the image in Factorio coordinates"""
return self._min_y
@property
def max_x(self) -> int:
"""Return the maximum x value of the image in Factorio coordinates"""
return self._max_x
@property
def max_y(self) -> int:
"""Return the maximum y value of the image in Factorio coordinates"""
return self._max_y
@property
def map_seed(self) -> str:
"""Return the name of file that is being analysed without extension. This is usually the map seed"""
return self.wrapped_map_analyser.map_seed
@property
def resource_types(self) -> list[str]:
"""Return a list of all resource types that can be analysed. Does not include 'all'"""
return self.wrapped_map_analyser.resource_types.copy()
@property
def ore_patches(self) -> dict[str, list[OrePatchFactorioCoordinateWrapper]]:
"""Return a Dictionary containing lists of patches for each resource type
example usages:
my_map_analyser.ore_patches['coal'] # return list of all coal patches
my_map_analyser.ore_patches['all'] # return list of ALL patches regardless of resource type
"""
if self._ore_patches is None: # lazy initialization
ore_patches = self.wrapped_map_analyser.ore_patches
# replace ore_patches with their ore_patch_coordinate_wrapper
ore_patches_with_wrapper_dict = dict.fromkeys(ore_patches)
for resource_type in ore_patches_with_wrapper_dict:
ore_patches_with_wrapper_dict[resource_type] = [
elem.ore_patch_coordinate_wrapper for elem in ore_patches[resource_type]
]
self._ore_patches = ore_patches_with_wrapper_dict
return self._ore_patches
@property
def ore_patch_combined(self) -> dict[str, OrePatchFactorioCoordinateWrapper]:
"""Return a dictionary containing each resource type as a single combined patch
example usages:
my_map_analyser.ore_patch_combined['coal'] # return all coal in only one patch as if it was a single one
"""
if self._ore_patch_combined is None: # lazy initialization
ore_patch_combined = self.wrapped_map_analyser.ore_patch_combined
# replace ore_patches with their ore_patch_coordinate_wrapper
ore_patch_combined_with_wrapper_dict = dict.fromkeys(ore_patch_combined)
for resource_type in ore_patch_combined_with_wrapper_dict:
ore_patch_combined_with_wrapper_dict[resource_type] = ore_patch_combined[
resource_type
].ore_patch_coordinate_wrapper
self._ore_patch_combined = ore_patch_combined_with_wrapper_dict
return self._ore_patch_combined
def is_in_bounds_x(self, x: int) -> bool:
"""Checks if the x value of a Factorio coordinate is withing the bounds of the image"""
return self.min_x <= x <= self.max_x
def is_in_bounds_y(self, y: int) -> bool:
"""Checks if the y value of a Factorio coordinate is withing the bounds of the image"""
return self.min_y <= y <= self.max_y
def is_in_bounds_point(self, point: tuple[int, int]) -> bool:
"""Checks if a Factorio coordinate is withing the bounds of the image"""
return self.is_in_bounds_x(point[0]) and self.is_in_bounds_y(point[1])
def count_resources_in_region(self, resource_type: str, start_x: int, start_y: int, end_x: int, end_y: int) -> int:
"""Return the amount of a given resource in the specified region in Factorio tiles"""
# convert Factorio coordinates to pixel - makes region larger, if inputs don't align
start_x_px, start_y_px, end_x_px, end_y_px = self._coordinate_region_to_pixel_region(
start_x, start_y, end_x, end_y
)
# call parent and convert area in square pixels to Factorio tiles
area_px = self.wrapped_map_analyser.count_resources_in_region(
resource_type, start_x_px, start_y_px, end_x_px, end_y_px
)
return self._tiles_per_pixel_sq * area_px
def get_ore_patches_partially_in_region(
self,
start_x: int,
start_y: int,
end_x: int,
end_y: int,
) -> dict[str, list[OrePatchFactorioCoordinateWrapper]]:
"""Return a dictionary containing lists of patches that are partially in a region for each resource type"""
# convert Factorio coordinates to pixel - makes region larger, if inputs don't align
start_x_px, start_y_px, end_x_px, end_y_px = self._coordinate_region_to_pixel_region(
start_x, start_y, end_x, end_y
)
ore_patches = self.wrapped_map_analyser.get_ore_patches_partially_in_region(
start_x_px, start_y_px, end_x_px, end_y_px
)
# replace ore_patches with their ore_patch_coordinate_wrapper
ore_patches_with_wrapper_dict = dict.fromkeys(ore_patches)
for resource_type in ore_patches_with_wrapper_dict:
ore_patches_with_wrapper_dict[resource_type] = [
elem.ore_patch_coordinate_wrapper for elem in ore_patches[resource_type]
]
return ore_patches_with_wrapper_dict
def find_longest_consecutive_line_of_resources_in_region(
self,
resource_type: str,
thickness: int,
tolerance: int = 0,
start_x: int = None,
start_y: int = None,
end_x: int = None,
end_y: int = None,
) -> tuple[int, Optional[tuple[int, int, int, int]]]:
"""Return the largest region of consecutive resources regarding a set width and its length in Factorio coords
Return (0, None) if nothing is found
param thickness: The width of the region
param tolerance: How many tiles of the given resource the region can miss"""
if start_x is None:
start_x = self.min_x
if start_y is None:
start_y = self.min_y
if end_x is None:
end_x = self.max_x
if end_y is None:
end_y = self.max_y
# convert Factorio coordinates to pixel - makes region larger, if inputs don't align
start_x_px, start_y_px, end_x_px, end_y_px = self._coordinate_region_to_pixel_region(
start_x, start_y, end_x, end_y
)
# call parent with conversions to pixel
max_length, region = self.wrapped_map_analyser.find_longest_consecutive_line_of_resources_in_region(
resource_type,
math.ceil(thickness / self._tiles_per_pixel),
math.ceil(tolerance / self._tiles_per_pixel_sq),
start_x_px,
start_y_px,
end_x_px,
end_y_px,
)
# convert back to Factorio tiles
if region is None:
return 0, None
return (
max_length * self._tiles_per_pixel,
self._pixel_region_to_coordinate_region(region[0], region[1], region[2], region[3]),
)
def calculate_min_distance_between_patches(
self, ore_patch: OrePatchFactorioCoordinateWrapper, other_ore_patch: OrePatchFactorioCoordinateWrapper
) -> float:
"""Return the distance between two ore patches in Factorio tiles"""
# call parent and convert distance in pixels to Factorio tiles
return (
analyser.MapAnalyser.calculate_min_distance_between_patches(
ore_patch.wrapped_ore_patch, other_ore_patch.wrapped_ore_patch
)
* self._tiles_per_pixel
)
def calculate_min_distance_between_patches_within_region(
self,
ore_patch: OrePatchFactorioCoordinateWrapper,
other_ore_patch: OrePatchFactorioCoordinateWrapper,
start_x: int,
start_y: int,
end_x: int,
end_y: int,
) -> float:
"""Return the distance between two ore patches in Factorio tiles within the specified region
This can be useful when very large patches have several points close to each other, but
one is only interested in the closest point within the starting area."""
# convert Factorio coordinates to pixel - makes region larger, if inputs don't align
start_x_px, start_y_px, end_x_px, end_y_px = self._coordinate_region_to_pixel_region(
start_x, start_y, end_x, end_y
)
# call parent and convert distance in pixels to Factorio tiles
return (
analyser.MapAnalyser.calculate_min_distance_between_patches_within_region(
ore_patch.wrapped_ore_patch,
other_ore_patch.wrapped_ore_patch,
start_x_px,
start_y_px,
end_x_px,
end_y_px,
)
* self._tiles_per_pixel
)
def _coordinate_to_pixel(self, point: tuple[int, int], round_up: bool = False) -> tuple[int, int]:
"""Converts Factorio coordinates to an image point in pixel"""
if round_up:
point = ( # This takes advantage of negative int rounding: 3 // 2 = 1, but -(-3 // 2) = 2
-((-point[0] + self.min_x) // self._tiles_per_pixel),
-((-point[1] + self.min_y) // self._tiles_per_pixel),
)
else: # round down
point = ((point[0] - self.min_x) // self._tiles_per_pixel, (point[1] - self.min_y) // self._tiles_per_pixel)
return point
def _coordinate_region_to_pixel_region(
self, start_x: int, start_y: int, end_x: int, end_y: int
) -> tuple[int, int, int, int]:
"""Converts a region of Factorio coordinates to a region of image points in pixel
makes the region larger, if inputs don't align"""
tiles_per_pixel = self._tiles_per_pixel # cheaper referencing
min_x = self.min_x # cheaper referencing
min_y = self.min_y # cheaper referencing
return (
# round start down
(start_x - min_x) // tiles_per_pixel,
(start_y - min_y) // tiles_per_pixel,
# round end up
# This takes advantage of negative int rounding: 3 // 2 = 1, but -(-3 // 2) = 2
-((-end_x + min_x) // tiles_per_pixel),
-((-end_y + min_y) // tiles_per_pixel),
)
def _pixel_region_to_coordinate_region(
self, start_x: int, start_y: int, end_x: int, end_y: int
) -> tuple[int, int, int, int]:
"""Converts a region of image points in pixel to a region of Factorio coordinates"""
tiles_per_pixel = self._tiles_per_pixel # cheaper referencing
min_x = self.min_x # cheaper referencing
min_y = self.min_y # cheaper referencing
return (
start_x * tiles_per_pixel + min_x,
start_y * tiles_per_pixel + min_y,
end_x * tiles_per_pixel + min_x,
end_y * tiles_per_pixel + min_y,
)