Create a debian package for debug symbols.
[tagua/yd.git] / src / game.cpp
blob7bac7a42f00f2c9fd8666e5b8d1151201de4abb9
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 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 <map>
12 #include <KDebug>
13 #ifdef Q_CC_MSVC
14 #pragma warning( push )
15 #pragma warning( disable : 4100 )
16 #include <boost/variant.hpp>
17 #pragma warning( pop )
18 #else
19 #include <boost/variant.hpp>
20 #endif
22 #include <core/moveserializer.h>
23 #include <core/state.h>
24 #include <core/statefactory.h>
25 #include <core/validator.h>
26 #include "variant.h"
28 #include "game.h"
29 #include "game_p.h"
30 #include "pgnparser.h"
31 #include "components.h"
32 #include "variants.h"
34 using namespace GamePrivate;
37 Game::Game(Components* components)
38 : current(-1)
39 , undo_pos(0)
40 , m_components(components) { }
42 Game::~Game() {
45 void Game::onAdded(const Index&) {
48 void Game::onRemoved(const Index&) {
51 void Game::onEntryChanged(const Index&, int) {
54 void Game::onPromoteVariation(const Index&, int) {
57 void Game::onSetComment(const Index&, const QString&) {
60 void Game::onSetVComment(const Index&, int, const QString&) {
63 void Game::onCurrentIndexChanged(const Index&) {
66 void Game::onAvailableUndo(bool) {
69 void Game::onAvailableRedo(bool) {
72 Entry* Game::fetch(const Index& ix) {
73 int at;
74 History *vec = fetchRef(ix, &at);
75 return vec ? &(*vec)[at] : NULL;
78 const Entry* Game::fetch(const Index& ix) const {
79 int at;
80 const History *vec = fetchRef(ix, &at);
81 return vec ? &(*vec)[at] : NULL;
84 History* Game::fetchRef(const Index& ix, int* idx) {
85 if(ix.num_moves >= (int)history.size() || ix.num_moves < 0 )
86 return NULL;
88 History* aretv = &history;
89 Entry* retv = &history[ix.num_moves];
90 if(idx) *idx = ix.num_moves;
92 for(int i=0; i<(int)ix.nested.size();i++) {
93 Variations::iterator it = retv->variations.find(ix.nested[i].variation);
94 if(it == retv->variations.end() || ix.nested[i].num_moves >= (int)it->second.size()
95 || ix.nested[i].num_moves < 0 )
96 return NULL;
98 aretv = &it->second;
99 retv = &it->second[ix.nested[i].num_moves];
100 if(idx) *idx = ix.nested[i].num_moves;
102 return aretv;
105 const History* Game::fetchRef(const Index& ix, int* idx) const {
106 return const_cast<const History*>(const_cast<Game*>(this)->fetchRef(ix, idx));
109 void Game::testMove(const Index& ix) {
110 if (ix != Index(0)) {
111 Entry *e1 = fetch(ix.prev());
112 Entry *e2 = fetch(ix);
113 if (!e1 || !e2 || !e1->position || e2->move == Move())
114 return;
116 if (!m_components->validator()->legal(e1->position.get(), e2->move))
117 kError() << "invalid move added to game history";
121 void Game::testMove() {
122 testMove(current);
125 void Game::saveUndo(const UndoOp& op) {
126 bool redo = undo_pos < (int)undo_history.size();
128 while(undo_pos < (int)undo_history.size())
129 undo_history.pop_back();
130 undo_history.push_back(op);
131 undo_pos++;
133 if(undo_pos == 1)
134 onAvailableUndo(true);
135 if(redo)
136 onAvailableRedo(false);
140 Index Game::index() const {
141 return current;
144 Index Game::lastMainlineIndex() const {
145 return Index(history.size()-1);
148 bool Game::containsIndex(const Index& index) const {
149 return !!fetch(index);
152 Move Game::move() const {
153 return move(current);
156 Move Game::move(const Index& index) const {
157 Entry *e = (Entry*)fetch(index);
158 if(!e) {
159 kError() << "Index out of range";
160 return Move();
162 return e->move;
165 StatePtr Game::position() const {
166 return position(current);
169 StatePtr Game::position(const Index& index) const {
170 Entry *e = (Entry*)fetch(index);
171 if(!e) {
172 kError() << "Index" << index << "out of range";
173 return StatePtr();
175 return e->position;
178 QString Game::comment() const {
179 return comment(current);
182 QString Game::comment(const Index& index) const {
183 const Entry *e = fetch(index);
184 if(!e) {
185 kError() << "Index out of range";
186 return QString();
188 return e->comment;
191 void Game::reset(StatePtr pos) {
192 Q_ASSERT(pos);
194 undo_pos = 0;
195 undo_history.clear();
196 history.clear();
197 history.push_back( Entry(Move(), pos) );
198 current = Index(0);
199 onCurrentIndexChanged();
202 void Game::undo() {
203 if(undo_pos <= 0) {
204 kError() << "Nothing to undo";
205 return;
208 bool last_undo = undo_pos == 1;
209 bool now_redo = undo_pos == (int)undo_history.size();
211 undo_pos--;
212 UndoOp* op = &(undo_history[undo_pos]);
214 if(boost::get<UndoAdd>(op)) {
215 UndoAdd *a = boost::get<UndoAdd>(op);
217 if(a->index.atVariationStart() ) {
218 Entry* e = fetch(a->index.prev());
219 Q_ASSERT(e);
221 int v = a->index.nested.back().variation;
222 Q_ASSERT(e->variations.count(v) == 1);
223 Q_ASSERT(e->variations[v].size() == 1);
225 e->variations.erase(v);
227 else {
228 int at;
229 std::vector<Entry>* vec = fetchRef(a->index, &at);
230 Q_ASSERT(vec);
231 Q_ASSERT((int)vec->size() == at+1);
233 vec->pop_back();
236 if(current == a->index) {
237 current = current.prev();
238 onCurrentIndexChanged();
241 onRemoved(a->index);
243 else if(boost::get<UndoPromote>(op)) {
244 UndoPromote *p = boost::get<UndoPromote>(op);
246 int at;
247 std::vector<Entry>* vec = fetchRef(p->index, &at);
248 Q_ASSERT(vec);
249 Q_ASSERT((*vec)[at].variations.count(p->variation)==1);
251 History vold = (*vec)[at].variations[p->variation];
252 History vnew;
253 for(int i=at+1; i<(int)vec->size(); i++)
254 vnew.push_back((*vec)[i]);
255 while((int)vec->size()>at+1)
256 vec->pop_back();
257 for(int i=0; i<(int)vold.size(); i++)
258 vec->push_back(vold[i]);
259 (*vec)[at].variations[p->variation] = vnew;
261 current = current.flipVariation(p->index, p->variation);
262 onPromoteVariation(p->index, p->variation);
263 //onCurrentIndexChanged();
265 else if(boost::get<UndoTruncate>(op)) {
266 UndoTruncate *t = boost::get<UndoTruncate>(op);
268 int at;
269 std::vector<Entry>* vec = fetchRef(t->index, &at);
270 Q_ASSERT(vec);
271 Q_ASSERT((int)vec->size() == at+1);
272 Q_ASSERT((*vec)[at].variations.empty());
274 for(int i=0;i<(int)t->history.size();i++)
275 vec->push_back(t->history[i]);
276 (*vec)[at].variations = t->variations;
277 (*vec)[at].vcomments = t->vcomments;
279 if(t->history.size())
280 onAdded(t->index.next());
281 for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
282 onAdded(t->index.next(it->first));
283 for(VComments::iterator it = t->vcomments.begin(); it != t->vcomments.end(); ++it)
284 onSetVComment(t->index, it->first, it->second);
286 else if(boost::get<UndoRemove>(op)) {
287 UndoRemove *r = boost::get<UndoRemove>(op);
289 Entry *e = fetch(r->index);
290 e->variations[r->variation] = r->history;
291 onAdded(r->index.next(r->variation));
292 if(!r->vcomment.isEmpty()) {
293 e->vcomments[r->variation] = r->vcomment;
294 onSetVComment(r->index, r->variation, r->vcomment);
297 else if(boost::get<UndoClear>(op)) {
298 UndoClear *c = boost::get<UndoClear>(op);
300 Entry *e = fetch(c->index);
301 e->variations = c->variations;
302 e->vcomments = c->vcomments;
303 for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
304 onAdded(c->index.next(it->first));
305 for(VComments::iterator it = c->vcomments.begin(); it != c->vcomments.end(); ++it)
306 onSetVComment(c->index, it->first, it->second);
308 else if(boost::get<UndoSetComment>(op)) {
309 UndoSetComment *sc = boost::get<UndoSetComment>(op);
310 Entry *e = fetch(sc->index);
311 Q_ASSERT(e);
313 if(sc->variation == -1) {
314 e->comment = sc->old_comment;
315 onSetComment(sc->index, sc->old_comment);
317 else {
318 if(sc->old_comment.isEmpty())
319 e->vcomments.erase(sc->variation);
320 else
321 e->vcomments[sc->variation] = sc->old_comment;
322 onSetVComment(sc->index, sc->variation, sc->old_comment);
325 else
326 kError() << "Unexpected type in boost::variant";
328 if(last_undo)
329 onAvailableUndo(false);
330 if(now_redo)
331 onAvailableRedo(true);
334 void Game::redo() {
335 if(undo_pos >= (int)undo_history.size()) {
336 kError() << "Nothing to redo";
337 return;
340 bool now_undo = undo_pos == 0;
341 bool last_redo = undo_pos == (int)undo_history.size()-1;
343 UndoOp* op = &(undo_history[undo_pos]);
344 undo_pos++;
346 if(boost::get<UndoAdd>(op)) {
347 UndoAdd *a = boost::get<UndoAdd>(op);
349 if(a->index.atVariationStart() ) {
350 Entry* e = fetch(a->index.prev());
351 Q_ASSERT(e);
353 int v = a->index.nested.back().variation;
354 Q_ASSERT(e->variations.count(v) == 0);
356 History h;
357 h.push_back(a->entry);
358 e->variations[v] = h;
360 else {
361 int at;
362 std::vector<Entry>* vec = fetchRef(a->index.prev(), &at);
363 Q_ASSERT(vec);
364 Q_ASSERT((int)vec->size() == at+1);
366 vec->push_back(a->entry);
369 onAdded(a->index);
371 else if(boost::get<UndoPromote>(op)) {
372 UndoPromote *p = boost::get<UndoPromote>(op);
374 int at;
375 std::vector<Entry>* vec = fetchRef(p->index, &at);
377 Q_ASSERT(vec);
378 Q_ASSERT((*vec)[at].variations.count(p->variation)==1);
379 History vold = (*vec)[at].variations[p->variation];
380 History vnew;
381 for(int i=at+1; i<(int)vec->size(); i++)
382 vnew.push_back((*vec)[i]);
383 while((int)vec->size()>at+1)
384 vec->pop_back();
385 for(int i=0; i<(int)vold.size(); i++)
386 vec->push_back(vold[i]);
387 (*vec)[at].variations[p->variation] = vnew;
389 current = current.flipVariation(p->index, p->variation);
390 onPromoteVariation(p->index, p->variation);
391 //onCurrentIndexChanged();
393 else if(boost::get<UndoTruncate>(op)) {
394 UndoTruncate *t = boost::get<UndoTruncate>(op);
396 int at;
397 std::vector<Entry>* vec = fetchRef(t->index, &at);
398 Q_ASSERT(vec);
399 Q_ASSERT((int)vec->size() == at+1+(int)t->history.size());
401 while((int)vec->size() > at+1)
402 vec->pop_back();
403 (*vec)[at].variations.clear();
404 (*vec)[at].vcomments.clear();
406 if(current > t->index) {
407 current = t->index;
408 onCurrentIndexChanged();
411 if(t->history.size())
412 onRemoved(t->index.next());
413 for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
414 onRemoved(t->index.next(it->first));
416 else if(boost::get<UndoRemove>(op)) {
417 UndoRemove *r = boost::get<UndoRemove>(op);
419 Entry *e = fetch(r->index);
420 e->variations.erase(r->variation);
421 e->vcomments.erase(r->variation);
422 onRemoved(r->index.next(r->variation));
424 else if(boost::get<UndoClear>(op)) {
425 UndoClear *c = boost::get<UndoClear>(op);
427 Entry *e = fetch(c->index);
428 e->variations.clear();
429 e->vcomments.clear();
430 for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
431 onRemoved(c->index.next(it->first));
433 else if(boost::get<UndoSetComment>(op)) {
434 UndoSetComment *sc = boost::get<UndoSetComment>(op);
435 Entry *e = fetch(sc->index);
436 Q_ASSERT(e);
438 if(sc->variation == -1) {
439 e->comment = sc->new_comment;
440 onSetComment(sc->index, sc->new_comment);
442 else {
443 if(sc->new_comment.isEmpty())
444 e->vcomments.erase(sc->variation);
445 else
446 e->vcomments[sc->variation] = sc->new_comment;
447 onSetVComment(sc->index, sc->variation, sc->new_comment);
450 else
451 kError() << "Unexpected type in boost::variant";
453 if(now_undo)
454 onAvailableUndo(true);
455 if(last_redo)
456 onAvailableRedo(false);
459 void Game::setComment(const QString& c) {
460 setComment(current, c);
463 void Game::setComment(const Index& ix, const QString& c) {
464 Entry* e = fetch(ix);
465 if(!e) {
466 kError() << "Invalid index";
467 return;
469 if(e->comment == c)
470 return;
472 saveUndo(UndoSetComment(ix, -1, e->comment, c));
473 e->comment = c;
474 onSetComment(ix, c);
477 void Game::setVComment(const Index& ix, int v, const QString& c) {
478 Entry* e = fetch(ix);
479 if(!e) {
480 kError() << "Invalid index";
481 return;
483 QString oc = e->vcomments.count(v) ? e->vcomments[v] : QString();
484 if(oc == c)
485 return;
487 saveUndo(UndoSetComment(ix, v, oc, c));
488 if(c.isEmpty())
489 e->vcomments.erase(v);
490 else
491 e->vcomments[v] = c;
492 onSetVComment(ix, v, c);
495 void Game::promoteVariation() {
496 promoteVariation(current);
499 void Game::promoteVariation(const Index& _ix) {
500 if(_ix.nested.size()==0) {
501 kError() << "Cannot promote main line";
502 return;
504 Index ix = _ix;
505 int v = ix.nested.back().variation;
506 ix.nested.pop_back();
508 promoteVariation(ix, v);
511 void Game::promoteVariation(const Index& ix, int v) {
512 int at;
513 std::vector<Entry>* vec = fetchRef(ix, &at);
514 Q_ASSERT(vec);
515 Q_ASSERT((*vec)[at].variations.count(v)==1);
517 History vold = (*vec)[at].variations[v];
518 History vnew;
519 for(int i=at+1; i<(int)vec->size(); i++)
520 vnew.push_back((*vec)[i]);
521 while((int)vec->size()>at+1)
522 vec->pop_back();
523 for(int i=0; i<(int)vold.size(); i++)
524 vec->push_back(vold[i]);
525 (*vec)[at].variations[v] = vnew;
527 saveUndo(UndoPromote(ix, v));
528 current = current.flipVariation(ix, v);
529 onPromoteVariation(ix, v);
530 //don't call onCurrentIndexChanged(), as the position did not change actually
533 void Game::removeVariation(int v) {
534 removeVariation(current, v);
537 void Game::removeVariation(const Index& _ix) {
538 if(_ix.nested.size()==0) {
539 kError() << "Cannot remove main line";
540 return;
542 Index ix = _ix;
543 int v = ix.nested.back().variation;
544 ix.nested.pop_back();
546 removeVariation(ix, v);
549 void Game::removeVariation(const Index& ix, int v) {
550 Entry* e = fetch(ix);
552 saveUndo(UndoRemove(ix, v, e->variations[v],
553 e->vcomments.count(v) ? e->vcomments[v] : QString() ));
554 e->variations.erase(v);
555 e->vcomments.erase(v);
557 onRemoved(ix.next(v));
558 if(current >= ix.next(v)) {
559 current = ix;
560 onCurrentIndexChanged();
564 void Game::clearVariations() {
565 clearVariations(current);
568 void Game::clearVariations(const Index& ix) {
569 Entry* e = fetch(ix);
571 UndoClear uc(ix, e->variations, e->vcomments);
572 saveUndo(uc);
573 e->variations.clear();
574 e->vcomments.clear();
576 for(Variations::iterator it = uc.variations.begin(); it != uc.variations.end(); ++it)
577 onRemoved(ix.next(it->first));
578 if(current > ix && !(current >= ix.next())) {
579 current = ix;
580 onCurrentIndexChanged();
584 void Game::truncate() {
585 truncate(current);
588 void Game::truncate(const Index& ix) {
589 int at;
590 History* vec = fetchRef(ix, &at);
591 if(!vec) {
592 kError() << "Truncating at an unexisting index";
593 return;
596 Entry *e = &(*vec)[at];
597 UndoTruncate undo(ix);
598 for(int i=at+1; i<(int)vec->size();i++)
599 undo.history.push_back((*vec)[i]);
600 while((int)vec->size()>at+1)
601 vec->pop_back();
603 undo.variations = e->variations;
604 undo.vcomments = e->vcomments;
605 saveUndo(undo);
606 e->variations.clear();
607 e->vcomments.clear();
609 if(undo.history.size())
610 onRemoved(undo.index.next());
611 for(Variations::iterator it = undo.variations.begin(); it != undo.variations.end(); ++it)
612 onRemoved(undo.index.next(it->first));
614 if(current > ix) {
615 current = ix;
616 onCurrentIndexChanged();
620 void Game::add(const Move& m, const StatePtr& pos) {
621 Q_ASSERT(pos);
623 Index old_c = current;
624 int at;
625 std::vector<Entry>* vec = fetchRef(current, &at);
626 Q_ASSERT(vec);
628 /* add the move on the mainline */
629 if((int)vec->size() <= at+1 ) {
630 Q_ASSERT((int)vec->size() == at+1);
631 vec->push_back(Entry(m, pos));
632 current = current.next();
633 testMove();
634 saveUndo(UndoAdd(current, Entry(m, pos)));
635 onAdded(current);
636 onCurrentIndexChanged(old_c);
638 /* we are playing the move that is already next in the mainline */
639 else if( (*vec)[at+1].position && (*vec)[at+1].position->equals(pos.get())) {
640 current = current.next();
641 onCurrentIndexChanged(old_c);
642 /* no need to test the move */
644 else {
645 Entry *e = fetch(current);
646 Q_ASSERT(e);
648 /* check if a variations with this move already exists. */
649 for(Variations::iterator it = e->variations.begin(); it != e->variations.end(); ++it)
650 if(it->second.size() > 0 && it->second[0].position
651 && it->second[0].position->equals(pos.get()) ) {
652 current = current.next(it->first);
653 onCurrentIndexChanged(old_c);
655 return;
658 int var_id = e->last_var_id++;
659 e->variations[var_id].push_back(Entry(m, pos));
660 current = current.next(var_id);
661 testMove();
662 saveUndo(UndoAdd(current, Entry(m, pos)));
663 onAdded(current);
664 onCurrentIndexChanged(old_c);
668 bool Game::insert(const Move& m, const StatePtr& pos, const Index& at) {
669 Entry *e = fetch(at);
671 if(!e) {
672 if(at.nested.size() == 0) {
673 if(undo_history.size()) {
674 undo_pos = 0;
675 undo_history.clear();
677 int hs = history.size();
678 history.resize(at.num_moves + 1);
679 history[at.num_moves] = Entry(m, pos);
680 testMove(at);
681 onAdded(Index(hs));
682 return true;
684 else {
685 kError() << "Index out if range";
686 return false;
690 if(undo_history.size()) {
691 undo_pos = 0;
692 undo_history.clear();
694 bool res = e->position && e->position->equals(pos.get());
695 e->move = m;
696 e->position = pos;
697 testMove(at);
698 testMove(at.next());
699 for (Variations::const_iterator it = e->variations.begin();
700 it != e->variations.end(); ++it)
701 testMove(at.next(it->first));
702 onEntryChanged(at);
703 return res;
706 bool Game::lastPosition() const {
707 return !fetch(current.next());
710 bool Game::back() {
711 if (current <= 0) return false; // first entry or uninitialized
712 Index old_c = current;
713 Index new_c = current.prev();
715 Entry *e = fetch(new_c);
716 if(!e || e->position == 0) return false; // gap immediately before current
717 current = new_c;
718 onCurrentIndexChanged(old_c);
720 return true;
723 bool Game::forward() {
724 Index old_c = current;
725 Index new_c = current.next();
727 Entry *e = fetch(new_c);
728 if(!e || e->position == 0) {
729 return false; // gap immediately before current
731 current = new_c;
732 onCurrentIndexChanged(old_c);
734 return true;
737 void Game::gotoFirst() {
738 Index old_c = current;
739 current = Index(0);
740 onCurrentIndexChanged(old_c);
743 void Game::gotoLast() {
744 int at;
745 std::vector<Entry>* vec = fetchRef(current, &at);
746 Q_ASSERT(vec);
747 Q_ASSERT((int)vec->size() > at);
749 if((int)vec->size() > at+1) {
750 Index old_c = current;
751 current = current.next(-1, vec->size()-1-at);
752 onCurrentIndexChanged(old_c);
756 bool Game::goTo(const Index& index) {
757 if (fetch(index)) {
758 Index old_c = current;
759 current = index;
760 onCurrentIndexChanged(old_c);
761 return true;
763 return false;
766 QString Game::variationPgn(const History& vec, const Entry& e,
767 int start, const Index& _ix) const {
768 Index ix = _ix;
769 QString res;
771 for (int i = start; i < static_cast<int>(vec.size()); i++) {
772 const Entry& preve = (i > start) ? vec[i-1] : e;
775 QString mv = (vec[i].move != Move() && preve.position)
776 ? m_components->moveSerializer("compact")->
777 serialize(vec[i].move, preve.position.get())
778 : "???";
779 #if 0
780 if (ix == current)
781 mv = "[[" + mv + "]]";
782 #endif
784 int n = ix.totalNumMoves()+1;
785 if(i==start || n%2==0)
786 mv = QString::number(n/2)+(n%2==1 ? ". ... " : ". ") + mv;
787 if (i > start)
788 mv = " " + mv;
790 res += mv;
792 if(!vec[i].comment.isEmpty())
793 res += " {" + vec[i].comment + "}";
795 if(i > 0) {
796 for(Variations::const_iterator it = vec[i-1].variations.begin();
797 it != vec[i-1].variations.end(); ++it) {
798 res += " (";
799 if(vec[i-1].vcomments.count(it->first))
800 res += "{" + vec[i-1].vcomments.find(it->first)->second + "} ";
801 res += variationPgn(it->second, vec[i - 1], 0,
802 ix.prev().next(it->first)) + ")";
806 ix = ix.next();
808 return res;
811 QString Game::pgn() const {
812 return variationPgn(history, history[0], 1, Index(1));
815 void Game::load(const PGN& pgn) {
816 std::map<QString, QString>::const_iterator var = pgn.m_tags.find("Variant");
817 Variant* vi;
819 // FIXME do not create a variant here
820 if (var == pgn.m_tags.end()) {
821 vi = Variants::self().create("chess");
823 else if (!(vi = Variants::self().create(var->second))) {
824 kError() << "No such variant" << var->second;
825 return;
828 std::map<QString, QString>::const_iterator fen = pgn.m_tags.find("FEN");
829 StatePtr pos;
831 //if(var == pgn.m_tags.end()) {
832 pos = StatePtr(m_components->createState());
833 pos->setup();
835 #if 0 // BROKEN
836 else if( !(pos = vi->createPositionFromFEN(fen->second))) {
837 kError() << "Wrong fen " << fen->second;
838 return;
840 #endif
842 //TODO: what about options? FEN rules?
844 load(pos, pgn);
847 void Game::load(StatePtr pos, const PGN& pgn) {
848 current = Index(0);
849 undo_history.clear();
850 undo_pos = 0;
852 // setup an empty history, clear as needed
854 if(history.size()) {
855 Entry* fe = &history[0];
856 int old_history_size = history.size();
857 std::vector<int> v_ids;
859 while(history.size()>1)
860 history.pop_back();
861 for(Variations::const_iterator it = fe->variations.begin();
862 it != fe->variations.end(); ++it)
863 v_ids.push_back(it->first);
864 fe->variations.clear();
865 fe->vcomments.clear();
867 for(int i=0;i<(int)v_ids.size();i++)
868 onRemoved(Index(0).next(v_ids[i]));
869 if(old_history_size>1)
870 onRemoved(Index(1));
871 v_ids.clear();
872 history[0].position = pos;
874 else
875 history.push_back( Entry(Move(), pos) );
877 // apply moves from PGN, one by one
879 QString vcomment;
880 std::vector<Index> var_stack;
881 bool var_start = false;
883 for (uint i = 0; i < pgn.m_entries.size(); i++) {
884 if(boost::get<QString>(pgn[i])) {
885 if(var_start)
886 vcomment += *boost::get<QString>(pgn[i]);
887 else {
888 Entry *e = fetch(current);
889 Q_ASSERT(e);
891 e->comment += *boost::get<QString>(pgn[i]);
894 else if(boost::get<PGN::BeginVariation>(pgn[i])) {
895 var_stack.push_back(current);
896 var_start = true;
898 else if(boost::get<PGN::EndVariation>(pgn[i])) {
899 if(var_stack.size() == 0) {
900 kError() << "Unexpected end variation";
901 break;
903 current = var_stack.back();
904 var_stack.pop_back();
906 else if(boost::get<PGN::Move>(pgn[i])) {
907 const PGN::Move *pm = boost::get<PGN::Move>(pgn[i]);
909 int n = current.totalNumMoves()+1;
910 if(var_start) {
911 if(!pm->m_number) // not all moves get numbered in PGN, usually only 1st player ones
912 current = current.prev();
913 else if(pm->m_number>n+1)
914 kError() << "Too far variation";
915 else {
916 if(pm->m_number<n)
917 kError() << "Too close variation";
918 current = current.prev(n + 1 - pm->m_number);
921 else if(pm->m_number && pm->m_number!=n+1)
922 kError() << "Move number mismatch";
924 StatePtr pos = position();
925 Move m = m_components->moveSerializer("compact")->
926 deserialize(pm->m_move, pos.get());
928 if (m == Move() || !m_components->validator()->legal(pos.get(), m))
929 break;
931 StatePtr newPos(pos->clone());
932 newPos->move(m);
934 int at;
935 History *vec = fetchRef(current, &at);
936 Q_ASSERT(vec);
938 if(var_start) {
939 Entry *e = &(*vec)[at];
940 int var_id = e->last_var_id++;
941 e->variations[var_id].push_back(Entry(m, newPos));
942 if(!vcomment.isEmpty()) {
943 e->vcomments[var_id] = vcomment;
944 vcomment = QString();
946 /* this is a hack, but the mainline should NEVER
947 be empty if there is a variation*/
948 if((int)vec->size() - 1 == at)
949 vec->push_back(Entry(m, newPos));
951 current = current.next(var_id);
953 else {
954 if((int)vec->size() - 1 == at)
955 vec->push_back(Entry(m, newPos));
956 else
957 (*vec)[at] = Entry(m, newPos);
959 current = current.next();
962 var_start = false;
964 else
965 kError() << "Unexpected type in boost::variant";
968 if(history.size()>1)
969 onAdded(Index(1));
970 Entry* e = fetch(Index(0));
971 for(Variations::const_iterator it = e->variations.begin();
972 it != e->variations.end(); ++it)
973 onAdded(Index(0).next(it->first));
974 for(VComments::const_iterator it = e->vcomments.begin();
975 it != e->vcomments.end(); ++it)
976 onSetVComment(Index(0), it->first, it->second);
978 current = Index(0);
979 onCurrentIndexChanged();