test_interiors.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # coding: utf-8
  2. from synergine2_xyz.map import TMXMap
  3. from PIL import Image
  4. from opencombat.simulation.interior import InteriorManager
  5. def test_interior_zones__one_zone():
  6. map_ = TMXMap('tests/fixtures/one_interior.tmx')
  7. image = Image.open('tests/fixtures/complex_40x40.png')
  8. manager = InteriorManager(map_, original_image=image)
  9. interiors = manager.get_interiors()
  10. assert interiors
  11. assert isinstance(interiors, list)
  12. assert 1 == len(interiors)
  13. assert (1, 1) in interiors[0]
  14. assert (1, 2) in interiors[0]
  15. assert (1, 3) in interiors[0]
  16. assert (2, 1) in interiors[0]
  17. assert (2, 2) in interiors[0]
  18. assert (2, 3) in interiors[0]
  19. assert (3, 1) in interiors[0]
  20. assert (3, 2) in interiors[0]
  21. assert (3, 3) in interiors[0]
  22. assert 9 == len(interiors[0])
  23. def test_interior_zones__two_separated_zones():
  24. map_ = TMXMap('tests/fixtures/two_interiors.tmx')
  25. image = Image.open('tests/fixtures/complex_40x40.png')
  26. manager = InteriorManager(map_, original_image=image)
  27. interiors = sorted(manager.get_interiors())
  28. assert interiors
  29. assert isinstance(interiors, list)
  30. assert 2 == len(interiors)
  31. assert (0, 1) in interiors[0]
  32. assert (1, 1) in interiors[0]
  33. assert (0, 2) in interiors[0]
  34. assert (1, 2) in interiors[0]
  35. assert (0, 3) in interiors[0]
  36. assert (1, 3) in interiors[0]
  37. assert 6 == len(interiors[0])
  38. assert (3, 1) in interiors[1]
  39. assert (4, 1) in interiors[1]
  40. assert (3, 2) in interiors[1]
  41. assert (4, 2) in interiors[1]
  42. assert (3, 3) in interiors[1]
  43. assert (4, 3) in interiors[1]
  44. assert 6 == len(interiors[1])
  45. def test_interiors_zones__side_by_side_zones_with_separator():
  46. map_ = TMXMap('tests/fixtures/side_by_side_interiors.tmx')
  47. image = Image.open('tests/fixtures/complex_40x40.png')
  48. manager = InteriorManager(map_, original_image=image)
  49. interiors = sorted(manager.get_interiors())
  50. assert interiors
  51. assert isinstance(interiors, list)
  52. assert 2 == len(interiors)
  53. assert (0, 1) in interiors[0]
  54. assert (1, 1) in interiors[0]
  55. assert (0, 2) in interiors[0]
  56. assert (1, 2) in interiors[0]
  57. assert (0, 3) in interiors[0]
  58. assert (1, 3) in interiors[0]
  59. assert (2, 1) in interiors[0]
  60. assert (2, 2) in interiors[0]
  61. assert (2, 3) in interiors[0]
  62. assert 9 == len(interiors[0])
  63. assert (2, 1) in interiors[1]
  64. assert (2, 2) in interiors[1]
  65. assert (2, 3) in interiors[1]
  66. assert (3, 1) in interiors[1]
  67. assert (4, 1) in interiors[1]
  68. assert (3, 2) in interiors[1]
  69. assert (4, 2) in interiors[1]
  70. assert (3, 3) in interiors[1]
  71. assert (4, 3) in interiors[1]
  72. assert 9 == len(interiors[1])
  73. def test_interiors_zones__active_zones():
  74. # active zones are zone where someone is in
  75. map_ = TMXMap('tests/fixtures/two_interiors.tmx')
  76. image = Image.open('tests/fixtures/complex_40x40.png')
  77. manager = InteriorManager(map_, original_image=image)
  78. interiors = manager.get_interiors(where_positions=[(0, 1)])
  79. assert (0, 1) in interiors[0]
  80. assert (1, 1) in interiors[0]
  81. assert (0, 2) in interiors[0]
  82. assert (1, 2) in interiors[0]
  83. assert (0, 3) in interiors[0]
  84. assert (1, 3) in interiors[0]
  85. assert 6 == len(interiors[0])
  86. interiors = manager.get_interiors(where_positions=[(4, 2)])
  87. assert 1 == len(interiors)
  88. assert (3, 1) in interiors[0]
  89. assert (4, 1) in interiors[0]
  90. assert (3, 2) in interiors[0]
  91. assert (4, 2) in interiors[0]
  92. assert (3, 3) in interiors[0]
  93. assert (4, 3) in interiors[0]
  94. assert 6 == len(interiors[0])
  95. interiors = manager.get_interiors(where_positions=[(0, 1), (4, 2)])
  96. assert 2 == len(interiors)
  97. interiors = sorted(interiors)
  98. assert (0, 1) in interiors[0]
  99. assert (1, 1) in interiors[0]
  100. assert (0, 2) in interiors[0]
  101. assert (1, 2) in interiors[0]
  102. assert (0, 3) in interiors[0]
  103. assert (1, 3) in interiors[0]
  104. assert 6 == len(interiors[0])
  105. assert (3, 1) in interiors[1]
  106. assert (4, 1) in interiors[1]
  107. assert (3, 2) in interiors[1]
  108. assert (4, 2) in interiors[1]
  109. assert (3, 3) in interiors[1]
  110. assert (4, 3) in interiors[1]
  111. assert 6 == len(interiors[1])
  112. def test_interiors_zones__make_image_transparent__just_replace():
  113. map_ = TMXMap('tests/fixtures/one_interior.tmx')
  114. image = Image.open('tests/fixtures/white_40x40.png')
  115. manager = InteriorManager(map_, original_image=image)
  116. interiors = manager.get_interiors()
  117. after_image_bytes = Image.open('tests/fixtures/white_one_interior_40x40.png').tobytes()
  118. image = manager.update_image_for_interiors(interiors, 8, 8)
  119. assert after_image_bytes == image.tobytes()
  120. def test_interiors_zones__make_image_complex_transparent__just_replace():
  121. map_ = TMXMap('tests/fixtures/one_interior.tmx')
  122. image = Image.open('tests/fixtures/complex_40x40.png')
  123. manager = InteriorManager(map_, original_image=image)
  124. interiors = manager.get_interiors()
  125. after_image_bytes = Image.open('tests/fixtures/complex_one_interior_40x40.png').tobytes()
  126. image = manager.update_image_for_interiors(interiors, 8, 8)
  127. assert after_image_bytes == image.tobytes()
  128. def test_interiors_zones__make_image_corner_transparent__just_replace():
  129. map_ = TMXMap('tests/fixtures/corner_interior.tmx')
  130. image = Image.open('tests/fixtures/white_40x40.png')
  131. manager = InteriorManager(map_, original_image=image)
  132. interiors = manager.get_interiors()
  133. after_image_bytes = Image.open('tests/fixtures/white_corner_interior_40x40.png').tobytes()
  134. image = manager.update_image_for_interiors(interiors, 8, 8)
  135. assert after_image_bytes == image.tobytes()