Pol  Revision:cb584c9
ufacing.cpp
Go to the documentation of this file.
1 #include "ufacing.h"
2 
3 #include "../../clib/clib.h"
4 #include "../../clib/random.h"
5 #include "../mdelta.h"
6 #include "../poltype.h"
7 #include "../uconst.h"
8 #include "../uobject.h"
9 #include "charactr.h"
10 
11 namespace Pol
12 {
13 namespace Core
14 {
15 // FIXME shouldn't be using PKTIN_02_FACING_MASK here
16 MoveDelta move_delta[8] = {{0, -1}, // 0 is N
17  {+1, -1}, // 1 is NE
18  {+1, 0}, // ...
19  {+1, +1}, {0, +1}, {-1, +1}, {-1, 0}, {-1, -1}};
20 
23 
24 std::array<int, 7> adjustments = {{0, +1, -1, +2, -2, +3, -3}};
25 }
26 namespace Mobile
27 {
29 {
30  int rval = Clib::random_int( 7 );
31  return static_cast<Core::UFACING>( rval );
32 }
33 
34 const char* FacingStr( Core::UFACING facing )
35 {
36  switch ( facing )
37  {
38  case Core::FACING_N:
39  return "N";
40  case Core::FACING_S:
41  return "S";
42  case Core::FACING_E:
43  return "E";
44  case Core::FACING_W:
45  return "W";
46  case Core::FACING_NE:
47  return "NE";
48  case Core::FACING_NW:
49  return "NW";
50  case Core::FACING_SE:
51  return "SE";
52  case Core::FACING_SW:
53  return "SW";
54  }
55  return "";
56 }
57 
59 {
60  using namespace Core;
61  const UObject* dst = idst->toplevel_owner();
62  if ( src->x < dst->x ) // East to target
63  {
64  if ( src->y < dst->y )
65  return FACING_SE;
66  else if ( src->y == dst->y )
67  return FACING_E;
68  else /* src->y > dst->y */
69  return FACING_NE;
70  }
71  else if ( src->x == dst->x )
72  {
73  if ( src->y < dst->y )
74  return FACING_S;
75  else if ( src->y > dst->y )
76  return FACING_N;
77  }
78  else /* src->x > dst->x */ // West to target
79  {
80  if ( src->y < dst->y )
81  return FACING_SW;
82  else if ( src->y == dst->y )
83  return FACING_W;
84  else /* src->y > dst->y */
85  return FACING_NW;
86  }
87  return FACING_N;
88 }
89 
91 {
92  using namespace Core;
93  if ( src->x < to_x ) // East to target
94  {
95  if ( src->y < to_y )
96  return FACING_SE;
97  else if ( src->y == to_y )
98  return FACING_E;
99  else /* src->y > dst->y */
100  return FACING_NE;
101  }
102  else if ( src->x == to_x )
103  {
104  if ( src->y < to_y )
105  return FACING_S;
106  else if ( src->y > to_y )
107  return FACING_N;
108  }
109  else /* src->x > dst->x */ // West to target
110  {
111  if ( src->y < to_y )
112  return FACING_SW;
113  else if ( src->y == to_y )
114  return FACING_W;
115  else /* src->y > dst->y */
116  return FACING_NW;
117  }
118  return FACING_N;
119 }
120 
122  Core::ycoord to_y )
123 {
124  using namespace Core;
125  if ( from_x < to_x ) // East to target
126  {
127  if ( from_y < to_y )
128  return FACING_SE;
129  else if ( from_y == to_y )
130  return FACING_E;
131  else /* from_y > to_y */
132  return FACING_NE;
133  }
134  else if ( from_x == to_x )
135  {
136  if ( from_y < to_y )
137  return FACING_S;
138  else if ( from_y > to_y )
139  return FACING_N;
140  }
141  else /* from_x > to_x */ // West to target
142  {
143  if ( from_y < to_y )
144  return FACING_SW;
145  else if ( from_y == to_y )
146  return FACING_W;
147  else /* from_y > to_y */
148  return FACING_NW;
149  }
150  return FACING_N;
151 }
152 
154 {
155  Core::UFACING toward = direction_toward( src, idst );
156  Core::UFACING away = Core::away_cvt[static_cast<int>( toward )];
157  return away;
158 }
159 
161 {
162  Core::UFACING toward = direction_toward( src, from_x, from_y );
163  Core::UFACING away = Core::away_cvt[static_cast<int>( toward )];
164  return away;
165 }
166 
167 bool DecodeFacing( const char* dir, Core::UFACING& facing )
168 {
169  if ( stricmp( dir, "N" ) == 0 )
170  facing = Core::FACING_N;
171  else if ( stricmp( dir, "S" ) == 0 )
172  facing = Core::FACING_S;
173  else if ( stricmp( dir, "E" ) == 0 )
174  facing = Core::FACING_E;
175  else if ( stricmp( dir, "W" ) == 0 )
176  facing = Core::FACING_W;
177  else if ( stricmp( dir, "NE" ) == 0 )
178  facing = Core::FACING_NE;
179  else if ( stricmp( dir, "SE" ) == 0 )
180  facing = Core::FACING_SE;
181  else if ( stricmp( dir, "SW" ) == 0 )
182  facing = Core::FACING_SW;
183  else if ( stricmp( dir, "NW" ) == 0 )
184  facing = Core::FACING_NW;
185  else
186  return false;
187  return true;
188 }
189 }
190 }
Core::UFACING direction_away(const Character *src, Core::xcoord from_x, Core::ycoord from_y)
Definition: ufacing.cpp:160
const char * FacingStr(Core::UFACING facing)
Definition: ufacing.cpp:34
unsigned short ycoord
Definition: poltype.h:20
UFACING away_cvt[8]
Definition: ufacing.cpp:21
Core::UFACING GetRandomFacing()
Definition: ufacing.cpp:28
MoveDelta move_delta[8]
Definition: ufacing.cpp:16
Core::UFACING direction_toward(Core::xcoord from_x, Core::ycoord from_y, Core::xcoord to_x, Core::ycoord to_y)
Definition: ufacing.cpp:121
int random_int(int i)
Definition: random.cpp:34
virtual UObject * toplevel_owner()
Definition: uobject.cpp:234
std::array< int, 7 > adjustments
Definition: ufacing.cpp:24
Definition: berror.cpp:12
bool DecodeFacing(const char *dir, Core::UFACING &facing)
Definition: ufacing.cpp:167
unsigned short xcoord
Definition: poltype.h:19