1Position.directionOffset = {
2 [DIRECTION_NORTH] = {x = 0, y = -1},
3 [DIRECTION_EAST] = {x = 1, y = 0},
4 [DIRECTION_SOUTH] = {x = 0, y = 1},
5 [DIRECTION_WEST] = {x = -1, y = 0},
6 [DIRECTION_SOUTHWEST] = {x = -1, y = 1},
7 [DIRECTION_SOUTHEAST] = {x = 1, y = 1},
8 [DIRECTION_NORTHWEST] = {x = -1, y = -1},
9 [DIRECTION_NORTHEAST] = {x = 1, y = -1}
10}
11
12function Position:getNextPosition(direction, steps)
13 local offset = Position.directionOffset[direction]
14 if offset then
15 steps = steps or 1
16 self.x = self.x + offset.x * steps
17 self.y = self.y + offset.y * steps
18 end
19end
20
21function Position:moveUpstairs()
22 local swap = function (lhs, rhs)
23 lhs.x, rhs.x = rhs.x, lhs.x
24 lhs.y, rhs.y = rhs.y, lhs.y
25 lhs.z, rhs.z = rhs.z, lhs.z
26 end
27
28 self.z = self.z - 1
29
30 local defaultPosition = self + Position.directionOffset[DIRECTION_SOUTH]
31 local toTile = Tile(defaultPosition)
32 if not toTile or not toTile:isWalkable() then
33 for direction = DIRECTION_NORTH, DIRECTION_NORTHEAST do
34 if direction == DIRECTION_SOUTH then
35 direction = DIRECTION_WEST
36 end
37
38 local position = self + Position.directionOffset[direction]
39 toTile = Tile(position)
40 if toTile and toTile:isWalkable() then
41 swap(self, position)
42 return self
43 end
44 end
45 end
46 swap(self, defaultPosition)
47 return self
48end
49
50function Position:isInRange(from, to)
51 -- No matter what corner from and to is, we want to make
52 -- life easier by calculating north-west and south-east
53 local zone = {
54 nW = {
55 x = (from.x < to.x and from.x or to.x),
56 y = (from.y < to.y and from.y or to.y),
57 z = (from.z < to.z and from.z or to.z)
58 },
59 sE = {
60 x = (to.x > from.x and to.x or from.x),
61 y = (to.y > from.y and to.y or from.y),
62 z = (to.z > from.z and to.z or from.z)
63 }
64 }
65
66 if self.x >= zone.nW.x and self.x <= zone.sE.x
67 and self.y >= zone.nW.y and self.y <= zone.sE.y
68 and self.z >= zone.nW.z and self.z <= zone.sE.z then
69 return true
70 end
71 return false
72end
73
74function Position:notifySummonAppear(summon)
75 local spectators = Game.getSpectators(self)
76 for _, spectator in ipairs(spectators) do
77 if spectator:isMonster() and spectator ~= summon then
78 spectator:addTarget(summon)
79 end
80 end
81end
82