All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
effectUtil.tcc
Go to the documentation of this file.
1 #ifndef _EFFECTUTIL_TCC
2 #define _EFFECTUTIL_TCC
3 
8 #include <boost/static_assert.hpp>
9 
10 namespace osl
11 {
12  namespace effect_util
13  {
14  template <osl::Player P, bool InterestEmpty, Direction Dir>
16  {
17  template <class State, class Function>
18  static void testShort(const State& s, int mask, Square from,
19  Function& f)
20  {
22  if (! (mask & DirectionTraits<Dir>::mask))
23  return;
24 
26  const Square target = from+offset;
27  const Piece piece = s.pieceAt(target);
28  if (piece.isEdge())
29  return;
30  if (InterestEmpty || (! piece.isEmpty()))
31  f(target);
32  }
33  template <class State, class Function>
34  static void testLong(const State& s, int mask, Square from,
35  Function& f)
36  {
38  if (! (mask & DirectionTraits<Dir>::mask))
39  return;
40 
42 
43  Square target = from+offset;
44  Piece piece = s.pieceAt(target);
45  while (piece.isEmpty())
46  {
47  if (InterestEmpty)
48  f(target);
49  target = target+offset;
50  piece = s.pieceAt(target);
51  }
52  if (piece.isPiece())
53  {
54  f(target);
55  }
56  }
57  };
58  } // namespace effect_util
59 } // namespace osl
60 
61 template <osl::Player P, class Function, bool InterestEmpty>
63 forEachEffectOfPtypeO(const NumEffectState& state, Square from, Ptype ptype,
64  Function& f)
65 {
66  const int mask = Ptype_Table.getMoveMask(ptype);
85 }
86 
87 template <class Function, bool InterestEmpty>
89 forEachEffectOfPtypeO(const NumEffectState& state, Square from, PtypeO ptypeo,
90  Function& f)
91 {
92  const Player P = getOwner(ptypeo);
93  if (P == BLACK)
94  forEachEffectOfPtypeO<BLACK,Function,InterestEmpty>
95  (state, from, getPtype(ptypeo), f);
96  else
97  forEachEffectOfPtypeO<WHITE,Function,InterestEmpty>
98  (state, from, getPtype(ptypeo), f);
99 }
100 
102 {
103 public:
104  const NumEffectState& state;
106  SafeCapture(const NumEffectState& s) : state(s), safe_one(Piece::EMPTY())
107  {
108  }
109  template <Player P>
110  void doAction(Piece effect_piece, Square target)
111  {
113  (state, effect_piece.ptype(), effect_piece.square(), target))
114  return;
115  safe_one = effect_piece;
116  }
117 };
118 
119 template <osl::Player P>
122  Piece king)
123 {
124  assert(king.owner() == P);
125  assert(king.ptype() == KING);
126  PieceMask ignore = state.pin(P);
127  ignore.set(king.number());
128  const Piece piece = state.findAttackNotBy(P, target, ignore);
129  if (piece.isPiece())
130  return piece;
131  SafeCapture safe_captures(state);
132  state.template forEachEffectNotBy<P>(target, king, safe_captures);
133 
134  return safe_captures.safe_one;
135 }
136 
137 template <class EvalT>
139 {
140  const NumEffectState& state;
143  PieceVector& supported, & unsupported;
144  FindThreat(const NumEffectState& st, Player t, int a,
145  PieceVector& s, PieceVector& u)
146  : state(st), target(t), attacker_value(a), supported(s), unsupported(u)
147  {
148  }
149  void operator()(Square pos)
150  {
151  const Piece cur = state.pieceOnBoard(pos);
152  assert(cur.isPiece());
153  if (cur.owner() != target)
154  return;
155  if (state.hasEffectAt(target, pos))
156  {
157  if (abs(EvalT::captureValue(cur.ptypeO()))
158  > attacker_value)
159  supported.push_back(cur);
160  }
161  else
162  {
163  unsupported.push_back(cur);
164  }
165  }
166 };
167 
168 template <class EvalT>
169 void osl::EffectUtil::
170 findThreat(const NumEffectState& state, Square position,
171  PtypeO ptypeo, PieceVector& out)
172 {
173  PieceVector supported, unsupported;
174  const int attacker_value = abs(EvalT::captureValue(ptypeo));
175  FindThreat<EvalT> f(state, alt(getOwner(ptypeo)), attacker_value,
176  supported, unsupported);
177  forEachEffectOfPtypeO<FindThreat<EvalT>, false>
178  (state, position, ptypeo, f);
179 
180  unsupported.sortByPtype();
181  supported.sortByPtype();
182  PieceVector::iterator u=unsupported.begin(), s=supported.begin();
183 
184  if (u!=unsupported.end())
185  {
186  while ((s!=supported.end())
187  && ((abs(EvalT::captureValue(s->ptypeO()))
188  - attacker_value)
189  > abs(EvalT::captureValue(u->ptypeO()))))
190  {
191  out.push_back(*s);
192  ++s;
193  }
194  }
195  out.push_back(u, unsupported.end());
196  out.push_back(s, supported.end());
197 }
198 
199 #endif /* _EFFECTUTIL_TCC */
200 // ;;; Local Variables:
201 // ;;; mode:c++
202 // ;;; c-basic-offset:2
203 // ;;; End: