map_overlay.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from itertools import product
  2. import numpy as np
  3. from app.verkolst import verkolst
  4. from app.colormap.colormap import apply_colormap
  5. from app.trade.trade_utils import trade_distance
  6. class Overlay:
  7. def __init__(self, ui, hexmap_layer, opacity=120):
  8. self._ui = ui
  9. self._overlay = hexmap_layer
  10. self._enabled = False
  11. self._opacity = opacity
  12. def enable(self):
  13. self._enabled = True
  14. self.update()
  15. def disable(self):
  16. self._enabled = False
  17. def toggle(self):
  18. if self._enabled:
  19. self.disable()
  20. else:
  21. self.enable()
  22. @property
  23. def enabled(self):
  24. return self._enabled
  25. def _recalculate(self):
  26. pass
  27. def update(self):
  28. if not self._enabled:
  29. return
  30. self._recalculate()
  31. for i, j in product(range(verkolst.width), range(verkolst.height)):
  32. self._overlay.set_cell_color(i, j, self.get_cell_color(i, j))
  33. self._overlay.set_cell_opacity(i, j, self.get_cell_opacity(i, j))
  34. def get_cell_color(self, i, j):
  35. raise NotImplementedError()
  36. def get_cell_opacity(self, i, j):
  37. return self._opacity
  38. class FoodOverlay(Overlay):
  39. def __init__(self, ui, hexmap_layer):
  40. super().__init__(ui, hexmap_layer, opacity=200)
  41. self._colors = None
  42. def _recalculate(self):
  43. self._colors = apply_colormap(verkolst.resources.food_stored/verkolst.resources.food_stored_capacity,
  44. 'magma', min_=0)
  45. def get_cell_color(self, i, j):
  46. # res = verkolst.resources
  47. if verkolst.resources.any_starvation[i, j]:
  48. return 255, 0, 0 # Red
  49. else:
  50. return self._colors[i, j]
  51. class PopsOverlay(Overlay):
  52. def __init__(self, ui, hexmap_layer, class_):
  53. super().__init__(ui, hexmap_layer, opacity=240)
  54. self._class = class_
  55. self._colors = None
  56. def _recalculate(self):
  57. pops = {
  58. 'lc': verkolst.resources.lc_pops,
  59. 'mc': verkolst.resources.mc_pops,
  60. 'uc': verkolst.resources.uc_pops,
  61. }[self._class]
  62. self._colors = apply_colormap(pops, 'magma', min_=0)
  63. def get_cell_color(self, i, j):
  64. return self._colors[i, j]
  65. class TradeDistanceOverlay(Overlay):
  66. def __init__(self, ui, hexmap_layer):
  67. super().__init__(ui, hexmap_layer, opacity=180)
  68. self._colors = None
  69. self.trade_distance = np.full_like(verkolst.resources.lc_pops, 0)
  70. self.exp_partner = None
  71. def _recalculate(self):
  72. if self._ui.hl_province is not None:
  73. i, j = self._ui.hl_province.i, self._ui.hl_province.j
  74. self.exp_partner, self.trade_distance = trade_distance(i, j,
  75. verkolst.resources.trade_range[i, j],
  76. verkolst.resources.trade_impedance,
  77. verkolst.resources.trade_value)
  78. else:
  79. self.trade_distance[:, :] = 0.0
  80. self._colors = apply_colormap(self.trade_distance, 'viridis', min_=0)
  81. def get_cell_color(self, i, j):
  82. if self._ui.hl_province is None:
  83. return 0, 0, 0
  84. elif (i, j) == self.exp_partner:
  85. return 255, 204, 0 # Gold
  86. else:
  87. return self._colors[i, j]
  88. def get_cell_opacity(self, i, j):
  89. if self._ui.hl_province is None:
  90. return 0
  91. elif self.trade_distance[i, j] < 0: # out of range
  92. return 0
  93. elif (i, j) == self.exp_partner:
  94. return 255
  95. else:
  96. return self._opacity