All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
escapeFilter.h
Go to the documentation of this file.
1 /* escapeFilter.h
2  */
3 #ifndef OSL_ESCAPE_FILTER_H
4 #define OSL_ESCAPE_FILTER_H
5 
6 #include "osl/player.h"
8 #include "osl/eval/pieceEval.h"
9 
10 namespace osl
11 {
12  namespace move_action
13  {
17  template<Player P,class OrigAction>
19  {
20  BOOST_CLASS_REQUIRE(OrigAction,osl::move_action,Concept);
21  const NumEffectState& state;
22  OrigAction & action;
23  PieceMask attack_pin, defense_pin;
28 
30  {
31  Piece attack = state.findCheapAttackNotBy(alt(P), to, state.pinOrOpen(alt(P))); // == pin
32  if (! attack.isEmpty())
33  return attack;
34  if (! has_long)
35  return Piece::EMPTY();
37  if (!isLong(d))
38  return Piece::EMPTY();
39  const int num=state.longEffectNumTable()[removed.number()][longToShort(d)];
40  if (Piece::isEmptyNum(num))
41  return Piece::EMPTY();
42  attack = state.pieceOf(num);
43  if (attack.owner()==alt(P))
44  return attack;
45  return Piece::EMPTY();
46  }
47  template <bool IsSimple>
48  bool suitable(Square to) const
49  {
50  const Piece attack = findThreat(to);
51  if (attack.isEmpty())
52  return true;
53  if (! IsSimple)
54  {
55  const Ptype capture = state.pieceAt(to).ptype();
56  if (capture != PTYPE_EMPTY)
57  if (eval::Ptype_Eval_Table.captureValue(newPtypeO(WHITE, capture)) >= my_value)
58  return true;
59  }
60  if (eval::Ptype_Eval_Table.captureValue(newPtypeO(WHITE, attack.ptype())) <= value)
61  return false;
62  const int attack_count = state.countEffect(alt(P), to, attack_pin); // rough estimation
63  const int defense_count = state.countEffect(P, to, defense_pin);
64  if (defense_count > attack_count)
65  return true;
66  if (defense_count < attack_count)
67  return false;
68  if (! has_long_support)
69  return false;
71  if (!isLong(d))
72  return false;
73  const int num=state.longEffectNumTable()[removed.number()][longToShort(d)];
74  return ! Piece::isEmptyNum(num)
75  && state.pieceOf(num).owner() == P;
76  }
77  public:
78  EscapeFilter(const NumEffectState& s, OrigAction & action,Square pos, Ptype ptype)
79  : state(s), action(action),
80  attack_pin(state.pin(alt(P))), defense_pin(state.pin(P)),
81  removed(state.pieceAt(pos)),
82  has_long(state.longEffectAt(pos, alt(P)).any()),
83  has_long_support(state.longEffectAt(pos, P).any())
84  {
85  assert(removed.isPiece());
86  assert(removed.owner() == P);
87 
88  if (ptype == PTYPE_EMPTY)
89  ptype = state.findCheapAttackNotBy(alt(P), pos, state.pinOrOpen(alt(P))).ptype();
90  attacking = ptype;
94  }
95  void simpleMove(Square from,Square to,Ptype ptype, bool isPromote,Player /* p */,Move m){
96  if (suitable<true>(to))
97  action.simpleMove(from,to,ptype,isPromote,P,m);
98  }
99  void unknownMove(Square from,Square to,Piece p1,Ptype ptype,bool isPromote,Player /* p */,Move m){
100  if (suitable<false>(to))
101  action.unknownMove(from,to,p1,ptype,isPromote,P,m);
102  }
103  void dropMove(Square to,Ptype ptype,Player /* p */,Move){
104  assert(0);
105  }
106 
107  };
108  // old interfaces
109  void simpleMove(Square from,Square to,Ptype ptype,
110  bool isPromote,Player p)
111  {
112  simpleMove(from,to,ptype,isPromote,p,
113  Move(from,to,ptype,PTYPE_EMPTY,isPromote,p));
114  }
116  Ptype ptype,bool isPromote,Player p)
117  {
118  unknownMove(from,to,captured,ptype,isPromote,p,
119  Move(from,to,ptype,captured.ptype(),isPromote,p));
120  }
121  void dropMove(Square to,Ptype ptype,Player p)
122  {
123  dropMove(to,ptype,p,
124  Move(to,ptype,p));
125  }
126  } // namespace move_action
127 } // namespace osl
128 
129 
130 #endif /* _NO_EFFECT_FILTER_H */
131 // ;;; Local Variables:
132 // ;;; mode:c++
133 // ;;; c-basic-offset:2
134 // ;;; coding:utf-8
135 // ;;; End: