Debian Codegraph @

tests/swig_tests/location_tests.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # ####################################################################
5 # Copyright (C) 2005-2019 by the FIFE team
6 # http://www.fifengine.net
7 # This file is part of FIFE.
8 #
9 # FIFE is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13 #
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the
21 # Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # ####################################################################
24
25 from .swig_test_utils import *
26
27 P = fife.ModelCoordinate
28 D = fife.ExactModelCoordinate
29
30 NEAR_TRESHOLD = 0.001
31
32
33 def is_near(x, y):
34 return (y - NEAR_TRESHOLD) < x < (y + NEAR_TRESHOLD)
35
36
37 def pt_is_near(p1, p2):
38 return ((p2.x - NEAR_TRESHOLD) < p1.x < (p2.x + NEAR_TRESHOLD)) and \
39 ((p2.y - NEAR_TRESHOLD) < p1.y < (p2.y + NEAR_TRESHOLD))
40
41
42 class TestLocation(unittest.TestCase):
43 def setUp(self):
44 self.engine = getEngine(True)
45 self.model = self.engine.getModel()
46 self.squaregrid1 = self.model.getCellGrid("square")
47 self.squaregrid2 = self.model.getCellGrid("square")
48 self.layer1 = fife.Layer("layer1", None, self.squaregrid1)
49 self.layer2 = fife.Layer("layer2", None, self.squaregrid2)
50 self.loc1 = fife.Location(self.layer1)
51 self.loc2 = fife.Location(self.layer2)
52
53 def testBasicMapping(self):
54 self.loc1.setLayerCoordinates(P(5, 5))
55 pt = self.loc1.getLayerCoordinates()
56 self.assertEqual(pt, P(5, 5))
57 pt = self.loc1.getExactLayerCoordinates()
58 self.assertEqual(pt, D(5, 5))
59 pt = self.loc1.getMapCoordinates()
60 self.assertEqual(pt, D(5, 5))
61 pt = self.loc1.getLayerCoordinates(self.layer2)
62 self.assertEqual(pt, P(5, 5))
63 pt = self.loc1.getExactLayerCoordinates(self.layer2)
64 self.assertEqual(pt, D(5, 5))
65
66 def testSquareGridScale(self):
67 self.squaregrid2.setXScale(5)
68 self.squaregrid2.setYScale(5)
69 self.loc1.setLayerCoordinates(P(5, 5))
70 pt = self.loc1.getLayerCoordinates(self.layer2)
71 self.assertEqual(pt, P(1, 1))
72 pt = self.loc1.getExactLayerCoordinates(self.layer1)
73 self.assertEqual(pt, D(5, 5))
74
75 def testSquareGridRotation(self):
76 self.squaregrid1.setRotation(90)
77 self.loc1.setLayerCoordinates(P(3, 3))
78 pt = self.loc1.getMapCoordinates()
79 self.assertEqual(pt, D(-3, 3))
80
81 def testSquareGridShifts(self):
82 self.squaregrid1.setXShift(-3)
83 self.squaregrid1.setYShift(-3)
84 self.loc1.setLayerCoordinates(P(3, 3))
85 pt = self.loc1.getMapCoordinates()
86 self.assertEqual(pt, D(0, 0))
87
88 def testSquareCombinations(self):
89 # correct order in combinations = translate, rotate, scale
90 self.squaregrid1.setXShift(2)
91 self.squaregrid1.setYShift(2)
92 self.squaregrid1.setRotation(90)
93 self.squaregrid1.setXScale(5)
94 self.squaregrid1.setYScale(5)
95 self.loc1.setLayerCoordinates(P(1, 1))
96 pt = self.loc1.getMapCoordinates()
97 print(pt)
98 self.assert_(is_near(pt.x, -3))
99 self.assert_(is_near(pt.y, 7))
100
101
102 class TestHexGrid(unittest.TestCase):
103 def setUp(self):
104 self.engine = getEngine(True)
105 self.model = self.engine.getModel()
106 self.map = self.model.createMap("test")
107 self.hexgrid1 = self.model.getCellGrid("hexagonal")
108 self.squaregrid1 = self.model.getCellGrid("square")
109 self.hex_layer1 = self.map.createLayer("hexlayer", self.hexgrid1)
110 self.square_layer1 = self.map.createLayer("squarelayer",
111 self.squaregrid1)
112 self.loc1 = fife.Location(self.hex_layer1)
113 self.loc2 = fife.Location(self.square_layer1)
114 self.log = fifelog.LogManager(self.engine)
115
116 # self.log.setVisibleModules('hexgrid', 'cellgrid')
117
118 def tearDown(self):
119 del self.engine
120 del self.log
121
122 def testHexGrid0row(self):
123 self.loc1.setMapCoordinates(D(-2, 0))
124 self.assert_(self.loc1.getLayerCoordinates() == P(-2, 0))
125
126 self.loc1.setMapCoordinates(D(-1, 0))
127 self.assert_(self.loc1.getLayerCoordinates() == P(-1, 0))
128
129 self.loc1.setMapCoordinates(D(0, 0))
130 self.assert_(self.loc1.getLayerCoordinates() == P(0, 0))
131
132 self.loc1.setMapCoordinates(D(1, 0))
133 self.assert_(self.loc1.getLayerCoordinates() == P(1, 0))
134
135 self.loc1.setMapCoordinates(D(2, 0))
136 self.assert_(self.loc1.getLayerCoordinates() == P(2, 0))
137
138 def testHexGrid1row(self):
139 self.loc1.setMapCoordinates(D(-1.1, 1))
140 self.assert_(self.loc1.getLayerCoordinates() == P(-2, 1))
141
142 self.loc1.setMapCoordinates(D(-0.5, 1))
143 self.assert_(self.loc1.getLayerCoordinates() == P(-1, 1))
144
145 self.loc1.setMapCoordinates(D(-0.1, 1))
146 self.assert_(self.loc1.getLayerCoordinates() == P(-1, 1))
147
148 self.loc1.setMapCoordinates(D(0.1, 1))
149 self.assert_(self.loc1.getLayerCoordinates() == P(0, 1))
150
151 def testHexGridm1row(self):
152 self.loc1.setMapCoordinates(D(-1.1, -1))
153 self.assert_(self.loc1.getLayerCoordinates() == P(-2, -1))
154
155 self.loc1.setMapCoordinates(D(-0.5, -1))
156 self.assert_(self.loc1.getLayerCoordinates() == P(-1, -1))
157
158 self.loc1.setMapCoordinates(D(-0.1, -1))
159 self.assert_(self.loc1.getLayerCoordinates() == P(-1, -1))
160
161 self.loc1.setMapCoordinates(D(0.1, -1))
162 self.assert_(self.loc1.getLayerCoordinates() == P(0, -1))
163
164 def testEdgeHits(self):
165 self.loc1.setMapCoordinates(D(0.5, 0.5))
166 self.assert_(self.loc1.getLayerCoordinates() == P(0, 1))
167
168 self.loc1.setMapCoordinates(D(0.1, 0.4))
169 # print "______''___" + str(self.loc1.getLayerCoordinates())
170 self.assert_(self.loc1.getLayerCoordinates() == P(0, 0))
171
172 self.loc1.setMapCoordinates(D(0.1, -0.4))
173 self.assert_(self.loc1.getLayerCoordinates() == P(0, 0))
174
175 self.loc1.setMapCoordinates(D(-0.5, -0.5))
176 self.assert_(self.loc1.getLayerCoordinates() == P(-1, -1))
177
178
179 TEST_CLASSES = [TestHexGrid, TestLocation]
180
181 if __name__ == '__main__':
182 unittest.main()