Allow building as monolithic app (no plugins) for easier developement.
[tagua/yd.git] / src / positioninfo.cpp
blobd1c2fcdb62471c983da7055821d6c8aa2466eb5f
1 /*
2 Copyright (c) 2006-2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006-2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "positioninfo.h"
12 #include <KDebug>
14 #include <core/board.h>
15 #include <core/state.h>
17 #include "gameinfo.h"
18 #include "icsapi.h"
20 using namespace boost;
22 // Style 12 was designed by Daniel Sleator (sleator+@cs.cmu.edu) Darooha@ICC
23 QRegExp PositionInfo::pattern(
24 "^<12>\\s+" //header
25 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
26 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
27 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
28 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
29 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
30 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
31 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
32 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
33 "([BW])\\s+" //black/white
34 "(-1|[0-7])\\s+" //passing pawn
35 "([01])\\s+" //castle rights
36 "([01])\\s+" //castle rights
37 "([01])\\s+" //castle rights
38 "([01])\\s+" //castle rights
39 "(-?\\d+)\\s+" //50 moves made
40 "(\\d+)\\s+" //game num
41 "(\\S+)\\s+" //white name
42 "(\\S+)\\s+" //black name
43 "(-[1-4]|[0-2])\\s+" //status
44 "(\\d+)\\s+" //time
45 "(\\d+)\\s+" //inc
46 "(\\d+)\\s+" //w material
47 "(\\d+)\\s+" //b material
48 "(-?\\d+)\\s+" //w time
49 "(-?\\d+)\\s+" //b time
50 "(\\d+)\\s+" //move made
51 "(\\S+)\\s+" //coordmove
52 "(\\S+)\\s+" //time used
53 "(\\S+)\\s+" //algmove
54 "([0-1])" //flip
58 PositionInfo::PositionRow::PositionRow(const ICSAPIPtr& icsapi, const QString& str) {
59 Q_ASSERT(str.length() == 8);
61 row.resize(str.length());
62 for (int i = 0; i < str.length(); ++i) {
63 QChar c = str[i];
65 row[i] = icsapi->createPiece(c);
69 /**
70 * @return Position index as a 0-based halfmove count.
72 int PositionInfo::index() const {
73 int res = (moveIndex - 1) * 2;
74 if (turn == 1) res++;
75 return res;
78 PositionInfo::PositionInfo()
79 : valid(false) { }
81 bool PositionInfo::load(std::map<int, ICSGameData>& games, const QString& str) {
82 if (pattern.indexIn(str) != 0) {
83 valid = false;
84 return true;
87 bool new_game = false;
89 valid = true;
90 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
91 std::map<int, ICSGameData>::iterator gi = games.find(gn);
92 ICSAPIPtr icsapi;
94 if (gi == games.end()) {
95 kWarning() << "Received style12 for unknown game" << gn;
96 // create a gameinfo of type "dummy"
97 gi = games.insert(std::make_pair(gn, ICSGameData(gn, ""))).first;
98 new_game = true;
101 // FIXME load icsapi
102 // icsapi = gi->second.icsapi;
104 std::vector<PositionRow> rows;
105 for (uint i = 0; i < 8; ++i)
106 rows.push_back(PositionRow(icsapi, pattern.cap(CaptureIndexes::ChessboardStart + i)));
108 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
109 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
110 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
111 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
112 turn = pattern.cap(CaptureIndexes::Turn) == "W"? 0 : 1;
114 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
115 if (ep == -1)
116 enPassantSquare = Point::invalid();
117 else
118 enPassantSquare = Point(ep, turn == 0? 2 : 5);
120 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
121 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
122 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
123 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
125 position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
126 for (uint i = 0; i < 8; ++i) {
127 for (uint j = 0; j < rows[i].row.size(); ++j) {
128 position->board()->set(Point(j,i), rows[i].row[j]);
132 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
134 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
135 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
136 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
137 // time is in seconds
138 whiteTime *= 1000;
139 blackTime *= 1000;
142 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
143 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);
145 return new_game;