00001
00002
00003 #ifndef _PTYPETABLE_H
00004 #define _PTYPETABLE_H
00005
00006 #include "osl/config.h"
00007 #include "osl/ptype.h"
00008 #include "osl/ptypeTraits.h"
00009 #include "osl/effectContent.h"
00010 #include "osl/direction.h"
00011 #include "osl/position.h"
00012 #include "osl/misc/carray.h"
00013 #include "osl/misc/carray2d.h"
00014 #include "osl/offset32.h"
00015
00016 namespace osl
00017 {
00018 class PtypeTable
00019 {
00020 private:
00021 CArray<mask_t, PTYPE_SIZE> numMaskLows;
00022 CArray<int, PTYPE_SIZE> numIndices;
00023 CArray<const char *, PTYPE_SIZE> names;
00024 CArray<const char *, PTYPE_SIZE> csaNames;
00025 CArray<bool, PTYPE_SIZE> betterToPromote;
00026 CArray<int, PTYPE_SIZE> moveMasks;
00027 CArray<int, PTYPE_SIZE> indexMins;
00028 CArray<int, PTYPE_SIZE> indexLimits;
00029
00030 CArray2d<int, 2, PTYPE_SIZE> canDropLimit;
00031
00032 CArray2d<EffectContent,PTYPEO_SIZE,Offset32::SIZE> effectTable;
00033 CArray2d<EffectContent,PTYPEO_SIZE,Offset32::SIZE> effectTableNotLongU;
00034 CArray2d<unsigned int, 2, SHORT_DIRECTION_SIZE> shortMoveMask;
00035
00036 template<Ptype T> void initPtypeSub(Int2Type<false> isBasic);
00037 template<Ptype T> void initPtypeSub(Int2Type<true> isBasic);
00038 template<Ptype T> void initPtype();
00039 public:
00040 PtypeTable();
00041 private:
00042 void init();
00043 public:
00044 unsigned int getShortMoveMask(Player p,PtypeO ptypeo,Direction dir) const
00045 {
00046 return shortMoveMask[playerToIndex(p)][static_cast<int>(dir)] &
00047 (1<<(ptypeo-PTYPEO_MIN));
00048 }
00049 mask_t getMaskLow(Ptype ptype) const
00050 {
00051 return numMaskLows[ptype];
00052 }
00053 #if OSL_WORDSIZE == 64
00054 int getIndex(Ptype) const
00055 {
00056 return 0;
00057 }
00058 #elif OSL_WORDSIZE == 32
00059 int getIndex(Ptype ptype) const
00060 {
00061 return numIndices[ptype];
00062 }
00063 #endif
00064
00067 bool hasLongMove(Ptype ptype) const
00068 {
00069 return getIndexMin(unpromote(ptype))>=32;
00070 }
00071 bool isBetterToPromote(Ptype ptype) const
00072 {
00073 return betterToPromote[ptype];
00074 }
00075 int getCanDropLimit(Player player,Ptype ptype) const
00076 {
00077 assert(isValid(ptype) && !isPromoted(ptype));
00078 return canDropLimit[playerToIndex(player)][ptype];
00079 }
00080
00081 private:
00082 bool canDropTo(Ptype ptype, Position pos, Int2Type<BLACK>) const
00083 {
00084 return pos.y() >= getCanDropLimit(BLACK,ptype);
00085 }
00086 bool canDropTo(Ptype ptype, Position pos, Int2Type<WHITE>) const
00087 {
00088 return pos.y() <= getCanDropLimit(WHITE,ptype);
00089 }
00090 public:
00091 bool canDropTo(Player pl, Ptype ptype, Position pos) const
00092 {
00093 if (pl == BLACK)
00094 return canDropTo(ptype, pos, Int2Type<BLACK>());
00095 else
00096 return canDropTo(ptype, pos, Int2Type<WHITE>());
00097 }
00098
00099 const char *getName(Ptype ptype) const
00100 {
00101 return names[ptype];
00102 }
00103 const char *getCsaName(Ptype ptype) const
00104 {
00105 return csaNames[ptype];
00106 }
00107 int getMoveMask(Ptype ptype) const
00108 {
00109 return moveMasks[ptype];
00110 }
00111 int getIndexMin(Ptype ptype) const
00112 {
00113 assert(isBasic(ptype));
00114 return indexMins[ptype];
00115 }
00116 int getIndexLimit(Ptype ptype) const
00117 {
00118 assert(isBasic(ptype));
00119 return indexLimits[ptype];
00120 }
00121 static int getKingIndex(Player p)
00122 {
00123 assert(isValid(p));
00124 if (p==BLACK)
00125 return KingTraits<BLACK>::index;
00126 else
00127 return KingTraits<WHITE>::index;
00128 }
00135 const EffectContent getEffect(PtypeO ptypeo,Position from, Position to) const
00136 {
00137 assert(from.isOnBoard() && to.isOnBoard());
00138 return getEffect(ptypeo,Offset32(to,from));
00139 }
00140 const EffectContent& getEffect(PtypeO ptypeo,Offset32 offset32) const
00141 {
00142 assert(isValidPtypeO(ptypeo));
00143 return effectTable[ptypeo-PTYPEO_MIN][offset32.index()];
00144 }
00145 private:
00146 EffectContent& effect(PtypeO ptypeo,Offset32 offset32)
00147 {
00148 assert(isValidPtypeO(ptypeo));
00149 const int i1 = ptypeo-PTYPEO_MIN;
00150 const int i2 = offset32.index();
00151 return effectTable[i1][i2];
00152 }
00153 public:
00155 const EffectContent
00156 getEffectNotLongU(PtypeO ptypeo, Position from, Position to) const
00157 {
00158 assert(isValidPtypeO(ptypeo));
00159 assert(from.isOnBoard() && to.isOnBoard());
00160 Offset32 offset32=Offset32(to,from);
00161 return effectTableNotLongU[ptypeo-PTYPEO_MIN][offset32.index()];
00162 }
00163 bool hasUnblockableEffect(PtypeO attacker, Position from, Position to) const
00164 {
00165 const EffectContent effect = getEffect(attacker, from, to);
00166 return effect.hasUnblockableEffect();
00167 }
00168 };
00169
00170 extern const PtypeTable Ptype_Table;
00171
00172 }
00173
00174
00175 #endif
00176
00177
00178
00179