maps4fs 2.1.1__py3-none-any.whl → 2.1.2__py3-none-any.whl

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.
@@ -11,8 +11,13 @@ from tqdm import tqdm
11
11
 
12
12
  from maps4fs.generator.component.base.component_image import ImageComponent
13
13
  from maps4fs.generator.component.base.component_xml import XMLComponent
14
+ from maps4fs.generator.component.layer import Layer
14
15
  from maps4fs.generator.settings import Parameters
15
16
 
17
+ # This value sums up the pixel value of the basic area type to convert it from "No Water" to "Near Water".
18
+ # For example, if the basic area type is "city" (1), then the pixel value for "near water" will be 9.
19
+ WATER_AREA_PIXEL_VALUE = 8
20
+
16
21
 
17
22
  def plant_to_pixel_value(plant_name: str) -> int | None:
18
23
  """Returns the pixel value representation of the plant.
@@ -31,6 +36,27 @@ def plant_to_pixel_value(plant_name: str) -> int | None:
31
36
  return plants.get(plant_name)
32
37
 
33
38
 
39
+ def area_type_to_pixel_value(area_type: str) -> int | None:
40
+ """Returns the pixel value representation of the area type.
41
+ If not found, returns None.
42
+
43
+ Arguments:
44
+ area_type (str): name of the area type
45
+
46
+ Returns:
47
+ int | None: pixel value of the area type or None if not found.
48
+ """
49
+ area_types = {
50
+ "open_land": 0,
51
+ "city": 1,
52
+ "village": 2,
53
+ "harbor": 3,
54
+ "industrial": 4,
55
+ "open_water": 5,
56
+ }
57
+ return area_types.get(area_type)
58
+
59
+
34
60
  class GRLE(ImageComponent, XMLComponent):
35
61
  """Component for to generate InfoLayer PNG files based on GRLE schema.
36
62
 
@@ -104,6 +130,9 @@ class GRLE(ImageComponent, XMLComponent):
104
130
  self._add_farmlands()
105
131
  if self.game.plants_processing and self.map.grle_settings.add_grass:
106
132
  self._add_plants()
133
+ if self.game.environment_processing:
134
+ self._process_environment()
135
+ self._process_indoor()
107
136
 
108
137
  def previews(self) -> list[str]:
109
138
  """Returns a list of paths to the preview images (empty list).
@@ -130,6 +159,9 @@ class GRLE(ImageComponent, XMLComponent):
130
159
  cv2.imwrite(save_path, image_colored)
131
160
  preview_paths.append(save_path)
132
161
 
162
+ if preview_name != "farmlands":
163
+ continue
164
+
133
165
  with_fields_save_path = os.path.join(
134
166
  self.previews_directory, f"{preview_name}_with_fields.png"
135
167
  )
@@ -462,3 +494,149 @@ class GRLE(ImageComponent, XMLComponent):
462
494
  image_np[:, 0] = 0 # Left side
463
495
  image_np[:, -1] = 0 # Right side
464
496
  return image_np
497
+
498
+ def _process_environment(self) -> None:
499
+ info_layer_environment_path = self.game.get_environment_path(self.map_directory)
500
+ if not info_layer_environment_path or not os.path.isfile(info_layer_environment_path):
501
+ self.logger.warning(
502
+ "Environment InfoLayer PNG file not found in %s.", info_layer_environment_path
503
+ )
504
+ return
505
+
506
+ self.logger.debug(
507
+ "Processing environment InfoLayer PNG file: %s.", info_layer_environment_path
508
+ )
509
+
510
+ environment_image = cv2.imread(info_layer_environment_path, cv2.IMREAD_UNCHANGED)
511
+ if environment_image is None:
512
+ self.logger.error("Failed to read the environment InfoLayer PNG file.")
513
+ return
514
+
515
+ self.logger.debug(
516
+ "Environment InfoLayer PNG file loaded, shape: %s.", environment_image.shape
517
+ )
518
+
519
+ environment_size = int(environment_image.shape[0])
520
+
521
+ # 1. Get the texture layers that contain "area_type" property.
522
+ # 2. Read the corresponding weight image (not the dissolved ones!).
523
+ # 3. Resize it to match the environment image size (probably 1/4 of the texture size).
524
+ # 4. Dilate the texture mask to make it little bit bigger.
525
+ # 5. Set the corresponding pixel values of the environment image to the pixel value of the area type.
526
+ # 6. Get the texture layer that "area_water" is True.
527
+ # 7. Same as resize, dilate, etc.
528
+ # 8. Sum the current pixel value with the WATER_AREA_PIXEL_VALUE.
529
+
530
+ texture_component = self.map.get_texture_component()
531
+ if not texture_component:
532
+ self.logger.warning("Texture component not found in the map.")
533
+ return
534
+
535
+ for layer in texture_component.get_area_type_layers():
536
+ pixel_value = area_type_to_pixel_value(layer.area_type) # type: ignore
537
+ weight_image = self.get_resized_weight(layer, environment_size) # type: ignore
538
+ if weight_image is None:
539
+ self.logger.warning("Weight image for area type layer not found in %s.", layer.name)
540
+ continue
541
+ environment_image[weight_image > 0] = pixel_value # type: ignore
542
+
543
+ for layer in texture_component.get_water_area_layers():
544
+ pixel_value = WATER_AREA_PIXEL_VALUE
545
+ weight_image = self.get_resized_weight(layer, environment_size)
546
+ if weight_image is None:
547
+ self.logger.warning(
548
+ "Weight image for water area layer not found in %s.", layer.name
549
+ )
550
+ continue
551
+ environment_image[weight_image > 0] += pixel_value # type: ignore
552
+
553
+ cv2.imwrite(info_layer_environment_path, environment_image)
554
+ self.logger.debug("Environment InfoLayer PNG file saved: %s.", info_layer_environment_path)
555
+ self.preview_paths["environment"] = info_layer_environment_path
556
+
557
+ def get_resized_weight(
558
+ self, layer: Layer, resize_to: int, dilations: int = 3
559
+ ) -> np.ndarray | None:
560
+ """Get the resized weight image for a given layer.
561
+
562
+ Arguments:
563
+ layer (Layer): The layer for which to get the weight image.
564
+ resize_to (int): The size to which the weight image should be resized.
565
+ dilations (int): The number of dilations to apply to the weight image.
566
+
567
+ Returns:
568
+ np.ndarray | None: The resized and dilated weight image, or None if the image could not be loaded.
569
+ """
570
+ weight_image_path = layer.get_preview_or_path(
571
+ self.game.weights_dir_path(self.map_directory)
572
+ )
573
+ self.logger.debug("Weight image path for area type layer: %s.", weight_image_path)
574
+
575
+ if not weight_image_path or not os.path.isfile(weight_image_path):
576
+ self.logger.warning(
577
+ "Weight image for area type layer not found in %s.", weight_image_path
578
+ )
579
+ return None
580
+
581
+ weight_image = cv2.imread(weight_image_path, cv2.IMREAD_UNCHANGED)
582
+ if weight_image is None:
583
+ self.logger.error("Failed to read the weight image for area type layer.")
584
+ return None
585
+
586
+ self.logger.debug("Weight image for area type layer loaded, shape: %s.", weight_image.shape)
587
+
588
+ # Resize the weight image to match the environment image size.
589
+ weight_image = cv2.resize(
590
+ weight_image,
591
+ (resize_to, resize_to),
592
+ interpolation=cv2.INTER_NEAREST,
593
+ )
594
+
595
+ self.logger.debug(
596
+ "Resized weight image for area type layer, new shape: %s.", weight_image.shape
597
+ )
598
+
599
+ if dilations <= 0:
600
+ return weight_image
601
+
602
+ dilated_weight_image = cv2.dilate(
603
+ weight_image.astype(np.uint8), np.ones((dilations, dilations), np.uint8), iterations=dilations # type: ignore
604
+ )
605
+
606
+ return dilated_weight_image
607
+
608
+ def _process_indoor(self) -> None:
609
+ """Processes the indoor layers."""
610
+ info_layer_indoor_path = self.game.get_indoor_mask_path(self.map_directory)
611
+ if not info_layer_indoor_path or not os.path.isfile(info_layer_indoor_path):
612
+ self.logger.warning(
613
+ "Indoor InfoLayer PNG file not found in %s.", info_layer_indoor_path
614
+ )
615
+ return
616
+
617
+ indoor_mask_image = cv2.imread(info_layer_indoor_path, cv2.IMREAD_UNCHANGED)
618
+ if indoor_mask_image is None:
619
+ self.logger.warning(
620
+ "Failed to read the indoor InfoLayer PNG file %s.", info_layer_indoor_path
621
+ )
622
+ return
623
+
624
+ indoor_mask_size = int(indoor_mask_image.shape[0])
625
+ self.logger.debug("Indoor InfoLayer PNG file loaded, shape: %s.", indoor_mask_image.shape)
626
+
627
+ texture_component = self.map.get_texture_component()
628
+ if not texture_component:
629
+ self.logger.warning("Texture component not found in the map.")
630
+ return
631
+
632
+ for layer in texture_component.get_indoor_layers():
633
+ weight_image = self.get_resized_weight(layer, indoor_mask_size, dilations=0)
634
+ if weight_image is None:
635
+ self.logger.warning("Weight image for indoor layer not found in %s.", layer.name)
636
+ continue
637
+
638
+ indoor_mask_image[weight_image > 0] = 1
639
+
640
+ cv2.imwrite(info_layer_indoor_path, indoor_mask_image)
641
+ self.logger.debug("Indoor InfoLayer PNG file saved: %s.", info_layer_indoor_path)
642
+ self.preview_paths["indoor"] = info_layer_indoor_path
@@ -46,6 +46,9 @@ class Layer:
46
46
  border: int | None = None,
47
47
  precise_tags: dict[str, str | list[str] | bool] | None = None,
48
48
  precise_usage: str | None = None,
49
+ area_type: str | None = None,
50
+ area_water: bool = False,
51
+ indoor: bool = False,
49
52
  ):
50
53
  self.name = name
51
54
  self.count = count
@@ -62,6 +65,9 @@ class Layer:
62
65
  self.border = border
63
66
  self.precise_tags = precise_tags
64
67
  self.precise_usage = precise_usage
68
+ self.area_type = area_type
69
+ self.area_water = area_water
70
+ self.indoor = indoor
65
71
 
66
72
  def to_json(self) -> dict[str, str | list[str] | bool]: # type: ignore
67
73
  """Returns dictionary with layer data.
@@ -84,6 +90,9 @@ class Layer:
84
90
  "border": self.border,
85
91
  "precise_tags": self.precise_tags,
86
92
  "precise_usage": self.precise_usage,
93
+ "area_type": self.area_type,
94
+ "area_water": self.area_water,
95
+ "indoor": self.indoor,
87
96
  }
88
97
 
89
98
  data = {k: v for k, v in data.items() if v is not None}
@@ -137,6 +137,36 @@ class Texture(ImageComponent):
137
137
  """
138
138
  return [layer for layer in self.layers if layer.usage == usage]
139
139
 
140
+ def get_area_type_layers(self, area_type: str | None = None) -> list[Layer]:
141
+ """Returns layers by area type. If area_type is None, returns all layers
142
+ with area_type set (not None).
143
+
144
+ Arguments:
145
+ area_type (str | None): Area type of the layer.
146
+
147
+ Returns:
148
+ list[Layer]: List of layers.
149
+ """
150
+ if area_type is None:
151
+ return [layer for layer in self.layers if layer.area_type is not None]
152
+ return [layer for layer in self.layers if layer.area_type == area_type]
153
+
154
+ def get_water_area_layers(self) -> list[Layer]:
155
+ """Returns layers which are water areas.
156
+
157
+ Returns:
158
+ list[Layer]: List of layers which are water areas.
159
+ """
160
+ return [layer for layer in self.layers if layer.area_water]
161
+
162
+ def get_indoor_layers(self) -> list[Layer]:
163
+ """Returns layers which are indoor areas.
164
+
165
+ Returns:
166
+ list[Layer]: List of layers which are indoor areas.
167
+ """
168
+ return [layer for layer in self.layers if layer.indoor]
169
+
140
170
  def process(self) -> None:
141
171
  """Processes the data to generate textures."""
142
172
  self._prepare_weights()
maps4fs/generator/game.py CHANGED
@@ -40,6 +40,7 @@ class Game:
40
40
  _tree_schema: str | None = None
41
41
  _i3d_processing: bool = True
42
42
  _plants_processing: bool = True
43
+ _environment_processing: bool = True
43
44
  _fog_processing: bool = True
44
45
  _dissolve: bool = True
45
46
 
@@ -180,6 +181,28 @@ class Game:
180
181
  weights_dir = self.weights_dir_path(map_directory)
181
182
  return os.path.join(weights_dir, "infoLayer_farmlands.png")
182
183
 
184
+ def get_environment_path(self, map_directory: str) -> str:
185
+ """Returns the path to the environment file.
186
+
187
+ Arguments:
188
+ map_directory (str): The path to the map directory.
189
+
190
+ Returns:
191
+ str: The path to the environment file."""
192
+ weights_dir = self.weights_dir_path(map_directory)
193
+ return os.path.join(weights_dir, "infoLayer_environment.png")
194
+
195
+ def get_indoor_mask_path(self, map_directory: str) -> str:
196
+ """Returns the path to the indoor mask file.
197
+
198
+ Arguments:
199
+ map_directory (str): The path to the map directory.
200
+
201
+ Returns:
202
+ str: The path to the indoor mask file."""
203
+ weights_dir = self.weights_dir_path(map_directory)
204
+ return os.path.join(weights_dir, "infoLayer_indoorMask.png")
205
+
183
206
  def get_farmlands_xml_path(self, map_directory: str) -> str:
184
207
  """Returns the path to the farmlands xml file.
185
208
 
@@ -218,6 +241,14 @@ class Game:
218
241
  bool: True if the i3d file should be processed, False otherwise."""
219
242
  return self._i3d_processing
220
243
 
244
+ @property
245
+ def environment_processing(self) -> bool:
246
+ """Returns whether the environment should be processed.
247
+
248
+ Returns:
249
+ bool: True if the environment should be processed, False otherwise."""
250
+ return self._environment_processing
251
+
221
252
  @property
222
253
  def fog_processing(self) -> bool:
223
254
  """Returns whether the fog should be processed.
@@ -269,6 +300,7 @@ class FS22(Game):
269
300
  _map_template_path = os.path.join(working_directory, "data", "fs22-map-template.zip")
270
301
  _texture_schema = os.path.join(working_directory, "data", "fs22-texture-schema.json")
271
302
  _i3d_processing = False
303
+ _environment_processing = False
272
304
  _fog_processing = False
273
305
  _plants_processing = False
274
306
  _dissolve = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maps4fs
3
- Version: 2.1.1
3
+ Version: 2.1.2
4
4
  Summary: Generate map templates for Farming Simulator from real places.
5
5
  Author-email: iwatkot <iwatkot@gmail.com>
6
6
  License: Apache License 2.0
@@ -546,6 +546,9 @@ Let's have a closer look at the fields:
546
546
  - `border` - this value defines the border between the texture and the edge of the map. It's used to prevent the texture from being drawn on the edge of the map. The value is in pixels.
547
547
  - `precise_tags` - can be used for more specific tags, for example instead of `"natural": "wood"` you can use `"leaf_type": "broadleaved"` to draw only broadleaved trees.
548
548
  - `precise_usage` - the same as `usage`, but being used with `precise_tags`.
549
+ - `area_type` - one of the supported by Giants Editor area types, such as: "open_land", "city", "village", "harbor", "industrial", "open_water". It will be reflected in the environment info layer file.
550
+ - `area_water` - whenever this field is set to true, the area will be considered as water, and it will be changed in the environment info layer file.
551
+ - `indoor` - whenever this field is set to true, the area will be considered as indoor, and it will be reflected in the indoorMask info layer.
549
552
 
550
553
  ## Background terrain
551
554
 
@@ -1,7 +1,7 @@
1
1
  maps4fs/__init__.py,sha256=Fy521EmVAWnhu6OvOInc97yrtJotFzcV0YfRB2b9O4s,314
2
2
  maps4fs/logger.py,sha256=WDfR14hxqy8b6xtwL6YIu2LGzFO1sbt0LxMgfsDTOkA,865
3
3
  maps4fs/generator/__init__.py,sha256=zZMLEkGzb4z0xql650gOtGSvcgX58DnJ2yN3vC2daRk,43
4
- maps4fs/generator/game.py,sha256=Nl8xqEwW-E04l2D3e9ff7kkzPcolr1ndnXxQJlLMBDM,12627
4
+ maps4fs/generator/game.py,sha256=Qqeh1rg0RFghhu9uhBMn_XKXYkTkg7fsjY1xO2eIZRw,13777
5
5
  maps4fs/generator/map.py,sha256=x_NjUwKkDnh2cC61QpTfX88mFHAkQG1sxVwZckoV0mE,16357
6
6
  maps4fs/generator/qgis.py,sha256=Es8hLuqN_KH8lDfnJE6He2rWYbAKJ3RGPn-o87S6CPI,6116
7
7
  maps4fs/generator/settings.py,sha256=OXgCjpmcqvTENRcKKYcEhES3GD3KPpvrbeJDv8g25yY,7676
@@ -10,18 +10,18 @@ maps4fs/generator/component/__init__.py,sha256=s01yVVVi8R2xxNvflu2D6wTd9I_g73AMM
10
10
  maps4fs/generator/component/background.py,sha256=wqTgndTfRIGlLMpMXqJMw_6eVdH1JtMei46ZIXlT9X4,29480
11
11
  maps4fs/generator/component/config.py,sha256=uL76h9UwyhZKZmbxz0mBmWtEPN6qYay4epTEqqtej60,8601
12
12
  maps4fs/generator/component/dem.py,sha256=SH_2Zu5O4dhWtZeOkCwzDF4RU04XhTdpGFYaRYJkdjc,11905
13
- maps4fs/generator/component/grle.py,sha256=8K32pC_ar9CR6p0EhCe2X--wEoIxFzJCPcN9ydHQ1LE,19747
13
+ maps4fs/generator/component/grle.py,sha256=3BKGlR0q0t0NvmqeT81WieS6MIc_UlMQjIDDZiqTiws,27243
14
14
  maps4fs/generator/component/i3d.py,sha256=L-QAbr3Z7Ye5N0BeS_qvY9bqYxYs0eVnRCGWp77etrE,26693
15
- maps4fs/generator/component/layer.py,sha256=-br4gAGcGeBNb3ldch9XFEK0lhXqb1IbArhFB5Owu54,6186
15
+ maps4fs/generator/component/layer.py,sha256=U_DzJTn1m_yGOtwuvbXxr7oL7YHHBGBcK37lyJNnZDk,6508
16
16
  maps4fs/generator/component/satellite.py,sha256=OsxoNOCgkUtRzL7Geuqubsf6uoKXAIN8jQvrJ7IFeAI,4958
17
- maps4fs/generator/component/texture.py,sha256=krtvOS0hH8BTzfxd2jsTo3bIJYRkIVbaz6FGhWY8L1o,33921
17
+ maps4fs/generator/component/texture.py,sha256=bpHBwUksEbwPQrdQ8K94m_MIKXnWYnUMvP987JVabAA,34987
18
18
  maps4fs/generator/component/base/__init__.py,sha256=zZMLEkGzb4z0xql650gOtGSvcgX58DnJ2yN3vC2daRk,43
19
19
  maps4fs/generator/component/base/component.py,sha256=Vgmdsn1ZC37EwWi4Va4uYVt0RnFLiARTtZ-R5GTSrrM,22877
20
20
  maps4fs/generator/component/base/component_image.py,sha256=2NYJgCU8deHl7O2FYFYk38WKZVJygFoc2gjBXwH6vjM,5970
21
21
  maps4fs/generator/component/base/component_mesh.py,sha256=_thzgjJDroMn-9SBsBmAWizcSsnV9U5445SD18Tx1kc,9090
22
22
  maps4fs/generator/component/base/component_xml.py,sha256=MT-VhU2dEckLFxAgmxg6V3gnv11di_94Qq6atfpOLdc,5342
23
- maps4fs-2.1.1.dist-info/licenses/LICENSE.md,sha256=pTKD_oUexcn-yccFCTrMeLkZy0ifLRa-VNcDLqLZaIw,10749
24
- maps4fs-2.1.1.dist-info/METADATA,sha256=uHY1Ap5uahWTaFPIAOulIBz754lJ7Ga4S4RJ6ZOAUpI,44934
25
- maps4fs-2.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
26
- maps4fs-2.1.1.dist-info/top_level.txt,sha256=Ue9DSRlejRQRCaJueB0uLcKrWwsEq9zezfv5dI5mV1M,8
27
- maps4fs-2.1.1.dist-info/RECORD,,
23
+ maps4fs-2.1.2.dist-info/licenses/LICENSE.md,sha256=pTKD_oUexcn-yccFCTrMeLkZy0ifLRa-VNcDLqLZaIw,10749
24
+ maps4fs-2.1.2.dist-info/METADATA,sha256=L6AJvi0CMTG_RfUP9isMXDYTnugeBPWQzWQwwy2NyTg,45431
25
+ maps4fs-2.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
26
+ maps4fs-2.1.2.dist-info/top_level.txt,sha256=Ue9DSRlejRQRCaJueB0uLcKrWwsEq9zezfv5dI5mV1M,8
27
+ maps4fs-2.1.2.dist-info/RECORD,,